Docker group privilege escalation
Table of Contents
Summary
If a user belongs to the docker group, this effectively means that you can create a Docker container with a root user. Using this we can create a mount inside the Docker container mapping to the host file-system and read any files as if we were the root user on the host machine.
Prerequisites
- Local shell access to victim machine
- Victim machine running
docker
- Victim machine with a local (none root) user in the
docker
group
We can check that these prerequisites are met with a few simple commands
Check docker
is installed with which
Check local user is part of the docker
group with id
Privilege escalation
This method of privilege escalation abuses user namespaces in Linux, where the User ID (uid) of a user inside a container is mapped to the User ID (uid) of a user on the host. If a user belongs to the Docker group, this effectively means that you can create a Docker container with a root user on the host machine. Using this we can create a mount inside the Docker container mapping to the host file-system and read any files as if we were the root user.
Steps
We can run the lightweight Docker container alpine
and use a volume
with the -v
flag to mount the host file-system within the Docker container. By passing the -it
flag we can also spawn an interactive terminal within the Docker container.
This gives us access to the file-system on the host machine as the root
user.
We can check the permissions on a file that is only usually readable/modifiable by the root user.
Next let’s see if we can read the /etc/passwd
file, this will be located at /root/passwd
as that’s where we mounted the volume in the first step.
One liner
We can shorten this whole process into a one-liner to read the /etc/passwd
file of the host file-system.
Mitigation
To mitigate this technique we can run the Docker daemon as a none root user (rootless mode), rootless mode executes the Docker daemon and containers inside a user namespace, which separates the User IDs (uid) and Group IDs (gid) between the host Operating System and containers. More information can be found on the official Docker website.
This can be combined with running the container without root privileges using userns-remap
which isolates containers with a user namespace, further reading on this can be found on the official Docker website.