| name | mount-namespace |
| description | How to work with Linux mount namespaces for file system isolation and privilege escalation. Use this skill whenever the user mentions mount namespaces, namespace isolation, file system isolation, container security, or needs to create/enter/inspect mount namespaces. Also use when debugging namespace-related errors like "Cannot allocate memory" with unshare. Make sure to use this skill for any Linux security research involving namespaces, container escape scenarios, or when the user wants to understand how processes can have different views of the file system. |
Mount Namespace Operations
Mount namespaces provide isolation of file system mount points between processes. Each namespace has its own view of the file system hierarchy, and changes in one namespace don't affect others.
When to Use This Skill
Use this skill when:
- You need to create isolated file system views for security testing
- You're researching container escape or privilege escalation techniques
- You need to understand how processes can have different file system views
- You're debugging namespace-related errors
- You want to isolate sensitive mounts from the host system
Core Concepts
How Mount Namespaces Work
- Inheritance: New namespaces start with a copy of the parent's mount points
- Isolation: Mount/unmount operations are local to the namespace
- Shared Resources: File descriptors and inodes are shared across namespaces
- Movement: Processes can move between namespaces using
setns(), unshare(), or clone()
Key System Calls
unshare() - Create a new namespace
setns() - Move to an existing namespace
clone() with CLONE_NEWNS - Create namespace with new process
Creating Mount Namespaces
Using unshare (CLI)
sudo unshare -m /bin/bash
sudo unshare -m --mount-proc /bin/bash
sudo unshare -mf /bin/bash
Important: Use -f flag when creating namespaces to fork a new process. Without it, you may encounter "Cannot allocate memory" errors because the unshare process doesn't enter the new namespace, and when PID 1 exits, the namespace cleans up and disables PID allocation.
Using Docker
docker run -ti --name container-name -v /host/path:/container/path ubuntu bash
Inspecting Namespaces
Check Current Namespace
ls -l /proc/self/ns/mnt
Find All Mount Namespaces
sudo find /proc -maxdepth 3 -type l -name mnt -exec readlink {} \; 2>/dev/null | sort -u
sudo find /proc -maxdepth 3 -type l -name mnt -exec ls -l {} \; 2>/dev/null | grep <ns-number>
findmnt
Entering Mount Namespaces
Using nsenter
nsenter -m TARGET_PID --pid /bin/bash
nsenter -t /proc/TARGET_PID/ns/mnt -m /bin/bash
Requirements:
- You must be root to enter another process's namespace
- You need a descriptor pointing to the namespace (like
/proc/self/ns/mnt)
Mounting in Namespaces
Create and Mount in Isolated Namespace
unshare -m /bin/bash
mkdir /tmp/mount_ns_example
mount -t tmpfs tmpfs /tmp/mount_ns_example
mount | grep tmpfs
echo test > /tmp/mount_ns_example/test
ls /tmp/mount_ns_example/test
Verify Isolation from Host
mount | grep tmpfs
ls /tmp/mount_ns_example/test
Bind Mount Example
unshare --mount
mount --bind /usr/bin/ /mnt/
ls /mnt/cp
exit
ls /mnt/cp
Security Considerations
Why This Matters for Privilege Escalation
- Hidden Mounts: A namespace can contain sensitive information only accessible from within it
- File Descriptor Sharing: Open file descriptors can be passed between namespaces, allowing access to files even if the path doesn't exist in your namespace
- Container Escape: Understanding mount namespaces is crucial for container escape research
- Isolation Testing: Verify that containers properly isolate their file systems
Common Attack Vectors
- Finding processes in different mount namespaces with sensitive mounts
- Using file descriptor passing to access files across namespaces
- Exploiting improper namespace isolation in containers
- Leveraging namespace entry to access hidden filesystems
Troubleshooting
"Cannot allocate memory" Error
Problem: When running unshare -p /bin/bash without -f flag
Cause: The unshare process doesn't enter the new PID namespace. When the first child (PID 1) exits, the namespace cleans up and disables PID allocation.
Solution: Use the -f flag to fork:
unshare -fp /bin/bash
Permission Denied
Problem: Cannot enter another namespace
Cause: You need root privileges to enter another process's namespace
Solution: Run with sudo or ensure you have appropriate capabilities
Namespace Not Found
Problem: Cannot find namespace descriptor
Cause: The namespace file doesn't exist or the process has exited
Solution: Verify the target process is still running:
ls -l /proc/TARGET_PID/ns/mnt
Practical Use Cases
1. Testing Container Isolation
unshare -m --mount-proc /bin/bash
mount | grep tmpfs
2. Accessing Hidden Files
sudo find /proc -maxdepth 3 -type l -name mnt -exec readlink {} \; 2>/dev/null | sort -u
nsenter -m TARGET_PID --pid /bin/bash
ls -la /
3. Creating Secure Environments
unshare -m --mount-proc /bin/bash
mount -t proc proc /proc
mount -t sysfs sysfs /sys
References