Search K
Appearance
Appearance
Other ways to support HackTricks:
A network namespace is a Linux kernel feature that provides isolation of the network stack, allowing each network namespace to have its own independent network configuration, interfaces, IP addresses, routing tables, and firewall rules. This isolation is useful in various scenarios, such as containerization, where each container should have its own network configuration, independent of other containers and the host system.
setns()
system call, or create new namespaces using the unshare()
or clone()
system calls with the CLONE_NEWNET
flag. When a process moves to a new namespace or creates one, it will start using the network configuration and interfaces associated with that namespace.sudo unshare -n [--mount-proc] /bin/bash
# Run ifconfig or ip -a
By mounting a new instance of the /proc
filesystem if you use the param --mount-proc
, you ensure that the new mount namespace has an accurate and isolated view of the process information specific to that namespace.
When unshare
is executed without the -f
option, an error is encountered due to the way Linux handles new PID (Process ID) namespaces. The key details and the solution are outlined below:
Problem Explanation:
unshare
system call. However, the process that initiates the creation of a new PID namespace (referred to as the "unshare" process) does not enter the new namespace; only its child processes do.%unshare -p /bin/bash%
starts /bin/bash
in the same process as unshare
. Consequently, /bin/bash
and its child processes are in the original PID namespace./bin/bash
in the new namespace becomes PID 1. When this process exits, it triggers the cleanup of the namespace if there are no other processes, as PID 1 has the special role of adopting orphan processes. The Linux kernel will then disable PID allocation in that namespace.Consequence:
PIDNS_HASH_ADDING
flag. This results in the alloc_pid
function failing to allocate a new PID when creating a new process, producing the "Cannot allocate memory" error.Solution:
-f
option with unshare
. This option makes unshare
fork a new process after creating the new PID namespace.%unshare -fp /bin/bash%
ensures that the unshare
command itself becomes PID 1 in the new namespace. /bin/bash
and its child processes are then safely contained within this new namespace, preventing the premature exit of PID 1 and allowing normal PID allocation.By ensuring that unshare
runs with the -f
flag, the new PID namespace is correctly maintained, allowing /bin/bash
and its sub-processes to operate without encountering the memory allocation error.
docker run -ti --name ubuntu1 -v /usr:/ubuntu1 ubuntu bash
# Run ifconfig or ip -a
ls -l /proc/self/ns/net
lrwxrwxrwx 1 root root 0 Apr 4 20:30 /proc/self/ns/net -> 'net:[4026531840]'
sudo find /proc -maxdepth 3 -type l -name net -exec readlink {} \; 2>/dev/null | sort -u | grep "net:"
# Find the processes with an specific namespace
sudo find /proc -maxdepth 3 -type l -name net -exec ls -l {} \; 2>/dev/null | grep <ns-number>
nsenter -n TARGET_PID --pid /bin/bash
Also, you can only enter in another process namespace if you are root. And you cannot enter in other namespace without a descriptor pointing to it (like /proc/self/ns/net
).
Other ways to support HackTricks: