execute.c revision 6bf6e43e7e214a4bd03008a91a7fc77ce6934d65
383N/A along with systemd; If not, see <http://www.gnu.org/licenses/>.
383N/A#ifdef HAVE_SELINUX
383N/A#ifdef HAVE_SECCOMP
383N/A#ifdef HAVE_APPARMOR
383N/A#include "sd-messages.h"
383N/A#include "capability.h"
383N/A#include "securebits.h"
383N/A#include "namespace.h"
383N/A#include "exit-status.h"
383N/A#include "utmp-wtmp.h"
383N/A#include "path-util.h"
383N/A#include "env-util.h"
383N/A#include "selinux-util.h"
383N/A#include "errno-list.h"
383N/A#include "smack-util.h"
383N/A#include "bus-endpoint.h"
383N/A#include "cap-list.h"
383N/A#include "formats-util.h"
383N/A#include "process-util.h"
383N/A#include "terminal-util.h"
383N/A#include "signal-util.h"
383N/A#ifdef HAVE_APPARMOR
383N/A#include "apparmor-util.h"
383N/A#ifdef HAVE_SECCOMP
383N/A#include "seccomp-util.h"
383N/A restart_from = i;
383N/A if (restart_from < 0)
383N/A o == EXEC_OUTPUT_TTY ||
383N/A o == EXEC_OUTPUT_SYSLOG_AND_CONSOLE ||
383N/A o == EXEC_OUTPUT_KMSG_AND_CONSOLE ||
383N/A goto restore_gid;
383N/Astatic int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, const char *unit_id, int nfd, uid_t uid, gid_t gid) {
383N/A i == EXEC_INPUT_TTY ||
383N/A i == EXEC_INPUT_TTY_FORCE ||
383N/A i == EXEC_INPUT_TTY_FAIL;
383N/A return EXEC_INPUT_NULL;
383N/A return EXEC_INPUT_NULL;
383N/A return EXEC_OUTPUT_INHERIT;
383N/A return std_output;
383N/A case EXEC_INPUT_NULL:
383N/A case EXEC_INPUT_TTY:
383N/A case EXEC_INPUT_TTY_FORCE:
383N/A case EXEC_INPUT_TTY_FAIL: {
383N/A i == EXEC_INPUT_TTY_FAIL,
383N/A i == EXEC_INPUT_TTY_FORCE,
383N/A r = STDIN_FILENO;
383N/A case EXEC_INPUT_SOCKET:
383N/Astatic int setup_output(Unit *unit, const ExecContext *context, int fileno, int socket_fd, const char *ident, bool apply_tty_stdin, uid_t uid, gid_t gid) {
383N/A ExecOutput o;
383N/A ExecOutput e;
383N/A if (e == EXEC_OUTPUT_INHERIT &&
383N/A o == EXEC_OUTPUT_INHERIT &&
383N/A i == EXEC_INPUT_NULL &&
383N/A if (e == o || e == EXEC_OUTPUT_INHERIT)
383N/A } else if (o == EXEC_OUTPUT_INHERIT) {
383N/A if (i != EXEC_INPUT_NULL)
383N/A case EXEC_OUTPUT_NULL:
383N/A case EXEC_OUTPUT_TTY:
383N/A if (is_terminal_input(i))
383N/A case EXEC_OUTPUT_SYSLOG:
383N/A case EXEC_OUTPUT_KMSG:
383N/A case EXEC_OUTPUT_JOURNAL:
383N/A log_unit_error_errno(unit, r, "Failed to connect %s to the journal socket, ignoring: %m", fileno == STDOUT_FILENO ? "stdout" : "stderr");
383N/A case EXEC_OUTPUT_SOCKET:
383N/A int *_saved_stdout) {
383N/A if (saved_stdin < 0)
383N/A if (saved_stdout < 0) {
383N/A int *saved_stdout) {
383N/A if (*saved_stdin >= 0)
383N/A if (*saved_stdout >= 0)
383N/A bool keep_groups = false;
383N/A keep_groups = true;
383N/A int ngroups_max, k;
383N/A if (keep_groups) {
383N/A if (k >= ngroups_max) {
383N/A if (cap_set_proc(d) < 0)
383N/A void *appdata_ptr) {
383N/A return PAM_CONV_ERR;
383N/A bool close_session = false;
383N/A close_session = true;
383N/A goto child_finish;
383N/A goto child_finish;
383N/A goto child_finish;
383N/A if (close_session)
383N/A#ifdef HAVE_SECCOMP
383N/A if (c->syscall_archs) {
383N/A if (c->address_families_whitelist) {
383N/A r = seccomp_rule_add(
383N/A r = seccomp_rule_add(
383N/A r = seccomp_rule_add(
383N/A r = seccomp_rule_add(
383N/A r = seccomp_rule_add(
if (idle_pipe[0] >= 0) {
ssize_t n;
static int build_environment(
const ExecContext *c,
unsigned n_fds,
const char *home,
const char *username,
const char *shell,
char ***ret) {
unsigned n_env = 0;
assert(c);
if (!our_env)
return -ENOMEM;
if (n_fds > 0) {
return -ENOMEM;
return -ENOMEM;
if (watchdog_usec > 0) {
return -ENOMEM;
return -ENOMEM;
if (home) {
return -ENOMEM;
if (username) {
return -ENOMEM;
return -ENOMEM;
if (shell) {
return -ENOMEM;
c->tty_path) {
return -ENOMEM;
static bool exec_needs_mount_namespace(
static int exec_child(
char **argv,
int socket_fd,
char **files_env,
int *exit_status) {
_cleanup_strv_free_ char **our_env = NULL, **pam_env = NULL, **final_env = NULL, **final_argv = NULL;
unsigned n_dont_close = 0;
bool needs_mount_namespace;
r = reset_signal_mask();
if (socket_fd >= 0)
if (n_fds > 0) {
if (runtime) {
if (setsid() < 0) {
return -errno;
char response;
if (r == -ETIMEDOUT)
write_confirm_message("Couldn't ask confirmation question, assuming positive response: %s\n", strerror(-r));
return -ECANCELED;
*exit_status = 0;
if (socket_fd >= 0)
r = setup_output(unit, context, STDOUT_FILENO, socket_fd, basename(command->path), params->apply_tty_stdin, uid, gid);
r = setup_output(unit, context, STDERR_FILENO, socket_fd, basename(command->path), params->apply_tty_stdin, uid, gid);
log_open();
log_unit_debug_errno(unit, r, "Failed to adjust OOM setting, assuming containerized execution, ignoring: %m");
log_close();
return -errno;
return -errno;
r = sched_setscheduler(0,
SCHED_RESET_ON_FORK : 0),
¶m);
return -errno;
return -errno;
return -errno;
return -errno;
return -errno;
char **rt;
_cleanup_free_ char *p;
return -ENOMEM;
#ifdef HAVE_SMACK
#ifdef SMACK_DEFAULT_PROCESS_LABEL
#ifdef HAVE_PAM
if (needs_mount_namespace) {
r = setup_namespace(
tmp,
var,
log_open();
log_unit_debug_errno(unit, r, "Failed to set up namespace, assuming containerized execution, ignoring: %m");
log_close();
return -errno;
return -errno;
return -ENOMEM;
if (chdir(d) < 0 &&
return -errno;
#ifdef HAVE_SELINUX
if (params->apply_permissions && mac_selinux_use() && params->selinux_context_net && socket_fd >= 0) {
r = mac_selinux_get_child_mls_label(socket_fd, command->path, context->selinux_context, &mac_selinux_context_net);
for (i = 0; i < _RLIMIT_MAX; i++) {
return -errno;
return -errno;
return -errno;
return -errno;
#ifdef HAVE_SECCOMP
#ifdef HAVE_SELINUX
if (mac_selinux_use()) {
if (exec_context) {
#ifdef HAVE_APPARMOR
return -errno;
NULL);
if (!final_env) {
return -ENOMEM;
if (!final_argv) {
return -ENOMEM;
if (line) {
log_open();
NULL);
log_close();
return -errno;
int socket_fd, r;
char **argv;
return -EINVAL;
if (!line)
return log_oom();
NULL);
if (pid < 0)
if (pid == 0) {
int exit_status;
argv,
&exit_status);
log_open();
NULL);
assert(c);
c->syslog_level_prefix = true;
c->ignore_sigpipe = true;
assert(c);
if (c->capabilities) {
if (c->cpuset)
assert(c);
if (!runtime_prefix)
_cleanup_free_ char *p;
return -ENOMEM;
assert(c);
exec_command_done(c+i);
ExecCommand *i;
free(i);
return NULL;
c[i] = exec_command_free_list(c[i]);
typedef struct InvalidEnvInfo {
const char *path;
char **i, **r = NULL;
assert(c);
assert(l);
char *fn;
bool ignore = false;
int count, n;
fn = *i;
ignore = true;
fn ++;
if (ignore)
strv_free(r);
return -EINVAL;
errno = 0;
if (ignore)
strv_free(r);
if (count == 0) {
if (ignore)
strv_free(r);
return -EINVAL;
for (n = 0; n < count; n++) {
if (ignore)
strv_free(r);
if (r == NULL)
strv_free(r);
strv_free(p);
return -ENOMEM;
char *console;
if (!console)
assert(f);
STRV_FOREACH(g, l)
assert(c);
assert(f);
fprintf(f,
if (c->nice_set)
fprintf(f,
if (c->oom_score_adjust_set)
fprintf(f,
for (i = 0; i < RLIM_NLIMITS; i++)
if (c->rlimit[i])
if (c->ioprio_set) {
fprintf(f,
if (c->cpu_sched_set) {
fprintf(f,
if (c->cpuset) {
for (i = 0; i < c->cpuset_ncpus; i++)
fprintf(f,
if (c->tty_path)
fprintf(f,
fprintf(f,
if (c->capabilities) {
_cleanup_cap_free_charp_ char *t;
if (c->secure_bits)
if (c->capability_bounding_set_drop) {
for (l = 0; l <= cap_last_cap(); l++)
if (c->user)
if (c->group)
if (c->pam_name)
if (c->utmp_id)
fprintf(f,
if (c->selinux_context)
fprintf(f,
fprintf(f,
if (c->syscall_filter) {
#ifdef HAVE_SECCOMP
Iterator j;
void *id;
bool first = true;
fprintf(f,
prefix);
if (!c->syscall_whitelist)
#ifdef HAVE_SECCOMP
if (first)
first = false;
if (c->syscall_archs) {
#ifdef HAVE_SECCOMP
Iterator j;
void *id;
fprintf(f,
prefix);
#ifdef HAVE_SECCOMP
if (c->syscall_errno != 0)
fprintf(f,
if (c->apparmor_profile)
fprintf(f,
assert(c);
if (!c->user)
assert(s);
zero(*s);
assert(s);
zero(*s);
if (context) {
assert(s);
assert(f);
if (s->pid <= 0)
fprintf(f,
fprintf(f,
fprintf(f,
size_t k;
bool first = true;
if (!(n = new(char, k)))
return NULL;
if (!first)
first = false;
p = stpcpy(p, *a);
p = stpcpy(p, *a);
const char *prefix2;
assert(c);
assert(f);
fprintf(f,
assert(f);
assert(l);
assert(e);
assert(c);
return -ENOMEM;
strv_free(l);
return -ENOMEM;
c->path = p;
c->argv = l;
assert(c);
return -ENOMEM;
if (*rt)
if (!*rt)
return -ENOMEM;
assert(c);
if (*rt)
return -errno;
assert(r);
r->n_ref++;
return NULL;
r->n_ref--;
if (r->n_ref > 0)
return NULL;
free(r);
return NULL;
assert(u);
assert(f);
if (!rt)
int copy;
if (copy < 0)
return copy;
int copy;
if (copy < 0)
return copy;
int exec_runtime_deserialize_item(Unit *u, ExecRuntime **rt, const char *key, const char *value, FDSet *fds) {
char *copy;
return log_oom();
if (!copy)
return log_oom();
char *copy;
return log_oom();
if (!copy)
return log_oom();
int fd;
return log_oom();
int fd;
return log_oom();
static void *remove_tmpdir_thread(void *p) {
return NULL;
if (!rt)