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