Kernel Vulnerabilities
Kernel Exploitation · Advanced Exploitation track
7 min read
In this module
Bugs with Elevated Consequences
The same classes of memory corruption bugs that plague user-space programs show up in the kernel too. Buffer overflows, use-after-free, type confusion, integer overflows — all of them. The difference is that in the kernel, everything runs with full hardware privileges, so the blast radius is far larger.
Here are the most common vulnerability classes in real-world kernel exploits.
Null Pointer Dereferences
A null pointer dereference in user space simply crashes the process — SIGSEGV, process dies. In the kernel, the situation turns interesting.
When the kernel dereferences a null pointer, it accesses virtual address 0x0. If an attacker can map memory at address zero from user space before triggering the kernel bug, the kernel will read or write attacker-controlled data at that address. If the kernel treats the data at address zero as a function pointer and calls it, the attacker achieves code execution in ring 0.
/* Vulnerable kernel code */struct operations *ops = get_ops(device);/* If get_ops() returns NULL and there is no check... */ops->execute(data); /* Calls function pointer at address 0x0 + offset */If the attacker has mapped page zero with a crafted structure containing a pointer to their own code, the kernel will jump to it. This is why modern Linux kernels set mmap_min_addr to a non-zero value (typically 65536), preventing user-space processes from mapping the lowest pages of memory.
mmap_min_addr is a kernel parameter that sets the minimum virtual address that user-space processes are allowed to map. By preventing allocations at low addresses (including address zero), it mitigates null pointer dereference exploits in the kernel. The default is typically 65536 (0x10000).
Use-After-Free in the Kernel
UAF vulnerabilities are the workhorse of modern kernel exploitation. They occur when kernel code frees an object but retains a pointer to it, then later dereferences that stale pointer.
The Linux kernel manages small objects through a slab allocator. Today that's SLUB: the older SLOB was removed in Linux 6.4 and SLAB in 6.8, though you'll still meet both in write-ups about older kernels. The allocator groups objects of similar size into caches. When an object is freed, its memory returns to the slab cache. The catch: a new allocation of the same size from the same cache will often reuse the exact same memory.
An attacker exploits a kernel UAF in three steps:
- Trigger the free: Cause the kernel to free the vulnerable object while a reference to it still exists somewhere.
- Reclaim the memory: Allocate a new object of the same size that lands in the same memory location. The attacker fills this new object with controlled data.
- Trigger the use: Cause the kernel to use the stale pointer. It now reads the attacker's controlled data instead of the original object.
Step 1: Free Step 2: Reclaim┌──────────────┐ ┌──────────────┐│ Object A │ ← freed │ Attacker Data│ ← new allocation│ (stale ptr │ │ (same size, ││ still │ │ same slab ││ exists) │ │ cache) │└──────────────┘ └──────────────┘
Step 3: Use stale pointer → reads attacker dataIf the original object contained function pointers (which is extremely common in the kernel's object-oriented C style), the attacker's controlled data will be interpreted as function pointers. When the kernel calls one, it jumps to an attacker-specified address.
Race Conditions in the Kernel
The kernel is inherently concurrent. Multiple CPUs execute kernel code simultaneously, interrupt handlers can preempt running code, and multiple processes can enter the kernel through system calls at the same time. This concurrency makes race conditions a persistent source of kernel vulnerabilities.
A particularly dangerous pattern is the double fetch, a TOCTOU (time-of-check-to-time-of-use) race on syscall arguments. The kernel reads a value from user memory, validates it, then reads the same user memory again and trusts the second copy:
/* Vulnerable: the header is fetched from user space twice */u32 len;if (get_user(len, &uptr->len)) /* fetch 1 */ return -EFAULT;if (len > MAX_LEN) /* check */ return -EINVAL;kbuf = kmalloc(sizeof(*kbuf) + len, GFP_KERNEL);/* WINDOW: another thread rewrites uptr->len here */if (copy_from_user(kbuf, uptr, sizeof(*kbuf) + len)) /* fetch 2 */ return -EFAULT;process_msg(kbuf); /* uses kbuf->len, which was never checked */User memory belongs to the attacker, and a second thread in the same process can flip uptr->len to a huge value between the two fetches. The check passed on the first copy; process_msg() walks the buffer using the second. The fix is to fetch once and only ever use the validated value (here, kbuf->len = len; after the copy). Note what isn't the bug: copy_from_user() does its own range check and copes with pages that vanish under it, so an access_ok() followed by copy_from_user() on the same pointer isn't a race by itself.
Race conditions in reference counting are also common. If two code paths simultaneously decrement an object's reference count without proper locking, they might both see the count transition from 1 to 0 and both attempt to free the object - leading to a double-free.
Dirty COW (CVE-2016-5195) is the famous real-world case: a race in the copy-on-write path that had sat in Linux for about nine years. One thread writes through /proc/self/mem into a private, read-only mapping of a file it can't modify, such as a setuid binary. The kernel's write path faults in a private copy, then retries the page lookup without its write flag, on the assumption that the copy is still there. A second thread calls madvise(MADV_DONTNEED) in that gap and throws the copy away, so the retry finds the file's own page-cache page instead.
Integer Overflows and Size Miscalculations
The kernel performs extensive arithmetic on sizes, offsets, and counts. Integer overflow bugs occur when these calculations wrap around, producing unexpectedly small values.
Consider a kernel function that allocates a buffer based on a user-provided count:
size_t total = count * element_size;/* If count * element_size overflows, total is small */buf = kmalloc(total, GFP_KERNEL);/* The loop still trusts count, so it writes far past the end of buf */for (size_t i = 0; i < count; i++) copy_from_user(buf + i * element_size, user_data + i * element_size, element_size);If count * element_size overflows a 32-bit or 64-bit integer, total will be much smaller than expected. The kernel allocates a tiny buffer, then copies a large amount of user-controlled data into it - a heap buffer overflow in kernel space.
The kernel uses helper functions like check_mul_overflow() and array_size() to detect these conditions, but legacy code may not use them consistently.
Out-of-Bounds Access
Array indexing errors in the kernel can allow an attacker to read or write memory outside the intended bounds. These often stem from insufficient validation of user-supplied indices:
/* If index is not properly bounds-checked */value = kernel_array[user_index];If user_index is larger than the array, the read accesses memory beyond the array - potentially leaking kernel pointers or other sensitive data. An out-of-bounds write can corrupt adjacent kernel objects, leading to code execution.
The Common Thread
All these classes share a pattern: the kernel trusts data it shouldn't, or fails to enforce invariants under concurrency. The next lesson covers how attackers convert these bugs into privilege escalation — going from an unprivileged user to full root.