Skip to content

docker:19.03.6-dind: chown not working with "docker build" when dockerd is run in userns #217

@jappenzesr

Description

@jappenzesr

When configuring dockerd in 19.03.6-dind to run in a user-namespace, chown doesn't work anymore.

Why is it relevant
We use dind to provide docker building services in a CI/CD platform shared among thousands of developers. Running docker (even in dind) in a user-namespace is an essential part of the security strategy to reduce the attack vector on the underlying build node.

Testcase

  1. Create an image with dockerd run in userns, based on 19.03.6-dind
FROM docker.artifact.swissre.com/docker:19.03.6-dind
RUN mkdir /etc/docker && echo '{ "userns-remap": "dockerns:dockerns" }' > /etc/docker/daemon.json && \
    addgroup -g 1000 dockerns && adduser dockerns -u 1000 -D -H -G dockerns -s /sbin/nologin && \
    echo "dockerns:1000:65536" > /etc/subuid && echo "dockerns:1000:65536" > /etc/subgid
ENTRYPOINT ["dockerd-entrypoint.sh"]
  1. Build the image (tag 'my-dind')
  2. Run it
docker run --privileged --userns=host --name some-docker -d \
           --network some-network --network-alias docker \
           -e DOCKER_TLS_CERTDIR=/certs \
           -v some-docker-certs-ca:/certs/ca \
           -v some-docker-certs-client:/certs/client \
           my-dind
  1. Run the client in a second container, and connect to it via interactive shell
docker run -it --rm --network some-network \
    -e DOCKER_TLS_CERTDIR=/certs \
    -v some-docker-certs-client:/certs/client:ro \
    docker:latest sh
  1. In the client, create the below Dockerfile
FROM docker-hub.artifact.swissre.com/ubuntu:latest
RUN useradd -m testuser
RUN chown testuser /home/testuser
RUN ls -la /home/testuser
USER testuser
RUN touch /home/testuser/testfile
RUN ls -la /home/testuser/testfile

Expected outcome
The docker build should complete successfully. The users home-directory should be created and owned by testuser, and it should be possible to create a file in that directory after USER testuser.
This is indeed the case with a pure 19.03.6-dind setup

# Create Dockerfile in /tmp as per above
/tmp # docker build .
Sending build context to Docker daemon  2.048kB
Step 1/7 : FROM docker-hub.artifact.swissre.com/ubuntu:latest
 ---> 72300a873c2c
Step 2/7 : RUN useradd -m testuser
 ---> Running in 713f1bf1e37e
Removing intermediate container 713f1bf1e37e
 ---> 42ef5d91553b
Step 3/7 : RUN chown testuser /home/testuser
 ---> Running in 442e8fee949f
Removing intermediate container 442e8fee949f
 ---> c12d55a9831c
Step 4/7 : RUN ls -la /home/testuser
 ---> Running in f1e0129af7fd
total 20
drwxr-xr-x 1 testuser testuser 4096 Feb 28 16:30 .
drwxr-xr-x 1 root     root     4096 Feb 28 16:30 ..
-rw-r--r-- 1 testuser testuser  220 Apr  4  2018 .bash_logout
-rw-r--r-- 1 testuser testuser 3771 Apr  4  2018 .bashrc
-rw-r--r-- 1 testuser testuser  807 Apr  4  2018 .profile
Removing intermediate container f1e0129af7fd
 ---> 706cb90a08a2
Step 5/7 : USER testuser
 ---> Running in a093b47ab2a0
Removing intermediate container a093b47ab2a0
 ---> 56be4664d148
Step 6/7 : RUN touch /home/testuser/testfile
 ---> Running in 1b35e9f3a788
Removing intermediate container 1b35e9f3a788
 ---> 674dfc04616a
Step 7/7 : RUN ls -la /home/testuser/testfile
 ---> Running in c339d69b8061
-rw-r--r-- 1 testuser testuser 0 Feb 28 16:30 /home/testuser/testfile
Removing intermediate container c339d69b8061
 ---> e33001c2b1ac
Successfully built e33001c2b1ac

Actual Outcome
The build breaks, because the directory /home/testuser as well as all its content is still owned by root

Sending build context to Docker daemon  2.048kB
Step 1/7 : FROM docker-hub.artifact.swissre.com/ubuntu:latest
latest: Pulling from ubuntu
423ae2b273f4: Pull complete
de83a2304fa1: Pull complete
f9a83bce3af0: Pull complete
b6b53be908de: Pull complete
Digest: sha256:04d48df82c938587820d7b6006f5071dbbffceb7ca01d2814f81857c631d44df
Status: Downloaded newer image for docker-hub.artifact.swissre.com/ubuntu:latest
 ---> 72300a873c2c
Step 2/7 : RUN useradd -m testuser
 ---> Running in 5e6d664b48b9
Removing intermediate container 5e6d664b48b9
 ---> 55eabfb2eb21
Step 3/7 : RUN chown testuser /home/testuser
 ---> Running in b1edc2bd93da
Removing intermediate container b1edc2bd93da
 ---> 06285e4a100f
Step 4/7 : RUN ls -la /home/testuser
 ---> Running in f8a6b0c02012
total 20
drwxr-xr-x 1 root root 4096 Feb 24 15:39 .
drwxr-xr-x 1 root root 4096 Feb 24 15:39 ..
-rw-r--r-- 1 root root  220 Apr  4  2018 .bash_logout
-rw-r--r-- 1 root root 3771 Apr  4  2018 .bashrc
-rw-r--r-- 1 root root  807 Apr  4  2018 .profile
Removing intermediate container f8a6b0c02012
 ---> cf1d0d2a4d24
Step 5/7 : USER testuser
 ---> Running in cd2378512500
Removing intermediate container cd2378512500
 ---> bf2196e69fee
Step 6/7 : RUN touch /home/testuser/testfile
 ---> Running in 74456fd74def
touch: cannot touch '/home/testuser/testfile': Permission denied
The command '/bin/sh -c touch /home/testuser/testfile' returned a non-zero code: 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions