X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fcore%2Fexecute.c;h=91e4352f9a0a44cb045b16fb96561d03fa1a530b;hb=cccb0b2cdbd25e90ae92d2d5b107125cb1ca3433;hp=5abc69dec4e6de7fcf146ea64fe9772a98cf72fe;hpb=531dca789ea6b4c269ca2646515b42962f83d64a;p=elogind.git diff --git a/src/core/execute.c b/src/core/execute.c index 5abc69dec..91e4352f9 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -650,14 +650,13 @@ static int enforce_groups(const ExecContext *context, const char *username, gid_ } static int enforce_user(const ExecContext *context, uid_t uid) { - int r; assert(context); /* Sets (but doesn't lookup) the uid and make sure we keep the * capabilities while doing so. */ if (context->capabilities) { - cap_t d; + _cleanup_cap_free_ cap_t d = NULL; static const cap_value_t bits[] = { CAP_SETUID, /* Necessary so that we can run setresuid() below */ CAP_SETPCAP /* Necessary so that we can set PR_SET_SECUREBITS later on */ @@ -677,23 +676,16 @@ static int enforce_user(const ExecContext *context, uid_t uid) { /* Second step: set the capabilities. This will reduce * the capabilities to the minimum we need. */ - if (!(d = cap_dup(context->capabilities))) + d = cap_dup(context->capabilities); + if (!d) return -errno; if (cap_set_flag(d, CAP_EFFECTIVE, ELEMENTSOF(bits), bits, CAP_SET) < 0 || - cap_set_flag(d, CAP_PERMITTED, ELEMENTSOF(bits), bits, CAP_SET) < 0) { - r = -errno; - cap_free(d); - return r; - } - - if (cap_set_proc(d) < 0) { - r = -errno; - cap_free(d); - return r; - } + cap_set_flag(d, CAP_PERMITTED, ELEMENTSOF(bits), bits, CAP_SET) < 0) + return -errno; - cap_free(d); + if (cap_set_proc(d) < 0) + return -errno; } /* Third step: actually set the uids */ @@ -1017,6 +1009,92 @@ static void do_idle_pipe_dance(int idle_pipe[4]) { close_nointr_nofail(idle_pipe[3]); } +static int build_environment( + ExecContext *c, + unsigned n_fds, + usec_t watchdog_usec, + const char *home, + const char *username, + const char *shell, + char ***ret) { + + _cleanup_strv_free_ char **our_env = NULL; + unsigned n_env = 0; + char *x; + + assert(c); + assert(ret); + + our_env = new0(char*, 10); + if (!our_env) + return -ENOMEM; + + if (n_fds > 0) { + if (asprintf(&x, "LISTEN_PID="PID_FMT, getpid()) < 0) + return -ENOMEM; + our_env[n_env++] = x; + + if (asprintf(&x, "LISTEN_FDS=%u", n_fds) < 0) + return -ENOMEM; + our_env[n_env++] = x; + } + + if (watchdog_usec > 0) { + if (asprintf(&x, "WATCHDOG_PID="PID_FMT, getpid()) < 0) + return -ENOMEM; + our_env[n_env++] = x; + + if (asprintf(&x, "WATCHDOG_USEC=%llu", (unsigned long long) watchdog_usec) < 0) + return -ENOMEM; + our_env[n_env++] = x; + } + + if (home) { + x = strappend("HOME=", home); + if (!x) + return -ENOMEM; + our_env[n_env++] = x; + } + + if (username) { + x = strappend("LOGNAME=", username); + if (!x) + return -ENOMEM; + our_env[n_env++] = x; + + x = strappend("USER=", username); + if (!x) + return -ENOMEM; + our_env[n_env++] = x; + } + + if (shell) { + x = strappend("SHELL=", shell); + if (!x) + return -ENOMEM; + our_env[n_env++] = x; + } + + if (is_terminal_input(c->std_input) || + c->std_output == EXEC_OUTPUT_TTY || + c->std_error == EXEC_OUTPUT_TTY || + c->tty_path) { + + x = strdup(default_term_for_tty(tty_path(c))); + if (!x) + return -ENOMEM; + our_env[n_env++] = x; + } + + our_env[n_env++] = NULL; + assert(n_env <= 10); + + *ret = our_env; + our_env = NULL; + + return 0; +} + int exec_spawn(ExecCommand *command, char **argv, ExecContext *context, @@ -1029,6 +1107,7 @@ int exec_spawn(ExecCommand *command, CGroupControllerMask cgroup_supported, const char *cgroup_path, const char *unit_id, + usec_t watchdog_usec, int idle_pipe[4], ExecRuntime *runtime, pid_t *ret) { @@ -1089,7 +1168,7 @@ int exec_spawn(ExecCommand *command, if (pid == 0) { _cleanup_strv_free_ char **our_env = NULL, **pam_env = NULL, **final_env = NULL, **final_argv = NULL; const char *username = NULL, *home = NULL, *shell = NULL; - unsigned n_dont_close = 0, n_env = 0; + unsigned n_dont_close = 0; int dont_close[n_fds + 3]; uid_t uid = (uid_t) -1; gid_t gid = (gid_t) -1; @@ -1348,7 +1427,8 @@ int exec_spawn(ExecCommand *command, !strv_isempty(context->read_only_dirs) || !strv_isempty(context->inaccessible_dirs) || context->mount_flags != 0 || - (context->private_tmp && runtime && (runtime->tmp_dir || runtime->var_tmp_dir))) { + (context->private_tmp && runtime && (runtime->tmp_dir || runtime->var_tmp_dir)) || + context->private_devices) { char *tmp = NULL, *var = NULL; @@ -1371,6 +1451,7 @@ int exec_spawn(ExecCommand *command, context->inaccessible_dirs, tmp, var, + context->private_devices, context->mount_flags); if (err < 0) { @@ -1485,29 +1566,12 @@ int exec_spawn(ExecCommand *command, } } - our_env = new(char*, 8); - if (!our_env || - (n_fds > 0 && ( - asprintf(our_env + n_env++, "LISTEN_PID=%lu", (unsigned long) getpid()) < 0 || - asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0)) || - (home && asprintf(our_env + n_env++, "HOME=%s", home) < 0) || - (username && ( - asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 || - asprintf(our_env + n_env++, "USER=%s", username) < 0)) || - (shell && asprintf(our_env + n_env++, "SHELL=%s", shell) < 0) || - ((is_terminal_input(context->std_input) || - context->std_output == EXEC_OUTPUT_TTY || - context->std_error == EXEC_OUTPUT_TTY) && ( - !(our_env[n_env++] = strdup(default_term_for_tty(tty_path(context))))))) { - - err = -ENOMEM; + err = build_environment(context, n_fds, watchdog_usec, home, username, shell, &our_env); + if (r < 0) { r = EXIT_MEMORY; goto fail_child; } - our_env[n_env++] = NULL; - assert(n_env <= 8); - final_env = strv_env_merge(5, environment, our_env, @@ -1566,8 +1630,8 @@ int exec_spawn(ExecCommand *command, log_struct_unit(LOG_DEBUG, unit_id, - "MESSAGE=Forked %s as %lu", - command->path, (unsigned long) pid, + "MESSAGE=Forked %s as "PID_FMT, + command->path, pid, NULL); /* We add the new process to the cgroup both in the child (so @@ -1834,6 +1898,7 @@ void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) { "%sNonBlocking: %s\n" "%sPrivateTmp: %s\n" "%sPrivateNetwork: %s\n" + "%sPrivateDevices: %s\n" "%sIgnoreSIGPIPE: %s\n", prefix, c->umask, prefix, c->working_directory ? c->working_directory : "/", @@ -1841,6 +1906,7 @@ void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) { prefix, yes_no(c->non_blocking), prefix, yes_no(c->private_tmp), prefix, yes_no(c->private_network), + prefix, yes_no(c->private_devices), prefix, yes_no(c->ignore_sigpipe)); STRV_FOREACH(e, c->environment) @@ -1904,12 +1970,12 @@ void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) { fprintf(f, "%sCPUAffinity:", prefix); for (i = 0; i < c->cpuset_ncpus; i++) if (CPU_ISSET_S(i, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset)) - fprintf(f, " %i", i); + fprintf(f, " %u", i); fputs("\n", f); } if (c->timer_slack_nsec != (nsec_t) -1) - fprintf(f, "%sTimerSlackNSec: %lu\n", prefix, (unsigned long)c->timer_slack_nsec); + fprintf(f, "%sTimerSlackNSec: "NSEC_FMT "\n", prefix, c->timer_slack_nsec); fprintf(f, "%sStandardInput: %s\n" @@ -1930,37 +1996,37 @@ void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) { prefix, yes_no(c->tty_vhangup), prefix, yes_no(c->tty_vt_disallocate)); - if (c->std_output == EXEC_OUTPUT_SYSLOG || c->std_output == EXEC_OUTPUT_KMSG || c->std_output == EXEC_OUTPUT_JOURNAL || - c->std_output == EXEC_OUTPUT_SYSLOG_AND_CONSOLE || c->std_output == EXEC_OUTPUT_KMSG_AND_CONSOLE || c->std_output == EXEC_OUTPUT_JOURNAL_AND_CONSOLE || - c->std_error == EXEC_OUTPUT_SYSLOG || c->std_error == EXEC_OUTPUT_KMSG || c->std_error == EXEC_OUTPUT_JOURNAL || - c->std_error == EXEC_OUTPUT_SYSLOG_AND_CONSOLE || c->std_error == EXEC_OUTPUT_KMSG_AND_CONSOLE || c->std_error == EXEC_OUTPUT_JOURNAL_AND_CONSOLE) { - char *fac_str, *lvl_str; - int r; + if (c->std_output == EXEC_OUTPUT_SYSLOG || + c->std_output == EXEC_OUTPUT_KMSG || + c->std_output == EXEC_OUTPUT_JOURNAL || + c->std_output == EXEC_OUTPUT_SYSLOG_AND_CONSOLE || + c->std_output == EXEC_OUTPUT_KMSG_AND_CONSOLE || + c->std_output == EXEC_OUTPUT_JOURNAL_AND_CONSOLE || + c->std_error == EXEC_OUTPUT_SYSLOG || + c->std_error == EXEC_OUTPUT_KMSG || + c->std_error == EXEC_OUTPUT_JOURNAL || + c->std_error == EXEC_OUTPUT_SYSLOG_AND_CONSOLE || + c->std_error == EXEC_OUTPUT_KMSG_AND_CONSOLE || + c->std_error == EXEC_OUTPUT_JOURNAL_AND_CONSOLE) { - r = log_facility_unshifted_to_string_alloc(c->syslog_priority >> 3, &fac_str); - if (r < 0) - fac_str = NULL; + _cleanup_free_ char *fac_str = NULL, *lvl_str = NULL; - r = log_level_to_string_alloc(LOG_PRI(c->syslog_priority), &lvl_str); - if (r < 0) - lvl_str = NULL; + log_facility_unshifted_to_string_alloc(c->syslog_priority >> 3, &fac_str); + log_level_to_string_alloc(LOG_PRI(c->syslog_priority), &lvl_str); fprintf(f, "%sSyslogFacility: %s\n" "%sSyslogLevel: %s\n", prefix, strna(fac_str), prefix, strna(lvl_str)); - free(lvl_str); - free(fac_str); } if (c->capabilities) { - char *t; - if ((t = cap_to_text(c->capabilities, NULL))) { - fprintf(f, "%sCapabilities: %s\n", - prefix, t); - cap_free(t); - } + _cleanup_cap_free_charp_ char *t; + + t = cap_to_text(c->capabilities, NULL); + if (t) + fprintf(f, "%sCapabilities: %s\n", prefix, t); } if (c->secure_bits) @@ -1979,12 +2045,11 @@ void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) { for (l = 0; l <= cap_last_cap(); l++) if (!(c->capability_bounding_set_drop & ((uint64_t) 1ULL << (uint64_t) l))) { - char *t; + _cleanup_cap_free_charp_ char *t; - if ((t = cap_to_name(l))) { + t = cap_to_name(l); + if (t) fprintf(f, " %s", t); - cap_free(t); - } } fputs("\n", f); @@ -2069,8 +2134,8 @@ void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) { return; fprintf(f, - "%sPID: %lu\n", - prefix, (unsigned long) s->pid); + "%sPID: "PID_FMT"\n", + prefix, s->pid); if (s->start_timestamp.realtime > 0) fprintf(f, @@ -2212,7 +2277,7 @@ static int exec_runtime_allocate(ExecRuntime **rt) { return 0; *rt = new0(ExecRuntime, 1); - if (!rt) + if (!*rt) return -ENOMEM; (*rt)->n_ref = 1;