UTS Namespace

Support HackTricks

Podstawowe informacje

Namespace UTS (UNIX Time-Sharing System) to funkcja jądra Linux, która zapewnia izolację dwóch identyfikatorów systemowych: nazwy hosta i nazwy domeny NIS (Network Information Service). Ta izolacja pozwala każdemu namespace UTS mieć własną niezależną nazwę hosta i nazwę domeny NIS, co jest szczególnie przydatne w scenariuszach konteneryzacji, gdzie każdy kontener powinien wyglądać jak oddzielny system z własną nazwą hosta.

Jak to działa:

  1. Gdy nowy namespace UTS jest tworzony, zaczyna od kopii nazwy hosta i nazwy domeny NIS z jego rodzicielskiego namespace. Oznacza to, że w momencie utworzenia nowy namespace dzieli te same identyfikatory co jego rodzic. Jednak wszelkie późniejsze zmiany w nazwie hosta lub nazwie domeny NIS w obrębie namespace nie wpłyną na inne namespace.

  2. Procesy w obrębie namespace UTS mogą zmieniać nazwę hosta i nazwę domeny NIS za pomocą wywołań systemowych sethostname() i setdomainname(), odpowiednio. Te zmiany są lokalne dla namespace i nie wpływają na inne namespace ani na system gospodarza.

  3. Procesy mogą przemieszczać się między namespace za pomocą wywołania systemowego setns() lub tworzyć nowe namespace za pomocą wywołań systemowych unshare() lub clone() z flagą CLONE_NEWUTS. Gdy proces przemieszcza się do nowego namespace lub go tworzy, zacznie używać nazwy hosta i nazwy domeny NIS związanej z tym namespace.

Laboratorium:

Tworzenie różnych Namespace

CLI

sudo unshare -u [--mount-proc] /bin/bash

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 dokładny i izolowany widok informacji o procesach specyficznych dla tej przestrzeni nazw.

Błąd: bash: fork: Nie można przydzielić pamięci

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:

  1. Wyjaśnienie problemu:

  • The Linux kernel allows a process to create new namespaces using the 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.

  • Running %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.

  • The first child process of /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.

  1. Konsekwencja:

  • The exit of PID 1 in a new namespace leads to the cleaning of the 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.

  1. Rozwiązanie:

  • The issue can be resolved by using the -f option with unshare. This option makes unshare fork a new process after creating the new PID namespace.

  • Executing %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

docker run -ti --name ubuntu1 -v /usr:/ubuntu1 ubuntu bash

Sprawdź, w jakiej przestrzeni nazw znajduje się twój proces

ls -l /proc/self/ns/uts
lrwxrwxrwx 1 root root 0 Apr  4 20:49 /proc/self/ns/uts -> 'uts:[4026531838]'

Znajdź wszystkie przestrzenie nazw UTS

sudo find /proc -maxdepth 3 -type l -name uts -exec readlink {} \; 2>/dev/null | sort -u
# Find the processes with an specific namespace
sudo find /proc -maxdepth 3 -type l -name uts -exec ls -l  {} \; 2>/dev/null | grep <ns-number>

Wejście do przestrzeni nazw UTS


<div data-gb-custom-block data-tag="hint" data-style='success'>

Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)

<details>

<summary>Support HackTricks</summary>

* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.

</details>

</div>

</details>

</div>

</details>

</div>

</details>

</div>

</details>

</div>

</details>

</div>

</details>

</div>

</details>

</div>

</details>

</div>

</details>

</div>

</details>

</div>

</details>

</div>

</details>

</div>

</details>

</div>

</details>

</div>

</details>

</div>

Last updated