Skip to main content
Be Bitwise

Kernel vs Userspace

Kernel Exploitation · Advanced Exploitation track

7 min read

In this module

The Great Divide

Every modern operating system splits execution into two distinct worlds: kernel space and user space. This boundary isn't just a software convention — the CPU hardware enforces it. Understanding this divide is the first step towards understanding kernel exploitation.

When you run a regular program — a web browser, a text editor, a game — that code executes in user space. It can access its own memory, perform computations, call functions within its own address space. But it can't directly touch hardware, read other processes' memory, or tamper with the OS's internal data structures.

The kernel runs with full privileges. It manages all hardware, schedules processes, handles memory allocation, and mediates every interaction between software and the physical machine. Effectively, it's the arbiter of everything.

Privilege Rings

On x86 processors, this separation is implemented through privilege rings. The CPU defines four rings, numbered 0 through 3, but modern operating systems use only two:

  • Ring 0 (kernel mode): The most privileged level. Code running in ring 0 can execute any CPU instruction, access any memory address, and interact directly with hardware.
  • Ring 3 (user mode): The least privileged level. Code running in ring 3 is restricted - it cannot execute privileged instructions, access kernel memory, or perform direct I/O.

When a user-space program needs to interact with hardware or the OS, it makes a system call. The system call mechanism is a controlled gateway: the CPU switches from ring 3 to ring 0, executes the requested kernel function, and then returns to ring 3. This transition is the only legitimate way for user code to enter the kernel.

Here's that round trip for a write(1, buf, 5) on x86-64 Linux with KPTI enabled. Track two things down the diagram: the privilege level (CPL) and which page tables CR3 points at. Neither changes in the same instruction, so there's a brief window at each end where the kernel is in ring 0 on the user's page tables.

Privilege Ring

A privilege ring is a hardware-enforced execution level in the CPU. Ring 0 has unrestricted access to all instructions and memory. Ring 3 is restricted to prevent user-space code from interfering with the kernel. The CPU checks the current privilege level (CPL) on every instruction and memory access.

Kernel Memory

The kernel has its own region of virtual memory that is mapped into every process's address space. On a typical 64-bit Linux system, the upper portion of the virtual address space belongs to the kernel, and the lower portion belongs to the user-space process.

With 4-level paging, x86-64 uses 48-bit virtual addresses, and bits 63 to 48 must all copy bit 47. That splits the space into a user half at the bottom, a kernel half at the top, and an unusable gap in between.

Even though kernel memory is mapped into the process's virtual address space, user-space code cannot read or write it. The page table entries for kernel pages have a supervisor bit set, and the CPU enforces that only ring 0 code can access supervisor pages. Any attempt from ring 3 results in a page fault and the process is killed.

You can watch both halves of that from user space. The runner prints the top of its own /proc/self/maps, where every line is a user address, then reads from a kernel address with a SIGSEGV handler standing by. Predict whether the read returns anything before you run it.

Try itPredict the outcome of the read, then try 0x0000800000000000, the first non-canonical address: it faults too, but as a general-protection fault, so the reported address comes back as 0.
C (GCC 9.2.0)

The stack sits just under 0x00007fffffffffff, the top of user space. You may also see a [vsyscall] line up at 0xffffffffff600000: a legacy page the kernel maps into every process for old binaries, the one user-visible mapping in the kernel half.

This design exists for performance: keeping the kernel mapped means the CPU doesn't need to flush the TLB on every syscall. The cost is that if an attacker finds a vulnerability letting them read or write kernel memory from user space, the target data is already sitting in the same address space. Meltdown (2018) showed that the supervisor bit alone wasn't enough, and Linux now uses KPTI to unmap most of the kernel while user code runs, giving back some of that performance; lesson 04 covers it.

Why Kernel Bugs Are Different

When a bug exists in a user-space program, the damage is bounded. A buffer overflow in your text editor might let an attacker run code as your user account, but it can't directly access other users' files, modify the kernel, or take over the machine.

A bug in the kernel is a different beast entirely:

  • Full privilege: Code executing in kernel context has ring 0 privileges. An attacker who gains code execution in the kernel can do anything: read any file, modify any process, install rootkits, and hide their presence from the operating system itself.
  • No containment: There is no higher authority to catch the kernel misbehaving. The kernel IS the authority. If the kernel's own memory is corrupted, there is no safety net.
  • Affects all processes: Since the kernel is shared by all processes, a kernel vulnerability affects every user and every program on the system.

The Attack Surface

The kernel's attack surface - the set of ways an attacker can reach kernel code - includes:

  • System calls: Every syscall is a potential entry point. The kernel must validate all arguments from user space, and any failure to do so can lead to vulnerabilities.
  • Device drivers: Drivers run in kernel space and interact with complex hardware. They are often written by third-party vendors with less rigorous code review. Historically, drivers have been a rich source of kernel vulnerabilities.
  • File systems: Mounting a crafted file system image (from a USB drive, for example) triggers complex parsing code in the kernel.
  • Network stack: Incoming network packets are processed by kernel code. A malformed packet can trigger bugs in protocol handlers.
  • ioctl interfaces: Many drivers expose custom interfaces through ioctl system calls, which often have complex input parsing and insufficient validation.
  • Newer subsystems: eBPF (programs checked by an in-kernel verifier), io_uring (a second, asynchronous path into many syscalls) and unprivileged user namespaces (which open up code once reachable only by root) have each produced a steady run of privilege-escalation bugs.

Each of these entry points can be reached by unprivileged user-space code. An attacker who finds a vulnerability in any of them can potentially escalate from an unprivileged user to full root access - or worse, to kernel-level code execution that even root cannot detect.

The next lesson looks at the specific vulnerability classes that appear in kernel code.