#include <config.h>
#include <sys/types.h>
#include <sys/param.h>
#ifdef HAVE_SYS_SYSMACROS_H
# include <sys/sysmacros.h>
#endif
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#ifdef HAVE_TERMIOS_H
# include <termios.h>
#else
# include <termio.h>
#endif
#include <sys/ioctl.h>
#ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
#endif
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#if TIME_WITH_SYS_TIME
# include <time.h>
#endif
#ifdef HAVE_SETLOCALE
# include <locale.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#ifdef HAVE_SELINUX
# include <selinux/selinux.h>
#endif
#include "sudo.h"
#include "sudo_exec.h"
int signal_pipe[2];
#ifdef _PATH_SUDO_IO_LOGDIR
struct sigforward {
struct sigforward *prev, *next;
int signo;
};
TQ_DECLARE(sigforward)
static struct sigforward_list sigfwd_list;
static pid_t ppgrp = -1;
static void forward_signals __P((int fd));
static void schedule_signal __P((int signo));
static int log_io;
#endif
volatile pid_t cmnd_pid = -1;
static int handle_signals __P((int sv[2], pid_t child,
struct command_status *cstat));
#ifdef SA_SIGINFO
static void handler_user_only __P((int s, siginfo_t *info, void *context));
#endif
int
my_execve(path, argv, envp)
const char *path;
char *argv[];
char *envp[];
{
execve(path, argv, envp);
if (errno == ENOEXEC) {
argv--;
argv[0] = "sh";
argv[1] = (char *)path;
execve(_PATH_BSHELL, argv, envp);
}
return -1;
}
static int fork_cmnd(path, argv, envp, sv, rbac_enabled)
const char *path;
char *argv[];
char *envp[];
int sv[2];
int rbac_enabled;
{
struct command_status cstat;
sigaction_t sa;
ppgrp = getpgrp();
zero_bytes(&sa, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_INTERRUPT;
#ifdef SA_SIGINFO
sa.sa_flags |= SA_SIGINFO;
sa.sa_sigaction = handler;
#else
sa.sa_handler = handler;
#endif
sigaction(SIGCONT, &sa, NULL);
#ifdef SA_SIGINFO
sa.sa_sigaction = handler_user_only;
#endif
sigaction(SIGTSTP, &sa, NULL);
cmnd_pid = fork();
switch (cmnd_pid) {
case -1:
error(1, "fork");
break;
case 0:
close(sv[0]);
close(signal_pipe[0]);
close(signal_pipe[1]);
fcntl(sv[1], F_SETFD, FD_CLOEXEC);
restore_signals();
if (exec_setup(rbac_enabled, user_ttypath, -1) == TRUE) {
int maxfd = def_closefrom;
dup2(sv[1], maxfd);
(void)fcntl(maxfd, F_SETFD, FD_CLOEXEC);
sv[1] = maxfd++;
closefrom(maxfd);
#ifdef HAVE_SELINUX
if (rbac_enabled)
selinux_execve(path, argv, envp);
else
#endif
my_execve(path, argv, envp);
}
cstat.type = CMD_ERRNO;
cstat.val = errno;
send(sv[1], &cstat, sizeof(cstat), 0);
_exit(1);
}
return 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 }
};
void
save_signals()
{
struct signal_state *ss;
for (ss = saved_signals; ss->signo != -1; ss++)
sigaction(ss->signo, NULL, &ss->sa);
}
void
restore_signals()
{
struct signal_state *ss;
for (ss = saved_signals; ss->signo != -1; ss++)
sigaction(ss->signo, &ss->sa, NULL);
}
int
sudo_execve(path, argv, envp, uid, cstat, dowait, bgmode)
const char *path;
char *argv[];
char *envp[];
uid_t uid;
struct command_status *cstat;
int dowait;
int bgmode;
{
int maxfd, n, nready, sv[2];
int rbac_enabled = 0;
fd_set *fdsr, *fdsw;
sigaction_t sa;
sigset_t omask;
pid_t child;
if (bgmode) {
switch (fork()) {
case -1:
cstat->type = CMD_ERRNO;
cstat->val = errno;
return -1;
case 0:
(void)setpgid(0, 0);
break;
default:
_exit(0);
}
}
#ifdef _PATH_SUDO_IO_LOGDIR
log_io = def_log_output || def_log_input || def_use_pty;
if (log_io) {
pty_setup(uid);
io_log_open();
dowait = TRUE;
}
#endif
#ifdef HAVE_SELINUX
rbac_enabled = is_selinux_enabled() > 0 && user_role != NULL;
if (rbac_enabled)
dowait = TRUE;
#endif
if (!dowait) {
exec_setup(FALSE, NULL, -1);
closefrom(def_closefrom);
my_execve(path, argv, envp);
cstat->type = CMD_ERRNO;
cstat->val = errno;
return 127;
}
if (socketpair(PF_UNIX, SOCK_DGRAM, 0, sv) == -1)
error(1, "cannot create sockets");
if (pipe_nonblock(signal_pipe) != 0)
error(1, "cannot create pipe");
zero_bytes(&sa, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_INTERRUPT;
#ifdef SA_SIGINFO
sa.sa_flags |= SA_SIGINFO;
sa.sa_sigaction = handler;
#else
sa.sa_handler = handler;
#endif
sigaction(SIGALRM, &sa, NULL);
sigaction(SIGCHLD, &sa, NULL);
sigaction(SIGPIPE, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
#ifdef SA_SIGINFO
# ifdef _PATH_SUDO_IO_LOGDIR
if (!log_io)
# endif
{
sa.sa_flags |= SA_SIGINFO;
sa.sa_sigaction = handler_user_only;
}
#endif
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
maxfd = MAX(sv[0], signal_pipe[0]);
#ifdef _PATH_SUDO_IO_LOGDIR
if (log_io)
child = fork_pty(path, argv, envp, sv, rbac_enabled, bgmode, &maxfd, &omask);
else
#endif
child = fork_cmnd(path, argv, envp, sv, rbac_enabled);
close(sv[1]);
#ifdef HAVE_SETLOCALE
setlocale(LC_ALL, "C");
#endif
fdsr = emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask));
fdsw = emalloc2(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask));
for (;;) {
memset(fdsw, 0, 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(sv[0], fdsr);
#ifdef _PATH_SUDO_IO_LOGDIR
if (!tq_empty(&sigfwd_list))
FD_SET(sv[0], fdsw);
if (log_io)
fd_set_iobs(fdsr, fdsw);
#endif
nready = select(maxfd + 1, fdsr, fdsw, NULL, NULL);
if (nready == -1) {
if (errno == EINTR || errno == ENOMEM)
continue;
#ifdef _PATH_SUDO_IO_LOGDIR
if (errno == EBADF || errno == EIO) {
goto do_tty_io;
}
#endif
warning("select failed");
#ifdef _PATH_SUDO_IO_LOGDIR
schedule_signal(SIGKILL);
forward_signals(sv[0]);
#endif
break;
}
#ifdef _PATH_SUDO_IO_LOGDIR
if (FD_ISSET(sv[0], fdsw)) {
forward_signals(sv[0]);
}
#endif
if (FD_ISSET(signal_pipe[0], fdsr)) {
n = handle_signals(sv, child, cstat);
if (n == 0) {
goto done;
}
if (n == -1) {
break;
}
continue;
}
if (FD_ISSET(sv[0], fdsr)) {
n = recv(sv[0], cstat, sizeof(*cstat), 0);
if (n != sizeof(*cstat)) {
if (n == -1) {
if (errno == EINTR)
continue;
#ifdef _PATH_SUDO_IO_LOGDIR
if (log_io && errno != EAGAIN) {
cstat->type = CMD_ERRNO;
cstat->val = errno;
break;
}
#endif
} else {
break;
}
}
#ifdef _PATH_SUDO_IO_LOGDIR
if (cstat->type == CMD_PID) {
cmnd_pid = cstat->val;
if (log_io)
sigprocmask(SIG_SETMASK, &omask, NULL);
} else if (cstat->type == CMD_WSTATUS) {
if (WIFSTOPPED(cstat->val)) {
n = suspend_parent(WSTOPSIG(cstat->val));
schedule_signal(n);
continue;
} else {
break;
}
} else
#endif
if (cstat->type == CMD_ERRNO) {
break;
}
}
#ifdef _PATH_SUDO_IO_LOGDIR
do_tty_io:
if (perform_io(fdsr, fdsw, cstat) != 0) {
schedule_signal(SIGKILL);
forward_signals(sv[0]);
break;
}
#endif
}
#ifdef _PATH_SUDO_IO_LOGDIR
if (log_io) {
pty_close(cstat);
}
#endif
#ifdef HAVE_SELINUX
if (rbac_enabled) {
if (selinux_restore_tty() != 0)
warningx("unable to restore tty label");
}
#endif
done:
efree(fdsr);
efree(fdsw);
#ifdef _PATH_SUDO_IO_LOGDIR
while (!tq_empty(&sigfwd_list)) {
struct sigforward *sigfwd = tq_first(&sigfwd_list);
tq_remove(&sigfwd_list, sigfwd);
efree(sigfwd);
}
#endif
return cstat->type == CMD_ERRNO ? -1 : 0;
}
static int
handle_signals(sv, child, cstat)
int sv[2];
pid_t child;
struct command_status *cstat;
{
unsigned char signo;
ssize_t nread;
int status;
pid_t pid;
for (;;) {
nread = read(signal_pipe[0], &signo, sizeof(signo));
if (nread <= 0) {
if (nread == 0)
errno = ECONNRESET;
if (errno == EINTR)
continue;
if (errno == EAGAIN)
break;
cstat->type = CMD_ERRNO;
cstat->val = errno;
return -1;
}
if (signo == SIGCHLD) {
do {
#ifdef sudo_waitpid
pid = sudo_waitpid(child, &status, WUNTRACED|WNOHANG);
#else
pid = wait(&status);
#endif
} while (pid == -1 && errno == EINTR);
if (pid == child) {
#ifdef _PATH_SUDO_IO_LOGDIR
if (log_io) {
(void) shutdown(sv[0], SHUT_WR);
} else
#endif
{
if (WIFSTOPPED(status)) {
#ifdef HAVE_TCSETPGRP
sigaction_t sa, osa;
pid_t saved_pgrp = (pid_t)-1;
int signo = WSTOPSIG(status);
int fd = open(_PATH_TTY, O_RDWR|O_NOCTTY, 0);
if (fd != -1) {
if ((saved_pgrp = tcgetpgrp(fd)) == ppgrp)
saved_pgrp = -1;
}
#endif
if (signo == SIGTSTP) {
zero_bytes(&sa, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_DFL;
sigaction(SIGTSTP, &sa, &osa);
}
if (kill(getpid(), signo) != 0)
warning("kill(%d, %d)", getpid(), signo);
if (signo == SIGTSTP)
sigaction(SIGTSTP, &osa, NULL);
#ifdef HAVE_TCSETPGRP
if (fd != -1) {
if (saved_pgrp != (pid_t)-1)
(void)tcsetpgrp(fd, saved_pgrp);
close(fd);
}
#endif
} else {
cstat->type = CMD_WSTATUS;
cstat->val = status;
return 0;
}
}
}
} else {
#ifdef _PATH_SUDO_IO_LOGDIR
if (log_io) {
schedule_signal(signo);
} else
#endif
{
if (kill(child, signo) != 0)
warning("kill(%d, %d)", child, signo);
}
}
}
return 1;
}
#ifdef _PATH_SUDO_IO_LOGDIR
static void
forward_signals(sock)
int sock;
{
struct sigforward *sigfwd;
struct command_status cstat;
ssize_t nsent;
while (!tq_empty(&sigfwd_list)) {
sigfwd = tq_first(&sigfwd_list);
cstat.type = CMD_SIGNO;
cstat.val = sigfwd->signo;
do {
nsent = send(sock, &cstat, sizeof(cstat), 0);
} while (nsent == -1 && errno == EINTR);
tq_remove(&sigfwd_list, sigfwd);
efree(sigfwd);
if (nsent != sizeof(cstat)) {
if (errno == EPIPE) {
while (!tq_empty(&sigfwd_list)) {
sigfwd = tq_first(&sigfwd_list);
tq_remove(&sigfwd_list, sigfwd);
efree(sigfwd);
}
}
break;
}
}
}
static void
schedule_signal(signo)
int signo;
{
struct sigforward *sigfwd;
sigfwd = ecalloc(1, sizeof(*sigfwd));
sigfwd->prev = sigfwd;
sigfwd->signo = signo;
tq_append(&sigfwd_list, sigfwd);
}
#endif
#ifdef SA_SIGINFO
RETSIGTYPE
handler(s, info, context)
int s;
siginfo_t *info;
void *context;
{
unsigned char signo = (unsigned char)s;
if (info != NULL && info->si_code == SI_USER && info->si_pid == cmnd_pid)
return;
ignore_result(write(signal_pipe[1], &signo, sizeof(signo)));
}
#else
RETSIGTYPE
handler(s)
int s;
{
unsigned char signo = (unsigned char)s;
ignore_result(write(signal_pipe[1], &signo, sizeof(signo)));
}
#endif
#ifdef SA_SIGINFO
static RETSIGTYPE
handler_user_only(s, info, context)
int s;
siginfo_t *info;
void *context;
{
unsigned char signo = (unsigned char)s;
if (info != NULL && info->si_code == SI_USER) {
ignore_result(write(signal_pipe[1], &signo, sizeof(signo)));
}
}
#endif
int
pipe_nonblock(fds)
int fds[2];
{
int flags, rval;
rval = pipe(fds);
if (rval != -1) {
flags = fcntl(fds[0], F_GETFL, 0);
if (flags != -1 && !ISSET(flags, O_NONBLOCK))
rval = fcntl(fds[0], F_SETFL, flags | O_NONBLOCK);
if (rval != -1) {
flags = fcntl(fds[1], F_GETFL, 0);
if (flags != -1 && !ISSET(flags, O_NONBLOCK))
rval = fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
}
if (rval == -1) {
close(fds[0]);
close(fds[1]);
}
}
return rval;
}