Skip to content

๐Ÿž WithMounted{File,Directory,Cache,Secret,Temp} don't allow setting arbitrary uid/gid ย #4739

Closed

Description

@marcosnils

What is the issue?

WithMounted{File,Directory,Cache,Secret,Temp} operations are currently assigning root:root permissions inside the containers which makes any pipeline that doesn't use a root user fail.

Log output

#3 test
#3 0.147 uid=1000(foo) gid=1000(foo)
#3 DONE 0.3s

#3 test
0
#3 0.147 -rw-rw-r--    1 root     root            12 Mar  9 14:25 message.txt

As you can see from the repro steps, message.txt gets mounted as root even though current user is foo

Steps to reproduce

func main() {
	ctx := context.Background()
	client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
	if err != nil {
		panic(err)
	}
	defer client.Close()

	msgFile := client.Host().Workdir().File("message.txt")
	output, err := client.Pipeline("test").
		Container().
		From("alpine").
		WithExec([]string{"adduser", "-D", "foo"}).
		WithUser("foo").
		WithMountedFile("message.txt", msgFile).
		WithExec([]string{"id"}).
		WithExec([]string{"ls", "-la", "message.txt"}).
		ExitCode(ctx)
	if err != nil {
		panic(err)
	}
	fmt.Println(output)
}

SDK version

Go v0.5.0

OS version

Linux xps 5.19.0-35-generic #36-Ubuntu SMP PREEMPT_DYNAMIC Fri Feb 3 18:36:56 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Activity

  1. added
    kind/bug
    on Mar 9, 2023
  2. dolanor commented on Mar 28, 2023

    @dolanor
    Contributor

    related issue regarding secrets: #3323

  3. changed the title [-]๐Ÿž WiithMounted[File,Directory,Cache,Secret,Temp} don't allow setting arbitrary uid/gid [/-] [+]๐Ÿž WiithMounted{File,Directory,Cache,Secret,Temp} don't allow setting arbitrary uid/gid [/+] on Mar 28, 2023
  4. changed the title [-]๐Ÿž WiithMounted{File,Directory,Cache,Secret,Temp} don't allow setting arbitrary uid/gid [/-] [+]๐Ÿž WithMounted{File,Directory,Cache,Secret,Temp} don't allow setting arbitrary uid/gid [/+] on Mar 29, 2023
  5. added
    area/engine
    on Apr 4, 2023
  6. mircubed commented on Apr 10, 2023

    @mircubed
    Contributor

    talked with @vito, and he will take a look at this one.

  7. marcosnils commented on Apr 10, 2023

    @marcosnils
    ContributorAuthor

    We were speaking with @grouville on Friday that this should also take into consideration withFile, withDirectory, withNewFile, etc

  8. vito commented on Apr 10, 2023

    @vito
    Contributor

    Proposal: for File and Directory we can add withOwners(uid: Int, gid: Int) which seems to fit naturally alongside with withTimestamps(timestamp: Int) (chown vs chtimes).

    extend type Directory {
      withOwners(uid: Int, gid: Int): Directory!
    }
    
    extend type File {
      withOwners(uid: Int, gid: Int): File!
    }

    For caches + secrets + sockets we can add mount options:

    type Container {
      withMountedCache(..., uid: Int, gid: Int): Container!
      withMountedSecret(..., uid: Int, gid: Int): Container!
      withUnixSocket(..., uid: Int, gid: Int): Container!
    }

    Not sure about withMountedTemp yet.

  9. marcosnils commented on Apr 11, 2023

    @marcosnils
    ContributorAuthor

    Proposal: for File and Directory we can add withOwners(uid: Int, gid: Int) which seems to fit naturally alongside with withTimestamps(timestamp: Int) (chown vs chtimes).

    +1. One thing that brings my attention is that {Container, Directory}.WithNewFile and Directory.WithNewDirectory API's have the permissions (chmod) option as a field argument. Would it be possible to consolidate?

    For caches + secrets + sockets we can add mount options:

    SGTM here!

  10. vito commented on Apr 11, 2023

    @vito
    Contributor

    For caches + secrets + sockets we can add mount options:

    I just realized we could technically make the same API change for these, it just feels a little odd for some reason:

    extend type CacheVolume {
      withOwners(uid: Int, gid: Int): CacheVolume!
    }
    
    extend type Secret {
      withOwners(uid: Int, gid: Int): Secret!
    }
    
    extend type Socket {
      withOwners(uid: Int, gid: Int): Socket!
    }

    I think this feels weird because these three aren't so strictly tied to the idea of filesystems/permissions/etc. compared to files and directories. For example, Secret { withOwners } is pointless if the secret is only ever passed as an env var instead.

    Right now I'm leaning towards sticking to mount options instead, just farming for dissent.

    Arguably even File/Directory should be mount-time because the value is influenced by the running context (valid UID in container), as opposed to withTimestamps which applies to the object itself. In fact, there might be a way to support usernames instead of user IDS which seems like a major DX win.

    Hmm...

  11. marcosnils commented on Apr 11, 2023

    @marcosnils
    ContributorAuthor

    Right now I'm leaning towards sticking to mount options instead, just farming for dissent.

    yep, sharing the same feeling about this.

    Arguably even File/Directory should be mount-time because the value is influenced by the running context (valid UID in container), as opposed to withTimestamps which applies to the object itself.

    True. Another reason to add these as query arguments I guess?

    In fact, there might be a way to support user_names_ instead of user IDS which seems like a major DX win.

    yep, that'd be great. IIRC Dockerfiles have the same DX as well for --chown. I don't think it works for mounts though

  12. vito commented on Apr 11, 2023

    @vito
    Contributor

    True. Another reason to add these as query arguments I guess?

    ๐Ÿ‘

    yep, that'd be great. IIRC Dockerfiles have the same DX as well for --chown. I don't think it works for mounts though

    It looks like this is easy (ish) for mounting files/directories. You can just use llb.WithUser("foo:foo") as long as the base of the llb.Copy operation stems from the filesystem. It'll resolve those names to IDs by looking at /etc/passwd on its own.

    It seems a lot harder for secrets/sockets/caches. We'll have to manually load up /etc/passwd and /etc/group and resolve names to UIDs in that case. I'll try to implement it anyway, it's definitely a lot nicer to be able to use names everywhere rather than guess or print the ID beforehand.

  13. vito commented on Apr 13, 2023

    @vito
    Contributor

    Do we want container mounts to inherit the container's configured user by default? That seems to be the crux of the original report, but the title mentions explicitly configuring it.

    I started implementing it, but it adds a bit of overhead (a copy + chmod for every mount), and I wonder if it's too surprising. For example maybe you want to keep ownership as-is when passing data from one container to another. I'm not sure when you would want to do that, and users in one container might not be present with the same ID in another, but maybe sometimes it matters?

  14. dolanor commented on Apr 14, 2023

    @dolanor
    Contributor

    If we pass withMountedFile with foo:foo, what would be the expected behavior?
    Do we check in the host /etc/passwd to transform this foo:foo into the host uid:gid? Or do we check in the /etc/passwd in the container this file is mounted into?
    What if we don't find it (in any of both cases), do we fall down to the default user (on host, that would be the user running the pipeline, on the container, it would be image's default)? Or do we fail with an error telling the mapping could not be found?

  15. vito commented on Apr 14, 2023

    @vito
    Contributor

    Do we check in the host /etc/passwd to transform this foo:foo into the host uid:gid? Or do we check in the /etc/passwd in the container this file is mounted into?

    The latter

    What if we don't find it (in any of both cases), do we fall down to the default user (on host, that would be the user running the pipeline, on the container, it would be image's default)? Or do we fail with an error telling the mapping could not be found?

    The latter again, but now that you mention it, I'll add an explicit test ๐Ÿ‘

  16. vito commented on Apr 14, 2023

    @vito
    Contributor

    There's a sneaky interaction here with WithTimestamps, and file timestamps in general: chown is technically a write, so it bumps the timestamps, causing this test to fail:

    func TestFileWithTimestamps(t *testing.T) {
    	c, ctx := connect(t)
    	defer c.Close()
    
    	reallyImportantTime := time.Date(1985, 10, 26, 8, 15, 0, 0, time.UTC)
    
    	file := c.Directory().
    		WithNewFile("sub-dir/sub-file", "sub-content").
    		File("sub-dir/sub-file").
    		WithTimestamps(int(reallyImportantTime.Unix()))
    
    	ls, err := c.Container().
    		From("alpine:3.16.2").
    		WithMountedFile("/file", file).
    		WithEnvVariable("RANDOM", identity.NewID()).
    		WithExec([]string{"stat", "/file"}).
    		Stdout(ctx)
    	require.NoError(t, err)
    	require.Contains(t, ls, "Access: 1985-10-26 08:15:00.000000000 +0000")
    	require.Contains(t, ls, "Modify: 1985-10-26 08:15:00.000000000 +0000")
    }

    Seems like more evidence that we shouldn't automatically chown things. If it's opt-in, you can at least mount + chown and then fetch that directory back out and apply WithTimestamps to it.

  17. vito commented on Apr 15, 2023

    @vito
    Contributor

    Somehow the above test is passing in CI. ๐Ÿค”

    The only reason I can think of is that the file is already chowned to 0:0 and somehow Buildkit does a no-op, but I don't know why that wouldn't apply to my local machine:

    โฏ gotestsum ./core/integration/ -skip RemoteCache
    [core/integration]ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทโœ–ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท
    === Failed
    === FAIL: core/integration TestFileWithTimestamps (2.95s)
    #1 resolve image config for docker.io/library/alpine:3.16.2
    #1 DONE 0.2s
    
    #2 importing cache manifest from dagger:10686922502337221602
    #2 DONE 0.0s
    
    #3
    #3 CACHED
    
    #4 chown /file to 0:0
    #4 CACHED
    
    #5 from alpine:3.16.2
    #5 resolve docker.io/library/alpine:3.16.2 0.1s done
    #5 CACHED
    
    #6
    #6 0.124   File: /file
    #6 0.124   Size: 11        	Blocks: 8          IO Block: 4096   regular file
    #6 0.124 Device: 8ah/138d	Inode: 55974303    Links: 1
    #6 0.124 Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    #6 0.124 Access: 2023-04-12 21:24:19.616862545 +0000
    #6 0.124 Modify: 2023-04-12 21:24:19.616862545 +0000
    #6 0.124 Change: 2023-04-13 19:51:53.191900704 +0000
    #6 DONE 0.2s
        file_test.go:192:
            	Error Trace:	/home/vito/src/dagger/core/integration/file_test.go:192
            	Error:      	"  File: /file\n  Size: 11        \tBlocks: 8          IO Block: 4096   regular file\nDevice: 8ah/138d\tInode: 55974303    Links: 1\nAccess: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)\nAccess: 2023-04-12 21:24:19.616862545 +0000\nModify: 2023-04-12 21:24:19.616862545 +0000\nChange: 2023-04-13 19:51:53.191900704 +0000\n" does not contain "Access: 1985-10-26 08:15:00.000000000 +0000"
            	Test:       	TestFileWithTimestamps
    
    DONE 366 tests, 1 failure in 276.621s
    

    I noticed the dates above looked a little funny, so I pruned the Buildkit cache and re-ran and sure enough, it passes:

    === RUN   TestFileWithTimestamps
    #1 resolve image config for docker.io/library/alpine:3.16.2
    #1 DONE 0.2s
    
    #2 importing cache manifest from dagger:10686922502337221602
    #2 DONE 0.0s
    
    #3
    #3 CACHED
    
    #4
    #4 DONE 0.0s
    
    #5 from alpine:3.16.2
    #5 resolve docker.io/library/alpine:3.16.2 0.1s done
    #5 DONE 0.1s
    
    #6 chown /file to 0:0
    #6 ...
    
    #5 from alpine:3.16.2
    #5 CACHED
    
    #4
    #4 0.125   File: /file
    #4 0.125   Size: 11        	Blocks: 8          IO Block: 4096   regular file
    #4 0.125 Device: 8ah/138d	Inode: 59113495    Links: 1
    #4 0.125 Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    #4 0.125 Access: 1985-10-26 08:15:00.000000000 +0000
    #4 0.125 Modify: 1985-10-26 08:15:00.000000000 +0000
    #4 0.125 Change: 2023-04-14 20:52:16.330798492 +0000
    #4 DONE 0.2s
    
    #6 chown /file to 0:0
    --- PASS: TestFileWithTimestamps (2.61s)
    

    No idea why this would happen.

  18. grouville commented on Apr 17, 2023

    @grouville
    Member

    Do we want container mounts to inherit the container's configured user by default? That seems to be the crux of the original report, but the title mentions explicitly configuring it.

    I would personally find it pretty surprising. Even though it adds some verbosity to the pipeline, having a magical inheritance of the uid/gid might become confusing. WDYT @helderco ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/enginekind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions