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