[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <87lefmbppo.fsf@oldenburg.str.redhat.com>
Date: Tue, 11 Jul 2023 14:24:51 +0200
From: Florian Weimer <fweimer@...hat.com>
To: Alexey Gladkov <legion@...nel.org>
Cc: LKML <linux-kernel@...r.kernel.org>, Arnd Bergmann <arnd@...db.de>,
linux-api@...r.kernel.org, linux-fsdevel@...r.kernel.org,
viro@...iv.linux.org.uk, James.Bottomley@...senPartnership.com,
acme@...nel.org, alexander.shishkin@...ux.intel.com,
axboe@...nel.dk, benh@...nel.crashing.org, borntraeger@...ibm.com,
bp@...en8.de, catalin.marinas@....com, christian@...uner.io,
dalias@...c.org, davem@...emloft.net, deepa.kernel@...il.com,
deller@....de, dhowells@...hat.com, fenghua.yu@...el.com,
geert@...ux-m68k.org, glebfm@...linux.org, gor@...ux.ibm.com,
hare@...e.com, hpa@...or.com, ink@...assic.park.msu.ru,
jhogan@...nel.org, kim.phillips@....com, ldv@...linux.org,
linux-alpha@...r.kernel.org, linux-arch@...r.kernel.org,
linux-arm-kernel@...ts.infradead.org, linux-ia64@...r.kernel.org,
linux-m68k@...ts.linux-m68k.org, linux-mips@...r.kernel.org,
linux-parisc@...r.kernel.org, linux-s390@...r.kernel.org,
linux-sh@...r.kernel.org, linux@...linux.org.uk,
linuxppc-dev@...ts.ozlabs.org, luto@...nel.org, mattst88@...il.com,
mingo@...hat.com, monstr@...str.eu, mpe@...erman.id.au,
namhyung@...nel.org, paul.burton@...s.com, paulus@...ba.org,
peterz@...radead.org, ralf@...ux-mips.org, rth@...ddle.net,
sparclinux@...r.kernel.org, stefan@...er.ch, tglx@...utronix.de,
tony.luck@...el.com, tycho@...ho.ws, will@...nel.org,
x86@...nel.org, ysato@...rs.sourceforge.jp
Subject: Re: [PATCH v3 0/5] Add a new fchmodat4() syscall
* Alexey Gladkov:
> This patch set adds fchmodat4(), a new syscall. The actual
> implementation is super simple: essentially it's just the same as
> fchmodat(), but LOOKUP_FOLLOW is conditionally set based on the flags.
> I've attempted to make this match "man 2 fchmodat" as closely as
> possible, which says EINVAL is returned for invalid flags (as opposed to
> ENOTSUPP, which is currently returned by glibc for AT_SYMLINK_NOFOLLOW).
> I have a sketch of a glibc patch that I haven't even compiled yet, but
> seems fairly straight-forward:
>
> diff --git a/sysdeps/unix/sysv/linux/fchmodat.c b/sysdeps/unix/sysv/linux/fchmodat.c
> index 6d9cbc1ce9e0..b1beab76d56c 100644
> --- a/sysdeps/unix/sysv/linux/fchmodat.c
> +++ b/sysdeps/unix/sysv/linux/fchmodat.c
> @@ -29,12 +29,36 @@
> int
> fchmodat (int fd, const char *file, mode_t mode, int flag)
> {
> - if (flag & ~AT_SYMLINK_NOFOLLOW)
> - return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
> -#ifndef __NR_lchmod /* Linux so far has no lchmod syscall. */
> + /* There are four paths through this code:
> + - The flags are zero. In this case it's fine to call fchmodat.
> + - The flags are non-zero and glibc doesn't have access to
> + __NR_fchmodat4. In this case all we can do is emulate the error codes
> + defined by the glibc interface from userspace.
> + - The flags are non-zero, glibc has __NR_fchmodat4, and the kernel has
> + fchmodat4. This is the simplest case, as the fchmodat4 syscall exactly
> + matches glibc's library interface so it can be called directly.
> + - The flags are non-zero, glibc has __NR_fchmodat4, but the kernel does
If you define __NR_fchmodat4 on all architectures, we can use these
constants directly in glibc. We no longer depend on the UAPI
definitions of those constants, to cut down the number of code variants,
and to make glibc's system call profile independent of the kernel header
version at build time.
Your version is based on 2.31, more recent versions have some reasonable
emulation for fchmodat based on /proc/self/fd. I even wrote a comment
describing the same buggy behavior that you witnessed:
+ /* Some Linux versions with some file systems can actually
+ change symbolic link permissions via /proc, but this is not
+ intentional, and it gives inconsistent results (e.g., error
+ return despite mode change). The expected behavior is that
+ symbolic link modes cannot be changed at all, and this check
+ enforces that. */
+ if (S_ISLNK (st.st_mode))
+ {
+ __close_nocancel (pathfd);
+ __set_errno (EOPNOTSUPP);
+ return -1;
+ }
I think there was some kernel discussion about that behavior before, but
apparently, it hasn't led to fixes.
I wonder if it makes sense to add a similar error return to the system
call implementation?
> + not. In this case we must respect the error codes defined by the glibc
> + interface instead of returning ENOSYS.
> + The intent here is to ensure that the kernel is called at most once per
> + library call, and that the error types defined by glibc are always
> + respected. */
> +
> +#ifdef __NR_fchmodat4
> + long result;
> +#endif
> +
> + if (flag == 0)
> + return INLINE_SYSCALL (fchmodat, 3, fd, file, mode);
> +
> +#ifdef __NR_fchmodat4
> + result = INLINE_SYSCALL (fchmodat4, 4, fd, file, mode, flag);
> + if (result == 0 || errno != ENOSYS)
> + return result;
> +#endif
The last if condition is the recommended approach, but in the past, it
broke container host compatibility pretty badly due to seccomp filters
that return EPERM instead of ENOSYS. I guess we'll learn soon enough if
that's been fixed by now. 8-P
Thanks,
Florian
Powered by blists - more mailing lists