]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-execute.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / test / test-execute.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <grp.h>
4 #include <pwd.h>
5 #include <stdio.h>
6 #include <sys/prctl.h>
7 #include <sys/types.h>
8
9 #include "capability-util.h"
10 #include "cpu-set-util.h"
11 #include "errno-list.h"
12 #include "fileio.h"
13 #include "fs-util.h"
14 #include "macro.h"
15 #include "manager.h"
16 #include "mkdir.h"
17 #include "path-util.h"
18 #include "rm-rf.h"
19 #if HAVE_SECCOMP
20 #include "seccomp-util.h"
21 #endif
22 #include "service.h"
23 #include "stat-util.h"
24 #include "test-helper.h"
25 #include "tests.h"
26 #include "unit.h"
27 #include "user-util.h"
28 #include "util.h"
29 #include "virt.h"
30
31 static bool can_unshare;
32
33 typedef void (*test_function_t)(Manager *m);
34
35 static void check(Manager *m, Unit *unit, int status_expected, int code_expected) {
36 Service *service = NULL;
37 usec_t ts;
38 usec_t timeout = 2 * USEC_PER_MINUTE;
39
40 assert_se(m);
41 assert_se(unit);
42
43 service = SERVICE(unit);
44 printf("%s\n", unit->id);
45 exec_context_dump(&service->exec_context, stdout, "\t");
46 ts = now(CLOCK_MONOTONIC);
47 while (!IN_SET(service->state, SERVICE_DEAD, SERVICE_FAILED)) {
48 int r;
49 usec_t n;
50
51 r = sd_event_run(m->event, 100 * USEC_PER_MSEC);
52 assert_se(r >= 0);
53
54 n = now(CLOCK_MONOTONIC);
55 if (ts + timeout < n) {
56 log_error("Test timeout when testing %s", unit->id);
57 exit(EXIT_FAILURE);
58 }
59 }
60 exec_status_dump(&service->main_exec_status, stdout, "\t");
61 assert_se(service->main_exec_status.status == status_expected);
62 assert_se(service->main_exec_status.code == code_expected);
63 }
64
65 static bool check_nobody_user_and_group(void) {
66 static int cache = -1;
67 struct passwd *p;
68 struct group *g;
69
70 if (cache >= 0)
71 return !!cache;
72
73 if (!synthesize_nobody())
74 goto invalid;
75
76 p = getpwnam(NOBODY_USER_NAME);
77 if (!p ||
78 !streq(p->pw_name, NOBODY_USER_NAME) ||
79 p->pw_uid != UID_NOBODY ||
80 p->pw_gid != GID_NOBODY)
81 goto invalid;
82
83 p = getpwuid(UID_NOBODY);
84 if (!p ||
85 !streq(p->pw_name, NOBODY_USER_NAME) ||
86 p->pw_uid != UID_NOBODY ||
87 p->pw_gid != GID_NOBODY)
88 goto invalid;
89
90 g = getgrnam(NOBODY_GROUP_NAME);
91 if (!g ||
92 !streq(g->gr_name, NOBODY_GROUP_NAME) ||
93 g->gr_gid != GID_NOBODY)
94 goto invalid;
95
96 g = getgrgid(GID_NOBODY);
97 if (!g ||
98 !streq(g->gr_name, NOBODY_GROUP_NAME) ||
99 g->gr_gid != GID_NOBODY)
100 goto invalid;
101
102 cache = 1;
103 return true;
104
105 invalid:
106 cache = 0;
107 return false;
108 }
109
110 static bool check_user_has_group_with_same_name(const char *name) {
111 struct passwd *p;
112 struct group *g;
113
114 assert(name);
115
116 p = getpwnam(name);
117 if (!p ||
118 !streq(p->pw_name, name))
119 return false;
120
121 g = getgrgid(p->pw_gid);
122 if (!g ||
123 !streq(g->gr_name, name))
124 return false;
125
126 return true;
127 }
128
129 static bool is_inaccessible_available(void) {
130 char *p;
131
132 FOREACH_STRING(p,
133 "/run/systemd/inaccessible/reg",
134 "/run/systemd/inaccessible/dir",
135 "/run/systemd/inaccessible/chr",
136 "/run/systemd/inaccessible/blk",
137 "/run/systemd/inaccessible/fifo",
138 "/run/systemd/inaccessible/sock"
139 ) {
140 if (access(p, F_OK) < 0)
141 return false;
142 }
143
144 return true;
145 }
146
147 static void test(Manager *m, const char *unit_name, int status_expected, int code_expected) {
148 Unit *unit;
149
150 assert_se(unit_name);
151
152 assert_se(manager_load_startable_unit_or_warn(m, unit_name, NULL, &unit) >= 0);
153 assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
154 check(m, unit, status_expected, code_expected);
155 }
156
157 static void test_exec_bindpaths(Manager *m) {
158 assert_se(mkdir_p("/tmp/test-exec-bindpaths", 0755) >= 0);
159 assert_se(mkdir_p("/tmp/test-exec-bindreadonlypaths", 0755) >= 0);
160
161 test(m, "exec-bindpaths.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
162
163 (void) rm_rf("/tmp/test-exec-bindpaths", REMOVE_ROOT|REMOVE_PHYSICAL);
164 (void) rm_rf("/tmp/test-exec-bindreadonlypaths", REMOVE_ROOT|REMOVE_PHYSICAL);
165 }
166
167 static void test_exec_cpuaffinity(Manager *m) {
168 _cleanup_cpu_free_ cpu_set_t *c = NULL;
169 unsigned n;
170
171 assert_se(c = cpu_set_malloc(&n));
172 assert_se(sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0);
173
174 if (CPU_ISSET_S(0, CPU_ALLOC_SIZE(n), c) == 0) {
175 log_notice("Cannot use CPU 0, skipping %s", __func__);
176 return;
177 }
178
179 test(m, "exec-cpuaffinity1.service", 0, CLD_EXITED);
180 test(m, "exec-cpuaffinity2.service", 0, CLD_EXITED);
181
182 if (CPU_ISSET_S(1, CPU_ALLOC_SIZE(n), c) == 0 ||
183 CPU_ISSET_S(2, CPU_ALLOC_SIZE(n), c) == 0) {
184 log_notice("Cannot use CPU 1 or 2, skipping remaining tests in %s", __func__);
185 return;
186 }
187
188 test(m, "exec-cpuaffinity3.service", 0, CLD_EXITED);
189 }
190
191 static void test_exec_workingdirectory(Manager *m) {
192 assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0);
193
194 test(m, "exec-workingdirectory.service", 0, CLD_EXITED);
195 test(m, "exec-workingdirectory-trailing-dot.service", 0, CLD_EXITED);
196
197 (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT|REMOVE_PHYSICAL);
198 }
199
200 static void test_exec_personality(Manager *m) {
201 #if defined(__x86_64__)
202 test(m, "exec-personality-x86-64.service", 0, CLD_EXITED);
203
204 #elif defined(__s390__)
205 test(m, "exec-personality-s390.service", 0, CLD_EXITED);
206
207 #elif defined(__powerpc64__)
208 # if __BYTE_ORDER == __BIG_ENDIAN
209 test(m, "exec-personality-ppc64.service", 0, CLD_EXITED);
210 # else
211 test(m, "exec-personality-ppc64le.service", 0, CLD_EXITED);
212 # endif
213
214 #elif defined(__aarch64__)
215 test(m, "exec-personality-aarch64.service", 0, CLD_EXITED);
216
217 #elif defined(__i386__)
218 test(m, "exec-personality-x86.service", 0, CLD_EXITED);
219 #else
220 log_notice("Unknown personality, skipping %s", __func__);
221 #endif
222 }
223
224 static void test_exec_ignoresigpipe(Manager *m) {
225 test(m, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED);
226 test(m, "exec-ignoresigpipe-no.service", SIGPIPE, CLD_KILLED);
227 }
228
229 static void test_exec_privatetmp(Manager *m) {
230 assert_se(touch("/tmp/test-exec_privatetmp") >= 0);
231
232 test(m, "exec-privatetmp-yes.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
233 test(m, "exec-privatetmp-no.service", 0, CLD_EXITED);
234
235 unlink("/tmp/test-exec_privatetmp");
236 }
237
238 static void test_exec_privatedevices(Manager *m) {
239 int r;
240
241 if (detect_container() > 0) {
242 log_notice("Testing in container, skipping %s", __func__);
243 return;
244 }
245 if (!is_inaccessible_available()) {
246 log_notice("Testing without inaccessible, skipping %s", __func__);
247 return;
248 }
249
250 test(m, "exec-privatedevices-yes.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
251 test(m, "exec-privatedevices-no.service", 0, CLD_EXITED);
252 test(m, "exec-privatedevices-disabled-by-prefix.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
253
254 /* We use capsh to test if the capabilities are
255 * properly set, so be sure that it exists */
256 r = find_binary("capsh", NULL);
257 if (r < 0) {
258 log_notice_errno(r, "Could not find capsh binary, skipping remaining tests in %s: %m", __func__);
259 return;
260 }
261
262 test(m, "exec-privatedevices-yes-capability-mknod.service", 0, CLD_EXITED);
263 test(m, "exec-privatedevices-no-capability-mknod.service", 0, CLD_EXITED);
264 test(m, "exec-privatedevices-yes-capability-sys-rawio.service", 0, CLD_EXITED);
265 test(m, "exec-privatedevices-no-capability-sys-rawio.service", 0, CLD_EXITED);
266 }
267
268 static void test_exec_protectkernelmodules(Manager *m) {
269 int r;
270
271 if (detect_container() > 0) {
272 log_notice("Testing in container, skipping %s", __func__);
273 return;
274 }
275 if (!is_inaccessible_available()) {
276 log_notice("Testing without inaccessible, skipping %s", __func__);
277 return;
278 }
279
280 r = find_binary("capsh", NULL);
281 if (r < 0) {
282 log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
283 return;
284 }
285
286 test(m, "exec-protectkernelmodules-no-capabilities.service", 0, CLD_EXITED);
287 test(m, "exec-protectkernelmodules-yes-capabilities.service", 0, CLD_EXITED);
288 test(m, "exec-protectkernelmodules-yes-mount-propagation.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
289 }
290
291 static void test_exec_readonlypaths(Manager *m) {
292
293 test(m, "exec-readonlypaths-simple.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
294
295 if (path_is_read_only_fs("/var") > 0) {
296 log_notice("Directory /var is readonly, skipping remaining tests in %s", __func__);
297 return;
298 }
299
300 test(m, "exec-readonlypaths.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
301 test(m, "exec-readonlypaths-with-bindpaths.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
302 test(m, "exec-readonlypaths-mount-propagation.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
303 }
304
305 static void test_exec_readwritepaths(Manager *m) {
306
307 if (path_is_read_only_fs("/") > 0) {
308 log_notice("Root directory is readonly, skipping %s", __func__);
309 return;
310 }
311
312 test(m, "exec-readwritepaths-mount-propagation.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
313 }
314
315 static void test_exec_inaccessiblepaths(Manager *m) {
316
317 if (!is_inaccessible_available()) {
318 log_notice("Testing without inaccessible, skipping %s", __func__);
319 return;
320 }
321
322 test(m, "exec-inaccessiblepaths-proc.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
323
324 if (path_is_read_only_fs("/") > 0) {
325 log_notice("Root directory is readonly, skipping remaining tests in %s", __func__);
326 return;
327 }
328
329 test(m, "exec-inaccessiblepaths-mount-propagation.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
330 }
331
332 static void test_exec_temporaryfilesystem(Manager *m) {
333
334 test(m, "exec-temporaryfilesystem-options.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
335 test(m, "exec-temporaryfilesystem-ro.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
336 test(m, "exec-temporaryfilesystem-rw.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
337 test(m, "exec-temporaryfilesystem-usr.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
338 }
339
340 static void test_exec_systemcallfilter(Manager *m) {
341 #if HAVE_SECCOMP
342 int r;
343
344 if (!is_seccomp_available()) {
345 log_notice("Seccomp not available, skipping %s", __func__);
346 return;
347 }
348
349 test(m, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED);
350 test(m, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED);
351 test(m, "exec-systemcallfilter-failing.service", SIGSYS, CLD_KILLED);
352 test(m, "exec-systemcallfilter-failing2.service", SIGSYS, CLD_KILLED);
353
354 r = find_binary("python3", NULL);
355 if (r < 0) {
356 log_notice_errno(r, "Skipping remaining tests in %s, could not find python3 binary: %m", __func__);
357 return;
358 }
359
360 test(m, "exec-systemcallfilter-with-errno-name.service", errno_from_name("EILSEQ"), CLD_EXITED);
361 test(m, "exec-systemcallfilter-with-errno-number.service", 255, CLD_EXITED);
362 test(m, "exec-systemcallfilter-with-errno-multi.service", errno_from_name("EILSEQ"), CLD_EXITED);
363 #endif
364 }
365
366 static void test_exec_systemcallerrornumber(Manager *m) {
367 #if HAVE_SECCOMP
368 int r;
369
370 if (!is_seccomp_available()) {
371 log_notice("Seccomp not available, skipping %s", __func__);
372 return;
373 }
374
375 r = find_binary("python3", NULL);
376 if (r < 0) {
377 log_notice_errno(r, "Skipping %s, could not find python3 binary: %m", __func__);
378 return;
379 }
380
381 test(m, "exec-systemcallerrornumber-name.service", errno_from_name("EACCES"), CLD_EXITED);
382 test(m, "exec-systemcallerrornumber-number.service", 255, CLD_EXITED);
383 #endif
384 }
385
386 static void test_exec_restrictnamespaces(Manager *m) {
387 #if HAVE_SECCOMP
388 if (!is_seccomp_available()) {
389 log_notice("Seccomp not available, skipping %s", __func__);
390 return;
391 }
392
393 test(m, "exec-restrictnamespaces-no.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
394 test(m, "exec-restrictnamespaces-yes.service", 1, CLD_EXITED);
395 test(m, "exec-restrictnamespaces-mnt.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
396 test(m, "exec-restrictnamespaces-mnt-blacklist.service", 1, CLD_EXITED);
397 test(m, "exec-restrictnamespaces-merge-and.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
398 test(m, "exec-restrictnamespaces-merge-or.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
399 test(m, "exec-restrictnamespaces-merge-all.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
400 #endif
401 }
402
403 static void test_exec_systemcallfilter_system(Manager *m) {
404 #if HAVE_SECCOMP
405 if (!is_seccomp_available()) {
406 log_notice("Seccomp not available, skipping %s", __func__);
407 return;
408 }
409
410 test(m, "exec-systemcallfilter-system-user.service", 0, CLD_EXITED);
411
412 if (!check_nobody_user_and_group()) {
413 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
414 return;
415 }
416
417 if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
418 log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
419 return;
420 }
421
422 test(m, "exec-systemcallfilter-system-user-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
423 #endif
424 }
425
426 static void test_exec_user(Manager *m) {
427 test(m, "exec-user.service", 0, CLD_EXITED);
428
429 if (!check_nobody_user_and_group()) {
430 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
431 return;
432 }
433
434 if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
435 log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
436 return;
437 }
438
439 test(m, "exec-user-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
440 }
441
442 static void test_exec_group(Manager *m) {
443 test(m, "exec-group.service", 0, CLD_EXITED);
444
445 if (!check_nobody_user_and_group()) {
446 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
447 return;
448 }
449
450 if (!STR_IN_SET(NOBODY_GROUP_NAME, "nobody", "nfsnobody", "nogroup")) {
451 log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME, __func__);
452 return;
453 }
454
455 test(m, "exec-group-" NOBODY_GROUP_NAME ".service", 0, CLD_EXITED);
456 }
457
458 static void test_exec_supplementarygroups(Manager *m) {
459 test(m, "exec-supplementarygroups.service", 0, CLD_EXITED);
460 test(m, "exec-supplementarygroups-single-group.service", 0, CLD_EXITED);
461 test(m, "exec-supplementarygroups-single-group-user.service", 0, CLD_EXITED);
462 test(m, "exec-supplementarygroups-multiple-groups-default-group-user.service", 0, CLD_EXITED);
463 test(m, "exec-supplementarygroups-multiple-groups-withgid.service", 0, CLD_EXITED);
464 test(m, "exec-supplementarygroups-multiple-groups-withuid.service", 0, CLD_EXITED);
465 }
466
467 static void test_exec_dynamicuser(Manager *m) {
468
469 test(m, "exec-dynamicuser-fixeduser.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
470 if (check_user_has_group_with_same_name("adm"))
471 test(m, "exec-dynamicuser-fixeduser-adm.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
472 if (check_user_has_group_with_same_name("games"))
473 test(m, "exec-dynamicuser-fixeduser-games.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
474 test(m, "exec-dynamicuser-fixeduser-one-supplementarygroup.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
475 test(m, "exec-dynamicuser-supplementarygroups.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
476 test(m, "exec-dynamicuser-statedir.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
477
478 (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
479 (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
480 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
481 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
482
483 test(m, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED);
484 test(m, "exec-dynamicuser-statedir-migrate-step2.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
485
486 (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
487 (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
488 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
489 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
490 }
491
492 static void test_exec_environment(Manager *m) {
493 test(m, "exec-environment.service", 0, CLD_EXITED);
494 test(m, "exec-environment-multiple.service", 0, CLD_EXITED);
495 test(m, "exec-environment-empty.service", 0, CLD_EXITED);
496 }
497
498 static void test_exec_environmentfile(Manager *m) {
499 static const char e[] =
500 "VAR1='word1 word2'\n"
501 "VAR2=word3 \n"
502 "# comment1\n"
503 "\n"
504 "; comment2\n"
505 " ; # comment3\n"
506 "line without an equal\n"
507 "VAR3='$word 5 6'\n"
508 "VAR4='new\nline'\n"
509 "VAR5=password\\with\\backslashes";
510 int r;
511
512 r = write_string_file("/tmp/test-exec_environmentfile.conf", e, WRITE_STRING_FILE_CREATE);
513 assert_se(r == 0);
514
515 test(m, "exec-environmentfile.service", 0, CLD_EXITED);
516
517 (void) unlink("/tmp/test-exec_environmentfile.conf");
518 }
519
520 static void test_exec_passenvironment(Manager *m) {
521 /* test-execute runs under MANAGER_USER which, by default, forwards all
522 * variables present in the environment, but only those that are
523 * present _at the time it is created_!
524 *
525 * So these PassEnvironment checks are still expected to work, since we
526 * are ensuring the variables are not present at manager creation (they
527 * are unset explicitly in main) and are only set here.
528 *
529 * This is still a good approximation of how a test for MANAGER_SYSTEM
530 * would work.
531 */
532 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
533 assert_se(setenv("VAR2", "word3", 1) == 0);
534 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
535 assert_se(setenv("VAR4", "new\nline", 1) == 0);
536 assert_se(setenv("VAR5", "passwordwithbackslashes", 1) == 0);
537 test(m, "exec-passenvironment.service", 0, CLD_EXITED);
538 test(m, "exec-passenvironment-repeated.service", 0, CLD_EXITED);
539 test(m, "exec-passenvironment-empty.service", 0, CLD_EXITED);
540 assert_se(unsetenv("VAR1") == 0);
541 assert_se(unsetenv("VAR2") == 0);
542 assert_se(unsetenv("VAR3") == 0);
543 assert_se(unsetenv("VAR4") == 0);
544 assert_se(unsetenv("VAR5") == 0);
545 test(m, "exec-passenvironment-absent.service", 0, CLD_EXITED);
546 }
547
548 static void test_exec_umask(Manager *m) {
549 test(m, "exec-umask-default.service", 0, CLD_EXITED);
550 test(m, "exec-umask-0177.service", 0, CLD_EXITED);
551 }
552
553 static void test_exec_runtimedirectory(Manager *m) {
554 test(m, "exec-runtimedirectory.service", 0, CLD_EXITED);
555 test(m, "exec-runtimedirectory-mode.service", 0, CLD_EXITED);
556 test(m, "exec-runtimedirectory-owner.service", 0, CLD_EXITED);
557
558 if (!check_nobody_user_and_group()) {
559 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
560 return;
561 }
562
563 if (!STR_IN_SET(NOBODY_GROUP_NAME, "nobody", "nfsnobody", "nogroup")) {
564 log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME, __func__);
565 return;
566 }
567
568 test(m, "exec-runtimedirectory-owner-" NOBODY_GROUP_NAME ".service", 0, CLD_EXITED);
569 }
570
571 static void test_exec_capabilityboundingset(Manager *m) {
572 int r;
573
574 r = find_binary("capsh", NULL);
575 if (r < 0) {
576 log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
577 return;
578 }
579
580 if (have_effective_cap(CAP_CHOWN) <= 0 ||
581 have_effective_cap(CAP_FOWNER) <= 0 ||
582 have_effective_cap(CAP_KILL) <= 0) {
583 log_notice("Skipping %s, this process does not have enough capabilities", __func__);
584 return;
585 }
586
587 test(m, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED);
588 test(m, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED);
589 test(m, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED);
590 test(m, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED);
591 }
592
593 static void test_exec_basic(Manager *m) {
594 test(m, "exec-basic.service", 0, CLD_EXITED);
595 }
596
597 static void test_exec_ambientcapabilities(Manager *m) {
598 int r;
599
600 /* Check if the kernel has support for ambient capabilities. Run
601 * the tests only if that's the case. Clearing all ambient
602 * capabilities is fine, since we are expecting them to be unset
603 * in the first place for the tests. */
604 r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
605 if (r < 0 && IN_SET(errno, EINVAL, EOPNOTSUPP, ENOSYS)) {
606 log_notice("Skipping %s, the kernel does not support ambient capabilities", __func__);
607 return;
608 }
609
610 if (have_effective_cap(CAP_CHOWN) <= 0 ||
611 have_effective_cap(CAP_NET_RAW) <= 0) {
612 log_notice("Skipping %s, this process does not have enough capabilities", __func__);
613 return;
614 }
615
616 test(m, "exec-ambientcapabilities.service", 0, CLD_EXITED);
617 test(m, "exec-ambientcapabilities-merge.service", 0, CLD_EXITED);
618
619 if (!check_nobody_user_and_group()) {
620 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
621 return;
622 }
623
624 if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
625 log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
626 return;
627 }
628
629 test(m, "exec-ambientcapabilities-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
630 test(m, "exec-ambientcapabilities-merge-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
631 }
632
633 static void test_exec_privatenetwork(Manager *m) {
634 int r;
635
636 r = find_binary("ip", NULL);
637 if (r < 0) {
638 log_notice_errno(r, "Skipping %s, could not find ip binary: %m", __func__);
639 return;
640 }
641
642 test(m, "exec-privatenetwork-yes.service", can_unshare ? 0 : EXIT_NETWORK, CLD_EXITED);
643 }
644
645 static void test_exec_oomscoreadjust(Manager *m) {
646 test(m, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED);
647
648 if (detect_container() > 0) {
649 log_notice("Testing in container, skipping remaining tests in %s", __func__);
650 return;
651 }
652 test(m, "exec-oomscoreadjust-negative.service", 0, CLD_EXITED);
653 }
654
655 static void test_exec_ioschedulingclass(Manager *m) {
656 test(m, "exec-ioschedulingclass-none.service", 0, CLD_EXITED);
657 test(m, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED);
658 test(m, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED);
659
660 if (detect_container() > 0) {
661 log_notice("Testing in container, skipping remaining tests in %s", __func__);
662 return;
663 }
664 test(m, "exec-ioschedulingclass-realtime.service", 0, CLD_EXITED);
665 }
666
667 static void test_exec_unsetenvironment(Manager *m) {
668 test(m, "exec-unsetenvironment.service", 0, CLD_EXITED);
669 }
670
671 static void test_exec_specifier(Manager *m) {
672 test(m, "exec-specifier.service", 0, CLD_EXITED);
673 test(m, "exec-specifier@foo-bar.service", 0, CLD_EXITED);
674 test(m, "exec-specifier-interpolation.service", 0, CLD_EXITED);
675 }
676
677 static void test_exec_standardinput(Manager *m) {
678 test(m, "exec-standardinput-data.service", 0, CLD_EXITED);
679 test(m, "exec-standardinput-file.service", 0, CLD_EXITED);
680 }
681
682 static void test_exec_standardoutput(Manager *m) {
683 test(m, "exec-standardoutput-file.service", 0, CLD_EXITED);
684 }
685
686 static void test_exec_standardoutput_append(Manager *m) {
687 test(m, "exec-standardoutput-append.service", 0, CLD_EXITED);
688 }
689
690 static int run_tests(UnitFileScope scope, const test_function_t *tests) {
691 const test_function_t *test = NULL;
692 _cleanup_(manager_freep) Manager *m = NULL;
693 int r;
694
695 assert_se(tests);
696
697 r = manager_new(scope, MANAGER_TEST_RUN_BASIC, &m);
698 if (MANAGER_SKIP_TEST(r))
699 return log_tests_skipped_errno(r, "manager_new");
700 assert_se(r >= 0);
701 assert_se(manager_startup(m, NULL, NULL) >= 0);
702
703 for (test = tests; test && *test; test++)
704 (*test)(m);
705
706 return 0;
707 }
708
709 int main(int argc, char *argv[]) {
710 _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
711 _cleanup_free_ char *test_execute_path = NULL;
712 _cleanup_hashmap_free_ Hashmap *s = NULL;
713 static const test_function_t user_tests[] = {
714 test_exec_basic,
715 test_exec_ambientcapabilities,
716 test_exec_bindpaths,
717 test_exec_capabilityboundingset,
718 test_exec_cpuaffinity,
719 test_exec_environment,
720 test_exec_environmentfile,
721 test_exec_group,
722 test_exec_ignoresigpipe,
723 test_exec_inaccessiblepaths,
724 test_exec_ioschedulingclass,
725 test_exec_oomscoreadjust,
726 test_exec_passenvironment,
727 test_exec_personality,
728 test_exec_privatedevices,
729 test_exec_privatenetwork,
730 test_exec_privatetmp,
731 test_exec_protectkernelmodules,
732 test_exec_readonlypaths,
733 test_exec_readwritepaths,
734 test_exec_restrictnamespaces,
735 test_exec_runtimedirectory,
736 test_exec_standardinput,
737 test_exec_standardoutput,
738 test_exec_standardoutput_append,
739 test_exec_supplementarygroups,
740 test_exec_systemcallerrornumber,
741 test_exec_systemcallfilter,
742 test_exec_temporaryfilesystem,
743 test_exec_umask,
744 test_exec_unsetenvironment,
745 test_exec_user,
746 test_exec_workingdirectory,
747 NULL,
748 };
749 static const test_function_t system_tests[] = {
750 test_exec_dynamicuser,
751 test_exec_specifier,
752 test_exec_systemcallfilter_system,
753 NULL,
754 };
755 int r;
756
757 test_setup_logging(LOG_DEBUG);
758
759 (void) unsetenv("USER");
760 (void) unsetenv("LOGNAME");
761 (void) unsetenv("SHELL");
762
763 can_unshare = have_namespaces();
764
765 /* It is needed otherwise cgroup creation fails */
766 if (getuid() != 0)
767 return log_tests_skipped("not root");
768
769 r = enter_cgroup_subroot();
770 if (r == -ENOMEDIUM)
771 return log_tests_skipped("cgroupfs not available");
772
773 assert_se(runtime_dir = setup_fake_runtime_dir());
774 test_execute_path = path_join(NULL, get_testdata_dir(), "test-execute");
775 assert_se(set_unit_path(test_execute_path) >= 0);
776
777 /* Unset VAR1, VAR2 and VAR3 which are used in the PassEnvironment test
778 * cases, otherwise (and if they are present in the environment),
779 * `manager_default_environment` will copy them into the default
780 * environment which is passed to each created job, which will make the
781 * tests that expect those not to be present to fail.
782 */
783 assert_se(unsetenv("VAR1") == 0);
784 assert_se(unsetenv("VAR2") == 0);
785 assert_se(unsetenv("VAR3") == 0);
786
787 r = run_tests(UNIT_FILE_USER, user_tests);
788 if (r != 0)
789 return r;
790
791 r = run_tests(UNIT_FILE_SYSTEM, system_tests);
792 if (r != 0)
793 return r;
794
795 #if HAVE_SECCOMP
796 /* The following tests are for 1beab8b0d0ff2d7d1436b52d4a0c3d56dc908962. */
797 if (!is_seccomp_available()) {
798 log_notice("Seccomp not available, skipping unshare() filtered tests.");
799 return 0;
800 }
801
802 assert_se(s = hashmap_new(NULL));
803 r = seccomp_syscall_resolve_name("unshare");
804 assert_se(r != __NR_SCMP_ERROR);
805 assert_se(hashmap_put(s, UINT32_TO_PTR(r + 1), INT_TO_PTR(-1)) >= 0);
806 assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EOPNOTSUPP), true) >= 0);
807 assert_se(unshare(CLONE_NEWNS) < 0);
808 assert_se(errno == EOPNOTSUPP);
809
810 can_unshare = false;
811
812 r = run_tests(UNIT_FILE_USER, user_tests);
813 if (r != 0)
814 return r;
815
816 return run_tests(UNIT_FILE_SYSTEM, system_tests);
817 #else
818 return 0;
819 #endif
820 }