Section 6.10

Privilege level

Definition

A privilege level is a mode of a processor, recorded in hardware, that decides which instructions, registers and memory the running code may use. Code at the higher level can change memory protection, turn interrupts §6.11 off and choose the handlers that run on events. Code at a lower level cannot; it must ask higher-level code through a special instruction that enters the higher level only at code the higher level chose. Arm Cortex-M processors have two levels, privileged and unprivileged; desktop systems call theirs kernel mode and user mode.

The idea

A protection mechanism that software configures must itself be protected, or the code it confines simply switches it off. A memory protection unit §6.8 (MPU), which blocks accesses to chosen regions of memory, is configured by writing a few registers at fixed addresses. If the confined code could write those addresses, one store instruction would switch the MPU off, and every rule it enforced would be gone. The same holds for the instruction that turns interrupts off: code that could run it would stop the operating system from ever taking the processor back.

Privilege levels solve this in hardware. The processor records the level of the running code and, at the lower level, refuses the instructions and register accesses that control protection. A theater is a fair picture. The actors, standing for unprivileged code, work on the stage, but only the stage manager, standing for privileged code, works the locks and the lights. An actor who needs the curtain moved passes a request through one fixed hatch. The picture fails at one point: the stage manager must check every request, because the request comes from an actor whom the stage manager does not trust, and a manager who obeys blindly gives every actor the manager’s own power.

How it works

On Arm Cortex-M processors of the Armv7-M architecture, such as the Cortex-M4 §6.3, the rules are these.

Privilege changes in a fixed pattern.

  1. Set up. Privileged startup code configures the MPU, places the vector table and sets the interrupt priorities.

  2. Drop. It sets nPRIV to 1 and starts the confined code. Unprivileged code cannot set nPRIV back to 0, so the drop is one-way.

  3. Enter. The only way back up is an exception. The confined code requests a service with the instruction SVC (supervisor call), which raises the SVCall exception. The processor switches to Handler mode and jumps to the address that the vector table holds for that exception, an address that privileged code chose. The confined code cannot choose where privileged execution starts.

  4. Check. The handler reads the request and its arguments, which the confined code chose, checks them, and performs only an effect that the caller is allowed to have.

  5. Return. The return from the exception puts the processor back in Thread mode, still unprivileged.

Other processors follow the same pattern with more levels. x86 processors have four rings, of which operating systems use ring 0 for the kernel, the core of the operating system, and ring 3 for programs. Arm application processors have exception levels: EL0 for programs, EL1 for the kernel, EL2 for a hypervisor, which runs several operating systems on one machine, and EL3 for the secure monitor, firmware that switches between a normal world and a secure world. RISC-V processors (RISC stands for reduced instruction set computer) have machine, supervisor and user modes. Arm TrustZone adds a separate dimension, a secure and a non-secure state, which is independent of the privilege level.

A system gets this protection only if it uses it. In the Zephyr RTOS §6.33 (real-time operating system), every thread runs privileged, as a supervisor thread, unless the build enables user mode with the option CONFIG_USERSPACE and the thread is created as a user thread. Many small systems run everything privileged. The MPU can then still catch accidents, such as a stack overflow §4.10, in which a thread’s stack (the memory for its local variables) grows into a small no-access region placed below it. It cannot confine code that is written, or tricked, to switch the MPU off.

Confinement holds only if the confined code cannot change any part of the state that enforces it, called here the control state:

Every privileged service that writes memory for its caller is part of the boundary as well, because the service holds rights that the caller lacks.

Worked example

An untrusted thread, a sequence of instructions that the kernel schedules, runs unprivileged on a Cortex-M4. An MPU region lets it read and write one 1024-byte buffer from address 0x20004000 to 0x200043FF (the prefix 0x marks hexadecimal). The thread tries four ways out.

  1. It stores 0 to address 0xE000ED94, the register MPU_CTRL, to switch the MPU off. The access is unprivileged and goes to a system control register, so the store does not happen and a fault exception runs instead.

  2. It executes CPSID i, the instruction that blocks interrupts, to keep the processor forever. At the unprivileged level the instruction does nothing. The timer interrupt still arrives, and the scheduler, the kernel code that decides which thread runs, can switch to another thread.

  3. It writes 0 to nPRIV in CONTROL to become privileged. The write is ignored.

  4. It asks the kernel. The kernel offers a system call that writes nn bytes starting at address aa, with aa and nn chosen by the caller. The system call handler runs privileged, and the kernel’s MPU settings let privileged code write all of RAM.

Route 4 is the open one. If the handler writes wherever it is told, the thread passes the address of a kernel variable and the handler overwrites it. The handler, acting with its own rights on the thread’s behalf, is a confused deputy §1.25. A first repair accepts the request only if a≥Ba \ge B and a+n≤B+1024a + n \le B + 1024, where BB, equal to 0x20004000, is the start of the buffer. In 32-bit arithmetic this check fails open, meaning that it accepts a request it should refuse. Take aa = 0x20004000 and nn = 0xFFFFFFF0. The sum a+na + n is 0x120003FF0, which does not fit in 32 bits; the processor keeps the low 32 bits, 0x20003FF0, which is below B+1024B + 1024, that is 0x20004400. The check passes, and the handler starts writing almost 4 GiB of data from the start of the buffer, over everything above it, until a fault stops it.

The repair is an overflow-safe range check §4.17 that never forms the sum: accept only if a≥Ba \ge B, n≤1024n \le 1024, and a−B≤1024−na - B \le 1024 - n. For nn = 0xFFFFFFF0, the second test fails. For aa = 0x200043F8 and n=16n = 16, a−Ba - B is 0x3F8, which is 1016, and 1024−16=10081024 - 16 = 1008. Since 1016>10081016 > 1008, the request is refused, correctly: its last 8 bytes would land at 0x20004400 to 0x20004407, outside the buffer. For aa = 0x200043F0 and n=16n = 16, a−B=1008≤1008a - B = 1008 \le 1008, and the request is accepted. It ends exactly at the last byte of the buffer, 0x200043FF.

Stated precisely

Let CC be the confined code and EE an effect, such as a write to a given address. The claim “CC cannot cause EE” holds if all four of the following conditions hold.

  1. Level. Every instruction of CC runs at the lower privilege level.

  2. Protected control state. No action available at the lower level changes the control state: the protection registers and interrupt masks are writable only at the higher level, and the memory protection denies CC write access to every part of the control state that lies in ordinary memory.

  3. Controlled entry. Every transition to the higher level starts at code chosen by higher-level software, and that code checks every value CC supplied before acting on it.

  4. No other writers. No agent that CC can direct, such as a DMA controller that CC can program, or a debugger, writes the control state or causes EE.

Under these conditions the effects CC can cause are limited to its own memory accesses that the memory protection allows, plus the effects that privileged services perform after their checks succeed.

Limits and common mistakes

Privileged code is trusted completely. Everything that runs privileged, including every driver and every library linked into the kernel, belongs to the trusted computing base §1.4 (TCB), the code on whose correctness the protection depends. An RTOS that runs all threads privileged has one large TCB.

Calling code untrusted in its source changes nothing. Only the hardware level at which the code runs, and the rights it is given, confine it.

A privileged service that copies, writes or frees memory on request is a boundary of its own. One unchecked address argument hands the caller the service’s rights, as the worked example shows.

Privilege separation limits what code can touch, not how long it runs. Unprivileged code cannot mask interrupts, so a scheduler that preempts threads on a timer interrupt can always take the processor back. The confined code can still use all the processor time and memory it is given; a time limit needs a timer or an instruction budget §2.32.

The level does not stop agents outside the processor’s instruction stream: a DMA transfer, a debugger attached to the debug port, or a physical fault attack such as a deliberate voltage glitch.

The drop to the lower level must happen on every path that leads into untrusted code, including a restart of the thread after a fault.

Questions answered

Can untrusted native code disable the mechanism intended to confine it?

Yes, untrusted native code can disable the mechanism meant to confine it if it runs at the privileged level, or if it can make a privileged service, a direct memory access (DMA) controller or a debug port change the mechanism’s settings on its behalf. Native code means machine code that the processor runs directly, not code run by an interpreter §2.13. On a Cortex-M processor, privileged code can switch off the memory protection unit (MPU), mask interrupts and rewrite the vector table with ordinary instructions.

It cannot do so when four conditions hold. It runs unprivileged. The MPU registers, the vector table, the interrupt settings and all kernel data are out of its reach. Every entry into privileged code goes through an exception handler §6.15 that checks the caller’s arguments, including with overflow-safe address checks. And no device or debugger acts for it. The hardware enforces the first condition; the system’s design must establish the other three. Calling code untrusted, or giving a service a reassuring name, enforces nothing.

Related entries: Trusted computing base (TCB) (§1.4), Reference monitor (§1.17), Virtual machine (VM) (§2.3), Memory protection unit (MPU) (§6.8), Confused deputy (§1.25), Overflow-safe range check (§4.17), Least privilege (§1.15). Further reading: [13, 81].