Skip to content

Commit 91ba8a3

Browse files
committed
fix(subprocess): close inherited descriptors with kernel range operation
Use the Linux close_range syscall or BSD closefrom before exec, retaining the bounded close loop when the range operation is unavailable. Add coverage of descriptor closure, strategy selection, and forced fallback. Refs DeusData#1484 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 96c3f41 commit 91ba8a3

3 files changed

Lines changed: 174 additions & 3 deletions

File tree

‎src/foundation/subprocess.c‎

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#include <spawn.h>
3131
#endif
3232
extern char **environ;
33+
#ifdef __linux__
34+
#include <sys/syscall.h> /* SYS_close_range: raw syscall, see cbm_posix_close_fds_from */
35+
#endif
3336
#include <sys/stat.h>
3437
#include <sys/wait.h>
3538
#include <unistd.h>
@@ -1069,6 +1072,64 @@ static void cbm_posix_reset_child_signals(void) {
10691072
(void)sigprocmask(SIG_SETMASK, &empty, NULL);
10701073
}
10711074

1075+
/* Close every descriptor >= lowfd in the fork+exec child (#1484).
1076+
*
1077+
* The portable answer is a close() per possible descriptor, which costs
1078+
* O(RLIMIT_NOFILE) syscalls: at nofile=524288 that is ~0.8 s between fork and
1079+
* exec for EVERY spawn, independent of how many descriptors are actually open.
1080+
* The kernel can do the same in one call:
1081+
* - Linux >= 5.9: close_range(lowfd, ~0U, 0). Issued as a raw syscall, not
1082+
* through the libc wrapper: the static musl release build has no wrapper
1083+
* and the glibc wrapper only exists from 2.34, while the syscall number is
1084+
* 436 on the supported x86-64 and AArch64 targets. Other architectures
1085+
* use their header's number, or the loop if no number is available.
1086+
* An older kernel answers
1087+
* ENOSYS (or EPERM under a seccomp filter that predates it) and we fall back.
1088+
* - FreeBSD/OpenBSD/NetBSD/DragonFly: closefrom(lowfd).
1089+
* - otherwise (and on fallback): the bounded close() loop.
1090+
* macOS never reaches this on its primary path: posix_spawn's
1091+
* CLOEXEC_DEFAULT does the closing (see cbm_posix_spawn_apple).
1092+
*
1093+
* Runs between fork and exec, so it is async-signal-safe: raw syscalls and
1094+
* close() only, no allocation, no stdio. Returns the strategy that did the
1095+
* work so the test seam can assert which one ran. */
1096+
#if defined(__linux__) && !defined(SYS_close_range) && (defined(__x86_64__) || defined(__aarch64__))
1097+
#define SYS_close_range 436
1098+
#endif
1099+
1100+
#ifdef CBM_ENABLE_TEST_SEAMS
1101+
static bool g_force_close_range_enosys = false;
1102+
#endif
1103+
1104+
static cbm_fd_close_strategy_t cbm_posix_close_fds_from(int lowfd, long max_fd) {
1105+
#if defined(__linux__) && defined(SYS_close_range)
1106+
bool range_unavailable = false;
1107+
#ifdef CBM_ENABLE_TEST_SEAMS
1108+
range_unavailable = g_force_close_range_enosys;
1109+
#endif
1110+
if (!range_unavailable && syscall(SYS_close_range, (unsigned int)lowfd, ~0U, 0U) == 0) {
1111+
return CBM_FD_CLOSE_RANGE;
1112+
}
1113+
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
1114+
(void)max_fd;
1115+
closefrom(lowfd);
1116+
return CBM_FD_CLOSEFROM;
1117+
#endif
1118+
for (int fd = lowfd; fd < max_fd; fd++) {
1119+
(void)close(fd);
1120+
}
1121+
return CBM_FD_CLOSE_LOOP;
1122+
}
1123+
1124+
#ifdef CBM_ENABLE_TEST_SEAMS
1125+
void cbm_subprocess_force_close_range_enosys_for_testing(bool force) {
1126+
g_force_close_range_enosys = force;
1127+
}
1128+
cbm_fd_close_strategy_t cbm_subprocess_close_fds_from_for_testing(int lowfd, long max_fd) {
1129+
return cbm_posix_close_fds_from(lowfd, max_fd);
1130+
}
1131+
#endif
1132+
10721133
/* fork+exec child setup. On Apple this runs ONLY for the exec-failure
10731134
* fallback (see cbm_posix_spawn_apple), which preserves the documented
10741135
* "bogus binary => child exits 127" contract across platforms. */
@@ -1090,9 +1151,7 @@ static void cbm_posix_child_exec(cbm_subprocess_t *process, int input, int outpu
10901151
if (output > STDERR_FILENO) {
10911152
(void)close(output);
10921153
}
1093-
for (int fd = STDERR_FILENO + 1; fd < max_fd; fd++) {
1094-
(void)close(fd);
1095-
}
1154+
(void)cbm_posix_close_fds_from(STDERR_FILENO + 1, max_fd);
10961155
if (process->envp) {
10971156
environ = process->envp; /* execvp passes environ to the new image */
10981157
}

‎src/foundation/subprocess.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,21 @@ void cbm_subprocess_force_spawn_eagain_for_testing(int attempts);
193193
int cbm_subprocess_pending_spawn_eagain_for_testing(void);
194194
#endif
195195

196+
/* How the POSIX fork+exec child closed its inherited descriptors (#1484). */
197+
typedef enum {
198+
CBM_FD_CLOSE_RANGE = 1, /* Linux close_range(2): one syscall */
199+
CBM_FD_CLOSEFROM, /* BSD closefrom(3): one call */
200+
CBM_FD_CLOSE_LOOP /* close() per descriptor up to _SC_OPEN_MAX: O(RLIMIT_NOFILE) */
201+
} cbm_fd_close_strategy_t;
202+
203+
#if defined(CBM_ENABLE_TEST_SEAMS) && !defined(_WIN32)
204+
/* Run the child's close-inherited-descriptors step in the CALLING process for
205+
* descriptors >= lowfd (callers pick a lowfd above everything they need) and
206+
* report which strategy did the work. Test builds only. */
207+
cbm_fd_close_strategy_t cbm_subprocess_close_fds_from_for_testing(int lowfd, long max_fd);
208+
/* Make the close_range fast path behave as if the kernel lacked it (ENOSYS),
209+
* so the fallback loop is exercised deterministically. Test builds only. */
210+
void cbm_subprocess_force_close_range_enosys_for_testing(bool force);
211+
#endif
212+
196213
#endif /* CBM_SUBPROCESS_H */

‎tests/test_subprocess.c‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
#include <sys/time.h>
2828
#include <sys/wait.h>
2929
#include <unistd.h>
30+
#ifdef __linux__
31+
#include <sys/syscall.h>
32+
#if !defined(SYS_close_range) && (defined(__x86_64__) || defined(__aarch64__))
33+
#define SYS_close_range 436
34+
#endif
35+
#endif
3036
#else
3137
#include <windows.h>
3238
#include "../src/foundation/win_utf8.h"
@@ -1012,6 +1018,94 @@ TEST(subprocess_posix_child_closes_unrelated_descriptors) {
10121018
#endif
10131019
}
10141020

1021+
/* #1484: the child's close-inherited-descriptors step must not cost
1022+
* O(RLIMIT_NOFILE) syscalls. The seam runs the exact child routine in this
1023+
* process against two descriptors parked at the TOP of the descriptor table
1024+
* (nothing else lives there), and reports which strategy did the work. On
1025+
* Linux that must be close_range(2) whenever the kernel has it -- the per-fd
1026+
* loop is the bug. The ENOSYS-injected leg proves the fallback still closes. */
1027+
#ifndef _WIN32
1028+
static bool subprocess_fd_is_closed(int fd) {
1029+
return fcntl(fd, F_GETFD) == -1 && errno == EBADF;
1030+
}
1031+
1032+
static cbm_fd_close_strategy_t subprocess_expected_close_strategy(void) {
1033+
#if defined(__linux__) && defined(SYS_close_range)
1034+
/* Probe the kernel on an empty range: an old kernel (< 5.9) or a seccomp
1035+
* filter without it answers ENOSYS/EPERM, and then the loop is correct. */
1036+
long probe = syscall(SYS_close_range, ~0U, ~0U, 0U);
1037+
return (probe == 0 || errno == EINVAL) ? CBM_FD_CLOSE_RANGE : CBM_FD_CLOSE_LOOP;
1038+
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
1039+
return CBM_FD_CLOSEFROM;
1040+
#else
1041+
return CBM_FD_CLOSE_LOOP;
1042+
#endif
1043+
}
1044+
1045+
/* Park two non-CLOEXEC descriptors near the top of the table; returns the lower
1046+
* of the two and hands back the original (low) descriptor in *keep_low. The
1047+
* kernel may cap the table below _SC_OPEN_MAX (macOS: kern.maxfilesperproc),
1048+
* so the start point halves until F_DUPFD accepts it. */
1049+
static int subprocess_park_high_fds(int *keep_low) {
1050+
char path[] = "/tmp/cbm-subprocess-highfd-XXXXXX";
1051+
int low = cbm_mkstemp(path);
1052+
if (low < 0) {
1053+
return -1;
1054+
}
1055+
(void)unlink(path);
1056+
long top = sysconf(_SC_OPEN_MAX);
1057+
if (top <= 0 || top > 1048576L) {
1058+
top = 1048576L;
1059+
}
1060+
int high = -1;
1061+
for (long base = top - 4; high < 0 && base > 64; base /= 2) {
1062+
high = fcntl(low, F_DUPFD, (int)base);
1063+
}
1064+
int higher = high >= 0 ? fcntl(low, F_DUPFD, high + 1) : -1;
1065+
if (higher != high + 1) {
1066+
(void)close(low);
1067+
if (high >= 0) {
1068+
(void)close(high);
1069+
}
1070+
if (higher >= 0) {
1071+
(void)close(higher);
1072+
}
1073+
return -1;
1074+
}
1075+
*keep_low = low;
1076+
return high;
1077+
}
1078+
#endif
1079+
1080+
TEST(subprocess_child_close_fds_uses_one_syscall_not_rlimit_loop) {
1081+
#ifdef _WIN32
1082+
SKIP_PLATFORM("POSIX fork+exec descriptor closing; Windows uses a handle allow-list");
1083+
#else
1084+
for (int inject_enosys = 0; inject_enosys <= 1; inject_enosys++) {
1085+
int keep_low = -1;
1086+
int high = subprocess_park_high_fds(&keep_low);
1087+
ASSERT_TRUE(high > STDERR_FILENO);
1088+
cbm_subprocess_force_close_range_enosys_for_testing(inject_enosys != 0);
1089+
/* The loop bound covers exactly the parked pair; close_range has none. */
1090+
cbm_fd_close_strategy_t used =
1091+
cbm_subprocess_close_fds_from_for_testing(high, (long)high + 2);
1092+
cbm_subprocess_force_close_range_enosys_for_testing(false);
1093+
bool closed = subprocess_fd_is_closed(high) && subprocess_fd_is_closed(high + 1);
1094+
bool low_survived = fcntl(keep_low, F_GETFD) >= 0;
1095+
(void)close(keep_low);
1096+
1097+
ASSERT_TRUE(closed);
1098+
ASSERT_TRUE(low_survived); /* only descriptors >= lowfd are touched */
1099+
cbm_fd_close_strategy_t expected = subprocess_expected_close_strategy();
1100+
if (inject_enosys && expected == CBM_FD_CLOSE_RANGE) {
1101+
expected = CBM_FD_CLOSE_LOOP; /* kernel "lacks" it: the fallback must run */
1102+
}
1103+
ASSERT_EQ((int)used, (int)expected);
1104+
}
1105+
PASS();
1106+
#endif
1107+
}
1108+
10151109
TEST(subprocess_root_exit_drains_surviving_descendant) {
10161110
#ifdef _WIN32
10171111
SKIP_PLATFORM("POSIX process-group descendant probe; native Windows coverage pending");
@@ -1396,6 +1490,7 @@ SUITE(subprocess) {
13961490
RUN_TEST(subprocess_poll_log_delivery_is_bounded_and_terminal_is_lossless);
13971491
RUN_TEST(subprocess_final_log_drain_error_is_terminal_and_preserves_classification);
13981492
RUN_TEST(subprocess_posix_child_closes_unrelated_descriptors);
1493+
RUN_TEST(subprocess_child_close_fds_uses_one_syscall_not_rlimit_loop);
13991494
RUN_TEST(subprocess_root_exit_drains_surviving_descendant);
14001495
RUN_TEST(subprocess_strip_git_repo_env_is_per_child);
14011496
RUN_TEST(popen_git_strips_repo_env_and_reports_exit_status);

0 commit comments

Comments
 (0)