1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
10 #include <sys/socket.h>
12 #include <sys/prctl.h>
21 #include "securebits.h"
23 static int close_fds(int except[], unsigned n_except) {
28 /* Modifies the fds array! (sorts it) */
30 if (!(d = opendir("/proc/self/fd")))
33 while ((de = readdir(d))) {
36 if (de->d_name[0] == '.')
39 if ((r = safe_atoi(de->d_name, &fd)) < 0)
53 for (i = 0; i < n_except; i++)
54 if (except[i] == fd) {
63 if ((r = close_nointr(fd)) < 0)
72 static int shift_fds(int fds[], unsigned n_fds) {
73 int start, restart_from;
86 for (i = start; i < (int) n_fds; i++) {
89 /* Already at right index? */
93 if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
96 assert_se(close_nointr(fds[i]) == 0);
99 /* Hmm, the fd we wanted isn't free? Then
100 * let's remember that and try again from here*/
101 if (nfd != i+3 && restart_from < 0)
105 if (restart_from < 0)
108 start = restart_from;
114 static int flags_fds(int fds[], unsigned n_fds) {
122 /* Drops O_NONBLOCK and FD_CLOEXEC from the file flags */
124 for (i = 0; i < n_fds; i++) {
127 if ((flags = fcntl(fds[i], F_GETFL, 0)) < 0)
130 /* Since we are at it, let's make sure that nobody
131 * forgot setting O_NONBLOCK for all our fds */
133 if (fcntl(fds[i], F_SETFL, flags &~O_NONBLOCK) < 0)
136 if ((flags = fcntl(fds[i], F_GETFD, 0)) < 0)
139 /* Also make sure nobody forgot O_CLOEXEC for all our
141 if (fcntl(fds[i], F_SETFD, flags &~FD_CLOEXEC) < 0)
148 static int replace_null_fd(int fd, int flags) {
154 if ((nfd = open("/dev/null", flags|O_NOCTTY)) < 0)
158 close_nointr_nofail(nfd);
165 static int setup_output(const ExecContext *context, const char *ident) {
170 switch (context->output) {
172 case EXEC_OUTPUT_CONSOLE:
175 case EXEC_OUTPUT_NULL:
177 if ((r = replace_null_fd(STDOUT_FILENO, O_WRONLY)) < 0 ||
178 (r = replace_null_fd(STDERR_FILENO, O_WRONLY)) < 0)
183 case EXEC_OUTPUT_KERNEL:
184 case EXEC_OUTPUT_SYSLOG: {
189 struct sockaddr_un un;
192 close_nointr(STDOUT_FILENO);
193 close_nointr(STDERR_FILENO);
195 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
198 if (fd != STDOUT_FILENO) {
199 close_nointr_nofail(fd);
204 sa.sa.sa_family = AF_UNIX;
205 strncpy(sa.un.sun_path+1, LOGGER_SOCKET, sizeof(sa.un.sun_path)-1);
207 if (connect(fd, &sa.sa, sizeof(sa)) < 0) {
208 close_nointr_nofail(fd);
212 if (shutdown(fd, SHUT_RD) < 0) {
213 close_nointr_nofail(fd);
217 if ((fd = dup(fd)) < 0) {
218 close_nointr_nofail(fd);
222 if (fd != STDERR_FILENO) {
223 close_nointr_nofail(fd);
227 /* We speak a very simple protocol between log server
228 * and client: one line for the log destination (kmsg
229 * or syslog), followed by the priority field,
230 * followed by the process name. Since we replaced
231 * stdin/stderr we simple use stdio to write to
232 * it. Note that we use stderr, to minimize buffer
233 * flushing issues. */
239 context->output == EXEC_OUTPUT_KERNEL ? "kmsg" : "syslog",
240 context->syslog_priority,
241 context->syslog_identifier ? context->syslog_identifier : ident);
247 assert_not_reached("Unknown output type");
251 int setup_input(const ExecContext *context) {
256 switch (context->input) {
258 case EXEC_INPUT_CONSOLE:
261 case EXEC_INPUT_NULL:
262 if ((r = replace_null_fd(STDIN_FILENO, O_RDONLY)) < 0)
268 assert_not_reached("Unknown input type");
272 int exec_spawn(const ExecCommand *command, const ExecContext *context, int *fds, unsigned n_fds, pid_t *ret) {
278 assert(fds || n_fds <= 0);
280 log_debug("about to execute %s", command->path);
282 if ((pid = fork()) < 0)
286 char **e, **f = NULL;
292 if (sigemptyset(&ss) < 0 ||
293 sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
294 r = EXIT_SIGNAL_MASK;
298 if (setpgid(0, 0) < 0) {
303 umask(context->umask);
305 if (setup_input(context) < 0) {
310 if (setup_output(context, file_name_from_path(command->path)) < 0) {
315 if (context->oom_adjust_set) {
318 snprintf(t, sizeof(t), "%i", context->oom_adjust);
321 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
327 if (context->root_directory)
328 if (chroot(context->root_directory) < 0) {
333 if (chdir(context->working_directory ? context->working_directory : "/") < 0) {
338 if (context->nice_set)
339 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
344 if (context->cpu_sched_set) {
345 struct sched_param param;
348 param.sched_priority = context->cpu_sched_priority;
350 if (sched_setscheduler(0, context->cpu_sched_policy |
351 (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), ¶m) < 0) {
352 r = EXIT_SETSCHEDULER;
357 if (context->cpu_affinity_set)
358 if (sched_setaffinity(0, sizeof(context->cpu_affinity), &context->cpu_affinity) < 0) {
359 r = EXIT_CPUAFFINITY;
363 if (context->ioprio_set)
364 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) {
369 if (context->timer_slack_ns_set)
370 if (prctl(PR_SET_TIMERSLACK, context->timer_slack_ns_set) < 0) {
375 if (close_fds(fds, n_fds) < 0 ||
376 shift_fds(fds, n_fds) < 0 ||
377 flags_fds(fds, n_fds) < 0) {
382 for (i = 0; i < RLIMIT_NLIMITS; i++) {
383 if (!context->rlimit[i])
386 if (setrlimit(i, context->rlimit[i]) < 0) {
392 if (context->secure_bits) {
393 if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) {
401 char *listen_env[3] = {
407 snprintf(a, sizeof(a), "LISTEN_PID=%llu", (unsigned long long) getpid());
408 snprintf(b, sizeof(b), "LISTEN_FDS=%u", n_fds);
413 if (context->environment) {
414 if (!(f = strv_merge(listen_env, context->environment))) {
423 e = context->environment;
425 execve(command->path, command->argv, e);
434 log_debug("executed %s as %llu", command->path, (unsigned long long) pid);
440 void exec_context_init(ExecContext *c) {
445 c->oom_adjust_set = false;
448 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
449 c->ioprio_set = false;
450 c->cpu_sched_policy = SCHED_OTHER;
451 c->cpu_sched_priority = 0;
452 c->cpu_sched_set = false;
453 CPU_ZERO(&c->cpu_affinity);
454 c->cpu_affinity_set = false;
458 c->syslog_priority = LOG_DAEMON|LOG_INFO;
461 c->capability_bounding_set_drop = 0;
464 void exec_context_done(ExecContext *c) {
469 strv_free(c->environment);
470 c->environment = NULL;
472 for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
477 free(c->working_directory);
478 c->working_directory = NULL;
479 free(c->root_directory);
480 c->root_directory = NULL;
482 free(c->syslog_identifier);
483 c->syslog_identifier = NULL;
491 strv_free(c->supplementary_groups);
492 c->supplementary_groups = NULL;
494 if (c->capabilities) {
495 cap_free(c->capabilities);
496 c->capabilities = NULL;
500 void exec_command_free_list(ExecCommand *c) {
504 LIST_REMOVE(ExecCommand, command, c, i);
512 void exec_command_free_array(ExecCommand **c, unsigned n) {
515 for (i = 0; i < n; i++) {
516 exec_command_free_list(c[i]);
521 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
533 "%sWorkingDirectory: %s\n"
534 "%sRootDirectory: %s\n",
536 prefix, c->working_directory ? c->working_directory : "/",
537 prefix, c->root_directory ? c->root_directory : "/");
540 for (e = c->environment; *e; e++)
541 fprintf(f, "%sEnvironment: %s\n", prefix, *e);
548 if (c->oom_adjust_set)
551 prefix, c->oom_adjust);
553 for (i = 0; i < RLIM_NLIMITS; i++)
555 fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max);
559 "%sIOSchedulingClass: %s\n"
560 "%sIOPriority: %i\n",
561 prefix, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c->ioprio)),
562 prefix, (int) IOPRIO_PRIO_DATA(c->ioprio));
564 if (c->cpu_sched_set)
566 "%sCPUSchedulingPolicy: %s\n"
567 "%sCPUSchedulingPriority: %i\n"
568 "%sCPUSchedulingResetOnFork: %s\n",
569 prefix, sched_policy_to_string(c->cpu_sched_policy),
570 prefix, c->cpu_sched_priority,
571 prefix, yes_no(c->cpu_sched_reset_on_fork));
573 if (c->cpu_affinity_set) {
574 fprintf(f, "%sCPUAffinity:", prefix);
575 for (i = 0; i < CPU_SETSIZE; i++)
576 if (CPU_ISSET(i, &c->cpu_affinity))
577 fprintf(f, " %i", i);
581 if (c->timer_slack_ns_set)
582 fprintf(f, "%sTimerSlackNS: %lu\n", prefix, c->timer_slack_ns);
587 prefix, exec_input_to_string(c->input),
588 prefix, exec_output_to_string(c->output));
590 if (c->output == EXEC_OUTPUT_SYSLOG || c->output == EXEC_OUTPUT_KERNEL)
592 "%sSyslogFacility: %s\n"
593 "%sSyslogLevel: %s\n",
594 prefix, log_facility_to_string(LOG_FAC(c->syslog_priority)),
595 prefix, log_level_to_string(LOG_PRI(c->syslog_priority)));
597 if (c->capabilities) {
599 if ((t = cap_to_text(c->capabilities, NULL))) {
600 fprintf(f, "%sCapabilities: %s\n",
607 fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n",
609 (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "",
610 (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "",
611 (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "",
612 (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "",
613 (c->secure_bits & SECURE_NOROOT) ? " noroot" : "",
614 (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : "");
616 if (c->capability_bounding_set_drop) {
617 fprintf(f, "%sCapabilityBoundingSetDrop:", prefix);
619 for (i = 0; i <= CAP_LAST_CAP; i++)
620 if (c->capability_bounding_set_drop & (1 << i)) {
623 if ((t = cap_to_name(i))) {
624 fprintf(f, " %s", t);
633 fprintf(f, "%sUser: %s", prefix, c->user);
635 fprintf(f, "%sGroup: %s", prefix, c->group);
637 if (c->supplementary_groups) {
640 fprintf(f, "%sSupplementaryGroups:", prefix);
642 STRV_FOREACH(g, c->supplementary_groups)
643 fprintf(f, " %s", *g);
649 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
655 s->timestamp = now(CLOCK_REALTIME);
658 char *exec_command_line(ExecCommand *c) {
667 STRV_FOREACH(a, c->argv)
670 if (!(n = new(char, k)))
674 STRV_FOREACH(a, c->argv) {
681 if (strpbrk(*a, WHITESPACE)) {
692 /* FIXME: this doesn't really handle arguments that have
693 * spaces and ticks in them */
698 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
707 cmd = exec_command_line(c);
710 "%sCommand Line: %s\n",
711 prefix, cmd ? cmd : strerror(ENOMEM));
716 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
722 LIST_FOREACH(command, c, c)
723 exec_command_dump(c, f, prefix);
726 static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
727 [EXEC_OUTPUT_CONSOLE] = "console",
728 [EXEC_OUTPUT_NULL] = "null",
729 [EXEC_OUTPUT_SYSLOG] = "syslog",
730 [EXEC_OUTPUT_KERNEL] = "kernel"
733 DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);
735 static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
736 [EXEC_INPUT_NULL] = "null",
737 [EXEC_INPUT_CONSOLE] = "console"
740 DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput);