]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-execute.c
manager: move various fields that declare unit defaults into a new structure UnitDefaults
[thirdparty/systemd.git] / src / test / test-execute.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
281e05b6 2
ff4ca461 3#include <stdio.h>
4e032f65 4#include <sys/mount.h>
70d7aea5 5#include <sys/prctl.h>
ff4ca461 6#include <sys/types.h>
281e05b6 7
42867dfe
YW
8#include "sd-event.h"
9
b7856f92 10#include "capability-util.h"
4e79aeaa 11#include "cpu-set-util.h"
8c35c10d 12#include "copy.h"
42867dfe 13#include "dropin.h"
b4891260 14#include "errno-list.h"
42867dfe 15#include "fd-util.h"
03bd70dd 16#include "fileio.h"
f4f15635 17#include "fs-util.h"
281e05b6 18#include "macro.h"
f4f15635 19#include "manager.h"
a22692d7 20#include "missing_prctl.h"
281e05b6 21#include "mkdir.h"
4e032f65 22#include "mount-util.h"
ff4ca461 23#include "path-util.h"
42867dfe 24#include "process-util.h"
c6878637 25#include "rm-rf.h"
83f12b27 26#include "seccomp-util.h"
57b7a260 27#include "service.h"
42867dfe
YW
28#include "signal-util.h"
29#include "static-destruct.h"
34b86909 30#include "stat-util.h"
cc100a5a 31#include "tests.h"
8c35c10d 32#include "tmpfile-util.h"
f4f15635 33#include "unit.h"
57c2efa0 34#include "user-util.h"
4dd4cb8f 35#include "virt.h"
281e05b6 36
4e032f65
YW
37#define PRIVATE_UNIT_DIR "/run/test-execute-unit-dir"
38
42867dfe 39static char *user_runtime_unit_dir = NULL;
5f00dc4d 40static bool can_unshare;
b6349ffd 41static unsigned n_ran_tests = 0;
b7172f34 42
42867dfe
YW
43STATIC_DESTRUCTOR_REGISTER(user_runtime_unit_dir, freep);
44
281e05b6
RC
45typedef void (*test_function_t)(Manager *m);
46
c3ab2c38
LP
47static 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;
51}
52
31cd5f63 53static void wait_for_service_finish(Manager *m, Unit *unit) {
281e05b6
RC
54 Service *service = NULL;
55 usec_t ts;
8adb3d63 56 usec_t timeout = 2 * USEC_PER_MINUTE;
281e05b6
RC
57
58 assert_se(m);
59 assert_se(unit);
60
61 service = SERVICE(unit);
62 printf("%s\n", unit->id);
63 exec_context_dump(&service->exec_context, stdout, "\t");
64 ts = now(CLOCK_MONOTONIC);
ec2ce0c5 65 while (!IN_SET(service->state, SERVICE_DEAD, SERVICE_FAILED)) {
281e05b6
RC
66 int r;
67 usec_t n;
68
69 r = sd_event_run(m->event, 100 * USEC_PER_MSEC);
70 assert_se(r >= 0);
71
72 n = now(CLOCK_MONOTONIC);
73 if (ts + timeout < n) {
74 log_error("Test timeout when testing %s", unit->id);
a721cd00 75 r = unit_kill(unit, KILL_ALL, SIGKILL, SI_USER, 0, NULL);
f7f8e8cb
EV
76 if (r < 0)
77 log_error_errno(r, "Failed to kill %s: %m", unit->id);
281e05b6
RC
78 exit(EXIT_FAILURE);
79 }
80 }
31cd5f63
AZ
81}
82
fe65d692
ZJS
83static void check_main_result(const char *file, unsigned line, const char *func,
84 Manager *m, Unit *unit, int status_expected, int code_expected) {
31cd5f63
AZ
85 Service *service = NULL;
86
87 assert_se(m);
88 assert_se(unit);
89
90 wait_for_service_finish(m, unit);
91
92 service = SERVICE(unit);
281e05b6 93 exec_status_dump(&service->main_exec_status, stdout, "\t");
18f8c5d4 94
c3ab2c38 95 if (cld_dumped_to_killed(service->main_exec_status.code) != cld_dumped_to_killed(code_expected)) {
aa272751
YW
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),
6aed6a11
ZJS
98 service->main_exec_status.code, code_expected);
99 abort();
18f8c5d4
LP
100 }
101
102 if (service->main_exec_status.status != status_expected) {
aa272751
YW
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),
18f8c5d4
LP
105 service->main_exec_status.status, status_expected);
106 abort();
6aed6a11 107 }
281e05b6
RC
108}
109
fe65d692
ZJS
110static void check_service_result(const char *file, unsigned line, const char *func,
111 Manager *m, Unit *unit, ServiceResult result_expected) {
31cd5f63
AZ
112 Service *service = NULL;
113
114 assert_se(m);
115 assert_se(unit);
116
117 wait_for_service_finish(m, unit);
118
119 service = SERVICE(unit);
120
121 if (service->result != result_expected) {
aa272751
YW
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),
31cd5f63
AZ
124 service_result_to_string(service->result),
125 service_result_to_string(result_expected));
126 abort();
127 }
128}
129
57c2efa0
YW
130static bool check_nobody_user_and_group(void) {
131 static int cache = -1;
132 struct passwd *p;
133 struct group *g;
134
135 if (cache >= 0)
136 return !!cache;
137
138 if (!synthesize_nobody())
139 goto invalid;
140
141 p = getpwnam(NOBODY_USER_NAME);
142 if (!p ||
143 !streq(p->pw_name, NOBODY_USER_NAME) ||
144 p->pw_uid != UID_NOBODY ||
145 p->pw_gid != GID_NOBODY)
146 goto invalid;
147
148 p = getpwuid(UID_NOBODY);
149 if (!p ||
150 !streq(p->pw_name, NOBODY_USER_NAME) ||
151 p->pw_uid != UID_NOBODY ||
152 p->pw_gid != GID_NOBODY)
153 goto invalid;
154
155 g = getgrnam(NOBODY_GROUP_NAME);
156 if (!g ||
157 !streq(g->gr_name, NOBODY_GROUP_NAME) ||
158 g->gr_gid != GID_NOBODY)
159 goto invalid;
160
161 g = getgrgid(GID_NOBODY);
162 if (!g ||
163 !streq(g->gr_name, NOBODY_GROUP_NAME) ||
164 g->gr_gid != GID_NOBODY)
165 goto invalid;
166
167 cache = 1;
168 return true;
169
170invalid:
171 cache = 0;
172 return false;
173}
174
9f82d685
YW
175static bool check_user_has_group_with_same_name(const char *name) {
176 struct passwd *p;
177 struct group *g;
178
f21b863e 179 assert_se(name);
9f82d685
YW
180
181 p = getpwnam(name);
182 if (!p ||
183 !streq(p->pw_name, name))
184 return false;
185
186 g = getgrgid(p->pw_gid);
187 if (!g ||
188 !streq(g->gr_name, name))
189 return false;
190
191 return true;
192}
193
6086d2da 194static bool is_inaccessible_available(void) {
6086d2da 195 FOREACH_STRING(p,
5980d463
ZJS
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")
6086d2da
DP
202 if (access(p, F_OK) < 0)
203 return false;
6086d2da
DP
204
205 return true;
206}
207
fc6172b1
RP
208static void start_parent_slices(Unit *unit) {
209 Unit *slice;
210
211 slice = UNIT_GET_SLICE(unit);
212 if (slice) {
213 start_parent_slices(slice);
214 int r = unit_start(slice, NULL);
215 assert_se(r >= 0 || r == -EALREADY);
216 }
217}
218
fe65d692
ZJS
219static void _test(const char *file, unsigned line, const char *func,
220 Manager *m, const char *unit_name, int status_expected, int code_expected) {
281e05b6
RC
221 Unit *unit;
222
223 assert_se(unit_name);
224
ba412430 225 assert_se(manager_load_startable_unit_or_warn(m, unit_name, NULL, &unit) >= 0);
fc6172b1
RP
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);
48b92b37 229 assert_se(unit_start(unit, NULL) >= 0);
fe65d692 230 check_main_result(file, line, func, m, unit, status_expected, code_expected);
b6349ffd
LB
231
232 ++n_ran_tests;
31cd5f63 233}
fe65d692
ZJS
234#define test(m, unit_name, status_expected, code_expected) \
235 _test(PROJECT_FILE, __LINE__, __func__, m, unit_name, status_expected, code_expected)
31cd5f63 236
fe65d692
ZJS
237static void _test_service(const char *file, unsigned line, const char *func,
238 Manager *m, const char *unit_name, ServiceResult result_expected) {
31cd5f63
AZ
239 Unit *unit;
240
241 assert_se(unit_name);
242
243 assert_se(manager_load_startable_unit_or_warn(m, unit_name, NULL, &unit) >= 0);
48b92b37 244 assert_se(unit_start(unit, NULL) >= 0);
fe65d692 245 check_service_result(file, line, func, m, unit, result_expected);
281e05b6 246}
fe65d692
ZJS
247#define test_service(m, unit_name, result_expected) \
248 _test_service(PROJECT_FILE, __LINE__, __func__, m, unit_name, result_expected)
281e05b6 249
f0e018e7
YW
250static 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);
d053b72b 253
fe65d692 254 test(m, "exec-bindpaths.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
d053b72b 255
f0e018e7
YW
256 (void) rm_rf("/tmp/test-exec-bindpaths", REMOVE_ROOT|REMOVE_PHYSICAL);
257 (void) rm_rf("/tmp/test-exec-bindreadonlypaths", REMOVE_ROOT|REMOVE_PHYSICAL);
d053b72b
YW
258}
259
4e79aeaa 260static void test_exec_cpuaffinity(Manager *m) {
167a776d 261 _cleanup_(cpu_set_reset) CPUSet c = {};
4e79aeaa 262
167a776d
ZJS
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);
4e79aeaa 265
167a776d 266 if (!CPU_ISSET_S(0, c.allocated, c.set)) {
4e79aeaa
YW
267 log_notice("Cannot use CPU 0, skipping %s", __func__);
268 return;
269 }
270
fe65d692
ZJS
271 test(m, "exec-cpuaffinity1.service", 0, CLD_EXITED);
272 test(m, "exec-cpuaffinity2.service", 0, CLD_EXITED);
4e79aeaa 273
167a776d
ZJS
274 if (!CPU_ISSET_S(1, c.allocated, c.set) ||
275 !CPU_ISSET_S(2, c.allocated, c.set)) {
4e79aeaa
YW
276 log_notice("Cannot use CPU 1 or 2, skipping remaining tests in %s", __func__);
277 return;
278 }
279
fe65d692 280 test(m, "exec-cpuaffinity3.service", 0, CLD_EXITED);
4e79aeaa
YW
281}
282
b7cca6cc
YW
283static void test_exec_credentials(Manager *m) {
284 test(m, "exec-set-credential.service", 0, CLD_EXITED);
285 test(m, "exec-load-credential.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_CREDENTIALS, CLD_EXITED);
286 test(m, "exec-credentials-dir-specifier.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_CREDENTIALS, CLD_EXITED);
287}
288
281e05b6
RC
289static void test_exec_workingdirectory(Manager *m) {
290 assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0);
291
fe65d692
ZJS
292 test(m, "exec-workingdirectory.service", 0, CLD_EXITED);
293 test(m, "exec-workingdirectory-trailing-dot.service", 0, CLD_EXITED);
281e05b6 294
c6878637 295 (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT|REMOVE_PHYSICAL);
281e05b6
RC
296}
297
8c35c10d 298static void test_exec_execsearchpath(Manager *m) {
299 assert_se(mkdir_p("/tmp/test-exec_execsearchpath", 0755) >= 0);
300
7c2f5495 301 assert_se(copy_file("/bin/ls", "/tmp/test-exec_execsearchpath/ls_temp", 0, 0777, COPY_REPLACE) >= 0);
8c35c10d 302
303 test(m, "exec-execsearchpath.service", 0, CLD_EXITED);
304
305 assert_se(rm_rf("/tmp/test-exec_execsearchpath", REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
306
307 test(m, "exec-execsearchpath.service", EXIT_EXEC, CLD_EXITED);
308}
309
310static void test_exec_execsearchpath_specifier(Manager *m) {
311 test(m, "exec-execsearchpath-unit-specifier.service", 0, CLD_EXITED);
312}
313
314static void test_exec_execsearchpath_environment(Manager *m) {
315 test(m, "exec-execsearchpath-environment.service", 0, CLD_EXITED);
316 test(m, "exec-execsearchpath-environment-path-set.service", 0, CLD_EXITED);
317}
318
319static void test_exec_execsearchpath_environment_files(Manager *m) {
320 static const char path_not_set[] =
321 "VAR1='word1 word2'\n"
322 "VAR2=word3 \n"
323 "# comment1\n"
324 "\n"
325 "; comment2\n"
326 " ; # comment3\n"
327 "line without an equal\n"
328 "VAR3='$word 5 6'\n"
329 "VAR4='new\nline'\n"
330 "VAR5=password\\with\\backslashes";
331
332 static const char path_set[] =
333 "VAR1='word1 word2'\n"
334 "VAR2=word3 \n"
335 "# comment1\n"
336 "\n"
337 "; comment2\n"
338 " ; # comment3\n"
339 "line without an equal\n"
340 "VAR3='$word 5 6'\n"
341 "VAR4='new\nline'\n"
342 "VAR5=password\\with\\backslashes\n"
343 "PATH=/usr";
344
345 int r;
346
347 r = write_string_file("/tmp/test-exec_execsearchpath_environmentfile.conf", path_not_set, WRITE_STRING_FILE_CREATE);
348
349 assert_se(r == 0);
350
351 test(m, "exec-execsearchpath-environmentfile.service", 0, CLD_EXITED);
352
353 (void) unlink("/tmp/test-exec_environmentfile.conf");
354
355
356 r = write_string_file("/tmp/test-exec_execsearchpath_environmentfile-set.conf", path_set, WRITE_STRING_FILE_CREATE);
357
358 assert_se(r == 0);
359
360 test(m, "exec-execsearchpath-environmentfile-set.service", 0, CLD_EXITED);
361
362 (void) unlink("/tmp/test-exec_environmentfile-set.conf");
363}
364
365static void test_exec_execsearchpath_passenvironment(Manager *m) {
366 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
367 assert_se(setenv("VAR2", "word3", 1) == 0);
368 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
369 assert_se(setenv("VAR4", "new\nline", 1) == 0);
370 assert_se(setenv("VAR5", "passwordwithbackslashes", 1) == 0);
371
372 test(m, "exec-execsearchpath-passenvironment.service", 0, CLD_EXITED);
373
374 assert_se(setenv("PATH", "/usr", 1) == 0);
375 test(m, "exec-execsearchpath-passenvironment-set.service", 0, CLD_EXITED);
376
377 assert_se(unsetenv("VAR1") == 0);
378 assert_se(unsetenv("VAR2") == 0);
379 assert_se(unsetenv("VAR3") == 0);
380 assert_se(unsetenv("VAR4") == 0);
381 assert_se(unsetenv("VAR5") == 0);
382 assert_se(unsetenv("PATH") == 0);
383}
384
281e05b6 385static void test_exec_personality(Manager *m) {
281e05b6 386#if defined(__x86_64__)
fe65d692 387 test(m, "exec-personality-x86-64.service", 0, CLD_EXITED);
7517f51e
HB
388
389#elif defined(__s390__)
fe65d692 390 test(m, "exec-personality-s390.service", 0, CLD_EXITED);
7517f51e 391
12591863
JS
392#elif defined(__powerpc64__)
393# if __BYTE_ORDER == __BIG_ENDIAN
fe65d692 394 test(m, "exec-personality-ppc64.service", 0, CLD_EXITED);
12591863 395# else
fe65d692 396 test(m, "exec-personality-ppc64le.service", 0, CLD_EXITED);
12591863
JS
397# endif
398
399#elif defined(__aarch64__)
fe65d692 400 test(m, "exec-personality-aarch64.service", 0, CLD_EXITED);
12591863 401
5798eb4c 402#elif defined(__i386__)
fe65d692 403 test(m, "exec-personality-x86.service", 0, CLD_EXITED);
f106a639 404#elif defined(__loongarch_lp64)
646b0112 405 test(m, "exec-personality-loongarch64.service", 0, CLD_EXITED);
f0e018e7
YW
406#else
407 log_notice("Unknown personality, skipping %s", __func__);
281e05b6
RC
408#endif
409}
410
411static void test_exec_ignoresigpipe(Manager *m) {
fe65d692
ZJS
412 test(m, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED);
413 test(m, "exec-ignoresigpipe-no.service", SIGPIPE, CLD_KILLED);
281e05b6
RC
414}
415
416static void test_exec_privatetmp(Manager *m) {
417 assert_se(touch("/tmp/test-exec_privatetmp") >= 0);
418
6ef721cb 419 test(m, "exec-privatetmp-yes.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
fe65d692 420 test(m, "exec-privatetmp-no.service", 0, CLD_EXITED);
6ef721cb 421 test(m, "exec-privatetmp-disabled-by-prefix.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
281e05b6 422
5000cea8 423 (void) unlink("/tmp/test-exec_privatetmp");
281e05b6
RC
424}
425
426static void test_exec_privatedevices(Manager *m) {
f0e018e7
YW
427 int r;
428
4dd4cb8f 429 if (detect_container() > 0) {
f0e018e7 430 log_notice("Testing in container, skipping %s", __func__);
4dd4cb8f
SM
431 return;
432 }
6086d2da 433 if (!is_inaccessible_available()) {
f0e018e7 434 log_notice("Testing without inaccessible, skipping %s", __func__);
6086d2da
DP
435 return;
436 }
437
6ef721cb 438 test(m, "exec-privatedevices-yes.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
fe65d692 439 test(m, "exec-privatedevices-no.service", 0, CLD_EXITED);
6ef721cb
LB
440 test(m, "exec-privatedevices-disabled-by-prefix.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
441 test(m, "exec-privatedevices-yes-with-group.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
6086d2da 442
0608ba98
ZJS
443 /* We use capsh to test if the capabilities are
444 * properly set, so be sure that it exists */
f7bc0c32 445 r = find_executable("capsh", NULL);
0608ba98 446 if (r < 0) {
5cd33ccc 447 log_notice_errno(r, "Could not find capsh binary, skipping remaining tests in %s: %m", __func__);
6086d2da
DP
448 return;
449 }
450
6ef721cb 451 test(m, "exec-privatedevices-yes-capability-mknod.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
adeff822 452 test(m, "exec-privatedevices-no-capability-mknod.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED);
6ef721cb 453 test(m, "exec-privatedevices-yes-capability-sys-rawio.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
adeff822 454 test(m, "exec-privatedevices-no-capability-sys-rawio.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED);
615a1f4b
DH
455}
456
7e46b29b 457static void test_exec_protecthome(Manager *m) {
9ca58284
ZJS
458 if (!can_unshare) {
459 log_notice("Cannot reliably unshare, skipping %s", __func__);
460 return;
461 }
462
fe65d692 463 test(m, "exec-protecthome-tmpfs-vs-protectsystem-strict.service", 0, CLD_EXITED);
7e46b29b
YW
464}
465
4982dbcc 466static void test_exec_protectkernelmodules(Manager *m) {
0608ba98
ZJS
467 int r;
468
3ae33295 469 if (detect_container() > 0) {
f0e018e7 470 log_notice("Testing in container, skipping %s", __func__);
3ae33295
DH
471 return;
472 }
6086d2da 473 if (!is_inaccessible_available()) {
f0e018e7 474 log_notice("Testing without inaccessible, skipping %s", __func__);
6086d2da
DP
475 return;
476 }
3ae33295 477
f7bc0c32 478 r = find_executable("capsh", NULL);
0608ba98 479 if (r < 0) {
5cd33ccc 480 log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
0608ba98
ZJS
481 return;
482 }
483
adeff822 484 test(m, "exec-protectkernelmodules-no-capabilities.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED);
6ef721cb
LB
485 test(m, "exec-protectkernelmodules-yes-capabilities.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
486 test(m, "exec-protectkernelmodules-yes-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
3ae33295
DH
487}
488
f78b36f0 489static void test_exec_readonlypaths(Manager *m) {
34b86909 490
6ef721cb 491 test(m, "exec-readonlypaths-simple.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
f0e018e7
YW
492
493 if (path_is_read_only_fs("/var") > 0) {
494 log_notice("Directory /var is readonly, skipping remaining tests in %s", __func__);
34b86909 495 return;
f0e018e7 496 }
34b86909 497
6ef721cb 498 test(m, "exec-readonlypaths.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
fe65d692 499 test(m, "exec-readonlypaths-with-bindpaths.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
6ef721cb 500 test(m, "exec-readonlypaths-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
cdfbd1fb
DH
501}
502
503static void test_exec_readwritepaths(Manager *m) {
34b86909 504
f0e018e7
YW
505 if (path_is_read_only_fs("/") > 0) {
506 log_notice("Root directory is readonly, skipping %s", __func__);
34b86909 507 return;
f0e018e7 508 }
34b86909 509
6ef721cb 510 test(m, "exec-readwritepaths-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
cdfbd1fb
DH
511}
512
513static void test_exec_inaccessiblepaths(Manager *m) {
34b86909 514
f0e018e7
YW
515 if (!is_inaccessible_available()) {
516 log_notice("Testing without inaccessible, skipping %s", __func__);
34b86909 517 return;
f0e018e7 518 }
34b86909 519
6ef721cb 520 test(m, "exec-inaccessiblepaths-sys.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
f78b36f0 521
f0e018e7
YW
522 if (path_is_read_only_fs("/") > 0) {
523 log_notice("Root directory is readonly, skipping remaining tests in %s", __func__);
af4af186
EV
524 return;
525 }
526
6ef721cb 527 test(m, "exec-inaccessiblepaths-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
c090d74d
TR
528}
529
42867dfe
YW
530static int on_spawn_io(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
531 char **result = userdata;
532 char buf[4096];
533 ssize_t l;
534
f21b863e
YW
535 assert_se(s);
536 assert_se(fd >= 0);
42867dfe
YW
537
538 l = read(fd, buf, sizeof(buf) - 1);
539 if (l < 0) {
540 if (errno == EAGAIN)
541 goto reenable;
542
543 return 0;
544 }
545 if (l == 0)
546 return 0;
547
548 buf[l] = '\0';
549 if (result)
550 assert_se(strextend(result, buf));
551 else
552 log_error("ldd: %s", buf);
553
554reenable:
555 /* Re-enable the event source if we did not encounter EOF */
556 assert_se(sd_event_source_set_enabled(s, SD_EVENT_ONESHOT) >= 0);
557 return 0;
558}
559
560static int on_spawn_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
561 pid_t *pid = userdata;
562
f21b863e 563 assert_se(pid);
42867dfe
YW
564
565 (void) kill(*pid, SIGKILL);
566
567 return 1;
568}
569
570static int on_spawn_sigchld(sd_event_source *s, const siginfo_t *si, void *userdata) {
571 int ret = -EIO;
572
f21b863e 573 assert_se(si);
42867dfe
YW
574
575 if (si->si_code == CLD_EXITED)
576 ret = si->si_status;
577
578 sd_event_exit(sd_event_source_get_event(s), ret);
579 return 1;
580}
581
582static int find_libraries(const char *exec, char ***ret) {
583 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
584 _cleanup_(sd_event_source_unrefp) sd_event_source *sigchld_source = NULL;
585 _cleanup_(sd_event_source_unrefp) sd_event_source *stdout_source = NULL;
586 _cleanup_(sd_event_source_unrefp) sd_event_source *stderr_source = NULL;
19ee48a6 587 _cleanup_close_pair_ int outpipe[2] = PIPE_EBADF, errpipe[2] = PIPE_EBADF;
42867dfe
YW
588 _cleanup_strv_free_ char **libraries = NULL;
589 _cleanup_free_ char *result = NULL;
590 pid_t pid;
591 int r;
592
f21b863e
YW
593 assert_se(exec);
594 assert_se(ret);
42867dfe
YW
595
596 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0);
597
598 assert_se(pipe2(outpipe, O_NONBLOCK|O_CLOEXEC) == 0);
599 assert_se(pipe2(errpipe, O_NONBLOCK|O_CLOEXEC) == 0);
600
0c2aedb4
YW
601 r = safe_fork_full("(spawn-ldd)",
602 (int[]) { -EBADF, outpipe[1], errpipe[1] },
603 NULL, 0,
604 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG|FORK_REARRANGE_STDIO|FORK_LOG, &pid);
42867dfe
YW
605 assert_se(r >= 0);
606 if (r == 0) {
42867dfe
YW
607 execlp("ldd", "ldd", exec, NULL);
608 _exit(EXIT_FAILURE);
609 }
610
611 outpipe[1] = safe_close(outpipe[1]);
612 errpipe[1] = safe_close(errpipe[1]);
613
614 assert_se(sd_event_new(&e) >= 0);
615
616 assert_se(sd_event_add_time_relative(e, NULL, CLOCK_MONOTONIC,
617 10 * USEC_PER_SEC, USEC_PER_SEC, on_spawn_timeout, &pid) >= 0);
618 assert_se(sd_event_add_io(e, &stdout_source, outpipe[0], EPOLLIN, on_spawn_io, &result) >= 0);
619 assert_se(sd_event_source_set_enabled(stdout_source, SD_EVENT_ONESHOT) >= 0);
620 assert_se(sd_event_add_io(e, &stderr_source, errpipe[0], EPOLLIN, on_spawn_io, NULL) >= 0);
621 assert_se(sd_event_source_set_enabled(stderr_source, SD_EVENT_ONESHOT) >= 0);
622 assert_se(sd_event_add_child(e, &sigchld_source, pid, WEXITED, on_spawn_sigchld, NULL) >= 0);
623 /* SIGCHLD should be processed after IO is complete */
624 assert_se(sd_event_source_set_priority(sigchld_source, SD_EVENT_PRIORITY_NORMAL + 1) >= 0);
625
626 assert_se(sd_event_loop(e) >= 0);
627
628 _cleanup_strv_free_ char **v = NULL;
629 assert_se(strv_split_newlines_full(&v, result, 0) >= 0);
630
42867dfe
YW
631 STRV_FOREACH(q, v) {
632 _cleanup_free_ char *word = NULL;
633 const char *p = *q;
634
635 r = extract_first_word(&p, &word, NULL, 0);
636 assert_se(r >= 0);
637 if (r == 0)
638 continue;
639
640 if (path_is_absolute(word)) {
641 assert_se(strv_consume(&libraries, TAKE_PTR(word)) >= 0);
642 continue;
643 }
644
645 word = mfree(word);
646 r = extract_first_word(&p, &word, NULL, 0);
647 assert_se(r >= 0);
648 if (r == 0)
649 continue;
650
651 if (!streq_ptr(word, "=>"))
652 continue;
653
654 word = mfree(word);
655 r = extract_first_word(&p, &word, NULL, 0);
656 assert_se(r >= 0);
657 if (r == 0)
658 continue;
659
660 if (path_is_absolute(word)) {
661 assert_se(strv_consume(&libraries, TAKE_PTR(word)) >= 0);
662 continue;
663 }
664 }
665
666 *ret = TAKE_PTR(libraries);
667 return 0;
668}
669
670static void test_exec_mount_apivfs(Manager *m) {
671 _cleanup_free_ char *fullpath_touch = NULL, *fullpath_test = NULL, *data = NULL;
672 _cleanup_strv_free_ char **libraries = NULL, **libraries_test = NULL;
673 int r;
674
f21b863e 675 assert_se(user_runtime_unit_dir);
42867dfe 676
9f452259
YW
677 r = find_executable("ldd", NULL);
678 if (r < 0) {
679 log_notice_errno(r, "Skipping %s, could not find 'ldd' command: %m", __func__);
680 return;
681 }
42867dfe
YW
682 r = find_executable("touch", &fullpath_touch);
683 if (r < 0) {
684 log_notice_errno(r, "Skipping %s, could not find 'touch' command: %m", __func__);
685 return;
686 }
687 r = find_executable("test", &fullpath_test);
688 if (r < 0) {
689 log_notice_errno(r, "Skipping %s, could not find 'test' command: %m", __func__);
690 return;
691 }
692
693 assert_se(find_libraries(fullpath_touch, &libraries) >= 0);
694 assert_se(find_libraries(fullpath_test, &libraries_test) >= 0);
695 assert_se(strv_extend_strv(&libraries, libraries_test, true) >= 0);
696
697 assert_se(strextend(&data, "[Service]\n"));
698 assert_se(strextend(&data, "ExecStart=", fullpath_touch, " /aaa\n"));
699 assert_se(strextend(&data, "ExecStart=", fullpath_test, " -f /aaa\n"));
700 assert_se(strextend(&data, "BindReadOnlyPaths=", fullpath_touch, "\n"));
701 assert_se(strextend(&data, "BindReadOnlyPaths=", fullpath_test, "\n"));
702
42867dfe
YW
703 STRV_FOREACH(p, libraries)
704 assert_se(strextend(&data, "BindReadOnlyPaths=", *p, "\n"));
705
706 assert_se(write_drop_in(user_runtime_unit_dir, "exec-mount-apivfs-no.service", 10, "bind-mount", data) >= 0);
707
708 assert_se(mkdir_p("/tmp/test-exec-mount-apivfs-no/root", 0755) >= 0);
709
6ef721cb 710 test(m, "exec-mount-apivfs-no.service", can_unshare || !MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
42867dfe
YW
711
712 (void) rm_rf("/tmp/test-exec-mount-apivfs-no/root", REMOVE_ROOT|REMOVE_PHYSICAL);
713}
714
ddc155b2
TM
715static void test_exec_noexecpaths(Manager *m) {
716
6ef721cb 717 test(m, "exec-noexecpaths-simple.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
ddc155b2
TM
718}
719
4cac89bd
YW
720static void test_exec_temporaryfilesystem(Manager *m) {
721
fe65d692
ZJS
722 test(m, "exec-temporaryfilesystem-options.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
723 test(m, "exec-temporaryfilesystem-ro.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
724 test(m, "exec-temporaryfilesystem-rw.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
725 test(m, "exec-temporaryfilesystem-usr.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
4cac89bd
YW
726}
727
281e05b6 728static void test_exec_systemcallfilter(Manager *m) {
349cc4a5 729#if HAVE_SECCOMP
738c74d7
YW
730 int r;
731
f0e018e7
YW
732 if (!is_seccomp_available()) {
733 log_notice("Seccomp not available, skipping %s", __func__);
83f12b27 734 return;
f0e018e7
YW
735 }
736
fe65d692
ZJS
737 test(m, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED);
738 test(m, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED);
e975a945 739 test(m, "exec-systemcallfilter-not-failing3.service", 0, CLD_EXITED);
fe65d692
ZJS
740 test(m, "exec-systemcallfilter-failing.service", SIGSYS, CLD_KILLED);
741 test(m, "exec-systemcallfilter-failing2.service", SIGSYS, CLD_KILLED);
e975a945 742 test(m, "exec-systemcallfilter-failing3.service", SIGSYS, CLD_KILLED);
738c74d7 743
f7bc0c32 744 r = find_executable("python3", NULL);
738c74d7
YW
745 if (r < 0) {
746 log_notice_errno(r, "Skipping remaining tests in %s, could not find python3 binary: %m", __func__);
747 return;
748 }
749
fe65d692
ZJS
750 test(m, "exec-systemcallfilter-with-errno-name.service", errno_from_name("EILSEQ"), CLD_EXITED);
751 test(m, "exec-systemcallfilter-with-errno-number.service", 255, CLD_EXITED);
752 test(m, "exec-systemcallfilter-with-errno-multi.service", errno_from_name("EILSEQ"), CLD_EXITED);
a62f651b 753 test(m, "exec-systemcallfilter-with-errno-in-allow-list.service", errno_from_name("EILSEQ"), CLD_EXITED);
fe65d692
ZJS
754 test(m, "exec-systemcallfilter-override-error-action.service", SIGSYS, CLD_KILLED);
755 test(m, "exec-systemcallfilter-override-error-action2.service", errno_from_name("EILSEQ"), CLD_EXITED);
281e05b6
RC
756#endif
757}
758
759static void test_exec_systemcallerrornumber(Manager *m) {
349cc4a5 760#if HAVE_SECCOMP
738c74d7
YW
761 int r;
762
f0e018e7
YW
763 if (!is_seccomp_available()) {
764 log_notice("Seccomp not available, skipping %s", __func__);
7a18854f 765 return;
f0e018e7
YW
766 }
767
f7bc0c32 768 r = find_executable("python3", NULL);
738c74d7
YW
769 if (r < 0) {
770 log_notice_errno(r, "Skipping %s, could not find python3 binary: %m", __func__);
771 return;
772 }
773
fe65d692
ZJS
774 test(m, "exec-systemcallerrornumber-name.service", errno_from_name("EACCES"), CLD_EXITED);
775 test(m, "exec-systemcallerrornumber-number.service", 255, CLD_EXITED);
281e05b6
RC
776#endif
777}
778
f0e018e7 779static void test_exec_restrictnamespaces(Manager *m) {
349cc4a5 780#if HAVE_SECCOMP
f0e018e7
YW
781 if (!is_seccomp_available()) {
782 log_notice("Seccomp not available, skipping %s", __func__);
97e60383 783 return;
f0e018e7 784 }
97e60383 785
fe65d692
ZJS
786 test(m, "exec-restrictnamespaces-no.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
787 test(m, "exec-restrictnamespaces-yes.service", 1, CLD_EXITED);
788 test(m, "exec-restrictnamespaces-mnt.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
789 test(m, "exec-restrictnamespaces-mnt-deny-list.service", 1, CLD_EXITED);
790 test(m, "exec-restrictnamespaces-merge-and.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
791 test(m, "exec-restrictnamespaces-merge-or.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
792 test(m, "exec-restrictnamespaces-merge-all.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
97e60383
DH
793#endif
794}
795
f0e018e7 796static void test_exec_systemcallfilter_system(Manager *m) {
4967da2d
FS
797/* Skip this particular test case when running under ASan, as
798 * LSan intermittently segfaults when accessing memory right
799 * after the test finishes. Generally, ASan & LSan don't like
800 * the seccomp stuff.
801 */
802#if HAVE_SECCOMP && !HAS_FEATURE_ADDRESS_SANITIZER
f0e018e7
YW
803 if (!is_seccomp_available()) {
804 log_notice("Seccomp not available, skipping %s", __func__);
83f12b27 805 return;
f0e018e7 806 }
57c2efa0 807
4e032f65 808 test(m, "exec-systemcallfilter-system-user.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
69b07407 809
57c2efa0 810 if (!check_nobody_user_and_group()) {
5cd33ccc 811 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
57c2efa0
YW
812 return;
813 }
814
69b07407 815 if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
5cd33ccc 816 log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
69b07407
YW
817 return;
818 }
819
4e032f65 820 test(m, "exec-systemcallfilter-system-user-" NOBODY_USER_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
19c0b0b9
RC
821#endif
822}
823
281e05b6 824static void test_exec_user(Manager *m) {
4e032f65 825 test(m, "exec-user.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
69b07407 826
57c2efa0 827 if (!check_nobody_user_and_group()) {
5cd33ccc 828 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
57c2efa0
YW
829 return;
830 }
831
69b07407 832 if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
5cd33ccc 833 log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
69b07407
YW
834 return;
835 }
836
4e032f65 837 test(m, "exec-user-" NOBODY_USER_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
281e05b6
RC
838}
839
840static void test_exec_group(Manager *m) {
4e032f65 841 test(m, "exec-group.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
69b07407 842
57c2efa0 843 if (!check_nobody_user_and_group()) {
5cd33ccc 844 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
57c2efa0
YW
845 return;
846 }
847
69b07407 848 if (!STR_IN_SET(NOBODY_GROUP_NAME, "nobody", "nfsnobody", "nogroup")) {
5cd33ccc 849 log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME, __func__);
69b07407
YW
850 return;
851 }
852
4e032f65 853 test(m, "exec-group-" NOBODY_GROUP_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
281e05b6
RC
854}
855
f0e018e7 856static void test_exec_supplementarygroups(Manager *m) {
4e032f65
YW
857 int status = MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP;
858 test(m, "exec-supplementarygroups.service", status, CLD_EXITED);
859 test(m, "exec-supplementarygroups-single-group.service", status, CLD_EXITED);
860 test(m, "exec-supplementarygroups-single-group-user.service", status, CLD_EXITED);
861 test(m, "exec-supplementarygroups-multiple-groups-default-group-user.service", status, CLD_EXITED);
862 test(m, "exec-supplementarygroups-multiple-groups-withgid.service", status, CLD_EXITED);
863 test(m, "exec-supplementarygroups-multiple-groups-withuid.service", status, CLD_EXITED);
86b838ea
DH
864}
865
cced2b98
ZJS
866static char* private_directory_bad(Manager *m) {
867 /* This mirrors setup_exec_directory(). */
868
869 for (ExecDirectoryType dt = 0; dt < _EXEC_DIRECTORY_TYPE_MAX; dt++) {
870 _cleanup_free_ char *p = NULL;
871 struct stat st;
872
873 assert_se(p = path_join(m->prefix[dt], "private"));
874
875 if (stat(p, &st) >= 0 &&
876 (st.st_mode & (S_IRWXG|S_IRWXO)))
877 return TAKE_PTR(p);
878 }
879
880 return NULL;
881}
882
f0e018e7 883static void test_exec_dynamicuser(Manager *m) {
cced2b98
ZJS
884 _cleanup_free_ char *bad = private_directory_bad(m);
885 if (bad) {
886 log_warning("%s: %s has bad permissions, skipping test.", __func__, bad);
887 return;
888 }
b7172f34 889
d1b74295
FS
890 if (strstr_ptr(ci_environment(), "github-actions")) {
891 log_notice("%s: skipping test on GH Actions because of systemd/systemd#10337", __func__);
892 return;
893 }
894
4e032f65
YW
895 int status = can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NAMESPACE : EXIT_GROUP;
896
897 test(m, "exec-dynamicuser-fixeduser.service", status, CLD_EXITED);
9f82d685 898 if (check_user_has_group_with_same_name("adm"))
4e032f65 899 test(m, "exec-dynamicuser-fixeduser-adm.service", status, CLD_EXITED);
9f82d685 900 if (check_user_has_group_with_same_name("games"))
4e032f65
YW
901 test(m, "exec-dynamicuser-fixeduser-games.service", status, CLD_EXITED);
902 test(m, "exec-dynamicuser-fixeduser-one-supplementarygroup.service", status, CLD_EXITED);
903 test(m, "exec-dynamicuser-supplementarygroups.service", status, CLD_EXITED);
904 test(m, "exec-dynamicuser-statedir.service", status, CLD_EXITED);
028f3a7f 905
f5939651 906 (void) rm_rf("/var/lib/quux", REMOVE_ROOT|REMOVE_PHYSICAL);
1c587309
YW
907 (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
908 (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
f5939651
TM
909 (void) rm_rf("/var/lib/waldo", REMOVE_ROOT|REMOVE_PHYSICAL);
910 (void) rm_rf("/var/lib/private/quux", REMOVE_ROOT|REMOVE_PHYSICAL);
1c587309
YW
911 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
912 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
f5939651 913 (void) rm_rf("/var/lib/private/waldo", REMOVE_ROOT|REMOVE_PHYSICAL);
1c587309 914
fe65d692 915 test(m, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED);
4e032f65 916 test(m, "exec-dynamicuser-statedir-migrate-step2.service", status, CLD_EXITED);
fe65d692 917 test(m, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED);
1c587309 918
028f3a7f
YW
919 (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
920 (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
921 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
922 (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
e4c01fe6 923
4e032f65
YW
924 test(m, "exec-dynamicuser-runtimedirectory1.service", status, CLD_EXITED);
925 test(m, "exec-dynamicuser-runtimedirectory2.service", status, CLD_EXITED);
926 test(m, "exec-dynamicuser-runtimedirectory3.service", status, CLD_EXITED);
2b9ac11e
DH
927}
928
281e05b6 929static void test_exec_environment(Manager *m) {
fe65d692
ZJS
930 test(m, "exec-environment-no-substitute.service", 0, CLD_EXITED);
931 test(m, "exec-environment.service", 0, CLD_EXITED);
932 test(m, "exec-environment-multiple.service", 0, CLD_EXITED);
933 test(m, "exec-environment-empty.service", 0, CLD_EXITED);
281e05b6
RC
934}
935
03bd70dd
RC
936static void test_exec_environmentfile(Manager *m) {
937 static const char e[] =
938 "VAR1='word1 word2'\n"
939 "VAR2=word3 \n"
940 "# comment1\n"
941 "\n"
942 "; comment2\n"
943 " ; # comment3\n"
944 "line without an equal\n"
9b796f35 945 "VAR3='$word 5 6'\n"
b6887d7a
JH
946 "VAR4='new\nline'\n"
947 "VAR5=password\\with\\backslashes";
03bd70dd
RC
948 int r;
949
950 r = write_string_file("/tmp/test-exec_environmentfile.conf", e, WRITE_STRING_FILE_CREATE);
951 assert_se(r == 0);
952
fe65d692 953 test(m, "exec-environmentfile.service", 0, CLD_EXITED);
03bd70dd 954
f0e018e7 955 (void) unlink("/tmp/test-exec_environmentfile.conf");
03bd70dd
RC
956}
957
4c80d201 958static void test_exec_passenvironment(Manager *m) {
e1abca2e
FB
959 /* test-execute runs under MANAGER_USER which, by default, forwards all
960 * variables present in the environment, but only those that are
961 * present _at the time it is created_!
962 *
963 * So these PassEnvironment checks are still expected to work, since we
964 * are ensuring the variables are not present at manager creation (they
965 * are unset explicitly in main) and are only set here.
966 *
967 * This is still a good approximation of how a test for MANAGER_SYSTEM
968 * would work.
969 */
4c80d201
FB
970 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
971 assert_se(setenv("VAR2", "word3", 1) == 0);
972 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
9b796f35 973 assert_se(setenv("VAR4", "new\nline", 1) == 0);
b6887d7a 974 assert_se(setenv("VAR5", "passwordwithbackslashes", 1) == 0);
fe65d692
ZJS
975 test(m, "exec-passenvironment.service", 0, CLD_EXITED);
976 test(m, "exec-passenvironment-repeated.service", 0, CLD_EXITED);
977 test(m, "exec-passenvironment-empty.service", 0, CLD_EXITED);
4c80d201
FB
978 assert_se(unsetenv("VAR1") == 0);
979 assert_se(unsetenv("VAR2") == 0);
980 assert_se(unsetenv("VAR3") == 0);
9b796f35 981 assert_se(unsetenv("VAR4") == 0);
b6887d7a 982 assert_se(unsetenv("VAR5") == 0);
fe65d692 983 test(m, "exec-passenvironment-absent.service", 0, CLD_EXITED);
4c80d201
FB
984}
985
27c5347c 986static void test_exec_umask(Manager *m) {
6ef721cb
LB
987 test(m, "exec-umask-default.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
988 test(m, "exec-umask-0177.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
27c5347c
RC
989}
990
cc3ddc85 991static void test_exec_runtimedirectory(Manager *m) {
4e032f65 992 (void) rm_rf("/run/test-exec_runtimedirectory2", REMOVE_ROOT|REMOVE_PHYSICAL);
fe65d692 993 test(m, "exec-runtimedirectory.service", 0, CLD_EXITED);
4e032f65
YW
994 (void) rm_rf("/run/test-exec_runtimedirectory2", REMOVE_ROOT|REMOVE_PHYSICAL);
995
fe65d692 996 test(m, "exec-runtimedirectory-mode.service", 0, CLD_EXITED);
4e032f65 997 test(m, "exec-runtimedirectory-owner.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
57c2efa0
YW
998
999 if (!check_nobody_user_and_group()) {
5cd33ccc 1000 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
69b07407
YW
1001 return;
1002 }
1003
1004 if (!STR_IN_SET(NOBODY_GROUP_NAME, "nobody", "nfsnobody", "nogroup")) {
5cd33ccc 1005 log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME, __func__);
57c2efa0
YW
1006 return;
1007 }
1008
4e032f65 1009 test(m, "exec-runtimedirectory-owner-" NOBODY_GROUP_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
ff4ca461
RC
1010}
1011
1012static void test_exec_capabilityboundingset(Manager *m) {
1013 int r;
1014
f7bc0c32 1015 r = find_executable("capsh", NULL);
ff4ca461 1016 if (r < 0) {
5cd33ccc 1017 log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
ff4ca461
RC
1018 return;
1019 }
1020
b7856f92
YW
1021 if (have_effective_cap(CAP_CHOWN) <= 0 ||
1022 have_effective_cap(CAP_FOWNER) <= 0 ||
1023 have_effective_cap(CAP_KILL) <= 0) {
1024 log_notice("Skipping %s, this process does not have enough capabilities", __func__);
1025 return;
1026 }
1027
fe65d692
ZJS
1028 test(m, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED);
1029 test(m, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED);
1030 test(m, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED);
1031 test(m, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED);
cc3ddc85
RC
1032}
1033
5008da1e 1034static void test_exec_basic(Manager *m) {
6ef721cb 1035 test(m, "exec-basic.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
5008da1e
ZJS
1036}
1037
b6dc25ee 1038static void test_exec_ambientcapabilities(Manager *m) {
70d7aea5
IP
1039 int r;
1040
1041 /* Check if the kernel has support for ambient capabilities. Run
1042 * the tests only if that's the case. Clearing all ambient
1043 * capabilities is fine, since we are expecting them to be unset
1044 * in the first place for the tests. */
1045 r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
f0e018e7 1046 if (r < 0 && IN_SET(errno, EINVAL, EOPNOTSUPP, ENOSYS)) {
5cd33ccc 1047 log_notice("Skipping %s, the kernel does not support ambient capabilities", __func__);
f0e018e7
YW
1048 return;
1049 }
1050
e5ba1d32 1051 if (have_effective_cap(CAP_CHOWN) <= 0 ||
b7856f92
YW
1052 have_effective_cap(CAP_NET_RAW) <= 0) {
1053 log_notice("Skipping %s, this process does not have enough capabilities", __func__);
1054 return;
1055 }
1056
fe65d692
ZJS
1057 test(m, "exec-ambientcapabilities.service", 0, CLD_EXITED);
1058 test(m, "exec-ambientcapabilities-merge.service", 0, CLD_EXITED);
69b07407 1059
57c2efa0 1060 if (!check_nobody_user_and_group()) {
5cd33ccc 1061 log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
69b07407
YW
1062 return;
1063 }
1064
1065 if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
5cd33ccc 1066 log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
57c2efa0
YW
1067 return;
1068 }
1069
fe65d692
ZJS
1070 test(m, "exec-ambientcapabilities-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
1071 test(m, "exec-ambientcapabilities-merge-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
70d7aea5
IP
1072}
1073
63447f11 1074static void test_exec_privatenetwork(Manager *m) {
6ef721cb 1075 int r;
63447f11 1076
f7bc0c32 1077 r = find_executable("ip", NULL);
63447f11 1078 if (r < 0) {
5cd33ccc 1079 log_notice_errno(r, "Skipping %s, could not find ip binary: %m", __func__);
63447f11
RC
1080 return;
1081 }
1082
6ef721cb
LB
1083 test(m, "exec-privatenetwork-yes-privatemounts-no.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NETWORK : EXIT_FAILURE, CLD_EXITED);
1084 test(m, "exec-privatenetwork-yes-privatemounts-yes.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NETWORK : EXIT_NAMESPACE, CLD_EXITED);
63447f11
RC
1085}
1086
600ed5c2
YW
1087static void test_exec_networknamespacepath(Manager *m) {
1088 int r;
1089
1090 r = find_executable("ip", NULL);
1091 if (r < 0) {
1092 log_notice_errno(r, "Skipping %s, could not find ip binary: %m", __func__);
1093 return;
1094 }
1095
1096 test(m, "exec-networknamespacepath-privatemounts-no.service", MANAGER_IS_SYSTEM(m) ? EXIT_SUCCESS : EXIT_FAILURE, CLD_EXITED);
6ef721cb 1097 test(m, "exec-networknamespacepath-privatemounts-yes.service", can_unshare ? EXIT_SUCCESS : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
600ed5c2
YW
1098}
1099
c388dfea 1100static void test_exec_oomscoreadjust(Manager *m) {
fe65d692 1101 test(m, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED);
642d1a6d
YW
1102
1103 if (detect_container() > 0) {
1104 log_notice("Testing in container, skipping remaining tests in %s", __func__);
1105 return;
1106 }
4e032f65 1107 test(m, "exec-oomscoreadjust-negative.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED);
c388dfea
RC
1108}
1109
a6226758 1110static void test_exec_ioschedulingclass(Manager *m) {
fe65d692
ZJS
1111 test(m, "exec-ioschedulingclass-none.service", 0, CLD_EXITED);
1112 test(m, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED);
1113 test(m, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED);
642d1a6d
YW
1114
1115 if (detect_container() > 0) {
1116 log_notice("Testing in container, skipping remaining tests in %s", __func__);
1117 return;
1118 }
4e032f65 1119 test(m, "exec-ioschedulingclass-realtime.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_IOPRIO, CLD_EXITED);
a6226758
RC
1120}
1121
f0e018e7 1122static void test_exec_unsetenvironment(Manager *m) {
fe65d692 1123 test(m, "exec-unsetenvironment.service", 0, CLD_EXITED);
42cc99d5
LP
1124}
1125
9672b583 1126static void test_exec_specifier(Manager *m) {
adeff822 1127 test(m, "exec-specifier.service", 0, CLD_EXITED);
4e032f65
YW
1128 if (MANAGER_IS_SYSTEM(m))
1129 test(m, "exec-specifier-system.service", 0, CLD_EXITED);
1130 else
1131 test(m, "exec-specifier-user.service", 0, CLD_EXITED);
adeff822 1132 test(m, "exec-specifier@foo-bar.service", 0, CLD_EXITED);
fe65d692 1133 test(m, "exec-specifier-interpolation.service", 0, CLD_EXITED);
9672b583
LP
1134}
1135
f0e018e7 1136static void test_exec_standardinput(Manager *m) {
fe65d692
ZJS
1137 test(m, "exec-standardinput-data.service", 0, CLD_EXITED);
1138 test(m, "exec-standardinput-file.service", 0, CLD_EXITED);
1139 test(m, "exec-standardinput-file-cat.service", 0, CLD_EXITED);
666d7877
LP
1140}
1141
566b7d23 1142static void test_exec_standardoutput(Manager *m) {
fe65d692 1143 test(m, "exec-standardoutput-file.service", 0, CLD_EXITED);
566b7d23
ZD
1144}
1145
1146static void test_exec_standardoutput_append(Manager *m) {
fe65d692 1147 test(m, "exec-standardoutput-append.service", 0, CLD_EXITED);
566b7d23
ZD
1148}
1149
8d7dab1f
LW
1150static void test_exec_standardoutput_truncate(Manager *m) {
1151 test(m, "exec-standardoutput-truncate.service", 0, CLD_EXITED);
1152}
1153
31cd5f63 1154static void test_exec_condition(Manager *m) {
fe65d692
ZJS
1155 test_service(m, "exec-condition-failed.service", SERVICE_FAILURE_EXIT_CODE);
1156 test_service(m, "exec-condition-skip.service", SERVICE_SKIP_CONDITION);
31cd5f63
AZ
1157}
1158
875afa02 1159static void test_exec_umask_namespace(Manager *m) {
bfd67106
YW
1160 /* exec-specifier-credentials-dir.service creates /run/credentials and enables implicit
1161 * InaccessiblePath= for the directory for all later services with mount namespace. */
1162 if (!is_inaccessible_available()) {
1163 log_notice("Testing without inaccessible, skipping %s", __func__);
1164 return;
1165 }
4e032f65 1166 test(m, "exec-umask-namespace.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NAMESPACE : EXIT_GROUP, CLD_EXITED);
875afa02
LP
1167}
1168
9efb9631
ZJS
1169typedef struct test_entry {
1170 test_function_t f;
1171 const char *name;
1172} test_entry;
1173
1174#define entry(x) {x, #x}
1175
4870133b 1176static void run_tests(RuntimeScope scope, char **patterns) {
4e032f65
YW
1177 _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
1178 _cleanup_free_ char *unit_paths = NULL;
c70cac54 1179 _cleanup_(manager_freep) Manager *m = NULL;
791a84ad 1180 usec_t start, finish;
19c0b0b9
RC
1181 int r;
1182
4e032f65 1183 static const test_entry tests[] = {
9efb9631
ZJS
1184 entry(test_exec_basic),
1185 entry(test_exec_ambientcapabilities),
1186 entry(test_exec_bindpaths),
1187 entry(test_exec_capabilityboundingset),
31cd5f63 1188 entry(test_exec_condition),
9efb9631 1189 entry(test_exec_cpuaffinity),
b7cca6cc 1190 entry(test_exec_credentials),
4e032f65 1191 entry(test_exec_dynamicuser),
9efb9631
ZJS
1192 entry(test_exec_environment),
1193 entry(test_exec_environmentfile),
4e032f65
YW
1194 entry(test_exec_execsearchpath),
1195 entry(test_exec_execsearchpath_environment),
1196 entry(test_exec_execsearchpath_environment_files),
1197 entry(test_exec_execsearchpath_passenvironment),
1198 entry(test_exec_execsearchpath_specifier),
9efb9631
ZJS
1199 entry(test_exec_group),
1200 entry(test_exec_ignoresigpipe),
1201 entry(test_exec_inaccessiblepaths),
1202 entry(test_exec_ioschedulingclass),
42867dfe 1203 entry(test_exec_mount_apivfs),
600ed5c2 1204 entry(test_exec_networknamespacepath),
ddc155b2 1205 entry(test_exec_noexecpaths),
9efb9631
ZJS
1206 entry(test_exec_oomscoreadjust),
1207 entry(test_exec_passenvironment),
1208 entry(test_exec_personality),
1209 entry(test_exec_privatedevices),
1210 entry(test_exec_privatenetwork),
1211 entry(test_exec_privatetmp),
1212 entry(test_exec_protecthome),
1213 entry(test_exec_protectkernelmodules),
1214 entry(test_exec_readonlypaths),
1215 entry(test_exec_readwritepaths),
1216 entry(test_exec_restrictnamespaces),
1217 entry(test_exec_runtimedirectory),
4e032f65 1218 entry(test_exec_specifier),
9efb9631
ZJS
1219 entry(test_exec_standardinput),
1220 entry(test_exec_standardoutput),
1221 entry(test_exec_standardoutput_append),
8d7dab1f 1222 entry(test_exec_standardoutput_truncate),
9efb9631
ZJS
1223 entry(test_exec_supplementarygroups),
1224 entry(test_exec_systemcallerrornumber),
1225 entry(test_exec_systemcallfilter),
4e032f65 1226 entry(test_exec_systemcallfilter_system),
9efb9631
ZJS
1227 entry(test_exec_temporaryfilesystem),
1228 entry(test_exec_umask),
4e032f65 1229 entry(test_exec_umask_namespace),
9efb9631
ZJS
1230 entry(test_exec_unsetenvironment),
1231 entry(test_exec_user),
1232 entry(test_exec_workingdirectory),
9efb9631 1233 {},
19c0b0b9 1234 };
176ceb2c 1235
44ee03d1
ZJS
1236 assert_se(unsetenv("USER") == 0);
1237 assert_se(unsetenv("LOGNAME") == 0);
1238 assert_se(unsetenv("SHELL") == 0);
1239 assert_se(unsetenv("HOME") == 0);
1240 assert_se(unsetenv("TMPDIR") == 0);
2482f88d 1241
4e032f65
YW
1242 /* Unset VARx, especially, VAR1, VAR2 and VAR3, which are used in the PassEnvironment test cases,
1243 * otherwise (and if they are present in the environment), `manager_default_environment` will copy
1244 * them into the default environment which is passed to each created job, which will make the tests
1245 * that expect those not to be present to fail. */
1246 assert_se(unsetenv("VAR1") == 0);
1247 assert_se(unsetenv("VAR2") == 0);
1248 assert_se(unsetenv("VAR3") == 0);
1249 assert_se(unsetenv("VAR4") == 0);
1250 assert_se(unsetenv("VAR5") == 0);
34b59770 1251
ac1f08b9 1252 assert_se(runtime_dir = setup_fake_runtime_dir());
42867dfe 1253 assert_se(user_runtime_unit_dir = path_join(runtime_dir, "systemd/user"));
4e032f65 1254 assert_se(unit_paths = strjoin(PRIVATE_UNIT_DIR, ":", user_runtime_unit_dir));
42867dfe 1255 assert_se(set_unit_path(unit_paths) >= 0);
281e05b6 1256
4e032f65
YW
1257 r = manager_new(scope, MANAGER_TEST_RUN_BASIC, &m);
1258 if (manager_errno_skip_test(r))
1259 return (void) log_tests_skipped_errno(r, "manager_new");
1260 assert_se(r >= 0);
1261
c9e120e0 1262 m->defaults.std_output = EXEC_OUTPUT_NULL; /* don't rely on host journald */
4e032f65
YW
1263 assert_se(manager_startup(m, NULL, NULL, NULL) >= 0);
1264
1265 /* Uncomment below if you want to make debugging logs stored to journal. */
1266 //manager_override_log_target(m, LOG_TARGET_AUTO);
1267 //manager_override_log_level(m, LOG_DEBUG);
1268
791a84ad
LB
1269 /* Measure and print the time that it takes to run tests, excluding startup of the manager object,
1270 * to try and measure latency of spawning services */
b6349ffd 1271 n_ran_tests = 0;
791a84ad
LB
1272 start = now(CLOCK_MONOTONIC);
1273
4e032f65
YW
1274 for (const test_entry *test = tests; test->f; test++)
1275 if (strv_fnmatch_or_empty(patterns, test->name, FNM_NOESCAPE))
1276 test->f(m);
1277 else
1278 log_info("Skipping %s because it does not match any pattern.", test->name);
791a84ad
LB
1279
1280 finish = now(CLOCK_MONOTONIC);
1281
b6349ffd
LB
1282 log_info("ran %u tests with %s manager + unshare=%s in: %s",
1283 n_ran_tests,
791a84ad
LB
1284 scope == RUNTIME_SCOPE_SYSTEM ? "system" : "user",
1285 yes_no(can_unshare),
1286 FORMAT_TIMESPAN(finish - start, USEC_PER_MSEC));
4e032f65
YW
1287}
1288
1289static int prepare_ns(const char *process_name) {
1290 int r;
1291
1292 r = safe_fork(process_name,
1293 FORK_RESET_SIGNALS |
1294 FORK_CLOSE_ALL_FDS |
1295 FORK_DEATHSIG |
1296 FORK_WAIT |
1297 FORK_REOPEN_LOG |
1298 FORK_LOG |
1299 FORK_NEW_MOUNTNS |
1300 FORK_MOUNTNS_SLAVE,
1301 NULL);
1302 assert_se(r >= 0);
1303 if (r == 0) {
1304 _cleanup_free_ char *unit_dir = NULL;
1305
1306 /* Make "/" read-only. */
e15ad182 1307 assert_se(mount_nofollow_verbose(LOG_DEBUG, NULL, "/", NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL) >= 0);
4e032f65
YW
1308
1309 /* Creating a new user namespace in the above means all MS_SHARED mounts become MS_SLAVE.
1310 * Let's put them back to MS_SHARED here, since that's what we want as defaults. (This will
1311 * not reconnect propagation, but simply create new peer groups for all our mounts). */
1312 assert_se(mount_follow_verbose(LOG_DEBUG, NULL, "/", NULL, MS_SHARED|MS_REC, NULL) >= 0);
1313
1314 assert_se(mkdir_p(PRIVATE_UNIT_DIR, 0755) >= 0);
c109cff9 1315 assert_se(mount_nofollow_verbose(LOG_DEBUG, "tmpfs", PRIVATE_UNIT_DIR, "tmpfs", MS_NOSUID|MS_NODEV, NULL) >= 0);
e1abca2e 1316
4e032f65
YW
1317 /* Copy unit files to make them accessible even when unprivileged. */
1318 assert_se(get_testdata_dir("test-execute/", &unit_dir) >= 0);
f9f70e06 1319 assert_se(copy_directory_at(AT_FDCWD, unit_dir, AT_FDCWD, PRIVATE_UNIT_DIR, COPY_MERGE_EMPTY) >= 0);
b7172f34 1320
c109cff9
FS
1321 /* Mount tmpfs on the following directories to make not StateDirectory= or friends disturb the host. */
1322 FOREACH_STRING(p, "/dev/shm", "/root", "/tmp", "/var/tmp", "/var/lib")
1323 assert_se(mount_nofollow_verbose(LOG_DEBUG, "tmpfs", p, "tmpfs", MS_NOSUID|MS_NODEV, NULL) >= 0);
1324
4e032f65
YW
1325 /* Prepare credstore like tmpfiles.d/credstore.conf for LoadCredential= tests. */
1326 FOREACH_STRING(p, "/run/credstore", "/run/credstore.encrypted") {
1327 assert_se(mkdir_p(p, 0) >= 0);
1328 assert_se(mount_nofollow_verbose(LOG_DEBUG, "tmpfs", p, "tmpfs", MS_NOSUID|MS_NODEV, "mode=0000") >= 0);
1329 }
b7cca6cc
YW
1330
1331 assert_se(write_string_file("/run/credstore/test-execute.load-credential", "foo", WRITE_STRING_FILE_CREATE) >= 0);
4e032f65
YW
1332 }
1333
1334 return r;
1335}
1336
1337TEST(run_tests_root) {
1338 _cleanup_strv_free_ char **filters = NULL;
1339
1340 if (!have_namespaces())
1341 return (void) log_tests_skipped("unshare() is disabled");
1342
1343 /* safe_fork() clears saved_argv in the child process. Let's copy it. */
1344 assert_se(filters = strv_copy(strv_skip(saved_argv, 1)));
1345
1346 if (prepare_ns("(test-execute-root)") == 0) {
1347 can_unshare = true;
4870133b 1348 run_tests(RUNTIME_SCOPE_SYSTEM, filters);
4e032f65
YW
1349 _exit(EXIT_SUCCESS);
1350 }
1351}
1352
1353TEST(run_tests_without_unshare) {
1354 if (!have_namespaces()) {
1355 /* unshare() is already filtered. */
1356 can_unshare = false;
4870133b 1357 run_tests(RUNTIME_SCOPE_SYSTEM, strv_skip(saved_argv, 1));
4e032f65
YW
1358 return;
1359 }
b7172f34
YW
1360
1361#if HAVE_SECCOMP
4e032f65
YW
1362 _cleanup_strv_free_ char **filters = NULL;
1363 int r;
1364
b7172f34 1365 /* The following tests are for 1beab8b0d0ff2d7d1436b52d4a0c3d56dc908962. */
4e032f65
YW
1366 if (!is_seccomp_available())
1367 return (void) log_tests_skipped("Seccomp not available, cannot run unshare() filtered tests");
1368
1369 /* safe_fork() clears saved_argv in the child process. Let's copy it. */
1370 assert_se(filters = strv_copy(strv_skip(saved_argv, 1)));
b7172f34 1371
4e032f65
YW
1372 if (prepare_ns("(test-execute-without-unshare)") == 0) {
1373 _cleanup_hashmap_free_ Hashmap *s = NULL;
b7172f34 1374
4e032f65
YW
1375 r = seccomp_syscall_resolve_name("unshare");
1376 assert_se(r != __NR_SCMP_ERROR);
1377 assert_se(hashmap_ensure_put(&s, NULL, UINT32_TO_PTR(r + 1), INT_TO_PTR(-1)) >= 0);
1378 assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EOPNOTSUPP), true) >= 0);
b7172f34 1379
4e032f65
YW
1380 /* Check unshare() is actually filtered. */
1381 assert_se(unshare(CLONE_NEWNS) < 0);
1382 assert_se(errno == EOPNOTSUPP);
281e05b6 1383
4e032f65 1384 can_unshare = false;
4870133b 1385 run_tests(RUNTIME_SCOPE_SYSTEM, filters);
4e032f65
YW
1386 _exit(EXIT_SUCCESS);
1387 }
b7172f34 1388#else
4e032f65
YW
1389 log_tests_skipped("Built without seccomp support, cannot run unshare() filtered tests");
1390#endif
1391}
1392
1393TEST(run_tests_unprivileged) {
1394 _cleanup_strv_free_ char **filters = NULL;
1395
1396 if (!have_namespaces())
1397 return (void) log_tests_skipped("unshare() is disabled");
1398
1399 /* safe_fork() clears saved_argv in the child process. Let's copy it. */
1400 assert_se(filters = strv_copy(strv_skip(saved_argv, 1)));
1401
1402 if (prepare_ns("(test-execute-unprivileged)") == 0) {
1403 assert_se(capability_bounding_set_drop(0, /* right_now = */ true) >= 0);
1404
1405 can_unshare = false;
4870133b 1406 run_tests(RUNTIME_SCOPE_USER, filters);
4e032f65
YW
1407 _exit(EXIT_SUCCESS);
1408 }
1409}
1410
1411static int intro(void) {
1412#if HAS_FEATURE_ADDRESS_SANITIZER
1413 if (strstr_ptr(ci_environment(), "travis") || strstr_ptr(ci_environment(), "github-actions"))
1414 return log_tests_skipped("Running on Travis CI/GH Actions under ASan, see https://github.com/systemd/systemd/issues/10696");
b7172f34 1415#endif
4e032f65
YW
1416 /* It is needed otherwise cgroup creation fails */
1417 if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0)
1418 return log_tests_skipped("not privileged");
1419
1420 if (enter_cgroup_subroot(NULL) == -ENOMEDIUM)
1421 return log_tests_skipped("cgroupfs not available");
1422
1423 if (path_is_read_only_fs("/sys") > 0)
1424 return log_tests_skipped("/sys is mounted read-only");
1425
1426 /* Create dummy network interface for testing PrivateNetwork=yes */
1427 (void) system("ip link add dummy-test-exec type dummy");
1428
600ed5c2
YW
1429 /* Create a network namespace and a dummy interface in it for NetworkNamespacePath= */
1430 (void) system("ip netns add test-execute-netns");
1431 (void) system("ip netns exec test-execute-netns ip link add dummy-test-ns type dummy");
1432
4e032f65 1433 return EXIT_SUCCESS;
281e05b6 1434}
4e032f65
YW
1435
1436static int outro(void) {
1437 (void) system("ip link del dummy-test-exec");
600ed5c2 1438 (void) system("ip netns del test-execute-netns");
4e032f65
YW
1439 (void) rmdir(PRIVATE_UNIT_DIR);
1440
1441 return EXIT_SUCCESS;
1442}
1443
1444DEFINE_TEST_MAIN_FULL(LOG_DEBUG, intro, outro);