Skip to content

Commit 09da082

Browse files
legionusbrauner
authored andcommitted
fs: Add fchmodat2()
On the userspace side fchmodat(3) is implemented as a wrapper function which implements the POSIX-specified interface. This interface differs from the underlying kernel system call, which does not have a flags argument. Most implementations require procfs [1][2]. There doesn't appear to be a good userspace workaround for this issue but the implementation in the kernel is pretty straight-forward. The new fchmodat2() syscall allows to pass the AT_SYMLINK_NOFOLLOW flag, unlike existing fchmodat. [1] https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/fchmodat.c;h=17eca54051ee28ba1ec3f9aed170a62630959143;hb=a492b1e5ef7ab50c6fdd4e4e9879ea5569ab0a6c#l35 [2] https://git.musl-libc.org/cgit/musl/tree/src/stat/fchmodat.c?id=718f363bc2067b6487900eddc9180c84e7739f80#n28 Co-developed-by: Palmer Dabbelt <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]> Signed-off-by: Alexey Gladkov <[email protected]> Acked-by: Arnd Bergmann <[email protected]> Message-Id: <f2a846ef495943c5d101011eebcf01179d0c7b61.1689092120.git.legion@kernel.org> [brauner: pre reviews, do flag conversion in do_fchmodat() directly] Signed-off-by: Christian Brauner <[email protected]>
1 parent 06a0213 commit 09da082

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

fs/open.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,18 @@ SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
671671
return err;
672672
}
673673

674-
static int do_fchmodat(int dfd, const char __user *filename, umode_t mode)
674+
static int do_fchmodat(int dfd, const char __user *filename, umode_t mode,
675+
unsigned int flags)
675676
{
676677
struct path path;
677678
int error;
678-
unsigned int lookup_flags = LOOKUP_FOLLOW;
679+
unsigned int lookup_flags;
680+
681+
if (unlikely(flags & ~AT_SYMLINK_NOFOLLOW))
682+
return -EINVAL;
683+
684+
lookup_flags = (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
685+
679686
retry:
680687
error = user_path_at(dfd, filename, lookup_flags, &path);
681688
if (!error) {
@@ -689,15 +696,21 @@ static int do_fchmodat(int dfd, const char __user *filename, umode_t mode)
689696
return error;
690697
}
691698

699+
SYSCALL_DEFINE4(fchmodat2, int, dfd, const char __user *, filename,
700+
umode_t, mode, unsigned int, flags)
701+
{
702+
return do_fchmodat(dfd, filename, mode, flags);
703+
}
704+
692705
SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename,
693706
umode_t, mode)
694707
{
695-
return do_fchmodat(dfd, filename, mode);
708+
return do_fchmodat(dfd, filename, mode, 0);
696709
}
697710

698711
SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
699712
{
700-
return do_fchmodat(AT_FDCWD, filename, mode);
713+
return do_fchmodat(AT_FDCWD, filename, mode, 0);
701714
}
702715

703716
/*

include/linux/syscalls.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,8 @@ asmlinkage long sys_chroot(const char __user *filename);
440440
asmlinkage long sys_fchmod(unsigned int fd, umode_t mode);
441441
asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
442442
umode_t mode);
443+
asmlinkage long sys_fchmodat2(int dfd, const char __user *filename,
444+
umode_t mode, unsigned int flags);
443445
asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
444446
gid_t group, int flag);
445447
asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group);

0 commit comments

Comments
 (0)