]>
Commit | Line | Data |
---|---|---|
5cb5a6ff LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
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 | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
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 | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
5cb5a6ff | 22 | #include <assert.h> |
034c6ed7 LP |
23 | #include <dirent.h> |
24 | #include <errno.h> | |
25 | #include <fcntl.h> | |
26 | #include <unistd.h> | |
44d8db9e | 27 | #include <string.h> |
309bff19 | 28 | #include <signal.h> |
071830ff LP |
29 | #include <sys/socket.h> |
30 | #include <sys/un.h> | |
94f04347 | 31 | #include <sys/prctl.h> |
66ee3769 | 32 | #include <linux/sched.h> |
451a074f LP |
33 | #include <sys/types.h> |
34 | #include <sys/stat.h> | |
81a2b7ce LP |
35 | #include <grp.h> |
36 | #include <pwd.h> | |
5cb5a6ff LP |
37 | |
38 | #include "execute.h" | |
39 | #include "strv.h" | |
40 | #include "macro.h" | |
41 | #include "util.h" | |
acbb0225 | 42 | #include "log.h" |
9eba9da4 | 43 | #include "ioprio.h" |
94f04347 | 44 | #include "securebits.h" |
8e274523 | 45 | #include "cgroup.h" |
5cb5a6ff | 46 | |
034c6ed7 LP |
47 | static int shift_fds(int fds[], unsigned n_fds) { |
48 | int start, restart_from; | |
49 | ||
50 | if (n_fds <= 0) | |
51 | return 0; | |
52 | ||
a0d40ac5 LP |
53 | /* Modifies the fds array! (sorts it) */ |
54 | ||
034c6ed7 LP |
55 | assert(fds); |
56 | ||
57 | start = 0; | |
58 | for (;;) { | |
59 | int i; | |
60 | ||
61 | restart_from = -1; | |
62 | ||
63 | for (i = start; i < (int) n_fds; i++) { | |
64 | int nfd; | |
65 | ||
66 | /* Already at right index? */ | |
67 | if (fds[i] == i+3) | |
68 | continue; | |
69 | ||
70 | if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0) | |
71 | return -errno; | |
72 | ||
2da3263a | 73 | assert_se(close_nointr(fds[i]) == 0); |
034c6ed7 LP |
74 | fds[i] = nfd; |
75 | ||
76 | /* Hmm, the fd we wanted isn't free? Then | |
77 | * let's remember that and try again from here*/ | |
78 | if (nfd != i+3 && restart_from < 0) | |
79 | restart_from = i; | |
80 | } | |
81 | ||
82 | if (restart_from < 0) | |
83 | break; | |
84 | ||
85 | start = restart_from; | |
86 | } | |
87 | ||
88 | return 0; | |
89 | } | |
90 | ||
451a074f | 91 | static int flags_fds(int fds[], unsigned n_fds, bool nonblock) { |
47a71eed | 92 | unsigned i; |
e2c76839 | 93 | int r; |
47a71eed LP |
94 | |
95 | if (n_fds <= 0) | |
96 | return 0; | |
97 | ||
98 | assert(fds); | |
99 | ||
451a074f | 100 | /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags */ |
47a71eed LP |
101 | |
102 | for (i = 0; i < n_fds; i++) { | |
47a71eed | 103 | |
e2c76839 LP |
104 | if ((r = fd_nonblock(fds[i], nonblock)) < 0) |
105 | return r; | |
47a71eed | 106 | |
451a074f LP |
107 | /* We unconditionally drop FD_CLOEXEC from the fds, |
108 | * since after all we want to pass these fds to our | |
109 | * children */ | |
47a71eed | 110 | |
e2c76839 LP |
111 | if ((r = fd_cloexec(fds[i], false)) < 0) |
112 | return r; | |
47a71eed LP |
113 | } |
114 | ||
115 | return 0; | |
116 | } | |
117 | ||
071830ff LP |
118 | static int replace_null_fd(int fd, int flags) { |
119 | int nfd; | |
120 | assert(fd >= 0); | |
121 | ||
122 | close_nointr(fd); | |
123 | ||
124 | if ((nfd = open("/dev/null", flags|O_NOCTTY)) < 0) | |
125 | return -errno; | |
126 | ||
127 | if (nfd != fd) { | |
128 | close_nointr_nofail(nfd); | |
129 | return -EIO; | |
130 | } | |
131 | ||
132 | return 0; | |
133 | } | |
134 | ||
135 | static int setup_output(const ExecContext *context, const char *ident) { | |
136 | int r; | |
137 | ||
138 | assert(context); | |
139 | ||
140 | switch (context->output) { | |
141 | ||
94f04347 | 142 | case EXEC_OUTPUT_CONSOLE: |
071830ff LP |
143 | return 0; |
144 | ||
94f04347 | 145 | case EXEC_OUTPUT_NULL: |
071830ff | 146 | |
94f04347 | 147 | if ((r = replace_null_fd(STDOUT_FILENO, O_WRONLY)) < 0 || |
071830ff LP |
148 | (r = replace_null_fd(STDERR_FILENO, O_WRONLY)) < 0) |
149 | return r; | |
150 | ||
151 | return 0; | |
152 | ||
94f04347 LP |
153 | case EXEC_OUTPUT_KERNEL: |
154 | case EXEC_OUTPUT_SYSLOG: { | |
071830ff LP |
155 | |
156 | int fd; | |
157 | union { | |
158 | struct sockaddr sa; | |
159 | struct sockaddr_un un; | |
160 | } sa; | |
161 | ||
071830ff LP |
162 | close_nointr(STDOUT_FILENO); |
163 | close_nointr(STDERR_FILENO); | |
164 | ||
165 | if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) | |
166 | return -errno; | |
167 | ||
168 | if (fd != STDOUT_FILENO) { | |
169 | close_nointr_nofail(fd); | |
170 | return -EIO; | |
171 | } | |
172 | ||
173 | zero(sa); | |
174 | sa.sa.sa_family = AF_UNIX; | |
175 | strncpy(sa.un.sun_path+1, LOGGER_SOCKET, sizeof(sa.un.sun_path)-1); | |
176 | ||
177 | if (connect(fd, &sa.sa, sizeof(sa)) < 0) { | |
178 | close_nointr_nofail(fd); | |
179 | return -errno; | |
180 | } | |
181 | ||
182 | if (shutdown(fd, SHUT_RD) < 0) { | |
183 | close_nointr_nofail(fd); | |
184 | return -errno; | |
185 | } | |
186 | ||
187 | if ((fd = dup(fd)) < 0) { | |
188 | close_nointr_nofail(fd); | |
189 | return -errno; | |
190 | } | |
191 | ||
192 | if (fd != STDERR_FILENO) { | |
193 | close_nointr_nofail(fd); | |
194 | return -EIO; | |
195 | } | |
196 | ||
197 | /* We speak a very simple protocol between log server | |
198 | * and client: one line for the log destination (kmsg | |
199 | * or syslog), followed by the priority field, | |
200 | * followed by the process name. Since we replaced | |
201 | * stdin/stderr we simple use stdio to write to | |
202 | * it. Note that we use stderr, to minimize buffer | |
203 | * flushing issues. */ | |
204 | ||
205 | fprintf(stderr, | |
206 | "%s\n" | |
207 | "%i\n" | |
208 | "%s\n", | |
94f04347 | 209 | context->output == EXEC_OUTPUT_KERNEL ? "kmsg" : "syslog", |
071830ff LP |
210 | context->syslog_priority, |
211 | context->syslog_identifier ? context->syslog_identifier : ident); | |
212 | ||
213 | return 0; | |
214 | } | |
94f04347 LP |
215 | |
216 | default: | |
217 | assert_not_reached("Unknown output type"); | |
071830ff | 218 | } |
94f04347 LP |
219 | } |
220 | ||
47be870b | 221 | static int setup_input(const ExecContext *context) { |
94f04347 LP |
222 | int r; |
223 | ||
224 | assert(context); | |
071830ff | 225 | |
94f04347 LP |
226 | switch (context->input) { |
227 | ||
228 | case EXEC_INPUT_CONSOLE: | |
229 | return 0; | |
230 | ||
231 | case EXEC_INPUT_NULL: | |
232 | if ((r = replace_null_fd(STDIN_FILENO, O_RDONLY)) < 0) | |
233 | return r; | |
234 | ||
235 | return 0; | |
236 | ||
237 | default: | |
238 | assert_not_reached("Unknown input type"); | |
239 | } | |
071830ff LP |
240 | } |
241 | ||
81a2b7ce LP |
242 | static int get_group_creds(const char *groupname, gid_t *gid) { |
243 | struct group *g; | |
244 | unsigned long lu; | |
245 | ||
246 | assert(groupname); | |
247 | assert(gid); | |
248 | ||
249 | /* We enforce some special rules for gid=0: in order to avoid | |
250 | * NSS lookups for root we hardcode its data. */ | |
251 | ||
252 | if (streq(groupname, "root") || streq(groupname, "0")) { | |
253 | *gid = 0; | |
254 | return 0; | |
255 | } | |
256 | ||
257 | if (safe_atolu(groupname, &lu) >= 0) { | |
258 | errno = 0; | |
259 | g = getgrgid((gid_t) lu); | |
260 | } else { | |
261 | errno = 0; | |
262 | g = getgrnam(groupname); | |
263 | } | |
264 | ||
265 | if (!g) | |
266 | return errno != 0 ? -errno : -ESRCH; | |
267 | ||
268 | *gid = g->gr_gid; | |
269 | return 0; | |
270 | } | |
271 | ||
272 | static int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home) { | |
273 | struct passwd *p; | |
274 | unsigned long lu; | |
275 | ||
276 | assert(username); | |
277 | assert(*username); | |
278 | assert(uid); | |
279 | assert(gid); | |
280 | assert(home); | |
281 | ||
282 | /* We enforce some special rules for uid=0: in order to avoid | |
283 | * NSS lookups for root we hardcode its data. */ | |
284 | ||
285 | if (streq(*username, "root") || streq(*username, "0")) { | |
286 | *username = "root"; | |
287 | *uid = 0; | |
288 | *gid = 0; | |
289 | *home = "/root"; | |
290 | return 0; | |
291 | } | |
292 | ||
293 | if (safe_atolu(*username, &lu) >= 0) { | |
294 | errno = 0; | |
295 | p = getpwuid((uid_t) lu); | |
296 | ||
297 | /* If there are multiple users with the same id, make | |
298 | * sure to leave $USER to the configured value instead | |
299 | * of the first occurence in the database. However if | |
300 | * the uid was configured by a numeric uid, then let's | |
301 | * pick the real username from /etc/passwd. */ | |
302 | if (*username && p) | |
303 | *username = p->pw_name; | |
304 | } else { | |
305 | errno = 0; | |
306 | p = getpwnam(*username); | |
307 | } | |
308 | ||
309 | if (!p) | |
310 | return errno != 0 ? -errno : -ESRCH; | |
311 | ||
312 | *uid = p->pw_uid; | |
313 | *gid = p->pw_gid; | |
314 | *home = p->pw_dir; | |
315 | return 0; | |
316 | } | |
317 | ||
318 | static int enforce_groups(const ExecContext *context, const char *username, gid_t gid) { | |
319 | bool keep_groups = false; | |
320 | int r; | |
321 | ||
322 | assert(context); | |
323 | ||
324 | /* Lookup and ser GID and supplementary group list. Here too | |
325 | * we avoid NSS lookups for gid=0. */ | |
326 | ||
327 | if (context->group || username) { | |
328 | ||
329 | if (context->group) | |
330 | if ((r = get_group_creds(context->group, &gid)) < 0) | |
331 | return r; | |
332 | ||
333 | /* First step, initialize groups from /etc/groups */ | |
334 | if (username && gid != 0) { | |
335 | if (initgroups(username, gid) < 0) | |
336 | return -errno; | |
337 | ||
338 | keep_groups = true; | |
339 | } | |
340 | ||
341 | /* Second step, set our gids */ | |
342 | if (setresgid(gid, gid, gid) < 0) | |
343 | return -errno; | |
344 | } | |
345 | ||
346 | if (context->supplementary_groups) { | |
347 | int ngroups_max, k; | |
348 | gid_t *gids; | |
349 | char **i; | |
350 | ||
351 | /* Final step, initialize any manually set supplementary groups */ | |
352 | ngroups_max = (int) sysconf(_SC_NGROUPS_MAX); | |
353 | ||
354 | if (!(gids = new(gid_t, ngroups_max))) | |
355 | return -ENOMEM; | |
356 | ||
357 | if (keep_groups) { | |
358 | if ((k = getgroups(ngroups_max, gids)) < 0) { | |
359 | free(gids); | |
360 | return -errno; | |
361 | } | |
362 | } else | |
363 | k = 0; | |
364 | ||
365 | STRV_FOREACH(i, context->supplementary_groups) { | |
366 | ||
367 | if (k >= ngroups_max) { | |
368 | free(gids); | |
369 | return -E2BIG; | |
370 | } | |
371 | ||
372 | if ((r = get_group_creds(*i, gids+k)) < 0) { | |
373 | free(gids); | |
374 | return r; | |
375 | } | |
376 | ||
377 | k++; | |
378 | } | |
379 | ||
380 | if (setgroups(k, gids) < 0) { | |
381 | free(gids); | |
382 | return -errno; | |
383 | } | |
384 | ||
385 | free(gids); | |
386 | } | |
387 | ||
388 | return 0; | |
389 | } | |
390 | ||
391 | static int enforce_user(const ExecContext *context, uid_t uid) { | |
392 | int r; | |
393 | assert(context); | |
394 | ||
395 | /* Sets (but doesn't lookup) the uid and make sure we keep the | |
396 | * capabilities while doing so. */ | |
397 | ||
398 | if (context->capabilities) { | |
399 | cap_t d; | |
400 | static const cap_value_t bits[] = { | |
401 | CAP_SETUID, /* Necessary so that we can run setresuid() below */ | |
402 | CAP_SETPCAP /* Necessary so that we can set PR_SET_SECUREBITS later on */ | |
403 | }; | |
404 | ||
405 | /* First step: If we need to keep capabilities but | |
406 | * drop privileges we need to make sure we keep our | |
407 | * caps, whiel we drop priviliges. */ | |
693ced48 LP |
408 | if (uid != 0) { |
409 | int sb = context->secure_bits|SECURE_KEEP_CAPS; | |
410 | ||
411 | if (prctl(PR_GET_SECUREBITS) != sb) | |
412 | if (prctl(PR_SET_SECUREBITS, sb) < 0) | |
413 | return -errno; | |
414 | } | |
81a2b7ce LP |
415 | |
416 | /* Second step: set the capabilites. This will reduce | |
417 | * the capabilities to the minimum we need. */ | |
418 | ||
419 | if (!(d = cap_dup(context->capabilities))) | |
420 | return -errno; | |
421 | ||
422 | if (cap_set_flag(d, CAP_EFFECTIVE, ELEMENTSOF(bits), bits, CAP_SET) < 0 || | |
423 | cap_set_flag(d, CAP_PERMITTED, ELEMENTSOF(bits), bits, CAP_SET) < 0) { | |
424 | r = -errno; | |
425 | cap_free(d); | |
426 | return r; | |
427 | } | |
428 | ||
429 | if (cap_set_proc(d) < 0) { | |
430 | r = -errno; | |
431 | cap_free(d); | |
432 | return r; | |
433 | } | |
434 | ||
435 | cap_free(d); | |
436 | } | |
437 | ||
438 | /* Third step: actually set the uids */ | |
439 | if (setresuid(uid, uid, uid) < 0) | |
440 | return -errno; | |
441 | ||
442 | /* At this point we should have all necessary capabilities but | |
443 | are otherwise a normal user. However, the caps might got | |
444 | corrupted due to the setresuid() so we need clean them up | |
445 | later. This is done outside of this call. */ | |
446 | ||
447 | return 0; | |
448 | } | |
449 | ||
9fb86720 | 450 | int exec_spawn(ExecCommand *command, |
81a2b7ce LP |
451 | const ExecContext *context, |
452 | int *fds, unsigned n_fds, | |
453 | bool apply_permissions, | |
454 | bool apply_chroot, | |
8e274523 | 455 | CGroupBonding *cgroup_bondings, |
81a2b7ce LP |
456 | pid_t *ret) { |
457 | ||
034c6ed7 | 458 | pid_t pid; |
8e274523 | 459 | int r; |
1a63a750 | 460 | char *line; |
034c6ed7 | 461 | |
5cb5a6ff LP |
462 | assert(command); |
463 | assert(context); | |
464 | assert(ret); | |
034c6ed7 LP |
465 | assert(fds || n_fds <= 0); |
466 | ||
1a63a750 LP |
467 | if (!(line = exec_command_line(command))) |
468 | return -ENOMEM; | |
469 | ||
470 | log_debug("About to execute: %s", line); | |
471 | free(line); | |
acbb0225 | 472 | |
8e274523 LP |
473 | if (cgroup_bondings) |
474 | if ((r = cgroup_bonding_realize_list(cgroup_bondings))) | |
475 | return r; | |
476 | ||
034c6ed7 LP |
477 | if ((pid = fork()) < 0) |
478 | return -errno; | |
479 | ||
480 | if (pid == 0) { | |
8e274523 | 481 | int i; |
309bff19 | 482 | sigset_t ss; |
81a2b7ce LP |
483 | const char *username = NULL, *home = NULL; |
484 | uid_t uid = (uid_t) -1; | |
485 | gid_t gid = (gid_t) -1; | |
486 | char **our_env = NULL, **final_env = NULL; | |
487 | unsigned n_env = 0; | |
309bff19 | 488 | |
034c6ed7 | 489 | /* child */ |
5cb5a6ff | 490 | |
309bff19 LP |
491 | if (sigemptyset(&ss) < 0 || |
492 | sigprocmask(SIG_SETMASK, &ss, NULL) < 0) { | |
493 | r = EXIT_SIGNAL_MASK; | |
494 | goto fail; | |
495 | } | |
496 | ||
ee2b4894 LP |
497 | if (context->new_session) { |
498 | if (setsid() < 0) { | |
499 | r = EXIT_SETSID; | |
500 | goto fail; | |
501 | } | |
502 | } else { | |
503 | if (setpgid(0, 0) < 0) { | |
504 | r = EXIT_PGID; | |
505 | goto fail; | |
506 | } | |
034c6ed7 LP |
507 | } |
508 | ||
9eba9da4 LP |
509 | umask(context->umask); |
510 | ||
94f04347 LP |
511 | if (setup_input(context) < 0) { |
512 | r = EXIT_INPUT; | |
513 | goto fail; | |
514 | } | |
515 | ||
071830ff LP |
516 | if (setup_output(context, file_name_from_path(command->path)) < 0) { |
517 | r = EXIT_OUTPUT; | |
518 | goto fail; | |
519 | } | |
520 | ||
8e274523 LP |
521 | if (cgroup_bondings) |
522 | if ((r = cgroup_bonding_install_list(cgroup_bondings, 0)) < 0) { | |
523 | r = EXIT_CGROUP; | |
524 | goto fail; | |
525 | } | |
526 | ||
fb33a393 LP |
527 | if (context->oom_adjust_set) { |
528 | char t[16]; | |
034c6ed7 | 529 | |
fb33a393 LP |
530 | snprintf(t, sizeof(t), "%i", context->oom_adjust); |
531 | char_array_0(t); | |
034c6ed7 | 532 | |
fb33a393 LP |
533 | if (write_one_line_file("/proc/self/oom_adj", t) < 0) { |
534 | r = EXIT_OOM_ADJUST; | |
535 | goto fail; | |
536 | } | |
034c6ed7 LP |
537 | } |
538 | ||
fb33a393 LP |
539 | if (context->nice_set) |
540 | if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) { | |
541 | r = EXIT_NICE; | |
542 | goto fail; | |
543 | } | |
544 | ||
94f04347 LP |
545 | if (context->cpu_sched_set) { |
546 | struct sched_param param; | |
547 | ||
548 | zero(param); | |
549 | param.sched_priority = context->cpu_sched_priority; | |
550 | ||
38b48754 LP |
551 | if (sched_setscheduler(0, context->cpu_sched_policy | |
552 | (context->cpu_sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0), ¶m) < 0) { | |
94f04347 LP |
553 | r = EXIT_SETSCHEDULER; |
554 | goto fail; | |
555 | } | |
556 | } | |
557 | ||
558 | if (context->cpu_affinity_set) | |
559 | if (sched_setaffinity(0, sizeof(context->cpu_affinity), &context->cpu_affinity) < 0) { | |
560 | r = EXIT_CPUAFFINITY; | |
561 | goto fail; | |
562 | } | |
563 | ||
9eba9da4 LP |
564 | if (context->ioprio_set) |
565 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) { | |
566 | r = EXIT_IOPRIO; | |
567 | goto fail; | |
568 | } | |
569 | ||
94f04347 LP |
570 | if (context->timer_slack_ns_set) |
571 | if (prctl(PR_SET_TIMERSLACK, context->timer_slack_ns_set) < 0) { | |
572 | r = EXIT_TIMERSLACK; | |
573 | goto fail; | |
574 | } | |
575 | ||
81a2b7ce LP |
576 | if (context->user) { |
577 | username = context->user; | |
578 | if (get_user_creds(&username, &uid, &gid, &home) < 0) { | |
579 | r = EXIT_USER; | |
580 | goto fail; | |
581 | } | |
582 | } | |
583 | ||
584 | if (apply_permissions) | |
585 | if (enforce_groups(context, username, uid) < 0) { | |
586 | r = EXIT_GROUP; | |
587 | goto fail; | |
588 | } | |
589 | ||
590 | if (apply_chroot) { | |
591 | if (context->root_directory) | |
592 | if (chroot(context->root_directory) < 0) { | |
593 | r = EXIT_CHROOT; | |
594 | goto fail; | |
595 | } | |
596 | ||
597 | if (chdir(context->working_directory ? context->working_directory : "/") < 0) { | |
598 | r = EXIT_CHDIR; | |
599 | goto fail; | |
600 | } | |
601 | } else { | |
602 | ||
603 | char *d; | |
604 | ||
605 | if (asprintf(&d, "%s/%s", | |
606 | context->root_directory ? context->root_directory : "", | |
607 | context->working_directory ? context->working_directory : "") < 0) { | |
608 | r = EXIT_MEMORY; | |
609 | goto fail; | |
610 | } | |
611 | ||
612 | if (chdir(d) < 0) { | |
613 | free(d); | |
614 | r = EXIT_CHDIR; | |
615 | goto fail; | |
616 | } | |
617 | ||
618 | free(d); | |
619 | } | |
620 | ||
a0d40ac5 | 621 | if (close_all_fds(fds, n_fds) < 0 || |
47a71eed | 622 | shift_fds(fds, n_fds) < 0 || |
451a074f | 623 | flags_fds(fds, n_fds, context->non_blocking) < 0) { |
034c6ed7 LP |
624 | r = EXIT_FDS; |
625 | goto fail; | |
626 | } | |
627 | ||
81a2b7ce | 628 | if (apply_permissions) { |
034c6ed7 | 629 | |
81a2b7ce LP |
630 | for (i = 0; i < RLIMIT_NLIMITS; i++) { |
631 | if (!context->rlimit[i]) | |
632 | continue; | |
633 | ||
634 | if (setrlimit(i, context->rlimit[i]) < 0) { | |
635 | r = EXIT_LIMITS; | |
636 | goto fail; | |
637 | } | |
034c6ed7 | 638 | } |
034c6ed7 | 639 | |
81a2b7ce LP |
640 | if (context->user) |
641 | if (enforce_user(context, uid) < 0) { | |
642 | r = EXIT_USER; | |
643 | goto fail; | |
644 | } | |
645 | ||
693ced48 LP |
646 | /* PR_GET_SECUREBITS is not priviliged, while |
647 | * PR_SET_SECUREBITS is. So to suppress | |
648 | * potential EPERMs we'll try not to call | |
649 | * PR_SET_SECUREBITS unless necessary. */ | |
650 | if (prctl(PR_GET_SECUREBITS) != context->secure_bits) | |
651 | if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) { | |
652 | r = EXIT_SECUREBITS; | |
653 | goto fail; | |
654 | } | |
81a2b7ce LP |
655 | |
656 | if (context->capabilities) | |
657 | if (cap_set_proc(context->capabilities) < 0) { | |
658 | r = EXIT_CAPABILITIES; | |
659 | goto fail; | |
660 | } | |
94f04347 LP |
661 | } |
662 | ||
81a2b7ce LP |
663 | if (!(our_env = new0(char*, 6))) { |
664 | r = EXIT_MEMORY; | |
665 | goto fail; | |
666 | } | |
034c6ed7 | 667 | |
81a2b7ce LP |
668 | if (n_fds > 0) |
669 | if (asprintf(our_env + n_env++, "LISTEN_PID=%llu", (unsigned long long) getpid()) < 0 || | |
670 | asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0) { | |
671 | r = EXIT_MEMORY; | |
672 | goto fail; | |
673 | } | |
034c6ed7 | 674 | |
81a2b7ce LP |
675 | if (home) |
676 | if (asprintf(our_env + n_env++, "HOME=%s", home) < 0) { | |
677 | r = EXIT_MEMORY; | |
678 | goto fail; | |
679 | } | |
034c6ed7 | 680 | |
81a2b7ce LP |
681 | if (username) |
682 | if (asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 || | |
683 | asprintf(our_env + n_env++, "USER=%s", username) < 0) { | |
684 | r = EXIT_MEMORY; | |
685 | goto fail; | |
686 | } | |
034c6ed7 | 687 | |
81a2b7ce LP |
688 | if (!(final_env = strv_env_merge(environ, our_env, context->environment, NULL))) { |
689 | r = EXIT_MEMORY; | |
690 | goto fail; | |
691 | } | |
034c6ed7 | 692 | |
81a2b7ce | 693 | execve(command->path, command->argv, final_env); |
034c6ed7 LP |
694 | r = EXIT_EXEC; |
695 | ||
696 | fail: | |
81a2b7ce LP |
697 | strv_free(our_env); |
698 | strv_free(final_env); | |
699 | ||
034c6ed7 LP |
700 | _exit(r); |
701 | } | |
702 | ||
2da3263a | 703 | |
81a2b7ce | 704 | log_debug("Forked %s as %llu", command->path, (unsigned long long) pid); |
2da3263a | 705 | |
9fb86720 LP |
706 | command->exec_status.pid = pid; |
707 | command->exec_status.start_timestamp = now(CLOCK_REALTIME); | |
708 | ||
034c6ed7 | 709 | *ret = pid; |
5cb5a6ff LP |
710 | return 0; |
711 | } | |
712 | ||
034c6ed7 LP |
713 | void exec_context_init(ExecContext *c) { |
714 | assert(c); | |
715 | ||
716 | c->umask = 0002; | |
034c6ed7 | 717 | c->oom_adjust = 0; |
9eba9da4 | 718 | c->oom_adjust_set = false; |
034c6ed7 | 719 | c->nice = 0; |
9eba9da4 LP |
720 | c->nice_set = false; |
721 | c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0); | |
722 | c->ioprio_set = false; | |
94f04347 LP |
723 | c->cpu_sched_policy = SCHED_OTHER; |
724 | c->cpu_sched_priority = 0; | |
725 | c->cpu_sched_set = false; | |
726 | CPU_ZERO(&c->cpu_affinity); | |
727 | c->cpu_affinity_set = false; | |
ee2b4894 LP |
728 | c->timer_slack_ns = 0; |
729 | c->timer_slack_ns_set = false; | |
730 | ||
731 | c->cpu_sched_reset_on_fork = false; | |
732 | c->non_blocking = false; | |
733 | c->new_session = false; | |
071830ff | 734 | |
94f04347 | 735 | c->input = 0; |
071830ff LP |
736 | c->output = 0; |
737 | c->syslog_priority = LOG_DAEMON|LOG_INFO; | |
94f04347 LP |
738 | |
739 | c->secure_bits = 0; | |
740 | c->capability_bounding_set_drop = 0; | |
034c6ed7 LP |
741 | } |
742 | ||
743 | void exec_context_done(ExecContext *c) { | |
5cb5a6ff LP |
744 | unsigned l; |
745 | ||
746 | assert(c); | |
747 | ||
748 | strv_free(c->environment); | |
034c6ed7 | 749 | c->environment = NULL; |
5cb5a6ff | 750 | |
034c6ed7 | 751 | for (l = 0; l < ELEMENTSOF(c->rlimit); l++) { |
5cb5a6ff | 752 | free(c->rlimit[l]); |
034c6ed7 LP |
753 | c->rlimit[l] = NULL; |
754 | } | |
755 | ||
9eba9da4 LP |
756 | free(c->working_directory); |
757 | c->working_directory = NULL; | |
758 | free(c->root_directory); | |
759 | c->root_directory = NULL; | |
5cb5a6ff | 760 | |
071830ff LP |
761 | free(c->syslog_identifier); |
762 | c->syslog_identifier = NULL; | |
763 | ||
5cb5a6ff | 764 | free(c->user); |
034c6ed7 LP |
765 | c->user = NULL; |
766 | ||
5cb5a6ff | 767 | free(c->group); |
034c6ed7 LP |
768 | c->group = NULL; |
769 | ||
770 | strv_free(c->supplementary_groups); | |
771 | c->supplementary_groups = NULL; | |
94f04347 LP |
772 | |
773 | if (c->capabilities) { | |
774 | cap_free(c->capabilities); | |
775 | c->capabilities = NULL; | |
776 | } | |
5cb5a6ff LP |
777 | } |
778 | ||
779 | void exec_command_free_list(ExecCommand *c) { | |
780 | ExecCommand *i; | |
781 | ||
782 | while ((i = c)) { | |
034c6ed7 | 783 | LIST_REMOVE(ExecCommand, command, c, i); |
5cb5a6ff LP |
784 | |
785 | free(i->path); | |
44d8db9e | 786 | strv_free(i->argv); |
5cb5a6ff LP |
787 | free(i); |
788 | } | |
789 | } | |
790 | ||
034c6ed7 LP |
791 | void exec_command_free_array(ExecCommand **c, unsigned n) { |
792 | unsigned i; | |
793 | ||
794 | for (i = 0; i < n; i++) { | |
795 | exec_command_free_list(c[i]); | |
796 | c[i] = NULL; | |
797 | } | |
798 | } | |
799 | ||
5cb5a6ff | 800 | void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) { |
94f04347 LP |
801 | char ** e; |
802 | unsigned i; | |
9eba9da4 | 803 | |
5cb5a6ff LP |
804 | assert(c); |
805 | assert(f); | |
806 | ||
807 | if (!prefix) | |
808 | prefix = ""; | |
809 | ||
810 | fprintf(f, | |
94f04347 LP |
811 | "%sUMask: %04o\n" |
812 | "%sWorkingDirectory: %s\n" | |
451a074f | 813 | "%sRootDirectory: %s\n" |
ee2b4894 LP |
814 | "%sNonBlocking: %s\n" |
815 | "%sNewSession: %s\n", | |
5cb5a6ff | 816 | prefix, c->umask, |
9eba9da4 | 817 | prefix, c->working_directory ? c->working_directory : "/", |
451a074f | 818 | prefix, c->root_directory ? c->root_directory : "/", |
ee2b4894 LP |
819 | prefix, yes_no(c->non_blocking), |
820 | prefix, yes_no(c->new_session)); | |
fb33a393 | 821 | |
94f04347 LP |
822 | if (c->environment) |
823 | for (e = c->environment; *e; e++) | |
824 | fprintf(f, "%sEnvironment: %s\n", prefix, *e); | |
825 | ||
fb33a393 LP |
826 | if (c->nice_set) |
827 | fprintf(f, | |
828 | "%sNice: %i\n", | |
829 | prefix, c->nice); | |
830 | ||
831 | if (c->oom_adjust_set) | |
832 | fprintf(f, | |
833 | "%sOOMAdjust: %i\n", | |
834 | prefix, c->oom_adjust); | |
9eba9da4 | 835 | |
94f04347 LP |
836 | for (i = 0; i < RLIM_NLIMITS; i++) |
837 | if (c->rlimit[i]) | |
ea430986 | 838 | fprintf(f, "%s%s: %llu\n", prefix, rlimit_to_string(i), (unsigned long long) c->rlimit[i]->rlim_max); |
94f04347 | 839 | |
9eba9da4 LP |
840 | if (c->ioprio_set) |
841 | fprintf(f, | |
842 | "%sIOSchedulingClass: %s\n" | |
843 | "%sIOPriority: %i\n", | |
94f04347 | 844 | prefix, ioprio_class_to_string(IOPRIO_PRIO_CLASS(c->ioprio)), |
9eba9da4 | 845 | prefix, (int) IOPRIO_PRIO_DATA(c->ioprio)); |
94f04347 LP |
846 | |
847 | if (c->cpu_sched_set) | |
848 | fprintf(f, | |
849 | "%sCPUSchedulingPolicy: %s\n" | |
38b48754 LP |
850 | "%sCPUSchedulingPriority: %i\n" |
851 | "%sCPUSchedulingResetOnFork: %s\n", | |
94f04347 | 852 | prefix, sched_policy_to_string(c->cpu_sched_policy), |
38b48754 LP |
853 | prefix, c->cpu_sched_priority, |
854 | prefix, yes_no(c->cpu_sched_reset_on_fork)); | |
94f04347 LP |
855 | |
856 | if (c->cpu_affinity_set) { | |
857 | fprintf(f, "%sCPUAffinity:", prefix); | |
858 | for (i = 0; i < CPU_SETSIZE; i++) | |
859 | if (CPU_ISSET(i, &c->cpu_affinity)) | |
860 | fprintf(f, " %i", i); | |
861 | fputs("\n", f); | |
862 | } | |
863 | ||
864 | if (c->timer_slack_ns_set) | |
865 | fprintf(f, "%sTimerSlackNS: %lu\n", prefix, c->timer_slack_ns); | |
866 | ||
867 | fprintf(f, | |
868 | "%sInput: %s\n" | |
869 | "%sOutput: %s\n", | |
870 | prefix, exec_input_to_string(c->input), | |
871 | prefix, exec_output_to_string(c->output)); | |
872 | ||
873 | if (c->output == EXEC_OUTPUT_SYSLOG || c->output == EXEC_OUTPUT_KERNEL) | |
874 | fprintf(f, | |
875 | "%sSyslogFacility: %s\n" | |
876 | "%sSyslogLevel: %s\n", | |
877 | prefix, log_facility_to_string(LOG_FAC(c->syslog_priority)), | |
878 | prefix, log_level_to_string(LOG_PRI(c->syslog_priority))); | |
879 | ||
880 | if (c->capabilities) { | |
881 | char *t; | |
882 | if ((t = cap_to_text(c->capabilities, NULL))) { | |
883 | fprintf(f, "%sCapabilities: %s\n", | |
884 | prefix, t); | |
885 | cap_free(t); | |
886 | } | |
887 | } | |
888 | ||
889 | if (c->secure_bits) | |
890 | fprintf(f, "%sSecure Bits:%s%s%s%s%s%s\n", | |
891 | prefix, | |
892 | (c->secure_bits & SECURE_KEEP_CAPS) ? " keep-caps" : "", | |
893 | (c->secure_bits & SECURE_KEEP_CAPS_LOCKED) ? " keep-caps-locked" : "", | |
894 | (c->secure_bits & SECURE_NO_SETUID_FIXUP) ? " no-setuid-fixup" : "", | |
895 | (c->secure_bits & SECURE_NO_SETUID_FIXUP_LOCKED) ? " no-setuid-fixup-locked" : "", | |
896 | (c->secure_bits & SECURE_NOROOT) ? " noroot" : "", | |
897 | (c->secure_bits & SECURE_NOROOT_LOCKED) ? "noroot-locked" : ""); | |
898 | ||
899 | if (c->capability_bounding_set_drop) { | |
900 | fprintf(f, "%sCapabilityBoundingSetDrop:", prefix); | |
901 | ||
902 | for (i = 0; i <= CAP_LAST_CAP; i++) | |
903 | if (c->capability_bounding_set_drop & (1 << i)) { | |
904 | char *t; | |
905 | ||
906 | if ((t = cap_to_name(i))) { | |
907 | fprintf(f, " %s", t); | |
908 | free(t); | |
909 | } | |
910 | } | |
911 | ||
912 | fputs("\n", f); | |
913 | } | |
914 | ||
915 | if (c->user) | |
916 | fprintf(f, "%sUser: %s", prefix, c->user); | |
917 | if (c->group) | |
918 | fprintf(f, "%sGroup: %s", prefix, c->group); | |
919 | ||
920 | if (c->supplementary_groups) { | |
921 | char **g; | |
922 | ||
923 | fprintf(f, "%sSupplementaryGroups:", prefix); | |
924 | ||
925 | STRV_FOREACH(g, c->supplementary_groups) | |
926 | fprintf(f, " %s", *g); | |
927 | ||
928 | fputs("\n", f); | |
929 | } | |
5cb5a6ff LP |
930 | } |
931 | ||
034c6ed7 LP |
932 | void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) { |
933 | assert(s); | |
5cb5a6ff | 934 | |
034c6ed7 | 935 | s->pid = pid; |
9fb86720 LP |
936 | s->exit_timestamp = now(CLOCK_REALTIME); |
937 | ||
034c6ed7 LP |
938 | s->code = code; |
939 | s->status = status; | |
9fb86720 LP |
940 | } |
941 | ||
942 | void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) { | |
943 | char buf[FORMAT_TIMESTAMP_MAX]; | |
944 | ||
945 | assert(s); | |
946 | assert(f); | |
947 | ||
948 | if (!prefix) | |
949 | prefix = ""; | |
950 | ||
951 | if (s->pid <= 0) | |
952 | return; | |
953 | ||
954 | fprintf(f, | |
955 | "%sPID: %llu\n", | |
956 | prefix, (unsigned long long) s->pid); | |
957 | ||
958 | if (s->start_timestamp > 0) | |
959 | fprintf(f, | |
960 | "%sStart Timestamp: %s\n", | |
961 | prefix, format_timestamp(buf, sizeof(buf), s->start_timestamp)); | |
962 | ||
963 | if (s->exit_timestamp > 0) | |
964 | fprintf(f, | |
965 | "%sExit Timestamp: %s\n" | |
966 | "%sExit Code: %s\n" | |
967 | "%sExit Status: %i\n", | |
968 | prefix, format_timestamp(buf, sizeof(buf), s->exit_timestamp), | |
969 | prefix, sigchld_code_to_string(s->code), | |
970 | prefix, s->status); | |
5cb5a6ff | 971 | } |
44d8db9e LP |
972 | |
973 | char *exec_command_line(ExecCommand *c) { | |
974 | size_t k; | |
975 | char *n, *p, **a; | |
976 | bool first = true; | |
977 | ||
978 | assert(c); | |
979 | assert(c->argv); | |
980 | ||
9164977d | 981 | k = 1; |
44d8db9e LP |
982 | STRV_FOREACH(a, c->argv) |
983 | k += strlen(*a)+3; | |
984 | ||
985 | if (!(n = new(char, k))) | |
986 | return NULL; | |
987 | ||
988 | p = n; | |
989 | STRV_FOREACH(a, c->argv) { | |
990 | ||
991 | if (!first) | |
992 | *(p++) = ' '; | |
993 | else | |
994 | first = false; | |
995 | ||
996 | if (strpbrk(*a, WHITESPACE)) { | |
997 | *(p++) = '\''; | |
998 | p = stpcpy(p, *a); | |
999 | *(p++) = '\''; | |
1000 | } else | |
1001 | p = stpcpy(p, *a); | |
1002 | ||
1003 | } | |
1004 | ||
9164977d LP |
1005 | *p = 0; |
1006 | ||
44d8db9e LP |
1007 | /* FIXME: this doesn't really handle arguments that have |
1008 | * spaces and ticks in them */ | |
1009 | ||
1010 | return n; | |
1011 | } | |
1012 | ||
1013 | void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) { | |
9fb86720 LP |
1014 | char *p2; |
1015 | const char *prefix2; | |
1016 | ||
44d8db9e LP |
1017 | char *cmd; |
1018 | ||
1019 | assert(c); | |
1020 | assert(f); | |
1021 | ||
1022 | if (!prefix) | |
1023 | prefix = ""; | |
9fb86720 LP |
1024 | p2 = strappend(prefix, "\t"); |
1025 | prefix2 = p2 ? p2 : prefix; | |
44d8db9e LP |
1026 | |
1027 | cmd = exec_command_line(c); | |
1028 | ||
1029 | fprintf(f, | |
1030 | "%sCommand Line: %s\n", | |
1031 | prefix, cmd ? cmd : strerror(ENOMEM)); | |
1032 | ||
1033 | free(cmd); | |
9fb86720 LP |
1034 | |
1035 | exec_status_dump(&c->exec_status, f, prefix2); | |
1036 | ||
1037 | free(p2); | |
44d8db9e LP |
1038 | } |
1039 | ||
1040 | void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) { | |
1041 | assert(f); | |
1042 | ||
1043 | if (!prefix) | |
1044 | prefix = ""; | |
1045 | ||
1046 | LIST_FOREACH(command, c, c) | |
1047 | exec_command_dump(c, f, prefix); | |
1048 | } | |
94f04347 | 1049 | |
a6a80b4f LP |
1050 | void exec_command_append_list(ExecCommand **l, ExecCommand *e) { |
1051 | ExecCommand *end; | |
1052 | ||
1053 | assert(l); | |
1054 | assert(e); | |
1055 | ||
1056 | if (*l) { | |
1057 | /* It's kinda important that we keep the order here */ | |
1058 | LIST_FIND_TAIL(ExecCommand, command, *l, end); | |
1059 | LIST_INSERT_AFTER(ExecCommand, command, *l, end, e); | |
1060 | } else | |
1061 | *l = e; | |
1062 | } | |
1063 | ||
94f04347 LP |
1064 | static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = { |
1065 | [EXEC_OUTPUT_CONSOLE] = "console", | |
1066 | [EXEC_OUTPUT_NULL] = "null", | |
1067 | [EXEC_OUTPUT_SYSLOG] = "syslog", | |
1068 | [EXEC_OUTPUT_KERNEL] = "kernel" | |
1069 | }; | |
1070 | ||
1071 | DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput); | |
1072 | ||
1073 | static const char* const exec_input_table[_EXEC_INPUT_MAX] = { | |
1074 | [EXEC_INPUT_NULL] = "null", | |
1075 | [EXEC_INPUT_CONSOLE] = "console" | |
1076 | }; | |
1077 | ||
1078 | DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput); |