1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
3 #pragma GCC diagnostic ignored "-Wattributes"
12 #include <sys/socket.h>
14 #include <sys/prctl.h>
15 #include <linux/sched.h>
23 #include "securebits.h"
25 static int close_fds(int except
[], unsigned n_except
) {
30 /* Modifies the fds array! (sorts it) */
32 if (!(d
= opendir("/proc/self/fd")))
35 while ((de
= readdir(d
))) {
38 if (de
->d_name
[0] == '.')
41 if ((r
= safe_atoi(de
->d_name
, &fd
)) < 0)
55 for (i
= 0; i
< n_except
; i
++)
56 if (except
[i
] == fd
) {
65 if ((r
= close_nointr(fd
)) < 0)
74 static int shift_fds(int fds
[], unsigned n_fds
) {
75 int start
, restart_from
;
88 for (i
= start
; i
< (int) n_fds
; i
++) {
91 /* Already at right index? */
95 if ((nfd
= fcntl(fds
[i
], F_DUPFD
, i
+3)) < 0)
98 assert_se(close_nointr(fds
[i
]) == 0);
101 /* Hmm, the fd we wanted isn't free? Then
102 * let's remember that and try again from here*/
103 if (nfd
!= i
+3 && restart_from
< 0)
107 if (restart_from
< 0)
110 start
= restart_from
;
116 static int flags_fds(int fds
[], unsigned n_fds
) {
124 /* Drops O_NONBLOCK and FD_CLOEXEC from the file flags */
126 for (i
= 0; i
< n_fds
; i
++) {
129 if ((flags
= fcntl(fds
[i
], F_GETFL
, 0)) < 0)
132 /* Since we are at it, let's make sure that nobody
133 * forgot setting O_NONBLOCK for all our fds */
135 if (fcntl(fds
[i
], F_SETFL
, flags
&~O_NONBLOCK
) < 0)
138 if ((flags
= fcntl(fds
[i
], F_GETFD
, 0)) < 0)
141 /* Also make sure nobody forgot O_CLOEXEC for all our
143 if (fcntl(fds
[i
], F_SETFD
, flags
&~FD_CLOEXEC
) < 0)
150 static int replace_null_fd(int fd
, int flags
) {
156 if ((nfd
= open("/dev/null", flags
|O_NOCTTY
)) < 0)
160 close_nointr_nofail(nfd
);
167 static int setup_output(const ExecContext
*context
, const char *ident
) {
172 switch (context
->output
) {
174 case EXEC_OUTPUT_CONSOLE
:
177 case EXEC_OUTPUT_NULL
:
179 if ((r
= replace_null_fd(STDOUT_FILENO
, O_WRONLY
)) < 0 ||
180 (r
= replace_null_fd(STDERR_FILENO
, O_WRONLY
)) < 0)
185 case EXEC_OUTPUT_KERNEL
:
186 case EXEC_OUTPUT_SYSLOG
: {
191 struct sockaddr_un un
;
194 close_nointr(STDOUT_FILENO
);
195 close_nointr(STDERR_FILENO
);
197 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
200 if (fd
!= STDOUT_FILENO
) {
201 close_nointr_nofail(fd
);
206 sa
.sa
.sa_family
= AF_UNIX
;
207 strncpy(sa
.un
.sun_path
+1, LOGGER_SOCKET
, sizeof(sa
.un
.sun_path
)-1);
209 if (connect(fd
, &sa
.sa
, sizeof(sa
)) < 0) {
210 close_nointr_nofail(fd
);
214 if (shutdown(fd
, SHUT_RD
) < 0) {
215 close_nointr_nofail(fd
);
219 if ((fd
= dup(fd
)) < 0) {
220 close_nointr_nofail(fd
);
224 if (fd
!= STDERR_FILENO
) {
225 close_nointr_nofail(fd
);
229 /* We speak a very simple protocol between log server
230 * and client: one line for the log destination (kmsg
231 * or syslog), followed by the priority field,
232 * followed by the process name. Since we replaced
233 * stdin/stderr we simple use stdio to write to
234 * it. Note that we use stderr, to minimize buffer
235 * flushing issues. */
241 context
->output
== EXEC_OUTPUT_KERNEL
? "kmsg" : "syslog",
242 context
->syslog_priority
,
243 context
->syslog_identifier
? context
->syslog_identifier
: ident
);
249 assert_not_reached("Unknown output type");
253 int setup_input(const ExecContext
*context
) {
258 switch (context
->input
) {
260 case EXEC_INPUT_CONSOLE
:
263 case EXEC_INPUT_NULL
:
264 if ((r
= replace_null_fd(STDIN_FILENO
, O_RDONLY
)) < 0)
270 assert_not_reached("Unknown input type");
274 int exec_spawn(const ExecCommand
*command
, const ExecContext
*context
, int *fds
, unsigned n_fds
, pid_t
*ret
) {
280 assert(fds
|| n_fds
<= 0);
282 log_debug("about to execute %s", command
->path
);
284 if ((pid
= fork()) < 0)
288 char **e
, **f
= NULL
;
294 if (sigemptyset(&ss
) < 0 ||
295 sigprocmask(SIG_SETMASK
, &ss
, NULL
) < 0) {
296 r
= EXIT_SIGNAL_MASK
;
300 if (setpgid(0, 0) < 0) {
305 umask(context
->umask
);
307 if (setup_input(context
) < 0) {
312 if (setup_output(context
, file_name_from_path(command
->path
)) < 0) {
317 if (context
->oom_adjust_set
) {
320 snprintf(t
, sizeof(t
), "%i", context
->oom_adjust
);
323 if (write_one_line_file("/proc/self/oom_adj", t
) < 0) {
329 if (context
->root_directory
)
330 if (chroot(context
->root_directory
) < 0) {
335 if (chdir(context
->working_directory
? context
->working_directory
: "/") < 0) {
340 if (context
->nice_set
)
341 if (setpriority(PRIO_PROCESS
, 0, context
->nice
) < 0) {
346 if (context
->cpu_sched_set
) {
347 struct sched_param param
;
350 param
.sched_priority
= context
->cpu_sched_priority
;
352 if (sched_setscheduler(0, context
->cpu_sched_policy
|
353 (context
->cpu_sched_reset_on_fork
? SCHED_RESET_ON_FORK
: 0), ¶m
) < 0) {
354 r
= EXIT_SETSCHEDULER
;
359 if (context
->cpu_affinity_set
)
360 if (sched_setaffinity(0, sizeof(context
->cpu_affinity
), &context
->cpu_affinity
) < 0) {
361 r
= EXIT_CPUAFFINITY
;
365 if (context
->ioprio_set
)
366 if (ioprio_set(IOPRIO_WHO_PROCESS
, 0, context
->ioprio
) < 0) {
371 if (context
->timer_slack_ns_set
)
372 if (prctl(PR_SET_TIMERSLACK
, context
->timer_slack_ns_set
) < 0) {
377 if (close_fds(fds
, n_fds
) < 0 ||
378 shift_fds(fds
, n_fds
) < 0 ||
379 flags_fds(fds
, n_fds
) < 0) {
384 for (i
= 0; i
< RLIMIT_NLIMITS
; i
++) {
385 if (!context
->rlimit
[i
])
388 if (setrlimit(i
, context
->rlimit
[i
]) < 0) {
394 if (context
->secure_bits
) {
395 if (prctl(PR_SET_SECUREBITS
, context
->secure_bits
) < 0) {
403 char *listen_env
[3] = {
409 snprintf(a
, sizeof(a
), "LISTEN_PID=%llu", (unsigned long long) getpid());
410 snprintf(b
, sizeof(b
), "LISTEN_FDS=%u", n_fds
);
415 if (context
->environment
) {
416 if (!(f
= strv_merge(listen_env
, context
->environment
))) {
425 e
= context
->environment
;
427 execve(command
->path
, command
->argv
, e
);
436 log_debug("executed %s as %llu", command
->path
, (unsigned long long) pid
);
442 void exec_context_init(ExecContext
*c
) {
447 c
->oom_adjust_set
= false;
450 c
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, 0);
451 c
->ioprio_set
= false;
452 c
->cpu_sched_policy
= SCHED_OTHER
;
453 c
->cpu_sched_priority
= 0;
454 c
->cpu_sched_set
= false;
455 CPU_ZERO(&c
->cpu_affinity
);
456 c
->cpu_affinity_set
= false;
460 c
->syslog_priority
= LOG_DAEMON
|LOG_INFO
;
463 c
->capability_bounding_set_drop
= 0;
466 void exec_context_done(ExecContext
*c
) {
471 strv_free(c
->environment
);
472 c
->environment
= NULL
;
474 for (l
= 0; l
< ELEMENTSOF(c
->rlimit
); l
++) {
479 free(c
->working_directory
);
480 c
->working_directory
= NULL
;
481 free(c
->root_directory
);
482 c
->root_directory
= NULL
;
484 free(c
->syslog_identifier
);
485 c
->syslog_identifier
= NULL
;
493 strv_free(c
->supplementary_groups
);
494 c
->supplementary_groups
= NULL
;
496 if (c
->capabilities
) {
497 cap_free(c
->capabilities
);
498 c
->capabilities
= NULL
;
502 void exec_command_free_list(ExecCommand
*c
) {
506 LIST_REMOVE(ExecCommand
, command
, c
, i
);
514 void exec_command_free_array(ExecCommand
**c
, unsigned n
) {
517 for (i
= 0; i
< n
; i
++) {
518 exec_command_free_list(c
[i
]);
523 void exec_context_dump(ExecContext
*c
, FILE* f
, const char *prefix
) {
535 "%sWorkingDirectory: %s\n"
536 "%sRootDirectory: %s\n",
538 prefix
, c
->working_directory
? c
->working_directory
: "/",
539 prefix
, c
->root_directory
? c
->root_directory
: "/");
542 for (e
= c
->environment
; *e
; e
++)
543 fprintf(f
, "%sEnvironment: %s\n", prefix
, *e
);
550 if (c
->oom_adjust_set
)
553 prefix
, c
->oom_adjust
);
555 for (i
= 0; i
< RLIM_NLIMITS
; i
++)
557 fprintf(f
, "%s%s: %llu\n", prefix
, rlimit_to_string(i
), (unsigned long long) c
->rlimit
[i
]->rlim_max
);
561 "%sIOSchedulingClass: %s\n"
562 "%sIOPriority: %i\n",
563 prefix
, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c
->ioprio
)),
564 prefix
, (int) IOPRIO_PRIO_DATA(c
->ioprio
));
566 if (c
->cpu_sched_set
)
568 "%sCPUSchedulingPolicy: %s\n"
569 "%sCPUSchedulingPriority: %i\n"
570 "%sCPUSchedulingResetOnFork: %s\n",
571 prefix
, sched_policy_to_string(c
->cpu_sched_policy
),
572 prefix
, c
->cpu_sched_priority
,
573 prefix
, yes_no(c
->cpu_sched_reset_on_fork
));
575 if (c
->cpu_affinity_set
) {
576 fprintf(f
, "%sCPUAffinity:", prefix
);
577 for (i
= 0; i
< CPU_SETSIZE
; i
++)
578 if (CPU_ISSET(i
, &c
->cpu_affinity
))
579 fprintf(f
, " %i", i
);
583 if (c
->timer_slack_ns_set
)
584 fprintf(f
, "%sTimerSlackNS: %lu\n", prefix
, c
->timer_slack_ns
);
589 prefix
, exec_input_to_string(c
->input
),
590 prefix
, exec_output_to_string(c
->output
));
592 if (c
->output
== EXEC_OUTPUT_SYSLOG
|| c
->output
== EXEC_OUTPUT_KERNEL
)
594 "%sSyslogFacility: %s\n"
595 "%sSyslogLevel: %s\n",
596 prefix
, log_facility_to_string(LOG_FAC(c
->syslog_priority
)),
597 prefix
, log_level_to_string(LOG_PRI(c
->syslog_priority
)));
599 if (c
->capabilities
) {
601 if ((t
= cap_to_text(c
->capabilities
, NULL
))) {
602 fprintf(f
, "%sCapabilities: %s\n",
609 fprintf(f
, "%sSecure Bits:%s%s%s%s%s%s\n",
611 (c
->secure_bits
& SECURE_KEEP_CAPS
) ? " keep-caps" : "",
612 (c
->secure_bits
& SECURE_KEEP_CAPS_LOCKED
) ? " keep-caps-locked" : "",
613 (c
->secure_bits
& SECURE_NO_SETUID_FIXUP
) ? " no-setuid-fixup" : "",
614 (c
->secure_bits
& SECURE_NO_SETUID_FIXUP_LOCKED
) ? " no-setuid-fixup-locked" : "",
615 (c
->secure_bits
& SECURE_NOROOT
) ? " noroot" : "",
616 (c
->secure_bits
& SECURE_NOROOT_LOCKED
) ? "noroot-locked" : "");
618 if (c
->capability_bounding_set_drop
) {
619 fprintf(f
, "%sCapabilityBoundingSetDrop:", prefix
);
621 for (i
= 0; i
<= CAP_LAST_CAP
; i
++)
622 if (c
->capability_bounding_set_drop
& (1 << i
)) {
625 if ((t
= cap_to_name(i
))) {
626 fprintf(f
, " %s", t
);
635 fprintf(f
, "%sUser: %s", prefix
, c
->user
);
637 fprintf(f
, "%sGroup: %s", prefix
, c
->group
);
639 if (c
->supplementary_groups
) {
642 fprintf(f
, "%sSupplementaryGroups:", prefix
);
644 STRV_FOREACH(g
, c
->supplementary_groups
)
645 fprintf(f
, " %s", *g
);
651 void exec_status_fill(ExecStatus
*s
, pid_t pid
, int code
, int status
) {
657 s
->timestamp
= now(CLOCK_REALTIME
);
660 char *exec_command_line(ExecCommand
*c
) {
669 STRV_FOREACH(a
, c
->argv
)
672 if (!(n
= new(char, k
)))
676 STRV_FOREACH(a
, c
->argv
) {
683 if (strpbrk(*a
, WHITESPACE
)) {
694 /* FIXME: this doesn't really handle arguments that have
695 * spaces and ticks in them */
700 void exec_command_dump(ExecCommand
*c
, FILE *f
, const char *prefix
) {
709 cmd
= exec_command_line(c
);
712 "%sCommand Line: %s\n",
713 prefix
, cmd
? cmd
: strerror(ENOMEM
));
718 void exec_command_dump_list(ExecCommand
*c
, FILE *f
, const char *prefix
) {
724 LIST_FOREACH(command
, c
, c
)
725 exec_command_dump(c
, f
, prefix
);
728 static const char* const exec_output_table
[_EXEC_OUTPUT_MAX
] = {
729 [EXEC_OUTPUT_CONSOLE
] = "console",
730 [EXEC_OUTPUT_NULL
] = "null",
731 [EXEC_OUTPUT_SYSLOG
] = "syslog",
732 [EXEC_OUTPUT_KERNEL
] = "kernel"
735 DEFINE_STRING_TABLE_LOOKUP(exec_output
, ExecOutput
);
737 static const char* const exec_input_table
[_EXEC_INPUT_MAX
] = {
738 [EXEC_INPUT_NULL
] = "null",
739 [EXEC_INPUT_CONSOLE
] = "console"
742 DEFINE_STRING_TABLE_LOOKUP(exec_input
, ExecInput
);