From 794bc23c57250c50d6f13f5eaf1d9e577314c457 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Wed, 2 Dec 2026 04:45:41 +0000 Subject: [PATCH] fix: enable close_range on older Linux sysroots and fix fd fallback Use __NR_close_range and linux/close_range.h with ABI fallbacks so glibc 2.28 sysroots (including older 4.19 kernel headers) compile in close_range() without requiring newer glibc symbols. Fall back to close_range(..., 0) on EINVAL for Linux 5.9-5.10, and replace the fd loop with a bounded RLIMIT_NOFILE scan when /proc/self/fd is unavailable. --- src/unix/pty.cc | 71 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 16 deletions(-) diff --git a/src/unix/pty.cc b/src/unix/pty.cc index 2083367bd..ec9e561f7 100644 --- a/src/unix/pty.cc +++ b/src/unix/pty.cc @@ -37,8 +37,32 @@ /* http://www.gnu.org/software/gnulib/manual/html_node/forkpty.html */ #if defined(__linux__) #include -#include +#include +#include #include +#if !defined(CLOSE_RANGE_CLOEXEC) +#if defined(__has_include) +#if __has_include() +#include +#endif +#endif +#ifndef CLOSE_RANGE_CLOEXEC +#define CLOSE_RANGE_CLOEXEC (1U << 2) +#endif +#endif +#if !defined(__NR_close_range) +#if defined(SYS_close_range) +#define __NR_close_range SYS_close_range +#elif (defined(__x86_64__) && !defined(__ILP32__)) || \ + defined(__i386__) || \ + defined(__aarch64__) || \ + (defined(__arm__) && defined(__ARM_EABI__)) || \ + defined(__riscv) || \ + defined(__powerpc__) || \ + defined(__s390__) +#define __NR_close_range 436 +#endif +#endif #elif defined(__APPLE__) #include #elif defined(__FreeBSD__) @@ -119,7 +143,7 @@ struct linux_dirent64 { int64_t d_off; unsigned short d_reclen; unsigned char d_type; - char d_name[256]; + char d_name[1]; }; static int @@ -134,27 +158,37 @@ SetCloseOnExec(int fd) { /** * Close all file descriptors >= 3 to prevent FD leakage to child processes. - * Uses close_range() syscall on Linux 5.9+, falls back to /proc/self/fd iteration. + * Uses close_range() syscall on Linux 5.9+, falls back to /proc/self/fd + * iteration, and finally a bounded RLIMIT_NOFILE scan. */ static void pty_close_inherited_fds() { - // Try close_range() first (Linux 5.9+, glibc 2.34+) - #if defined(SYS_close_range) && defined(CLOSE_RANGE_CLOEXEC) - if (syscall(SYS_close_range, 3, ~0U, CLOSE_RANGE_CLOEXEC) == 0) { + #if defined(__NR_close_range) + if (syscall(__NR_close_range, 3U, ~0U, CLOSE_RANGE_CLOEXEC) == 0 || + (errno == EINVAL && syscall(__NR_close_range, 3U, ~0U, 0U) == 0)) { return; } #endif - int dir = open("/proc/self/fd", O_RDONLY | O_DIRECTORY | O_CLOEXEC); + int dir; + do { + dir = open("/proc/self/fd", O_RDONLY | O_DIRECTORY | O_CLOEXEC); + } while (dir == -1 && errno == EINTR); if (dir != -1) { alignas(linux_dirent64) char buf[4096]; long n; - while ((n = syscall(SYS_getdents64, dir, buf, sizeof(buf))) > 0) { + for (;;) { + n = syscall(SYS_getdents64, dir, buf, sizeof(buf)); + if (n == -1 && errno == EINTR) + continue; + if (n <= 0) + break; for (long offset = 0; offset < n;) { const linux_dirent64 *entry = reinterpret_cast(buf + offset); offset += entry->d_reclen; - int fd = atoi(entry->d_name); - if (fd > 2) + int fd = -1; + auto [ptr, ec] = std::from_chars(entry->d_name, buf + offset, fd); + if (ec == std::errc() && ptr < buf + offset && *ptr == '\0' && fd > 2 && fd != dir) SetCloseOnExec(fd); } } @@ -163,12 +197,17 @@ pty_close_inherited_fds() { return; } - int fd; - // Set the CLOEXEC flag on all open descriptors. Unconditionally try the first - // 16 file descriptors. After that, bail out after the first error. - for (fd = 3; ; fd++) - if (SetCloseOnExec(fd) && fd > 15) - break; + constexpr int kMaxFallbackFd = 65536; + int max_fd = kMaxFallbackFd; + struct rlimit rl; + if (getrlimit(RLIMIT_NOFILE, &rl) == 0 && + rl.rlim_cur != RLIM_INFINITY && + rl.rlim_cur < static_cast(kMaxFallbackFd)) { + max_fd = static_cast(rl.rlim_cur); + } + for (int fd = 3; fd < max_fd; fd++) { + SetCloseOnExec(fd); + } } #endif