AI agents are only useful when they can run code, reach networks and hold credentials, and that’s exactly what security teams don’t want to hand out. Agent sandboxes sit in the middle, and every one of them is built from a handful of Linux kernel features. This series is me learning those features one at a time, hands-on. I’m writing it up so I can come back to it, and in case it helps someone else coming from the AI side.
Part 1 is namespaces. Every command and output below is from my own lab. I worked through it with Claude as a tutor and ran every exercise myself.
The Big Idea: A Sandbox Is Just a Process
A sandbox isn’t a special kind of object. It’s an ordinary Linux process with restrictions stacked on it. A Docker container, a Kubernetes pod and an AI agent sandbox are all the same idea with different stacks. Each layer answers one question about the process:
| The process can… | Mechanism | This series |
|---|---|---|
| see other processes, network, mounts | namespaces | Part 1 (this post) |
| use CPU, memory, process count | cgroups | Part 2 |
| be root? which privileges? | users and capabilities | Part 3 |
| call which system calls | seccomp | Part 4 |
| touch which files | Landlock | Part 5 |
| gain more privilege later | no_new_privs | Parts 3–4 |
When I looked inside a real agent sandbox (OpenShell), the process ran as user 1000, /proc/self/status showed Seccomp: 2 and NoNewPrivs: 1, and an outbound connection was denied and logged. The goal of this series is to be able to build and explain each of those by hand.
What a Namespace Is
The kernel keeps lists: the list of processes, the list of network interfaces, the list of mounted filesystems, the list of user IDs. A namespace gives a process its own copy of one of those lists.
Nothing is blocked. The process just can’t name anything that isn’t in its list. If a process isn’t in your process list, you can’t see it, and you can’t send it a signal, because you have no way to refer to it.
Inside the kernel, every process has a small record with one pointer per namespace type. Each pointer leads to one list. When a process asks “what processes exist?” or “what network interfaces do I have?”, the kernel answers only from the list its pointer leads to:
process "bash" ──┬── pid pointer ──► PID namespace A { sleep, bash, ps }
├── net pointer ──► net namespace X { lo, eth0 }
└── mnt pointer ──► mount namespace M { /, /proc, ... }
after unshare --pid --net:
process "ps" ──┬── pid pointer ──► PID namespace B { ps } ← new, empty
├── net pointer ──► net namespace Y { lo } ← new, empty
└── mnt pointer ──► mount namespace M ← still shared
A useful analogy is phone books. Everyone using the same phone book can look each other up. Hand someone a new, blank phone book and they can’t find anyone, even though everyone still exists.
There are eight kinds of namespace:
| Namespace | Isolates | Why it matters for agents |
|---|---|---|
pid | process IDs | the agent can’t see or signal other processes, or read their command lines and environments |
net | network interfaces, routes, ports | a new one has no way out: deny-by-default networking |
mnt | the mount table | the agent gets its own view of the filesystem tree |
user | user and group IDs | “root” inside can be an unprivileged user outside |
uts | hostname | mostly cosmetic |
ipc | shared memory, message queues | no side channel to other processes |
cgroup | view of the cgroup tree | hides resource layout |
time | clocks | rarely matters |
Three verbs cover how processes relate to namespaces:
- Inherit: the default. A new process copies its parent’s pointers, so it sees the same lists.
- Unshare (“stop sharing”): give a process new, empty namespaces instead. The
unsharecommand wraps theunshare()system call that container runtimes use. - Enter: point a process at another process’s existing namespace, with
nsenter. This needs a powerful privilege, which matters later.
The Lab
My Mac doesn’t run Linux, so the lab is an Ubuntu container on Docker Desktop, which runs a Linux VM in the background. The container is privileged, because creating namespaces needs a powerful kernel permission (CAP_SYS_ADMIN). In the lab I’m playing the part of the container runtime. An agent should never get this.
docker run -d --name iso-lab --hostname lab --privileged ubuntu:24.04 sleep infinity
docker exec iso-lab bash -c 'apt-get update -qq && apt-get install -y -qq procps iproute2 util-linux'
docker exec -it iso-lab bashOne thing confused me at first: what “normal” means in the lab. Everything runs inside the container, and the container is already namespaced. Docker made those namespaces, and my shell inherits them. The unshare command makes a new namespace nested inside:
my Mac (macOS, no Linux namespaces)
└── Docker Desktop's Linux VM ← the real Linux kernel
└── iso-lab container ← Docker already gave it its own namespaces
└── unshare ... ← a fresh one nested inside, made by me
Exercise 1: A PID Namespace
How many processes can I see, normally and in a new PID namespace?
root@lab:/# ps -e | wc -l
5
root@lab:/# unshare --pid --fork --mount-proc ps -e
PID TTY TIME CMD
1 pts/0 00:00:00 ps
The container shows 5 lines: four processes plus the header. In the new PID namespace, ps is alone, as PID 1. The new namespace isn’t a filtered view of the container’s processes. It’s a brand-new, empty list.
Two flags matter:
--fork: the process that callsunsharestays in the old PID namespace, and only its children enter the new one.--forkmakes the command a child, so it lands inside as PID 1.--mount-proc:psreads its list from/proc, so/prochas to be remounted to show the new namespace.
Exercise 2: A Network Namespace
What network interfaces exist, normally and in a new network namespace?
root@lab:/# ip -brief link
lo UNKNOWN <LOOPBACK,UP,LOWER_UP>
eth0@if22 UP <BROADCAST,MULTICAST,UP,LOWER_UP>
root@lab:/# unshare --net ip -brief link
lo DOWN <LOOPBACK>
(Here and below, I’ve trimmed the MAC-address column and a list of tunnel interfaces that show up everywhere, all switched off. They’re kernel stubs, not a way out.)
The container has eth0, its way out. The new namespace has no eth0 at all, and even its loopback is switched off. Trying to connect out confirms it:
root@lab:/# unshare --net bash -c 'exec 3<>/dev/tcp/1.1.1.1/80 && echo OPEN || echo NO ROUTE'
bash: connect: Network is unreachable
NO ROUTE
The kernel refused instantly, because there’s no route anywhere.
So how does a container get a network? eth0@if22 is one end of a veth pair, a virtual cable: two network interfaces created together, where whatever goes in one end comes out the other. Docker created an empty network namespace for the container, put one end of the cable inside (named eth0), and plugged the other end into a virtual switch in the VM (docker0). The @if22 means the far end is interface number 22 on the other side, and you can check it:
root@lab:/# cat /sys/class/net/eth0/iflink
22
container's net namespace VM's net namespace
┌──────────────┐ ┌───────────────────────────────┐
│ eth0 ●═════╪══ veth cable ════╪══● if22 ── docker0 ── out │
└──────────────┘ └───────────────────────────────┘
a new unshare --net namespace: no cable at all → "Network is unreachable"
The starting point for any sandbox is no network at all. Every way out is a decision someone made, and that decision point is exactly where the rules go.
Isolation Versus Enforcement
That “Network is unreachable” was different from what I’d seen inside the OpenShell agent sandbox. There, a connection failed with Permission denied, and the sandbox logged it:
NET:OPEN [MED] DENIED /usr/bin/bash(391) -> example.com:443 [reason:transparent_tcp_policy_denied]
| Isolation | Enforcement | |
|---|---|---|
| Example | my empty network namespace | the OpenShell sandbox |
| Error | Network is unreachable | Permission denied |
| What exists | no path | a path, a rule, and a log |
| Useful for agents? | all-or-nothing; an agent with no network can’t do much | yes: allow what’s needed, deny the rest, and record what was tried |
Real agent sandboxes need enforcement. They start from isolation (an empty namespace), then add a controlled way out (a proxy) with rules and a log. The log answers the question security teams care about: what did the agent try to do?
Exercise 3: A User Namespace
Can an unprivileged user become root?
root@lab:/# su -s /bin/bash nobody -c 'id; unshare --user --map-root-user id'
uid=65534(nobody) gid=65534(nogroup) groups=65534(nogroup)
uid=0(root) gid=0(root) groups=0(root)
Outside, nobody. Inside a new user namespace, root. But try something only real root can do:
root@lab:/# su -s /bin/bash nobody -c 'unshare --user --map-root-user cat /etc/shadow'
cat: /etc/shadow: Permission denied
The kernel checks who the process really is outside the namespace, and outside it’s still nobody. “Root inside” is a label that only means something inside its own namespace. It lets a normal user do root-like setup there, like creating more namespaces, without power over the real system. This is how rootless containers work. Note also that nobody created this namespace without being root: user namespaces are the exception to the privilege rule.
Exercise 4: Break It. What Namespaces Don’t Do
Here’s a process with its own process list and no network. Can it read a protected file?
root@lab:/# unshare --pid --net --fork --mount-proc bash -c 'ps -e; ip -brief link; head -2 /etc/shadow'
PID TTY TIME CMD
1 pts/0 00:00:00 bash
2 pts/0 00:00:00 ps
lo DOWN <LOOPBACK>
root:*:20767:0:99999:7:::
daemon:*:20767:0:99999:7:::
Isolated processes, no network, and it still read the password file. Namespaces only isolate the kinds of things they’re named for. I created PID and network namespaces, so nothing here controlled file reads. The container does have its own filesystem, so it can’t see my Mac’s files, but inside it, root can read anything root can read.
This is the difference between can’t see and can’t touch, which are two separate checks in the kernel:
- See: will the kernel look the thing up for you? That’s namespaces.
- Touch: will the kernel let you act on what it found? That’s file permissions, capabilities, seccomp and Landlock.
/etc/shadow was both visible and touchable here. In the OpenShell sandbox, /etc was visible, but writing to it was denied. Hiding does remove touch, but only for that kind of thing: you can’t kill a process you can’t name, but a PID namespace does nothing for files. That’s why the other layers exist. Don’t assume “it’s in a container” means “it can’t read secrets.”
Exercise 5: Joining a Namespace, and the Exercise That Fooled Me
nsenter (“namespace enter”) runs a command inside a namespace that already exists, by borrowing another process’s pointer. My first try looked like it worked:
root@lab:/# unshare --net sleep 1000 &
root@lab:/# nsenter --target $! --net ip -brief link
lo UNKNOWN <LOOPBACK,UP,LOWER_UP>
eth0@if22 UP <BROADCAST,MULTICAST,UP,LOWER_UP>
But that’s the container’s network: eth0 is there and the loopback is on. I didn’t enter the new namespace at all. nsenter ran a split second before unshare had created it, so it joined the old one.
The fix is to check the evidence. Every namespace has an ID number, which you can read from /proc. Same number means same namespace:
root@lab:/# unshare --net sleep 1000 & sleep 1
root@lab:/# readlink /proc/self/ns/net /proc/$!/ns/net
net:[4026534101]
net:[4026534359]
root@lab:/# nsenter --target $! --net ip -brief link
lo DOWN <LOOPBACK>
Two different numbers: now the sleep process really is in its own namespace, and nsenter lands in the empty one. The command “worked” the first time, printed believable output, and measured the wrong thing. The lesson is the same one I learned red-teaming agents: check the evidence, not the output. (Security people call this kind of timing gap TOCTOU, “time of check to time of use.“)
Joining a namespace needs CAP_SYS_ADMIN, the same powerful permission as creating most of them. If an agent’s process had it, it could join the host’s namespaces and walk straight out of its sandbox. The namespaces around an agent matter less than the agent’s own privileges, which is what Part 3 covers.
What I Took Away
- A namespace controls what a process can see. New ones start empty. They’re not filtered views.
- An empty network namespace is deny-by-default. Every way out is a cable someone plugged in.
- Isolation isn’t enough for agents. They need enforcement: a path, a rule and a log.
- “Root” inside a user namespace isn’t root outside.
- Namespaces hide, they don’t protect. Files need other layers.
- Privilege beats namespaces. An agent that can join namespaces can leave.
- Check the evidence. Namespace IDs, not output that looks right.
Next: Part 2, cgroups, the layer that controls what a process can use: CPU, memory and how many processes it can start. That’s what stops a runaway agent, or an injected fork bomb, from taking down the machine.
Sources and Further Reading
- namespaces(7), unshare(1), nsenter(1), user_namespaces(7) (man pages)
- Ken Muse, “Building Container Isolation From the Linux Kernel Up”: the same
unsharedemos plus UTS (hostname) and mount namespaces, a one-line “DIY container”, and a hands-on intro to cgroups. A good companion to this post. - OpenShell, the agent sandbox I looked inside.