]> granicus.if.org Git - sudo/commitdiff
Check for dup2() failure.
authorTodd C. Miller <Todd.Miller@courtesan.com>
Tue, 15 Jun 2010 13:05:19 +0000 (09:05 -0400)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Tue, 15 Jun 2010 13:05:19 +0000 (09:05 -0400)
--HG--
branch : 1.7

exec_pty.c
logging.c
selinux.c
sudo.c
tgetpass.c

index 0090394d4ceacef210983569cae62babe9554120..17b0fc62effee801ec4721b1f069f7210765cf08 100644 (file)
@@ -986,9 +986,10 @@ exec_pty(path, argv, envp, rbac_enabled)
     setpgid(0, self);
 
     /* Wire up standard fds, note that stdout/stderr may be pipes. */
-    dup2(io_fds[SFD_STDIN], STDIN_FILENO);
-    dup2(io_fds[SFD_STDOUT], STDOUT_FILENO);
-    dup2(io_fds[SFD_STDERR], STDERR_FILENO);
+    if (dup2(io_fds[SFD_STDIN], STDIN_FILENO) == -1 ||
+       dup2(io_fds[SFD_STDOUT], STDOUT_FILENO) == -1 ||
+       dup2(io_fds[SFD_STDERR], STDERR_FILENO) == -1)
+       error(1, "dup2");
 
     /* Wait for parent to grant us the tty if we are foreground. */
     if (foreground) {
index c772a1def0a0edcd28eb65d00991b22c31c42a44..ff225fb1e60a4895b00e8c4ce2793286f0020aef 100644 (file)
--- a/logging.c
+++ b/logging.c
@@ -490,7 +490,7 @@ send_mail(fmt, va_alist)
     }
 # endif
 #endif
-    chdir("/");
+    (void) chdir("/");
     if ((fd = open(_PATH_DEVNULL, O_RDWR, 0644)) != -1) {
        (void) dup2(fd, STDIN_FILENO);
        (void) dup2(fd, STDOUT_FILENO);
@@ -528,12 +528,15 @@ send_mail(fmt, va_alist)
 
                /* Child, set stdin to output side of the pipe */
                if (pfd[0] != STDIN_FILENO) {
-                   (void) dup2(pfd[0], STDIN_FILENO);
+                   if (dup2(pfd[0], STDIN_FILENO) != -1) {
+                       mysyslog(LOG_ERR, "cannot dup stdin: %m");
+                       _exit(127);
+                   }
                    (void) close(pfd[0]);
                }
                (void) close(pfd[1]);
 
-               /* Build up an argv based the mailer path and flags */
+               /* Build up an argv based on the mailer path and flags */
                mflags = estrdup(def_mailerflags);
                mpath = estrdup(def_mailerpath);
                if ((argv[0] = strrchr(mpath, ' ')))
index de3c43eeb096e8a0f2a2bb2c88f5fdf52d2920d0..8bbd6ebb9f3bdb300dc17e2338d539d3c4335073 100644 (file)
--- a/selinux.c
+++ b/selinux.c
@@ -114,6 +114,7 @@ relabel_tty(const char *ttyn, int ptyfd)
 {
     security_context_t tty_con = NULL;
     security_context_t new_tty_con = NULL;
+    int fd;
 
     se_state.ttyfd = ptyfd;
 
@@ -162,22 +163,26 @@ relabel_tty(const char *ttyn, int ptyfd)
            if (se_state.enforcing)
                goto bad;
        }
-       dup2(se_state.ttyfd, ptyfd);
+       if (dup2(se_state.ttyfd, ptyfd) == -1) {
+           warning("dup2");
+           goto bad;
+       }
     } else {
        /* Re-open tty to get new label and reset std{in,out,err} */
        close(se_state.ttyfd);
        se_state.ttyfd = open(ttyn, O_RDWR|O_NONBLOCK);
-       if (se_state.ttyfd == -1)
+       if (se_state.ttyfd == -1) {
            warning("unable to open %s", ttyn);
-       else
-           (void)fcntl(se_state.ttyfd, F_SETFL,
-               fcntl(se_state.ttyfd, F_GETFL, 0) & ~O_NONBLOCK);
-       if (isatty(STDIN_FILENO))
-           dup2(se_state.ttyfd, STDIN_FILENO);
-       if (isatty(STDOUT_FILENO))
-           dup2(se_state.ttyfd, STDOUT_FILENO);
-       if (isatty(STDERR_FILENO))
-           dup2(se_state.ttyfd, STDERR_FILENO);
+           goto bad;
+       }
+       (void)fcntl(se_state.ttyfd, F_SETFL,
+           fcntl(se_state.ttyfd, F_GETFL, 0) & ~O_NONBLOCK);
+       for (fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++) {
+           if (isatty(fd) && dup2(se_state.ttyfd, fd) == -1) {
+               warning("dup2");
+               goto bad;
+           }
+       }
     }
     /* Retain se_state.ttyfd so we can restore label when command finishes. */
     (void)fcntl(se_state.ttyfd, F_SETFD, FD_CLOEXEC);
diff --git a/sudo.c b/sudo.c
index c54f6a22daf0ac4b54bbffba008585322d5c547b..cbc8d0af3332f57ed2496b6469bc737b346693d5 100644 (file)
--- a/sudo.c
+++ b/sudo.c
@@ -1044,16 +1044,16 @@ initial_setup()
     miss[STDOUT_FILENO] = fcntl(STDOUT_FILENO, F_GETFL, 0) == -1;
     miss[STDERR_FILENO] = fcntl(STDERR_FILENO, F_GETFL, 0) == -1;
     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);
-           if (devnull > STDERR_FILENO)
-               close(devnull);
-       }
+       if ((devnull = open(_PATH_DEVNULL, O_RDWR, 0644)) == -1)
+           error(1, "unable to open %s", _PATH_DEVNULL);
+       if (miss[STDIN_FILENO] && dup2(devnull, STDIN_FILENO) == -1)
+           error(1, "dup2");
+       if (miss[STDOUT_FILENO] && dup2(devnull, STDOUT_FILENO) == -1)
+           error(1, "dup2");
+       if (miss[STDERR_FILENO] && dup2(devnull, STDERR_FILENO) == -1)
+           error(1, "dup2");
+       if (devnull > STDERR_FILENO)
+           close(devnull);
     }
 }
 
index f0d2a36d39ed02a2d4aaaa2b7978a2da79f0daeb..b8aa25dd9108dc0c0aa991cac31bbdcb473d13d1 100644 (file)
@@ -199,6 +199,10 @@ sudo_askpass(prompt)
 
     if (pid == 0) {
        /* child, point stdout to output side of the pipe and exec askpass */
+       if (dup2(pfd[1], STDOUT_FILENO) == -1) {
+           warning("dup2");
+           _exit(255);
+       }
        (void) dup2(pfd[1], STDOUT_FILENO);
        set_perms(PERM_FULL_USER);
        closefrom(STDERR_FILENO + 1);