Skip to content

Commit

Permalink
Merge pull request containers#11937 from flouthoc/overlay-rootfs-chown
Browse files Browse the repository at this point in the history
libpod: change mountpoint ownership when creating overlays on top of external rootfs
  • Loading branch information
openshift-merge-robot authored Oct 19, 2021
2 parents 139dca0 + 9500e11 commit 82fd299
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
23 changes: 21 additions & 2 deletions libpod/container_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import (
"github.com/containers/buildah/copier"
"github.com/containers/buildah/pkg/overlay"
butil "github.com/containers/buildah/util"
"github.com/containers/common/pkg/chown"
"github.com/containers/podman/v3/libpod/define"
"github.com/containers/podman/v3/libpod/events"
"github.com/containers/podman/v3/pkg/cgroups"
"github.com/containers/podman/v3/pkg/ctime"
"github.com/containers/podman/v3/pkg/hooks"
"github.com/containers/podman/v3/pkg/hooks/exec"
"github.com/containers/podman/v3/pkg/lookup"
"github.com/containers/podman/v3/pkg/rootless"
"github.com/containers/podman/v3/pkg/selinux"
"github.com/containers/podman/v3/pkg/util"
Expand Down Expand Up @@ -485,8 +487,12 @@ func (c *Container) setupStorage(ctx context.Context) error {
return errors.Wrapf(err, "error creating container storage")
}

c.config.IDMappings.UIDMap = containerInfo.UIDMap
c.config.IDMappings.GIDMap = containerInfo.GIDMap
// only reconfig IDMappings if layer was mounted from storage
// if its a external overlay do not reset IDmappings
if !c.config.RootfsOverlay {
c.config.IDMappings.UIDMap = containerInfo.UIDMap
c.config.IDMappings.GIDMap = containerInfo.GIDMap
}

processLabel, err := c.processLabel(containerInfo.ProcessLabel)
if err != nil {
Expand Down Expand Up @@ -1515,6 +1521,19 @@ func (c *Container) mountStorage() (_ string, deferredErr error) {
}

mountPoint = overlayMount.Source
execUser, err := lookup.GetUserGroupInfo(mountPoint, c.config.User, nil)
if err != nil {
return "", err
}
hostUID, hostGID, err := butil.GetHostIDs(util.IDtoolsToRuntimeSpec(c.config.IDMappings.UIDMap), util.IDtoolsToRuntimeSpec(c.config.IDMappings.GIDMap), uint32(execUser.Uid), uint32(execUser.Gid))
if err != nil {
return "", errors.Wrap(err, "unable to get host UID and host GID")
}

//note: this should not be recursive, if using external rootfs users should be responsible on configuring ownership.
if err := chown.ChangeHostPathOwnership(mountPoint, false, int(hostUID), int(hostGID)); err != nil {
return "", err
}
}

if mountPoint == "" {
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,18 @@ var _ = Describe("Podman run", func() {
startsession.WaitWithDefaultTimeout()
Expect(startsession).Should(Exit(0))
Expect(startsession.OutputToString()).To(Equal("hello"))

// remove container for above test overlay-foo
osession = podmanTest.Podman([]string{"rm", "overlay-foo"})
osession.WaitWithDefaultTimeout()
Expect(osession).Should(Exit(0))

// Test --rootfs with an external overlay with --uidmap
osession = podmanTest.Podman([]string{"run", "--uidmap", "0:1000:1000", "--rm", "--security-opt", "label=disable",
"--rootfs", rootfs + ":O", "echo", "hello"})
osession.WaitWithDefaultTimeout()
Expect(osession).Should(Exit(0))
Expect(osession.OutputToString()).To(Equal("hello"))
})

It("podman run a container with --init", func() {
Expand Down

0 comments on commit 82fd299

Please sign in to comment.