]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-execute.c
Merge pull request #5131 from keszybz/environment-generators
[thirdparty/systemd.git] / src / test / test-execute.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Ronny Chevalier
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <grp.h>
21 #include <pwd.h>
22 #include <stdio.h>
23 #include <sys/prctl.h>
24 #include <sys/types.h>
25
26 #include "fileio.h"
27 #include "fs-util.h"
28 #include "macro.h"
29 #include "manager.h"
30 #include "mkdir.h"
31 #include "path-util.h"
32 #include "rm-rf.h"
33 #ifdef HAVE_SECCOMP
34 #include "seccomp-util.h"
35 #endif
36 #include "stat-util.h"
37 #include "test-helper.h"
38 #include "tests.h"
39 #include "unit.h"
40 #include "util.h"
41 #include "virt.h"
42
43 typedef void (*test_function_t)(Manager *m);
44
45 static void check(Manager *m, Unit *unit, int status_expected, int code_expected) {
46 Service *service = NULL;
47 usec_t ts;
48 usec_t timeout = 2 * USEC_PER_SEC;
49
50 assert_se(m);
51 assert_se(unit);
52
53 service = SERVICE(unit);
54 printf("%s\n", unit->id);
55 exec_context_dump(&service->exec_context, stdout, "\t");
56 ts = now(CLOCK_MONOTONIC);
57 while (service->state != SERVICE_DEAD && service->state != SERVICE_FAILED) {
58 int r;
59 usec_t n;
60
61 r = sd_event_run(m->event, 100 * USEC_PER_MSEC);
62 assert_se(r >= 0);
63
64 n = now(CLOCK_MONOTONIC);
65 if (ts + timeout < n) {
66 log_error("Test timeout when testing %s", unit->id);
67 exit(EXIT_FAILURE);
68 }
69 }
70 exec_status_dump(&service->main_exec_status, stdout, "\t");
71 assert_se(service->main_exec_status.status == status_expected);
72 assert_se(service->main_exec_status.code == code_expected);
73 }
74
75 static bool is_inaccessible_available(void) {
76 char *p;
77
78 FOREACH_STRING(p,
79 "/run/systemd/inaccessible/reg",
80 "/run/systemd/inaccessible/dir",
81 "/run/systemd/inaccessible/chr",
82 "/run/systemd/inaccessible/blk",
83 "/run/systemd/inaccessible/fifo",
84 "/run/systemd/inaccessible/sock"
85 ) {
86 if (access(p, F_OK) < 0)
87 return false;
88 }
89
90 return true;
91 }
92
93 static void test(Manager *m, const char *unit_name, int status_expected, int code_expected) {
94 Unit *unit;
95
96 assert_se(unit_name);
97
98 assert_se(manager_load_unit(m, unit_name, NULL, NULL, &unit) >= 0);
99 assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
100 check(m, unit, status_expected, code_expected);
101 }
102
103 static void test_exec_workingdirectory(Manager *m) {
104 assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0);
105
106 test(m, "exec-workingdirectory.service", 0, CLD_EXITED);
107
108 (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT|REMOVE_PHYSICAL);
109 }
110
111 static void test_exec_personality(Manager *m) {
112 #if defined(__x86_64__)
113 test(m, "exec-personality-x86-64.service", 0, CLD_EXITED);
114
115 #elif defined(__s390__)
116 test(m, "exec-personality-s390.service", 0, CLD_EXITED);
117
118 #elif defined(__powerpc64__)
119 # if __BYTE_ORDER == __BIG_ENDIAN
120 test(m, "exec-personality-ppc64.service", 0, CLD_EXITED);
121 # else
122 test(m, "exec-personality-ppc64le.service", 0, CLD_EXITED);
123 # endif
124
125 #elif defined(__aarch64__)
126 test(m, "exec-personality-aarch64.service", 0, CLD_EXITED);
127
128 #elif defined(__i386__)
129 test(m, "exec-personality-x86.service", 0, CLD_EXITED);
130 #endif
131 }
132
133 static void test_exec_ignoresigpipe(Manager *m) {
134 test(m, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED);
135 test(m, "exec-ignoresigpipe-no.service", SIGPIPE, CLD_KILLED);
136 }
137
138 static void test_exec_privatetmp(Manager *m) {
139 assert_se(touch("/tmp/test-exec_privatetmp") >= 0);
140
141 test(m, "exec-privatetmp-yes.service", 0, CLD_EXITED);
142 test(m, "exec-privatetmp-no.service", 0, CLD_EXITED);
143
144 unlink("/tmp/test-exec_privatetmp");
145 }
146
147 static void test_exec_privatedevices(Manager *m) {
148 if (detect_container() > 0) {
149 log_notice("testing in container, skipping %s", __func__);
150 return;
151 }
152 if (!is_inaccessible_available()) {
153 log_notice("testing without inaccessible, skipping %s", __func__);
154 return;
155 }
156
157 test(m, "exec-privatedevices-yes.service", 0, CLD_EXITED);
158 test(m, "exec-privatedevices-no.service", 0, CLD_EXITED);
159 }
160
161 static void test_exec_privatedevices_capabilities(Manager *m) {
162 int r;
163
164 if (detect_container() > 0) {
165 log_notice("testing in container, skipping %s", __func__);
166 return;
167 }
168 if (!is_inaccessible_available()) {
169 log_notice("testing without inaccessible, skipping %s", __func__);
170 return;
171 }
172
173 /* We use capsh to test if the capabilities are
174 * properly set, so be sure that it exists */
175 r = find_binary("capsh", NULL);
176 if (r < 0) {
177 log_error_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
178 return;
179 }
180
181 test(m, "exec-privatedevices-yes-capability-mknod.service", 0, CLD_EXITED);
182 test(m, "exec-privatedevices-no-capability-mknod.service", 0, CLD_EXITED);
183 test(m, "exec-privatedevices-yes-capability-sys-rawio.service", 0, CLD_EXITED);
184 test(m, "exec-privatedevices-no-capability-sys-rawio.service", 0, CLD_EXITED);
185 }
186
187 static void test_exec_protectkernelmodules(Manager *m) {
188 int r;
189
190 if (detect_container() > 0) {
191 log_notice("testing in container, skipping %s", __func__);
192 return;
193 }
194 if (!is_inaccessible_available()) {
195 log_notice("testing without inaccessible, skipping %s", __func__);
196 return;
197 }
198
199 r = find_binary("capsh", NULL);
200 if (r < 0) {
201 log_error_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
202 return;
203 }
204
205
206 test(m, "exec-protectkernelmodules-no-capabilities.service", 0, CLD_EXITED);
207 test(m, "exec-protectkernelmodules-yes-capabilities.service", 0, CLD_EXITED);
208 test(m, "exec-protectkernelmodules-yes-mount-propagation.service", 0, CLD_EXITED);
209 }
210
211 static void test_exec_readonlypaths(Manager *m) {
212
213 if (path_is_read_only_fs("/var") > 0)
214 return;
215
216 test(m, "exec-readonlypaths.service", 0, CLD_EXITED);
217 test(m, "exec-readonlypaths-mount-propagation.service", 0, CLD_EXITED);
218 }
219
220 static void test_exec_readwritepaths(Manager *m) {
221
222 if (path_is_read_only_fs("/") > 0)
223 return;
224
225 test(m, "exec-readwritepaths-mount-propagation.service", 0, CLD_EXITED);
226 }
227
228 static void test_exec_inaccessiblepaths(Manager *m) {
229
230 if (path_is_read_only_fs("/") > 0)
231 return;
232
233 test(m, "exec-inaccessiblepaths-mount-propagation.service", 0, CLD_EXITED);
234 }
235
236 static void test_exec_systemcallfilter(Manager *m) {
237 #ifdef HAVE_SECCOMP
238 if (!is_seccomp_available())
239 return;
240 test(m, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED);
241 test(m, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED);
242 test(m, "exec-systemcallfilter-failing.service", SIGSYS, CLD_KILLED);
243 test(m, "exec-systemcallfilter-failing2.service", SIGSYS, CLD_KILLED);
244
245 #endif
246 }
247
248 static void test_exec_systemcallerrornumber(Manager *m) {
249 #ifdef HAVE_SECCOMP
250 if (is_seccomp_available())
251 test(m, "exec-systemcallerrornumber.service", 1, CLD_EXITED);
252 #endif
253 }
254
255 static void test_exec_restrict_namespaces(Manager *m) {
256 #ifdef HAVE_SECCOMP
257 if (!is_seccomp_available())
258 return;
259
260 test(m, "exec-restrict-namespaces-no.service", 0, CLD_EXITED);
261 test(m, "exec-restrict-namespaces-yes.service", 1, CLD_EXITED);
262 test(m, "exec-restrict-namespaces-mnt.service", 0, CLD_EXITED);
263 test(m, "exec-restrict-namespaces-mnt-blacklist.service", 1, CLD_EXITED);
264 #endif
265 }
266
267 static void test_exec_systemcall_system_mode_with_user(Manager *m) {
268 #ifdef HAVE_SECCOMP
269 if (!is_seccomp_available())
270 return;
271 if (getpwnam("nobody"))
272 test(m, "exec-systemcallfilter-system-user.service", 0, CLD_EXITED);
273 else if (getpwnam("nfsnobody"))
274 test(m, "exec-systemcallfilter-system-user-nfsnobody.service", 0, CLD_EXITED);
275 else
276 log_error_errno(errno, "Skipping %s, could not find nobody/nfsnobody user: %m", __func__);
277 #endif
278 }
279
280 static void test_exec_user(Manager *m) {
281 if (getpwnam("nobody"))
282 test(m, "exec-user.service", 0, CLD_EXITED);
283 else if (getpwnam("nfsnobody"))
284 test(m, "exec-user-nfsnobody.service", 0, CLD_EXITED);
285 else
286 log_error_errno(errno, "Skipping %s, could not find nobody/nfsnobody user: %m", __func__);
287 }
288
289 static void test_exec_group(Manager *m) {
290 if (getgrnam("nobody"))
291 test(m, "exec-group.service", 0, CLD_EXITED);
292 else if (getgrnam("nfsnobody"))
293 test(m, "exec-group-nfsnobody.service", 0, CLD_EXITED);
294 else
295 log_error_errno(errno, "Skipping %s, could not find nobody/nfsnobody group: %m", __func__);
296 }
297
298 static void test_exec_supplementary_groups(Manager *m) {
299 test(m, "exec-supplementarygroups.service", 0, CLD_EXITED);
300 test(m, "exec-supplementarygroups-single-group.service", 0, CLD_EXITED);
301 test(m, "exec-supplementarygroups-single-group-user.service", 0, CLD_EXITED);
302 test(m, "exec-supplementarygroups-multiple-groups-default-group-user.service", 0, CLD_EXITED);
303 test(m, "exec-supplementarygroups-multiple-groups-withgid.service", 0, CLD_EXITED);
304 test(m, "exec-supplementarygroups-multiple-groups-withuid.service", 0, CLD_EXITED);
305 }
306
307 static void test_exec_dynamic_user(Manager *m) {
308 test(m, "exec-dynamicuser-fixeduser.service", 0, CLD_EXITED);
309 test(m, "exec-dynamicuser-fixeduser-one-supplementarygroup.service", 0, CLD_EXITED);
310 test(m, "exec-dynamicuser-supplementarygroups.service", 0, CLD_EXITED);
311 }
312
313 static void test_exec_environment(Manager *m) {
314 test(m, "exec-environment.service", 0, CLD_EXITED);
315 test(m, "exec-environment-multiple.service", 0, CLD_EXITED);
316 test(m, "exec-environment-empty.service", 0, CLD_EXITED);
317 }
318
319 static void test_exec_environmentfile(Manager *m) {
320 static const char e[] =
321 "VAR1='word1 word2'\n"
322 "VAR2=word3 \n"
323 "# comment1\n"
324 "\n"
325 "; comment2\n"
326 " ; # comment3\n"
327 "line without an equal\n"
328 "VAR3='$word 5 6'\n";
329 int r;
330
331 r = write_string_file("/tmp/test-exec_environmentfile.conf", e, WRITE_STRING_FILE_CREATE);
332 assert_se(r == 0);
333
334 test(m, "exec-environmentfile.service", 0, CLD_EXITED);
335
336 unlink("/tmp/test-exec_environmentfile.conf");
337 }
338
339 static void test_exec_passenvironment(Manager *m) {
340 /* test-execute runs under MANAGER_USER which, by default, forwards all
341 * variables present in the environment, but only those that are
342 * present _at the time it is created_!
343 *
344 * So these PassEnvironment checks are still expected to work, since we
345 * are ensuring the variables are not present at manager creation (they
346 * are unset explicitly in main) and are only set here.
347 *
348 * This is still a good approximation of how a test for MANAGER_SYSTEM
349 * would work.
350 */
351 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
352 assert_se(setenv("VAR2", "word3", 1) == 0);
353 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
354 test(m, "exec-passenvironment.service", 0, CLD_EXITED);
355 test(m, "exec-passenvironment-repeated.service", 0, CLD_EXITED);
356 test(m, "exec-passenvironment-empty.service", 0, CLD_EXITED);
357 assert_se(unsetenv("VAR1") == 0);
358 assert_se(unsetenv("VAR2") == 0);
359 assert_se(unsetenv("VAR3") == 0);
360 test(m, "exec-passenvironment-absent.service", 0, CLD_EXITED);
361 }
362
363 static void test_exec_umask(Manager *m) {
364 test(m, "exec-umask-default.service", 0, CLD_EXITED);
365 test(m, "exec-umask-0177.service", 0, CLD_EXITED);
366 }
367
368 static void test_exec_runtimedirectory(Manager *m) {
369 test(m, "exec-runtimedirectory.service", 0, CLD_EXITED);
370 test(m, "exec-runtimedirectory-mode.service", 0, CLD_EXITED);
371 if (getgrnam("nobody"))
372 test(m, "exec-runtimedirectory-owner.service", 0, CLD_EXITED);
373 else if (getgrnam("nfsnobody"))
374 test(m, "exec-runtimedirectory-owner-nfsnobody.service", 0, CLD_EXITED);
375 else
376 log_error_errno(errno, "Skipping %s, could not find nobody/nfsnobody group: %m", __func__);
377 }
378
379 static void test_exec_capabilityboundingset(Manager *m) {
380 int r;
381
382 r = find_binary("capsh", NULL);
383 if (r < 0) {
384 log_error_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
385 return;
386 }
387
388 test(m, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED);
389 test(m, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED);
390 test(m, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED);
391 test(m, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED);
392 }
393
394 static void test_exec_capabilityambientset(Manager *m) {
395 int r;
396
397 /* Check if the kernel has support for ambient capabilities. Run
398 * the tests only if that's the case. Clearing all ambient
399 * capabilities is fine, since we are expecting them to be unset
400 * in the first place for the tests. */
401 r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
402 if (r >= 0 || errno != EINVAL) {
403 if (getpwnam("nobody")) {
404 test(m, "exec-capabilityambientset.service", 0, CLD_EXITED);
405 test(m, "exec-capabilityambientset-merge.service", 0, CLD_EXITED);
406 } else if (getpwnam("nfsnobody")) {
407 test(m, "exec-capabilityambientset-nfsnobody.service", 0, CLD_EXITED);
408 test(m, "exec-capabilityambientset-merge-nfsnobody.service", 0, CLD_EXITED);
409 } else
410 log_error_errno(errno, "Skipping %s, could not find nobody/nfsnobody user: %m", __func__);
411 } else
412 log_error_errno(errno, "Skipping %s, the kernel does not support ambient capabilities: %m", __func__);
413 }
414
415 static void test_exec_privatenetwork(Manager *m) {
416 int r;
417
418 r = find_binary("ip", NULL);
419 if (r < 0) {
420 log_error_errno(r, "Skipping %s, could not find ip binary: %m", __func__);
421 return;
422 }
423
424 test(m, "exec-privatenetwork-yes.service", 0, CLD_EXITED);
425 }
426
427 static void test_exec_oomscoreadjust(Manager *m) {
428 test(m, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED);
429 test(m, "exec-oomscoreadjust-negative.service", 0, CLD_EXITED);
430 }
431
432 static void test_exec_ioschedulingclass(Manager *m) {
433 test(m, "exec-ioschedulingclass-none.service", 0, CLD_EXITED);
434 test(m, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED);
435 test(m, "exec-ioschedulingclass-realtime.service", 0, CLD_EXITED);
436 test(m, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED);
437 }
438
439 static void test_exec_spec_interpolation(Manager *m) {
440 test(m, "exec-spec-interpolation.service", 0, CLD_EXITED);
441 }
442
443 static void test_exec_read_only_path_suceed(Manager *m) {
444 test(m, "exec-read-only-path-succeed.service", 0, CLD_EXITED);
445 }
446
447 static int run_tests(UnitFileScope scope, const test_function_t *tests) {
448 const test_function_t *test = NULL;
449 Manager *m = NULL;
450 int r;
451
452 assert_se(tests);
453
454 r = manager_new(scope, true, &m);
455 if (MANAGER_SKIP_TEST(r)) {
456 log_notice_errno(r, "Skipping test: manager_new: %m");
457 return EXIT_TEST_SKIP;
458 }
459 assert_se(r >= 0);
460 assert_se(manager_startup(m, NULL, NULL) >= 0);
461
462 for (test = tests; test && *test; test++)
463 (*test)(m);
464
465 manager_free(m);
466
467 return 0;
468 }
469
470 int main(int argc, char *argv[]) {
471 static const test_function_t user_tests[] = {
472 test_exec_workingdirectory,
473 test_exec_personality,
474 test_exec_ignoresigpipe,
475 test_exec_privatetmp,
476 test_exec_privatedevices,
477 test_exec_privatedevices_capabilities,
478 test_exec_protectkernelmodules,
479 test_exec_readonlypaths,
480 test_exec_readwritepaths,
481 test_exec_inaccessiblepaths,
482 test_exec_privatenetwork,
483 test_exec_systemcallfilter,
484 test_exec_systemcallerrornumber,
485 test_exec_restrict_namespaces,
486 test_exec_user,
487 test_exec_group,
488 test_exec_supplementary_groups,
489 test_exec_dynamic_user,
490 test_exec_environment,
491 test_exec_environmentfile,
492 test_exec_passenvironment,
493 test_exec_umask,
494 test_exec_runtimedirectory,
495 test_exec_capabilityboundingset,
496 test_exec_capabilityambientset,
497 test_exec_oomscoreadjust,
498 test_exec_ioschedulingclass,
499 test_exec_spec_interpolation,
500 test_exec_read_only_path_suceed,
501 NULL,
502 };
503 static const test_function_t system_tests[] = {
504 test_exec_systemcall_system_mode_with_user,
505 NULL,
506 };
507 int r;
508
509 log_set_max_level(LOG_DEBUG);
510 log_parse_environment();
511 log_open();
512
513 /* It is needed otherwise cgroup creation fails */
514 if (getuid() != 0) {
515 printf("Skipping test: not root\n");
516 return EXIT_TEST_SKIP;
517 }
518
519 assert_se(setenv("XDG_RUNTIME_DIR", "/tmp/", 1) == 0);
520 assert_se(set_unit_path(get_testdata_dir("/test-execute")) >= 0);
521
522 /* Unset VAR1, VAR2 and VAR3 which are used in the PassEnvironment test
523 * cases, otherwise (and if they are present in the environment),
524 * `manager_default_environment` will copy them into the default
525 * environment which is passed to each created job, which will make the
526 * tests that expect those not to be present to fail.
527 */
528 assert_se(unsetenv("VAR1") == 0);
529 assert_se(unsetenv("VAR2") == 0);
530 assert_se(unsetenv("VAR3") == 0);
531
532 r = run_tests(UNIT_FILE_USER, user_tests);
533 if (r != 0)
534 return r;
535
536 return run_tests(UNIT_FILE_SYSTEM, system_tests);
537 }