Do not close error pipe or debug fd via closefrom() as we need them

to report an exec error should one occur.
This commit is contained in:
Todd C. Miller
2011-11-29 19:51:24 -05:00
parent 3b2998d554
commit a64f1eaea8
4 changed files with 43 additions and 9 deletions

View File

@@ -101,7 +101,7 @@ static struct io_buffer *iobufs;
static void flush_output(void);
static int exec_monitor(struct command_details *details, int backchannel);
static void exec_pty(struct command_details *detail);
static void exec_pty(struct command_details *detail, int *errfd);
static void sigwinch(int s);
static void sync_ttysize(int src, int dst);
static void deliver_signal(pid_t pid, int signo);
@@ -936,7 +936,7 @@ exec_monitor(struct command_details *details, int backchannel)
restore_signals();
/* setup tty and exec command */
exec_pty(details);
exec_pty(details, &errpipe[1]);
cstat.type = CMD_ERRNO;
cstat.val = errno;
if (write(errpipe[1], &cstat, sizeof(cstat)) == -1)
@@ -1137,7 +1137,7 @@ flush_output(void)
* Returns only if execve() fails.
*/
static void
exec_pty(struct command_details *details)
exec_pty(struct command_details *details, int *errfd)
{
pid_t self = getpid();
debug_decl(exec_pty, SUDO_DEBUG_EXEC);
@@ -1170,14 +1170,23 @@ exec_pty(struct command_details *details)
sudo_debug_execve(SUDO_DEBUG_INFO, details->command,
details->argv, details->envp);
if (details->closefrom >= 0)
closefrom(details->closefrom);
if (details->closefrom >= 0) {
int maxfd = details->closefrom;
dup2(*errfd, maxfd);
(void)fcntl(maxfd, F_SETFD, FD_CLOEXEC);
*errfd = maxfd++;
if (sudo_debug_fd_set(maxfd) != -1)
maxfd++;
closefrom(maxfd);
}
#ifdef HAVE_SELINUX
if (ISSET(details->flags, CD_RBAC_ENABLED))
selinux_execve(details->command, details->argv, details->envp);
else
#endif
my_execve(details->command, details->argv, details->envp);
sudo_debug_printf(SUDO_DEBUG_ERROR, "unable to exec %s: %s",
details->command, strerror(errno));
debug_return;
}
@@ -1190,7 +1199,7 @@ sync_ttysize(int src, int dst)
#ifdef TIOCGWINSZ
struct winsize wsize;
pid_t pgrp;
debug_decl(exec_pty, SUDO_DEBUG_EXEC);
debug_decl(sync_ttysize, SUDO_DEBUG_EXEC);
if (ioctl(src, TIOCGWINSZ, &wsize) == 0) {
ioctl(dst, TIOCSWINSZ, &wsize);