Chown files when copying into chroot

Fixes #2552

Not needed when hardlinking. Only adds Linux support but other OS's may
be easy.
This commit is contained in:
Michael Schurter 2017-04-12 12:31:00 -07:00
parent 0c72aa9bd2
commit 547d5a81b4
8 changed files with 49 additions and 11 deletions

View File

@ -385,7 +385,9 @@ func getFileWatcher(path string) watch.FileWatcher {
return watch.NewPollingFileWatcher(path) return watch.NewPollingFileWatcher(path)
} }
func fileCopy(src, dst string, perm os.FileMode) error { // fileCopy from src to dst setting the permissions and owner (if uid & gid are
// both greater than 0)
func fileCopy(src, dst string, uid, gid int, perm os.FileMode) error {
// Do a simple copy. // Do a simple copy.
srcFile, err := os.Open(src) srcFile, err := os.Open(src)
if err != nil { if err != nil {
@ -400,7 +402,13 @@ func fileCopy(src, dst string, perm os.FileMode) error {
defer dstFile.Close() defer dstFile.Close()
if _, err := io.Copy(dstFile, srcFile); err != nil { if _, err := io.Copy(dstFile, srcFile); err != nil {
return fmt.Errorf("Couldn't copy %v to %v: %v", src, dst, err) return fmt.Errorf("Couldn't copy %q to %q: %v", src, dst, err)
}
if uid >= 0 && gid >= 0 {
if err := dstFile.Chown(uid, gid); err != nil {
return fmt.Errorf("Couldn't copy %q to %q: %v", src, dst, err)
}
} }
return nil return nil

View File

@ -24,3 +24,8 @@ func createSecretDir(dir string) error {
func removeSecretDir(dir string) error { func removeSecretDir(dir string) error {
return os.RemoveAll(dir) return os.RemoveAll(dir)
} }
// getOwner isn't implemented for Darwin
func getOwner(os.FileInfo) (int, int) {
return -1, -1
}

View File

@ -24,3 +24,8 @@ func createSecretDir(dir string) error {
func removeSecretDir(dir string) error { func removeSecretDir(dir string) error {
return os.RemoveAll(dir) return os.RemoveAll(dir)
} }
// getOwner isn't implemented for FreeBSD
func getOwner(os.FileInfo) (int, int) {
return -1, -1
}

View File

@ -88,3 +88,11 @@ func removeSecretDir(dir string) error {
} }
return os.RemoveAll(dir) return os.RemoveAll(dir)
} }
func getOwner(fi os.FileInfo) (int, int) {
stat, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return -1, -1
}
return int(stat.Uid), int(stat.Gid)
}

View File

@ -25,3 +25,8 @@ func createSecretDir(dir string) error {
func removeSecretDir(dir string) error { func removeSecretDir(dir string) error {
return os.RemoveAll(dir) return os.RemoveAll(dir)
} }
// getOwner isn't implemented for Solaris
func getOwner(os.FileInfo) (int, int) {
return -1, -1
}

View File

@ -82,7 +82,7 @@ func getGid(u *user.User) (int, error) {
// linkOrCopy attempts to hardlink dst to src and fallsback to copying if the // linkOrCopy attempts to hardlink dst to src and fallsback to copying if the
// hardlink fails. // hardlink fails.
func linkOrCopy(src, dst string, perm os.FileMode) error { func linkOrCopy(src, dst string, uid, gid int, perm os.FileMode) error {
// Avoid link/copy if the file already exists in the chroot // Avoid link/copy if the file already exists in the chroot
// TODO 0.6 clean this up. This was needed because chroot creation fails // TODO 0.6 clean this up. This was needed because chroot creation fails
// when a process restarts. // when a process restarts.
@ -90,9 +90,9 @@ func linkOrCopy(src, dst string, perm os.FileMode) error {
return nil return nil
} }
// Attempt to hardlink. // Attempt to hardlink.
if err := os.Link(src, dst); err == nil { //if err := os.Link(src, dst); err == nil {
return nil // return nil
} //}
return fileCopy(src, dst, perm) return fileCopy(src, dst, uid, gid, perm)
} }

View File

@ -21,8 +21,8 @@ var (
) )
// linkOrCopy is always copies dst to src on Windows. // linkOrCopy is always copies dst to src on Windows.
func linkOrCopy(src, dst string, perm os.FileMode) error { func linkOrCopy(src, dst string, uid, gid int, perm os.FileMode) error {
return fileCopy(src, dst, perm) return fileCopy(src, dst, uid, gid, perm)
} }
// The windows version does nothing currently. // The windows version does nothing currently.
@ -70,3 +70,8 @@ func MountSpecialDirs(taskDir string) error {
func unmountSpecialDirs(taskDir string) error { func unmountSpecialDirs(taskDir string) error {
return nil return nil
} }
// getOwner doesn't work on Windows as Windows doesn't use int user IDs
func getOwner(os.FileInfo) (int, int) {
return -1, -1
}

View File

@ -163,7 +163,8 @@ func (t *TaskDir) embedDirs(entries map[string]string) error {
// Copy the file. // Copy the file.
taskEntry := filepath.Join(t.Dir, dest) taskEntry := filepath.Join(t.Dir, dest)
if err := linkOrCopy(source, taskEntry, s.Mode().Perm()); err != nil { uid, gid := getOwner(s)
if err := linkOrCopy(source, taskEntry, uid, gid, s.Mode().Perm()); err != nil {
return err return err
} }
@ -217,7 +218,8 @@ func (t *TaskDir) embedDirs(entries map[string]string) error {
continue continue
} }
if err := linkOrCopy(hostEntry, taskEntry, entry.Mode().Perm()); err != nil { uid, gid := getOwner(entry)
if err := linkOrCopy(hostEntry, taskEntry, uid, gid, entry.Mode().Perm()); err != nil {
return err return err
} }
} }