• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 20669300594

02 Jan 2026 09:00PM UTC coverage: 72.677% (-0.02%) from 72.697%
20669300594

push

github

web-flow
clang-tidy: Enable more warnings (#39910)

25 of 27 new or added lines in 3 files covered. (92.59%)

5655 existing lines in 111 files now uncovered.

310023 of 426578 relevant lines covered (72.68%)

1136999.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

1.46
/src/test/test-execute.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fnmatch.h>
4
#include <gnu/libc-version.h>
5
#include <stdio.h>
6
#include <stdlib.h>
7
#include <sys/mount.h>
8
#include <sys/prctl.h>
9
#include <unistd.h>
10

11
#include "sd-event.h"
12

13
#include "argv-util.h"
14
#include "build-path.h"
15
#include "capability-util.h"
16
#include "copy.h"
17
#include "cpu-set-util.h"
18
#include "dropin.h"
19
#include "errno-list.h"
20
#include "event-util.h"
21
#include "extract-word.h"
22
#include "fd-util.h"
23
#include "fileio.h"
24
#include "fs-util.h"
25
#include "libmount-util.h"
26
#include "manager.h"
27
#include "mkdir.h"
28
#include "mount-util.h"
29
#include "path-util.h"
30
#include "process-util.h"
31
#include "rm-rf.h"
32
#include "seccomp-util.h"
33
#include "service.h"
34
#include "signal-util.h"
35
#include "stat-util.h"
36
#include "static-destruct.h"
37
#include "strv.h"
38
#include "sysctl-util.h"
39
#include "tests.h"
40
#include "unit.h"
41
#include "user-util.h"
42
#include "virt.h"
43

44
#define PRIVATE_UNIT_DIR "/run/test-execute-unit-dir"
45

46
static char *user_runtime_unit_dir = NULL;
47
static bool can_unshare;
48
static bool have_net_dummy;
49
static bool have_netns;
50
static unsigned n_ran_tests = 0;
51

52
STATIC_DESTRUCTOR_REGISTER(user_runtime_unit_dir, freep);
1✔
53

54
typedef void (*test_function_t)(Manager *m);
55

UNCOV
56
static int cld_dumped_to_killed(int code) {
×
57
        /* Depending on the system, seccomp version, … some signals might result in dumping, others in plain
58
         * killing. Let's ignore the difference here, and map both cases to CLD_KILLED */
UNCOV
59
        return code == CLD_DUMPED ? CLD_KILLED : code;
×
60
}
61

62
_noreturn_
63
static int time_handler(sd_event_source *s, uint64_t usec, void *userdata) {
×
64
        Unit *unit = ASSERT_PTR(userdata);
×
UNCOV
65
        int r;
×
66

67
        log_error("Test timeout when testing %s", unit->id);
×
68
        r = unit_kill(unit, KILL_ALL, /* subgroup= */ NULL, SIGKILL, SI_USER, /* value= */ 0, /* ret_error= */ NULL);
×
69
        if (r < 0)
×
UNCOV
70
                log_error_errno(r, "Failed to kill %s, ignoring: %m", unit->id);
×
71

UNCOV
72
        abort();
×
73
}
74

75
static void wait_for_service_finish(Manager *m, Unit *unit) {
×
76
        Service *service = SERVICE(ASSERT_PTR(unit));
×
UNCOV
77
        usec_t timeout = 2 * USEC_PER_MINUTE;
×
78

UNCOV
79
        ASSERT_NOT_NULL(m);
×
80

81
        /* Bump the timeout when running in plain QEMU, as some more involved tests might start hitting the
82
         * default 2m timeout (like exec-dynamicuser-statedir.service) */
83
        if (detect_virtualization() == VIRTUALIZATION_QEMU)
×
UNCOV
84
                timeout *= 2;
×
85

86
        printf("%s\n", unit->id);
×
UNCOV
87
        exec_context_dump(&service->exec_context, stdout, "\t");
×
88

89
        _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
×
UNCOV
90
        ASSERT_OK(sd_event_add_time_relative(m->event, &s, CLOCK_MONOTONIC, timeout, 0, time_handler, unit));
×
91

92
        /* Here, sd_event_loop() cannot be used, as the sd_event object will be reused in the next test case. */
93
        while (!IN_SET(service->state, SERVICE_DEAD, SERVICE_FAILED))
×
94
                ASSERT_OK(sd_event_run(m->event, 100 * USEC_PER_MSEC));
×
UNCOV
95
}
×
96

UNCOV
97
static void check_main_result(const char *file, unsigned line, const char *func,
×
98
                              Manager *m, Unit *unit, int status_expected, int code_expected) {
UNCOV
99
        Service *service = NULL;
×
100

101
        ASSERT_NOT_NULL(m);
×
UNCOV
102
        ASSERT_NOT_NULL(unit);
×
103

UNCOV
104
        wait_for_service_finish(m, unit);
×
105

106
        service = SERVICE(unit);
×
UNCOV
107
        exec_status_dump(&service->main_exec_status, stdout, "\t");
×
108

109
        if (cld_dumped_to_killed(service->main_exec_status.code) != cld_dumped_to_killed(code_expected)) {
×
UNCOV
110
                log_error("%s:%u:%s %s: can_unshare=%s: exit code %d, expected %d",
×
111
                          file, line, func, unit->id, yes_no(can_unshare),
112
                          service->main_exec_status.code, code_expected);
UNCOV
113
                abort();
×
114
        }
115

116
        if (service->main_exec_status.status != status_expected) {
×
UNCOV
117
                log_error("%s:%u:%s: %s: can_unshare=%s: exit status %d, expected %d",
×
118
                          file, line, func, unit->id, yes_no(can_unshare),
119
                          service->main_exec_status.status, status_expected);
UNCOV
120
                abort();
×
121
        }
UNCOV
122
}
×
123

UNCOV
124
static void check_service_result(const char *file, unsigned line, const char *func,
×
125
                                 Manager *m, Unit *unit, ServiceResult result_expected) {
UNCOV
126
        Service *service = NULL;
×
127

128
        ASSERT_NOT_NULL(m);
×
UNCOV
129
        ASSERT_NOT_NULL(unit);
×
130

UNCOV
131
        wait_for_service_finish(m, unit);
×
132

UNCOV
133
        service = SERVICE(unit);
×
134

135
        if (service->result != result_expected) {
×
UNCOV
136
                log_error("%s:%u:%s: %s: can_unshare=%s: service end result %s, expected %s",
×
137
                          file, line, func, unit->id, yes_no(can_unshare),
138
                          service_result_to_string(service->result),
139
                          service_result_to_string(result_expected));
UNCOV
140
                abort();
×
141
        }
UNCOV
142
}
×
143

144
static bool check_nobody_user_and_group(void) {
×
145
        static int cache = -1;
×
146
        struct passwd *p;
×
UNCOV
147
        struct group *g;
×
148

149
        if (cache >= 0)
×
UNCOV
150
                return !!cache;
×
151

152
        if (!synthesize_nobody())
×
UNCOV
153
                goto invalid;
×
154

155
        p = getpwnam(NOBODY_USER_NAME);
×
156
        if (!p ||
×
157
            !streq(p->pw_name, NOBODY_USER_NAME) ||
×
158
            p->pw_uid != UID_NOBODY ||
×
159
            p->pw_gid != GID_NOBODY)
×
UNCOV
160
                goto invalid;
×
161

162
        p = getpwuid(UID_NOBODY);
×
163
        if (!p ||
×
164
            !streq(p->pw_name, NOBODY_USER_NAME) ||
×
165
            p->pw_uid != UID_NOBODY ||
×
166
            p->pw_gid != GID_NOBODY)
×
UNCOV
167
                goto invalid;
×
168

169
        g = getgrnam(NOBODY_GROUP_NAME);
×
170
        if (!g ||
×
171
            !streq(g->gr_name, NOBODY_GROUP_NAME) ||
×
172
            g->gr_gid != GID_NOBODY)
×
UNCOV
173
                goto invalid;
×
174

175
        g = getgrgid(GID_NOBODY);
×
176
        if (!g ||
×
177
            !streq(g->gr_name, NOBODY_GROUP_NAME) ||
×
178
            g->gr_gid != GID_NOBODY)
×
UNCOV
179
                goto invalid;
×
180

181
        cache = 1;
×
UNCOV
182
        return true;
×
183

184
invalid:
×
185
        cache = 0;
×
UNCOV
186
        return false;
×
187
}
188

189
static bool check_user_has_group_with_same_name(const char *name) {
×
190
        struct passwd *p;
×
UNCOV
191
        struct group *g;
×
192

UNCOV
193
        ASSERT_NOT_NULL(name);
×
194

195
        p = getpwnam(name);
×
196
        if (!p ||
×
UNCOV
197
            !streq(p->pw_name, name))
×
198
                return false;
199

200
        g = getgrgid(p->pw_gid);
×
201
        if (!g ||
×
202
            !streq(g->gr_name, name))
×
UNCOV
203
                return false;
×
204

205
        return true;
206
}
207

208
static bool is_inaccessible_available(void) {
×
UNCOV
209
        FOREACH_STRING(p,
×
210
                       "/run/systemd/inaccessible/reg",
211
                       "/run/systemd/inaccessible/dir",
212
                       "/run/systemd/inaccessible/chr",
213
                       "/run/systemd/inaccessible/blk",
214
                       "/run/systemd/inaccessible/fifo",
215
                       "/run/systemd/inaccessible/sock")
216
                if (access(p, F_OK) < 0)
×
UNCOV
217
                        return false;
×
218

UNCOV
219
        return true;
×
220
}
221

222
static void start_parent_slices(Unit *unit) {
×
UNCOV
223
        Unit *slice;
×
224

225
        slice = UNIT_GET_SLICE(unit);
×
226
        if (slice) {
×
227
                start_parent_slices(slice);
×
UNCOV
228
                ASSERT_OK_OR(unit_start(slice, NULL), -EALREADY);
×
229
        }
UNCOV
230
}
×
231

232
static bool apparmor_restrict_unprivileged_userns(void) {
×
233
        _cleanup_free_ char *v = NULL;
×
UNCOV
234
        int r;
×
235

236
        /* If kernel.apparmor_restrict_unprivileged_userns=1, then we cannot
237
         * use unprivileged user namespaces. */
238
        r = sysctl_read("kernel/apparmor_restrict_unprivileged_userns", &v);
×
239
        if (r < 0) {
×
240
                if (r != -ENOENT)
×
UNCOV
241
                        log_debug_errno(r, "Failed to read kernel.apparmor_restrict_unprivileged_userns sysctl, ignoring: %m");
×
242

UNCOV
243
                return false;
×
244
        }
245

UNCOV
246
        return streq(v, "1");
×
247
}
248

249
static bool have_userns_privileges(void) {
×
250
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
×
UNCOV
251
        int r;
×
252

UNCOV
253
        if (apparmor_restrict_unprivileged_userns())
×
254
                return false;
255

UNCOV
256
        r = ASSERT_OK(pidref_safe_fork(
×
257
                        "(sd-test-check-userns)",
258
                        FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGKILL,
259
                        &pidref));
260

UNCOV
261
        if (r == 0) {
×
262
                /* Keep CAP_SYS_ADMIN if we have it to ensure we give an
263
                 * accurate result to the caller. Some kernels have a
264
                 * kernel.unprivileged_userns_clone sysctl which can be
265
                 * configured to make CLONE_NEWUSER require CAP_SYS_ADMIN.
266
                 * Additionally, AppArmor may restrict unprivileged user
267
                 * namespace creation. */
268
                r = capability_bounding_set_drop(UINT64_C(1) << CAP_SYS_ADMIN, /* right_now= */ true);
×
269
                if (r < 0) {
×
270
                        log_debug_errno(r, "Failed to drop capabilities: %m");
×
UNCOV
271
                        _exit(2);
×
272
                }
273

274
                r = RET_NERRNO(unshare(CLONE_NEWUSER));
×
275
                if (r < 0 && !ERRNO_IS_NEG_PRIVILEGE(r))
×
UNCOV
276
                        log_debug_errno(r, "Failed to create user namespace: %m");
×
277

UNCOV
278
                _exit(r >= 0 ? EXIT_SUCCESS : ERRNO_IS_NEG_PRIVILEGE(r) ? EXIT_FAILURE : 2);
×
279
        }
280

281
        /* The exit code records the result of the check:
282
         *  EXIT_SUCCESS => we can use user namespaces
283
         *  EXIT_FAILURE => we can NOT use user namespaces
284
         *  2            => some other error occurred */
285
        r = pidref_wait_for_terminate_and_check("(sd-test-check-userns)", &pidref, 0);
×
286
        if (!IN_SET(r, EXIT_SUCCESS, EXIT_FAILURE))
×
UNCOV
287
                log_debug("Failed to check if user namespaces can be used, assuming not.");
×
288

UNCOV
289
        return r == EXIT_SUCCESS;
×
290
}
291

UNCOV
292
static void _test(const char *file, unsigned line, const char *func,
×
293
                  Manager *m, const char *unit_name, int status_expected, int code_expected) {
UNCOV
294
        Unit *unit;
×
295

UNCOV
296
        ASSERT_NOT_NULL(unit_name);
×
297

UNCOV
298
        ASSERT_OK(manager_load_startable_unit_or_warn(m, unit_name, NULL, &unit));
×
299
        /* We need to start the slices as well otherwise the slice cgroups might be pruned
300
         * in on_cgroup_empty_event. */
301
        start_parent_slices(unit);
×
302
        ASSERT_OK(unit_start(unit, NULL));
×
UNCOV
303
        check_main_result(file, line, func, m, unit, status_expected, code_expected);
×
304

305
        ++n_ran_tests;
×
UNCOV
306
}
×
307
#define test(m, unit_name, status_expected, code_expected) \
308
        _test(PROJECT_FILE, __LINE__, __func__, m, unit_name, status_expected, code_expected)
309

UNCOV
310
static void _test_service(const char *file, unsigned line, const char *func,
×
311
                          Manager *m, const char *unit_name, ServiceResult result_expected) {
UNCOV
312
        Unit *unit;
×
313

UNCOV
314
        ASSERT_NOT_NULL(unit_name);
×
315

316
        ASSERT_OK(manager_load_startable_unit_or_warn(m, unit_name, NULL, &unit));
×
317
        ASSERT_OK(unit_start(unit, NULL));
×
318
        check_service_result(file, line, func, m, unit, result_expected);
×
UNCOV
319
}
×
320
#define test_service(m, unit_name, result_expected) \
321
        _test_service(PROJECT_FILE, __LINE__, __func__, m, unit_name, result_expected)
322

323
static void test_exec_bindpaths(Manager *m) {
×
324
        ASSERT_OK(mkdir_p("/tmp/test-exec-bindpaths", 0755));
×
UNCOV
325
        ASSERT_OK(mkdir_p("/tmp/test-exec-bindreadonlypaths", 0755));
×
326

UNCOV
327
        test(m, "exec-bindpaths.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
328

329
        (void) rm_rf("/tmp/test-exec-bindpaths", REMOVE_ROOT|REMOVE_PHYSICAL);
×
330
        (void) rm_rf("/tmp/test-exec-bindreadonlypaths", REMOVE_ROOT|REMOVE_PHYSICAL);
×
UNCOV
331
}
×
332

333
static void test_exec_cpuaffinity(Manager *m) {
×
UNCOV
334
        _cleanup_(cpu_set_done) CPUSet c = {};
×
335

336
        ASSERT_OK(cpu_set_realloc(&c, 8192)); /* just allocate the maximum possible size */
×
UNCOV
337
        ASSERT_OK_ERRNO(sched_getaffinity(0, c.allocated, c.set));
×
338

339
        if (!CPU_ISSET_S(0, c.allocated, c.set)) {
×
340
                log_notice("Cannot use CPU 0, skipping %s", __func__);
×
UNCOV
341
                return;
×
342
        }
343

344
        test(m, "exec-cpuaffinity1.service", 0, CLD_EXITED);
×
UNCOV
345
        test(m, "exec-cpuaffinity2.service", 0, CLD_EXITED);
×
346

347
        if (!CPU_ISSET_S(1, c.allocated, c.set) ||
×
348
            !CPU_ISSET_S(2, c.allocated, c.set)) {
×
349
                log_notice("Cannot use CPU 1 or 2, skipping remaining tests in %s", __func__);
×
UNCOV
350
                return;
×
351
        }
352

UNCOV
353
        test(m, "exec-cpuaffinity3.service", 0, CLD_EXITED);
×
354
}
355

356
static void test_exec_credentials(Manager *m) {
×
357
        test(m, "exec-set-credential.service", 0, CLD_EXITED);
×
358
        test(m, "exec-load-credential.service", 0, CLD_EXITED);
×
359
        test(m, "exec-credentials-dir-specifier.service", 0, CLD_EXITED);
×
UNCOV
360
}
×
361

362
static void test_exec_workingdirectory(Manager *m) {
×
UNCOV
363
        ASSERT_OK(mkdir_p("/tmp/test-exec_workingdirectory", 0755));
×
364

365
        test(m, "exec-workingdirectory.service", 0, CLD_EXITED);
×
UNCOV
366
        test(m, "exec-workingdirectory-trailing-dot.service", 0, CLD_EXITED);
×
367

368
        (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT|REMOVE_PHYSICAL);
×
UNCOV
369
}
×
370

371
static void test_exec_execsearchpath(Manager *m) {
×
UNCOV
372
        int r;
×
373

374
        ASSERT_OK(r = is_symlink("/bin/ls"));
×
375
        if (r > 0)
×
UNCOV
376
                return (void) log_tests_skipped("/bin/ls is a symlink, maybe coreutils is built with --enable-single-binary=symlinks");
×
377

UNCOV
378
        ASSERT_OK(mkdir_p("/tmp/test-exec_execsearchpath", 0755));
×
379

UNCOV
380
        ASSERT_OK(copy_file("/bin/ls", "/tmp/test-exec_execsearchpath/ls_temp", 0,  0777, COPY_REPLACE));
×
381

UNCOV
382
        test(m, "exec-execsearchpath.service", 0, CLD_EXITED);
×
383

UNCOV
384
        ASSERT_OK(rm_rf("/tmp/test-exec_execsearchpath", REMOVE_ROOT|REMOVE_PHYSICAL));
×
385

UNCOV
386
        test(m, "exec-execsearchpath.service", EXIT_EXEC, CLD_EXITED);
×
387
}
388

389
static void test_exec_execsearchpath_specifier(Manager *m) {
×
390
        test(m, "exec-execsearchpath-unit-specifier.service", 0, CLD_EXITED);
×
UNCOV
391
}
×
392

393
static void test_exec_execsearchpath_environment(Manager *m) {
×
394
        test(m, "exec-execsearchpath-environment.service", 0, CLD_EXITED);
×
395
        test(m, "exec-execsearchpath-environment-path-set.service", 0, CLD_EXITED);
×
UNCOV
396
}
×
397

398
static void test_exec_execsearchpath_environment_files(Manager *m) {
×
UNCOV
399
        static const char path_not_set[] =
×
400
                "VAR1='word1 word2'\n"
401
                "VAR2=word3 \n"
402
                "# comment1\n"
403
                "\n"
404
                "; comment2\n"
405
                " ; # comment3\n"
406
                "line without an equal\n"
407
                "VAR3='$word 5 6'\n"
408
                "VAR4='new\nline'\n"
409
                "VAR5=password\\with\\backslashes";
410

UNCOV
411
        static const char path_set[] =
×
412
                "VAR1='word1 word2'\n"
413
                "VAR2=word3 \n"
414
                "# comment1\n"
415
                "\n"
416
                "; comment2\n"
417
                " ; # comment3\n"
418
                "line without an equal\n"
419
                "VAR3='$word 5 6'\n"
420
                "VAR4='new\nline'\n"
421
                "VAR5=password\\with\\backslashes\n"
422
                "PATH=/usr";
423

UNCOV
424
        int r;
×
425

426
        r = write_string_file("/tmp/test-exec_execsearchpath_environmentfile.conf", path_not_set, WRITE_STRING_FILE_CREATE);
×
UNCOV
427
        ASSERT_OK(r);
×
428

UNCOV
429
        test(m, "exec-execsearchpath-environmentfile.service", 0, CLD_EXITED);
×
430

UNCOV
431
        (void) unlink("/tmp/test-exec_environmentfile.conf");
×
432

433
        r = write_string_file("/tmp/test-exec_execsearchpath_environmentfile-set.conf", path_set, WRITE_STRING_FILE_CREATE);
×
UNCOV
434
        ASSERT_OK(r);
×
435

UNCOV
436
        test(m, "exec-execsearchpath-environmentfile-set.service", 0, CLD_EXITED);
×
437

438
        (void) unlink("/tmp/test-exec_environmentfile-set.conf");
×
UNCOV
439
}
×
440

441
static void test_exec_execsearchpath_passenvironment(Manager *m) {
×
442
        ASSERT_OK_ERRNO(setenv("VAR1", "word1 word2", 1));
×
443
        ASSERT_OK_ERRNO(setenv("VAR2", "word3", 1));
×
444
        ASSERT_OK_ERRNO(setenv("VAR3", "$word 5 6", 1));
×
445
        ASSERT_OK_ERRNO(setenv("VAR4", "new\nline", 1));
×
UNCOV
446
        ASSERT_OK_ERRNO(setenv("VAR5", "passwordwithbackslashes", 1));
×
447

UNCOV
448
        test(m, "exec-execsearchpath-passenvironment.service", 0, CLD_EXITED);
×
449

450
        ASSERT_OK_ERRNO(setenv("PATH", "/usr", 1));
×
UNCOV
451
        test(m, "exec-execsearchpath-passenvironment-set.service", 0, CLD_EXITED);
×
452

453
        ASSERT_OK_ERRNO(unsetenv("VAR1"));
×
454
        ASSERT_OK_ERRNO(unsetenv("VAR2"));
×
455
        ASSERT_OK_ERRNO(unsetenv("VAR3"));
×
456
        ASSERT_OK_ERRNO(unsetenv("VAR4"));
×
457
        ASSERT_OK_ERRNO(unsetenv("VAR5"));
×
458
        ASSERT_OK_ERRNO(unsetenv("PATH"));
×
UNCOV
459
}
×
460

UNCOV
461
static void test_exec_personality(Manager *m) {
×
462
#if defined(__x86_64__)
UNCOV
463
        test(m, "exec-personality-x86-64.service", 0, CLD_EXITED);
×
464

465
#elif defined(__s390x__)
466
        test(m, "exec-personality-s390x.service", 0, CLD_EXITED);
467

468
#elif defined(__s390__)
469
        test(m, "exec-personality-s390.service", 0, CLD_EXITED);
470

471
#elif defined(__powerpc64__)
472
#  if __BYTE_ORDER == __BIG_ENDIAN
473
        test(m, "exec-personality-ppc64.service", 0, CLD_EXITED);
474
#  else
475
        test(m, "exec-personality-ppc64le.service", 0, CLD_EXITED);
476
#  endif
477

478
#elif defined(__aarch64__)
479
        test(m, "exec-personality-aarch64.service", 0, CLD_EXITED);
480

481
#elif defined(__i386__)
482
        test(m, "exec-personality-x86.service", 0, CLD_EXITED);
483
#elif defined(__loongarch_lp64)
484
        test(m, "exec-personality-loongarch64.service", 0, CLD_EXITED);
485
#else
486
        log_notice("Unknown personality, skipping %s", __func__);
487
#endif
UNCOV
488
}
×
489

490
static void test_exec_ignoresigpipe(Manager *m) {
×
491
        test(m, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED);
×
492
        test(m, "exec-ignoresigpipe-no.service", SIGPIPE, CLD_KILLED);
×
UNCOV
493
}
×
494

495
static void test_exec_privatetmp(Manager *m) {
×
UNCOV
496
        ASSERT_OK(touch("/tmp/test-exec_privatetmp"));
×
497

498
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) {
×
499
                test(m, "exec-privatetmp-yes.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
500
                test(m, "exec-privatetmp-disabled-by-prefix.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
501

502
                (void) unlink("/tmp/test-exec_privatetmp_disconnected");
×
503
                test(m, "exec-privatetmp-disconnected-nodefaultdeps-nor-sandboxing.service", 0, CLD_EXITED);
×
UNCOV
504
                ASSERT_OK_ERRNO(access("/tmp/test-exec_privatetmp_disconnected", F_OK));
×
505

UNCOV
506
                FOREACH_STRING(s,
×
507
                               "exec-privatetmp-disconnected.service",
508
                               "exec-privatetmp-disconnected-defaultdependencies-no.service",
509
                               "exec-privatetmp-disconnected-requires-mounts-for-var.service",
510
                               "exec-privatetmp-disconnected-wants-mounts-for-var.service",
511
                               "exec-privatetmp-disconnected-after-and-requires-for-var.service",
512
                               "exec-privatetmp-disconnected-after-and-wants-for-var.service") {
513
                        (void) unlink("/tmp/test-exec_privatetmp_disconnected");
×
514
                        (void) unlink("/var/tmp/test-exec_privatetmp_disconnected");
×
515
                        test(m, s, can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
516
                        ASSERT_FAIL(access("/tmp/test-exec_privatetmp_disconnected", F_OK));
×
UNCOV
517
                        ASSERT_FAIL(access("/var/tmp/test-exec_privatetmp_disconnected", F_OK));
×
518
                }
519
        }
520

UNCOV
521
        test(m, "exec-privatetmp-no.service", 0, CLD_EXITED);
×
522

523
        (void) unlink("/tmp/test-exec_privatetmp");
×
UNCOV
524
}
×
525

526
static void test_exec_privatedevices(Manager *m) {
×
UNCOV
527
        int r;
×
528

529
        if (detect_container() > 0) {
×
530
                log_notice("Testing in container, skipping %s", __func__);
×
UNCOV
531
                return;
×
532
        }
533
        if (!is_inaccessible_available()) {
×
534
                log_notice("Testing without inaccessible, skipping %s", __func__);
×
UNCOV
535
                return;
×
536
        }
537

538
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) {
×
539
                test(m, "exec-privatedevices-yes.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
540
                if (access("/dev/kmsg", F_OK) >= 0)
×
541
                        test(m, "exec-privatedevices-bind.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
542
                test(m, "exec-privatedevices-disabled-by-prefix.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
543
                test(m, "exec-privatedevices-yes-with-group.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
544
        }
545

UNCOV
546
        test(m, "exec-privatedevices-no.service", 0, CLD_EXITED);
×
547

548
        /* We use capsh to test if the capabilities are
549
         * properly set, so be sure that it exists */
550
        r = find_executable("capsh", NULL);
×
551
        if (r < 0) {
×
552
                log_notice_errno(r, "Could not find capsh binary, skipping remaining tests in %s: %m", __func__);
×
UNCOV
553
                return;
×
554
        }
555

556
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) {
×
557
                test(m, "exec-privatedevices-yes-capability-mknod.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
558
                test(m, "exec-privatedevices-yes-capability-sys-rawio.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
559
        }
560

561
        test(m, "exec-privatedevices-no-capability-mknod.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED);
×
UNCOV
562
        test(m, "exec-privatedevices-no-capability-sys-rawio.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED);
×
563
}
564

565
static void test_exec_protecthome(Manager *m) {
×
566
        if (!can_unshare) {
×
567
                log_notice("Cannot reliably unshare, skipping %s", __func__);
×
UNCOV
568
                return;
×
569
        }
570

UNCOV
571
        test(m, "exec-protecthome-tmpfs-vs-protectsystem-strict.service", 0, CLD_EXITED);
×
572
}
573

574
static void test_exec_protectkernelmodules(Manager *m) {
×
UNCOV
575
        int r;
×
576

577
        if (detect_container() > 0) {
×
578
                log_notice("Testing in container, skipping %s", __func__);
×
UNCOV
579
                return;
×
580
        }
581
        if (!is_inaccessible_available()) {
×
582
                log_notice("Testing without inaccessible, skipping %s", __func__);
×
UNCOV
583
                return;
×
584
        }
585

586
        r = find_executable("capsh", NULL);
×
587
        if (r < 0) {
×
588
                log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
×
UNCOV
589
                return;
×
590
        }
591

UNCOV
592
        test(m, "exec-protectkernelmodules-no-capabilities.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED);
×
593

594
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) {
×
595
                test(m, "exec-protectkernelmodules-yes-capabilities.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
596
                test(m, "exec-protectkernelmodules-yes-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
597
        }
598
}
599

UNCOV
600
static void test_exec_readonlypaths(Manager *m) {
×
601

602
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges())
×
UNCOV
603
                test(m, "exec-readonlypaths-simple.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
604

605
        if (path_is_read_only_fs("/var") > 0) {
×
606
                log_notice("Directory /var is readonly, skipping remaining tests in %s", __func__);
×
UNCOV
607
                return;
×
608
        }
609

610
        test(m, "exec-readonlypaths.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
611
        test(m, "exec-readonlypaths-with-bindpaths.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
612
        test(m, "exec-readonlypaths-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
613
}
614

UNCOV
615
static void test_exec_readwritepaths(Manager *m) {
×
616

617
        if (path_is_read_only_fs("/") > 0) {
×
618
                log_notice("Root directory is readonly, skipping %s", __func__);
×
UNCOV
619
                return;
×
620
        }
621

UNCOV
622
        test(m, "exec-readwritepaths-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
623
}
624

UNCOV
625
static void test_exec_inaccessiblepaths(Manager *m) {
×
626

627
        if (!is_inaccessible_available()) {
×
628
                log_notice("Testing without inaccessible, skipping %s", __func__);
×
UNCOV
629
                return;
×
630
        }
631

632
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges())
×
UNCOV
633
                test(m, "exec-inaccessiblepaths-sys.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
634

635
        if (path_is_read_only_fs("/") > 0) {
×
636
                log_notice("Root directory is readonly, skipping remaining tests in %s", __func__);
×
UNCOV
637
                return;
×
638
        }
639

UNCOV
640
        test(m, "exec-inaccessiblepaths-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
641
}
642

643
#if !HAS_FEATURE_ADDRESS_SANITIZER
644
static int on_spawn_io(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
×
645
        char **result = userdata;
×
646
        char buf[4096];
×
UNCOV
647
        ssize_t l;
×
648

649
        ASSERT_NOT_NULL(s);
×
UNCOV
650
        ASSERT_GT(fd, 0);
×
651

652
        l = read(fd, buf, sizeof(buf) - 1);
×
653
        if (l < 0) {
×
654
                if (errno == EAGAIN)
×
UNCOV
655
                        goto reenable;
×
656

UNCOV
657
                return 0;
×
658
        }
UNCOV
659
        if (l == 0)
×
660
                return 0;
661

662
        buf[l] = '\0';
×
663
        if (result)
×
UNCOV
664
                ASSERT_NOT_NULL(strextend(result, buf));
×
665
        else
UNCOV
666
                log_error("ldd: %s", buf);
×
667

UNCOV
668
reenable:
×
669
        /* Re-enable the event source if we did not encounter EOF */
UNCOV
670
        ASSERT_OK(sd_event_source_set_enabled(s, SD_EVENT_ONESHOT));
×
671
        return 0;
672
}
673

674
static int on_spawn_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
×
UNCOV
675
        PidRef *pidref = ASSERT_PTR(userdata);
×
676

UNCOV
677
        (void) pidref_kill(pidref, SIGKILL);
×
678

UNCOV
679
        return 1;
×
680
}
681

UNCOV
682
static int on_spawn_exit(sd_event_source *s, const siginfo_t *si, void *userdata) {
×
683
        int ret = -EIO;
×
684

UNCOV
685
        ASSERT_NOT_NULL(si);
×
686

UNCOV
687
        if (si->si_code == CLD_EXITED)
×
688
                ret = si->si_status;
×
689

UNCOV
690
        sd_event_exit(sd_event_source_get_event(s), ret);
×
691
        return 1;
×
692
}
693

UNCOV
694
static int find_libraries(const char *exec, char ***ret) {
×
695
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
×
696
        _cleanup_(sd_event_source_unrefp) sd_event_source *sigchld_source = NULL;
×
697
        _cleanup_(sd_event_source_unrefp) sd_event_source *stdout_source = NULL;
×
698
        _cleanup_(sd_event_source_unrefp) sd_event_source *stderr_source = NULL;
×
699
        _cleanup_close_pair_ int outpipe[2] = EBADF_PAIR, errpipe[2] = EBADF_PAIR;
×
700
        _cleanup_strv_free_ char **libraries = NULL;
×
701
        _cleanup_free_ char *result = NULL;
×
702
        int r;
×
703

704
        ASSERT_NOT_NULL(exec);
×
UNCOV
705
        ASSERT_NOT_NULL(ret);
×
706

707
        ASSERT_OK_ERRNO(pipe2(outpipe, O_NONBLOCK|O_CLOEXEC));
×
UNCOV
708
        ASSERT_OK_ERRNO(pipe2(errpipe, O_NONBLOCK|O_CLOEXEC));
×
709

710
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
×
UNCOV
711
        r = pidref_safe_fork_full(
×
712
                        "(spawn-ldd)",
713
                        (int[]) { -EBADF, outpipe[1], errpipe[1] },
×
714
                        NULL, 0,
715
                        FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REARRANGE_STDIO|FORK_LOG,
716
                        &pidref);
717
        ASSERT_OK(r);
×
718
        if (r == 0) {
×
719
                execlp("ldd", "ldd", exec, NULL);
×
UNCOV
720
                _exit(EXIT_FAILURE);
×
721
        }
722

723
        outpipe[1] = safe_close(outpipe[1]);
×
UNCOV
724
        errpipe[1] = safe_close(errpipe[1]);
×
725

UNCOV
726
        ASSERT_OK(sd_event_new(&e));
×
727

UNCOV
728
        ASSERT_OK(sd_event_add_time_relative(e, NULL, CLOCK_MONOTONIC,
×
729
                                             10 * USEC_PER_SEC, USEC_PER_SEC, on_spawn_timeout, &pidref));
730
        ASSERT_OK(sd_event_add_io(e, &stdout_source, outpipe[0], EPOLLIN, on_spawn_io, &result));
×
731
        ASSERT_OK(sd_event_source_set_enabled(stdout_source, SD_EVENT_ONESHOT));
×
732
        ASSERT_OK(sd_event_add_io(e, &stderr_source, errpipe[0], EPOLLIN, on_spawn_io, NULL));
×
733
        ASSERT_OK(sd_event_source_set_enabled(stderr_source, SD_EVENT_ONESHOT));
×
UNCOV
734
        ASSERT_OK(event_add_child_pidref(e, &sigchld_source, &pidref, WEXITED, on_spawn_exit, NULL));
×
735
        /* Child exit should be processed after IO is complete */
UNCOV
736
        ASSERT_OK(sd_event_source_set_priority(sigchld_source, SD_EVENT_PRIORITY_NORMAL + 1));
×
737

UNCOV
738
        ASSERT_OK(sd_event_loop(e));
×
739

740
        _cleanup_strv_free_ char **v = NULL;
×
UNCOV
741
        ASSERT_OK(strv_split_newlines_full(&v, result, 0));
×
742

743
        STRV_FOREACH(q, v) {
×
744
                _cleanup_free_ char *word = NULL;
×
UNCOV
745
                const char *p = *q;
×
746

747
                r = extract_first_word(&p, &word, NULL, 0);
×
748
                ASSERT_OK(r);
×
749
                if (r == 0)
×
UNCOV
750
                        continue;
×
751

752
                if (path_is_absolute(word)) {
×
753
                        ASSERT_OK(strv_consume(&libraries, TAKE_PTR(word)));
×
UNCOV
754
                        continue;
×
755
                }
756

757
                word = mfree(word);
×
758
                r = extract_first_word(&p, &word, NULL, 0);
×
759
                ASSERT_OK(r);
×
760
                if (r == 0)
×
UNCOV
761
                        continue;
×
762

763
                if (!streq_ptr(word, "=>"))
×
UNCOV
764
                        continue;
×
765

766
                word = mfree(word);
×
767
                r = extract_first_word(&p, &word, NULL, 0);
×
768
                ASSERT_OK(r);
×
769
                if (r == 0)
×
UNCOV
770
                        continue;
×
771

772
                if (path_is_absolute(word)) {
×
773
                        ASSERT_OK(strv_consume(&libraries, TAKE_PTR(word)));
×
UNCOV
774
                        continue;
×
775
                }
776
        }
777

778
        *ret = TAKE_PTR(libraries);
×
UNCOV
779
        return 0;
×
780
}
781
#endif
782

UNCOV
783
static void test_exec_mount_apivfs(Manager *m) {
×
784
#if !HAS_FEATURE_ADDRESS_SANITIZER
785
        _cleanup_free_ char *fullpath_touch = NULL, *fullpath_test = NULL, *data = NULL;
×
786
        _cleanup_strv_free_ char **libraries = NULL, **libraries_test = NULL;
×
UNCOV
787
        int r;
×
788

UNCOV
789
        ASSERT_NOT_NULL(user_runtime_unit_dir);
×
790

791
        r = find_executable("ldd", NULL);
×
792
        if (r < 0) {
×
793
                log_notice_errno(r, "Skipping %s, could not find 'ldd' command: %m", __func__);
×
UNCOV
794
                return;
×
795
        }
796
        r = find_executable("touch", &fullpath_touch);
×
797
        if (r < 0) {
×
798
                log_notice_errno(r, "Skipping %s, could not find 'touch' command: %m", __func__);
×
UNCOV
799
                return;
×
800
        }
801
        r = find_executable("test", &fullpath_test);
×
802
        if (r < 0) {
×
803
                log_notice_errno(r, "Skipping %s, could not find 'test' command: %m", __func__);
×
UNCOV
804
                return;
×
805
        }
806

807
        if (MANAGER_IS_USER(m) && !have_userns_privileges())
×
UNCOV
808
                return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__);
×
809

810
        ASSERT_OK(find_libraries(fullpath_touch, &libraries));
×
811
        ASSERT_OK(find_libraries(fullpath_test, &libraries_test));
×
UNCOV
812
        ASSERT_OK(strv_extend_strv(&libraries, libraries_test, true));
×
813

814
        ASSERT_NOT_NULL(strextend(&data, "[Service]\n"));
×
815
        ASSERT_NOT_NULL((strextend(&data, "ExecStart=", fullpath_touch, " /aaa\n")));
×
816
        ASSERT_NOT_NULL((strextend(&data, "ExecStart=", fullpath_test, " -f /aaa\n")));
×
817
        ASSERT_NOT_NULL((strextend(&data, "BindReadOnlyPaths=", fullpath_touch, "\n")));
×
UNCOV
818
        ASSERT_NOT_NULL((strextend(&data, "BindReadOnlyPaths=", fullpath_test, "\n")));
×
819

820
        STRV_FOREACH(p, libraries)
×
UNCOV
821
                ASSERT_NOT_NULL((strextend(&data, "BindReadOnlyPaths=", *p, "\n")));
×
822

UNCOV
823
        ASSERT_OK(write_drop_in(user_runtime_unit_dir, "exec-mount-apivfs-no.service", 10, "bind-mount", data));
×
824

UNCOV
825
        ASSERT_OK(mkdir_p("/tmp/test-exec-mount-apivfs-no/root", 0755));
×
826

UNCOV
827
        test(m, "exec-mount-apivfs-no.service", can_unshare || !MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
828

UNCOV
829
        (void) rm_rf("/tmp/test-exec-mount-apivfs-no/root", REMOVE_ROOT|REMOVE_PHYSICAL);
×
830
#endif
831
}
832

UNCOV
833
static void test_exec_noexecpaths(Manager *m) {
×
834

835
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges())
×
UNCOV
836
                test(m, "exec-noexecpaths-simple.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
837
        else
UNCOV
838
                return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__);
×
839
}
840

UNCOV
841
static void test_exec_temporaryfilesystem(Manager *m) {
×
842

843
        test(m, "exec-temporaryfilesystem-options.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
844
        test(m, "exec-temporaryfilesystem-ro.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
845
        test(m, "exec-temporaryfilesystem-rw.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
846
        test(m, "exec-temporaryfilesystem-usr.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
847
}
×
848

UNCOV
849
static void test_exec_systemcallfilter(Manager *m) {
×
850
#if HAVE_SECCOMP && !HAS_FEATURE_ADDRESS_SANITIZER
UNCOV
851
        int r;
×
852

853
        if (!is_seccomp_available()) {
×
854
                log_notice("Seccomp not available, skipping %s", __func__);
×
UNCOV
855
                return;
×
856
        }
857

UNCOV
858
        test(m, "exec-systemcallfilter-writing-handoff-timestamp.service", 0, CLD_EXITED);
×
859

860
        test(m, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED);
×
861
        test(m, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED);
×
862
        test(m, "exec-systemcallfilter-not-failing3.service", 0, CLD_EXITED);
×
863
        test(m, "exec-systemcallfilter-failing.service", SIGSYS, CLD_KILLED);
×
864
        test(m, "exec-systemcallfilter-failing2.service", SIGSYS, CLD_KILLED);
×
UNCOV
865
        test(m, "exec-systemcallfilter-failing3.service", SIGSYS, CLD_KILLED);
×
866

867
        r = find_executable("python3", NULL);
×
868
        if (r < 0) {
×
869
                log_notice_errno(r, "Skipping remaining tests in %s, could not find python3 binary: %m", __func__);
×
UNCOV
870
                return;
×
871
        }
872

873
        test(m, "exec-systemcallfilter-with-errno-name.service", errno_from_name("EILSEQ"), CLD_EXITED);
×
874
        test(m, "exec-systemcallfilter-with-errno-number.service", 255, CLD_EXITED);
×
875
        test(m, "exec-systemcallfilter-with-errno-multi.service", errno_from_name("EILSEQ"), CLD_EXITED);
×
876
        test(m, "exec-systemcallfilter-with-errno-in-allow-list.service", errno_from_name("EILSEQ"), CLD_EXITED);
×
877
        test(m, "exec-systemcallfilter-override-error-action.service", SIGSYS, CLD_KILLED);
×
UNCOV
878
        test(m, "exec-systemcallfilter-override-error-action2.service", errno_from_name("EILSEQ"), CLD_EXITED);
×
879

880
        test(m, "exec-systemcallfilter-nonewprivileges.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
UNCOV
881
        test(m, "exec-systemcallfilter-nonewprivileges-protectclock.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
882

883
        r = find_executable("capsh", NULL);
×
884
        if (r < 0) {
×
885
                log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
×
UNCOV
886
                return;
×
887
        }
888

889
        test(m, "exec-systemcallfilter-nonewprivileges-bounding1.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
UNCOV
890
        test(m, "exec-systemcallfilter-nonewprivileges-bounding2.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
891
#endif
892
}
893

UNCOV
894
static void test_exec_systemcallerrornumber(Manager *m) {
×
895
#if HAVE_SECCOMP && !HAS_FEATURE_ADDRESS_SANITIZER
UNCOV
896
        int r;
×
897

898
        if (!is_seccomp_available()) {
×
899
                log_notice("Seccomp not available, skipping %s", __func__);
×
UNCOV
900
                return;
×
901
        }
902

903
        r = find_executable("python3", NULL);
×
904
        if (r < 0) {
×
905
                log_notice_errno(r, "Skipping %s, could not find python3 binary: %m", __func__);
×
UNCOV
906
                return;
×
907
        }
908

909
        test(m, "exec-systemcallerrornumber-name.service", errno_from_name("EACCES"), CLD_EXITED);
×
UNCOV
910
        test(m, "exec-systemcallerrornumber-number.service", 255, CLD_EXITED);
×
911
#endif
912
}
913

UNCOV
914
static void test_exec_restrictnamespaces(Manager *m) {
×
915
#if HAVE_SECCOMP
916
        if (!is_seccomp_available()) {
×
917
                log_notice("Seccomp not available, skipping %s", __func__);
×
UNCOV
918
                return;
×
919
        }
920

921
        test(m, "exec-restrictnamespaces-no.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
×
922
        test(m, "exec-restrictnamespaces-yes.service", 1, CLD_EXITED);
×
923
        test(m, "exec-restrictnamespaces-mnt.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
×
924
        test(m, "exec-restrictnamespaces-mnt-deny-list.service", 1, CLD_EXITED);
×
925
        test(m, "exec-restrictnamespaces-merge-and.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
×
926
        test(m, "exec-restrictnamespaces-merge-or.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
×
UNCOV
927
        test(m, "exec-restrictnamespaces-merge-all.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
×
928
#endif
929
}
930

UNCOV
931
static void test_exec_systemcallfilter_system(Manager *m) {
×
932
/* Skip this particular test case when running under ASan, as
933
 * LSan intermittently segfaults when accessing memory right
934
 * after the test finishes. Generally, ASan & LSan don't like
935
 * the seccomp stuff.
936
 */
937
#if HAVE_SECCOMP && !HAS_FEATURE_ADDRESS_SANITIZER
938
        if (!is_seccomp_available()) {
×
939
                log_notice("Seccomp not available, skipping %s", __func__);
×
UNCOV
940
                return;
×
941
        }
942

UNCOV
943
        test(m, "exec-systemcallfilter-system-user.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
944

945
        if (!check_nobody_user_and_group()) {
×
946
                log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
×
UNCOV
947
                return;
×
948
        }
949

950
        if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
×
951
                log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
×
UNCOV
952
                return;
×
953
        }
954

UNCOV
955
        test(m, "exec-systemcallfilter-system-user-" NOBODY_USER_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
956
#endif
957
}
958

959
static void test_exec_user(Manager *m) {
×
UNCOV
960
        test(m, "exec-user.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
961

962
        if (!check_nobody_user_and_group()) {
×
963
                log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
×
UNCOV
964
                return;
×
965
        }
966

967
        if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
×
968
                log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
×
UNCOV
969
                return;
×
970
        }
971

UNCOV
972
        test(m, "exec-user-" NOBODY_USER_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
973
}
974

975
static void test_exec_group(Manager *m) {
×
UNCOV
976
        test(m, "exec-group.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
977

978
        if (!check_nobody_user_and_group()) {
×
979
                log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
×
UNCOV
980
                return;
×
981
        }
982

983
        if (!STR_IN_SET(NOBODY_GROUP_NAME, "nobody", "nfsnobody", "nogroup")) {
×
984
                log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME, __func__);
×
UNCOV
985
                return;
×
986
        }
987

UNCOV
988
        test(m, "exec-group-" NOBODY_GROUP_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
989
}
990

991
static void test_exec_supplementarygroups(Manager *m) {
×
992
        int status = MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP;
×
993
        test(m, "exec-supplementarygroups.service", status, CLD_EXITED);
×
994
        test(m, "exec-supplementarygroups-single-group.service", status, CLD_EXITED);
×
995
        test(m, "exec-supplementarygroups-single-group-user.service", status, CLD_EXITED);
×
996
        test(m, "exec-supplementarygroups-multiple-groups-default-group-user.service", status, CLD_EXITED);
×
997
        test(m, "exec-supplementarygroups-multiple-groups-withgid.service", status, CLD_EXITED);
×
998
        test(m, "exec-supplementarygroups-multiple-groups-withuid.service", status, CLD_EXITED);
×
UNCOV
999
}
×
1000

UNCOV
1001
static char* private_directory_bad(Manager *m) {
×
1002
        /* This mirrors setup_exec_directory(). */
1003

1004
        for (ExecDirectoryType dt = 0; dt < _EXEC_DIRECTORY_TYPE_MAX; dt++) {
×
1005
                _cleanup_free_ char *p = NULL;
×
UNCOV
1006
                struct stat st;
×
1007

UNCOV
1008
                ASSERT_NOT_NULL((p = path_join(m->prefix[dt], "private")));
×
1009

1010
                if (stat(p, &st) >= 0 &&
×
1011
                    (st.st_mode & (S_IRWXG|S_IRWXO)))
×
UNCOV
1012
                        return TAKE_PTR(p);
×
1013
        }
1014

1015
        return NULL;
1016
}
1017

1018
static void test_exec_dynamicuser(Manager *m) {
×
1019
        if (MANAGER_IS_USER(m)) {
×
1020
                log_notice("Skipping %s for user manager", __func__);
×
UNCOV
1021
                return;
×
1022
        }
1023

1024
        _cleanup_free_ char *bad = private_directory_bad(m);
×
1025
        if (bad) {
×
1026
                log_warning("%s: %s has bad permissions, skipping test.", __func__, bad);
×
UNCOV
1027
                return;
×
1028
        }
1029

1030
        if (strstr_ptr(ci_environment(), "github-actions")) {
×
1031
                log_notice("%s: skipping test on GH Actions because of systemd/systemd#10337", __func__);
×
UNCOV
1032
                return;
×
1033
        }
1034

UNCOV
1035
        int status = can_unshare ? 0 : EXIT_NAMESPACE;
×
1036

1037
        test(m, "exec-dynamicuser-fixeduser.service", status, CLD_EXITED);
×
1038
        if (check_user_has_group_with_same_name("adm"))
×
1039
                test(m, "exec-dynamicuser-fixeduser-adm.service", status, CLD_EXITED);
×
1040
        if (check_user_has_group_with_same_name("games"))
×
1041
                test(m, "exec-dynamicuser-fixeduser-games.service", status, CLD_EXITED);
×
1042
        test(m, "exec-dynamicuser-fixeduser-one-supplementarygroup.service", status, CLD_EXITED);
×
1043
        test(m, "exec-dynamicuser-supplementarygroups.service", status, CLD_EXITED);
×
UNCOV
1044
        test(m, "exec-dynamicuser-statedir.service", status, CLD_EXITED);
×
1045

1046
        (void) rm_rf("/var/lib/quux", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1047
        (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1048
        (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1049
        (void) rm_rf("/var/lib/waldo", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1050
        (void) rm_rf("/var/lib/private/quux", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1051
        (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1052
        (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
UNCOV
1053
        (void) rm_rf("/var/lib/private/waldo", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1054

1055
        test(m, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED);
×
1056
        test(m, "exec-dynamicuser-statedir-migrate-step2.service", status, CLD_EXITED);
×
UNCOV
1057
        test(m, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED);
×
1058

1059
        (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1060
        (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1061
        (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
×
UNCOV
1062
        (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1063

1064
        test(m, "exec-dynamicuser-runtimedirectory1.service", status, CLD_EXITED);
×
1065
        test(m, "exec-dynamicuser-runtimedirectory2.service", status, CLD_EXITED);
×
UNCOV
1066
        test(m, "exec-dynamicuser-runtimedirectory3.service", status, CLD_EXITED);
×
1067
}
1068

1069
static void test_exec_environment(Manager *m) {
×
1070
        test(m, "exec-environment-no-substitute.service", 0, CLD_EXITED);
×
1071
        test(m, "exec-environment.service", 0, CLD_EXITED);
×
1072
        test(m, "exec-environment-multiple.service", 0, CLD_EXITED);
×
1073
        test(m, "exec-environment-empty.service", 0, CLD_EXITED);
×
UNCOV
1074
}
×
1075

1076
static void test_exec_environmentfile(Manager *m) {
×
UNCOV
1077
        static const char e[] =
×
1078
                "VAR1='word1 word2'\n"
1079
                "VAR2=word3 \n"
1080
                "# comment1\n"
1081
                "\n"
1082
                "; comment2\n"
1083
                " ; # comment3\n"
1084
                "line without an equal\n"
1085
                "VAR3='$word 5 6'\n"
1086
                "VAR4='new\nline'\n"
1087
                "VAR5=password\\with\\backslashes";
UNCOV
1088
        int r;
×
1089

1090
        r = write_string_file("/tmp/test-exec_environmentfile.conf", e, WRITE_STRING_FILE_CREATE);
×
UNCOV
1091
        ASSERT_OK(r);
×
1092

UNCOV
1093
        test(m, "exec-environmentfile.service", 0, CLD_EXITED);
×
1094

1095
        (void) unlink("/tmp/test-exec_environmentfile.conf");
×
UNCOV
1096
}
×
1097

UNCOV
1098
static void test_exec_passenvironment(Manager *m) {
×
1099
        /* test-execute runs under MANAGER_USER which, by default, forwards all
1100
         * variables present in the environment, but only those that are
1101
         * present _at the time it is created_!
1102
         *
1103
         * So these PassEnvironment checks are still expected to work, since we
1104
         * are ensuring the variables are not present at manager creation (they
1105
         * are unset explicitly in main) and are only set here.
1106
         *
1107
         * This is still a good approximation of how a test for MANAGER_SYSTEM
1108
         * would work.
1109
         */
1110
        ASSERT_OK_ERRNO(setenv("VAR1", "word1 word2", 1));
×
1111
        ASSERT_OK_ERRNO(setenv("VAR2", "word3", 1));
×
1112
        ASSERT_OK_ERRNO(setenv("VAR3", "$word 5 6", 1));
×
1113
        ASSERT_OK_ERRNO(setenv("VAR4", "new\nline", 1));
×
1114
        ASSERT_OK_ERRNO(setenv("VAR5", "passwordwithbackslashes", 1));
×
1115
        test(m, "exec-passenvironment.service", 0, CLD_EXITED);
×
1116
        test(m, "exec-passenvironment-repeated.service", 0, CLD_EXITED);
×
1117
        test(m, "exec-passenvironment-empty.service", 0, CLD_EXITED);
×
1118
        ASSERT_OK_ERRNO(unsetenv("VAR1"));
×
1119
        ASSERT_OK_ERRNO(unsetenv("VAR2"));
×
1120
        ASSERT_OK_ERRNO(unsetenv("VAR3"));
×
1121
        ASSERT_OK_ERRNO(unsetenv("VAR4"));
×
1122
        ASSERT_OK_ERRNO(unsetenv("VAR5"));
×
1123
        test(m, "exec-passenvironment-absent.service", 0, CLD_EXITED);
×
UNCOV
1124
}
×
1125

1126
static void test_exec_umask(Manager *m) {
×
1127
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) {
×
1128
                test(m, "exec-umask-default.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
1129
                test(m, "exec-umask-0177.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
1130
        } else
UNCOV
1131
                return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__);
×
1132
}
1133

1134
static void test_exec_runtimedirectory(Manager *m) {
×
1135
        (void) rm_rf("/run/test-exec_runtimedirectory2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1136
        test(m, "exec-runtimedirectory.service", 0, CLD_EXITED);
×
UNCOV
1137
        (void) rm_rf("/run/test-exec_runtimedirectory2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1138

1139
        test(m, "exec-runtimedirectory-mode.service", 0, CLD_EXITED);
×
UNCOV
1140
        test(m, "exec-runtimedirectory-owner.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
1141

1142
        if (!check_nobody_user_and_group()) {
×
1143
                log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
×
UNCOV
1144
                return;
×
1145
        }
1146

1147
        if (!STR_IN_SET(NOBODY_GROUP_NAME, "nobody", "nfsnobody", "nogroup")) {
×
1148
                log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME, __func__);
×
UNCOV
1149
                return;
×
1150
        }
1151

UNCOV
1152
        test(m, "exec-runtimedirectory-owner-" NOBODY_GROUP_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
1153
}
1154

1155
static void test_exec_capabilityboundingset(Manager *m) {
×
UNCOV
1156
        int r;
×
1157

1158
        r = find_executable("capsh", NULL);
×
1159
        if (r < 0) {
×
1160
                log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
×
UNCOV
1161
                return;
×
1162
        }
1163

1164
        if (have_effective_cap(CAP_CHOWN) <= 0 ||
×
1165
            have_effective_cap(CAP_FOWNER) <= 0 ||
×
1166
            have_effective_cap(CAP_KILL) <= 0) {
×
1167
                log_notice("Skipping %s, this process does not have enough capabilities", __func__);
×
UNCOV
1168
                return;
×
1169
        }
1170

1171
        test(m, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED);
×
1172
        test(m, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED);
×
1173
        test(m, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED);
×
UNCOV
1174
        test(m, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED);
×
1175
}
1176

1177
static void test_exec_basic(Manager *m) {
×
1178
        if (isempty(gnu_get_libc_version()))
×
UNCOV
1179
                return (void) log_tests_skipped("ConditionVersion=glibc will not pass under musl");
×
1180

1181
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges())
×
UNCOV
1182
                test(m, "exec-basic.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
1183
        else
UNCOV
1184
                return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__);
×
1185
}
1186

1187
static void test_exec_ambientcapabilities(Manager *m) {
×
UNCOV
1188
        int r;
×
1189

1190
        /* Check if the kernel has support for ambient capabilities. Run
1191
         * the tests only if that's the case. Clearing all ambient
1192
         * capabilities is fine, since we are expecting them to be unset
1193
         * in the first place for the tests. */
1194
        r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
×
1195
        if (r < 0 && IN_SET(errno, EINVAL, EOPNOTSUPP, ENOSYS)) {
×
1196
                log_notice("Skipping %s, the kernel does not support ambient capabilities", __func__);
×
UNCOV
1197
                return;
×
1198
        }
1199

1200
        if (have_effective_cap(CAP_CHOWN) <= 0 ||
×
1201
            have_effective_cap(CAP_NET_RAW) <= 0) {
×
1202
                log_notice("Skipping %s, this process does not have enough capabilities", __func__);
×
UNCOV
1203
                return;
×
1204
        }
1205

1206
        test(m, "exec-ambientcapabilities.service", 0, CLD_EXITED);
×
UNCOV
1207
        test(m, "exec-ambientcapabilities-merge.service", 0, CLD_EXITED);
×
1208

1209
        if (have_effective_cap(CAP_SETUID) > 0)
×
UNCOV
1210
                test(m, "exec-ambientcapabilities-dynuser.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
1211

1212
        if (!check_nobody_user_and_group()) {
×
1213
                log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
×
UNCOV
1214
                return;
×
1215
        }
1216

1217
        if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
×
1218
                log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
×
UNCOV
1219
                return;
×
1220
        }
1221

1222
        test(m, "exec-ambientcapabilities-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
×
UNCOV
1223
        test(m, "exec-ambientcapabilities-merge-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
×
1224
}
1225

1226
static void test_exec_privatenetwork(Manager *m) {
×
UNCOV
1227
        int r;
×
1228

1229
        if (!have_net_dummy)
×
UNCOV
1230
                return (void)log_notice("Skipping %s, dummy network interface not available", __func__);
×
1231

1232
        if (MANAGER_IS_USER(m) && !have_userns_privileges())
×
UNCOV
1233
                return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__);
×
1234

1235
        r = find_executable("ip", NULL);
×
1236
        if (r < 0) {
×
1237
                log_notice_errno(r, "Skipping %s, could not find ip binary: %m", __func__);
×
UNCOV
1238
                return;
×
1239
        }
1240

1241
        test(m, "exec-privatenetwork-yes-privatemounts-no.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NETWORK : EXIT_FAILURE, CLD_EXITED);
×
UNCOV
1242
        test(m, "exec-privatenetwork-yes-privatemounts-yes.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NETWORK : EXIT_NAMESPACE, CLD_EXITED);
×
1243
}
1244

1245
static void test_exec_networknamespacepath(Manager *m) {
×
UNCOV
1246
        int r;
×
1247

1248
        if (!have_net_dummy)
×
UNCOV
1249
                return (void)log_notice("Skipping %s, dummy network interface not available", __func__);
×
1250

1251
        if (!have_netns)
×
UNCOV
1252
                return (void)log_notice("Skipping %s, network namespace not available", __func__);
×
1253

1254
        if (MANAGER_IS_USER(m) && !have_userns_privileges())
×
UNCOV
1255
                return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__);
×
1256

1257
        r = find_executable("ip", NULL);
×
1258
        if (r < 0) {
×
1259
                log_notice_errno(r, "Skipping %s, could not find ip binary: %m", __func__);
×
UNCOV
1260
                return;
×
1261
        }
1262

1263
        test(m, "exec-networknamespacepath-privatemounts-no.service", MANAGER_IS_SYSTEM(m) ? EXIT_SUCCESS : EXIT_FAILURE, CLD_EXITED);
×
UNCOV
1264
        test(m, "exec-networknamespacepath-privatemounts-yes.service", can_unshare ? EXIT_SUCCESS : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
1265
}
1266

1267
static void test_exec_oomscoreadjust(Manager *m) {
×
UNCOV
1268
        test(m, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED);
×
1269

1270
        if (detect_container() > 0) {
×
1271
                log_notice("Testing in container, skipping remaining tests in %s", __func__);
×
UNCOV
1272
                return;
×
1273
        }
UNCOV
1274
        test(m, "exec-oomscoreadjust-negative.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED);
×
1275
}
1276

1277
static void test_exec_ioschedulingclass(Manager *m) {
×
1278
        test(m, "exec-ioschedulingclass-none.service", 0, CLD_EXITED);
×
1279
        test(m, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED);
×
UNCOV
1280
        test(m, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED);
×
1281

1282
        if (detect_container() > 0) {
×
1283
                log_notice("Testing in container, skipping remaining tests in %s", __func__);
×
UNCOV
1284
                return;
×
1285
        }
UNCOV
1286
        test(m, "exec-ioschedulingclass-realtime.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_IOPRIO, CLD_EXITED);
×
1287
}
1288

1289
static void test_exec_unsetenvironment(Manager *m) {
×
1290
        test(m, "exec-unsetenvironment.service", 0, CLD_EXITED);
×
UNCOV
1291
}
×
1292

1293
static void test_exec_specifier(Manager *m) {
×
1294
        test(m, "exec-specifier.service", 0, CLD_EXITED);
×
1295
        if (MANAGER_IS_SYSTEM(m))
×
UNCOV
1296
                test(m, "exec-specifier-system.service", 0, CLD_EXITED);
×
1297
        else
1298
                test(m, "exec-specifier-user.service", 0, CLD_EXITED);
×
1299
        test(m, "exec-specifier@foo-bar.service", 0, CLD_EXITED);
×
1300
        test(m, "exec-specifier-interpolation.service", 0, CLD_EXITED);
×
UNCOV
1301
}
×
1302

1303
static void test_exec_standardinput(Manager *m) {
×
1304
        test(m, "exec-standardinput-data.service", 0, CLD_EXITED);
×
UNCOV
1305
        test(m, "exec-standardinput-file.service", 0, CLD_EXITED);
×
1306

1307
        ExecOutput saved = m->defaults.std_output;
×
1308
        m->defaults.std_output = EXEC_OUTPUT_NULL;
×
1309
        test(m, "exec-standardinput-file-cat.service", 0, CLD_EXITED);
×
1310
        m->defaults.std_output = saved;
×
UNCOV
1311
}
×
1312

1313
static void test_exec_standardoutput(Manager *m) {
×
1314
        test(m, "exec-standardoutput-file.service", 0, CLD_EXITED);
×
UNCOV
1315
}
×
1316

1317
static void test_exec_standardoutput_append(Manager *m) {
×
1318
        test(m, "exec-standardoutput-append.service", 0, CLD_EXITED);
×
UNCOV
1319
}
×
1320

1321
static void test_exec_standardoutput_truncate(Manager *m) {
×
1322
        test(m, "exec-standardoutput-truncate.service", 0, CLD_EXITED);
×
UNCOV
1323
}
×
1324

1325
static void test_exec_condition(Manager *m) {
×
1326
        test_service(m, "exec-condition-failed.service", SERVICE_FAILURE_EXIT_CODE);
×
1327
        test_service(m, "exec-condition-skip.service", SERVICE_SKIP_CONDITION);
×
UNCOV
1328
}
×
1329

UNCOV
1330
static void test_exec_umask_namespace(Manager *m) {
×
1331
        /* exec-specifier-credentials-dir.service creates /run/credentials and enables implicit
1332
         * InaccessiblePath= for the directory for all later services with mount namespace. */
1333
        if (!is_inaccessible_available()) {
×
1334
                log_notice("Testing without inaccessible, skipping %s", __func__);
×
UNCOV
1335
                return;
×
1336
        }
UNCOV
1337
        test(m, "exec-umask-namespace.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NAMESPACE : EXIT_GROUP, CLD_EXITED);
×
1338
}
1339

1340
typedef struct test_entry {
1341
        test_function_t f;
1342
        const char *name;
1343
} test_entry;
1344

1345
#define entry(x) {x, #x}
1346

1347
static void run_tests(RuntimeScope scope, char **patterns) {
×
1348
        _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
×
1349
        _cleanup_free_ char *unit_paths = NULL;
×
1350
        _cleanup_(manager_freep) Manager *m = NULL;
×
1351
        usec_t start, finish;
×
UNCOV
1352
        int r;
×
1353

UNCOV
1354
        static const test_entry tests[] = {
×
1355
                entry(test_exec_basic),
1356
                entry(test_exec_ambientcapabilities),
1357
                entry(test_exec_bindpaths),
1358
                entry(test_exec_capabilityboundingset),
1359
                entry(test_exec_condition),
1360
                entry(test_exec_cpuaffinity),
1361
                entry(test_exec_credentials),
1362
                entry(test_exec_dynamicuser),
1363
                entry(test_exec_environment),
1364
                entry(test_exec_environmentfile),
1365
                entry(test_exec_execsearchpath),
1366
                entry(test_exec_execsearchpath_environment),
1367
                entry(test_exec_execsearchpath_environment_files),
1368
                entry(test_exec_execsearchpath_passenvironment),
1369
                entry(test_exec_execsearchpath_specifier),
1370
                entry(test_exec_group),
1371
                entry(test_exec_ignoresigpipe),
1372
                entry(test_exec_inaccessiblepaths),
1373
                entry(test_exec_ioschedulingclass),
1374
                entry(test_exec_mount_apivfs),
1375
                entry(test_exec_networknamespacepath),
1376
                entry(test_exec_noexecpaths),
1377
                entry(test_exec_oomscoreadjust),
1378
                entry(test_exec_passenvironment),
1379
                entry(test_exec_personality),
1380
                entry(test_exec_privatedevices),
1381
                entry(test_exec_privatenetwork),
1382
                entry(test_exec_privatetmp),
1383
                entry(test_exec_protecthome),
1384
                entry(test_exec_protectkernelmodules),
1385
                entry(test_exec_readonlypaths),
1386
                entry(test_exec_readwritepaths),
1387
                entry(test_exec_restrictnamespaces),
1388
                entry(test_exec_runtimedirectory),
1389
                entry(test_exec_specifier),
1390
                entry(test_exec_standardinput),
1391
                entry(test_exec_standardoutput),
1392
                entry(test_exec_standardoutput_append),
1393
                entry(test_exec_standardoutput_truncate),
1394
                entry(test_exec_supplementarygroups),
1395
                entry(test_exec_systemcallerrornumber),
1396
                entry(test_exec_systemcallfilter),
1397
                entry(test_exec_systemcallfilter_system),
1398
                entry(test_exec_temporaryfilesystem),
1399
                entry(test_exec_umask),
1400
                entry(test_exec_umask_namespace),
1401
                entry(test_exec_unsetenvironment),
1402
                entry(test_exec_user),
1403
                entry(test_exec_workingdirectory),
1404
                {},
1405
        };
1406

1407
        ASSERT_OK_ERRNO(unsetenv("USER"));
×
1408
        ASSERT_OK_ERRNO(unsetenv("LOGNAME"));
×
1409
        ASSERT_OK_ERRNO(unsetenv("SHELL"));
×
1410
        ASSERT_OK_ERRNO(unsetenv("HOME"));
×
UNCOV
1411
        ASSERT_OK_ERRNO(unsetenv("TMPDIR"));
×
1412

1413
        /* Unset VARx, especially, VAR1, VAR2 and VAR3, which are used in the PassEnvironment test cases,
1414
         * otherwise (and if they are present in the environment), `manager_default_environment` will copy
1415
         * them into the default environment which is passed to each created job, which will make the tests
1416
         * that expect those not to be present to fail. */
1417
        ASSERT_OK_ERRNO(unsetenv("VAR1"));
×
1418
        ASSERT_OK_ERRNO(unsetenv("VAR2"));
×
1419
        ASSERT_OK_ERRNO(unsetenv("VAR3"));
×
1420
        ASSERT_OK_ERRNO(unsetenv("VAR4"));
×
UNCOV
1421
        ASSERT_OK_ERRNO(unsetenv("VAR5"));
×
1422

1423
        ASSERT_NOT_NULL((runtime_dir = setup_fake_runtime_dir()));
×
1424
        ASSERT_NOT_NULL((user_runtime_unit_dir = path_join(runtime_dir, "systemd/user")));
×
1425
        ASSERT_NOT_NULL((unit_paths = strjoin(PRIVATE_UNIT_DIR, ":", user_runtime_unit_dir)));
×
UNCOV
1426
        ASSERT_OK(setenv_unit_path(unit_paths));
×
1427

1428
        /* Write credential for test-execute-load-credential to the fake runtime dir, too */
1429
        _cleanup_free_ char *j = ASSERT_PTR(path_join(runtime_dir, "credstore/test-execute.load-credential"));
×
UNCOV
1430
        ASSERT_OK(write_string_file(j, "foo", WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755));
×
1431

1432
        r = manager_new(scope, MANAGER_TEST_RUN_BASIC, &m);
×
1433
        if (manager_errno_skip_test(r))
×
1434
                return (void) log_tests_skipped_errno(r, "manager_new");
×
UNCOV
1435
        ASSERT_OK(r);
×
1436

1437
        m->defaults.std_output = EXEC_OUTPUT_INHERIT; /* don't rely on host journald */
×
UNCOV
1438
        ASSERT_OK(manager_startup(m, NULL, NULL, NULL));
×
1439

1440
        /* Uncomment below if you want to make debugging logs stored to journal. */
1441
        //manager_override_log_target(m, LOG_TARGET_AUTO);
1442
        //manager_override_log_level(m, LOG_DEBUG);
1443

1444
        /* Measure and print the time that it takes to run tests, excluding startup of the manager object,
1445
         * to try and measure latency of spawning services */
1446
        n_ran_tests = 0;
×
UNCOV
1447
        start = now(CLOCK_MONOTONIC);
×
1448

1449
        for (const test_entry *test = tests; test->f; test++)
×
1450
                if (strv_fnmatch_or_empty(patterns, test->name, FNM_NOESCAPE)) {
×
1451
                        log_info("Starting %s.", test->name);
×
UNCOV
1452
                        test->f(m);
×
1453
                } else
UNCOV
1454
                        log_info("Skipping %s because it does not match any pattern.", test->name);
×
1455

UNCOV
1456
        finish = now(CLOCK_MONOTONIC);
×
1457

UNCOV
1458
        log_info("ran %u tests with %s manager + unshare=%s in: %s",
×
1459
                 n_ran_tests,
1460
                 scope == RUNTIME_SCOPE_SYSTEM ? "system" : "user",
1461
                 yes_no(can_unshare),
1462
                 FORMAT_TIMESPAN(finish - start, USEC_PER_MSEC));
1463
}
1464

1465
static int prepare_ns(const char *process_name) {
×
UNCOV
1466
        int r;
×
1467

UNCOV
1468
        r = pidref_safe_fork(
×
1469
                        process_name,
1470
                        FORK_RESET_SIGNALS|
1471
                        FORK_CLOSE_ALL_FDS|
1472
                        FORK_DEATHSIG_SIGTERM|
1473
                        FORK_WAIT|
1474
                        FORK_REOPEN_LOG|
1475
                        FORK_LOG|
1476
                        FORK_NEW_MOUNTNS|
1477
                        FORK_MOUNTNS_SLAVE,
1478
                        NULL);
1479
        ASSERT_OK(r);
×
UNCOV
1480
        if (r == 0) {
×
1481
                _cleanup_free_ char *unit_dir = NULL, *build_dir = NULL, *build_dir_mount = NULL;
×
1482

UNCOV
1483
                const char *coverage = getenv("COVERAGE_BUILD_DIR");
×
1484
                if (!coverage)
×
1485
                        /* Make "/" read-only. */
UNCOV
1486
                        ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, NULL, "/", NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL));
×
1487

1488
                /* Creating a new user namespace in the above means all MS_SHARED mounts become MS_SLAVE.
1489
                 * Let's put them back to MS_SHARED here, since that's what we want as defaults. (This will
1490
                 * not reconnect propagation, but simply create new peer groups for all our mounts). */
1491
                ASSERT_OK(mount_follow_verbose(LOG_DEBUG, NULL, "/", NULL, MS_SHARED|MS_REC, NULL));
×
1492

UNCOV
1493
                ASSERT_OK(mkdir_p(PRIVATE_UNIT_DIR, 0755));
×
1494
                ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, "tmpfs", PRIVATE_UNIT_DIR, "tmpfs", MS_NOSUID|MS_NODEV, NULL));
×
1495
                /* Mark our test "playground" as MS_SLAVE, so we can MS_MOVE mounts underneath it. */
UNCOV
1496
                ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, NULL, PRIVATE_UNIT_DIR, NULL, MS_SLAVE, NULL));
×
1497

1498
                /* Copy unit files to make them accessible even when unprivileged. */
UNCOV
1499
                ASSERT_OK(get_testdata_dir("test-execute/", &unit_dir));
×
UNCOV
1500
                ASSERT_OK(copy_directory_at(AT_FDCWD, unit_dir, AT_FDCWD, PRIVATE_UNIT_DIR, COPY_MERGE_EMPTY));
×
1501

1502
                /* Mount tmpfs on the following directories to make not StateDirectory= or friends disturb the host. */
1503
                ASSERT_OK_OR(get_build_exec_dir(&build_dir), -ENOEXEC);
×
1504

UNCOV
1505
                if (build_dir) {
×
1506
                        /* Account for a build directory being in one of the soon-to-be-tmpfs directories. If we
1507
                         * overmount it with an empty tmpfs, manager_new() will pin the wrong systemd-executor binary,
1508
                         * which can then lead to unexpected (and painful to debug) test fails. */
1509
                        ASSERT_OK_ERRNO(access(build_dir, F_OK));
×
1510
                        ASSERT_NOT_NULL((build_dir_mount = path_join(PRIVATE_UNIT_DIR, "build_dir")));
×
UNCOV
1511
                        ASSERT_OK(mkdir_p(build_dir_mount, 0755));
×
UNCOV
1512
                        ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, build_dir, build_dir_mount, NULL, MS_BIND, NULL));
×
1513
                }
1514

UNCOV
1515
                FOREACH_STRING(p, "/dev/shm", "/root", "/tmp", "/var/tmp", "/var/lib")
×
1516
                        ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, "tmpfs", p, "tmpfs", MS_NOSUID|MS_NODEV, NULL));
×
1517

UNCOV
1518
                if (build_dir_mount) {
×
1519
                        int k;
×
1520

1521
                        ASSERT_OK_OR(k = RET_NERRNO(access(build_dir, F_OK)), -ENOENT);
×
1522

UNCOV
1523
                        if (k == -ENOENT) {
×
1524
                                /* The build directory got overmounted by tmpfs, so let's use the "backup" bind mount to
1525
                                 * bring it back. */
UNCOV
1526
                                ASSERT_OK(mkdir_p(build_dir, 0755));
×
UNCOV
1527
                                ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, build_dir_mount, build_dir, NULL, MS_MOVE, NULL));
×
1528
                        }
1529
                }
1530

1531
                /* Prepare credstore like tmpfiles.d/credstore.conf for LoadCredential= tests. */
1532
                FOREACH_STRING(p, "/run/credstore", "/run/credstore.encrypted") {
×
UNCOV
1533
                        ASSERT_OK(mkdir_p(p, 0700));
×
UNCOV
1534
                        ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, "tmpfs", p, "tmpfs", MS_NOSUID|MS_NODEV, "mode=0700"));
×
1535
                }
1536

UNCOV
1537
                ASSERT_OK(write_string_file("/run/credstore/test-execute.load-credential", "foo", WRITE_STRING_FILE_CREATE));
×
1538
        }
1539

UNCOV
1540
        return r;
×
1541
}
1542

UNCOV
1543
TEST(run_tests_root) {
×
1544
        _cleanup_strv_free_ char **filters = NULL;
×
1545

UNCOV
1546
        if (!have_namespaces())
×
UNCOV
1547
                return (void) log_tests_skipped("unshare() is disabled");
×
1548

1549
        /* safe_fork() clears saved_argv in the child process. Let's copy it. */
1550
        ASSERT_NOT_NULL((filters = strv_copy(strv_skip(saved_argv, 1))));
×
1551

1552
        if (prepare_ns("(test-execute-root)") == 0) {
×
1553
                can_unshare = true;
×
UNCOV
1554
                run_tests(RUNTIME_SCOPE_SYSTEM, filters);
×
UNCOV
1555
                _exit(EXIT_SUCCESS);
×
1556
        }
1557
}
1558

UNCOV
1559
TEST(run_tests_without_unshare) {
×
1560
        if (!have_namespaces()) {
×
1561
                /* unshare() is already filtered. */
1562
                can_unshare = false;
×
UNCOV
1563
                run_tests(RUNTIME_SCOPE_SYSTEM, strv_skip(saved_argv, 1));
×
UNCOV
1564
                return;
×
1565
        }
1566

1567
#if HAVE_SECCOMP
UNCOV
1568
        _cleanup_strv_free_ char **filters = NULL;
×
UNCOV
1569
        int r;
×
1570

1571
        /* The following tests are for 1beab8b0d0ff2d7d1436b52d4a0c3d56dc908962. */
UNCOV
1572
        if (!is_seccomp_available())
×
UNCOV
1573
                return (void) log_tests_skipped("Seccomp not available, cannot run unshare() filtered tests");
×
1574

1575
        /* safe_fork() clears saved_argv in the child process. Let's copy it. */
1576
        ASSERT_NOT_NULL((filters = strv_copy(strv_skip(saved_argv, 1))));
×
1577

UNCOV
1578
        if (prepare_ns("(test-execute-without-unshare)") == 0) {
×
1579
                _cleanup_hashmap_free_ Hashmap *s = NULL;
×
1580

1581
                r = sym_seccomp_syscall_resolve_name("unshare");
×
1582
                ASSERT_NE(r, __NR_SCMP_ERROR);
×
UNCOV
1583
                ASSERT_OK(hashmap_ensure_put(&s, NULL, UINT32_TO_PTR(r + 1), INT_TO_PTR(-1)));
×
UNCOV
1584
                ASSERT_OK(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EOPNOTSUPP), true));
×
1585

1586
                /* Check unshare() is actually filtered. */
1587
                ASSERT_ERROR_ERRNO(unshare(CLONE_NEWNS), EOPNOTSUPP);
×
1588

1589
                can_unshare = false;
×
UNCOV
1590
                run_tests(RUNTIME_SCOPE_SYSTEM, filters);
×
UNCOV
1591
                _exit(EXIT_SUCCESS);
×
1592
        }
1593
#else
1594
        log_tests_skipped("Built without seccomp support, cannot run unshare() filtered tests");
1595
#endif
1596
}
1597

UNCOV
1598
TEST(run_tests_unprivileged) {
×
1599
        _cleanup_strv_free_ char **filters = NULL;
×
1600

UNCOV
1601
        if (!have_namespaces())
×
UNCOV
1602
                return (void) log_tests_skipped("unshare() is disabled");
×
1603

1604
        /* safe_fork() clears saved_argv in the child process. Let's copy it. */
1605
        ASSERT_NOT_NULL((filters = strv_copy(strv_skip(saved_argv, 1))));
×
1606

UNCOV
1607
        if (prepare_ns("(test-execute-unprivileged)") == 0) {
×
1608
                ASSERT_OK(capability_bounding_set_drop(0, /* right_now= */ true));
×
1609

1610
                can_unshare = false;
×
UNCOV
1611
                run_tests(RUNTIME_SCOPE_USER, filters);
×
UNCOV
1612
                _exit(EXIT_SUCCESS);
×
1613
        }
1614
}
1615

1616
static int intro(void) {
1✔
1617
        int r;
1✔
1618

1619
#if HAS_FEATURE_ADDRESS_SANITIZER
1620
        if (strstr_ptr(ci_environment(), "travis") || strstr_ptr(ci_environment(), "github-actions"))
1621
                return log_tests_skipped("Running on Travis CI/GH Actions under ASan, see https://github.com/systemd/systemd/issues/10696");
1622
#endif
1623
        /* It is needed otherwise cgroup creation fails */
1624
        if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0)
1✔
UNCOV
1625
                return log_tests_skipped("not privileged");
×
1626

1627
        if (running_in_chroot() != 0)
1✔
UNCOV
1628
                return log_tests_skipped("running in chroot");
×
1629

1630
        if (enter_cgroup_subroot(NULL) == -ENOMEDIUM)
1✔
UNCOV
1631
                return log_tests_skipped("cgroupfs not available");
×
1632

1633
        if (path_is_read_only_fs("/sys") > 0)
1✔
1634
                return log_tests_skipped("/sys is mounted read-only");
1✔
1635

1636
        r = dlopen_libmount();
×
UNCOV
1637
        if (r < 0)
×
UNCOV
1638
                return log_tests_skipped("libmount not available.");
×
1639

1640
        /* Create dummy network interface for testing PrivateNetwork=yes */
1641
        have_net_dummy = system("ip link add dummy-test-exec type dummy") == 0;
×
1642

1643
        if (have_net_dummy) {
×
1644
                /* Create a network namespace and a dummy interface in it for NetworkNamespacePath= */
UNCOV
1645
                have_netns = system("ip netns add test-execute-netns") == 0;
×
UNCOV
1646
                have_netns = have_netns && system("ip netns exec test-execute-netns ip link add dummy-test-ns type dummy") == 0;
×
1647
        }
1648

1649
        return EXIT_SUCCESS;
1650
}
1651

1652
static int outro(void) {
1✔
1653
        if (have_net_dummy) {
1✔
UNCOV
1654
                (void) system("ip link del dummy-test-exec");
×
UNCOV
1655
                (void) system("ip netns del test-execute-netns");
×
1656
        }
1657

1658
        (void) rmdir(PRIVATE_UNIT_DIR);
1✔
1659

1660
        return EXIT_SUCCESS;
1✔
1661
}
1662

1663
DEFINE_TEST_MAIN_FULL(LOG_DEBUG, intro, outro);
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc