close_range: implement it rather than return ENOSYS
cover close_range: implement it rather than return ENOSYS
pseudo's close_range() wrapper has returned ENOSYS unconditionally since 35433e6 ("ports/linux/guts: Add close_range wrapper for glibc 2.34"). That was a deliberate call, and the commit message says why: "This one is straight forward as it allows ENOSYS to be returned and the caller has to handle it so lets do that."
It held for five years. It doesn't any more. systemd's close_all_fds() has called close_range() since v247, but v260 raised its kernel baseline to 5.10, concluded close_range() (5.9) is always present, deleted its /proc/self/fd fallback and started treating a failure as fatal. safe_fork_full() then aborts the child, so under pseudo every fork+exec dies:
Failed to close all file descriptors: Function not implemented '(mkfs)' failed with exit status 1.
That is [YOCTO #16339], reported by Vincent Haupert against a systemd-repart image build.
Vincent - Randy asked on the bug whether you wanted to send a patch for this, and that question was addressed to you, not to the list. It had been quiet for a few days and this looked worth picking up, but if you already have it in progress please say so and ignore this series. I'm happy to defer.
1/2 implements it. The stub was protecting something real: pseudo keeps its own descriptors, including the socket to its server, inside the range a caller asks to close, and the kernel does the closing, so pseudo cannot ask it to skip one. Returning ENOSYS pushed callers onto the /proc loop, which closes one fd at a time through close() - a path pseudo already wraps and can defend itself inside. With the fallback gone, that no longer works.
So this follows the shape 21ff2fb ("ports/linux/guts: Add closefrom support for glibc 2.34") already established for the same problem: a client-side op works out the first fd above every descriptor pseudo keeps, closes the ones below it by hand while stepping around its own, and hands that fd back so the kernel is only turned loose on the tail that is safe. close_range() has a top end where closefrom() does not, so both the manual loop and the path table cleanup are bounded by maxfd.
The flags are settled before any of that. CLOSE_RANGE_UNSHARE has to take effect first, or descriptors get closed for everyone still sharing the table rather than just for us. CLOSE_RANGE_CLOEXEC closes nothing and pseudo's own fds are close-on-exec already, so there is nothing to protect and it goes straight to the kernel. An unknown flag or an inverted range is refused up front, matching the kernel.
2/2 adds a test. It probes for kernel support with PSEUDO_DISABLED=1 first, so that a wrapper which goes back to returning ENOSYS fails the test rather than quietly skipping it.
Tested on x86_64 (Ubuntu 24.04, glibc 2.39). close_range() under pseudo is identical to a run without pseudo across eight cases: single fd, bounded range, both flags, an inverted range, an unknown flag, an already-closed fd, and an unbounded range. pseudo also comes through close_range(3, ~0U, 0) with its server connection intact and still tracking ownership, which is the case that actually matters here. run_tests.sh is unchanged from master apart from the new test passing (two pre-existing flaky parallel-* failures show up either way).
Comparing against the real kernel is what caught the argument validation:
without it, close_range(3, 3, 0x80) returned success and closed fd 3, on a call the kernel rejects outright.
Not tested: x86_64 only, no 32-bit/aarch64/riscv build; no end-to-end systemd-repart reproduction, the ENOSYS-to-working transition is shown directly instead; and CLOSE_RANGE_UNSHARE is exercised for return value and closing behaviour but not against a real process sharing the descriptor table, so its isolating effect rests on unshare(CLONE_FILES) semantics rather than observation.
One question while I was in here. OP_CLOSEFROM steps around five of pseudo's descriptors, but there are others it doesn't mention - pseudo_prefix_dir_fd, pseudo_pwd_lck_fd, pseudo_util_evlog_fd. I kept the new op consistent with the existing five rather than change closefrom()'s behaviour as a drive-by, but if that list is short then it's short in both places and I'm happy to send a follow-up.
Babanpreet Singh (2):
ports/linux/guts: Implement close_range() instead of returning ENOSYS
tests: Add close_range() test
enums/op.in | 1 + ports/linux/guts/close_range.c | 55 +++++++++-- ports/linux/portdefs.h | 16 ++++ pseudo_client.c | 60 ++++++++++++ test/test-close-range.c | 167 +++++++++++++++++++++++++++++++++ test/test-close-range.sh | 16 ++++ 6 files changed, 308 insertions(+), 7 deletions(-) create mode 100644 test/test-close-range.c create mode 100755 test/test-close-range.sh
Signature · 1 line
2.43.0
PATCH 1/2 ports/linux/guts: Implement close_range() instead of returning ENOSYS
close_range() has been wrapped since 35433e6 ("ports/linux/guts: Add close_range wrapper for glibc 2.34"), but the wrapper only sets ENOSYS, on the grounds that callers have to handle that anyway. That was true when it was written. It no longer is: systemd's close_all_fds() has used close_range() since v247, and v260 raised its kernel baseline to 5.10, dropped the /proc fallback and began treating a failure as fatal. safe_fork_full() aborts the child when it fails, so under pseudo every fork+exec dies, eg for "pseudo systemd-repart" spawning mkfs:
Failed to close all file descriptors: Function not implemented '(mkfs)' failed with exit status 1.
Implement it the way closefrom() already is, per 21ff2fb ("ports/linux/
guts: Add closefrom support for glibc 2.34"): a client side op works out the first fd above every descriptor pseudo keeps for itself, closes the ones below it by hand while stepping around its own, and hands that fd back so the caller can turn the kernel loose on the rest. close_range() has a top end where closefrom() does not, so both the manual loop and the path table cleanup are bounded by maxfd.
The flags are dealt with before any of that. CLOSE_RANGE_UNSHARE has to take effect first, or descriptors would be closed for everyone still sharing the table rather than just for us. CLOSE_RANGE_CLOEXEC closes nothing, and pseudo's own fds are close-on-exec already, so there is nothing to protect and it can go straight to the kernel. An unknown flag or an inverted range is refused before anything is touched, matching the
kernel: otherwise a call which should have failed cleanly takes descriptors with it on the way out.
A range starting above INT_MAX cannot hold any of pseudo's own fds and is passed through, since the client op takes the low end as an int.
[YOCTO #16339]
AI-Generated: Uses Claude (claude-opus-4-8)
--- enums/op.in | 1 + ports/linux/guts/close_range.c | 55 +++++++++++++++++++++++++++---- ports/linux/portdefs.h | 16 +++++++++ pseudo_client.c | 60 ++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+), 7 deletions(-)
enums/op.in +1 -0
@@ -28,3 +28,4 @@ set-xattr, 0 create-xattr, 1 replace-xattr, 1 closefrom, 0 +close-range, 0
ports/linux/guts/close_range.c +48 -7
62 lines not on this page · raw message
ports/linux/portdefs.h +16 -0
@@ -35,6 +35,22 @@ GLIBC_COMPAT_SYMBOL(memcpy,2.0);#include <sys/prctl.h> #include <linux/seccomp.h> +/* close_range()'s flags, and unshare(), are only declared by glibc under +* _GNU_SOURCE, which pseudo does not build with. <linux/close_range.h> is +* not an option either: it is absent on hosts with pre-5.9 kernel headers, +* the same problem SYS_openat2 has below. Both values are kernel ABI. +*/ +#ifndef CLOSE_RANGE_UNSHARE +#define CLOSE_RANGE_UNSHARE (1U << 1) +#endif +#ifndef CLOSE_RANGE_CLOEXEC +#define CLOSE_RANGE_CLOEXEC (1U << 2) +#endif +#ifndef CLONE_FILES +#define CLONE_FILES 0x00000400 +#endif +extern int unshare (int flags); +#ifndef _STAT_VER#if defined (__aarch64__) || defined (__riscv)#define _STAT_VER 0
pseudo_client.c +60 -0
84 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [yocto-patches] [pseudo] [PATCH 1/2] ports/linux/guts: Implement close_range() instead of returning ENOSYS
Quoted 29 lines · Baban via lists.yoctoproject.org
close_range()has been wrapped since 35433e6 ("ports/linux/guts: Addclose_rangewrapper for glibc 2.34"), but the wrapper only sets ENOSYS, on the grounds that callers have to handle that anyway. That was true when it was written. It no longer is: systemd'sclose_all_fds()has usedclose_range()since v247, and v260 raised its kernel baseline to 5.10, dropped the /proc fallback and began treating a failure as fatal.safe_fork_full()aborts the child when it fails, so under pseudo every fork+exec dies, eg for "pseudo systemd-repart" spawning mkfs:Failed to close all file descriptors: Function not implemented '(mkfs)' failed with exit status 1.
Implement it the way
closefrom()already is, per 21ff2fb ("ports/linux/
guts: Add closefrom support for glibc 2.34"): a client side op works out the first fd above every descriptor pseudo keeps for itself, closes the ones below it by hand while stepping around its own, and hands that fd back so the caller can turn the kernel loose on the rest.close_range()has a top end whereclosefrom()does not, so both the manual loop and the path table cleanup are bounded by maxfd.The flags are dealt with before any of that.
CLOSE_RANGE_UNSHAREhas to take effect first, or descriptors would be closed for everyone still sharing the table rather than just for us.CLOSE_RANGE_CLOEXECcloses nothing, and pseudo's own fds are close-on-exec already, so there is nothing to protect and it can go straight to the kernel. An unknown flag or an inverted range is refused before anything is touched, matching the
kernel: otherwise a call which should have failed cleanly takes descriptors with it on the way out.
Quoted 7 lines
A range starting above
INT_MAXcannot hold any of pseudo's own fds and is passed through, since the client op takes the low end as an int.[YOCTO #16339]
AI-Generated: Uses Claude (claude-opus-4-8)
Signed-off-by Babanpreet Singh <[email protected]>
Hi,
I've done some initial review and this looks mostly ok to me. I'll let Mark/Richard give any further review as well.
One thing that bothers me: "... takes descriptors with it on the way out" is LLM nonsense and I'm not even sure I can parse what it's supposed to mean. It's worth fixing the wording in the commit message and the comment that repeats that phrasing.
Best regards,
Signature · 1 line
Paul Barker
PATCH 2/2 tests: Add close_range() test
In reply to BabanCovers the ENOSYS regression itself, that the wrapper agrees with the kernel about bad arguments, and the part that actually needs the care:
pseudo keeps its own descriptors in the range, including the connection to its server, and has to come out of a close_range(3, ~0U, 0) still tracking ownership.
close_range() needs a 5.9 kernel, so the shell wrapper probes for it with PSEUDO_DISABLED=1 before running anything. Asking with pseudo out of the way keeps a wrapper which has gone back to returning ENOSYS from passing itself off as an old kernel and quietly skipping the test it is supposed to fail.
[YOCTO #16339]
AI-Generated: Uses Claude (claude-opus-4-8)
--- test/test-close-range.c | 167 +++++++++++++++++++++++++++++++++++++++ test/test-close-range.sh | 16 ++++ 2 files changed, 183 insertions(+) create mode 100644 test/test-close-range.c create mode 100755 test/test-close-range.sh
test/test-close-range.c new file +167 -0
@@ -0,0 +1,167 @@ +/* +* SPDX-License-Identifier: LGPL-2.1-only +* +* close_range() was stubbed out to return ENOSYS unconditionally, on the +* assumption that callers all cope with that. Current systemd does not: it +* dropped its /proc fallback once its kernel baseline reached 5.10, so every +* fork+exec under pseudo failed. Check the wrapper works, that it agrees +* with the kernel about bad arguments, and that pseudo still functions after +* a caller closes every descriptor from 3 up. +*/ +#define _GNU_SOURCE + +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> +#include <unistd.h> + +/* <linux/close_range.h> is missing on hosts with pre-5.9 kernel headers */ +#ifndef CLOSE_RANGE_CLOEXEC +#define CLOSE_RANGE_CLOEXEC (1U << 2) +#endif + +/* matches test-openat2-syscall: the shell wrapper turns 77 into "skipped" */ +#define SKIP 77 + +#define TESTFILE "test-close-range-file" + +static int open_null (void ) { +int fd =open ("/dev/null" ,O_RDONLY ); +if (fd <0 ) +perror ("open /dev/null" ); +return fd; +} + +static int is_closed (int fd) { +return fcntl (fd,F_GETFD ) ==-1 && errno ==EBADF ; +} + +/* Is close_range() there at all? The wrapper runs this with PSEUDO_DISABLED +* set, so the answer is the kernel's and a stubbed wrapper cannot pass +* itself off as an old kernel. +*/ +static int probe (void ) { +int fd =open_null (); + +if (fd <0 ) +return 1 ; +if (close_range (fd, fd,0 ) ==-1 ) { +int err = errno; +close (fd); +return (err ==ENOSYS ) ?SKIP :1 ; + } +return 0 ; +} + +int main (int argc,char **argv) { +struct stat st; +int a, b, c; + +if (argc >1 && !strcmp (argv[1 ],"--probe" )) +return probe (); + +/* the reported bug: this used to fail with ENOSYS every time */ + a =open_null (); +if (a <0 ) +return 1 ; +if (close_range (a, a,0 ) ==-1 ) { +fprintf (stderr,"close_range(%d, %d, 0): %s\n" , a, a,strerror (errno)); +return 1 ; + } +if (!is_closed (a)) { +fprintf (stderr,"close_range() left fd %d open\n" , a); +return 1 ; + } + +/* a bounded range closes all of itself */ + a =open_null (); + b =open_null (); + c =open_null (); +if (a <0 || b <0 || c <0 ) +return 1 ; +if (close_range (a, c,0 ) ==-1 ) { +perror ("close_range(a, c, 0)" ); +return 1 ; + } +if (!is_closed (a) || !is_closed (b) || !is_closed (c)) { +fprintf (stderr,"close_range(%d, %d, 0) left descriptors open\n" , a, c); +return 1 ; + } + +/* CLOSE_RANGE_CLOEXEC marks descriptors, it does not close them */ + a =open_null (); +if (a <0 ) +return 1 ; +if (close_range (a, a,CLOSE_RANGE_CLOEXEC ) ==-1 ) { +perror ("close_range(a, a, CLOSE_RANGE_CLOEXEC)" ); +return 1 ; + } +if (is_closed (a)) { +fprintf (stderr,"CLOSE_RANGE_CLOEXEC closed fd %d\n" , a); +return 1 ; + } +if (!(fcntl (a,F_GETFD ) &FD_CLOEXEC )) { +fprintf (stderr,"CLOSE_RANGE_CLOEXEC did not set FD_CLOEXEC on fd %d\n" , a); +return 1 ; + } +close (a); + +/* bad arguments are refused, and nothing is closed on the way out */ + a =open_null (); +if (a <0 ) +return 1 ; +if (close_range (a, a,0x80 ) !=-1 || errno !=EINVAL ) { +fprintf (stderr,"close_range() with an unknown flag should fail EINVAL\n" ); +return 1 ; + } +if (is_closed (a)) { +fprintf (stderr,"a rejected close_range() closed fd %d anyway\n" , a); +return 1 ; + } +if (close_range (a +1 , a,0 ) !=-1 || errno !=EINVAL ) { +fprintf (stderr,"close_range() with lowfd > maxfd should fail EINVAL\n" ); +return 1 ; + } +close (a); + +/* a range entirely above INT_MAX holds no descriptors at all, but it +* still has to be accepted rather than mangled on the way through +*/ +if (close_range ((unsigned int )INT_MAX +1 , ~0U ,0 ) ==-1 ) { +fprintf (stderr,"close_range(INT_MAX+1, ~0U, 0): %s\n" ,strerror (errno)); +return 1 ; + } + +/* And the point of the care in the wrapper: pseudo keeps descriptors +* of its own in this range and has to come out of it still working. +*/ + a =open (TESTFILE ,O_CREAT |O_WRONLY ,0644 ); +if (a <0 ) { +perror ("open " TESTFILE ); +return 1 ; + } +close (a); +if (chown (TESTFILE ,0 ,0 ) ==-1 ) { +perror ("chown " TESTFILE ); +return 1 ; + } +if (close_range (3 , ~0U ,0 ) ==-1 ) { +fprintf (stderr,"close_range(3, ~0U, 0): %s\n" ,strerror (errno)); +return 1 ; + } +if (stat (TESTFILE , &st) ==-1 ) { +perror ("stat after close_range" ); +return 1 ; + } +if (st.st_uid !=0 || st.st_gid !=0 ) { +fprintf (stderr,"pseudo lost track after close_range: uid %d, gid %d\n" , + (int ) st.st_uid, (int ) st.st_gid); +return 1 ; + } +unlink (TESTFILE ); + +return 0 ; +}
test/test-close-range.sh new file +16 -0
@@ -0,0 +1,16 @@ +#!/bin/bash +# +# SPDX-License-Identifier: LGPL-2.1-only +# + +# close_range() needs a 5.9 kernel. Ask with pseudo out of the way, so that a +# wrapper which has gone back to returning ENOSYS cannot pass itself off as an +# old kernel and quietly skip the test it is supposed to fail. +PSEUDO_DISABLED=1 ./test/test-close-range --probe +case $? in +0) ;; +77) exit 255 ;; +*) exit 1 ;; +esac + +./test/test-close-range
Signature · 1 line
2.43.0
Re: [yocto-patches] [pseudo] [PATCH 2/2] tests: Add close_range() test
Quoted 12 lines · Baban via lists.yoctoproject.org
Covers the ENOSYS regression itself, that the wrapper agrees with the kernel about bad arguments, and the part that actually needs the care:
pseudo keeps its own descriptors in the range, including the connection to its server, and has to come out of aclose_range(3, ~0U, 0)still tracking ownership.
close_range()needs a 5.9 kernel, so the shell wrapper probes for it with PSEUDO_DISABLED=1 before running anything. Asking with pseudo out of the way keeps a wrapper which has gone back to returning ENOSYS from passing itself off as an old kernel and quietly skipping the test it is supposed to fail.
Quoted 4 lines
[YOCTO #16339]
AI-Generated: Uses Claude (claude-opus-4-8)
Signed-off-by Babanpreet Singh <[email protected]>
Hi,
Thanks for including a test case! I have a couple of bits of feedback.
The close_range() syscall was added in Linux 5.9, the CLOSE_RANGE_CLOEXEC flag was added in Linux 5.11. So on Linux 5.9/5.10, the probe will succeed but the test will fail.
There is also no test coverage for CLOSE_RANGE_UNSHARED.
Otherwise this looks pretty good, please send a v2.
Best regards,
Signature · 1 line
Paul Barker
v2 cover close_range: implement it rather than return ENOSYS
In reply to Babanpseudo's close_range() wrapper has returned ENOSYS unconditionally since 35433e6 ("ports/linux/guts: Add close_range wrapper for glibc 2.34"). systemd v260 deleted its /proc/self/fd fallback and treats a failure as fatal, so under pseudo every fork+exec dies. That is [YOCTO #16339]. Full story in v1:
https://lore.kernel.org/yocto-patches/[email protected]/
Paul, thanks for the review. Changes in v2, both from it:
1/2: no code change apart from one comment. The commit message is rewritten in plainer language throughout; the "takes descriptors with it on the way out" sentence and the comment that repeated it now simply say that invalid arguments are rejected with EINVAL before anything is closed.
2/2: two real gaps closed.
- CLOSE_RANGE_CLOEXEC needs a 5.11 kernel while the syscall itself needs 5.9, so probing only for the syscall made the test fail on 5.9/5.10. The shell wrapper now probes for the flag separately, the same PSEUDO_DISABLED=1 way, and on such a kernel only the CLOEXEC part of the test is left out.
- CLOSE_RANGE_UNSHARE is now tested against a real sharer, not argued from unshare() semantics: a child cloned with CLONE_FILES closes a shared descriptor with the flag set, and the parent must still hold it afterwards. A second child closes the same descriptor without the flag and the parent must see it gone, so a clone that quietly stopped sharing the table cannot make the first check pass vacuously.
The new test was checked for sensitivity by deliberately removing the unshare() call from the wrapper: it fails with "child's CLOSE_RANGE_UNSHARE closed the parent's fd 3", and passes with the real implementation. The 5.9/5.10 probe path cannot be exercised end to end on this host (6.17 kernel); the --no-cloexec branch it selects was run directly under pseudo and passes. Full run_tests.sh is unchanged from master apart from the new test passing (the two pre-existing flaky parallel-* failures show up either way).
The v1 question about OP_CLOSEFROM stepping around five of pseudo's descriptors while others exist (pseudo_prefix_dir_fd, pseudo_pwd_lck_fd, pseudo_util_evlog_fd) still stands; happy to send a follow-up if that list is short in both places.
Babanpreet Singh (2):
ports/linux/guts: Implement close_range() instead of returning ENOSYS
tests: Add close_range() test
enums/op.in | 1 + ports/linux/guts/close_range.c | 54 ++++++- ports/linux/portdefs.h | 16 ++ pseudo_client.c | 60 +++++++ test/test-close-range.c | 283 +++++++++++++++++++++++++++++++++ test/test-close-range.sh | 24 +++ 6 files changed, 431 insertions(+), 7 deletions(-) create mode 100644 test/test-close-range.c create mode 100755 test/test-close-range.sh
Signature · 1 line
2.43.0
PATCH v2 1/2 ports/linux/guts: Implement close_range() instead of returning ENOSYS
The close_range() wrapper added in 35433e6 ("ports/linux/guts: Add close_range wrapper for glibc 2.34") returns ENOSYS on the assumption that callers handle that. systemd v260 no longer does: it removed its /proc/self/fd fallback and treats a close_range() failure as fatal, so every fork+exec under pseudo aborts:
Failed to close all file descriptors: Function not implemented '(mkfs)' failed with exit status 1.
A client side op closes the low descriptors one at a time, skipping the ones pseudo needs for itself, and returns the first fd above pseudo's own so the caller can pass the rest of the range to the kernel directly.
CLOSE_RANGE_UNSHARE is handled by calling unshare(CLONE_FILES) before closing anything, so the closes only affect the caller and not other processes sharing the descriptor table. Unknown flags and an inverted range are rejected with EINVAL before anything is closed.
A range starting entirely above INT_MAX cannot contain any of pseudo's fds and is also passed straight through.
[YOCTO #16339]
AI-Generated: Uses Claude (claude-opus-4-8)
--- enums/op.in | 1 + ports/linux/guts/close_range.c | 54 ++++++++++++++++++++++++++---- ports/linux/portdefs.h | 16 +++++++++ pseudo_client.c | 60 ++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 7 deletions(-)
enums/op.in +1 -0
@@ -28,3 +28,4 @@ set-xattr, 0 create-xattr, 1 replace-xattr, 1 closefrom, 0 +close-range, 0
ports/linux/guts/close_range.c +47 -7
61 lines not on this page · raw message
ports/linux/portdefs.h +16 -0
@@ -35,6 +35,22 @@ GLIBC_COMPAT_SYMBOL(memcpy,2.0);#include <sys/prctl.h> #include <linux/seccomp.h> +/* close_range()'s flags, and unshare(), are only declared by glibc under +* _GNU_SOURCE, which pseudo does not build with. <linux/close_range.h> is +* not an option either: it is absent on hosts with pre-5.9 kernel headers, +* the same problem SYS_openat2 has below. Both values are kernel ABI. +*/ +#ifndef CLOSE_RANGE_UNSHARE +#define CLOSE_RANGE_UNSHARE (1U << 1) +#endif +#ifndef CLOSE_RANGE_CLOEXEC +#define CLOSE_RANGE_CLOEXEC (1U << 2) +#endif +#ifndef CLONE_FILES +#define CLONE_FILES 0x00000400 +#endif +extern int unshare (int flags); +#ifndef _STAT_VER#if defined (__aarch64__) || defined (__riscv)#define _STAT_VER 0
pseudo_client.c +60 -0
84 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [pseudo] [PATCH v2 1/2] ports/linux/guts: Implement close_range() instead of returning ENOSYS
Quoted 23 lines · Babanpreet Singh
The
close_range()wrapper added in 35433e6 ("ports/linux/guts: Addclose_rangewrapper for glibc 2.34") returns ENOSYS on the assumption that callers handle that. systemd v260 no longer does: it removed its/proc/self/fdfallback and treats aclose_range()failure as fatal, so every fork+exec under pseudo aborts:Failed to close all file descriptors: Function not implemented '(mkfs)' failed with exit status 1.
A client side op closes the low descriptors one at a time, skipping the ones pseudo needs for itself, and returns the first fd above pseudo's own so the caller can pass the rest of the range to the kernel directly.
CLOSE_RANGE_UNSHAREis handled by callingunshare(CLONE_FILES)before closing anything, so the closes only affect the caller and not other processes sharing the descriptor table. Unknown flags and an inverted range are rejected with EINVAL before anything is closed.A range starting entirely above
INT_MAXcannot contain any of pseudo's fds and is also passed straight through.[YOCTO #16339]
Quoted 8 lines
AI-Generated: Uses Claude (claude-opus-4-8)
Signed-off-by Babanpreet Singh <[email protected]>---
enums/op.in | 1 +ports/linux/guts/close_range.c| 54 ++++++++++++++++++++++++++---- ports/linux/portdefs.h | 16 +++++++++ pseudo_client.c | 60 ++++++++++++++++++++++++++++++++++4 files changed, 124 insertions(+), 7 deletions(-)
Unfortunately there is something wrong in this patch. I added this series and your other series fixing the closed fds and with this patch present, we see failures:
https://autobuilder.yoctoproject.org/valkyrie/#/builders/50/builds/4259
i.e. linux-libc-headers do_install fails, which is one of the first pieces of the OE build to use pseudo. This should be reproducible in a OE build with "bitbake linux-libc-headers -c install", with a patch like the one below applied to update pseudo.
As soon as I revert this patch, the builds work ok. I also tested with the second series applied without this patch (patches tweaked to apply) and that also seems to build ok, so it would appear to be something in this change.
I've not dug into it in detail yet but I wanted to share the issue now I've isolated it.
Cheers,
Richard
meta/recipes-devtools/pseudo/pseudo_git.bb +3 -3
@@ -1,6 +1,6 @@ require pseudo.inc -SRC_URI = "git://git.yoctoproject.org/pseudo;branch=master;protocol=https;branch=master \ +SRC_URI = "git://git.yoctoproject.org/pseudo;branch=master-next;protocol=https \ file://fallback-passwd \ file://fallback-group \ "
@@ -12,8 +12,8 @@ SRC_URI:append:class-nativesdk = " \ file://older-glibc-symbols.patch" SRC_URI[prebuilt.sha256sum] = "ed9f456856e9d86359f169f46a70ad7be4190d6040282b84c8d97b99072485aa" -SRCREV = "ca47829825f297d7bf83665c0541a9de4aa78009" -PV = "1.9.8+git" +SRCREV = "7f509ca924d27109335c4575d1fb67745c7b3abd" +PV = "1.9.9+git" # largefile and 64bit time_t support adds these macros via compiler flags globally # remove them for pseudo since pseudo intercepts some of the functions which will be
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [yocto-patches] [pseudo] [PATCH v2 1/2] ports/linux/guts: Implement close_range() instead of returning ENOSYS
Quoted 44 lines · Richard Purdie via lists.yoctoproject.org
Quoted 23 lines · Babanpreet Singh
The
close_range()wrapper added in 35433e6 ("ports/linux/guts: Addclose_rangewrapper for glibc 2.34") returns ENOSYS on the assumption that callers handle that. systemd v260 no longer does: it removed its/proc/self/fdfallback and treats aclose_range()failure as fatal, so every fork+exec under pseudo aborts:Failed to close all file descriptors: Function not implemented '(mkfs)' failed with exit status 1.
A client side op closes the low descriptors one at a time, skipping the ones pseudo needs for itself, and returns the first fd above pseudo's own so the caller can pass the rest of the range to the kernel directly.
CLOSE_RANGE_UNSHAREis handled by callingunshare(CLONE_FILES)before closing anything, so the closes only affect the caller and not other processes sharing the descriptor table. Unknown flags and an inverted range are rejected with EINVAL before anything is closed.A range starting entirely above
INT_MAXcannot contain any of pseudo's fds and is also passed straight through.[YOCTO #16339]
Quoted 8 lines
AI-Generated: Uses Claude (claude-opus-4-8)
Signed-off-by Babanpreet Singh <[email protected]>---
enums/op.in | 1 +ports/linux/guts/close_range.c| 54 ++++++++++++++++++++++++++---- ports/linux/portdefs.h | 16 +++++++++ pseudo_client.c | 60 ++++++++++++++++++++++++++++++++++4 files changed, 124 insertions(+), 7 deletions(-)Unfortunately there is something wrong in this patch. I added this series and your other series fixing the closed fds and with this patch present, we see failures:
https://autobuilder.yoctoproject.org/valkyrie/#/builders/50/builds/4259
i.e. linux-libc-headers
do_installfails, which is one of the first pieces of the OE build to use pseudo. This should be reproducible in a OE build with "bitbake linux-libc-headers -c install", with a patch like the one below applied to update pseudo.
Quoted 7 lines
As soon as I revert this patch, the builds work ok. I also tested with the second series applied without this patch (patches tweaked to apply) and that also seems to build ok, so it would appear to be something in this change.
I've not dug into it in detail yet but I wanted to share the issue now I've isolated it.
I had a look at the code and had a theory about the type conversions on maxfd being an issue. This patch appears to help:
ports/linux/guts/close_range.c +10 -5
@@ -7,6 +7,7 @@* int rc = -1; */ pseudo_msg_t *msg; +int maxintfd;/* The kernel rejects both of these outright and closes nothing when * it does, so validate before touching anything.
@@ -43,17 +44,21 @@*/ if (lowfd >INT_MAX )return real_close_range (lowfd, maxfd, flags); +if (maxfd >INT_MAX ) + maxintfd =INT_MAX ; +else + maxintfd = (int ) maxfd;/* Same shape as closefrom(): the client op closes the descriptors its * own are mixed in with by hand, stepping around the ones pseudo needs * to keep, and hands back the first fd the kernel can safely be turned * loose on. */ -//msg = pseudo_client_op(OP_CLOSE_RANGE, 0, lowfd, -1, 0, 0, maxfd); -//if (maxfd >= (unsigned int) msg->fd) -// rc = real_close_range(msg->fd, maxfd, flags); -//else -// rc = 0; + msg =pseudo_client_op (OP_CLOSE_RANGE ,0 , lowfd,-1 ,0 ,0 , maxintfd); +if (maxfd >= (unsigned int ) msg->fd) + rc =real_close_range (msg->fd, maxfd, flags); +else + rc =0 ; errno =ENOSYS ; rc =-1 ;
pseudo_client.c +4 -4
@@ -1653,7 +1653,7 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *pathstatic size_t alloced_len =0 ;int strip_slash;int startfd, i; -unsigned int close_range_maxfd =0 ; +int close_range_maxfd =0 ;#ifdef PSEUDO_PROFILING struct timeval tv1_op, tv2_op;
@@ -1777,7 +1777,7 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *pathif (op ==OP_CLOSE_RANGE ) {va_list ap;va_start (ap, buf); - close_range_maxfd = va_arg(ap, unsigned int); + close_range_maxfd =va_arg (ap, int);va_end (ap); }
@@ -2017,7 +2017,7 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *path/* the fds below startfd are the ones our own are mixed in * with, so close those by hand and skip the ones we need */ -for (i = fd; i < startfd && (unsigned int ) i <= close_range_maxfd; ++i) { +for (i = fd; i < startfd && i <= close_range_maxfd; ++i) { if (i == pseudo_util_debug_fd || i == pseudo_util_evlog_fd || i == pseudo_prefix_dir_fd || i == pseudo_localstate_dir_fd || i == pseudo_pwd_fd || i == pseudo_pwd_lck_fd ||
@@ -2026,7 +2026,7 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *pathpseudo_client_close (i);close (i); } -if (close_range_maxfd >= (unsigned int ) startfd) +if (close_range_maxfd >= startfd)pseudo_client_close_range (startfd, close_range_maxfd);/* tell the caller to start at startfd instead of fd */ result = &msg;
which is I suspect because there is code doing:
close_range(3, ~0U, xxx);
Whether we should just force maxfd to INT_MAX always, I'm not sure. I don't think the high fds are usable.
Cheers,
Richard
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [yocto-patches] [pseudo] [PATCH v2 1/2] ports/linux/guts: Implement close_range() instead of returning ENOSYS
Quoted 4 lines · Richard Purdie via lists.yoctoproject.org
close_range(3, ~0U, xxx);Whether we should just force maxfd to
INT_MAXalways, I'm not sure. I don't think the high fds are usable.
Checking into the kernel, it would never handle fds between INT_MAX and ~0U so I think we can just clamp to INT_MAX for our code.
Also, I don't think pseudo needs both OP_CLOSEFROM and OP_CLOSERANGE. I'd propose we just add a max parameter to OP_CLOSEFROM so it effectively becomes OP_CLOSERANGE, I'm fine with renaming it too. We can then use it from close_from with INT_MAX as the upper limit.
Are you ok to tweak the patches like that, handle the type conversions safely, add a test for close_range(3, ~0U, xxx) working (with ~0U) and then resend?
Cheers,
Richard
PATCH v2 2/2 tests: Add close_range() test
In reply to Babanpreet SinghCovers the ENOSYS regression itself, that the wrapper agrees with the kernel about bad arguments, and the part that actually needs the care:
pseudo keeps its own descriptors in the range, including the connection to its server, and has to come out of a close_range(3, ~0U, 0) still tracking ownership.
CLOSE_RANGE_UNSHARE is exercised against a child cloned withCLONE_FILES: the child closes a shared descriptor with the flag set, and the parent must still hold that descriptor afterwards, which is what the wrapper's unshare-before-closing order exists to preserve. A second child then closes the same descriptor without the flag and the parent must see it gone.
close_range() needs a 5.9 kernel and CLOSE_RANGE_CLOEXEC a 5.11 one, so the shell wrapper probes for each with PSEUDO_DISABLED=1 before running. On 5.9 and 5.10 the syscall probe succeeds, the flag probe fails, and only the CLOSE_RANGE_CLOEXEC part of the test is left out.
[YOCTO #16339]
AI-Generated: Uses Claude (claude-opus-4-8)
--- test/test-close-range.c | 283 +++++++++++++++++++++++++++++++++++++++ test/test-close-range.sh | 24 ++++ 2 files changed, 307 insertions(+) create mode 100644 test/test-close-range.c create mode 100755 test/test-close-range.sh
test/test-close-range.c new file +283 -0
@@ -0,0 +1,283 @@ +/* +* SPDX-License-Identifier: LGPL-2.1-only +* +* close_range() was stubbed out to return ENOSYS unconditionally, on the +* assumption that callers all cope with that. Current systemd does not: it +* dropped its /proc fallback once its kernel baseline reached 5.10, so every +* fork+exec under pseudo failed. Check the wrapper works, that it agrees +* with the kernel about bad arguments, that CLOSE_RANGE_UNSHARE keeps a +* process sharing the descriptor table unaffected, and that pseudo still +* functions after a caller closes every descriptor from 3 up. +*/ +#define _GNU_SOURCE + +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <sched.h> +#include <signal.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/wait.h> +#include <unistd.h> + +/* <linux/close_range.h> is missing on hosts with pre-5.9 kernel headers */ +#ifndef CLOSE_RANGE_UNSHARE +#define CLOSE_RANGE_UNSHARE (1U << 1) +#endif +#ifndef CLOSE_RANGE_CLOEXEC +#define CLOSE_RANGE_CLOEXEC (1U << 2) +#endif + +/* matches test-openat2-syscall: the shell wrapper turns 77 into "skipped" */ +#define SKIP 77 + +#define TESTFILE "test-close-range-file" + +static int open_null (void ) { +int fd =open ("/dev/null" ,O_RDONLY ); +if (fd <0 ) +perror ("open /dev/null" ); +return fd; +} + +static int is_closed (int fd) { +return fcntl (fd,F_GETFD ) ==-1 && errno ==EBADF ; +} + +/* Is close_range() there at all? The wrapper runs this with PSEUDO_DISABLED +* set, so the answer is the kernel's and a stubbed wrapper cannot pass +* itself off as an old kernel. +*/ +static int probe (void ) { +int fd =open_null (); + +if (fd <0 ) +return 1 ; +if (close_range (fd, fd,0 ) ==-1 ) { +int err = errno; +close (fd); +return (err ==ENOSYS ) ?SKIP :1 ; + } +return 0 ; +} + +/* CLOSE_RANGE_CLOEXEC is newer than the syscall: 5.11 rather than 5.9. Probe +* for it separately, the same way, so that on a 5.9 or 5.10 kernel only the +* CLOSE_RANGE_CLOEXEC part of the test is left out, not the whole thing. +*/ +static int probe_cloexec (void ) { +int fd =open_null (); +int rc; + +if (fd <0 ) +return 1 ; + rc =close_range (fd, fd,CLOSE_RANGE_CLOEXEC ); +if (rc ==-1 ) { +int err = errno; +close (fd); +return (err ==EINVAL || err ==ENOSYS ) ?SKIP :1 ; + } +close (fd); +return 0 ; +} + +/* for the CLONE_FILES children: close one descriptor, with or without +* CLOSE_RANGE_UNSHARE, and report whether it is gone for the child itself. +*/ +struct child_close { +int fd; +int flags; +}; + +static char child_stack[65536 ]; + +static int child_close_one (void *arg) { +struct child_close *cc = arg; + +if (close_range (cc->fd, cc->fd, cc->flags) ==-1 ) +return 2 ; +return is_closed (cc->fd) ?0 :3 ; +} + +int main (int argc,char **argv) { +struct child_close cc; +struct stat st; +int a, b, c; +int status; +int test_cloexec =1 ; +pid_t pid; + +if (argc >1 && !strcmp (argv[1 ],"--probe" )) +return probe (); +if (argc >1 && !strcmp (argv[1 ],"--probe-cloexec" )) +return probe_cloexec (); +if (argc >1 && !strcmp (argv[1 ],"--no-cloexec" )) + test_cloexec =0 ; + +/* the reported bug: this used to fail with ENOSYS every time */ + a =open_null (); +if (a <0 ) +return 1 ; +if (close_range (a, a,0 ) ==-1 ) { +fprintf (stderr,"close_range(%d, %d, 0): %s\n" , a, a,strerror (errno)); +return 1 ; + } +if (!is_closed (a)) { +fprintf (stderr,"close_range() left fd %d open\n" , a); +return 1 ; + } + +/* a bounded range closes all of itself */ + a =open_null (); + b =open_null (); + c =open_null (); +if (a <0 || b <0 || c <0 ) +return 1 ; +if (close_range (a, c,0 ) ==-1 ) { +perror ("close_range(a, c, 0)" ); +return 1 ; + } +if (!is_closed (a) || !is_closed (b) || !is_closed (c)) { +fprintf (stderr,"close_range(%d, %d, 0) left descriptors open\n" , a, c); +return 1 ; + } + +/* CLOSE_RANGE_CLOEXEC marks descriptors, it does not close them. The +* flag needs a 5.11 kernel; the shell wrapper passes --no-cloexec +* when the probe says this kernel has the syscall but not the flag. +*/ +if (test_cloexec) { + a =open_null (); +if (a <0 ) +return 1 ; +if (close_range (a, a,CLOSE_RANGE_CLOEXEC ) ==-1 ) { +perror ("close_range(a, a, CLOSE_RANGE_CLOEXEC)" ); +return 1 ; + } +if (is_closed (a)) { +fprintf (stderr,"CLOSE_RANGE_CLOEXEC closed fd %d\n" , a); +return 1 ; + } +if (!(fcntl (a,F_GETFD ) &FD_CLOEXEC )) { +fprintf (stderr,"CLOSE_RANGE_CLOEXEC did not set FD_CLOEXEC on fd %d\n" , a); +return 1 ; + } +close (a); + } + +/* bad arguments are refused, and a refused call closes nothing */ + a =open_null (); +if (a <0 ) +return 1 ; +if (close_range (a, a,0x80 ) !=-1 || errno !=EINVAL ) { +fprintf (stderr,"close_range() with an unknown flag should fail EINVAL\n" ); +return 1 ; + } +if (is_closed (a)) { +fprintf (stderr,"a rejected close_range() closed fd %d anyway\n" , a); +return 1 ; + } +if (close_range (a +1 , a,0 ) !=-1 || errno !=EINVAL ) { +fprintf (stderr,"close_range() with lowfd > maxfd should fail EINVAL\n" ); +return 1 ; + } +close (a); + +/* a range entirely above INT_MAX holds no descriptors at all, but it +* still has to be accepted rather than mangled on the way through +*/ +if (close_range ((unsigned int )INT_MAX +1 , ~0U ,0 ) ==-1 ) { +fprintf (stderr,"close_range(INT_MAX+1, ~0U, 0): %s\n" ,strerror (errno)); +return 1 ; + } + +/* CLOSE_RANGE_UNSHARE unshares the descriptor table before closing: +* when a child cloned with CLONE_FILES closes our shared descriptor +* with the flag set, we must still hold that descriptor afterwards. +* This is what the wrapper's unshare-first order preserves; closing +* by hand before unsharing would go through the still-shared table. +*/ + a =open_null (); +if (a <0 ) +return 1 ; + cc.fd = a; + cc.flags =CLOSE_RANGE_UNSHARE ; + pid =clone (child_close_one, child_stack +sizeof (child_stack), +CLONE_FILES |SIGCHLD , &cc); +if (pid ==-1 ) { +perror ("clone(CLONE_FILES)" ); +return 1 ; + } +if (waitpid (pid, &status,0 ) ==-1 ) { +perror ("waitpid" ); +return 1 ; + } +if (!WIFEXITED (status) ||WEXITSTATUS (status) !=0 ) { +fprintf (stderr,"CLOSE_RANGE_UNSHARE failed in the child (status %d)\n" , + status); +return 1 ; + } +if (is_closed (a)) { +fprintf (stderr,"child's CLOSE_RANGE_UNSHARE closed the parent's fd %d\n" , a); +return 1 ; + } + +/* the control for the test above: the same close without the flag +* happens in the shared table, so this time fd a must be gone for +* us as well. A clone that quietly stopped sharing the table would +* pass the test above no matter what the wrapper did; it fails here +* instead. +*/ + cc.flags =0 ; + pid =clone (child_close_one, child_stack +sizeof (child_stack), +CLONE_FILES |SIGCHLD , &cc); +if (pid ==-1 ) { +perror ("clone(CLONE_FILES)" ); +return 1 ; + } +if (waitpid (pid, &status,0 ) ==-1 ) { +perror ("waitpid" ); +return 1 ; + } +if (!WIFEXITED (status) ||WEXITSTATUS (status) !=0 ) { +fprintf (stderr,"close_range() failed in the CLONE_FILES child (status %d)\n" , + status); +return 1 ; + } +if (!is_closed (a)) { +fprintf (stderr,"close in a CLONE_FILES child did not reach the parent's fd %d\n" , a); +return 1 ; + } + +/* And the point of the care in the wrapper: pseudo keeps descriptors +* of its own in this range and has to come out of it still working. +*/ + a =open (TESTFILE ,O_CREAT |O_WRONLY ,0644 ); +if (a <0 ) { +perror ("open " TESTFILE ); +return 1 ; + } +close (a); +if (chown (TESTFILE ,0 ,0 ) ==-1 ) { +perror ("chown " TESTFILE ); +return 1 ; + } +if (close_range (3 , ~0U ,0 ) ==-1 ) { +fprintf (stderr,"close_range(3, ~0U, 0): %s\n" ,strerror (errno)); +return 1 ; + } +if (stat (TESTFILE , &st) ==-1 ) { +perror ("stat after close_range" ); +return 1 ; + } +if (st.st_uid !=0 || st.st_gid !=0 ) { +fprintf (stderr,"pseudo lost track after close_range: uid %d, gid %d\n" , + (int ) st.st_uid, (int ) st.st_gid); +return 1 ; + } +unlink (TESTFILE ); + +return 0 ; +}
test/test-close-range.sh new file +24 -0
@@ -0,0 +1,24 @@ +#!/bin/bash +# +# SPDX-License-Identifier: LGPL-2.1-only +# + +# close_range() needs a 5.9 kernel. Ask with pseudo out of the way, so that a +# wrapper which has gone back to returning ENOSYS cannot pass itself off as an +# old kernel and quietly skip the test it is supposed to fail. +PSEUDO_DISABLED=1 ./test/test-close-range --probe +case $? in +0) ;; +77) exit 255 ;; +*) exit 1 ;; +esac + +# CLOSE_RANGE_CLOEXEC needs 5.11. On a 5.9 or 5.10 kernel the syscall probe +# above succeeds but the flag does not exist yet, so probe for it the same +# way and leave only the CLOSE_RANGE_CLOEXEC part of the test out. +PSEUDO_DISABLED=1 ./test/test-close-range --probe-cloexec +case $? in +0) ./test/test-close-range ;; +77) ./test/test-close-range --no-cloexec ;; +*) exit 1 ;; +esac
Signature · 1 line
2.43.0
Quoted 7 lines · Babanpreet Singh
pseudo's
close_range()wrapper has returned ENOSYS unconditionally since 35433e6 ("ports/linux/guts: Addclose_rangewrapper for glibc 2.34"). systemd v260 deleted its/proc/self/fdfallback and treats a failure as fatal, so under pseudo every fork+exec dies. That is [YOCTO #16339]. Full story in v1:
I had a quick look through this and it looks ok to me, thanks!
Quoted 4 lines
The v1 question about
OP_CLOSEFROMstepping around five of pseudo's descriptors while others exist (pseudo_prefix_dir_fd,pseudo_pwd_lck_fd,pseudo_util_evlog_fd) still stands; happy to send a follow-up if that list is short in both places.
I had a quick look and yes, we should really be protecting these too.
That said, I couldn't see what pseudo_prefix_dir_fd actually helps with and I couldn't see why we need that...
Cheers,
Richard
Thanks!
Quoted 2 lines
That said, I couldn't see what
pseudo_prefix_dir_fdactually helps with
and I couldn't see why we need that...
I flagged it since it is held open and a close_range() sweep will wipe it out. I hadn't checked if anything still reads it untill now. It seems like it is just kept open and alive and the socket connection + fchdir() has been moved to pseudo_localstate_dir_fd.
So, for the follow-up I'd purpose:
1/2: remove pseudo_prefix_dir_fd
2/2: add pseudo_pwd_lck_fd and pseudo_util_evlog_fd to the descriptors OP_CLOSEFROM and OP_CLOSE_RANGE step around
If you'd rather keep the fd, I'll drop the removal and add it to the protected set in 2/2 instead.
Thanks for reviewing,
Baban.
Quoted 27 lines · Richard Purdie
Quoted 7 lines · Babanpreet Singh
pseudo's
close_range()wrapper has returned ENOSYS unconditionally since 35433e6 ("ports/linux/guts: Addclose_rangewrapper for glibc 2.34"). systemd v260 deleted its/proc/self/fdfallback and treats a failure as fatal, so under pseudo every fork+exec dies. That is [YOCTO #16339]. Full story in v1:https://lore.kernel.org/yocto-patches/[email protected]/
I had a quick look through this and it looks ok to me, thanks!
Quoted 3 lines
The v1 question about
OP_CLOSEFROMstepping around five of pseudo's descriptors while others exist (pseudo_prefix_dir_fd,pseudo_pwd_lck_fd,pseudo_util_evlog_fd) still stands; happy to send a follow-up if thatlist
Quoted 1 line
is short in both places.
I had a quick look and yes, we should really be protecting these too.
That said, I couldn't see what
pseudo_prefix_dir_fdactually helps with and I couldn't see why we need that...Cheers,
Richard
Quoted 5 lines · Babanpreet Singh
Thanks!
Quoted 2 lines
That said, I couldn't see what
pseudo_prefix_dir_fdactually helps with and I couldn't see why we need that...
Quoted 8 lines
I flagged it since it is held open and a
close_range()sweep will wipe it out. I hadn't checked if anything still reads it untill now. It seems like it is just kept open and alive and the socket connection +fchdir()has been moved topseudo_localstate_dir_fd.So, for the follow-up I'd purpose:
1/2: remove
pseudo_prefix_dir_fd
2/2: addpseudo_pwd_lck_fdandpseudo_util_evlog_fdto the descriptorsOP_CLOSEFROMandOP_CLOSE_RANGEstep aroundIf you'd rather keep the fd, I'll drop the removal and add it to the protected set in 2/2 instead.
I'd suggest making the series the other way around, protect it for now, send a removal patch on top and we can look at the removal patch and test and check it isn't really used anywhere. It is possible I'm missing some usage of it and I'm curious what Mark thinks.
The protection patch makes sense and should be easy to merge compared to that.
Cheers,
Richard
Quoted 13 lines · Richard Purdie via lists.yoctoproject.org
Quoted 12 lines · Babanpreet Singh
Thanks!
Quoted 2 lines
That said, I couldn't see what
pseudo_prefix_dir_fdactually helps with
and I couldn't see why we need that...I flagged it since it is held open and a
close_range()sweep will wipe it out. I hadn't checked if anything still reads it untill now. It seems like it is just kept open and alive and the socket connection +fchdir()has been moved topseudo_localstate_dir_fd.So, for the follow-up I'd purpose:
1/2: remove
pseudo_prefix_dir_fd
2/2: addpseudo_pwd_lck_fdandpseudo_util_evlog_fdto the descriptorsOP_CLOSEFROMandOP_CLOSE_RANGEstep around
Quoted 6 lines
Quoted 1 line
If you'd rather keep the fd, I'll drop the removal and add it to the protected set in 2/2 instead.
I'd suggest making the series the other way around, protect it for now, send a removal patch on top and we can look at the removal patch and test and check it isn't really used anywhere. It is possible I'm missing some usage of it and I'm curious what Mark thinks.
I agree. Maybe it's no longer needed, but we're not sure.
The code was introduced VERY early in pseudo development, 2010 era.
The variable, pseudo_prefix_dir_fd was introduced in 551bf567c171c9f6f475f02de80e35df9563dce7, but this is based on an earlier commit.
^33d9386 (Peter Seebach 2010-03-16 19:26:24 -0500 713) pseudo_path = pseudo_prefix_path(NULL);
^33d9386 (Peter Seebach 2010-03-16 19:26:24 -0500 714) if (pseudo_dir_fd == -1) {
^33d9386 (Peter Seebach 2010-03-16 19:26:24 -0500 715) if (pseudo_path) {
^33d9386 (Peter Seebach 2010-03-16 19:26:24 -0500 716) pseudo_dir_fd = open(pseudo_path, O_RDONLY);
^33d9386 (Peter Seebach 2010-03-16 19:26:24 -0500 717) pseudo_dir_fd = pseudo_fd(pseudo_dir_fd, COPY_FD);
^33d9386 (Peter Seebach 2010-03-16 19:26:24 -0500 718) free(pseudo_path);Unfortunately this was the _FIRST_ commit. So based on that, there really is no history why this was added to begin with.
From looking at that first commit, it was using this fd to spawn the server and change the cwd. By holding an open fd on the path we were sure that the directory wouldn't be removed until all the FDs cleared. (Some filesystems you can't delete open directories, other just postpone the removal until the fds clear..)
--Mark
Quoted 17 lines
The protection patch makes sense and should be easy to merge compared to that.
Cheers,
Richard
-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#4497): https://lists.yoctoproject.org/g/yocto-patches/message/4497
Mute This Topic: https://lists.yoctoproject.org/mt/120294678/3616948
Group Owner: [email protected]
Unsubscribe: https://lists.yoctoproject.org/g/yocto-patches/leave/13201099/3616948/947757854/xyzzy [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-
Quoted 16 lines · Richard Purdie via lists.yoctoproject.org
Quoted 7 lines · Babanpreet Singh
pseudo's
close_range()wrapper has returned ENOSYS unconditionally since 35433e6 ("ports/linux/guts: Addclose_rangewrapper for glibc 2.34"). systemd v260 deleted its/proc/self/fdfallback and treats a failure as fatal, so under pseudo every fork+exec dies. That is [YOCTO #16339]. Full story in v1:I had a quick look through this and it looks ok to me, thanks!
Quoted 4 lines
The v1 question about
OP_CLOSEFROMstepping around five of pseudo's descriptors while others exist (pseudo_prefix_dir_fd,pseudo_pwd_lck_fd,pseudo_util_evlog_fd) still stands; happy to send a follow-up if that list is short in both places.
Quoted 4 lines
I had a quick look and yes, we should really be protecting these too.
That said, I couldn't see what
pseudo_prefix_dir_fdactually helps with and I couldn't see why we need that...
This isn't a part of the system I've touched in a very long time. I THOUGHT it was the fd of the directory PSEUDO_PREFIX, so then when other files (sockets, etc) needed to be opened, it was all relative to the fd. This allowed the directory to 'move' if necessary, since we were sure we could still access it via the 'fd'. (or I'm completely wrong and remembering something different.)
The only 'definitive' reference I can find doesn't explain the why, just the what doc/program_flow.
I wonder if it was done to hold a file description on the directory so that if the PSEUDO_PATH was rm'd it wouldn't clear until pseudo itself was unloaded.
--Mark
Quoted 14 lines
Cheers,
Richard
-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#4494): https://lists.yoctoproject.org/g/yocto-patches/message/4494
Mute This Topic: https://lists.yoctoproject.org/mt/120294678/3616948
Group Owner: [email protected]
Unsubscribe: https://lists.yoctoproject.org/g/yocto-patches/leave/13201099/3616948/947757854/xyzzy [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-
cover closefrom/close_range: protect every pseudo fd, then drop one
In reply to Babanpreet SinghThis is the follow-up from the close_range v2 thread [1], in the order Richard suggested and Mark agreed with there: protect the whole set of descriptors first, then remove pseudo_prefix_dir_fd on top as a separately revertable commit. This has been built on top of v2.
While writing a probe to demonstrate the missing protection I noticed
something: the sweeps can close a descriptor that is already on the protect list. The startfd computation uses >, so a protected descriptor exactly equal to the running startfd is stepped around by neither the by-hand loop nor the kernel sweep. My understanding is that the default layout reliably produces that case: pseudo moves its own descriptors up with F_DUPFD from PSEUDO_MIN_FD, so the local state fd and connect_fd end up on consecutive numbers and connect_fd lands exactly on the computed startfd. If I am reading the history right, this means every closefrom(3) under pseudo has quietly closed the server connection since 21ff2fb in 2021, and it went unnoticed because the client transparently reconnects on the next operation. Patch 1 addresses that. Patches 2 and 3 are the protection and removal we discussed on the thread.
To convince myself I tested with a probe that locates pseudo's descriptors via /proc/self/fd, sweeps with close_range(3, ~0U, 0) or closefrom(3), and checks what survived. On the v2 baseline:
pwd lock fd (lckpwdf held) : fd 3 closed by sweep
PSEUDO_PREFIX dir fd : fd 20 closed by sweep
server socket fd : fd 22 closed by sweep
After patch 1 the socket survives, after patch 2 everything survives, and after patch 3 the prefix descriptor no longer exists to need protecting.
[1] https://lore.kernel.org/yocto-patches/[email protected]/
Babanpreet Singh (3):
pseudo_client: step fully past pseudo's own fds when computing startfd
pseudo_client: step around all of pseudo's own fds in
closefrom/close_range
pseudo_client: remove the unused pseudo_prefix_dir_fd
doc/program_flow | 1 - pseudo.h | 1 + pseudo_client.c | 90 ++++++++++++++---------------------------------- pseudo_client.h | 1 - 4 files changed, 27 insertions(+), 66 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/3 pseudo_client: step fully past pseudo's own fds when computing startfd
OP_CLOSEFROM and OP_CLOSE_RANGE compute startfd, the first descriptor the caller may hand to the kernel wholesale, by stepping past each descriptor pseudo needs to keep:
if (connect_fd > startfd) startfd = connect_fd + 1;
A protected descriptor exactly equal to the running startfd fails that test and ends up protected by nothing: the close-by-hand loop stops below startfd, the kernel sweep starts at startfd, and the descriptor is closed even though it is on the protect list.
The default descriptor layout hits this. Client init opens the local state directory fd and then connects to the server, so the two sit on consecutive numbers, say 21 and 22: the local state fd raises startfd to 22, connect_fd == 22 fails the > test, and closefrom(3) or close_range(3, ~0U, 0) hands 22 straight to the kernel. The server connection is closed out from under the client.
The client transparently reconnects on the next operation, which is why this has gone unnoticed, but until that reconnect the stale descriptor number is free for the process to reuse, and anything pseudo believes about the connection is attached to the wrong descriptor.
Use >= so startfd always ends up above every protected descriptor.
Present since the startfd computation was introduced in 21ff2fb ("ports/linux/guts: Add closefrom support for glibc 2.34").
AI-Generated: Uses Claude (claude-sonnet-5)
--- pseudo_client.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
pseudo_client.c +10 -10
@@ -1966,15 +1966,15 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *pathcase OP_CLOSEFROM :/* no request needed */ startfd = fd; -if (pseudo_util_debug_fd > startfd) +if (pseudo_util_debug_fd >= startfd) startfd = pseudo_util_debug_fd +1 ; -if (pseudo_localstate_dir_fd > startfd) +if (pseudo_localstate_dir_fd >= startfd) startfd = pseudo_localstate_dir_fd +1 ; -if (pseudo_pwd_fd > startfd) +if (pseudo_pwd_fd >= startfd) startfd = pseudo_pwd_fd +1 ; -if (pseudo_grp_fd > startfd) +if (pseudo_grp_fd >= startfd) startfd = pseudo_grp_fd +1 ; -if (connect_fd > startfd) +if (connect_fd >= startfd) startfd = connect_fd +1 ;for (i = fd; i < startfd; ++i) { if (i == pseudo_util_debug_fd || i == pseudo_localstate_dir_fd || i == pseudo_pwd_fd ||
@@ -1992,15 +1992,15 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *pathcase OP_CLOSE_RANGE :/* no request needed */ startfd = fd; -if (pseudo_util_debug_fd > startfd) +if (pseudo_util_debug_fd >= startfd) startfd = pseudo_util_debug_fd +1 ; -if (pseudo_localstate_dir_fd > startfd) +if (pseudo_localstate_dir_fd >= startfd) startfd = pseudo_localstate_dir_fd +1 ; -if (pseudo_pwd_fd > startfd) +if (pseudo_pwd_fd >= startfd) startfd = pseudo_pwd_fd +1 ; -if (pseudo_grp_fd > startfd) +if (pseudo_grp_fd >= startfd) startfd = pseudo_grp_fd +1 ; -if (connect_fd > startfd) +if (connect_fd >= startfd) startfd = connect_fd +1 ;/* the fds below startfd are the ones our own are mixed in * with, so close those by hand and skip the ones we need
Signature · 1 line
2.43.0
PATCH 2/3 pseudo_client: step around all of pseudo's own fds in closefrom/close_range
In reply to Babanpreet SinghOP_CLOSEFROM and OP_CLOSE_RANGE step around five descriptors pseudo keeps for itself (debug output, the local state directory, passwd, group, and the server connection), but pseudo can hold three more, and a sweep will close those:
- pseudo_util_evlog_fd is where pseudo_evlog_dump() writes the in-memory event log at the moment the client gives up on the server connection for good. It defaults to 2, so stepping around it matches the existing treatment of pseudo_util_debug_fd, which also defaults to 2 and is already protected. A closed (or worse, silently reused) descriptor here would misdirect exactly the diagnostics needed to debug a dead server.
- pseudo_pwd_lck_fd is open between lckpwdf() and ulckpwdf(), which is precisely the window in which a caller works on the passwd files and may sweep descriptors. Once the sweep has closed it, ulckpwdf()'s close() can land on an unrelated descriptor that reused the number.
- pseudo_prefix_dir_fd is opened at client init and stays open for the life of the process.
pseudo_pwd_lck_fd is -1 while no lock is held, which the existing pattern already copes with: -1 never raises startfd and never matches a descriptor the close-by-hand loop visits.
pseudo_util_evlog_fd had no declaration outside pseudo_util.c, so give it one in pseudo.h next to pseudo_util_debug_fd.
[YOCTO #16339]
AI-Generated: Uses Claude (claude-sonnet-5)
--- pseudo.h | 1 + pseudo_client.c | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-)
pseudo.h +1 -0
@@ -29,6 +29,7 @@ extern void pseudo_debug_flags_finalize(void);extern unsigned long pseudo_util_debug_flags;extern unsigned long pseudo_util_evlog_flags;extern int pseudo_util_debug_fd; +extern int pseudo_util_evlog_fd;extern int pseudo_disabled;extern int pseudo_allow_fsync;extern int pseudo_diag (char *, ...) __attribute__ ((format (printf,1 ,2 )));
pseudo_client.c +18 -2
@@ -1968,16 +1968,24 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *path startfd = fd;if (pseudo_util_debug_fd >= startfd) startfd = pseudo_util_debug_fd +1 ; +if (pseudo_util_evlog_fd >= startfd) + startfd = pseudo_util_evlog_fd +1 ; +if (pseudo_prefix_dir_fd >= startfd) + startfd = pseudo_prefix_dir_fd +1 ;if (pseudo_localstate_dir_fd >= startfd) startfd = pseudo_localstate_dir_fd +1 ;if (pseudo_pwd_fd >= startfd) startfd = pseudo_pwd_fd +1 ; +if (pseudo_pwd_lck_fd >= startfd) + startfd = pseudo_pwd_lck_fd +1 ;if (pseudo_grp_fd >= startfd) startfd = pseudo_grp_fd +1 ;if (connect_fd >= startfd) startfd = connect_fd +1 ;for (i = fd; i < startfd; ++i) { -if (i == pseudo_util_debug_fd || i == pseudo_localstate_dir_fd || i == pseudo_pwd_fd || +if (i == pseudo_util_debug_fd || i == pseudo_util_evlog_fd || + i == pseudo_prefix_dir_fd || i == pseudo_localstate_dir_fd || + i == pseudo_pwd_fd || i == pseudo_pwd_lck_fd || i == pseudo_grp_fd || i == connect_fd)continue ;pseudo_client_close (i);
@@ -1994,10 +2002,16 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *path startfd = fd;if (pseudo_util_debug_fd >= startfd) startfd = pseudo_util_debug_fd +1 ; +if (pseudo_util_evlog_fd >= startfd) + startfd = pseudo_util_evlog_fd +1 ; +if (pseudo_prefix_dir_fd >= startfd) + startfd = pseudo_prefix_dir_fd +1 ;if (pseudo_localstate_dir_fd >= startfd) startfd = pseudo_localstate_dir_fd +1 ;if (pseudo_pwd_fd >= startfd) startfd = pseudo_pwd_fd +1 ; +if (pseudo_pwd_lck_fd >= startfd) + startfd = pseudo_pwd_lck_fd +1 ;if (pseudo_grp_fd >= startfd) startfd = pseudo_grp_fd +1 ;if (connect_fd >= startfd)
@@ -2006,7 +2020,9 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *path* with, so close those by hand and skip the ones we need */ for (i = fd; i < startfd && (unsigned int ) i <= close_range_maxfd; ++i) { -if (i == pseudo_util_debug_fd || i == pseudo_localstate_dir_fd || i == pseudo_pwd_fd || +if (i == pseudo_util_debug_fd || i == pseudo_util_evlog_fd || + i == pseudo_prefix_dir_fd || i == pseudo_localstate_dir_fd || + i == pseudo_pwd_fd || i == pseudo_pwd_lck_fd || i == pseudo_grp_fd || i == connect_fd)continue ;pseudo_client_close (i);
Signature · 1 line
2.43.0
PATCH 3/3 pseudo_client: remove the unused pseudo_prefix_dir_fd
In reply to Babanpreet Singhpseudo has kept an O_RDONLY descriptor on PSEUDO_PREFIX since its first commit (33d9386, 2010), where client_connect() fchdir()'d through it so the server socket could be reached by a path short enough for sun_path. 551bf567 ("Add new environment values to allow easy override of default locations") moved that job to pseudo_localstate_dir_fd. Nothing has read pseudo_prefix_dir_fd back since then.
Two side effects of opening it go away. Client init created a missing PSEUDO_PREFIX with mkdir_p(); the default PSEUDO_LOCALSTATEDIR lives under the prefix and its own setup runs the same mkdir_p(), so the directory is still created there, while a custom PSEUDO_LOCALSTATEDIR never needed the prefix directory at client init. An unset or unopenable PSEUDO_PREFIX was a diagnostic and exit(1), and still is, from the local state setup directly below.
The sweeps in OP_CLOSEFROM and OP_CLOSE_RANGE no longer need to step around the descriptor, so the entries the previous commit added for it go away again.
Discussed in
https://lore.kernel.org/yocto-patches/[email protected]/
AI-Generated: Uses Claude (claude-sonnet-5)
--- doc/program_flow | 1 - pseudo_client.c | 66 +++++------------------------------------------- pseudo_client.h | 1 - 3 files changed, 6 insertions(+), 62 deletions(-)
doc/program_flow +0 -1
@@ -15,7 +15,6 @@ libpseudo execution flow: setup pseudo_logfile pseudo_client.c: pseudo_init_client() setup PSEUDO_DISABLED - setup pseudo_prefix_dir_fd setup pseudo_localstate_dir_fd setup PSEUDO_NOSYMLINKEXP setup PSEUDO_UIDS
pseudo_client.c +6 -60
120 lines not on this page · raw message
pseudo_client.h +0 -1
@@ -39,7 +39,6 @@ extern gid_t pseudo_egid;extern gid_t pseudo_sgid;extern gid_t pseudo_rgid;extern gid_t pseudo_fgid; -extern int pseudo_prefix_dir_fd;extern int pseudo_localstate_dir_fd;extern FILE *pseudo_pwd_open (void );extern FILE *pseudo_grp_open (void );
Signature · 1 line
2.43.0
Re: [pseudo] [PATCH 0/3] closefrom/close_range: protect every pseudo fd, then drop one
In reply to Babanpreet SinghHi Richard, Mark,
A gentle ping on this series and the close_range v2 it stacks on (which Richard okayed on-list on 16 Jul).
I noticed pseudo master has moved since (now ca47829, with the memory-handling and openat2 changes). I've checked that both series still apply cleanly on top of current master, so no rebase is needed. Happy to rebase and resend anyway if that's more convenient.
Thanks,
Baban
Re: [pseudo] [PATCH 0/3] closefrom/close_range: protect every pseudo fd, then drop one
Hi Baban,
Quoted 7 lines · Babanpreet Singh
A gentle ping on this series and the
close_rangev2 it stacks on (which Richard okayed on-list on 16 Jul).I noticed pseudo master has moved since (now ca47829, with the memory-handling and openat2 changes). I've checked that both series still apply cleanly on top of current master, so no rebase is needed. Happy to rebase and resend anyway if that's more convenient.
Sorry, we haven't forgotten about this, we even did discuss it briefly yesterday! Mark has struggled for time and I've been juggling a few other issues.
There were a few fixes ahead of yours in the queue, I have those merged as you mentioned. We then have yours and the logging fixes along with some tests Mark worked on which are next.
I've pushed your changes onto master-next where there were some minor conflicts with other changes. I'll run some wider tests on the resulting branch.
Cheers,
Richard
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [yocto-patches] [pseudo] [PATCH 0/3] closefrom/close_range: protect every pseudo fd, then drop one
Quoted 17 lines · Richard Purdie via lists.yoctoproject.org
Hi Baban,
Quoted 8 lines · Babanpreet Singh
A gentle ping on this series and the
close_rangev2 it stacks on (which Richard okayed on-list on 16 Jul).I noticed pseudo master has moved since (now ca47829, with the memory-handling and openat2 changes). I've checked that both series still apply cleanly on top of current master, so no rebase is needed.
Happy to rebase and resend anyway if that's more convenient.Sorry, we haven't forgotten about this, we even did discuss it briefly
yesterday! Mark has struggled for time and I've been juggling a few other issues.
Quoted 8 lines
There were a few fixes ahead of yours in the queue, I have those merged
as you mentioned. We then have yours and the logging fixes along with some tests Mark worked on which are next.I've pushed your changes onto master-next where there were some minor conflicts with other changes. I'll run some wider tests on the resulting branch.
Further testing showed the problems came back so I think I messed up the testing somewhere along the way, sorry :/. I'm pretty sure the close_range() implementation is the point things break so there must be some subtle issue in that code...
I'd suggest we reorder the patches and get the skip fds and drop the prefix fd patches merged/queued, then come back to how to get close_range working. I still thing one common op codepath for both cases makes the most sense.
Cheers,
Richard
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [yocto-patches] [pseudo] [PATCH 0/3] closefrom/close_range: protect every pseudo fd, then drop one
Quoted 33 lines · Richard Purdie via lists.yoctoproject.org
Quoted 17 lines · Richard Purdie via lists.yoctoproject.org
Hi Baban,
Quoted 8 lines · Babanpreet Singh
A gentle ping on this series and the
close_rangev2 it stacks on (which Richard okayed on-list on 16 Jul).I noticed pseudo master has moved since (now ca47829, with the memory-handling and openat2 changes). I've checked that both series still apply cleanly on top of current master, so no rebase is needed.
Happy to rebase and resend anyway if that's more convenient.Sorry, we haven't forgotten about this, we even did discuss it briefly
yesterday! Mark has struggled for time and I've been juggling a few other issues.Quoted 8 lines
There were a few fixes ahead of yours in the queue, I have those merged
as you mentioned. We then have yours and the logging fixes along with some tests Mark worked on which are next.I've pushed your changes onto master-next where there were some minor conflicts with other changes. I'll run some wider tests on the resulting branch.
Further testing showed the problems came back so I think I messed up the testing somewhere along the way, sorry :/. I'm pretty sure the
close_range()implementation is the point things break so there must be some subtle issue in that code...
Quoted 4 lines
I'd suggest we reorder the patches and get the skip fds and drop the prefix fd patches merged/queued, then come back to how to get
close_rangeworking. I still thing one common op codepath for both cases makes the most sense.
I think the missing piece is this:
pseudo_client.c +1 -1
@@ -1633,7 +1633,7 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *path } } -if (op !=OP_CHROOT && op !=OP_CHDIR && op !=OP_CLOSE && op !=OP_CLOSEFROM && op !=OP_DUP +if (op !=OP_CHROOT && op !=OP_CHDIR && op !=OP_CLOSE && op !=OP_CLOSEFROM && op !=OP_CLOSE_RANGE && op !=OP_DUP &&pseudo_client_ignore_path_chroot (path,0 )) {if (op ==OP_OPEN ) {/* Sanitise the path to have no trailing slash as this is convention in the database */
Cheers,
Richard
Re: [pseudo] [PATCH 0/3] closefrom/close_range: protect every pseudo fd, then drop one
Quoted 4 lines · Richard Purdie
I think the missing piece is this:
- if (op !=
OP_CHROOT&& op !=OP_CHDIR&& op !=OP_CLOSE&& op !=OP_CLOSEFROM&& op !=OP_DUP
+ if (op !=OP_CHROOT&& op !=OP_CHDIR&& op !=OP_CLOSE&& op !=OP_CLOSEFROM&& op !=OP_CLOSE_RANGE&& op !=OP_DUP
Sorry for the breakage, and thanks for chasing it down.
I can reproduce it both ways on master-next at 6d65d74, with a probe that opens an fd and calls close_range(fd, ~0U, 0):
no PSEUDO_*_PATHS set OK PSEUDO_IGNORE_PATHS covering the fd's path SIGSEGV PSEUDO_INCLUDE_PATHS set, path not in it SIGSEGV same env, closefrom() instead OK all of the above, plus your line OK
For OP_CLOSE_RANGE the path comes from fd_path(lowfd), the ignore check hits it, and pseudo_client_op() returns early with result still 0, which close_range.c then dereferences.
Two things before I respin. Do you want the three fd-protect patches resent, or will you carry the master-next commits since you have already resolved the conflicts? And the ignore-path line: fold it into the close_range patch with a Suggested-by, or keep it as your own commit ahead of it? Either works for me.
Thanks,
Baban
Re: [pseudo] [PATCH 0/3] closefrom/close_range: protect every pseudo fd, then drop one
Quoted 21 lines · Babanpreet Singh
Quoted 4 lines · Richard Purdie
I think the missing piece is this:
- if (op !=
OP_CHROOT&& op !=OP_CHDIR&& op !=OP_CLOSE&& op !=OP_CLOSEFROM&& op !=OP_DUP
+ if (op !=OP_CHROOT&& op !=OP_CHDIR&& op !=OP_CLOSE&& op !=OP_CLOSEFROM&& op !=OP_CLOSE_RANGE&& op !=OP_DUPSorry for the breakage, and thanks for chasing it down.
I can reproduce it both ways on master-next at 6d65d74, with a probe that opens an fd and calls
close_range(fd, ~0U, 0):no PSEUDO_*_PATHS set OK
PSEUDO_IGNORE_PATHScovering the fd's path SIGSEGVPSEUDO_INCLUDE_PATHSset, path not in it SIGSEGV same env,closefrom()instead OK all of the above, plus your line OKFor
OP_CLOSE_RANGEthe path comes fromfd_path(lowfd), the ignore check hits it, andpseudo_client_op()returns early with result still 0, whichclose_range.cthen dereferences.
Quoted 5 lines
Two things before I respin. Do you want the three fd-protect patches resent, or will you carry the master-next commits since you have already resolved the conflicts? And the ignore-path line: fold it into the
close_rangepatch with a Suggested-by, or keep it as your own commit ahead of it? Either works for me.
You can fold the above piece in with a suggested by, it doesn't make sense in a commit on its own. I can take the other fd-protect patches if you're ok with that change, I need to update the commit messages.
Whilst most builds are testing cleanly, we are seeing these two failures:
https://autobuilder.yoctoproject.org/valkyrie/#/builders/35/builds/4403
https://autobuilder.yoctoproject.org/valkyrie/#/builders/23/builds/4466
which look like a pseudo problem, and that is with the current pseudo master-next. I think there is still some issue remaining somewhere unfortunately. I'm afraid I don't know what it is.
Cheers,
Richard
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [yocto-patches] [pseudo] [PATCH 0/3] closefrom/close_range: protect every pseudo fd, then drop one
Quoted 43 lines · Richard Purdie via lists.yoctoproject.org
Quoted 26 lines · Babanpreet Singh
Quoted 6 lines · Richard Purdie
I think the missing piece is this:
- if (op !=
OP_CHROOT&& op !=OP_CHDIR&& op !=OP_CLOSE&& op !=OP_CLOSEFROM&& op !=OP_DUP
+ if (op !=OP_CHROOT&& op !=OP_CHDIR&& op !=OP_CLOSE&& op !=OP_CLOSEFROM&& op !=OP_CLOSE_RANGE&& op !=OP_DUPSorry for the breakage, and thanks for chasing it down.
I can reproduce it both ways on master-next at 6d65d74, with a probe that
opens an fd and callsclose_range(fd, ~0U, 0):no PSEUDO_*_PATHS set OK
PSEUDO_IGNORE_PATHScovering the fd's path SIGSEGVPSEUDO_INCLUDE_PATHSset, path not in it SIGSEGV same env,closefrom()instead OK all of the above, plus your line OKFor
OP_CLOSE_RANGEthe path comes fromfd_path(lowfd), the ignore check
hits it, andpseudo_client_op()returns early with result still 0, whichclose_range.cthen dereferences.Quoted 8 lines
Two things before I respin. Do you want the three fd-protect patches
resent, or will you carry the master-next commits since you have already
resolved the conflicts? And the ignore-path line: fold it into theclose_rangepatch with a Suggested-by, or keep it as your own commit ahead
of it? Either works for me.You can fold the above piece in with a suggested by, it doesn't make sense in a commit on its own. I can take the other fd-protect patches if you're ok with that change, I need to update the commit messages.
Whilst most builds are testing cleanly, we are seeing these two failures:
Quoted 6 lines
https://autobuilder.yoctoproject.org/valkyrie/#/builders/35/builds/4403
https://autobuilder.yoctoproject.org/valkyrie/#/builders/23/builds/4466which look like a pseudo problem, and that is with the current pseudo master-next. I think there is still some issue remaining somewhere unfortunately. I'm afraid I don't know what it is.
One of those doesn't look to be pseudo, the arm one definitely does though. It looks like it may be arch specific.
Cheers,
Richard
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [yocto-patches] [pseudo] [PATCH 0/3] closefrom/close_range: protect every pseudo fd, then drop one
Quoted 48 lines · Richard Purdie via lists.yoctoproject.org
Quoted 46 lines · Richard Purdie via lists.yoctoproject.org
Quoted 26 lines · Babanpreet Singh
Quoted 6 lines · Richard Purdie
I think the missing piece is this:
- if (op !=
OP_CHROOT&& op !=OP_CHDIR&& op !=OP_CLOSE&& op !=OP_CLOSEFROM&& op !=OP_DUP
+ if (op !=OP_CHROOT&& op !=OP_CHDIR&& op !=OP_CLOSE&& op !=OP_CLOSEFROM&& op !=OP_CLOSE_RANGE&& op !=OP_DUPSorry for the breakage, and thanks for chasing it down.
I can reproduce it both ways on master-next at 6d65d74, with a probe that
opens an fd and callsclose_range(fd, ~0U, 0):no PSEUDO_*_PATHS set OK
PSEUDO_IGNORE_PATHScovering the fd's path SIGSEGVPSEUDO_INCLUDE_PATHSset, path not in it SIGSEGV same env,closefrom()instead OK all of the above, plus your line OKFor
OP_CLOSE_RANGEthe path comes fromfd_path(lowfd), the ignore check
hits it, andpseudo_client_op()returns early with result still 0, whichclose_range.cthen dereferences.Quoted 8 lines
Two things before I respin. Do you want the three fd-protect patches
resent, or will you carry the master-next commits since you have already
resolved the conflicts? And the ignore-path line: fold it into theclose_rangepatch with a Suggested-by, or keep it as your own commit ahead
of it? Either works for me.You can fold the above piece in with a suggested by, it doesn't make sense in a commit on its own. I can take the other fd-protect patches if you're ok with that change, I need to update the commit messages.
Whilst most builds are testing cleanly, we are seeing these two failures:
https://autobuilder.yoctoproject.org/valkyrie/#/builders/35/builds/4403
https://autobuilder.yoctoproject.org/valkyrie/#/builders/23/builds/4466
Quoted 6 lines
Quoted 3 lines
which look like a pseudo problem, and that is with the current pseudo master-next. I think there is still some issue remaining somewhere unfortunately. I'm afraid I don't know what it is.
One of those doesn't look to be pseudo, the arm one definitely does though. It looks like it may be arch specific.
It took hours but I was able to work out this was a missing __open64_2 wrapper so I've sent a patch for that. That is unrelated to the close_range patch but somehow implementing that triggers a different codepath in systemd which then broke. I'll retest things and check there isn't anything else.
Cheers,
Richard
Re: [pseudo] [PATCH 0/3] closefrom/close_range: protect every pseudo fd, then drop one
Hi Richard,
Yes, that's fine by me, thanks for folding it in and for redoing the commit messages. I see the fd-protect patches on master with 1.9.10 on top.
I've also read through the close_range respin on master-next. The clamp and the OP_CLOSE_RANGE exemption match what I reproduced here.
Sorry the __open64_2 hunt cost you an afternoon. Happy to run the close_range probe and the OE reproducer against current master-next and report back if another data point would help.
I'll add a note to bug 16339 once the close_range side lands.
Cheers,
Baban
Re: [pseudo] [PATCH 0/3] closefrom/close_range: protect every pseudo fd, then drop one
Hi Baban,
Quoted 4 lines · Babanpreet Singh
Yes, that's fine by me, thanks for folding it in and for redoing the commit messages. I see the fd-protect patches on master with 1.9.10 on top.
Quoted 8 lines
I've also read through the
close_rangerespin on master-next. The clamp and theOP_CLOSE_RANGEexemption match what I reproduced here.Sorry the
__open64_2hunt cost you an afternoon. Happy to run theclose_rangeprobe and the OE reproducer against current master-next and report back if another data point would help.I'll add a note to bug 16339 once the
close_rangeside lands.
We're close, we just need a new version of the patch which makes close_range and close_from use the same OP call within pseudo. Do you want to write that or should I?
Cheers,
Richard
v3 cover close_range: implement it rather than return ENOSYS
Richard, I took the writing rather than leave it to you. This is the one op version.
close_range() and closefrom() both go through OP_CLOSE_RANGE now. Based on the two commits you have on master-next (5ce47e3 + 2d235dd), and the only changes from those are:
- OP_CLOSEFROM dropped from enums/op.in.
- Its case in pseudo_client_op() went with it. OP_CLOSE_RANGE already does the same walk over pseudo's own descriptors, and the ignore-path exemption now names one op instead of two.
- closefrom() calls the op with INT_MAX for the maximum.
- pseudo_client_closefrom() folded into pseudo_client_close_range(), which clamps to nfds anyway, so INT_MAX means what closefrom() wants.
The rest is your version, tweaks and all. pseudo_client.c comes out at +28/-10 against master rather than +67/-1.
2/2 is your test commit with nothing touched in it. I dropped your Signed-off-by from both, since 1/2 is no longer the code you signed off on and carrying it on 2/2 alone looked worse than dropping both.
Tested on x86_64, glibc 2.39, kernel 6.17.0-22:
- ./run_tests.sh: 45 tests, 5 skipped for missing xattr and acl support in this container, everything else passing except test-parallel-rename and test-parallel-symlinks. Those two are flaky here rather than broken by this: they fail on pristine 2a5521e as well, and across three runs of this branch one of them passed once.
- The probe matrix from July, run against this branch and against a build of your master-next version: byte identical output. It covers close_range(fd, ~0U, 0) with no path lists, under PSEUDO_IGNORE_PATHS, and under an OE-style PSEUDO_INCLUDE_PATHS, the maxfd variants (~0U, UINT_MAX-1, INT_MAX, 4096, fd), closefrom() under that same include list, and pseudo still faking ownership after both calls.
- linux-libc-headers do_install, with pseudo-native built from this
branch: succeeds. That is the task that died on valkyrie in July.
One gap I should name: the suite has no closefrom() test, so what the rewiring did to closefrom is covered by that probe and not by anything in the tree. I left it out because closefrom() is not declared before glibc 2.34 and I did not want to guess at what that does to the test build on older hosts. Happy to add a case to 2/2 if you would rather have it.
Babanpreet Singh (2):
ports/linux/guts: Implement close_range() instead of returning ENOSYS
tests: Add close_range() test
enums/op.in | 2 +- ports/linux/guts/close_range.c | 59 ++++++- ports/linux/guts/closefrom.c | 3 +- ports/linux/portdefs.h | 16 ++ pseudo_client.c | 38 +++-- test/test-close-range.c | 283 +++++++++++++++++++++++++++++++++ test/test-close-range.sh | 24 +++ 7 files changed, 406 insertions(+), 19 deletions(-) create mode 100644 test/test-close-range.c create mode 100755 test/test-close-range.sh
Signature · 1 line
2.43.0
PATCH v3 1/2 ports/linux/guts: Implement close_range() instead of returning ENOSYS
close_range() runs through a client side op, OP_CLOSE_RANGE. The op closes the descriptors below pseudo's own one at a time, skipping the ones pseudo keeps, clears the tracked paths across the range, and returns the first descriptor above pseudo's own so the caller can hand the rest of the range to the kernel.
closefrom() uses the same op, with INT_MAX as the maximum.
Unknown flags and a lowfd above maxfd return EINVAL before anything is closed. CLOSE_RANGE_UNSHARE unshares the descriptor table first, so the closes do not reach other processes sharing it. CLOSE_RANGE_CLOEXEC and a range starting above INT_MAX go straight to the kernel. A maxfd above INT_MAX is clamped for the op; the kernel still gets the caller's maxfd.
[YOCTO #16339]
[RP: Various tweaks for type conversion and path handling]
AI-Generated: Uses Claude (claude-opus-4-8)
--- enums/op.in | 2 +- ports/linux/guts/close_range.c | 59 ++++++++++++++++++++++++++++++---- ports/linux/guts/closefrom.c | 3 +- ports/linux/portdefs.h | 16 +++++++++ pseudo_client.c | 38 ++++++++++++++++------ 5 files changed, 99 insertions(+), 19 deletions(-)
enums/op.in +1 -1
@@ -27,4 +27,4 @@ remove-xattr, 1 set-xattr, 0 create-xattr, 1 replace-xattr, 1 -closefrom, 0 +close-range, 0
ports/linux/guts/close_range.c +52 -7
66 lines not on this page · raw message
ports/linux/guts/closefrom.c +2 -1
@@ -7,7 +7,8 @@*/ pseudo_msg_t *msg;/* this cleans up internal tables, and shouldn't make it to the server. Avoids pseudo's internal fds */ - msg =pseudo_client_op (OP_CLOSEFROM ,0 , fd,-1 ,0 ,0 ); +/* closefrom() has no top end, so the range op gets the highest fd there can be */ + msg =pseudo_client_op (OP_CLOSE_RANGE ,0 , fd,-1 ,0 ,0 ,INT_MAX );/* fds between fd and msg->fd are closed within the above function avoiding pseudo's own fds */ real_closefrom (msg->fd);
ports/linux/portdefs.h +16 -0
@@ -35,6 +35,22 @@ GLIBC_COMPAT_SYMBOL(memcpy,2.0);#include <sys/prctl.h> #include <linux/seccomp.h> +/* close_range()'s flags, and unshare(), are only declared by glibc under +* _GNU_SOURCE, which pseudo does not build with. <linux/close_range.h> is +* not an option either: it is absent on hosts with pre-5.9 kernel headers, +* the same problem SYS_openat2 has below. Both values are kernel ABI. +*/ +#ifndef CLOSE_RANGE_UNSHARE +#define CLOSE_RANGE_UNSHARE (1U << 1) +#endif +#ifndef CLOSE_RANGE_CLOEXEC +#define CLOSE_RANGE_CLOEXEC (1U << 2) +#endif +#ifndef CLONE_FILES +#define CLONE_FILES 0x00000400 +#endif +extern int unshare (int flags); +#ifndef _STAT_VER#if defined (__aarch64__) || defined (__riscv)#define _STAT_VER 0
pseudo_client.c +28 -10
@@ -950,13 +950,19 @@ pseudo_client_close(int fd) { } } +/* Drop the tracked paths for a range of descriptors. The range has a top +* end, so entries above it have to be left alone; closefrom() asks for +* everything by passing INT_MAX. +*/ static void -pseudo_client_closefrom (int fd) { -int i; -if (fd <0 || fd >= nfds) +pseudo_client_close_range (int lowfd,unsigned int maxfd) { +int i, top; + +if (lowfd <0 || lowfd >= nfds)return ; -for (i = fd; i < nfds; ++i) { + top = (maxfd >= (unsigned int ) nfds) ? nfds -1 : (int ) maxfd; +for (i = lowfd; i <= top; ++i) {free (fd_paths[i]); fd_paths[i] =0 ;
@@ -1584,6 +1590,7 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *pathstatic size_t alloced_len =0 ;int strip_slash;int startfd, i; +int close_range_maxfd =0 ;#ifdef PSEUDO_PROFILING struct timeval tv1_op, tv2_op;
@@ -1611,7 +1618,7 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *path } } -if (op !=OP_CHROOT && op !=OP_CHDIR && op !=OP_CLOSE && op !=OP_CLOSEFROM && op !=OP_DUP +if (op !=OP_CHROOT && op !=OP_CHDIR && op !=OP_CLOSE && op !=OP_CLOSE_RANGE && op !=OP_DUP &&pseudo_client_ignore_path_chroot (path,0 )) {if (op ==OP_OPEN ) {/* Sanitise the path to have no trailing slash as this is convention in the database */
@@ -1704,6 +1711,13 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *path }#endif +if (op ==OP_CLOSE_RANGE ) { +va_list ap; +va_start (ap, buf); + close_range_maxfd =va_arg (ap, int); +va_end (ap); + } +if (op ==OP_RENAME ) {va_list ap;if (!path) {
@@ -1884,7 +1898,7 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *pathcase OP_EXEC : do_request = pseudo_client_logging;break ; -case OP_CLOSEFROM : +case OP_CLOSE_RANGE :/* no request needed */ startfd = fd;if (pseudo_util_debug_fd >= startfd)
@@ -1901,7 +1915,10 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *path startfd = pseudo_grp_fd +1 ;if (connect_fd >= startfd) startfd = connect_fd +1 ; -for (i = fd; i < startfd; ++i) { +/* the fds below startfd are the ones our own are mixed in +* with, so close those by hand and skip the ones we need +*/ +for (i = fd; i < startfd && i <= close_range_maxfd; ++i) { if (i == pseudo_util_debug_fd || i == pseudo_util_evlog_fd || i == pseudo_localstate_dir_fd || i == pseudo_pwd_fd || i == pseudo_pwd_lck_fd || i == pseudo_grp_fd ||
@@ -1910,8 +1927,9 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *pathpseudo_client_close (i);close (i); } -pseudo_client_closefrom (startfd); -/* tell the caller to close from startfd instead of fd */ +if (close_range_maxfd >= startfd) +pseudo_client_close_range (startfd, close_range_maxfd); +/* tell the caller to start at startfd instead of fd */ result = &msg; msg.fd = startfd; do_request =0 ;
@@ -1996,7 +2014,7 @@ pseudo_client_op(pseudo_op_t op, int access, int fd, int dirfd, const char *pathbreak ; }/* result can only be set when PSEUDO_XATTRDB resulted in a -* successful store to or read from the local database or for OP_CLOSEFROM. +* successful store to or read from the local database or for OP_CLOSE_RANGE. */ if (do_request && !result) {#ifdef PSEUDO_PROFILING
Signature · 1 line
2.43.0
PATCH v3 2/2 tests: Add close_range() test
In reply to Babanpreet SinghCovers the ENOSYS regression itself, that the wrapper agrees with the kernel about bad arguments, and the part that actually needs the care:
pseudo keeps its own descriptors in the range, including the connection to its server, and has to come out of a close_range(3, ~0U, 0) still tracking ownership.
CLOSE_RANGE_UNSHARE is exercised against a child cloned withCLONE_FILES: the child closes a shared descriptor with the flag set, and the parent must still hold that descriptor afterwards, which is what the wrapper's unshare-before-closing order exists to preserve. A second child then closes the same descriptor without the flag and the parent must see it gone.
close_range() needs a 5.9 kernel and CLOSE_RANGE_CLOEXEC a 5.11 one, so the shell wrapper probes for each with PSEUDO_DISABLED=1 before running. On 5.9 and 5.10 the syscall probe succeeds, the flag probe fails, and only the CLOSE_RANGE_CLOEXEC part of the test is left out.
[YOCTO #16339]
AI-Generated: Uses Claude (claude-opus-4-8)
--- test/test-close-range.c | 283 +++++++++++++++++++++++++++++++++++++++ test/test-close-range.sh | 24 ++++ 2 files changed, 307 insertions(+) create mode 100644 test/test-close-range.c create mode 100755 test/test-close-range.sh
test/test-close-range.c new file +283 -0
@@ -0,0 +1,283 @@ +/* +* SPDX-License-Identifier: LGPL-2.1-only +* +* close_range() was stubbed out to return ENOSYS unconditionally, on the +* assumption that callers all cope with that. Current systemd does not: it +* dropped its /proc fallback once its kernel baseline reached 5.10, so every +* fork+exec under pseudo failed. Check the wrapper works, that it agrees +* with the kernel about bad arguments, that CLOSE_RANGE_UNSHARE keeps a +* process sharing the descriptor table unaffected, and that pseudo still +* functions after a caller closes every descriptor from 3 up. +*/ +#define _GNU_SOURCE + +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <sched.h> +#include <signal.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/wait.h> +#include <unistd.h> + +/* <linux/close_range.h> is missing on hosts with pre-5.9 kernel headers */ +#ifndef CLOSE_RANGE_UNSHARE +#define CLOSE_RANGE_UNSHARE (1U << 1) +#endif +#ifndef CLOSE_RANGE_CLOEXEC +#define CLOSE_RANGE_CLOEXEC (1U << 2) +#endif + +/* matches test-openat2-syscall: the shell wrapper turns 77 into "skipped" */ +#define SKIP 77 + +#define TESTFILE "test-close-range-file" + +static int open_null (void ) { +int fd =open ("/dev/null" ,O_RDONLY ); +if (fd <0 ) +perror ("open /dev/null" ); +return fd; +} + +static int is_closed (int fd) { +return fcntl (fd,F_GETFD ) ==-1 && errno ==EBADF ; +} + +/* Is close_range() there at all? The wrapper runs this with PSEUDO_DISABLED +* set, so the answer is the kernel's and a stubbed wrapper cannot pass +* itself off as an old kernel. +*/ +static int probe (void ) { +int fd =open_null (); + +if (fd <0 ) +return 1 ; +if (close_range (fd, fd,0 ) ==-1 ) { +int err = errno; +close (fd); +return (err ==ENOSYS ) ?SKIP :1 ; + } +return 0 ; +} + +/* CLOSE_RANGE_CLOEXEC is newer than the syscall: 5.11 rather than 5.9. Probe +* for it separately, the same way, so that on a 5.9 or 5.10 kernel only the +* CLOSE_RANGE_CLOEXEC part of the test is left out, not the whole thing. +*/ +static int probe_cloexec (void ) { +int fd =open_null (); +int rc; + +if (fd <0 ) +return 1 ; + rc =close_range (fd, fd,CLOSE_RANGE_CLOEXEC ); +if (rc ==-1 ) { +int err = errno; +close (fd); +return (err ==EINVAL || err ==ENOSYS ) ?SKIP :1 ; + } +close (fd); +return 0 ; +} + +/* for the CLONE_FILES children: close one descriptor, with or without +* CLOSE_RANGE_UNSHARE, and report whether it is gone for the child itself. +*/ +struct child_close { +int fd; +int flags; +}; + +static char child_stack[65536 ]; + +static int child_close_one (void *arg) { +struct child_close *cc = arg; + +if (close_range (cc->fd, cc->fd, cc->flags) ==-1 ) +return 2 ; +return is_closed (cc->fd) ?0 :3 ; +} + +int main (int argc,char **argv) { +struct child_close cc; +struct stat st; +int a, b, c; +int status; +int test_cloexec =1 ; +pid_t pid; + +if (argc >1 && !strcmp (argv[1 ],"--probe" )) +return probe (); +if (argc >1 && !strcmp (argv[1 ],"--probe-cloexec" )) +return probe_cloexec (); +if (argc >1 && !strcmp (argv[1 ],"--no-cloexec" )) + test_cloexec =0 ; + +/* the reported bug: this used to fail with ENOSYS every time */ + a =open_null (); +if (a <0 ) +return 1 ; +if (close_range (a, a,0 ) ==-1 ) { +fprintf (stderr,"close_range(%d, %d, 0): %s\n" , a, a,strerror (errno)); +return 1 ; + } +if (!is_closed (a)) { +fprintf (stderr,"close_range() left fd %d open\n" , a); +return 1 ; + } + +/* a bounded range closes all of itself */ + a =open_null (); + b =open_null (); + c =open_null (); +if (a <0 || b <0 || c <0 ) +return 1 ; +if (close_range (a, c,0 ) ==-1 ) { +perror ("close_range(a, c, 0)" ); +return 1 ; + } +if (!is_closed (a) || !is_closed (b) || !is_closed (c)) { +fprintf (stderr,"close_range(%d, %d, 0) left descriptors open\n" , a, c); +return 1 ; + } + +/* CLOSE_RANGE_CLOEXEC marks descriptors, it does not close them. The +* flag needs a 5.11 kernel; the shell wrapper passes --no-cloexec +* when the probe says this kernel has the syscall but not the flag. +*/ +if (test_cloexec) { + a =open_null (); +if (a <0 ) +return 1 ; +if (close_range (a, a,CLOSE_RANGE_CLOEXEC ) ==-1 ) { +perror ("close_range(a, a, CLOSE_RANGE_CLOEXEC)" ); +return 1 ; + } +if (is_closed (a)) { +fprintf (stderr,"CLOSE_RANGE_CLOEXEC closed fd %d\n" , a); +return 1 ; + } +if (!(fcntl (a,F_GETFD ) &FD_CLOEXEC )) { +fprintf (stderr,"CLOSE_RANGE_CLOEXEC did not set FD_CLOEXEC on fd %d\n" , a); +return 1 ; + } +close (a); + } + +/* bad arguments are refused, and a refused call closes nothing */ + a =open_null (); +if (a <0 ) +return 1 ; +if (close_range (a, a,0x80 ) !=-1 || errno !=EINVAL ) { +fprintf (stderr,"close_range() with an unknown flag should fail EINVAL\n" ); +return 1 ; + } +if (is_closed (a)) { +fprintf (stderr,"a rejected close_range() closed fd %d anyway\n" , a); +return 1 ; + } +if (close_range (a +1 , a,0 ) !=-1 || errno !=EINVAL ) { +fprintf (stderr,"close_range() with lowfd > maxfd should fail EINVAL\n" ); +return 1 ; + } +close (a); + +/* a range entirely above INT_MAX holds no descriptors at all, but it +* still has to be accepted rather than mangled on the way through +*/ +if (close_range ((unsigned int )INT_MAX +1 , ~0U ,0 ) ==-1 ) { +fprintf (stderr,"close_range(INT_MAX+1, ~0U, 0): %s\n" ,strerror (errno)); +return 1 ; + } + +/* CLOSE_RANGE_UNSHARE unshares the descriptor table before closing: +* when a child cloned with CLONE_FILES closes our shared descriptor +* with the flag set, we must still hold that descriptor afterwards. +* This is what the wrapper's unshare-first order preserves; closing +* by hand before unsharing would go through the still-shared table. +*/ + a =open_null (); +if (a <0 ) +return 1 ; + cc.fd = a; + cc.flags =CLOSE_RANGE_UNSHARE ; + pid =clone (child_close_one, child_stack +sizeof (child_stack), +CLONE_FILES |SIGCHLD , &cc); +if (pid ==-1 ) { +perror ("clone(CLONE_FILES)" ); +return 1 ; + } +if (waitpid (pid, &status,0 ) ==-1 ) { +perror ("waitpid" ); +return 1 ; + } +if (!WIFEXITED (status) ||WEXITSTATUS (status) !=0 ) { +fprintf (stderr,"CLOSE_RANGE_UNSHARE failed in the child (status %d)\n" , + status); +return 1 ; + } +if (is_closed (a)) { +fprintf (stderr,"child's CLOSE_RANGE_UNSHARE closed the parent's fd %d\n" , a); +return 1 ; + } + +/* the control for the test above: the same close without the flag +* happens in the shared table, so this time fd a must be gone for +* us as well. A clone that quietly stopped sharing the table would +* pass the test above no matter what the wrapper did; it fails here +* instead. +*/ + cc.flags =0 ; + pid =clone (child_close_one, child_stack +sizeof (child_stack), +CLONE_FILES |SIGCHLD , &cc); +if (pid ==-1 ) { +perror ("clone(CLONE_FILES)" ); +return 1 ; + } +if (waitpid (pid, &status,0 ) ==-1 ) { +perror ("waitpid" ); +return 1 ; + } +if (!WIFEXITED (status) ||WEXITSTATUS (status) !=0 ) { +fprintf (stderr,"close_range() failed in the CLONE_FILES child (status %d)\n" , + status); +return 1 ; + } +if (!is_closed (a)) { +fprintf (stderr,"close in a CLONE_FILES child did not reach the parent's fd %d\n" , a); +return 1 ; + } + +/* And the point of the care in the wrapper: pseudo keeps descriptors +* of its own in this range and has to come out of it still working. +*/ + a =open (TESTFILE ,O_CREAT |O_WRONLY ,0644 ); +if (a <0 ) { +perror ("open " TESTFILE ); +return 1 ; + } +close (a); +if (chown (TESTFILE ,0 ,0 ) ==-1 ) { +perror ("chown " TESTFILE ); +return 1 ; + } +if (close_range (3 , ~0U ,0 ) ==-1 ) { +fprintf (stderr,"close_range(3, ~0U, 0): %s\n" ,strerror (errno)); +return 1 ; + } +if (stat (TESTFILE , &st) ==-1 ) { +perror ("stat after close_range" ); +return 1 ; + } +if (st.st_uid !=0 || st.st_gid !=0 ) { +fprintf (stderr,"pseudo lost track after close_range: uid %d, gid %d\n" , + (int ) st.st_uid, (int ) st.st_gid); +return 1 ; + } +unlink (TESTFILE ); + +return 0 ; +}
test/test-close-range.sh new file +24 -0
@@ -0,0 +1,24 @@ +#!/bin/bash +# +# SPDX-License-Identifier: LGPL-2.1-only +# + +# close_range() needs a 5.9 kernel. Ask with pseudo out of the way, so that a +# wrapper which has gone back to returning ENOSYS cannot pass itself off as an +# old kernel and quietly skip the test it is supposed to fail. +PSEUDO_DISABLED=1 ./test/test-close-range --probe +case $? in +0) ;; +77) exit 255 ;; +*) exit 1 ;; +esac + +# CLOSE_RANGE_CLOEXEC needs 5.11. On a 5.9 or 5.10 kernel the syscall probe +# above succeeds but the flag does not exist yet, so probe for it the same +# way and leave only the CLOSE_RANGE_CLOEXEC part of the test out. +PSEUDO_DISABLED=1 ./test/test-close-range --probe-cloexec +case $? in +0) ./test/test-close-range ;; +77) ./test/test-close-range --no-cloexec ;; +*) exit 1 ;; +esac
Signature · 1 line
2.43.0