milabs / khook

Linux Kernel hooking engine (x86)
GNU General Public License v2.0
327 stars 50 forks source link

doesn't compile #16

Closed k1gen closed 9 months ago

k1gen commented 9 months ago

I am trying to integrate khook into https://github.com/k1gen/diamorphine, and I can't compile the module with khook:

$ make
make -C /lib/modules/6.6.9-1-clear/build M=/home/olk/temp/Diamorphine modules
make[1]: Entering directory '/usr/lib/modules/6.6.9-1-clear/build'
  CC [M]  /home/olk/temp/Diamorphine/../../../../../home/olk/temp/Diamorphine/khook/engine.o
In file included from ./arch/x86/include/asm/linkage.h:6,
                 from ./include/linux/linkage.h:8,
                 from ./include/linux/printk.h:8,
                 from ./include/asm-generic/bug.h:22,
                 from ./arch/x86/include/asm/bug.h:87,
                 from ./include/linux/bug.h:5,
                 from ./include/linux/mmdebug.h:5,
                 from ./include/linux/mm.h:6,
                 from /home/olk/temp/Diamorphine/./khook/internal.h:3,
                 from /home/olk/temp/Diamorphine/./khook/engine.c:1:
./arch/x86/include/asm/ibt.h:77:1: warning: ‘nocf_check’ attribute ignored. Use ‘-fcf-protection’ option to enable it [-Wattributes]
   77 | extern __noendbr u64 ibt_save(bool disable);
      | ^~~~~~
./arch/x86/include/asm/ibt.h:78:1: warning: ‘nocf_check’ attribute ignored. Use ‘-fcf-protection’ option to enable it [-Wattributes]
   78 | extern __noendbr void ibt_restore(u64 save);
      | ^~~~~~
  CC [M]  /home/olk/temp/Diamorphine/../../../../../home/olk/temp/Diamorphine/khook/x86/hook.o
In file included from ./arch/x86/include/asm/linkage.h:6,
                 from ./include/linux/linkage.h:8,
                 from ./include/linux/printk.h:8,
                 from ./include/asm-generic/bug.h:22,
                 from ./arch/x86/include/asm/bug.h:87,
                 from ./include/linux/bug.h:5,
                 from ./include/linux/mmdebug.h:5,
                 from ./include/linux/mm.h:6,
                 from /home/olk/temp/Diamorphine/../../../../../home/olk/temp/Diamorphine/khook/x86/../internal.h:3,
                 from /home/olk/temp/Diamorphine/../../../../../home/olk/temp/Diamorphine/khook/x86/hook.c:1:
./arch/x86/include/asm/ibt.h:77:1: warning: ‘nocf_check’ attribute ignored. Use ‘-fcf-protection’ option to enable it [-Wattributes]
   77 | extern __noendbr u64 ibt_save(bool disable);
      | ^~~~~~
./arch/x86/include/asm/ibt.h:78:1: warning: ‘nocf_check’ attribute ignored. Use ‘-fcf-protection’ option to enable it [-Wattributes]
   78 | extern __noendbr void ibt_restore(u64 save);
      | ^~~~~~
  AS [M]  /home/olk/temp/Diamorphine/../../../../../home/olk/temp/Diamorphine/khook/x86/stub.o
  LD [M]  /home/olk/temp/Diamorphine/diamorphine.o
/home/olk/temp/Diamorphine/diamorphine.o: warning: objtool: .text+0xace: indirect call found in RETPOLINE build
/home/olk/temp/Diamorphine/diamorphine.o: warning: objtool: .text+0xadb: 'naked' return found in RETHUNK build
/home/olk/temp/Diamorphine/diamorphine.o: warning: objtool: .text+0xadc: 'naked' return found in RETHUNK build
/home/olk/temp/Diamorphine/diamorphine.o: warning: objtool: .text+0xb3d: indirect call found in RETPOLINE build
/home/olk/temp/Diamorphine/diamorphine.o: warning: objtool: .text+0xb43: 'naked' return found in RETHUNK build
/home/olk/temp/Diamorphine/diamorphine.o: warning: objtool: .text+0xb44: 'naked' return found in RETHUNK build
/home/olk/temp/Diamorphine/diamorphine.o: warning: objtool: khook_lookup_name+0xb0: relocation to !ENDBR: callback.0+0x0
/home/olk/temp/Diamorphine/diamorphine.o: warning: objtool: khook_init+0x1: relocation to !ENDBR: .text+0xadc
/home/olk/temp/Diamorphine/diamorphine.o: warning: objtool: khook_cleanup+0x5: relocation to !ENDBR: khook_sm_cleanup_hooks+0x0
/home/olk/temp/Diamorphine/diamorphine.o: warning: objtool: khook_arch_create_stub+0x16: relocation to !ENDBR: .text+0xadc
/home/olk/temp/Diamorphine/diamorphine.o: warning: objtool: .text+0xadb: missing int3 after ret
/home/olk/temp/Diamorphine/diamorphine.o: warning: objtool: .text+0xadc: missing int3 after ret
/home/olk/temp/Diamorphine/diamorphine.o: warning: objtool: .text+0xb43: missing int3 after ret
/home/olk/temp/Diamorphine/diamorphine.o: warning: objtool: .text+0xb44: missing int3 after ret
  MODPOST /home/olk/temp/Diamorphine/Module.symvers
ERROR: modpost: missing MODULE_LICENSE() in /home/olk/temp/Diamorphine/diamorphine.o
make[3]: *** [scripts/Makefile.modpost:145: /home/olk/temp/Diamorphine/Module.symvers] Error 1
make[2]: *** [/usr/lib/modules/6.6.9-1-clear/build/Makefile:1865: modpost] Error 2
make[1]: *** [Makefile:234: __sub-make] Error 2
make[1]: Leaving directory '/usr/lib/modules/6.6.9-1-clear/build'
make: *** [Makefile:11: all] Error 2
milabs commented 9 months ago

Hey,

...
ERROR: modpost: missing MODULE_LICENSE() in /home/olk/temp/Diamorphine/diamorphine.o
...

Try to add MODULE_LICENSE to your module.

vagrant@vagrant:~/diamorphine$ uname -r
5.4.0-120-generic
vagrant@vagrant:~/diamorphine$ make
make -C /lib/modules/5.4.0-120-generic/build M=/home/vagrant/diamorphine modules
make[1]: Entering directory '/usr/src/linux-headers-5.4.0-120-generic'
  CC [M]  /home/vagrant/diamorphine/../../../home/vagrant/diamorphine/khook/engine.o
  CC [M]  /home/vagrant/diamorphine/../../../home/vagrant/diamorphine/khook/x86/hook.o
  AS [M]  /home/vagrant/diamorphine/../../../home/vagrant/diamorphine/khook/x86/stub.o
  LD [M]  /home/vagrant/diamorphine/diamorphine.o
  Building modules, stage 2.
  MODPOST 1 modules
WARNING: modpost: missing MODULE_LICENSE() in /home/vagrant/diamorphine/diamorphine.o
see include/linux/module.h for more information
  CC [M]  /home/vagrant/diamorphine/diamorphine.mod.o
  LD [M]  /home/vagrant/diamorphine/diamorphine.ko
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-120-generic'

Works for 5.4.0-120-generic kernel (ubuntu).

k1gen commented 9 months ago

@milabs: hey, thanks for the quick reply. this was the first thing I checked - do you mean this: https://github.com/k1gen/diamorphine/blob/main/diamorphine.c#L295 ? this also doesn't work for 6.1 on debian 12: Screenshot_debian_2024-01-03_20:51:44

milabs commented 9 months ago

Ok, it seems the problem is that you name your module diamorphine. It's the issue of Kbuild system, check that change:

image

Also, check how khook_demo uses khook. Ideally, you'll need just to add a khook as submodule and include it's Makefile.khook.

k1gen commented 9 months ago

thanks, it works now! can't believe I didn't try that. the whole reason behind integrating khook though - skipping cpu ticks for a hidden process (https://github.com/k1gen/diamorphine/blob/main/diamorphine.c#L222) doesn't work for some reason :( could you please have a look at that if you have time? I got that piece of code from here: https://github.com/alfonmga/hiding-cryptominers-linux-rootkit/blob/master/main.c#L331