execute.c revision 8e27452380193a5f81bfd08a59aab8b07008ba0b
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <grp.h>
#include <pwd.h>
#include "execute.h"
#include "strv.h"
#include "macro.h"
#include "util.h"
#include "log.h"
#include "ioprio.h"
#include "securebits.h"
#include "cgroup.h"
DIR *d;
int r = 0;
/* Modifies the fds array! (sorts it) */
return -errno;
int fd;
continue;
goto finish;
if (fd < 3)
continue;
continue;
if (except) {
bool found;
unsigned i;
found = false;
for (i = 0; i < n_except; i++)
found = true;
break;
}
if (found)
continue;
}
if ((r = close_nointr(fd)) < 0)
goto finish;
}
closedir(d);
return r;
}
int start, restart_from;
if (n_fds <= 0)
return 0;
start = 0;
for (;;) {
int i;
restart_from = -1;
int nfd;
/* Already at right index? */
if (fds[i] == i+3)
continue;
return -errno;
/* Hmm, the fd we wanted isn't free? Then
* let's remember that and try again from here*/
restart_from = i;
}
if (restart_from < 0)
break;
}
return 0;
}
unsigned i;
if (n_fds <= 0)
return 0;
for (i = 0; i < n_fds; i++) {
int flags;
return -errno;
if (nonblock)
flags |= O_NONBLOCK;
else
flags &= ~O_NONBLOCK;
return -errno;
/* We unconditionally drop FD_CLOEXEC from the fds,
* since after all we want to pass these fds to our
* children */
return -errno;
return -errno;
}
return 0;
}
int nfd;
return -errno;
return -EIO;
}
return 0;
}
int r;
case EXEC_OUTPUT_CONSOLE:
return 0;
case EXEC_OUTPUT_NULL:
return r;
return 0;
case EXEC_OUTPUT_KERNEL:
case EXEC_OUTPUT_SYSLOG: {
int fd;
union {
struct sockaddr_un un;
} sa;
return -errno;
if (fd != STDOUT_FILENO) {
return -EIO;
}
return -errno;
}
return -errno;
}
return -errno;
}
if (fd != STDERR_FILENO) {
return -EIO;
}
/* We speak a very simple protocol between log server
* and client: one line for the log destination (kmsg
* or syslog), followed by the priority field,
* followed by the process name. Since we replaced
* it. Note that we use stderr, to minimize buffer
* flushing issues. */
"%s\n"
"%i\n"
"%s\n",
return 0;
}
default:
assert_not_reached("Unknown output type");
}
}
int r;
case EXEC_INPUT_CONSOLE:
return 0;
case EXEC_INPUT_NULL:
return r;
return 0;
default:
assert_not_reached("Unknown input type");
}
}
struct group *g;
unsigned long lu;
/* We enforce some special rules for gid=0: in order to avoid
* NSS lookups for root we hardcode its data. */
*gid = 0;
return 0;
}
errno = 0;
} else {
errno = 0;
}
if (!g)
return 0;
}
struct passwd *p;
unsigned long lu;
/* We enforce some special rules for uid=0: in order to avoid
* NSS lookups for root we hardcode its data. */
*username = "root";
*uid = 0;
*gid = 0;
*home = "/root";
return 0;
}
errno = 0;
/* If there are multiple users with the same id, make
* sure to leave $USER to the configured value instead
* of the first occurence in the database. However if
* the uid was configured by a numeric uid, then let's
if (*username && p)
} else {
errno = 0;
}
if (!p)
return 0;
}
bool keep_groups = false;
int r;
/* Lookup and ser GID and supplementary group list. Here too
* we avoid NSS lookups for gid=0. */
return r;
return -errno;
keep_groups = true;
}
/* Second step, set our gids */
return -errno;
}
if (context->supplementary_groups) {
int ngroups_max, k;
char **i;
/* Final step, initialize any manually set supplementary groups */
return -ENOMEM;
if (keep_groups) {
return -errno;
}
} else
k = 0;
if (k >= ngroups_max) {
return -E2BIG;
}
if ((r = get_group_creds(*i, gids+k)) < 0) {
return r;
}
k++;
}
return -errno;
}
}
return 0;
}
int r;
/* Sets (but doesn't lookup) the uid and make sure we keep the
* capabilities while doing so. */
if (context->capabilities) {
cap_t d;
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 */
};
/* First step: If we need to keep capabilities but
* drop privileges we need to make sure we keep our
* caps, whiel we drop priviliges. */
if (uid != 0) {
return -errno;
}
/* Second step: set the capabilites. This will reduce
* the capabilities to the minimum we need. */
return -errno;
r = -errno;
cap_free(d);
return r;
}
if (cap_set_proc(d) < 0) {
r = -errno;
cap_free(d);
return r;
}
cap_free(d);
}
/* Third step: actually set the uids */
return -errno;
/* At this point we should have all necessary capabilities but
are otherwise a normal user. However, the caps might got
corrupted due to the setresuid() so we need clean them up
later. This is done outside of this call. */
return 0;
}
const ExecContext *context,
bool apply_permissions,
bool apply_chroot,
int r;
if (cgroup_bondings)
if ((r = cgroup_bonding_realize_list(cgroup_bondings)))
return r;
return -errno;
if (pid == 0) {
int i;
unsigned n_env = 0;
/* child */
if (sigemptyset(&ss) < 0 ||
r = EXIT_SIGNAL_MASK;
goto fail;
}
if (setpgid(0, 0) < 0) {
r = EXIT_PGID;
goto fail;
}
if (setup_input(context) < 0) {
r = EXIT_INPUT;
goto fail;
}
r = EXIT_OUTPUT;
goto fail;
}
if (cgroup_bondings)
if ((r = cgroup_bonding_install_list(cgroup_bondings, 0)) < 0) {
r = EXIT_CGROUP;
goto fail;
}
if (context->oom_adjust_set) {
char t[16];
char_array_0(t);
if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
r = EXIT_OOM_ADJUST;
goto fail;
}
}
r = EXIT_NICE;
goto fail;
}
if (context->cpu_sched_set) {
struct sched_param param;
r = EXIT_SETSCHEDULER;
goto fail;
}
}
if (context->cpu_affinity_set)
r = EXIT_CPUAFFINITY;
goto fail;
}
if (context->ioprio_set)
r = EXIT_IOPRIO;
goto fail;
}
if (context->timer_slack_ns_set)
r = EXIT_TIMERSLACK;
goto fail;
}
r = EXIT_USER;
goto fail;
}
}
if (apply_permissions)
r = EXIT_GROUP;
goto fail;
}
if (apply_chroot) {
if (context->root_directory)
r = EXIT_CHROOT;
goto fail;
}
r = EXIT_CHDIR;
goto fail;
}
} else {
char *d;
if (asprintf(&d, "%s/%s",
r = EXIT_MEMORY;
goto fail;
}
if (chdir(d) < 0) {
free(d);
r = EXIT_CHDIR;
goto fail;
}
free(d);
}
r = EXIT_FDS;
goto fail;
}
if (apply_permissions) {
for (i = 0; i < RLIMIT_NLIMITS; i++) {
continue;
r = EXIT_LIMITS;
goto fail;
}
}
r = EXIT_USER;
goto fail;
}
/* PR_GET_SECUREBITS is not priviliged, while
* PR_SET_SECUREBITS is. So to suppress
* potential EPERMs we'll try not to call
* PR_SET_SECUREBITS unless necessary. */
r = EXIT_SECUREBITS;
goto fail;
}
if (context->capabilities)
r = EXIT_CAPABILITIES;
goto fail;
}
}
r = EXIT_MEMORY;
goto fail;
}
if (n_fds > 0)
r = EXIT_MEMORY;
goto fail;
}
if (home)
r = EXIT_MEMORY;
goto fail;
}
if (username)
r = EXIT_MEMORY;
goto fail;
}
r = EXIT_MEMORY;
goto fail;
}
r = EXIT_EXEC;
fail:
_exit(r);
}
return 0;
}
void exec_context_init(ExecContext *c) {
assert(c);
c->umask = 0002;
c->oom_adjust = 0;
c->oom_adjust_set = false;
c->nice = 0;
c->nice_set = false;
c->ioprio_set = false;
c->cpu_sched_policy = SCHED_OTHER;
c->cpu_sched_priority = 0;
c->cpu_sched_set = false;
CPU_ZERO(&c->cpu_affinity);
c->cpu_affinity_set = false;
c->input = 0;
c->output = 0;
c->secure_bits = 0;
c->capability_bounding_set_drop = 0;
}
void exec_context_done(ExecContext *c) {
unsigned l;
assert(c);
strv_free(c->environment);
c->environment = NULL;
for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
}
free(c->working_directory);
c->working_directory = NULL;
free(c->root_directory);
c->root_directory = NULL;
free(c->syslog_identifier);
c->syslog_identifier = NULL;
c->supplementary_groups = NULL;
if (c->capabilities) {
cap_free(c->capabilities);
c->capabilities = NULL;
}
}
void exec_command_free_list(ExecCommand *c) {
ExecCommand *i;
while ((i = c)) {
free(i);
}
}
void exec_command_free_array(ExecCommand **c, unsigned n) {
unsigned i;
for (i = 0; i < n; i++) {
exec_command_free_list(c[i]);
c[i] = NULL;
}
}
char ** e;
unsigned i;
assert(c);
assert(f);
if (!prefix)
prefix = "";
fprintf(f,
"%sUMask: %04o\n"
"%sWorkingDirectory: %s\n"
"%sRootDirectory: %s\n"
"%sNonBlocking: %s\n",
if (c->environment)
for (e = c->environment; *e; e++)
if (c->nice_set)
fprintf(f,
"%sNice: %i\n",
if (c->oom_adjust_set)
fprintf(f,
"%sOOMAdjust: %i\n",
prefix, c->oom_adjust);
for (i = 0; i < RLIM_NLIMITS; i++)
if (c->rlimit[i])
fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max);
if (c->ioprio_set)
fprintf(f,
"%sIOSchedulingClass: %s\n"
"%sIOPriority: %i\n",
if (c->cpu_sched_set)
fprintf(f,
"%sCPUSchedulingPolicy: %s\n"
"%sCPUSchedulingPriority: %i\n"
"%sCPUSchedulingResetOnFork: %s\n",
prefix, c->cpu_sched_priority,
if (c->cpu_affinity_set) {
for (i = 0; i < CPU_SETSIZE; i++)
if (CPU_ISSET(i, &c->cpu_affinity))
fprintf(f, " %i", i);
fputs("\n", f);
}
if (c->timer_slack_ns_set)
fprintf(f,
"%sInput: %s\n"
"%sOutput: %s\n",
fprintf(f,
"%sSyslogFacility: %s\n"
"%sSyslogLevel: %s\n",
if (c->capabilities) {
char *t;
fprintf(f, "%sCapabilities: %s\n",
prefix, t);
cap_free(t);
}
}
if (c->secure_bits)
fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n",
if (c->capability_bounding_set_drop) {
for (i = 0; i <= CAP_LAST_CAP; i++)
if (c->capability_bounding_set_drop & (1 << i)) {
char *t;
if ((t = cap_to_name(i))) {
fprintf(f, " %s", t);
free(t);
}
}
fputs("\n", f);
}
if (c->user)
if (c->group)
if (c->supplementary_groups) {
char **g;
STRV_FOREACH(g, c->supplementary_groups)
fprintf(f, " %s", *g);
fputs("\n", f);
}
}
assert(s);
}
char *exec_command_line(ExecCommand *c) {
size_t k;
char *n, *p, **a;
bool first = true;
assert(c);
k = 1;
STRV_FOREACH(a, c->argv)
k += strlen(*a)+3;
if (!(n = new(char, k)))
return NULL;
p = n;
STRV_FOREACH(a, c->argv) {
if (!first)
*(p++) = ' ';
else
first = false;
if (strpbrk(*a, WHITESPACE)) {
*(p++) = '\'';
p = stpcpy(p, *a);
*(p++) = '\'';
} else
p = stpcpy(p, *a);
}
*p = 0;
/* FIXME: this doesn't really handle arguments that have
* spaces and ticks in them */
return n;
}
char *cmd;
assert(c);
assert(f);
if (!prefix)
prefix = "";
cmd = exec_command_line(c);
fprintf(f,
"%sCommand Line: %s\n",
}
assert(f);
if (!prefix)
prefix = "";
LIST_FOREACH(command, c, c)
exec_command_dump(c, f, prefix);
}
assert(l);
assert(e);
if (*l) {
/* It's kinda important that we keep the order here */
} else
*l = e;
}
static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
[EXEC_OUTPUT_CONSOLE] = "console",
[EXEC_OUTPUT_NULL] = "null",
[EXEC_OUTPUT_SYSLOG] = "syslog",
[EXEC_OUTPUT_KERNEL] = "kernel"
};
static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
[EXEC_INPUT_NULL] = "null",
[EXEC_INPUT_CONSOLE] = "console"
};