Skip to content

Commit

Permalink
diffapply: do chown before xattrs
Browse files Browse the repository at this point in the history
Chown will result file capabilities getting reset, so it should be done
before setting xattrs to ensure they are retained.

Signed-off-by: Erik Sipsma <[email protected]>
(cherry picked from commit 0a36f1a)
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
sipsma authored and tonistiigi committed Feb 28, 2023
1 parent 1705562 commit 7b09afd
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions snapshot/diffapply_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,18 @@ func (a *applier) applyCopy(ctx context.Context, ca *changeApply) error {
return errors.Errorf("unhandled file type %d during merge at path %q", ca.srcStat.Mode&unix.S_IFMT, ca.srcPath)
}

// NOTE: it's important that chown happens before setting xattrs due to the fact that chown will
// reset the security.capabilities xattr which results in file capabilities being lost.
if err := os.Lchown(ca.dstPath, int(ca.srcStat.Uid), int(ca.srcStat.Gid)); err != nil {
return errors.Wrap(err, "failed to chown during apply")
}

if ca.srcStat.Mode&unix.S_IFMT != unix.S_IFLNK {
if err := unix.Chmod(ca.dstPath, ca.srcStat.Mode); err != nil {
return errors.Wrapf(err, "failed to chmod path %q during apply", ca.dstPath)
}
}

if ca.srcPath != "" {
xattrs, err := sysx.LListxattr(ca.srcPath)
if err != nil {
Expand Down Expand Up @@ -410,16 +422,6 @@ func (a *applier) applyCopy(ctx context.Context, ca *changeApply) error {
}
}

if err := os.Lchown(ca.dstPath, int(ca.srcStat.Uid), int(ca.srcStat.Gid)); err != nil {
return errors.Wrap(err, "failed to chown during apply")
}

if ca.srcStat.Mode&unix.S_IFMT != unix.S_IFLNK {
if err := unix.Chmod(ca.dstPath, ca.srcStat.Mode); err != nil {
return errors.Wrapf(err, "failed to chmod path %q during apply", ca.dstPath)
}
}

atimeSpec := unix.Timespec{Sec: ca.srcStat.Atim.Sec, Nsec: ca.srcStat.Atim.Nsec}
mtimeSpec := unix.Timespec{Sec: ca.srcStat.Mtim.Sec, Nsec: ca.srcStat.Mtim.Nsec}
if ca.srcStat.Mode&unix.S_IFMT != unix.S_IFDIR {
Expand Down

0 comments on commit 7b09afd

Please sign in to comment.