version 1.1.1.2, 2012/05/29 12:26:49
|
version 1.1.1.4, 2013/07/22 10:46:13
|
Line 1
|
Line 1
|
/* |
/* |
* Copyright (c) 2009-2011 Todd C. Miller <Todd.Miller@courtesan.com> | * Copyright (c) 2009-2013 Todd C. Miller <Todd.Miller@courtesan.com> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
Line 17
|
Line 17
|
#include <config.h> |
#include <config.h> |
|
|
#include <sys/types.h> |
#include <sys/types.h> |
#include <sys/param.h> |
|
#ifdef HAVE_SYS_SYSMACROS_H |
#ifdef HAVE_SYS_SYSMACROS_H |
# include <sys/sysmacros.h> |
# include <sys/sysmacros.h> |
#endif |
#endif |
Line 86 struct io_buffer {
|
Line 85 struct io_buffer {
|
int rfd; /* reader (producer) */ |
int rfd; /* reader (producer) */ |
int wfd; /* writer (consumer) */ |
int wfd; /* writer (consumer) */ |
bool (*action)(const char *buf, unsigned int len); |
bool (*action)(const char *buf, unsigned int len); |
char buf[16 * 1024]; | char buf[32 * 1024]; |
}; |
}; |
|
|
static char slavename[PATH_MAX]; |
static char slavename[PATH_MAX]; |
static bool foreground, pipeline, tty_initialized; |
static bool foreground, pipeline, tty_initialized; |
static int io_fds[6] = { -1, -1, -1, -1, -1, -1}; |
static int io_fds[6] = { -1, -1, -1, -1, -1, -1}; |
static int ttymode = TERM_COOKED; |
static int ttymode = TERM_COOKED; |
static pid_t ppgrp, child, child_pgrp; | static pid_t ppgrp, cmnd_pgrp, mon_pgrp; |
static sigset_t ttyblock; |
static sigset_t ttyblock; |
static struct io_buffer *iobufs; |
static struct io_buffer *iobufs; |
|
|
static void flush_output(void); |
static void flush_output(void); |
static int exec_monitor(struct command_details *details, int backchannel); |
static int exec_monitor(struct command_details *details, int backchannel); |
static void exec_pty(struct command_details *detail, int *errfd); | static void exec_pty(struct command_details *details, |
| struct command_status *cstat, int *errfd); |
static void sigwinch(int s); |
static void sigwinch(int s); |
static void sync_ttysize(int src, int dst); |
static void sync_ttysize(int src, int dst); |
static void deliver_signal(pid_t pid, int signo, bool from_parent); |
static void deliver_signal(pid_t pid, int signo, bool from_parent); |
Line 107 static int safe_close(int fd);
|
Line 107 static int safe_close(int fd);
|
static void check_foreground(void); |
static void check_foreground(void); |
|
|
/* |
/* |
* Cleanup hook for error()/errorx() | * Cleanup hook for fatal()/fatalx() |
*/ |
*/ |
void | static void |
cleanup(int gotsignal) | pty_cleanup(void) |
{ |
{ |
debug_decl(cleanup, SUDO_DEBUG_EXEC); |
debug_decl(cleanup, SUDO_DEBUG_EXEC); |
|
|
Line 128 cleanup(int gotsignal)
|
Line 128 cleanup(int gotsignal)
|
} |
} |
|
|
/* |
/* |
|
* Generic handler for signals recieved by the monitor process. |
|
* The other end of signal_pipe is checked in the monitor event loop. |
|
*/ |
|
#ifdef SA_SIGINFO |
|
void |
|
mon_handler(int s, siginfo_t *info, void *context) |
|
{ |
|
unsigned char signo = (unsigned char)s; |
|
|
|
/* |
|
* If the signal came from the command we ran, just ignore |
|
* it since we don't want the command to indirectly kill itself. |
|
* This can happen with, e.g. BSD-derived versions of reboot |
|
* that call kill(-1, SIGTERM) to kill all other processes. |
|
*/ |
|
if (info != NULL && info->si_code == SI_USER && info->si_pid == cmnd_pid) |
|
return; |
|
|
|
/* |
|
* The pipe is non-blocking, if we overflow the kernel's pipe |
|
* buffer we drop the signal. This is not a problem in practice. |
|
*/ |
|
ignore_result(write(signal_pipe[1], &signo, sizeof(signo))); |
|
} |
|
#else |
|
void |
|
mon_handler(int s) |
|
{ |
|
unsigned char signo = (unsigned char)s; |
|
|
|
/* |
|
* The pipe is non-blocking, if we overflow the kernel's pipe |
|
* buffer we drop the signal. This is not a problem in practice. |
|
*/ |
|
ignore_result(write(signal_pipe[1], &signo, sizeof(signo))); |
|
} |
|
#endif |
|
|
|
/* |
* Allocate a pty if /dev/tty is a tty. |
* Allocate a pty if /dev/tty is a tty. |
* Fills in io_fds[SFD_USERTTY], io_fds[SFD_MASTER], io_fds[SFD_SLAVE] |
* Fills in io_fds[SFD_USERTTY], io_fds[SFD_MASTER], io_fds[SFD_SLAVE] |
* and slavename globals. |
* and slavename globals. |
Line 141 pty_setup(uid_t uid, const char *tty, const char *utmp
|
Line 180 pty_setup(uid_t uid, const char *tty, const char *utmp
|
if (io_fds[SFD_USERTTY] != -1) { |
if (io_fds[SFD_USERTTY] != -1) { |
if (!get_pty(&io_fds[SFD_MASTER], &io_fds[SFD_SLAVE], |
if (!get_pty(&io_fds[SFD_MASTER], &io_fds[SFD_SLAVE], |
slavename, sizeof(slavename), uid)) |
slavename, sizeof(slavename), uid)) |
error(1, _("unable to allocate pty")); | fatal(_("unable to allocate pty")); |
/* Add entry to utmp/utmpx? */ |
/* Add entry to utmp/utmpx? */ |
if (utmp_user != NULL) |
if (utmp_user != NULL) |
utmp_login(tty, slavename, io_fds[SFD_SLAVE], utmp_user); |
utmp_login(tty, slavename, io_fds[SFD_SLAVE], utmp_user); |
Line 290 check_foreground(void)
|
Line 329 check_foreground(void)
|
|
|
/* |
/* |
* Suspend sudo if the underlying command is suspended. |
* Suspend sudo if the underlying command is suspended. |
* Returns SIGCONT_FG if the child should be resume in the | * Returns SIGCONT_FG if the command should be resumed in the |
* foreground or SIGCONT_BG if it is a background process. |
* foreground or SIGCONT_BG if it is a background process. |
*/ |
*/ |
int |
int |
suspend_parent(int signo) |
suspend_parent(int signo) |
{ |
{ |
|
char signame[SIG2STR_MAX]; |
sigaction_t sa, osa; |
sigaction_t sa, osa; |
int n, oldmode = ttymode, rval = 0; | int n, rval = 0; |
debug_decl(suspend_parent, SUDO_DEBUG_EXEC); |
debug_decl(suspend_parent, SUDO_DEBUG_EXEC); |
|
|
switch (signo) { |
switch (signo) { |
case SIGTTOU: |
case SIGTTOU: |
case SIGTTIN: |
case SIGTTIN: |
/* |
/* |
* If we are the foreground process, just resume the child. | * If sudo is already the foreground process, just resume the command |
* Otherwise, re-send the signal with the handler disabled. | * in the foreground. If not, we'll suspend sudo and resume later. |
*/ |
*/ |
if (!foreground) |
if (!foreground) |
check_foreground(); |
check_foreground(); |
Line 316 suspend_parent(int signo)
|
Line 356 suspend_parent(int signo)
|
} while (!n && errno == EINTR); |
} while (!n && errno == EINTR); |
ttymode = TERM_RAW; |
ttymode = TERM_RAW; |
} |
} |
rval = SIGCONT_FG; /* resume child in foreground */ | rval = SIGCONT_FG; /* resume command in foreground */ |
break; |
break; |
} |
} |
ttymode = TERM_RAW; |
|
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case SIGSTOP: |
case SIGSTOP: |
case SIGTSTP: |
case SIGTSTP: |
Line 327 suspend_parent(int signo)
|
Line 366 suspend_parent(int signo)
|
flush_output(); |
flush_output(); |
|
|
/* Restore original tty mode before suspending. */ |
/* Restore original tty mode before suspending. */ |
if (oldmode != TERM_COOKED) { | if (ttymode != TERM_COOKED) { |
do { |
do { |
n = term_restore(io_fds[SFD_USERTTY], 0); |
n = term_restore(io_fds[SFD_USERTTY], 0); |
} while (!n && errno == EINTR); |
} while (!n && errno == EINTR); |
} |
} |
|
|
/* Suspend self and continue child when we resume. */ | if (sig2str(signo, signame) == -1) |
zero_bytes(&sa, sizeof(sa)); | snprintf(signame, sizeof(signame), "%d", signo); |
sigemptyset(&sa.sa_mask); | |
sa.sa_flags = SA_INTERRUPT; /* do not restart syscalls */ | /* Suspend self and continue command when we resume. */ |
sa.sa_handler = SIG_DFL; | if (signo != SIGSTOP) { |
sigaction(signo, &sa, &osa); | memset(&sa, 0, sizeof(sa)); |
sudo_debug_printf(SUDO_DEBUG_INFO, "kill parent %d", signo); | sigemptyset(&sa.sa_mask); |
| sa.sa_flags = SA_RESTART; |
| sa.sa_handler = SIG_DFL; |
| sudo_sigaction(signo, &sa, &osa); |
| } |
| sudo_debug_printf(SUDO_DEBUG_INFO, "kill parent SIG%s", signame); |
if (killpg(ppgrp, signo) != 0) |
if (killpg(ppgrp, signo) != 0) |
warning("killpg(%d, %d)", (int)ppgrp, signo); | warning("killpg(%d, SIG%s)", (int)ppgrp, signame); |
|
|
/* Check foreground/background status on resume. */ |
/* Check foreground/background status on resume. */ |
check_foreground(); |
check_foreground(); |
|
|
/* |
/* |
* Only modify term if we are foreground process and either | * We always resume the command in the foreground if sudo itself |
* the old tty mode was not cooked or child got SIGTT{IN,OU} | * is the foreground process. This helps work around poorly behaved |
| * programs that catch SIGTTOU/SIGTTIN but suspend themselves with |
| * SIGSTOP. At worst, sudo will go into the background but upon |
| * resume the command will be runnable. Otherwise, we can get into |
| * a situation where the command will immediately suspend itself. |
*/ |
*/ |
sudo_debug_printf(SUDO_DEBUG_INFO, "parent is in %s, ttymode %d -> %d", |
sudo_debug_printf(SUDO_DEBUG_INFO, "parent is in %s, ttymode %d -> %d", |
foreground ? "foreground" : "background", oldmode, ttymode); | foreground ? "foreground" : "background", ttymode, |
| foreground ? TERM_RAW : TERM_COOKED); |
|
|
if (ttymode != TERM_COOKED) { | if (foreground) { |
if (foreground) { | /* Foreground process, set tty to raw mode. */ |
/* Set raw mode. */ | do { |
do { | n = term_raw(io_fds[SFD_USERTTY], 0); |
n = term_raw(io_fds[SFD_USERTTY], 0); | } while (!n && errno == EINTR); |
} while (!n && errno == EINTR); | ttymode = TERM_RAW; |
} else { | } else { |
/* Background process, no access to tty. */ | /* Background process, no access to tty. */ |
ttymode = TERM_COOKED; | ttymode = TERM_COOKED; |
} | |
} |
} |
|
|
sigaction(signo, &osa, NULL); | if (signo != SIGSTOP) |
| sudo_sigaction(signo, &osa, NULL); |
rval = ttymode == TERM_RAW ? SIGCONT_FG : SIGCONT_BG; |
rval = ttymode == TERM_RAW ? SIGCONT_FG : SIGCONT_BG; |
break; |
break; |
} |
} |
Line 374 suspend_parent(int signo)
|
Line 423 suspend_parent(int signo)
|
} |
} |
|
|
/* |
/* |
* Kill child with increasing urgency. | * Kill command with increasing urgency. |
*/ |
*/ |
void |
void |
terminate_child(pid_t pid, bool use_pgrp) | terminate_command(pid_t pid, bool use_pgrp) |
{ |
{ |
debug_decl(terminate_child, SUDO_DEBUG_EXEC); | debug_decl(terminate_command, SUDO_DEBUG_EXEC); |
|
|
/* |
/* |
* Note that SIGCHLD will interrupt the sleep() |
* Note that SIGCHLD will interrupt the sleep() |
Line 460 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
Line 509 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
sudo_debug_printf(SUDO_DEBUG_INFO, |
sudo_debug_printf(SUDO_DEBUG_INFO, |
"read %d bytes from fd %d", n, iob->rfd); |
"read %d bytes from fd %d", n, iob->rfd); |
if (!iob->action(iob->buf + iob->len, n)) |
if (!iob->action(iob->buf + iob->len, n)) |
terminate_child(child, true); | terminate_command(cmnd_pid, true); |
iob->len += n; |
iob->len += n; |
break; |
break; |
} |
} |
Line 509 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
Line 558 perform_io(fd_set *fdsr, fd_set *fdsw, struct command_
|
* Returns the child pid. |
* Returns the child pid. |
*/ |
*/ |
int |
int |
fork_pty(struct command_details *details, int sv[], int *maxfd) | fork_pty(struct command_details *details, int sv[], int *maxfd, sigset_t *omask) |
{ |
{ |
struct command_status cstat; |
struct command_status cstat; |
struct io_buffer *iob; |
struct io_buffer *iob; |
int io_pipe[3][2], n; |
int io_pipe[3][2], n; |
sigaction_t sa; |
sigaction_t sa; |
|
sigset_t mask; |
|
pid_t child; |
debug_decl(fork_pty, SUDO_DEBUG_EXEC); |
debug_decl(fork_pty, SUDO_DEBUG_EXEC); |
| |
ppgrp = getpgrp(); /* parent's pgrp, so child can signal us */ |
ppgrp = getpgrp(); /* parent's pgrp, so child can signal us */ |
| |
zero_bytes(&sa, sizeof(sa)); | memset(&sa, 0, sizeof(sa)); |
sigemptyset(&sa.sa_mask); |
sigemptyset(&sa.sa_mask); |
| |
if (io_fds[SFD_USERTTY] != -1) { |
if (io_fds[SFD_USERTTY] != -1) { |
sa.sa_flags = SA_RESTART; |
sa.sa_flags = SA_RESTART; |
sa.sa_handler = sigwinch; |
sa.sa_handler = sigwinch; |
sigaction(SIGWINCH, &sa, NULL); | sudo_sigaction(SIGWINCH, &sa, NULL); |
} |
} |
|
|
/* So we can block tty-generated signals */ |
/* So we can block tty-generated signals */ |
Line 569 fork_pty(struct command_details *details, int sv[], in
|
Line 620 fork_pty(struct command_details *details, int sv[], in
|
sudo_debug_printf(SUDO_DEBUG_INFO, "stdin not a tty, creating a pipe"); |
sudo_debug_printf(SUDO_DEBUG_INFO, "stdin not a tty, creating a pipe"); |
pipeline = true; |
pipeline = true; |
if (pipe(io_pipe[STDIN_FILENO]) != 0) |
if (pipe(io_pipe[STDIN_FILENO]) != 0) |
error(1, _("unable to create pipe")); | fatal(_("unable to create pipe")); |
iobufs = io_buf_new(STDIN_FILENO, io_pipe[STDIN_FILENO][1], |
iobufs = io_buf_new(STDIN_FILENO, io_pipe[STDIN_FILENO][1], |
log_stdin, iobufs); |
log_stdin, iobufs); |
io_fds[SFD_STDIN] = io_pipe[STDIN_FILENO][0]; |
io_fds[SFD_STDIN] = io_pipe[STDIN_FILENO][0]; |
Line 578 fork_pty(struct command_details *details, int sv[], in
|
Line 629 fork_pty(struct command_details *details, int sv[], in
|
sudo_debug_printf(SUDO_DEBUG_INFO, "stdout not a tty, creating a pipe"); |
sudo_debug_printf(SUDO_DEBUG_INFO, "stdout not a tty, creating a pipe"); |
pipeline = true; |
pipeline = true; |
if (pipe(io_pipe[STDOUT_FILENO]) != 0) |
if (pipe(io_pipe[STDOUT_FILENO]) != 0) |
error(1, _("unable to create pipe")); | fatal(_("unable to create pipe")); |
iobufs = io_buf_new(io_pipe[STDOUT_FILENO][0], STDOUT_FILENO, |
iobufs = io_buf_new(io_pipe[STDOUT_FILENO][0], STDOUT_FILENO, |
log_stdout, iobufs); |
log_stdout, iobufs); |
io_fds[SFD_STDOUT] = io_pipe[STDOUT_FILENO][1]; |
io_fds[SFD_STDOUT] = io_pipe[STDOUT_FILENO][1]; |
Line 586 fork_pty(struct command_details *details, int sv[], in
|
Line 637 fork_pty(struct command_details *details, int sv[], in
|
if (io_fds[SFD_STDERR] == -1 || !isatty(STDERR_FILENO)) { |
if (io_fds[SFD_STDERR] == -1 || !isatty(STDERR_FILENO)) { |
sudo_debug_printf(SUDO_DEBUG_INFO, "stderr not a tty, creating a pipe"); |
sudo_debug_printf(SUDO_DEBUG_INFO, "stderr not a tty, creating a pipe"); |
if (pipe(io_pipe[STDERR_FILENO]) != 0) |
if (pipe(io_pipe[STDERR_FILENO]) != 0) |
error(1, _("unable to create pipe")); | fatal(_("unable to create pipe")); |
iobufs = io_buf_new(io_pipe[STDERR_FILENO][0], STDERR_FILENO, |
iobufs = io_buf_new(io_pipe[STDERR_FILENO][0], STDERR_FILENO, |
log_stderr, iobufs); |
log_stderr, iobufs); |
io_fds[SFD_STDERR] = io_pipe[STDERR_FILENO][1]; |
io_fds[SFD_STDERR] = io_pipe[STDERR_FILENO][1]; |
} |
} |
|
|
|
/* We don't want to receive SIGTTIN/SIGTTOU, getting EIO is preferable. */ |
|
sa.sa_handler = SIG_IGN; |
|
sudo_sigaction(SIGTTIN, &sa, NULL); |
|
sudo_sigaction(SIGTTOU, &sa, NULL); |
|
|
/* Job control signals to relay from parent to child. */ |
/* Job control signals to relay from parent to child. */ |
|
sigfillset(&sa.sa_mask); |
sa.sa_flags = SA_INTERRUPT; /* do not restart syscalls */ |
sa.sa_flags = SA_INTERRUPT; /* do not restart syscalls */ |
|
#ifdef SA_SIGINFO |
|
sa.sa_flags |= SA_SIGINFO; |
|
sa.sa_sigaction = handler; |
|
#else |
sa.sa_handler = handler; |
sa.sa_handler = handler; |
sigaction(SIGTSTP, &sa, NULL); | #endif |
| sudo_sigaction(SIGTSTP, &sa, NULL); |
|
|
/* We don't want to receive SIGTTIN/SIGTTOU, getting EIO is preferable. */ |
|
sa.sa_handler = SIG_IGN; |
|
sigaction(SIGTTIN, &sa, NULL); |
|
sigaction(SIGTTOU, &sa, NULL); |
|
|
|
if (foreground) { |
if (foreground) { |
/* Copy terminal attrs from user tty -> pty slave. */ |
/* Copy terminal attrs from user tty -> pty slave. */ |
if (term_copy(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE])) { |
if (term_copy(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE])) { |
Line 609 fork_pty(struct command_details *details, int sv[], in
|
Line 666 fork_pty(struct command_details *details, int sv[], in
|
sync_ttysize(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE]); |
sync_ttysize(io_fds[SFD_USERTTY], io_fds[SFD_SLAVE]); |
} |
} |
|
|
/* Start out in raw mode if we are not part of a pipeline. */ | /* Start out in raw mode unless part of a pipeline or backgrounded. */ |
if (!pipeline) { | if (!pipeline && !ISSET(details->flags, CD_EXEC_BG)) { |
ttymode = TERM_RAW; |
ttymode = TERM_RAW; |
do { |
do { |
n = term_raw(io_fds[SFD_USERTTY], 0); |
n = term_raw(io_fds[SFD_USERTTY], 0); |
} while (!n && errno == EINTR); |
} while (!n && errno == EINTR); |
if (!n) |
if (!n) |
error(1, _("unable to set terminal to raw mode")); | fatal(_("unable to set terminal to raw mode")); |
} |
} |
} |
} |
|
|
Line 625 fork_pty(struct command_details *details, int sv[], in
|
Line 682 fork_pty(struct command_details *details, int sv[], in
|
* or certain pam modules won't be able to track their state. |
* or certain pam modules won't be able to track their state. |
*/ |
*/ |
if (policy_init_session(details) != true) |
if (policy_init_session(details) != true) |
errorx(1, _("policy plugin failed session initialization")); | fatalx(_("policy plugin failed session initialization")); |
|
|
|
/* |
|
* Block some signals until cmnd_pid is set in the parent to avoid a |
|
* race between exec of the command and receipt of a fatal signal from it. |
|
*/ |
|
sigemptyset(&mask); |
|
sigaddset(&mask, SIGTERM); |
|
sigaddset(&mask, SIGHUP); |
|
sigaddset(&mask, SIGINT); |
|
sigaddset(&mask, SIGQUIT); |
|
sigprocmask(SIG_BLOCK, &mask, omask); |
|
|
child = sudo_debug_fork(); |
child = sudo_debug_fork(); |
switch (child) { |
switch (child) { |
case -1: |
case -1: |
error(1, _("unable to fork")); | fatal(_("unable to fork")); |
break; |
break; |
case 0: |
case 0: |
/* child */ |
/* child */ |
Line 638 fork_pty(struct command_details *details, int sv[], in
|
Line 706 fork_pty(struct command_details *details, int sv[], in
|
close(signal_pipe[0]); |
close(signal_pipe[0]); |
close(signal_pipe[1]); |
close(signal_pipe[1]); |
fcntl(sv[1], F_SETFD, FD_CLOEXEC); |
fcntl(sv[1], F_SETFD, FD_CLOEXEC); |
if (exec_setup(details, slavename, io_fds[SFD_SLAVE]) == true) { | sigprocmask(SIG_SETMASK, omask, NULL); |
/* Close the other end of the stdin/stdout/stderr pipes and exec. */ | /* Close the other end of the stdin/stdout/stderr pipes and exec. */ |
if (io_pipe[STDIN_FILENO][1]) | if (io_pipe[STDIN_FILENO][1]) |
close(io_pipe[STDIN_FILENO][1]); | close(io_pipe[STDIN_FILENO][1]); |
if (io_pipe[STDOUT_FILENO][0]) | if (io_pipe[STDOUT_FILENO][0]) |
close(io_pipe[STDOUT_FILENO][0]); | close(io_pipe[STDOUT_FILENO][0]); |
if (io_pipe[STDERR_FILENO][0]) | if (io_pipe[STDERR_FILENO][0]) |
close(io_pipe[STDERR_FILENO][0]); | close(io_pipe[STDERR_FILENO][0]); |
exec_monitor(details, sv[1]); | exec_monitor(details, sv[1]); |
} | |
cstat.type = CMD_ERRNO; |
cstat.type = CMD_ERRNO; |
cstat.val = errno; |
cstat.val = errno; |
send(sv[1], &cstat, sizeof(cstat), 0); | ignore_result(send(sv[1], &cstat, sizeof(cstat), 0)); |
_exit(1); |
_exit(1); |
} |
} |
|
|
Line 659 fork_pty(struct command_details *details, int sv[], in
|
Line 726 fork_pty(struct command_details *details, int sv[], in
|
close(io_pipe[STDIN_FILENO][0]); |
close(io_pipe[STDIN_FILENO][0]); |
if (io_pipe[STDOUT_FILENO][1]) |
if (io_pipe[STDOUT_FILENO][1]) |
close(io_pipe[STDOUT_FILENO][1]); |
close(io_pipe[STDOUT_FILENO][1]); |
if (io_pipe[STDERR_FILENO][1]) | if (io_pipe[STDERR_FILENO][1]) |
close(io_pipe[STDERR_FILENO][1]); |
close(io_pipe[STDERR_FILENO][1]); |
|
|
for (iob = iobufs; iob; iob = iob->next) { |
for (iob = iobufs; iob; iob = iob->next) { |
Line 764 fd_set_iobs(fd_set *fdsr, fd_set *fdsw)
|
Line 831 fd_set_iobs(fd_set *fdsr, fd_set *fdsw)
|
static void |
static void |
deliver_signal(pid_t pid, int signo, bool from_parent) |
deliver_signal(pid_t pid, int signo, bool from_parent) |
{ |
{ |
|
char signame[SIG2STR_MAX]; |
int status; |
int status; |
debug_decl(deliver_signal, SUDO_DEBUG_EXEC); |
debug_decl(deliver_signal, SUDO_DEBUG_EXEC); |
|
|
|
if (signo == SIGCONT_FG) |
|
strlcpy(signame, "CONT_FG", sizeof(signame)); |
|
else if (signo == SIGCONT_BG) |
|
strlcpy(signame, "CONT_BG", sizeof(signame)); |
|
else if (sig2str(signo, signame) == -1) |
|
snprintf(signame, sizeof(signame), "%d", signo); |
|
|
/* Handle signal from parent. */ |
/* Handle signal from parent. */ |
sudo_debug_printf(SUDO_DEBUG_INFO, "received signal %d%s", signo, | sudo_debug_printf(SUDO_DEBUG_INFO, "received SIG%s%s", |
from_parent ? " from parent" : ""); | signame, from_parent ? " from parent" : ""); |
switch (signo) { |
switch (signo) { |
case SIGALRM: |
case SIGALRM: |
terminate_child(pid, true); | terminate_command(pid, true); |
break; |
break; |
case SIGCONT_FG: |
case SIGCONT_FG: |
/* Continue in foreground, grant it controlling tty. */ |
/* Continue in foreground, grant it controlling tty. */ |
do { |
do { |
status = tcsetpgrp(io_fds[SFD_SLAVE], child_pgrp); | status = tcsetpgrp(io_fds[SFD_SLAVE], cmnd_pgrp); |
} while (status == -1 && errno == EINTR); |
} while (status == -1 && errno == EINTR); |
killpg(pid, SIGCONT); |
killpg(pid, SIGCONT); |
break; |
break; |
case SIGCONT_BG: |
case SIGCONT_BG: |
/* Continue in background, I take controlling tty. */ |
/* Continue in background, I take controlling tty. */ |
do { |
do { |
status = tcsetpgrp(io_fds[SFD_SLAVE], getpid()); | status = tcsetpgrp(io_fds[SFD_SLAVE], mon_pgrp); |
} while (status == -1 && errno == EINTR); |
} while (status == -1 && errno == EINTR); |
killpg(pid, SIGCONT); |
killpg(pid, SIGCONT); |
break; |
break; |
Line 792 deliver_signal(pid_t pid, int signo, bool from_parent)
|
Line 867 deliver_signal(pid_t pid, int signo, bool from_parent)
|
_exit(1); /* XXX */ |
_exit(1); /* XXX */ |
/* NOTREACHED */ |
/* NOTREACHED */ |
default: |
default: |
/* Relay signal to child. */ | /* Relay signal to command. */ |
killpg(pid, signo); |
killpg(pid, signo); |
break; |
break; |
} |
} |
Line 826 send_status(int fd, struct command_status *cstat)
|
Line 901 send_status(int fd, struct command_status *cstat)
|
} |
} |
|
|
/* |
/* |
* Wait for child status after receiving SIGCHLD. | * Wait for command status after receiving SIGCHLD. |
* If the child was stopped, the status is send back to the parent. | * If the command was stopped, the status is send back to the parent. |
* Otherwise, cstat is filled in but not sent. |
* Otherwise, cstat is filled in but not sent. |
* Returns true if child is still alive, else false. | * Returns true if command is still alive, else false. |
*/ |
*/ |
static bool |
static bool |
handle_sigchld(int backchannel, struct command_status *cstat) |
handle_sigchld(int backchannel, struct command_status *cstat) |
Line 839 handle_sigchld(int backchannel, struct command_status
|
Line 914 handle_sigchld(int backchannel, struct command_status
|
pid_t pid; |
pid_t pid; |
debug_decl(handle_sigchld, SUDO_DEBUG_EXEC); |
debug_decl(handle_sigchld, SUDO_DEBUG_EXEC); |
|
|
/* read child status */ | /* read command status */ |
do { |
do { |
pid = waitpid(child, &status, WUNTRACED|WNOHANG); | pid = waitpid(cmnd_pid, &status, WUNTRACED|WNOHANG); |
} while (pid == -1 && errno == EINTR); |
} while (pid == -1 && errno == EINTR); |
if (pid == child) { | if (pid == cmnd_pid) { |
if (cstat->type != CMD_ERRNO) { |
if (cstat->type != CMD_ERRNO) { |
|
char signame[SIG2STR_MAX]; |
|
|
cstat->type = CMD_WSTATUS; |
cstat->type = CMD_WSTATUS; |
cstat->val = status; |
cstat->val = status; |
if (WIFSTOPPED(status)) { |
if (WIFSTOPPED(status)) { |
sudo_debug_printf(SUDO_DEBUG_INFO, "command stopped, signal %d", | if (sig2str(WSTOPSIG(status), signame) == -1) |
WSTOPSIG(status)); | snprintf(signame, sizeof(signame), "%d", WSTOPSIG(status)); |
| sudo_debug_printf(SUDO_DEBUG_INFO, |
| "command stopped, SIG%s", signame); |
| /* Saved the foreground pgid so we can restore it later. */ |
do { |
do { |
child_pgrp = tcgetpgrp(io_fds[SFD_SLAVE]); | pid = tcgetpgrp(io_fds[SFD_SLAVE]); |
} while (child_pgrp == -1 && errno == EINTR); | } while (pid == -1 && errno == EINTR); |
| if (pid != mon_pgrp) |
| cmnd_pgrp = pid; |
if (send_status(backchannel, cstat) == -1) |
if (send_status(backchannel, cstat) == -1) |
return alive; /* XXX */ |
return alive; /* XXX */ |
} else if (WIFSIGNALED(status)) { |
} else if (WIFSIGNALED(status)) { |
sudo_debug_printf(SUDO_DEBUG_INFO, "command killed, signal %d", | if (sig2str(WTERMSIG(status), signame) == -1) |
WTERMSIG(status)); | snprintf(signame, sizeof(signame), "%d", WTERMSIG(status)); |
| sudo_debug_printf(SUDO_DEBUG_INFO, |
| "command killed, SIG%s", signame); |
} else { |
} else { |
sudo_debug_printf(SUDO_DEBUG_INFO, "command exited: %d", |
sudo_debug_printf(SUDO_DEBUG_INFO, "command exited: %d", |
WEXITSTATUS(status)); |
WEXITSTATUS(status)); |
Line 883 exec_monitor(struct command_details *details, int back
|
Line 967 exec_monitor(struct command_details *details, int back
|
struct timeval tv; |
struct timeval tv; |
fd_set *fdsr; |
fd_set *fdsr; |
sigaction_t sa; |
sigaction_t sa; |
int errpipe[2], maxfd, n, status; | int errpipe[2], maxfd, n; |
bool alive = true; |
bool alive = true; |
unsigned char signo; |
unsigned char signo; |
debug_decl(exec_monitor, SUDO_DEBUG_EXEC); |
debug_decl(exec_monitor, SUDO_DEBUG_EXEC); |
Line 899 exec_monitor(struct command_details *details, int back
|
Line 983 exec_monitor(struct command_details *details, int back
|
* the select() loop. |
* the select() loop. |
*/ |
*/ |
if (pipe_nonblock(signal_pipe) != 0) |
if (pipe_nonblock(signal_pipe) != 0) |
error(1, _("unable to create pipe")); | fatal(_("unable to create pipe")); |
|
|
/* Reset SIGWINCH and SIGALRM. */ |
/* Reset SIGWINCH and SIGALRM. */ |
zero_bytes(&sa, sizeof(sa)); | memset(&sa, 0, sizeof(sa)); |
sigemptyset(&sa.sa_mask); |
sigemptyset(&sa.sa_mask); |
sa.sa_flags = SA_RESTART; |
sa.sa_flags = SA_RESTART; |
sa.sa_handler = SIG_DFL; |
sa.sa_handler = SIG_DFL; |
sigaction(SIGWINCH, &sa, NULL); | sudo_sigaction(SIGWINCH, &sa, NULL); |
sigaction(SIGALRM, &sa, NULL); | sudo_sigaction(SIGALRM, &sa, NULL); |
|
|
/* Ignore any SIGTTIN or SIGTTOU we get. */ |
/* Ignore any SIGTTIN or SIGTTOU we get. */ |
sa.sa_handler = SIG_IGN; |
sa.sa_handler = SIG_IGN; |
sigaction(SIGTTIN, &sa, NULL); | sudo_sigaction(SIGTTIN, &sa, NULL); |
sigaction(SIGTTOU, &sa, NULL); | sudo_sigaction(SIGTTOU, &sa, NULL); |
|
|
|
/* Block all signals in mon_handler(). */ |
|
sigfillset(&sa.sa_mask); |
|
|
/* Note: HP-UX select() will not be interrupted if SA_RESTART set */ |
/* Note: HP-UX select() will not be interrupted if SA_RESTART set */ |
sa.sa_flags = SA_INTERRUPT; |
sa.sa_flags = SA_INTERRUPT; |
sa.sa_handler = handler; | #ifdef SA_SIGINFO |
sigaction(SIGCHLD, &sa, NULL); | sa.sa_flags |= SA_SIGINFO; |
| sa.sa_sigaction = mon_handler; |
| #else |
| sa.sa_handler = mon_handler; |
| #endif |
| sudo_sigaction(SIGCHLD, &sa, NULL); |
|
|
/* Catch common signals so we can cleanup properly. */ |
/* Catch common signals so we can cleanup properly. */ |
sa.sa_flags = SA_RESTART; |
sa.sa_flags = SA_RESTART; |
sa.sa_handler = handler; | #ifdef SA_SIGINFO |
sigaction(SIGHUP, &sa, NULL); | sa.sa_flags |= SA_SIGINFO; |
sigaction(SIGINT, &sa, NULL); | sa.sa_sigaction = mon_handler; |
sigaction(SIGQUIT, &sa, NULL); | #else |
sigaction(SIGTERM, &sa, NULL); | sa.sa_handler = mon_handler; |
sigaction(SIGTSTP, &sa, NULL); | #endif |
sigaction(SIGUSR1, &sa, NULL); | sudo_sigaction(SIGHUP, &sa, NULL); |
sigaction(SIGUSR2, &sa, NULL); | sudo_sigaction(SIGINT, &sa, NULL); |
| sudo_sigaction(SIGQUIT, &sa, NULL); |
| sudo_sigaction(SIGTERM, &sa, NULL); |
| sudo_sigaction(SIGTSTP, &sa, NULL); |
| sudo_sigaction(SIGUSR1, &sa, NULL); |
| sudo_sigaction(SIGUSR2, &sa, NULL); |
|
|
/* |
/* |
* Start a new session with the parent as the session leader |
* Start a new session with the parent as the session leader |
* and the slave pty as the controlling terminal. |
* and the slave pty as the controlling terminal. |
* This allows us to be notified when the child has been suspended. | * This allows us to be notified when the command has been suspended. |
*/ |
*/ |
if (setsid() == -1) { |
if (setsid() == -1) { |
warning("setsid"); |
warning("setsid"); |
Line 942 exec_monitor(struct command_details *details, int back
|
Line 1039 exec_monitor(struct command_details *details, int back
|
if (io_fds[SFD_SLAVE] != -1) { |
if (io_fds[SFD_SLAVE] != -1) { |
#ifdef TIOCSCTTY |
#ifdef TIOCSCTTY |
if (ioctl(io_fds[SFD_SLAVE], TIOCSCTTY, NULL) != 0) |
if (ioctl(io_fds[SFD_SLAVE], TIOCSCTTY, NULL) != 0) |
error(1, _("unable to set controlling tty")); | fatal(_("unable to set controlling tty")); |
#else |
#else |
/* Set controlling tty by reopening slave. */ |
/* Set controlling tty by reopening slave. */ |
if ((n = open(slavename, O_RDWR)) >= 0) |
if ((n = open(slavename, O_RDWR)) >= 0) |
Line 950 exec_monitor(struct command_details *details, int back
|
Line 1047 exec_monitor(struct command_details *details, int back
|
#endif |
#endif |
} |
} |
|
|
|
mon_pgrp = getpgrp(); /* save a copy of our process group */ |
|
|
/* |
/* |
* If stdin/stdout is not a tty, start command in the background |
* If stdin/stdout is not a tty, start command in the background |
* since it might be part of a pipeline that reads from /dev/tty. |
* since it might be part of a pipeline that reads from /dev/tty. |
Line 961 exec_monitor(struct command_details *details, int back
|
Line 1060 exec_monitor(struct command_details *details, int back
|
|
|
/* Start command and wait for it to stop or exit */ |
/* Start command and wait for it to stop or exit */ |
if (pipe(errpipe) == -1) |
if (pipe(errpipe) == -1) |
error(1, _("unable to create pipe")); | fatal(_("unable to create pipe")); |
child = sudo_debug_fork(); | cmnd_pid = sudo_debug_fork(); |
if (child == -1) { | if (cmnd_pid == -1) { |
warning(_("unable to fork")); |
warning(_("unable to fork")); |
goto bad; |
goto bad; |
} |
} |
if (child == 0) { | if (cmnd_pid == 0) { |
/* We pass errno back to our parent via pipe on exec failure. */ |
/* We pass errno back to our parent via pipe on exec failure. */ |
close(backchannel); |
close(backchannel); |
close(signal_pipe[0]); |
close(signal_pipe[0]); |
Line 977 exec_monitor(struct command_details *details, int back
|
Line 1076 exec_monitor(struct command_details *details, int back
|
restore_signals(); |
restore_signals(); |
|
|
/* setup tty and exec command */ |
/* setup tty and exec command */ |
exec_pty(details, &errpipe[1]); | exec_pty(details, &cstat, &errpipe[1]); |
cstat.type = CMD_ERRNO; | |
cstat.val = errno; | |
ignore_result(write(errpipe[1], &cstat, sizeof(cstat))); |
ignore_result(write(errpipe[1], &cstat, sizeof(cstat))); |
_exit(1); |
_exit(1); |
} |
} |
close(errpipe[1]); |
close(errpipe[1]); |
|
|
|
/* Send the command's pid to main sudo process. */ |
|
cstat.type = CMD_PID; |
|
cstat.val = cmnd_pid; |
|
ignore_result(send(backchannel, &cstat, sizeof(cstat), 0)); |
|
|
/* If any of stdin/stdout/stderr are pipes, close them in parent. */ |
/* If any of stdin/stdout/stderr are pipes, close them in parent. */ |
if (io_fds[SFD_STDIN] != io_fds[SFD_SLAVE]) |
if (io_fds[SFD_STDIN] != io_fds[SFD_SLAVE]) |
close(io_fds[SFD_STDIN]); |
close(io_fds[SFD_STDIN]); |
Line 993 exec_monitor(struct command_details *details, int back
|
Line 1095 exec_monitor(struct command_details *details, int back
|
if (io_fds[SFD_STDERR] != io_fds[SFD_SLAVE]) |
if (io_fds[SFD_STDERR] != io_fds[SFD_SLAVE]) |
close(io_fds[SFD_STDERR]); |
close(io_fds[SFD_STDERR]); |
|
|
/* | /* Put command in its own process group. */ |
* Put child in its own process group. If we are starting the command | cmnd_pgrp = cmnd_pid; |
* in the foreground, assign its pgrp to the tty. | setpgid(cmnd_pid, cmnd_pgrp); |
*/ | |
child_pgrp = child; | /* Make the command the foreground process for the pty slave. */ |
setpgid(child, child_pgrp); | if (foreground && !ISSET(details->flags, CD_EXEC_BG)) { |
if (foreground) { | |
do { |
do { |
status = tcsetpgrp(io_fds[SFD_SLAVE], child_pgrp); | n = tcsetpgrp(io_fds[SFD_SLAVE], cmnd_pgrp); |
} while (status == -1 && errno == EINTR); | } while (n == -1 && errno == EINTR); |
} |
} |
|
|
/* Wait for errno on pipe, signal on backchannel or for SIGCHLD */ |
/* Wait for errno on pipe, signal on backchannel or for SIGCHLD */ |
Line 1040 exec_monitor(struct command_details *details, int back
|
Line 1141 exec_monitor(struct command_details *details, int back
|
} |
} |
/* |
/* |
* Handle SIGCHLD specially and deliver other signals |
* Handle SIGCHLD specially and deliver other signals |
* directly to the child. | * directly to the command. |
*/ |
*/ |
if (signo == SIGCHLD) { |
if (signo == SIGCHLD) { |
if (!handle_sigchld(backchannel, &cstat)) |
if (!handle_sigchld(backchannel, &cstat)) |
alive = false; |
alive = false; |
} else { |
} else { |
deliver_signal(child, signo, false); | deliver_signal(cmnd_pid, signo, false); |
} |
} |
continue; |
continue; |
} |
} |
Line 1080 exec_monitor(struct command_details *details, int back
|
Line 1181 exec_monitor(struct command_details *details, int back
|
cstmp.type); |
cstmp.type); |
continue; |
continue; |
} |
} |
deliver_signal(child, cstmp.val, true); | deliver_signal(cmnd_pid, cstmp.val, true); |
} |
} |
} |
} |
|
|
done: |
done: |
if (alive) { |
if (alive) { |
/* XXX An error occurred, should send an error back. */ |
/* XXX An error occurred, should send an error back. */ |
kill(child, SIGKILL); | kill(cmnd_pid, SIGKILL); |
} else { |
} else { |
/* Send parent status. */ |
/* Send parent status. */ |
send_status(backchannel, &cstat); |
send_status(backchannel, &cstat); |
Line 1179 flush_output(void)
|
Line 1280 flush_output(void)
|
* Returns only if execve() fails. |
* Returns only if execve() fails. |
*/ |
*/ |
static void |
static void |
exec_pty(struct command_details *details, int *errfd) | exec_pty(struct command_details *details, |
| struct command_status *cstat, int *errfd) |
{ |
{ |
pid_t self = getpid(); |
pid_t self = getpid(); |
debug_decl(exec_pty, SUDO_DEBUG_EXEC); |
debug_decl(exec_pty, SUDO_DEBUG_EXEC); |
|
|
/* Set child process group here too to avoid a race. */ | /* Register cleanup function */ |
| fatal_callback_register(pty_cleanup); |
| |
| /* Set command process group here too to avoid a race. */ |
setpgid(0, self); |
setpgid(0, self); |
|
|
/* Wire up standard fds, note that stdout/stderr may be pipes. */ |
/* Wire up standard fds, note that stdout/stderr may be pipes. */ |
if (dup2(io_fds[SFD_STDIN], STDIN_FILENO) == -1 || |
if (dup2(io_fds[SFD_STDIN], STDIN_FILENO) == -1 || |
dup2(io_fds[SFD_STDOUT], STDOUT_FILENO) == -1 || |
dup2(io_fds[SFD_STDOUT], STDOUT_FILENO) == -1 || |
dup2(io_fds[SFD_STDERR], STDERR_FILENO) == -1) |
dup2(io_fds[SFD_STDERR], STDERR_FILENO) == -1) |
error(1, "dup2"); | fatal("dup2"); |
|
|
/* Wait for parent to grant us the tty if we are foreground. */ |
/* Wait for parent to grant us the tty if we are foreground. */ |
if (foreground) { | if (foreground && !ISSET(details->flags, CD_EXEC_BG)) { |
while (tcgetpgrp(io_fds[SFD_SLAVE]) != self) |
while (tcgetpgrp(io_fds[SFD_SLAVE]) != self) |
; /* spin */ |
; /* spin */ |
} |
} |
Line 1209 exec_pty(struct command_details *details, int *errfd)
|
Line 1314 exec_pty(struct command_details *details, int *errfd)
|
if (io_fds[SFD_STDERR] != io_fds[SFD_SLAVE]) |
if (io_fds[SFD_STDERR] != io_fds[SFD_SLAVE]) |
close(io_fds[SFD_STDERR]); |
close(io_fds[SFD_STDERR]); |
|
|
sudo_debug_execve(SUDO_DEBUG_INFO, details->command, | /* Execute command; only returns on error. */ |
details->argv, details->envp); | exec_cmnd(details, cstat, errfd); |
|
|
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, |
|
ISSET(details->flags, CD_NOEXEC)); |
|
} else |
|
#endif |
|
{ |
|
sudo_execve(details->command, details->argv, details->envp, |
|
ISSET(details->flags, CD_NOEXEC)); |
|
} |
|
sudo_debug_printf(SUDO_DEBUG_ERROR, "unable to exec %s: %s", |
|
details->command, strerror(errno)); |
|
debug_return; |
debug_return; |
} |
} |
|
|