#include <config.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_PRIV_SET
# include <priv.h>
#endif
#include <errno.h>
#include <sudo.h>
#include <sudo_exec.h>
char **
disable_execute(char *envp[], const char *dso)
{
debug_decl(disable_execute, SUDO_DEBUG_UTIL);
#ifdef HAVE_PRIV_SET
(void)priv_set(PRIV_ON, PRIV_INHERITABLE, "PRIV_FILE_DAC_READ", NULL);
(void)priv_set(PRIV_ON, PRIV_INHERITABLE, "PRIV_FILE_DAC_WRITE", NULL);
(void)priv_set(PRIV_ON, PRIV_INHERITABLE, "PRIV_FILE_DAC_SEARCH", NULL);
if (priv_set(PRIV_OFF, PRIV_LIMIT, "PRIV_PROC_EXEC", NULL) == 0)
debug_return_ptr(envp);
sudo_warn("%s", U_("unable to remove PRIV_PROC_EXEC from PRIV_LIMIT"));
#endif
#ifdef RTLD_PRELOAD_VAR
if (dso != NULL)
envp = sudo_preload_dso(envp, dso, -1);
#endif
debug_return_ptr(envp);
}
static char **
enable_intercept(char *envp[], const char *dso, int intercept_fd)
{
debug_decl(enable_intercept, SUDO_DEBUG_UTIL);
if (dso != NULL) {
#ifdef RTLD_PRELOAD_VAR
if (intercept_fd == -1)
sudo_fatalx("%s: no intercept fd", __func__);
envp = sudo_preload_dso(envp, dso, intercept_fd);
#else
if (intercept_fd != -1)
close(intercept_fd);
#endif
}
debug_return_ptr(envp);
}
int
sudo_execve(int fd, const char *path, char *const argv[], char *envp[],
int intercept_fd, unsigned int flags)
{
debug_decl(sudo_execve, SUDO_DEBUG_UTIL);
sudo_debug_execve(SUDO_DEBUG_INFO, path, argv, envp);
if (ISSET(flags, CD_NOEXEC))
envp = disable_execute(envp, sudo_conf_noexec_path());
if (ISSET(flags, CD_INTERCEPT|CD_LOG_SUBCMDS)) {
if (!ISSET(flags, CD_USE_PTRACE)) {
envp = enable_intercept(envp, sudo_conf_intercept_path(),
intercept_fd);
}
}
#ifdef HAVE_FEXECVE
if (fd != -1)
fexecve(fd, argv, envp);
else
#endif
execve(path, argv, envp);
if (fd == -1 && errno == ENOEXEC) {
int argc;
const char **nargv;
for (argc = 0; argv[argc] != NULL; argc++)
continue;
nargv = reallocarray(NULL, (size_t)argc + 2, sizeof(char *));
if (nargv != NULL) {
nargv[0] = "sh";
nargv[1] = path;
memcpy(nargv + 2, argv + 1, (size_t)argc * sizeof(char *));
execve(_PATH_SUDO_BSHELL, (char **)nargv, envp);
free(nargv);
}
}
debug_return_int(-1);
}