Closed geekjy closed 4 months ago
All linux 6.8 kernel,test fedora40 and Ubuntu 24.04 LTS,test intel cpu ok.Amd cpu all crash,other kernel ok
[ 242.351280] [ T5535] khook_demo: module verification failed: signature and/or required key missing - tainting kernel
[ 242.364868] [ T18] general protection fault, maybe for address 0x80040033: 0000 [#1] PREEMPT SMP NOPTI
[ 242.364888] [ T18] CPU: 0 PID: 18 Comm: migration/0 Kdump: loaded Tainted: G OE 6.8.0-31-generic #31-Ubuntu
[ 242.364900] [ T18] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 11/12/2020
[ 242.364911] [ T18] Stopper: multi_cpu_stop+0x0/0x120 <- stop_machine_cpuslocked+0x13b/0x170
[ 242.364924] [ T18] RIP: 0010:khook_arch_sm_init_one+0x56/0x170 [khook_demo]
[ 242.364934] [ T18] Code: 76 06 eb 1c 48 8b 7b 10 48 01 c7 e8 e4 fe ff ff 48 98 48 03 43 30 48 89 43 30 48 83 f8 04 76 e4 fa 0f 20 c0 48 25 ff ff fe ff <0f> 22 c0 48 8b 7b 20 f6 43 28 01 0f 84 e9 00 00 00 48 c7 c2 39 1e
[ 242.364950] [ T18] RSP: 0018:ffffb7aa400bfe08 EFLAGS: 00010006
[ 242.364958] [ T18] RAX: 0000000080040033 RBX: ffffffffc0a830e0 RCX: 0000000000000000
[ 242.364967] [ T18] RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000000
[ 242.364975] [ T18] RBP: ffffb7aa400bfe10 R08: 0000000000000000 R09: 0000000000000000
[ 242.364983] [ T18] R10: 0000000000000000 R11: 0000000000000000 R12: ffffb7aa4077bafc
[ 242.364992] [ T18] R13: 0000000000000002 R14: ffffffffa8e48660 R15: 0000000000000003
[ 242.365000] [ T18] FS: 0000000000000000(0000) GS:ffff89c437e00000(0000) knlGS:0000000000000000
[ 242.365010] [ T18] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 242.365017] [ T18] CR2: 000055654f694688 CR3: 0000000105234000 CR4: 0000000000f50ef0
[ 242.365033] [ T18] PKRU: 55555554
[ 242.365039] [ T18] Call Trace:
[ 242.365045] [ T18]
I guess it's Virtual Machine issue ...
I use AMD 7950X Physical machine crash
Ok, that is the code which failed:
[ 242.364934] [ T18] Code: 76 06 eb 1c 48 8b 7b 10 48 01 c7 e8 e4 fe ff ff 48 98 48 03 43 30 48 89 43 30 48 83 f8 04 76 e4 fa 0f 20 c0 48 25 ff ff fe ff <0f> 22 c0 48 8b 7b 20 f6 43 28 01 0f 84 e9 00 00 00 48 c7 c2 39 1e
The following instruction failed:
0f 22 c0 mov %rax,%cr0
I will try to understand why it failed that way for AMD but not on Intel cpu...
Ok, that is the code which failed:
[ 242.364934] [ T18] Code: 76 06 eb 1c 48 8b 7b 10 48 01 c7 e8 e4 fe ff ff 48 98 48 03 43 30 48 89 43 30 48 83 f8 04 76 e4 fa 0f 20 c0 48 25 ff ff fe ff <0f> 22 c0 48 8b 7b 20 f6 43 28 01 0f 84 e9 00 00 00 48 c7 c2 39 1e
The following instruction failed:
0f 22 c0 mov %rax,%cr0
I will try to understand why it failed that way for AMD but not on Intel cpu...
ok
unsigned long cr0; static inline void write_cr0_forced(unsigned long val) { unsigned long __force_order;
asm volatile(
"mov %0, %%cr0"
: "+r"(val), "+m"(__force_order));
}
static int __init rootkit_init(void) { cr0 = read_cr0(); printk(KERN_INFO "cr0 is %lx\n",cr0); write_cr0_forced(cr0 & ~0x00010000); write_cr0_forced(cr0); return 0; } I tried to write a very simple CR0 operation, and it crashed. However, other virtual machines on the same computer with kernels less than 6.8 will not crash.
[ 367.805404] [ T2002] cr0 is 80050033
[ 367.805512] [ T2002] RIP: 0010:rootkit_init+0x82/0xff0 [main]
[ 367.805521] [ T2002] Code: b4 c0 48 89 c6 48 89 05 4c c5 ff ff e8 07 a0 c6 e6 48 8b 05 40 c5 ff ff 48 c7 45 e8 00 00 00 00 48 89 c2 48 81 e2 ff ff fe ff <0f> 22 c2 48 c7 45 e8 00 00 00 00 0f 22 c0 48 c7 c7 83 20 b4 c0 e8
[ 367.805537] [ T2002] RSP: 0018:ffffaaf6c092fac8 EFLAGS: 00010206
[ 367.805545] [ T2002] RAX: 0000000080050033 RBX: ffffffffa7828b50 RCX: 0000000000000000
[ 367.805553] [ T2002] RDX: 0000000080040033 RSI: 0000000000000000 RDI: 0000000000000000
[ 367.805561] [ T2002] RBP: ffffaaf6c092fae0 R08: 0000000000000000 R09: 0000000000000000
[ 367.805569] [ T2002] R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000000
[ 367.805577] [ T2002] R13: ffff978bc0d8f260 R14: ffffaaf6c092faf0 R15: 0000000000000000
[ 367.805585] [ T2002] FS: 000071f8a73b8080(0000) GS:ffff978bf7e00000(0000) knlGS:0000000000000000
[ 367.805594] [ T2002] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 367.805602] [ T2002] CR2: 0000629172bda688 CR3: 000000010534e000 CR4: 0000000000f50ef0
[ 367.805617] [ T2002] PKRU: 55555554
[ 367.805622] [ T2002] Call Trace:
[ 367.805628] [ T2002]
@geekjy CET might be the reason, will you be able to make a change and check according to the following https://lore.kernel.org/all/20211126123446.32324-59-andrew.cooper3@citrix.com/ ?
@geekjy CET might be the reason, will you be able to make a change and check according to the following https://lore.kernel.org/all/20211126123446.32324-59-andrew.cooper3@citrix.com/ ?
Very good, I tested it and it loaded successfully
Would you mind to share the fix?
Would you mind to share the fix?
"cli\n\t" \
"mov %%cr4, %%rbx\n\t" \
"and $~(1 << 23), %%rbx\n\t" \
"mov %%rbx, %%cr4\n\t" \
"mov %%cr0, %%rax\n\t" \
"and $0xfffffffffffeffff, %%rax\n\t" \
"mov %%rax, %%cr0\n\t" \
::: "%rax", "%rbx" )
"mov %%cr0, %%rax\n\t" \
"or $0x0000000000010000, %%rax\n\t" \
"mov %%rax, %%cr0\n\t" \
"mov %%cr4, %%rbx\n\t" \
"or $1 << 23, %%rbx\n\t" \
"mov %%rbx, %%cr4\n\t" \
"sti\n\t" \
::: "%rax", "%rbx" )
@geekjy please, check the fix proposed
@geekjy please, check the fix proposed
make -C /lib/modules/6.8.0-31-generic/build M=$PWD modules
make[1]: Entering directory '/usr/src/linux-headers-6.8.0-31-generic'
warning: the compiler differs from the one used to build the kernel
The kernel was built by: x86_64-linux-gnu-gcc-13 (Ubuntu 13.2.0-23ubuntu4) 13.2.0
You are using: gcc-13 (Ubuntu 13.2.0-23ubuntu4) 13.2.0
CC [M] /root/khook/khook_demo/main.o
CC [M] /root/khook/khook_demo/../khook/engine.o
CC [M] /root/khook/khook_demo/../khook/x86/hook.o
/root/khook/khook_demo/../khook/x86/hook.c: In function ‘khook_arch_write_kernel’:
/root/khook/khook_demo/../khook/x86/hook.c:148:15: error: implicit declaration of function ‘read_cr4’; did you mean ‘read_cr2’? [-Werror=implicit-function-declaration]
148 | cr4 = read_cr4();
| ^~~~
| read_cr2
/root/khook/khook_demo/../khook/x86/hook.c:151:17: error: implicit declaration of function ‘write_cr4’; did you mean ‘write_cr3’? [-Werror=implicit-function-declaration]
151 | write_cr4(cr4 & ~X86_CR4_CET);
| ^~~~~
| write_cr3
cc1: some warnings being treated as errors
make[3]: [scripts/Makefile.build:243: /root/khook/khook_demo/../khook/x86/hook.o] Error 1
make[2]: [/usr/src/linux-headers-6.8.0-31-generic/Makefile:1926: /root/khook/khook_demo] Error 2
make[1]: [Makefile:240: __sub-make] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-6.8.0-31-generic'
make: [Makefile:4: all] Error 2
@geekjy please, check the fix proposed
I test code ok.
"cli\n\t" \
"mov %%cr4, %%ebx\n\t" \
"test $0x800000, %%ebx\n\t" \
"jz 1f\n\t" \
"and $~0x800000, %%ebx\n\t" \
"mov %%ebx, %%cr4\n\t" \
"1:\n\t" \
"mov %%cr0, %%eax\n\t" \
"and $0xfffeffff, %%eax\n\t" \
"mov %%eax, %%cr0\n\t" \
::: "%eax", "%ebx")
"mov %%cr0, %%eax\n\t" \
"or $0x00010000, %%eax\n\t" \
"mov %%eax, %%cr0\n\t" \
"mov %%cr4, %%ebx\n\t" \
"test $0x800000, %%ebx\n\t" \
"jz 1f\n\t" \
"or $0x800000, %%ebx\n\t" \
"mov %%ebx, %%cr4\n\t" \
"1:\n\t" \
"sti\n\t" \
::: "%eax", "%ebx")
"cli\n\t" \
"mov %%cr4, %%rbx\n\t" \
"test $0x800000, %%rbx\n\t" \
"jz 1f\n\t" \
"and $~0x800000, %%rbx\n\t" \
"mov %%rbx, %%cr4\n\t" \
"1:\n\t" \
"mov %%cr0, %%rax\n\t" \
"and $0xfffffffffffeffff, %%rax\n\t" \
"mov %%rax, %%cr0\n\t" \
::: "%rax", "%rbx")
"mov %%cr0, %%rax\n\t" \
"or $0x0000000000010000, %%rax\n\t" \
"mov %%rax, %%cr0\n\t" \
"mov %%cr4, %%rbx\n\t" \
"test $0x800000, %%rbx\n\t" \
"jz 1f\n\t" \
"or $0x800000, %%rbx\n\t" \
"mov %%rbx, %%cr4\n\t" \
"1:\n\t" \
"sti\n\t" \
::: "%rax", "%rbx")
@geekjy thanks for sharing your code, though I've used slightly different approach to simplify maintenance, please check the update one more time
@geekjy thanks for sharing your code, though I've used slightly different approach to simplify maintenance, please check the update one more time
Test khook-cet-fix ok,thanks!
@geekjy merged to master
I test Intel cpu insmod ok,but amd cpu crash.