A program that cannot see the rest
A Linux namespace gives a process its own private view of one thing the kernel shares, such as the processes or the network. Containers are built from them.
That matters because code that cannot see your network or your files cannot steal from them. Today you make each kind by hand with the unshare command and watch what disappears.
By the end of this lesson
Note
I can use unshare to hide processes, the network and files from a program.
Key words
| Word | What it means | Example |
|---|---|---|
| Namespace | a private copy of one kernel view for some processes | |
| unshare | a command that runs a program in new namespaces | unshare --net |
| PID 1 | the first process in a PID namespace | |
| Mount | attaching a file system at a folder | |
| tmpfs | an empty file system kept in memory | |
| ID map | how user numbers inside match numbers outside |
The cells students fill in are left blank in this preview.
One namespace for each kind of view
Linux has eight kinds of namespace. Five matter most for a sandbox. pid gives a new list of processes. net gives a new network with only a loopback device, switched off. mnt gives a separate set of mounts, so files can be hidden or swapped. uts gives a separate machine name. user maps user numbers, so root inside can be nobody outside.
Each unshare option makes one kind. Every Run here uses sudo, as making most namespaces needs root.
Go to the lab folder
cd ~/labShown here, not run. Press Start lab to type it into your own lab’s terminal.
List processes in a new PID namespace
sudo unshare --pid --fork --mount-proc ps -e -o pid,commShown here, not run. Press Start lab to type it into your own lab’s terminal.
Rename the machine inside only
sudo unshare --uts sh -c 'hostname sandbox && hostname'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check the name outside
hostnameShown here, not run. Press Start lab to type it into your own lab’s terminal.
The network and the files can vanish
A new network namespace has no route anywhere. Asking for a route gives Network is unreachable, which is not the Connection refused of lesson 1: there is no network at all.
A mount namespace starts as a copy of the mounts outside, but changes stay inside. Mounting an empty tmpfs over the secrets folder hides the key from everything in that namespace. Outside, the key is still there.
Ask for a route with no network
sudo unshare --net ip route get 1.1.1.1Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Hide the secrets folder
sudo unshare --mount sh -c 'mount -t tmpfs none /home/student/lab/secrets && ls -A /home/student/lab/secrets'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check the key outside
cat secrets/api_key.txtShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think a namespace is a security boundary on its own. It hides things, but every namespace still shares one kernel. A kernel bug reached from inside reaches the whole machine, so namespaces are one fence among several.
A user namespace changes who root is
A user namespace maps user numbers. The file /proc/self/uid_map has three numbers a line: the first number inside, the first number outside and how many follow.
Outside, the map covers everyone unchanged. In the Run below, root inside is user 100000 outside, a user who owns nothing. Root inside can still make mounts and namespaces, but the files outside see an ordinary stranger.
Read the ID map outside
cat /proc/self/uid_mapShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read the ID map inside a user namespace
sudo unshare --user --map-users=0:100000:65536 --map-groups=0:100000:65536 cat /proc/self/uid_mapShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: what each namespace hid
| Namespace | The Run printed | What it hides from the agent |
|---|---|---|
| pid | PID 1 ps, alone | Every other process |
| uts | sandbox, while outside kept its name | The machine's real name |
| net | Network is unreachable | Every network, so call fails |
| mnt | an empty secrets folder | The key, with a tmpfs on top |
| user | 0 100000 65536 | Root's power over files outside |
The cells students fill in are left blank in this preview.
Try it: choose namespaces for the agent
# Which namespace hides which thing from a sandboxed program?
hides = {
"pid": "the other processes",
"net": "the network",
"mnt": "the files, by a different set of mounts",
"uts": "the machine's name",
"user": "who is root",
}
wanted = ["net", "mnt"]
for kind in wanted:
print(kind, "hides", hides[kind])
# Which one would stop agent.py call? Add "pid" and run it again.