Containers are not a security boundary in themselves: they are ordinary Linux processes that the kernel shows a partitioned view of the system through namespaces and limits through cgroups. Knowing exactly what each namespace does and does not isolate is what separates a sound container or sandbox risk decision from a guess. Many escapes and noisy-neighbour outages trace back to a namespace that was shared with the host, a capability that reached further than expected, or a cgroup limit that was never set. This guide explains the eight namespace types, how user namespaces change the meaning of root and capabilities, what cgroups v1 and v2 control, which settings break isolation, and how to detect and measure it. It is for Linux, container platform and security engineers. For the basics of Linux processes and permissions, start with the Linux Fundamentals for Defenders Guide.
Key takeaways
- All containers on a host share one kernel: namespaces change what a process can see, not the kernel it talks to, so a kernel bug reachable from the container can cross every namespace.
- User namespaces change what root means: root inside a user namespace has full capabilities only over resources that namespace owns, and none in the parent; this is the strongest namespace control and the one most often left off.
- Some things are not namespaced at all: the kernel keyring, system time (outside the time namespace's limited clocks), kernel modules and many
/procand/syspaths are global, which is why seccomp, capabilities and LSMs are still needed. - Sharing host namespaces undoes isolation:
hostPID,hostNetwork,hostIPCand privileged mode each remove a layer, and together they amount to host access. - cgroups are a security control too: without memory and pids limits, one compromised workload can exhaust a node; on cgroup v1, the writable
release_agenthas been an escape path.
How identity and access work in namespaces
A namespace wraps a global resource so processes inside it see their own instance. Every process belongs to exactly one namespace of each type, visible as links under /proc/<pid>/ns/ (for example uts:[4026531838]); two processes are in the same namespace when the inode numbers match. Namespaces are created with clone(2) or unshare(2) and joined with setns(2), which is what nsenter and docker exec use.
| Namespace | Flag | Isolates | Does not isolate |
|---|---|---|---|
| Mount | CLONE_NEWNS | Mount table, so the container sees its own root filesystem | Files reachable through bind mounts or shared volumes; host paths mounted in |
| PID | CLONE_NEWPID | Process ID numbering and visibility | Processes in the host or a shared PID namespace, which can be signalled or inspected |
| Network | CLONE_NEWNET | Interfaces, routing, firewall rules, ports | Traffic once it leaves through a bridge; host network if shared |
| IPC | CLONE_NEWIPC | System V IPC and POSIX message queues | Shared memory through mounted files such as /dev/shm if shared |
| UTS | CLONE_NEWUTS | Hostname and NIS domain name | Nothing security-relevant beyond identity confusion |
| User | CLONE_NEWUSER | User and group IDs, and the scope of capabilities | Files on host filesystems are still checked against mapped host IDs |
| Cgroup | CLONE_NEWCGROUP | The view of the cgroup hierarchy (the cgroup root) | Resource limits themselves, which are set by the cgroup, not the namespace |
| Time | CLONE_NEWTIME | Boot-time and monotonic clocks (Linux 5.6 and later) | Wall-clock time: setting the system clock is still global |
User namespaces, UID mapping and capabilities
User namespaces are where namespace security becomes an identity question. According to the user_namespaces(7) manual:
- Capabilities are scoped: a process that creates or joins a user namespace gets a full capability set inside it but none in the parent, even if it was root before.
- Ownership decides reach: every non-user namespace is owned by a user namespace, and capabilities only allow privileged operations on resources governed by namespaces that user namespace owns. Loading kernel modules, setting the system time and creating devices still need privileges in the initial user namespace, and mounting block-device filesystems needs
CAP_SYS_ADMINin the initial namespace. - But some mounts open up:
CAP_SYS_ADMINinside a user namespace permits bind mounts and mounting proc, sysfs, tmpfs, mqueue, bpf and overlayfs. That is why unprivileged user namespaces expand the kernel code an unprivileged user can reach. - ID maps:
/proc/<pid>/uid_mapandgid_mapmap IDs inside to IDs outside, can be written once, and an unprivileged writer may only map its own ID. Unmapped IDs appear as the overflow ID (usually 65534). - Unprivileged creation: since Linux 3.8, any user can create a user namespace, and creating it together with other namespaces gives the creator privileges over those too. This is a deliberate design that many kernel exploits rely on, which is why distributions now offer controls to restrict it (Ubuntu does this through AppArmor, requiring a profile to permit user namespace use).
Creating any other namespace type normally needs CAP_SYS_ADMIN. Per-user limits under /proc/sys/user/ (max_user_namespaces, max_net_namespaces and so on) cap how many can be created; setting max_user_namespaces to 0 disables new user namespaces entirely, at the cost of breaking rootless containers and browser sandboxes that depend on them.
cgroups: resource limits and their security role
Control groups organise processes into a hierarchy and apply resource controllers. Modern distributions and Kubernetes use the unified cgroup v2 hierarchy; cgroup v1 still appears on older hosts.
| Control | cgroup v2 file | Security purpose |
|---|---|---|
| Process limit | pids.max | Contains fork bombs; forks beyond the limit fail with EAGAIN |
| Memory hard limit | memory.max | OOM kill inside the cgroup only, never outside it |
| Memory throttle | memory.high | Slows a runaway workload without killing it |
| CPU | cpu.max, cpu.weight | Stops cryptomining or loops starving neighbours (ATT&CK T1496 Resource Hijacking) |
| I/O | io.max | Limits disk bandwidth and IOPS |
| Device access | eBPF program of type BPF_CGROUP_DEVICE (no interface files in v2) | Controls which device nodes a container can create or open |
| Emergency stop | cgroup.kill, cgroup.freeze | Kill or freeze every process in a cgroup tree, useful in incident response |
- v1 release_agent: in cgroup v1 the hierarchy root has a
release_agentfile naming a program the kernel runs, as root on the host, when a cgroup withnotify_on_releaseset becomes empty. A container that can mount or write a v1 hierarchy can abuse it to run code on the host. v2 removes both files. Keep kernels patched against cgroup flaws as well as moving to v2. - Delegation: v2 lets a less privileged user manage a subtree by owning its
cgroup.procs,cgroup.threadsandcgroup.subtree_controlfiles, or automatically through thensdelegatemount option. A delegatee cannot escape its parent's limits, and moving a process needs write access to the common ancestor. - No limits on depth by default:
cgroup.max.depthandcgroup.max.descendantsdefault tomax, so delegated subtrees can create unlimited cgroups unless you cap them.
Admin roles and privileged access
Namespaces and cgroups have no administrator role of their own. Control comes from who holds capabilities in which user namespace, and who can talk to the components that create namespaces.
| Who or what | Power over isolation | Control |
|---|---|---|
| Host root | Can enter any namespace with nsenter, change any cgroup | Privileged access management for node logins. See the Unix and Linux Privileged Access Guide |
Container runtime socket (/var/run/docker.sock, containerd socket) | Can start a privileged container with host namespaces, which equals host root | Never mount runtime sockets into workloads; restrict socket group membership |
| Kubernetes users who can create pods | Can request hostPID, hostNetwork, hostIPC, privileged or hostPath | Pod Security Admission Baseline or Restricted; RBAC on pod creation. See the Kubernetes RBAC Guide |
Kubernetes users with pods/exec | Can run commands inside a container's namespaces (ATT&CK T1609) | Grant exec sparingly, audit its use |
Containers with CAP_SYS_ADMIN | Can mount, create namespaces and reach far more kernel code | Drop ALL capabilities and add back only what is needed. See the Linux Capabilities Guide |
Integrations, API tokens and service accounts
Namespaces decide which credentials a process can see. Breaking a namespace boundary is often a credential exposure first and a code execution problem second.
- Shared PID namespace exposes environment and memory: with
hostPID: trueor a shared process namespace, a sufficiently privileged process can read other processes'/proc/<pid>/environand command lines, where tokens and passwords are often passed. - Mount namespace and volumes: Kubernetes mounts the service account token and Secrets on tmpfs inside the pod's mount namespace. A
hostPathmount of/var/lib/kubeletexposes every pod's tokens on the node. See the Kubernetes Service Account Token Guide. - Network namespace and metadata services: pods in their own network namespace can still reach the cloud instance metadata endpoint unless blocked, and with
hostNetwork: truethey share the node's network identity. Block or restrict metadata access and use workload identity for cloud credentials. - Runtime and kubelet credentials: the kubelet's client certificate and the runtime socket live on the host. Anything that escapes to the host mount namespace can use them to act as the node.
- User namespaces reduce the payoff: with user namespaces, root in the container maps to an unprivileged host UID, so host files owned by root stay protected even after an escape from the mount namespace.
User namespaces in containers and Kubernetes
- Kubernetes: set
spec.hostUsers: falseto run a pod in its own user namespace. The kubelet picks non-overlapping host ID ranges per pod. TheUserNamespacesSupportfeature is beta and enabled by default from v1.33 and stable from v1.36. Requirements include idmap mount support on the filesystems used (in practice Linux 6.3 or later, because tmpfs gained idmap support then), runc 1.2 or crun 1.9 or later, and containerd 2.0 or CRI-O 1.25 or later. It cannot be combined withhostNetwork,hostIPCorhostPID. Kubernetes notes several HIGH or CRITICAL vulnerabilities were not exploitable with user namespaces active. - Docker:
userns-remapmaps container root to a high, unprivileged UID range from/etc/subuidand/etc/subgid, using adockremapuser when set todefault. The daemon still runs as root; rootless mode runs the daemon unprivileged as well. Enable remapping on a new installation, because existing images and containers are hidden once it is on.
Logging and detection
| What to detect | Source | Why |
|---|---|---|
Pods or containers created with hostPID, hostNetwork, hostIPC, privileged or sensitive hostPath | Kubernetes audit log, admission logs | Deliberate removal of isolation |
setns or nsenter into container namespaces from unusual parents | auditd or eBPF runtime sensors | Host-to-container or container-to-container movement |
unshare or clone with CLONE_NEWUSER inside containers | Runtime sensors, seccomp logs | Common first step in kernel exploit chains (T1068) |
Writes to release_agent or cgroup v1 mounts inside containers | auditd, runtime sensors | Known escape technique (T1611 Escape to Host) |
| Containers listing host processes or cgroup hierarchy | Runtime sensors | Container and resource discovery (T1613) |
Repeated OOM kills, pids.max hits, CPU throttling | memory.events, pids.events, node metrics | Resource abuse or denial of service (T1496, T1499) |
Metrics: percentage of production pods running with hostUsers: false or a user-namespaced runtime; number of pods using any host namespace (target: only named system components); percentage of nodes on cgroup v2; percentage of containers with memory and pids limits set; and count of containers with CAP_SYS_ADMIN. Runtime detection is covered in the Container Runtime Security Guide.
Hardening baseline
- Move to cgroup v2: confirm
/sys/fs/cgroupis a cgroup2 mount on every node; this removes release_agent and brings eBPF device control. - Patch the kernel: because every container shares it, kernel patching is the first container isolation control. Apply your distribution's kernel security updates promptly.
- Ban host namespaces and privileged mode: enforce Pod Security Admission Baseline everywhere and Restricted for application namespaces; allow exceptions only for named system DaemonSets.
- Drop capabilities and apply seccomp: drop ALL, add back only what is needed, and apply RuntimeDefault seccomp so
unshare,mountandsetnsstay blocked. See the seccomp Guide. - Enable user namespaces: pilot
hostUsers: falseon a node pool with a supported kernel, filesystem and runtime, then expand. Roll back per pod by removing the field. - Restrict unprivileged user namespaces on hosts that do not need them: use your distribution's control (an AppArmor restriction on Ubuntu, or
user.max_user_namespaces) on servers that run no rootless containers or sandboxed browsers. - Set resource limits: require memory and CPU limits through LimitRange or admission policy, and set a node-wide pod pids limit in the kubelet configuration.
- Add an LSM: SELinux or AppArmor confinement limits what an escaped process can touch. See the SELinux Guide.
- Use stronger isolation for untrusted code: where tenants are mutually hostile, use sandboxed runtimes that run each pod in a lightweight VM or a user-space kernel, or separate nodes.
Small teams running their own trusted workloads get most of the benefit from steps 1 to 4 and 7. Multi-tenant platforms, CI runners executing untrusted builds and AI agent sandboxes should treat namespaces as the minimum and add user namespaces, an LSM and VM-level isolation.
Common misconfigurations
- Running the Docker socket inside a container: CI agents and monitoring tools mount
docker.sock, giving anyone who controls the container the ability to start a privileged one. - Privileged containers for convenience:
privileged: truegrants all capabilities, all devices and no seccomp, so namespaces are the only remaining boundary and they are easy to leave. - hostPID or hostNetwork on application pods: added for debugging or performance and never removed, exposing other processes and the node's network identity.
- Writable hostPath mounts: mounting
/,/etc,/var/lib/kubeletor/procfrom the host defeats the mount namespace. - No resource limits: a compromised pod mines cryptocurrency or fork-bombs the node. Limits contain the damage but are not detection: in the Tesla Kubernetes cryptojacking incident attackers deliberately kept CPU use low so mining would not stand out, so watch per-workload usage trends as well.
- Assuming containers are VMs: placing mutually untrusted tenants on shared nodes with only namespace isolation.
Framework mapping
| Area | NIST SP 800-53 Rev. 5 | NIST CSF 2.0 | MITRE ATT&CK |
|---|---|---|---|
| Namespace and user namespace isolation | SC-39 Process Isolation | PR.PS-01 | T1611 |
| Capabilities and privileged pods | AC-6, CM-7 | PR.AA-05 | T1611, T1068 |
| Resource limits | SC-6 Resource Availability | PR.IR-04 | T1496, T1499 |
| Kernel patching | SI-2 | PR.PS-02 | T1068 |
| Monitoring namespace changes | AU-2, AU-12 | DE.CM-01 | T1609, T1613 |
Practitioner checklist
- Confirm every node runs cgroup v2 and a supported, patched kernel.
- Enforce Pod Security Admission so host namespaces, privileged mode and sensitive hostPath mounts are rejected outside named exceptions.
- Drop ALL capabilities by default and never grant CAP_SYS_ADMIN to application containers.
- Apply RuntimeDefault seccomp to every pod so unshare, mount and setns stay blocked.
- Pilot and then roll out hostUsers: false (or userns-remap or rootless mode for Docker hosts).
- Restrict unprivileged user namespace creation on servers that do not need it.
- Require memory, CPU and pids limits for every workload.
- Never mount container runtime sockets into workloads.
- Alert on host-namespace pods, nsenter into containers, user namespace creation in containers and cgroup release_agent writes.
Standards and references
- Linux man-pages: namespaces(7)
- Linux man-pages: user_namespaces(7)
- Linux man-pages: cgroups(7)
- Linux kernel documentation: Control Group v2
- Kubernetes: User Namespaces
- Docker Docs: Isolate containers with a user namespace
- Ubuntu: AppArmor (unprivileged user namespace restrictions)
- MITRE ATT&CK: Escape to Host
Related NHI Mgmt Group resources: Linux Fundamentals for Defenders Guide · Container Security Guide · seccomp Guide · Linux Capabilities Guide