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