From 7ad5f303441927c76e6f081de56ca75d17c29e25 Mon Sep 17 00:00:00 2001 From: obiwac Date: Tue, 30 Aug 2022 23:17:37 +0200 Subject: [PATCH] src: remap invalid file descriptors using `dup2` When checking for the validity of the stdio file descriptors (#875), ones which don't exist are intended to be remapped to /dev/null (and, if that doesn't work, we abort). This however doesn't work on all platforms and in all cases (e.g. /dev/null could already have been opened by the acting process and not actually be mapped to the expected file descriptor); instead, use the `dup2` syscall as a more robust solution (conforms to POSIX.1). --- src/node.cc | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/node.cc b/src/node.cc index 2891c18bb9aa9a..46dc50511f391f 100644 --- a/src/node.cc +++ b/src/node.cc @@ -607,12 +607,24 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) { // anything. for (auto& s : stdio) { const int fd = &s - stdio; - if (fstat(fd, &s.stat) == 0) continue; + if (fstat(fd, &s.stat) == 0) + continue; + // Anything but EBADF means something is seriously wrong. We don't // have to special-case EINTR, fstat() is not interruptible. - if (errno != EBADF) ABORT(); - if (fd != open("/dev/null", O_RDWR)) ABORT(); - if (fstat(fd, &s.stat) != 0) ABORT(); + if (errno != EBADF) + ABORT(); + + // If EBADF (file descriptor doesn't exist), open /dev/null and duplicate + // its file descriptor to the invalid file descriptor. Make sure *that* + // file descriptor is valid. + int null_fd = open("/dev/null", O_RDWR); + if (dup2(null_fd, fd) != 0) + ABORT(); + + if (fstat(fd, &s.stat) == 0) + continue; + ABORT(); } }