]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
281e05b6 | 2 | |
ff4ca461 | 3 | #include <stdio.h> |
4e032f65 | 4 | #include <sys/mount.h> |
70d7aea5 | 5 | #include <sys/prctl.h> |
ff4ca461 | 6 | #include <sys/types.h> |
281e05b6 | 7 | |
42867dfe YW |
8 | #include "sd-event.h" |
9 | ||
b7856f92 | 10 | #include "capability-util.h" |
4e79aeaa | 11 | #include "cpu-set-util.h" |
8c35c10d | 12 | #include "copy.h" |
42867dfe | 13 | #include "dropin.h" |
b4891260 | 14 | #include "errno-list.h" |
42867dfe | 15 | #include "fd-util.h" |
03bd70dd | 16 | #include "fileio.h" |
f4f15635 | 17 | #include "fs-util.h" |
281e05b6 | 18 | #include "macro.h" |
f4f15635 | 19 | #include "manager.h" |
a22692d7 | 20 | #include "missing_prctl.h" |
281e05b6 | 21 | #include "mkdir.h" |
4e032f65 | 22 | #include "mount-util.h" |
ff4ca461 | 23 | #include "path-util.h" |
42867dfe | 24 | #include "process-util.h" |
c6878637 | 25 | #include "rm-rf.h" |
83f12b27 | 26 | #include "seccomp-util.h" |
57b7a260 | 27 | #include "service.h" |
42867dfe YW |
28 | #include "signal-util.h" |
29 | #include "static-destruct.h" | |
34b86909 | 30 | #include "stat-util.h" |
cc100a5a | 31 | #include "tests.h" |
8c35c10d | 32 | #include "tmpfile-util.h" |
f4f15635 | 33 | #include "unit.h" |
57c2efa0 | 34 | #include "user-util.h" |
4dd4cb8f | 35 | #include "virt.h" |
281e05b6 | 36 | |
4e032f65 YW |
37 | #define PRIVATE_UNIT_DIR "/run/test-execute-unit-dir" |
38 | ||
42867dfe | 39 | static char *user_runtime_unit_dir = NULL; |
5f00dc4d | 40 | static bool can_unshare; |
8a87a16b | 41 | static bool have_net_dummy; |
76808638 | 42 | static bool have_netns; |
b6349ffd | 43 | static unsigned n_ran_tests = 0; |
b7172f34 | 44 | |
42867dfe YW |
45 | STATIC_DESTRUCTOR_REGISTER(user_runtime_unit_dir, freep); |
46 | ||
281e05b6 RC |
47 | typedef void (*test_function_t)(Manager *m); |
48 | ||
c3ab2c38 LP |
49 | static int cld_dumped_to_killed(int code) { |
50 | /* Depending on the system, seccomp version, … some signals might result in dumping, others in plain | |
51 | * killing. Let's ignore the difference here, and map both cases to CLD_KILLED */ | |
52 | return code == CLD_DUMPED ? CLD_KILLED : code; | |
53 | } | |
54 | ||
31cd5f63 | 55 | static void wait_for_service_finish(Manager *m, Unit *unit) { |
281e05b6 RC |
56 | Service *service = NULL; |
57 | usec_t ts; | |
8adb3d63 | 58 | usec_t timeout = 2 * USEC_PER_MINUTE; |
281e05b6 RC |
59 | |
60 | assert_se(m); | |
61 | assert_se(unit); | |
62 | ||
63 | service = SERVICE(unit); | |
64 | printf("%s\n", unit->id); | |
65 | exec_context_dump(&service->exec_context, stdout, "\t"); | |
66 | ts = now(CLOCK_MONOTONIC); | |
ec2ce0c5 | 67 | while (!IN_SET(service->state, SERVICE_DEAD, SERVICE_FAILED)) { |
281e05b6 RC |
68 | int r; |
69 | usec_t n; | |
70 | ||
71 | r = sd_event_run(m->event, 100 * USEC_PER_MSEC); | |
72 | assert_se(r >= 0); | |
73 | ||
74 | n = now(CLOCK_MONOTONIC); | |
75 | if (ts + timeout < n) { | |
76 | log_error("Test timeout when testing %s", unit->id); | |
a721cd00 | 77 | r = unit_kill(unit, KILL_ALL, SIGKILL, SI_USER, 0, NULL); |
f7f8e8cb EV |
78 | if (r < 0) |
79 | log_error_errno(r, "Failed to kill %s: %m", unit->id); | |
281e05b6 RC |
80 | exit(EXIT_FAILURE); |
81 | } | |
82 | } | |
31cd5f63 AZ |
83 | } |
84 | ||
fe65d692 ZJS |
85 | static void check_main_result(const char *file, unsigned line, const char *func, |
86 | Manager *m, Unit *unit, int status_expected, int code_expected) { | |
31cd5f63 AZ |
87 | Service *service = NULL; |
88 | ||
89 | assert_se(m); | |
90 | assert_se(unit); | |
91 | ||
92 | wait_for_service_finish(m, unit); | |
93 | ||
94 | service = SERVICE(unit); | |
281e05b6 | 95 | exec_status_dump(&service->main_exec_status, stdout, "\t"); |
18f8c5d4 | 96 | |
c3ab2c38 | 97 | if (cld_dumped_to_killed(service->main_exec_status.code) != cld_dumped_to_killed(code_expected)) { |
aa272751 YW |
98 | log_error("%s:%u:%s %s: can_unshare=%s: exit code %d, expected %d", |
99 | file, line, func, unit->id, yes_no(can_unshare), | |
6aed6a11 ZJS |
100 | service->main_exec_status.code, code_expected); |
101 | abort(); | |
18f8c5d4 LP |
102 | } |
103 | ||
104 | if (service->main_exec_status.status != status_expected) { | |
aa272751 YW |
105 | log_error("%s:%u:%s: %s: can_unshare=%s: exit status %d, expected %d", |
106 | file, line, func, unit->id, yes_no(can_unshare), | |
18f8c5d4 LP |
107 | service->main_exec_status.status, status_expected); |
108 | abort(); | |
6aed6a11 | 109 | } |
281e05b6 RC |
110 | } |
111 | ||
fe65d692 ZJS |
112 | static void check_service_result(const char *file, unsigned line, const char *func, |
113 | Manager *m, Unit *unit, ServiceResult result_expected) { | |
31cd5f63 AZ |
114 | Service *service = NULL; |
115 | ||
116 | assert_se(m); | |
117 | assert_se(unit); | |
118 | ||
119 | wait_for_service_finish(m, unit); | |
120 | ||
121 | service = SERVICE(unit); | |
122 | ||
123 | if (service->result != result_expected) { | |
aa272751 YW |
124 | log_error("%s:%u:%s: %s: can_unshare=%s: service end result %s, expected %s", |
125 | file, line, func, unit->id, yes_no(can_unshare), | |
31cd5f63 AZ |
126 | service_result_to_string(service->result), |
127 | service_result_to_string(result_expected)); | |
128 | abort(); | |
129 | } | |
130 | } | |
131 | ||
57c2efa0 YW |
132 | static bool check_nobody_user_and_group(void) { |
133 | static int cache = -1; | |
134 | struct passwd *p; | |
135 | struct group *g; | |
136 | ||
137 | if (cache >= 0) | |
138 | return !!cache; | |
139 | ||
140 | if (!synthesize_nobody()) | |
141 | goto invalid; | |
142 | ||
143 | p = getpwnam(NOBODY_USER_NAME); | |
144 | if (!p || | |
145 | !streq(p->pw_name, NOBODY_USER_NAME) || | |
146 | p->pw_uid != UID_NOBODY || | |
147 | p->pw_gid != GID_NOBODY) | |
148 | goto invalid; | |
149 | ||
150 | p = getpwuid(UID_NOBODY); | |
151 | if (!p || | |
152 | !streq(p->pw_name, NOBODY_USER_NAME) || | |
153 | p->pw_uid != UID_NOBODY || | |
154 | p->pw_gid != GID_NOBODY) | |
155 | goto invalid; | |
156 | ||
157 | g = getgrnam(NOBODY_GROUP_NAME); | |
158 | if (!g || | |
159 | !streq(g->gr_name, NOBODY_GROUP_NAME) || | |
160 | g->gr_gid != GID_NOBODY) | |
161 | goto invalid; | |
162 | ||
163 | g = getgrgid(GID_NOBODY); | |
164 | if (!g || | |
165 | !streq(g->gr_name, NOBODY_GROUP_NAME) || | |
166 | g->gr_gid != GID_NOBODY) | |
167 | goto invalid; | |
168 | ||
169 | cache = 1; | |
170 | return true; | |
171 | ||
172 | invalid: | |
173 | cache = 0; | |
174 | return false; | |
175 | } | |
176 | ||
9f82d685 YW |
177 | static bool check_user_has_group_with_same_name(const char *name) { |
178 | struct passwd *p; | |
179 | struct group *g; | |
180 | ||
f21b863e | 181 | assert_se(name); |
9f82d685 YW |
182 | |
183 | p = getpwnam(name); | |
184 | if (!p || | |
185 | !streq(p->pw_name, name)) | |
186 | return false; | |
187 | ||
188 | g = getgrgid(p->pw_gid); | |
189 | if (!g || | |
190 | !streq(g->gr_name, name)) | |
191 | return false; | |
192 | ||
193 | return true; | |
194 | } | |
195 | ||
6086d2da | 196 | static bool is_inaccessible_available(void) { |
6086d2da | 197 | FOREACH_STRING(p, |
5980d463 ZJS |
198 | "/run/systemd/inaccessible/reg", |
199 | "/run/systemd/inaccessible/dir", | |
200 | "/run/systemd/inaccessible/chr", | |
201 | "/run/systemd/inaccessible/blk", | |
202 | "/run/systemd/inaccessible/fifo", | |
203 | "/run/systemd/inaccessible/sock") | |
6086d2da DP |
204 | if (access(p, F_OK) < 0) |
205 | return false; | |
6086d2da DP |
206 | |
207 | return true; | |
208 | } | |
209 | ||
fc6172b1 RP |
210 | static void start_parent_slices(Unit *unit) { |
211 | Unit *slice; | |
212 | ||
213 | slice = UNIT_GET_SLICE(unit); | |
214 | if (slice) { | |
215 | start_parent_slices(slice); | |
216 | int r = unit_start(slice, NULL); | |
217 | assert_se(r >= 0 || r == -EALREADY); | |
218 | } | |
219 | } | |
220 | ||
d0c6136f NR |
221 | static bool have_userns_privileges(void) { |
222 | pid_t pid; | |
223 | int r; | |
224 | ||
225 | r = safe_fork("(sd-test-check-userns)", | |
226 | FORK_RESET_SIGNALS | | |
227 | FORK_CLOSE_ALL_FDS | | |
228 | FORK_DEATHSIG_SIGKILL, | |
229 | &pid); | |
230 | assert(r >= 0); | |
231 | if (r == 0) { | |
232 | /* Keep CAP_SYS_ADMIN if we have it to ensure we give an | |
233 | * accurate result to the caller. Some kernels have a | |
234 | * kernel.unprivileged_userns_clone sysctl which can be | |
235 | * configured to make CLONE_NEWUSER require CAP_SYS_ADMIN. | |
236 | * Additionally, AppArmor may restrict unprivileged user | |
237 | * namespace creation. */ | |
238 | r = capability_bounding_set_drop(UINT64_C(1) << CAP_SYS_ADMIN, /* right_now = */ true); | |
239 | if (r < 0) { | |
240 | log_debug_errno(r, "Failed to drop capabilities: %m"); | |
241 | _exit(2); | |
242 | } | |
243 | ||
244 | r = RET_NERRNO(unshare(CLONE_NEWUSER)); | |
245 | if (r < 0 && !ERRNO_IS_NEG_PRIVILEGE(r)) | |
246 | log_debug_errno(r, "Failed to create user namespace: %m"); | |
247 | ||
248 | _exit(r >= 0 ? EXIT_SUCCESS : ERRNO_IS_NEG_PRIVILEGE(r) ? EXIT_FAILURE : 2); | |
249 | } | |
250 | ||
251 | /* The exit code records the result of the check: | |
252 | * EXIT_SUCCESS => we can use user namespaces | |
253 | * EXIT_FAILURE => we can NOT use user namespaces | |
254 | * 2 => some other error occurred */ | |
255 | r = wait_for_terminate_and_check("(sd-test-check-userns)", pid, 0); | |
256 | if (!IN_SET(r, EXIT_SUCCESS, EXIT_FAILURE)) | |
257 | log_debug("Failed to check if user namespaces can be used, assuming not."); | |
258 | ||
259 | return r == EXIT_SUCCESS; | |
260 | } | |
261 | ||
fe65d692 ZJS |
262 | static void _test(const char *file, unsigned line, const char *func, |
263 | Manager *m, const char *unit_name, int status_expected, int code_expected) { | |
281e05b6 RC |
264 | Unit *unit; |
265 | ||
266 | assert_se(unit_name); | |
267 | ||
ba412430 | 268 | assert_se(manager_load_startable_unit_or_warn(m, unit_name, NULL, &unit) >= 0); |
fc6172b1 RP |
269 | /* We need to start the slices as well otherwise the slice cgroups might be pruned |
270 | * in on_cgroup_empty_event. */ | |
271 | start_parent_slices(unit); | |
48b92b37 | 272 | assert_se(unit_start(unit, NULL) >= 0); |
fe65d692 | 273 | check_main_result(file, line, func, m, unit, status_expected, code_expected); |
b6349ffd LB |
274 | |
275 | ++n_ran_tests; | |
31cd5f63 | 276 | } |
fe65d692 ZJS |
277 | #define test(m, unit_name, status_expected, code_expected) \ |
278 | _test(PROJECT_FILE, __LINE__, __func__, m, unit_name, status_expected, code_expected) | |
31cd5f63 | 279 | |
fe65d692 ZJS |
280 | static void _test_service(const char *file, unsigned line, const char *func, |
281 | Manager *m, const char *unit_name, ServiceResult result_expected) { | |
31cd5f63 AZ |
282 | Unit *unit; |
283 | ||
284 | assert_se(unit_name); | |
285 | ||
286 | assert_se(manager_load_startable_unit_or_warn(m, unit_name, NULL, &unit) >= 0); | |
48b92b37 | 287 | assert_se(unit_start(unit, NULL) >= 0); |
fe65d692 | 288 | check_service_result(file, line, func, m, unit, result_expected); |
281e05b6 | 289 | } |
fe65d692 ZJS |
290 | #define test_service(m, unit_name, result_expected) \ |
291 | _test_service(PROJECT_FILE, __LINE__, __func__, m, unit_name, result_expected) | |
281e05b6 | 292 | |
f0e018e7 YW |
293 | static void test_exec_bindpaths(Manager *m) { |
294 | assert_se(mkdir_p("/tmp/test-exec-bindpaths", 0755) >= 0); | |
295 | assert_se(mkdir_p("/tmp/test-exec-bindreadonlypaths", 0755) >= 0); | |
d053b72b | 296 | |
fe65d692 | 297 | test(m, "exec-bindpaths.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED); |
d053b72b | 298 | |
f0e018e7 YW |
299 | (void) rm_rf("/tmp/test-exec-bindpaths", REMOVE_ROOT|REMOVE_PHYSICAL); |
300 | (void) rm_rf("/tmp/test-exec-bindreadonlypaths", REMOVE_ROOT|REMOVE_PHYSICAL); | |
d053b72b YW |
301 | } |
302 | ||
4e79aeaa | 303 | static void test_exec_cpuaffinity(Manager *m) { |
167a776d | 304 | _cleanup_(cpu_set_reset) CPUSet c = {}; |
4e79aeaa | 305 | |
167a776d ZJS |
306 | assert_se(cpu_set_realloc(&c, 8192) >= 0); /* just allocate the maximum possible size */ |
307 | assert_se(sched_getaffinity(0, c.allocated, c.set) >= 0); | |
4e79aeaa | 308 | |
167a776d | 309 | if (!CPU_ISSET_S(0, c.allocated, c.set)) { |
4e79aeaa YW |
310 | log_notice("Cannot use CPU 0, skipping %s", __func__); |
311 | return; | |
312 | } | |
313 | ||
fe65d692 ZJS |
314 | test(m, "exec-cpuaffinity1.service", 0, CLD_EXITED); |
315 | test(m, "exec-cpuaffinity2.service", 0, CLD_EXITED); | |
4e79aeaa | 316 | |
167a776d ZJS |
317 | if (!CPU_ISSET_S(1, c.allocated, c.set) || |
318 | !CPU_ISSET_S(2, c.allocated, c.set)) { | |
4e79aeaa YW |
319 | log_notice("Cannot use CPU 1 or 2, skipping remaining tests in %s", __func__); |
320 | return; | |
321 | } | |
322 | ||
fe65d692 | 323 | test(m, "exec-cpuaffinity3.service", 0, CLD_EXITED); |
4e79aeaa YW |
324 | } |
325 | ||
b7cca6cc YW |
326 | static void test_exec_credentials(Manager *m) { |
327 | test(m, "exec-set-credential.service", 0, CLD_EXITED); | |
328 | test(m, "exec-load-credential.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_CREDENTIALS, CLD_EXITED); | |
329 | test(m, "exec-credentials-dir-specifier.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_CREDENTIALS, CLD_EXITED); | |
330 | } | |
331 | ||
281e05b6 RC |
332 | static void test_exec_workingdirectory(Manager *m) { |
333 | assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0); | |
334 | ||
fe65d692 ZJS |
335 | test(m, "exec-workingdirectory.service", 0, CLD_EXITED); |
336 | test(m, "exec-workingdirectory-trailing-dot.service", 0, CLD_EXITED); | |
281e05b6 | 337 | |
c6878637 | 338 | (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT|REMOVE_PHYSICAL); |
281e05b6 RC |
339 | } |
340 | ||
8c35c10d | 341 | static void test_exec_execsearchpath(Manager *m) { |
342 | assert_se(mkdir_p("/tmp/test-exec_execsearchpath", 0755) >= 0); | |
343 | ||
7c2f5495 | 344 | assert_se(copy_file("/bin/ls", "/tmp/test-exec_execsearchpath/ls_temp", 0, 0777, COPY_REPLACE) >= 0); |
8c35c10d | 345 | |
346 | test(m, "exec-execsearchpath.service", 0, CLD_EXITED); | |
347 | ||
348 | assert_se(rm_rf("/tmp/test-exec_execsearchpath", REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); | |
349 | ||
350 | test(m, "exec-execsearchpath.service", EXIT_EXEC, CLD_EXITED); | |
351 | } | |
352 | ||
353 | static void test_exec_execsearchpath_specifier(Manager *m) { | |
354 | test(m, "exec-execsearchpath-unit-specifier.service", 0, CLD_EXITED); | |
355 | } | |
356 | ||
357 | static void test_exec_execsearchpath_environment(Manager *m) { | |
358 | test(m, "exec-execsearchpath-environment.service", 0, CLD_EXITED); | |
359 | test(m, "exec-execsearchpath-environment-path-set.service", 0, CLD_EXITED); | |
360 | } | |
361 | ||
362 | static void test_exec_execsearchpath_environment_files(Manager *m) { | |
363 | static const char path_not_set[] = | |
364 | "VAR1='word1 word2'\n" | |
365 | "VAR2=word3 \n" | |
366 | "# comment1\n" | |
367 | "\n" | |
368 | "; comment2\n" | |
369 | " ; # comment3\n" | |
370 | "line without an equal\n" | |
371 | "VAR3='$word 5 6'\n" | |
372 | "VAR4='new\nline'\n" | |
373 | "VAR5=password\\with\\backslashes"; | |
374 | ||
375 | static const char path_set[] = | |
376 | "VAR1='word1 word2'\n" | |
377 | "VAR2=word3 \n" | |
378 | "# comment1\n" | |
379 | "\n" | |
380 | "; comment2\n" | |
381 | " ; # comment3\n" | |
382 | "line without an equal\n" | |
383 | "VAR3='$word 5 6'\n" | |
384 | "VAR4='new\nline'\n" | |
385 | "VAR5=password\\with\\backslashes\n" | |
386 | "PATH=/usr"; | |
387 | ||
388 | int r; | |
389 | ||
390 | r = write_string_file("/tmp/test-exec_execsearchpath_environmentfile.conf", path_not_set, WRITE_STRING_FILE_CREATE); | |
391 | ||
392 | assert_se(r == 0); | |
393 | ||
394 | test(m, "exec-execsearchpath-environmentfile.service", 0, CLD_EXITED); | |
395 | ||
396 | (void) unlink("/tmp/test-exec_environmentfile.conf"); | |
397 | ||
398 | ||
399 | r = write_string_file("/tmp/test-exec_execsearchpath_environmentfile-set.conf", path_set, WRITE_STRING_FILE_CREATE); | |
400 | ||
401 | assert_se(r == 0); | |
402 | ||
403 | test(m, "exec-execsearchpath-environmentfile-set.service", 0, CLD_EXITED); | |
404 | ||
405 | (void) unlink("/tmp/test-exec_environmentfile-set.conf"); | |
406 | } | |
407 | ||
408 | static void test_exec_execsearchpath_passenvironment(Manager *m) { | |
409 | assert_se(setenv("VAR1", "word1 word2", 1) == 0); | |
410 | assert_se(setenv("VAR2", "word3", 1) == 0); | |
411 | assert_se(setenv("VAR3", "$word 5 6", 1) == 0); | |
412 | assert_se(setenv("VAR4", "new\nline", 1) == 0); | |
413 | assert_se(setenv("VAR5", "passwordwithbackslashes", 1) == 0); | |
414 | ||
415 | test(m, "exec-execsearchpath-passenvironment.service", 0, CLD_EXITED); | |
416 | ||
417 | assert_se(setenv("PATH", "/usr", 1) == 0); | |
418 | test(m, "exec-execsearchpath-passenvironment-set.service", 0, CLD_EXITED); | |
419 | ||
420 | assert_se(unsetenv("VAR1") == 0); | |
421 | assert_se(unsetenv("VAR2") == 0); | |
422 | assert_se(unsetenv("VAR3") == 0); | |
423 | assert_se(unsetenv("VAR4") == 0); | |
424 | assert_se(unsetenv("VAR5") == 0); | |
425 | assert_se(unsetenv("PATH") == 0); | |
426 | } | |
427 | ||
281e05b6 | 428 | static void test_exec_personality(Manager *m) { |
281e05b6 | 429 | #if defined(__x86_64__) |
fe65d692 | 430 | test(m, "exec-personality-x86-64.service", 0, CLD_EXITED); |
7517f51e HB |
431 | |
432 | #elif defined(__s390__) | |
fe65d692 | 433 | test(m, "exec-personality-s390.service", 0, CLD_EXITED); |
7517f51e | 434 | |
12591863 JS |
435 | #elif defined(__powerpc64__) |
436 | # if __BYTE_ORDER == __BIG_ENDIAN | |
fe65d692 | 437 | test(m, "exec-personality-ppc64.service", 0, CLD_EXITED); |
12591863 | 438 | # else |
fe65d692 | 439 | test(m, "exec-personality-ppc64le.service", 0, CLD_EXITED); |
12591863 JS |
440 | # endif |
441 | ||
442 | #elif defined(__aarch64__) | |
fe65d692 | 443 | test(m, "exec-personality-aarch64.service", 0, CLD_EXITED); |
12591863 | 444 | |
5798eb4c | 445 | #elif defined(__i386__) |
fe65d692 | 446 | test(m, "exec-personality-x86.service", 0, CLD_EXITED); |
f106a639 | 447 | #elif defined(__loongarch_lp64) |
646b0112 | 448 | test(m, "exec-personality-loongarch64.service", 0, CLD_EXITED); |
f0e018e7 YW |
449 | #else |
450 | log_notice("Unknown personality, skipping %s", __func__); | |
281e05b6 RC |
451 | #endif |
452 | } | |
453 | ||
454 | static void test_exec_ignoresigpipe(Manager *m) { | |
fe65d692 ZJS |
455 | test(m, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED); |
456 | test(m, "exec-ignoresigpipe-no.service", SIGPIPE, CLD_KILLED); | |
281e05b6 RC |
457 | } |
458 | ||
459 | static void test_exec_privatetmp(Manager *m) { | |
460 | assert_se(touch("/tmp/test-exec_privatetmp") >= 0); | |
461 | ||
d0c6136f NR |
462 | if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) { |
463 | test(m, "exec-privatetmp-yes.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED); | |
464 | test(m, "exec-privatetmp-disabled-by-prefix.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED); | |
465 | } | |
466 | ||
fe65d692 | 467 | test(m, "exec-privatetmp-no.service", 0, CLD_EXITED); |
281e05b6 | 468 | |
5000cea8 | 469 | (void) unlink("/tmp/test-exec_privatetmp"); |
281e05b6 RC |
470 | } |
471 | ||
472 | static void test_exec_privatedevices(Manager *m) { | |
f0e018e7 YW |
473 | int r; |
474 | ||
4dd4cb8f | 475 | if (detect_container() > 0) { |
f0e018e7 | 476 | log_notice("Testing in container, skipping %s", __func__); |
4dd4cb8f SM |
477 | return; |
478 | } | |
6086d2da | 479 | if (!is_inaccessible_available()) { |
f0e018e7 | 480 | log_notice("Testing without inaccessible, skipping %s", __func__); |
6086d2da DP |
481 | return; |
482 | } | |
483 | ||
d0c6136f NR |
484 | if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) { |
485 | test(m, "exec-privatedevices-yes.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED); | |
486 | if (access("/dev/kmsg", F_OK) >= 0) | |
487 | test(m, "exec-privatedevices-bind.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED); | |
488 | test(m, "exec-privatedevices-disabled-by-prefix.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED); | |
489 | test(m, "exec-privatedevices-yes-with-group.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED); | |
490 | } | |
491 | ||
fe65d692 | 492 | test(m, "exec-privatedevices-no.service", 0, CLD_EXITED); |
6086d2da | 493 | |
0608ba98 ZJS |
494 | /* We use capsh to test if the capabilities are |
495 | * properly set, so be sure that it exists */ | |
f7bc0c32 | 496 | r = find_executable("capsh", NULL); |
0608ba98 | 497 | if (r < 0) { |
5cd33ccc | 498 | log_notice_errno(r, "Could not find capsh binary, skipping remaining tests in %s: %m", __func__); |
6086d2da DP |
499 | return; |
500 | } | |
501 | ||
d0c6136f NR |
502 | if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) { |
503 | test(m, "exec-privatedevices-yes-capability-mknod.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED); | |
504 | test(m, "exec-privatedevices-yes-capability-sys-rawio.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED); | |
505 | } | |
506 | ||
adeff822 | 507 | test(m, "exec-privatedevices-no-capability-mknod.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED); |
adeff822 | 508 | test(m, "exec-privatedevices-no-capability-sys-rawio.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED); |
615a1f4b DH |
509 | } |
510 | ||
7e46b29b | 511 | static void test_exec_protecthome(Manager *m) { |
9ca58284 ZJS |
512 | if (!can_unshare) { |
513 | log_notice("Cannot reliably unshare, skipping %s", __func__); | |
514 | return; | |
515 | } | |
516 | ||
fe65d692 | 517 | test(m, "exec-protecthome-tmpfs-vs-protectsystem-strict.service", 0, CLD_EXITED); |
7e46b29b YW |
518 | } |
519 | ||
4982dbcc | 520 | static void test_exec_protectkernelmodules(Manager *m) { |
0608ba98 ZJS |
521 | int r; |
522 | ||
3ae33295 | 523 | if (detect_container() > 0) { |
f0e018e7 | 524 | log_notice("Testing in container, skipping %s", __func__); |
3ae33295 DH |
525 | return; |
526 | } | |
6086d2da | 527 | if (!is_inaccessible_available()) { |
f0e018e7 | 528 | log_notice("Testing without inaccessible, skipping %s", __func__); |
6086d2da DP |
529 | return; |
530 | } | |
3ae33295 | 531 | |
f7bc0c32 | 532 | r = find_executable("capsh", NULL); |
0608ba98 | 533 | if (r < 0) { |
5cd33ccc | 534 | log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__); |
0608ba98 ZJS |
535 | return; |
536 | } | |
537 | ||
adeff822 | 538 | test(m, "exec-protectkernelmodules-no-capabilities.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED); |
d0c6136f NR |
539 | |
540 | if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) { | |
541 | test(m, "exec-protectkernelmodules-yes-capabilities.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED); | |
542 | test(m, "exec-protectkernelmodules-yes-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED); | |
543 | } | |
3ae33295 DH |
544 | } |
545 | ||
f78b36f0 | 546 | static void test_exec_readonlypaths(Manager *m) { |
34b86909 | 547 | |
d0c6136f NR |
548 | if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) |
549 | test(m, "exec-readonlypaths-simple.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED); | |
f0e018e7 YW |
550 | |
551 | if (path_is_read_only_fs("/var") > 0) { | |
552 | log_notice("Directory /var is readonly, skipping remaining tests in %s", __func__); | |
34b86909 | 553 | return; |
f0e018e7 | 554 | } |
34b86909 | 555 | |
6ef721cb | 556 | test(m, "exec-readonlypaths.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED); |
fe65d692 | 557 | test(m, "exec-readonlypaths-with-bindpaths.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED); |
6ef721cb | 558 | test(m, "exec-readonlypaths-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED); |
cdfbd1fb DH |
559 | } |
560 | ||
561 | static void test_exec_readwritepaths(Manager *m) { | |
34b86909 | 562 | |
f0e018e7 YW |
563 | if (path_is_read_only_fs("/") > 0) { |
564 | log_notice("Root directory is readonly, skipping %s", __func__); | |
34b86909 | 565 | return; |
f0e018e7 | 566 | } |
34b86909 | 567 | |
6ef721cb | 568 | test(m, "exec-readwritepaths-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED); |
cdfbd1fb DH |
569 | } |
570 | ||
571 | static void test_exec_inaccessiblepaths(Manager *m) { | |
34b86909 | 572 | |
f0e018e7 YW |
573 | if (!is_inaccessible_available()) { |
574 | log_notice("Testing without inaccessible, skipping %s", __func__); | |
34b86909 | 575 | return; |
f0e018e7 | 576 | } |
34b86909 | 577 | |
d0c6136f NR |
578 | if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) |
579 | test(m, "exec-inaccessiblepaths-sys.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED); | |
f78b36f0 | 580 | |
f0e018e7 YW |
581 | if (path_is_read_only_fs("/") > 0) { |
582 | log_notice("Root directory is readonly, skipping remaining tests in %s", __func__); | |
af4af186 EV |
583 | return; |
584 | } | |
585 | ||
6ef721cb | 586 | test(m, "exec-inaccessiblepaths-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED); |
c090d74d TR |
587 | } |
588 | ||
42867dfe YW |
589 | static int on_spawn_io(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
590 | char **result = userdata; | |
591 | char buf[4096]; | |
592 | ssize_t l; | |
593 | ||
f21b863e YW |
594 | assert_se(s); |
595 | assert_se(fd >= 0); | |
42867dfe YW |
596 | |
597 | l = read(fd, buf, sizeof(buf) - 1); | |
598 | if (l < 0) { | |
599 | if (errno == EAGAIN) | |
600 | goto reenable; | |
601 | ||
602 | return 0; | |
603 | } | |
604 | if (l == 0) | |
605 | return 0; | |
606 | ||
607 | buf[l] = '\0'; | |
608 | if (result) | |
609 | assert_se(strextend(result, buf)); | |
610 | else | |
611 | log_error("ldd: %s", buf); | |
612 | ||
613 | reenable: | |
614 | /* Re-enable the event source if we did not encounter EOF */ | |
615 | assert_se(sd_event_source_set_enabled(s, SD_EVENT_ONESHOT) >= 0); | |
616 | return 0; | |
617 | } | |
618 | ||
619 | static int on_spawn_timeout(sd_event_source *s, uint64_t usec, void *userdata) { | |
620 | pid_t *pid = userdata; | |
621 | ||
f21b863e | 622 | assert_se(pid); |
42867dfe YW |
623 | |
624 | (void) kill(*pid, SIGKILL); | |
625 | ||
626 | return 1; | |
627 | } | |
628 | ||
629 | static int on_spawn_sigchld(sd_event_source *s, const siginfo_t *si, void *userdata) { | |
630 | int ret = -EIO; | |
631 | ||
f21b863e | 632 | assert_se(si); |
42867dfe YW |
633 | |
634 | if (si->si_code == CLD_EXITED) | |
635 | ret = si->si_status; | |
636 | ||
637 | sd_event_exit(sd_event_source_get_event(s), ret); | |
638 | return 1; | |
639 | } | |
640 | ||
641 | static int find_libraries(const char *exec, char ***ret) { | |
642 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; | |
643 | _cleanup_(sd_event_source_unrefp) sd_event_source *sigchld_source = NULL; | |
644 | _cleanup_(sd_event_source_unrefp) sd_event_source *stdout_source = NULL; | |
645 | _cleanup_(sd_event_source_unrefp) sd_event_source *stderr_source = NULL; | |
71136404 | 646 | _cleanup_close_pair_ int outpipe[2] = EBADF_PAIR, errpipe[2] = EBADF_PAIR; |
42867dfe YW |
647 | _cleanup_strv_free_ char **libraries = NULL; |
648 | _cleanup_free_ char *result = NULL; | |
649 | pid_t pid; | |
650 | int r; | |
651 | ||
f21b863e YW |
652 | assert_se(exec); |
653 | assert_se(ret); | |
42867dfe | 654 | |
db7136ec | 655 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD) >= 0); |
42867dfe YW |
656 | |
657 | assert_se(pipe2(outpipe, O_NONBLOCK|O_CLOEXEC) == 0); | |
658 | assert_se(pipe2(errpipe, O_NONBLOCK|O_CLOEXEC) == 0); | |
659 | ||
0c2aedb4 YW |
660 | r = safe_fork_full("(spawn-ldd)", |
661 | (int[]) { -EBADF, outpipe[1], errpipe[1] }, | |
662 | NULL, 0, | |
e9ccae31 | 663 | FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REARRANGE_STDIO|FORK_LOG, &pid); |
42867dfe YW |
664 | assert_se(r >= 0); |
665 | if (r == 0) { | |
42867dfe YW |
666 | execlp("ldd", "ldd", exec, NULL); |
667 | _exit(EXIT_FAILURE); | |
668 | } | |
669 | ||
670 | outpipe[1] = safe_close(outpipe[1]); | |
671 | errpipe[1] = safe_close(errpipe[1]); | |
672 | ||
673 | assert_se(sd_event_new(&e) >= 0); | |
674 | ||
675 | assert_se(sd_event_add_time_relative(e, NULL, CLOCK_MONOTONIC, | |
676 | 10 * USEC_PER_SEC, USEC_PER_SEC, on_spawn_timeout, &pid) >= 0); | |
677 | assert_se(sd_event_add_io(e, &stdout_source, outpipe[0], EPOLLIN, on_spawn_io, &result) >= 0); | |
678 | assert_se(sd_event_source_set_enabled(stdout_source, SD_EVENT_ONESHOT) >= 0); | |
679 | assert_se(sd_event_add_io(e, &stderr_source, errpipe[0], EPOLLIN, on_spawn_io, NULL) >= 0); | |
680 | assert_se(sd_event_source_set_enabled(stderr_source, SD_EVENT_ONESHOT) >= 0); | |
681 | assert_se(sd_event_add_child(e, &sigchld_source, pid, WEXITED, on_spawn_sigchld, NULL) >= 0); | |
682 | /* SIGCHLD should be processed after IO is complete */ | |
683 | assert_se(sd_event_source_set_priority(sigchld_source, SD_EVENT_PRIORITY_NORMAL + 1) >= 0); | |
684 | ||
685 | assert_se(sd_event_loop(e) >= 0); | |
686 | ||
687 | _cleanup_strv_free_ char **v = NULL; | |
688 | assert_se(strv_split_newlines_full(&v, result, 0) >= 0); | |
689 | ||
42867dfe YW |
690 | STRV_FOREACH(q, v) { |
691 | _cleanup_free_ char *word = NULL; | |
692 | const char *p = *q; | |
693 | ||
694 | r = extract_first_word(&p, &word, NULL, 0); | |
695 | assert_se(r >= 0); | |
696 | if (r == 0) | |
697 | continue; | |
698 | ||
699 | if (path_is_absolute(word)) { | |
700 | assert_se(strv_consume(&libraries, TAKE_PTR(word)) >= 0); | |
701 | continue; | |
702 | } | |
703 | ||
704 | word = mfree(word); | |
705 | r = extract_first_word(&p, &word, NULL, 0); | |
706 | assert_se(r >= 0); | |
707 | if (r == 0) | |
708 | continue; | |
709 | ||
710 | if (!streq_ptr(word, "=>")) | |
711 | continue; | |
712 | ||
713 | word = mfree(word); | |
714 | r = extract_first_word(&p, &word, NULL, 0); | |
715 | assert_se(r >= 0); | |
716 | if (r == 0) | |
717 | continue; | |
718 | ||
719 | if (path_is_absolute(word)) { | |
720 | assert_se(strv_consume(&libraries, TAKE_PTR(word)) >= 0); | |
721 | continue; | |
722 | } | |
723 | } | |
724 | ||
725 | *ret = TAKE_PTR(libraries); | |
726 | return 0; | |
727 | } | |
728 | ||
729 | static void test_exec_mount_apivfs(Manager *m) { | |
730 | _cleanup_free_ char *fullpath_touch = NULL, *fullpath_test = NULL, *data = NULL; | |
731 | _cleanup_strv_free_ char **libraries = NULL, **libraries_test = NULL; | |
732 | int r; | |
733 | ||
f21b863e | 734 | assert_se(user_runtime_unit_dir); |
42867dfe | 735 | |
9f452259 YW |
736 | r = find_executable("ldd", NULL); |
737 | if (r < 0) { | |
738 | log_notice_errno(r, "Skipping %s, could not find 'ldd' command: %m", __func__); | |
739 | return; | |
740 | } | |
42867dfe YW |
741 | r = find_executable("touch", &fullpath_touch); |
742 | if (r < 0) { | |
743 | log_notice_errno(r, "Skipping %s, could not find 'touch' command: %m", __func__); | |
744 | return; | |
745 | } | |
746 | r = find_executable("test", &fullpath_test); | |
747 | if (r < 0) { | |
748 | log_notice_errno(r, "Skipping %s, could not find 'test' command: %m", __func__); | |
749 | return; | |
750 | } | |
751 | ||
d0c6136f NR |
752 | if (MANAGER_IS_USER(m) && !have_userns_privileges()) |
753 | return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__); | |
754 | ||
42867dfe YW |
755 | assert_se(find_libraries(fullpath_touch, &libraries) >= 0); |
756 | assert_se(find_libraries(fullpath_test, &libraries_test) >= 0); | |
757 | assert_se(strv_extend_strv(&libraries, libraries_test, true) >= 0); | |
758 | ||
759 | assert_se(strextend(&data, "[Service]\n")); | |
760 | assert_se(strextend(&data, "ExecStart=", fullpath_touch, " /aaa\n")); | |
761 | assert_se(strextend(&data, "ExecStart=", fullpath_test, " -f /aaa\n")); | |
762 | assert_se(strextend(&data, "BindReadOnlyPaths=", fullpath_touch, "\n")); | |
763 | assert_se(strextend(&data, "BindReadOnlyPaths=", fullpath_test, "\n")); | |
764 | ||
42867dfe YW |
765 | STRV_FOREACH(p, libraries) |
766 | assert_se(strextend(&data, "BindReadOnlyPaths=", *p, "\n")); | |
767 | ||
768 | assert_se(write_drop_in(user_runtime_unit_dir, "exec-mount-apivfs-no.service", 10, "bind-mount", data) >= 0); | |
769 | ||
770 | assert_se(mkdir_p("/tmp/test-exec-mount-apivfs-no/root", 0755) >= 0); | |
771 | ||
6ef721cb | 772 | test(m, "exec-mount-apivfs-no.service", can_unshare || !MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED); |
42867dfe YW |
773 | |
774 | (void) rm_rf("/tmp/test-exec-mount-apivfs-no/root", REMOVE_ROOT|REMOVE_PHYSICAL); | |
775 | } | |
776 | ||
ddc155b2 TM |
777 | static void test_exec_noexecpaths(Manager *m) { |
778 | ||
d0c6136f NR |
779 | if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) |
780 | test(m, "exec-noexecpaths-simple.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED); | |
781 | else | |
782 | return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__); | |
ddc155b2 TM |
783 | } |
784 | ||
4cac89bd YW |
785 | static void test_exec_temporaryfilesystem(Manager *m) { |
786 | ||
fe65d692 ZJS |
787 | test(m, "exec-temporaryfilesystem-options.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED); |
788 | test(m, "exec-temporaryfilesystem-ro.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED); | |
789 | test(m, "exec-temporaryfilesystem-rw.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED); | |
790 | test(m, "exec-temporaryfilesystem-usr.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED); | |
4cac89bd YW |
791 | } |
792 | ||
281e05b6 | 793 | static void test_exec_systemcallfilter(Manager *m) { |
349cc4a5 | 794 | #if HAVE_SECCOMP |
738c74d7 YW |
795 | int r; |
796 | ||
f0e018e7 YW |
797 | if (!is_seccomp_available()) { |
798 | log_notice("Seccomp not available, skipping %s", __func__); | |
83f12b27 | 799 | return; |
f0e018e7 YW |
800 | } |
801 | ||
fe65d692 ZJS |
802 | test(m, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED); |
803 | test(m, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED); | |
e975a945 | 804 | test(m, "exec-systemcallfilter-not-failing3.service", 0, CLD_EXITED); |
fe65d692 ZJS |
805 | test(m, "exec-systemcallfilter-failing.service", SIGSYS, CLD_KILLED); |
806 | test(m, "exec-systemcallfilter-failing2.service", SIGSYS, CLD_KILLED); | |
e975a945 | 807 | test(m, "exec-systemcallfilter-failing3.service", SIGSYS, CLD_KILLED); |
738c74d7 | 808 | |
f7bc0c32 | 809 | r = find_executable("python3", NULL); |
738c74d7 YW |
810 | if (r < 0) { |
811 | log_notice_errno(r, "Skipping remaining tests in %s, could not find python3 binary: %m", __func__); | |
812 | return; | |
813 | } | |
814 | ||
fe65d692 ZJS |
815 | test(m, "exec-systemcallfilter-with-errno-name.service", errno_from_name("EILSEQ"), CLD_EXITED); |
816 | test(m, "exec-systemcallfilter-with-errno-number.service", 255, CLD_EXITED); | |
817 | test(m, "exec-systemcallfilter-with-errno-multi.service", errno_from_name("EILSEQ"), CLD_EXITED); | |
a62f651b | 818 | test(m, "exec-systemcallfilter-with-errno-in-allow-list.service", errno_from_name("EILSEQ"), CLD_EXITED); |
fe65d692 ZJS |
819 | test(m, "exec-systemcallfilter-override-error-action.service", SIGSYS, CLD_KILLED); |
820 | test(m, "exec-systemcallfilter-override-error-action2.service", errno_from_name("EILSEQ"), CLD_EXITED); | |
e720cebf ILG |
821 | |
822 | test(m, "exec-systemcallfilter-nonewprivileges.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED); | |
823 | test(m, "exec-systemcallfilter-nonewprivileges-protectclock.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED); | |
824 | ||
825 | r = find_executable("capsh", NULL); | |
826 | if (r < 0) { | |
827 | log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__); | |
828 | return; | |
829 | } | |
830 | ||
831 | test(m, "exec-systemcallfilter-nonewprivileges-bounding1.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED); | |
832 | test(m, "exec-systemcallfilter-nonewprivileges-bounding2.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED); | |
281e05b6 RC |
833 | #endif |
834 | } | |
835 | ||
836 | static void test_exec_systemcallerrornumber(Manager *m) { | |
349cc4a5 | 837 | #if HAVE_SECCOMP |
738c74d7 YW |
838 | int r; |
839 | ||
f0e018e7 YW |
840 | if (!is_seccomp_available()) { |
841 | log_notice("Seccomp not available, skipping %s", __func__); | |
7a18854f | 842 | return; |
f0e018e7 YW |
843 | } |
844 | ||
f7bc0c32 | 845 | r = find_executable("python3", NULL); |
738c74d7 YW |
846 | if (r < 0) { |
847 | log_notice_errno(r, "Skipping %s, could not find python3 binary: %m", __func__); | |
848 | return; | |
849 | } | |
850 | ||
fe65d692 ZJS |
851 | test(m, "exec-systemcallerrornumber-name.service", errno_from_name("EACCES"), CLD_EXITED); |
852 | test(m, "exec-systemcallerrornumber-number.service", 255, CLD_EXITED); | |
281e05b6 RC |
853 | #endif |
854 | } | |
855 | ||
f0e018e7 | 856 | static void test_exec_restrictnamespaces(Manager *m) { |
349cc4a5 | 857 | #if HAVE_SECCOMP |
f0e018e7 YW |
858 | if (!is_seccomp_available()) { |
859 | log_notice("Seccomp not available, skipping %s", __func__); | |
97e60383 | 860 | return; |
f0e018e7 | 861 | } |
97e60383 | 862 | |
fe65d692 ZJS |
863 | test(m, "exec-restrictnamespaces-no.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED); |
864 | test(m, "exec-restrictnamespaces-yes.service", 1, CLD_EXITED); | |
865 | test(m, "exec-restrictnamespaces-mnt.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED); | |
866 | test(m, "exec-restrictnamespaces-mnt-deny-list.service", 1, CLD_EXITED); | |
867 | test(m, "exec-restrictnamespaces-merge-and.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED); | |
868 | test(m, "exec-restrictnamespaces-merge-or.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED); | |
869 | test(m, "exec-restrictnamespaces-merge-all.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED); | |
97e60383 DH |
870 | #endif |
871 | } | |
872 | ||
f0e018e7 | 873 | static void test_exec_systemcallfilter_system(Manager *m) { |
4967da2d FS |
874 | /* Skip this particular test case when running under ASan, as |
875 | * LSan intermittently segfaults when accessing memory right | |
876 | * after the test finishes. Generally, ASan & LSan don't like | |
877 | * the seccomp stuff. | |
878 | */ | |
879 | #if HAVE_SECCOMP && !HAS_FEATURE_ADDRESS_SANITIZER | |
f0e018e7 YW |
880 | if (!is_seccomp_available()) { |
881 | log_notice("Seccomp not available, skipping %s", __func__); | |
83f12b27 | 882 | return; |
f0e018e7 | 883 | } |
57c2efa0 | 884 | |
4e032f65 | 885 | test(m, "exec-systemcallfilter-system-user.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED); |
69b07407 | 886 | |
57c2efa0 | 887 | if (!check_nobody_user_and_group()) { |
5cd33ccc | 888 | log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__); |
57c2efa0 YW |
889 | return; |
890 | } | |
891 | ||
69b07407 | 892 | if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) { |
5cd33ccc | 893 | log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__); |
69b07407 YW |
894 | return; |
895 | } | |
896 | ||
4e032f65 | 897 | test(m, "exec-systemcallfilter-system-user-" NOBODY_USER_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED); |
19c0b0b9 RC |
898 | #endif |
899 | } | |
900 | ||
281e05b6 | 901 | static void test_exec_user(Manager *m) { |
4e032f65 | 902 | test(m, "exec-user.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED); |
69b07407 | 903 | |
57c2efa0 | 904 | if (!check_nobody_user_and_group()) { |
5cd33ccc | 905 | log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__); |
57c2efa0 YW |
906 | return; |
907 | } | |
908 | ||
69b07407 | 909 | if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) { |
5cd33ccc | 910 | log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__); |
69b07407 YW |
911 | return; |
912 | } | |
913 | ||
4e032f65 | 914 | test(m, "exec-user-" NOBODY_USER_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED); |
281e05b6 RC |
915 | } |
916 | ||
917 | static void test_exec_group(Manager *m) { | |
4e032f65 | 918 | test(m, "exec-group.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED); |
69b07407 | 919 | |
57c2efa0 | 920 | if (!check_nobody_user_and_group()) { |
5cd33ccc | 921 | log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__); |
57c2efa0 YW |
922 | return; |
923 | } | |
924 | ||
69b07407 | 925 | if (!STR_IN_SET(NOBODY_GROUP_NAME, "nobody", "nfsnobody", "nogroup")) { |
5cd33ccc | 926 | log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME, __func__); |
69b07407 YW |
927 | return; |
928 | } | |
929 | ||
4e032f65 | 930 | test(m, "exec-group-" NOBODY_GROUP_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED); |
281e05b6 RC |
931 | } |
932 | ||
f0e018e7 | 933 | static void test_exec_supplementarygroups(Manager *m) { |
4e032f65 YW |
934 | int status = MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP; |
935 | test(m, "exec-supplementarygroups.service", status, CLD_EXITED); | |
936 | test(m, "exec-supplementarygroups-single-group.service", status, CLD_EXITED); | |
937 | test(m, "exec-supplementarygroups-single-group-user.service", status, CLD_EXITED); | |
938 | test(m, "exec-supplementarygroups-multiple-groups-default-group-user.service", status, CLD_EXITED); | |
939 | test(m, "exec-supplementarygroups-multiple-groups-withgid.service", status, CLD_EXITED); | |
940 | test(m, "exec-supplementarygroups-multiple-groups-withuid.service", status, CLD_EXITED); | |
86b838ea DH |
941 | } |
942 | ||
cced2b98 ZJS |
943 | static char* private_directory_bad(Manager *m) { |
944 | /* This mirrors setup_exec_directory(). */ | |
945 | ||
946 | for (ExecDirectoryType dt = 0; dt < _EXEC_DIRECTORY_TYPE_MAX; dt++) { | |
947 | _cleanup_free_ char *p = NULL; | |
948 | struct stat st; | |
949 | ||
950 | assert_se(p = path_join(m->prefix[dt], "private")); | |
951 | ||
952 | if (stat(p, &st) >= 0 && | |
953 | (st.st_mode & (S_IRWXG|S_IRWXO))) | |
954 | return TAKE_PTR(p); | |
955 | } | |
956 | ||
957 | return NULL; | |
958 | } | |
959 | ||
f0e018e7 | 960 | static void test_exec_dynamicuser(Manager *m) { |
cced2b98 ZJS |
961 | _cleanup_free_ char *bad = private_directory_bad(m); |
962 | if (bad) { | |
963 | log_warning("%s: %s has bad permissions, skipping test.", __func__, bad); | |
964 | return; | |
965 | } | |
b7172f34 | 966 | |
d1b74295 FS |
967 | if (strstr_ptr(ci_environment(), "github-actions")) { |
968 | log_notice("%s: skipping test on GH Actions because of systemd/systemd#10337", __func__); | |
969 | return; | |
970 | } | |
971 | ||
4e032f65 YW |
972 | int status = can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NAMESPACE : EXIT_GROUP; |
973 | ||
974 | test(m, "exec-dynamicuser-fixeduser.service", status, CLD_EXITED); | |
9f82d685 | 975 | if (check_user_has_group_with_same_name("adm")) |
4e032f65 | 976 | test(m, "exec-dynamicuser-fixeduser-adm.service", status, CLD_EXITED); |
9f82d685 | 977 | if (check_user_has_group_with_same_name("games")) |
4e032f65 YW |
978 | test(m, "exec-dynamicuser-fixeduser-games.service", status, CLD_EXITED); |
979 | test(m, "exec-dynamicuser-fixeduser-one-supplementarygroup.service", status, CLD_EXITED); | |
980 | test(m, "exec-dynamicuser-supplementarygroups.service", status, CLD_EXITED); | |
981 | test(m, "exec-dynamicuser-statedir.service", status, CLD_EXITED); | |
028f3a7f | 982 | |
f5939651 | 983 | (void) rm_rf("/var/lib/quux", REMOVE_ROOT|REMOVE_PHYSICAL); |
1c587309 YW |
984 | (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL); |
985 | (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL); | |
f5939651 TM |
986 | (void) rm_rf("/var/lib/waldo", REMOVE_ROOT|REMOVE_PHYSICAL); |
987 | (void) rm_rf("/var/lib/private/quux", REMOVE_ROOT|REMOVE_PHYSICAL); | |
1c587309 YW |
988 | (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL); |
989 | (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL); | |
f5939651 | 990 | (void) rm_rf("/var/lib/private/waldo", REMOVE_ROOT|REMOVE_PHYSICAL); |
1c587309 | 991 | |
fe65d692 | 992 | test(m, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED); |
4e032f65 | 993 | test(m, "exec-dynamicuser-statedir-migrate-step2.service", status, CLD_EXITED); |
fe65d692 | 994 | test(m, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED); |
1c587309 | 995 | |
028f3a7f YW |
996 | (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL); |
997 | (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL); | |
998 | (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL); | |
999 | (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL); | |
e4c01fe6 | 1000 | |
4e032f65 YW |
1001 | test(m, "exec-dynamicuser-runtimedirectory1.service", status, CLD_EXITED); |
1002 | test(m, "exec-dynamicuser-runtimedirectory2.service", status, CLD_EXITED); | |
1003 | test(m, "exec-dynamicuser-runtimedirectory3.service", status, CLD_EXITED); | |
2b9ac11e DH |
1004 | } |
1005 | ||
281e05b6 | 1006 | static void test_exec_environment(Manager *m) { |
fe65d692 ZJS |
1007 | test(m, "exec-environment-no-substitute.service", 0, CLD_EXITED); |
1008 | test(m, "exec-environment.service", 0, CLD_EXITED); | |
1009 | test(m, "exec-environment-multiple.service", 0, CLD_EXITED); | |
1010 | test(m, "exec-environment-empty.service", 0, CLD_EXITED); | |
281e05b6 RC |
1011 | } |
1012 | ||
03bd70dd RC |
1013 | static void test_exec_environmentfile(Manager *m) { |
1014 | static const char e[] = | |
1015 | "VAR1='word1 word2'\n" | |
1016 | "VAR2=word3 \n" | |
1017 | "# comment1\n" | |
1018 | "\n" | |
1019 | "; comment2\n" | |
1020 | " ; # comment3\n" | |
1021 | "line without an equal\n" | |
9b796f35 | 1022 | "VAR3='$word 5 6'\n" |
b6887d7a JH |
1023 | "VAR4='new\nline'\n" |
1024 | "VAR5=password\\with\\backslashes"; | |
03bd70dd RC |
1025 | int r; |
1026 | ||
1027 | r = write_string_file("/tmp/test-exec_environmentfile.conf", e, WRITE_STRING_FILE_CREATE); | |
1028 | assert_se(r == 0); | |
1029 | ||
fe65d692 | 1030 | test(m, "exec-environmentfile.service", 0, CLD_EXITED); |
03bd70dd | 1031 | |
f0e018e7 | 1032 | (void) unlink("/tmp/test-exec_environmentfile.conf"); |
03bd70dd RC |
1033 | } |
1034 | ||
4c80d201 | 1035 | static void test_exec_passenvironment(Manager *m) { |
e1abca2e FB |
1036 | /* test-execute runs under MANAGER_USER which, by default, forwards all |
1037 | * variables present in the environment, but only those that are | |
1038 | * present _at the time it is created_! | |
1039 | * | |
1040 | * So these PassEnvironment checks are still expected to work, since we | |
1041 | * are ensuring the variables are not present at manager creation (they | |
1042 | * are unset explicitly in main) and are only set here. | |
1043 | * | |
1044 | * This is still a good approximation of how a test for MANAGER_SYSTEM | |
1045 | * would work. | |
1046 | */ | |
4c80d201 FB |
1047 | assert_se(setenv("VAR1", "word1 word2", 1) == 0); |
1048 | assert_se(setenv("VAR2", "word3", 1) == 0); | |
1049 | assert_se(setenv("VAR3", "$word 5 6", 1) == 0); | |
9b796f35 | 1050 | assert_se(setenv("VAR4", "new\nline", 1) == 0); |
b6887d7a | 1051 | assert_se(setenv("VAR5", "passwordwithbackslashes", 1) == 0); |
fe65d692 ZJS |
1052 | test(m, "exec-passenvironment.service", 0, CLD_EXITED); |
1053 | test(m, "exec-passenvironment-repeated.service", 0, CLD_EXITED); | |
1054 | test(m, "exec-passenvironment-empty.service", 0, CLD_EXITED); | |
4c80d201 FB |
1055 | assert_se(unsetenv("VAR1") == 0); |
1056 | assert_se(unsetenv("VAR2") == 0); | |
1057 | assert_se(unsetenv("VAR3") == 0); | |
9b796f35 | 1058 | assert_se(unsetenv("VAR4") == 0); |
b6887d7a | 1059 | assert_se(unsetenv("VAR5") == 0); |
fe65d692 | 1060 | test(m, "exec-passenvironment-absent.service", 0, CLD_EXITED); |
4c80d201 FB |
1061 | } |
1062 | ||
27c5347c | 1063 | static void test_exec_umask(Manager *m) { |
d0c6136f NR |
1064 | if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) { |
1065 | test(m, "exec-umask-default.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED); | |
1066 | test(m, "exec-umask-0177.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED); | |
1067 | } else | |
1068 | return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__); | |
27c5347c RC |
1069 | } |
1070 | ||
cc3ddc85 | 1071 | static void test_exec_runtimedirectory(Manager *m) { |
4e032f65 | 1072 | (void) rm_rf("/run/test-exec_runtimedirectory2", REMOVE_ROOT|REMOVE_PHYSICAL); |
fe65d692 | 1073 | test(m, "exec-runtimedirectory.service", 0, CLD_EXITED); |
4e032f65 YW |
1074 | (void) rm_rf("/run/test-exec_runtimedirectory2", REMOVE_ROOT|REMOVE_PHYSICAL); |
1075 | ||
fe65d692 | 1076 | test(m, "exec-runtimedirectory-mode.service", 0, CLD_EXITED); |
4e032f65 | 1077 | test(m, "exec-runtimedirectory-owner.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED); |
57c2efa0 YW |
1078 | |
1079 | if (!check_nobody_user_and_group()) { | |
5cd33ccc | 1080 | log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__); |
69b07407 YW |
1081 | return; |
1082 | } | |
1083 | ||
1084 | if (!STR_IN_SET(NOBODY_GROUP_NAME, "nobody", "nfsnobody", "nogroup")) { | |
5cd33ccc | 1085 | log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME, __func__); |
57c2efa0 YW |
1086 | return; |
1087 | } | |
1088 | ||
4e032f65 | 1089 | test(m, "exec-runtimedirectory-owner-" NOBODY_GROUP_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED); |
ff4ca461 RC |
1090 | } |
1091 | ||
1092 | static void test_exec_capabilityboundingset(Manager *m) { | |
1093 | int r; | |
1094 | ||
f7bc0c32 | 1095 | r = find_executable("capsh", NULL); |
ff4ca461 | 1096 | if (r < 0) { |
5cd33ccc | 1097 | log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__); |
ff4ca461 RC |
1098 | return; |
1099 | } | |
1100 | ||
b7856f92 YW |
1101 | if (have_effective_cap(CAP_CHOWN) <= 0 || |
1102 | have_effective_cap(CAP_FOWNER) <= 0 || | |
1103 | have_effective_cap(CAP_KILL) <= 0) { | |
1104 | log_notice("Skipping %s, this process does not have enough capabilities", __func__); | |
1105 | return; | |
1106 | } | |
1107 | ||
fe65d692 ZJS |
1108 | test(m, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED); |
1109 | test(m, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED); | |
1110 | test(m, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED); | |
1111 | test(m, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED); | |
cc3ddc85 RC |
1112 | } |
1113 | ||
5008da1e | 1114 | static void test_exec_basic(Manager *m) { |
d0c6136f NR |
1115 | if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) |
1116 | test(m, "exec-basic.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED); | |
1117 | else | |
1118 | return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__); | |
5008da1e ZJS |
1119 | } |
1120 | ||
b6dc25ee | 1121 | static void test_exec_ambientcapabilities(Manager *m) { |
70d7aea5 IP |
1122 | int r; |
1123 | ||
1124 | /* Check if the kernel has support for ambient capabilities. Run | |
1125 | * the tests only if that's the case. Clearing all ambient | |
1126 | * capabilities is fine, since we are expecting them to be unset | |
1127 | * in the first place for the tests. */ | |
1128 | r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0); | |
f0e018e7 | 1129 | if (r < 0 && IN_SET(errno, EINVAL, EOPNOTSUPP, ENOSYS)) { |
5cd33ccc | 1130 | log_notice("Skipping %s, the kernel does not support ambient capabilities", __func__); |
f0e018e7 YW |
1131 | return; |
1132 | } | |
1133 | ||
e5ba1d32 | 1134 | if (have_effective_cap(CAP_CHOWN) <= 0 || |
b7856f92 YW |
1135 | have_effective_cap(CAP_NET_RAW) <= 0) { |
1136 | log_notice("Skipping %s, this process does not have enough capabilities", __func__); | |
1137 | return; | |
1138 | } | |
1139 | ||
fe65d692 ZJS |
1140 | test(m, "exec-ambientcapabilities.service", 0, CLD_EXITED); |
1141 | test(m, "exec-ambientcapabilities-merge.service", 0, CLD_EXITED); | |
69b07407 | 1142 | |
f4a35f2a LB |
1143 | if (have_effective_cap(CAP_SETUID) > 0) |
1144 | test(m, "exec-ambientcapabilities-dynuser.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED); | |
1145 | ||
57c2efa0 | 1146 | if (!check_nobody_user_and_group()) { |
5cd33ccc | 1147 | log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__); |
69b07407 YW |
1148 | return; |
1149 | } | |
1150 | ||
1151 | if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) { | |
5cd33ccc | 1152 | log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__); |
57c2efa0 YW |
1153 | return; |
1154 | } | |
1155 | ||
fe65d692 ZJS |
1156 | test(m, "exec-ambientcapabilities-" NOBODY_USER_NAME ".service", 0, CLD_EXITED); |
1157 | test(m, "exec-ambientcapabilities-merge-" NOBODY_USER_NAME ".service", 0, CLD_EXITED); | |
70d7aea5 IP |
1158 | } |
1159 | ||
63447f11 | 1160 | static void test_exec_privatenetwork(Manager *m) { |
6ef721cb | 1161 | int r; |
63447f11 | 1162 | |
8a87a16b MK |
1163 | if (!have_net_dummy) |
1164 | return (void)log_notice("Skipping %s, dummy network interface not available", __func__); | |
1165 | ||
d0c6136f NR |
1166 | if (MANAGER_IS_USER(m) && !have_userns_privileges()) |
1167 | return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__); | |
1168 | ||
f7bc0c32 | 1169 | r = find_executable("ip", NULL); |
63447f11 | 1170 | if (r < 0) { |
5cd33ccc | 1171 | log_notice_errno(r, "Skipping %s, could not find ip binary: %m", __func__); |
63447f11 RC |
1172 | return; |
1173 | } | |
1174 | ||
6ef721cb LB |
1175 | test(m, "exec-privatenetwork-yes-privatemounts-no.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NETWORK : EXIT_FAILURE, CLD_EXITED); |
1176 | test(m, "exec-privatenetwork-yes-privatemounts-yes.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NETWORK : EXIT_NAMESPACE, CLD_EXITED); | |
63447f11 RC |
1177 | } |
1178 | ||
600ed5c2 YW |
1179 | static void test_exec_networknamespacepath(Manager *m) { |
1180 | int r; | |
1181 | ||
8a87a16b MK |
1182 | if (!have_net_dummy) |
1183 | return (void)log_notice("Skipping %s, dummy network interface not available", __func__); | |
1184 | ||
76808638 NR |
1185 | if (!have_netns) |
1186 | return (void)log_notice("Skipping %s, network namespace not available", __func__); | |
1187 | ||
d0c6136f NR |
1188 | if (MANAGER_IS_USER(m) && !have_userns_privileges()) |
1189 | return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__); | |
1190 | ||
600ed5c2 YW |
1191 | r = find_executable("ip", NULL); |
1192 | if (r < 0) { | |
1193 | log_notice_errno(r, "Skipping %s, could not find ip binary: %m", __func__); | |
1194 | return; | |
1195 | } | |
1196 | ||
1197 | test(m, "exec-networknamespacepath-privatemounts-no.service", MANAGER_IS_SYSTEM(m) ? EXIT_SUCCESS : EXIT_FAILURE, CLD_EXITED); | |
6ef721cb | 1198 | test(m, "exec-networknamespacepath-privatemounts-yes.service", can_unshare ? EXIT_SUCCESS : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED); |
600ed5c2 YW |
1199 | } |
1200 | ||
c388dfea | 1201 | static void test_exec_oomscoreadjust(Manager *m) { |
fe65d692 | 1202 | test(m, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED); |
642d1a6d YW |
1203 | |
1204 | if (detect_container() > 0) { | |
1205 | log_notice("Testing in container, skipping remaining tests in %s", __func__); | |
1206 | return; | |
1207 | } | |
4e032f65 | 1208 | test(m, "exec-oomscoreadjust-negative.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED); |
c388dfea RC |
1209 | } |
1210 | ||
a6226758 | 1211 | static void test_exec_ioschedulingclass(Manager *m) { |
fe65d692 ZJS |
1212 | test(m, "exec-ioschedulingclass-none.service", 0, CLD_EXITED); |
1213 | test(m, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED); | |
1214 | test(m, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED); | |
642d1a6d YW |
1215 | |
1216 | if (detect_container() > 0) { | |
1217 | log_notice("Testing in container, skipping remaining tests in %s", __func__); | |
1218 | return; | |
1219 | } | |
4e032f65 | 1220 | test(m, "exec-ioschedulingclass-realtime.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_IOPRIO, CLD_EXITED); |
a6226758 RC |
1221 | } |
1222 | ||
f0e018e7 | 1223 | static void test_exec_unsetenvironment(Manager *m) { |
fe65d692 | 1224 | test(m, "exec-unsetenvironment.service", 0, CLD_EXITED); |
42cc99d5 LP |
1225 | } |
1226 | ||
9672b583 | 1227 | static void test_exec_specifier(Manager *m) { |
adeff822 | 1228 | test(m, "exec-specifier.service", 0, CLD_EXITED); |
4e032f65 YW |
1229 | if (MANAGER_IS_SYSTEM(m)) |
1230 | test(m, "exec-specifier-system.service", 0, CLD_EXITED); | |
1231 | else | |
1232 | test(m, "exec-specifier-user.service", 0, CLD_EXITED); | |
adeff822 | 1233 | test(m, "exec-specifier@foo-bar.service", 0, CLD_EXITED); |
fe65d692 | 1234 | test(m, "exec-specifier-interpolation.service", 0, CLD_EXITED); |
9672b583 LP |
1235 | } |
1236 | ||
f0e018e7 | 1237 | static void test_exec_standardinput(Manager *m) { |
fe65d692 ZJS |
1238 | test(m, "exec-standardinput-data.service", 0, CLD_EXITED); |
1239 | test(m, "exec-standardinput-file.service", 0, CLD_EXITED); | |
1240 | test(m, "exec-standardinput-file-cat.service", 0, CLD_EXITED); | |
666d7877 LP |
1241 | } |
1242 | ||
566b7d23 | 1243 | static void test_exec_standardoutput(Manager *m) { |
fe65d692 | 1244 | test(m, "exec-standardoutput-file.service", 0, CLD_EXITED); |
566b7d23 ZD |
1245 | } |
1246 | ||
1247 | static void test_exec_standardoutput_append(Manager *m) { | |
fe65d692 | 1248 | test(m, "exec-standardoutput-append.service", 0, CLD_EXITED); |
566b7d23 ZD |
1249 | } |
1250 | ||
8d7dab1f LW |
1251 | static void test_exec_standardoutput_truncate(Manager *m) { |
1252 | test(m, "exec-standardoutput-truncate.service", 0, CLD_EXITED); | |
1253 | } | |
1254 | ||
31cd5f63 | 1255 | static void test_exec_condition(Manager *m) { |
fe65d692 ZJS |
1256 | test_service(m, "exec-condition-failed.service", SERVICE_FAILURE_EXIT_CODE); |
1257 | test_service(m, "exec-condition-skip.service", SERVICE_SKIP_CONDITION); | |
31cd5f63 AZ |
1258 | } |
1259 | ||
875afa02 | 1260 | static void test_exec_umask_namespace(Manager *m) { |
bfd67106 YW |
1261 | /* exec-specifier-credentials-dir.service creates /run/credentials and enables implicit |
1262 | * InaccessiblePath= for the directory for all later services with mount namespace. */ | |
1263 | if (!is_inaccessible_available()) { | |
1264 | log_notice("Testing without inaccessible, skipping %s", __func__); | |
1265 | return; | |
1266 | } | |
4e032f65 | 1267 | test(m, "exec-umask-namespace.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NAMESPACE : EXIT_GROUP, CLD_EXITED); |
875afa02 LP |
1268 | } |
1269 | ||
9efb9631 ZJS |
1270 | typedef struct test_entry { |
1271 | test_function_t f; | |
1272 | const char *name; | |
1273 | } test_entry; | |
1274 | ||
1275 | #define entry(x) {x, #x} | |
1276 | ||
4870133b | 1277 | static void run_tests(RuntimeScope scope, char **patterns) { |
4e032f65 YW |
1278 | _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL; |
1279 | _cleanup_free_ char *unit_paths = NULL; | |
c70cac54 | 1280 | _cleanup_(manager_freep) Manager *m = NULL; |
791a84ad | 1281 | usec_t start, finish; |
19c0b0b9 RC |
1282 | int r; |
1283 | ||
4e032f65 | 1284 | static const test_entry tests[] = { |
9efb9631 ZJS |
1285 | entry(test_exec_basic), |
1286 | entry(test_exec_ambientcapabilities), | |
1287 | entry(test_exec_bindpaths), | |
1288 | entry(test_exec_capabilityboundingset), | |
31cd5f63 | 1289 | entry(test_exec_condition), |
9efb9631 | 1290 | entry(test_exec_cpuaffinity), |
b7cca6cc | 1291 | entry(test_exec_credentials), |
4e032f65 | 1292 | entry(test_exec_dynamicuser), |
9efb9631 ZJS |
1293 | entry(test_exec_environment), |
1294 | entry(test_exec_environmentfile), | |
4e032f65 YW |
1295 | entry(test_exec_execsearchpath), |
1296 | entry(test_exec_execsearchpath_environment), | |
1297 | entry(test_exec_execsearchpath_environment_files), | |
1298 | entry(test_exec_execsearchpath_passenvironment), | |
1299 | entry(test_exec_execsearchpath_specifier), | |
9efb9631 ZJS |
1300 | entry(test_exec_group), |
1301 | entry(test_exec_ignoresigpipe), | |
1302 | entry(test_exec_inaccessiblepaths), | |
1303 | entry(test_exec_ioschedulingclass), | |
42867dfe | 1304 | entry(test_exec_mount_apivfs), |
600ed5c2 | 1305 | entry(test_exec_networknamespacepath), |
ddc155b2 | 1306 | entry(test_exec_noexecpaths), |
9efb9631 ZJS |
1307 | entry(test_exec_oomscoreadjust), |
1308 | entry(test_exec_passenvironment), | |
1309 | entry(test_exec_personality), | |
1310 | entry(test_exec_privatedevices), | |
1311 | entry(test_exec_privatenetwork), | |
1312 | entry(test_exec_privatetmp), | |
1313 | entry(test_exec_protecthome), | |
1314 | entry(test_exec_protectkernelmodules), | |
1315 | entry(test_exec_readonlypaths), | |
1316 | entry(test_exec_readwritepaths), | |
1317 | entry(test_exec_restrictnamespaces), | |
1318 | entry(test_exec_runtimedirectory), | |
4e032f65 | 1319 | entry(test_exec_specifier), |
9efb9631 ZJS |
1320 | entry(test_exec_standardinput), |
1321 | entry(test_exec_standardoutput), | |
1322 | entry(test_exec_standardoutput_append), | |
8d7dab1f | 1323 | entry(test_exec_standardoutput_truncate), |
9efb9631 ZJS |
1324 | entry(test_exec_supplementarygroups), |
1325 | entry(test_exec_systemcallerrornumber), | |
1326 | entry(test_exec_systemcallfilter), | |
4e032f65 | 1327 | entry(test_exec_systemcallfilter_system), |
9efb9631 ZJS |
1328 | entry(test_exec_temporaryfilesystem), |
1329 | entry(test_exec_umask), | |
4e032f65 | 1330 | entry(test_exec_umask_namespace), |
9efb9631 ZJS |
1331 | entry(test_exec_unsetenvironment), |
1332 | entry(test_exec_user), | |
1333 | entry(test_exec_workingdirectory), | |
9efb9631 | 1334 | {}, |
19c0b0b9 | 1335 | }; |
176ceb2c | 1336 | |
44ee03d1 ZJS |
1337 | assert_se(unsetenv("USER") == 0); |
1338 | assert_se(unsetenv("LOGNAME") == 0); | |
1339 | assert_se(unsetenv("SHELL") == 0); | |
1340 | assert_se(unsetenv("HOME") == 0); | |
1341 | assert_se(unsetenv("TMPDIR") == 0); | |
2482f88d | 1342 | |
4e032f65 YW |
1343 | /* Unset VARx, especially, VAR1, VAR2 and VAR3, which are used in the PassEnvironment test cases, |
1344 | * otherwise (and if they are present in the environment), `manager_default_environment` will copy | |
1345 | * them into the default environment which is passed to each created job, which will make the tests | |
1346 | * that expect those not to be present to fail. */ | |
1347 | assert_se(unsetenv("VAR1") == 0); | |
1348 | assert_se(unsetenv("VAR2") == 0); | |
1349 | assert_se(unsetenv("VAR3") == 0); | |
1350 | assert_se(unsetenv("VAR4") == 0); | |
1351 | assert_se(unsetenv("VAR5") == 0); | |
34b59770 | 1352 | |
ac1f08b9 | 1353 | assert_se(runtime_dir = setup_fake_runtime_dir()); |
42867dfe | 1354 | assert_se(user_runtime_unit_dir = path_join(runtime_dir, "systemd/user")); |
4e032f65 | 1355 | assert_se(unit_paths = strjoin(PRIVATE_UNIT_DIR, ":", user_runtime_unit_dir)); |
42867dfe | 1356 | assert_se(set_unit_path(unit_paths) >= 0); |
281e05b6 | 1357 | |
4e032f65 YW |
1358 | r = manager_new(scope, MANAGER_TEST_RUN_BASIC, &m); |
1359 | if (manager_errno_skip_test(r)) | |
1360 | return (void) log_tests_skipped_errno(r, "manager_new"); | |
1361 | assert_se(r >= 0); | |
1362 | ||
c9e120e0 | 1363 | m->defaults.std_output = EXEC_OUTPUT_NULL; /* don't rely on host journald */ |
4e032f65 YW |
1364 | assert_se(manager_startup(m, NULL, NULL, NULL) >= 0); |
1365 | ||
1366 | /* Uncomment below if you want to make debugging logs stored to journal. */ | |
1367 | //manager_override_log_target(m, LOG_TARGET_AUTO); | |
1368 | //manager_override_log_level(m, LOG_DEBUG); | |
1369 | ||
791a84ad LB |
1370 | /* Measure and print the time that it takes to run tests, excluding startup of the manager object, |
1371 | * to try and measure latency of spawning services */ | |
b6349ffd | 1372 | n_ran_tests = 0; |
791a84ad LB |
1373 | start = now(CLOCK_MONOTONIC); |
1374 | ||
4e032f65 YW |
1375 | for (const test_entry *test = tests; test->f; test++) |
1376 | if (strv_fnmatch_or_empty(patterns, test->name, FNM_NOESCAPE)) | |
1377 | test->f(m); | |
1378 | else | |
1379 | log_info("Skipping %s because it does not match any pattern.", test->name); | |
791a84ad LB |
1380 | |
1381 | finish = now(CLOCK_MONOTONIC); | |
1382 | ||
b6349ffd LB |
1383 | log_info("ran %u tests with %s manager + unshare=%s in: %s", |
1384 | n_ran_tests, | |
791a84ad LB |
1385 | scope == RUNTIME_SCOPE_SYSTEM ? "system" : "user", |
1386 | yes_no(can_unshare), | |
1387 | FORMAT_TIMESPAN(finish - start, USEC_PER_MSEC)); | |
4e032f65 YW |
1388 | } |
1389 | ||
1390 | static int prepare_ns(const char *process_name) { | |
1391 | int r; | |
1392 | ||
1393 | r = safe_fork(process_name, | |
1394 | FORK_RESET_SIGNALS | | |
1395 | FORK_CLOSE_ALL_FDS | | |
e9ccae31 | 1396 | FORK_DEATHSIG_SIGTERM | |
4e032f65 YW |
1397 | FORK_WAIT | |
1398 | FORK_REOPEN_LOG | | |
1399 | FORK_LOG | | |
1400 | FORK_NEW_MOUNTNS | | |
1401 | FORK_MOUNTNS_SLAVE, | |
1402 | NULL); | |
1403 | assert_se(r >= 0); | |
1404 | if (r == 0) { | |
1405 | _cleanup_free_ char *unit_dir = NULL; | |
1406 | ||
1407 | /* Make "/" read-only. */ | |
e15ad182 | 1408 | assert_se(mount_nofollow_verbose(LOG_DEBUG, NULL, "/", NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL) >= 0); |
4e032f65 YW |
1409 | |
1410 | /* Creating a new user namespace in the above means all MS_SHARED mounts become MS_SLAVE. | |
1411 | * Let's put them back to MS_SHARED here, since that's what we want as defaults. (This will | |
1412 | * not reconnect propagation, but simply create new peer groups for all our mounts). */ | |
1413 | assert_se(mount_follow_verbose(LOG_DEBUG, NULL, "/", NULL, MS_SHARED|MS_REC, NULL) >= 0); | |
1414 | ||
1415 | assert_se(mkdir_p(PRIVATE_UNIT_DIR, 0755) >= 0); | |
c109cff9 | 1416 | assert_se(mount_nofollow_verbose(LOG_DEBUG, "tmpfs", PRIVATE_UNIT_DIR, "tmpfs", MS_NOSUID|MS_NODEV, NULL) >= 0); |
e1abca2e | 1417 | |
4e032f65 YW |
1418 | /* Copy unit files to make them accessible even when unprivileged. */ |
1419 | assert_se(get_testdata_dir("test-execute/", &unit_dir) >= 0); | |
f9f70e06 | 1420 | assert_se(copy_directory_at(AT_FDCWD, unit_dir, AT_FDCWD, PRIVATE_UNIT_DIR, COPY_MERGE_EMPTY) >= 0); |
b7172f34 | 1421 | |
c109cff9 FS |
1422 | /* Mount tmpfs on the following directories to make not StateDirectory= or friends disturb the host. */ |
1423 | FOREACH_STRING(p, "/dev/shm", "/root", "/tmp", "/var/tmp", "/var/lib") | |
1424 | assert_se(mount_nofollow_verbose(LOG_DEBUG, "tmpfs", p, "tmpfs", MS_NOSUID|MS_NODEV, NULL) >= 0); | |
1425 | ||
4e032f65 YW |
1426 | /* Prepare credstore like tmpfiles.d/credstore.conf for LoadCredential= tests. */ |
1427 | FOREACH_STRING(p, "/run/credstore", "/run/credstore.encrypted") { | |
1428 | assert_se(mkdir_p(p, 0) >= 0); | |
1429 | assert_se(mount_nofollow_verbose(LOG_DEBUG, "tmpfs", p, "tmpfs", MS_NOSUID|MS_NODEV, "mode=0000") >= 0); | |
1430 | } | |
b7cca6cc YW |
1431 | |
1432 | assert_se(write_string_file("/run/credstore/test-execute.load-credential", "foo", WRITE_STRING_FILE_CREATE) >= 0); | |
4e032f65 YW |
1433 | } |
1434 | ||
1435 | return r; | |
1436 | } | |
1437 | ||
1438 | TEST(run_tests_root) { | |
1439 | _cleanup_strv_free_ char **filters = NULL; | |
1440 | ||
1441 | if (!have_namespaces()) | |
1442 | return (void) log_tests_skipped("unshare() is disabled"); | |
1443 | ||
1444 | /* safe_fork() clears saved_argv in the child process. Let's copy it. */ | |
1445 | assert_se(filters = strv_copy(strv_skip(saved_argv, 1))); | |
1446 | ||
1447 | if (prepare_ns("(test-execute-root)") == 0) { | |
1448 | can_unshare = true; | |
4870133b | 1449 | run_tests(RUNTIME_SCOPE_SYSTEM, filters); |
4e032f65 YW |
1450 | _exit(EXIT_SUCCESS); |
1451 | } | |
1452 | } | |
1453 | ||
1454 | TEST(run_tests_without_unshare) { | |
1455 | if (!have_namespaces()) { | |
1456 | /* unshare() is already filtered. */ | |
1457 | can_unshare = false; | |
4870133b | 1458 | run_tests(RUNTIME_SCOPE_SYSTEM, strv_skip(saved_argv, 1)); |
4e032f65 YW |
1459 | return; |
1460 | } | |
b7172f34 YW |
1461 | |
1462 | #if HAVE_SECCOMP | |
4e032f65 YW |
1463 | _cleanup_strv_free_ char **filters = NULL; |
1464 | int r; | |
1465 | ||
b7172f34 | 1466 | /* The following tests are for 1beab8b0d0ff2d7d1436b52d4a0c3d56dc908962. */ |
4e032f65 YW |
1467 | if (!is_seccomp_available()) |
1468 | return (void) log_tests_skipped("Seccomp not available, cannot run unshare() filtered tests"); | |
1469 | ||
1470 | /* safe_fork() clears saved_argv in the child process. Let's copy it. */ | |
1471 | assert_se(filters = strv_copy(strv_skip(saved_argv, 1))); | |
b7172f34 | 1472 | |
4e032f65 YW |
1473 | if (prepare_ns("(test-execute-without-unshare)") == 0) { |
1474 | _cleanup_hashmap_free_ Hashmap *s = NULL; | |
b7172f34 | 1475 | |
4e032f65 YW |
1476 | r = seccomp_syscall_resolve_name("unshare"); |
1477 | assert_se(r != __NR_SCMP_ERROR); | |
1478 | assert_se(hashmap_ensure_put(&s, NULL, UINT32_TO_PTR(r + 1), INT_TO_PTR(-1)) >= 0); | |
1479 | assert_se(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EOPNOTSUPP), true) >= 0); | |
b7172f34 | 1480 | |
4e032f65 YW |
1481 | /* Check unshare() is actually filtered. */ |
1482 | assert_se(unshare(CLONE_NEWNS) < 0); | |
1483 | assert_se(errno == EOPNOTSUPP); | |
281e05b6 | 1484 | |
4e032f65 | 1485 | can_unshare = false; |
4870133b | 1486 | run_tests(RUNTIME_SCOPE_SYSTEM, filters); |
4e032f65 YW |
1487 | _exit(EXIT_SUCCESS); |
1488 | } | |
b7172f34 | 1489 | #else |
4e032f65 YW |
1490 | log_tests_skipped("Built without seccomp support, cannot run unshare() filtered tests"); |
1491 | #endif | |
1492 | } | |
1493 | ||
1494 | TEST(run_tests_unprivileged) { | |
1495 | _cleanup_strv_free_ char **filters = NULL; | |
1496 | ||
1497 | if (!have_namespaces()) | |
1498 | return (void) log_tests_skipped("unshare() is disabled"); | |
1499 | ||
1500 | /* safe_fork() clears saved_argv in the child process. Let's copy it. */ | |
1501 | assert_se(filters = strv_copy(strv_skip(saved_argv, 1))); | |
1502 | ||
1503 | if (prepare_ns("(test-execute-unprivileged)") == 0) { | |
1504 | assert_se(capability_bounding_set_drop(0, /* right_now = */ true) >= 0); | |
1505 | ||
1506 | can_unshare = false; | |
4870133b | 1507 | run_tests(RUNTIME_SCOPE_USER, filters); |
4e032f65 YW |
1508 | _exit(EXIT_SUCCESS); |
1509 | } | |
1510 | } | |
1511 | ||
1512 | static int intro(void) { | |
1513 | #if HAS_FEATURE_ADDRESS_SANITIZER | |
1514 | if (strstr_ptr(ci_environment(), "travis") || strstr_ptr(ci_environment(), "github-actions")) | |
1515 | return log_tests_skipped("Running on Travis CI/GH Actions under ASan, see https://github.com/systemd/systemd/issues/10696"); | |
b7172f34 | 1516 | #endif |
4e032f65 YW |
1517 | /* It is needed otherwise cgroup creation fails */ |
1518 | if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0) | |
1519 | return log_tests_skipped("not privileged"); | |
1520 | ||
1521 | if (enter_cgroup_subroot(NULL) == -ENOMEDIUM) | |
1522 | return log_tests_skipped("cgroupfs not available"); | |
1523 | ||
1524 | if (path_is_read_only_fs("/sys") > 0) | |
1525 | return log_tests_skipped("/sys is mounted read-only"); | |
1526 | ||
1527 | /* Create dummy network interface for testing PrivateNetwork=yes */ | |
8a87a16b | 1528 | have_net_dummy = system("ip link add dummy-test-exec type dummy") == 0; |
4e032f65 | 1529 | |
8a87a16b MK |
1530 | if (have_net_dummy) { |
1531 | /* Create a network namespace and a dummy interface in it for NetworkNamespacePath= */ | |
76808638 NR |
1532 | have_netns = system("ip netns add test-execute-netns") == 0; |
1533 | have_netns = have_netns && system("ip netns exec test-execute-netns ip link add dummy-test-ns type dummy") == 0; | |
8a87a16b | 1534 | } |
600ed5c2 | 1535 | |
4e032f65 | 1536 | return EXIT_SUCCESS; |
281e05b6 | 1537 | } |
4e032f65 YW |
1538 | |
1539 | static int outro(void) { | |
8a87a16b MK |
1540 | if (have_net_dummy) { |
1541 | (void) system("ip link del dummy-test-exec"); | |
1542 | (void) system("ip netns del test-execute-netns"); | |
1543 | } | |
1544 | ||
4e032f65 YW |
1545 | (void) rmdir(PRIVATE_UNIT_DIR); |
1546 | ||
1547 | return EXIT_SUCCESS; | |
1548 | } | |
1549 | ||
1550 | DEFINE_TEST_MAIN_FULL(LOG_DEBUG, intro, outro); |