Skip to main content
Be Bitwise

iOS Architecture & the XNU Kernel

iOS Internals · Mobile Internals track

5 min read

In this module

The iOS Stack

Every iPhone, iPad, and Apple Watch runs a stack built on top of Darwin, Apple's open-source OS foundation. The heart of Darwin is the XNU kernel, a hybrid design that crams three different components into a single address space. If you want to understand iOS security, you have to understand XNU. Every exploit that reaches the kernel has to navigate its plumbing.

The name "X is Not Unix" is a bit of a wind-up, since XNU exposes a fully POSIX-compliant Unix interface. The kernel ships as one binary. Inside, it splits into three major subsystems with three very different lineages.

The Three Pillars of XNU

Mach: The Microkernel Foundation

XNU's bottom layer comes from the Mach microkernel, born at Carnegie Mellon. Mach hands out the primitives everything else stands on:

  • Tasks and threads: Tasks hold resources (memory, ports). Threads do the actual running. Every Unix process is, underneath, a Mach task.
  • Virtual memory: Mach owns the virtual address space — page faults, memory mapping, copy-on-write.
  • Inter-process communication (IPC): Mach ports are the kernel's messaging fabric. Every IPC operation — bytes between processes, service lookups, all of it — rides on Mach messages.

The IPC layer is where most of the security drama happens. Mach ports are capabilities: hold a port right and you hold access to whatever sits behind it. A lot of iOS exploits boil down to stealing or forging port rights to talk to privileged services you weren't supposed to reach.

BSD: The Unix Layer

Sitting above Mach is a BSD subsystem lifted from FreeBSD. This is the bit applications actually see — the familiar Unix face of XNU:

  • POSIX compliance: Process model (fork, exec, wait), file descriptors, signals, and the VFS (Virtual File System) layer.
  • Networking: The TCP/IP stack, sockets, and packet filtering all live in the BSD layer.
  • Security framework: The TrustedBSD Mandatory Access Control (MAC) framework, which powers the App Sandbox, is part of the BSD layer.

IOKit: The Driver Framework

The third pillar is IOKit, an object-oriented driver framework written in a stripped-down subset of C++. IOKit owns every conversation with hardware: displays, sensors, USB, Bluetooth, storage controllers.

IOKit

IOKit is XNU's C++ driver framework. Drivers are kernel objects arranged in a provider-client tree. Each driver hands out user clients so userland processes can talk to the hardware. Those user clients are a notorious source of kernel bugs. They parse messy structured data straight from sandboxed apps, and any flaw in the parsing lands inside the kernel.

System Call Dispatch

When an app needs the kernel, it fires a syscall. XNU handles two families through separate dispatch paths:

  • Mach traps: Low-level operations on Mach primitives (ports, tasks, threads). These enter the kernel through a dedicated trap table.
  • BSD syscalls: Standard POSIX operations (file I/O, networking, process management). These are dispatched through the BSD sysent table.

The CPU's exception mechanism shoves the trap into the kernel, and the syscall number decides who answers it: Mach or BSD.

On arm64 the app puts the number in x16 and executes svc #0x80. XNU's synchronous-exception handler (handle_svc() in osfmk/arm64/sleh.c) then splits on the sign. Simplified from the real source:

c
// Simplified from XNU's handle_svc() on arm64
int trap_no = (int)state->x[16]; // syscall number from x16
if (trap_no == (int)PLATFORM_SYSCALL_TRAP_NO) {
platform_syscall(state); // a few machine-specific calls
} else if (trap_no < 0) {
mach_syscall(state); // Mach traps: negative numbers
} else {
unix_syscall(state, thread, uthread, proc); // BSD: positive numbers
}

The Kernel Address Space

Mach, BSD, and IOKit all live inside one kernel address space. That's what makes XNU a hybrid rather than a real microkernel. In a textbook microkernel, drivers and filesystems run as ordinary user processes. In XNU, they run with the keys to the kingdom.

The three arrows leaving the sandboxed app are its kernel attack surface: whatever syscalls, Mach interfaces and IOKit user clients its sandbox profile still lets through. On A15 and M2 chips and later, current iOS adds the bottom tier. The Secure Page Table Monitor (SPTM) and Trusted Execution Monitor (TXM) run at a higher privilege than XNU, so the kernel has to ask SPTM to change page tables and ask TXM whether code may run. Code execution in the kernel no longer means you can quietly map new executable pages.

Shared address space, shared blast radius. A bug in any IOKit driver, any BSD network handler, or any Mach primitive runs with full kernel privilege, so it can still be a route to the whole kernel. Apple has spent years putting partial walls inside that space: type-segregated allocation (kalloc_type) keeps different object types out of each other's memory, pointer authentication (PAC) guards code and many data pointers, and SPTM and TXM keep page tables and code-signing decisions out of the kernel's own hands. Those raise the cost of turning a driver bug into full control, but they don't separate a display driver's privilege from the VM subsystem's the way a microkernel would. (On macOS, DriverKit moves many third-party drivers out of the kernel altogether.)

Why This Architecture Matters

XNU's hybrid design is a trade. Sharing one address space makes everything fast — no IPC overhead between kernel components — and it stretches the attack surface to match. Every IOKit user client, every BSD syscall, every Mach trap is reachable from sandboxed app code, and any of them can be a foothold.

Knowing which subsystem owns which operation is step one for mapping the kernel attack surface. It's also the prerequisite for the exploit chains we pull apart later in the module.