From 4a0fa0cc7546e8e44e05697d8bebb9ef536477b0 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Tue, 31 May 2022 21:38:02 +0100 Subject: [PATCH] Crashlog: Do not try to read from other executables if /dev/null is missing --- src/os/macosx/crashlog_osx.cpp | 12 +++++++----- src/os/unix/crashlog_unix.cpp | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/os/macosx/crashlog_osx.cpp b/src/os/macosx/crashlog_osx.cpp index 31b8bc4b1a..9f3038e51b 100644 --- a/src/os/macosx/crashlog_osx.cpp +++ b/src/os/macosx/crashlog_osx.cpp @@ -50,6 +50,9 @@ #if !defined(WITHOUT_DBG_LLDB) static bool ExecReadStdout(const char *file, char *const *args, char *&buffer, const char *last) { + int null_fd = open("/dev/null", O_RDWR); + if (null_fd == -1) return false; + int pipefd[2]; if (pipe(pipefd) == -1) return false; @@ -62,11 +65,9 @@ static bool ExecReadStdout(const char *file, char *const *args, char *&buffer, c close(pipefd[0]); /* Close unused read end */ dup2(pipefd[1], STDOUT_FILENO); close(pipefd[1]); - int null_fd = open("/dev/null", O_RDWR); - if (null_fd != -1) { - dup2(null_fd, STDERR_FILENO); - dup2(null_fd, STDIN_FILENO); - } + dup2(null_fd, STDERR_FILENO); + dup2(null_fd, STDIN_FILENO); + close(null_fd); execvp(file, args); exit(42); @@ -74,6 +75,7 @@ static bool ExecReadStdout(const char *file, char *const *args, char *&buffer, c /* parent */ + close(null_fd); close(pipefd[1]); /* Close unused write end */ while (buffer < last) { diff --git a/src/os/unix/crashlog_unix.cpp b/src/os/unix/crashlog_unix.cpp index 69290539eb..5037bfa9c6 100644 --- a/src/os/unix/crashlog_unix.cpp +++ b/src/os/unix/crashlog_unix.cpp @@ -74,6 +74,9 @@ static void LogStacktraceSigSegvHandler(int sig) static bool ExecReadStdout(const char *file, char *const *args, char *&buffer, const char *last) { + int null_fd = open("/dev/null", O_RDWR); + if (null_fd == -1) return false; + int pipefd[2]; if (pipe(pipefd) == -1) return false; @@ -86,11 +89,9 @@ static bool ExecReadStdout(const char *file, char *const *args, char *&buffer, c close(pipefd[0]); /* Close unused read end */ dup2(pipefd[1], STDOUT_FILENO); close(pipefd[1]); - int null_fd = open("/dev/null", O_RDWR); - if (null_fd != -1) { - dup2(null_fd, STDERR_FILENO); - dup2(null_fd, STDIN_FILENO); - } + dup2(null_fd, STDERR_FILENO); + dup2(null_fd, STDIN_FILENO); + close(null_fd); execvp(file, args); exit(42); @@ -98,6 +99,7 @@ static bool ExecReadStdout(const char *file, char *const *args, char *&buffer, c /* parent */ + close(null_fd); close(pipefd[1]); /* Close unused write end */ while (buffer < last) {