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