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