|
27 | 27 | #include <sys/time.h> |
28 | 28 | #include <sys/wait.h> |
29 | 29 | #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 |
30 | 36 | #else |
31 | 37 | #include <windows.h> |
32 | 38 | #include "../src/foundation/win_utf8.h" |
@@ -1012,6 +1018,94 @@ TEST(subprocess_posix_child_closes_unrelated_descriptors) { |
1012 | 1018 | #endif |
1013 | 1019 | } |
1014 | 1020 |
|
| 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 | + |
1015 | 1109 | TEST(subprocess_root_exit_drains_surviving_descendant) { |
1016 | 1110 | #ifdef _WIN32 |
1017 | 1111 | SKIP_PLATFORM("POSIX process-group descendant probe; native Windows coverage pending"); |
@@ -1396,6 +1490,7 @@ SUITE(subprocess) { |
1396 | 1490 | RUN_TEST(subprocess_poll_log_delivery_is_bounded_and_terminal_is_lossless); |
1397 | 1491 | RUN_TEST(subprocess_final_log_drain_error_is_terminal_and_preserves_classification); |
1398 | 1492 | RUN_TEST(subprocess_posix_child_closes_unrelated_descriptors); |
| 1493 | + RUN_TEST(subprocess_child_close_fds_uses_one_syscall_not_rlimit_loop); |
1399 | 1494 | RUN_TEST(subprocess_root_exit_drains_surviving_descendant); |
1400 | 1495 | RUN_TEST(subprocess_strip_git_repo_env_is_per_child); |
1401 | 1496 | RUN_TEST(popen_git_strips_repo_env_and_reports_exit_status); |
|
0 commit comments