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