Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dnm] testing if FileMode is affected by umask #4936

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

thaJeztah
Copy link
Member

I was trying to run a build stage as non-root user, but it uses some cache mounts. So I tried to mount the cache-mount with a specific group (gid), and set permissions to 0775 (instead of the default 0755) to allow the stage to write, but it looks like it still ends up with 0755 permissions.

Most minimal reproducer;

docker build --progress=plain --no-cache -<<'EOF'
# syntax=docker/dockerfile:1

FROM alpine
RUN --mount=type=cache,target=/hello/.cache,mode=0775 ls -la /hello/.cache/

Which shows that the target has 0755 permissions;

#6 [stage-0 1/2] FROM docker.io/library/alpine:latest
#6 DONE 0.0s

#7 [internal] settings cache mount permissions
#7 DONE 0.1s

#8 [stage-0 2/2] RUN --mount=type=cache,target=/hello/.cache,mode=0775  ls -la /hello/.cache/
#8 0.641 total 8
#8 0.641 drwxr-xr-x    2 root     root          4096 May 17 12:04 .
#8 0.641 drwxr-xr-x    3 root     root          4096 May 17 12:04 ..
#8 DONE 0.7s

Looking at that settings cache mount permissions message brings me to this code, which uses llb.Mkdir;

return st.File(llb.Mkdir("/cache", mode, llb.WithUIDGID(uid, gid)), llb.WithCustomName("[internal] settings cache mount permissions"))

My initial assumption is that possibly the given filemode still gets an umask applied, causing the effective permissions to be different.

It's a bit hard to find where the actual Mkdir happens, because that function returns a FileOp (which only contains the options), but looking through tests I see that there's various tests setting a mode, but all of them seem to use a mode that's not affected by umask (assuming default 022 umask); most (if not "all") of them seem to be using (e.g.) 0700 or 0600.

Let's change a test to see if it fails, which may confirm my suspicion.

I was trying to run a build stage as non-root user, but it uses some cache
mounts. So I tried to mount the cache-mount with a specific group (gid), and
set permissions to 0775 (instead of the default 0755) to allow the stage to
write, but it looks like it still ends up with 0755 permissions.

Most minimal reproducer;

    docker build --progress=plain --no-cache -<<'EOF'
    # syntax=docker/dockerfile:1

    FROM alpine
    RUN --mount=type=cache,target=/hello/.cache,mode=0775 ls -la /hello/.cache/

Which shows that the target has 0755 permissions;

    #6 [stage-0 1/2] FROM docker.io/library/alpine:latest
    #6 DONE 0.0s

    #7 [internal] settings cache mount permissions
    #7 DONE 0.1s

    #8 [stage-0 2/2] RUN --mount=type=cache,target=/hello/.cache,mode=0775  ls -la /hello/.cache/
    #8 0.641 total 8
    #8 0.641 drwxr-xr-x    2 root     root          4096 May 17 12:04 .
    #8 0.641 drwxr-xr-x    3 root     root          4096 May 17 12:04 ..
    #8 DONE 0.7s

Looking at that `settings cache mount permissions` message brings me to this
code, which uses `llb.Mkdir`;
https://github.com/moby/buildkit/blob/4b5bf7ef9b3cd0a2485c173f0700dce21e83d964/frontend/dockerfile/dockerfile2llb/convert_runmount.go#L60

My initial assumption is that possibly the given filemode still gets an umask
applied, causing the effective permissions to be different.

It's a bit hard to find where the actual `Mkdir` happens, because that function
returns a `FileOp` (which only contains the options), but looking through tests
I see that there's various tests setting a mode, but all of them seem to use
a mode that's not affected by umask (assuming default `022` umask); most (if
not "all") of them seem to be using (e.g.) `0700` or `0600`.

Let's change a test to see if it fails, which may confirm my suspicion.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah
Copy link
Member Author

Hm... right this test doesn't test any file changes, so I'd have to check for some other place to test this.

@tonistiigi
Copy link
Member

tonistiigi commented May 22, 2024

As discussed in slack, the cause of this seems to be different umask behavior in moby vs buildkitd. buildkitd resets umask in

syscall.Umask(0)
and doesn't seem to have any leakage issues.

@thaJeztah
Copy link
Member Author

Hm… right. Curious though if we can reset the Umask globally for dockerd; any idea why we never did so? (I know umask has come up a few times, but I think we resolved those on a per-case base by doing a chmod afterwards)

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.

None yet

2 participants