In this series, we will talk a bit about various concepts related to Docker internals. I do not have a specific structure in mind, but I might figure something out as we go. I will at least try to be consistent this time (KCX mentioned), unlike before.

We will start by talking about Linux Namespaces and how they are used within Docker containers.

What is a Linux Namespace

A Linux namespace is a feature of the Linux kernel that allows you to isolate system resources for specific processes. For example, say you want to test out a suspicious program that you think might attempt to compromise your machine in some form (e.g., sending your data to a remote server). You can use a Linux namespace to execute the process in its own space (a network namespace in the example I mentioned) and restrict its internet access.

Since a Linux system doesn't have a single set of resources, it has different namespaces to isolate each type of resource (network, mount, IPC, PID, ...). We will cover some of them in this series.

Not only do namespaces isolate a number of different resources, they can also be combined to allow for finer-grained isolation and to create different isolation setups per application. For example, you can use a network namespace and share the same host mount namespace (i.e., the same filesystem) so you have your networking tools inside your isolated environment.

User namespace

User namespaces allow the isolation of users (as you probably guessed) inside the namespace from those on the host. They do this by mapping user IDs (UIDs) and group IDs (GIDs) in a given namespace to a specific range in the parent namespace.

To better understand, let us consider an example of how to use them to secure against a Docker vulnerability.

Docker Breakout

In this "agentic" era that we live in, you cannot give full access to your agent (except your prod database, obviously). However, even if you make sure that it doesn't do any shenanigans and has proper access, there is still a way for it to execute commands as root on the host.

For our specific case, let us consider that the agent somehow needs cowsay to execute the task you provided it with. How can it install it without your permission?

 _____ < moo > ----- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || 

You might say, well... it's impossible. However, all it needs is to run this command:

docker run -v /:/host --rm --name fedora fedora chroot /host dnf install cowsay -y 

There are many mechanisms behind the success of the command above; let us break them down.

  1. In many container platforms (e.g., Docker, Podman, ...), if a user is not specified in the image recipe, the container defaults to using root. This means that you can execute superuser commands (like installing a package) inside the container.

-> The user inside the container is a superuser.

  1. The command mounts the root directory into the fedora container in a folder named /host.

-> The root filesystem is mounted into the container.

/host was used in order to avoid the invalid mount path error from Docker:

 docker: Error response from daemon: invalid volume specification: '/:/': invalid mount config for type "bind": invalid specification: destination can't be '/' 
  1. After mounting it, the command runs the install command with /host (thus our root filesystem) as the root directory (the chroot /host part). Because of the mount (-v /:/host), installing to the /host directory means installing into our root filesystem /.

-> Installing in the container = installing in the host's root folder.

Why this works is twofold:

  1. The Docker daemon has root access on the host and thus can mount the root folder into the container. In contrast, Podman fails as it is rootless:
 $ podman run -v /:/host --rm --name fedora fedora chroot /host dnf install cowsay -y Cannot open log file: (13) - Permission denied [/var/log/dnf5.log]. Dropping all logs. To redirect the log file location use the "logdir" configuration option. The requested operation requires superuser privileges. Please log in as a user with elevated rights, or use the "--assumeno" or "--downloadonly" options to run the command without modifying the system state. 
  1. The fact that we can write to the host root filesystem from inside the container is another problem. This is what we will delve into next.

User namespaces to the rescue!

User namespaces make it so that even if a malicious actor gains access to a container, they do not get the container's root privileges on the host.

The primary use of user namespaces is to create an isolated namespace where UIDs and GIDs are separate between a namespace and its parent. But they don't just do that. They also allow specifying UID translations between the two namespaces.

For any particular process, this mapping is stored under /proc/$PID/uid_map for UIDs and /proc/$PID/gid_map for GIDs.

Let us look at an example comparison between Podman and Docker:

$ docker run --rm --name fedora -it fedora cat /proc/1/uid_map /proc/1/cmdline 0 0 4294967295 cat/proc/1/uid_map/proc/1/cmdline 
$ podman run --rm --name fedora -it fedora cat /proc/1/uid_map /proc/1/cmdline 0 1000 1 1 524288 65536 cat/proc/1/uid_map/proc/1/cmdline 

In this case, PID=1 corresponds to our cat command since no other command is running in the container.

The uid_map file is structured as follows:

Child Start UID Parent Start UID Number of UIDs 0 0 4294967295 

This file reads: UIDs 0-4294967295 in the child user namespace are mapped to 0-4294967295 in the parent namespace. More specifically, root in the child (container) namespace is mapped to the root user in the host namespace. Or, without the technical jargon, the root user in the container has the same access privileges as the root user on the host.

To illustrate this, let us consider a simple example. As a normal user, attempting to create a file in a root-only directory (/etc in our example) will fail:

$ whoami sample-user $ touch /etc/test touch: cannot touch '/etc/test': Permission denied $ ls /etc/test ls: cannot access '/etc/test': No such file or directory 

Now, since root inside the container maps to root on the host (as per the uid_map file), we are able to create the file using the docker run command:

$ docker run -v /etc:/etc --rm --name fedora -it fedora touch /etc/test $ ls /etc/test /etc/test 

In contrast, running the same command using Podman raises the same permission error, even though the user (root) is the same in both the Docker and Podman cases:

$ docker run --rm --name fedora -it fedora whoami root $ podman run --rm --name fedora -it fedora whoami root $ podman run -v /etc:/etc --rm --name fedora -it fedora touch /etc/test touch: cannot touch '/etc/test': Permission denied $ ls /etc/test ls: cannot access '/etc/test': No such file or directory 

For our previous example, if an attacker were to get access to the host filesystem (through some mount, for example), they wouldn't be able to perform actions they are not entitled to.

Applying user namespace scoping to Docker

So, how can we set up Docker to avoid such a threat?

Docker allows specifying the mappings used for created containers, to map container UIDs to host UIDs.

We first need to create a user. This is a Docker requirement related to "an implementation detail". The user will own the namespaced storage directory under /var/lib/docker:

$ groupadd test $ useradd test -g test # these might change depending on your system 

After this, we need to modify the two files /etc/subuid and /etc/subgid to define the ranges that will be used for the user namespace mapping:

# /etc/subgid test:589824:65536 # /etc/subuid test:589824:65536 

We then need to start the Docker daemon with the new configuration. First, we need to make sure that no other instance of dockerd is running:

sudo systemctl stop docker.socket 

Then we can start dockerd:

sudo dockerd --userns-remap "test:test" 

Running our previous command to check:

$ docker run --rm --name fedora -it fedora cat /proc/1/uid_map 0 589824 65536 

And attempting to install cowsay:

$ docker run -v /:/host --rm --name fedora fedora chroot /host dnf install cowsay Cannot open log file: (13) - Permission denied [/var/log/dnf5.log]. Dropping all logs. To redirect the log file location use the "logdir" configuration option. The requested operation requires superuser privileges. Please log in as a user with elevated rights, or use the "--assumeno" or "--downloadonly" options to run the command without modifying the system state. 

While the example we considered was a bit simple, Docker breakouts can be dangerous, especially in scenarios such as CI/CD, where an attacker can use a Docker daemon connection (commonly used for building Docker images) to gain root-level access to the builder machine.

Cool!!! We have secured our Docker. I hope you had fun reading this. We will look into Mount Namespaces in the next post: how they isolate filesystem resources and how they are used in containers.

References

  1. Ifeanyi Ubah's Blog Post
  2. Docker's docs for userns setup