Skip to main content
Be Bitwise

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:

c
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):

c
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 call commit_creds(&init_cred) directly, or prepare_kernel_cred(&init_task).
  • Data-only attacks: many exploits skip code execution entirely. With an arbitrary write they overwrite their own task's cred pointer or its UID fields, or rewrite modprobe_path (or core_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; iretq returns 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.
commit_creds / prepare_kernel_cred

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.

text
1. Trigger kernel vulnerability
2. Overwrite function pointer → points to user-space code
3. Kernel calls function pointer
4. CPU executes user-space code IN RING 0
5. Attacker code: commit_creds(prepare_kernel_cred(0))
6. Return to user space → process is now root

This 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.

text
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 0

Bypassing 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.

Stack (SP →)
1.0x401136→payload() in user memory
Code (Gadgets)
0x401136payload() in user memory
commit_creds(prepare_kernel_cred(0))
return to user space
The kernel return address now points into the attacker's own binary.

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:

text
Gadget 1: pop rdi; ret → load desired CR4 value (SMEP bit cleared)
Gadget 2: mov cr4, rdi; ret → write to CR4, disabling SMEP
Gadget 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.

Stack (SP →)
1.0xFFFFFFFF8108E3A0→pop rdi; ret
2.0x6F0→new CR4, bits 20 and 21 clear
3.0xFFFFFFFF81004D70→mov cr4, rdi; ret
4.0x401136→payload() in user memory
Code (Gadgets)
0xFFFFFFFF8108E3A0pop rdi; ret
pop rdi; ret
0xFFFFFFFF81004D70mov cr4, rdi; ret
mov cr4, rdi; ret
0x401136payload() in user memory
commit_creds(prepare_kernel_cred(0))
Works only where a bare mov cr4 gadget exists outside native_write_cr4's pinning check.

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:

asm
xchg rax, rsp; ret ; Swap RAX and RSP, then return from new stack
mov rsp, [rdi]; ret ; Load RSP from memory pointed to by RDI
leave; ret ; Set RSP = RBP, pop RBP, then return

The 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:

text
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 segment

Put 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.

Stack (SP →)
1.0xFFFFFFFF8108E3A0→pop rdi; ret
2.0x00→NULL argument
3.0xFFFFFFFF810C9540→prepare_kernel_cred
4.0xFFFFFFFF8121F8D2→mov rdi, rax; ret
5.0xFFFFFFFF810C92E0→commit_creds
6.0xFFFFFFFF81C00F58→swapgs; ret
7.0xFFFFFFFF8102284B→iretq
8.0x401136→user RIP: get_shell()
9.0x33→user CS
10.0x246→user RFLAGS
11.0x7FFE3C41F000→user RSP
12.0x2B→user SS
Code (Gadgets)
0xFFFFFFFF8108E3A0pop rdi; ret
pop rdi; ret
0xFFFFFFFF810C9540prepare_kernel_cred
kernel function
0xFFFFFFFF8121F8D2mov rdi, rax; ret
mov rdi, rax; ret
0xFFFFFFFF810C92E0commit_creds
kernel function
0xFFFFFFFF81C00F58swapgs; ret
swapgs; ret
0xFFFFFFFF8102284Biretq
iretq
With KPTI on, the chain returns through swapgs_restore_regs_and_return_to_usermode instead, which also switches back to the user page tables.

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:

  1. Trigger the vulnerability from user space (via syscall, ioctl, etc.)
  2. Gain control of a kernel code pointer or return address
  3. Pivot the stack (if needed) to controlled data
  4. Execute a ROP chain to call commit_creds(prepare_kernel_cred(0)) (or commit_creds(&init_cred) on 6.2 and later), or skip code execution and overwrite credentials as data
  5. Return to user space cleanly via iretq or swapgs; sysretq, through the KPTI trampoline if KPTI is on
  6. 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.