Skip to content

Commit 97b37f9

Browse files
sipsmatonistiigi
authored andcommitted
diffapply: do chown before xattrs
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 <erik@sipsma.dev> (cherry picked from commit 0a36f1a) Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
1 parent 17401b5 commit 97b37f9

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

snapshot/diffapply_unix.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,18 @@ func (a *applier) applyCopy(ctx context.Context, ca *changeApply) error {
379379
return errors.Errorf("unhandled file type %d during merge at path %q", ca.srcStat.Mode&unix.S_IFMT, ca.srcPath)
380380
}
381381

382+
// NOTE: it's important that chown happens before setting xattrs due to the fact that chown will
383+
// reset the security.capabilities xattr which results in file capabilities being lost.
384+
if err := os.Lchown(ca.dstPath, int(ca.srcStat.Uid), int(ca.srcStat.Gid)); err != nil {
385+
return errors.Wrap(err, "failed to chown during apply")
386+
}
387+
388+
if ca.srcStat.Mode&unix.S_IFMT != unix.S_IFLNK {
389+
if err := unix.Chmod(ca.dstPath, ca.srcStat.Mode); err != nil {
390+
return errors.Wrapf(err, "failed to chmod path %q during apply", ca.dstPath)
391+
}
392+
}
393+
382394
if ca.srcPath != "" {
383395
xattrs, err := sysx.LListxattr(ca.srcPath)
384396
if err != nil {
@@ -410,16 +422,6 @@ func (a *applier) applyCopy(ctx context.Context, ca *changeApply) error {
410422
}
411423
}
412424

413-
if err := os.Lchown(ca.dstPath, int(ca.srcStat.Uid), int(ca.srcStat.Gid)); err != nil {
414-
return errors.Wrap(err, "failed to chown during apply")
415-
}
416-
417-
if ca.srcStat.Mode&unix.S_IFMT != unix.S_IFLNK {
418-
if err := unix.Chmod(ca.dstPath, ca.srcStat.Mode); err != nil {
419-
return errors.Wrapf(err, "failed to chmod path %q during apply", ca.dstPath)
420-
}
421-
}
422-
423425
atimeSpec := unix.Timespec{Sec: ca.srcStat.Atim.Sec, Nsec: ca.srcStat.Atim.Nsec}
424426
mtimeSpec := unix.Timespec{Sec: ca.srcStat.Mtim.Sec, Nsec: ca.srcStat.Mtim.Nsec}
425427
if ca.srcStat.Mode&unix.S_IFMT != unix.S_IFDIR {

0 commit comments

Comments
 (0)