Protection, privilege and sandboxing
About this lesson
The kernel checks every request a program makes. Ordinary checks ask who is asking: the process's user ID against a file's owner and permission bits. But a program that handles untrusted data can be hijacked, and then it has every right its user has. A sandbox applies least privilege: it gives one program fewer rights than its user. Follow five lines of C that open an image, are refused /etc/shadow, switch on seccomp strict mode, read the image, and are killed when they reach for the user's private key. User mode and kernel mode explains the gate every request goes through; Hardware support for virtualisation, the Software lesson Containers and deployment, and the Linux lesson Namespaces and cgroups: how containers work show the bigger walls built on the same ideas.
Keeping programs apart
Protection means each program may touch only what it is allowed to. Every request a program makes enters the kernel through the system call gate, so the kernel can check each one. Our program opens an image, cat.png, and reads it.
protection each program touches only what it may
the gate every request is a system call
the kernel checks every request, every time
("complete mediation", OSTEP ch. 53)
the program (C99, Linux):
#include <fcntl.h>
#include <sys/prctl.h>
#include <unistd.h>
int main(void) {
char buf[64];
int fd;
fd = open("cat.png", O_RDONLY);
open("/etc/shadow", O_RDONLY);
prctl(PR_SET_SECCOMP, 1);
read(fd, buf, 64);
open(".ssh/id_rsa", O_RDONLY);
return 0;
}Read the lesson step by step
Keeping programs apart
Protection means each program may touch only what it is allowed to. Every request a program makes enters the kernel through the system call gate, so the kernel can check each one. Our program opens an image, cat.png, and reads it.
protection each program touches only what it may the gate every request is a system call the kernel checks every request, every time ("complete mediation", OSTEP ch. 53) the program (C99, Linux): #include <fcntl.h> #include <sys/prctl.h> #include <unistd.h> int main(void) { char buf[64]; int fd; fd = open("cat.png", O_RDONLY); open("/etc/shadow", O_RDONLY); prctl(PR_SET_SECCOMP, 1); read(fd, buf, 64); open(".ssh/id_rsa", O_RDONLY); return 0; }Who is asking?
Each process carries credentials: here user ID 1000, alice. Each file records an owner and permission bits that say who may read or write it. The kernel compares the two on every open.
process uid 1000 (alice) file owner may alice read? cat.png alice yes (owner bits rw-) /etc/shadow root no (other bits ---) .ssh/id_rsa alice yes (owner bits rw-) rule: owner bits if your uid owns the file, group bits if you are in its group, else the other bitsLine 1: allowed
open() traps into the kernel. alice owns cat.png and the owner may read it, so the kernel opens it and hands back file descriptor 3, a small number that now stands for this open file.
line 1 fd = open("cat.png", O_RDONLY); syscall openat (glibc open() uses openat) filter off check uid 1000 owns cat.png -> owner bits rw- -> r: yes result fd = 3 (0, 1, 2 are taken: lowest free is 3)Line 2: refused
/etc/shadow holds password hashes. It belongs to root, and other users get no read bit. alice is neither, so the kernel refuses: open() returns -1 with the error EACCES. The program is told no and carries on.
line 2 open("/etc/shadow", O_RDONLY); check owner root, uid 1000 is not root other bits --- -> r: no result -1, errno = EACCES (permission denied) a refusal is an error code, not a crash root (uid 0) would pass: CAP_DAC_OVERRIDE skips these checks (capabilities(7))One bug away
cat.png came from the internet. If a bug in the image code lets crafted bytes take over the program, the attacker runs as alice, with every right alice has, including her private key. Least privilege says: keep only the rights you need.
the kernel checks WHO asks, not WHY a hijacked program still has uid 1000 so it may read .ssh/id_rsa (owner rw-) least privilege (OSTEP ch. 53): give a process the minimum privileges it needs to do its job this program only needs: read the imageLine 3: into the sandbox
prctl() turns on seccomp strict mode, a filter the kernel runs on every system call this thread makes from now on. Only four calls pass: read, write, _exit and sigreturn. Any other call, even one to undo it, is fatal.
line 3 prctl(PR_SET_SECCOMP, 1); 1 = SECCOMP_MODE_STRICT (<linux/seccomp.h>) filter off -> strict allowed read, write, _exit, sigreturn other the thread is killed (SIGKILL) the program already holds what it needs: fd 3, open for reading cat.pngLine 4: still allowed
read is on the list, so the call passes the filter. Then the usual checks run: fd 3 is open for reading. The kernel copies the first 64 bytes of cat.png into buf. Honest work still works.
line 4 read(fd, buf, 64); filter read: on the list -> pass check fd 3 open for reading -> yes result 64 (bytes copied into buf)Line 5: the attack
Now suppose the attacker's code runs and asks for alice's private key. open() becomes the openat system call, which is not on the list. The filter stops it at the door, before any file lookup or permission check.
line 5 open(".ssh/id_rsa", O_RDONLY); (stands for code an attacker slipped in) syscall openat filter openat: not on the list -> stop the call never runsThe kernel ends it
In strict mode the penalty for a call off the list is death: the kernel sends SIGKILL and the process ends. The key was never opened, even though alice's own permission bits would have allowed it.
signal SIGKILL (cannot be caught or ignored) program ends; .ssh/id_rsa never opened without the sandbox: uid 1000 owns the key -> fd with the sandbox: openat not allowed -> killedWalls inside walls
Protection comes in layers. The CPU's modes guard the kernel, credentials guard each user's files, and a sandbox shrinks one program's rights further. Containers and virtual machines add bigger walls around whole groups of programs.
the rules as a Python simulation: ALLOWED = {"read", "write", "_exit", "sigreturn"} # seccomp strict FILES = {"cat.png": (1000, 0o644), # (owner uid, mode) "/etc/shadow": (0, 0o640), ".ssh/id_rsa": (1000, 0o600)} uid, strict = 1000, False def syscall(name, path=None): if strict and name not in ALLOWED: # the filter goes first return "SIGKILL" if path: # then the permission bits owner, mode = FILES[path] bits = mode >> 6 if uid == owner else mode & 7 if not bits & 4: return "EACCES" return "ok" print(syscall("openat", "cat.png")) print(syscall("openat", "/etc/shadow")) strict = True # prctl(PR_SET_SECCOMP, 1) print(syscall("read")) print(syscall("openat", ".ssh/id_rsa")) prints: ok EACCES ok SIGKILL
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A process runs with user ID 1000. It opens a file owned by user 1000 whose permission bits are rw-------. What happens?
The kernel uses the owner bits when the process's user ID matches the file's owner. rw- includes read, so the open succeeds. The other bits only apply to users who are neither the owner nor in the file's group.
02Line 2 tries to open /etc/shadow without permission. What does the program see?
A failed permission check is an ordinary error: the call returns -1 and sets errno to EACCES (permission denied). The program can handle it and continue.
03Why sandbox a program that runs as an ordinary user, not as root?
The kernel checks who is asking, not why. A hijacked program still has its user's ID and so all of that user's rights. Least privilege says to give it only what it needs, for example just reading one already-open file.
04In seccomp strict mode, which system calls may the thread still make?
Strict mode allows exactly read, write, _exit (but not exit_group) and sigreturn. Any other call ends the thread with SIGKILL.
05Line 5 asks to open alice's private key, which alice's own permission bits allow. Why is the program killed anyway?
Seccomp checks the system call before the call itself runs, so no file lookup or permission check ever happens. The sandbox is stricter than the user's own rights, which is the whole point.
Glossary
- Protection
- The kernel's job of letting each program touch only what it is allowed to: its own memory, the files its user may use, and nothing else.
- Complete mediation
- Checking every access, every time. Because every request is a system call, the kernel sees and can check each one.
- Credentials
- The user and group IDs a process runs with. The kernel compares them with a file's owner and permission bits.
- Permission bits
- Nine bits on every Unix file: read, write and execute, for the owner, for the file's group, and for everyone else.
- Least privilege
- Give a program only the rights it needs for its job, so a bug or an attacker can do as little harm as possible.
- Sandbox
- A setup that runs a program with fewer rights than its user has, so that if it is taken over the damage stays inside.
- Seccomp
- Linux secure computing mode. Strict mode allows only read, write, _exit and sigreturn; filter mode runs a small program that decides call by call.
- Capabilities
- Linux splits root's power into about 40 separate rights, such as CAP_DAC_OVERRIDE (skip file permission checks), so a program can keep only the ones it needs.
Notes and sources
- The card shows the five lines of main() that matter; the whole program is real C99 for Linux. The 1 in prctl() is SECCOMP_MODE_STRICT from <linux/seccomp.h>. Line 5 stands for code an attacker slipped in through a bug; it is written plainly so the example runs.
- Real permission checks also look at groups (a process may belong to several), and Linux checks the filesystem user ID, which is normally the same as the effective user ID. The example leaves groups out: alice is not in the group of any file shown.
- The exact permission bits of /etc/shadow differ between distributions; shadow(5) only requires that regular users cannot read it. The figure shows just that.
- Strict mode is the oldest and bluntest seccomp mode. Real sandboxes (browsers, container runtimes, systemd services) use filter mode: a small BPF program that can allow, refuse with an error code, or kill, call by call. Its filters are inherited by child processes and survive execve.
- Root (user ID 0) is not checked against permission bits: Linux splits root's power into about 40 capabilities, and CAP_DAC_OVERRIDE alone skips the read, write and execute checks. Dropping capabilities is another way to apply least privilege.
- The kernel documentation is careful to say that system call filtering is not a sandbox on its own but a tool for building one; real sandboxes combine it with separate namespaces, dropped privileges and other limits.
- R. and A. Arpaci-Dusseau (eds.), OSTEP ch. 53: Introduction to Operating System Security (P. Reiher)
- OSTEP ch. 55: Access Control (P. Reiher)
- MIT 6.1810 (2024), lecture notes: OS design and isolation
- Berkeley CS162, Lecture 2: Four Fundamental OS Concepts
- Linux man-pages: seccomp(2)
- Linux man-pages: PR_SET_SECCOMP(2const)
- Linux kernel documentation: Seccomp BPF
- Linux source: include/uapi/linux/seccomp.h
- Linux man-pages: open(2)
- Linux man-pages: path_resolution(7)
- Linux man-pages: credentials(7)
- Linux man-pages: capabilities(7)
- Linux man-pages: shadow(5)
- Linux man-pages: namespaces(7)
Quiz Unlocks after the last step.