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