Skip to content

Fix secret mounting with userns_remap enabled#2723

Closed
jiashuChen wants to merge 2 commits intomoby:masterfrom
jiashuChen:master
Closed

Fix secret mounting with userns_remap enabled#2723
jiashuChen wants to merge 2 commits intomoby:masterfrom
jiashuChen:master

Conversation

@jiashuChen
Copy link
Copy Markdown

@jiashuChen jiashuChen commented Mar 13, 2022

This Pull Request is created based on feedback from @cpuguy83 in this pull request moby/moby#43354

Tested by compiling my own version of dockerd from this pull request moby/moby#43354 with a cherry-picked version of this changed because moby still depends on buildkit commit 9f254e1 released in 2021.

Related issue:

The previous commit here moby/moby@bfedd27 modified
the owner and permission of the DOCKER_TMP to root:root and 700 respectively,
and it seems to have assumed that the folder is not needed by the remapped
root user, however, this is not actually the case for buildkit secrets.

Although the original issue was raised only relating to docker secrets mounts which uses temp file
created in DOCKER_TMP directory. This issue actually impact other locations as well.

The proposed solution will created an environment variable BUILDKIT_TMPDIR which buildkit will lookup and this
directory will take priority over the TMPDIR that the default os.tempDir returns.

Main change here https://github.com/jiashuChen/buildkit/blob/256c02dcc6d50c99fe6396fe83d241940eefbe96/util/system/tempdir.go#L9.

I have then refactored all location that uses os.MkdirTemp to use the newly created function above.

NOTE:

In order to resolve the issue, the solution proposed here need both this pull request and moby/moby#43354, currently moby/moby#43354 uses a private fork from my repo in order to demostrate the idea, however, it would highly appreciated if code owner from moby can help with either upgrade buildkit from moby/moby to the latest version or provide a guide on whether backport is allowed.

Signed-off-by: Jiashu Chen cjs20080808@hotmail.com

@jiashuChen
Copy link
Copy Markdown
Author

While trying to compile the buidkit for local testing. I notice the same problem seems to occur in the localmounter_unix module. As I get the following error with userns_remap enabled when running make binaries.

 > [buildkitd 1/1] RUN --mount=target=. --mount=target=/root/.cache,type=cache   --mount=target=/go/pkg/mod,type=cache   --mount=source=/tmp/.ldflags,target=/tmp/.ldflags,from=buildkit-version   CGO_ENABLED=0 xx-go build -ldflags "$(cat /tmp/.ldflags) -extldflags '-static'" -tags "osusergo netgo static_build seccomp ${BUILDKITD_TAGS}" -o /usr/bin/buildkitd ./cmd/buildkitd &&   xx-verify --static /usr/bin/buildkitd:
#0 0.198 container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:75: mounting "/var/lib/docker/1000.997/tmp/buildkit-mount592200980/tmp/.ldflags" to rootfs at "/tmp/.ldflags" caused: stat /var/lib/docker/1000.997/tmp/buildkit-mount592200980/tmp/.ldflags: permission denied

@tonistiigi
Copy link
Copy Markdown
Member

Isn't the place you are looking to configure

dir, err := os.MkdirTemp("", "buildkit-secrets")
and not this code at all. I'm not sure I understand how it is related to mounting secrets. There might be other mounts that also need the same perms (at least ssh socket and tmpfs probably).

@jiashuChen
Copy link
Copy Markdown
Author

jiashuChen commented Mar 15, 2022

Isn't the place you are looking to configure

dir, err := os.MkdirTemp("", "buildkit-secrets")

and not this code at all. I'm not sure I understand how it is related to mounting secrets. There might be other mounts that also need the same perms (at least ssh socket and tmpfs probably).

yeah, this is the same file I was trying to modify (my repo seems to be behind master change (which removed deprecated ioutil.TempDir). the reason why I changed the other place was because when i was testing locally, I noticed other places that uses the /var/lib/docker/tmp would have the same problem under userns_remap node.

@jiashuChen jiashuChen marked this pull request as ready for review March 20, 2022 22:13
@jiashuChen jiashuChen requested a review from tonistiigi March 20, 2022 22:17
Jiashu Chen added 2 commits April 10, 2022 23:08
The previous commit here moby/moby@bfedd27 modified
the owner and permission of the DOCKER_TMP to root:root and 700 respectively,
and it seems to have assumed that the folder is not needed by the remapped
root user, however, this is not actually the case for buildkit secrets.

The solution here is to move the secret file created on host into the
`/var/lib/docker/${UID}:${GID}/buildkit/mounts/secrets/` directory

Since the `/var/lib/docker/${UID}:${GID}/buildkit` is created with
`711` permission, this should resolve the issue with secret mount.

Based on code logic here:
moby/moby@5263bea/cmd/dockerd/daemon.go#L299
moby/moby@5c01d06/builder/builder-next/controller.go#L47-L49

Signed-off-by: Jiashu Chen <cjs20080808@hotmail.com>
This is because docker create `TMPDIR` at`/var/lib/docker/tmp` it
only allows `root:root` user to access. However, this means
when we use userns_remap mode in docker daemon, we will not
be able to mount any volume in the container from this tmp directory

The solution is to create a separate directory under the env var
`BUILDKIT_TMPDIR`, and treat it as the default temp directory.

Signed-off-by: Jiashu Chen <cjs20080808@hotmail.com>
@jiashuChen
Copy link
Copy Markdown
Author

Looks like this issue has been fixed in the latest docker release 20.10.15 through an upgrade of runc from 1.0.3 to 1.1.0. I haven't had time to investigate in detail yet. However, I suspect this change fixed the issue opencontainers/runc#3057.

For others experiencing the same issue. Please upgrade docker engine to 20.10.15

@jiashuChen jiashuChen closed this May 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants