Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 55 additions & 16 deletions src/unix/pty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,32 @@
/* http://www.gnu.org/software/gnulib/manual/html_node/forkpty.html */
#if defined(__linux__)
#include <pty.h>
#include <dirent.h>
#include <charconv>
#include <sys/resource.h>
#include <sys/syscall.h>
#if !defined(CLOSE_RANGE_CLOEXEC)
#if defined(__has_include)
#if __has_include(<linux/close_range.h>)
#include <linux/close_range.h>
#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 <util.h>
#elif defined(__FreeBSD__)
Expand Down Expand Up @@ -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
Expand All @@ -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<const linux_dirent64 *>(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);
}
}
Expand All @@ -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<rlim_t>(kMaxFallbackFd)) {
max_fd = static_cast<int>(rl.rlim_cur);
}
for (int fd = 3; fd < max_fd; fd++) {
SetCloseOnExec(fd);
}
}
#endif

Expand Down