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

Chown files when copying into chroot #2553

Merged
merged 4 commits into from
Apr 19, 2017
Merged

Conversation

schmichael
Copy link
Member

Fixes #2552

Not needed when hardlinking. Only adds Linux support but other Unixes may
be easy.

Copy link

@jshuping jshuping left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing this so quickly, today was my first day reading go so please bear with me!

return fmt.Errorf("Couldn't copy %q to %q: %v", src, dst, err)
}

if uid >= 0 && gid >= 0 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to say || here in case the perms are like root:bob?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to make sure they're both both valid IDs. Even if it's root:bob that's uid=0, gid>0 which will pass this conditional.

if err := os.Link(src, dst); err == nil {
return nil
}
//if err := os.Link(src, dst); err == nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were you maybe just doing this for testing / can we keep hardlinking?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow! Huge goof on my part. Thanks for the review! Fixing.

@@ -88,3 +88,11 @@ func removeSecretDir(dir string) error {
}
return os.RemoveAll(dir)
}

func getOwner(fi os.FileInfo) (int, int) {
stat, ok := fi.Sys().(*syscall.Stat_t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is syscall.Stat_t not implemented on the other platforms we chroot in? We shouldn't leave the bug open on other platforms we chroot in

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jshuping
Copy link

Not needed when hardlinking.

We tried a build with the existing patch, but even when hardlinking files, the code is creating the directories to contain these hardlinks, without honoring the ownership (you can't hardlink directories).

I think we need to check the ownership of the source directory somehwere around here: https://github.com/hashicorp/nomad/blob/master/client/allocdir/task_dir.go#L176

@jcua
Copy link

jcua commented Apr 17, 2017

With this change, this allows ownership of directories to be preserved.

diff --git a/client/allocdir/task_dir.go b/client/allocdir/task_dir.go
index d7e5fcf..e58d21a 100644
--- a/client/allocdir/task_dir.go
+++ b/client/allocdir/task_dir.go
@@ -155,6 +155,7 @@ func (t *TaskDir) embedDirs(entries map[string]string) error {
                        continue
                }

+               uid, gid := getOwner(s)
                // Embedding a single file
                if !s.IsDir() {
                        if err := createDir(t.Dir, filepath.Dir(dest)); err != nil {
@@ -163,7 +164,6 @@ func (t *TaskDir) embedDirs(entries map[string]string) error {

                        // Copy the file.
                        taskEntry := filepath.Join(t.Dir, dest)
-                       uid, gid := getOwner(s)
                        if err := linkOrCopy(source, taskEntry, uid, gid, s.Mode().Perm()); err != nil {
                                return err
                        }
@@ -178,6 +178,12 @@ func (t *TaskDir) embedDirs(entries map[string]string) error {
                        return fmt.Errorf("Couldn't create destination directory %v: %v", destDir, err)
                }

+               if uid >= 0 && gid >= 0 {
+                       if err := os.Chown(destDir, uid, gid); err != nil {
+                               return fmt.Errorf("Couldn't change owner of directory %v: %v", destDir, err)
+                       }
+               }
+
                // Enumerate the files in source.
                dirEntries, err := ioutil.ReadDir(source)
                if err != nil {

@sean-
Copy link
Contributor

sean- commented Apr 17, 2017

This looks good except for the use of the syscall package. Instead, please find compatible syscalls from the golang.org/x/sys/unix package, which should address nearly all of the portability concerns. If you want to merge this with syscall instead, let me know and I'll follow up with a PR to use the aforementioned unix package instead.

Fixes #2552

Not needed when hardlinking. Only adds Linux support but other OS's may
be easy.
@schmichael schmichael force-pushed the b-2552-chown-on-copy branch from e4e7a4c to cabe9a6 Compare April 17, 2017 19:31
Also support getOwner on all Unixes as they all have `Stat_t.{U,G}id`
@schmichael schmichael force-pushed the b-2552-chown-on-copy branch from cabe9a6 to 17471bf Compare April 17, 2017 19:41
@schmichael
Copy link
Member Author

schmichael commented Apr 17, 2017

@dadgar Fixed! A quick peek reveals all Unixes we support have Stat_t.{U,G}id fields, so we can have a single getOwner implementation in fs_unix.go.

@jshuping Great catch. Fixed!

@sean- The only place we use syscall is to type assert the return value of FileInfo.Sys(). To switch to sys/unix I think we'd need to change a lot of our cross-platform os.Foo(). I hate our mess of fs_<OS>.go helper files, so any PRs to clean it up are welcome! Just noticed x/sys/unix.Stat_t is a type alias for syscall.Stat_t so sticking with the stdlib copy.

@dadgar
Copy link
Contributor

dadgar commented Apr 17, 2017

LGTM

@schmichael schmichael merged commit 55965cb into master Apr 19, 2017
@schmichael schmichael deleted the b-2552-chown-on-copy branch April 19, 2017 19:38
@github-actions
Copy link

github-actions bot commented Apr 2, 2023

I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants