vfs: add fchmodat2 syscall
Subsystems (17)
Rich Felker
maintainerPATCH 1/1 vfs: add fchmodat2 syscall
POSIX defines fchmodat as having a 4th argument, flags, that can be AT_SYMLINK_NOFOLLOW. Support for changing the access mode of symbolic links is optional (EOPNOTSUPP allowed if not supported), but this flag is important even on systems where symlinks do not have access modes, since it's the only way to safely change the mode of a file which might be asynchronously replaced with a symbolic link, without a race condition whereby the link target is changed.
It's possible to emulate AT_SYMLINK_NOFOLLOW in userspace, and both musl libc and glibc do this, by opening an O_PATH file descriptor and performing chmod on the corresponding magic symlink in /proc/self/fd. However, this requires procfs to be mounted and accessible.
It was determined (see glibc issue #14578 and commit a492b1e5ef) that, on some filesystems, performing chmod on the link itself produces a change in the inode's access mode, but returns an EOPNOTSUPP error. This is non-conforming and wrong. Rather than try to fix all the broken filesystem backends, block attempts to change the symlink access mode via fchmodat2 at the frontend layer. This matches the userspace emulation done in libc implementations. No change is made to the underlying chmod_common(), so it's still possible to attempt changes via procfs, if desired. If at some point all filesystems have been fixed, this could be relaxed to allow filesystems to make their own decision whether changing access mode of links is supported.
--- arch/alpha/kernel/syscalls/syscall.tbl | 1 + arch/arm/tools/syscall.tbl | 1 + arch/arm64/include/asm/unistd.h | 2 +- arch/arm64/include/asm/unistd32.h | 2 ++ arch/ia64/kernel/syscalls/syscall.tbl | 1 + arch/m68k/kernel/syscalls/syscall.tbl | 1 + arch/microblaze/kernel/syscalls/syscall.tbl | 1 + arch/mips/kernel/syscalls/syscall_n32.tbl | 1 + arch/mips/kernel/syscalls/syscall_n64.tbl | 1 + arch/mips/kernel/syscalls/syscall_o32.tbl | 1 + arch/parisc/kernel/syscalls/syscall.tbl | 1 + arch/powerpc/kernel/syscalls/syscall.tbl | 1 + arch/s390/kernel/syscalls/syscall.tbl | 1 + arch/sh/kernel/syscalls/syscall.tbl | 1 + arch/sparc/kernel/syscalls/syscall.tbl | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + arch/xtensa/kernel/syscalls/syscall.tbl | 1 + fs/open.c | 29 ++++++++++++++++++--- include/linux/syscalls.h | 2 ++ include/uapi/asm-generic/unistd.h | 4 ++- 21 files changed, 49 insertions(+), 6 deletions(-)
arch/alpha/kernel/syscalls/syscall.tbl +1 -0
@@ -479,3 +479,4 @@ 547 common openat2 sys_openat2 548 common pidfd_getfd sys_pidfd_getfd 549 common faccessat2 sys_faccessat2 +550 common fchmodat2 sys_fchmodat2
arch/arm/tools/syscall.tbl +1 -0
@@ -453,3 +453,4 @@ 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd 439 common faccessat2 sys_faccessat2 +440 common fchmodat2 sys_fchmodat2
arch/arm64/include/asm/unistd.h +1 -1
@@ -38,7 +38,7 @@#define __ARM_NR_compat_set_tls (__ARM_NR_COMPAT_BASE + 5)#define __ARM_NR_COMPAT_END (__ARM_NR_COMPAT_BASE + 0x800) -#define __NR_compat_syscalls 440 +#define __NR_compat_syscalls 441#endif #define __ARCH_WANT_SYS_CLONE
arch/arm64/include/asm/unistd32.h +2 -0
@@ -887,6 +887,8 @@ __SYSCALL(__NR_openat2, sys_openat2) __SYSCALL(__NR_pidfd_getfd, sys_pidfd_getfd)#define __NR_faccessat2 439 __SYSCALL(__NR_faccessat2, sys_faccessat2) +#define __NR_fchmodat2 440 +__SYSCALL (__NR_fchmodat2, sys_fchmodat2)/* * Please add new compat syscalls above this comment and update
arch/ia64/kernel/syscalls/syscall.tbl +1 -0
@@ -360,3 +360,4 @@ 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd 439 common faccessat2 sys_faccessat2 +440 common fchmodat2 sys_fchmodat2
arch/m68k/kernel/syscalls/syscall.tbl +1 -0
@@ -439,3 +439,4 @@ 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd 439 common faccessat2 sys_faccessat2 +440 common fchmodat2 sys_fchmodat2
arch/microblaze/kernel/syscalls/syscall.tbl +1 -0
@@ -445,3 +445,4 @@ 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd 439 common faccessat2 sys_faccessat2 +440 common fchmodat2 sys_fchmodat2
arch/mips/kernel/syscalls/syscall_n32.tbl +1 -0
@@ -378,3 +378,4 @@ 437 n32 openat2 sys_openat2 438 n32 pidfd_getfd sys_pidfd_getfd 439 n32 faccessat2 sys_faccessat2 +440 n32 fchmodat2 sys_fchmodat2
arch/mips/kernel/syscalls/syscall_n64.tbl +1 -0
@@ -354,3 +354,4 @@ 437 n64 openat2 sys_openat2 438 n64 pidfd_getfd sys_pidfd_getfd 439 n64 faccessat2 sys_faccessat2 +440 n64 fchmodat2 sys_fchmodat2
arch/mips/kernel/syscalls/syscall_o32.tbl +1 -0
@@ -427,3 +427,4 @@ 437 o32 openat2 sys_openat2 438 o32 pidfd_getfd sys_pidfd_getfd 439 o32 faccessat2 sys_faccessat2 +440 o32 fchmodat2 sys_fchmodat2
arch/parisc/kernel/syscalls/syscall.tbl +1 -0
@@ -437,3 +437,4 @@ 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd 439 common faccessat2 sys_faccessat2 +440 common fchmodat2 sys_fchmodat2
arch/powerpc/kernel/syscalls/syscall.tbl +1 -0
@@ -529,3 +529,4 @@ 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd 439 common faccessat2 sys_faccessat2 +440 common fchmodat2 sys_fchmodat2
arch/s390/kernel/syscalls/syscall.tbl +1 -0
@@ -442,3 +442,4 @@ 437 common openat2 sys_openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd sys_pidfd_getfd 439 common faccessat2 sys_faccessat2 sys_faccessat2 +440 common fchmodat2 sys_fchmodat2 sys_fchmodat2
arch/sh/kernel/syscalls/syscall.tbl +1 -0
@@ -442,3 +442,4 @@ 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd 439 common faccessat2 sys_faccessat2 +440 common fchmodat2 sys_fchmodat2
arch/sparc/kernel/syscalls/syscall.tbl +1 -0
@@ -485,3 +485,4 @@ 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd 439 common faccessat2 sys_faccessat2 +440 common fchmodat2 sys_fchmodat2
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
@@ -444,3 +444,4 @@ 437 i386 openat2 sys_openat2 438 i386 pidfd_getfd sys_pidfd_getfd 439 i386 faccessat2 sys_faccessat2 +440 i386 fchmodat2 sys_fchmodat2
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -361,6 +361,7 @@ 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd 439 common faccessat2 sys_faccessat2 +440 common fchmodat2 sys_fchmodat2 # # x32-specific system call numbers start at 512 to avoid cache impact
arch/xtensa/kernel/syscalls/syscall.tbl +1 -0
@@ -410,3 +410,4 @@ 437 common openat2 sys_openat2 438 common pidfd_getfd sys_pidfd_getfd 439 common faccessat2 sys_faccessat2 +440 common fchmodat2 sys_fchmodat2
fs/open.c +25 -4
@@ -610,15 +610,30 @@ SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)return err; } -static int do_fchmodat (int dfd,const char __user *filename,umode_t mode) +static int do_fchmodat (int dfd,const char __user *filename,umode_t mode,int flags) {struct path path;int error;unsigned int lookup_flags =LOOKUP_FOLLOW ; + +if (flags &AT_SYMLINK_NOFOLLOW ) + lookup_flags &= ~LOOKUP_FOLLOW ; +if (flags & ~AT_SYMLINK_NOFOLLOW ) +return -EINVAL ; retry: error =user_path_at (dfd, filename, lookup_flags, &path);if (!error) { - error =chmod_common (&path, mode); +/* Block chmod from getting to fs layer. Ideally the +* fs would either allow it or fail with EOPNOTSUPP, +* but some are buggy and return an error but change +* the mode, which is non-conforming and wrong. +* Userspace emulation of AT_SYMLINK_NOFOLLOW in +* glibc and musl blocked it too, for same reason. */ +if (S_ISLNK (path.dentry->d_inode->i_mode) + && (flags &AT_SYMLINK_NOFOLLOW )) + error = -EOPNOTSUPP ; +else + error =chmod_common (&path, mode);path_put (&path);if (retry_estale (error, lookup_flags)) { lookup_flags |=LOOKUP_REVAL ;
@@ -628,15 +643,21 @@ static int do_fchmodat(int dfd, const char __user *filename, umode_t mode)return error; } +SYSCALL_DEFINE4(fchmodat2, int, dfd, const char __user *, filename, + umode_t, mode, int, flags) +{ +return do_fchmodat (dfd, filename, mode, flags); +} + SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode) { -return do_fchmodat (dfd, filename, mode); +return do_fchmodat (dfd, filename, mode,0 ); } SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode) { -return do_fchmodat (AT_FDCWD , filename, mode); +return do_fchmodat (AT_FDCWD , filename, mode,0 ); }int chown_common (const struct path *path,uid_t user,gid_t group)
include/linux/syscalls.h +2 -0
@@ -435,6 +435,8 @@ asmlinkage long sys_chroot(const char __user *filename);asmlinkage long sys_fchmod (unsigned int fd,umode_t mode);asmlinkage long sys_fchmodat (int dfd,const char __user * filename,umode_t mode); +asmlinkage long sys_fchmodat2 (int dfd,const char __user * filename, +umode_t mode,int flags);asmlinkage long sys_fchownat (int dfd,const char __user *filename,uid_t user,gid_t group,int flag);asmlinkage long sys_fchown (unsigned int fd,uid_t user,gid_t group);
include/uapi/asm-generic/unistd.h +3 -1
@@ -859,9 +859,11 @@ __SYSCALL(__NR_openat2, sys_openat2) __SYSCALL(__NR_pidfd_getfd, sys_pidfd_getfd)#define __NR_faccessat2 439 __SYSCALL(__NR_faccessat2, sys_faccessat2) +#define __NR_fchmodat2 440 +__SYSCALL(__NR_fchmodat2, sys_fchmodat2)#undef __NR_syscalls -#define __NR_syscalls 440 +#define __NR_syscalls 441/* * 32 bit systems traditionally used different
Signature · 1 line
2.21.0
Al Viro
maintainerQuoted 3 lines · Rich Felker
It was determined (see glibc issue #14578 and commit
a492b1e5ef) that, on some filesystems, performing chmod on the link itself produces a change in the inode's access mode, but returns an EOPNOTSUPP error.
Which filesystem types are those?
Rich Felker
maintainerQuoted 7 lines · Al Viro
Quoted 3 lines · Rich Felker
It was determined (see glibc issue #14578 and commit
a492b1e5ef) that, on some filesystems, performing chmod on the link itself produces a change in the inode's access mode, but returns an EOPNOTSUPP error.Which filesystem types are those?
It's been a long time and I don't know if the details were recorded. It was reported for xfs but I believe we later found it happening for others. See:
https://sourceware.org/bugzilla/show_bug.cgi?id=14578#c17
https://sourceware.org/legacy-ml/libc-alpha/2020-02/msg00467.html
and especially:
https://sourceware.org/legacy-ml/libc-alpha/2020-02/msg00518.html
where Christoph seems to have endorsed the approach in my patch. I'm fine with doing it differently if you'd prefer, though.
Rich
Quoted 3 lines · Rich Felker
userspace emulation done in libc implementations. No change is made to the underlying
chmod_common(), so it's still possible to attempt changes via procfs, if desired.
And that is the goddamn problem. We need to fix that _first_. After that we can add sugarcoating using new syscalls if needed.
Rich Felker
maintainerQuoted 6 lines · Christoph Hellwig
Quoted 3 lines · Rich Felker
userspace emulation done in libc implementations. No change is made to the underlying
chmod_common(), so it's still possible to attempt changes via procfs, if desired.And that is the goddamn problem. We need to fix that _first_.
Can you clarify exactly what that is? Do you mean fixing the underlying fs backends, or just ensuring that the chmod for symlinks doesn't reach them by putting the check in chmod_common? I'm ok with any of these.
Quoted 1 line
After that we can add sugarcoating using new syscalls if needed.
The new syscall is _not_ about this problem. It's about the missing flags argument and inability to implement fchmodat() without access to procfs. The above problem is just something you encounter and have to make a decision about in order to fix the missing flags problem and make a working AT_SYMLINK_NOFOLLOW.
Rich
Quoted 6 lines · Rich Felker
Quoted 5 lines · Christoph Hellwig
Quoted 3 lines · Rich Felker
userspace emulation done in libc implementations. No change is made to the underlying
chmod_common(), so it's still possible to attempt changes via procfs, if desired.
Quoted 6 lines
Quoted 1 line
And that is the goddamn problem. We need to fix that _first_.
Can you clarify exactly what that is? Do you mean fixing the underlying fs backends, or just ensuring that the chmod for symlinks doesn't reach them by putting the check in
chmod_common? I'm ok with any of these.
Either - we need to make sure the user can't change the permission bits.
Quoted 7 lines
Quoted 1 line
After that we can add sugarcoating using new syscalls if needed.
The new syscall is _not_ about this problem. It's about the missing flags argument and inability to implement
fchmodat()without access to procfs. The above problem is just something you encounter and have to make a decision about in order to fix the missing flags problem and make a workingAT_SYMLINK_NOFOLLOW.
And I'm generally supportive of that. But we need to fix the damn bug first an then do nice to haves.
Rich Felker
maintainerQuoted 19 lines · Christoph Hellwig
Quoted 6 lines · Rich Felker
Quoted 5 lines · Christoph Hellwig
Quoted 3 lines · Rich Felker
userspace emulation done in libc implementations. No change is made to the underlying
chmod_common(), so it's still possible to attempt changes via procfs, if desired.Quoted 6 lines
Quoted 1 line
And that is the goddamn problem. We need to fix that _first_.
Can you clarify exactly what that is? Do you mean fixing the underlying fs backends, or just ensuring that the chmod for symlinks doesn't reach them by putting the check in
chmod_common? I'm ok with any of these.Either - we need to make sure the user can't change the permission bits.
Quoted 2 lines
Quoted 1 line
After that we can add sugarcoating using new syscalls if needed.
Quoted 8 lines
Quoted 5 lines
The new syscall is _not_ about this problem. It's about the missing flags argument and inability to implement
fchmodat()without access to procfs. The above problem is just something you encounter and have to make a decision about in order to fix the missing flags problem and make a workingAT_SYMLINK_NOFOLLOW.And I'm generally supportive of that. But we need to fix the damn bug first an then do nice to haves.
Would you be happy with a pair of patches where the first blocks chmod of symlinks in chmod_common and the second adds the syscall with flags? I think this is a clearly understandable fix, but it does eliminate the ability to *fix* link access modes that have been set to ridiculous values (note: I don't think it really matters since the modes don't do anything anyway) in the past.
That's why I preferred to *start* with the forced-EOPNOTSUPP just in the new interface, so that links won't inadvertently get bogus modes set on them when libc starts using it. As long as some filesystems are representing access modes in links (and returning them via stat), it seems like there should be a way to "fix" any that were set in the past. The patch as I've submitted it now is the least invasive change in this sense; it does not take away any capability that already existed.
Rich
Quoted 6 lines · Rich Felker
Would you be happy with a pair of patches where the first blocks chmod of symlinks in
chmod_commonand the second adds the syscall with flags? I think this is a clearly understandable fix, but it does eliminate the ability to *fix* link access modes that have been set to ridiculous values (note: I don't think it really matters since the modes don't do anything anyway) in the past.
I'd be much happier with that, yes.
Quoted 13 lines · Rich Felker
POSIX defines fchmodat as having a 4th argument, flags, that can be
AT_SYMLINK_NOFOLLOW. Support for changing the access mode of symbolic links is optional (EOPNOTSUPP allowed if not supported), but this flag is important even on systems where symlinks do not have access modes, since it's the only way to safely change the mode of a file which might be asynchronously replaced with a symbolic link, without a race condition whereby the link target is changed.It's possible to emulate
AT_SYMLINK_NOFOLLOWin userspace, and both musl libc and glibc do this, by opening anO_PATHfile descriptor and performing chmod on the corresponding magic symlink in/proc/self/fd. However, this requires procfs to be mounted and accessible.
Quoted 11 lines
It was determined (see glibc issue #14578 and commit
a492b1e5ef) that, on some filesystems, performing chmod on the link itself produces a change in the inode's access mode, but returns an EOPNOTSUPP error. This is non-conforming and wrong. Rather than try to fix all the broken filesystem backends, block attempts to change the symlink access mode via fchmodat2 at the frontend layer. This matches the userspace emulation done in libc implementations. No change is made to the underlyingchmod_common(), so it's still possible to attempt changes via procfs, if desired. If at some point all filesystems have been fixed, this could be relaxed to allow filesystems to make their own decision whether changing access mode of links is supported.
A new syscall just because we have broken filesystems seems really odd, why not just fix the filesystems instead?
thanks,
greg k-h
Rich Felker
maintainerQuoted 26 lines · Greg KH
Quoted 13 lines · Rich Felker
POSIX defines fchmodat as having a 4th argument, flags, that can be
AT_SYMLINK_NOFOLLOW. Support for changing the access mode of symbolic links is optional (EOPNOTSUPP allowed if not supported), but this flag is important even on systems where symlinks do not have access modes, since it's the only way to safely change the mode of a file which might be asynchronously replaced with a symbolic link, without a race condition whereby the link target is changed.It's possible to emulate
AT_SYMLINK_NOFOLLOWin userspace, and both musl libc and glibc do this, by opening anO_PATHfile descriptor and performing chmod on the corresponding magic symlink in/proc/self/fd. However, this requires procfs to be mounted and accessible.Quoted 11 lines
It was determined (see glibc issue #14578 and commit
a492b1e5ef) that, on some filesystems, performing chmod on the link itself produces a change in the inode's access mode, but returns an EOPNOTSUPP error. This is non-conforming and wrong. Rather than try to fix all the broken filesystem backends, block attempts to change the symlink access mode via fchmodat2 at the frontend layer. This matches the userspace emulation done in libc implementations. No change is made to the underlyingchmod_common(), so it's still possible to attempt changes via procfs, if desired. If at some point all filesystems have been fixed, this could be relaxed to allow filesystems to make their own decision whether changing access mode of links is supported.
Quoted 2 lines
A new syscall just because we have broken filesystems seems really odd, why not just fix the filesystems instead?
The part about broken filesystems is just the justification for doing the EOPNOTSUPP check at this layer rather than relying on the filesystem to do it, not the purposse for the syscall.
The purpose of the syscall is fixing the deficiency in the original one, which lacked the flags argument, making it so you have to do a complicated emulation dance involving O_PATH and procfs magic symlinks to implement the standard functionality.
Rich
From: Rich Felker
Quoted 1 line
Sent: 10 September 2020 15:24
...
Quoted 8 lines
index
9af548fb841b..570a21f4d81e100644fs/open.c +0
-1@@ -610,15 +610,30 @@ SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)return err; } -static int do_fchmodat (int dfd,const char __user *filename,umode_t mode)
fs/open.c SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode) · line 622
Quoted 10 lines
fs/open.c +6
-0+static int do_fchmodat (int dfd,const char __user *filename,umode_t mode,int flags) {struct path path;int error;unsigned int lookup_flags =LOOKUP_FOLLOW ; + +if (flags &AT_SYMLINK_NOFOLLOW ) + lookup_flags &= ~LOOKUP_FOLLOW ; +if (flags & ~AT_SYMLINK_NOFOLLOW ) +return -EINVAL ;
I think I'd swap over those two tests.
So unsupported flags are clearly errored.
fs/open.c SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode) · line 634
Quoted 13 lines
fs/open.c +9
-1retry: error =user_path_at (dfd, filename, lookup_flags, &path);if (!error) { - error =chmod_common (&path, mode); +/* Block chmod from getting to fs layer. Ideally the +* fs would either allow it or fail with EOPNOTSUPP, +* but some are buggy and return an error but change +* the mode, which is non-conforming and wrong. +* Userspace emulation of AT_SYMLINK_NOFOLLOW in +* glibc and musl blocked it too, for same reason. */ +if (S_ISLNK (path.dentry->d_inode->i_mode) + && (flags &AT_SYMLINK_NOFOLLOW )) + error = -EOPNOTSUPP ;
Again swap the order of the tests. I think it reads better as:
if ((flags & AT_SYMLINK_NOFOLLOW) && S_ISLNK(path.dentry->d_inode->i_mode)) error = -EOPNOTSUPP;
As well as saving a few clock cycles.
fs/open.c SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode) · line 639
Quoted 5 lines
fs/open.c +2
-0+else + error =chmod_common (&path, mode);path_put (&path);if (retry_estale (error, lookup_flags)) { lookup_flags |=LOOKUP_REVAL ;
...
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)