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.
The processor runs in Thread mode for ordinary code and in Handler mode while it handles an exception. An exception is the processor’s general mechanism for leaving the current code to run a handler for an interrupt, a fault or a system call.
Handler mode is always privileged. Thread mode is privileged or unprivileged according to bit 0, called
nPRIV, of theCONTROLregister. After reset the processor is in privileged Thread mode.Unprivileged code cannot access the system control registers, which include those of the MPU, those of the interrupt controller, and the vector table §6.14 offset register
VTOR, which says where the vector table (the table of handler addresses) lies; such an access faults. Its instructions that change the interrupt masks, the registersPRIMASK,FAULTMASKandBASEPRIthat block interrupts, or theCONTROLregister, have no effect. Its memory accesses are checked against the unprivileged rights of each MPU region.
Privilege changes in a fixed pattern.
Set up. Privileged startup code configures the MPU, places the vector table and sets the interrupt priorities.
Drop. It sets
nPRIVto 1 and starts the confined code. Unprivileged code cannot setnPRIVback to 0, so the drop is one-way.Enter. The only way back up is an exception. The confined code requests a service with the instruction
SVC(supervisor call), which raises theSVCallexception. 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.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.
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:
the MPU registers and the kernel data from which the kernel reprograms them;
the vector table and
VTOR, which decide where privileged execution starts;the interrupt enables, priorities and masks;
the kernel’s code, data and stacks;
the registers of any DMA §4.36 (direct memory access) controller, a device that copies memory without the processor and that the MPU does not check;
the debug port, through which an attached debugger can read and write all memory.
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.
It stores 0 to address
0xE000ED94, the registerMPU_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.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.It writes 0 to
nPRIVinCONTROLto become privileged. The write is ignored.It asks the kernel. The kernel offers a system call that writes bytes starting at address , with and 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 and , where , 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 = 0x20004000 and = 0xFFFFFFF0. The sum is 0x120003FF0, which does not fit in 32 bits; the processor keeps the low 32 bits, 0x20003FF0, which is below , 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 , , and . For = 0xFFFFFFF0, the second test fails. For = 0x200043F8 and , is 0x3F8, which is 1016, and . Since , the request is refused, correctly: its last 8 bytes would land at 0x20004400 to 0x20004407, outside the buffer. For = 0x200043F0 and , , and the request is accepted. It ends exactly at the last byte of the buffer, 0x200043FF.
Stated precisely
Let be the confined code and an effect, such as a write to a given address. The claim “ cannot cause ” holds if all four of the following conditions hold.
Level. Every instruction of runs at the lower privilege level.
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 write access to every part of the control state that lies in ordinary memory.
Controlled entry. Every transition to the higher level starts at code chosen by higher-level software, and that code checks every value supplied before acting on it.
No other writers. No agent that can direct, such as a DMA controller that can program, or a debugger, writes the control state or causes .
Under these conditions the effects 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].