Skip to content

Commit 2d0c736

Browse files
committed
unix: use fchmodat2 in Fchmodat
The fchmodat2 syscall was added in Linux kernel 6.6. Use it in Fchmodat if flags are given. It will return ENOSYS on older kernels (or EINVAL or any other bogus error in some container implementations). Fixes golang/go#61636 Change-Id: Ibbc9a08733b7186907b2fe5837cb997446c38357 Reviewed-on: https://go-review.googlesource.com/c/sys/+/539635 Reviewed-by: Heschi Kreinick <heschi@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Bryan Mills <bcmills@google.com> Reviewed-by: Mauri de Souza Meneguzzo <mauri870@gmail.com>
1 parent ec230da commit 2d0c736

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

unix/syscall_linux.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,23 @@ func FanotifyMark(fd int, flags uint, mask uint64, dirFd int, pathname string) (
6161
}
6262

6363
//sys fchmodat(dirfd int, path string, mode uint32) (err error)
64-
65-
func Fchmodat(dirfd int, path string, mode uint32, flags int) (err error) {
66-
// Linux fchmodat doesn't support the flags parameter. Mimick glibc's behavior
67-
// and check the flags. Otherwise the mode would be applied to the symlink
68-
// destination which is not what the user expects.
69-
if flags&^AT_SYMLINK_NOFOLLOW != 0 {
70-
return EINVAL
71-
} else if flags&AT_SYMLINK_NOFOLLOW != 0 {
72-
return EOPNOTSUPP
64+
//sys fchmodat2(dirfd int, path string, mode uint32, flags int) (err error)
65+
66+
func Fchmodat(dirfd int, path string, mode uint32, flags int) error {
67+
// Linux fchmodat doesn't support the flags parameter, but fchmodat2 does.
68+
// Try fchmodat2 if flags are specified.
69+
if flags != 0 {
70+
err := fchmodat2(dirfd, path, mode, flags)
71+
if err == ENOSYS {
72+
// fchmodat2 isn't available. If the flags are known to be valid,
73+
// return EOPNOTSUPP to indicate that fchmodat doesn't support them.
74+
if flags&^(AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) != 0 {
75+
return EINVAL
76+
} else if flags&(AT_SYMLINK_NOFOLLOW|AT_EMPTY_PATH) != 0 {
77+
return EOPNOTSUPP
78+
}
79+
}
80+
return err
7381
}
7482
return fchmodat(dirfd, path, mode)
7583
}

unix/zsyscall_linux.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)