The Kernel as a Target: Syscalls and Mitigations
Every process on a system, sandboxed browser tab or container included, shares one kernel. A bug in user-space code gives an attacker that one process; a bug in the kernel can give them everything: all processes, all files, the ability to leave a container or switch off security features. That is why kernel security is its own research field, with its own tools (syzkaller) and its own layers of mitigation. This lesson maps the attack surface, the boundary it crosses and the mitigations on Linux and Windows, at the level of concepts.
The boundary
User programs run in an unprivileged CPU mode (ring 3 on x86-64, EL0 on ARM64); the kernel runs privileged (ring 0, EL1). A program cannot touch kernel memory or hardware directly. It asks the kernel through a system call: it puts a number and arguments in registers and executes syscall; the CPU switches mode and jumps to the kernel's entry point; the kernel validates the arguments, does the work, and returns. Processes and System Calls introduced the mechanism; the security point is that every argument of every system call is attacker input from the kernel's point of view: pointers, lengths, flags, file descriptors, structures.
The attack surface
| Surface | Why it is risky |
|---|---|
| system calls (several hundred on Linux x86-64) | each validates its own arguments; rarely used ones get less testing |
ioctl on device files |
a second, driver-defined syscall interface with structures parsed by each driver |
| file systems | mounting an image means parsing an attacker's file system in the kernel (USB sticks, disk images) |
| network stack | packets parsed before any process sees them; remote attack surface |
| drivers (USB, Bluetooth, GPU, Wi-Fi) | large, often vendor-written code; USB and radio input arrive without user action |
| eBPF | user-supplied programs run inside the kernel after a verifier checks them |
| namespaces and user namespaces | let unprivileged users reach code paths that used to need root |
The session shows the boundary from the user side. A small program's four requests to the kernel are traced with strace, then the total number of system calls a tiny program makes is counted, then the session looks at two defences already applied to it: the container's seccomp filter, and kernel symbol addresses hidden from unprivileged users. It runs in your sandbox:
cat > hello.c <<'SRC'
#include <fcntl.h>
#include <unistd.h>
int main(void) {
char buf[64];
int fd = open("/opt/sec-samples/files/hello.txt", O_RDONLY); /* syscall: openat */
ssize_t n = read(fd, buf, sizeof buf); /* syscall: read */
if (n > 0 && write(1, buf, n) != n) return 1; /* syscall: write */
close(fd); /* syscall: close */
return 0;
}
SRC
gcc -O1 hello.c -o hello
echo "== every request this program makes to the kernel (after loading)"
strace -e trace=openat,read,write,close -o trace.txt ./hello > /dev/null
sed -n '/hello.txt/,$p' trace.txt
echo "== how many system calls does starting a tiny program take?"
strace -c -o counts.txt ./hello > /dev/null
awk '$NF == "total" {print $4, "system calls,", NR - 4, "different kinds"}' counts.txt
echo "== this sandbox filters system calls (seccomp) and hides kernel addresses"
grep -E '^Seccomp' /proc/self/status
head -3 /proc/kallsyms== every request this program makes to the kernel (after loading)
openat(AT_FDCWD, "/opt/sec-samples/files/hello.txt", O_RDONLY) = 3
read(3, "Hello from the lab.\nThis file is"..., 64) = 51
write(1, "Hello from the lab.\nThis file is"..., 51) = 51
close(3) = 0
+++ exited with 0 +++
== how many system calls does starting a tiny program take?
33 system calls, 17 different kinds
== this sandbox filters system calls (seccomp) and hides kernel addresses
Seccomp: 2
Seccomp_filters: 1
0000000000000000 A fixed_percpu_data
0000000000000000 A __per_cpu_start
0000000000000000 A cpu_debug_store
- The four lines after the program's own start-up are its whole conversation with the kernel:
openatreturns file descriptor 3,readcopies 51 bytes,writesends them to standard output,closereleases the descriptor. In each call the kernel had to check the pointer (is this user memory?), the length and the descriptor. - Even a four-call program makes 33 system calls of 17 kinds, most of them in the dynamic loader. Attack surface is what is reachable, not what a program uses.
Seccomp: 2means a seccomp filter is active (Docker applies one by default): system calls outside an allow-list fail before reaching the kernel code behind them. Browsers and container runtimes use this to shrink the kernel's attack surface for untrusted code./proc/kallsymslists kernel symbols, but every address reads as zeros for a user withoutCAP_SYSLOG(controlled bykptr_restrict, and on most systems hidden even at its default): kernel addresses would defeat KASLR, so they are hidden.
Mitigations on Linux
| Mitigation | What it stops | Notes |
|---|---|---|
| KASLR | knowing where kernel code and data are | defeated by any address leak; hence kptr_restrict, dmesg_restrict |
| SMEP (x86), PXN (ARM) | the kernel executing code in user-space pages | the old trick of jumping to a user page no longer works |
| SMAP (x86), PAN (ARM) | the kernel reading or writing user pages outside the explicit copy routines (copy_from_user) |
catches pointer confusions between user and kernel memory |
| KPTI | Meltdown-style reads of kernel memory from user mode (2018) | unmaps most of the kernel while user code runs (Spectre and Meltdown) |
stack protector, FORTIFY_SOURCE, hardened usercopy, CONFIG_INIT_STACK_ALL_ZERO |
classic memory bugs in kernel code | the user-space mitigations of Memory Safety, in the kernel |
| kernel CFI (kCFI), shadow call stacks on ARM64 | control-flow hijacking | |
| seccomp, Landlock, LSMs (SELinux, AppArmor) | reaching code at all; what a process may touch | attack-surface reduction |
lockdown, module signing, unprivileged_bpf_disabled, restricting unprivileged user namespaces |
turning root or a feature into kernel compromise | each one closes a whole path |
Mitigations on Windows
Windows has the same building blocks (kernel ASLR, SMEP, SMAP support, Kernel Control Flow Guard) plus a design that uses the hypervisor. Virtualization-based Security (VBS) runs a small secure kernel beside the normal one, isolated by Hyper-V, so that even a fully compromised normal kernel cannot read its secrets. Hypervisor-protected Code Integrity (HVCI), shown in Windows Security as "Memory integrity", moves the decision "may this page become executable kernel code?" into that secure world: kernel pages cannot be both writable and executable, and only signed code can become executable. Drivers must be signed, and Microsoft maintains a vulnerable driver blocklist, because attackers who already have administrator rights load a legitimately signed but buggy driver to reach the kernel ("bring your own vulnerable driver"). Kernel-mode hardware-enforced stack protection (shadow stacks) protects return addresses on supported CPUs. Windows & Active Directory Security covers the user-mode side.
How kernel bugs are detected
- Sanitizers for the kernel: KASAN (address), KMSAN (uninitialised memory), KCSAN (data races), UBSAN, plus lockdep; enabled in test kernels, they turn silent corruption into reports in the kernel log.
- Kernel fuzzing with syzkaller, which generates syscall sequences and reads coverage through
kcov(next lesson). - Crash reports (oops, panics) from fleets and continuous-integration systems such as syzbot, which posts its findings to the kernel mailing lists.
- On production systems, behavioural monitoring: unexpected privilege changes, unknown modules or drivers loading, eBPF programs appearing, processes whose credentials change without a
setuidbinary.
How the risk is reduced
Patch quickly (kernel bugs are high-value), and reduce what is reachable: seccomp filters for every service and container, no unprivileged eBPF, restrict unprivileged user namespaces where they are not needed, keep drivers to what the hardware needs, enable the mitigations above, and on Windows turn on VBS and memory integrity and keep the driver blocklist current. In the longer term, memory-safe languages are entering both kernels (Rust in Linux drivers, Rust components in Windows).
Pitfalls
- "Containers are a security boundary." They share the kernel; a kernel bug crosses them. Use seccomp, user namespaces and, for hostile tenants, VMs.
- Treating a local privilege escalation as minor. Combined with any code execution (a browser renderer, a web app), it is a full compromise.
- Leaking addresses. A single kernel pointer in a log or error message defeats KASLR.
- Admin is not kernel. On Windows with HVCI and driver signing, administrator rights alone should not give arbitrary kernel code; keeping that true is what the blocklist is for.
Practice
1. Why is ioctl often called a "second system-call table"?
ioctl(fd, request, arg) lets each driver define its own commands and argument structures. Every driver that implements it adds new code paths that parse user-supplied structures in the kernel, with less review and testing than the core system calls.
2. A kernel bug lets a user write to an attacker-chosen kernel address. Which mitigation in the table is most directly relevant, and which ones are not?
KASLR and address hiding matter first (the attacker must know where to write); kernel CFI and HVCI limit what an overwritten pointer can lead to. SMEP and SMAP are about touching user pages and help only against some follow-up steps. The real fix is the patch.
3. What does Seccomp: 2 mean, and why does it reduce kernel attack surface?
Mode 2 is filter mode: a BPF program decides for each system call whether it is allowed. Calls that are not allowed never reach the kernel code that implements them, so bugs in that code cannot be triggered by the filtered process.
Test yourself
Why can a single kernel bug compromise every container on a host?
- Containers share files
- All containers share one kernel
- Containers run as root
- Docker is insecure
What does SMAP prevent?
- Executing user pages
- The kernel accessing user-space memory outside the explicit copy routines
- Reading kernel memory from user mode
- Loading unsigned drivers
What does HVCI ('Memory integrity') move into the hypervisor-protected world?
- The file system
- The decision whether a kernel page may become executable code
- Network filtering
- User passwords
Why were kernel symbol addresses shown as zeros in /proc/kallsyms?
- The kernel has no symbols
- Kernel addresses are hidden from unprivileged users so they cannot defeat KASLR
- The file is empty
- seccomp blocked the read