Skip to content

Commit

Permalink
volumes: do not recurse when chowning
Browse files Browse the repository at this point in the history
keep the file ownership when chowning and honor the user namespace
mappings.

Closes: containers#7130

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
  • Loading branch information
giuseppe committed Jul 30, 2020
1 parent 4132b71 commit bbff6df
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions libpod/container_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1534,9 +1534,6 @@ func (c *Container) chownVolume(volumeName string) error {
return errors.Wrapf(err, "error retrieving named volume %s for container %s", volumeName, c.ID())
}

uid := int(c.config.Spec.Process.User.UID)
gid := int(c.config.Spec.Process.User.GID)

vol.lock.Lock()
defer vol.lock.Unlock()

Expand All @@ -1547,22 +1544,34 @@ func (c *Container) chownVolume(volumeName string) error {

if vol.state.NeedsChown {
vol.state.NeedsChown = false

uid := int(c.config.Spec.Process.User.UID)
gid := int(c.config.Spec.Process.User.GID)

if c.config.IDMappings.UIDMap != nil {
p := idtools.IDPair{
UID: uid,
GID: gid,
}
mappings := idtools.NewIDMappingsFromMaps(c.config.IDMappings.UIDMap, c.config.IDMappings.GIDMap)
newPair, err := mappings.ToHost(p)
if err != nil {
return errors.Wrapf(err, "error mapping user %d:%d", uid, gid)
}
uid = newPair.UID
gid = newPair.GID
}

vol.state.UIDChowned = uid
vol.state.GIDChowned = gid

if err := vol.save(); err != nil {
return err
}
err := filepath.Walk(vol.MountPoint(), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if err := os.Lchown(path, uid, gid); err != nil {
return err
}
return nil
})
if err != nil {

mountPoint := vol.MountPoint()

if err := os.Lchown(mountPoint, uid, gid); err != nil {
return err
}
}
Expand Down

0 comments on commit bbff6df

Please sign in to comment.