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