1if (len > 8) return -EINVAL;2copy_from_user(name, buf, len);name[8]uid1000KERNEL ONLYKERNEL ONLY1set_name("bob", 4);2set_name(evil, 12);3set_name(kptr, 8);CPUSMEPonSMAPonbobAAAAAAAA00 00 00 000
Back: the kernel's side (the set_name handler, kernel memory name and uid)Front: the user's side (the attacker's calls, the CPU and its guards)Tokens: calls through the gate, the 12-byte overflow, and stops where a defence holdsThe kernel's side at the back: the set_name handler and kernel memory, name[8] with uid right after it. A wall marked kernel only, with one gate. The user's side in front: the attacker's three calls and the CPU with its SMEP and SMAP guards. Line 2's 12 bytes cover name and spill onto uid, which becomes 0: root. The fix stops it at the length check, copy_from_user stops a kernel pointer, and the CPU stops a kernel jump into user code. set_name is a toy system call made up for this lesson, and its memory layout is a toy too. Real Linux keeps a process's user ids in a credentials structure, and real exploits take more steps. The shape is the same: a missing check lets user data overwrite kernel data that decides privilege.

Trust

  1. Kernel code runs with full power over the machine. Every system call hands it numbers and pointers chosen by a program, which may be hostile. If the kernel trusts them and has a bug, the program borrows that power.

The attack

The defences

What happens

;; step 1/10 · trust
attack surface   every way in: system calls, files, devices
arguments        chosen by the caller, so treat them as hostile

a system call handler must (Berkeley CS162):
  copy arguments from user memory into kernel memory
  validate arguments
  copy results back

a kernel bug runs in kernel mode: no wall stops it

set_name is a toy system call made up for this lesson, and its memory layout is a toy too. Real Linux keeps a process's user ids in a credentials structure, and real exploits take more steps. The shape is the same: a missing check lets user data overwrite kernel data that decides privilege. This lesson shows how an attack works so you can spot and prevent it. Testing attacks on systems you do not own, or without permission, is illegal in most countries.