]>
Commit | Line | Data |
---|---|---|
a7334b09 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
7 | under the terms of the GNU Lesser General Public License as published by |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
9 | (at your option) any later version. |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 14 | Lesser General Public License for more details. |
a7334b09 | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
034c6ed7 LP |
20 | #include <errno.h> |
21 | #include <fcntl.h> | |
8dd4c05b LP |
22 | #include <glob.h> |
23 | #include <grp.h> | |
24 | #include <poll.h> | |
309bff19 | 25 | #include <signal.h> |
8dd4c05b | 26 | #include <string.h> |
19c0b0b9 | 27 | #include <sys/capability.h> |
d251207d | 28 | #include <sys/eventfd.h> |
f3e43635 | 29 | #include <sys/mman.h> |
8dd4c05b | 30 | #include <sys/personality.h> |
94f04347 | 31 | #include <sys/prctl.h> |
8dd4c05b | 32 | #include <sys/socket.h> |
451a074f | 33 | #include <sys/stat.h> |
8dd4c05b LP |
34 | #include <sys/un.h> |
35 | #include <unistd.h> | |
023a4f67 | 36 | #include <utmpx.h> |
5cb5a6ff | 37 | |
5b6319dc LP |
38 | #ifdef HAVE_PAM |
39 | #include <security/pam_appl.h> | |
40 | #endif | |
41 | ||
7b52a628 MS |
42 | #ifdef HAVE_SELINUX |
43 | #include <selinux/selinux.h> | |
44 | #endif | |
45 | ||
17df7223 LP |
46 | #ifdef HAVE_SECCOMP |
47 | #include <seccomp.h> | |
48 | #endif | |
49 | ||
eef65bf3 MS |
50 | #ifdef HAVE_APPARMOR |
51 | #include <sys/apparmor.h> | |
52 | #endif | |
53 | ||
24882e06 | 54 | #include "sd-messages.h" |
8dd4c05b LP |
55 | |
56 | #include "af-list.h" | |
b5efdb8a | 57 | #include "alloc-util.h" |
3ffd4af2 LP |
58 | #ifdef HAVE_APPARMOR |
59 | #include "apparmor-util.h" | |
60 | #endif | |
8dd4c05b LP |
61 | #include "async.h" |
62 | #include "barrier.h" | |
8dd4c05b | 63 | #include "cap-list.h" |
430f0182 | 64 | #include "capability-util.h" |
f6a6225e | 65 | #include "def.h" |
4d1a6904 | 66 | #include "env-util.h" |
17df7223 | 67 | #include "errno-list.h" |
3ffd4af2 | 68 | #include "execute.h" |
8dd4c05b | 69 | #include "exit-status.h" |
3ffd4af2 | 70 | #include "fd-util.h" |
8dd4c05b | 71 | #include "fileio.h" |
6482f626 | 72 | #include "formats-util.h" |
f4f15635 | 73 | #include "fs-util.h" |
7d50b32a | 74 | #include "glob-util.h" |
c004493c | 75 | #include "io-util.h" |
8dd4c05b LP |
76 | #include "ioprio.h" |
77 | #include "log.h" | |
78 | #include "macro.h" | |
79 | #include "missing.h" | |
80 | #include "mkdir.h" | |
81 | #include "namespace.h" | |
6bedfcbb | 82 | #include "parse-util.h" |
8dd4c05b | 83 | #include "path-util.h" |
0b452006 | 84 | #include "process-util.h" |
78f22b97 | 85 | #include "rlimit-util.h" |
8dd4c05b | 86 | #include "rm-rf.h" |
3ffd4af2 LP |
87 | #ifdef HAVE_SECCOMP |
88 | #include "seccomp-util.h" | |
89 | #endif | |
8dd4c05b LP |
90 | #include "securebits.h" |
91 | #include "selinux-util.h" | |
24882e06 | 92 | #include "signal-util.h" |
8dd4c05b | 93 | #include "smack-util.h" |
fd63e712 | 94 | #include "special.h" |
8b43440b | 95 | #include "string-table.h" |
07630cea | 96 | #include "string-util.h" |
8dd4c05b | 97 | #include "strv.h" |
7ccbd1ae | 98 | #include "syslog-util.h" |
8dd4c05b LP |
99 | #include "terminal-util.h" |
100 | #include "unit.h" | |
b1d4f8e1 | 101 | #include "user-util.h" |
8dd4c05b LP |
102 | #include "util.h" |
103 | #include "utmp-wtmp.h" | |
5cb5a6ff | 104 | |
e056b01d | 105 | #define IDLE_TIMEOUT_USEC (5*USEC_PER_SEC) |
31a7eb86 | 106 | #define IDLE_TIMEOUT2_USEC (1*USEC_PER_SEC) |
e6a26745 | 107 | |
02a51aba LP |
108 | /* This assumes there is a 'tty' group */ |
109 | #define TTY_MODE 0620 | |
110 | ||
531dca78 LP |
111 | #define SNDBUF_SIZE (8*1024*1024) |
112 | ||
034c6ed7 LP |
113 | static int shift_fds(int fds[], unsigned n_fds) { |
114 | int start, restart_from; | |
115 | ||
116 | if (n_fds <= 0) | |
117 | return 0; | |
118 | ||
a0d40ac5 LP |
119 | /* Modifies the fds array! (sorts it) */ |
120 | ||
034c6ed7 LP |
121 | assert(fds); |
122 | ||
123 | start = 0; | |
124 | for (;;) { | |
125 | int i; | |
126 | ||
127 | restart_from = -1; | |
128 | ||
129 | for (i = start; i < (int) n_fds; i++) { | |
130 | int nfd; | |
131 | ||
132 | /* Already at right index? */ | |
133 | if (fds[i] == i+3) | |
134 | continue; | |
135 | ||
3cc2aff1 LP |
136 | nfd = fcntl(fds[i], F_DUPFD, i + 3); |
137 | if (nfd < 0) | |
034c6ed7 LP |
138 | return -errno; |
139 | ||
03e334a1 | 140 | safe_close(fds[i]); |
034c6ed7 LP |
141 | fds[i] = nfd; |
142 | ||
143 | /* Hmm, the fd we wanted isn't free? Then | |
ee33e53a | 144 | * let's remember that and try again from here */ |
034c6ed7 LP |
145 | if (nfd != i+3 && restart_from < 0) |
146 | restart_from = i; | |
147 | } | |
148 | ||
149 | if (restart_from < 0) | |
150 | break; | |
151 | ||
152 | start = restart_from; | |
153 | } | |
154 | ||
155 | return 0; | |
156 | } | |
157 | ||
c2748801 | 158 | static int flags_fds(const int fds[], unsigned n_fds, bool nonblock) { |
47a71eed | 159 | unsigned i; |
e2c76839 | 160 | int r; |
47a71eed LP |
161 | |
162 | if (n_fds <= 0) | |
163 | return 0; | |
164 | ||
165 | assert(fds); | |
166 | ||
451a074f | 167 | /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags */ |
47a71eed LP |
168 | |
169 | for (i = 0; i < n_fds; i++) { | |
47a71eed | 170 | |
3cc2aff1 LP |
171 | r = fd_nonblock(fds[i], nonblock); |
172 | if (r < 0) | |
e2c76839 | 173 | return r; |
47a71eed | 174 | |
451a074f LP |
175 | /* We unconditionally drop FD_CLOEXEC from the fds, |
176 | * since after all we want to pass these fds to our | |
177 | * children */ | |
47a71eed | 178 | |
3cc2aff1 LP |
179 | r = fd_cloexec(fds[i], false); |
180 | if (r < 0) | |
e2c76839 | 181 | return r; |
47a71eed LP |
182 | } |
183 | ||
184 | return 0; | |
185 | } | |
186 | ||
1e22b5cd | 187 | static const char *exec_context_tty_path(const ExecContext *context) { |
80876c20 LP |
188 | assert(context); |
189 | ||
1e22b5cd LP |
190 | if (context->stdio_as_fds) |
191 | return NULL; | |
192 | ||
80876c20 LP |
193 | if (context->tty_path) |
194 | return context->tty_path; | |
195 | ||
196 | return "/dev/console"; | |
197 | } | |
198 | ||
1e22b5cd LP |
199 | static void exec_context_tty_reset(const ExecContext *context, const ExecParameters *p) { |
200 | const char *path; | |
201 | ||
6ea832a2 LP |
202 | assert(context); |
203 | ||
1e22b5cd | 204 | path = exec_context_tty_path(context); |
6ea832a2 | 205 | |
1e22b5cd LP |
206 | if (context->tty_vhangup) { |
207 | if (p && p->stdin_fd >= 0) | |
208 | (void) terminal_vhangup_fd(p->stdin_fd); | |
209 | else if (path) | |
210 | (void) terminal_vhangup(path); | |
211 | } | |
6ea832a2 | 212 | |
1e22b5cd LP |
213 | if (context->tty_reset) { |
214 | if (p && p->stdin_fd >= 0) | |
215 | (void) reset_terminal_fd(p->stdin_fd, true); | |
216 | else if (path) | |
217 | (void) reset_terminal(path); | |
218 | } | |
219 | ||
220 | if (context->tty_vt_disallocate && path) | |
221 | (void) vt_disallocate(path); | |
6ea832a2 LP |
222 | } |
223 | ||
6af760f3 LP |
224 | static bool is_terminal_input(ExecInput i) { |
225 | return IN_SET(i, | |
226 | EXEC_INPUT_TTY, | |
227 | EXEC_INPUT_TTY_FORCE, | |
228 | EXEC_INPUT_TTY_FAIL); | |
229 | } | |
230 | ||
3a1286b6 | 231 | static bool is_terminal_output(ExecOutput o) { |
6af760f3 LP |
232 | return IN_SET(o, |
233 | EXEC_OUTPUT_TTY, | |
234 | EXEC_OUTPUT_SYSLOG_AND_CONSOLE, | |
235 | EXEC_OUTPUT_KMSG_AND_CONSOLE, | |
236 | EXEC_OUTPUT_JOURNAL_AND_CONSOLE); | |
237 | } | |
238 | ||
239 | static bool exec_context_needs_term(const ExecContext *c) { | |
240 | assert(c); | |
241 | ||
242 | /* Return true if the execution context suggests we should set $TERM to something useful. */ | |
243 | ||
244 | if (is_terminal_input(c->std_input)) | |
245 | return true; | |
246 | ||
247 | if (is_terminal_output(c->std_output)) | |
248 | return true; | |
249 | ||
250 | if (is_terminal_output(c->std_error)) | |
251 | return true; | |
252 | ||
253 | return !!c->tty_path; | |
3a1286b6 MS |
254 | } |
255 | ||
80876c20 LP |
256 | static int open_null_as(int flags, int nfd) { |
257 | int fd, r; | |
071830ff | 258 | |
80876c20 | 259 | assert(nfd >= 0); |
071830ff | 260 | |
613b411c LP |
261 | fd = open("/dev/null", flags|O_NOCTTY); |
262 | if (fd < 0) | |
071830ff LP |
263 | return -errno; |
264 | ||
80876c20 LP |
265 | if (fd != nfd) { |
266 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
03e334a1 | 267 | safe_close(fd); |
80876c20 LP |
268 | } else |
269 | r = nfd; | |
071830ff | 270 | |
80876c20 | 271 | return r; |
071830ff LP |
272 | } |
273 | ||
524daa8c | 274 | static int connect_journal_socket(int fd, uid_t uid, gid_t gid) { |
b92bea5d ZJS |
275 | union sockaddr_union sa = { |
276 | .un.sun_family = AF_UNIX, | |
277 | .un.sun_path = "/run/systemd/journal/stdout", | |
278 | }; | |
524daa8c ZJS |
279 | uid_t olduid = UID_INVALID; |
280 | gid_t oldgid = GID_INVALID; | |
281 | int r; | |
282 | ||
283 | if (gid != GID_INVALID) { | |
284 | oldgid = getgid(); | |
285 | ||
286 | r = setegid(gid); | |
287 | if (r < 0) | |
288 | return -errno; | |
289 | } | |
290 | ||
291 | if (uid != UID_INVALID) { | |
292 | olduid = getuid(); | |
293 | ||
294 | r = seteuid(uid); | |
295 | if (r < 0) { | |
296 | r = -errno; | |
297 | goto restore_gid; | |
298 | } | |
299 | } | |
300 | ||
fc2fffe7 | 301 | r = connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)); |
524daa8c ZJS |
302 | if (r < 0) |
303 | r = -errno; | |
304 | ||
305 | /* If we fail to restore the uid or gid, things will likely | |
306 | fail later on. This should only happen if an LSM interferes. */ | |
307 | ||
308 | if (uid != UID_INVALID) | |
309 | (void) seteuid(olduid); | |
310 | ||
311 | restore_gid: | |
312 | if (gid != GID_INVALID) | |
313 | (void) setegid(oldgid); | |
314 | ||
315 | return r; | |
316 | } | |
317 | ||
fd1f9c89 | 318 | static int connect_logger_as( |
7a1ab780 | 319 | Unit *unit, |
fd1f9c89 LP |
320 | const ExecContext *context, |
321 | ExecOutput output, | |
322 | const char *ident, | |
fd1f9c89 LP |
323 | int nfd, |
324 | uid_t uid, | |
325 | gid_t gid) { | |
326 | ||
524daa8c | 327 | int fd, r; |
071830ff LP |
328 | |
329 | assert(context); | |
80876c20 LP |
330 | assert(output < _EXEC_OUTPUT_MAX); |
331 | assert(ident); | |
332 | assert(nfd >= 0); | |
071830ff | 333 | |
54fe0cdb LP |
334 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
335 | if (fd < 0) | |
80876c20 | 336 | return -errno; |
071830ff | 337 | |
524daa8c ZJS |
338 | r = connect_journal_socket(fd, uid, gid); |
339 | if (r < 0) | |
340 | return r; | |
071830ff | 341 | |
80876c20 | 342 | if (shutdown(fd, SHUT_RD) < 0) { |
03e334a1 | 343 | safe_close(fd); |
80876c20 LP |
344 | return -errno; |
345 | } | |
071830ff | 346 | |
fd1f9c89 | 347 | (void) fd_inc_sndbuf(fd, SNDBUF_SIZE); |
531dca78 | 348 | |
80876c20 | 349 | dprintf(fd, |
62bca2c6 | 350 | "%s\n" |
80876c20 LP |
351 | "%s\n" |
352 | "%i\n" | |
54fe0cdb LP |
353 | "%i\n" |
354 | "%i\n" | |
355 | "%i\n" | |
4f4a1dbf | 356 | "%i\n", |
4f4a1dbf | 357 | context->syslog_identifier ? context->syslog_identifier : ident, |
7a1ab780 | 358 | unit->id, |
54fe0cdb LP |
359 | context->syslog_priority, |
360 | !!context->syslog_level_prefix, | |
361 | output == EXEC_OUTPUT_SYSLOG || output == EXEC_OUTPUT_SYSLOG_AND_CONSOLE, | |
362 | output == EXEC_OUTPUT_KMSG || output == EXEC_OUTPUT_KMSG_AND_CONSOLE, | |
3a1286b6 | 363 | is_terminal_output(output)); |
80876c20 | 364 | |
fd1f9c89 LP |
365 | if (fd == nfd) |
366 | return nfd; | |
367 | ||
368 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
369 | safe_close(fd); | |
071830ff | 370 | |
80876c20 LP |
371 | return r; |
372 | } | |
373 | static int open_terminal_as(const char *path, mode_t mode, int nfd) { | |
374 | int fd, r; | |
071830ff | 375 | |
80876c20 LP |
376 | assert(path); |
377 | assert(nfd >= 0); | |
071830ff | 378 | |
3cc2aff1 LP |
379 | fd = open_terminal(path, mode | O_NOCTTY); |
380 | if (fd < 0) | |
80876c20 | 381 | return fd; |
071830ff | 382 | |
80876c20 LP |
383 | if (fd != nfd) { |
384 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
03e334a1 | 385 | safe_close(fd); |
80876c20 LP |
386 | } else |
387 | r = nfd; | |
071830ff | 388 | |
80876c20 LP |
389 | return r; |
390 | } | |
071830ff | 391 | |
1e3ad081 LP |
392 | static int fixup_input(ExecInput std_input, int socket_fd, bool apply_tty_stdin) { |
393 | ||
394 | if (is_terminal_input(std_input) && !apply_tty_stdin) | |
395 | return EXEC_INPUT_NULL; | |
071830ff | 396 | |
03fd9c49 | 397 | if (std_input == EXEC_INPUT_SOCKET && socket_fd < 0) |
4f2d528d LP |
398 | return EXEC_INPUT_NULL; |
399 | ||
03fd9c49 | 400 | return std_input; |
4f2d528d LP |
401 | } |
402 | ||
03fd9c49 | 403 | static int fixup_output(ExecOutput std_output, int socket_fd) { |
4f2d528d | 404 | |
03fd9c49 | 405 | if (std_output == EXEC_OUTPUT_SOCKET && socket_fd < 0) |
4f2d528d LP |
406 | return EXEC_OUTPUT_INHERIT; |
407 | ||
03fd9c49 | 408 | return std_output; |
4f2d528d LP |
409 | } |
410 | ||
a34ceba6 LP |
411 | static int setup_input( |
412 | const ExecContext *context, | |
413 | const ExecParameters *params, | |
414 | int socket_fd) { | |
415 | ||
4f2d528d LP |
416 | ExecInput i; |
417 | ||
418 | assert(context); | |
a34ceba6 LP |
419 | assert(params); |
420 | ||
421 | if (params->stdin_fd >= 0) { | |
422 | if (dup2(params->stdin_fd, STDIN_FILENO) < 0) | |
423 | return -errno; | |
424 | ||
425 | /* Try to make this the controlling tty, if it is a tty, and reset it */ | |
426 | (void) ioctl(STDIN_FILENO, TIOCSCTTY, context->std_input == EXEC_INPUT_TTY_FORCE); | |
427 | (void) reset_terminal_fd(STDIN_FILENO, true); | |
428 | ||
429 | return STDIN_FILENO; | |
430 | } | |
4f2d528d | 431 | |
c39f1ce2 | 432 | i = fixup_input(context->std_input, socket_fd, params->flags & EXEC_APPLY_TTY_STDIN); |
4f2d528d LP |
433 | |
434 | switch (i) { | |
071830ff | 435 | |
80876c20 LP |
436 | case EXEC_INPUT_NULL: |
437 | return open_null_as(O_RDONLY, STDIN_FILENO); | |
438 | ||
439 | case EXEC_INPUT_TTY: | |
440 | case EXEC_INPUT_TTY_FORCE: | |
441 | case EXEC_INPUT_TTY_FAIL: { | |
442 | int fd, r; | |
071830ff | 443 | |
1e22b5cd | 444 | fd = acquire_terminal(exec_context_tty_path(context), |
970edce6 ZJS |
445 | i == EXEC_INPUT_TTY_FAIL, |
446 | i == EXEC_INPUT_TTY_FORCE, | |
447 | false, | |
3a43da28 | 448 | USEC_INFINITY); |
970edce6 | 449 | if (fd < 0) |
80876c20 LP |
450 | return fd; |
451 | ||
452 | if (fd != STDIN_FILENO) { | |
453 | r = dup2(fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO; | |
03e334a1 | 454 | safe_close(fd); |
80876c20 LP |
455 | } else |
456 | r = STDIN_FILENO; | |
457 | ||
458 | return r; | |
459 | } | |
460 | ||
4f2d528d LP |
461 | case EXEC_INPUT_SOCKET: |
462 | return dup2(socket_fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO; | |
463 | ||
80876c20 LP |
464 | default: |
465 | assert_not_reached("Unknown input type"); | |
466 | } | |
467 | } | |
468 | ||
a34ceba6 LP |
469 | static int setup_output( |
470 | Unit *unit, | |
471 | const ExecContext *context, | |
472 | const ExecParameters *params, | |
473 | int fileno, | |
474 | int socket_fd, | |
475 | const char *ident, | |
7bce046b LP |
476 | uid_t uid, |
477 | gid_t gid, | |
478 | dev_t *journal_stream_dev, | |
479 | ino_t *journal_stream_ino) { | |
a34ceba6 | 480 | |
4f2d528d LP |
481 | ExecOutput o; |
482 | ExecInput i; | |
47c1d80d | 483 | int r; |
4f2d528d | 484 | |
f2341e0a | 485 | assert(unit); |
80876c20 | 486 | assert(context); |
a34ceba6 | 487 | assert(params); |
80876c20 | 488 | assert(ident); |
7bce046b LP |
489 | assert(journal_stream_dev); |
490 | assert(journal_stream_ino); | |
80876c20 | 491 | |
a34ceba6 LP |
492 | if (fileno == STDOUT_FILENO && params->stdout_fd >= 0) { |
493 | ||
494 | if (dup2(params->stdout_fd, STDOUT_FILENO) < 0) | |
495 | return -errno; | |
496 | ||
497 | return STDOUT_FILENO; | |
498 | } | |
499 | ||
500 | if (fileno == STDERR_FILENO && params->stderr_fd >= 0) { | |
501 | if (dup2(params->stderr_fd, STDERR_FILENO) < 0) | |
502 | return -errno; | |
503 | ||
504 | return STDERR_FILENO; | |
505 | } | |
506 | ||
c39f1ce2 | 507 | i = fixup_input(context->std_input, socket_fd, params->flags & EXEC_APPLY_TTY_STDIN); |
03fd9c49 | 508 | o = fixup_output(context->std_output, socket_fd); |
4f2d528d | 509 | |
eb17e935 MS |
510 | if (fileno == STDERR_FILENO) { |
511 | ExecOutput e; | |
512 | e = fixup_output(context->std_error, socket_fd); | |
80876c20 | 513 | |
eb17e935 MS |
514 | /* This expects the input and output are already set up */ |
515 | ||
516 | /* Don't change the stderr file descriptor if we inherit all | |
517 | * the way and are not on a tty */ | |
518 | if (e == EXEC_OUTPUT_INHERIT && | |
519 | o == EXEC_OUTPUT_INHERIT && | |
520 | i == EXEC_INPUT_NULL && | |
521 | !is_terminal_input(context->std_input) && | |
522 | getppid () != 1) | |
523 | return fileno; | |
524 | ||
525 | /* Duplicate from stdout if possible */ | |
526 | if (e == o || e == EXEC_OUTPUT_INHERIT) | |
527 | return dup2(STDOUT_FILENO, fileno) < 0 ? -errno : fileno; | |
071830ff | 528 | |
eb17e935 | 529 | o = e; |
80876c20 | 530 | |
eb17e935 | 531 | } else if (o == EXEC_OUTPUT_INHERIT) { |
21d21ea4 LP |
532 | /* If input got downgraded, inherit the original value */ |
533 | if (i == EXEC_INPUT_NULL && is_terminal_input(context->std_input)) | |
1e22b5cd | 534 | return open_terminal_as(exec_context_tty_path(context), O_WRONLY, fileno); |
21d21ea4 | 535 | |
acb591e4 | 536 | /* If the input is connected to anything that's not a /dev/null, inherit that... */ |
ff876e28 | 537 | if (i != EXEC_INPUT_NULL) |
eb17e935 | 538 | return dup2(STDIN_FILENO, fileno) < 0 ? -errno : fileno; |
071830ff | 539 | |
acb591e4 LP |
540 | /* If we are not started from PID 1 we just inherit STDOUT from our parent process. */ |
541 | if (getppid() != 1) | |
eb17e935 | 542 | return fileno; |
94f04347 | 543 | |
eb17e935 MS |
544 | /* We need to open /dev/null here anew, to get the right access mode. */ |
545 | return open_null_as(O_WRONLY, fileno); | |
071830ff | 546 | } |
94f04347 | 547 | |
eb17e935 | 548 | switch (o) { |
80876c20 LP |
549 | |
550 | case EXEC_OUTPUT_NULL: | |
eb17e935 | 551 | return open_null_as(O_WRONLY, fileno); |
80876c20 LP |
552 | |
553 | case EXEC_OUTPUT_TTY: | |
4f2d528d | 554 | if (is_terminal_input(i)) |
eb17e935 | 555 | return dup2(STDIN_FILENO, fileno) < 0 ? -errno : fileno; |
80876c20 LP |
556 | |
557 | /* We don't reset the terminal if this is just about output */ | |
1e22b5cd | 558 | return open_terminal_as(exec_context_tty_path(context), O_WRONLY, fileno); |
80876c20 LP |
559 | |
560 | case EXEC_OUTPUT_SYSLOG: | |
28dbc1e8 | 561 | case EXEC_OUTPUT_SYSLOG_AND_CONSOLE: |
9a6bca7a | 562 | case EXEC_OUTPUT_KMSG: |
28dbc1e8 | 563 | case EXEC_OUTPUT_KMSG_AND_CONSOLE: |
706343f4 LP |
564 | case EXEC_OUTPUT_JOURNAL: |
565 | case EXEC_OUTPUT_JOURNAL_AND_CONSOLE: | |
7a1ab780 | 566 | r = connect_logger_as(unit, context, o, ident, fileno, uid, gid); |
47c1d80d | 567 | if (r < 0) { |
f2341e0a | 568 | log_unit_error_errno(unit, r, "Failed to connect %s to the journal socket, ignoring: %m", fileno == STDOUT_FILENO ? "stdout" : "stderr"); |
eb17e935 | 569 | r = open_null_as(O_WRONLY, fileno); |
7bce046b LP |
570 | } else { |
571 | struct stat st; | |
572 | ||
573 | /* If we connected this fd to the journal via a stream, patch the device/inode into the passed | |
574 | * parameters, but only then. This is useful so that we can set $JOURNAL_STREAM that permits | |
575 | * services to detect whether they are connected to the journal or not. */ | |
576 | ||
577 | if (fstat(fileno, &st) >= 0) { | |
578 | *journal_stream_dev = st.st_dev; | |
579 | *journal_stream_ino = st.st_ino; | |
580 | } | |
47c1d80d MS |
581 | } |
582 | return r; | |
4f2d528d LP |
583 | |
584 | case EXEC_OUTPUT_SOCKET: | |
585 | assert(socket_fd >= 0); | |
eb17e935 | 586 | return dup2(socket_fd, fileno) < 0 ? -errno : fileno; |
94f04347 LP |
587 | |
588 | default: | |
80876c20 | 589 | assert_not_reached("Unknown error type"); |
94f04347 | 590 | } |
071830ff LP |
591 | } |
592 | ||
02a51aba LP |
593 | static int chown_terminal(int fd, uid_t uid) { |
594 | struct stat st; | |
595 | ||
596 | assert(fd >= 0); | |
02a51aba | 597 | |
1ff74fb6 LP |
598 | /* Before we chown/chmod the TTY, let's ensure this is actually a tty */ |
599 | if (isatty(fd) < 1) | |
600 | return 0; | |
601 | ||
02a51aba | 602 | /* This might fail. What matters are the results. */ |
bab45044 LP |
603 | (void) fchown(fd, uid, -1); |
604 | (void) fchmod(fd, TTY_MODE); | |
02a51aba LP |
605 | |
606 | if (fstat(fd, &st) < 0) | |
607 | return -errno; | |
608 | ||
d8b4e2e9 | 609 | if (st.st_uid != uid || (st.st_mode & 0777) != TTY_MODE) |
02a51aba LP |
610 | return -EPERM; |
611 | ||
612 | return 0; | |
613 | } | |
614 | ||
3d18b167 LP |
615 | static int setup_confirm_stdio(int *_saved_stdin, int *_saved_stdout) { |
616 | _cleanup_close_ int fd = -1, saved_stdin = -1, saved_stdout = -1; | |
617 | int r; | |
80876c20 | 618 | |
80876c20 LP |
619 | assert(_saved_stdin); |
620 | assert(_saved_stdout); | |
621 | ||
af6da548 LP |
622 | saved_stdin = fcntl(STDIN_FILENO, F_DUPFD, 3); |
623 | if (saved_stdin < 0) | |
624 | return -errno; | |
80876c20 | 625 | |
af6da548 | 626 | saved_stdout = fcntl(STDOUT_FILENO, F_DUPFD, 3); |
3d18b167 LP |
627 | if (saved_stdout < 0) |
628 | return -errno; | |
80876c20 | 629 | |
af6da548 LP |
630 | fd = acquire_terminal( |
631 | "/dev/console", | |
632 | false, | |
633 | false, | |
634 | false, | |
635 | DEFAULT_CONFIRM_USEC); | |
3d18b167 LP |
636 | if (fd < 0) |
637 | return fd; | |
80876c20 | 638 | |
af6da548 LP |
639 | r = chown_terminal(fd, getuid()); |
640 | if (r < 0) | |
3d18b167 | 641 | return r; |
02a51aba | 642 | |
3d18b167 LP |
643 | r = reset_terminal_fd(fd, true); |
644 | if (r < 0) | |
645 | return r; | |
80876c20 | 646 | |
3d18b167 LP |
647 | if (dup2(fd, STDIN_FILENO) < 0) |
648 | return -errno; | |
649 | ||
650 | if (dup2(fd, STDOUT_FILENO) < 0) | |
651 | return -errno; | |
80876c20 LP |
652 | |
653 | if (fd >= 2) | |
03e334a1 | 654 | safe_close(fd); |
3d18b167 | 655 | fd = -1; |
80876c20 LP |
656 | |
657 | *_saved_stdin = saved_stdin; | |
658 | *_saved_stdout = saved_stdout; | |
659 | ||
3d18b167 | 660 | saved_stdin = saved_stdout = -1; |
80876c20 | 661 | |
3d18b167 | 662 | return 0; |
80876c20 LP |
663 | } |
664 | ||
44b601bc | 665 | _printf_(1, 2) static int write_confirm_message(const char *format, ...) { |
03e334a1 | 666 | _cleanup_close_ int fd = -1; |
af6da548 | 667 | va_list ap; |
80876c20 | 668 | |
af6da548 | 669 | assert(format); |
80876c20 | 670 | |
af6da548 LP |
671 | fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); |
672 | if (fd < 0) | |
673 | return fd; | |
80876c20 | 674 | |
af6da548 LP |
675 | va_start(ap, format); |
676 | vdprintf(fd, format, ap); | |
677 | va_end(ap); | |
80876c20 | 678 | |
af6da548 LP |
679 | return 0; |
680 | } | |
80876c20 | 681 | |
3d18b167 | 682 | static int restore_confirm_stdio(int *saved_stdin, int *saved_stdout) { |
af6da548 | 683 | int r = 0; |
80876c20 | 684 | |
af6da548 LP |
685 | assert(saved_stdin); |
686 | assert(saved_stdout); | |
687 | ||
688 | release_terminal(); | |
689 | ||
690 | if (*saved_stdin >= 0) | |
80876c20 | 691 | if (dup2(*saved_stdin, STDIN_FILENO) < 0) |
af6da548 | 692 | r = -errno; |
80876c20 | 693 | |
af6da548 | 694 | if (*saved_stdout >= 0) |
80876c20 | 695 | if (dup2(*saved_stdout, STDOUT_FILENO) < 0) |
af6da548 | 696 | r = -errno; |
80876c20 | 697 | |
3d18b167 LP |
698 | *saved_stdin = safe_close(*saved_stdin); |
699 | *saved_stdout = safe_close(*saved_stdout); | |
af6da548 LP |
700 | |
701 | return r; | |
702 | } | |
703 | ||
704 | static int ask_for_confirmation(char *response, char **argv) { | |
705 | int saved_stdout = -1, saved_stdin = -1, r; | |
e1d75803 | 706 | _cleanup_free_ char *line = NULL; |
af6da548 LP |
707 | |
708 | r = setup_confirm_stdio(&saved_stdin, &saved_stdout); | |
709 | if (r < 0) | |
710 | return r; | |
711 | ||
712 | line = exec_command_line(argv); | |
713 | if (!line) | |
714 | return -ENOMEM; | |
715 | ||
418b9be5 | 716 | r = ask_char(response, "yns", "Execute %s? [Yes, No, Skip] ", line); |
af6da548 LP |
717 | |
718 | restore_confirm_stdio(&saved_stdin, &saved_stdout); | |
719 | ||
720 | return r; | |
80876c20 LP |
721 | } |
722 | ||
81a2b7ce LP |
723 | static int enforce_groups(const ExecContext *context, const char *username, gid_t gid) { |
724 | bool keep_groups = false; | |
725 | int r; | |
726 | ||
727 | assert(context); | |
728 | ||
35b8ca3a | 729 | /* Lookup and set GID and supplementary group list. Here too |
81a2b7ce LP |
730 | * we avoid NSS lookups for gid=0. */ |
731 | ||
732 | if (context->group || username) { | |
81a2b7ce LP |
733 | /* First step, initialize groups from /etc/groups */ |
734 | if (username && gid != 0) { | |
735 | if (initgroups(username, gid) < 0) | |
736 | return -errno; | |
737 | ||
738 | keep_groups = true; | |
739 | } | |
740 | ||
741 | /* Second step, set our gids */ | |
742 | if (setresgid(gid, gid, gid) < 0) | |
743 | return -errno; | |
744 | } | |
745 | ||
746 | if (context->supplementary_groups) { | |
747 | int ngroups_max, k; | |
748 | gid_t *gids; | |
749 | char **i; | |
750 | ||
751 | /* Final step, initialize any manually set supplementary groups */ | |
da19d5c1 | 752 | assert_se((ngroups_max = (int) sysconf(_SC_NGROUPS_MAX)) > 0); |
81a2b7ce LP |
753 | |
754 | if (!(gids = new(gid_t, ngroups_max))) | |
755 | return -ENOMEM; | |
756 | ||
757 | if (keep_groups) { | |
3cc2aff1 LP |
758 | k = getgroups(ngroups_max, gids); |
759 | if (k < 0) { | |
81a2b7ce LP |
760 | free(gids); |
761 | return -errno; | |
762 | } | |
763 | } else | |
764 | k = 0; | |
765 | ||
766 | STRV_FOREACH(i, context->supplementary_groups) { | |
4b67834e | 767 | const char *g; |
81a2b7ce LP |
768 | |
769 | if (k >= ngroups_max) { | |
770 | free(gids); | |
771 | return -E2BIG; | |
772 | } | |
773 | ||
4b67834e LP |
774 | g = *i; |
775 | r = get_group_creds(&g, gids+k); | |
776 | if (r < 0) { | |
81a2b7ce LP |
777 | free(gids); |
778 | return r; | |
779 | } | |
780 | ||
781 | k++; | |
782 | } | |
783 | ||
784 | if (setgroups(k, gids) < 0) { | |
785 | free(gids); | |
786 | return -errno; | |
787 | } | |
788 | ||
789 | free(gids); | |
790 | } | |
791 | ||
792 | return 0; | |
793 | } | |
794 | ||
795 | static int enforce_user(const ExecContext *context, uid_t uid) { | |
81a2b7ce LP |
796 | assert(context); |
797 | ||
479050b3 | 798 | /* Sets (but doesn't look up) the uid and make sure we keep the |
81a2b7ce LP |
799 | * capabilities while doing so. */ |
800 | ||
479050b3 | 801 | if (context->capability_ambient_set != 0) { |
81a2b7ce LP |
802 | |
803 | /* First step: If we need to keep capabilities but | |
804 | * drop privileges we need to make sure we keep our | |
cbb21cca | 805 | * caps, while we drop privileges. */ |
693ced48 | 806 | if (uid != 0) { |
cbb21cca | 807 | int sb = context->secure_bits | 1<<SECURE_KEEP_CAPS; |
693ced48 LP |
808 | |
809 | if (prctl(PR_GET_SECUREBITS) != sb) | |
810 | if (prctl(PR_SET_SECUREBITS, sb) < 0) | |
811 | return -errno; | |
812 | } | |
81a2b7ce LP |
813 | } |
814 | ||
479050b3 | 815 | /* Second step: actually set the uids */ |
81a2b7ce LP |
816 | if (setresuid(uid, uid, uid) < 0) |
817 | return -errno; | |
818 | ||
819 | /* At this point we should have all necessary capabilities but | |
820 | are otherwise a normal user. However, the caps might got | |
821 | corrupted due to the setresuid() so we need clean them up | |
822 | later. This is done outside of this call. */ | |
823 | ||
824 | return 0; | |
825 | } | |
826 | ||
5b6319dc LP |
827 | #ifdef HAVE_PAM |
828 | ||
829 | static int null_conv( | |
830 | int num_msg, | |
831 | const struct pam_message **msg, | |
832 | struct pam_response **resp, | |
833 | void *appdata_ptr) { | |
834 | ||
835 | /* We don't support conversations */ | |
836 | ||
837 | return PAM_CONV_ERR; | |
838 | } | |
839 | ||
840 | static int setup_pam( | |
841 | const char *name, | |
842 | const char *user, | |
940c5210 | 843 | uid_t uid, |
5b6319dc | 844 | const char *tty, |
2065ca69 | 845 | char ***env, |
5b6319dc LP |
846 | int fds[], unsigned n_fds) { |
847 | ||
848 | static const struct pam_conv conv = { | |
849 | .conv = null_conv, | |
850 | .appdata_ptr = NULL | |
851 | }; | |
852 | ||
2d7c6aa2 | 853 | _cleanup_(barrier_destroy) Barrier barrier = BARRIER_NULL; |
5b6319dc | 854 | pam_handle_t *handle = NULL; |
d6e5f3ad | 855 | sigset_t old_ss; |
7bb70b6e | 856 | int pam_code = PAM_SUCCESS, r; |
84eada2f | 857 | char **nv, **e = NULL; |
5b6319dc LP |
858 | bool close_session = false; |
859 | pid_t pam_pid = 0, parent_pid; | |
970edce6 | 860 | int flags = 0; |
5b6319dc LP |
861 | |
862 | assert(name); | |
863 | assert(user); | |
2065ca69 | 864 | assert(env); |
5b6319dc LP |
865 | |
866 | /* We set up PAM in the parent process, then fork. The child | |
35b8ca3a | 867 | * will then stay around until killed via PR_GET_PDEATHSIG or |
5b6319dc LP |
868 | * systemd via the cgroup logic. It will then remove the PAM |
869 | * session again. The parent process will exec() the actual | |
870 | * daemon. We do things this way to ensure that the main PID | |
871 | * of the daemon is the one we initially fork()ed. */ | |
872 | ||
7bb70b6e LP |
873 | r = barrier_create(&barrier); |
874 | if (r < 0) | |
2d7c6aa2 DH |
875 | goto fail; |
876 | ||
553d2243 | 877 | if (log_get_max_level() < LOG_DEBUG) |
970edce6 ZJS |
878 | flags |= PAM_SILENT; |
879 | ||
f546241b ZJS |
880 | pam_code = pam_start(name, user, &conv, &handle); |
881 | if (pam_code != PAM_SUCCESS) { | |
5b6319dc LP |
882 | handle = NULL; |
883 | goto fail; | |
884 | } | |
885 | ||
f546241b ZJS |
886 | if (tty) { |
887 | pam_code = pam_set_item(handle, PAM_TTY, tty); | |
888 | if (pam_code != PAM_SUCCESS) | |
5b6319dc | 889 | goto fail; |
f546241b | 890 | } |
5b6319dc | 891 | |
84eada2f JW |
892 | STRV_FOREACH(nv, *env) { |
893 | pam_code = pam_putenv(handle, *nv); | |
2065ca69 JW |
894 | if (pam_code != PAM_SUCCESS) |
895 | goto fail; | |
896 | } | |
897 | ||
970edce6 | 898 | pam_code = pam_acct_mgmt(handle, flags); |
f546241b | 899 | if (pam_code != PAM_SUCCESS) |
5b6319dc LP |
900 | goto fail; |
901 | ||
970edce6 | 902 | pam_code = pam_open_session(handle, flags); |
f546241b | 903 | if (pam_code != PAM_SUCCESS) |
5b6319dc LP |
904 | goto fail; |
905 | ||
906 | close_session = true; | |
907 | ||
f546241b ZJS |
908 | e = pam_getenvlist(handle); |
909 | if (!e) { | |
5b6319dc LP |
910 | pam_code = PAM_BUF_ERR; |
911 | goto fail; | |
912 | } | |
913 | ||
914 | /* Block SIGTERM, so that we know that it won't get lost in | |
915 | * the child */ | |
ce30c8dc | 916 | |
72c0a2c2 | 917 | assert_se(sigprocmask_many(SIG_BLOCK, &old_ss, SIGTERM, -1) >= 0); |
5b6319dc LP |
918 | |
919 | parent_pid = getpid(); | |
920 | ||
f546241b | 921 | pam_pid = fork(); |
7bb70b6e LP |
922 | if (pam_pid < 0) { |
923 | r = -errno; | |
5b6319dc | 924 | goto fail; |
7bb70b6e | 925 | } |
5b6319dc LP |
926 | |
927 | if (pam_pid == 0) { | |
7bb70b6e | 928 | int sig, ret = EXIT_PAM; |
5b6319dc LP |
929 | |
930 | /* The child's job is to reset the PAM session on | |
931 | * termination */ | |
2d7c6aa2 | 932 | barrier_set_role(&barrier, BARRIER_CHILD); |
5b6319dc LP |
933 | |
934 | /* This string must fit in 10 chars (i.e. the length | |
5d6b1584 LP |
935 | * of "/sbin/init"), to look pretty in /bin/ps */ |
936 | rename_process("(sd-pam)"); | |
5b6319dc LP |
937 | |
938 | /* Make sure we don't keep open the passed fds in this | |
939 | child. We assume that otherwise only those fds are | |
940 | open here that have been opened by PAM. */ | |
941 | close_many(fds, n_fds); | |
942 | ||
940c5210 AK |
943 | /* Drop privileges - we don't need any to pam_close_session |
944 | * and this will make PR_SET_PDEATHSIG work in most cases. | |
945 | * If this fails, ignore the error - but expect sd-pam threads | |
946 | * to fail to exit normally */ | |
947 | if (setresuid(uid, uid, uid) < 0) | |
da927ba9 | 948 | log_error_errno(r, "Error: Failed to setresuid() in sd-pam: %m"); |
940c5210 | 949 | |
ce30c8dc LP |
950 | (void) ignore_signals(SIGPIPE, -1); |
951 | ||
940c5210 AK |
952 | /* Wait until our parent died. This will only work if |
953 | * the above setresuid() succeeds, otherwise the kernel | |
954 | * will not allow unprivileged parents kill their privileged | |
955 | * children this way. We rely on the control groups kill logic | |
5b6319dc LP |
956 | * to do the rest for us. */ |
957 | if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0) | |
958 | goto child_finish; | |
959 | ||
2d7c6aa2 DH |
960 | /* Tell the parent that our setup is done. This is especially |
961 | * important regarding dropping privileges. Otherwise, unit | |
962 | * setup might race against our setresuid(2) call. */ | |
963 | barrier_place(&barrier); | |
964 | ||
5b6319dc LP |
965 | /* Check if our parent process might already have |
966 | * died? */ | |
967 | if (getppid() == parent_pid) { | |
d6e5f3ad DM |
968 | sigset_t ss; |
969 | ||
970 | assert_se(sigemptyset(&ss) >= 0); | |
971 | assert_se(sigaddset(&ss, SIGTERM) >= 0); | |
972 | ||
3dead8d9 LP |
973 | for (;;) { |
974 | if (sigwait(&ss, &sig) < 0) { | |
975 | if (errno == EINTR) | |
976 | continue; | |
977 | ||
978 | goto child_finish; | |
979 | } | |
5b6319dc | 980 | |
3dead8d9 LP |
981 | assert(sig == SIGTERM); |
982 | break; | |
983 | } | |
5b6319dc LP |
984 | } |
985 | ||
3dead8d9 | 986 | /* If our parent died we'll end the session */ |
f546241b | 987 | if (getppid() != parent_pid) { |
970edce6 | 988 | pam_code = pam_close_session(handle, flags); |
f546241b | 989 | if (pam_code != PAM_SUCCESS) |
5b6319dc | 990 | goto child_finish; |
f546241b | 991 | } |
5b6319dc | 992 | |
7bb70b6e | 993 | ret = 0; |
5b6319dc LP |
994 | |
995 | child_finish: | |
970edce6 | 996 | pam_end(handle, pam_code | flags); |
7bb70b6e | 997 | _exit(ret); |
5b6319dc LP |
998 | } |
999 | ||
2d7c6aa2 DH |
1000 | barrier_set_role(&barrier, BARRIER_PARENT); |
1001 | ||
5b6319dc LP |
1002 | /* If the child was forked off successfully it will do all the |
1003 | * cleanups, so forget about the handle here. */ | |
1004 | handle = NULL; | |
1005 | ||
3b8bddde | 1006 | /* Unblock SIGTERM again in the parent */ |
72c0a2c2 | 1007 | assert_se(sigprocmask(SIG_SETMASK, &old_ss, NULL) >= 0); |
5b6319dc LP |
1008 | |
1009 | /* We close the log explicitly here, since the PAM modules | |
1010 | * might have opened it, but we don't want this fd around. */ | |
1011 | closelog(); | |
1012 | ||
2d7c6aa2 DH |
1013 | /* Synchronously wait for the child to initialize. We don't care for |
1014 | * errors as we cannot recover. However, warn loudly if it happens. */ | |
1015 | if (!barrier_place_and_sync(&barrier)) | |
1016 | log_error("PAM initialization failed"); | |
1017 | ||
2065ca69 JW |
1018 | strv_free(*env); |
1019 | *env = e; | |
aa87e624 | 1020 | |
5b6319dc LP |
1021 | return 0; |
1022 | ||
1023 | fail: | |
970edce6 ZJS |
1024 | if (pam_code != PAM_SUCCESS) { |
1025 | log_error("PAM failed: %s", pam_strerror(handle, pam_code)); | |
7bb70b6e LP |
1026 | r = -EPERM; /* PAM errors do not map to errno */ |
1027 | } else | |
1028 | log_error_errno(r, "PAM failed: %m"); | |
9ba35398 | 1029 | |
5b6319dc LP |
1030 | if (handle) { |
1031 | if (close_session) | |
970edce6 | 1032 | pam_code = pam_close_session(handle, flags); |
5b6319dc | 1033 | |
970edce6 | 1034 | pam_end(handle, pam_code | flags); |
5b6319dc LP |
1035 | } |
1036 | ||
1037 | strv_free(e); | |
5b6319dc LP |
1038 | closelog(); |
1039 | ||
7bb70b6e | 1040 | return r; |
5b6319dc LP |
1041 | } |
1042 | #endif | |
1043 | ||
5d6b1584 LP |
1044 | static void rename_process_from_path(const char *path) { |
1045 | char process_name[11]; | |
1046 | const char *p; | |
1047 | size_t l; | |
1048 | ||
1049 | /* This resulting string must fit in 10 chars (i.e. the length | |
1050 | * of "/sbin/init") to look pretty in /bin/ps */ | |
1051 | ||
2b6bf07d | 1052 | p = basename(path); |
5d6b1584 LP |
1053 | if (isempty(p)) { |
1054 | rename_process("(...)"); | |
1055 | return; | |
1056 | } | |
1057 | ||
1058 | l = strlen(p); | |
1059 | if (l > 8) { | |
1060 | /* The end of the process name is usually more | |
1061 | * interesting, since the first bit might just be | |
1062 | * "systemd-" */ | |
1063 | p = p + l - 8; | |
1064 | l = 8; | |
1065 | } | |
1066 | ||
1067 | process_name[0] = '('; | |
1068 | memcpy(process_name+1, p, l); | |
1069 | process_name[1+l] = ')'; | |
1070 | process_name[1+l+1] = 0; | |
1071 | ||
1072 | rename_process(process_name); | |
1073 | } | |
1074 | ||
c0467cf3 | 1075 | #ifdef HAVE_SECCOMP |
17df7223 | 1076 | |
83f12b27 FS |
1077 | static bool skip_seccomp_unavailable(const Unit* u, const char* msg) { |
1078 | if (!is_seccomp_available()) { | |
1079 | log_open(); | |
d347d902 | 1080 | log_unit_debug(u, "SECCOMP features not detected in the kernel, skipping %s", msg); |
83f12b27 FS |
1081 | log_close(); |
1082 | return true; | |
1083 | } | |
1084 | return false; | |
1085 | } | |
1086 | ||
1087 | static int apply_seccomp(const Unit* u, const ExecContext *c) { | |
17df7223 LP |
1088 | uint32_t negative_action, action; |
1089 | scmp_filter_ctx *seccomp; | |
c0467cf3 RC |
1090 | Iterator i; |
1091 | void *id; | |
17df7223 | 1092 | int r; |
8351ceae | 1093 | |
c0467cf3 | 1094 | assert(c); |
8351ceae | 1095 | |
83f12b27 FS |
1096 | if (skip_seccomp_unavailable(u, "syscall filtering")) |
1097 | return 0; | |
1098 | ||
17df7223 LP |
1099 | negative_action = c->syscall_errno == 0 ? SCMP_ACT_KILL : SCMP_ACT_ERRNO(c->syscall_errno); |
1100 | ||
1101 | seccomp = seccomp_init(c->syscall_whitelist ? negative_action : SCMP_ACT_ALLOW); | |
1102 | if (!seccomp) | |
1103 | return -ENOMEM; | |
8351ceae | 1104 | |
e9642be2 LP |
1105 | if (c->syscall_archs) { |
1106 | ||
1107 | SET_FOREACH(id, c->syscall_archs, i) { | |
1108 | r = seccomp_arch_add(seccomp, PTR_TO_UINT32(id) - 1); | |
1109 | if (r == -EEXIST) | |
1110 | continue; | |
7c66bae2 LP |
1111 | if (r < 0) |
1112 | goto finish; | |
e9642be2 | 1113 | } |
e9642be2 | 1114 | |
7c66bae2 | 1115 | } else { |
e9642be2 | 1116 | r = seccomp_add_secondary_archs(seccomp); |
7c66bae2 LP |
1117 | if (r < 0) |
1118 | goto finish; | |
57183d11 | 1119 | } |
8351ceae | 1120 | |
57183d11 | 1121 | action = c->syscall_whitelist ? SCMP_ACT_ALLOW : negative_action; |
17df7223 LP |
1122 | SET_FOREACH(id, c->syscall_filter, i) { |
1123 | r = seccomp_rule_add(seccomp, action, PTR_TO_INT(id) - 1, 0); | |
7c66bae2 LP |
1124 | if (r < 0) |
1125 | goto finish; | |
c0467cf3 | 1126 | } |
8351ceae | 1127 | |
7c66bae2 LP |
1128 | r = seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_NNP, 0); |
1129 | if (r < 0) | |
1130 | goto finish; | |
1131 | ||
17df7223 | 1132 | r = seccomp_load(seccomp); |
7c66bae2 LP |
1133 | |
1134 | finish: | |
17df7223 | 1135 | seccomp_release(seccomp); |
4298d0b5 LP |
1136 | return r; |
1137 | } | |
1138 | ||
83f12b27 | 1139 | static int apply_address_families(const Unit* u, const ExecContext *c) { |
4298d0b5 LP |
1140 | scmp_filter_ctx *seccomp; |
1141 | Iterator i; | |
1142 | int r; | |
1143 | ||
1144 | assert(c); | |
1145 | ||
83f12b27 FS |
1146 | if (skip_seccomp_unavailable(u, "RestrictAddressFamilies=")) |
1147 | return 0; | |
1148 | ||
4298d0b5 LP |
1149 | seccomp = seccomp_init(SCMP_ACT_ALLOW); |
1150 | if (!seccomp) | |
1151 | return -ENOMEM; | |
1152 | ||
1153 | r = seccomp_add_secondary_archs(seccomp); | |
1154 | if (r < 0) | |
1155 | goto finish; | |
1156 | ||
1157 | if (c->address_families_whitelist) { | |
1158 | int af, first = 0, last = 0; | |
1159 | void *afp; | |
1160 | ||
1161 | /* If this is a whitelist, we first block the address | |
1162 | * families that are out of range and then everything | |
1163 | * that is not in the set. First, we find the lowest | |
1164 | * and highest address family in the set. */ | |
1165 | ||
1166 | SET_FOREACH(afp, c->address_families, i) { | |
1167 | af = PTR_TO_INT(afp); | |
17df7223 | 1168 | |
4298d0b5 LP |
1169 | if (af <= 0 || af >= af_max()) |
1170 | continue; | |
1171 | ||
1172 | if (first == 0 || af < first) | |
1173 | first = af; | |
1174 | ||
1175 | if (last == 0 || af > last) | |
1176 | last = af; | |
1177 | } | |
1178 | ||
1179 | assert((first == 0) == (last == 0)); | |
1180 | ||
1181 | if (first == 0) { | |
1182 | ||
1183 | /* No entries in the valid range, block everything */ | |
1184 | r = seccomp_rule_add( | |
1185 | seccomp, | |
1186 | SCMP_ACT_ERRNO(EPROTONOSUPPORT), | |
1187 | SCMP_SYS(socket), | |
1188 | 0); | |
1189 | if (r < 0) | |
1190 | goto finish; | |
1191 | ||
1192 | } else { | |
1193 | ||
1194 | /* Block everything below the first entry */ | |
1195 | r = seccomp_rule_add( | |
1196 | seccomp, | |
1197 | SCMP_ACT_ERRNO(EPROTONOSUPPORT), | |
1198 | SCMP_SYS(socket), | |
1199 | 1, | |
1200 | SCMP_A0(SCMP_CMP_LT, first)); | |
1201 | if (r < 0) | |
1202 | goto finish; | |
1203 | ||
1204 | /* Block everything above the last entry */ | |
1205 | r = seccomp_rule_add( | |
1206 | seccomp, | |
1207 | SCMP_ACT_ERRNO(EPROTONOSUPPORT), | |
1208 | SCMP_SYS(socket), | |
1209 | 1, | |
1210 | SCMP_A0(SCMP_CMP_GT, last)); | |
1211 | if (r < 0) | |
1212 | goto finish; | |
1213 | ||
1214 | /* Block everything between the first and last | |
1215 | * entry */ | |
1216 | for (af = 1; af < af_max(); af++) { | |
1217 | ||
1218 | if (set_contains(c->address_families, INT_TO_PTR(af))) | |
1219 | continue; | |
1220 | ||
1221 | r = seccomp_rule_add( | |
1222 | seccomp, | |
1223 | SCMP_ACT_ERRNO(EPROTONOSUPPORT), | |
1224 | SCMP_SYS(socket), | |
1225 | 1, | |
1226 | SCMP_A0(SCMP_CMP_EQ, af)); | |
1227 | if (r < 0) | |
1228 | goto finish; | |
1229 | } | |
1230 | } | |
1231 | ||
1232 | } else { | |
1233 | void *af; | |
1234 | ||
1235 | /* If this is a blacklist, then generate one rule for | |
1236 | * each address family that are then combined in OR | |
1237 | * checks. */ | |
1238 | ||
1239 | SET_FOREACH(af, c->address_families, i) { | |
1240 | ||
1241 | r = seccomp_rule_add( | |
1242 | seccomp, | |
1243 | SCMP_ACT_ERRNO(EPROTONOSUPPORT), | |
1244 | SCMP_SYS(socket), | |
1245 | 1, | |
1246 | SCMP_A0(SCMP_CMP_EQ, PTR_TO_INT(af))); | |
1247 | if (r < 0) | |
1248 | goto finish; | |
1249 | } | |
1250 | } | |
1251 | ||
1252 | r = seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_NNP, 0); | |
1253 | if (r < 0) | |
1254 | goto finish; | |
1255 | ||
1256 | r = seccomp_load(seccomp); | |
1257 | ||
1258 | finish: | |
1259 | seccomp_release(seccomp); | |
17df7223 | 1260 | return r; |
8351ceae | 1261 | } |
4298d0b5 | 1262 | |
83f12b27 | 1263 | static int apply_memory_deny_write_execute(const Unit* u, const ExecContext *c) { |
f3e43635 TM |
1264 | scmp_filter_ctx *seccomp; |
1265 | int r; | |
1266 | ||
1267 | assert(c); | |
1268 | ||
83f12b27 FS |
1269 | if (skip_seccomp_unavailable(u, "MemoryDenyWriteExecute=")) |
1270 | return 0; | |
1271 | ||
f3e43635 TM |
1272 | seccomp = seccomp_init(SCMP_ACT_ALLOW); |
1273 | if (!seccomp) | |
1274 | return -ENOMEM; | |
1275 | ||
1276 | r = seccomp_rule_add( | |
1277 | seccomp, | |
abd84d4d | 1278 | SCMP_ACT_ERRNO(EPERM), |
f3e43635 TM |
1279 | SCMP_SYS(mmap), |
1280 | 1, | |
1281 | SCMP_A2(SCMP_CMP_MASKED_EQ, PROT_EXEC|PROT_WRITE, PROT_EXEC|PROT_WRITE)); | |
1282 | if (r < 0) | |
1283 | goto finish; | |
1284 | ||
1285 | r = seccomp_rule_add( | |
1286 | seccomp, | |
abd84d4d | 1287 | SCMP_ACT_ERRNO(EPERM), |
f3e43635 TM |
1288 | SCMP_SYS(mprotect), |
1289 | 1, | |
1290 | SCMP_A2(SCMP_CMP_MASKED_EQ, PROT_EXEC, PROT_EXEC)); | |
1291 | if (r < 0) | |
1292 | goto finish; | |
1293 | ||
1294 | r = seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_NNP, 0); | |
1295 | if (r < 0) | |
1296 | goto finish; | |
1297 | ||
1298 | r = seccomp_load(seccomp); | |
1299 | ||
1300 | finish: | |
1301 | seccomp_release(seccomp); | |
1302 | return r; | |
1303 | } | |
1304 | ||
83f12b27 | 1305 | static int apply_restrict_realtime(const Unit* u, const ExecContext *c) { |
f4170c67 LP |
1306 | static const int permitted_policies[] = { |
1307 | SCHED_OTHER, | |
1308 | SCHED_BATCH, | |
1309 | SCHED_IDLE, | |
1310 | }; | |
1311 | ||
1312 | scmp_filter_ctx *seccomp; | |
1313 | unsigned i; | |
1314 | int r, p, max_policy = 0; | |
1315 | ||
1316 | assert(c); | |
1317 | ||
83f12b27 FS |
1318 | if (skip_seccomp_unavailable(u, "RestrictRealtime=")) |
1319 | return 0; | |
1320 | ||
f4170c67 LP |
1321 | seccomp = seccomp_init(SCMP_ACT_ALLOW); |
1322 | if (!seccomp) | |
1323 | return -ENOMEM; | |
1324 | ||
1325 | /* Determine the highest policy constant we want to allow */ | |
1326 | for (i = 0; i < ELEMENTSOF(permitted_policies); i++) | |
1327 | if (permitted_policies[i] > max_policy) | |
1328 | max_policy = permitted_policies[i]; | |
1329 | ||
1330 | /* Go through all policies with lower values than that, and block them -- unless they appear in the | |
1331 | * whitelist. */ | |
1332 | for (p = 0; p < max_policy; p++) { | |
1333 | bool good = false; | |
1334 | ||
1335 | /* Check if this is in the whitelist. */ | |
1336 | for (i = 0; i < ELEMENTSOF(permitted_policies); i++) | |
1337 | if (permitted_policies[i] == p) { | |
1338 | good = true; | |
1339 | break; | |
1340 | } | |
1341 | ||
1342 | if (good) | |
1343 | continue; | |
1344 | ||
1345 | /* Deny this policy */ | |
1346 | r = seccomp_rule_add( | |
1347 | seccomp, | |
1348 | SCMP_ACT_ERRNO(EPERM), | |
1349 | SCMP_SYS(sched_setscheduler), | |
1350 | 1, | |
1351 | SCMP_A1(SCMP_CMP_EQ, p)); | |
1352 | if (r < 0) | |
1353 | goto finish; | |
1354 | } | |
1355 | ||
1356 | /* Blacklist all other policies, i.e. the ones with higher values. Note that all comparisons are unsigned here, | |
1357 | * hence no need no check for < 0 values. */ | |
1358 | r = seccomp_rule_add( | |
1359 | seccomp, | |
1360 | SCMP_ACT_ERRNO(EPERM), | |
1361 | SCMP_SYS(sched_setscheduler), | |
1362 | 1, | |
1363 | SCMP_A1(SCMP_CMP_GT, max_policy)); | |
1364 | if (r < 0) | |
1365 | goto finish; | |
1366 | ||
1367 | r = seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_NNP, 0); | |
1368 | if (r < 0) | |
1369 | goto finish; | |
1370 | ||
1371 | r = seccomp_load(seccomp); | |
1372 | ||
1373 | finish: | |
1374 | seccomp_release(seccomp); | |
1375 | return r; | |
1376 | } | |
1377 | ||
c0467cf3 | 1378 | #endif |
8351ceae | 1379 | |
31a7eb86 ZJS |
1380 | static void do_idle_pipe_dance(int idle_pipe[4]) { |
1381 | assert(idle_pipe); | |
1382 | ||
03e334a1 | 1383 | |
54eb2300 LP |
1384 | idle_pipe[1] = safe_close(idle_pipe[1]); |
1385 | idle_pipe[2] = safe_close(idle_pipe[2]); | |
31a7eb86 ZJS |
1386 | |
1387 | if (idle_pipe[0] >= 0) { | |
1388 | int r; | |
1389 | ||
1390 | r = fd_wait_for_event(idle_pipe[0], POLLHUP, IDLE_TIMEOUT_USEC); | |
1391 | ||
1392 | if (idle_pipe[3] >= 0 && r == 0 /* timeout */) { | |
c7cc737f LP |
1393 | ssize_t n; |
1394 | ||
31a7eb86 | 1395 | /* Signal systemd that we are bored and want to continue. */ |
c7cc737f LP |
1396 | n = write(idle_pipe[3], "x", 1); |
1397 | if (n > 0) | |
cd972d69 ZJS |
1398 | /* Wait for systemd to react to the signal above. */ |
1399 | fd_wait_for_event(idle_pipe[0], POLLHUP, IDLE_TIMEOUT2_USEC); | |
31a7eb86 ZJS |
1400 | } |
1401 | ||
54eb2300 | 1402 | idle_pipe[0] = safe_close(idle_pipe[0]); |
31a7eb86 ZJS |
1403 | |
1404 | } | |
1405 | ||
54eb2300 | 1406 | idle_pipe[3] = safe_close(idle_pipe[3]); |
31a7eb86 ZJS |
1407 | } |
1408 | ||
7cae38c4 | 1409 | static int build_environment( |
fd63e712 | 1410 | Unit *u, |
9fa95f85 | 1411 | const ExecContext *c, |
1e22b5cd | 1412 | const ExecParameters *p, |
7cae38c4 LP |
1413 | unsigned n_fds, |
1414 | const char *home, | |
1415 | const char *username, | |
1416 | const char *shell, | |
7bce046b LP |
1417 | dev_t journal_stream_dev, |
1418 | ino_t journal_stream_ino, | |
7cae38c4 LP |
1419 | char ***ret) { |
1420 | ||
1421 | _cleanup_strv_free_ char **our_env = NULL; | |
1422 | unsigned n_env = 0; | |
1423 | char *x; | |
1424 | ||
1425 | assert(c); | |
1426 | assert(ret); | |
1427 | ||
fd63e712 | 1428 | our_env = new0(char*, 13); |
7cae38c4 LP |
1429 | if (!our_env) |
1430 | return -ENOMEM; | |
1431 | ||
1432 | if (n_fds > 0) { | |
8dd4c05b LP |
1433 | _cleanup_free_ char *joined = NULL; |
1434 | ||
ccd06097 | 1435 | if (asprintf(&x, "LISTEN_PID="PID_FMT, getpid()) < 0) |
7cae38c4 LP |
1436 | return -ENOMEM; |
1437 | our_env[n_env++] = x; | |
1438 | ||
1439 | if (asprintf(&x, "LISTEN_FDS=%u", n_fds) < 0) | |
1440 | return -ENOMEM; | |
1441 | our_env[n_env++] = x; | |
8dd4c05b | 1442 | |
1e22b5cd | 1443 | joined = strv_join(p->fd_names, ":"); |
8dd4c05b LP |
1444 | if (!joined) |
1445 | return -ENOMEM; | |
1446 | ||
1447 | x = strjoin("LISTEN_FDNAMES=", joined, NULL); | |
1448 | if (!x) | |
1449 | return -ENOMEM; | |
1450 | our_env[n_env++] = x; | |
7cae38c4 LP |
1451 | } |
1452 | ||
b08af3b1 | 1453 | if ((p->flags & EXEC_SET_WATCHDOG) && p->watchdog_usec > 0) { |
ccd06097 | 1454 | if (asprintf(&x, "WATCHDOG_PID="PID_FMT, getpid()) < 0) |
09812eb7 LP |
1455 | return -ENOMEM; |
1456 | our_env[n_env++] = x; | |
1457 | ||
1e22b5cd | 1458 | if (asprintf(&x, "WATCHDOG_USEC="USEC_FMT, p->watchdog_usec) < 0) |
09812eb7 LP |
1459 | return -ENOMEM; |
1460 | our_env[n_env++] = x; | |
1461 | } | |
1462 | ||
fd63e712 LP |
1463 | /* If this is D-Bus, tell the nss-systemd module, since it relies on being able to use D-Bus look up dynamic |
1464 | * users via PID 1, possibly dead-locking the dbus daemon. This way it will not use D-Bus to resolve names, but | |
1465 | * check the database directly. */ | |
1466 | if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) { | |
1467 | x = strdup("SYSTEMD_NSS_BYPASS_BUS=1"); | |
1468 | if (!x) | |
1469 | return -ENOMEM; | |
1470 | our_env[n_env++] = x; | |
1471 | } | |
1472 | ||
7cae38c4 LP |
1473 | if (home) { |
1474 | x = strappend("HOME=", home); | |
1475 | if (!x) | |
1476 | return -ENOMEM; | |
1477 | our_env[n_env++] = x; | |
1478 | } | |
1479 | ||
1480 | if (username) { | |
1481 | x = strappend("LOGNAME=", username); | |
1482 | if (!x) | |
1483 | return -ENOMEM; | |
1484 | our_env[n_env++] = x; | |
1485 | ||
1486 | x = strappend("USER=", username); | |
1487 | if (!x) | |
1488 | return -ENOMEM; | |
1489 | our_env[n_env++] = x; | |
1490 | } | |
1491 | ||
1492 | if (shell) { | |
1493 | x = strappend("SHELL=", shell); | |
1494 | if (!x) | |
1495 | return -ENOMEM; | |
1496 | our_env[n_env++] = x; | |
1497 | } | |
1498 | ||
6af760f3 LP |
1499 | if (exec_context_needs_term(c)) { |
1500 | const char *tty_path, *term = NULL; | |
1501 | ||
1502 | tty_path = exec_context_tty_path(c); | |
1503 | ||
1504 | /* If we are forked off PID 1 and we are supposed to operate on /dev/console, then let's try to inherit | |
1505 | * the $TERM set for PID 1. This is useful for containers so that the $TERM the container manager | |
1506 | * passes to PID 1 ends up all the way in the console login shown. */ | |
1507 | ||
1508 | if (path_equal(tty_path, "/dev/console") && getppid() == 1) | |
1509 | term = getenv("TERM"); | |
1510 | if (!term) | |
1511 | term = default_term_for_tty(tty_path); | |
7cae38c4 | 1512 | |
6af760f3 | 1513 | x = strappend("TERM=", term); |
7cae38c4 LP |
1514 | if (!x) |
1515 | return -ENOMEM; | |
1516 | our_env[n_env++] = x; | |
1517 | } | |
1518 | ||
7bce046b LP |
1519 | if (journal_stream_dev != 0 && journal_stream_ino != 0) { |
1520 | if (asprintf(&x, "JOURNAL_STREAM=" DEV_FMT ":" INO_FMT, journal_stream_dev, journal_stream_ino) < 0) | |
1521 | return -ENOMEM; | |
1522 | ||
1523 | our_env[n_env++] = x; | |
1524 | } | |
1525 | ||
7cae38c4 | 1526 | our_env[n_env++] = NULL; |
7bce046b | 1527 | assert(n_env <= 12); |
7cae38c4 LP |
1528 | |
1529 | *ret = our_env; | |
1530 | our_env = NULL; | |
1531 | ||
1532 | return 0; | |
1533 | } | |
1534 | ||
b4c14404 FB |
1535 | static int build_pass_environment(const ExecContext *c, char ***ret) { |
1536 | _cleanup_strv_free_ char **pass_env = NULL; | |
1537 | size_t n_env = 0, n_bufsize = 0; | |
1538 | char **i; | |
1539 | ||
1540 | STRV_FOREACH(i, c->pass_environment) { | |
1541 | _cleanup_free_ char *x = NULL; | |
1542 | char *v; | |
1543 | ||
1544 | v = getenv(*i); | |
1545 | if (!v) | |
1546 | continue; | |
1547 | x = strjoin(*i, "=", v, NULL); | |
1548 | if (!x) | |
1549 | return -ENOMEM; | |
1550 | if (!GREEDY_REALLOC(pass_env, n_bufsize, n_env + 2)) | |
1551 | return -ENOMEM; | |
1552 | pass_env[n_env++] = x; | |
1553 | pass_env[n_env] = NULL; | |
1554 | x = NULL; | |
1555 | } | |
1556 | ||
1557 | *ret = pass_env; | |
1558 | pass_env = NULL; | |
1559 | ||
1560 | return 0; | |
1561 | } | |
1562 | ||
8b44a3d2 LP |
1563 | static bool exec_needs_mount_namespace( |
1564 | const ExecContext *context, | |
1565 | const ExecParameters *params, | |
1566 | ExecRuntime *runtime) { | |
1567 | ||
1568 | assert(context); | |
1569 | assert(params); | |
1570 | ||
2a624c36 AP |
1571 | if (!strv_isempty(context->read_write_paths) || |
1572 | !strv_isempty(context->read_only_paths) || | |
1573 | !strv_isempty(context->inaccessible_paths)) | |
8b44a3d2 LP |
1574 | return true; |
1575 | ||
1576 | if (context->mount_flags != 0) | |
1577 | return true; | |
1578 | ||
1579 | if (context->private_tmp && runtime && (runtime->tmp_dir || runtime->var_tmp_dir)) | |
1580 | return true; | |
1581 | ||
8b44a3d2 LP |
1582 | if (context->private_devices || |
1583 | context->protect_system != PROTECT_SYSTEM_NO || | |
1584 | context->protect_home != PROTECT_HOME_NO) | |
1585 | return true; | |
1586 | ||
1587 | return false; | |
1588 | } | |
1589 | ||
d251207d LP |
1590 | static int setup_private_users(uid_t uid, gid_t gid) { |
1591 | _cleanup_free_ char *uid_map = NULL, *gid_map = NULL; | |
1592 | _cleanup_close_pair_ int errno_pipe[2] = { -1, -1 }; | |
1593 | _cleanup_close_ int unshare_ready_fd = -1; | |
1594 | _cleanup_(sigkill_waitp) pid_t pid = 0; | |
1595 | uint64_t c = 1; | |
1596 | siginfo_t si; | |
1597 | ssize_t n; | |
1598 | int r; | |
1599 | ||
1600 | /* Set up a user namespace and map root to root, the selected UID/GID to itself, and everything else to | |
1601 | * nobody. In order to be able to write this mapping we need CAP_SETUID in the original user namespace, which | |
1602 | * we however lack after opening the user namespace. To work around this we fork() a temporary child process, | |
1603 | * which waits for the parent to create the new user namespace while staying in the original namespace. The | |
1604 | * child then writes the UID mapping, under full privileges. The parent waits for the child to finish and | |
1605 | * continues execution normally. */ | |
1606 | ||
1607 | if (uid != 0 && uid_is_valid(uid)) | |
1608 | asprintf(&uid_map, | |
1609 | "0 0 1\n" /* Map root → root */ | |
1610 | UID_FMT " " UID_FMT " 1\n", /* Map $UID → $UID */ | |
1611 | uid, uid); /* The case where the above is the same */ | |
1612 | else | |
1613 | uid_map = strdup("0 0 1\n"); | |
1614 | if (!uid_map) | |
1615 | return -ENOMEM; | |
1616 | ||
1617 | if (gid != 0 && gid_is_valid(gid)) | |
1618 | asprintf(&gid_map, | |
1619 | "0 0 1\n" /* Map root → root */ | |
1620 | GID_FMT " " GID_FMT " 1\n", /* Map $GID → $GID */ | |
1621 | gid, gid); | |
1622 | else | |
1623 | gid_map = strdup("0 0 1\n"); /* The case where the above is the same */ | |
1624 | if (!gid_map) | |
1625 | return -ENOMEM; | |
1626 | ||
1627 | /* Create a communication channel so that the parent can tell the child when it finished creating the user | |
1628 | * namespace. */ | |
1629 | unshare_ready_fd = eventfd(0, EFD_CLOEXEC); | |
1630 | if (unshare_ready_fd < 0) | |
1631 | return -errno; | |
1632 | ||
1633 | /* Create a communication channel so that the child can tell the parent a proper error code in case it | |
1634 | * failed. */ | |
1635 | if (pipe2(errno_pipe, O_CLOEXEC) < 0) | |
1636 | return -errno; | |
1637 | ||
1638 | pid = fork(); | |
1639 | if (pid < 0) | |
1640 | return -errno; | |
1641 | ||
1642 | if (pid == 0) { | |
1643 | _cleanup_close_ int fd = -1; | |
1644 | const char *a; | |
1645 | pid_t ppid; | |
1646 | ||
1647 | /* Child process, running in the original user namespace. Let's update the parent's UID/GID map from | |
1648 | * here, after the parent opened its own user namespace. */ | |
1649 | ||
1650 | ppid = getppid(); | |
1651 | errno_pipe[0] = safe_close(errno_pipe[0]); | |
1652 | ||
1653 | /* Wait until the parent unshared the user namespace */ | |
1654 | if (read(unshare_ready_fd, &c, sizeof(c)) < 0) { | |
1655 | r = -errno; | |
1656 | goto child_fail; | |
1657 | } | |
1658 | ||
1659 | /* Disable the setgroups() system call in the child user namespace, for good. */ | |
1660 | a = procfs_file_alloca(ppid, "setgroups"); | |
1661 | fd = open(a, O_WRONLY|O_CLOEXEC); | |
1662 | if (fd < 0) { | |
1663 | if (errno != ENOENT) { | |
1664 | r = -errno; | |
1665 | goto child_fail; | |
1666 | } | |
1667 | ||
1668 | /* If the file is missing the kernel is too old, let's continue anyway. */ | |
1669 | } else { | |
1670 | if (write(fd, "deny\n", 5) < 0) { | |
1671 | r = -errno; | |
1672 | goto child_fail; | |
1673 | } | |
1674 | ||
1675 | fd = safe_close(fd); | |
1676 | } | |
1677 | ||
1678 | /* First write the GID map */ | |
1679 | a = procfs_file_alloca(ppid, "gid_map"); | |
1680 | fd = open(a, O_WRONLY|O_CLOEXEC); | |
1681 | if (fd < 0) { | |
1682 | r = -errno; | |
1683 | goto child_fail; | |
1684 | } | |
1685 | if (write(fd, gid_map, strlen(gid_map)) < 0) { | |
1686 | r = -errno; | |
1687 | goto child_fail; | |
1688 | } | |
1689 | fd = safe_close(fd); | |
1690 | ||
1691 | /* The write the UID map */ | |
1692 | a = procfs_file_alloca(ppid, "uid_map"); | |
1693 | fd = open(a, O_WRONLY|O_CLOEXEC); | |
1694 | if (fd < 0) { | |
1695 | r = -errno; | |
1696 | goto child_fail; | |
1697 | } | |
1698 | if (write(fd, uid_map, strlen(uid_map)) < 0) { | |
1699 | r = -errno; | |
1700 | goto child_fail; | |
1701 | } | |
1702 | ||
1703 | _exit(EXIT_SUCCESS); | |
1704 | ||
1705 | child_fail: | |
1706 | (void) write(errno_pipe[1], &r, sizeof(r)); | |
1707 | _exit(EXIT_FAILURE); | |
1708 | } | |
1709 | ||
1710 | errno_pipe[1] = safe_close(errno_pipe[1]); | |
1711 | ||
1712 | if (unshare(CLONE_NEWUSER) < 0) | |
1713 | return -errno; | |
1714 | ||
1715 | /* Let the child know that the namespace is ready now */ | |
1716 | if (write(unshare_ready_fd, &c, sizeof(c)) < 0) | |
1717 | return -errno; | |
1718 | ||
1719 | /* Try to read an error code from the child */ | |
1720 | n = read(errno_pipe[0], &r, sizeof(r)); | |
1721 | if (n < 0) | |
1722 | return -errno; | |
1723 | if (n == sizeof(r)) { /* an error code was sent to us */ | |
1724 | if (r < 0) | |
1725 | return r; | |
1726 | return -EIO; | |
1727 | } | |
1728 | if (n != 0) /* on success we should have read 0 bytes */ | |
1729 | return -EIO; | |
1730 | ||
1731 | r = wait_for_terminate(pid, &si); | |
1732 | if (r < 0) | |
1733 | return r; | |
1734 | pid = 0; | |
1735 | ||
1736 | /* If something strange happened with the child, let's consider this fatal, too */ | |
1737 | if (si.si_code != CLD_EXITED || si.si_status != 0) | |
1738 | return -EIO; | |
1739 | ||
1740 | return 0; | |
1741 | } | |
1742 | ||
29206d46 LP |
1743 | static void append_socket_pair(int *array, unsigned *n, int pair[2]) { |
1744 | assert(array); | |
1745 | assert(n); | |
1746 | ||
1747 | if (!pair) | |
1748 | return; | |
1749 | ||
1750 | if (pair[0] >= 0) | |
1751 | array[(*n)++] = pair[0]; | |
1752 | if (pair[1] >= 0) | |
1753 | array[(*n)++] = pair[1]; | |
1754 | } | |
1755 | ||
a34ceba6 LP |
1756 | static int close_remaining_fds( |
1757 | const ExecParameters *params, | |
1758 | ExecRuntime *runtime, | |
29206d46 | 1759 | DynamicCreds *dcreds, |
00d9ef85 | 1760 | int user_lookup_fd, |
a34ceba6 LP |
1761 | int socket_fd, |
1762 | int *fds, unsigned n_fds) { | |
1763 | ||
1764 | unsigned n_dont_close = 0; | |
00d9ef85 | 1765 | int dont_close[n_fds + 12]; |
a34ceba6 LP |
1766 | |
1767 | assert(params); | |
1768 | ||
1769 | if (params->stdin_fd >= 0) | |
1770 | dont_close[n_dont_close++] = params->stdin_fd; | |
1771 | if (params->stdout_fd >= 0) | |
1772 | dont_close[n_dont_close++] = params->stdout_fd; | |
1773 | if (params->stderr_fd >= 0) | |
1774 | dont_close[n_dont_close++] = params->stderr_fd; | |
1775 | ||
1776 | if (socket_fd >= 0) | |
1777 | dont_close[n_dont_close++] = socket_fd; | |
1778 | if (n_fds > 0) { | |
1779 | memcpy(dont_close + n_dont_close, fds, sizeof(int) * n_fds); | |
1780 | n_dont_close += n_fds; | |
1781 | } | |
1782 | ||
29206d46 LP |
1783 | if (runtime) |
1784 | append_socket_pair(dont_close, &n_dont_close, runtime->netns_storage_socket); | |
1785 | ||
1786 | if (dcreds) { | |
1787 | if (dcreds->user) | |
1788 | append_socket_pair(dont_close, &n_dont_close, dcreds->user->storage_socket); | |
1789 | if (dcreds->group) | |
1790 | append_socket_pair(dont_close, &n_dont_close, dcreds->group->storage_socket); | |
a34ceba6 LP |
1791 | } |
1792 | ||
00d9ef85 LP |
1793 | if (user_lookup_fd >= 0) |
1794 | dont_close[n_dont_close++] = user_lookup_fd; | |
1795 | ||
a34ceba6 LP |
1796 | return close_all_fds(dont_close, n_dont_close); |
1797 | } | |
1798 | ||
00d9ef85 LP |
1799 | static int send_user_lookup( |
1800 | Unit *unit, | |
1801 | int user_lookup_fd, | |
1802 | uid_t uid, | |
1803 | gid_t gid) { | |
1804 | ||
1805 | assert(unit); | |
1806 | ||
1807 | /* Send the resolved UID/GID to PID 1 after we learnt it. We send a single datagram, containing the UID/GID | |
1808 | * data as well as the unit name. Note that we suppress sending this if no user/group to resolve was | |
1809 | * specified. */ | |
1810 | ||
1811 | if (user_lookup_fd < 0) | |
1812 | return 0; | |
1813 | ||
1814 | if (!uid_is_valid(uid) && !gid_is_valid(gid)) | |
1815 | return 0; | |
1816 | ||
1817 | if (writev(user_lookup_fd, | |
1818 | (struct iovec[]) { | |
1819 | { .iov_base = &uid, .iov_len = sizeof(uid) }, | |
1820 | { .iov_base = &gid, .iov_len = sizeof(gid) }, | |
1821 | { .iov_base = unit->id, .iov_len = strlen(unit->id) }}, 3) < 0) | |
1822 | return -errno; | |
1823 | ||
1824 | return 0; | |
1825 | } | |
1826 | ||
ff0af2a1 | 1827 | static int exec_child( |
f2341e0a | 1828 | Unit *unit, |
ff0af2a1 LP |
1829 | ExecCommand *command, |
1830 | const ExecContext *context, | |
1831 | const ExecParameters *params, | |
1832 | ExecRuntime *runtime, | |
29206d46 | 1833 | DynamicCreds *dcreds, |
ff0af2a1 LP |
1834 | char **argv, |
1835 | int socket_fd, | |
1836 | int *fds, unsigned n_fds, | |
1837 | char **files_env, | |
00d9ef85 | 1838 | int user_lookup_fd, |
ff0af2a1 | 1839 | int *exit_status) { |
d35fbf6b | 1840 | |
2065ca69 | 1841 | _cleanup_strv_free_ char **our_env = NULL, **pass_env = NULL, **accum_env = NULL, **final_argv = NULL; |
9008e1ac | 1842 | _cleanup_free_ char *mac_selinux_context_net = NULL; |
5f5d8eab | 1843 | const char *username = NULL, *home = NULL, *shell = NULL, *wd; |
7bce046b LP |
1844 | dev_t journal_stream_dev = 0; |
1845 | ino_t journal_stream_ino = 0; | |
1846 | bool needs_mount_namespace; | |
fed1e721 LP |
1847 | uid_t uid = UID_INVALID; |
1848 | gid_t gid = GID_INVALID; | |
ff0af2a1 | 1849 | int i, r; |
034c6ed7 | 1850 | |
f2341e0a | 1851 | assert(unit); |
5cb5a6ff LP |
1852 | assert(command); |
1853 | assert(context); | |
d35fbf6b | 1854 | assert(params); |
ff0af2a1 | 1855 | assert(exit_status); |
d35fbf6b DM |
1856 | |
1857 | rename_process_from_path(command->path); | |
1858 | ||
1859 | /* We reset exactly these signals, since they are the | |
1860 | * only ones we set to SIG_IGN in the main daemon. All | |
1861 | * others we leave untouched because we set them to | |
1862 | * SIG_DFL or a valid handler initially, both of which | |
1863 | * will be demoted to SIG_DFL. */ | |
ce30c8dc LP |
1864 | (void) default_signals(SIGNALS_CRASH_HANDLER, |
1865 | SIGNALS_IGNORE, -1); | |
d35fbf6b DM |
1866 | |
1867 | if (context->ignore_sigpipe) | |
ce30c8dc | 1868 | (void) ignore_signals(SIGPIPE, -1); |
d35fbf6b | 1869 | |
ff0af2a1 LP |
1870 | r = reset_signal_mask(); |
1871 | if (r < 0) { | |
1872 | *exit_status = EXIT_SIGNAL_MASK; | |
1873 | return r; | |
d35fbf6b | 1874 | } |
034c6ed7 | 1875 | |
d35fbf6b DM |
1876 | if (params->idle_pipe) |
1877 | do_idle_pipe_dance(params->idle_pipe); | |
4f2d528d | 1878 | |
d35fbf6b DM |
1879 | /* Close sockets very early to make sure we don't |
1880 | * block init reexecution because it cannot bind its | |
1881 | * sockets */ | |
ff0af2a1 | 1882 | |
d35fbf6b | 1883 | log_forget_fds(); |
4f2d528d | 1884 | |
00d9ef85 | 1885 | r = close_remaining_fds(params, runtime, dcreds, user_lookup_fd, socket_fd, fds, n_fds); |
ff0af2a1 LP |
1886 | if (r < 0) { |
1887 | *exit_status = EXIT_FDS; | |
1888 | return r; | |
8c7be95e LP |
1889 | } |
1890 | ||
d35fbf6b DM |
1891 | if (!context->same_pgrp) |
1892 | if (setsid() < 0) { | |
ff0af2a1 | 1893 | *exit_status = EXIT_SETSID; |
d35fbf6b DM |
1894 | return -errno; |
1895 | } | |
9e2f7c11 | 1896 | |
1e22b5cd | 1897 | exec_context_tty_reset(context, params); |
d35fbf6b | 1898 | |
c39f1ce2 | 1899 | if (params->flags & EXEC_CONFIRM_SPAWN) { |
d35fbf6b DM |
1900 | char response; |
1901 | ||
ff0af2a1 LP |
1902 | r = ask_for_confirmation(&response, argv); |
1903 | if (r == -ETIMEDOUT) | |
d35fbf6b | 1904 | write_confirm_message("Confirmation question timed out, assuming positive response.\n"); |
ff0af2a1 LP |
1905 | else if (r < 0) |
1906 | write_confirm_message("Couldn't ask confirmation question, assuming positive response: %s\n", strerror(-r)); | |
d35fbf6b DM |
1907 | else if (response == 's') { |
1908 | write_confirm_message("Skipping execution.\n"); | |
ff0af2a1 | 1909 | *exit_status = EXIT_CONFIRM; |
d35fbf6b DM |
1910 | return -ECANCELED; |
1911 | } else if (response == 'n') { | |
1912 | write_confirm_message("Failing execution.\n"); | |
ff0af2a1 | 1913 | *exit_status = 0; |
d35fbf6b DM |
1914 | return 0; |
1915 | } | |
1916 | } | |
1a63a750 | 1917 | |
29206d46 LP |
1918 | if (context->dynamic_user && dcreds) { |
1919 | ||
409093fe LP |
1920 | /* Make sure we bypass our own NSS module for any NSS checks */ |
1921 | if (putenv((char*) "SYSTEMD_NSS_DYNAMIC_BYPASS=1") != 0) { | |
1922 | *exit_status = EXIT_USER; | |
1923 | return -errno; | |
1924 | } | |
1925 | ||
29206d46 | 1926 | r = dynamic_creds_realize(dcreds, &uid, &gid); |
ff0af2a1 LP |
1927 | if (r < 0) { |
1928 | *exit_status = EXIT_USER; | |
1929 | return r; | |
524daa8c | 1930 | } |
524daa8c | 1931 | |
92b25bca | 1932 | if (!uid_is_valid(uid) || !gid_is_valid(gid)) { |
29206d46 LP |
1933 | *exit_status = EXIT_USER; |
1934 | return -ESRCH; | |
1935 | } | |
5bc7452b | 1936 | |
29206d46 LP |
1937 | if (dcreds->user) |
1938 | username = dcreds->user->name; | |
1939 | ||
1940 | } else { | |
1941 | if (context->user) { | |
1942 | username = context->user; | |
1943 | r = get_user_creds(&username, &uid, &gid, &home, &shell); | |
1944 | if (r < 0) { | |
1945 | *exit_status = EXIT_USER; | |
1946 | return r; | |
1947 | } | |
70493828 LP |
1948 | |
1949 | /* Don't set $HOME or $SHELL if they are are not particularly enlightening anyway. */ | |
1950 | if (isempty(home) || path_equal(home, "/")) | |
1951 | home = NULL; | |
1952 | ||
1953 | if (isempty(shell) || PATH_IN_SET(shell, | |
1954 | "/bin/nologin", | |
1955 | "/sbin/nologin", | |
1956 | "/usr/bin/nologin", | |
1957 | "/usr/sbin/nologin")) | |
1958 | shell = NULL; | |
5bc7452b | 1959 | } |
5bc7452b | 1960 | |
29206d46 LP |
1961 | if (context->group) { |
1962 | const char *g = context->group; | |
1963 | ||
1964 | r = get_group_creds(&g, &gid); | |
1965 | if (r < 0) { | |
1966 | *exit_status = EXIT_GROUP; | |
1967 | return r; | |
1968 | } | |
1969 | } | |
1970 | } | |
5bc7452b | 1971 | |
00d9ef85 LP |
1972 | r = send_user_lookup(unit, user_lookup_fd, uid, gid); |
1973 | if (r < 0) { | |
1974 | *exit_status = EXIT_USER; | |
1975 | return r; | |
1976 | } | |
1977 | ||
1978 | user_lookup_fd = safe_close(user_lookup_fd); | |
1979 | ||
d35fbf6b DM |
1980 | /* If a socket is connected to STDIN/STDOUT/STDERR, we |
1981 | * must sure to drop O_NONBLOCK */ | |
1982 | if (socket_fd >= 0) | |
a34ceba6 | 1983 | (void) fd_nonblock(socket_fd, false); |
acbb0225 | 1984 | |
a34ceba6 | 1985 | r = setup_input(context, params, socket_fd); |
ff0af2a1 LP |
1986 | if (r < 0) { |
1987 | *exit_status = EXIT_STDIN; | |
1988 | return r; | |
d35fbf6b | 1989 | } |
034c6ed7 | 1990 | |
7bce046b | 1991 | r = setup_output(unit, context, params, STDOUT_FILENO, socket_fd, basename(command->path), uid, gid, &journal_stream_dev, &journal_stream_ino); |
ff0af2a1 LP |
1992 | if (r < 0) { |
1993 | *exit_status = EXIT_STDOUT; | |
1994 | return r; | |
d35fbf6b DM |
1995 | } |
1996 | ||
7bce046b | 1997 | r = setup_output(unit, context, params, STDERR_FILENO, socket_fd, basename(command->path), uid, gid, &journal_stream_dev, &journal_stream_ino); |
ff0af2a1 LP |
1998 | if (r < 0) { |
1999 | *exit_status = EXIT_STDERR; | |
2000 | return r; | |
d35fbf6b DM |
2001 | } |
2002 | ||
2003 | if (params->cgroup_path) { | |
ff0af2a1 LP |
2004 | r = cg_attach_everywhere(params->cgroup_supported, params->cgroup_path, 0, NULL, NULL); |
2005 | if (r < 0) { | |
2006 | *exit_status = EXIT_CGROUP; | |
2007 | return r; | |
309bff19 | 2008 | } |
d35fbf6b | 2009 | } |
309bff19 | 2010 | |
d35fbf6b | 2011 | if (context->oom_score_adjust_set) { |
d5243d62 | 2012 | char t[DECIMAL_STR_MAX(context->oom_score_adjust)]; |
f2b68789 | 2013 | |
d5243d62 LP |
2014 | /* When we can't make this change due to EPERM, then |
2015 | * let's silently skip over it. User namespaces | |
2016 | * prohibit write access to this file, and we | |
2017 | * shouldn't trip up over that. */ | |
613b411c | 2018 | |
d5243d62 | 2019 | sprintf(t, "%i", context->oom_score_adjust); |
ad118bda | 2020 | r = write_string_file("/proc/self/oom_score_adj", t, 0); |
6cb7fa17 | 2021 | if (r == -EPERM || r == -EACCES) { |
ff0af2a1 | 2022 | log_open(); |
f2341e0a | 2023 | log_unit_debug_errno(unit, r, "Failed to adjust OOM setting, assuming containerized execution, ignoring: %m"); |
ff0af2a1 LP |
2024 | log_close(); |
2025 | } else if (r < 0) { | |
2026 | *exit_status = EXIT_OOM_ADJUST; | |
d35fbf6b | 2027 | return -errno; |
613b411c | 2028 | } |
d35fbf6b DM |
2029 | } |
2030 | ||
2031 | if (context->nice_set) | |
2032 | if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) { | |
ff0af2a1 | 2033 | *exit_status = EXIT_NICE; |
d35fbf6b | 2034 | return -errno; |
613b411c LP |
2035 | } |
2036 | ||
d35fbf6b DM |
2037 | if (context->cpu_sched_set) { |
2038 | struct sched_param param = { | |
2039 | .sched_priority = context->cpu_sched_priority, | |
2040 | }; | |
2041 | ||
ff0af2a1 LP |
2042 | r = sched_setscheduler(0, |
2043 | context->cpu_sched_policy | | |
2044 | (context->cpu_sched_reset_on_fork ? | |
2045 | SCHED_RESET_ON_FORK : 0), | |
2046 | ¶m); | |
2047 | if (r < 0) { | |
2048 | *exit_status = EXIT_SETSCHEDULER; | |
d35fbf6b | 2049 | return -errno; |
fc9b2a84 | 2050 | } |
d35fbf6b | 2051 | } |
fc9b2a84 | 2052 | |
d35fbf6b DM |
2053 | if (context->cpuset) |
2054 | if (sched_setaffinity(0, CPU_ALLOC_SIZE(context->cpuset_ncpus), context->cpuset) < 0) { | |
ff0af2a1 | 2055 | *exit_status = EXIT_CPUAFFINITY; |
d35fbf6b | 2056 | return -errno; |
034c6ed7 LP |
2057 | } |
2058 | ||
d35fbf6b DM |
2059 | if (context->ioprio_set) |
2060 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) { | |
ff0af2a1 | 2061 | *exit_status = EXIT_IOPRIO; |
d35fbf6b DM |
2062 | return -errno; |
2063 | } | |
da726a4d | 2064 | |
d35fbf6b DM |
2065 | if (context->timer_slack_nsec != NSEC_INFINITY) |
2066 | if (prctl(PR_SET_TIMERSLACK, context->timer_slack_nsec) < 0) { | |
ff0af2a1 | 2067 | *exit_status = EXIT_TIMERSLACK; |
d35fbf6b | 2068 | return -errno; |
4c2630eb | 2069 | } |
9eba9da4 | 2070 | |
050f7277 | 2071 | if (context->personality != PERSONALITY_INVALID) |
d35fbf6b | 2072 | if (personality(context->personality) < 0) { |
ff0af2a1 | 2073 | *exit_status = EXIT_PERSONALITY; |
d35fbf6b | 2074 | return -errno; |
4c2630eb | 2075 | } |
94f04347 | 2076 | |
d35fbf6b | 2077 | if (context->utmp_id) |
023a4f67 LP |
2078 | utmp_put_init_process(context->utmp_id, getpid(), getsid(0), context->tty_path, |
2079 | context->utmp_mode == EXEC_UTMP_INIT ? INIT_PROCESS : | |
2080 | context->utmp_mode == EXEC_UTMP_LOGIN ? LOGIN_PROCESS : | |
2081 | USER_PROCESS, | |
2082 | username ? "root" : context->user); | |
d35fbf6b | 2083 | |
524daa8c | 2084 | if (context->user && is_terminal_input(context->std_input)) { |
ff0af2a1 LP |
2085 | r = chown_terminal(STDIN_FILENO, uid); |
2086 | if (r < 0) { | |
2087 | *exit_status = EXIT_STDIN; | |
2088 | return r; | |
071830ff | 2089 | } |
d35fbf6b | 2090 | } |
8e274523 | 2091 | |
a931ad47 LP |
2092 | /* If delegation is enabled we'll pass ownership of the cgroup |
2093 | * (but only in systemd's own controller hierarchy!) to the | |
2094 | * user of the new process. */ | |
2095 | if (params->cgroup_path && context->user && params->cgroup_delegate) { | |
ff0af2a1 LP |
2096 | r = cg_set_task_access(SYSTEMD_CGROUP_CONTROLLER, params->cgroup_path, 0644, uid, gid); |
2097 | if (r < 0) { | |
2098 | *exit_status = EXIT_CGROUP; | |
2099 | return r; | |
d35fbf6b | 2100 | } |
034c6ed7 | 2101 | |
034c6ed7 | 2102 | |
ff0af2a1 LP |
2103 | r = cg_set_group_access(SYSTEMD_CGROUP_CONTROLLER, params->cgroup_path, 0755, uid, gid); |
2104 | if (r < 0) { | |
2105 | *exit_status = EXIT_CGROUP; | |
2106 | return r; | |
034c6ed7 | 2107 | } |
d35fbf6b | 2108 | } |
034c6ed7 | 2109 | |
d35fbf6b DM |
2110 | if (!strv_isempty(context->runtime_directory) && params->runtime_prefix) { |
2111 | char **rt; | |
fb33a393 | 2112 | |
d35fbf6b DM |
2113 | STRV_FOREACH(rt, context->runtime_directory) { |
2114 | _cleanup_free_ char *p; | |
94f04347 | 2115 | |
d35fbf6b DM |
2116 | p = strjoin(params->runtime_prefix, "/", *rt, NULL); |
2117 | if (!p) { | |
ff0af2a1 | 2118 | *exit_status = EXIT_RUNTIME_DIRECTORY; |
d35fbf6b | 2119 | return -ENOMEM; |
94f04347 | 2120 | } |
94f04347 | 2121 | |
6bfe5c28 LP |
2122 | r = mkdir_p_label(p, context->runtime_directory_mode); |
2123 | if (r < 0) { | |
2124 | *exit_status = EXIT_RUNTIME_DIRECTORY; | |
2125 | return r; | |
2126 | } | |
2127 | ||
2128 | r = chmod_and_chown(p, context->runtime_directory_mode, uid, gid); | |
ff0af2a1 LP |
2129 | if (r < 0) { |
2130 | *exit_status = EXIT_RUNTIME_DIRECTORY; | |
2131 | return r; | |
94f04347 | 2132 | } |
d35fbf6b DM |
2133 | } |
2134 | } | |
94f04347 | 2135 | |
7bce046b | 2136 | r = build_environment( |
fd63e712 | 2137 | unit, |
7bce046b LP |
2138 | context, |
2139 | params, | |
2140 | n_fds, | |
2141 | home, | |
2142 | username, | |
2143 | shell, | |
2144 | journal_stream_dev, | |
2145 | journal_stream_ino, | |
2146 | &our_env); | |
2065ca69 JW |
2147 | if (r < 0) { |
2148 | *exit_status = EXIT_MEMORY; | |
2149 | return r; | |
2150 | } | |
2151 | ||
2152 | r = build_pass_environment(context, &pass_env); | |
2153 | if (r < 0) { | |
2154 | *exit_status = EXIT_MEMORY; | |
2155 | return r; | |
2156 | } | |
2157 | ||
2158 | accum_env = strv_env_merge(5, | |
2159 | params->environment, | |
2160 | our_env, | |
2161 | pass_env, | |
2162 | context->environment, | |
2163 | files_env, | |
2164 | NULL); | |
2165 | if (!accum_env) { | |
2166 | *exit_status = EXIT_MEMORY; | |
2167 | return -ENOMEM; | |
2168 | } | |
1280503b | 2169 | accum_env = strv_env_clean(accum_env); |
2065ca69 | 2170 | |
b213e1c1 SW |
2171 | umask(context->umask); |
2172 | ||
c39f1ce2 | 2173 | if ((params->flags & EXEC_APPLY_PERMISSIONS) && !command->privileged) { |
ff0af2a1 LP |
2174 | r = enforce_groups(context, username, gid); |
2175 | if (r < 0) { | |
2176 | *exit_status = EXIT_GROUP; | |
2177 | return r; | |
d35fbf6b | 2178 | } |
6bf6e43e | 2179 | #ifdef HAVE_SMACK |
6bf6e43e SW |
2180 | if (context->smack_process_label) { |
2181 | r = mac_smack_apply_pid(0, context->smack_process_label); | |
2182 | if (r < 0) { | |
2183 | *exit_status = EXIT_SMACK_PROCESS_LABEL; | |
2184 | return r; | |
2185 | } | |
2186 | } | |
2187 | #ifdef SMACK_DEFAULT_PROCESS_LABEL | |
2188 | else { | |
2189 | _cleanup_free_ char *exec_label = NULL; | |
2190 | ||
2191 | r = mac_smack_read(command->path, SMACK_ATTR_EXEC, &exec_label); | |
2192 | if (r < 0 && r != -ENODATA && r != -EOPNOTSUPP) { | |
2193 | *exit_status = EXIT_SMACK_PROCESS_LABEL; | |
2194 | return r; | |
2195 | } | |
2196 | ||
2197 | r = mac_smack_apply_pid(0, exec_label ? : SMACK_DEFAULT_PROCESS_LABEL); | |
2198 | if (r < 0) { | |
2199 | *exit_status = EXIT_SMACK_PROCESS_LABEL; | |
2200 | return r; | |
2201 | } | |
2202 | } | |
6bf6e43e SW |
2203 | #endif |
2204 | #endif | |
d35fbf6b | 2205 | #ifdef HAVE_PAM |
b213e1c1 | 2206 | if (context->pam_name && username) { |
2065ca69 | 2207 | r = setup_pam(context->pam_name, username, uid, context->tty_path, &accum_env, fds, n_fds); |
b213e1c1 SW |
2208 | if (r < 0) { |
2209 | *exit_status = EXIT_PAM; | |
2210 | return r; | |
2211 | } | |
d35fbf6b | 2212 | } |
d35fbf6b | 2213 | #endif |
b213e1c1 | 2214 | } |
ac45f971 | 2215 | |
d35fbf6b | 2216 | if (context->private_network && runtime && runtime->netns_storage_socket[0] >= 0) { |
ff0af2a1 LP |
2217 | r = setup_netns(runtime->netns_storage_socket); |
2218 | if (r < 0) { | |
2219 | *exit_status = EXIT_NETWORK; | |
2220 | return r; | |
d35fbf6b DM |
2221 | } |
2222 | } | |
169c1bda | 2223 | |
ee818b89 AC |
2224 | needs_mount_namespace = exec_needs_mount_namespace(context, params, runtime); |
2225 | ||
2226 | if (needs_mount_namespace) { | |
d35fbf6b DM |
2227 | char *tmp = NULL, *var = NULL; |
2228 | ||
2229 | /* The runtime struct only contains the parent | |
2230 | * of the private /tmp, which is | |
2231 | * non-accessible to world users. Inside of it | |
2232 | * there's a /tmp that is sticky, and that's | |
2233 | * the one we want to use here. */ | |
2234 | ||
2235 | if (context->private_tmp && runtime) { | |
2236 | if (runtime->tmp_dir) | |
63c372cb | 2237 | tmp = strjoina(runtime->tmp_dir, "/tmp"); |
d35fbf6b | 2238 | if (runtime->var_tmp_dir) |
63c372cb | 2239 | var = strjoina(runtime->var_tmp_dir, "/tmp"); |
d35fbf6b | 2240 | } |
d8b4e2e9 | 2241 | |
ff0af2a1 | 2242 | r = setup_namespace( |
c39f1ce2 | 2243 | (params->flags & EXEC_APPLY_CHROOT) ? context->root_directory : NULL, |
2a624c36 AP |
2244 | context->read_write_paths, |
2245 | context->read_only_paths, | |
2246 | context->inaccessible_paths, | |
d35fbf6b DM |
2247 | tmp, |
2248 | var, | |
2249 | context->private_devices, | |
2250 | context->protect_home, | |
2251 | context->protect_system, | |
2252 | context->mount_flags); | |
0015ebf3 | 2253 | |
ff0af2a1 LP |
2254 | /* If we couldn't set up the namespace this is |
2255 | * probably due to a missing capability. In this case, | |
2256 | * silently proceeed. */ | |
2257 | if (r == -EPERM || r == -EACCES) { | |
2258 | log_open(); | |
f2341e0a | 2259 | log_unit_debug_errno(unit, r, "Failed to set up namespace, assuming containerized execution, ignoring: %m"); |
ff0af2a1 LP |
2260 | log_close(); |
2261 | } else if (r < 0) { | |
2262 | *exit_status = EXIT_NAMESPACE; | |
2263 | return r; | |
81a2b7ce | 2264 | } |
d35fbf6b | 2265 | } |
81a2b7ce | 2266 | |
5f5d8eab LP |
2267 | if (context->working_directory_home) |
2268 | wd = home; | |
2269 | else if (context->working_directory) | |
2270 | wd = context->working_directory; | |
2271 | else | |
2272 | wd = "/"; | |
2273 | ||
c39f1ce2 | 2274 | if (params->flags & EXEC_APPLY_CHROOT) { |
ee818b89 | 2275 | if (!needs_mount_namespace && context->root_directory) |
d35fbf6b | 2276 | if (chroot(context->root_directory) < 0) { |
ff0af2a1 | 2277 | *exit_status = EXIT_CHROOT; |
d35fbf6b | 2278 | return -errno; |
8aa75193 LP |
2279 | } |
2280 | ||
5f5d8eab | 2281 | if (chdir(wd) < 0 && |
4c08c824 | 2282 | !context->working_directory_missing_ok) { |
ff0af2a1 | 2283 | *exit_status = EXIT_CHDIR; |
d35fbf6b DM |
2284 | return -errno; |
2285 | } | |
2286 | } else { | |
5f5d8eab | 2287 | const char *d; |
8aa75193 | 2288 | |
5f5d8eab | 2289 | d = strjoina(strempty(context->root_directory), "/", strempty(wd)); |
cf1d0302 LP |
2290 | if (chdir(d) < 0 && |
2291 | !context->working_directory_missing_ok) { | |
ff0af2a1 | 2292 | *exit_status = EXIT_CHDIR; |
d35fbf6b DM |
2293 | return -errno; |
2294 | } | |
2295 | } | |
e66cf1a3 | 2296 | |
9008e1ac | 2297 | #ifdef HAVE_SELINUX |
c39f1ce2 LP |
2298 | if ((params->flags & EXEC_APPLY_PERMISSIONS) && |
2299 | mac_selinux_use() && | |
2300 | params->selinux_context_net && | |
2301 | socket_fd >= 0 && | |
2302 | !command->privileged) { | |
2303 | ||
ff0af2a1 LP |
2304 | r = mac_selinux_get_child_mls_label(socket_fd, command->path, context->selinux_context, &mac_selinux_context_net); |
2305 | if (r < 0) { | |
2306 | *exit_status = EXIT_SELINUX_CONTEXT; | |
2307 | return r; | |
9008e1ac MS |
2308 | } |
2309 | } | |
2310 | #endif | |
2311 | ||
d87a2ef7 | 2312 | if ((params->flags & EXEC_APPLY_PERMISSIONS) && context->private_users) { |
d251207d LP |
2313 | r = setup_private_users(uid, gid); |
2314 | if (r < 0) { | |
2315 | *exit_status = EXIT_USER; | |
2316 | return r; | |
2317 | } | |
2318 | } | |
2319 | ||
d35fbf6b DM |
2320 | /* We repeat the fd closing here, to make sure that |
2321 | * nothing is leaked from the PAM modules. Note that | |
2322 | * we are more aggressive this time since socket_fd | |
e44da745 DM |
2323 | * and the netns fds we don't need anymore. The custom |
2324 | * endpoint fd was needed to upload the policy and can | |
2325 | * now be closed as well. */ | |
ff0af2a1 LP |
2326 | r = close_all_fds(fds, n_fds); |
2327 | if (r >= 0) | |
2328 | r = shift_fds(fds, n_fds); | |
2329 | if (r >= 0) | |
2330 | r = flags_fds(fds, n_fds, context->non_blocking); | |
2331 | if (r < 0) { | |
2332 | *exit_status = EXIT_FDS; | |
2333 | return r; | |
d35fbf6b | 2334 | } |
e66cf1a3 | 2335 | |
c39f1ce2 | 2336 | if ((params->flags & EXEC_APPLY_PERMISSIONS) && !command->privileged) { |
e66cf1a3 | 2337 | |
19c0b0b9 RC |
2338 | bool use_address_families = context->address_families_whitelist || |
2339 | !set_isempty(context->address_families); | |
2340 | bool use_syscall_filter = context->syscall_whitelist || | |
2341 | !set_isempty(context->syscall_filter) || | |
2342 | !set_isempty(context->syscall_archs); | |
755d4b67 IP |
2343 | int secure_bits = context->secure_bits; |
2344 | ||
d35fbf6b | 2345 | for (i = 0; i < _RLIMIT_MAX; i++) { |
03857c43 | 2346 | |
d35fbf6b DM |
2347 | if (!context->rlimit[i]) |
2348 | continue; | |
2349 | ||
03857c43 LP |
2350 | r = setrlimit_closest(i, context->rlimit[i]); |
2351 | if (r < 0) { | |
ff0af2a1 | 2352 | *exit_status = EXIT_LIMITS; |
03857c43 | 2353 | return r; |
e66cf1a3 LP |
2354 | } |
2355 | } | |
2356 | ||
f4170c67 LP |
2357 | /* Set the RTPRIO resource limit to 0, but only if nothing else was explicitly requested. */ |
2358 | if (context->restrict_realtime && !context->rlimit[RLIMIT_RTPRIO]) { | |
2359 | if (setrlimit(RLIMIT_RTPRIO, &RLIMIT_MAKE_CONST(0)) < 0) { | |
2360 | *exit_status = EXIT_LIMITS; | |
2361 | return -errno; | |
2362 | } | |
2363 | } | |
2364 | ||
a103496c IP |
2365 | if (!cap_test_all(context->capability_bounding_set)) { |
2366 | r = capability_bounding_set_drop(context->capability_bounding_set, false); | |
ff0af2a1 LP |
2367 | if (r < 0) { |
2368 | *exit_status = EXIT_CAPABILITIES; | |
2369 | return r; | |
3b8bddde | 2370 | } |
4c2630eb | 2371 | } |
3b8bddde | 2372 | |
755d4b67 IP |
2373 | /* This is done before enforce_user, but ambient set |
2374 | * does not survive over setresuid() if keep_caps is not set. */ | |
2375 | if (context->capability_ambient_set != 0) { | |
2376 | r = capability_ambient_set_apply(context->capability_ambient_set, true); | |
2377 | if (r < 0) { | |
2378 | *exit_status = EXIT_CAPABILITIES; | |
2379 | return r; | |
2380 | } | |
755d4b67 IP |
2381 | } |
2382 | ||
d35fbf6b | 2383 | if (context->user) { |
ff0af2a1 LP |
2384 | r = enforce_user(context, uid); |
2385 | if (r < 0) { | |
2386 | *exit_status = EXIT_USER; | |
2387 | return r; | |
5b6319dc | 2388 | } |
755d4b67 IP |
2389 | if (context->capability_ambient_set != 0) { |
2390 | ||
2391 | /* Fix the ambient capabilities after user change. */ | |
2392 | r = capability_ambient_set_apply(context->capability_ambient_set, false); | |
2393 | if (r < 0) { | |
2394 | *exit_status = EXIT_CAPABILITIES; | |
2395 | return r; | |
2396 | } | |
2397 | ||
2398 | /* If we were asked to change user and ambient capabilities | |
2399 | * were requested, we had to add keep-caps to the securebits | |
2400 | * so that we would maintain the inherited capability set | |
2401 | * through the setresuid(). Make sure that the bit is added | |
2402 | * also to the context secure_bits so that we don't try to | |
2403 | * drop the bit away next. */ | |
2404 | ||
7f508f2c | 2405 | secure_bits |= 1<<SECURE_KEEP_CAPS; |
755d4b67 | 2406 | } |
5b6319dc | 2407 | } |
d35fbf6b DM |
2408 | |
2409 | /* PR_GET_SECUREBITS is not privileged, while | |
2410 | * PR_SET_SECUREBITS is. So to suppress | |
2411 | * potential EPERMs we'll try not to call | |
2412 | * PR_SET_SECUREBITS unless necessary. */ | |
755d4b67 IP |
2413 | if (prctl(PR_GET_SECUREBITS) != secure_bits) |
2414 | if (prctl(PR_SET_SECUREBITS, secure_bits) < 0) { | |
ff0af2a1 | 2415 | *exit_status = EXIT_SECUREBITS; |
d35fbf6b | 2416 | return -errno; |
ff01d048 | 2417 | } |
5b6319dc | 2418 | |
19c0b0b9 | 2419 | if (context->no_new_privileges || |
f4170c67 | 2420 | (!have_effective_cap(CAP_SYS_ADMIN) && (use_address_families || context->memory_deny_write_execute || context->restrict_realtime || use_syscall_filter))) |
d35fbf6b | 2421 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) { |
ff0af2a1 | 2422 | *exit_status = EXIT_NO_NEW_PRIVILEGES; |
d35fbf6b DM |
2423 | return -errno; |
2424 | } | |
2425 | ||
2426 | #ifdef HAVE_SECCOMP | |
19c0b0b9 | 2427 | if (use_address_families) { |
83f12b27 | 2428 | r = apply_address_families(unit, context); |
ff0af2a1 LP |
2429 | if (r < 0) { |
2430 | *exit_status = EXIT_ADDRESS_FAMILIES; | |
2431 | return r; | |
4c2630eb MS |
2432 | } |
2433 | } | |
04aa0cb9 | 2434 | |
f3e43635 | 2435 | if (context->memory_deny_write_execute) { |
83f12b27 | 2436 | r = apply_memory_deny_write_execute(unit, context); |
f3e43635 TM |
2437 | if (r < 0) { |
2438 | *exit_status = EXIT_SECCOMP; | |
2439 | return r; | |
2440 | } | |
2441 | } | |
f4170c67 LP |
2442 | |
2443 | if (context->restrict_realtime) { | |
83f12b27 | 2444 | r = apply_restrict_realtime(unit, context); |
f4170c67 LP |
2445 | if (r < 0) { |
2446 | *exit_status = EXIT_SECCOMP; | |
2447 | return r; | |
2448 | } | |
2449 | } | |
2450 | ||
19c0b0b9 | 2451 | if (use_syscall_filter) { |
83f12b27 | 2452 | r = apply_seccomp(unit, context); |
ff0af2a1 LP |
2453 | if (r < 0) { |
2454 | *exit_status = EXIT_SECCOMP; | |
2455 | return r; | |
81a2b7ce | 2456 | } |
d35fbf6b DM |
2457 | } |
2458 | #endif | |
81a2b7ce | 2459 | |
d35fbf6b | 2460 | #ifdef HAVE_SELINUX |
6baa7db0 | 2461 | if (mac_selinux_use()) { |
9008e1ac | 2462 | char *exec_context = mac_selinux_context_net ?: context->selinux_context; |
16115b0a | 2463 | |
9008e1ac | 2464 | if (exec_context) { |
ff0af2a1 LP |
2465 | r = setexeccon(exec_context); |
2466 | if (r < 0) { | |
2467 | *exit_status = EXIT_SELINUX_CONTEXT; | |
2468 | return r; | |
16115b0a | 2469 | } |
81a2b7ce | 2470 | } |
81a2b7ce | 2471 | } |
d35fbf6b | 2472 | #endif |
81a2b7ce | 2473 | |
d35fbf6b | 2474 | #ifdef HAVE_APPARMOR |
6baa7db0 | 2475 | if (context->apparmor_profile && mac_apparmor_use()) { |
ff0af2a1 LP |
2476 | r = aa_change_onexec(context->apparmor_profile); |
2477 | if (r < 0 && !context->apparmor_profile_ignore) { | |
2478 | *exit_status = EXIT_APPARMOR_PROFILE; | |
5482192e | 2479 | return -errno; |
d35fbf6b | 2480 | } |
034c6ed7 | 2481 | } |
d35fbf6b DM |
2482 | #endif |
2483 | } | |
034c6ed7 | 2484 | |
2065ca69 | 2485 | final_argv = replace_env_argv(argv, accum_env); |
d35fbf6b | 2486 | if (!final_argv) { |
ff0af2a1 | 2487 | *exit_status = EXIT_MEMORY; |
d35fbf6b DM |
2488 | return -ENOMEM; |
2489 | } | |
034c6ed7 | 2490 | |
553d2243 | 2491 | if (_unlikely_(log_get_max_level() >= LOG_DEBUG)) { |
d35fbf6b | 2492 | _cleanup_free_ char *line; |
81a2b7ce | 2493 | |
d35fbf6b DM |
2494 | line = exec_command_line(final_argv); |
2495 | if (line) { | |
2496 | log_open(); | |
f2341e0a LP |
2497 | log_struct(LOG_DEBUG, |
2498 | LOG_UNIT_ID(unit), | |
2499 | "EXECUTABLE=%s", command->path, | |
2500 | LOG_UNIT_MESSAGE(unit, "Executing: %s", line), | |
2501 | NULL); | |
d35fbf6b DM |
2502 | log_close(); |
2503 | } | |
2504 | } | |
dd305ec9 | 2505 | |
2065ca69 | 2506 | execve(command->path, final_argv, accum_env); |
ff0af2a1 | 2507 | *exit_status = EXIT_EXEC; |
d35fbf6b DM |
2508 | return -errno; |
2509 | } | |
81a2b7ce | 2510 | |
f2341e0a LP |
2511 | int exec_spawn(Unit *unit, |
2512 | ExecCommand *command, | |
d35fbf6b DM |
2513 | const ExecContext *context, |
2514 | const ExecParameters *params, | |
2515 | ExecRuntime *runtime, | |
29206d46 | 2516 | DynamicCreds *dcreds, |
d35fbf6b | 2517 | pid_t *ret) { |
8351ceae | 2518 | |
d35fbf6b DM |
2519 | _cleanup_strv_free_ char **files_env = NULL; |
2520 | int *fds = NULL; unsigned n_fds = 0; | |
ff0af2a1 LP |
2521 | _cleanup_free_ char *line = NULL; |
2522 | int socket_fd, r; | |
2523 | char **argv; | |
d35fbf6b | 2524 | pid_t pid; |
8351ceae | 2525 | |
f2341e0a | 2526 | assert(unit); |
d35fbf6b DM |
2527 | assert(command); |
2528 | assert(context); | |
2529 | assert(ret); | |
2530 | assert(params); | |
2531 | assert(params->fds || params->n_fds <= 0); | |
4298d0b5 | 2532 | |
d35fbf6b DM |
2533 | if (context->std_input == EXEC_INPUT_SOCKET || |
2534 | context->std_output == EXEC_OUTPUT_SOCKET || | |
2535 | context->std_error == EXEC_OUTPUT_SOCKET) { | |
17df7223 | 2536 | |
ff0af2a1 | 2537 | if (params->n_fds != 1) { |
f2341e0a | 2538 | log_unit_error(unit, "Got more than one socket."); |
d35fbf6b | 2539 | return -EINVAL; |
ff0af2a1 | 2540 | } |
eef65bf3 | 2541 | |
d35fbf6b DM |
2542 | socket_fd = params->fds[0]; |
2543 | } else { | |
2544 | socket_fd = -1; | |
2545 | fds = params->fds; | |
2546 | n_fds = params->n_fds; | |
2547 | } | |
94f04347 | 2548 | |
f2341e0a | 2549 | r = exec_context_load_environment(unit, context, &files_env); |
ff0af2a1 | 2550 | if (r < 0) |
f2341e0a | 2551 | return log_unit_error_errno(unit, r, "Failed to load environment files: %m"); |
034c6ed7 | 2552 | |
d35fbf6b | 2553 | argv = params->argv ?: command->argv; |
d35fbf6b DM |
2554 | line = exec_command_line(argv); |
2555 | if (!line) | |
2556 | return log_oom(); | |
fab56fc5 | 2557 | |
f2341e0a LP |
2558 | log_struct(LOG_DEBUG, |
2559 | LOG_UNIT_ID(unit), | |
2560 | LOG_UNIT_MESSAGE(unit, "About to execute: %s", line), | |
2561 | "EXECUTABLE=%s", command->path, | |
2562 | NULL); | |
d35fbf6b DM |
2563 | pid = fork(); |
2564 | if (pid < 0) | |
74129a12 | 2565 | return log_unit_error_errno(unit, errno, "Failed to fork: %m"); |
d35fbf6b DM |
2566 | |
2567 | if (pid == 0) { | |
ff0af2a1 LP |
2568 | int exit_status; |
2569 | ||
f2341e0a LP |
2570 | r = exec_child(unit, |
2571 | command, | |
ff0af2a1 LP |
2572 | context, |
2573 | params, | |
2574 | runtime, | |
29206d46 | 2575 | dcreds, |
ff0af2a1 LP |
2576 | argv, |
2577 | socket_fd, | |
2578 | fds, n_fds, | |
2579 | files_env, | |
00d9ef85 | 2580 | unit->manager->user_lookup_fds[1], |
ff0af2a1 LP |
2581 | &exit_status); |
2582 | if (r < 0) { | |
4c2630eb | 2583 | log_open(); |
f2341e0a LP |
2584 | log_struct_errno(LOG_ERR, r, |
2585 | LOG_MESSAGE_ID(SD_MESSAGE_SPAWN_FAILED), | |
2586 | LOG_UNIT_ID(unit), | |
2587 | LOG_UNIT_MESSAGE(unit, "Failed at step %s spawning %s: %m", | |
2588 | exit_status_to_string(exit_status, EXIT_STATUS_SYSTEMD), | |
2589 | command->path), | |
2590 | "EXECUTABLE=%s", command->path, | |
2591 | NULL); | |
4c2630eb MS |
2592 | } |
2593 | ||
ff0af2a1 | 2594 | _exit(exit_status); |
034c6ed7 LP |
2595 | } |
2596 | ||
f2341e0a | 2597 | log_unit_debug(unit, "Forked %s as "PID_FMT, command->path, pid); |
23635a85 | 2598 | |
80876c20 LP |
2599 | /* We add the new process to the cgroup both in the child (so |
2600 | * that we can be sure that no user code is ever executed | |
2601 | * outside of the cgroup) and in the parent (so that we can be | |
2602 | * sure that when we kill the cgroup the process will be | |
2603 | * killed too). */ | |
d35fbf6b | 2604 | if (params->cgroup_path) |
dd305ec9 | 2605 | (void) cg_attach(SYSTEMD_CGROUP_CONTROLLER, params->cgroup_path, pid); |
2da3263a | 2606 | |
b58b4116 | 2607 | exec_status_start(&command->exec_status, pid); |
9fb86720 | 2608 | |
034c6ed7 | 2609 | *ret = pid; |
5cb5a6ff LP |
2610 | return 0; |
2611 | } | |
2612 | ||
034c6ed7 LP |
2613 | void exec_context_init(ExecContext *c) { |
2614 | assert(c); | |
2615 | ||
4c12626c | 2616 | c->umask = 0022; |
9eba9da4 | 2617 | c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0); |
94f04347 | 2618 | c->cpu_sched_policy = SCHED_OTHER; |
071830ff | 2619 | c->syslog_priority = LOG_DAEMON|LOG_INFO; |
74922904 | 2620 | c->syslog_level_prefix = true; |
353e12c2 | 2621 | c->ignore_sigpipe = true; |
3a43da28 | 2622 | c->timer_slack_nsec = NSEC_INFINITY; |
050f7277 | 2623 | c->personality = PERSONALITY_INVALID; |
e66cf1a3 | 2624 | c->runtime_directory_mode = 0755; |
a103496c | 2625 | c->capability_bounding_set = CAP_ALL; |
034c6ed7 LP |
2626 | } |
2627 | ||
613b411c | 2628 | void exec_context_done(ExecContext *c) { |
5cb5a6ff LP |
2629 | unsigned l; |
2630 | ||
2631 | assert(c); | |
2632 | ||
6796073e LP |
2633 | c->environment = strv_free(c->environment); |
2634 | c->environment_files = strv_free(c->environment_files); | |
b4c14404 | 2635 | c->pass_environment = strv_free(c->pass_environment); |
8c7be95e | 2636 | |
1f6b4113 | 2637 | for (l = 0; l < ELEMENTSOF(c->rlimit); l++) |
a1e58e8e | 2638 | c->rlimit[l] = mfree(c->rlimit[l]); |
034c6ed7 | 2639 | |
a1e58e8e LP |
2640 | c->working_directory = mfree(c->working_directory); |
2641 | c->root_directory = mfree(c->root_directory); | |
2642 | c->tty_path = mfree(c->tty_path); | |
2643 | c->syslog_identifier = mfree(c->syslog_identifier); | |
2644 | c->user = mfree(c->user); | |
2645 | c->group = mfree(c->group); | |
034c6ed7 | 2646 | |
6796073e | 2647 | c->supplementary_groups = strv_free(c->supplementary_groups); |
94f04347 | 2648 | |
a1e58e8e | 2649 | c->pam_name = mfree(c->pam_name); |
5b6319dc | 2650 | |
2a624c36 AP |
2651 | c->read_only_paths = strv_free(c->read_only_paths); |
2652 | c->read_write_paths = strv_free(c->read_write_paths); | |
2653 | c->inaccessible_paths = strv_free(c->inaccessible_paths); | |
82c121a4 LP |
2654 | |
2655 | if (c->cpuset) | |
2656 | CPU_FREE(c->cpuset); | |
86a3475b | 2657 | |
a1e58e8e LP |
2658 | c->utmp_id = mfree(c->utmp_id); |
2659 | c->selinux_context = mfree(c->selinux_context); | |
2660 | c->apparmor_profile = mfree(c->apparmor_profile); | |
eef65bf3 | 2661 | |
525d3cc7 LP |
2662 | c->syscall_filter = set_free(c->syscall_filter); |
2663 | c->syscall_archs = set_free(c->syscall_archs); | |
2664 | c->address_families = set_free(c->address_families); | |
e66cf1a3 | 2665 | |
6796073e | 2666 | c->runtime_directory = strv_free(c->runtime_directory); |
e66cf1a3 LP |
2667 | } |
2668 | ||
2669 | int exec_context_destroy_runtime_directory(ExecContext *c, const char *runtime_prefix) { | |
2670 | char **i; | |
2671 | ||
2672 | assert(c); | |
2673 | ||
2674 | if (!runtime_prefix) | |
2675 | return 0; | |
2676 | ||
2677 | STRV_FOREACH(i, c->runtime_directory) { | |
2678 | _cleanup_free_ char *p; | |
2679 | ||
2680 | p = strjoin(runtime_prefix, "/", *i, NULL); | |
2681 | if (!p) | |
2682 | return -ENOMEM; | |
2683 | ||
2684 | /* We execute this synchronously, since we need to be | |
2685 | * sure this is gone when we start the service | |
2686 | * next. */ | |
c6878637 | 2687 | (void) rm_rf(p, REMOVE_ROOT); |
e66cf1a3 LP |
2688 | } |
2689 | ||
2690 | return 0; | |
5cb5a6ff LP |
2691 | } |
2692 | ||
43d0fcbd LP |
2693 | void exec_command_done(ExecCommand *c) { |
2694 | assert(c); | |
2695 | ||
a1e58e8e | 2696 | c->path = mfree(c->path); |
43d0fcbd | 2697 | |
6796073e | 2698 | c->argv = strv_free(c->argv); |
43d0fcbd LP |
2699 | } |
2700 | ||
2701 | void exec_command_done_array(ExecCommand *c, unsigned n) { | |
2702 | unsigned i; | |
2703 | ||
2704 | for (i = 0; i < n; i++) | |
2705 | exec_command_done(c+i); | |
2706 | } | |
2707 | ||
f1acf85a | 2708 | ExecCommand* exec_command_free_list(ExecCommand *c) { |
5cb5a6ff LP |
2709 | ExecCommand *i; |
2710 | ||
2711 | while ((i = c)) { | |
71fda00f | 2712 | LIST_REMOVE(command, c, i); |
43d0fcbd | 2713 | exec_command_done(i); |
5cb5a6ff LP |
2714 | free(i); |
2715 | } | |
f1acf85a ZJS |
2716 | |
2717 | return NULL; | |
5cb5a6ff LP |
2718 | } |
2719 | ||
034c6ed7 LP |
2720 | void exec_command_free_array(ExecCommand **c, unsigned n) { |
2721 | unsigned i; | |
2722 | ||
f1acf85a ZJS |
2723 | for (i = 0; i < n; i++) |
2724 | c[i] = exec_command_free_list(c[i]); | |
034c6ed7 LP |
2725 | } |
2726 | ||
039f0e70 | 2727 | typedef struct InvalidEnvInfo { |
f2341e0a | 2728 | Unit *unit; |
039f0e70 LP |
2729 | const char *path; |
2730 | } InvalidEnvInfo; | |
2731 | ||
2732 | static void invalid_env(const char *p, void *userdata) { | |
2733 | InvalidEnvInfo *info = userdata; | |
2734 | ||
f2341e0a | 2735 | log_unit_error(info->unit, "Ignoring invalid environment assignment '%s': %s", p, info->path); |
039f0e70 LP |
2736 | } |
2737 | ||
f2341e0a | 2738 | int exec_context_load_environment(Unit *unit, const ExecContext *c, char ***l) { |
8c7be95e LP |
2739 | char **i, **r = NULL; |
2740 | ||
2741 | assert(c); | |
2742 | assert(l); | |
2743 | ||
2744 | STRV_FOREACH(i, c->environment_files) { | |
2745 | char *fn; | |
2746 | int k; | |
2747 | bool ignore = false; | |
2748 | char **p; | |
7fd1b19b | 2749 | _cleanup_globfree_ glob_t pglob = {}; |
2bef10ab | 2750 | int count, n; |
8c7be95e LP |
2751 | |
2752 | fn = *i; | |
2753 | ||
2754 | if (fn[0] == '-') { | |
2755 | ignore = true; | |
313cefa1 | 2756 | fn++; |
8c7be95e LP |
2757 | } |
2758 | ||
2759 | if (!path_is_absolute(fn)) { | |
8c7be95e LP |
2760 | if (ignore) |
2761 | continue; | |
2762 | ||
2763 | strv_free(r); | |
2764 | return -EINVAL; | |
2765 | } | |
2766 | ||
2bef10ab | 2767 | /* Filename supports globbing, take all matching files */ |
2bef10ab PL |
2768 | errno = 0; |
2769 | if (glob(fn, 0, NULL, &pglob) != 0) { | |
2bef10ab PL |
2770 | if (ignore) |
2771 | continue; | |
8c7be95e | 2772 | |
2bef10ab | 2773 | strv_free(r); |
f5e5c28f | 2774 | return errno > 0 ? -errno : -EINVAL; |
2bef10ab PL |
2775 | } |
2776 | count = pglob.gl_pathc; | |
2777 | if (count == 0) { | |
8c7be95e LP |
2778 | if (ignore) |
2779 | continue; | |
2780 | ||
2781 | strv_free(r); | |
2bef10ab | 2782 | return -EINVAL; |
8c7be95e | 2783 | } |
2bef10ab | 2784 | for (n = 0; n < count; n++) { |
717603e3 | 2785 | k = load_env_file(NULL, pglob.gl_pathv[n], NULL, &p); |
2bef10ab PL |
2786 | if (k < 0) { |
2787 | if (ignore) | |
2788 | continue; | |
8c7be95e | 2789 | |
2bef10ab | 2790 | strv_free(r); |
2bef10ab | 2791 | return k; |
e9c1ea9d | 2792 | } |
ebc05a09 | 2793 | /* Log invalid environment variables with filename */ |
039f0e70 LP |
2794 | if (p) { |
2795 | InvalidEnvInfo info = { | |
f2341e0a | 2796 | .unit = unit, |
039f0e70 LP |
2797 | .path = pglob.gl_pathv[n] |
2798 | }; | |
2799 | ||
2800 | p = strv_env_clean_with_callback(p, invalid_env, &info); | |
2801 | } | |
8c7be95e | 2802 | |
2bef10ab PL |
2803 | if (r == NULL) |
2804 | r = p; | |
2805 | else { | |
2806 | char **m; | |
8c7be95e | 2807 | |
2bef10ab PL |
2808 | m = strv_env_merge(2, r, p); |
2809 | strv_free(r); | |
2810 | strv_free(p); | |
c84a9488 | 2811 | if (!m) |
2bef10ab | 2812 | return -ENOMEM; |
2bef10ab PL |
2813 | |
2814 | r = m; | |
2815 | } | |
8c7be95e LP |
2816 | } |
2817 | } | |
2818 | ||
2819 | *l = r; | |
2820 | ||
2821 | return 0; | |
2822 | } | |
2823 | ||
6ac8fdc9 | 2824 | static bool tty_may_match_dev_console(const char *tty) { |
e1d75803 | 2825 | _cleanup_free_ char *active = NULL; |
7d6884b6 | 2826 | char *console; |
6ac8fdc9 | 2827 | |
1e22b5cd LP |
2828 | if (!tty) |
2829 | return true; | |
2830 | ||
6ac8fdc9 MS |
2831 | if (startswith(tty, "/dev/")) |
2832 | tty += 5; | |
2833 | ||
2834 | /* trivial identity? */ | |
2835 | if (streq(tty, "console")) | |
2836 | return true; | |
2837 | ||
2838 | console = resolve_dev_console(&active); | |
2839 | /* if we could not resolve, assume it may */ | |
2840 | if (!console) | |
2841 | return true; | |
2842 | ||
2843 | /* "tty0" means the active VC, so it may be the same sometimes */ | |
e1d75803 | 2844 | return streq(console, tty) || (streq(console, "tty0") && tty_is_vc(tty)); |
6ac8fdc9 MS |
2845 | } |
2846 | ||
2847 | bool exec_context_may_touch_console(ExecContext *ec) { | |
1e22b5cd LP |
2848 | |
2849 | return (ec->tty_reset || | |
2850 | ec->tty_vhangup || | |
2851 | ec->tty_vt_disallocate || | |
6ac8fdc9 MS |
2852 | is_terminal_input(ec->std_input) || |
2853 | is_terminal_output(ec->std_output) || | |
2854 | is_terminal_output(ec->std_error)) && | |
1e22b5cd | 2855 | tty_may_match_dev_console(exec_context_tty_path(ec)); |
6ac8fdc9 MS |
2856 | } |
2857 | ||
15ae422b LP |
2858 | static void strv_fprintf(FILE *f, char **l) { |
2859 | char **g; | |
2860 | ||
2861 | assert(f); | |
2862 | ||
2863 | STRV_FOREACH(g, l) | |
2864 | fprintf(f, " %s", *g); | |
2865 | } | |
2866 | ||
5cb5a6ff | 2867 | void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) { |
c2bbd90b | 2868 | char **e, **d; |
94f04347 | 2869 | unsigned i; |
9eba9da4 | 2870 | |
5cb5a6ff LP |
2871 | assert(c); |
2872 | assert(f); | |
2873 | ||
4ad49000 | 2874 | prefix = strempty(prefix); |
5cb5a6ff LP |
2875 | |
2876 | fprintf(f, | |
94f04347 LP |
2877 | "%sUMask: %04o\n" |
2878 | "%sWorkingDirectory: %s\n" | |
451a074f | 2879 | "%sRootDirectory: %s\n" |
15ae422b | 2880 | "%sNonBlocking: %s\n" |
64747e2d | 2881 | "%sPrivateTmp: %s\n" |
7f112f50 | 2882 | "%sPrivateDevices: %s\n" |
d251207d LP |
2883 | "%sPrivateNetwork: %s\n" |
2884 | "%sPrivateUsers: %s\n" | |
1b8689f9 LP |
2885 | "%sProtectHome: %s\n" |
2886 | "%sProtectSystem: %s\n" | |
f3e43635 | 2887 | "%sIgnoreSIGPIPE: %s\n" |
f4170c67 LP |
2888 | "%sMemoryDenyWriteExecute: %s\n" |
2889 | "%sRestrictRealtime: %s\n", | |
5cb5a6ff | 2890 | prefix, c->umask, |
9eba9da4 | 2891 | prefix, c->working_directory ? c->working_directory : "/", |
451a074f | 2892 | prefix, c->root_directory ? c->root_directory : "/", |
15ae422b | 2893 | prefix, yes_no(c->non_blocking), |
64747e2d | 2894 | prefix, yes_no(c->private_tmp), |
7f112f50 | 2895 | prefix, yes_no(c->private_devices), |
d251207d LP |
2896 | prefix, yes_no(c->private_network), |
2897 | prefix, yes_no(c->private_users), | |
1b8689f9 LP |
2898 | prefix, protect_home_to_string(c->protect_home), |
2899 | prefix, protect_system_to_string(c->protect_system), | |
f3e43635 | 2900 | prefix, yes_no(c->ignore_sigpipe), |
f4170c67 LP |
2901 | prefix, yes_no(c->memory_deny_write_execute), |
2902 | prefix, yes_no(c->restrict_realtime)); | |
fb33a393 | 2903 | |
8c7be95e LP |
2904 | STRV_FOREACH(e, c->environment) |
2905 | fprintf(f, "%sEnvironment: %s\n", prefix, *e); | |
2906 | ||
2907 | STRV_FOREACH(e, c->environment_files) | |
2908 | fprintf(f, "%sEnvironmentFile: %s\n", prefix, *e); | |
94f04347 | 2909 | |
b4c14404 FB |
2910 | STRV_FOREACH(e, c->pass_environment) |
2911 | fprintf(f, "%sPassEnvironment: %s\n", prefix, *e); | |
2912 | ||
c2bbd90b EV |
2913 | fprintf(f, "%sRuntimeDirectoryMode: %04o\n", prefix, c->runtime_directory_mode); |
2914 | ||
2915 | STRV_FOREACH(d, c->runtime_directory) | |
2916 | fprintf(f, "%sRuntimeDirectory: %s\n", prefix, *d); | |
2917 | ||
fb33a393 LP |
2918 | if (c->nice_set) |
2919 | fprintf(f, | |
2920 | "%sNice: %i\n", | |
2921 | prefix, c->nice); | |
2922 | ||
dd6c17b1 | 2923 | if (c->oom_score_adjust_set) |
fb33a393 | 2924 | fprintf(f, |
dd6c17b1 LP |
2925 | "%sOOMScoreAdjust: %i\n", |
2926 | prefix, c->oom_score_adjust); | |
9eba9da4 | 2927 | |
94f04347 | 2928 | for (i = 0; i < RLIM_NLIMITS; i++) |
3c11da9d EV |
2929 | if (c->rlimit[i]) { |
2930 | fprintf(f, "%s%s: " RLIM_FMT "\n", | |
2931 | prefix, rlimit_to_string(i), c->rlimit[i]->rlim_max); | |
2932 | fprintf(f, "%s%sSoft: " RLIM_FMT "\n", | |
2933 | prefix, rlimit_to_string(i), c->rlimit[i]->rlim_cur); | |
2934 | } | |
94f04347 | 2935 | |
f8b69d1d | 2936 | if (c->ioprio_set) { |
1756a011 | 2937 | _cleanup_free_ char *class_str = NULL; |
f8b69d1d | 2938 | |
1756a011 | 2939 | ioprio_class_to_string_alloc(IOPRIO_PRIO_CLASS(c->ioprio), &class_str); |
9eba9da4 LP |
2940 | fprintf(f, |
2941 | "%sIOSchedulingClass: %s\n" | |
2942 | "%sIOPriority: %i\n", | |
f8b69d1d | 2943 | prefix, strna(class_str), |
9eba9da4 | 2944 | prefix, (int) IOPRIO_PRIO_DATA(c->ioprio)); |
f8b69d1d | 2945 | } |
94f04347 | 2946 | |
f8b69d1d | 2947 | if (c->cpu_sched_set) { |
1756a011 | 2948 | _cleanup_free_ char *policy_str = NULL; |
f8b69d1d | 2949 | |
1756a011 | 2950 | sched_policy_to_string_alloc(c->cpu_sched_policy, &policy_str); |
94f04347 LP |
2951 | fprintf(f, |
2952 | "%sCPUSchedulingPolicy: %s\n" | |
38b48754 LP |
2953 | "%sCPUSchedulingPriority: %i\n" |
2954 | "%sCPUSchedulingResetOnFork: %s\n", | |
f8b69d1d | 2955 | prefix, strna(policy_str), |
38b48754 LP |
2956 | prefix, c->cpu_sched_priority, |
2957 | prefix, yes_no(c->cpu_sched_reset_on_fork)); | |
b929bf04 | 2958 | } |
94f04347 | 2959 | |
82c121a4 | 2960 | if (c->cpuset) { |
94f04347 | 2961 | fprintf(f, "%sCPUAffinity:", prefix); |
82c121a4 LP |
2962 | for (i = 0; i < c->cpuset_ncpus; i++) |
2963 | if (CPU_ISSET_S(i, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset)) | |
43a99a7a | 2964 | fprintf(f, " %u", i); |
94f04347 LP |
2965 | fputs("\n", f); |
2966 | } | |
2967 | ||
3a43da28 | 2968 | if (c->timer_slack_nsec != NSEC_INFINITY) |
ccd06097 | 2969 | fprintf(f, "%sTimerSlackNSec: "NSEC_FMT "\n", prefix, c->timer_slack_nsec); |
94f04347 LP |
2970 | |
2971 | fprintf(f, | |
80876c20 LP |
2972 | "%sStandardInput: %s\n" |
2973 | "%sStandardOutput: %s\n" | |
2974 | "%sStandardError: %s\n", | |
2975 | prefix, exec_input_to_string(c->std_input), | |
2976 | prefix, exec_output_to_string(c->std_output), | |
2977 | prefix, exec_output_to_string(c->std_error)); | |
2978 | ||
2979 | if (c->tty_path) | |
2980 | fprintf(f, | |
6ea832a2 LP |
2981 | "%sTTYPath: %s\n" |
2982 | "%sTTYReset: %s\n" | |
2983 | "%sTTYVHangup: %s\n" | |
2984 | "%sTTYVTDisallocate: %s\n", | |
2985 | prefix, c->tty_path, | |
2986 | prefix, yes_no(c->tty_reset), | |
2987 | prefix, yes_no(c->tty_vhangup), | |
2988 | prefix, yes_no(c->tty_vt_disallocate)); | |
94f04347 | 2989 | |
5ce70e5b ZJS |
2990 | if (c->std_output == EXEC_OUTPUT_SYSLOG || |
2991 | c->std_output == EXEC_OUTPUT_KMSG || | |
2992 | c->std_output == EXEC_OUTPUT_JOURNAL || | |
2993 | c->std_output == EXEC_OUTPUT_SYSLOG_AND_CONSOLE || | |
2994 | c->std_output == EXEC_OUTPUT_KMSG_AND_CONSOLE || | |
2995 | c->std_output == EXEC_OUTPUT_JOURNAL_AND_CONSOLE || | |
2996 | c->std_error == EXEC_OUTPUT_SYSLOG || | |
2997 | c->std_error == EXEC_OUTPUT_KMSG || | |
2998 | c->std_error == EXEC_OUTPUT_JOURNAL || | |
2999 | c->std_error == EXEC_OUTPUT_SYSLOG_AND_CONSOLE || | |
3000 | c->std_error == EXEC_OUTPUT_KMSG_AND_CONSOLE || | |
3001 | c->std_error == EXEC_OUTPUT_JOURNAL_AND_CONSOLE) { | |
f8b69d1d | 3002 | |
5ce70e5b | 3003 | _cleanup_free_ char *fac_str = NULL, *lvl_str = NULL; |
f8b69d1d | 3004 | |
5ce70e5b ZJS |
3005 | log_facility_unshifted_to_string_alloc(c->syslog_priority >> 3, &fac_str); |
3006 | log_level_to_string_alloc(LOG_PRI(c->syslog_priority), &lvl_str); | |
f8b69d1d | 3007 | |
94f04347 LP |
3008 | fprintf(f, |
3009 | "%sSyslogFacility: %s\n" | |
3010 | "%sSyslogLevel: %s\n", | |
f8b69d1d MS |
3011 | prefix, strna(fac_str), |
3012 | prefix, strna(lvl_str)); | |
f8b69d1d | 3013 | } |
94f04347 | 3014 | |
94f04347 LP |
3015 | if (c->secure_bits) |
3016 | fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n", | |
3017 | prefix, | |
cbb21cca ZJS |
3018 | (c->secure_bits & 1<<SECURE_KEEP_CAPS) ? " keep-caps" : "", |
3019 | (c->secure_bits & 1<<SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "", | |
3020 | (c->secure_bits & 1<<SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "", | |
3021 | (c->secure_bits & 1<<SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "", | |
3022 | (c->secure_bits & 1<<SECURE_NOROOT) ? " noroot" : "", | |
3023 | (c->secure_bits & 1<<SECURE_NOROOT_LOCKED) ? "noroot-locked" : ""); | |
94f04347 | 3024 | |
a103496c | 3025 | if (c->capability_bounding_set != CAP_ALL) { |
ae556c21 | 3026 | unsigned long l; |
260abb78 | 3027 | fprintf(f, "%sCapabilityBoundingSet:", prefix); |
94f04347 | 3028 | |
64685e0c | 3029 | for (l = 0; l <= cap_last_cap(); l++) |
a103496c | 3030 | if (c->capability_bounding_set & (UINT64_C(1) << l)) |
2822da4f | 3031 | fprintf(f, " %s", strna(capability_to_name(l))); |
94f04347 LP |
3032 | |
3033 | fputs("\n", f); | |
755d4b67 IP |
3034 | } |
3035 | ||
3036 | if (c->capability_ambient_set != 0) { | |
3037 | unsigned long l; | |
3038 | fprintf(f, "%sAmbientCapabilities:", prefix); | |
3039 | ||
3040 | for (l = 0; l <= cap_last_cap(); l++) | |
3041 | if (c->capability_ambient_set & (UINT64_C(1) << l)) | |
3042 | fprintf(f, " %s", strna(capability_to_name(l))); | |
3043 | ||
3044 | fputs("\n", f); | |
94f04347 LP |
3045 | } |
3046 | ||
3047 | if (c->user) | |
f2d3769a | 3048 | fprintf(f, "%sUser: %s\n", prefix, c->user); |
94f04347 | 3049 | if (c->group) |
f2d3769a | 3050 | fprintf(f, "%sGroup: %s\n", prefix, c->group); |
94f04347 | 3051 | |
29206d46 LP |
3052 | fprintf(f, "%sDynamicUser: %s\n", prefix, yes_no(c->dynamic_user)); |
3053 | ||
15ae422b | 3054 | if (strv_length(c->supplementary_groups) > 0) { |
94f04347 | 3055 | fprintf(f, "%sSupplementaryGroups:", prefix); |
15ae422b LP |
3056 | strv_fprintf(f, c->supplementary_groups); |
3057 | fputs("\n", f); | |
3058 | } | |
94f04347 | 3059 | |
5b6319dc | 3060 | if (c->pam_name) |
f2d3769a | 3061 | fprintf(f, "%sPAMName: %s\n", prefix, c->pam_name); |
5b6319dc | 3062 | |
2a624c36 AP |
3063 | if (strv_length(c->read_write_paths) > 0) { |
3064 | fprintf(f, "%sReadWritePaths:", prefix); | |
3065 | strv_fprintf(f, c->read_write_paths); | |
15ae422b LP |
3066 | fputs("\n", f); |
3067 | } | |
3068 | ||
2a624c36 AP |
3069 | if (strv_length(c->read_only_paths) > 0) { |
3070 | fprintf(f, "%sReadOnlyPaths:", prefix); | |
3071 | strv_fprintf(f, c->read_only_paths); | |
15ae422b LP |
3072 | fputs("\n", f); |
3073 | } | |
94f04347 | 3074 | |
2a624c36 AP |
3075 | if (strv_length(c->inaccessible_paths) > 0) { |
3076 | fprintf(f, "%sInaccessiblePaths:", prefix); | |
3077 | strv_fprintf(f, c->inaccessible_paths); | |
94f04347 LP |
3078 | fputs("\n", f); |
3079 | } | |
2e22afe9 | 3080 | |
169c1bda LP |
3081 | if (c->utmp_id) |
3082 | fprintf(f, | |
3083 | "%sUtmpIdentifier: %s\n", | |
3084 | prefix, c->utmp_id); | |
7b52a628 MS |
3085 | |
3086 | if (c->selinux_context) | |
3087 | fprintf(f, | |
5f8640fb LP |
3088 | "%sSELinuxContext: %s%s\n", |
3089 | prefix, c->selinux_context_ignore ? "-" : "", c->selinux_context); | |
17df7223 | 3090 | |
050f7277 | 3091 | if (c->personality != PERSONALITY_INVALID) |
ac45f971 LP |
3092 | fprintf(f, |
3093 | "%sPersonality: %s\n", | |
3094 | prefix, strna(personality_to_string(c->personality))); | |
3095 | ||
17df7223 | 3096 | if (c->syscall_filter) { |
351a19b1 | 3097 | #ifdef HAVE_SECCOMP |
17df7223 LP |
3098 | Iterator j; |
3099 | void *id; | |
3100 | bool first = true; | |
351a19b1 | 3101 | #endif |
17df7223 LP |
3102 | |
3103 | fprintf(f, | |
57183d11 | 3104 | "%sSystemCallFilter: ", |
17df7223 LP |
3105 | prefix); |
3106 | ||
3107 | if (!c->syscall_whitelist) | |
3108 | fputc('~', f); | |
3109 | ||
351a19b1 | 3110 | #ifdef HAVE_SECCOMP |
17df7223 LP |
3111 | SET_FOREACH(id, c->syscall_filter, j) { |
3112 | _cleanup_free_ char *name = NULL; | |
3113 | ||
3114 | if (first) | |
3115 | first = false; | |
3116 | else | |
3117 | fputc(' ', f); | |
3118 | ||
57183d11 | 3119 | name = seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE, PTR_TO_INT(id) - 1); |
17df7223 LP |
3120 | fputs(strna(name), f); |
3121 | } | |
351a19b1 | 3122 | #endif |
17df7223 LP |
3123 | |
3124 | fputc('\n', f); | |
3125 | } | |
3126 | ||
57183d11 LP |
3127 | if (c->syscall_archs) { |
3128 | #ifdef HAVE_SECCOMP | |
3129 | Iterator j; | |
3130 | void *id; | |
3131 | #endif | |
3132 | ||
3133 | fprintf(f, | |
3134 | "%sSystemCallArchitectures:", | |
3135 | prefix); | |
3136 | ||
3137 | #ifdef HAVE_SECCOMP | |
3138 | SET_FOREACH(id, c->syscall_archs, j) | |
3139 | fprintf(f, " %s", strna(seccomp_arch_to_string(PTR_TO_UINT32(id) - 1))); | |
3140 | #endif | |
3141 | fputc('\n', f); | |
3142 | } | |
3143 | ||
b3267152 | 3144 | if (c->syscall_errno > 0) |
17df7223 LP |
3145 | fprintf(f, |
3146 | "%sSystemCallErrorNumber: %s\n", | |
3147 | prefix, strna(errno_to_name(c->syscall_errno))); | |
eef65bf3 MS |
3148 | |
3149 | if (c->apparmor_profile) | |
3150 | fprintf(f, | |
3151 | "%sAppArmorProfile: %s%s\n", | |
3152 | prefix, c->apparmor_profile_ignore ? "-" : "", c->apparmor_profile); | |
5cb5a6ff LP |
3153 | } |
3154 | ||
a931ad47 LP |
3155 | bool exec_context_maintains_privileges(ExecContext *c) { |
3156 | assert(c); | |
3157 | ||
61233823 | 3158 | /* Returns true if the process forked off would run under |
a931ad47 LP |
3159 | * an unchanged UID or as root. */ |
3160 | ||
3161 | if (!c->user) | |
3162 | return true; | |
3163 | ||
3164 | if (streq(c->user, "root") || streq(c->user, "0")) | |
3165 | return true; | |
3166 | ||
3167 | return false; | |
3168 | } | |
3169 | ||
b58b4116 | 3170 | void exec_status_start(ExecStatus *s, pid_t pid) { |
034c6ed7 | 3171 | assert(s); |
5cb5a6ff | 3172 | |
b58b4116 LP |
3173 | zero(*s); |
3174 | s->pid = pid; | |
3175 | dual_timestamp_get(&s->start_timestamp); | |
3176 | } | |
3177 | ||
6ea832a2 | 3178 | void exec_status_exit(ExecStatus *s, ExecContext *context, pid_t pid, int code, int status) { |
b58b4116 LP |
3179 | assert(s); |
3180 | ||
0b1f4ae6 | 3181 | if (s->pid && s->pid != pid) |
b58b4116 LP |
3182 | zero(*s); |
3183 | ||
034c6ed7 | 3184 | s->pid = pid; |
63983207 | 3185 | dual_timestamp_get(&s->exit_timestamp); |
9fb86720 | 3186 | |
034c6ed7 LP |
3187 | s->code = code; |
3188 | s->status = status; | |
169c1bda | 3189 | |
6ea832a2 LP |
3190 | if (context) { |
3191 | if (context->utmp_id) | |
3192 | utmp_put_dead_process(context->utmp_id, pid, code, status); | |
3193 | ||
1e22b5cd | 3194 | exec_context_tty_reset(context, NULL); |
6ea832a2 | 3195 | } |
9fb86720 LP |
3196 | } |
3197 | ||
3198 | void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) { | |
3199 | char buf[FORMAT_TIMESTAMP_MAX]; | |
3200 | ||
3201 | assert(s); | |
3202 | assert(f); | |
3203 | ||
9fb86720 LP |
3204 | if (s->pid <= 0) |
3205 | return; | |
3206 | ||
4c940960 LP |
3207 | prefix = strempty(prefix); |
3208 | ||
9fb86720 | 3209 | fprintf(f, |
ccd06097 ZJS |
3210 | "%sPID: "PID_FMT"\n", |
3211 | prefix, s->pid); | |
9fb86720 | 3212 | |
af9d16e1 | 3213 | if (dual_timestamp_is_set(&s->start_timestamp)) |
9fb86720 LP |
3214 | fprintf(f, |
3215 | "%sStart Timestamp: %s\n", | |
63983207 | 3216 | prefix, format_timestamp(buf, sizeof(buf), s->start_timestamp.realtime)); |
9fb86720 | 3217 | |
af9d16e1 | 3218 | if (dual_timestamp_is_set(&s->exit_timestamp)) |
9fb86720 LP |
3219 | fprintf(f, |
3220 | "%sExit Timestamp: %s\n" | |
3221 | "%sExit Code: %s\n" | |
3222 | "%sExit Status: %i\n", | |
63983207 | 3223 | prefix, format_timestamp(buf, sizeof(buf), s->exit_timestamp.realtime), |
9fb86720 LP |
3224 | prefix, sigchld_code_to_string(s->code), |
3225 | prefix, s->status); | |
5cb5a6ff | 3226 | } |
44d8db9e | 3227 | |
9e2f7c11 | 3228 | char *exec_command_line(char **argv) { |
44d8db9e LP |
3229 | size_t k; |
3230 | char *n, *p, **a; | |
3231 | bool first = true; | |
3232 | ||
9e2f7c11 | 3233 | assert(argv); |
44d8db9e | 3234 | |
9164977d | 3235 | k = 1; |
9e2f7c11 | 3236 | STRV_FOREACH(a, argv) |
44d8db9e LP |
3237 | k += strlen(*a)+3; |
3238 | ||
3239 | if (!(n = new(char, k))) | |
3240 | return NULL; | |
3241 | ||
3242 | p = n; | |
9e2f7c11 | 3243 | STRV_FOREACH(a, argv) { |
44d8db9e LP |
3244 | |
3245 | if (!first) | |
3246 | *(p++) = ' '; | |
3247 | else | |
3248 | first = false; | |
3249 | ||
3250 | if (strpbrk(*a, WHITESPACE)) { | |
3251 | *(p++) = '\''; | |
3252 | p = stpcpy(p, *a); | |
3253 | *(p++) = '\''; | |
3254 | } else | |
3255 | p = stpcpy(p, *a); | |
3256 | ||
3257 | } | |
3258 | ||
9164977d LP |
3259 | *p = 0; |
3260 | ||
44d8db9e LP |
3261 | /* FIXME: this doesn't really handle arguments that have |
3262 | * spaces and ticks in them */ | |
3263 | ||
3264 | return n; | |
3265 | } | |
3266 | ||
3267 | void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) { | |
e1d75803 | 3268 | _cleanup_free_ char *cmd = NULL; |
4c940960 | 3269 | const char *prefix2; |
44d8db9e LP |
3270 | |
3271 | assert(c); | |
3272 | assert(f); | |
3273 | ||
4c940960 | 3274 | prefix = strempty(prefix); |
63c372cb | 3275 | prefix2 = strjoina(prefix, "\t"); |
44d8db9e | 3276 | |
9e2f7c11 | 3277 | cmd = exec_command_line(c->argv); |
44d8db9e LP |
3278 | fprintf(f, |
3279 | "%sCommand Line: %s\n", | |
3280 | prefix, cmd ? cmd : strerror(ENOMEM)); | |
3281 | ||
9fb86720 | 3282 | exec_status_dump(&c->exec_status, f, prefix2); |
44d8db9e LP |
3283 | } |
3284 | ||
3285 | void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) { | |
3286 | assert(f); | |
3287 | ||
4c940960 | 3288 | prefix = strempty(prefix); |
44d8db9e LP |
3289 | |
3290 | LIST_FOREACH(command, c, c) | |
3291 | exec_command_dump(c, f, prefix); | |
3292 | } | |
94f04347 | 3293 | |
a6a80b4f LP |
3294 | void exec_command_append_list(ExecCommand **l, ExecCommand *e) { |
3295 | ExecCommand *end; | |
3296 | ||
3297 | assert(l); | |
3298 | assert(e); | |
3299 | ||
3300 | if (*l) { | |
35b8ca3a | 3301 | /* It's kind of important, that we keep the order here */ |
71fda00f LP |
3302 | LIST_FIND_TAIL(command, *l, end); |
3303 | LIST_INSERT_AFTER(command, *l, end, e); | |
a6a80b4f LP |
3304 | } else |
3305 | *l = e; | |
3306 | } | |
3307 | ||
26fd040d LP |
3308 | int exec_command_set(ExecCommand *c, const char *path, ...) { |
3309 | va_list ap; | |
3310 | char **l, *p; | |
3311 | ||
3312 | assert(c); | |
3313 | assert(path); | |
3314 | ||
3315 | va_start(ap, path); | |
3316 | l = strv_new_ap(path, ap); | |
3317 | va_end(ap); | |
3318 | ||
3319 | if (!l) | |
3320 | return -ENOMEM; | |
3321 | ||
250a918d LP |
3322 | p = strdup(path); |
3323 | if (!p) { | |
26fd040d LP |
3324 | strv_free(l); |
3325 | return -ENOMEM; | |
3326 | } | |
3327 | ||
3328 | free(c->path); | |
3329 | c->path = p; | |
3330 | ||
3331 | strv_free(c->argv); | |
3332 | c->argv = l; | |
3333 | ||
3334 | return 0; | |
3335 | } | |
3336 | ||
86b23b07 | 3337 | int exec_command_append(ExecCommand *c, const char *path, ...) { |
e63ff941 | 3338 | _cleanup_strv_free_ char **l = NULL; |
86b23b07 | 3339 | va_list ap; |
86b23b07 JS |
3340 | int r; |
3341 | ||
3342 | assert(c); | |
3343 | assert(path); | |
3344 | ||
3345 | va_start(ap, path); | |
3346 | l = strv_new_ap(path, ap); | |
3347 | va_end(ap); | |
3348 | ||
3349 | if (!l) | |
3350 | return -ENOMEM; | |
3351 | ||
e287086b | 3352 | r = strv_extend_strv(&c->argv, l, false); |
e63ff941 | 3353 | if (r < 0) |
86b23b07 | 3354 | return r; |
86b23b07 JS |
3355 | |
3356 | return 0; | |
3357 | } | |
3358 | ||
3359 | ||
613b411c LP |
3360 | static int exec_runtime_allocate(ExecRuntime **rt) { |
3361 | ||
3362 | if (*rt) | |
3363 | return 0; | |
3364 | ||
3365 | *rt = new0(ExecRuntime, 1); | |
f146f5e1 | 3366 | if (!*rt) |
613b411c LP |
3367 | return -ENOMEM; |
3368 | ||
3369 | (*rt)->n_ref = 1; | |
3370 | (*rt)->netns_storage_socket[0] = (*rt)->netns_storage_socket[1] = -1; | |
3371 | ||
3372 | return 0; | |
3373 | } | |
3374 | ||
3375 | int exec_runtime_make(ExecRuntime **rt, ExecContext *c, const char *id) { | |
3376 | int r; | |
3377 | ||
3378 | assert(rt); | |
3379 | assert(c); | |
3380 | assert(id); | |
3381 | ||
3382 | if (*rt) | |
3383 | return 1; | |
3384 | ||
3385 | if (!c->private_network && !c->private_tmp) | |
3386 | return 0; | |
3387 | ||
3388 | r = exec_runtime_allocate(rt); | |
3389 | if (r < 0) | |
3390 | return r; | |
3391 | ||
3392 | if (c->private_network && (*rt)->netns_storage_socket[0] < 0) { | |
33df919d | 3393 | if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, (*rt)->netns_storage_socket) < 0) |
613b411c LP |
3394 | return -errno; |
3395 | } | |
3396 | ||
3397 | if (c->private_tmp && !(*rt)->tmp_dir) { | |
3398 | r = setup_tmp_dirs(id, &(*rt)->tmp_dir, &(*rt)->var_tmp_dir); | |
3399 | if (r < 0) | |
3400 | return r; | |
3401 | } | |
3402 | ||
3403 | return 1; | |
3404 | } | |
3405 | ||
3406 | ExecRuntime *exec_runtime_ref(ExecRuntime *r) { | |
3407 | assert(r); | |
3408 | assert(r->n_ref > 0); | |
3409 | ||
3410 | r->n_ref++; | |
3411 | return r; | |
3412 | } | |
3413 | ||
3414 | ExecRuntime *exec_runtime_unref(ExecRuntime *r) { | |
3415 | ||
3416 | if (!r) | |
3417 | return NULL; | |
3418 | ||
3419 | assert(r->n_ref > 0); | |
3420 | ||
3421 | r->n_ref--; | |
f2341e0a LP |
3422 | if (r->n_ref > 0) |
3423 | return NULL; | |
3424 | ||
3425 | free(r->tmp_dir); | |
3426 | free(r->var_tmp_dir); | |
3427 | safe_close_pair(r->netns_storage_socket); | |
3428 | free(r); | |
613b411c LP |
3429 | |
3430 | return NULL; | |
3431 | } | |
3432 | ||
f2341e0a | 3433 | int exec_runtime_serialize(Unit *u, ExecRuntime *rt, FILE *f, FDSet *fds) { |
613b411c LP |
3434 | assert(u); |
3435 | assert(f); | |
3436 | assert(fds); | |
3437 | ||
3438 | if (!rt) | |
3439 | return 0; | |
3440 | ||
3441 | if (rt->tmp_dir) | |
3442 | unit_serialize_item(u, f, "tmp-dir", rt->tmp_dir); | |
3443 | ||
3444 | if (rt->var_tmp_dir) | |
3445 | unit_serialize_item(u, f, "var-tmp-dir", rt->var_tmp_dir); | |
3446 | ||
3447 | if (rt->netns_storage_socket[0] >= 0) { | |
3448 | int copy; | |
3449 | ||
3450 | copy = fdset_put_dup(fds, rt->netns_storage_socket[0]); | |
3451 | if (copy < 0) | |
3452 | return copy; | |
3453 | ||
3454 | unit_serialize_item_format(u, f, "netns-socket-0", "%i", copy); | |
3455 | } | |
3456 | ||
3457 | if (rt->netns_storage_socket[1] >= 0) { | |
3458 | int copy; | |
3459 | ||
3460 | copy = fdset_put_dup(fds, rt->netns_storage_socket[1]); | |
3461 | if (copy < 0) | |
3462 | return copy; | |
3463 | ||
3464 | unit_serialize_item_format(u, f, "netns-socket-1", "%i", copy); | |
3465 | } | |
3466 | ||
3467 | return 0; | |
3468 | } | |
3469 | ||
f2341e0a | 3470 | int exec_runtime_deserialize_item(Unit *u, ExecRuntime **rt, const char *key, const char *value, FDSet *fds) { |
613b411c LP |
3471 | int r; |
3472 | ||
3473 | assert(rt); | |
3474 | assert(key); | |
3475 | assert(value); | |
3476 | ||
3477 | if (streq(key, "tmp-dir")) { | |
3478 | char *copy; | |
3479 | ||
3480 | r = exec_runtime_allocate(rt); | |
3481 | if (r < 0) | |
f2341e0a | 3482 | return log_oom(); |
613b411c LP |
3483 | |
3484 | copy = strdup(value); | |
3485 | if (!copy) | |
3486 | return log_oom(); | |
3487 | ||
3488 | free((*rt)->tmp_dir); | |
3489 | (*rt)->tmp_dir = copy; | |
3490 | ||
3491 | } else if (streq(key, "var-tmp-dir")) { | |
3492 | char *copy; | |
3493 | ||
3494 | r = exec_runtime_allocate(rt); | |
3495 | if (r < 0) | |
f2341e0a | 3496 | return log_oom(); |
613b411c LP |
3497 | |
3498 | copy = strdup(value); | |
3499 | if (!copy) | |
3500 | return log_oom(); | |
3501 | ||
3502 | free((*rt)->var_tmp_dir); | |
3503 | (*rt)->var_tmp_dir = copy; | |
3504 | ||
3505 | } else if (streq(key, "netns-socket-0")) { | |
3506 | int fd; | |
3507 | ||
3508 | r = exec_runtime_allocate(rt); | |
3509 | if (r < 0) | |
f2341e0a | 3510 | return log_oom(); |
613b411c LP |
3511 | |
3512 | if (safe_atoi(value, &fd) < 0 || !fdset_contains(fds, fd)) | |
f2341e0a | 3513 | log_unit_debug(u, "Failed to parse netns socket value: %s", value); |
613b411c | 3514 | else { |
03e334a1 | 3515 | safe_close((*rt)->netns_storage_socket[0]); |
613b411c LP |
3516 | (*rt)->netns_storage_socket[0] = fdset_remove(fds, fd); |
3517 | } | |
3518 | } else if (streq(key, "netns-socket-1")) { | |
3519 | int fd; | |
3520 | ||
3521 | r = exec_runtime_allocate(rt); | |
3522 | if (r < 0) | |
f2341e0a | 3523 | return log_oom(); |
613b411c LP |
3524 | |
3525 | if (safe_atoi(value, &fd) < 0 || !fdset_contains(fds, fd)) | |
f2341e0a | 3526 | log_unit_debug(u, "Failed to parse netns socket value: %s", value); |
613b411c | 3527 | else { |
03e334a1 | 3528 | safe_close((*rt)->netns_storage_socket[1]); |
613b411c LP |
3529 | (*rt)->netns_storage_socket[1] = fdset_remove(fds, fd); |
3530 | } | |
3531 | } else | |
3532 | return 0; | |
3533 | ||
3534 | return 1; | |
3535 | } | |
3536 | ||
3537 | static void *remove_tmpdir_thread(void *p) { | |
3538 | _cleanup_free_ char *path = p; | |
3539 | ||
c6878637 | 3540 | (void) rm_rf(path, REMOVE_ROOT|REMOVE_PHYSICAL); |
613b411c LP |
3541 | return NULL; |
3542 | } | |
3543 | ||
3544 | void exec_runtime_destroy(ExecRuntime *rt) { | |
98b47d54 LP |
3545 | int r; |
3546 | ||
613b411c LP |
3547 | if (!rt) |
3548 | return; | |
3549 | ||
3550 | /* If there are multiple users of this, let's leave the stuff around */ | |
3551 | if (rt->n_ref > 1) | |
3552 | return; | |
3553 | ||
3554 | if (rt->tmp_dir) { | |
3555 | log_debug("Spawning thread to nuke %s", rt->tmp_dir); | |
98b47d54 LP |
3556 | |
3557 | r = asynchronous_job(remove_tmpdir_thread, rt->tmp_dir); | |
3558 | if (r < 0) { | |
da927ba9 | 3559 | log_warning_errno(r, "Failed to nuke %s: %m", rt->tmp_dir); |
98b47d54 LP |
3560 | free(rt->tmp_dir); |
3561 | } | |
3562 | ||
613b411c LP |
3563 | rt->tmp_dir = NULL; |
3564 | } | |
3565 | ||
3566 | if (rt->var_tmp_dir) { | |
3567 | log_debug("Spawning thread to nuke %s", rt->var_tmp_dir); | |
98b47d54 LP |
3568 | |
3569 | r = asynchronous_job(remove_tmpdir_thread, rt->var_tmp_dir); | |
3570 | if (r < 0) { | |
da927ba9 | 3571 | log_warning_errno(r, "Failed to nuke %s: %m", rt->var_tmp_dir); |
98b47d54 LP |
3572 | free(rt->var_tmp_dir); |
3573 | } | |
3574 | ||
613b411c LP |
3575 | rt->var_tmp_dir = NULL; |
3576 | } | |
3577 | ||
3d94f76c | 3578 | safe_close_pair(rt->netns_storage_socket); |
613b411c LP |
3579 | } |
3580 | ||
80876c20 LP |
3581 | static const char* const exec_input_table[_EXEC_INPUT_MAX] = { |
3582 | [EXEC_INPUT_NULL] = "null", | |
3583 | [EXEC_INPUT_TTY] = "tty", | |
3584 | [EXEC_INPUT_TTY_FORCE] = "tty-force", | |
4f2d528d LP |
3585 | [EXEC_INPUT_TTY_FAIL] = "tty-fail", |
3586 | [EXEC_INPUT_SOCKET] = "socket" | |
80876c20 LP |
3587 | }; |
3588 | ||
8a0867d6 LP |
3589 | DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput); |
3590 | ||
94f04347 | 3591 | static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = { |
80876c20 | 3592 | [EXEC_OUTPUT_INHERIT] = "inherit", |
94f04347 | 3593 | [EXEC_OUTPUT_NULL] = "null", |
80876c20 | 3594 | [EXEC_OUTPUT_TTY] = "tty", |
94f04347 | 3595 | [EXEC_OUTPUT_SYSLOG] = "syslog", |
28dbc1e8 | 3596 | [EXEC_OUTPUT_SYSLOG_AND_CONSOLE] = "syslog+console", |
9a6bca7a | 3597 | [EXEC_OUTPUT_KMSG] = "kmsg", |
28dbc1e8 | 3598 | [EXEC_OUTPUT_KMSG_AND_CONSOLE] = "kmsg+console", |
706343f4 LP |
3599 | [EXEC_OUTPUT_JOURNAL] = "journal", |
3600 | [EXEC_OUTPUT_JOURNAL_AND_CONSOLE] = "journal+console", | |
4f2d528d | 3601 | [EXEC_OUTPUT_SOCKET] = "socket" |
94f04347 LP |
3602 | }; |
3603 | ||
3604 | DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput); | |
023a4f67 LP |
3605 | |
3606 | static const char* const exec_utmp_mode_table[_EXEC_UTMP_MODE_MAX] = { | |
3607 | [EXEC_UTMP_INIT] = "init", | |
3608 | [EXEC_UTMP_LOGIN] = "login", | |
3609 | [EXEC_UTMP_USER] = "user", | |
3610 | }; | |
3611 | ||
3612 | DEFINE_STRING_TABLE_LOOKUP(exec_utmp_mode, ExecUtmpMode); |