@@ -693,7 +693,8 @@ func (fs *filesystem) LinkAt(ctx context.Context, rp *vfs.ResolvingPath, vd vfs.
693693 return err
694694 }
695695 }
696- if err := vfsObj .LinkAt (ctx , fs .creds , & vfs.PathOperation {
696+ createCreds := parent .credsForCreate (rp .Credentials (), parent .isSGIDSet ())
697+ if err := vfsObj .LinkAt (ctx , createCreds , & vfs.PathOperation {
697698 Root : old .upperVD ,
698699 Start : old .upperVD ,
699700 }, & newpop ); err != nil {
@@ -702,21 +703,6 @@ func (fs *filesystem) LinkAt(ctx context.Context, rp *vfs.ResolvingPath, vd vfs.
702703 }
703704 return err
704705 }
705- creds := rp .Credentials ()
706- if err := vfsObj .SetStatAt (ctx , fs .creds , & newpop , & vfs.SetStatOptions {
707- Stat : linux.Statx {
708- Mask : linux .STATX_UID | linux .STATX_GID ,
709- UID : uint32 (creds .EffectiveKUID ),
710- GID : uint32 (creds .EffectiveKGID ),
711- },
712- }); err != nil {
713- if cleanupErr := vfsObj .UnlinkAt (ctx , fs .creds , & newpop ); cleanupErr != nil {
714- panic (fmt .Sprintf ("unrecoverable overlayfs inconsistency: failed to delete upper layer file after LinkAt metadata update failure: %v" , cleanupErr ))
715- } else if haveUpperWhiteout {
716- fs .cleanupRecreateWhiteout (ctx , vfsObj , & newpop )
717- }
718- return err
719- }
720706 old .watches .Notify (ctx , "" , linux .IN_ATTRIB , 0 /* cookie */ , vfs .InodeEvent , false /* unlinked */ )
721707 return nil
722708 })
@@ -740,23 +726,19 @@ func (fs *filesystem) MkdirAt(ctx context.Context, rp *vfs.ResolvingPath, opts v
740726 return err
741727 }
742728 }
743- if err := vfsObj .MkdirAt (ctx , fs .creds , & pop , & opts ); err != nil {
729+ sgidSet := parent .isSGIDSet ()
730+ if sgidSet {
731+ // Directories inherit the SGID bit.
732+ opts .Mode |= linux .ModeSetGID
733+ }
734+ createCreds := parent .credsForCreate (rp .Credentials (), sgidSet )
735+ if err := vfsObj .MkdirAt (ctx , createCreds , & pop , & opts ); err != nil {
744736 if haveUpperWhiteout {
745737 fs .cleanupRecreateWhiteout (ctx , vfsObj , & pop )
746738 }
747739 return err
748740 }
749741
750- if err := vfsObj .SetStatAt (ctx , fs .creds , & pop , & vfs.SetStatOptions {
751- Stat : parent .newChildOwnerStat (opts .Mode , rp .Credentials ()),
752- }); err != nil {
753- if cleanupErr := vfsObj .RmdirAt (ctx , fs .creds , & pop ); cleanupErr != nil {
754- panic (fmt .Sprintf ("unrecoverable overlayfs inconsistency: failed to delete upper layer directory after MkdirAt metadata update failure: %v" , cleanupErr ))
755- } else if haveUpperWhiteout {
756- fs .cleanupRecreateWhiteout (ctx , vfsObj , & pop )
757- }
758- return err
759- }
760742 if haveUpperWhiteout {
761743 // A whiteout is being replaced with this new directory. There may be
762744 // directories on lower layers (previously hidden by the whiteout) that
@@ -807,23 +789,13 @@ func (fs *filesystem) MknodAt(ctx context.Context, rp *vfs.ResolvingPath, opts v
807789 return err
808790 }
809791 }
810- if err := vfsObj .MknodAt (ctx , fs .creds , & pop , & opts ); err != nil {
792+ createCreds := parent .credsForCreate (rp .Credentials (), parent .isSGIDSet ())
793+ if err := vfsObj .MknodAt (ctx , createCreds , & pop , & opts ); err != nil {
811794 if haveUpperWhiteout {
812795 fs .cleanupRecreateWhiteout (ctx , vfsObj , & pop )
813796 }
814797 return err
815798 }
816- creds := rp .Credentials ()
817- if err := vfsObj .SetStatAt (ctx , fs .creds , & pop , & vfs.SetStatOptions {
818- Stat : parent .newChildOwnerStat (opts .Mode , creds ),
819- }); err != nil {
820- if cleanupErr := vfsObj .UnlinkAt (ctx , fs .creds , & pop ); cleanupErr != nil {
821- panic (fmt .Sprintf ("unrecoverable overlayfs inconsistency: failed to delete upper layer file after MknodAt metadata update failure: %v" , cleanupErr ))
822- } else if haveUpperWhiteout {
823- fs .cleanupRecreateWhiteout (ctx , vfsObj , & pop )
824- }
825- return err
826- }
827799 return nil
828800 })
829801}
@@ -1027,7 +999,8 @@ func (fs *filesystem) createAndOpenLocked(ctx context.Context, rp *vfs.Resolving
1027999 }
10281000 }
10291001 // Create the file on the upper layer, and get an FD representing it.
1030- upperFD , err := vfsObj .OpenAt (ctx , fs .creds , & pop , & vfs.OpenOptions {
1002+ createCreds := parent .credsForCreate (creds , parent .isSGIDSet ())
1003+ upperFD , err := vfsObj .OpenAt (ctx , createCreds , & pop , & vfs.OpenOptions {
10311004 Flags : opts .Flags &^vfs .FileCreationFlags | linux .O_CREAT | linux .O_EXCL ,
10321005 Mode : opts .Mode ,
10331006 })
@@ -1038,18 +1011,6 @@ func (fs *filesystem) createAndOpenLocked(ctx context.Context, rp *vfs.Resolving
10381011 return nil , err
10391012 }
10401013
1041- // Change the file's owner to the caller. We can't use upperFD.SetStat()
1042- // because it will pick up creds from ctx.
1043- if err := vfsObj .SetStatAt (ctx , fs .creds , & pop , & vfs.SetStatOptions {
1044- Stat : parent .newChildOwnerStat (opts .Mode , creds ),
1045- }); err != nil {
1046- if cleanupErr := vfsObj .UnlinkAt (ctx , fs .creds , & pop ); cleanupErr != nil {
1047- panic (fmt .Sprintf ("unrecoverable overlayfs inconsistency: failed to delete upper layer file after OpenAt(O_CREAT) metadata update failure: %v" , cleanupErr ))
1048- } else if haveUpperWhiteout {
1049- fs .cleanupRecreateWhiteout (ctx , vfsObj , & pop )
1050- }
1051- return nil , err
1052- }
10531014 // Re-lookup to get a dentry representing the new file, which is needed for
10541015 // the returned FD.
10551016 child , _ , err := fs .getChildLocked (ctx , parent , childName , ds )
@@ -1609,27 +1570,13 @@ func (fs *filesystem) SymlinkAt(ctx context.Context, rp *vfs.ResolvingPath, targ
16091570 return err
16101571 }
16111572 }
1612- if err := vfsObj .SymlinkAt (ctx , fs .creds , & pop , target ); err != nil {
1573+ createCreds := parent .credsForCreate (rp .Credentials (), parent .isSGIDSet ())
1574+ if err := vfsObj .SymlinkAt (ctx , createCreds , & pop , target ); err != nil {
16131575 if haveUpperWhiteout {
16141576 fs .cleanupRecreateWhiteout (ctx , vfsObj , & pop )
16151577 }
16161578 return err
16171579 }
1618- creds := rp .Credentials ()
1619- if err := vfsObj .SetStatAt (ctx , fs .creds , & pop , & vfs.SetStatOptions {
1620- Stat : linux.Statx {
1621- Mask : linux .STATX_UID | linux .STATX_GID ,
1622- UID : uint32 (creds .EffectiveKUID ),
1623- GID : uint32 (creds .EffectiveKGID ),
1624- },
1625- }); err != nil {
1626- if cleanupErr := vfsObj .UnlinkAt (ctx , fs .creds , & pop ); cleanupErr != nil {
1627- panic (fmt .Sprintf ("unrecoverable overlayfs inconsistency: failed to delete upper layer file after SymlinkAt metadata update failure: %v" , cleanupErr ))
1628- } else if haveUpperWhiteout {
1629- fs .cleanupRecreateWhiteout (ctx , vfsObj , & pop )
1630- }
1631- return err
1632- }
16331580 return nil
16341581 })
16351582}
0 commit comments