The x86 privilege rings
Video 028 of 140 in Virtualization Foundations, in the unit “Privilege, system calls and interrupts”. 3:39, with English and Hindi audio.
Most servers today use x86 processors, a family made by Intel and AMD. Its designers sorted code into four levels of privilege, called rings. The rings decide what a piece of code may read and which instructions it may run. This article explains each ring, what the processor checks on every access, why the rings exist, and how a controlled gate is the only safe way for code to move into the kernel.
What you will learn
- An x86 processor has four rings, numbered zero to three. The lower the number, the more the code may do.
- The kernel runs in ring zero, and your programs run in ring three, under limits.
- The processor checks every access against the ring of the code making it.
- Code moves inward only through a gate, which is a fixed entry point the kernel sets up.
Four rings, from the most powerful to the least
- Ring 0 is the most powerful ring. The kernel runs here, with full access.
- Rings 1 and 2 are defined by the chip. Mainstream systems leave them unused.
- Ring 3 is the least powerful ring. Your programs run here, under limits.
Each ring is a level of privilege. Ring zero is the innermost, and the most powerful. Ring three is the outermost, and the most limited. Linux and Windows use only two of the four, ring zero for the kernel and ring three for programs.
What the processor checks on every access
The processor checks every access against the ring of the code making it. Code in ring three cannot read data kept in ring zero, and it cannot run privileged instructions. Those refusals are the core of the protection.
Code in ring zero can run privileged instructions. These include instructions that load the memory maps, called page tables, and instructions that halt the processor.
To move inward, code must pass through a gate. A gate is a fixed entry point that the kernel sets up. A program cannot simply jump into kernel code, because the only way in is the gate the kernel controls.
Why the rings exist: protection, containment and control
Early personal computers, such as MS-DOS, had no such walls. A program could overwrite anything, including the operating system itself. The rings add those walls. A program in ring three cannot change the kernel's own data, or read another program's memory.
Consider a database process on a logistics firm's server. It runs in ring three, next to a web server, also in ring three. If the database tries to read the web server's memory, the processor and the kernel refuse. The rings keep each program to its own space.
Note: Containment follows from this. When a program in ring three crashes, the damage stops at its edge, and the rest of the machine keeps running.
When the walls can fail
Walls can have holes. A flaw in the kernel can let an ordinary program reach ring zero, and then it controls everything. That is why kernel security patches matter so much. You would notice the risk through a security scan that reports a kernel flaw, or a process running as root when it should not.
Terms
- Privilege ring
- One of the four levels of privilege on an x86 processor, from ring zero, the most powerful, to ring three, the most limited.
- Gate
- A fixed entry point, set up by the kernel, through which code may move into kernel mode.
- Page table
- The map that tells the processor which memory a program may use, and where each part of it lives.
- Privileged instruction
- An instruction that only the most powerful ring may run, such as one that loads the page tables.
Check yourself
Which ring does the kernel run in, and which ring do your programs run in?
The kernel runs in ring zero, the most powerful. Your programs run in ring three, the most limited.
How can code in ring three reach the kernel?
Only through a gate, which is a fixed entry point that the kernel sets up. The program cannot jump into kernel code directly.
Why does a crash in one ring-three program usually leave the rest of the machine running?
The rings stop the program from reaching other programs' memory or the kernel's data, so the damage stays at the program's edge.
Previous: User mode and kernel mode. Next: What is a system call?.