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