]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
5cb5a6ff | 2 | |
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
9 | under the terms of the GNU Lesser General Public License as published by |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
11 | (at your option) any later version. |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 16 | Lesser General Public License for more details. |
a7334b09 | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
034c6ed7 LP |
22 | #include <errno.h> |
23 | #include <fcntl.h> | |
24 | #include <unistd.h> | |
44d8db9e | 25 | #include <string.h> |
309bff19 | 26 | #include <signal.h> |
071830ff LP |
27 | #include <sys/socket.h> |
28 | #include <sys/un.h> | |
94f04347 | 29 | #include <sys/prctl.h> |
451a074f | 30 | #include <sys/stat.h> |
81a2b7ce | 31 | #include <grp.h> |
0a6f50c0 | 32 | #include <poll.h> |
2bef10ab | 33 | #include <glob.h> |
ac45f971 | 34 | #include <sys/personality.h> |
5cb5a6ff | 35 | |
5b6319dc LP |
36 | #ifdef HAVE_PAM |
37 | #include <security/pam_appl.h> | |
38 | #endif | |
39 | ||
7b52a628 MS |
40 | #ifdef HAVE_SELINUX |
41 | #include <selinux/selinux.h> | |
42 | #endif | |
43 | ||
17df7223 LP |
44 | #ifdef HAVE_SECCOMP |
45 | #include <seccomp.h> | |
46 | #endif | |
47 | ||
eef65bf3 MS |
48 | #ifdef HAVE_APPARMOR |
49 | #include <sys/apparmor.h> | |
50 | #endif | |
51 | ||
24882e06 | 52 | #include "sd-messages.h" |
c6878637 | 53 | #include "rm-rf.h" |
5cb5a6ff LP |
54 | #include "strv.h" |
55 | #include "macro.h" | |
d7832d2c | 56 | #include "capability.h" |
5cb5a6ff | 57 | #include "util.h" |
acbb0225 | 58 | #include "log.h" |
9eba9da4 | 59 | #include "ioprio.h" |
94f04347 | 60 | #include "securebits.h" |
15ae422b | 61 | #include "namespace.h" |
d06dacd0 | 62 | #include "exit-status.h" |
dd6c17b1 | 63 | #include "missing.h" |
169c1bda | 64 | #include "utmp-wtmp.h" |
f6a6225e | 65 | #include "def.h" |
9eb977db | 66 | #include "path-util.h" |
4d1a6904 | 67 | #include "env-util.h" |
a5c32cff | 68 | #include "fileio.h" |
4ad49000 | 69 | #include "unit.h" |
f485606b | 70 | #include "async.h" |
0d3f7bb3 | 71 | #include "selinux-util.h" |
17df7223 | 72 | #include "errno-list.h" |
4298d0b5 | 73 | #include "af-list.h" |
e66cf1a3 | 74 | #include "mkdir.h" |
2ca620c4 | 75 | #include "smack-util.h" |
3c70e3bb | 76 | #include "bus-endpoint.h" |
2822da4f | 77 | #include "cap-list.h" |
6482f626 | 78 | #include "formats-util.h" |
0b452006 | 79 | #include "process-util.h" |
288a74cc | 80 | #include "terminal-util.h" |
24882e06 | 81 | #include "signal-util.h" |
5cb5a6ff | 82 | |
2eec67ac TA |
83 | #ifdef HAVE_APPARMOR |
84 | #include "apparmor-util.h" | |
85 | #endif | |
86 | ||
57183d11 LP |
87 | #ifdef HAVE_SECCOMP |
88 | #include "seccomp-util.h" | |
89 | #endif | |
90 | ||
24882e06 LP |
91 | #include "execute.h" |
92 | ||
e056b01d | 93 | #define IDLE_TIMEOUT_USEC (5*USEC_PER_SEC) |
31a7eb86 | 94 | #define IDLE_TIMEOUT2_USEC (1*USEC_PER_SEC) |
e6a26745 | 95 | |
02a51aba LP |
96 | /* This assumes there is a 'tty' group */ |
97 | #define TTY_MODE 0620 | |
98 | ||
531dca78 LP |
99 | #define SNDBUF_SIZE (8*1024*1024) |
100 | ||
034c6ed7 LP |
101 | static int shift_fds(int fds[], unsigned n_fds) { |
102 | int start, restart_from; | |
103 | ||
104 | if (n_fds <= 0) | |
105 | return 0; | |
106 | ||
a0d40ac5 LP |
107 | /* Modifies the fds array! (sorts it) */ |
108 | ||
034c6ed7 LP |
109 | assert(fds); |
110 | ||
111 | start = 0; | |
112 | for (;;) { | |
113 | int i; | |
114 | ||
115 | restart_from = -1; | |
116 | ||
117 | for (i = start; i < (int) n_fds; i++) { | |
118 | int nfd; | |
119 | ||
120 | /* Already at right index? */ | |
121 | if (fds[i] == i+3) | |
122 | continue; | |
123 | ||
124 | if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0) | |
125 | return -errno; | |
126 | ||
03e334a1 | 127 | safe_close(fds[i]); |
034c6ed7 LP |
128 | fds[i] = nfd; |
129 | ||
130 | /* Hmm, the fd we wanted isn't free? Then | |
ee33e53a | 131 | * let's remember that and try again from here */ |
034c6ed7 LP |
132 | if (nfd != i+3 && restart_from < 0) |
133 | restart_from = i; | |
134 | } | |
135 | ||
136 | if (restart_from < 0) | |
137 | break; | |
138 | ||
139 | start = restart_from; | |
140 | } | |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
c2748801 | 145 | static int flags_fds(const int fds[], unsigned n_fds, bool nonblock) { |
47a71eed | 146 | unsigned i; |
e2c76839 | 147 | int r; |
47a71eed LP |
148 | |
149 | if (n_fds <= 0) | |
150 | return 0; | |
151 | ||
152 | assert(fds); | |
153 | ||
451a074f | 154 | /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags */ |
47a71eed LP |
155 | |
156 | for (i = 0; i < n_fds; i++) { | |
47a71eed | 157 | |
e2c76839 LP |
158 | if ((r = fd_nonblock(fds[i], nonblock)) < 0) |
159 | return r; | |
47a71eed | 160 | |
451a074f LP |
161 | /* We unconditionally drop FD_CLOEXEC from the fds, |
162 | * since after all we want to pass these fds to our | |
163 | * children */ | |
47a71eed | 164 | |
e2c76839 LP |
165 | if ((r = fd_cloexec(fds[i], false)) < 0) |
166 | return r; | |
47a71eed LP |
167 | } |
168 | ||
169 | return 0; | |
170 | } | |
171 | ||
44a6b1b6 | 172 | _pure_ static const char *tty_path(const ExecContext *context) { |
80876c20 LP |
173 | assert(context); |
174 | ||
175 | if (context->tty_path) | |
176 | return context->tty_path; | |
177 | ||
178 | return "/dev/console"; | |
179 | } | |
180 | ||
9588bc32 | 181 | static void exec_context_tty_reset(const ExecContext *context) { |
6ea832a2 LP |
182 | assert(context); |
183 | ||
184 | if (context->tty_vhangup) | |
185 | terminal_vhangup(tty_path(context)); | |
186 | ||
187 | if (context->tty_reset) | |
188 | reset_terminal(tty_path(context)); | |
189 | ||
190 | if (context->tty_vt_disallocate && context->tty_path) | |
191 | vt_disallocate(context->tty_path); | |
192 | } | |
193 | ||
3a1286b6 MS |
194 | static bool is_terminal_output(ExecOutput o) { |
195 | return | |
196 | o == EXEC_OUTPUT_TTY || | |
197 | o == EXEC_OUTPUT_SYSLOG_AND_CONSOLE || | |
198 | o == EXEC_OUTPUT_KMSG_AND_CONSOLE || | |
199 | o == EXEC_OUTPUT_JOURNAL_AND_CONSOLE; | |
200 | } | |
201 | ||
80876c20 LP |
202 | static int open_null_as(int flags, int nfd) { |
203 | int fd, r; | |
071830ff | 204 | |
80876c20 | 205 | assert(nfd >= 0); |
071830ff | 206 | |
613b411c LP |
207 | fd = open("/dev/null", flags|O_NOCTTY); |
208 | if (fd < 0) | |
071830ff LP |
209 | return -errno; |
210 | ||
80876c20 LP |
211 | if (fd != nfd) { |
212 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
03e334a1 | 213 | safe_close(fd); |
80876c20 LP |
214 | } else |
215 | r = nfd; | |
071830ff | 216 | |
80876c20 | 217 | return r; |
071830ff LP |
218 | } |
219 | ||
524daa8c | 220 | static int connect_journal_socket(int fd, uid_t uid, gid_t gid) { |
b92bea5d ZJS |
221 | union sockaddr_union sa = { |
222 | .un.sun_family = AF_UNIX, | |
223 | .un.sun_path = "/run/systemd/journal/stdout", | |
224 | }; | |
524daa8c ZJS |
225 | uid_t olduid = UID_INVALID; |
226 | gid_t oldgid = GID_INVALID; | |
227 | int r; | |
228 | ||
229 | if (gid != GID_INVALID) { | |
230 | oldgid = getgid(); | |
231 | ||
232 | r = setegid(gid); | |
233 | if (r < 0) | |
234 | return -errno; | |
235 | } | |
236 | ||
237 | if (uid != UID_INVALID) { | |
238 | olduid = getuid(); | |
239 | ||
240 | r = seteuid(uid); | |
241 | if (r < 0) { | |
242 | r = -errno; | |
243 | goto restore_gid; | |
244 | } | |
245 | } | |
246 | ||
247 | r = connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)); | |
248 | if (r < 0) | |
249 | r = -errno; | |
250 | ||
251 | /* If we fail to restore the uid or gid, things will likely | |
252 | fail later on. This should only happen if an LSM interferes. */ | |
253 | ||
254 | if (uid != UID_INVALID) | |
255 | (void) seteuid(olduid); | |
256 | ||
257 | restore_gid: | |
258 | if (gid != GID_INVALID) | |
259 | (void) setegid(oldgid); | |
260 | ||
261 | return r; | |
262 | } | |
263 | ||
264 | static int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, const char *unit_id, int nfd, uid_t uid, gid_t gid) { | |
265 | int fd, r; | |
071830ff LP |
266 | |
267 | assert(context); | |
80876c20 LP |
268 | assert(output < _EXEC_OUTPUT_MAX); |
269 | assert(ident); | |
270 | assert(nfd >= 0); | |
071830ff | 271 | |
54fe0cdb LP |
272 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
273 | if (fd < 0) | |
80876c20 | 274 | return -errno; |
071830ff | 275 | |
524daa8c ZJS |
276 | r = connect_journal_socket(fd, uid, gid); |
277 | if (r < 0) | |
278 | return r; | |
071830ff | 279 | |
80876c20 | 280 | if (shutdown(fd, SHUT_RD) < 0) { |
03e334a1 | 281 | safe_close(fd); |
80876c20 LP |
282 | return -errno; |
283 | } | |
071830ff | 284 | |
531dca78 LP |
285 | fd_inc_sndbuf(fd, SNDBUF_SIZE); |
286 | ||
80876c20 | 287 | dprintf(fd, |
62bca2c6 | 288 | "%s\n" |
80876c20 LP |
289 | "%s\n" |
290 | "%i\n" | |
54fe0cdb LP |
291 | "%i\n" |
292 | "%i\n" | |
293 | "%i\n" | |
4f4a1dbf | 294 | "%i\n", |
4f4a1dbf | 295 | context->syslog_identifier ? context->syslog_identifier : ident, |
62bca2c6 | 296 | unit_id, |
54fe0cdb LP |
297 | context->syslog_priority, |
298 | !!context->syslog_level_prefix, | |
299 | output == EXEC_OUTPUT_SYSLOG || output == EXEC_OUTPUT_SYSLOG_AND_CONSOLE, | |
300 | output == EXEC_OUTPUT_KMSG || output == EXEC_OUTPUT_KMSG_AND_CONSOLE, | |
3a1286b6 | 301 | is_terminal_output(output)); |
80876c20 LP |
302 | |
303 | if (fd != nfd) { | |
304 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
03e334a1 | 305 | safe_close(fd); |
80876c20 LP |
306 | } else |
307 | r = nfd; | |
071830ff | 308 | |
80876c20 LP |
309 | return r; |
310 | } | |
311 | static int open_terminal_as(const char *path, mode_t mode, int nfd) { | |
312 | int fd, r; | |
071830ff | 313 | |
80876c20 LP |
314 | assert(path); |
315 | assert(nfd >= 0); | |
071830ff | 316 | |
80876c20 LP |
317 | if ((fd = open_terminal(path, mode | O_NOCTTY)) < 0) |
318 | return fd; | |
071830ff | 319 | |
80876c20 LP |
320 | if (fd != nfd) { |
321 | r = dup2(fd, nfd) < 0 ? -errno : nfd; | |
03e334a1 | 322 | safe_close(fd); |
80876c20 LP |
323 | } else |
324 | r = nfd; | |
071830ff | 325 | |
80876c20 LP |
326 | return r; |
327 | } | |
071830ff | 328 | |
80876c20 LP |
329 | static bool is_terminal_input(ExecInput i) { |
330 | return | |
331 | i == EXEC_INPUT_TTY || | |
332 | i == EXEC_INPUT_TTY_FORCE || | |
333 | i == EXEC_INPUT_TTY_FAIL; | |
334 | } | |
071830ff | 335 | |
1e3ad081 LP |
336 | static int fixup_input(ExecInput std_input, int socket_fd, bool apply_tty_stdin) { |
337 | ||
338 | if (is_terminal_input(std_input) && !apply_tty_stdin) | |
339 | return EXEC_INPUT_NULL; | |
071830ff | 340 | |
03fd9c49 | 341 | if (std_input == EXEC_INPUT_SOCKET && socket_fd < 0) |
4f2d528d LP |
342 | return EXEC_INPUT_NULL; |
343 | ||
03fd9c49 | 344 | return std_input; |
4f2d528d LP |
345 | } |
346 | ||
03fd9c49 | 347 | static int fixup_output(ExecOutput std_output, int socket_fd) { |
4f2d528d | 348 | |
03fd9c49 | 349 | if (std_output == EXEC_OUTPUT_SOCKET && socket_fd < 0) |
4f2d528d LP |
350 | return EXEC_OUTPUT_INHERIT; |
351 | ||
03fd9c49 | 352 | return std_output; |
4f2d528d LP |
353 | } |
354 | ||
1e3ad081 | 355 | static int setup_input(const ExecContext *context, int socket_fd, bool apply_tty_stdin) { |
4f2d528d LP |
356 | ExecInput i; |
357 | ||
358 | assert(context); | |
359 | ||
1e3ad081 | 360 | i = fixup_input(context->std_input, socket_fd, apply_tty_stdin); |
4f2d528d LP |
361 | |
362 | switch (i) { | |
071830ff | 363 | |
80876c20 LP |
364 | case EXEC_INPUT_NULL: |
365 | return open_null_as(O_RDONLY, STDIN_FILENO); | |
366 | ||
367 | case EXEC_INPUT_TTY: | |
368 | case EXEC_INPUT_TTY_FORCE: | |
369 | case EXEC_INPUT_TTY_FAIL: { | |
370 | int fd, r; | |
071830ff | 371 | |
970edce6 ZJS |
372 | fd = acquire_terminal(tty_path(context), |
373 | i == EXEC_INPUT_TTY_FAIL, | |
374 | i == EXEC_INPUT_TTY_FORCE, | |
375 | false, | |
3a43da28 | 376 | USEC_INFINITY); |
970edce6 | 377 | if (fd < 0) |
80876c20 LP |
378 | return fd; |
379 | ||
380 | if (fd != STDIN_FILENO) { | |
381 | r = dup2(fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO; | |
03e334a1 | 382 | safe_close(fd); |
80876c20 LP |
383 | } else |
384 | r = STDIN_FILENO; | |
385 | ||
386 | return r; | |
387 | } | |
388 | ||
4f2d528d LP |
389 | case EXEC_INPUT_SOCKET: |
390 | return dup2(socket_fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO; | |
391 | ||
80876c20 LP |
392 | default: |
393 | assert_not_reached("Unknown input type"); | |
394 | } | |
395 | } | |
396 | ||
f2341e0a | 397 | static int setup_output(Unit *unit, const ExecContext *context, int fileno, int socket_fd, const char *ident, bool apply_tty_stdin, uid_t uid, gid_t gid) { |
4f2d528d LP |
398 | ExecOutput o; |
399 | ExecInput i; | |
47c1d80d | 400 | int r; |
4f2d528d | 401 | |
f2341e0a | 402 | assert(unit); |
80876c20 LP |
403 | assert(context); |
404 | assert(ident); | |
405 | ||
1e3ad081 | 406 | i = fixup_input(context->std_input, socket_fd, apply_tty_stdin); |
03fd9c49 | 407 | o = fixup_output(context->std_output, socket_fd); |
4f2d528d | 408 | |
eb17e935 MS |
409 | if (fileno == STDERR_FILENO) { |
410 | ExecOutput e; | |
411 | e = fixup_output(context->std_error, socket_fd); | |
80876c20 | 412 | |
eb17e935 MS |
413 | /* This expects the input and output are already set up */ |
414 | ||
415 | /* Don't change the stderr file descriptor if we inherit all | |
416 | * the way and are not on a tty */ | |
417 | if (e == EXEC_OUTPUT_INHERIT && | |
418 | o == EXEC_OUTPUT_INHERIT && | |
419 | i == EXEC_INPUT_NULL && | |
420 | !is_terminal_input(context->std_input) && | |
421 | getppid () != 1) | |
422 | return fileno; | |
423 | ||
424 | /* Duplicate from stdout if possible */ | |
425 | if (e == o || e == EXEC_OUTPUT_INHERIT) | |
426 | return dup2(STDOUT_FILENO, fileno) < 0 ? -errno : fileno; | |
071830ff | 427 | |
eb17e935 | 428 | o = e; |
80876c20 | 429 | |
eb17e935 | 430 | } else if (o == EXEC_OUTPUT_INHERIT) { |
21d21ea4 LP |
431 | /* If input got downgraded, inherit the original value */ |
432 | if (i == EXEC_INPUT_NULL && is_terminal_input(context->std_input)) | |
eb17e935 | 433 | return open_terminal_as(tty_path(context), O_WRONLY, fileno); |
21d21ea4 | 434 | |
acb591e4 | 435 | /* If the input is connected to anything that's not a /dev/null, inherit that... */ |
ff876e28 | 436 | if (i != EXEC_INPUT_NULL) |
eb17e935 | 437 | return dup2(STDIN_FILENO, fileno) < 0 ? -errno : fileno; |
071830ff | 438 | |
acb591e4 LP |
439 | /* If we are not started from PID 1 we just inherit STDOUT from our parent process. */ |
440 | if (getppid() != 1) | |
eb17e935 | 441 | return fileno; |
94f04347 | 442 | |
eb17e935 MS |
443 | /* We need to open /dev/null here anew, to get the right access mode. */ |
444 | return open_null_as(O_WRONLY, fileno); | |
071830ff | 445 | } |
94f04347 | 446 | |
eb17e935 | 447 | switch (o) { |
80876c20 LP |
448 | |
449 | case EXEC_OUTPUT_NULL: | |
eb17e935 | 450 | return open_null_as(O_WRONLY, fileno); |
80876c20 LP |
451 | |
452 | case EXEC_OUTPUT_TTY: | |
4f2d528d | 453 | if (is_terminal_input(i)) |
eb17e935 | 454 | return dup2(STDIN_FILENO, fileno) < 0 ? -errno : fileno; |
80876c20 LP |
455 | |
456 | /* We don't reset the terminal if this is just about output */ | |
eb17e935 | 457 | return open_terminal_as(tty_path(context), O_WRONLY, fileno); |
80876c20 LP |
458 | |
459 | case EXEC_OUTPUT_SYSLOG: | |
28dbc1e8 | 460 | case EXEC_OUTPUT_SYSLOG_AND_CONSOLE: |
9a6bca7a | 461 | case EXEC_OUTPUT_KMSG: |
28dbc1e8 | 462 | case EXEC_OUTPUT_KMSG_AND_CONSOLE: |
706343f4 LP |
463 | case EXEC_OUTPUT_JOURNAL: |
464 | case EXEC_OUTPUT_JOURNAL_AND_CONSOLE: | |
f2341e0a | 465 | r = connect_logger_as(context, o, ident, unit->id, fileno, uid, gid); |
47c1d80d | 466 | if (r < 0) { |
f2341e0a | 467 | log_unit_error_errno(unit, r, "Failed to connect %s to the journal socket, ignoring: %m", fileno == STDOUT_FILENO ? "stdout" : "stderr"); |
eb17e935 | 468 | r = open_null_as(O_WRONLY, fileno); |
47c1d80d MS |
469 | } |
470 | return r; | |
4f2d528d LP |
471 | |
472 | case EXEC_OUTPUT_SOCKET: | |
473 | assert(socket_fd >= 0); | |
eb17e935 | 474 | return dup2(socket_fd, fileno) < 0 ? -errno : fileno; |
94f04347 LP |
475 | |
476 | default: | |
80876c20 | 477 | assert_not_reached("Unknown error type"); |
94f04347 | 478 | } |
071830ff LP |
479 | } |
480 | ||
02a51aba LP |
481 | static int chown_terminal(int fd, uid_t uid) { |
482 | struct stat st; | |
483 | ||
484 | assert(fd >= 0); | |
02a51aba LP |
485 | |
486 | /* This might fail. What matters are the results. */ | |
bab45044 LP |
487 | (void) fchown(fd, uid, -1); |
488 | (void) fchmod(fd, TTY_MODE); | |
02a51aba LP |
489 | |
490 | if (fstat(fd, &st) < 0) | |
491 | return -errno; | |
492 | ||
d8b4e2e9 | 493 | if (st.st_uid != uid || (st.st_mode & 0777) != TTY_MODE) |
02a51aba LP |
494 | return -EPERM; |
495 | ||
496 | return 0; | |
497 | } | |
498 | ||
af6da548 | 499 | static int setup_confirm_stdio(int *_saved_stdin, |
80876c20 LP |
500 | int *_saved_stdout) { |
501 | int fd = -1, saved_stdin, saved_stdout = -1, r; | |
502 | ||
80876c20 LP |
503 | assert(_saved_stdin); |
504 | assert(_saved_stdout); | |
505 | ||
af6da548 LP |
506 | saved_stdin = fcntl(STDIN_FILENO, F_DUPFD, 3); |
507 | if (saved_stdin < 0) | |
508 | return -errno; | |
80876c20 | 509 | |
af6da548 LP |
510 | saved_stdout = fcntl(STDOUT_FILENO, F_DUPFD, 3); |
511 | if (saved_stdout < 0) { | |
512 | r = errno; | |
80876c20 LP |
513 | goto fail; |
514 | } | |
515 | ||
af6da548 LP |
516 | fd = acquire_terminal( |
517 | "/dev/console", | |
518 | false, | |
519 | false, | |
520 | false, | |
521 | DEFAULT_CONFIRM_USEC); | |
522 | if (fd < 0) { | |
523 | r = fd; | |
80876c20 LP |
524 | goto fail; |
525 | } | |
526 | ||
af6da548 LP |
527 | r = chown_terminal(fd, getuid()); |
528 | if (r < 0) | |
02a51aba | 529 | goto fail; |
02a51aba | 530 | |
80876c20 | 531 | if (dup2(fd, STDIN_FILENO) < 0) { |
af6da548 | 532 | r = -errno; |
80876c20 LP |
533 | goto fail; |
534 | } | |
535 | ||
536 | if (dup2(fd, STDOUT_FILENO) < 0) { | |
af6da548 | 537 | r = -errno; |
80876c20 LP |
538 | goto fail; |
539 | } | |
540 | ||
541 | if (fd >= 2) | |
03e334a1 | 542 | safe_close(fd); |
80876c20 LP |
543 | |
544 | *_saved_stdin = saved_stdin; | |
545 | *_saved_stdout = saved_stdout; | |
546 | ||
547 | return 0; | |
548 | ||
549 | fail: | |
03e334a1 LP |
550 | safe_close(saved_stdout); |
551 | safe_close(saved_stdin); | |
552 | safe_close(fd); | |
80876c20 LP |
553 | |
554 | return r; | |
555 | } | |
556 | ||
44b601bc | 557 | _printf_(1, 2) static int write_confirm_message(const char *format, ...) { |
03e334a1 | 558 | _cleanup_close_ int fd = -1; |
af6da548 | 559 | va_list ap; |
80876c20 | 560 | |
af6da548 | 561 | assert(format); |
80876c20 | 562 | |
af6da548 LP |
563 | fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); |
564 | if (fd < 0) | |
565 | return fd; | |
80876c20 | 566 | |
af6da548 LP |
567 | va_start(ap, format); |
568 | vdprintf(fd, format, ap); | |
569 | va_end(ap); | |
80876c20 | 570 | |
af6da548 LP |
571 | return 0; |
572 | } | |
80876c20 | 573 | |
af6da548 LP |
574 | static int restore_confirm_stdio(int *saved_stdin, |
575 | int *saved_stdout) { | |
80876c20 | 576 | |
af6da548 | 577 | int r = 0; |
80876c20 | 578 | |
af6da548 LP |
579 | assert(saved_stdin); |
580 | assert(saved_stdout); | |
581 | ||
582 | release_terminal(); | |
583 | ||
584 | if (*saved_stdin >= 0) | |
80876c20 | 585 | if (dup2(*saved_stdin, STDIN_FILENO) < 0) |
af6da548 | 586 | r = -errno; |
80876c20 | 587 | |
af6da548 | 588 | if (*saved_stdout >= 0) |
80876c20 | 589 | if (dup2(*saved_stdout, STDOUT_FILENO) < 0) |
af6da548 | 590 | r = -errno; |
80876c20 | 591 | |
03e334a1 LP |
592 | safe_close(*saved_stdin); |
593 | safe_close(*saved_stdout); | |
af6da548 LP |
594 | |
595 | return r; | |
596 | } | |
597 | ||
598 | static int ask_for_confirmation(char *response, char **argv) { | |
599 | int saved_stdout = -1, saved_stdin = -1, r; | |
e1d75803 | 600 | _cleanup_free_ char *line = NULL; |
af6da548 LP |
601 | |
602 | r = setup_confirm_stdio(&saved_stdin, &saved_stdout); | |
603 | if (r < 0) | |
604 | return r; | |
605 | ||
606 | line = exec_command_line(argv); | |
607 | if (!line) | |
608 | return -ENOMEM; | |
609 | ||
418b9be5 | 610 | r = ask_char(response, "yns", "Execute %s? [Yes, No, Skip] ", line); |
af6da548 LP |
611 | |
612 | restore_confirm_stdio(&saved_stdin, &saved_stdout); | |
613 | ||
614 | return r; | |
80876c20 LP |
615 | } |
616 | ||
81a2b7ce LP |
617 | static int enforce_groups(const ExecContext *context, const char *username, gid_t gid) { |
618 | bool keep_groups = false; | |
619 | int r; | |
620 | ||
621 | assert(context); | |
622 | ||
35b8ca3a | 623 | /* Lookup and set GID and supplementary group list. Here too |
81a2b7ce LP |
624 | * we avoid NSS lookups for gid=0. */ |
625 | ||
626 | if (context->group || username) { | |
627 | ||
4b67834e LP |
628 | if (context->group) { |
629 | const char *g = context->group; | |
630 | ||
631 | if ((r = get_group_creds(&g, &gid)) < 0) | |
81a2b7ce | 632 | return r; |
4b67834e | 633 | } |
81a2b7ce LP |
634 | |
635 | /* First step, initialize groups from /etc/groups */ | |
636 | if (username && gid != 0) { | |
637 | if (initgroups(username, gid) < 0) | |
638 | return -errno; | |
639 | ||
640 | keep_groups = true; | |
641 | } | |
642 | ||
643 | /* Second step, set our gids */ | |
644 | if (setresgid(gid, gid, gid) < 0) | |
645 | return -errno; | |
646 | } | |
647 | ||
648 | if (context->supplementary_groups) { | |
649 | int ngroups_max, k; | |
650 | gid_t *gids; | |
651 | char **i; | |
652 | ||
653 | /* Final step, initialize any manually set supplementary groups */ | |
da19d5c1 | 654 | assert_se((ngroups_max = (int) sysconf(_SC_NGROUPS_MAX)) > 0); |
81a2b7ce LP |
655 | |
656 | if (!(gids = new(gid_t, ngroups_max))) | |
657 | return -ENOMEM; | |
658 | ||
659 | if (keep_groups) { | |
660 | if ((k = getgroups(ngroups_max, gids)) < 0) { | |
661 | free(gids); | |
662 | return -errno; | |
663 | } | |
664 | } else | |
665 | k = 0; | |
666 | ||
667 | STRV_FOREACH(i, context->supplementary_groups) { | |
4b67834e | 668 | const char *g; |
81a2b7ce LP |
669 | |
670 | if (k >= ngroups_max) { | |
671 | free(gids); | |
672 | return -E2BIG; | |
673 | } | |
674 | ||
4b67834e LP |
675 | g = *i; |
676 | r = get_group_creds(&g, gids+k); | |
677 | if (r < 0) { | |
81a2b7ce LP |
678 | free(gids); |
679 | return r; | |
680 | } | |
681 | ||
682 | k++; | |
683 | } | |
684 | ||
685 | if (setgroups(k, gids) < 0) { | |
686 | free(gids); | |
687 | return -errno; | |
688 | } | |
689 | ||
690 | free(gids); | |
691 | } | |
692 | ||
693 | return 0; | |
694 | } | |
695 | ||
696 | static int enforce_user(const ExecContext *context, uid_t uid) { | |
81a2b7ce LP |
697 | assert(context); |
698 | ||
699 | /* Sets (but doesn't lookup) the uid and make sure we keep the | |
700 | * capabilities while doing so. */ | |
701 | ||
702 | if (context->capabilities) { | |
5ce70e5b | 703 | _cleanup_cap_free_ cap_t d = NULL; |
81a2b7ce LP |
704 | static const cap_value_t bits[] = { |
705 | CAP_SETUID, /* Necessary so that we can run setresuid() below */ | |
706 | CAP_SETPCAP /* Necessary so that we can set PR_SET_SECUREBITS later on */ | |
707 | }; | |
708 | ||
709 | /* First step: If we need to keep capabilities but | |
710 | * drop privileges we need to make sure we keep our | |
cbb21cca | 711 | * caps, while we drop privileges. */ |
693ced48 | 712 | if (uid != 0) { |
cbb21cca | 713 | int sb = context->secure_bits | 1<<SECURE_KEEP_CAPS; |
693ced48 LP |
714 | |
715 | if (prctl(PR_GET_SECUREBITS) != sb) | |
716 | if (prctl(PR_SET_SECUREBITS, sb) < 0) | |
717 | return -errno; | |
718 | } | |
81a2b7ce | 719 | |
35b8ca3a | 720 | /* Second step: set the capabilities. This will reduce |
81a2b7ce LP |
721 | * the capabilities to the minimum we need. */ |
722 | ||
5ce70e5b ZJS |
723 | d = cap_dup(context->capabilities); |
724 | if (!d) | |
81a2b7ce LP |
725 | return -errno; |
726 | ||
727 | if (cap_set_flag(d, CAP_EFFECTIVE, ELEMENTSOF(bits), bits, CAP_SET) < 0 || | |
5ce70e5b ZJS |
728 | cap_set_flag(d, CAP_PERMITTED, ELEMENTSOF(bits), bits, CAP_SET) < 0) |
729 | return -errno; | |
81a2b7ce | 730 | |
5ce70e5b ZJS |
731 | if (cap_set_proc(d) < 0) |
732 | return -errno; | |
81a2b7ce LP |
733 | } |
734 | ||
735 | /* Third step: actually set the uids */ | |
736 | if (setresuid(uid, uid, uid) < 0) | |
737 | return -errno; | |
738 | ||
739 | /* At this point we should have all necessary capabilities but | |
740 | are otherwise a normal user. However, the caps might got | |
741 | corrupted due to the setresuid() so we need clean them up | |
742 | later. This is done outside of this call. */ | |
743 | ||
744 | return 0; | |
745 | } | |
746 | ||
5b6319dc LP |
747 | #ifdef HAVE_PAM |
748 | ||
749 | static int null_conv( | |
750 | int num_msg, | |
751 | const struct pam_message **msg, | |
752 | struct pam_response **resp, | |
753 | void *appdata_ptr) { | |
754 | ||
755 | /* We don't support conversations */ | |
756 | ||
757 | return PAM_CONV_ERR; | |
758 | } | |
759 | ||
760 | static int setup_pam( | |
761 | const char *name, | |
762 | const char *user, | |
940c5210 | 763 | uid_t uid, |
5b6319dc LP |
764 | const char *tty, |
765 | char ***pam_env, | |
766 | int fds[], unsigned n_fds) { | |
767 | ||
768 | static const struct pam_conv conv = { | |
769 | .conv = null_conv, | |
770 | .appdata_ptr = NULL | |
771 | }; | |
772 | ||
773 | pam_handle_t *handle = NULL; | |
d6e5f3ad | 774 | sigset_t old_ss; |
5b6319dc | 775 | int pam_code = PAM_SUCCESS; |
9ba35398 | 776 | int err; |
5b6319dc LP |
777 | char **e = NULL; |
778 | bool close_session = false; | |
779 | pid_t pam_pid = 0, parent_pid; | |
970edce6 | 780 | int flags = 0; |
5b6319dc LP |
781 | |
782 | assert(name); | |
783 | assert(user); | |
784 | assert(pam_env); | |
785 | ||
786 | /* We set up PAM in the parent process, then fork. The child | |
35b8ca3a | 787 | * will then stay around until killed via PR_GET_PDEATHSIG or |
5b6319dc LP |
788 | * systemd via the cgroup logic. It will then remove the PAM |
789 | * session again. The parent process will exec() the actual | |
790 | * daemon. We do things this way to ensure that the main PID | |
791 | * of the daemon is the one we initially fork()ed. */ | |
792 | ||
553d2243 | 793 | if (log_get_max_level() < LOG_DEBUG) |
970edce6 ZJS |
794 | flags |= PAM_SILENT; |
795 | ||
f546241b ZJS |
796 | pam_code = pam_start(name, user, &conv, &handle); |
797 | if (pam_code != PAM_SUCCESS) { | |
5b6319dc LP |
798 | handle = NULL; |
799 | goto fail; | |
800 | } | |
801 | ||
f546241b ZJS |
802 | if (tty) { |
803 | pam_code = pam_set_item(handle, PAM_TTY, tty); | |
804 | if (pam_code != PAM_SUCCESS) | |
5b6319dc | 805 | goto fail; |
f546241b | 806 | } |
5b6319dc | 807 | |
970edce6 | 808 | pam_code = pam_acct_mgmt(handle, flags); |
f546241b | 809 | if (pam_code != PAM_SUCCESS) |
5b6319dc LP |
810 | goto fail; |
811 | ||
970edce6 | 812 | pam_code = pam_open_session(handle, flags); |
f546241b | 813 | if (pam_code != PAM_SUCCESS) |
5b6319dc LP |
814 | goto fail; |
815 | ||
816 | close_session = true; | |
817 | ||
f546241b ZJS |
818 | e = pam_getenvlist(handle); |
819 | if (!e) { | |
5b6319dc LP |
820 | pam_code = PAM_BUF_ERR; |
821 | goto fail; | |
822 | } | |
823 | ||
824 | /* Block SIGTERM, so that we know that it won't get lost in | |
825 | * the child */ | |
ce30c8dc | 826 | |
72c0a2c2 | 827 | assert_se(sigprocmask_many(SIG_BLOCK, &old_ss, SIGTERM, -1) >= 0); |
5b6319dc LP |
828 | |
829 | parent_pid = getpid(); | |
830 | ||
f546241b ZJS |
831 | pam_pid = fork(); |
832 | if (pam_pid < 0) | |
5b6319dc LP |
833 | goto fail; |
834 | ||
835 | if (pam_pid == 0) { | |
836 | int sig; | |
837 | int r = EXIT_PAM; | |
838 | ||
839 | /* The child's job is to reset the PAM session on | |
840 | * termination */ | |
841 | ||
842 | /* This string must fit in 10 chars (i.e. the length | |
5d6b1584 LP |
843 | * of "/sbin/init"), to look pretty in /bin/ps */ |
844 | rename_process("(sd-pam)"); | |
5b6319dc LP |
845 | |
846 | /* Make sure we don't keep open the passed fds in this | |
847 | child. We assume that otherwise only those fds are | |
848 | open here that have been opened by PAM. */ | |
849 | close_many(fds, n_fds); | |
850 | ||
940c5210 AK |
851 | /* Drop privileges - we don't need any to pam_close_session |
852 | * and this will make PR_SET_PDEATHSIG work in most cases. | |
853 | * If this fails, ignore the error - but expect sd-pam threads | |
854 | * to fail to exit normally */ | |
855 | if (setresuid(uid, uid, uid) < 0) | |
da927ba9 | 856 | log_error_errno(r, "Error: Failed to setresuid() in sd-pam: %m"); |
940c5210 | 857 | |
ce30c8dc LP |
858 | (void) ignore_signals(SIGPIPE, -1); |
859 | ||
940c5210 AK |
860 | /* Wait until our parent died. This will only work if |
861 | * the above setresuid() succeeds, otherwise the kernel | |
862 | * will not allow unprivileged parents kill their privileged | |
863 | * children this way. We rely on the control groups kill logic | |
5b6319dc LP |
864 | * to do the rest for us. */ |
865 | if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0) | |
866 | goto child_finish; | |
867 | ||
868 | /* Check if our parent process might already have | |
869 | * died? */ | |
870 | if (getppid() == parent_pid) { | |
d6e5f3ad DM |
871 | sigset_t ss; |
872 | ||
873 | assert_se(sigemptyset(&ss) >= 0); | |
874 | assert_se(sigaddset(&ss, SIGTERM) >= 0); | |
875 | ||
3dead8d9 LP |
876 | for (;;) { |
877 | if (sigwait(&ss, &sig) < 0) { | |
878 | if (errno == EINTR) | |
879 | continue; | |
880 | ||
881 | goto child_finish; | |
882 | } | |
5b6319dc | 883 | |
3dead8d9 LP |
884 | assert(sig == SIGTERM); |
885 | break; | |
886 | } | |
5b6319dc LP |
887 | } |
888 | ||
3dead8d9 | 889 | /* If our parent died we'll end the session */ |
f546241b | 890 | if (getppid() != parent_pid) { |
970edce6 | 891 | pam_code = pam_close_session(handle, flags); |
f546241b | 892 | if (pam_code != PAM_SUCCESS) |
5b6319dc | 893 | goto child_finish; |
f546241b | 894 | } |
5b6319dc LP |
895 | |
896 | r = 0; | |
897 | ||
898 | child_finish: | |
970edce6 | 899 | pam_end(handle, pam_code | flags); |
5b6319dc LP |
900 | _exit(r); |
901 | } | |
902 | ||
903 | /* If the child was forked off successfully it will do all the | |
904 | * cleanups, so forget about the handle here. */ | |
905 | handle = NULL; | |
906 | ||
3b8bddde | 907 | /* Unblock SIGTERM again in the parent */ |
72c0a2c2 | 908 | assert_se(sigprocmask(SIG_SETMASK, &old_ss, NULL) >= 0); |
5b6319dc LP |
909 | |
910 | /* We close the log explicitly here, since the PAM modules | |
911 | * might have opened it, but we don't want this fd around. */ | |
912 | closelog(); | |
913 | ||
aa87e624 LP |
914 | *pam_env = e; |
915 | e = NULL; | |
916 | ||
5b6319dc LP |
917 | return 0; |
918 | ||
919 | fail: | |
970edce6 ZJS |
920 | if (pam_code != PAM_SUCCESS) { |
921 | log_error("PAM failed: %s", pam_strerror(handle, pam_code)); | |
9ba35398 | 922 | err = -EPERM; /* PAM errors do not map to errno */ |
970edce6 | 923 | } else { |
56f64d95 | 924 | log_error_errno(errno, "PAM failed: %m"); |
9ba35398 | 925 | err = -errno; |
970edce6 | 926 | } |
9ba35398 | 927 | |
5b6319dc LP |
928 | if (handle) { |
929 | if (close_session) | |
970edce6 | 930 | pam_code = pam_close_session(handle, flags); |
5b6319dc | 931 | |
970edce6 | 932 | pam_end(handle, pam_code | flags); |
5b6319dc LP |
933 | } |
934 | ||
935 | strv_free(e); | |
936 | ||
937 | closelog(); | |
938 | ||
430c18ed | 939 | if (pam_pid > 1) { |
5b6319dc | 940 | kill(pam_pid, SIGTERM); |
430c18ed LP |
941 | kill(pam_pid, SIGCONT); |
942 | } | |
5b6319dc | 943 | |
9ba35398 | 944 | return err; |
5b6319dc LP |
945 | } |
946 | #endif | |
947 | ||
5d6b1584 LP |
948 | static void rename_process_from_path(const char *path) { |
949 | char process_name[11]; | |
950 | const char *p; | |
951 | size_t l; | |
952 | ||
953 | /* This resulting string must fit in 10 chars (i.e. the length | |
954 | * of "/sbin/init") to look pretty in /bin/ps */ | |
955 | ||
2b6bf07d | 956 | p = basename(path); |
5d6b1584 LP |
957 | if (isempty(p)) { |
958 | rename_process("(...)"); | |
959 | return; | |
960 | } | |
961 | ||
962 | l = strlen(p); | |
963 | if (l > 8) { | |
964 | /* The end of the process name is usually more | |
965 | * interesting, since the first bit might just be | |
966 | * "systemd-" */ | |
967 | p = p + l - 8; | |
968 | l = 8; | |
969 | } | |
970 | ||
971 | process_name[0] = '('; | |
972 | memcpy(process_name+1, p, l); | |
973 | process_name[1+l] = ')'; | |
974 | process_name[1+l+1] = 0; | |
975 | ||
976 | rename_process(process_name); | |
977 | } | |
978 | ||
c0467cf3 | 979 | #ifdef HAVE_SECCOMP |
17df7223 | 980 | |
822a5960 | 981 | static int apply_seccomp(const ExecContext *c) { |
17df7223 LP |
982 | uint32_t negative_action, action; |
983 | scmp_filter_ctx *seccomp; | |
c0467cf3 RC |
984 | Iterator i; |
985 | void *id; | |
17df7223 | 986 | int r; |
8351ceae | 987 | |
c0467cf3 | 988 | assert(c); |
8351ceae | 989 | |
17df7223 LP |
990 | negative_action = c->syscall_errno == 0 ? SCMP_ACT_KILL : SCMP_ACT_ERRNO(c->syscall_errno); |
991 | ||
992 | seccomp = seccomp_init(c->syscall_whitelist ? negative_action : SCMP_ACT_ALLOW); | |
993 | if (!seccomp) | |
994 | return -ENOMEM; | |
8351ceae | 995 | |
e9642be2 LP |
996 | if (c->syscall_archs) { |
997 | ||
998 | SET_FOREACH(id, c->syscall_archs, i) { | |
999 | r = seccomp_arch_add(seccomp, PTR_TO_UINT32(id) - 1); | |
1000 | if (r == -EEXIST) | |
1001 | continue; | |
7c66bae2 LP |
1002 | if (r < 0) |
1003 | goto finish; | |
e9642be2 | 1004 | } |
e9642be2 | 1005 | |
7c66bae2 | 1006 | } else { |
e9642be2 | 1007 | r = seccomp_add_secondary_archs(seccomp); |
7c66bae2 LP |
1008 | if (r < 0) |
1009 | goto finish; | |
57183d11 | 1010 | } |
8351ceae | 1011 | |
57183d11 | 1012 | action = c->syscall_whitelist ? SCMP_ACT_ALLOW : negative_action; |
17df7223 LP |
1013 | SET_FOREACH(id, c->syscall_filter, i) { |
1014 | r = seccomp_rule_add(seccomp, action, PTR_TO_INT(id) - 1, 0); | |
7c66bae2 LP |
1015 | if (r < 0) |
1016 | goto finish; | |
c0467cf3 | 1017 | } |
8351ceae | 1018 | |
7c66bae2 LP |
1019 | r = seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_NNP, 0); |
1020 | if (r < 0) | |
1021 | goto finish; | |
1022 | ||
17df7223 | 1023 | r = seccomp_load(seccomp); |
7c66bae2 LP |
1024 | |
1025 | finish: | |
17df7223 | 1026 | seccomp_release(seccomp); |
4298d0b5 LP |
1027 | return r; |
1028 | } | |
1029 | ||
822a5960 | 1030 | static int apply_address_families(const ExecContext *c) { |
4298d0b5 LP |
1031 | scmp_filter_ctx *seccomp; |
1032 | Iterator i; | |
1033 | int r; | |
1034 | ||
1035 | assert(c); | |
1036 | ||
1037 | seccomp = seccomp_init(SCMP_ACT_ALLOW); | |
1038 | if (!seccomp) | |
1039 | return -ENOMEM; | |
1040 | ||
1041 | r = seccomp_add_secondary_archs(seccomp); | |
1042 | if (r < 0) | |
1043 | goto finish; | |
1044 | ||
1045 | if (c->address_families_whitelist) { | |
1046 | int af, first = 0, last = 0; | |
1047 | void *afp; | |
1048 | ||
1049 | /* If this is a whitelist, we first block the address | |
1050 | * families that are out of range and then everything | |
1051 | * that is not in the set. First, we find the lowest | |
1052 | * and highest address family in the set. */ | |
1053 | ||
1054 | SET_FOREACH(afp, c->address_families, i) { | |
1055 | af = PTR_TO_INT(afp); | |
17df7223 | 1056 | |
4298d0b5 LP |
1057 | if (af <= 0 || af >= af_max()) |
1058 | continue; | |
1059 | ||
1060 | if (first == 0 || af < first) | |
1061 | first = af; | |
1062 | ||
1063 | if (last == 0 || af > last) | |
1064 | last = af; | |
1065 | } | |
1066 | ||
1067 | assert((first == 0) == (last == 0)); | |
1068 | ||
1069 | if (first == 0) { | |
1070 | ||
1071 | /* No entries in the valid range, block everything */ | |
1072 | r = seccomp_rule_add( | |
1073 | seccomp, | |
1074 | SCMP_ACT_ERRNO(EPROTONOSUPPORT), | |
1075 | SCMP_SYS(socket), | |
1076 | 0); | |
1077 | if (r < 0) | |
1078 | goto finish; | |
1079 | ||
1080 | } else { | |
1081 | ||
1082 | /* Block everything below the first entry */ | |
1083 | r = seccomp_rule_add( | |
1084 | seccomp, | |
1085 | SCMP_ACT_ERRNO(EPROTONOSUPPORT), | |
1086 | SCMP_SYS(socket), | |
1087 | 1, | |
1088 | SCMP_A0(SCMP_CMP_LT, first)); | |
1089 | if (r < 0) | |
1090 | goto finish; | |
1091 | ||
1092 | /* Block everything above the last entry */ | |
1093 | r = seccomp_rule_add( | |
1094 | seccomp, | |
1095 | SCMP_ACT_ERRNO(EPROTONOSUPPORT), | |
1096 | SCMP_SYS(socket), | |
1097 | 1, | |
1098 | SCMP_A0(SCMP_CMP_GT, last)); | |
1099 | if (r < 0) | |
1100 | goto finish; | |
1101 | ||
1102 | /* Block everything between the first and last | |
1103 | * entry */ | |
1104 | for (af = 1; af < af_max(); af++) { | |
1105 | ||
1106 | if (set_contains(c->address_families, INT_TO_PTR(af))) | |
1107 | continue; | |
1108 | ||
1109 | r = seccomp_rule_add( | |
1110 | seccomp, | |
1111 | SCMP_ACT_ERRNO(EPROTONOSUPPORT), | |
1112 | SCMP_SYS(socket), | |
1113 | 1, | |
1114 | SCMP_A0(SCMP_CMP_EQ, af)); | |
1115 | if (r < 0) | |
1116 | goto finish; | |
1117 | } | |
1118 | } | |
1119 | ||
1120 | } else { | |
1121 | void *af; | |
1122 | ||
1123 | /* If this is a blacklist, then generate one rule for | |
1124 | * each address family that are then combined in OR | |
1125 | * checks. */ | |
1126 | ||
1127 | SET_FOREACH(af, c->address_families, i) { | |
1128 | ||
1129 | r = seccomp_rule_add( | |
1130 | seccomp, | |
1131 | SCMP_ACT_ERRNO(EPROTONOSUPPORT), | |
1132 | SCMP_SYS(socket), | |
1133 | 1, | |
1134 | SCMP_A0(SCMP_CMP_EQ, PTR_TO_INT(af))); | |
1135 | if (r < 0) | |
1136 | goto finish; | |
1137 | } | |
1138 | } | |
1139 | ||
1140 | r = seccomp_attr_set(seccomp, SCMP_FLTATR_CTL_NNP, 0); | |
1141 | if (r < 0) | |
1142 | goto finish; | |
1143 | ||
1144 | r = seccomp_load(seccomp); | |
1145 | ||
1146 | finish: | |
1147 | seccomp_release(seccomp); | |
17df7223 | 1148 | return r; |
8351ceae | 1149 | } |
4298d0b5 | 1150 | |
c0467cf3 | 1151 | #endif |
8351ceae | 1152 | |
31a7eb86 ZJS |
1153 | static void do_idle_pipe_dance(int idle_pipe[4]) { |
1154 | assert(idle_pipe); | |
1155 | ||
03e334a1 LP |
1156 | |
1157 | safe_close(idle_pipe[1]); | |
1158 | safe_close(idle_pipe[2]); | |
31a7eb86 ZJS |
1159 | |
1160 | if (idle_pipe[0] >= 0) { | |
1161 | int r; | |
1162 | ||
1163 | r = fd_wait_for_event(idle_pipe[0], POLLHUP, IDLE_TIMEOUT_USEC); | |
1164 | ||
1165 | if (idle_pipe[3] >= 0 && r == 0 /* timeout */) { | |
1166 | /* Signal systemd that we are bored and want to continue. */ | |
cd972d69 ZJS |
1167 | r = write(idle_pipe[3], "x", 1); |
1168 | if (r > 0) | |
1169 | /* Wait for systemd to react to the signal above. */ | |
1170 | fd_wait_for_event(idle_pipe[0], POLLHUP, IDLE_TIMEOUT2_USEC); | |
31a7eb86 ZJS |
1171 | } |
1172 | ||
03e334a1 | 1173 | safe_close(idle_pipe[0]); |
31a7eb86 ZJS |
1174 | |
1175 | } | |
1176 | ||
03e334a1 | 1177 | safe_close(idle_pipe[3]); |
31a7eb86 ZJS |
1178 | } |
1179 | ||
7cae38c4 | 1180 | static int build_environment( |
9fa95f85 | 1181 | const ExecContext *c, |
7cae38c4 | 1182 | unsigned n_fds, |
09812eb7 | 1183 | usec_t watchdog_usec, |
7cae38c4 LP |
1184 | const char *home, |
1185 | const char *username, | |
1186 | const char *shell, | |
1187 | char ***ret) { | |
1188 | ||
1189 | _cleanup_strv_free_ char **our_env = NULL; | |
1190 | unsigned n_env = 0; | |
1191 | char *x; | |
1192 | ||
1193 | assert(c); | |
1194 | assert(ret); | |
1195 | ||
09812eb7 | 1196 | our_env = new0(char*, 10); |
7cae38c4 LP |
1197 | if (!our_env) |
1198 | return -ENOMEM; | |
1199 | ||
1200 | if (n_fds > 0) { | |
ccd06097 | 1201 | if (asprintf(&x, "LISTEN_PID="PID_FMT, getpid()) < 0) |
7cae38c4 LP |
1202 | return -ENOMEM; |
1203 | our_env[n_env++] = x; | |
1204 | ||
1205 | if (asprintf(&x, "LISTEN_FDS=%u", n_fds) < 0) | |
1206 | return -ENOMEM; | |
1207 | our_env[n_env++] = x; | |
1208 | } | |
1209 | ||
09812eb7 | 1210 | if (watchdog_usec > 0) { |
ccd06097 | 1211 | if (asprintf(&x, "WATCHDOG_PID="PID_FMT, getpid()) < 0) |
09812eb7 LP |
1212 | return -ENOMEM; |
1213 | our_env[n_env++] = x; | |
1214 | ||
de0671ee | 1215 | if (asprintf(&x, "WATCHDOG_USEC="USEC_FMT, watchdog_usec) < 0) |
09812eb7 LP |
1216 | return -ENOMEM; |
1217 | our_env[n_env++] = x; | |
1218 | } | |
1219 | ||
7cae38c4 LP |
1220 | if (home) { |
1221 | x = strappend("HOME=", home); | |
1222 | if (!x) | |
1223 | return -ENOMEM; | |
1224 | our_env[n_env++] = x; | |
1225 | } | |
1226 | ||
1227 | if (username) { | |
1228 | x = strappend("LOGNAME=", username); | |
1229 | if (!x) | |
1230 | return -ENOMEM; | |
1231 | our_env[n_env++] = x; | |
1232 | ||
1233 | x = strappend("USER=", username); | |
1234 | if (!x) | |
1235 | return -ENOMEM; | |
1236 | our_env[n_env++] = x; | |
1237 | } | |
1238 | ||
1239 | if (shell) { | |
1240 | x = strappend("SHELL=", shell); | |
1241 | if (!x) | |
1242 | return -ENOMEM; | |
1243 | our_env[n_env++] = x; | |
1244 | } | |
1245 | ||
1246 | if (is_terminal_input(c->std_input) || | |
1247 | c->std_output == EXEC_OUTPUT_TTY || | |
1248 | c->std_error == EXEC_OUTPUT_TTY || | |
1249 | c->tty_path) { | |
1250 | ||
1251 | x = strdup(default_term_for_tty(tty_path(c))); | |
1252 | if (!x) | |
1253 | return -ENOMEM; | |
1254 | our_env[n_env++] = x; | |
1255 | } | |
1256 | ||
1257 | our_env[n_env++] = NULL; | |
09812eb7 | 1258 | assert(n_env <= 10); |
7cae38c4 LP |
1259 | |
1260 | *ret = our_env; | |
1261 | our_env = NULL; | |
1262 | ||
1263 | return 0; | |
1264 | } | |
1265 | ||
8b44a3d2 LP |
1266 | static bool exec_needs_mount_namespace( |
1267 | const ExecContext *context, | |
1268 | const ExecParameters *params, | |
1269 | ExecRuntime *runtime) { | |
1270 | ||
1271 | assert(context); | |
1272 | assert(params); | |
1273 | ||
1274 | if (!strv_isempty(context->read_write_dirs) || | |
1275 | !strv_isempty(context->read_only_dirs) || | |
1276 | !strv_isempty(context->inaccessible_dirs)) | |
1277 | return true; | |
1278 | ||
1279 | if (context->mount_flags != 0) | |
1280 | return true; | |
1281 | ||
1282 | if (context->private_tmp && runtime && (runtime->tmp_dir || runtime->var_tmp_dir)) | |
1283 | return true; | |
1284 | ||
1285 | if (params->bus_endpoint_path) | |
1286 | return true; | |
1287 | ||
1288 | if (context->private_devices || | |
1289 | context->protect_system != PROTECT_SYSTEM_NO || | |
1290 | context->protect_home != PROTECT_HOME_NO) | |
1291 | return true; | |
1292 | ||
1293 | return false; | |
1294 | } | |
1295 | ||
ff0af2a1 | 1296 | static int exec_child( |
f2341e0a | 1297 | Unit *unit, |
ff0af2a1 LP |
1298 | ExecCommand *command, |
1299 | const ExecContext *context, | |
1300 | const ExecParameters *params, | |
1301 | ExecRuntime *runtime, | |
1302 | char **argv, | |
1303 | int socket_fd, | |
1304 | int *fds, unsigned n_fds, | |
1305 | char **files_env, | |
1306 | int *exit_status) { | |
d35fbf6b DM |
1307 | |
1308 | _cleanup_strv_free_ char **our_env = NULL, **pam_env = NULL, **final_env = NULL, **final_argv = NULL; | |
9008e1ac | 1309 | _cleanup_free_ char *mac_selinux_context_net = NULL; |
d35fbf6b DM |
1310 | const char *username = NULL, *home = NULL, *shell = NULL; |
1311 | unsigned n_dont_close = 0; | |
e44da745 | 1312 | int dont_close[n_fds + 4]; |
fed1e721 LP |
1313 | uid_t uid = UID_INVALID; |
1314 | gid_t gid = GID_INVALID; | |
ff0af2a1 | 1315 | int i, r; |
ee818b89 | 1316 | bool needs_mount_namespace; |
034c6ed7 | 1317 | |
f2341e0a | 1318 | assert(unit); |
5cb5a6ff LP |
1319 | assert(command); |
1320 | assert(context); | |
d35fbf6b | 1321 | assert(params); |
ff0af2a1 | 1322 | assert(exit_status); |
d35fbf6b DM |
1323 | |
1324 | rename_process_from_path(command->path); | |
1325 | ||
1326 | /* We reset exactly these signals, since they are the | |
1327 | * only ones we set to SIG_IGN in the main daemon. All | |
1328 | * others we leave untouched because we set them to | |
1329 | * SIG_DFL or a valid handler initially, both of which | |
1330 | * will be demoted to SIG_DFL. */ | |
ce30c8dc LP |
1331 | (void) default_signals(SIGNALS_CRASH_HANDLER, |
1332 | SIGNALS_IGNORE, -1); | |
d35fbf6b DM |
1333 | |
1334 | if (context->ignore_sigpipe) | |
ce30c8dc | 1335 | (void) ignore_signals(SIGPIPE, -1); |
d35fbf6b | 1336 | |
ff0af2a1 LP |
1337 | r = reset_signal_mask(); |
1338 | if (r < 0) { | |
1339 | *exit_status = EXIT_SIGNAL_MASK; | |
1340 | return r; | |
d35fbf6b | 1341 | } |
034c6ed7 | 1342 | |
d35fbf6b DM |
1343 | if (params->idle_pipe) |
1344 | do_idle_pipe_dance(params->idle_pipe); | |
4f2d528d | 1345 | |
d35fbf6b DM |
1346 | /* Close sockets very early to make sure we don't |
1347 | * block init reexecution because it cannot bind its | |
1348 | * sockets */ | |
ff0af2a1 | 1349 | |
d35fbf6b | 1350 | log_forget_fds(); |
4f2d528d | 1351 | |
d35fbf6b DM |
1352 | if (socket_fd >= 0) |
1353 | dont_close[n_dont_close++] = socket_fd; | |
1354 | if (n_fds > 0) { | |
1355 | memcpy(dont_close + n_dont_close, fds, sizeof(int) * n_fds); | |
1356 | n_dont_close += n_fds; | |
1357 | } | |
e44da745 DM |
1358 | if (params->bus_endpoint_fd >= 0) |
1359 | dont_close[n_dont_close++] = params->bus_endpoint_fd; | |
d35fbf6b DM |
1360 | if (runtime) { |
1361 | if (runtime->netns_storage_socket[0] >= 0) | |
1362 | dont_close[n_dont_close++] = runtime->netns_storage_socket[0]; | |
1363 | if (runtime->netns_storage_socket[1] >= 0) | |
1364 | dont_close[n_dont_close++] = runtime->netns_storage_socket[1]; | |
9fa95f85 | 1365 | } |
4f2d528d | 1366 | |
ff0af2a1 LP |
1367 | r = close_all_fds(dont_close, n_dont_close); |
1368 | if (r < 0) { | |
1369 | *exit_status = EXIT_FDS; | |
1370 | return r; | |
8c7be95e LP |
1371 | } |
1372 | ||
d35fbf6b DM |
1373 | if (!context->same_pgrp) |
1374 | if (setsid() < 0) { | |
ff0af2a1 | 1375 | *exit_status = EXIT_SETSID; |
d35fbf6b DM |
1376 | return -errno; |
1377 | } | |
9e2f7c11 | 1378 | |
d35fbf6b DM |
1379 | exec_context_tty_reset(context); |
1380 | ||
1381 | if (params->confirm_spawn) { | |
1382 | char response; | |
1383 | ||
ff0af2a1 LP |
1384 | r = ask_for_confirmation(&response, argv); |
1385 | if (r == -ETIMEDOUT) | |
d35fbf6b | 1386 | write_confirm_message("Confirmation question timed out, assuming positive response.\n"); |
ff0af2a1 LP |
1387 | else if (r < 0) |
1388 | write_confirm_message("Couldn't ask confirmation question, assuming positive response: %s\n", strerror(-r)); | |
d35fbf6b DM |
1389 | else if (response == 's') { |
1390 | write_confirm_message("Skipping execution.\n"); | |
ff0af2a1 | 1391 | *exit_status = EXIT_CONFIRM; |
d35fbf6b DM |
1392 | return -ECANCELED; |
1393 | } else if (response == 'n') { | |
1394 | write_confirm_message("Failing execution.\n"); | |
ff0af2a1 | 1395 | *exit_status = 0; |
d35fbf6b DM |
1396 | return 0; |
1397 | } | |
1398 | } | |
1a63a750 | 1399 | |
524daa8c ZJS |
1400 | if (context->user) { |
1401 | username = context->user; | |
ff0af2a1 LP |
1402 | r = get_user_creds(&username, &uid, &gid, &home, &shell); |
1403 | if (r < 0) { | |
1404 | *exit_status = EXIT_USER; | |
1405 | return r; | |
524daa8c ZJS |
1406 | } |
1407 | } | |
1408 | ||
d35fbf6b DM |
1409 | /* If a socket is connected to STDIN/STDOUT/STDERR, we |
1410 | * must sure to drop O_NONBLOCK */ | |
1411 | if (socket_fd >= 0) | |
1412 | fd_nonblock(socket_fd, false); | |
acbb0225 | 1413 | |
ff0af2a1 LP |
1414 | r = setup_input(context, socket_fd, params->apply_tty_stdin); |
1415 | if (r < 0) { | |
1416 | *exit_status = EXIT_STDIN; | |
1417 | return r; | |
d35fbf6b | 1418 | } |
034c6ed7 | 1419 | |
f2341e0a | 1420 | r = setup_output(unit, context, STDOUT_FILENO, socket_fd, basename(command->path), params->apply_tty_stdin, uid, gid); |
ff0af2a1 LP |
1421 | if (r < 0) { |
1422 | *exit_status = EXIT_STDOUT; | |
1423 | return r; | |
d35fbf6b DM |
1424 | } |
1425 | ||
f2341e0a | 1426 | r = setup_output(unit, context, STDERR_FILENO, socket_fd, basename(command->path), params->apply_tty_stdin, uid, gid); |
ff0af2a1 LP |
1427 | if (r < 0) { |
1428 | *exit_status = EXIT_STDERR; | |
1429 | return r; | |
d35fbf6b DM |
1430 | } |
1431 | ||
1432 | if (params->cgroup_path) { | |
ff0af2a1 LP |
1433 | r = cg_attach_everywhere(params->cgroup_supported, params->cgroup_path, 0, NULL, NULL); |
1434 | if (r < 0) { | |
1435 | *exit_status = EXIT_CGROUP; | |
1436 | return r; | |
309bff19 | 1437 | } |
d35fbf6b | 1438 | } |
309bff19 | 1439 | |
d35fbf6b | 1440 | if (context->oom_score_adjust_set) { |
d5243d62 | 1441 | char t[DECIMAL_STR_MAX(context->oom_score_adjust)]; |
f2b68789 | 1442 | |
d5243d62 LP |
1443 | /* When we can't make this change due to EPERM, then |
1444 | * let's silently skip over it. User namespaces | |
1445 | * prohibit write access to this file, and we | |
1446 | * shouldn't trip up over that. */ | |
613b411c | 1447 | |
d5243d62 | 1448 | sprintf(t, "%i", context->oom_score_adjust); |
ad118bda | 1449 | r = write_string_file("/proc/self/oom_score_adj", t, 0); |
6cb7fa17 | 1450 | if (r == -EPERM || r == -EACCES) { |
ff0af2a1 | 1451 | log_open(); |
f2341e0a | 1452 | log_unit_debug_errno(unit, r, "Failed to adjust OOM setting, assuming containerized execution, ignoring: %m"); |
ff0af2a1 LP |
1453 | log_close(); |
1454 | } else if (r < 0) { | |
1455 | *exit_status = EXIT_OOM_ADJUST; | |
d35fbf6b | 1456 | return -errno; |
613b411c | 1457 | } |
d35fbf6b DM |
1458 | } |
1459 | ||
1460 | if (context->nice_set) | |
1461 | if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) { | |
ff0af2a1 | 1462 | *exit_status = EXIT_NICE; |
d35fbf6b | 1463 | return -errno; |
613b411c LP |
1464 | } |
1465 | ||
d35fbf6b DM |
1466 | if (context->cpu_sched_set) { |
1467 | struct sched_param param = { | |
1468 | .sched_priority = context->cpu_sched_priority, | |
1469 | }; | |
1470 | ||
ff0af2a1 LP |
1471 | r = sched_setscheduler(0, |
1472 | context->cpu_sched_policy | | |
1473 | (context->cpu_sched_reset_on_fork ? | |
1474 | SCHED_RESET_ON_FORK : 0), | |
1475 | ¶m); | |
1476 | if (r < 0) { | |
1477 | *exit_status = EXIT_SETSCHEDULER; | |
d35fbf6b | 1478 | return -errno; |
fc9b2a84 | 1479 | } |
d35fbf6b | 1480 | } |
fc9b2a84 | 1481 | |
d35fbf6b DM |
1482 | if (context->cpuset) |
1483 | if (sched_setaffinity(0, CPU_ALLOC_SIZE(context->cpuset_ncpus), context->cpuset) < 0) { | |
ff0af2a1 | 1484 | *exit_status = EXIT_CPUAFFINITY; |
d35fbf6b | 1485 | return -errno; |
034c6ed7 LP |
1486 | } |
1487 | ||
d35fbf6b DM |
1488 | if (context->ioprio_set) |
1489 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) { | |
ff0af2a1 | 1490 | *exit_status = EXIT_IOPRIO; |
d35fbf6b DM |
1491 | return -errno; |
1492 | } | |
da726a4d | 1493 | |
d35fbf6b DM |
1494 | if (context->timer_slack_nsec != NSEC_INFINITY) |
1495 | if (prctl(PR_SET_TIMERSLACK, context->timer_slack_nsec) < 0) { | |
ff0af2a1 | 1496 | *exit_status = EXIT_TIMERSLACK; |
d35fbf6b | 1497 | return -errno; |
4c2630eb | 1498 | } |
9eba9da4 | 1499 | |
050f7277 | 1500 | if (context->personality != PERSONALITY_INVALID) |
d35fbf6b | 1501 | if (personality(context->personality) < 0) { |
ff0af2a1 | 1502 | *exit_status = EXIT_PERSONALITY; |
d35fbf6b | 1503 | return -errno; |
4c2630eb | 1504 | } |
94f04347 | 1505 | |
d35fbf6b DM |
1506 | if (context->utmp_id) |
1507 | utmp_put_init_process(context->utmp_id, getpid(), getsid(0), context->tty_path); | |
1508 | ||
524daa8c | 1509 | if (context->user && is_terminal_input(context->std_input)) { |
ff0af2a1 LP |
1510 | r = chown_terminal(STDIN_FILENO, uid); |
1511 | if (r < 0) { | |
1512 | *exit_status = EXIT_STDIN; | |
1513 | return r; | |
071830ff | 1514 | } |
d35fbf6b | 1515 | } |
8e274523 | 1516 | |
e44da745 | 1517 | if (params->bus_endpoint_fd >= 0 && context->bus_endpoint) { |
fed1e721 | 1518 | uid_t ep_uid = (uid == UID_INVALID) ? 0 : uid; |
e44da745 | 1519 | |
ff0af2a1 LP |
1520 | r = bus_kernel_set_endpoint_policy(params->bus_endpoint_fd, ep_uid, context->bus_endpoint); |
1521 | if (r < 0) { | |
1522 | *exit_status = EXIT_BUS_ENDPOINT; | |
1523 | return r; | |
e44da745 DM |
1524 | } |
1525 | } | |
e44da745 | 1526 | |
a931ad47 LP |
1527 | /* If delegation is enabled we'll pass ownership of the cgroup |
1528 | * (but only in systemd's own controller hierarchy!) to the | |
1529 | * user of the new process. */ | |
1530 | if (params->cgroup_path && context->user && params->cgroup_delegate) { | |
ff0af2a1 LP |
1531 | r = cg_set_task_access(SYSTEMD_CGROUP_CONTROLLER, params->cgroup_path, 0644, uid, gid); |
1532 | if (r < 0) { | |
1533 | *exit_status = EXIT_CGROUP; | |
1534 | return r; | |
d35fbf6b | 1535 | } |
034c6ed7 | 1536 | |
034c6ed7 | 1537 | |
ff0af2a1 LP |
1538 | r = cg_set_group_access(SYSTEMD_CGROUP_CONTROLLER, params->cgroup_path, 0755, uid, gid); |
1539 | if (r < 0) { | |
1540 | *exit_status = EXIT_CGROUP; | |
1541 | return r; | |
034c6ed7 | 1542 | } |
d35fbf6b | 1543 | } |
034c6ed7 | 1544 | |
d35fbf6b DM |
1545 | if (!strv_isempty(context->runtime_directory) && params->runtime_prefix) { |
1546 | char **rt; | |
fb33a393 | 1547 | |
d35fbf6b DM |
1548 | STRV_FOREACH(rt, context->runtime_directory) { |
1549 | _cleanup_free_ char *p; | |
94f04347 | 1550 | |
d35fbf6b DM |
1551 | p = strjoin(params->runtime_prefix, "/", *rt, NULL); |
1552 | if (!p) { | |
ff0af2a1 | 1553 | *exit_status = EXIT_RUNTIME_DIRECTORY; |
d35fbf6b | 1554 | return -ENOMEM; |
94f04347 | 1555 | } |
94f04347 | 1556 | |
ca905b2f | 1557 | r = mkdir_safe_label(p, context->runtime_directory_mode, uid, gid); |
ff0af2a1 LP |
1558 | if (r < 0) { |
1559 | *exit_status = EXIT_RUNTIME_DIRECTORY; | |
1560 | return r; | |
94f04347 | 1561 | } |
d35fbf6b DM |
1562 | } |
1563 | } | |
94f04347 | 1564 | |
d35fbf6b | 1565 | if (params->apply_permissions) { |
ff0af2a1 LP |
1566 | r = enforce_groups(context, username, gid); |
1567 | if (r < 0) { | |
1568 | *exit_status = EXIT_GROUP; | |
1569 | return r; | |
d35fbf6b DM |
1570 | } |
1571 | } | |
9eba9da4 | 1572 | |
d35fbf6b | 1573 | umask(context->umask); |
94f04347 | 1574 | |
d35fbf6b DM |
1575 | #ifdef HAVE_PAM |
1576 | if (params->apply_permissions && context->pam_name && username) { | |
ff0af2a1 LP |
1577 | r = setup_pam(context->pam_name, username, uid, context->tty_path, &pam_env, fds, n_fds); |
1578 | if (r < 0) { | |
1579 | *exit_status = EXIT_PAM; | |
1580 | return r; | |
d35fbf6b DM |
1581 | } |
1582 | } | |
1583 | #endif | |
ac45f971 | 1584 | |
d35fbf6b | 1585 | if (context->private_network && runtime && runtime->netns_storage_socket[0] >= 0) { |
ff0af2a1 LP |
1586 | r = setup_netns(runtime->netns_storage_socket); |
1587 | if (r < 0) { | |
1588 | *exit_status = EXIT_NETWORK; | |
1589 | return r; | |
d35fbf6b DM |
1590 | } |
1591 | } | |
169c1bda | 1592 | |
ee818b89 AC |
1593 | needs_mount_namespace = exec_needs_mount_namespace(context, params, runtime); |
1594 | ||
1595 | if (needs_mount_namespace) { | |
d35fbf6b DM |
1596 | char *tmp = NULL, *var = NULL; |
1597 | ||
1598 | /* The runtime struct only contains the parent | |
1599 | * of the private /tmp, which is | |
1600 | * non-accessible to world users. Inside of it | |
1601 | * there's a /tmp that is sticky, and that's | |
1602 | * the one we want to use here. */ | |
1603 | ||
1604 | if (context->private_tmp && runtime) { | |
1605 | if (runtime->tmp_dir) | |
63c372cb | 1606 | tmp = strjoina(runtime->tmp_dir, "/tmp"); |
d35fbf6b | 1607 | if (runtime->var_tmp_dir) |
63c372cb | 1608 | var = strjoina(runtime->var_tmp_dir, "/tmp"); |
d35fbf6b | 1609 | } |
d8b4e2e9 | 1610 | |
ff0af2a1 | 1611 | r = setup_namespace( |
ee818b89 | 1612 | params->apply_chroot ? context->root_directory : NULL, |
d35fbf6b DM |
1613 | context->read_write_dirs, |
1614 | context->read_only_dirs, | |
1615 | context->inaccessible_dirs, | |
1616 | tmp, | |
1617 | var, | |
e44da745 | 1618 | params->bus_endpoint_path, |
d35fbf6b DM |
1619 | context->private_devices, |
1620 | context->protect_home, | |
1621 | context->protect_system, | |
1622 | context->mount_flags); | |
0015ebf3 | 1623 | |
ff0af2a1 LP |
1624 | /* If we couldn't set up the namespace this is |
1625 | * probably due to a missing capability. In this case, | |
1626 | * silently proceeed. */ | |
1627 | if (r == -EPERM || r == -EACCES) { | |
1628 | log_open(); | |
f2341e0a | 1629 | log_unit_debug_errno(unit, r, "Failed to set up namespace, assuming containerized execution, ignoring: %m"); |
ff0af2a1 LP |
1630 | log_close(); |
1631 | } else if (r < 0) { | |
1632 | *exit_status = EXIT_NAMESPACE; | |
1633 | return r; | |
81a2b7ce | 1634 | } |
d35fbf6b | 1635 | } |
81a2b7ce | 1636 | |
d35fbf6b | 1637 | if (params->apply_chroot) { |
ee818b89 | 1638 | if (!needs_mount_namespace && context->root_directory) |
d35fbf6b | 1639 | if (chroot(context->root_directory) < 0) { |
ff0af2a1 | 1640 | *exit_status = EXIT_CHROOT; |
d35fbf6b | 1641 | return -errno; |
8aa75193 LP |
1642 | } |
1643 | ||
4c08c824 LP |
1644 | if (chdir(context->working_directory ?: "/") < 0 && |
1645 | !context->working_directory_missing_ok) { | |
ff0af2a1 | 1646 | *exit_status = EXIT_CHDIR; |
d35fbf6b DM |
1647 | return -errno; |
1648 | } | |
1649 | } else { | |
1650 | _cleanup_free_ char *d = NULL; | |
8aa75193 | 1651 | |
d35fbf6b | 1652 | if (asprintf(&d, "%s/%s", |
cf1d0302 LP |
1653 | context->root_directory ?: "", |
1654 | context->working_directory ?: "") < 0) { | |
ff0af2a1 | 1655 | *exit_status = EXIT_MEMORY; |
d35fbf6b | 1656 | return -ENOMEM; |
8aa75193 | 1657 | } |
8aa75193 | 1658 | |
cf1d0302 LP |
1659 | if (chdir(d) < 0 && |
1660 | !context->working_directory_missing_ok) { | |
ff0af2a1 | 1661 | *exit_status = EXIT_CHDIR; |
d35fbf6b DM |
1662 | return -errno; |
1663 | } | |
1664 | } | |
e66cf1a3 | 1665 | |
9008e1ac MS |
1666 | #ifdef HAVE_SELINUX |
1667 | if (params->apply_permissions && mac_selinux_use() && params->selinux_context_net && socket_fd >= 0) { | |
ff0af2a1 LP |
1668 | r = mac_selinux_get_child_mls_label(socket_fd, command->path, context->selinux_context, &mac_selinux_context_net); |
1669 | if (r < 0) { | |
1670 | *exit_status = EXIT_SELINUX_CONTEXT; | |
1671 | return r; | |
9008e1ac MS |
1672 | } |
1673 | } | |
1674 | #endif | |
1675 | ||
d35fbf6b DM |
1676 | /* We repeat the fd closing here, to make sure that |
1677 | * nothing is leaked from the PAM modules. Note that | |
1678 | * we are more aggressive this time since socket_fd | |
e44da745 DM |
1679 | * and the netns fds we don't need anymore. The custom |
1680 | * endpoint fd was needed to upload the policy and can | |
1681 | * now be closed as well. */ | |
ff0af2a1 LP |
1682 | r = close_all_fds(fds, n_fds); |
1683 | if (r >= 0) | |
1684 | r = shift_fds(fds, n_fds); | |
1685 | if (r >= 0) | |
1686 | r = flags_fds(fds, n_fds, context->non_blocking); | |
1687 | if (r < 0) { | |
1688 | *exit_status = EXIT_FDS; | |
1689 | return r; | |
d35fbf6b | 1690 | } |
e66cf1a3 | 1691 | |
d35fbf6b | 1692 | if (params->apply_permissions) { |
e66cf1a3 | 1693 | |
d35fbf6b DM |
1694 | for (i = 0; i < _RLIMIT_MAX; i++) { |
1695 | if (!context->rlimit[i]) | |
1696 | continue; | |
1697 | ||
1698 | if (setrlimit_closest(i, context->rlimit[i]) < 0) { | |
ff0af2a1 | 1699 | *exit_status = EXIT_LIMITS; |
d35fbf6b | 1700 | return -errno; |
e66cf1a3 LP |
1701 | } |
1702 | } | |
1703 | ||
d35fbf6b | 1704 | if (context->capability_bounding_set_drop) { |
ff0af2a1 LP |
1705 | r = capability_bounding_set_drop(context->capability_bounding_set_drop, false); |
1706 | if (r < 0) { | |
1707 | *exit_status = EXIT_CAPABILITIES; | |
1708 | return r; | |
3b8bddde | 1709 | } |
4c2630eb | 1710 | } |
3b8bddde | 1711 | |
2ca620c4 WC |
1712 | #ifdef HAVE_SMACK |
1713 | if (context->smack_process_label) { | |
ff0af2a1 LP |
1714 | r = mac_smack_apply_pid(0, context->smack_process_label); |
1715 | if (r < 0) { | |
1716 | *exit_status = EXIT_SMACK_PROCESS_LABEL; | |
1717 | return r; | |
2ca620c4 WC |
1718 | } |
1719 | } | |
e174dce2 WC |
1720 | #ifdef SMACK_DEFAULT_PROCESS_LABEL |
1721 | else { | |
e419a0e3 WC |
1722 | _cleanup_free_ char *exec_label = NULL; |
1723 | ||
1724 | r = mac_smack_read(command->path, SMACK_ATTR_EXEC, &exec_label); | |
1725 | if (r < 0 && r != -ENODATA && r != -EOPNOTSUPP) { | |
1726 | *exit_status = EXIT_SMACK_PROCESS_LABEL; | |
1727 | return r; | |
1728 | } | |
1729 | ||
1730 | r = mac_smack_apply_pid(0, exec_label ? : SMACK_DEFAULT_PROCESS_LABEL); | |
e174dce2 WC |
1731 | if (r < 0) { |
1732 | *exit_status = EXIT_SMACK_PROCESS_LABEL; | |
1733 | return r; | |
1734 | } | |
1735 | } | |
1736 | #endif | |
2ca620c4 WC |
1737 | #endif |
1738 | ||
d35fbf6b | 1739 | if (context->user) { |
ff0af2a1 LP |
1740 | r = enforce_user(context, uid); |
1741 | if (r < 0) { | |
1742 | *exit_status = EXIT_USER; | |
1743 | return r; | |
5b6319dc LP |
1744 | } |
1745 | } | |
d35fbf6b DM |
1746 | |
1747 | /* PR_GET_SECUREBITS is not privileged, while | |
1748 | * PR_SET_SECUREBITS is. So to suppress | |
1749 | * potential EPERMs we'll try not to call | |
1750 | * PR_SET_SECUREBITS unless necessary. */ | |
1751 | if (prctl(PR_GET_SECUREBITS) != context->secure_bits) | |
1752 | if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) { | |
ff0af2a1 | 1753 | *exit_status = EXIT_SECUREBITS; |
d35fbf6b | 1754 | return -errno; |
ff01d048 | 1755 | } |
5b6319dc | 1756 | |
d35fbf6b DM |
1757 | if (context->capabilities) |
1758 | if (cap_set_proc(context->capabilities) < 0) { | |
ff0af2a1 | 1759 | *exit_status = EXIT_CAPABILITIES; |
d35fbf6b | 1760 | return -errno; |
613b411c LP |
1761 | } |
1762 | ||
d35fbf6b DM |
1763 | if (context->no_new_privileges) |
1764 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) { | |
ff0af2a1 | 1765 | *exit_status = EXIT_NO_NEW_PRIVILEGES; |
d35fbf6b DM |
1766 | return -errno; |
1767 | } | |
1768 | ||
1769 | #ifdef HAVE_SECCOMP | |
1770 | if (context->address_families_whitelist || | |
1771 | !set_isempty(context->address_families)) { | |
ff0af2a1 LP |
1772 | r = apply_address_families(context); |
1773 | if (r < 0) { | |
1774 | *exit_status = EXIT_ADDRESS_FAMILIES; | |
1775 | return r; | |
4c2630eb MS |
1776 | } |
1777 | } | |
04aa0cb9 | 1778 | |
d35fbf6b DM |
1779 | if (context->syscall_whitelist || |
1780 | !set_isempty(context->syscall_filter) || | |
1781 | !set_isempty(context->syscall_archs)) { | |
ff0af2a1 LP |
1782 | r = apply_seccomp(context); |
1783 | if (r < 0) { | |
1784 | *exit_status = EXIT_SECCOMP; | |
1785 | return r; | |
81a2b7ce | 1786 | } |
d35fbf6b DM |
1787 | } |
1788 | #endif | |
81a2b7ce | 1789 | |
d35fbf6b | 1790 | #ifdef HAVE_SELINUX |
6baa7db0 | 1791 | if (mac_selinux_use()) { |
9008e1ac | 1792 | char *exec_context = mac_selinux_context_net ?: context->selinux_context; |
16115b0a | 1793 | |
9008e1ac | 1794 | if (exec_context) { |
ff0af2a1 LP |
1795 | r = setexeccon(exec_context); |
1796 | if (r < 0) { | |
1797 | *exit_status = EXIT_SELINUX_CONTEXT; | |
1798 | return r; | |
16115b0a | 1799 | } |
81a2b7ce | 1800 | } |
81a2b7ce | 1801 | } |
d35fbf6b | 1802 | #endif |
81a2b7ce | 1803 | |
d35fbf6b | 1804 | #ifdef HAVE_APPARMOR |
6baa7db0 | 1805 | if (context->apparmor_profile && mac_apparmor_use()) { |
ff0af2a1 LP |
1806 | r = aa_change_onexec(context->apparmor_profile); |
1807 | if (r < 0 && !context->apparmor_profile_ignore) { | |
1808 | *exit_status = EXIT_APPARMOR_PROFILE; | |
5482192e | 1809 | return -errno; |
d35fbf6b | 1810 | } |
034c6ed7 | 1811 | } |
d35fbf6b DM |
1812 | #endif |
1813 | } | |
034c6ed7 | 1814 | |
ff0af2a1 LP |
1815 | r = build_environment(context, n_fds, params->watchdog_usec, home, username, shell, &our_env); |
1816 | if (r < 0) { | |
1817 | *exit_status = EXIT_MEMORY; | |
1818 | return r; | |
d35fbf6b | 1819 | } |
034c6ed7 | 1820 | |
d35fbf6b DM |
1821 | final_env = strv_env_merge(5, |
1822 | params->environment, | |
1823 | our_env, | |
1824 | context->environment, | |
1825 | files_env, | |
1826 | pam_env, | |
1827 | NULL); | |
1828 | if (!final_env) { | |
ff0af2a1 | 1829 | *exit_status = EXIT_MEMORY; |
d35fbf6b DM |
1830 | return -ENOMEM; |
1831 | } | |
81a2b7ce | 1832 | |
d35fbf6b DM |
1833 | final_argv = replace_env_argv(argv, final_env); |
1834 | if (!final_argv) { | |
ff0af2a1 | 1835 | *exit_status = EXIT_MEMORY; |
d35fbf6b DM |
1836 | return -ENOMEM; |
1837 | } | |
034c6ed7 | 1838 | |
d35fbf6b | 1839 | final_env = strv_env_clean(final_env); |
260abb78 | 1840 | |
553d2243 | 1841 | if (_unlikely_(log_get_max_level() >= LOG_DEBUG)) { |
d35fbf6b | 1842 | _cleanup_free_ char *line; |
81a2b7ce | 1843 | |
d35fbf6b DM |
1844 | line = exec_command_line(final_argv); |
1845 | if (line) { | |
1846 | log_open(); | |
f2341e0a LP |
1847 | log_struct(LOG_DEBUG, |
1848 | LOG_UNIT_ID(unit), | |
1849 | "EXECUTABLE=%s", command->path, | |
1850 | LOG_UNIT_MESSAGE(unit, "Executing: %s", line), | |
1851 | NULL); | |
d35fbf6b DM |
1852 | log_close(); |
1853 | } | |
1854 | } | |
dd305ec9 | 1855 | |
d35fbf6b | 1856 | execve(command->path, final_argv, final_env); |
ff0af2a1 | 1857 | *exit_status = EXIT_EXEC; |
d35fbf6b DM |
1858 | return -errno; |
1859 | } | |
81a2b7ce | 1860 | |
f2341e0a LP |
1861 | int exec_spawn(Unit *unit, |
1862 | ExecCommand *command, | |
d35fbf6b DM |
1863 | const ExecContext *context, |
1864 | const ExecParameters *params, | |
1865 | ExecRuntime *runtime, | |
1866 | pid_t *ret) { | |
8351ceae | 1867 | |
d35fbf6b DM |
1868 | _cleanup_strv_free_ char **files_env = NULL; |
1869 | int *fds = NULL; unsigned n_fds = 0; | |
ff0af2a1 LP |
1870 | _cleanup_free_ char *line = NULL; |
1871 | int socket_fd, r; | |
1872 | char **argv; | |
d35fbf6b | 1873 | pid_t pid; |
8351ceae | 1874 | |
f2341e0a | 1875 | assert(unit); |
d35fbf6b DM |
1876 | assert(command); |
1877 | assert(context); | |
1878 | assert(ret); | |
1879 | assert(params); | |
1880 | assert(params->fds || params->n_fds <= 0); | |
4298d0b5 | 1881 | |
d35fbf6b DM |
1882 | if (context->std_input == EXEC_INPUT_SOCKET || |
1883 | context->std_output == EXEC_OUTPUT_SOCKET || | |
1884 | context->std_error == EXEC_OUTPUT_SOCKET) { | |
17df7223 | 1885 | |
ff0af2a1 | 1886 | if (params->n_fds != 1) { |
f2341e0a | 1887 | log_unit_error(unit, "Got more than one socket."); |
d35fbf6b | 1888 | return -EINVAL; |
ff0af2a1 | 1889 | } |
eef65bf3 | 1890 | |
d35fbf6b DM |
1891 | socket_fd = params->fds[0]; |
1892 | } else { | |
1893 | socket_fd = -1; | |
1894 | fds = params->fds; | |
1895 | n_fds = params->n_fds; | |
1896 | } | |
94f04347 | 1897 | |
f2341e0a | 1898 | r = exec_context_load_environment(unit, context, &files_env); |
ff0af2a1 | 1899 | if (r < 0) |
f2341e0a | 1900 | return log_unit_error_errno(unit, r, "Failed to load environment files: %m"); |
034c6ed7 | 1901 | |
d35fbf6b | 1902 | argv = params->argv ?: command->argv; |
d35fbf6b DM |
1903 | line = exec_command_line(argv); |
1904 | if (!line) | |
1905 | return log_oom(); | |
fab56fc5 | 1906 | |
f2341e0a LP |
1907 | log_struct(LOG_DEBUG, |
1908 | LOG_UNIT_ID(unit), | |
1909 | LOG_UNIT_MESSAGE(unit, "About to execute: %s", line), | |
1910 | "EXECUTABLE=%s", command->path, | |
1911 | NULL); | |
d35fbf6b DM |
1912 | pid = fork(); |
1913 | if (pid < 0) | |
f2341e0a | 1914 | return log_unit_error_errno(unit, r, "Failed to fork: %m"); |
d35fbf6b DM |
1915 | |
1916 | if (pid == 0) { | |
ff0af2a1 LP |
1917 | int exit_status; |
1918 | ||
f2341e0a LP |
1919 | r = exec_child(unit, |
1920 | command, | |
ff0af2a1 LP |
1921 | context, |
1922 | params, | |
1923 | runtime, | |
1924 | argv, | |
1925 | socket_fd, | |
1926 | fds, n_fds, | |
1927 | files_env, | |
1928 | &exit_status); | |
1929 | if (r < 0) { | |
4c2630eb | 1930 | log_open(); |
f2341e0a LP |
1931 | log_struct_errno(LOG_ERR, r, |
1932 | LOG_MESSAGE_ID(SD_MESSAGE_SPAWN_FAILED), | |
1933 | LOG_UNIT_ID(unit), | |
1934 | LOG_UNIT_MESSAGE(unit, "Failed at step %s spawning %s: %m", | |
1935 | exit_status_to_string(exit_status, EXIT_STATUS_SYSTEMD), | |
1936 | command->path), | |
1937 | "EXECUTABLE=%s", command->path, | |
1938 | NULL); | |
4c2630eb MS |
1939 | } |
1940 | ||
ff0af2a1 | 1941 | _exit(exit_status); |
034c6ed7 LP |
1942 | } |
1943 | ||
f2341e0a | 1944 | log_unit_debug(unit, "Forked %s as "PID_FMT, command->path, pid); |
23635a85 | 1945 | |
80876c20 LP |
1946 | /* We add the new process to the cgroup both in the child (so |
1947 | * that we can be sure that no user code is ever executed | |
1948 | * outside of the cgroup) and in the parent (so that we can be | |
1949 | * sure that when we kill the cgroup the process will be | |
1950 | * killed too). */ | |
d35fbf6b | 1951 | if (params->cgroup_path) |
dd305ec9 | 1952 | (void) cg_attach(SYSTEMD_CGROUP_CONTROLLER, params->cgroup_path, pid); |
2da3263a | 1953 | |
b58b4116 | 1954 | exec_status_start(&command->exec_status, pid); |
9fb86720 | 1955 | |
034c6ed7 | 1956 | *ret = pid; |
5cb5a6ff LP |
1957 | return 0; |
1958 | } | |
1959 | ||
034c6ed7 LP |
1960 | void exec_context_init(ExecContext *c) { |
1961 | assert(c); | |
1962 | ||
4c12626c | 1963 | c->umask = 0022; |
9eba9da4 | 1964 | c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0); |
94f04347 | 1965 | c->cpu_sched_policy = SCHED_OTHER; |
071830ff | 1966 | c->syslog_priority = LOG_DAEMON|LOG_INFO; |
74922904 | 1967 | c->syslog_level_prefix = true; |
353e12c2 | 1968 | c->ignore_sigpipe = true; |
3a43da28 | 1969 | c->timer_slack_nsec = NSEC_INFINITY; |
050f7277 | 1970 | c->personality = PERSONALITY_INVALID; |
e66cf1a3 | 1971 | c->runtime_directory_mode = 0755; |
034c6ed7 LP |
1972 | } |
1973 | ||
613b411c | 1974 | void exec_context_done(ExecContext *c) { |
5cb5a6ff LP |
1975 | unsigned l; |
1976 | ||
1977 | assert(c); | |
1978 | ||
1979 | strv_free(c->environment); | |
034c6ed7 | 1980 | c->environment = NULL; |
5cb5a6ff | 1981 | |
8c7be95e LP |
1982 | strv_free(c->environment_files); |
1983 | c->environment_files = NULL; | |
1984 | ||
034c6ed7 | 1985 | for (l = 0; l < ELEMENTSOF(c->rlimit); l++) { |
5cb5a6ff | 1986 | free(c->rlimit[l]); |
034c6ed7 LP |
1987 | c->rlimit[l] = NULL; |
1988 | } | |
1989 | ||
9eba9da4 LP |
1990 | free(c->working_directory); |
1991 | c->working_directory = NULL; | |
1992 | free(c->root_directory); | |
1993 | c->root_directory = NULL; | |
5cb5a6ff | 1994 | |
80876c20 LP |
1995 | free(c->tty_path); |
1996 | c->tty_path = NULL; | |
1997 | ||
071830ff LP |
1998 | free(c->syslog_identifier); |
1999 | c->syslog_identifier = NULL; | |
2000 | ||
5cb5a6ff | 2001 | free(c->user); |
034c6ed7 LP |
2002 | c->user = NULL; |
2003 | ||
5cb5a6ff | 2004 | free(c->group); |
034c6ed7 LP |
2005 | c->group = NULL; |
2006 | ||
2007 | strv_free(c->supplementary_groups); | |
2008 | c->supplementary_groups = NULL; | |
94f04347 | 2009 | |
5b6319dc LP |
2010 | free(c->pam_name); |
2011 | c->pam_name = NULL; | |
2012 | ||
94f04347 LP |
2013 | if (c->capabilities) { |
2014 | cap_free(c->capabilities); | |
2015 | c->capabilities = NULL; | |
2016 | } | |
15ae422b LP |
2017 | |
2018 | strv_free(c->read_only_dirs); | |
2019 | c->read_only_dirs = NULL; | |
2020 | ||
2021 | strv_free(c->read_write_dirs); | |
2022 | c->read_write_dirs = NULL; | |
2023 | ||
2024 | strv_free(c->inaccessible_dirs); | |
2025 | c->inaccessible_dirs = NULL; | |
82c121a4 LP |
2026 | |
2027 | if (c->cpuset) | |
2028 | CPU_FREE(c->cpuset); | |
86a3475b LP |
2029 | |
2030 | free(c->utmp_id); | |
2031 | c->utmp_id = NULL; | |
b9a0e010 | 2032 | |
7b52a628 MS |
2033 | free(c->selinux_context); |
2034 | c->selinux_context = NULL; | |
2035 | ||
eef65bf3 MS |
2036 | free(c->apparmor_profile); |
2037 | c->apparmor_profile = NULL; | |
2038 | ||
17df7223 LP |
2039 | set_free(c->syscall_filter); |
2040 | c->syscall_filter = NULL; | |
57183d11 LP |
2041 | |
2042 | set_free(c->syscall_archs); | |
2043 | c->syscall_archs = NULL; | |
4298d0b5 LP |
2044 | |
2045 | set_free(c->address_families); | |
2046 | c->address_families = NULL; | |
e66cf1a3 LP |
2047 | |
2048 | strv_free(c->runtime_directory); | |
2049 | c->runtime_directory = NULL; | |
bb7dd0b0 DM |
2050 | |
2051 | bus_endpoint_free(c->bus_endpoint); | |
2052 | c->bus_endpoint = NULL; | |
e66cf1a3 LP |
2053 | } |
2054 | ||
2055 | int exec_context_destroy_runtime_directory(ExecContext *c, const char *runtime_prefix) { | |
2056 | char **i; | |
2057 | ||
2058 | assert(c); | |
2059 | ||
2060 | if (!runtime_prefix) | |
2061 | return 0; | |
2062 | ||
2063 | STRV_FOREACH(i, c->runtime_directory) { | |
2064 | _cleanup_free_ char *p; | |
2065 | ||
2066 | p = strjoin(runtime_prefix, "/", *i, NULL); | |
2067 | if (!p) | |
2068 | return -ENOMEM; | |
2069 | ||
2070 | /* We execute this synchronously, since we need to be | |
2071 | * sure this is gone when we start the service | |
2072 | * next. */ | |
c6878637 | 2073 | (void) rm_rf(p, REMOVE_ROOT); |
e66cf1a3 LP |
2074 | } |
2075 | ||
2076 | return 0; | |
5cb5a6ff LP |
2077 | } |
2078 | ||
43d0fcbd LP |
2079 | void exec_command_done(ExecCommand *c) { |
2080 | assert(c); | |
2081 | ||
2082 | free(c->path); | |
2083 | c->path = NULL; | |
2084 | ||
2085 | strv_free(c->argv); | |
2086 | c->argv = NULL; | |
2087 | } | |
2088 | ||
2089 | void exec_command_done_array(ExecCommand *c, unsigned n) { | |
2090 | unsigned i; | |
2091 | ||
2092 | for (i = 0; i < n; i++) | |
2093 | exec_command_done(c+i); | |
2094 | } | |
2095 | ||
f1acf85a | 2096 | ExecCommand* exec_command_free_list(ExecCommand *c) { |
5cb5a6ff LP |
2097 | ExecCommand *i; |
2098 | ||
2099 | while ((i = c)) { | |
71fda00f | 2100 | LIST_REMOVE(command, c, i); |
43d0fcbd | 2101 | exec_command_done(i); |
5cb5a6ff LP |
2102 | free(i); |
2103 | } | |
f1acf85a ZJS |
2104 | |
2105 | return NULL; | |
5cb5a6ff LP |
2106 | } |
2107 | ||
034c6ed7 LP |
2108 | void exec_command_free_array(ExecCommand **c, unsigned n) { |
2109 | unsigned i; | |
2110 | ||
f1acf85a ZJS |
2111 | for (i = 0; i < n; i++) |
2112 | c[i] = exec_command_free_list(c[i]); | |
034c6ed7 LP |
2113 | } |
2114 | ||
039f0e70 | 2115 | typedef struct InvalidEnvInfo { |
f2341e0a | 2116 | Unit *unit; |
039f0e70 LP |
2117 | const char *path; |
2118 | } InvalidEnvInfo; | |
2119 | ||
2120 | static void invalid_env(const char *p, void *userdata) { | |
2121 | InvalidEnvInfo *info = userdata; | |
2122 | ||
f2341e0a | 2123 | log_unit_error(info->unit, "Ignoring invalid environment assignment '%s': %s", p, info->path); |
039f0e70 LP |
2124 | } |
2125 | ||
f2341e0a | 2126 | int exec_context_load_environment(Unit *unit, const ExecContext *c, char ***l) { |
8c7be95e LP |
2127 | char **i, **r = NULL; |
2128 | ||
2129 | assert(c); | |
2130 | assert(l); | |
2131 | ||
2132 | STRV_FOREACH(i, c->environment_files) { | |
2133 | char *fn; | |
2134 | int k; | |
2135 | bool ignore = false; | |
2136 | char **p; | |
7fd1b19b | 2137 | _cleanup_globfree_ glob_t pglob = {}; |
2bef10ab | 2138 | int count, n; |
8c7be95e LP |
2139 | |
2140 | fn = *i; | |
2141 | ||
2142 | if (fn[0] == '-') { | |
2143 | ignore = true; | |
2144 | fn ++; | |
2145 | } | |
2146 | ||
2147 | if (!path_is_absolute(fn)) { | |
8c7be95e LP |
2148 | if (ignore) |
2149 | continue; | |
2150 | ||
2151 | strv_free(r); | |
2152 | return -EINVAL; | |
2153 | } | |
2154 | ||
2bef10ab | 2155 | /* Filename supports globbing, take all matching files */ |
2bef10ab PL |
2156 | errno = 0; |
2157 | if (glob(fn, 0, NULL, &pglob) != 0) { | |
2bef10ab PL |
2158 | if (ignore) |
2159 | continue; | |
8c7be95e | 2160 | |
2bef10ab PL |
2161 | strv_free(r); |
2162 | return errno ? -errno : -EINVAL; | |
2163 | } | |
2164 | count = pglob.gl_pathc; | |
2165 | if (count == 0) { | |
8c7be95e LP |
2166 | if (ignore) |
2167 | continue; | |
2168 | ||
2169 | strv_free(r); | |
2bef10ab | 2170 | return -EINVAL; |
8c7be95e | 2171 | } |
2bef10ab | 2172 | for (n = 0; n < count; n++) { |
717603e3 | 2173 | k = load_env_file(NULL, pglob.gl_pathv[n], NULL, &p); |
2bef10ab PL |
2174 | if (k < 0) { |
2175 | if (ignore) | |
2176 | continue; | |
8c7be95e | 2177 | |
2bef10ab | 2178 | strv_free(r); |
2bef10ab | 2179 | return k; |
e9c1ea9d | 2180 | } |
ebc05a09 | 2181 | /* Log invalid environment variables with filename */ |
039f0e70 LP |
2182 | if (p) { |
2183 | InvalidEnvInfo info = { | |
f2341e0a | 2184 | .unit = unit, |
039f0e70 LP |
2185 | .path = pglob.gl_pathv[n] |
2186 | }; | |
2187 | ||
2188 | p = strv_env_clean_with_callback(p, invalid_env, &info); | |
2189 | } | |
8c7be95e | 2190 | |
2bef10ab PL |
2191 | if (r == NULL) |
2192 | r = p; | |
2193 | else { | |
2194 | char **m; | |
8c7be95e | 2195 | |
2bef10ab PL |
2196 | m = strv_env_merge(2, r, p); |
2197 | strv_free(r); | |
2198 | strv_free(p); | |
c84a9488 | 2199 | if (!m) |
2bef10ab | 2200 | return -ENOMEM; |
2bef10ab PL |
2201 | |
2202 | r = m; | |
2203 | } | |
8c7be95e LP |
2204 | } |
2205 | } | |
2206 | ||
2207 | *l = r; | |
2208 | ||
2209 | return 0; | |
2210 | } | |
2211 | ||
6ac8fdc9 | 2212 | static bool tty_may_match_dev_console(const char *tty) { |
e1d75803 RC |
2213 | _cleanup_free_ char *active = NULL; |
2214 | char *console; | |
6ac8fdc9 MS |
2215 | |
2216 | if (startswith(tty, "/dev/")) | |
2217 | tty += 5; | |
2218 | ||
2219 | /* trivial identity? */ | |
2220 | if (streq(tty, "console")) | |
2221 | return true; | |
2222 | ||
2223 | console = resolve_dev_console(&active); | |
2224 | /* if we could not resolve, assume it may */ | |
2225 | if (!console) | |
2226 | return true; | |
2227 | ||
2228 | /* "tty0" means the active VC, so it may be the same sometimes */ | |
e1d75803 | 2229 | return streq(console, tty) || (streq(console, "tty0") && tty_is_vc(tty)); |
6ac8fdc9 MS |
2230 | } |
2231 | ||
2232 | bool exec_context_may_touch_console(ExecContext *ec) { | |
2233 | return (ec->tty_reset || ec->tty_vhangup || ec->tty_vt_disallocate || | |
2234 | is_terminal_input(ec->std_input) || | |
2235 | is_terminal_output(ec->std_output) || | |
2236 | is_terminal_output(ec->std_error)) && | |
2237 | tty_may_match_dev_console(tty_path(ec)); | |
2238 | } | |
2239 | ||
15ae422b LP |
2240 | static void strv_fprintf(FILE *f, char **l) { |
2241 | char **g; | |
2242 | ||
2243 | assert(f); | |
2244 | ||
2245 | STRV_FOREACH(g, l) | |
2246 | fprintf(f, " %s", *g); | |
2247 | } | |
2248 | ||
5cb5a6ff | 2249 | void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) { |
507f22bd | 2250 | char **e; |
94f04347 | 2251 | unsigned i; |
9eba9da4 | 2252 | |
5cb5a6ff LP |
2253 | assert(c); |
2254 | assert(f); | |
2255 | ||
4ad49000 | 2256 | prefix = strempty(prefix); |
5cb5a6ff LP |
2257 | |
2258 | fprintf(f, | |
94f04347 LP |
2259 | "%sUMask: %04o\n" |
2260 | "%sWorkingDirectory: %s\n" | |
451a074f | 2261 | "%sRootDirectory: %s\n" |
15ae422b | 2262 | "%sNonBlocking: %s\n" |
64747e2d | 2263 | "%sPrivateTmp: %s\n" |
4819ff03 | 2264 | "%sPrivateNetwork: %s\n" |
7f112f50 | 2265 | "%sPrivateDevices: %s\n" |
1b8689f9 LP |
2266 | "%sProtectHome: %s\n" |
2267 | "%sProtectSystem: %s\n" | |
3bb07b76 | 2268 | "%sIgnoreSIGPIPE: %s\n", |
5cb5a6ff | 2269 | prefix, c->umask, |
9eba9da4 | 2270 | prefix, c->working_directory ? c->working_directory : "/", |
451a074f | 2271 | prefix, c->root_directory ? c->root_directory : "/", |
15ae422b | 2272 | prefix, yes_no(c->non_blocking), |
64747e2d | 2273 | prefix, yes_no(c->private_tmp), |
4819ff03 | 2274 | prefix, yes_no(c->private_network), |
7f112f50 | 2275 | prefix, yes_no(c->private_devices), |
1b8689f9 LP |
2276 | prefix, protect_home_to_string(c->protect_home), |
2277 | prefix, protect_system_to_string(c->protect_system), | |
3bb07b76 | 2278 | prefix, yes_no(c->ignore_sigpipe)); |
fb33a393 | 2279 | |
8c7be95e LP |
2280 | STRV_FOREACH(e, c->environment) |
2281 | fprintf(f, "%sEnvironment: %s\n", prefix, *e); | |
2282 | ||
2283 | STRV_FOREACH(e, c->environment_files) | |
2284 | fprintf(f, "%sEnvironmentFile: %s\n", prefix, *e); | |
94f04347 | 2285 | |
fb33a393 LP |
2286 | if (c->nice_set) |
2287 | fprintf(f, | |
2288 | "%sNice: %i\n", | |
2289 | prefix, c->nice); | |
2290 | ||
dd6c17b1 | 2291 | if (c->oom_score_adjust_set) |
fb33a393 | 2292 | fprintf(f, |
dd6c17b1 LP |
2293 | "%sOOMScoreAdjust: %i\n", |
2294 | prefix, c->oom_score_adjust); | |
9eba9da4 | 2295 | |
94f04347 LP |
2296 | for (i = 0; i < RLIM_NLIMITS; i++) |
2297 | if (c->rlimit[i]) | |
de0671ee ZJS |
2298 | fprintf(f, "%s%s: "RLIM_FMT"\n", |
2299 | prefix, rlimit_to_string(i), c->rlimit[i]->rlim_max); | |
94f04347 | 2300 | |
f8b69d1d | 2301 | if (c->ioprio_set) { |
1756a011 | 2302 | _cleanup_free_ char *class_str = NULL; |
f8b69d1d | 2303 | |
1756a011 | 2304 | ioprio_class_to_string_alloc(IOPRIO_PRIO_CLASS(c->ioprio), &class_str); |
9eba9da4 LP |
2305 | fprintf(f, |
2306 | "%sIOSchedulingClass: %s\n" | |
2307 | "%sIOPriority: %i\n", | |
f8b69d1d | 2308 | prefix, strna(class_str), |
9eba9da4 | 2309 | prefix, (int) IOPRIO_PRIO_DATA(c->ioprio)); |
f8b69d1d | 2310 | } |
94f04347 | 2311 | |
f8b69d1d | 2312 | if (c->cpu_sched_set) { |
1756a011 | 2313 | _cleanup_free_ char *policy_str = NULL; |
f8b69d1d | 2314 | |
1756a011 | 2315 | sched_policy_to_string_alloc(c->cpu_sched_policy, &policy_str); |
94f04347 LP |
2316 | fprintf(f, |
2317 | "%sCPUSchedulingPolicy: %s\n" | |
38b48754 LP |
2318 | "%sCPUSchedulingPriority: %i\n" |
2319 | "%sCPUSchedulingResetOnFork: %s\n", | |
f8b69d1d | 2320 | prefix, strna(policy_str), |
38b48754 LP |
2321 | prefix, c->cpu_sched_priority, |
2322 | prefix, yes_no(c->cpu_sched_reset_on_fork)); | |
b929bf04 | 2323 | } |
94f04347 | 2324 | |
82c121a4 | 2325 | if (c->cpuset) { |
94f04347 | 2326 | fprintf(f, "%sCPUAffinity:", prefix); |
82c121a4 LP |
2327 | for (i = 0; i < c->cpuset_ncpus; i++) |
2328 | if (CPU_ISSET_S(i, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset)) | |
43a99a7a | 2329 | fprintf(f, " %u", i); |
94f04347 LP |
2330 | fputs("\n", f); |
2331 | } | |
2332 | ||
3a43da28 | 2333 | if (c->timer_slack_nsec != NSEC_INFINITY) |
ccd06097 | 2334 | fprintf(f, "%sTimerSlackNSec: "NSEC_FMT "\n", prefix, c->timer_slack_nsec); |
94f04347 LP |
2335 | |
2336 | fprintf(f, | |
80876c20 LP |
2337 | "%sStandardInput: %s\n" |
2338 | "%sStandardOutput: %s\n" | |
2339 | "%sStandardError: %s\n", | |
2340 | prefix, exec_input_to_string(c->std_input), | |
2341 | prefix, exec_output_to_string(c->std_output), | |
2342 | prefix, exec_output_to_string(c->std_error)); | |
2343 | ||
2344 | if (c->tty_path) | |
2345 | fprintf(f, | |
6ea832a2 LP |
2346 | "%sTTYPath: %s\n" |
2347 | "%sTTYReset: %s\n" | |
2348 | "%sTTYVHangup: %s\n" | |
2349 | "%sTTYVTDisallocate: %s\n", | |
2350 | prefix, c->tty_path, | |
2351 | prefix, yes_no(c->tty_reset), | |
2352 | prefix, yes_no(c->tty_vhangup), | |
2353 | prefix, yes_no(c->tty_vt_disallocate)); | |
94f04347 | 2354 | |
5ce70e5b ZJS |
2355 | if (c->std_output == EXEC_OUTPUT_SYSLOG || |
2356 | c->std_output == EXEC_OUTPUT_KMSG || | |
2357 | c->std_output == EXEC_OUTPUT_JOURNAL || | |
2358 | c->std_output == EXEC_OUTPUT_SYSLOG_AND_CONSOLE || | |
2359 | c->std_output == EXEC_OUTPUT_KMSG_AND_CONSOLE || | |
2360 | c->std_output == EXEC_OUTPUT_JOURNAL_AND_CONSOLE || | |
2361 | c->std_error == EXEC_OUTPUT_SYSLOG || | |
2362 | c->std_error == EXEC_OUTPUT_KMSG || | |
2363 | c->std_error == EXEC_OUTPUT_JOURNAL || | |
2364 | c->std_error == EXEC_OUTPUT_SYSLOG_AND_CONSOLE || | |
2365 | c->std_error == EXEC_OUTPUT_KMSG_AND_CONSOLE || | |
2366 | c->std_error == EXEC_OUTPUT_JOURNAL_AND_CONSOLE) { | |
f8b69d1d | 2367 | |
5ce70e5b | 2368 | _cleanup_free_ char *fac_str = NULL, *lvl_str = NULL; |
f8b69d1d | 2369 | |
5ce70e5b ZJS |
2370 | log_facility_unshifted_to_string_alloc(c->syslog_priority >> 3, &fac_str); |
2371 | log_level_to_string_alloc(LOG_PRI(c->syslog_priority), &lvl_str); | |
f8b69d1d | 2372 | |
94f04347 LP |
2373 | fprintf(f, |
2374 | "%sSyslogFacility: %s\n" | |
2375 | "%sSyslogLevel: %s\n", | |
f8b69d1d MS |
2376 | prefix, strna(fac_str), |
2377 | prefix, strna(lvl_str)); | |
f8b69d1d | 2378 | } |
94f04347 LP |
2379 | |
2380 | if (c->capabilities) { | |
5ce70e5b ZJS |
2381 | _cleanup_cap_free_charp_ char *t; |
2382 | ||
2383 | t = cap_to_text(c->capabilities, NULL); | |
2384 | if (t) | |
2385 | fprintf(f, "%sCapabilities: %s\n", prefix, t); | |
94f04347 LP |
2386 | } |
2387 | ||
2388 | if (c->secure_bits) | |
2389 | fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n", | |
2390 | prefix, | |
cbb21cca ZJS |
2391 | (c->secure_bits & 1<<SECURE_KEEP_CAPS) ? " keep-caps" : "", |
2392 | (c->secure_bits & 1<<SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "", | |
2393 | (c->secure_bits & 1<<SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "", | |
2394 | (c->secure_bits & 1<<SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "", | |
2395 | (c->secure_bits & 1<<SECURE_NOROOT) ? " noroot" : "", | |
2396 | (c->secure_bits & 1<<SECURE_NOROOT_LOCKED) ? "noroot-locked" : ""); | |
94f04347 LP |
2397 | |
2398 | if (c->capability_bounding_set_drop) { | |
ae556c21 | 2399 | unsigned long l; |
260abb78 | 2400 | fprintf(f, "%sCapabilityBoundingSet:", prefix); |
94f04347 | 2401 | |
64685e0c | 2402 | for (l = 0; l <= cap_last_cap(); l++) |
2822da4f LP |
2403 | if (!(c->capability_bounding_set_drop & ((uint64_t) 1ULL << (uint64_t) l))) |
2404 | fprintf(f, " %s", strna(capability_to_name(l))); | |
94f04347 LP |
2405 | |
2406 | fputs("\n", f); | |
2407 | } | |
2408 | ||
2409 | if (c->user) | |
f2d3769a | 2410 | fprintf(f, "%sUser: %s\n", prefix, c->user); |
94f04347 | 2411 | if (c->group) |
f2d3769a | 2412 | fprintf(f, "%sGroup: %s\n", prefix, c->group); |
94f04347 | 2413 | |
15ae422b | 2414 | if (strv_length(c->supplementary_groups) > 0) { |
94f04347 | 2415 | fprintf(f, "%sSupplementaryGroups:", prefix); |
15ae422b LP |
2416 | strv_fprintf(f, c->supplementary_groups); |
2417 | fputs("\n", f); | |
2418 | } | |
94f04347 | 2419 | |
5b6319dc | 2420 | if (c->pam_name) |
f2d3769a | 2421 | fprintf(f, "%sPAMName: %s\n", prefix, c->pam_name); |
5b6319dc | 2422 | |
15ae422b LP |
2423 | if (strv_length(c->read_write_dirs) > 0) { |
2424 | fprintf(f, "%sReadWriteDirs:", prefix); | |
2425 | strv_fprintf(f, c->read_write_dirs); | |
2426 | fputs("\n", f); | |
2427 | } | |
2428 | ||
2429 | if (strv_length(c->read_only_dirs) > 0) { | |
2430 | fprintf(f, "%sReadOnlyDirs:", prefix); | |
2431 | strv_fprintf(f, c->read_only_dirs); | |
2432 | fputs("\n", f); | |
2433 | } | |
94f04347 | 2434 | |
15ae422b LP |
2435 | if (strv_length(c->inaccessible_dirs) > 0) { |
2436 | fprintf(f, "%sInaccessibleDirs:", prefix); | |
2437 | strv_fprintf(f, c->inaccessible_dirs); | |
94f04347 LP |
2438 | fputs("\n", f); |
2439 | } | |
2e22afe9 | 2440 | |
169c1bda LP |
2441 | if (c->utmp_id) |
2442 | fprintf(f, | |
2443 | "%sUtmpIdentifier: %s\n", | |
2444 | prefix, c->utmp_id); | |
7b52a628 MS |
2445 | |
2446 | if (c->selinux_context) | |
2447 | fprintf(f, | |
5f8640fb LP |
2448 | "%sSELinuxContext: %s%s\n", |
2449 | prefix, c->selinux_context_ignore ? "-" : "", c->selinux_context); | |
17df7223 | 2450 | |
050f7277 | 2451 | if (c->personality != PERSONALITY_INVALID) |
ac45f971 LP |
2452 | fprintf(f, |
2453 | "%sPersonality: %s\n", | |
2454 | prefix, strna(personality_to_string(c->personality))); | |
2455 | ||
17df7223 | 2456 | if (c->syscall_filter) { |
351a19b1 | 2457 | #ifdef HAVE_SECCOMP |
17df7223 LP |
2458 | Iterator j; |
2459 | void *id; | |
2460 | bool first = true; | |
351a19b1 | 2461 | #endif |
17df7223 LP |
2462 | |
2463 | fprintf(f, | |
57183d11 | 2464 | "%sSystemCallFilter: ", |
17df7223 LP |
2465 | prefix); |
2466 | ||
2467 | if (!c->syscall_whitelist) | |
2468 | fputc('~', f); | |
2469 | ||
351a19b1 | 2470 | #ifdef HAVE_SECCOMP |
17df7223 LP |
2471 | SET_FOREACH(id, c->syscall_filter, j) { |
2472 | _cleanup_free_ char *name = NULL; | |
2473 | ||
2474 | if (first) | |
2475 | first = false; | |
2476 | else | |
2477 | fputc(' ', f); | |
2478 | ||
57183d11 | 2479 | name = seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE, PTR_TO_INT(id) - 1); |
17df7223 LP |
2480 | fputs(strna(name), f); |
2481 | } | |
351a19b1 | 2482 | #endif |
17df7223 LP |
2483 | |
2484 | fputc('\n', f); | |
2485 | } | |
2486 | ||
57183d11 LP |
2487 | if (c->syscall_archs) { |
2488 | #ifdef HAVE_SECCOMP | |
2489 | Iterator j; | |
2490 | void *id; | |
2491 | #endif | |
2492 | ||
2493 | fprintf(f, | |
2494 | "%sSystemCallArchitectures:", | |
2495 | prefix); | |
2496 | ||
2497 | #ifdef HAVE_SECCOMP | |
2498 | SET_FOREACH(id, c->syscall_archs, j) | |
2499 | fprintf(f, " %s", strna(seccomp_arch_to_string(PTR_TO_UINT32(id) - 1))); | |
2500 | #endif | |
2501 | fputc('\n', f); | |
2502 | } | |
2503 | ||
17df7223 LP |
2504 | if (c->syscall_errno != 0) |
2505 | fprintf(f, | |
2506 | "%sSystemCallErrorNumber: %s\n", | |
2507 | prefix, strna(errno_to_name(c->syscall_errno))); | |
eef65bf3 MS |
2508 | |
2509 | if (c->apparmor_profile) | |
2510 | fprintf(f, | |
2511 | "%sAppArmorProfile: %s%s\n", | |
2512 | prefix, c->apparmor_profile_ignore ? "-" : "", c->apparmor_profile); | |
5cb5a6ff LP |
2513 | } |
2514 | ||
a931ad47 LP |
2515 | bool exec_context_maintains_privileges(ExecContext *c) { |
2516 | assert(c); | |
2517 | ||
2518 | /* Returns true if the process forked off would run run under | |
2519 | * an unchanged UID or as root. */ | |
2520 | ||
2521 | if (!c->user) | |
2522 | return true; | |
2523 | ||
2524 | if (streq(c->user, "root") || streq(c->user, "0")) | |
2525 | return true; | |
2526 | ||
2527 | return false; | |
2528 | } | |
2529 | ||
b58b4116 | 2530 | void exec_status_start(ExecStatus *s, pid_t pid) { |
034c6ed7 | 2531 | assert(s); |
5cb5a6ff | 2532 | |
b58b4116 LP |
2533 | zero(*s); |
2534 | s->pid = pid; | |
2535 | dual_timestamp_get(&s->start_timestamp); | |
2536 | } | |
2537 | ||
6ea832a2 | 2538 | void exec_status_exit(ExecStatus *s, ExecContext *context, pid_t pid, int code, int status) { |
b58b4116 LP |
2539 | assert(s); |
2540 | ||
0b1f4ae6 | 2541 | if (s->pid && s->pid != pid) |
b58b4116 LP |
2542 | zero(*s); |
2543 | ||
034c6ed7 | 2544 | s->pid = pid; |
63983207 | 2545 | dual_timestamp_get(&s->exit_timestamp); |
9fb86720 | 2546 | |
034c6ed7 LP |
2547 | s->code = code; |
2548 | s->status = status; | |
169c1bda | 2549 | |
6ea832a2 LP |
2550 | if (context) { |
2551 | if (context->utmp_id) | |
2552 | utmp_put_dead_process(context->utmp_id, pid, code, status); | |
2553 | ||
2554 | exec_context_tty_reset(context); | |
2555 | } | |
9fb86720 LP |
2556 | } |
2557 | ||
2558 | void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) { | |
2559 | char buf[FORMAT_TIMESTAMP_MAX]; | |
2560 | ||
2561 | assert(s); | |
2562 | assert(f); | |
2563 | ||
9fb86720 LP |
2564 | if (s->pid <= 0) |
2565 | return; | |
2566 | ||
4c940960 LP |
2567 | prefix = strempty(prefix); |
2568 | ||
9fb86720 | 2569 | fprintf(f, |
ccd06097 ZJS |
2570 | "%sPID: "PID_FMT"\n", |
2571 | prefix, s->pid); | |
9fb86720 | 2572 | |
63983207 | 2573 | if (s->start_timestamp.realtime > 0) |
9fb86720 LP |
2574 | fprintf(f, |
2575 | "%sStart Timestamp: %s\n", | |
63983207 | 2576 | prefix, format_timestamp(buf, sizeof(buf), s->start_timestamp.realtime)); |
9fb86720 | 2577 | |
63983207 | 2578 | if (s->exit_timestamp.realtime > 0) |
9fb86720 LP |
2579 | fprintf(f, |
2580 | "%sExit Timestamp: %s\n" | |
2581 | "%sExit Code: %s\n" | |
2582 | "%sExit Status: %i\n", | |
63983207 | 2583 | prefix, format_timestamp(buf, sizeof(buf), s->exit_timestamp.realtime), |
9fb86720 LP |
2584 | prefix, sigchld_code_to_string(s->code), |
2585 | prefix, s->status); | |
5cb5a6ff | 2586 | } |
44d8db9e | 2587 | |
9e2f7c11 | 2588 | char *exec_command_line(char **argv) { |
44d8db9e LP |
2589 | size_t k; |
2590 | char *n, *p, **a; | |
2591 | bool first = true; | |
2592 | ||
9e2f7c11 | 2593 | assert(argv); |
44d8db9e | 2594 | |
9164977d | 2595 | k = 1; |
9e2f7c11 | 2596 | STRV_FOREACH(a, argv) |
44d8db9e LP |
2597 | k += strlen(*a)+3; |
2598 | ||
2599 | if (!(n = new(char, k))) | |
2600 | return NULL; | |
2601 | ||
2602 | p = n; | |
9e2f7c11 | 2603 | STRV_FOREACH(a, argv) { |
44d8db9e LP |
2604 | |
2605 | if (!first) | |
2606 | *(p++) = ' '; | |
2607 | else | |
2608 | first = false; | |
2609 | ||
2610 | if (strpbrk(*a, WHITESPACE)) { | |
2611 | *(p++) = '\''; | |
2612 | p = stpcpy(p, *a); | |
2613 | *(p++) = '\''; | |
2614 | } else | |
2615 | p = stpcpy(p, *a); | |
2616 | ||
2617 | } | |
2618 | ||
9164977d LP |
2619 | *p = 0; |
2620 | ||
44d8db9e LP |
2621 | /* FIXME: this doesn't really handle arguments that have |
2622 | * spaces and ticks in them */ | |
2623 | ||
2624 | return n; | |
2625 | } | |
2626 | ||
2627 | void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) { | |
e1d75803 | 2628 | _cleanup_free_ char *cmd = NULL; |
4c940960 | 2629 | const char *prefix2; |
44d8db9e LP |
2630 | |
2631 | assert(c); | |
2632 | assert(f); | |
2633 | ||
4c940960 | 2634 | prefix = strempty(prefix); |
63c372cb | 2635 | prefix2 = strjoina(prefix, "\t"); |
44d8db9e | 2636 | |
9e2f7c11 | 2637 | cmd = exec_command_line(c->argv); |
44d8db9e LP |
2638 | fprintf(f, |
2639 | "%sCommand Line: %s\n", | |
2640 | prefix, cmd ? cmd : strerror(ENOMEM)); | |
2641 | ||
9fb86720 | 2642 | exec_status_dump(&c->exec_status, f, prefix2); |
44d8db9e LP |
2643 | } |
2644 | ||
2645 | void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) { | |
2646 | assert(f); | |
2647 | ||
4c940960 | 2648 | prefix = strempty(prefix); |
44d8db9e LP |
2649 | |
2650 | LIST_FOREACH(command, c, c) | |
2651 | exec_command_dump(c, f, prefix); | |
2652 | } | |
94f04347 | 2653 | |
a6a80b4f LP |
2654 | void exec_command_append_list(ExecCommand **l, ExecCommand *e) { |
2655 | ExecCommand *end; | |
2656 | ||
2657 | assert(l); | |
2658 | assert(e); | |
2659 | ||
2660 | if (*l) { | |
35b8ca3a | 2661 | /* It's kind of important, that we keep the order here */ |
71fda00f LP |
2662 | LIST_FIND_TAIL(command, *l, end); |
2663 | LIST_INSERT_AFTER(command, *l, end, e); | |
a6a80b4f LP |
2664 | } else |
2665 | *l = e; | |
2666 | } | |
2667 | ||
26fd040d LP |
2668 | int exec_command_set(ExecCommand *c, const char *path, ...) { |
2669 | va_list ap; | |
2670 | char **l, *p; | |
2671 | ||
2672 | assert(c); | |
2673 | assert(path); | |
2674 | ||
2675 | va_start(ap, path); | |
2676 | l = strv_new_ap(path, ap); | |
2677 | va_end(ap); | |
2678 | ||
2679 | if (!l) | |
2680 | return -ENOMEM; | |
2681 | ||
250a918d LP |
2682 | p = strdup(path); |
2683 | if (!p) { | |
26fd040d LP |
2684 | strv_free(l); |
2685 | return -ENOMEM; | |
2686 | } | |
2687 | ||
2688 | free(c->path); | |
2689 | c->path = p; | |
2690 | ||
2691 | strv_free(c->argv); | |
2692 | c->argv = l; | |
2693 | ||
2694 | return 0; | |
2695 | } | |
2696 | ||
86b23b07 | 2697 | int exec_command_append(ExecCommand *c, const char *path, ...) { |
e63ff941 | 2698 | _cleanup_strv_free_ char **l = NULL; |
86b23b07 | 2699 | va_list ap; |
86b23b07 JS |
2700 | int r; |
2701 | ||
2702 | assert(c); | |
2703 | assert(path); | |
2704 | ||
2705 | va_start(ap, path); | |
2706 | l = strv_new_ap(path, ap); | |
2707 | va_end(ap); | |
2708 | ||
2709 | if (!l) | |
2710 | return -ENOMEM; | |
2711 | ||
2712 | r = strv_extend_strv(&c->argv, l); | |
e63ff941 | 2713 | if (r < 0) |
86b23b07 | 2714 | return r; |
86b23b07 JS |
2715 | |
2716 | return 0; | |
2717 | } | |
2718 | ||
2719 | ||
613b411c LP |
2720 | static int exec_runtime_allocate(ExecRuntime **rt) { |
2721 | ||
2722 | if (*rt) | |
2723 | return 0; | |
2724 | ||
2725 | *rt = new0(ExecRuntime, 1); | |
f146f5e1 | 2726 | if (!*rt) |
613b411c LP |
2727 | return -ENOMEM; |
2728 | ||
2729 | (*rt)->n_ref = 1; | |
2730 | (*rt)->netns_storage_socket[0] = (*rt)->netns_storage_socket[1] = -1; | |
2731 | ||
2732 | return 0; | |
2733 | } | |
2734 | ||
2735 | int exec_runtime_make(ExecRuntime **rt, ExecContext *c, const char *id) { | |
2736 | int r; | |
2737 | ||
2738 | assert(rt); | |
2739 | assert(c); | |
2740 | assert(id); | |
2741 | ||
2742 | if (*rt) | |
2743 | return 1; | |
2744 | ||
2745 | if (!c->private_network && !c->private_tmp) | |
2746 | return 0; | |
2747 | ||
2748 | r = exec_runtime_allocate(rt); | |
2749 | if (r < 0) | |
2750 | return r; | |
2751 | ||
2752 | if (c->private_network && (*rt)->netns_storage_socket[0] < 0) { | |
2753 | if (socketpair(AF_UNIX, SOCK_DGRAM, 0, (*rt)->netns_storage_socket) < 0) | |
2754 | return -errno; | |
2755 | } | |
2756 | ||
2757 | if (c->private_tmp && !(*rt)->tmp_dir) { | |
2758 | r = setup_tmp_dirs(id, &(*rt)->tmp_dir, &(*rt)->var_tmp_dir); | |
2759 | if (r < 0) | |
2760 | return r; | |
2761 | } | |
2762 | ||
2763 | return 1; | |
2764 | } | |
2765 | ||
2766 | ExecRuntime *exec_runtime_ref(ExecRuntime *r) { | |
2767 | assert(r); | |
2768 | assert(r->n_ref > 0); | |
2769 | ||
2770 | r->n_ref++; | |
2771 | return r; | |
2772 | } | |
2773 | ||
2774 | ExecRuntime *exec_runtime_unref(ExecRuntime *r) { | |
2775 | ||
2776 | if (!r) | |
2777 | return NULL; | |
2778 | ||
2779 | assert(r->n_ref > 0); | |
2780 | ||
2781 | r->n_ref--; | |
f2341e0a LP |
2782 | if (r->n_ref > 0) |
2783 | return NULL; | |
2784 | ||
2785 | free(r->tmp_dir); | |
2786 | free(r->var_tmp_dir); | |
2787 | safe_close_pair(r->netns_storage_socket); | |
2788 | free(r); | |
613b411c LP |
2789 | |
2790 | return NULL; | |
2791 | } | |
2792 | ||
f2341e0a | 2793 | int exec_runtime_serialize(Unit *u, ExecRuntime *rt, FILE *f, FDSet *fds) { |
613b411c LP |
2794 | assert(u); |
2795 | assert(f); | |
2796 | assert(fds); | |
2797 | ||
2798 | if (!rt) | |
2799 | return 0; | |
2800 | ||
2801 | if (rt->tmp_dir) | |
2802 | unit_serialize_item(u, f, "tmp-dir", rt->tmp_dir); | |
2803 | ||
2804 | if (rt->var_tmp_dir) | |
2805 | unit_serialize_item(u, f, "var-tmp-dir", rt->var_tmp_dir); | |
2806 | ||
2807 | if (rt->netns_storage_socket[0] >= 0) { | |
2808 | int copy; | |
2809 | ||
2810 | copy = fdset_put_dup(fds, rt->netns_storage_socket[0]); | |
2811 | if (copy < 0) | |
2812 | return copy; | |
2813 | ||
2814 | unit_serialize_item_format(u, f, "netns-socket-0", "%i", copy); | |
2815 | } | |
2816 | ||
2817 | if (rt->netns_storage_socket[1] >= 0) { | |
2818 | int copy; | |
2819 | ||
2820 | copy = fdset_put_dup(fds, rt->netns_storage_socket[1]); | |
2821 | if (copy < 0) | |
2822 | return copy; | |
2823 | ||
2824 | unit_serialize_item_format(u, f, "netns-socket-1", "%i", copy); | |
2825 | } | |
2826 | ||
2827 | return 0; | |
2828 | } | |
2829 | ||
f2341e0a | 2830 | int exec_runtime_deserialize_item(Unit *u, ExecRuntime **rt, const char *key, const char *value, FDSet *fds) { |
613b411c LP |
2831 | int r; |
2832 | ||
2833 | assert(rt); | |
2834 | assert(key); | |
2835 | assert(value); | |
2836 | ||
2837 | if (streq(key, "tmp-dir")) { | |
2838 | char *copy; | |
2839 | ||
2840 | r = exec_runtime_allocate(rt); | |
2841 | if (r < 0) | |
f2341e0a | 2842 | return log_oom(); |
613b411c LP |
2843 | |
2844 | copy = strdup(value); | |
2845 | if (!copy) | |
2846 | return log_oom(); | |
2847 | ||
2848 | free((*rt)->tmp_dir); | |
2849 | (*rt)->tmp_dir = copy; | |
2850 | ||
2851 | } else if (streq(key, "var-tmp-dir")) { | |
2852 | char *copy; | |
2853 | ||
2854 | r = exec_runtime_allocate(rt); | |
2855 | if (r < 0) | |
f2341e0a | 2856 | return log_oom(); |
613b411c LP |
2857 | |
2858 | copy = strdup(value); | |
2859 | if (!copy) | |
2860 | return log_oom(); | |
2861 | ||
2862 | free((*rt)->var_tmp_dir); | |
2863 | (*rt)->var_tmp_dir = copy; | |
2864 | ||
2865 | } else if (streq(key, "netns-socket-0")) { | |
2866 | int fd; | |
2867 | ||
2868 | r = exec_runtime_allocate(rt); | |
2869 | if (r < 0) | |
f2341e0a | 2870 | return log_oom(); |
613b411c LP |
2871 | |
2872 | if (safe_atoi(value, &fd) < 0 || !fdset_contains(fds, fd)) | |
f2341e0a | 2873 | log_unit_debug(u, "Failed to parse netns socket value: %s", value); |
613b411c | 2874 | else { |
03e334a1 | 2875 | safe_close((*rt)->netns_storage_socket[0]); |
613b411c LP |
2876 | (*rt)->netns_storage_socket[0] = fdset_remove(fds, fd); |
2877 | } | |
2878 | } else if (streq(key, "netns-socket-1")) { | |
2879 | int fd; | |
2880 | ||
2881 | r = exec_runtime_allocate(rt); | |
2882 | if (r < 0) | |
f2341e0a | 2883 | return log_oom(); |
613b411c LP |
2884 | |
2885 | if (safe_atoi(value, &fd) < 0 || !fdset_contains(fds, fd)) | |
f2341e0a | 2886 | log_unit_debug(u, "Failed to parse netns socket value: %s", value); |
613b411c | 2887 | else { |
03e334a1 | 2888 | safe_close((*rt)->netns_storage_socket[1]); |
613b411c LP |
2889 | (*rt)->netns_storage_socket[1] = fdset_remove(fds, fd); |
2890 | } | |
2891 | } else | |
2892 | return 0; | |
2893 | ||
2894 | return 1; | |
2895 | } | |
2896 | ||
2897 | static void *remove_tmpdir_thread(void *p) { | |
2898 | _cleanup_free_ char *path = p; | |
2899 | ||
c6878637 | 2900 | (void) rm_rf(path, REMOVE_ROOT|REMOVE_PHYSICAL); |
613b411c LP |
2901 | return NULL; |
2902 | } | |
2903 | ||
2904 | void exec_runtime_destroy(ExecRuntime *rt) { | |
98b47d54 LP |
2905 | int r; |
2906 | ||
613b411c LP |
2907 | if (!rt) |
2908 | return; | |
2909 | ||
2910 | /* If there are multiple users of this, let's leave the stuff around */ | |
2911 | if (rt->n_ref > 1) | |
2912 | return; | |
2913 | ||
2914 | if (rt->tmp_dir) { | |
2915 | log_debug("Spawning thread to nuke %s", rt->tmp_dir); | |
98b47d54 LP |
2916 | |
2917 | r = asynchronous_job(remove_tmpdir_thread, rt->tmp_dir); | |
2918 | if (r < 0) { | |
da927ba9 | 2919 | log_warning_errno(r, "Failed to nuke %s: %m", rt->tmp_dir); |
98b47d54 LP |
2920 | free(rt->tmp_dir); |
2921 | } | |
2922 | ||
613b411c LP |
2923 | rt->tmp_dir = NULL; |
2924 | } | |
2925 | ||
2926 | if (rt->var_tmp_dir) { | |
2927 | log_debug("Spawning thread to nuke %s", rt->var_tmp_dir); | |
98b47d54 LP |
2928 | |
2929 | r = asynchronous_job(remove_tmpdir_thread, rt->var_tmp_dir); | |
2930 | if (r < 0) { | |
da927ba9 | 2931 | log_warning_errno(r, "Failed to nuke %s: %m", rt->var_tmp_dir); |
98b47d54 LP |
2932 | free(rt->var_tmp_dir); |
2933 | } | |
2934 | ||
613b411c LP |
2935 | rt->var_tmp_dir = NULL; |
2936 | } | |
2937 | ||
3d94f76c | 2938 | safe_close_pair(rt->netns_storage_socket); |
613b411c LP |
2939 | } |
2940 | ||
80876c20 LP |
2941 | static const char* const exec_input_table[_EXEC_INPUT_MAX] = { |
2942 | [EXEC_INPUT_NULL] = "null", | |
2943 | [EXEC_INPUT_TTY] = "tty", | |
2944 | [EXEC_INPUT_TTY_FORCE] = "tty-force", | |
4f2d528d LP |
2945 | [EXEC_INPUT_TTY_FAIL] = "tty-fail", |
2946 | [EXEC_INPUT_SOCKET] = "socket" | |
80876c20 LP |
2947 | }; |
2948 | ||
8a0867d6 LP |
2949 | DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput); |
2950 | ||
94f04347 | 2951 | static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = { |
80876c20 | 2952 | [EXEC_OUTPUT_INHERIT] = "inherit", |
94f04347 | 2953 | [EXEC_OUTPUT_NULL] = "null", |
80876c20 | 2954 | [EXEC_OUTPUT_TTY] = "tty", |
94f04347 | 2955 | [EXEC_OUTPUT_SYSLOG] = "syslog", |
28dbc1e8 | 2956 | [EXEC_OUTPUT_SYSLOG_AND_CONSOLE] = "syslog+console", |
9a6bca7a | 2957 | [EXEC_OUTPUT_KMSG] = "kmsg", |
28dbc1e8 | 2958 | [EXEC_OUTPUT_KMSG_AND_CONSOLE] = "kmsg+console", |
706343f4 LP |
2959 | [EXEC_OUTPUT_JOURNAL] = "journal", |
2960 | [EXEC_OUTPUT_JOURNAL_AND_CONSOLE] = "journal+console", | |
4f2d528d | 2961 | [EXEC_OUTPUT_SOCKET] = "socket" |
94f04347 LP |
2962 | }; |
2963 | ||
2964 | DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput); |