Skip to content

Commit 646efe7

Browse files
committed
utils: mkdirall: mask silently ignored mode bits to match os.MkdirAll
It turns out that the suid and sgid mode bits are silently ignored by Linux (though the sticky bit is honoured), and some users are requesting mode bits that are ignored. While returning an error (as securejoin does) makes some sense, this is a regression. Ref: cyphar/filepath-securejoin#23 Fixes: dd827f7 ("utils: switch to securejoin.MkdirAllHandle") Signed-off-by: Aleksa Sarai <[email protected]>
1 parent 457e1ff commit 646efe7

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

libcontainer/utils/utils_unix.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,14 @@ func MkdirAllInRootOpen(root, unsafePath string, mode uint32) (_ *os.File, Err e
319319
if mode&^0o7777 != 0 {
320320
return nil, fmt.Errorf("tried to include non-mode bits in MkdirAll mode: 0o%.3o", mode)
321321
}
322+
// Linux (and thus os.MkdirAll) silently ignores the suid and sgid bits if
323+
// passed. While it would make sense to return an error in that case (since
324+
// the user has asked for a mode that won't be applied), for compatibility
325+
// reasons we have to ignore these bits.
326+
if ignoredBits := mode &^ 0o1777; ignoredBits != 0 {
327+
logrus.Warnf("MkdirAll called with no-op mode bits that are ignored by Linux: 0o%.3o", ignoredBits)
328+
mode &= 0o1777
329+
}
322330

323331
rootDir, err := os.OpenFile(root, unix.O_DIRECTORY|unix.O_CLOEXEC, 0)
324332
if err != nil {

0 commit comments

Comments
 (0)