Breaking Bits
Search…
Setup

Overview

The kernel module used for these exercises is based off hxpCTF 2020 kernel-rop . It didn't come with source, so I rewrote it and have it uploaded here. You can build the kernel yourself or use my prebuilt one here.
You will need qemu,gcc, and gdb to follow along with these problems.
The launch.sh script will rebuild the file system and launch qemu
1
#!/bin/bash
2
3
# build root fs
4
pushd fs
5
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../initramfs.cpio.gz
6
popd
7
8
# launch
9
/usr/bin/qemu-system-x86_64 \
10
-m 128M \
11
-cpu kvm64,+smep,+smap \
12
-no-reboot \
13
-kernel linux-5.4/arch/x86/boot/bzImage \
14
-initrd $PWD/initramfs.cpio.gz \
15
-fsdev local,security_model=passthrough,id=fsdev0,path=$HOME \
16
-device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=hostshare \
17
-nographic \
18
-monitor none \
19
-s \
20
-append "console=ttyS0 nokaslr nopti nosmep nosmap panic=1"
Copied!
The -s flag will start qemu with gdb debugging enabled on localhost port 1234 . Most gdb extensions like gef and pwndbg have trouble debugging kernels and you can disable them with the command: gdb -nx ./bzImage

Kernel-Overflow module

The module we will be exploiting for most of this series is named kernel-overflow and is located here [TODO github link]. The module creates a character device named kernel-overflow which is accessible at /dev/kernel-overflow . It supports read and write operations, which ultimately lead to a leak and overflow.

Leak

The leak happens when read is called on the character device with a length greater than 256 bytes. Our stack buffer tmp is only 256 bytes long and the length check below it is not sufficient to prevent an overread from happening. As long as our read is less than 0x1000 bytes, we can read past tmp and leak out the stack cookie and stack saved registers
1
static ssize_t device_read(struct file *filp, char *buf, size_t len, loff_t *offset)
2
{
3
int tmp[32] = {0};
4
tmp[0] = 0xDEADBEEF;
5
tmp[31] = 0xCAFEBABE;
6
7
memcpy(hackme_buf, tmp, len);
8
9
if ( len > 0x1000 )
10
{
11
printk("Buffer overflow detected (%d < %lu)!\n", 4096LL, len);
12
BUG();
13
}
14
15
if ( copy_to_user(buf, hackme_buf, len) ) return -14LL;
16
17
return len;
18
}
Copied!

Overflow

The overflow happens when write is called on the character device with a length greater than 256 bytes. Our stack buffer tmp is only 256 bytes long and just like the leak, the length check is not sufficient to prevent a stack overflow.
1
static ssize_t device_write(struct file *filp, const char *buf, size_t len, loff_t *off)
2
{
3
int tmp[32] = {0};
4
tmp[0] = 0xDEADBEEF;
5
tmp[31] = 0xCAFEBABE;
6
7
if ( len > 0x1000 )
8
{
9
printk("Buffer overflow detected (%d < %lu)!\n", 4096LL, len);
10
BUG();
11
}
12
check_object_size(hackme_buf, len, 0LL);
13
14
if ( copy_from_user(hackme_buf, buf, len) ) return -14LL;
15
16
memcpy(tmp, hackme_buf, len);
17
18
// my gcc is optimizing out the memcpy
19
// having tmp used after the copy ensures
20
// that it stays in
21
printk(KERN_ALERT "After %s",tmp);
22
23
return len;
24
}
Copied!