CoursesDocker from the kernel up
Puzzle 5 · 6 of 17
Root in the container is root on the host
Puzzle 4's table showed one link that matched the host's, user. Here's what that one equal number means when a container's program touches a file on the host, and the namespace that would change it.
All puzzles in this course
The problem
Puzzle 4 found every link of the container's process different from the host shell's, except one: user. Most images run their program as root, and inside the container that looks harmless: the root of a fenced-off box. But the kernel that checks every file access is the host's. When that program writes to a file, whose root does the kernel think it is?
See it
Scroll sideways to see the full diagram.
The kernel decides what a process may do by its UID. This host file belongs to UID 0, and only its owner may write it. UID 0 is the root user, whom the kernel lets past file permission checks.
Step 1 of 5By default, a container's program runs as UID 0, and Docker adds no fence for user IDs: the host sees UID 0 too. Predict: through a mounted folder, can it change notes.txt?
Step 2 of 5Yes: the write went through. To the kernel this is root, the same root as the host's. Only the other fences stand between it and the host.
Step 3 of 5Now the same program in a user namespace: a fence for user IDs. Inside, it is still UID 0. Predict: is the write allowed now?
Step 4 of 5No. The namespace's UID map says inside 0 is outside 1000, and the kernel checks host files as 1000: Permission denied.
Step 5 of 5
The building block
Every process carries a UID, a user ID number, and every file records the UID of its owner and who may read or write it. On each access, the kernel compares the two. The root user is the user with UID 0. Linux splits root's power into pieces called capabilities; one of them, CAP_DAC_OVERRIDE, lets a process past file permission checks, whatever the file says. A process of root's normally holds them all, so on a Linux machine root can change any file.
This is a second meaning of root. Puzzle 3's root filesystem is the top of the file tree, /; the root user is UID 0, the user at the top of the permissions. Same word, two things; in this puzzle root always means the user.
Look at the container's sleep from both sides:
$ docker exec box id -u
0
$ ps -eo user,uid,pid,cmd | grep "[s]leep 1000"
root 0 3393 sleep 1000
id -u prints the UID of the process that runs it. Unless the image sets a user, Docker runs its program as root: 0 inside, and 0 on the host, one number, because there's no second user namespace in between. The fences of puzzles 2 to 4 keep this root from seeing the host's processes, files and network, but they don't change who it is. The moment a host file gets into the container's tree, through a host folder mounted in with docker run's -v option (Module 5, coming soon), the kernel checks it against UID 0. On the run behind this page, a container changed a host file that belonged to UID 1234 and that only its owner may write: no error.
Docker does trim root inside a container: by default it removes many of root's capabilities, such as loading code into the kernel (Module 7, coming soon). DAC_OVERRIDE ("Bypass file read, write, and execute permission checks", in Docker's list of default capabilities) is one it keeps, as the run shows. Remove it with --cap-drop DAC_OVERRIDE, and the same write as root gets Permission denied.
A user namespace is the fence for user IDs. A process can have UID 0 inside one and an ordinary UID outside it. Its UID map says which outside UID each inside UID really is, one range per line: inside start, outside start, how many. The run behind this page made one, as the ordinary user 1000, with unshare --user --map-root-user:
$ id -u
1000
$ unshare --user --map-root-user sh -c 'id -u; cat /proc/self/uid_map; echo changed > /tmp/secret/notes.txt'
0
0 1000 1
sh: 1: cannot create /tmp/secret/notes.txt: Permission denied
Inside, the shell is UID 0; the map says that 0 is 1000 outside; the kernel checks the root-only file as 1000 and refuses. Inside the namespace it is root over what the namespace owns; to the host it is user 1000.
Docker can do the same for every container: its userns-remap setting maps each container's root to an unprivileged range of host UIDs. Docker's documentation calls it "available but not enabled by default", and warns that it adds "configuration complexity" when a container needs host resources such as mounted folders. The simpler fix, which works with or without it, is not to run as root inside at all: start the container with --user 1000, or set a non-root user in the image. On the run behind this page, the same write as UID 1000 got "Permission denied".
The analogy
The building has one staff list, and the caretaker checks badges against it. A flat's head of household wears a badge reading "No. 0", the number on the building's master key. Frosted windows don't change the badge: if a door from the building opens into the flat, the caretaker sees badge No. 0 and lets him through. A user namespace is a flat with its own badge office: inside, he is No. 0 of the flat; at any building door, his badge is translated to No. 1000, which opens nothing special.
Run it yourself
On a Linux machine with Docker, as the administrator. The first two lines make a host file that belongs to UID 1234, a user that doesn't exist on the machine, and that only its owner may write: only a process past the permission checks can change it. Before each docker run, predict whether the write succeeds.
mkdir -p /tmp/secret && echo "only its owner may change this" > /tmp/secret/notes.txt
chown 1234:1234 /tmp/secret/notes.txt && chmod 600 /tmp/secret/notes.txt
docker run --rm -v /tmp/secret:/data alpine:3.20 sh -c 'echo changed > /data/notes.txt'; cat /tmp/secret/notes.txt
docker run --rm --cap-drop DAC_OVERRIDE -v /tmp/secret:/data alpine:3.20 sh -c 'echo again > /data/notes.txt'
docker run --rm --user 1000 -v /tmp/secret:/data alpine:3.20 sh -c 'echo again > /data/notes.txt'
chown 1234:1234 makes UID 1234 the file's owner, chmod 600 lets only the owner read or write it, and -v /tmp/secret:/data mounts the host folder at /data inside. Output from the run behind this page:
changed
sh: can't create /data/notes.txt: Permission denied
sh: can't create /data/notes.txt: Permission denied
The first container, root with DAC_OVERRIDE, changed the file; the second, root without it, and the third, UID 1000, were refused. To try the user namespace without Docker, run the unshare command above as an ordinary user, not as root. On Ubuntu 23.10 and later, a security setting can forbid ordinary users to create user namespaces, and then unshare fails. This command lifts the restriction until the next restart:
sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
Check yourself
Sources
- Linux manual: capabilities(7) ("traditional UNIX implementations distinguish two categories of processes: privileged processes (whose effective user ID is 0, referred to as superuser or root), and unprivileged processes (whose effective UID is nonzero). Privileged processes bypass all kernel permission checks"; "Starting with Linux 2.2, Linux divides the privileges traditionally associated with superuser into distinct units, known as capabilities, which can be independently enabled and disabled."; "CAP_DAC_OVERRIDE Bypass file read, write, and execute permission checks.")
- Linux manual: user_namespaces(7) ("a process can have a normal unprivileged user ID outside a user namespace while at the same time having a user ID of 0 inside the namespace; in other words, the process has full privileges for operations inside the user namespace, but is unprivileged for operations outside the namespace."; "Each line in the uid_map file specifies a 1-to-1 mapping of a range of contiguous user IDs between two user namespaces.")
- OCI runtime spec v1.3.0: config-linux.md, Namespaces ("If a namespace type is not specified in the namespaces array, the container MUST inherit the runtime namespace of that type.")
- Docker docs: Docker Engine security ("As of Docker 1.10 User Namespaces are supported directly by the docker daemon. This feature allows for the root user in a container to be mapped to a non uid-0 user outside the container, which can help to mitigate the risks of container breakout. This facility is available but not enabled by default."; "By default, Docker starts containers with a restricted set of capabilities.")
- Docker docs: Running containers, runtime privilege and Linux capabilities ("The following table lists the Linux capability options which are allowed by default and can be dropped."; "DAC_OVERRIDE Bypass file read, write, and execute permission checks.")
- Ubuntu blog: Restricted unprivileged user namespaces are coming to Ubuntu 23.10 ("sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0")
- Docker docs: Isolate containers with a user namespace ("This re-mapping is transparent to the container, but introduces some configuration complexity in situations where the container needs access to resources on the Docker host, such as bind mounts"; "The best way to prevent privilege-escalation attacks from within a container is to configure your container's applications to run as unprivileged users. For containers whose processes must run as the root user within the container, you can re-map this user to a less-privileged user on the Docker host.")
- The id, ps, docker run and unshare outputs were produced in this session with Docker Engine 29.9.2 and util-linux 2.39.3 on the test machine (Ubuntu 24.04 userland in a Firecracker microVM, custom kernel 6.18.44, cgroup v1); the unshare command ran as UID 1000 (through setpriv). The container's runtime configuration listed the namespaces mount, network, time, uts, pid and ipc, and no user namespace.
What you now know
The kernel decides what a process may do by its UID, and UID 0, root, passes its permission checks. Docker gives a container no user namespace by default, so its root is UID 0 on the host too, and could change a root-only host file through a mounted folder. A user namespace maps UID 0 inside to an ordinary UID outside; a non-root user inside the image avoids the problem without one.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.