All of lore.kernel.org
 help / color / mirror / Atom feed
From: Babanpreet Singh <bbnpreetsingh@gmail.com>
To: yocto-patches@lists.yoctoproject.org
Cc: Richard Purdie <richard.purdie@linuxfoundation.org>,
	Mark Hatle <mark.hatle@amd.com>,
	Mark Hatle <mark.hatle@kernel.crashing.org>,
	Paul Barker <paul@pbarker.dev>,
	Randy MacLeod <randy.macleod@windriver.com>,
	Vincent Haupert <mail@vincent-haupert.de>,
	Babanpreet Singh <bbnpreetsingh@gmail.com>
Subject: [pseudo] [PATCH v3 1/2] ports/linux/guts: Implement close_range() instead of returning ENOSYS
Date: Wed,  5 Aug 2026 06:22:57 +0000	[thread overview]
Message-ID: <20260805062258.7-2-bbnpreetsingh@gmail.com> (raw)
In-Reply-To: <20260805062258.7-1-bbnpreetsingh@gmail.com>

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)
Signed-off-by: Babanpreet Singh <bbnpreetsingh@gmail.com>
---
 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(-)

diff --git a/enums/op.in b/enums/op.in
index 5b5e21b..5854b74 100644
--- a/enums/op.in
+++ b/enums/op.in
@@ -27,4 +27,4 @@ remove-xattr, 1
 set-xattr, 0
 create-xattr, 1
 replace-xattr, 1
-closefrom, 0
+close-range, 0
diff --git a/ports/linux/guts/close_range.c b/ports/linux/guts/close_range.c
index 4bd2fe1..1964c70 100644
--- a/ports/linux/guts/close_range.c
+++ b/ports/linux/guts/close_range.c
@@ -6,14 +6,59 @@
  * int close_range(unsigned int lowfd, unsigned int maxfd, int flags)
  *      int rc = -1;
  */
+	pseudo_msg_t *msg;
+	int maxintfd;
 
-        (void) lowfd;
-        (void) maxfd;
-        (void) flags;
-        /* for now pretend the kernel doesn't support it regardless 
-           which users are supposed to be able to handle */
-        errno = ENOSYS;
-        rc = -1;
+	/* The kernel rejects both of these outright and closes nothing when
+	 * it does, so validate before touching anything.
+	 */
+	if (flags & ~(CLOSE_RANGE_UNSHARE | CLOSE_RANGE_CLOEXEC)) {
+		errno = EINVAL;
+		return -1;
+	}
+	if (lowfd > maxfd) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	/* CLOSE_RANGE_UNSHARE has to take effect before anything is closed:
+	 * while the descriptor table is still shared, closing a descriptor
+	 * would close it for everyone sharing the table, not just for us.
+	 */
+	if (flags & CLOSE_RANGE_UNSHARE) {
+		if (unshare(CLONE_FILES) == -1)
+			return -1;
+		flags &= ~CLOSE_RANGE_UNSHARE;
+	}
+
+	/* CLOSE_RANGE_CLOEXEC closes nothing, it only marks descriptors, and
+	 * pseudo's own are close-on-exec already (pseudo_fd() sets that on
+	 * every one of them), so there is nothing here to protect.
+	 */
+	if (flags & CLOSE_RANGE_CLOEXEC)
+		return real_close_range(lowfd, maxfd, flags);
+
+	/* Descriptors are ints, so a range starting above INT_MAX cannot hold
+	 * any of pseudo's own and there is nothing to step around. Worth its
+	 * own case because pseudo_client_op() takes the low end as an int.
+	 */
+	if (lowfd > INT_MAX)
+		return real_close_range(lowfd, maxfd, flags);
+	if (maxfd > INT_MAX)
+		maxintfd = INT_MAX;
+	else
+		maxintfd = (int) maxfd;
+
+	/* The op closefrom() also goes through: it closes the descriptors
+	 * pseudo's 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, maxintfd);
+	if (maxfd >= (unsigned int) msg->fd)
+		rc = real_close_range(msg->fd, maxfd, flags);
+	else
+		rc = 0;
 
 /*      return rc;
  * }
diff --git a/ports/linux/guts/closefrom.c b/ports/linux/guts/closefrom.c
index 1350506..7d5df31 100644
--- a/ports/linux/guts/closefrom.c
+++ b/ports/linux/guts/closefrom.c
@@ -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);
 
diff --git a/ports/linux/portdefs.h b/ports/linux/portdefs.h
index 19bb232..1f1a41a 100644
--- a/ports/linux/portdefs.h
+++ b/ports/linux/portdefs.h
@@ -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
diff --git a/pseudo_client.c b/pseudo_client.c
index 6a7fef7..d83175e 100644
--- a/pseudo_client.c
+++ b/pseudo_client.c
@@ -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 *path
 	static 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 *path
 	case 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 *path
 			pseudo_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 *path
 		break;
 	}
 	/* 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
-- 
2.43.0



  reply	other threads:[~2026-08-05  6:23 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-15  5:41 [pseudo] [PATCH 0/2] close_range: implement it rather than return ENOSYS Baban
2026-07-15  5:41 ` [pseudo] [PATCH 1/2] ports/linux/guts: Implement close_range() instead of returning ENOSYS Baban
2026-07-15 19:51   ` [yocto-patches] " Paul Barker
2026-07-15  5:41 ` [pseudo] [PATCH 2/2] tests: Add close_range() test Baban
2026-07-15 20:03   ` [yocto-patches] " Paul Barker
2026-07-16  5:56 ` [pseudo] [PATCH v2 0/2] close_range: implement it rather than return ENOSYS Babanpreet Singh
2026-07-16  5:56   ` [pseudo] [PATCH v2 1/2] ports/linux/guts: Implement close_range() instead of returning ENOSYS Babanpreet Singh
2026-07-28  9:22     ` Richard Purdie
     [not found]     ` <18C669BEC7E582E7.1643407@lists.yoctoproject.org>
2026-07-28 10:15       ` [yocto-patches] " Richard Purdie
     [not found]       ` <18C66CA235EABC94.3010641@lists.yoctoproject.org>
2026-07-28 10:48         ` Richard Purdie
2026-07-16  5:56   ` [pseudo] [PATCH v2 2/2] tests: Add close_range() test Babanpreet Singh
2026-07-16 10:46   ` [pseudo] [PATCH v2 0/2] close_range: implement it rather than return ENOSYS Richard Purdie
2026-07-16 15:46     ` Babanpreet Singh
2026-07-16 16:55       ` Richard Purdie
2026-07-16 23:16         ` [yocto-patches] " Mark Hatle
2026-07-16 23:08     ` Mark Hatle
2026-07-18  4:37   ` [pseudo] [PATCH 0/3] closefrom/close_range: protect every pseudo fd, then drop one Babanpreet Singh
2026-07-18  4:37     ` [pseudo] [PATCH 1/3] pseudo_client: step fully past pseudo's own fds when computing startfd Babanpreet Singh
2026-07-18  4:37     ` [pseudo] [PATCH 2/3] pseudo_client: step around all of pseudo's own fds in closefrom/close_range Babanpreet Singh
2026-07-18  4:37     ` [pseudo] [PATCH 3/3] pseudo_client: remove the unused pseudo_prefix_dir_fd Babanpreet Singh
2026-07-28  5:08     ` [pseudo] [PATCH 0/3] closefrom/close_range: protect every pseudo fd, then drop one Babanpreet Singh
2026-07-28  8:14       ` Richard Purdie
     [not found]       ` <18C6660077364169.3010641@lists.yoctoproject.org>
2026-07-28 15:48         ` [yocto-patches] " Richard Purdie
     [not found]         ` <18C67ECA93F98D2F.1643407@lists.yoctoproject.org>
2026-07-28 15:59           ` Richard Purdie
2026-07-29  5:08             ` Babanpreet Singh
2026-07-29  6:19               ` Richard Purdie
     [not found]               ` <18C6AE58128B91FB.3067440@lists.yoctoproject.org>
2026-07-29  8:14                 ` [yocto-patches] " Richard Purdie
     [not found]                 ` <18C6B495C3317260.3067440@lists.yoctoproject.org>
2026-07-29 14:55                   ` Richard Purdie
2026-07-30  5:07                     ` Babanpreet Singh
2026-08-04 21:13                       ` Richard Purdie
2026-08-05  6:22                         ` [pseudo] [PATCH v3 0/2] close_range: implement it rather than return ENOSYS Babanpreet Singh
2026-08-05  6:22                           ` Babanpreet Singh [this message]
2026-08-05  6:22                           ` [pseudo] [PATCH v3 2/2] tests: Add close_range() test Babanpreet Singh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260805062258.7-2-bbnpreetsingh@gmail.com \
    --to=bbnpreetsingh@gmail.com \
    --cc=mail@vincent-haupert.de \
    --cc=mark.hatle@amd.com \
    --cc=mark.hatle@kernel.crashing.org \
    --cc=paul@pbarker.dev \
    --cc=randy.macleod@windriver.com \
    --cc=richard.purdie@linuxfoundation.org \
    --cc=yocto-patches@lists.yoctoproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.