Privilege Escalation Techniques
Kernel Exploitation · Advanced Exploitation track
8 min read
In this module
From Bug to Root
Finding a kernel vulnerability is only the start. Converting that bug into a privilege escalation — lifting from an unprivileged user to root, or more precisely to kernel-level control — is the harder problem.
Kernel exploitation is more constrained than user-space work. The attacker must avoid crashing the kernel (which crashes the entire system), must work within the kernel's memory layout, and must eventually return cleanly to user space with elevated privileges. Sloppy exploitation doesn't get you root; it gets you a kernel panic and a rebooted machine.
The Goal: Overwriting Credentials
On Linux, every process has an associated struct cred that holds its security credentials:
struct cred { kuid_t uid; /* real UID of the task */ kgid_t gid; /* real GID of the task */ kuid_t euid; /* effective UID */ kgid_t egid; /* effective GID */ /* ... capabilities, keyrings, etc. */};If an attacker can overwrite their process's uid and euid fields to 0, their process becomes root. The most direct kernel exploit technique targets these credential structures.
The kernel provides a convenient function for this exact purpose (intended for legitimate use by the kernel itself):
commit_creds(prepare_kernel_cred(NULL));On older kernels, prepare_kernel_cred(NULL) creates a new cred struct with full root privileges (UID 0, all capabilities). commit_creds() installs it as the current process's credentials. If an attacker can redirect kernel execution to call these two functions, they have root. That one-liner is all over exploit write-ups, so it's worth knowing it stopped working.
On modern kernels:
- No more NULL shortcut: since Linux 6.2,
prepare_kernel_cred(NULL)warns and returns NULL instead of handing back init's credentials (Kees Cook called out exactly this exploit pattern in the commit). Chains now callcommit_creds(&init_cred)directly, orprepare_kernel_cred(&init_task). - Data-only attacks: many exploits skip code execution entirely. With an arbitrary write they overwrite their own task's
credpointer or its UID fields, or rewritemodprobe_path(orcore_pattern) to point at a script of theirs and then make the kernel run it as root via the usermode helper. No ROP, so SMEP, SMAP and CFI never come into play. - KPTI trampolines: with KPTI on, a hand-rolled
swapgs; iretqreturns to user mode still on the kernel page tables, where user pages are non-executable, and the process segfaults the moment it lands. Exploits instead finish by jumping into the kernel's own exit path (swapgs_restore_regs_and_return_to_usermode), which switches CR3 back first. - CR4 pinning: the SMEP/SMAP-clearing trick below runs into pinned CR4 bits.
These are internal kernel functions used to manage process credentials. prepare_kernel_cred() builds a new credential struct from a given task's credentials; before Linux 6.2, passing NULL gave you root-level ones (UID 0, full capabilities). commit_creds() installs the new credentials on the current process. Kernel exploits commonly chain these functions to escalate privileges.
ret2usr: Return to User Space
The simplest kernel exploitation technique is ret2usr (return to user). The kernel can access all virtual memory, including user-space pages. If the attacker can overwrite a kernel function pointer or return address, they aim it at code in their own user-space memory.
That user-space code then runs at ring 0 — because the CPU hasn't switched back to ring 3 yet. The attacker calls commit_creds(prepare_kernel_cred(NULL)) and returns cleanly to user space. Elegant, really.
1. Trigger kernel vulnerability2. Overwrite function pointer → points to user-space code3. Kernel calls function pointer4. CPU executes user-space code IN RING 05. Attacker code: commit_creds(prepare_kernel_cred(0))6. Return to user space → process is now rootThis technique worked reliably for years because nothing prevented the kernel from executing user-space pages. The CPU was in ring 0, and ring 0 has access to all memory - including user-space mappings.
SMEP and SMAP: Closing the Door
Processor vendors introduced two hardware features to block ret2usr:
SMEP (Supervisor Mode Execution Prevention): When enabled, the CPU faults if ring 0 code attempts to execute instructions from a user-space page (a page whose U/S bit is set in the page tables). This directly blocks ret2usr by preventing the kernel from jumping to attacker-controlled code in user memory.
SMAP (Supervisor Mode Access Prevention): When enabled, the CPU faults if ring 0 code attempts to read or write user-space pages, except through designated functions (like copy_from_user). This prevents the kernel from accidentally using attacker-controlled data in user-space memory.
Both are controlled by bits in the CR4 control register. SMEP is bit 20, SMAP is bit 21. The kernel sets these bits during boot, and they apply to all ring 0 execution.
CR4 register: Bit 20 (SMEP): 1 = fault on user-page execution in ring 0 Bit 21 (SMAP): 1 = fault on user-page access in ring 0Bypassing SMEP
With SMEP enabled, the attacker can no longer simply jump to user-space code. They must find a way to execute their desired operations using only kernel-space code. The primary technique is kernel ROP - the same return-oriented programming approach from Return-Oriented Programming, but using gadgets found in the kernel image.
First, the failure that makes all this necessary. Step the hijacked return below with SMEP on:
ret2usr with SMEP on
The kernel return address has been overwritten. Press Step to let it return.
commit_creds(prepare_kernel_cred(0)) return to user space
The attacker constructs a ROP chain on the kernel stack (or uses a stack pivot to redirect the stack pointer to controlled data) and chains kernel gadgets to call prepare_kernel_cred(0) and commit_creds().
An alternative approach is to disable SMEP by modifying the CR4 register. If the attacker can find a gadget that writes to CR4, they can clear the SMEP bit and then execute user-space code as before:
Gadget 1: pop rdi; ret → load desired CR4 value (SMEP bit cleared)Gadget 2: mov cr4, rdi; ret → write to CR4, disabling SMEPGadget 3: ret → return to user-space code (now allowed)Here's that chain with the slots filled in. The kernel addresses are illustrative; with KASLR they change every boot.
Clearing SMEP through CR4
The kernel return address has been overwritten. Press Step to let it return.
pop rdi; ret
mov cr4, rdi; ret
commit_creds(prepare_kernel_cred(0))
Modern Linux shuts most of this down. Since 5.3, native_write_cr4() pins the SMEP and SMAP bits: straight after its mov cr4 it checks whether a pinned bit went missing, sets it again and warns. A ROP chain that jumps into that function gets nothing; the attacker would need a stray mov cr4 gadget somewhere else in the image, outside the check.
Kernel Stack Pivoting
Many kernel vulnerabilities give the attacker limited control - perhaps they can overwrite a single function pointer but not the entire stack. In this case, the attacker needs a stack pivot: a gadget that moves the stack pointer (RSP) to a location where the attacker has placed a ROP chain.
Common stack pivot gadgets look like:
xchg rax, rsp; ret ; Swap RAX and RSP, then return from new stackmov rsp, [rdi]; ret ; Load RSP from memory pointed to by RDIleave; ret ; Set RSP = RBP, pop RBP, then returnThe attacker places their ROP chain in a kernel heap allocation they control, then pivots the stack pointer to it.
Returning to User Space Safely
After modifying credentials, the attacker must return to user space without crashing the kernel. This requires restoring the CPU state that the kernel expects: proper register values, the user-space stack pointer, and critically, the switch from ring 0 back to ring 3.
The standard approach uses the iretq instruction (on x86-64), which pops five values from the stack and restores the CPU state:
iretq pops: RIP → user-space instruction to resume CS → user-space code segment (ring 3) RFLAGS → processor flags RSP → user-space stack pointer SS → user-space stack segmentPut the credential calls and the iretq frame together and the whole chain, run from kernel memory with SMEP left on, looks like this. Addresses are illustrative.
commit_creds, then iretq (no KPTI)
The kernel return address has been overwritten. Press Step to let it return.
pop rdi; ret
kernel function
mov rdi, rax; ret
kernel function
swapgs; ret
iretq
Alternatively, the swapgs; sysretq sequence can be used, which is the normal return path from system calls. The attacker's ROP chain ends with one of these sequences, configured to return to a user-space function that verifies the exploit succeeded (by checking if the process now has root privileges).
Putting It Together
A complete kernel exploit typically follows this pattern:
- Trigger the vulnerability from user space (via syscall, ioctl, etc.)
- Gain control of a kernel code pointer or return address
- Pivot the stack (if needed) to controlled data
- Execute a ROP chain to call
commit_creds(prepare_kernel_cred(0))(orcommit_creds(&init_cred)on 6.2 and later), or skip code execution and overwrite credentials as data - Return to user space cleanly via
iretqorswapgs; sysretq, through the KPTI trampoline if KPTI is on - Verify success by checking UID (if getuid() returns 0, you are root)
Each step requires careful setup and a working knowledge of the kernel's internal state. The next lesson examines the mitigations that make each step harder to pull off.