Skip to content

Commit

Permalink
src: remap invalid file descriptors using dup2
Browse files Browse the repository at this point in the history
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).

Fixes: nodejs/help#2411
Refs: #875
  • Loading branch information
obiwac committed Aug 31, 2022
1 parent 63aba56 commit c905070
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/node.cc
Expand Up @@ -608,11 +608,27 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) {
for (auto& s : stdio) {
const int fd = &s - stdio;
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 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. POSIX doesn't guarantee the next file
// descriptor open(2) gives us is the lowest available number anymore in
// POSIX.1-2017, which is why dup2(2) is needed.
const int null_fd = open("/dev/null", O_RDWR);
if (null_fd != fd) {
if (dup2(null_fd, fd) < 0) {
close(null_fd);
ABORT();
}
close(null_fd);
}

if (fstat(fd, &s.stat) == 0) continue;
ABORT();
}
}

Expand Down

0 comments on commit c905070

Please sign in to comment.