| name | docker-user-namespace |
| description | How to work with Linux user namespaces for Docker security testing and privilege escalation analysis. Use this skill whenever the user mentions user namespaces, UID/GID mapping, container isolation, Docker security, namespace enumeration, or wants to understand how user namespaces work for privilege escalation. This includes creating namespaces, checking mappings, entering namespaces, and understanding capability implications. |
Docker User Namespace Security
A skill for working with Linux user namespaces in Docker security testing and privilege escalation scenarios.
What User Namespaces Do
User namespaces provide isolation of user and group ID mappings, allowing each namespace to have its own set of UIDs/GIDs. This enables:
- Processes in different namespaces to have different privileges despite sharing the same numeric IDs
- Containers to run with root-like capabilities (UID 0) inside the namespace without full root on the host
- Fine-grained control over privileges through restricted ID mapping ranges
Key Commands
Create a User Namespace
sudo unshare -U /bin/bash
sudo unshare -U --mount-proc /bin/bash
sudo unshare -fU /bin/bash
sudo unshare -U --map-user=nobody /bin/bash
sudo unshare -U --map-current-user /bin/bash
Check Your Current Namespace
ls -l /proc/self/ns/user
cat /proc/self/uid_map
cat /proc/<pid>/uid_map
Find All User Namespaces on Host
sudo find /proc -maxdepth 3 -type l -name user -exec readlink {} \; 2>/dev/null | sort -u
sudo find /proc -maxdepth 3 -type l -name user -exec ls -l {} \; 2>/dev/null | grep <ns-number>
Enter Another User Namespace
nsenter -U <TARGET_PID> --pid /bin/bash
nsenter --user /proc/<pid>/ns/user --pid /bin/bash
Docker User Namespace Configuration
docker run -ti --name ubuntu1 -v /usr:/ubuntu1 ubuntu bash
DOCKER_OPTS="--userns-remap=default"
sudo service docker restart
Security Implications
Capability Recovery
When entering a new user namespace, you regain all capabilities (CapEff: 000001ffffffffff) within that namespace. However:
- You can only use capabilities related to the namespace (e.g., mount filesystems)
- You cannot use capabilities that affect the host or other namespaces
- This alone is not sufficient for Docker container escape
ID-Mapped Mounts
ID-mapped mounts attach user namespace mappings to mounts, remapping file ownership when accessed through that mount:
- Does not change on-disk ownership
- Makes files appear owned by your mapped UID/GID within the namespace
- Requires
CAP_SYS_ADMIN in your user namespace
- Useful for rootless containers to share host paths without recursive
chown
Unprivileged UID/GID Mapping Rules
When writing to uid_map/gid_map without CAP_SETUID/CAP_SETGID in the parent namespace:
- Only a single mapping is allowed for the caller's effective UID/GID
- For
gid_map, you must first disable setgroups(2):
cat /proc/self/setgroups
echo deny > /proc/self/setgroups
Common Patterns
Pattern 1: Enumerate Namespaces
for ns in $(find /proc -maxdepth 3 -type l -name user 2>/dev/null); do
readlink "$ns"
echo "---"
done | sort -u
Pattern 2: Check Namespace Isolation
cat /proc/self/uid_map | grep "^0[[:space:]]*0[[:space:]]"
Pattern 3: Test Capability Scope
unshare -U --mount-proc /bin/bash
cat /proc/self/status | grep Cap
Troubleshooting
"Cannot allocate memory" Error
When unshare fails with bash: fork: Cannot allocate memory:
Cause: The process creating the namespace doesn't enter it; only children do. When PID 1 exits, the namespace cleans up and disables PID allocation.
Solution: Use the -f flag to fork after creating the namespace:
unshare -p /bin/bash
unshare -fp /bin/bash
Cannot Enter Namespace
Requirements:
- You must be root to enter another process's namespace
- You need a descriptor pointing to the namespace (e.g.,
/proc/self/ns/user)
References
When to Use This Skill
Use this skill when:
- Testing Docker container security or isolation
- Analyzing privilege escalation paths through namespaces
- Understanding UID/GID mapping in containers
- Enumerating namespaces on a Linux system
- Working with user namespace capabilities and limitations
- Troubleshooting namespace-related errors