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
, ¶m
) < 0) {
351 r
= EXIT_SETSCHEDULER
;
356 if (context
->cpu_affinity_set
)
357 if (sched_setaffinity(0, sizeof(context
->cpu_affinity
), &context
->cpu_affinity
) < 0) {
358 r
= EXIT_CPUAFFINITY
;
362 if (context
->ioprio_set
)
363 if (ioprio_set(IOPRIO_WHO_PROCESS
, 0, context
->ioprio
) < 0) {
368 if (context
->timer_slack_ns_set
)
369 if (prctl(PR_SET_TIMERSLACK
, context
->timer_slack_ns_set
) < 0) {
374 if (close_fds(fds
, n_fds
) < 0 ||
375 shift_fds(fds
, n_fds
) < 0 ||
376 flags_fds(fds
, n_fds
) < 0) {
381 for (i
= 0; i
< RLIMIT_NLIMITS
; i
++) {
382 if (!context
->rlimit
[i
])
385 if (setrlimit(i
, context
->rlimit
[i
]) < 0) {
391 if (context
->secure_bits
) {
392 if (prctl(PR_SET_SECUREBITS
, context
->secure_bits
) < 0) {
400 char *listen_env
[3] = {
406 snprintf(a
, sizeof(a
), "LISTEN_PID=%llu", (unsigned long long) getpid());
407 snprintf(b
, sizeof(b
), "LISTEN_FDS=%u", n_fds
);
412 if (context
->environment
) {
413 if (!(f
= strv_merge(listen_env
, context
->environment
))) {
422 e
= context
->environment
;
424 execve(command
->path
, command
->argv
, e
);
433 log_debug("executed %s as %llu", command
->path
, (unsigned long long) pid
);
439 void exec_context_init(ExecContext
*c
) {
444 c
->oom_adjust_set
= false;
447 c
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, 0);
448 c
->ioprio_set
= false;
449 c
->cpu_sched_policy
= SCHED_OTHER
;
450 c
->cpu_sched_priority
= 0;
451 c
->cpu_sched_set
= false;
452 CPU_ZERO(&c
->cpu_affinity
);
453 c
->cpu_affinity_set
= false;
457 c
->syslog_priority
= LOG_DAEMON
|LOG_INFO
;
460 c
->capability_bounding_set_drop
= 0;
463 void exec_context_done(ExecContext
*c
) {
468 strv_free(c
->environment
);
469 c
->environment
= NULL
;
471 for (l
= 0; l
< ELEMENTSOF(c
->rlimit
); l
++) {
476 free(c
->working_directory
);
477 c
->working_directory
= NULL
;
478 free(c
->root_directory
);
479 c
->root_directory
= NULL
;
481 free(c
->syslog_identifier
);
482 c
->syslog_identifier
= NULL
;
490 strv_free(c
->supplementary_groups
);
491 c
->supplementary_groups
= NULL
;
493 if (c
->capabilities
) {
494 cap_free(c
->capabilities
);
495 c
->capabilities
= NULL
;
499 void exec_command_free_list(ExecCommand
*c
) {
503 LIST_REMOVE(ExecCommand
, command
, c
, i
);
511 void exec_command_free_array(ExecCommand
**c
, unsigned n
) {
514 for (i
= 0; i
< n
; i
++) {
515 exec_command_free_list(c
[i
]);
520 void exec_context_dump(ExecContext
*c
, FILE* f
, const char *prefix
) {
532 "%sWorkingDirectory: %s\n"
533 "%sRootDirectory: %s\n",
535 prefix
, c
->working_directory
? c
->working_directory
: "/",
536 prefix
, c
->root_directory
? c
->root_directory
: "/");
539 for (e
= c
->environment
; *e
; e
++)
540 fprintf(f
, "%sEnvironment: %s\n", prefix
, *e
);
547 if (c
->oom_adjust_set
)
550 prefix
, c
->oom_adjust
);
552 for (i
= 0; i
< RLIM_NLIMITS
; i
++)
554 fprintf(f
, "%s%s: %llu\n", prefix
, rlimit_to_string(i
), (unsigned long long) c
->rlimit
[i
]->rlim_max
);
558 "%sIOSchedulingClass: %s\n"
559 "%sIOPriority: %i\n",
560 prefix
, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c
->ioprio
)),
561 prefix
, (int) IOPRIO_PRIO_DATA(c
->ioprio
));
563 if (c
->cpu_sched_set
)
565 "%sCPUSchedulingPolicy: %s\n"
566 "%sCPUSchedulingPriority: %i\n",
567 prefix
, sched_policy_to_string(c
->cpu_sched_policy
),
568 prefix
, c
->cpu_sched_priority
);
570 if (c
->cpu_affinity_set
) {
571 fprintf(f
, "%sCPUAffinity:", prefix
);
572 for (i
= 0; i
< CPU_SETSIZE
; i
++)
573 if (CPU_ISSET(i
, &c
->cpu_affinity
))
574 fprintf(f
, " %i", i
);
578 if (c
->timer_slack_ns_set
)
579 fprintf(f
, "%sTimerSlackNS: %lu\n", prefix
, c
->timer_slack_ns
);
584 prefix
, exec_input_to_string(c
->input
),
585 prefix
, exec_output_to_string(c
->output
));
587 if (c
->output
== EXEC_OUTPUT_SYSLOG
|| c
->output
== EXEC_OUTPUT_KERNEL
)
589 "%sSyslogFacility: %s\n"
590 "%sSyslogLevel: %s\n",
591 prefix
, log_facility_to_string(LOG_FAC(c
->syslog_priority
)),
592 prefix
, log_level_to_string(LOG_PRI(c
->syslog_priority
)));
594 if (c
->capabilities
) {
596 if ((t
= cap_to_text(c
->capabilities
, NULL
))) {
597 fprintf(f
, "%sCapabilities: %s\n",
604 fprintf(f
, "%sSecure Bits:%s%s%s%s%s%s\n",
606 (c
->secure_bits
& SECURE_KEEP_CAPS
) ? " keep-caps" : "",
607 (c
->secure_bits
& SECURE_KEEP_CAPS_LOCKED
) ? " keep-caps-locked" : "",
608 (c
->secure_bits
& SECURE_NO_SETUID_FIXUP
) ? " no-setuid-fixup" : "",
609 (c
->secure_bits
& SECURE_NO_SETUID_FIXUP_LOCKED
) ? " no-setuid-fixup-locked" : "",
610 (c
->secure_bits
& SECURE_NOROOT
) ? " noroot" : "",
611 (c
->secure_bits
& SECURE_NOROOT_LOCKED
) ? "noroot-locked" : "");
613 if (c
->capability_bounding_set_drop
) {
614 fprintf(f
, "%sCapabilityBoundingSetDrop:", prefix
);
616 for (i
= 0; i
<= CAP_LAST_CAP
; i
++)
617 if (c
->capability_bounding_set_drop
& (1 << i
)) {
620 if ((t
= cap_to_name(i
))) {
621 fprintf(f
, " %s", t
);
630 fprintf(f
, "%sUser: %s", prefix
, c
->user
);
632 fprintf(f
, "%sGroup: %s", prefix
, c
->group
);
634 if (c
->supplementary_groups
) {
637 fprintf(f
, "%sSupplementaryGroups:", prefix
);
639 STRV_FOREACH(g
, c
->supplementary_groups
)
640 fprintf(f
, " %s", *g
);
646 void exec_status_fill(ExecStatus
*s
, pid_t pid
, int code
, int status
) {
652 s
->timestamp
= now(CLOCK_REALTIME
);
655 char *exec_command_line(ExecCommand
*c
) {
664 STRV_FOREACH(a
, c
->argv
)
667 if (!(n
= new(char, k
)))
671 STRV_FOREACH(a
, c
->argv
) {
678 if (strpbrk(*a
, WHITESPACE
)) {
689 /* FIXME: this doesn't really handle arguments that have
690 * spaces and ticks in them */
695 void exec_command_dump(ExecCommand
*c
, FILE *f
, const char *prefix
) {
704 cmd
= exec_command_line(c
);
707 "%sCommand Line: %s\n",
708 prefix
, cmd
? cmd
: strerror(ENOMEM
));
713 void exec_command_dump_list(ExecCommand
*c
, FILE *f
, const char *prefix
) {
719 LIST_FOREACH(command
, c
, c
)
720 exec_command_dump(c
, f
, prefix
);
723 static const char* const exec_output_table
[_EXEC_OUTPUT_MAX
] = {
724 [EXEC_OUTPUT_CONSOLE
] = "console",
725 [EXEC_OUTPUT_NULL
] = "null",
726 [EXEC_OUTPUT_SYSLOG
] = "syslog",
727 [EXEC_OUTPUT_KERNEL
] = "kernel"
730 DEFINE_STRING_TABLE_LOOKUP(exec_output
, ExecOutput
);
732 static const char* const exec_input_table
[_EXEC_INPUT_MAX
] = {
733 [EXEC_INPUT_NULL
] = "null",
734 [EXEC_INPUT_CONSOLE
] = "console"
737 DEFINE_STRING_TABLE_LOOKUP(exec_input
, ExecInput
);