]> granicus.if.org Git - sudo/commitdiff
Make sure stdin, stdout and stderr are open and dup them to /dev/null
authorTodd C. Miller <Todd.Miller@courtesan.com>
Fri, 10 Dec 2004 00:26:22 +0000 (00:26 +0000)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Fri, 10 Dec 2004 00:26:22 +0000 (00:26 +0000)
if not.

sudo.c

diff --git a/sudo.c b/sudo.c
index 8bebd53ffee0675c3650ba3af1db9c7a98531c60..9f569894a7f5b990473124c9ebd6b66ec67ef433 100644 (file)
--- a/sudo.c
+++ b/sudo.c
@@ -965,6 +965,7 @@ open_sudoers(sudoers, keepopen)
 static void
 initial_setup()
 {
+    int miss[3], devnull = -1;
 #if defined(RLIMIT_CORE) && !defined(SUDO_DEVEL)
     struct rlimit rl;
 
@@ -977,6 +978,23 @@ initial_setup()
     (void) setrlimit(RLIMIT_CORE, &rl);
 #endif /* RLIMIT_CORE && !SUDO_DEVEL */
 
+    /*
+     * stdin, stdout and stderr must be open; set them to /dev/null
+     * if they are closed and close all other fds.
+     */
+    miss[STDIN_FILENO] = fcntl(STDIN_FILENO, F_GETFL, 0) != 0;
+    miss[STDOUT_FILENO] = fcntl(STDOUT_FILENO, F_GETFL, 0) != 0;
+    miss[STDERR_FILENO] = fcntl(STDERR_FILENO, F_GETFL, 0) != 0;
+    if ((miss[STDIN_FILENO] || miss[STDOUT_FILENO] || miss[STDERR_FILENO])) {
+       if ((devnull = open(_PATH_DEVNULL, O_RDWR, 0644)) != -1) {
+           if (miss[STDIN_FILENO])
+               (void) dup2(devnull, STDIN_FILENO);
+           if (miss[STDOUT_FILENO])
+               (void) dup2(devnull, STDOUT_FILENO);
+           if (miss[STDERR_FILENO])
+               (void) dup2(devnull, STDERR_FILENO);
+       }
+    }
     closefrom(STDERR_FILENO + 1);
 }