]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-execute.c
Merge pull request #22791 from keszybz/bootctl-invert-order
[thirdparty/systemd.git] / src / test / test-execute.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4 #include <sys/prctl.h>
5 #include <sys/types.h>
6
7 #include "sd-event.h"
8
9 #include "capability-util.h"
10 #include "cpu-set-util.h"
11 #include "copy.h"
12 #include "dropin.h"
13 #include "errno-list.h"
14 #include "fd-util.h"
15 #include "fileio.h"
16 #include "fs-util.h"
17 #include "macro.h"
18 #include "manager.h"
19 #include "missing_prctl.h"
20 #include "mkdir.h"
21 #include "path-util.h"
22 #include "process-util.h"
23 #include "rm-rf.h"
24 #if HAVE_SECCOMP
25 #include "seccomp-util.h"
26 #endif
27 #include "service.h"
28 #include "signal-util.h"
29 #include "static-destruct.h"
30 #include "stat-util.h"
31 #include "tests.h"
32 #include "tmpfile-util.h"
33 #include "unit.h"
34 #include "user-util.h"
35 #include "util.h"
36 #include "virt.h"
37
38 static char *user_runtime_unit_dir = NULL;
39 static bool can_unshare;
40
41 STATIC_DESTRUCTOR_REGISTER(user_runtime_unit_dir, freep);
42
43 typedef void (*test_function_t)(Manager *m);
44
45 static int cld_dumped_to_killed(int code) {
46 /* Depending on the system, seccomp version, … some signals might result in dumping, others in plain
47 * killing. Let's ignore the difference here, and map both cases to CLD_KILLED */
48 return code == CLD_DUMPED ? CLD_KILLED : code;
49 }
50
51 static void wait_for_service_finish(Manager *m, Unit *unit) {
52 Service *service = NULL;
53 usec_t ts;
54 usec_t timeout = 2 * USEC_PER_MINUTE;
55
56 assert_se(m);
57 assert_se(unit);
58
59 service = SERVICE(unit);
60 printf("%s\n", unit->id);
61 exec_context_dump(&service->exec_context, stdout, "\t");
62 ts = now(CLOCK_MONOTONIC);
63 while (!IN_SET(service->state, SERVICE_DEAD, SERVICE_FAILED)) {
64 int r;
65 usec_t n;
66
67 r = sd_event_run(m->event, 100 * USEC_PER_MSEC);
68 assert_se(r >= 0);
69
70 n = now(CLOCK_MONOTONIC);
71 if (ts + timeout < n) {
72 log_error("Test timeout when testing %s", unit->id);
73 r = unit_kill(unit, KILL_ALL, SIGKILL, NULL);
74 if (r < 0)
75 log_error_errno(r, "Failed to kill %s: %m", unit->id);
76 exit(EXIT_FAILURE);
77 }
78 }
79 }
80
81 static void check_main_result(const char *file, unsigned line, const char *func,
82 Manager *m, Unit *unit, int status_expected, int code_expected) {
83 Service *service = NULL;
84
85 assert_se(m);
86 assert_se(unit);
87
88 wait_for_service_finish(m, unit);
89
90 service = SERVICE(unit);
91 exec_status_dump(&service->main_exec_status, stdout, "\t");
92
93 if (cld_dumped_to_killed(service->main_exec_status.code) != cld_dumped_to_killed(code_expected)) {
94 log_error("%s:%u:%s %s: can_unshare=%s: exit code %d, expected %d",
95 file, line, func, unit->id, yes_no(can_unshare),
96 service->main_exec_status.code, code_expected);
97 abort();
98 }
99
100 if (service->main_exec_status.status != status_expected) {
101 log_error("%s:%u:%s: %s: can_unshare=%s: exit status %d, expected %d",
102 file, line, func, unit->id, yes_no(can_unshare),
103 service->main_exec_status.status, status_expected);
104 abort();
105 }
106 }
107
108 static void check_service_result(const char *file, unsigned line, const char *func,
109 Manager *m, Unit *unit, ServiceResult result_expected) {
110 Service *service = NULL;
111
112 assert_se(m);
113 assert_se(unit);
114
115 wait_for_service_finish(m, unit);
116
117 service = SERVICE(unit);
118
119 if (service->result != result_expected) {
120 log_error("%s:%u:%s: %s: can_unshare=%s: service end result %s, expected %s",
121 file, line, func, unit->id, yes_no(can_unshare),
122 service_result_to_string(service->result),
123 service_result_to_string(result_expected));
124 abort();
125 }
126 }
127
128 static bool check_nobody_user_and_group(void) {
129 static int cache = -1;
130 struct passwd *p;
131 struct group *g;
132
133 if (cache >= 0)
134 return !!cache;
135
136 if (!synthesize_nobody())
137 goto invalid;
138
139 p = getpwnam(NOBODY_USER_NAME);
140 if (!p ||
141 !streq(p->pw_name, NOBODY_USER_NAME) ||
142 p->pw_uid != UID_NOBODY ||
143 p->pw_gid != GID_NOBODY)
144 goto invalid;
145
146 p = getpwuid(UID_NOBODY);
147 if (!p ||
148 !streq(p->pw_name, NOBODY_USER_NAME) ||
149 p->pw_uid != UID_NOBODY ||
150 p->pw_gid != GID_NOBODY)
151 goto invalid;
152
153 g = getgrnam(NOBODY_GROUP_NAME);
154 if (!g ||
155 !streq(g->gr_name, NOBODY_GROUP_NAME) ||
156 g->gr_gid != GID_NOBODY)
157 goto invalid;
158
159 g = getgrgid(GID_NOBODY);
160 if (!g ||
161 !streq(g->gr_name, NOBODY_GROUP_NAME) ||
162 g->gr_gid != GID_NOBODY)
163 goto invalid;
164
165 cache = 1;
166 return true;
167
168 invalid:
169 cache = 0;
170 return false;
171 }
172
173 static bool check_user_has_group_with_same_name(const char *name) {
174 struct passwd *p;
175 struct group *g;
176
177 assert_se(name);
178
179 p = getpwnam(name);
180 if (!p ||
181 !streq(p->pw_name, name))
182 return false;
183
184 g = getgrgid(p->pw_gid);
185 if (!g ||
186 !streq(g->gr_name, name))
187 return false;
188
189 return true;
190 }
191
192 static bool is_inaccessible_available(void) {
193 const char *p;
194
195 FOREACH_STRING(p,
196 "/run/systemd/inaccessible/reg",
197 "/run/systemd/inaccessible/dir",
198 "/run/systemd/inaccessible/chr",
199 "/run/systemd/inaccessible/blk",
200 "/run/systemd/inaccessible/fifo",
201 "/run/systemd/inaccessible/sock"
202 ) {
203 if (access(p, F_OK) < 0)
204 return false;
205 }
206
207 return true;
208 }
209
210 static void _test(const char *file, unsigned line, const char *func,
211 Manager *m, const char *unit_name, int status_expected, int code_expected) {
212 Unit *unit;
213
214 assert_se(unit_name);
215
216 assert_se(manager_load_startable_unit_or_warn(m, unit_name, NULL, &unit) >= 0);
217 assert_se(unit_start(unit) >= 0);
218 check_main_result(file, line, func, m, unit, status_expected, code_expected);
219 }
220 #define test(m, unit_name, status_expected, code_expected) \
221 _test(PROJECT_FILE, __LINE__, __func__, m, unit_name, status_expected, code_expected)
222
223 static void _test_service(const char *file, unsigned line, const char *func,
224 Manager *m, const char *unit_name, ServiceResult result_expected) {
225 Unit *unit;
226
227 assert_se(unit_name);
228
229 assert_se(manager_load_startable_unit_or_warn(m, unit_name, NULL, &unit) >= 0);
230 assert_se(unit_start(unit) >= 0);
231 check_service_result(file, line, func, m, unit, result_expected);
232 }
233 #define test_service(m, unit_name, result_expected) \
234 _test_service(PROJECT_FILE, __LINE__, __func__, m, unit_name, result_expected)
235
236 static void test_exec_bindpaths(Manager *m) {
237 assert_se(mkdir_p("/tmp/test-exec-bindpaths", 0755) >= 0);
238 assert_se(mkdir_p("/tmp/test-exec-bindreadonlypaths", 0755) >= 0);
239
240 test(m, "exec-bindpaths.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
241
242 (void) rm_rf("/tmp/test-exec-bindpaths", REMOVE_ROOT|REMOVE_PHYSICAL);
243 (void) rm_rf("/tmp/test-exec-bindreadonlypaths", REMOVE_ROOT|REMOVE_PHYSICAL);
244 }
245
246 static void test_exec_cpuaffinity(Manager *m) {
247 _cleanup_(cpu_set_reset) CPUSet c = {};
248
249 assert_se(cpu_set_realloc(&c, 8192) >= 0); /* just allocate the maximum possible size */
250 assert_se(sched_getaffinity(0, c.allocated, c.set) >= 0);
251
252 if (!CPU_ISSET_S(0, c.allocated, c.set)) {
253 log_notice("Cannot use CPU 0, skipping %s", __func__);
254 return;
255 }
256
257 test(m, "exec-cpuaffinity1.service", 0, CLD_EXITED);
258 test(m, "exec-cpuaffinity2.service", 0, CLD_EXITED);
259
260 if (!CPU_ISSET_S(1, c.allocated, c.set) ||
261 !CPU_ISSET_S(2, c.allocated, c.set)) {
262 log_notice("Cannot use CPU 1 or 2, skipping remaining tests in %s", __func__);
263 return;
264 }
265
266 test(m, "exec-cpuaffinity3.service", 0, CLD_EXITED);
267 }
268
269 static void test_exec_workingdirectory(Manager *m) {
270 assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0);
271
272 test(m, "exec-workingdirectory.service", 0, CLD_EXITED);
273 test(m, "exec-workingdirectory-trailing-dot.service", 0, CLD_EXITED);
274
275 (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT|REMOVE_PHYSICAL);
276 }
277
278 static void test_exec_execsearchpath(Manager *m) {
279 assert_se(mkdir_p("/tmp/test-exec_execsearchpath", 0755) >= 0);
280
281 assert_se(copy_file("/bin/ls", "/tmp/test-exec_execsearchpath/ls_temp", 0, 0777, 0, 0, COPY_REPLACE) >= 0);
282
283 test(m, "exec-execsearchpath.service", 0, CLD_EXITED);
284
285 assert_se(rm_rf("/tmp/test-exec_execsearchpath", REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
286
287 test(m, "exec-execsearchpath.service", EXIT_EXEC, CLD_EXITED);
288 }
289
290 static void test_exec_execsearchpath_specifier(Manager *m) {
291 test(m, "exec-execsearchpath-unit-specifier.service", 0, CLD_EXITED);
292 }
293
294 static void test_exec_execsearchpath_environment(Manager *m) {
295 test(m, "exec-execsearchpath-environment.service", 0, CLD_EXITED);
296 test(m, "exec-execsearchpath-environment-path-set.service", 0, CLD_EXITED);
297 }
298
299 static void test_exec_execsearchpath_environment_files(Manager *m) {
300 static const char path_not_set[] =
301 "VAR1='word1 word2'\n"
302 "VAR2=word3 \n"
303 "# comment1\n"
304 "\n"
305 "; comment2\n"
306 " ; # comment3\n"
307 "line without an equal\n"
308 "VAR3='$word 5 6'\n"
309 "VAR4='new\nline'\n"
310 "VAR5=password\\with\\backslashes";
311
312 static const char path_set[] =
313 "VAR1='word1 word2'\n"
314 "VAR2=word3 \n"
315 "# comment1\n"
316 "\n"
317 "; comment2\n"
318 " ; # comment3\n"
319 "line without an equal\n"
320 "VAR3='$word 5 6'\n"
321 "VAR4='new\nline'\n"
322 "VAR5=password\\with\\backslashes\n"
323 "PATH=/usr";
324
325 int r;
326
327 r = write_string_file("/tmp/test-exec_execsearchpath_environmentfile.conf", path_not_set, WRITE_STRING_FILE_CREATE);
328
329 assert_se(r == 0);
330
331 test(m, "exec-execsearchpath-environmentfile.service", 0, CLD_EXITED);
332
333 (void) unlink("/tmp/test-exec_environmentfile.conf");
334
335
336 r = write_string_file("/tmp/test-exec_execsearchpath_environmentfile-set.conf", path_set, WRITE_STRING_FILE_CREATE);
337
338 assert_se(r == 0);
339
340 test(m, "exec-execsearchpath-environmentfile-set.service", 0, CLD_EXITED);
341
342 (void) unlink("/tmp/test-exec_environmentfile-set.conf");
343 }
344
345 static void test_exec_execsearchpath_passenvironment(Manager *m) {
346 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
347 assert_se(setenv("VAR2", "word3", 1) == 0);
348 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
349 assert_se(setenv("VAR4", "new\nline", 1) == 0);
350 assert_se(setenv("VAR5", "passwordwithbackslashes", 1) == 0);
351
352 test(m, "exec-execsearchpath-passenvironment.service", 0, CLD_EXITED);
353
354 assert_se(setenv("PATH", "/usr", 1) == 0);
355 test(m, "exec-execsearchpath-passenvironment-set.service", 0, CLD_EXITED);
356
357 assert_se(unsetenv("VAR1") == 0);
358 assert_se(unsetenv("VAR2") == 0);
359 assert_se(unsetenv("VAR3") == 0);
360 assert_se(unsetenv("VAR4") == 0);
361 assert_se(unsetenv("VAR5") == 0);
362 assert_se(unsetenv("PATH") == 0);
363 }
364
365 static void test_exec_personality(Manager *m) {
366 #if defined(__x86_64__)
367 test(m, "exec-personality-x86-64.service", 0, CLD_EXITED);
368
369 #elif defined(__s390__)
370 test(m, "exec-personality-s390.service", 0, CLD_EXITED);
371
372 #elif defined(__powerpc64__)
373 # if __BYTE_ORDER == __BIG_ENDIAN
374 test(m, "exec-personality-ppc64.service", 0, CLD_EXITED);
375 # else
376 test(m, "exec-personality-ppc64le.service", 0, CLD_EXITED);
377 # endif
378
379 #elif defined(__aarch64__)
380 test(m, "exec-personality-aarch64.service", 0, CLD_EXITED);
381
382 #elif defined(__i386__)
383 test(m, "exec-personality-x86.service", 0, CLD_EXITED);
384 #elif defined(__loongarch64)
385 test(m, "exec-personality-loongarch64.service", 0, CLD_EXITED);
386 #else
387 log_notice("Unknown personality, skipping %s", __func__);
388 #endif
389 }
390
391 static void test_exec_ignoresigpipe(Manager *m) {
392 test(m, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED);
393 test(m, "exec-ignoresigpipe-no.service", SIGPIPE, CLD_KILLED);
394 }
395
396 static void test_exec_privatetmp(Manager *m) {
397 assert_se(touch("/tmp/test-exec_privatetmp") >= 0);
398
399 test(m, "exec-privatetmp-yes.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
400 test(m, "exec-privatetmp-no.service", 0, CLD_EXITED);
401 test(m, "exec-privatetmp-disabled-by-prefix.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
402
403 unlink("/tmp/test-exec_privatetmp");
404 }
405
406 static void test_exec_privatedevices(Manager *m) {
407 int r;
408
409 if (detect_container() > 0) {
410 log_notice("Testing in container, skipping %s", __func__);
411 return;
412 }
413 if (!is_inaccessible_available()) {
414 log_notice("Testing without inaccessible, skipping %s", __func__);
415 return;
416 }
417
418 test(m, "exec-privatedevices-yes.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
419 test(m, "exec-privatedevices-no.service", 0, CLD_EXITED);
420 test(m, "exec-privatedevices-disabled-by-prefix.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
421 test(m, "exec-privatedevices-yes-with-group.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
422
423 /* We use capsh to test if the capabilities are
424 * properly set, so be sure that it exists */
425 r = find_executable("capsh", NULL);
426 if (r < 0) {
427 log_notice_errno(r, "Could not find capsh binary, skipping remaining tests in %s: %m", __func__);
428 return;
429 }
430
431 test(m, "exec-privatedevices-yes-capability-mknod.service", 0, CLD_EXITED);
432 test(m, "exec-privatedevices-no-capability-mknod.service", 0, CLD_EXITED);
433 test(m, "exec-privatedevices-yes-capability-sys-rawio.service", 0, CLD_EXITED);
434 test(m, "exec-privatedevices-no-capability-sys-rawio.service", 0, CLD_EXITED);
435 }
436
437 static void test_exec_protecthome(Manager *m) {
438 if (!can_unshare) {
439 log_notice("Cannot reliably unshare, skipping %s", __func__);
440 return;
441 }
442
443 test(m, "exec-protecthome-tmpfs-vs-protectsystem-strict.service", 0, CLD_EXITED);
444 }
445
446 static void test_exec_protectkernelmodules(Manager *m) {
447 int r;
448
449 if (detect_container() > 0) {
450 log_notice("Testing in container, skipping %s", __func__);
451 return;
452 }
453 if (!is_inaccessible_available()) {
454 log_notice("Testing without inaccessible, skipping %s", __func__);
455 return;
456 }
457
458 r = find_executable("capsh", NULL);
459 if (r < 0) {
460 log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
461 return;
462 }
463
464 test(m, "exec-protectkernelmodules-no-capabilities.service", 0, CLD_EXITED);
465 test(m, "exec-protectkernelmodules-yes-capabilities.service", 0, CLD_EXITED);
466 test(m, "exec-protectkernelmodules-yes-mount-propagation.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
467 }
468
469 static void test_exec_readonlypaths(Manager *m) {
470
471 test(m, "exec-readonlypaths-simple.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
472
473 if (path_is_read_only_fs("/var") > 0) {
474 log_notice("Directory /var is readonly, skipping remaining tests in %s", __func__);
475 return;
476 }
477
478 test(m, "exec-readonlypaths.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
479 test(m, "exec-readonlypaths-with-bindpaths.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
480 test(m, "exec-readonlypaths-mount-propagation.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
481 }
482
483 static void test_exec_readwritepaths(Manager *m) {
484
485 if (path_is_read_only_fs("/") > 0) {
486 log_notice("Root directory is readonly, skipping %s", __func__);
487 return;
488 }
489
490 test(m, "exec-readwritepaths-mount-propagation.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
491 }
492
493 static void test_exec_inaccessiblepaths(Manager *m) {
494
495 if (!is_inaccessible_available()) {
496 log_notice("Testing without inaccessible, skipping %s", __func__);
497 return;
498 }
499
500 test(m, "exec-inaccessiblepaths-sys.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
501
502 if (path_is_read_only_fs("/") > 0) {
503 log_notice("Root directory is readonly, skipping remaining tests in %s", __func__);
504 return;
505 }
506
507 test(m, "exec-inaccessiblepaths-mount-propagation.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
508 }
509
510 static int on_spawn_io(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
511 char **result = userdata;
512 char buf[4096];
513 ssize_t l;
514
515 assert_se(s);
516 assert_se(fd >= 0);
517
518 l = read(fd, buf, sizeof(buf) - 1);
519 if (l < 0) {
520 if (errno == EAGAIN)
521 goto reenable;
522
523 return 0;
524 }
525 if (l == 0)
526 return 0;
527
528 buf[l] = '\0';
529 if (result)
530 assert_se(strextend(result, buf));
531 else
532 log_error("ldd: %s", buf);
533
534 reenable:
535 /* Re-enable the event source if we did not encounter EOF */
536 assert_se(sd_event_source_set_enabled(s, SD_EVENT_ONESHOT) >= 0);
537 return 0;
538 }
539
540 static int on_spawn_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
541 pid_t *pid = userdata;
542
543 assert_se(pid);
544
545 (void) kill(*pid, SIGKILL);
546
547 return 1;
548 }
549
550 static int on_spawn_sigchld(sd_event_source *s, const siginfo_t *si, void *userdata) {
551 int ret = -EIO;
552
553 assert_se(si);
554
555 if (si->si_code == CLD_EXITED)
556 ret = si->si_status;
557
558 sd_event_exit(sd_event_source_get_event(s), ret);
559 return 1;
560 }
561
562 static int find_libraries(const char *exec, char ***ret) {
563 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
564 _cleanup_(sd_event_source_unrefp) sd_event_source *sigchld_source = NULL;
565 _cleanup_(sd_event_source_unrefp) sd_event_source *stdout_source = NULL;
566 _cleanup_(sd_event_source_unrefp) sd_event_source *stderr_source = NULL;
567 _cleanup_close_pair_ int outpipe[2] = {-1, -1}, errpipe[2] = {-1, -1};
568 _cleanup_strv_free_ char **libraries = NULL;
569 _cleanup_free_ char *result = NULL;
570 pid_t pid;
571 int r;
572
573 assert_se(exec);
574 assert_se(ret);
575
576 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0);
577
578 assert_se(pipe2(outpipe, O_NONBLOCK|O_CLOEXEC) == 0);
579 assert_se(pipe2(errpipe, O_NONBLOCK|O_CLOEXEC) == 0);
580
581 r = safe_fork("(spawn-ldd)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
582 assert_se(r >= 0);
583 if (r == 0) {
584 if (rearrange_stdio(-1, TAKE_FD(outpipe[1]), TAKE_FD(errpipe[1])) < 0)
585 _exit(EXIT_FAILURE);
586
587 (void) close_all_fds(NULL, 0);
588
589 execlp("ldd", "ldd", exec, NULL);
590 _exit(EXIT_FAILURE);
591 }
592
593 outpipe[1] = safe_close(outpipe[1]);
594 errpipe[1] = safe_close(errpipe[1]);
595
596 assert_se(sd_event_new(&e) >= 0);
597
598 assert_se(sd_event_add_time_relative(e, NULL, CLOCK_MONOTONIC,
599 10 * USEC_PER_SEC, USEC_PER_SEC, on_spawn_timeout, &pid) >= 0);
600 assert_se(sd_event_add_io(e, &stdout_source, outpipe[0], EPOLLIN, on_spawn_io, &result) >= 0);
601 assert_se(sd_event_source_set_enabled(stdout_source, SD_EVENT_ONESHOT) >= 0);
602 assert_se(sd_event_add_io(e, &stderr_source, errpipe[0], EPOLLIN, on_spawn_io, NULL) >= 0);
603 assert_se(sd_event_source_set_enabled(stderr_source, SD_EVENT_ONESHOT) >= 0);
604 assert_se(sd_event_add_child(e, &sigchld_source, pid, WEXITED, on_spawn_sigchld, NULL) >= 0);
605 /* SIGCHLD should be processed after IO is complete */
606 assert_se(sd_event_source_set_priority(sigchld_source, SD_EVENT_PRIORITY_NORMAL + 1) >= 0);
607
608 assert_se(sd_event_loop(e) >= 0);
609
610 _cleanup_strv_free_ char **v = NULL;
611 assert_se(strv_split_newlines_full(&v, result, 0) >= 0);
612
613 STRV_FOREACH(q, v) {
614 _cleanup_free_ char *word = NULL;
615 const char *p = *q;
616
617 r = extract_first_word(&p, &word, NULL, 0);
618 assert_se(r >= 0);
619 if (r == 0)
620 continue;
621
622 if (path_is_absolute(word)) {
623 assert_se(strv_consume(&libraries, TAKE_PTR(word)) >= 0);
624 continue;
625 }
626
627 word = mfree(word);
628 r = extract_first_word(&p, &word, NULL, 0);
629 assert_se(r >= 0);
630 if (r == 0)
631 continue;
632
633 if (!streq_ptr(word, "=>"))
634 continue;
635
636 word = mfree(word);
637 r = extract_first_word(&p, &word, NULL, 0);
638 assert_se(r >= 0);
639 if (r == 0)
640 continue;
641
642 if (path_is_absolute(word)) {
643 assert_se(strv_consume(&libraries, TAKE_PTR(word)) >= 0);
644 continue;
645 }
646 }
647
648 *ret = TAKE_PTR(libraries);
649 return 0;
650 }
651
652 static void test_exec_mount_apivfs(Manager *m) {
653 _cleanup_free_ char *fullpath_touch = NULL, *fullpath_test = NULL, *data = NULL;
654 _cleanup_strv_free_ char **libraries = NULL, **libraries_test = NULL;
655 int r;
656
657 assert_se(user_runtime_unit_dir);
658
659 r = find_executable("touch", &fullpath_touch);
660 if (r < 0) {
661 log_notice_errno(r, "Skipping %s, could not find 'touch' command: %m", __func__);
662 return;
663 }
664 r = find_executable("test", &fullpath_test);
665 if (r < 0) {
666 log_notice_errno(r, "Skipping %s, could not find 'test' command: %m", __func__);
667 return;
668 }
669
670 assert_se(find_libraries(fullpath_touch, &libraries) >= 0);
671 assert_se(find_libraries(fullpath_test, &libraries_test) >= 0);
672 assert_se(strv_extend_strv(&libraries, libraries_test, true) >= 0);
673
674 assert_se(strextend(&data, "[Service]\n"));
675 assert_se(strextend(&data, "ExecStart=", fullpath_touch, " /aaa\n"));
676 assert_se(strextend(&data, "ExecStart=", fullpath_test, " -f /aaa\n"));
677 assert_se(strextend(&data, "BindReadOnlyPaths=", fullpath_touch, "\n"));
678 assert_se(strextend(&data, "BindReadOnlyPaths=", fullpath_test, "\n"));
679
680 STRV_FOREACH(p, libraries)
681 assert_se(strextend(&data, "BindReadOnlyPaths=", *p, "\n"));
682
683 assert_se(write_drop_in(user_runtime_unit_dir, "exec-mount-apivfs-no.service", 10, "bind-mount", data) >= 0);
684
685 assert_se(mkdir_p("/tmp/test-exec-mount-apivfs-no/root", 0755) >= 0);
686
687 test(m, "exec-mount-apivfs-no.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
688
689 (void) rm_rf("/tmp/test-exec-mount-apivfs-no/root", REMOVE_ROOT|REMOVE_PHYSICAL);
690 }
691
692 static void test_exec_noexecpaths(Manager *m) {
693
694 test(m, "exec-noexecpaths-simple.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
695 }
696
697 static void test_exec_temporaryfilesystem(Manager *m) {
698
699 test(m, "exec-temporaryfilesystem-options.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
700 test(m, "exec-temporaryfilesystem-ro.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
701 test(m, "exec-temporaryfilesystem-rw.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
702 test(m, "exec-temporaryfilesystem-usr.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
703 }
704
705 static void test_exec_systemcallfilter(Manager *m) {
706 #if HAVE_SECCOMP
707 int r;
708
709 if (!is_seccomp_available()) {
710 log_notice("Seccomp not available, skipping %s", __func__);
711 return;
712 }
713
714 test(m, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED);
715 test(m, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED);
716 test(m, "exec-systemcallfilter-not-failing3.service", 0, CLD_EXITED);
717 test(m, "exec-systemcallfilter-failing.service", SIGSYS, CLD_KILLED);
718 test(m, "exec-systemcallfilter-failing2.service", SIGSYS, CLD_KILLED);
719 test(m, "exec-systemcallfilter-failing3.service", SIGSYS, CLD_KILLED);
720
721 r = find_executable("python3", NULL);
722 if (r < 0) {
723 log_notice_errno(r, "Skipping remaining tests in %s, could not find python3 binary: %m", __func__);
724 return;
725 }
726
727 test(m, "exec-systemcallfilter-with-errno-name.service", errno_from_name("EILSEQ"), CLD_EXITED);
728 test(m, "exec-systemcallfilter-with-errno-number.service", 255, CLD_EXITED);
729 test(m, "exec-systemcallfilter-with-errno-multi.service", errno_from_name("EILSEQ"), CLD_EXITED);
730 test(m, "exec-systemcallfilter-with-errno-in-allow-list.service", errno_from_name("EILSEQ"), CLD_EXITED);
731 test(m, "exec-systemcallfilter-override-error-action.service", SIGSYS, CLD_KILLED);
732 test(m, "exec-systemcallfilter-override-error-action2.service", errno_from_name("EILSEQ"), CLD_EXITED);
733 #endif
734 }
735
736 static void test_exec_systemcallerrornumber(Manager *m) {
737 #if HAVE_SECCOMP
738 int r;
739
740 if (!is_seccomp_available()) {
741 log_notice("Seccomp not available, skipping %s", __func__);
742 return;
743 }
744
745 r = find_executable("python3", NULL);
746 if (r < 0) {
747 log_notice_errno(r, "Skipping %s, could not find python3 binary: %m", __func__);
748 return;
749 }
750
751 test(m, "exec-systemcallerrornumber-name.service", errno_from_name("EACCES"), CLD_EXITED);
752 test(m, "exec-systemcallerrornumber-number.service", 255, CLD_EXITED);
753 #endif
754 }
755
756 static void test_exec_restrictnamespaces(Manager *m) {
757 #if HAVE_SECCOMP
758 if (!is_seccomp_available()) {
759 log_notice("Seccomp not available, skipping %s", __func__);
760 return;
761 }
762
763 test(m, "exec-restrictnamespaces-no.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
764 test(m, "exec-restrictnamespaces-yes.service", 1, CLD_EXITED);
765 test(m, "exec-restrictnamespaces-mnt.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
766 test(m, "exec-restrictnamespaces-mnt-deny-list.service", 1, CLD_EXITED);
767 test(m, "exec-restrictnamespaces-merge-and.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
768 test(m, "exec-restrictnamespaces-merge-or.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
769 test(m, "exec-restrictnamespaces-merge-all.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
770 #endif
771 }
772
773 static void test_exec_systemcallfilter_system(Manager *m) {
774 /* Skip this particular test case when running under ASan, as
775 * LSan intermittently segfaults when accessing memory right
776 * after the test finishes. Generally, ASan & LSan don't like
777 * the seccomp stuff.
778 */
779 #if HAVE_SECCOMP && !HAS_FEATURE_ADDRESS_SANITIZER
780 if (!is_seccomp_available()) {
781 log_notice("Seccomp not available, skipping %s", __func__);
782 return;
783 }
784
785 test(m, "exec-systemcallfilter-system-user.service", 0, CLD_EXITED);
786
787 if (!check_nobody_user_and_group()) {
788 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
789 return;
790 }
791
792 if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
793 log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
794 return;
795 }
796
797 test(m, "exec-systemcallfilter-system-user-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
798 #endif
799 }
800
801 static void test_exec_user(Manager *m) {
802 test(m, "exec-user.service", 0, CLD_EXITED);
803
804 if (!check_nobody_user_and_group()) {
805 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
806 return;
807 }
808
809 if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
810 log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
811 return;
812 }
813
814 test(m, "exec-user-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
815 }
816
817 static void test_exec_group(Manager *m) {
818 test(m, "exec-group.service", 0, CLD_EXITED);
819
820 if (!check_nobody_user_and_group()) {
821 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
822 return;
823 }
824
825 if (!STR_IN_SET(NOBODY_GROUP_NAME, "nobody", "nfsnobody", "nogroup")) {
826 log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME, __func__);
827 return;
828 }
829
830 test(m, "exec-group-" NOBODY_GROUP_NAME ".service", 0, CLD_EXITED);
831 }
832
833 static void test_exec_supplementarygroups(Manager *m) {
834 test(m, "exec-supplementarygroups.service", 0, CLD_EXITED);
835 test(m, "exec-supplementarygroups-single-group.service", 0, CLD_EXITED);
836 test(m, "exec-supplementarygroups-single-group-user.service", 0, CLD_EXITED);
837 test(m, "exec-supplementarygroups-multiple-groups-default-group-user.service", 0, CLD_EXITED);
838 test(m, "exec-supplementarygroups-multiple-groups-withgid.service", 0, CLD_EXITED);
839 test(m, "exec-supplementarygroups-multiple-groups-withuid.service", 0, CLD_EXITED);
840 }
841
842 static char* private_directory_bad(Manager *m) {
843 /* This mirrors setup_exec_directory(). */
844
845 for (ExecDirectoryType dt = 0; dt < _EXEC_DIRECTORY_TYPE_MAX; dt++) {
846 _cleanup_free_ char *p = NULL;
847 struct stat st;
848
849 assert_se(p = path_join(m->prefix[dt], "private"));
850
851 if (stat(p, &st) >= 0 &&
852 (st.st_mode & (S_IRWXG|S_IRWXO)))
853 return TAKE_PTR(p);
854 }
855
856 return NULL;
857 }
858
859 static void test_exec_dynamicuser(Manager *m) {
860 _cleanup_free_ char *bad = private_directory_bad(m);
861 if (bad) {
862 log_warning("%s: %s has bad permissions, skipping test.", __func__, bad);
863 return;
864 }
865
866 if (strstr_ptr(ci_environment(), "github-actions")) {
867 log_notice("%s: skipping test on GH Actions because of systemd/systemd#10337", __func__);
868 return;
869 }
870
871 test(m, "exec-dynamicuser-fixeduser.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
872 if (check_user_has_group_with_same_name("adm"))
873 test(m, "exec-dynamicuser-fixeduser-adm.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
874 if (check_user_has_group_with_same_name("games"))
875 test(m, "exec-dynamicuser-fixeduser-games.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
876 test(m, "exec-dynamicuser-fixeduser-one-supplementarygroup.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
877 test(m, "exec-dynamicuser-supplementarygroups.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
878 test(m, "exec-dynamicuser-statedir.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
879
880 (void) rm_rf("/var/lib/quux", REMOVE_ROOT|REMOVE_PHYSICAL);
881 (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
882 (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
883 (void) rm_rf("/var/lib/waldo", REMOVE_ROOT|REMOVE_PHYSICAL);
884 (void) rm_rf("/var/lib/private/quux", REMOVE_ROOT|REMOVE_PHYSICAL);
885 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
886 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
887 (void) rm_rf("/var/lib/private/waldo", REMOVE_ROOT|REMOVE_PHYSICAL);
888
889 test(m, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED);
890 test(m, "exec-dynamicuser-statedir-migrate-step2.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
891 test(m, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED);
892
893 (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
894 (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
895 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
896 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
897
898 test(m, "exec-dynamicuser-runtimedirectory1.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
899 test(m, "exec-dynamicuser-runtimedirectory2.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
900 test(m, "exec-dynamicuser-runtimedirectory3.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
901 }
902
903 static void test_exec_environment(Manager *m) {
904 test(m, "exec-environment-no-substitute.service", 0, CLD_EXITED);
905 test(m, "exec-environment.service", 0, CLD_EXITED);
906 test(m, "exec-environment-multiple.service", 0, CLD_EXITED);
907 test(m, "exec-environment-empty.service", 0, CLD_EXITED);
908 }
909
910 static void test_exec_environmentfile(Manager *m) {
911 static const char e[] =
912 "VAR1='word1 word2'\n"
913 "VAR2=word3 \n"
914 "# comment1\n"
915 "\n"
916 "; comment2\n"
917 " ; # comment3\n"
918 "line without an equal\n"
919 "VAR3='$word 5 6'\n"
920 "VAR4='new\nline'\n"
921 "VAR5=password\\with\\backslashes";
922 int r;
923
924 r = write_string_file("/tmp/test-exec_environmentfile.conf", e, WRITE_STRING_FILE_CREATE);
925 assert_se(r == 0);
926
927 test(m, "exec-environmentfile.service", 0, CLD_EXITED);
928
929 (void) unlink("/tmp/test-exec_environmentfile.conf");
930 }
931
932 static void test_exec_passenvironment(Manager *m) {
933 /* test-execute runs under MANAGER_USER which, by default, forwards all
934 * variables present in the environment, but only those that are
935 * present _at the time it is created_!
936 *
937 * So these PassEnvironment checks are still expected to work, since we
938 * are ensuring the variables are not present at manager creation (they
939 * are unset explicitly in main) and are only set here.
940 *
941 * This is still a good approximation of how a test for MANAGER_SYSTEM
942 * would work.
943 */
944 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
945 assert_se(setenv("VAR2", "word3", 1) == 0);
946 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
947 assert_se(setenv("VAR4", "new\nline", 1) == 0);
948 assert_se(setenv("VAR5", "passwordwithbackslashes", 1) == 0);
949 test(m, "exec-passenvironment.service", 0, CLD_EXITED);
950 test(m, "exec-passenvironment-repeated.service", 0, CLD_EXITED);
951 test(m, "exec-passenvironment-empty.service", 0, CLD_EXITED);
952 assert_se(unsetenv("VAR1") == 0);
953 assert_se(unsetenv("VAR2") == 0);
954 assert_se(unsetenv("VAR3") == 0);
955 assert_se(unsetenv("VAR4") == 0);
956 assert_se(unsetenv("VAR5") == 0);
957 test(m, "exec-passenvironment-absent.service", 0, CLD_EXITED);
958 }
959
960 static void test_exec_umask(Manager *m) {
961 test(m, "exec-umask-default.service", 0, CLD_EXITED);
962 test(m, "exec-umask-0177.service", 0, CLD_EXITED);
963 }
964
965 static void test_exec_runtimedirectory(Manager *m) {
966 test(m, "exec-runtimedirectory.service", 0, CLD_EXITED);
967 test(m, "exec-runtimedirectory-mode.service", 0, CLD_EXITED);
968 test(m, "exec-runtimedirectory-owner.service", 0, CLD_EXITED);
969
970 if (!check_nobody_user_and_group()) {
971 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
972 return;
973 }
974
975 if (!STR_IN_SET(NOBODY_GROUP_NAME, "nobody", "nfsnobody", "nogroup")) {
976 log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME, __func__);
977 return;
978 }
979
980 test(m, "exec-runtimedirectory-owner-" NOBODY_GROUP_NAME ".service", 0, CLD_EXITED);
981 }
982
983 static void test_exec_capabilityboundingset(Manager *m) {
984 int r;
985
986 r = find_executable("capsh", NULL);
987 if (r < 0) {
988 log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
989 return;
990 }
991
992 if (have_effective_cap(CAP_CHOWN) <= 0 ||
993 have_effective_cap(CAP_FOWNER) <= 0 ||
994 have_effective_cap(CAP_KILL) <= 0) {
995 log_notice("Skipping %s, this process does not have enough capabilities", __func__);
996 return;
997 }
998
999 test(m, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED);
1000 test(m, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED);
1001 test(m, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED);
1002 test(m, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED);
1003 }
1004
1005 static void test_exec_basic(Manager *m) {
1006 test(m, "exec-basic.service", 0, CLD_EXITED);
1007 }
1008
1009 static void test_exec_ambientcapabilities(Manager *m) {
1010 int r;
1011
1012 /* Check if the kernel has support for ambient capabilities. Run
1013 * the tests only if that's the case. Clearing all ambient
1014 * capabilities is fine, since we are expecting them to be unset
1015 * in the first place for the tests. */
1016 r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
1017 if (r < 0 && IN_SET(errno, EINVAL, EOPNOTSUPP, ENOSYS)) {
1018 log_notice("Skipping %s, the kernel does not support ambient capabilities", __func__);
1019 return;
1020 }
1021
1022 if (have_effective_cap(CAP_CHOWN) <= 0 ||
1023 have_effective_cap(CAP_NET_RAW) <= 0) {
1024 log_notice("Skipping %s, this process does not have enough capabilities", __func__);
1025 return;
1026 }
1027
1028 test(m, "exec-ambientcapabilities.service", 0, CLD_EXITED);
1029 test(m, "exec-ambientcapabilities-merge.service", 0, CLD_EXITED);
1030
1031 if (!check_nobody_user_and_group()) {
1032 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
1033 return;
1034 }
1035
1036 if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
1037 log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
1038 return;
1039 }
1040
1041 test(m, "exec-ambientcapabilities-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
1042 test(m, "exec-ambientcapabilities-merge-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
1043 }
1044
1045 static void test_exec_privatenetwork(Manager *m) {
1046 int r;
1047
1048 r = find_executable("ip", NULL);
1049 if (r < 0) {
1050 log_notice_errno(r, "Skipping %s, could not find ip binary: %m", __func__);
1051 return;
1052 }
1053
1054 test(m, "exec-privatenetwork-yes.service", can_unshare ? 0 : EXIT_NETWORK, CLD_EXITED);
1055 }
1056
1057 static void test_exec_oomscoreadjust(Manager *m) {
1058 test(m, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED);
1059
1060 if (detect_container() > 0) {
1061 log_notice("Testing in container, skipping remaining tests in %s", __func__);
1062 return;
1063 }
1064 test(m, "exec-oomscoreadjust-negative.service", 0, CLD_EXITED);
1065 }
1066
1067 static void test_exec_ioschedulingclass(Manager *m) {
1068 test(m, "exec-ioschedulingclass-none.service", 0, CLD_EXITED);
1069 test(m, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED);
1070 test(m, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED);
1071
1072 if (detect_container() > 0) {
1073 log_notice("Testing in container, skipping remaining tests in %s", __func__);
1074 return;
1075 }
1076 test(m, "exec-ioschedulingclass-realtime.service", 0, CLD_EXITED);
1077 }
1078
1079 static void test_exec_unsetenvironment(Manager *m) {
1080 test(m, "exec-unsetenvironment.service", 0, CLD_EXITED);
1081 }
1082
1083 static void test_exec_specifier(Manager *m) {
1084 test(m, "exec-specifier.service", 0, CLD_EXITED);
1085 test(m, "exec-specifier@foo-bar.service", 0, CLD_EXITED);
1086 test(m, "exec-specifier-interpolation.service", 0, CLD_EXITED);
1087 test(m, "exec-specifier-credentials-dir.service", 0, CLD_EXITED);
1088 }
1089
1090 static void test_exec_standardinput(Manager *m) {
1091 test(m, "exec-standardinput-data.service", 0, CLD_EXITED);
1092 test(m, "exec-standardinput-file.service", 0, CLD_EXITED);
1093 test(m, "exec-standardinput-file-cat.service", 0, CLD_EXITED);
1094 }
1095
1096 static void test_exec_standardoutput(Manager *m) {
1097 test(m, "exec-standardoutput-file.service", 0, CLD_EXITED);
1098 }
1099
1100 static void test_exec_standardoutput_append(Manager *m) {
1101 test(m, "exec-standardoutput-append.service", 0, CLD_EXITED);
1102 }
1103
1104 static void test_exec_standardoutput_truncate(Manager *m) {
1105 test(m, "exec-standardoutput-truncate.service", 0, CLD_EXITED);
1106 }
1107
1108 static void test_exec_condition(Manager *m) {
1109 test_service(m, "exec-condition-failed.service", SERVICE_FAILURE_EXIT_CODE);
1110 test_service(m, "exec-condition-skip.service", SERVICE_SKIP_CONDITION);
1111 }
1112
1113 static void test_exec_umask_namespace(Manager *m) {
1114 test(m, "exec-umask-namespace.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
1115 }
1116
1117 typedef struct test_entry {
1118 test_function_t f;
1119 const char *name;
1120 } test_entry;
1121
1122 #define entry(x) {x, #x}
1123
1124 static int run_tests(UnitFileScope scope, const test_entry tests[], char **patterns) {
1125 _cleanup_(manager_freep) Manager *m = NULL;
1126 int r;
1127
1128 assert_se(tests);
1129
1130 r = manager_new(scope, MANAGER_TEST_RUN_BASIC, &m);
1131 m->default_std_output = EXEC_OUTPUT_NULL; /* don't rely on host journald */
1132 if (manager_errno_skip_test(r))
1133 return log_tests_skipped_errno(r, "manager_new");
1134 assert_se(r >= 0);
1135 assert_se(manager_startup(m, NULL, NULL, NULL) >= 0);
1136
1137 for (const test_entry *test = tests; test->f; test++)
1138 if (strv_fnmatch_or_empty(patterns, test->name, FNM_NOESCAPE))
1139 test->f(m);
1140 else
1141 log_info("Skipping %s because it does not match any pattern.", test->name);
1142
1143 return 0;
1144 }
1145
1146 int main(int argc, char *argv[]) {
1147 _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
1148
1149 static const test_entry user_tests[] = {
1150 entry(test_exec_basic),
1151 entry(test_exec_ambientcapabilities),
1152 entry(test_exec_bindpaths),
1153 entry(test_exec_capabilityboundingset),
1154 entry(test_exec_condition),
1155 entry(test_exec_cpuaffinity),
1156 entry(test_exec_environment),
1157 entry(test_exec_environmentfile),
1158 entry(test_exec_group),
1159 entry(test_exec_ignoresigpipe),
1160 entry(test_exec_inaccessiblepaths),
1161 entry(test_exec_ioschedulingclass),
1162 entry(test_exec_mount_apivfs),
1163 entry(test_exec_noexecpaths),
1164 entry(test_exec_oomscoreadjust),
1165 entry(test_exec_passenvironment),
1166 entry(test_exec_personality),
1167 entry(test_exec_privatedevices),
1168 entry(test_exec_privatenetwork),
1169 entry(test_exec_privatetmp),
1170 entry(test_exec_protecthome),
1171 entry(test_exec_protectkernelmodules),
1172 entry(test_exec_readonlypaths),
1173 entry(test_exec_readwritepaths),
1174 entry(test_exec_restrictnamespaces),
1175 entry(test_exec_runtimedirectory),
1176 entry(test_exec_standardinput),
1177 entry(test_exec_standardoutput),
1178 entry(test_exec_standardoutput_append),
1179 entry(test_exec_standardoutput_truncate),
1180 entry(test_exec_supplementarygroups),
1181 entry(test_exec_systemcallerrornumber),
1182 entry(test_exec_systemcallfilter),
1183 entry(test_exec_temporaryfilesystem),
1184 entry(test_exec_umask),
1185 entry(test_exec_unsetenvironment),
1186 entry(test_exec_user),
1187 entry(test_exec_workingdirectory),
1188 entry(test_exec_execsearchpath),
1189 entry(test_exec_execsearchpath_environment),
1190 entry(test_exec_execsearchpath_environment_files),
1191 entry(test_exec_execsearchpath_passenvironment),
1192 {},
1193 };
1194 static const test_entry system_tests[] = {
1195 entry(test_exec_dynamicuser),
1196 entry(test_exec_specifier),
1197 entry(test_exec_execsearchpath_specifier),
1198 entry(test_exec_systemcallfilter_system),
1199 entry(test_exec_umask_namespace),
1200 {},
1201 };
1202 int r;
1203
1204 test_setup_logging(LOG_DEBUG);
1205
1206 #if HAS_FEATURE_ADDRESS_SANITIZER
1207 if (strstr_ptr(ci_environment(), "travis") || strstr_ptr(ci_environment(), "github-actions")) {
1208 log_notice("Running on Travis CI/GH Actions under ASan, skipping, see https://github.com/systemd/systemd/issues/10696");
1209 return EXIT_TEST_SKIP;
1210 }
1211 #endif
1212
1213 assert_se(unsetenv("USER") == 0);
1214 assert_se(unsetenv("LOGNAME") == 0);
1215 assert_se(unsetenv("SHELL") == 0);
1216 assert_se(unsetenv("HOME") == 0);
1217 assert_se(unsetenv("TMPDIR") == 0);
1218
1219 can_unshare = have_namespaces();
1220
1221 /* It is needed otherwise cgroup creation fails */
1222 if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0)
1223 return log_tests_skipped("not privileged");
1224
1225 r = enter_cgroup_subroot(NULL);
1226 if (r == -ENOMEDIUM)
1227 return log_tests_skipped("cgroupfs not available");
1228
1229 _cleanup_free_ char *unit_dir = NULL, *unit_paths = NULL;
1230 assert_se(get_testdata_dir("test-execute/", &unit_dir) >= 0);
1231 assert_se(runtime_dir = setup_fake_runtime_dir());
1232 assert_se(user_runtime_unit_dir = path_join(runtime_dir, "systemd/user"));
1233 assert_se(unit_paths = strjoin(unit_dir, ":", user_runtime_unit_dir));
1234 assert_se(set_unit_path(unit_paths) >= 0);
1235
1236 /* Unset VAR1, VAR2 and VAR3 which are used in the PassEnvironment test
1237 * cases, otherwise (and if they are present in the environment),
1238 * `manager_default_environment` will copy them into the default
1239 * environment which is passed to each created job, which will make the
1240 * tests that expect those not to be present to fail.
1241 */
1242 assert_se(unsetenv("VAR1") == 0);
1243 assert_se(unsetenv("VAR2") == 0);
1244 assert_se(unsetenv("VAR3") == 0);
1245
1246 r = run_tests(UNIT_FILE_USER, user_tests, argv + 1);
1247 if (r != 0)
1248 return r;
1249
1250 r = run_tests(UNIT_FILE_SYSTEM, system_tests, argv + 1);
1251 if (r != 0)
1252 return r;
1253
1254 #if HAVE_SECCOMP
1255 /* The following tests are for 1beab8b0d0ff2d7d1436b52d4a0c3d56dc908962. */
1256 if (!is_seccomp_available()) {
1257 log_notice("Seccomp not available, skipping unshare() filtered tests.");
1258 return 0;
1259 }
1260
1261 _cleanup_hashmap_free_ Hashmap *s = NULL;
1262 assert_se(s = hashmap_new(NULL));
1263 r = seccomp_syscall_resolve_name("unshare");
1264 assert_se(r != __NR_SCMP_ERROR);
1265 assert_se(hashmap_put(s, UINT32_TO_PTR(r + 1), INT_TO_PTR(-1)) >= 0);
1266 assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EOPNOTSUPP), true) >= 0);
1267 assert_se(unshare(CLONE_NEWNS) < 0);
1268 assert_se(errno == EOPNOTSUPP);
1269
1270 can_unshare = false;
1271
1272 r = run_tests(UNIT_FILE_USER, user_tests, argv + 1);
1273 if (r != 0)
1274 return r;
1275
1276 return run_tests(UNIT_FILE_SYSTEM, system_tests, argv + 1);
1277 #else
1278 return 0;
1279 #endif
1280 }