Network Namespace
Learn & practice AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Learn & practice GCP Hacking:
HackTricks Training GCP Red Team Expert (GRTE)
Support HackTricks
Check the subscription plans!
Join the 💬 Discord group or the telegram group or follow us on Twitter 🐦 @hacktricks_live.
Share hacking tricks by submitting PRs to the HackTricks and HackTricks Cloud github repos.
Basic Information
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.
How it works:
When a new network namespace is created, it starts with a completely isolated network stack, with no network interfaces except for the loopback interface (lo). This means that processes running in the new network namespace cannot communicate with processes in other namespaces or the host system by default.
Virtual network interfaces, such as veth pairs, can be created and moved between network namespaces. This allows for establishing network connectivity between namespaces or between a namespace and the host system. For example, one end of a veth pair can be placed in a container's network namespace, and the other end can be connected to a bridge or another network interface in the host namespace, providing network connectivity to the container.
Network interfaces within a namespace can have their own IP addresses, routing tables, and firewall rules, independent of other namespaces. This allows processes in different network namespaces to have different network configurations and operate as if they are running on separate networked systems.
Processes can move between namespaces using the
setns()system call, or create new namespaces using theunshare()orclone()system calls with theCLONE_NEWNETflag. When a process moves to a new namespace or creates one, it will start using the network configuration and interfaces associated with that namespace.
Lab:
Create different Namespaces
CLI
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.
Error: bash: fork: Cannot allocate memory
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:
The Linux kernel allows a process to create new namespaces using the
unsharesystem 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.Running
%unshare -p /bin/bash%starts/bin/bashin the same process asunshare. Consequently,/bin/bashand its child processes are in the original PID namespace.The first child process of
/bin/bashin 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:
The exit of PID 1 in a new namespace leads to the cleaning of the
PIDNS_HASH_ADDINGflag. This results in thealloc_pidfunction failing to allocate a new PID when creating a new process, producing the "Cannot allocate memory" error.
Solution:
The issue can be resolved by using the
-foption withunshare. This option makesunsharefork a new process after creating the new PID namespace.Executing
%unshare -fp /bin/bash%ensures that theunsharecommand itself becomes PID 1 in the new namespace./bin/bashand 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
Check which namespace is your process in
Find all Network namespaces
Enter inside a Network namespace
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).
References
Learn & practice AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Learn & practice GCP Hacking:
HackTricks Training GCP Red Team Expert (GRTE)
Support HackTricks
Check the subscription plans!
Join the 💬 Discord group or the telegram group or follow us on Twitter 🐦 @hacktricks_live.
Share hacking tricks by submitting PRs to the HackTricks and HackTricks Cloud github repos.
Last updated