1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "capability-util.h"
8 #include "cpu-set-util.h"
9 #include "errno-list.h"
14 #include "missing_prctl.h"
16 #include "path-util.h"
19 #include "seccomp-util.h"
22 #include "stat-util.h"
25 #include "user-util.h"
29 static bool can_unshare
;
31 typedef void (*test_function_t
)(Manager
*m
);
33 static int cld_dumped_to_killed(int code
) {
34 /* Depending on the system, seccomp version, … some signals might result in dumping, others in plain
35 * killing. Let's ignore the difference here, and map both cases to CLD_KILLED */
36 return code
== CLD_DUMPED
? CLD_KILLED
: code
;
39 static void wait_for_service_finish(Manager
*m
, Unit
*unit
) {
40 Service
*service
= NULL
;
42 usec_t timeout
= 2 * USEC_PER_MINUTE
;
47 service
= SERVICE(unit
);
48 printf("%s\n", unit
->id
);
49 exec_context_dump(&service
->exec_context
, stdout
, "\t");
50 ts
= now(CLOCK_MONOTONIC
);
51 while (!IN_SET(service
->state
, SERVICE_DEAD
, SERVICE_FAILED
)) {
55 r
= sd_event_run(m
->event
, 100 * USEC_PER_MSEC
);
58 n
= now(CLOCK_MONOTONIC
);
59 if (ts
+ timeout
< n
) {
60 log_error("Test timeout when testing %s", unit
->id
);
61 r
= unit_kill(unit
, KILL_ALL
, SIGKILL
, NULL
);
63 log_error_errno(r
, "Failed to kill %s: %m", unit
->id
);
69 static void check_main_result(const char *file
, unsigned line
, const char *func
,
70 Manager
*m
, Unit
*unit
, int status_expected
, int code_expected
) {
71 Service
*service
= NULL
;
76 wait_for_service_finish(m
, unit
);
78 service
= SERVICE(unit
);
79 exec_status_dump(&service
->main_exec_status
, stdout
, "\t");
81 if (cld_dumped_to_killed(service
->main_exec_status
.code
) != cld_dumped_to_killed(code_expected
)) {
82 log_error("%s:%u:%s %s: exit code %d, expected %d",
85 service
->main_exec_status
.code
, code_expected
);
89 if (service
->main_exec_status
.status
!= status_expected
) {
90 log_error("%s:%u:%s: %s: exit status %d, expected %d",
91 file
, line
, func
, unit
->id
,
92 service
->main_exec_status
.status
, status_expected
);
97 static void check_service_result(const char *file
, unsigned line
, const char *func
,
98 Manager
*m
, Unit
*unit
, ServiceResult result_expected
) {
99 Service
*service
= NULL
;
104 wait_for_service_finish(m
, unit
);
106 service
= SERVICE(unit
);
108 if (service
->result
!= result_expected
) {
109 log_error("%s:%u:%s: %s: service end result %s, expected %s",
112 service_result_to_string(service
->result
),
113 service_result_to_string(result_expected
));
118 static bool check_nobody_user_and_group(void) {
119 static int cache
= -1;
126 if (!synthesize_nobody())
129 p
= getpwnam(NOBODY_USER_NAME
);
131 !streq(p
->pw_name
, NOBODY_USER_NAME
) ||
132 p
->pw_uid
!= UID_NOBODY
||
133 p
->pw_gid
!= GID_NOBODY
)
136 p
= getpwuid(UID_NOBODY
);
138 !streq(p
->pw_name
, NOBODY_USER_NAME
) ||
139 p
->pw_uid
!= UID_NOBODY
||
140 p
->pw_gid
!= GID_NOBODY
)
143 g
= getgrnam(NOBODY_GROUP_NAME
);
145 !streq(g
->gr_name
, NOBODY_GROUP_NAME
) ||
146 g
->gr_gid
!= GID_NOBODY
)
149 g
= getgrgid(GID_NOBODY
);
151 !streq(g
->gr_name
, NOBODY_GROUP_NAME
) ||
152 g
->gr_gid
!= GID_NOBODY
)
163 static bool check_user_has_group_with_same_name(const char *name
) {
171 !streq(p
->pw_name
, name
))
174 g
= getgrgid(p
->pw_gid
);
176 !streq(g
->gr_name
, name
))
182 static bool is_inaccessible_available(void) {
186 "/run/systemd/inaccessible/reg",
187 "/run/systemd/inaccessible/dir",
188 "/run/systemd/inaccessible/chr",
189 "/run/systemd/inaccessible/blk",
190 "/run/systemd/inaccessible/fifo",
191 "/run/systemd/inaccessible/sock"
193 if (access(p
, F_OK
) < 0)
200 static void _test(const char *file
, unsigned line
, const char *func
,
201 Manager
*m
, const char *unit_name
, int status_expected
, int code_expected
) {
204 assert_se(unit_name
);
206 assert_se(manager_load_startable_unit_or_warn(m
, unit_name
, NULL
, &unit
) >= 0);
207 assert_se(unit_start(unit
) >= 0);
208 check_main_result(file
, line
, func
, m
, unit
, status_expected
, code_expected
);
210 #define test(m, unit_name, status_expected, code_expected) \
211 _test(PROJECT_FILE, __LINE__, __func__, m, unit_name, status_expected, code_expected)
213 static void _test_service(const char *file
, unsigned line
, const char *func
,
214 Manager
*m
, const char *unit_name
, ServiceResult result_expected
) {
217 assert_se(unit_name
);
219 assert_se(manager_load_startable_unit_or_warn(m
, unit_name
, NULL
, &unit
) >= 0);
220 assert_se(unit_start(unit
) >= 0);
221 check_service_result(file
, line
, func
, m
, unit
, result_expected
);
223 #define test_service(m, unit_name, result_expected) \
224 _test_service(PROJECT_FILE, __LINE__, __func__, m, unit_name, result_expected)
226 static void test_exec_bindpaths(Manager
*m
) {
227 assert_se(mkdir_p("/tmp/test-exec-bindpaths", 0755) >= 0);
228 assert_se(mkdir_p("/tmp/test-exec-bindreadonlypaths", 0755) >= 0);
230 test(m
, "exec-bindpaths.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
232 (void) rm_rf("/tmp/test-exec-bindpaths", REMOVE_ROOT
|REMOVE_PHYSICAL
);
233 (void) rm_rf("/tmp/test-exec-bindreadonlypaths", REMOVE_ROOT
|REMOVE_PHYSICAL
);
236 static void test_exec_cpuaffinity(Manager
*m
) {
237 _cleanup_(cpu_set_reset
) CPUSet c
= {};
239 assert_se(cpu_set_realloc(&c
, 8192) >= 0); /* just allocate the maximum possible size */
240 assert_se(sched_getaffinity(0, c
.allocated
, c
.set
) >= 0);
242 if (!CPU_ISSET_S(0, c
.allocated
, c
.set
)) {
243 log_notice("Cannot use CPU 0, skipping %s", __func__
);
247 test(m
, "exec-cpuaffinity1.service", 0, CLD_EXITED
);
248 test(m
, "exec-cpuaffinity2.service", 0, CLD_EXITED
);
250 if (!CPU_ISSET_S(1, c
.allocated
, c
.set
) ||
251 !CPU_ISSET_S(2, c
.allocated
, c
.set
)) {
252 log_notice("Cannot use CPU 1 or 2, skipping remaining tests in %s", __func__
);
256 test(m
, "exec-cpuaffinity3.service", 0, CLD_EXITED
);
259 static void test_exec_workingdirectory(Manager
*m
) {
260 assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0);
262 test(m
, "exec-workingdirectory.service", 0, CLD_EXITED
);
263 test(m
, "exec-workingdirectory-trailing-dot.service", 0, CLD_EXITED
);
265 (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT
|REMOVE_PHYSICAL
);
268 static void test_exec_personality(Manager
*m
) {
269 #if defined(__x86_64__)
270 test(m
, "exec-personality-x86-64.service", 0, CLD_EXITED
);
272 #elif defined(__s390__)
273 test(m
, "exec-personality-s390.service", 0, CLD_EXITED
);
275 #elif defined(__powerpc64__)
276 # if __BYTE_ORDER == __BIG_ENDIAN
277 test(m
, "exec-personality-ppc64.service", 0, CLD_EXITED
);
279 test(m
, "exec-personality-ppc64le.service", 0, CLD_EXITED
);
282 #elif defined(__aarch64__)
283 test(m
, "exec-personality-aarch64.service", 0, CLD_EXITED
);
285 #elif defined(__i386__)
286 test(m
, "exec-personality-x86.service", 0, CLD_EXITED
);
288 log_notice("Unknown personality, skipping %s", __func__
);
292 static void test_exec_ignoresigpipe(Manager
*m
) {
293 test(m
, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED
);
294 test(m
, "exec-ignoresigpipe-no.service", SIGPIPE
, CLD_KILLED
);
297 static void test_exec_privatetmp(Manager
*m
) {
298 assert_se(touch("/tmp/test-exec_privatetmp") >= 0);
300 test(m
, "exec-privatetmp-yes.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
301 test(m
, "exec-privatetmp-no.service", 0, CLD_EXITED
);
302 test(m
, "exec-privatetmp-disabled-by-prefix.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
304 unlink("/tmp/test-exec_privatetmp");
307 static void test_exec_privatedevices(Manager
*m
) {
310 if (detect_container() > 0) {
311 log_notice("Testing in container, skipping %s", __func__
);
314 if (!is_inaccessible_available()) {
315 log_notice("Testing without inaccessible, skipping %s", __func__
);
319 test(m
, "exec-privatedevices-yes.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
320 test(m
, "exec-privatedevices-no.service", 0, CLD_EXITED
);
321 test(m
, "exec-privatedevices-disabled-by-prefix.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
322 test(m
, "exec-privatedevices-yes-with-group.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
324 /* We use capsh to test if the capabilities are
325 * properly set, so be sure that it exists */
326 r
= find_executable("capsh", NULL
);
328 log_notice_errno(r
, "Could not find capsh binary, skipping remaining tests in %s: %m", __func__
);
332 test(m
, "exec-privatedevices-yes-capability-mknod.service", 0, CLD_EXITED
);
333 test(m
, "exec-privatedevices-no-capability-mknod.service", 0, CLD_EXITED
);
334 test(m
, "exec-privatedevices-yes-capability-sys-rawio.service", 0, CLD_EXITED
);
335 test(m
, "exec-privatedevices-no-capability-sys-rawio.service", 0, CLD_EXITED
);
338 static void test_exec_protecthome(Manager
*m
) {
340 log_notice("Cannot reliably unshare, skipping %s", __func__
);
344 test(m
, "exec-protecthome-tmpfs-vs-protectsystem-strict.service", 0, CLD_EXITED
);
347 static void test_exec_protectkernelmodules(Manager
*m
) {
350 if (detect_container() > 0) {
351 log_notice("Testing in container, skipping %s", __func__
);
354 if (!is_inaccessible_available()) {
355 log_notice("Testing without inaccessible, skipping %s", __func__
);
359 r
= find_executable("capsh", NULL
);
361 log_notice_errno(r
, "Skipping %s, could not find capsh binary: %m", __func__
);
365 test(m
, "exec-protectkernelmodules-no-capabilities.service", 0, CLD_EXITED
);
366 test(m
, "exec-protectkernelmodules-yes-capabilities.service", 0, CLD_EXITED
);
367 test(m
, "exec-protectkernelmodules-yes-mount-propagation.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
370 static void test_exec_readonlypaths(Manager
*m
) {
372 test(m
, "exec-readonlypaths-simple.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
374 if (path_is_read_only_fs("/var") > 0) {
375 log_notice("Directory /var is readonly, skipping remaining tests in %s", __func__
);
379 test(m
, "exec-readonlypaths.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
380 test(m
, "exec-readonlypaths-with-bindpaths.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
381 test(m
, "exec-readonlypaths-mount-propagation.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
384 static void test_exec_readwritepaths(Manager
*m
) {
386 if (path_is_read_only_fs("/") > 0) {
387 log_notice("Root directory is readonly, skipping %s", __func__
);
391 test(m
, "exec-readwritepaths-mount-propagation.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
394 static void test_exec_inaccessiblepaths(Manager
*m
) {
396 if (!is_inaccessible_available()) {
397 log_notice("Testing without inaccessible, skipping %s", __func__
);
401 test(m
, "exec-inaccessiblepaths-sys.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
403 if (path_is_read_only_fs("/") > 0) {
404 log_notice("Root directory is readonly, skipping remaining tests in %s", __func__
);
408 test(m
, "exec-inaccessiblepaths-mount-propagation.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
411 static void test_exec_noexecpaths(Manager
*m
) {
413 test(m
, "exec-noexecpaths-simple.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
416 static void test_exec_temporaryfilesystem(Manager
*m
) {
418 test(m
, "exec-temporaryfilesystem-options.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
419 test(m
, "exec-temporaryfilesystem-ro.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
420 test(m
, "exec-temporaryfilesystem-rw.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
421 test(m
, "exec-temporaryfilesystem-usr.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
424 static void test_exec_systemcallfilter(Manager
*m
) {
428 if (!is_seccomp_available()) {
429 log_notice("Seccomp not available, skipping %s", __func__
);
433 test(m
, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED
);
434 test(m
, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED
);
435 test(m
, "exec-systemcallfilter-failing.service", SIGSYS
, CLD_KILLED
);
436 test(m
, "exec-systemcallfilter-failing2.service", SIGSYS
, CLD_KILLED
);
438 r
= find_executable("python3", NULL
);
440 log_notice_errno(r
, "Skipping remaining tests in %s, could not find python3 binary: %m", __func__
);
444 test(m
, "exec-systemcallfilter-with-errno-name.service", errno_from_name("EILSEQ"), CLD_EXITED
);
445 test(m
, "exec-systemcallfilter-with-errno-number.service", 255, CLD_EXITED
);
446 test(m
, "exec-systemcallfilter-with-errno-multi.service", errno_from_name("EILSEQ"), CLD_EXITED
);
447 test(m
, "exec-systemcallfilter-with-errno-in-allow-list.service", errno_from_name("EILSEQ"), CLD_EXITED
);
448 test(m
, "exec-systemcallfilter-override-error-action.service", SIGSYS
, CLD_KILLED
);
449 test(m
, "exec-systemcallfilter-override-error-action2.service", errno_from_name("EILSEQ"), CLD_EXITED
);
453 static void test_exec_systemcallerrornumber(Manager
*m
) {
457 if (!is_seccomp_available()) {
458 log_notice("Seccomp not available, skipping %s", __func__
);
462 r
= find_executable("python3", NULL
);
464 log_notice_errno(r
, "Skipping %s, could not find python3 binary: %m", __func__
);
468 test(m
, "exec-systemcallerrornumber-name.service", errno_from_name("EACCES"), CLD_EXITED
);
469 test(m
, "exec-systemcallerrornumber-number.service", 255, CLD_EXITED
);
473 static void test_exec_restrictnamespaces(Manager
*m
) {
475 if (!is_seccomp_available()) {
476 log_notice("Seccomp not available, skipping %s", __func__
);
480 test(m
, "exec-restrictnamespaces-no.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
481 test(m
, "exec-restrictnamespaces-yes.service", 1, CLD_EXITED
);
482 test(m
, "exec-restrictnamespaces-mnt.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
483 test(m
, "exec-restrictnamespaces-mnt-deny-list.service", 1, CLD_EXITED
);
484 test(m
, "exec-restrictnamespaces-merge-and.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
485 test(m
, "exec-restrictnamespaces-merge-or.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
486 test(m
, "exec-restrictnamespaces-merge-all.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
490 static void test_exec_systemcallfilter_system(Manager
*m
) {
491 /* Skip this particular test case when running under ASan, as
492 * LSan intermittently segfaults when accessing memory right
493 * after the test finishes. Generally, ASan & LSan don't like
496 #if HAVE_SECCOMP && !HAS_FEATURE_ADDRESS_SANITIZER
497 if (!is_seccomp_available()) {
498 log_notice("Seccomp not available, skipping %s", __func__
);
502 test(m
, "exec-systemcallfilter-system-user.service", 0, CLD_EXITED
);
504 if (!check_nobody_user_and_group()) {
505 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__
);
509 if (!STR_IN_SET(NOBODY_USER_NAME
, "nobody", "nfsnobody")) {
510 log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME
, __func__
);
514 test(m
, "exec-systemcallfilter-system-user-" NOBODY_USER_NAME
".service", 0, CLD_EXITED
);
518 static void test_exec_user(Manager
*m
) {
519 test(m
, "exec-user.service", 0, CLD_EXITED
);
521 if (!check_nobody_user_and_group()) {
522 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__
);
526 if (!STR_IN_SET(NOBODY_USER_NAME
, "nobody", "nfsnobody")) {
527 log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME
, __func__
);
531 test(m
, "exec-user-" NOBODY_USER_NAME
".service", 0, CLD_EXITED
);
534 static void test_exec_group(Manager
*m
) {
535 test(m
, "exec-group.service", 0, CLD_EXITED
);
537 if (!check_nobody_user_and_group()) {
538 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__
);
542 if (!STR_IN_SET(NOBODY_GROUP_NAME
, "nobody", "nfsnobody", "nogroup")) {
543 log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME
, __func__
);
547 test(m
, "exec-group-" NOBODY_GROUP_NAME
".service", 0, CLD_EXITED
);
550 static void test_exec_supplementarygroups(Manager
*m
) {
551 test(m
, "exec-supplementarygroups.service", 0, CLD_EXITED
);
552 test(m
, "exec-supplementarygroups-single-group.service", 0, CLD_EXITED
);
553 test(m
, "exec-supplementarygroups-single-group-user.service", 0, CLD_EXITED
);
554 test(m
, "exec-supplementarygroups-multiple-groups-default-group-user.service", 0, CLD_EXITED
);
555 test(m
, "exec-supplementarygroups-multiple-groups-withgid.service", 0, CLD_EXITED
);
556 test(m
, "exec-supplementarygroups-multiple-groups-withuid.service", 0, CLD_EXITED
);
559 static char* private_directory_bad(Manager
*m
) {
560 /* This mirrors setup_exec_directory(). */
562 for (ExecDirectoryType dt
= 0; dt
< _EXEC_DIRECTORY_TYPE_MAX
; dt
++) {
563 _cleanup_free_
char *p
= NULL
;
566 assert_se(p
= path_join(m
->prefix
[dt
], "private"));
568 if (stat(p
, &st
) >= 0 &&
569 (st
.st_mode
& (S_IRWXG
|S_IRWXO
)))
576 static void test_exec_dynamicuser(Manager
*m
) {
577 _cleanup_free_
char *bad
= private_directory_bad(m
);
579 log_warning("%s: %s has bad permissions, skipping test.", __func__
, bad
);
583 if (strstr_ptr(ci_environment(), "github-actions")) {
584 log_notice("%s: skipping test on GH Actions because of systemd/systemd#10337", __func__
);
588 test(m
, "exec-dynamicuser-fixeduser.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
589 if (check_user_has_group_with_same_name("adm"))
590 test(m
, "exec-dynamicuser-fixeduser-adm.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
591 if (check_user_has_group_with_same_name("games"))
592 test(m
, "exec-dynamicuser-fixeduser-games.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
593 test(m
, "exec-dynamicuser-fixeduser-one-supplementarygroup.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
594 test(m
, "exec-dynamicuser-supplementarygroups.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
595 test(m
, "exec-dynamicuser-statedir.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
597 (void) rm_rf("/var/lib/quux", REMOVE_ROOT
|REMOVE_PHYSICAL
);
598 (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT
|REMOVE_PHYSICAL
);
599 (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT
|REMOVE_PHYSICAL
);
600 (void) rm_rf("/var/lib/waldo", REMOVE_ROOT
|REMOVE_PHYSICAL
);
601 (void) rm_rf("/var/lib/private/quux", REMOVE_ROOT
|REMOVE_PHYSICAL
);
602 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT
|REMOVE_PHYSICAL
);
603 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT
|REMOVE_PHYSICAL
);
604 (void) rm_rf("/var/lib/private/waldo", REMOVE_ROOT
|REMOVE_PHYSICAL
);
606 test(m
, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED
);
607 test(m
, "exec-dynamicuser-statedir-migrate-step2.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
608 test(m
, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED
);
610 (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT
|REMOVE_PHYSICAL
);
611 (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT
|REMOVE_PHYSICAL
);
612 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT
|REMOVE_PHYSICAL
);
613 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT
|REMOVE_PHYSICAL
);
615 test(m
, "exec-dynamicuser-runtimedirectory1.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
616 test(m
, "exec-dynamicuser-runtimedirectory2.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
617 test(m
, "exec-dynamicuser-runtimedirectory3.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
620 static void test_exec_environment(Manager
*m
) {
621 test(m
, "exec-environment-no-substitute.service", 0, CLD_EXITED
);
622 test(m
, "exec-environment.service", 0, CLD_EXITED
);
623 test(m
, "exec-environment-multiple.service", 0, CLD_EXITED
);
624 test(m
, "exec-environment-empty.service", 0, CLD_EXITED
);
627 static void test_exec_environmentfile(Manager
*m
) {
628 static const char e
[] =
629 "VAR1='word1 word2'\n"
635 "line without an equal\n"
638 "VAR5=password\\with\\backslashes";
641 r
= write_string_file("/tmp/test-exec_environmentfile.conf", e
, WRITE_STRING_FILE_CREATE
);
644 test(m
, "exec-environmentfile.service", 0, CLD_EXITED
);
646 (void) unlink("/tmp/test-exec_environmentfile.conf");
649 static void test_exec_passenvironment(Manager
*m
) {
650 /* test-execute runs under MANAGER_USER which, by default, forwards all
651 * variables present in the environment, but only those that are
652 * present _at the time it is created_!
654 * So these PassEnvironment checks are still expected to work, since we
655 * are ensuring the variables are not present at manager creation (they
656 * are unset explicitly in main) and are only set here.
658 * This is still a good approximation of how a test for MANAGER_SYSTEM
661 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
662 assert_se(setenv("VAR2", "word3", 1) == 0);
663 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
664 assert_se(setenv("VAR4", "new\nline", 1) == 0);
665 assert_se(setenv("VAR5", "passwordwithbackslashes", 1) == 0);
666 test(m
, "exec-passenvironment.service", 0, CLD_EXITED
);
667 test(m
, "exec-passenvironment-repeated.service", 0, CLD_EXITED
);
668 test(m
, "exec-passenvironment-empty.service", 0, CLD_EXITED
);
669 assert_se(unsetenv("VAR1") == 0);
670 assert_se(unsetenv("VAR2") == 0);
671 assert_se(unsetenv("VAR3") == 0);
672 assert_se(unsetenv("VAR4") == 0);
673 assert_se(unsetenv("VAR5") == 0);
674 test(m
, "exec-passenvironment-absent.service", 0, CLD_EXITED
);
677 static void test_exec_umask(Manager
*m
) {
678 test(m
, "exec-umask-default.service", 0, CLD_EXITED
);
679 test(m
, "exec-umask-0177.service", 0, CLD_EXITED
);
682 static void test_exec_runtimedirectory(Manager
*m
) {
683 test(m
, "exec-runtimedirectory.service", 0, CLD_EXITED
);
684 test(m
, "exec-runtimedirectory-mode.service", 0, CLD_EXITED
);
685 test(m
, "exec-runtimedirectory-owner.service", 0, CLD_EXITED
);
687 if (!check_nobody_user_and_group()) {
688 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__
);
692 if (!STR_IN_SET(NOBODY_GROUP_NAME
, "nobody", "nfsnobody", "nogroup")) {
693 log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME
, __func__
);
697 test(m
, "exec-runtimedirectory-owner-" NOBODY_GROUP_NAME
".service", 0, CLD_EXITED
);
700 static void test_exec_capabilityboundingset(Manager
*m
) {
703 r
= find_executable("capsh", NULL
);
705 log_notice_errno(r
, "Skipping %s, could not find capsh binary: %m", __func__
);
709 if (have_effective_cap(CAP_CHOWN
) <= 0 ||
710 have_effective_cap(CAP_FOWNER
) <= 0 ||
711 have_effective_cap(CAP_KILL
) <= 0) {
712 log_notice("Skipping %s, this process does not have enough capabilities", __func__
);
716 test(m
, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED
);
717 test(m
, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED
);
718 test(m
, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED
);
719 test(m
, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED
);
722 static void test_exec_basic(Manager
*m
) {
723 test(m
, "exec-basic.service", 0, CLD_EXITED
);
726 static void test_exec_ambientcapabilities(Manager
*m
) {
729 /* Check if the kernel has support for ambient capabilities. Run
730 * the tests only if that's the case. Clearing all ambient
731 * capabilities is fine, since we are expecting them to be unset
732 * in the first place for the tests. */
733 r
= prctl(PR_CAP_AMBIENT
, PR_CAP_AMBIENT_CLEAR_ALL
, 0, 0, 0);
734 if (r
< 0 && IN_SET(errno
, EINVAL
, EOPNOTSUPP
, ENOSYS
)) {
735 log_notice("Skipping %s, the kernel does not support ambient capabilities", __func__
);
739 if (have_effective_cap(CAP_CHOWN
) <= 0 ||
740 have_effective_cap(CAP_NET_RAW
) <= 0) {
741 log_notice("Skipping %s, this process does not have enough capabilities", __func__
);
745 test(m
, "exec-ambientcapabilities.service", 0, CLD_EXITED
);
746 test(m
, "exec-ambientcapabilities-merge.service", 0, CLD_EXITED
);
748 if (!check_nobody_user_and_group()) {
749 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__
);
753 if (!STR_IN_SET(NOBODY_USER_NAME
, "nobody", "nfsnobody")) {
754 log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME
, __func__
);
758 test(m
, "exec-ambientcapabilities-" NOBODY_USER_NAME
".service", 0, CLD_EXITED
);
759 test(m
, "exec-ambientcapabilities-merge-" NOBODY_USER_NAME
".service", 0, CLD_EXITED
);
762 static void test_exec_privatenetwork(Manager
*m
) {
765 r
= find_executable("ip", NULL
);
767 log_notice_errno(r
, "Skipping %s, could not find ip binary: %m", __func__
);
771 test(m
, "exec-privatenetwork-yes.service", can_unshare
? 0 : EXIT_NETWORK
, CLD_EXITED
);
774 static void test_exec_oomscoreadjust(Manager
*m
) {
775 test(m
, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED
);
777 if (detect_container() > 0) {
778 log_notice("Testing in container, skipping remaining tests in %s", __func__
);
781 test(m
, "exec-oomscoreadjust-negative.service", 0, CLD_EXITED
);
784 static void test_exec_ioschedulingclass(Manager
*m
) {
785 test(m
, "exec-ioschedulingclass-none.service", 0, CLD_EXITED
);
786 test(m
, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED
);
787 test(m
, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED
);
789 if (detect_container() > 0) {
790 log_notice("Testing in container, skipping remaining tests in %s", __func__
);
793 test(m
, "exec-ioschedulingclass-realtime.service", 0, CLD_EXITED
);
796 static void test_exec_unsetenvironment(Manager
*m
) {
797 test(m
, "exec-unsetenvironment.service", 0, CLD_EXITED
);
800 static void test_exec_specifier(Manager
*m
) {
801 test(m
, "exec-specifier.service", 0, CLD_EXITED
);
802 test(m
, "exec-specifier@foo-bar.service", 0, CLD_EXITED
);
803 test(m
, "exec-specifier-interpolation.service", 0, CLD_EXITED
);
806 static void test_exec_standardinput(Manager
*m
) {
807 test(m
, "exec-standardinput-data.service", 0, CLD_EXITED
);
808 test(m
, "exec-standardinput-file.service", 0, CLD_EXITED
);
809 test(m
, "exec-standardinput-file-cat.service", 0, CLD_EXITED
);
812 static void test_exec_standardoutput(Manager
*m
) {
813 test(m
, "exec-standardoutput-file.service", 0, CLD_EXITED
);
816 static void test_exec_standardoutput_append(Manager
*m
) {
817 test(m
, "exec-standardoutput-append.service", 0, CLD_EXITED
);
820 static void test_exec_standardoutput_truncate(Manager
*m
) {
821 test(m
, "exec-standardoutput-truncate.service", 0, CLD_EXITED
);
824 static void test_exec_condition(Manager
*m
) {
825 test_service(m
, "exec-condition-failed.service", SERVICE_FAILURE_EXIT_CODE
);
826 test_service(m
, "exec-condition-skip.service", SERVICE_SKIP_CONDITION
);
829 typedef struct test_entry
{
834 #define entry(x) {x, #x}
836 static int run_tests(UnitFileScope scope
, const test_entry tests
[], char **patterns
) {
837 _cleanup_(manager_freep
) Manager
*m
= NULL
;
842 r
= manager_new(scope
, MANAGER_TEST_RUN_BASIC
, &m
);
843 m
->default_std_output
= EXEC_OUTPUT_NULL
; /* don't rely on host journald */
844 if (manager_errno_skip_test(r
))
845 return log_tests_skipped_errno(r
, "manager_new");
847 assert_se(manager_startup(m
, NULL
, NULL
) >= 0);
849 for (const test_entry
*test
= tests
; test
->f
; test
++)
850 if (strv_fnmatch_or_empty(patterns
, test
->name
, FNM_NOESCAPE
))
853 log_info("Skipping %s because it does not match any pattern.", test
->name
);
858 int main(int argc
, char *argv
[]) {
859 _cleanup_(rm_rf_physical_and_freep
) char *runtime_dir
= NULL
;
861 static const test_entry user_tests
[] = {
862 entry(test_exec_basic
),
863 entry(test_exec_ambientcapabilities
),
864 entry(test_exec_bindpaths
),
865 entry(test_exec_capabilityboundingset
),
866 entry(test_exec_condition
),
867 entry(test_exec_cpuaffinity
),
868 entry(test_exec_environment
),
869 entry(test_exec_environmentfile
),
870 entry(test_exec_group
),
871 entry(test_exec_ignoresigpipe
),
872 entry(test_exec_inaccessiblepaths
),
873 entry(test_exec_ioschedulingclass
),
874 entry(test_exec_noexecpaths
),
875 entry(test_exec_oomscoreadjust
),
876 entry(test_exec_passenvironment
),
877 entry(test_exec_personality
),
878 entry(test_exec_privatedevices
),
879 entry(test_exec_privatenetwork
),
880 entry(test_exec_privatetmp
),
881 entry(test_exec_protecthome
),
882 entry(test_exec_protectkernelmodules
),
883 entry(test_exec_readonlypaths
),
884 entry(test_exec_readwritepaths
),
885 entry(test_exec_restrictnamespaces
),
886 entry(test_exec_runtimedirectory
),
887 entry(test_exec_standardinput
),
888 entry(test_exec_standardoutput
),
889 entry(test_exec_standardoutput_append
),
890 entry(test_exec_standardoutput_truncate
),
891 entry(test_exec_supplementarygroups
),
892 entry(test_exec_systemcallerrornumber
),
893 entry(test_exec_systemcallfilter
),
894 entry(test_exec_temporaryfilesystem
),
895 entry(test_exec_umask
),
896 entry(test_exec_unsetenvironment
),
897 entry(test_exec_user
),
898 entry(test_exec_workingdirectory
),
901 static const test_entry system_tests
[] = {
902 entry(test_exec_dynamicuser
),
903 entry(test_exec_specifier
),
904 entry(test_exec_systemcallfilter_system
),
909 test_setup_logging(LOG_DEBUG
);
911 #if HAS_FEATURE_ADDRESS_SANITIZER
912 if (strstr_ptr(ci_environment(), "travis") || strstr_ptr(ci_environment(), "github-actions")) {
913 log_notice("Running on Travis CI/GH Actions under ASan, skipping, see https://github.com/systemd/systemd/issues/10696");
914 return EXIT_TEST_SKIP
;
918 assert_se(unsetenv("USER") == 0);
919 assert_se(unsetenv("LOGNAME") == 0);
920 assert_se(unsetenv("SHELL") == 0);
921 assert_se(unsetenv("HOME") == 0);
922 assert_se(unsetenv("TMPDIR") == 0);
924 can_unshare
= have_namespaces();
926 /* It is needed otherwise cgroup creation fails */
928 return log_tests_skipped("not root");
930 r
= enter_cgroup_subroot(NULL
);
932 return log_tests_skipped("cgroupfs not available");
934 _cleanup_free_
char *unit_dir
= NULL
;
935 assert_se(get_testdata_dir("test-execute/", &unit_dir
) >= 0);
936 assert_se(set_unit_path(unit_dir
) >= 0);
937 assert_se(runtime_dir
= setup_fake_runtime_dir());
939 /* Unset VAR1, VAR2 and VAR3 which are used in the PassEnvironment test
940 * cases, otherwise (and if they are present in the environment),
941 * `manager_default_environment` will copy them into the default
942 * environment which is passed to each created job, which will make the
943 * tests that expect those not to be present to fail.
945 assert_se(unsetenv("VAR1") == 0);
946 assert_se(unsetenv("VAR2") == 0);
947 assert_se(unsetenv("VAR3") == 0);
949 r
= run_tests(UNIT_FILE_USER
, user_tests
, argv
+ 1);
953 r
= run_tests(UNIT_FILE_SYSTEM
, system_tests
, argv
+ 1);
958 /* The following tests are for 1beab8b0d0ff2d7d1436b52d4a0c3d56dc908962. */
959 if (!is_seccomp_available()) {
960 log_notice("Seccomp not available, skipping unshare() filtered tests.");
964 _cleanup_hashmap_free_ Hashmap
*s
= NULL
;
965 assert_se(s
= hashmap_new(NULL
));
966 r
= seccomp_syscall_resolve_name("unshare");
967 assert_se(r
!= __NR_SCMP_ERROR
);
968 assert_se(hashmap_put(s
, UINT32_TO_PTR(r
+ 1), INT_TO_PTR(-1)) >= 0);
969 assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW
, s
, SCMP_ACT_ERRNO(EOPNOTSUPP
), true) >= 0);
970 assert_se(unshare(CLONE_NEWNS
) < 0);
971 assert_se(errno
== EOPNOTSUPP
);
975 r
= run_tests(UNIT_FILE_USER
, user_tests
, argv
+ 1);
979 return run_tests(UNIT_FILE_SYSTEM
, system_tests
, argv
+ 1);