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

Fix copy chown settings to not default to real root #20446

Merged
merged 1 commit into from
Feb 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions daemon/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,13 @@ func (daemon *Daemon) containerExtractToDir(container *container.Container, path
return ErrRootFSReadOnly
}

uid, gid := daemon.GetRemappedUIDGID()
options := &archive.TarOptions{
NoOverwriteDirNonDir: noOverwriteDirNonDir,
ChownOpts: &archive.TarChownOptions{
UID: 0, GID: 0, // TODO: use config.User? Remap to userns root?
UID: uid, GID: gid, // TODO: should all ownership be set to root (either real or remapped)?
},
NoOverwriteDirNonDir: noOverwriteDirNonDir,
}

if err := chrootarchive.Untar(content, resolvedPath, options); err != nil {
return err
}
Expand Down
39 changes: 39 additions & 0 deletions integration-cli/docker_cli_cp_to_container_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// +build !windows

package main

import (
"fmt"
"os"
"path/filepath"

"github.com/docker/docker/pkg/integration/checker"
"github.com/docker/docker/pkg/system"
"github.com/go-check/check"
)

// Check ownership is root, both in non-userns and userns enabled modes
func (s *DockerSuite) TestCpCheckDestOwnership(c *check.C) {
testRequires(c, DaemonIsLinux, SameHostDaemon)
tmpVolDir := getTestDir(c, "test-cp-tmpvol")
containerID := makeTestContainer(c,
testContainerOptions{volumes: []string{fmt.Sprintf("%s:/tmpvol", tmpVolDir)}})

tmpDir := getTestDir(c, "test-cp-to-check-ownership")
defer os.RemoveAll(tmpDir)

makeTestContentInDir(c, tmpDir)

srcPath := cpPath(tmpDir, "file1")
dstPath := containerCpPath(containerID, "/tmpvol", "file1")

err := runDockerCp(c, srcPath, dstPath)
c.Assert(err, checker.IsNil)

stat, err := system.Stat(filepath.Join(tmpVolDir, "file1"))
c.Assert(err, checker.IsNil)
uid, gid, err := getRootUIDGID()
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use idtools.GetRootUIDGID here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, that would require that I have the mappings from within the daemon, but the tests can be run as a separate client (and in this case they are)--so the only option is to parse the output of what the server will tell me about itself.. which in this case is the fact we know the root dir of the daemon contains the root uid, gid info..

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense.

TestDaemonUserNamespaceRootSetting is doing something similar and can be updated to use this new function.

Also, how is this testing against a userns enabled daemon, as mentioned in the function comment header?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TestDaemonUserNamespaceRootSetting is doing something similar and can be updated to use this new function.

True--absolutely!

Also, how is this testing against a userns enabled daemon, as mentioned in the function comment header?

Because one of our runs of make test-integration-cli in CI sets the DOCKER_REMAP_ROOT setting and runs all tests against a daemon with userns enabled; appropriately named userns :)

c.Assert(err, checker.IsNil)
c.Assert(stat.UID(), checker.Equals, uint32(uid), check.Commentf("Copied file not owned by container root UID"))
c.Assert(stat.GID(), checker.Equals, uint32(gid), check.Commentf("Copied file not owned by container root GID"))
}
17 changes: 17 additions & 0 deletions integration-cli/docker_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,23 @@ func runSleepingContainerInImage(c *check.C, image string, extraArgs ...string)
return dockerCmd(c, args...)
}

func getRootUIDGID() (int, int, error) {
uidgid := strings.Split(filepath.Base(dockerBasePath), ".")
if len(uidgid) == 1 {
//user namespace remapping is not turned on; return 0
return 0, 0, nil
}
uid, err := strconv.Atoi(uidgid[0])
if err != nil {
return 0, 0, err
}
gid, err := strconv.Atoi(uidgid[1])
if err != nil {
return 0, 0, err
}
return uid, gid, nil
}

// minimalBaseImage returns the name of the minimal base image for the current
// daemon platform.
func minimalBaseImage() string {
Expand Down