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