version 1.1, 2012/02/21 16:23:02
|
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 50
|
Line 49
|
#if TIME_WITH_SYS_TIME |
#if TIME_WITH_SYS_TIME |
# include <time.h> |
# include <time.h> |
#endif |
#endif |
#ifdef HAVE_SETLOCALE |
|
# include <locale.h> |
|
#endif |
|
#include <errno.h> |
#include <errno.h> |
#include <fcntl.h> |
#include <fcntl.h> |
#include <signal.h> |
#include <signal.h> |
Line 63
|
Line 59
|
#include "sudo_plugin.h" |
#include "sudo_plugin.h" |
#include "sudo_plugin_int.h" |
#include "sudo_plugin_int.h" |
|
|
/* Shared with exec_pty.c for use with handler(). */ |
|
int signal_pipe[2]; |
|
|
|
/* We keep a tailq of signals to forward to child. */ |
/* We keep a tailq of signals to forward to child. */ |
struct sigforward { |
struct sigforward { |
struct sigforward *prev, *next; |
struct sigforward *prev, *next; |
Line 73 struct sigforward {
|
Line 66 struct sigforward {
|
}; |
}; |
TQ_DECLARE(sigforward) |
TQ_DECLARE(sigforward) |
static struct sigforward_list sigfwd_list; |
static struct sigforward_list sigfwd_list; |
|
static pid_t ppgrp = -1; |
|
|
static int handle_signals(int fd, pid_t child, int log_io, | volatile pid_t cmnd_pid = -1; |
| |
| static int dispatch_signals(int sv[2], pid_t child, int log_io, |
struct command_status *cstat); |
struct command_status *cstat); |
|
static int dispatch_pending_signals(struct command_status *cstat); |
static void forward_signals(int fd); |
static void forward_signals(int fd); |
static void schedule_signal(int signo); |
static void schedule_signal(int signo); |
|
#ifdef SA_SIGINFO |
|
static void handler_user_only(int s, siginfo_t *info, void *context); |
|
#endif |
|
|
/* |
/* |
* Like execve(2) but falls back to running through /bin/sh |
|
* ala execvp(3) if we get ENOEXEC. |
|
*/ |
|
int |
|
my_execve(const char *path, char *const argv[], char *const envp[]) |
|
{ |
|
execve(path, argv, envp); |
|
if (errno == ENOEXEC) { |
|
int argc; |
|
char **nargv; |
|
|
|
for (argc = 0; argv[argc] != NULL; argc++) |
|
continue; |
|
nargv = emalloc2(argc + 2, sizeof(char *)); |
|
nargv[0] = "sh"; |
|
nargv[1] = (char *)path; |
|
memcpy(nargv + 2, argv + 1, argc * sizeof(char *)); |
|
execve(_PATH_BSHELL, nargv, envp); |
|
efree(nargv); |
|
} |
|
return -1; |
|
} |
|
|
|
/* |
|
* Fork and execute a command, returns the child's pid. |
* Fork and execute a command, returns the child's pid. |
* Sends errno back on sv[1] if execve() fails. |
* Sends errno back on sv[1] if execve() fails. |
*/ |
*/ |
Line 111 static int fork_cmnd(struct command_details *details,
|
Line 87 static int fork_cmnd(struct command_details *details,
|
{ |
{ |
struct command_status cstat; |
struct command_status cstat; |
sigaction_t sa; |
sigaction_t sa; |
pid_t child; | debug_decl(fork_cmnd, SUDO_DEBUG_EXEC) |
|
|
zero_bytes(&sa, sizeof(sa)); | ppgrp = getpgrp(); /* parent's process group */ |
sigemptyset(&sa.sa_mask); | |
| /* |
| * Handle suspend/restore of sudo and the command. |
| * In most cases, the command will be in the same process group as |
| * sudo and job control will "just work". However, if the command |
| * changes its process group ID and does not change it back (or is |
| * kill by SIGSTOP which is not catchable), we need to resume the |
| * command manually. Also, if SIGTSTP is sent directly to sudo, |
| * we need to suspend the command, and then suspend ourself, restoring |
| * the default SIGTSTP handler temporarily. |
| * |
| * XXX - currently we send SIGCONT upon resume in some cases where |
| * we don't need to (e.g. command pgrp == parent pgrp). |
| */ |
| memset(&sa, 0, sizeof(sa)); |
| 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(SIGCONT, &sa, NULL); | #endif |
| sudo_sigaction(SIGCONT, &sa, NULL); |
| #ifdef SA_SIGINFO |
| sa.sa_sigaction = handler_user_only; |
| #endif |
| sudo_sigaction(SIGTSTP, &sa, NULL); |
|
|
child = fork(); | /* |
switch (child) { | * The policy plugin's session init must be run before we fork |
| * or certain pam modules won't be able to track their state. |
| */ |
| if (policy_init_session(details) != true) |
| fatalx(_("policy plugin failed session initialization")); |
| |
| cmnd_pid = sudo_debug_fork(); |
| switch (cmnd_pid) { |
case -1: |
case -1: |
error(1, _("unable to fork")); | fatal(_("unable to fork")); |
break; |
break; |
case 0: |
case 0: |
/* child */ |
/* child */ |
Line 130 static int fork_cmnd(struct command_details *details,
|
Line 137 static int fork_cmnd(struct command_details *details,
|
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); |
restore_signals(); | exec_cmnd(details, &cstat, &sv[1]); |
if (exec_setup(details, NULL, -1) == TRUE) { | |
/* headed for execve() */ | |
if (details->closefrom >= 0) | |
closefrom(details->closefrom); | |
#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); | |
} | |
cstat.type = CMD_ERRNO; | |
cstat.val = errno; | |
send(sv[1], &cstat, sizeof(cstat), 0); |
send(sv[1], &cstat, sizeof(cstat), 0); |
|
sudo_debug_exit_int(__func__, __FILE__, __LINE__, sudo_debug_subsys, 1); |
_exit(1); |
_exit(1); |
} |
} |
return child; | sudo_debug_printf(SUDO_DEBUG_INFO, "executed %s, pid %d", details->command, |
| (int)cmnd_pid); |
| debug_return_int(cmnd_pid); |
} |
} |
|
|
static struct signal_state { |
|
int signo; |
|
sigaction_t sa; |
|
} saved_signals[] = { |
|
{ SIGALRM }, |
|
{ SIGCHLD }, |
|
{ SIGCONT }, |
|
{ SIGHUP }, |
|
{ SIGINT }, |
|
{ SIGPIPE }, |
|
{ SIGQUIT }, |
|
{ SIGTERM }, |
|
{ SIGTSTP }, |
|
{ SIGTTIN }, |
|
{ SIGTTOU }, |
|
{ SIGUSR1 }, |
|
{ SIGUSR2 }, |
|
{ -1 } |
|
}; |
|
|
|
/* |
/* |
* Save signal handler state so it can be restored before exec. | * Setup the execution environment and execute the command. |
| * If SELinux is enabled, run the command via sesh, otherwise |
| * execute it directly. |
| * If the exec fails, cstat is filled in with the value of errno. |
*/ |
*/ |
void |
void |
save_signals(void) | exec_cmnd(struct command_details *details, struct command_status *cstat, |
| int *errfd) |
{ |
{ |
struct signal_state *ss; | debug_decl(exec_cmnd, SUDO_DEBUG_EXEC) |
|
|
for (ss = saved_signals; ss->signo != -1; ss++) | restore_signals(); |
sigaction(ss->signo, NULL, &ss->sa); | if (exec_setup(details, NULL, -1) == true) { |
| /* headed for execve() */ |
| sudo_debug_execve(SUDO_DEBUG_INFO, details->command, |
| details->argv, details->envp); |
| if (details->closefrom >= 0) { |
| int maxfd = details->closefrom; |
| /* Preserve back channel if present. */ |
| if (errfd != NULL) { |
| 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)); |
| } |
| cstat->type = CMD_ERRNO; |
| cstat->val = errno; |
| sudo_debug_printf(SUDO_DEBUG_ERROR, "unable to exec %s: %s", |
| details->command, strerror(errno)); |
| } |
| debug_return; |
} |
} |
|
|
/* |
/* |
* Restore signal handlers to initial state. | * Execute a command, potentially in a pty with I/O loggging, and |
*/ | * wait for it to finish. |
void | |
restore_signals(void) | |
{ | |
struct signal_state *ss; | |
| |
for (ss = saved_signals; ss->signo != -1; ss++) | |
sigaction(ss->signo, &ss->sa, NULL); | |
} | |
| |
/* | |
* Execute a command, potentially in a pty with I/O loggging. | |
* This is a little bit tricky due to how POSIX job control works and |
* This is a little bit tricky due to how POSIX job control works and |
* we fact that we have two different controlling terminals to deal with. |
* we fact that we have two different controlling terminals to deal with. |
*/ |
*/ |
int |
int |
sudo_execve(struct command_details *details, struct command_status *cstat) | sudo_execute(struct command_details *details, struct command_status *cstat) |
{ |
{ |
int maxfd, n, nready, sv[2], log_io = FALSE; | int maxfd, n, nready, sv[2]; |
const char *utmp_user = NULL; |
const char *utmp_user = NULL; |
|
bool log_io = false; |
fd_set *fdsr, *fdsw; |
fd_set *fdsr, *fdsw; |
sigaction_t sa; |
sigaction_t sa; |
|
sigset_t omask; |
pid_t child; |
pid_t child; |
|
debug_decl(sudo_execute, SUDO_DEBUG_EXEC) |
|
|
|
dispatch_pending_signals(cstat); |
|
|
/* If running in background mode, fork and exit. */ |
/* If running in background mode, fork and exit. */ |
if (ISSET(details->flags, CD_BACKGROUND)) { |
if (ISSET(details->flags, CD_BACKGROUND)) { |
switch (fork()) { | switch (sudo_debug_fork()) { |
case -1: |
case -1: |
cstat->type = CMD_ERRNO; |
cstat->type = CMD_ERRNO; |
cstat->val = errno; |
cstat->val = errno; |
return -1; | debug_return_int(-1); |
case 0: |
case 0: |
/* child continues without controlling terminal */ |
/* child continues without controlling terminal */ |
(void)setpgid(0, 0); |
(void)setpgid(0, 0); |
break; |
break; |
default: |
default: |
/* parent exits (but does not flush buffers) */ |
/* parent exits (but does not flush buffers) */ |
|
sudo_debug_exit_int(__func__, __FILE__, __LINE__, |
|
sudo_debug_subsys, 0); |
_exit(0); |
_exit(0); |
} |
} |
} |
} |
Line 231 sudo_execve(struct command_details *details, struct co
|
Line 239 sudo_execve(struct command_details *details, struct co
|
* as the io plugin tailqueue will be empty and no I/O logging will occur. |
* as the io plugin tailqueue will be empty and no I/O logging will occur. |
*/ |
*/ |
if (!tq_empty(&io_plugins) || ISSET(details->flags, CD_USE_PTY)) { |
if (!tq_empty(&io_plugins) || ISSET(details->flags, CD_USE_PTY)) { |
log_io = TRUE; | log_io = true; |
if (ISSET(details->flags, CD_SET_UTMP)) |
if (ISSET(details->flags, CD_SET_UTMP)) |
utmp_user = details->utmp_user ? details->utmp_user : user_details.username; |
utmp_user = details->utmp_user ? details->utmp_user : user_details.username; |
sudo_debug(8, "allocate pty for I/O logging"); | sudo_debug_printf(SUDO_DEBUG_INFO, "allocate pty for I/O logging"); |
pty_setup(details->euid, user_details.tty, utmp_user); |
pty_setup(details->euid, user_details.tty, utmp_user); |
|
} else if (!ISSET(details->flags, CD_SET_TIMEOUT) && |
|
policy_plugin.u.policy->close == NULL) { |
|
/* If no I/O logging, timeout or policy close we can exec directly. */ |
|
exec_cmnd(details, cstat, NULL); |
|
goto done; |
} |
} |
|
|
/* |
/* |
Line 243 sudo_execve(struct command_details *details, struct co
|
Line 256 sudo_execve(struct command_details *details, struct co
|
* Parent sends signal info to child and child sends back wait status. |
* Parent sends signal info to child and child sends back wait status. |
*/ |
*/ |
if (socketpair(PF_UNIX, SOCK_DGRAM, 0, sv) == -1) |
if (socketpair(PF_UNIX, SOCK_DGRAM, 0, sv) == -1) |
error(1, _("unable to create sockets")); | fatal(_("unable to create sockets")); |
|
|
/* |
/* |
* We use a pipe to atomically handle signal notification within | * Signals to forward to the child process (excluding SIGALRM and SIGCHLD). |
* the select() loop. | * We block all other signals while running the signal handler. |
*/ | |
if (pipe_nonblock(signal_pipe) != 0) | |
error(1, _("unable to create pipe")); | |
| |
zero_bytes(&sa, sizeof(sa)); | |
sigemptyset(&sa.sa_mask); | |
| |
/* | |
* Signals for forward to the child process (excluding SIGALRM and SIGCHLD). | |
* Note: HP-UX select() will not be interrupted if SA_RESTART set. |
* Note: HP-UX select() will not be interrupted if SA_RESTART set. |
*/ |
*/ |
|
memset(&sa, 0, sizeof(sa)); |
|
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(SIGALRM, &sa, NULL); | #endif |
sigaction(SIGCHLD, &sa, NULL); | sudo_sigaction(SIGTERM, &sa, NULL); |
sigaction(SIGHUP, &sa, NULL); | sudo_sigaction(SIGALRM, &sa, NULL); /* XXX - only if there is a timeout */ |
sigaction(SIGINT, &sa, NULL); | sudo_sigaction(SIGCHLD, &sa, NULL); |
sigaction(SIGPIPE, &sa, NULL); | sudo_sigaction(SIGPIPE, &sa, NULL); |
sigaction(SIGQUIT, &sa, NULL); | sudo_sigaction(SIGUSR1, &sa, NULL); |
sigaction(SIGTERM, &sa, NULL); | sudo_sigaction(SIGUSR2, &sa, NULL); |
sigaction(SIGUSR1, &sa, NULL); | |
sigaction(SIGUSR2, &sa, NULL); | |
|
|
|
/* |
|
* When not running the command in a pty, we do not want to |
|
* forward signals generated by the kernel that the child will |
|
* already have received either by virtue of being in the |
|
* controlling tty's process group (SIGINT, SIGQUIT) or because |
|
* the session is terminating (SIGHUP). |
|
*/ |
|
#ifdef SA_SIGINFO |
|
if (!log_io) { |
|
sa.sa_flags |= SA_SIGINFO; |
|
sa.sa_sigaction = handler_user_only; |
|
} |
|
#endif |
|
sudo_sigaction(SIGHUP, &sa, NULL); |
|
sudo_sigaction(SIGINT, &sa, NULL); |
|
sudo_sigaction(SIGQUIT, &sa, NULL); |
|
|
/* Max fd we will be selecting on. */ |
/* Max fd we will be selecting on. */ |
maxfd = MAX(sv[0], signal_pipe[0]); |
maxfd = MAX(sv[0], signal_pipe[0]); |
|
|
Line 279 sudo_execve(struct command_details *details, struct co
|
Line 304 sudo_execve(struct command_details *details, struct co
|
* to and from pty. Adjusts maxfd as needed. |
* to and from pty. Adjusts maxfd as needed. |
*/ |
*/ |
if (log_io) |
if (log_io) |
child = fork_pty(details, sv, &maxfd); | child = fork_pty(details, sv, &maxfd, &omask); |
else |
else |
child = fork_cmnd(details, sv); |
child = fork_cmnd(details, sv); |
close(sv[1]); |
close(sv[1]); |
Line 288 sudo_execve(struct command_details *details, struct co
|
Line 313 sudo_execve(struct command_details *details, struct co
|
if (ISSET(details->flags, CD_SET_TIMEOUT)) |
if (ISSET(details->flags, CD_SET_TIMEOUT)) |
alarm(details->timeout); |
alarm(details->timeout); |
|
|
#ifdef HAVE_SETLOCALE |
|
/* |
/* |
* I/O logging must be in the C locale for floating point numbers |
* I/O logging must be in the C locale for floating point numbers |
* to be logged consistently. |
* to be logged consistently. |
*/ |
*/ |
setlocale(LC_ALL, "C"); |
setlocale(LC_ALL, "C"); |
#endif |
|
|
|
/* |
/* |
* In the event loop we pass input from user tty to master |
* In the event loop we pass input from user tty to master |
* and pass output from master to stdout and IO plugin. |
* and pass output from master to stdout and IO plugin. |
*/ |
*/ |
fdsr = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); | fdsr = emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); |
fdsw = (fd_set *)emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); | fdsw = emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask)); |
for (;;) { |
for (;;) { |
zero_bytes(fdsw, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); | memset(fdsw, 0, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); |
zero_bytes(fdsr, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); | memset(fdsr, 0, howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask)); |
|
|
FD_SET(signal_pipe[0], fdsr); |
FD_SET(signal_pipe[0], fdsr); |
FD_SET(sv[0], fdsr); |
FD_SET(sv[0], fdsr); |
Line 313 sudo_execve(struct command_details *details, struct co
|
Line 336 sudo_execve(struct command_details *details, struct co
|
if (log_io) |
if (log_io) |
fd_set_iobs(fdsr, fdsw); /* XXX - better name */ |
fd_set_iobs(fdsr, fdsw); /* XXX - better name */ |
nready = select(maxfd + 1, fdsr, fdsw, NULL, NULL); |
nready = select(maxfd + 1, fdsr, fdsw, NULL, NULL); |
|
sudo_debug_printf(SUDO_DEBUG_DEBUG, "select returns %d", nready); |
if (nready == -1) { |
if (nready == -1) { |
if (errno == EINTR) | if (errno == EINTR || errno == ENOMEM) |
continue; |
continue; |
error(1, _("select failed")); | if (errno == EBADF || errno == EIO) { |
| /* One of the ttys must have gone away. */ |
| goto do_tty_io; |
| } |
| warning(_("select failed")); |
| sudo_debug_printf(SUDO_DEBUG_ERROR, |
| "select failure, terminating child"); |
| schedule_signal(SIGKILL); |
| forward_signals(sv[0]); |
| break; |
} |
} |
if (FD_ISSET(sv[0], fdsw)) { |
if (FD_ISSET(sv[0], fdsw)) { |
forward_signals(sv[0]); |
forward_signals(sv[0]); |
} |
} |
if (FD_ISSET(signal_pipe[0], fdsr)) { |
if (FD_ISSET(signal_pipe[0], fdsr)) { |
n = handle_signals(signal_pipe[0], child, log_io, cstat); | n = dispatch_signals(sv, child, log_io, cstat); |
if (n == 0) { |
if (n == 0) { |
/* Child has exited, cstat is set, we are done. */ |
/* Child has exited, cstat is set, we are done. */ |
goto done; | break; |
} |
} |
if (n == -1) { |
if (n == -1) { |
/* Error reading signal_pipe[0], should not happen. */ |
/* Error reading signal_pipe[0], should not happen. */ |
Line 337 sudo_execve(struct command_details *details, struct co
|
Line 370 sudo_execve(struct command_details *details, struct co
|
if (FD_ISSET(sv[0], fdsr)) { |
if (FD_ISSET(sv[0], fdsr)) { |
/* read child status */ |
/* read child status */ |
n = recv(sv[0], cstat, sizeof(*cstat), 0); |
n = recv(sv[0], cstat, sizeof(*cstat), 0); |
if (n == -1) { | if (n != sizeof(*cstat)) { |
if (errno == EINTR) | if (n == -1) { |
continue; | if (errno == EINTR) |
/* | continue; |
* If not logging I/O we will receive ECONNRESET when | /* |
* the command is executed. It is safe to ignore this. | * If not logging I/O we may receive ECONNRESET when |
*/ | * the command is executed and sv is closed. |
if (log_io && errno != EAGAIN) { | * It is safe to ignore this. |
cstat->type = CMD_ERRNO; | */ |
cstat->val = errno; | if (log_io && errno != EAGAIN) { |
| cstat->type = CMD_ERRNO; |
| cstat->val = errno; |
| break; |
| } |
| sudo_debug_printf(SUDO_DEBUG_ERROR, |
| "failed to read child status: %s", strerror(errno)); |
| } else { |
| /* Short read or EOF. */ |
| sudo_debug_printf(SUDO_DEBUG_ERROR, |
| "failed to read child status: %s", |
| n ? "short read" : "EOF"); |
| /* XXX - should set cstat */ |
break; |
break; |
} |
} |
} |
} |
if (cstat->type == CMD_WSTATUS) { | if (cstat->type == CMD_PID) { |
| /* |
| * Once we know the command's pid we can unblock |
| * signals which ere blocked in fork_pty(). This |
| * avoids a race between exec of the command and |
| * receipt of a fatal signal from it. |
| */ |
| cmnd_pid = cstat->val; |
| sudo_debug_printf(SUDO_DEBUG_INFO, "executed %s, pid %d", |
| details->command, (int)cmnd_pid); |
| if (log_io) |
| sigprocmask(SIG_SETMASK, &omask, NULL); |
| } else if (cstat->type == CMD_WSTATUS) { |
if (WIFSTOPPED(cstat->val)) { |
if (WIFSTOPPED(cstat->val)) { |
/* Suspend parent and tell child how to resume on return. */ |
/* Suspend parent and tell child how to resume on return. */ |
sudo_debug(8, "child stopped, suspending parent"); | sudo_debug_printf(SUDO_DEBUG_INFO, |
| "child stopped, suspending parent"); |
n = suspend_parent(WSTOPSIG(cstat->val)); |
n = suspend_parent(WSTOPSIG(cstat->val)); |
schedule_signal(n); |
schedule_signal(n); |
continue; |
continue; |
} else { |
} else { |
/* Child exited or was killed, either way we are done. */ |
/* Child exited or was killed, either way we are done. */ |
|
sudo_debug_printf(SUDO_DEBUG_INFO, "child exited or was killed"); |
break; |
break; |
} |
} |
} else if (cstat->type == CMD_ERRNO) { |
} else if (cstat->type == CMD_ERRNO) { |
/* Child was unable to execute command or broken pipe. */ |
/* Child was unable to execute command or broken pipe. */ |
|
sudo_debug_printf(SUDO_DEBUG_INFO, "errno from child: %s", |
|
strerror(cstat->val)); |
break; |
break; |
} |
} |
} |
} |
| do_tty_io: |
if (perform_io(fdsr, fdsw, cstat) != 0) { |
if (perform_io(fdsr, fdsw, cstat) != 0) { |
/* I/O error, kill child if still alive and finish. */ |
/* I/O error, kill child if still alive and finish. */ |
|
sudo_debug_printf(SUDO_DEBUG_ERROR, "I/O error, terminating child"); |
schedule_signal(SIGKILL); |
schedule_signal(SIGKILL); |
forward_signals(sv[0]); |
forward_signals(sv[0]); |
break; |
break; |
Line 388 sudo_execve(struct command_details *details, struct co
|
Line 450 sudo_execve(struct command_details *details, struct co
|
} |
} |
#endif |
#endif |
|
|
done: |
|
efree(fdsr); |
efree(fdsr); |
efree(fdsw); |
efree(fdsw); |
while (!tq_empty(&sigfwd_list)) { |
while (!tq_empty(&sigfwd_list)) { |
Line 396 done:
|
Line 457 done:
|
tq_remove(&sigfwd_list, sigfwd); |
tq_remove(&sigfwd_list, sigfwd); |
efree(sigfwd); |
efree(sigfwd); |
} |
} |
| done: |
return cstat->type == CMD_ERRNO ? -1 : 0; | debug_return_int(cstat->type == CMD_ERRNO ? -1 : 0); |
} |
} |
|
|
/* |
/* |
* Read signals on fd written to by handler(). | * Read signals on signal_pipe written by handler(). |
* Returns -1 on error, 0 on child exit, else 1. |
* Returns -1 on error, 0 on child exit, else 1. |
*/ |
*/ |
static int |
static int |
handle_signals(int fd, pid_t child, int log_io, struct command_status *cstat) | dispatch_signals(int sv[2], pid_t child, int log_io, struct command_status *cstat) |
{ |
{ |
|
char signame[SIG2STR_MAX]; |
unsigned char signo; |
unsigned char signo; |
ssize_t nread; |
ssize_t nread; |
int status; |
int status; |
pid_t pid; |
pid_t pid; |
|
debug_decl(dispatch_signals, SUDO_DEBUG_EXEC) |
|
|
for (;;) { |
for (;;) { |
/* read signal pipe */ |
/* read signal pipe */ |
Line 425 handle_signals(int fd, pid_t child, int log_io, struct
|
Line 488 handle_signals(int fd, pid_t child, int log_io, struct
|
/* If pipe is empty, we are done. */ |
/* If pipe is empty, we are done. */ |
if (errno == EAGAIN) |
if (errno == EAGAIN) |
break; |
break; |
sudo_debug(9, "error reading signal pipe %s", strerror(errno)); | sudo_debug_printf(SUDO_DEBUG_ERROR, "error reading signal pipe %s", |
| strerror(errno)); |
cstat->type = CMD_ERRNO; |
cstat->type = CMD_ERRNO; |
cstat->val = errno; |
cstat->val = errno; |
return -1; | debug_return_int(-1); |
} |
} |
sudo_debug(9, "received signal %d", signo); | if (sig2str(signo, signame) == -1) |
| snprintf(signame, sizeof(signame), "%d", signo); |
| sudo_debug_printf(SUDO_DEBUG_DIAG, "received SIG%s", signame); |
if (signo == SIGCHLD) { |
if (signo == SIGCHLD) { |
/* |
/* |
* If logging I/O, child is the intermediate process, |
* If logging I/O, child is the intermediate process, |
Line 440 handle_signals(int fd, pid_t child, int log_io, struct
|
Line 506 handle_signals(int fd, pid_t child, int log_io, struct
|
pid = waitpid(child, &status, WUNTRACED|WNOHANG); |
pid = waitpid(child, &status, WUNTRACED|WNOHANG); |
} while (pid == -1 && errno == EINTR); |
} while (pid == -1 && errno == EINTR); |
if (pid == child) { |
if (pid == child) { |
/* If not logging I/O and child has exited we are done. */ | if (log_io) { |
if (!log_io) { | /* |
| * On BSD we get ECONNRESET on sv[0] if monitor dies |
| * and select() will return with sv[0] readable. |
| * On Linux that doesn't appear to happen so if the |
| * monitor dies, shut down the socketpair to force a |
| * select() notification. |
| */ |
| (void) shutdown(sv[0], SHUT_WR); |
| } else { |
if (WIFSTOPPED(status)) { |
if (WIFSTOPPED(status)) { |
/* |
/* |
* Save the controlling terminal's process group |
* Save the controlling terminal's process group |
* so we can restore it after we resume. | * so we can restore it after we resume, if needed. |
| * Most well-behaved shells change the pgrp back to |
| * its original value before suspending so we must |
| * not try to restore in that case, lest we race with |
| * the child upon resume, potentially stopping sudo |
| * with SIGTTOU while the command continues to run. |
*/ |
*/ |
|
sigaction_t sa, osa; |
pid_t saved_pgrp = (pid_t)-1; |
pid_t saved_pgrp = (pid_t)-1; |
|
int signo = WSTOPSIG(status); |
int fd = open(_PATH_TTY, O_RDWR|O_NOCTTY, 0); |
int fd = open(_PATH_TTY, O_RDWR|O_NOCTTY, 0); |
if (fd != -1) | if (fd != -1) { |
saved_pgrp = tcgetpgrp(fd); | if ((saved_pgrp = tcgetpgrp(fd)) == ppgrp) |
if (kill(getpid(), WSTOPSIG(status)) != 0) { | saved_pgrp = -1; |
warning("kill(%d, %d)", (int)getpid(), | |
WSTOPSIG(status)); | |
} |
} |
|
if (signo == SIGTSTP) { |
|
memset(&sa, 0, sizeof(sa)); |
|
sigemptyset(&sa.sa_mask); |
|
sa.sa_flags = SA_RESTART; |
|
sa.sa_handler = SIG_DFL; |
|
sudo_sigaction(SIGTSTP, &sa, &osa); |
|
} |
|
if (kill(getpid(), signo) != 0) |
|
warning("kill(%d, SIG%s)", (int)getpid(), signame); |
|
if (signo == SIGTSTP) |
|
sudo_sigaction(SIGTSTP, &osa, NULL); |
if (fd != -1) { |
if (fd != -1) { |
|
/* |
|
* Restore command's process group if different. |
|
* Otherwise, we cannot resume some shells. |
|
*/ |
if (saved_pgrp != (pid_t)-1) |
if (saved_pgrp != (pid_t)-1) |
(void)tcsetpgrp(fd, saved_pgrp); |
(void)tcsetpgrp(fd, saved_pgrp); |
close(fd); |
close(fd); |
Line 464 handle_signals(int fd, pid_t child, int log_io, struct
|
Line 558 handle_signals(int fd, pid_t child, int log_io, struct
|
/* Child has exited, we are done. */ |
/* Child has exited, we are done. */ |
cstat->type = CMD_WSTATUS; |
cstat->type = CMD_WSTATUS; |
cstat->val = status; |
cstat->val = status; |
return 0; | debug_return_int(0); |
} |
} |
} |
} |
/* Else we get ECONNRESET on sv[0] if child dies. */ |
|
} |
} |
} else { |
} else { |
if (log_io) { |
if (log_io) { |
Line 476 handle_signals(int fd, pid_t child, int log_io, struct
|
Line 569 handle_signals(int fd, pid_t child, int log_io, struct
|
} else { |
} else { |
/* Nothing listening on sv[0], send directly. */ |
/* Nothing listening on sv[0], send directly. */ |
if (signo == SIGALRM) |
if (signo == SIGALRM) |
terminate_child(child, FALSE); | terminate_command(child, false); |
else if (kill(child, signo) != 0) |
else if (kill(child, signo) != 0) |
warning("kill(%d, %d)", (int)child, signo); | warning("kill(%d, SIG%s)", (int)child, signame); |
} |
} |
} |
} |
} |
} |
return 1; | debug_return_int(1); |
} |
} |
|
|
/* |
/* |
|
* Drain pending signals from signale_pipe written by sudo_handler(). |
|
* Handles the case where the signal was sent to us before |
|
* we have executed the command. |
|
* Returns 1 if we should terminate, else 0. |
|
*/ |
|
static int |
|
dispatch_pending_signals(struct command_status *cstat) |
|
{ |
|
ssize_t nread; |
|
struct sigaction sa; |
|
unsigned char signo = 0; |
|
int rval = 0; |
|
debug_decl(dispatch_pending_signals, SUDO_DEBUG_EXEC) |
|
|
|
for (;;) { |
|
nread = read(signal_pipe[0], &signo, sizeof(signo)); |
|
if (nread <= 0) { |
|
/* It should not be possible to get EOF but just in case. */ |
|
if (nread == 0) |
|
errno = ECONNRESET; |
|
/* Restart if interrupted by signal so the pipe doesn't fill. */ |
|
if (errno == EINTR) |
|
continue; |
|
/* If pipe is empty, we are done. */ |
|
if (errno == EAGAIN) |
|
break; |
|
sudo_debug_printf(SUDO_DEBUG_ERROR, "error reading signal pipe %s", |
|
strerror(errno)); |
|
cstat->type = CMD_ERRNO; |
|
cstat->val = errno; |
|
rval = 1; |
|
break; |
|
} |
|
/* Take the first terminal signal. */ |
|
if (signo == SIGINT || signo == SIGQUIT) { |
|
cstat->type = CMD_WSTATUS; |
|
cstat->val = signo + 128; |
|
rval = 1; |
|
break; |
|
} |
|
} |
|
/* Only stop if we haven't already been terminated. */ |
|
if (signo == SIGTSTP) |
|
{ |
|
memset(&sa, 0, sizeof(sa)); |
|
sigemptyset(&sa.sa_mask); |
|
sa.sa_flags = SA_RESTART; |
|
sa.sa_handler = SIG_DFL; |
|
sudo_sigaction(SIGTSTP, &sa, NULL); |
|
if (kill(getpid(), SIGTSTP) != 0) |
|
warning("kill(%d, SIGTSTP)", (int)getpid()); |
|
/* No need to reinstall SIGTSTP handler. */ |
|
} |
|
debug_return_int(rval); |
|
} |
|
|
|
/* |
* Forward signals in sigfwd_list to child listening on fd. |
* Forward signals in sigfwd_list to child listening on fd. |
*/ |
*/ |
static void |
static void |
forward_signals(int sock) |
forward_signals(int sock) |
{ |
{ |
|
char signame[SIG2STR_MAX]; |
struct sigforward *sigfwd; |
struct sigforward *sigfwd; |
struct command_status cstat; |
struct command_status cstat; |
ssize_t nsent; |
ssize_t nsent; |
|
debug_decl(forward_signals, SUDO_DEBUG_EXEC) |
|
|
while (!tq_empty(&sigfwd_list)) { |
while (!tq_empty(&sigfwd_list)) { |
sigfwd = tq_first(&sigfwd_list); |
sigfwd = tq_first(&sigfwd_list); |
sudo_debug(9, "sending signal %d to child over backchannel", | if (sigfwd->signo == SIGCONT_FG) |
sigfwd->signo); | strlcpy(signame, "CONT_FG", sizeof(signame)); |
| else if (sigfwd->signo == SIGCONT_BG) |
| strlcpy(signame, "CONT_BG", sizeof(signame)); |
| else if (sig2str(sigfwd->signo, signame) == -1) |
| snprintf(signame, sizeof(signame), "%d", sigfwd->signo); |
| sudo_debug_printf(SUDO_DEBUG_INFO, |
| "sending SIG%s to child over backchannel", signame); |
cstat.type = CMD_SIGNO; |
cstat.type = CMD_SIGNO; |
cstat.val = sigfwd->signo; |
cstat.val = sigfwd->signo; |
do { |
do { |
Line 508 forward_signals(int sock)
|
Line 666 forward_signals(int sock)
|
efree(sigfwd); |
efree(sigfwd); |
if (nsent != sizeof(cstat)) { |
if (nsent != sizeof(cstat)) { |
if (errno == EPIPE) { |
if (errno == EPIPE) { |
|
sudo_debug_printf(SUDO_DEBUG_ERROR, |
|
"broken pipe writing to child over backchannel"); |
/* Other end of socket gone, empty out sigfwd_list. */ |
/* Other end of socket gone, empty out sigfwd_list. */ |
while (!tq_empty(&sigfwd_list)) { |
while (!tq_empty(&sigfwd_list)) { |
sigfwd = tq_first(&sigfwd_list); |
sigfwd = tq_first(&sigfwd_list); |
tq_remove(&sigfwd_list, sigfwd); |
tq_remove(&sigfwd_list, sigfwd); |
efree(sigfwd); |
efree(sigfwd); |
} |
} |
|
/* XXX - child (monitor) is dead, we should exit too? */ |
} |
} |
break; |
break; |
} |
} |
} |
} |
|
debug_return; |
} |
} |
|
|
/* |
/* |
Line 527 static void
|
Line 689 static void
|
schedule_signal(int signo) |
schedule_signal(int signo) |
{ |
{ |
struct sigforward *sigfwd; |
struct sigforward *sigfwd; |
|
char signame[SIG2STR_MAX]; |
|
debug_decl(schedule_signal, SUDO_DEBUG_EXEC) |
|
|
sigfwd = emalloc(sizeof(*sigfwd)); | 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); |
| sudo_debug_printf(SUDO_DEBUG_DIAG, "scheduled SIG%s for child", signame); |
| |
| sigfwd = ecalloc(1, sizeof(*sigfwd)); |
sigfwd->prev = sigfwd; |
sigfwd->prev = sigfwd; |
sigfwd->next = NULL; | /* sigfwd->next = NULL; */ |
sigfwd->signo = signo; |
sigfwd->signo = signo; |
tq_append(&sigfwd_list, sigfwd); |
tq_append(&sigfwd_list, sigfwd); |
|
|
|
debug_return; |
} |
} |
|
|
/* |
/* |
* Generic handler for signals passed from parent -> child. |
* Generic handler for signals passed from parent -> child. |
* The other end of signal_pipe is checked in the main event loop. |
* The other end of signal_pipe is checked in the main event loop. |
*/ |
*/ |
|
#ifdef SA_SIGINFO |
void |
void |
|
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 child 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 |
handler(int s) |
handler(int s) |
{ |
{ |
unsigned char signo = (unsigned char)s; |
unsigned char signo = (unsigned char)s; |
Line 548 handler(int s)
|
Line 744 handler(int s)
|
* The pipe is non-blocking, if we overflow the kernel's pipe |
* The pipe is non-blocking, if we overflow the kernel's pipe |
* buffer we drop the signal. This is not a problem in practice. |
* buffer we drop the signal. This is not a problem in practice. |
*/ |
*/ |
if (write(signal_pipe[1], &signo, sizeof(signo)) == -1) | ignore_result(write(signal_pipe[1], &signo, sizeof(signo))); |
/* shut up glibc */; | |
} |
} |
|
#endif |
|
|
|
#ifdef SA_SIGINFO |
/* |
/* |
|
* Generic handler for signals passed from parent -> child. |
|
* The other end of signal_pipe is checked in the main event loop. |
|
* This version is for the non-pty case and does not forward |
|
* signals that are generated by the kernel. |
|
*/ |
|
static void |
|
handler_user_only(int s, siginfo_t *info, void *context) |
|
{ |
|
unsigned char signo = (unsigned char)s; |
|
|
|
/* Only forward user-generated signals. */ |
|
if (info != NULL && info->si_code == SI_USER) { |
|
/* |
|
* 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 /* SA_SIGINFO */ |
|
|
|
/* |
* Open a pipe and make both ends non-blocking. |
* Open a pipe and make both ends non-blocking. |
* Returns 0 on success and -1 on error. |
* Returns 0 on success and -1 on error. |
*/ |
*/ |
Line 560 int
|
Line 779 int
|
pipe_nonblock(int fds[2]) |
pipe_nonblock(int fds[2]) |
{ |
{ |
int flags, rval; |
int flags, rval; |
|
debug_decl(pipe_nonblock, SUDO_DEBUG_EXEC) |
|
|
rval = pipe(fds); |
rval = pipe(fds); |
if (rval != -1) { |
if (rval != -1) { |
Line 577 pipe_nonblock(int fds[2])
|
Line 797 pipe_nonblock(int fds[2])
|
} |
} |
} |
} |
|
|
return rval; | debug_return_int(rval); |
} |
} |