Part 6 of 7 · 3 min read
Least privilege
Least privilege and Unix's compressed matrix
Think of a hotel key card that opens your room and the gym, and not the other rooms. A cleaner's card opens the rooms of one floor during the working day. The analogy stops in one place: a hotel can print many cards cheaply, while giving a process exactly the rights it needs can take real design work.
In plain words: The principle of least privilege says that each program and each user should have only the rights that the task needs, for only as long as needed. It limits the damage of a mistake or an attack, because a compromised program can do only what its domain allows. Unix compresses the matrix by giving every file one owner, one group and three classes: owner, group and others, each with read, write and execute bits. That is nine bits, and it is a very small ACL with at most three entries. Page 2 shows how the bits work.
A worked example (made up to show the idea). A backup script runs as root so that it can read every file. A flaw in the script lets a user make it run an arbitrary command, and that command runs as root: the whole machine is lost. If the script instead ran as a user backup that can read the files but cannot write them or change the system, the same flaw would let an attacker read files, which is bad, and not destroy the system. Least privilege turned a total loss into a smaller one.
The key lines. Least privilege is a habit as much as a mechanism. Ask of each program: which user, which files, which system calls, which network ports does it really need? The pages that follow give tools for each: users and permissions (page 2), capabilities (page 4) and sandboxing (page 6).
Principle of least privilege
The principle of least privilege is the design rule that a process, user or component should hold only the access rights that its task requires, and hold them only while the task needs them. It limits the harm that can follow from an error or a compromise.
Example: a web server that opens port 80 and then runs as an unprivileged user.
Interview angle
A common question is "What is the principle of least privilege, and where do you apply it in Linux?" A good answer gives the rule, and then three places: a service user in place of root, capabilities in place of full root power, and a sandbox with a system call filter.
Check yourself: 1. How many permission classes does a Unix file have? 2. How did least privilege reduce the damage in the backup example? 3. Name two questions to ask about a program to apply the principle.
Words you will meet
| Word | Meaning |
|---|---|
| Subject | A user or process that acts. |
| Object | A thing that is acted on: file, device, process. |
| Domain | The set of rights a process holds. |
| ACL | List of subjects and rights kept with an object. |
| Capability | A token that names an object and rights. |
Quick notes
- Protection is the mechanism. Security is the wider goal. The goals are confidentiality, integrity and availability.
- A domain is the set of rights of a process. On Linux it comes from user and group IDs and capabilities.
- The access matrix has subjects as rows and objects as columns. By column it is an ACL, by row a capability list.
- ACLs are easy to revoke. Capabilities are easy to pass on and check.
- An open file descriptor is a capability.
- Least privilege: give each part only what it needs, for only as long as needed.