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