1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "capability-util.h"
11 #include "cpu-set-util.h"
14 #include "errno-list.h"
20 #include "missing_prctl.h"
22 #include "mount-util.h"
23 #include "path-util.h"
24 #include "process-util.h"
26 #include "seccomp-util.h"
28 #include "signal-util.h"
29 #include "static-destruct.h"
30 #include "stat-util.h"
32 #include "tmpfile-util.h"
34 #include "user-util.h"
37 #define PRIVATE_UNIT_DIR "/run/test-execute-unit-dir"
39 static char *user_runtime_unit_dir
= NULL
;
40 static bool can_unshare
;
41 static unsigned n_ran_tests
= 0;
43 STATIC_DESTRUCTOR_REGISTER(user_runtime_unit_dir
, freep
);
45 typedef void (*test_function_t
)(Manager
*m
);
47 static int cld_dumped_to_killed(int code
) {
48 /* Depending on the system, seccomp version, … some signals might result in dumping, others in plain
49 * killing. Let's ignore the difference here, and map both cases to CLD_KILLED */
50 return code
== CLD_DUMPED
? CLD_KILLED
: code
;
53 static void wait_for_service_finish(Manager
*m
, Unit
*unit
) {
54 Service
*service
= NULL
;
56 usec_t timeout
= 2 * USEC_PER_MINUTE
;
61 service
= SERVICE(unit
);
62 printf("%s\n", unit
->id
);
63 exec_context_dump(&service
->exec_context
, stdout
, "\t");
64 ts
= now(CLOCK_MONOTONIC
);
65 while (!IN_SET(service
->state
, SERVICE_DEAD
, SERVICE_FAILED
)) {
69 r
= sd_event_run(m
->event
, 100 * USEC_PER_MSEC
);
72 n
= now(CLOCK_MONOTONIC
);
73 if (ts
+ timeout
< n
) {
74 log_error("Test timeout when testing %s", unit
->id
);
75 r
= unit_kill(unit
, KILL_ALL
, SIGKILL
, SI_USER
, 0, NULL
);
77 log_error_errno(r
, "Failed to kill %s: %m", unit
->id
);
83 static void check_main_result(const char *file
, unsigned line
, const char *func
,
84 Manager
*m
, Unit
*unit
, int status_expected
, int code_expected
) {
85 Service
*service
= NULL
;
90 wait_for_service_finish(m
, unit
);
92 service
= SERVICE(unit
);
93 exec_status_dump(&service
->main_exec_status
, stdout
, "\t");
95 if (cld_dumped_to_killed(service
->main_exec_status
.code
) != cld_dumped_to_killed(code_expected
)) {
96 log_error("%s:%u:%s %s: can_unshare=%s: exit code %d, expected %d",
97 file
, line
, func
, unit
->id
, yes_no(can_unshare
),
98 service
->main_exec_status
.code
, code_expected
);
102 if (service
->main_exec_status
.status
!= status_expected
) {
103 log_error("%s:%u:%s: %s: can_unshare=%s: exit status %d, expected %d",
104 file
, line
, func
, unit
->id
, yes_no(can_unshare
),
105 service
->main_exec_status
.status
, status_expected
);
110 static void check_service_result(const char *file
, unsigned line
, const char *func
,
111 Manager
*m
, Unit
*unit
, ServiceResult result_expected
) {
112 Service
*service
= NULL
;
117 wait_for_service_finish(m
, unit
);
119 service
= SERVICE(unit
);
121 if (service
->result
!= result_expected
) {
122 log_error("%s:%u:%s: %s: can_unshare=%s: service end result %s, expected %s",
123 file
, line
, func
, unit
->id
, yes_no(can_unshare
),
124 service_result_to_string(service
->result
),
125 service_result_to_string(result_expected
));
130 static bool check_nobody_user_and_group(void) {
131 static int cache
= -1;
138 if (!synthesize_nobody())
141 p
= getpwnam(NOBODY_USER_NAME
);
143 !streq(p
->pw_name
, NOBODY_USER_NAME
) ||
144 p
->pw_uid
!= UID_NOBODY
||
145 p
->pw_gid
!= GID_NOBODY
)
148 p
= getpwuid(UID_NOBODY
);
150 !streq(p
->pw_name
, NOBODY_USER_NAME
) ||
151 p
->pw_uid
!= UID_NOBODY
||
152 p
->pw_gid
!= GID_NOBODY
)
155 g
= getgrnam(NOBODY_GROUP_NAME
);
157 !streq(g
->gr_name
, NOBODY_GROUP_NAME
) ||
158 g
->gr_gid
!= GID_NOBODY
)
161 g
= getgrgid(GID_NOBODY
);
163 !streq(g
->gr_name
, NOBODY_GROUP_NAME
) ||
164 g
->gr_gid
!= GID_NOBODY
)
175 static bool check_user_has_group_with_same_name(const char *name
) {
183 !streq(p
->pw_name
, name
))
186 g
= getgrgid(p
->pw_gid
);
188 !streq(g
->gr_name
, name
))
194 static bool is_inaccessible_available(void) {
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 if (access(p
, F_OK
) < 0)
208 static void start_parent_slices(Unit
*unit
) {
211 slice
= UNIT_GET_SLICE(unit
);
213 start_parent_slices(slice
);
214 int r
= unit_start(slice
, NULL
);
215 assert_se(r
>= 0 || r
== -EALREADY
);
219 static void _test(const char *file
, unsigned line
, const char *func
,
220 Manager
*m
, const char *unit_name
, int status_expected
, int code_expected
) {
223 assert_se(unit_name
);
225 assert_se(manager_load_startable_unit_or_warn(m
, unit_name
, NULL
, &unit
) >= 0);
226 /* We need to start the slices as well otherwise the slice cgroups might be pruned
227 * in on_cgroup_empty_event. */
228 start_parent_slices(unit
);
229 assert_se(unit_start(unit
, NULL
) >= 0);
230 check_main_result(file
, line
, func
, m
, unit
, status_expected
, code_expected
);
234 #define test(m, unit_name, status_expected, code_expected) \
235 _test(PROJECT_FILE, __LINE__, __func__, m, unit_name, status_expected, code_expected)
237 static void _test_service(const char *file
, unsigned line
, const char *func
,
238 Manager
*m
, const char *unit_name
, ServiceResult result_expected
) {
241 assert_se(unit_name
);
243 assert_se(manager_load_startable_unit_or_warn(m
, unit_name
, NULL
, &unit
) >= 0);
244 assert_se(unit_start(unit
, NULL
) >= 0);
245 check_service_result(file
, line
, func
, m
, unit
, result_expected
);
247 #define test_service(m, unit_name, result_expected) \
248 _test_service(PROJECT_FILE, __LINE__, __func__, m, unit_name, result_expected)
250 static void test_exec_bindpaths(Manager
*m
) {
251 assert_se(mkdir_p("/tmp/test-exec-bindpaths", 0755) >= 0);
252 assert_se(mkdir_p("/tmp/test-exec-bindreadonlypaths", 0755) >= 0);
254 test(m
, "exec-bindpaths.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
256 (void) rm_rf("/tmp/test-exec-bindpaths", REMOVE_ROOT
|REMOVE_PHYSICAL
);
257 (void) rm_rf("/tmp/test-exec-bindreadonlypaths", REMOVE_ROOT
|REMOVE_PHYSICAL
);
260 static void test_exec_cpuaffinity(Manager
*m
) {
261 _cleanup_(cpu_set_reset
) CPUSet c
= {};
263 assert_se(cpu_set_realloc(&c
, 8192) >= 0); /* just allocate the maximum possible size */
264 assert_se(sched_getaffinity(0, c
.allocated
, c
.set
) >= 0);
266 if (!CPU_ISSET_S(0, c
.allocated
, c
.set
)) {
267 log_notice("Cannot use CPU 0, skipping %s", __func__
);
271 test(m
, "exec-cpuaffinity1.service", 0, CLD_EXITED
);
272 test(m
, "exec-cpuaffinity2.service", 0, CLD_EXITED
);
274 if (!CPU_ISSET_S(1, c
.allocated
, c
.set
) ||
275 !CPU_ISSET_S(2, c
.allocated
, c
.set
)) {
276 log_notice("Cannot use CPU 1 or 2, skipping remaining tests in %s", __func__
);
280 test(m
, "exec-cpuaffinity3.service", 0, CLD_EXITED
);
283 static void test_exec_credentials(Manager
*m
) {
284 test(m
, "exec-set-credential.service", 0, CLD_EXITED
);
285 test(m
, "exec-set-credential-with-mount-namespace.service", 0, CLD_EXITED
);
286 test(m
, "exec-set-credential-with-seccomp.service", 0, CLD_EXITED
);
287 test(m
, "exec-load-credential.service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_CREDENTIALS
, CLD_EXITED
);
288 test(m
, "exec-load-credential-with-mount-namespace.service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_CREDENTIALS
, CLD_EXITED
);
289 test(m
, "exec-load-credential-with-seccomp.service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_CREDENTIALS
, CLD_EXITED
);
290 test(m
, "exec-credentials-dir-specifier.service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_CREDENTIALS
, CLD_EXITED
);
293 static void test_exec_workingdirectory(Manager
*m
) {
294 assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0);
296 test(m
, "exec-workingdirectory.service", 0, CLD_EXITED
);
297 test(m
, "exec-workingdirectory-trailing-dot.service", 0, CLD_EXITED
);
299 (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT
|REMOVE_PHYSICAL
);
302 static void test_exec_execsearchpath(Manager
*m
) {
303 assert_se(mkdir_p("/tmp/test-exec_execsearchpath", 0755) >= 0);
305 assert_se(copy_file("/bin/ls", "/tmp/test-exec_execsearchpath/ls_temp", 0, 0777, COPY_REPLACE
) >= 0);
307 test(m
, "exec-execsearchpath.service", 0, CLD_EXITED
);
309 assert_se(rm_rf("/tmp/test-exec_execsearchpath", REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);
311 test(m
, "exec-execsearchpath.service", EXIT_EXEC
, CLD_EXITED
);
314 static void test_exec_execsearchpath_specifier(Manager
*m
) {
315 test(m
, "exec-execsearchpath-unit-specifier.service", 0, CLD_EXITED
);
318 static void test_exec_execsearchpath_environment(Manager
*m
) {
319 test(m
, "exec-execsearchpath-environment.service", 0, CLD_EXITED
);
320 test(m
, "exec-execsearchpath-environment-path-set.service", 0, CLD_EXITED
);
323 static void test_exec_execsearchpath_environment_files(Manager
*m
) {
324 static const char path_not_set
[] =
325 "VAR1='word1 word2'\n"
331 "line without an equal\n"
334 "VAR5=password\\with\\backslashes";
336 static const char path_set
[] =
337 "VAR1='word1 word2'\n"
343 "line without an equal\n"
346 "VAR5=password\\with\\backslashes\n"
351 r
= write_string_file("/tmp/test-exec_execsearchpath_environmentfile.conf", path_not_set
, WRITE_STRING_FILE_CREATE
);
355 test(m
, "exec-execsearchpath-environmentfile.service", 0, CLD_EXITED
);
357 (void) unlink("/tmp/test-exec_environmentfile.conf");
360 r
= write_string_file("/tmp/test-exec_execsearchpath_environmentfile-set.conf", path_set
, WRITE_STRING_FILE_CREATE
);
364 test(m
, "exec-execsearchpath-environmentfile-set.service", 0, CLD_EXITED
);
366 (void) unlink("/tmp/test-exec_environmentfile-set.conf");
369 static void test_exec_execsearchpath_passenvironment(Manager
*m
) {
370 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
371 assert_se(setenv("VAR2", "word3", 1) == 0);
372 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
373 assert_se(setenv("VAR4", "new\nline", 1) == 0);
374 assert_se(setenv("VAR5", "passwordwithbackslashes", 1) == 0);
376 test(m
, "exec-execsearchpath-passenvironment.service", 0, CLD_EXITED
);
378 assert_se(setenv("PATH", "/usr", 1) == 0);
379 test(m
, "exec-execsearchpath-passenvironment-set.service", 0, CLD_EXITED
);
381 assert_se(unsetenv("VAR1") == 0);
382 assert_se(unsetenv("VAR2") == 0);
383 assert_se(unsetenv("VAR3") == 0);
384 assert_se(unsetenv("VAR4") == 0);
385 assert_se(unsetenv("VAR5") == 0);
386 assert_se(unsetenv("PATH") == 0);
389 static void test_exec_personality(Manager
*m
) {
390 #if defined(__x86_64__)
391 test(m
, "exec-personality-x86-64.service", 0, CLD_EXITED
);
393 #elif defined(__s390__)
394 test(m
, "exec-personality-s390.service", 0, CLD_EXITED
);
396 #elif defined(__powerpc64__)
397 # if __BYTE_ORDER == __BIG_ENDIAN
398 test(m
, "exec-personality-ppc64.service", 0, CLD_EXITED
);
400 test(m
, "exec-personality-ppc64le.service", 0, CLD_EXITED
);
403 #elif defined(__aarch64__)
404 test(m
, "exec-personality-aarch64.service", 0, CLD_EXITED
);
406 #elif defined(__i386__)
407 test(m
, "exec-personality-x86.service", 0, CLD_EXITED
);
408 #elif defined(__loongarch_lp64)
409 test(m
, "exec-personality-loongarch64.service", 0, CLD_EXITED
);
411 log_notice("Unknown personality, skipping %s", __func__
);
415 static void test_exec_ignoresigpipe(Manager
*m
) {
416 test(m
, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED
);
417 test(m
, "exec-ignoresigpipe-no.service", SIGPIPE
, CLD_KILLED
);
420 static void test_exec_privatetmp(Manager
*m
) {
421 assert_se(touch("/tmp/test-exec_privatetmp") >= 0);
423 test(m
, "exec-privatetmp-yes.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_FAILURE
: EXIT_NAMESPACE
, CLD_EXITED
);
424 test(m
, "exec-privatetmp-no.service", 0, CLD_EXITED
);
425 test(m
, "exec-privatetmp-disabled-by-prefix.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_FAILURE
: EXIT_NAMESPACE
, CLD_EXITED
);
427 (void) unlink("/tmp/test-exec_privatetmp");
430 static void test_exec_privatedevices(Manager
*m
) {
433 if (detect_container() > 0) {
434 log_notice("Testing in container, skipping %s", __func__
);
437 if (!is_inaccessible_available()) {
438 log_notice("Testing without inaccessible, skipping %s", __func__
);
442 test(m
, "exec-privatedevices-yes.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_FAILURE
: EXIT_NAMESPACE
, CLD_EXITED
);
443 test(m
, "exec-privatedevices-no.service", 0, CLD_EXITED
);
444 test(m
, "exec-privatedevices-disabled-by-prefix.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_FAILURE
: EXIT_NAMESPACE
, CLD_EXITED
);
445 test(m
, "exec-privatedevices-yes-with-group.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_FAILURE
: EXIT_NAMESPACE
, CLD_EXITED
);
447 /* We use capsh to test if the capabilities are
448 * properly set, so be sure that it exists */
449 r
= find_executable("capsh", NULL
);
451 log_notice_errno(r
, "Could not find capsh binary, skipping remaining tests in %s: %m", __func__
);
455 test(m
, "exec-privatedevices-yes-capability-mknod.service", can_unshare
|| MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
456 test(m
, "exec-privatedevices-no-capability-mknod.service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_FAILURE
, CLD_EXITED
);
457 test(m
, "exec-privatedevices-yes-capability-sys-rawio.service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
458 test(m
, "exec-privatedevices-no-capability-sys-rawio.service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_FAILURE
, CLD_EXITED
);
461 static void test_exec_protecthome(Manager
*m
) {
463 log_notice("Cannot reliably unshare, skipping %s", __func__
);
467 test(m
, "exec-protecthome-tmpfs-vs-protectsystem-strict.service", 0, CLD_EXITED
);
470 static void test_exec_protectkernelmodules(Manager
*m
) {
473 if (detect_container() > 0) {
474 log_notice("Testing in container, skipping %s", __func__
);
477 if (!is_inaccessible_available()) {
478 log_notice("Testing without inaccessible, skipping %s", __func__
);
482 r
= find_executable("capsh", NULL
);
484 log_notice_errno(r
, "Skipping %s, could not find capsh binary: %m", __func__
);
488 test(m
, "exec-protectkernelmodules-no-capabilities.service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_FAILURE
, CLD_EXITED
);
489 test(m
, "exec-protectkernelmodules-yes-capabilities.service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
490 test(m
, "exec-protectkernelmodules-yes-mount-propagation.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_FAILURE
: EXIT_NAMESPACE
, CLD_EXITED
);
493 static void test_exec_readonlypaths(Manager
*m
) {
495 test(m
, "exec-readonlypaths-simple.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_FAILURE
: EXIT_NAMESPACE
, CLD_EXITED
);
497 if (path_is_read_only_fs("/var") > 0) {
498 log_notice("Directory /var is readonly, skipping remaining tests in %s", __func__
);
502 test(m
, "exec-readonlypaths.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_FAILURE
: EXIT_NAMESPACE
, CLD_EXITED
);
503 test(m
, "exec-readonlypaths-with-bindpaths.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
504 test(m
, "exec-readonlypaths-mount-propagation.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_FAILURE
: EXIT_NAMESPACE
, CLD_EXITED
);
507 static void test_exec_readwritepaths(Manager
*m
) {
509 if (path_is_read_only_fs("/") > 0) {
510 log_notice("Root directory is readonly, skipping %s", __func__
);
514 test(m
, "exec-readwritepaths-mount-propagation.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_FAILURE
: EXIT_NAMESPACE
, CLD_EXITED
);
517 static void test_exec_inaccessiblepaths(Manager
*m
) {
519 if (!is_inaccessible_available()) {
520 log_notice("Testing without inaccessible, skipping %s", __func__
);
524 test(m
, "exec-inaccessiblepaths-sys.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_FAILURE
: EXIT_NAMESPACE
, CLD_EXITED
);
526 if (path_is_read_only_fs("/") > 0) {
527 log_notice("Root directory is readonly, skipping remaining tests in %s", __func__
);
531 test(m
, "exec-inaccessiblepaths-mount-propagation.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_FAILURE
: EXIT_NAMESPACE
, CLD_EXITED
);
534 static int on_spawn_io(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
535 char **result
= userdata
;
542 l
= read(fd
, buf
, sizeof(buf
) - 1);
554 assert_se(strextend(result
, buf
));
556 log_error("ldd: %s", buf
);
559 /* Re-enable the event source if we did not encounter EOF */
560 assert_se(sd_event_source_set_enabled(s
, SD_EVENT_ONESHOT
) >= 0);
564 static int on_spawn_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
565 pid_t
*pid
= userdata
;
569 (void) kill(*pid
, SIGKILL
);
574 static int on_spawn_sigchld(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
579 if (si
->si_code
== CLD_EXITED
)
582 sd_event_exit(sd_event_source_get_event(s
), ret
);
586 static int find_libraries(const char *exec
, char ***ret
) {
587 _cleanup_(sd_event_unrefp
) sd_event
*e
= NULL
;
588 _cleanup_(sd_event_source_unrefp
) sd_event_source
*sigchld_source
= NULL
;
589 _cleanup_(sd_event_source_unrefp
) sd_event_source
*stdout_source
= NULL
;
590 _cleanup_(sd_event_source_unrefp
) sd_event_source
*stderr_source
= NULL
;
591 _cleanup_close_pair_
int outpipe
[2] = PIPE_EBADF
, errpipe
[2] = PIPE_EBADF
;
592 _cleanup_strv_free_
char **libraries
= NULL
;
593 _cleanup_free_
char *result
= NULL
;
600 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, -1) >= 0);
602 assert_se(pipe2(outpipe
, O_NONBLOCK
|O_CLOEXEC
) == 0);
603 assert_se(pipe2(errpipe
, O_NONBLOCK
|O_CLOEXEC
) == 0);
605 r
= safe_fork_full("(spawn-ldd)",
606 (int[]) { -EBADF
, outpipe
[1], errpipe
[1] },
608 FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_DEATHSIG
|FORK_REARRANGE_STDIO
|FORK_LOG
, &pid
);
611 execlp("ldd", "ldd", exec
, NULL
);
615 outpipe
[1] = safe_close(outpipe
[1]);
616 errpipe
[1] = safe_close(errpipe
[1]);
618 assert_se(sd_event_new(&e
) >= 0);
620 assert_se(sd_event_add_time_relative(e
, NULL
, CLOCK_MONOTONIC
,
621 10 * USEC_PER_SEC
, USEC_PER_SEC
, on_spawn_timeout
, &pid
) >= 0);
622 assert_se(sd_event_add_io(e
, &stdout_source
, outpipe
[0], EPOLLIN
, on_spawn_io
, &result
) >= 0);
623 assert_se(sd_event_source_set_enabled(stdout_source
, SD_EVENT_ONESHOT
) >= 0);
624 assert_se(sd_event_add_io(e
, &stderr_source
, errpipe
[0], EPOLLIN
, on_spawn_io
, NULL
) >= 0);
625 assert_se(sd_event_source_set_enabled(stderr_source
, SD_EVENT_ONESHOT
) >= 0);
626 assert_se(sd_event_add_child(e
, &sigchld_source
, pid
, WEXITED
, on_spawn_sigchld
, NULL
) >= 0);
627 /* SIGCHLD should be processed after IO is complete */
628 assert_se(sd_event_source_set_priority(sigchld_source
, SD_EVENT_PRIORITY_NORMAL
+ 1) >= 0);
630 assert_se(sd_event_loop(e
) >= 0);
632 _cleanup_strv_free_
char **v
= NULL
;
633 assert_se(strv_split_newlines_full(&v
, result
, 0) >= 0);
636 _cleanup_free_
char *word
= NULL
;
639 r
= extract_first_word(&p
, &word
, NULL
, 0);
644 if (path_is_absolute(word
)) {
645 assert_se(strv_consume(&libraries
, TAKE_PTR(word
)) >= 0);
650 r
= extract_first_word(&p
, &word
, NULL
, 0);
655 if (!streq_ptr(word
, "=>"))
659 r
= extract_first_word(&p
, &word
, NULL
, 0);
664 if (path_is_absolute(word
)) {
665 assert_se(strv_consume(&libraries
, TAKE_PTR(word
)) >= 0);
670 *ret
= TAKE_PTR(libraries
);
674 static void test_exec_mount_apivfs(Manager
*m
) {
675 _cleanup_free_
char *fullpath_touch
= NULL
, *fullpath_test
= NULL
, *data
= NULL
;
676 _cleanup_strv_free_
char **libraries
= NULL
, **libraries_test
= NULL
;
679 assert_se(user_runtime_unit_dir
);
681 r
= find_executable("ldd", NULL
);
683 log_notice_errno(r
, "Skipping %s, could not find 'ldd' command: %m", __func__
);
686 r
= find_executable("touch", &fullpath_touch
);
688 log_notice_errno(r
, "Skipping %s, could not find 'touch' command: %m", __func__
);
691 r
= find_executable("test", &fullpath_test
);
693 log_notice_errno(r
, "Skipping %s, could not find 'test' command: %m", __func__
);
697 assert_se(find_libraries(fullpath_touch
, &libraries
) >= 0);
698 assert_se(find_libraries(fullpath_test
, &libraries_test
) >= 0);
699 assert_se(strv_extend_strv(&libraries
, libraries_test
, true) >= 0);
701 assert_se(strextend(&data
, "[Service]\n"));
702 assert_se(strextend(&data
, "ExecStart=", fullpath_touch
, " /aaa\n"));
703 assert_se(strextend(&data
, "ExecStart=", fullpath_test
, " -f /aaa\n"));
704 assert_se(strextend(&data
, "BindReadOnlyPaths=", fullpath_touch
, "\n"));
705 assert_se(strextend(&data
, "BindReadOnlyPaths=", fullpath_test
, "\n"));
707 STRV_FOREACH(p
, libraries
)
708 assert_se(strextend(&data
, "BindReadOnlyPaths=", *p
, "\n"));
710 assert_se(write_drop_in(user_runtime_unit_dir
, "exec-mount-apivfs-no.service", 10, "bind-mount", data
) >= 0);
712 assert_se(mkdir_p("/tmp/test-exec-mount-apivfs-no/root", 0755) >= 0);
714 test(m
, "exec-mount-apivfs-no.service", can_unshare
|| !MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
716 (void) rm_rf("/tmp/test-exec-mount-apivfs-no/root", REMOVE_ROOT
|REMOVE_PHYSICAL
);
719 static void test_exec_noexecpaths(Manager
*m
) {
721 test(m
, "exec-noexecpaths-simple.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_FAILURE
: EXIT_NAMESPACE
, CLD_EXITED
);
724 static void test_exec_temporaryfilesystem(Manager
*m
) {
726 test(m
, "exec-temporaryfilesystem-options.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
727 test(m
, "exec-temporaryfilesystem-ro.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
728 test(m
, "exec-temporaryfilesystem-rw.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
729 test(m
, "exec-temporaryfilesystem-usr.service", can_unshare
? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
732 static void test_exec_systemcallfilter(Manager
*m
) {
736 if (!is_seccomp_available()) {
737 log_notice("Seccomp not available, skipping %s", __func__
);
741 test(m
, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED
);
742 test(m
, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED
);
743 test(m
, "exec-systemcallfilter-not-failing3.service", 0, CLD_EXITED
);
744 test(m
, "exec-systemcallfilter-failing.service", SIGSYS
, CLD_KILLED
);
745 test(m
, "exec-systemcallfilter-failing2.service", SIGSYS
, CLD_KILLED
);
746 test(m
, "exec-systemcallfilter-failing3.service", SIGSYS
, CLD_KILLED
);
748 r
= find_executable("python3", NULL
);
750 log_notice_errno(r
, "Skipping remaining tests in %s, could not find python3 binary: %m", __func__
);
754 test(m
, "exec-systemcallfilter-with-errno-name.service", errno_from_name("EILSEQ"), CLD_EXITED
);
755 test(m
, "exec-systemcallfilter-with-errno-number.service", 255, CLD_EXITED
);
756 test(m
, "exec-systemcallfilter-with-errno-multi.service", errno_from_name("EILSEQ"), CLD_EXITED
);
757 test(m
, "exec-systemcallfilter-with-errno-in-allow-list.service", errno_from_name("EILSEQ"), CLD_EXITED
);
758 test(m
, "exec-systemcallfilter-override-error-action.service", SIGSYS
, CLD_KILLED
);
759 test(m
, "exec-systemcallfilter-override-error-action2.service", errno_from_name("EILSEQ"), CLD_EXITED
);
763 static void test_exec_systemcallerrornumber(Manager
*m
) {
767 if (!is_seccomp_available()) {
768 log_notice("Seccomp not available, skipping %s", __func__
);
772 r
= find_executable("python3", NULL
);
774 log_notice_errno(r
, "Skipping %s, could not find python3 binary: %m", __func__
);
778 test(m
, "exec-systemcallerrornumber-name.service", errno_from_name("EACCES"), CLD_EXITED
);
779 test(m
, "exec-systemcallerrornumber-number.service", 255, CLD_EXITED
);
783 static void test_exec_restrictnamespaces(Manager
*m
) {
785 if (!is_seccomp_available()) {
786 log_notice("Seccomp not available, skipping %s", __func__
);
790 test(m
, "exec-restrictnamespaces-no.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
791 test(m
, "exec-restrictnamespaces-yes.service", 1, CLD_EXITED
);
792 test(m
, "exec-restrictnamespaces-mnt.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
793 test(m
, "exec-restrictnamespaces-mnt-deny-list.service", 1, CLD_EXITED
);
794 test(m
, "exec-restrictnamespaces-merge-and.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
795 test(m
, "exec-restrictnamespaces-merge-or.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
796 test(m
, "exec-restrictnamespaces-merge-all.service", can_unshare
? 0 : EXIT_FAILURE
, CLD_EXITED
);
800 static void test_exec_systemcallfilter_system(Manager
*m
) {
801 /* Skip this particular test case when running under ASan, as
802 * LSan intermittently segfaults when accessing memory right
803 * after the test finishes. Generally, ASan & LSan don't like
806 #if HAVE_SECCOMP && !HAS_FEATURE_ADDRESS_SANITIZER
807 if (!is_seccomp_available()) {
808 log_notice("Seccomp not available, skipping %s", __func__
);
812 test(m
, "exec-systemcallfilter-system-user.service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_GROUP
, CLD_EXITED
);
814 if (!check_nobody_user_and_group()) {
815 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__
);
819 if (!STR_IN_SET(NOBODY_USER_NAME
, "nobody", "nfsnobody")) {
820 log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME
, __func__
);
824 test(m
, "exec-systemcallfilter-system-user-" NOBODY_USER_NAME
".service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_GROUP
, CLD_EXITED
);
828 static void test_exec_user(Manager
*m
) {
829 test(m
, "exec-user.service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_GROUP
, CLD_EXITED
);
831 if (!check_nobody_user_and_group()) {
832 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__
);
836 if (!STR_IN_SET(NOBODY_USER_NAME
, "nobody", "nfsnobody")) {
837 log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME
, __func__
);
841 test(m
, "exec-user-" NOBODY_USER_NAME
".service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_GROUP
, CLD_EXITED
);
844 static void test_exec_group(Manager
*m
) {
845 test(m
, "exec-group.service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_GROUP
, CLD_EXITED
);
847 if (!check_nobody_user_and_group()) {
848 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__
);
852 if (!STR_IN_SET(NOBODY_GROUP_NAME
, "nobody", "nfsnobody", "nogroup")) {
853 log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME
, __func__
);
857 test(m
, "exec-group-" NOBODY_GROUP_NAME
".service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_GROUP
, CLD_EXITED
);
860 static void test_exec_supplementarygroups(Manager
*m
) {
861 int status
= MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_GROUP
;
862 test(m
, "exec-supplementarygroups.service", status
, CLD_EXITED
);
863 test(m
, "exec-supplementarygroups-single-group.service", status
, CLD_EXITED
);
864 test(m
, "exec-supplementarygroups-single-group-user.service", status
, CLD_EXITED
);
865 test(m
, "exec-supplementarygroups-multiple-groups-default-group-user.service", status
, CLD_EXITED
);
866 test(m
, "exec-supplementarygroups-multiple-groups-withgid.service", status
, CLD_EXITED
);
867 test(m
, "exec-supplementarygroups-multiple-groups-withuid.service", status
, CLD_EXITED
);
870 static char* private_directory_bad(Manager
*m
) {
871 /* This mirrors setup_exec_directory(). */
873 for (ExecDirectoryType dt
= 0; dt
< _EXEC_DIRECTORY_TYPE_MAX
; dt
++) {
874 _cleanup_free_
char *p
= NULL
;
877 assert_se(p
= path_join(m
->prefix
[dt
], "private"));
879 if (stat(p
, &st
) >= 0 &&
880 (st
.st_mode
& (S_IRWXG
|S_IRWXO
)))
887 static void test_exec_dynamicuser(Manager
*m
) {
888 _cleanup_free_
char *bad
= private_directory_bad(m
);
890 log_warning("%s: %s has bad permissions, skipping test.", __func__
, bad
);
894 if (strstr_ptr(ci_environment(), "github-actions")) {
895 log_notice("%s: skipping test on GH Actions because of systemd/systemd#10337", __func__
);
899 int status
= can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_NAMESPACE
: EXIT_GROUP
;
901 test(m
, "exec-dynamicuser-fixeduser.service", status
, CLD_EXITED
);
902 if (check_user_has_group_with_same_name("adm"))
903 test(m
, "exec-dynamicuser-fixeduser-adm.service", status
, CLD_EXITED
);
904 if (check_user_has_group_with_same_name("games"))
905 test(m
, "exec-dynamicuser-fixeduser-games.service", status
, CLD_EXITED
);
906 test(m
, "exec-dynamicuser-fixeduser-one-supplementarygroup.service", status
, CLD_EXITED
);
907 test(m
, "exec-dynamicuser-supplementarygroups.service", status
, CLD_EXITED
);
908 test(m
, "exec-dynamicuser-statedir.service", status
, CLD_EXITED
);
910 (void) rm_rf("/var/lib/quux", REMOVE_ROOT
|REMOVE_PHYSICAL
);
911 (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT
|REMOVE_PHYSICAL
);
912 (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT
|REMOVE_PHYSICAL
);
913 (void) rm_rf("/var/lib/waldo", REMOVE_ROOT
|REMOVE_PHYSICAL
);
914 (void) rm_rf("/var/lib/private/quux", REMOVE_ROOT
|REMOVE_PHYSICAL
);
915 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT
|REMOVE_PHYSICAL
);
916 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT
|REMOVE_PHYSICAL
);
917 (void) rm_rf("/var/lib/private/waldo", REMOVE_ROOT
|REMOVE_PHYSICAL
);
919 test(m
, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED
);
920 test(m
, "exec-dynamicuser-statedir-migrate-step2.service", status
, CLD_EXITED
);
921 test(m
, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED
);
923 (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT
|REMOVE_PHYSICAL
);
924 (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT
|REMOVE_PHYSICAL
);
925 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT
|REMOVE_PHYSICAL
);
926 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT
|REMOVE_PHYSICAL
);
928 test(m
, "exec-dynamicuser-runtimedirectory1.service", status
, CLD_EXITED
);
929 test(m
, "exec-dynamicuser-runtimedirectory2.service", status
, CLD_EXITED
);
930 test(m
, "exec-dynamicuser-runtimedirectory3.service", status
, CLD_EXITED
);
933 static void test_exec_environment(Manager
*m
) {
934 test(m
, "exec-environment-no-substitute.service", 0, CLD_EXITED
);
935 test(m
, "exec-environment.service", 0, CLD_EXITED
);
936 test(m
, "exec-environment-multiple.service", 0, CLD_EXITED
);
937 test(m
, "exec-environment-empty.service", 0, CLD_EXITED
);
940 static void test_exec_environmentfile(Manager
*m
) {
941 static const char e
[] =
942 "VAR1='word1 word2'\n"
948 "line without an equal\n"
951 "VAR5=password\\with\\backslashes";
954 r
= write_string_file("/tmp/test-exec_environmentfile.conf", e
, WRITE_STRING_FILE_CREATE
);
957 test(m
, "exec-environmentfile.service", 0, CLD_EXITED
);
959 (void) unlink("/tmp/test-exec_environmentfile.conf");
962 static void test_exec_passenvironment(Manager
*m
) {
963 /* test-execute runs under MANAGER_USER which, by default, forwards all
964 * variables present in the environment, but only those that are
965 * present _at the time it is created_!
967 * So these PassEnvironment checks are still expected to work, since we
968 * are ensuring the variables are not present at manager creation (they
969 * are unset explicitly in main) and are only set here.
971 * This is still a good approximation of how a test for MANAGER_SYSTEM
974 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
975 assert_se(setenv("VAR2", "word3", 1) == 0);
976 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
977 assert_se(setenv("VAR4", "new\nline", 1) == 0);
978 assert_se(setenv("VAR5", "passwordwithbackslashes", 1) == 0);
979 test(m
, "exec-passenvironment.service", 0, CLD_EXITED
);
980 test(m
, "exec-passenvironment-repeated.service", 0, CLD_EXITED
);
981 test(m
, "exec-passenvironment-empty.service", 0, CLD_EXITED
);
982 assert_se(unsetenv("VAR1") == 0);
983 assert_se(unsetenv("VAR2") == 0);
984 assert_se(unsetenv("VAR3") == 0);
985 assert_se(unsetenv("VAR4") == 0);
986 assert_se(unsetenv("VAR5") == 0);
987 test(m
, "exec-passenvironment-absent.service", 0, CLD_EXITED
);
990 static void test_exec_umask(Manager
*m
) {
991 test(m
, "exec-umask-default.service", can_unshare
|| MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
992 test(m
, "exec-umask-0177.service", can_unshare
|| MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
995 static void test_exec_runtimedirectory(Manager
*m
) {
996 (void) rm_rf("/run/test-exec_runtimedirectory2", REMOVE_ROOT
|REMOVE_PHYSICAL
);
997 test(m
, "exec-runtimedirectory.service", 0, CLD_EXITED
);
998 (void) rm_rf("/run/test-exec_runtimedirectory2", REMOVE_ROOT
|REMOVE_PHYSICAL
);
1000 test(m
, "exec-runtimedirectory-mode.service", 0, CLD_EXITED
);
1001 test(m
, "exec-runtimedirectory-owner.service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_GROUP
, CLD_EXITED
);
1003 if (!check_nobody_user_and_group()) {
1004 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__
);
1008 if (!STR_IN_SET(NOBODY_GROUP_NAME
, "nobody", "nfsnobody", "nogroup")) {
1009 log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME
, __func__
);
1013 test(m
, "exec-runtimedirectory-owner-" NOBODY_GROUP_NAME
".service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_GROUP
, CLD_EXITED
);
1016 static void test_exec_capabilityboundingset(Manager
*m
) {
1019 r
= find_executable("capsh", NULL
);
1021 log_notice_errno(r
, "Skipping %s, could not find capsh binary: %m", __func__
);
1025 if (have_effective_cap(CAP_CHOWN
) <= 0 ||
1026 have_effective_cap(CAP_FOWNER
) <= 0 ||
1027 have_effective_cap(CAP_KILL
) <= 0) {
1028 log_notice("Skipping %s, this process does not have enough capabilities", __func__
);
1032 test(m
, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED
);
1033 test(m
, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED
);
1034 test(m
, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED
);
1035 test(m
, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED
);
1038 static void test_exec_basic(Manager
*m
) {
1039 test(m
, "exec-basic.service", can_unshare
|| MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_NAMESPACE
, CLD_EXITED
);
1042 static void test_exec_ambientcapabilities(Manager
*m
) {
1045 /* Check if the kernel has support for ambient capabilities. Run
1046 * the tests only if that's the case. Clearing all ambient
1047 * capabilities is fine, since we are expecting them to be unset
1048 * in the first place for the tests. */
1049 r
= prctl(PR_CAP_AMBIENT
, PR_CAP_AMBIENT_CLEAR_ALL
, 0, 0, 0);
1050 if (r
< 0 && IN_SET(errno
, EINVAL
, EOPNOTSUPP
, ENOSYS
)) {
1051 log_notice("Skipping %s, the kernel does not support ambient capabilities", __func__
);
1055 if (have_effective_cap(CAP_CHOWN
) <= 0 ||
1056 have_effective_cap(CAP_NET_RAW
) <= 0) {
1057 log_notice("Skipping %s, this process does not have enough capabilities", __func__
);
1061 test(m
, "exec-ambientcapabilities.service", 0, CLD_EXITED
);
1062 test(m
, "exec-ambientcapabilities-merge.service", 0, CLD_EXITED
);
1064 if (!check_nobody_user_and_group()) {
1065 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__
);
1069 if (!STR_IN_SET(NOBODY_USER_NAME
, "nobody", "nfsnobody")) {
1070 log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME
, __func__
);
1074 test(m
, "exec-ambientcapabilities-" NOBODY_USER_NAME
".service", 0, CLD_EXITED
);
1075 test(m
, "exec-ambientcapabilities-merge-" NOBODY_USER_NAME
".service", 0, CLD_EXITED
);
1078 static void test_exec_privatenetwork(Manager
*m
) {
1081 r
= find_executable("ip", NULL
);
1083 log_notice_errno(r
, "Skipping %s, could not find ip binary: %m", __func__
);
1087 test(m
, "exec-privatenetwork-yes-privatemounts-no.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_NETWORK
: EXIT_FAILURE
, CLD_EXITED
);
1088 test(m
, "exec-privatenetwork-yes-privatemounts-yes.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_NETWORK
: EXIT_NAMESPACE
, CLD_EXITED
);
1091 static void test_exec_networknamespacepath(Manager
*m
) {
1094 r
= find_executable("ip", NULL
);
1096 log_notice_errno(r
, "Skipping %s, could not find ip binary: %m", __func__
);
1100 test(m
, "exec-networknamespacepath-privatemounts-no.service", MANAGER_IS_SYSTEM(m
) ? EXIT_SUCCESS
: EXIT_FAILURE
, CLD_EXITED
);
1101 test(m
, "exec-networknamespacepath-privatemounts-yes.service", can_unshare
? EXIT_SUCCESS
: MANAGER_IS_SYSTEM(m
) ? EXIT_FAILURE
: EXIT_NAMESPACE
, CLD_EXITED
);
1104 static void test_exec_oomscoreadjust(Manager
*m
) {
1105 test(m
, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED
);
1107 if (detect_container() > 0) {
1108 log_notice("Testing in container, skipping remaining tests in %s", __func__
);
1111 test(m
, "exec-oomscoreadjust-negative.service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_FAILURE
, CLD_EXITED
);
1114 static void test_exec_ioschedulingclass(Manager
*m
) {
1115 test(m
, "exec-ioschedulingclass-none.service", 0, CLD_EXITED
);
1116 test(m
, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED
);
1117 test(m
, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED
);
1119 if (detect_container() > 0) {
1120 log_notice("Testing in container, skipping remaining tests in %s", __func__
);
1123 test(m
, "exec-ioschedulingclass-realtime.service", MANAGER_IS_SYSTEM(m
) ? 0 : EXIT_IOPRIO
, CLD_EXITED
);
1126 static void test_exec_unsetenvironment(Manager
*m
) {
1127 test(m
, "exec-unsetenvironment.service", 0, CLD_EXITED
);
1130 static void test_exec_specifier(Manager
*m
) {
1131 test(m
, "exec-specifier.service", 0, CLD_EXITED
);
1132 if (MANAGER_IS_SYSTEM(m
))
1133 test(m
, "exec-specifier-system.service", 0, CLD_EXITED
);
1135 test(m
, "exec-specifier-user.service", 0, CLD_EXITED
);
1136 test(m
, "exec-specifier@foo-bar.service", 0, CLD_EXITED
);
1137 test(m
, "exec-specifier-interpolation.service", 0, CLD_EXITED
);
1140 static void test_exec_standardinput(Manager
*m
) {
1141 test(m
, "exec-standardinput-data.service", 0, CLD_EXITED
);
1142 test(m
, "exec-standardinput-file.service", 0, CLD_EXITED
);
1143 test(m
, "exec-standardinput-file-cat.service", 0, CLD_EXITED
);
1146 static void test_exec_standardoutput(Manager
*m
) {
1147 test(m
, "exec-standardoutput-file.service", 0, CLD_EXITED
);
1150 static void test_exec_standardoutput_append(Manager
*m
) {
1151 test(m
, "exec-standardoutput-append.service", 0, CLD_EXITED
);
1154 static void test_exec_standardoutput_truncate(Manager
*m
) {
1155 test(m
, "exec-standardoutput-truncate.service", 0, CLD_EXITED
);
1158 static void test_exec_condition(Manager
*m
) {
1159 test_service(m
, "exec-condition-failed.service", SERVICE_FAILURE_EXIT_CODE
);
1160 test_service(m
, "exec-condition-skip.service", SERVICE_SKIP_CONDITION
);
1163 static void test_exec_umask_namespace(Manager
*m
) {
1164 /* exec-specifier-credentials-dir.service creates /run/credentials and enables implicit
1165 * InaccessiblePath= for the directory for all later services with mount namespace. */
1166 if (!is_inaccessible_available()) {
1167 log_notice("Testing without inaccessible, skipping %s", __func__
);
1170 test(m
, "exec-umask-namespace.service", can_unshare
? 0 : MANAGER_IS_SYSTEM(m
) ? EXIT_NAMESPACE
: EXIT_GROUP
, CLD_EXITED
);
1173 typedef struct test_entry
{
1178 #define entry(x) {x, #x}
1180 static void run_tests(RuntimeScope scope
, char **patterns
) {
1181 _cleanup_(rm_rf_physical_and_freep
) char *runtime_dir
= NULL
;
1182 _cleanup_free_
char *unit_paths
= NULL
;
1183 _cleanup_(manager_freep
) Manager
*m
= NULL
;
1184 usec_t start
, finish
;
1187 static const test_entry tests
[] = {
1188 entry(test_exec_basic
),
1189 entry(test_exec_ambientcapabilities
),
1190 entry(test_exec_bindpaths
),
1191 entry(test_exec_capabilityboundingset
),
1192 entry(test_exec_condition
),
1193 entry(test_exec_cpuaffinity
),
1194 entry(test_exec_credentials
),
1195 entry(test_exec_dynamicuser
),
1196 entry(test_exec_environment
),
1197 entry(test_exec_environmentfile
),
1198 entry(test_exec_execsearchpath
),
1199 entry(test_exec_execsearchpath_environment
),
1200 entry(test_exec_execsearchpath_environment_files
),
1201 entry(test_exec_execsearchpath_passenvironment
),
1202 entry(test_exec_execsearchpath_specifier
),
1203 entry(test_exec_group
),
1204 entry(test_exec_ignoresigpipe
),
1205 entry(test_exec_inaccessiblepaths
),
1206 entry(test_exec_ioschedulingclass
),
1207 entry(test_exec_mount_apivfs
),
1208 entry(test_exec_networknamespacepath
),
1209 entry(test_exec_noexecpaths
),
1210 entry(test_exec_oomscoreadjust
),
1211 entry(test_exec_passenvironment
),
1212 entry(test_exec_personality
),
1213 entry(test_exec_privatedevices
),
1214 entry(test_exec_privatenetwork
),
1215 entry(test_exec_privatetmp
),
1216 entry(test_exec_protecthome
),
1217 entry(test_exec_protectkernelmodules
),
1218 entry(test_exec_readonlypaths
),
1219 entry(test_exec_readwritepaths
),
1220 entry(test_exec_restrictnamespaces
),
1221 entry(test_exec_runtimedirectory
),
1222 entry(test_exec_specifier
),
1223 entry(test_exec_standardinput
),
1224 entry(test_exec_standardoutput
),
1225 entry(test_exec_standardoutput_append
),
1226 entry(test_exec_standardoutput_truncate
),
1227 entry(test_exec_supplementarygroups
),
1228 entry(test_exec_systemcallerrornumber
),
1229 entry(test_exec_systemcallfilter
),
1230 entry(test_exec_systemcallfilter_system
),
1231 entry(test_exec_temporaryfilesystem
),
1232 entry(test_exec_umask
),
1233 entry(test_exec_umask_namespace
),
1234 entry(test_exec_unsetenvironment
),
1235 entry(test_exec_user
),
1236 entry(test_exec_workingdirectory
),
1240 assert_se(unsetenv("USER") == 0);
1241 assert_se(unsetenv("LOGNAME") == 0);
1242 assert_se(unsetenv("SHELL") == 0);
1243 assert_se(unsetenv("HOME") == 0);
1244 assert_se(unsetenv("TMPDIR") == 0);
1246 /* Unset VARx, especially, VAR1, VAR2 and VAR3, which are used in the PassEnvironment test cases,
1247 * otherwise (and if they are present in the environment), `manager_default_environment` will copy
1248 * them into the default environment which is passed to each created job, which will make the tests
1249 * that expect those not to be present to fail. */
1250 assert_se(unsetenv("VAR1") == 0);
1251 assert_se(unsetenv("VAR2") == 0);
1252 assert_se(unsetenv("VAR3") == 0);
1253 assert_se(unsetenv("VAR4") == 0);
1254 assert_se(unsetenv("VAR5") == 0);
1256 assert_se(runtime_dir
= setup_fake_runtime_dir());
1257 assert_se(user_runtime_unit_dir
= path_join(runtime_dir
, "systemd/user"));
1258 assert_se(unit_paths
= strjoin(PRIVATE_UNIT_DIR
, ":", user_runtime_unit_dir
));
1259 assert_se(set_unit_path(unit_paths
) >= 0);
1261 r
= manager_new(scope
, MANAGER_TEST_RUN_BASIC
, &m
);
1262 if (manager_errno_skip_test(r
))
1263 return (void) log_tests_skipped_errno(r
, "manager_new");
1266 m
->default_std_output
= EXEC_OUTPUT_NULL
; /* don't rely on host journald */
1267 assert_se(manager_startup(m
, NULL
, NULL
, NULL
) >= 0);
1269 /* Uncomment below if you want to make debugging logs stored to journal. */
1270 //manager_override_log_target(m, LOG_TARGET_AUTO);
1271 //manager_override_log_level(m, LOG_DEBUG);
1273 /* Measure and print the time that it takes to run tests, excluding startup of the manager object,
1274 * to try and measure latency of spawning services */
1276 start
= now(CLOCK_MONOTONIC
);
1278 for (const test_entry
*test
= tests
; test
->f
; test
++)
1279 if (strv_fnmatch_or_empty(patterns
, test
->name
, FNM_NOESCAPE
))
1282 log_info("Skipping %s because it does not match any pattern.", test
->name
);
1284 finish
= now(CLOCK_MONOTONIC
);
1286 log_info("ran %u tests with %s manager + unshare=%s in: %s",
1288 scope
== RUNTIME_SCOPE_SYSTEM
? "system" : "user",
1289 yes_no(can_unshare
),
1290 FORMAT_TIMESPAN(finish
- start
, USEC_PER_MSEC
));
1293 static int prepare_ns(const char *process_name
) {
1296 r
= safe_fork(process_name
,
1297 FORK_RESET_SIGNALS
|
1298 FORK_CLOSE_ALL_FDS
|
1308 _cleanup_free_
char *unit_dir
= NULL
;
1310 /* Make "/" read-only. */
1311 assert_se(mount_nofollow_verbose(LOG_DEBUG
, NULL
, "/", NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
, NULL
) >= 0);
1313 /* Creating a new user namespace in the above means all MS_SHARED mounts become MS_SLAVE.
1314 * Let's put them back to MS_SHARED here, since that's what we want as defaults. (This will
1315 * not reconnect propagation, but simply create new peer groups for all our mounts). */
1316 assert_se(mount_follow_verbose(LOG_DEBUG
, NULL
, "/", NULL
, MS_SHARED
|MS_REC
, NULL
) >= 0);
1318 assert_se(mkdir_p(PRIVATE_UNIT_DIR
, 0755) >= 0);
1319 assert_se(mount_nofollow_verbose(LOG_DEBUG
, "tmpfs", PRIVATE_UNIT_DIR
, "tmpfs", MS_NOSUID
|MS_NODEV
, NULL
) >= 0);
1321 /* Copy unit files to make them accessible even when unprivileged. */
1322 assert_se(get_testdata_dir("test-execute/", &unit_dir
) >= 0);
1323 assert_se(copy_directory_at(AT_FDCWD
, unit_dir
, AT_FDCWD
, PRIVATE_UNIT_DIR
, COPY_MERGE_EMPTY
) >= 0);
1325 /* Mount tmpfs on the following directories to make not StateDirectory= or friends disturb the host. */
1326 FOREACH_STRING(p
, "/dev/shm", "/root", "/tmp", "/var/tmp", "/var/lib")
1327 assert_se(mount_nofollow_verbose(LOG_DEBUG
, "tmpfs", p
, "tmpfs", MS_NOSUID
|MS_NODEV
, NULL
) >= 0);
1329 /* Prepare credstore like tmpfiles.d/credstore.conf for LoadCredential= tests. */
1330 FOREACH_STRING(p
, "/run/credstore", "/run/credstore.encrypted") {
1331 assert_se(mkdir_p(p
, 0) >= 0);
1332 assert_se(mount_nofollow_verbose(LOG_DEBUG
, "tmpfs", p
, "tmpfs", MS_NOSUID
|MS_NODEV
, "mode=0000") >= 0);
1335 assert_se(write_string_file("/run/credstore/test-execute.load-credential", "foo", WRITE_STRING_FILE_CREATE
) >= 0);
1341 TEST(run_tests_root
) {
1342 _cleanup_strv_free_
char **filters
= NULL
;
1344 if (!have_namespaces())
1345 return (void) log_tests_skipped("unshare() is disabled");
1347 /* safe_fork() clears saved_argv in the child process. Let's copy it. */
1348 assert_se(filters
= strv_copy(strv_skip(saved_argv
, 1)));
1350 if (prepare_ns("(test-execute-root)") == 0) {
1352 run_tests(RUNTIME_SCOPE_SYSTEM
, filters
);
1353 _exit(EXIT_SUCCESS
);
1357 TEST(run_tests_without_unshare
) {
1358 if (!have_namespaces()) {
1359 /* unshare() is already filtered. */
1360 can_unshare
= false;
1361 run_tests(RUNTIME_SCOPE_SYSTEM
, strv_skip(saved_argv
, 1));
1366 _cleanup_strv_free_
char **filters
= NULL
;
1369 /* The following tests are for 1beab8b0d0ff2d7d1436b52d4a0c3d56dc908962. */
1370 if (!is_seccomp_available())
1371 return (void) log_tests_skipped("Seccomp not available, cannot run unshare() filtered tests");
1373 /* safe_fork() clears saved_argv in the child process. Let's copy it. */
1374 assert_se(filters
= strv_copy(strv_skip(saved_argv
, 1)));
1376 if (prepare_ns("(test-execute-without-unshare)") == 0) {
1377 _cleanup_hashmap_free_ Hashmap
*s
= NULL
;
1379 r
= seccomp_syscall_resolve_name("unshare");
1380 assert_se(r
!= __NR_SCMP_ERROR
);
1381 assert_se(hashmap_ensure_put(&s
, NULL
, UINT32_TO_PTR(r
+ 1), INT_TO_PTR(-1)) >= 0);
1382 assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW
, s
, SCMP_ACT_ERRNO(EOPNOTSUPP
), true) >= 0);
1384 /* Check unshare() is actually filtered. */
1385 assert_se(unshare(CLONE_NEWNS
) < 0);
1386 assert_se(errno
== EOPNOTSUPP
);
1388 can_unshare
= false;
1389 run_tests(RUNTIME_SCOPE_SYSTEM
, filters
);
1390 _exit(EXIT_SUCCESS
);
1393 log_tests_skipped("Built without seccomp support, cannot run unshare() filtered tests");
1397 TEST(run_tests_unprivileged
) {
1398 _cleanup_strv_free_
char **filters
= NULL
;
1400 if (!have_namespaces())
1401 return (void) log_tests_skipped("unshare() is disabled");
1403 /* safe_fork() clears saved_argv in the child process. Let's copy it. */
1404 assert_se(filters
= strv_copy(strv_skip(saved_argv
, 1)));
1406 if (prepare_ns("(test-execute-unprivileged)") == 0) {
1407 assert_se(capability_bounding_set_drop(0, /* right_now = */ true) >= 0);
1409 can_unshare
= false;
1410 run_tests(RUNTIME_SCOPE_USER
, filters
);
1411 _exit(EXIT_SUCCESS
);
1415 static int intro(void) {
1416 #if HAS_FEATURE_ADDRESS_SANITIZER
1417 if (strstr_ptr(ci_environment(), "travis") || strstr_ptr(ci_environment(), "github-actions"))
1418 return log_tests_skipped("Running on Travis CI/GH Actions under ASan, see https://github.com/systemd/systemd/issues/10696");
1420 /* It is needed otherwise cgroup creation fails */
1421 if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN
) <= 0)
1422 return log_tests_skipped("not privileged");
1424 if (enter_cgroup_subroot(NULL
) == -ENOMEDIUM
)
1425 return log_tests_skipped("cgroupfs not available");
1427 if (path_is_read_only_fs("/sys") > 0)
1428 return log_tests_skipped("/sys is mounted read-only");
1430 /* Create dummy network interface for testing PrivateNetwork=yes */
1431 (void) system("ip link add dummy-test-exec type dummy");
1433 /* Create a network namespace and a dummy interface in it for NetworkNamespacePath= */
1434 (void) system("ip netns add test-execute-netns");
1435 (void) system("ip netns exec test-execute-netns ip link add dummy-test-ns type dummy");
1437 return EXIT_SUCCESS
;
1440 static int outro(void) {
1441 (void) system("ip link del dummy-test-exec");
1442 (void) system("ip netns del test-execute-netns");
1443 (void) rmdir(PRIVATE_UNIT_DIR
);
1445 return EXIT_SUCCESS
;
1448 DEFINE_TEST_MAIN_FULL(LOG_DEBUG
, intro
, outro
);