CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 5 of 7 · 2 min read

User namespaces

Think of a play in which an actor plays a king. On the stage the actor gives orders, and the stage hands obey. Outside the theatre the actor is an ordinary person with an ordinary key to an ordinary flat. The analogy stops in one place: the stage hands are also real people, and the kernel enforces that the role stops at the stage door.

In plain words: A user namespace has its own table of user and group IDs and a map from IDs inside to IDs outside. A process can be root (ID 0) inside while the map sends 0 to an ordinary user outside. It gets all capabilities inside the namespace, over the resources that the namespace owns (its host name, its mounts, its network), and none over resources owned by the parent. This lets a normal user create namespaces and run a container without being root on the host (rootless containers).

A worked example. The user nobody (65534) creates a user namespace, and a UTS namespace in it, with --map-root-user:

$ setpriv --reuid=65534 --regid=65534 --clear-groups unshare --user --map-root-user sh -c '...'
uid=0(root) gid=0(root) groups=0(root)
         0      65534          1                  (/proc/self/uid_map)
cat: /etc/shadow: Permission denied
hostname: you must be root to change the host name
CapEff: 000001ffffffffff

Inside, the process is root with 41 capabilities (the mask 1ffffffffff, compare page 4 of Level 10). The map line says "inside 0 is outside 65534, a range of 1". But the file /etc/shadow still refuses it, since the file's owner is outside root and the process is really nobody there. The hostname command failed in the first test because it had no UTS namespace of its own: the host's host name belongs to the parent, and root of the user namespace has no power over it. In a second run with --uts added, hostname inside worked, and the host name seen outside stayed vm.

The key lines. Capabilities are relative to a user namespace. The power is real only over what that namespace owns.

User namespace

A user namespace is a namespace with its own user and group IDs, linked to the parent's IDs by a map. A process that is root in it holds capabilities only for the resources that the namespace owns, and has the ordinary rights of its mapped user on everything else.

Example: nobody becomes root inside a user namespace, and cannot read /etc/shadow.

Common mistake

Thinking that "root in a container" is root on the host. With a user namespace it is not. Without one (the default in many setups), the container's root is the host's real user 0, restricted only by capabilities, filters and policy (Level 10), so those limits matter.

Check yourself: 1. What does a user-namespace map contain? 2. Why could the namespace's root not read /etc/shadow? 3. Why did hostname fail until a UTS namespace was added?