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

systemd / systemd / 20401947236

20 Dec 2025 09:56PM UTC coverage: 72.701% (+0.1%) from 72.578%
20401947236

push

github

DaanDeMeyer
core/socket: modernize listen/accept_in_cgroup

4 of 9 new or added lines in 1 file covered. (44.44%)

7723 existing lines in 114 files now uncovered.

309972 of 426363 relevant lines covered (72.7%)

1133403.64 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 "extract-word.h"
21
#include "fd-util.h"
22
#include "fileio.h"
23
#include "fs-util.h"
24
#include "libmount-util.h"
25
#include "manager.h"
26
#include "mkdir.h"
27
#include "mount-util.h"
28
#include "path-util.h"
29
#include "process-util.h"
30
#include "rm-rf.h"
31
#include "seccomp-util.h"
32
#include "service.h"
33
#include "signal-util.h"
34
#include "stat-util.h"
35
#include "static-destruct.h"
36
#include "strv.h"
37
#include "sysctl-util.h"
38
#include "tests.h"
39
#include "unit.h"
40
#include "user-util.h"
41
#include "virt.h"
42

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

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

51
STATIC_DESTRUCTOR_REGISTER(user_runtime_unit_dir, freep);
1✔
52

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

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

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

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

71
        abort();
×
72
}
73

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

78
        ASSERT_NOT_NULL(m);
×
79

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

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

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

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

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

100
        ASSERT_NOT_NULL(m);
×
101
        ASSERT_NOT_NULL(unit);
×
102

103
        wait_for_service_finish(m, unit);
×
104

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

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

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

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

127
        ASSERT_NOT_NULL(m);
×
128
        ASSERT_NOT_NULL(unit);
×
129

130
        wait_for_service_finish(m, unit);
×
131

132
        service = SERVICE(unit);
×
133

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

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

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

151
        if (!synthesize_nobody())
×
152
                goto invalid;
×
153

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

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

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

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

180
        cache = 1;
×
181
        return true;
×
182

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

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

192
        ASSERT_NOT_NULL(name);
×
193

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

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

204
        return true;
205
}
206

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

218
        return true;
×
219
}
220

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

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

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

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

242
                return false;
×
243
        }
244

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

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

252
        if (apparmor_restrict_unprivileged_userns())
×
253
                return false;
254

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

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

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

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

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

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

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

UNCOV
295
        ASSERT_NOT_NULL(unit_name);
×
296

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

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

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

UNCOV
313
        ASSERT_NOT_NULL(unit_name);
×
314

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
423
        int r;
×
424

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
673
static int on_spawn_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
×
674
        pid_t *pid = userdata;
×
675

UNCOV
676
        ASSERT_NOT_NULL(pid);
×
677

UNCOV
678
        (void) kill(*pid, SIGKILL);
×
679

UNCOV
680
        return 1;
×
681
}
682

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

UNCOV
686
        ASSERT_NOT_NULL(si);
×
687

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

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

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

UNCOV
706
        ASSERT_NOT_NULL(exec);
×
707
        ASSERT_NOT_NULL(ret);
×
708

UNCOV
709
        ASSERT_OK_ERRNO(pipe2(outpipe, O_NONBLOCK|O_CLOEXEC));
×
710
        ASSERT_OK_ERRNO(pipe2(errpipe, O_NONBLOCK|O_CLOEXEC));
×
711

712
        r = safe_fork_full("(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, &pid);
716
        ASSERT_OK(r);
×
UNCOV
717
        if (r == 0) {
×
UNCOV
718
                execlp("ldd", "ldd", exec, NULL);
×
719
                _exit(EXIT_FAILURE);
×
720
        }
721

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

725
        ASSERT_OK(sd_event_new(&e));
×
726

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

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

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

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

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

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

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

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

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

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

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

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

788
        ASSERT_NOT_NULL(user_runtime_unit_dir);
×
789

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

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

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

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

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

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

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

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

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

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

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

840
static void test_exec_temporaryfilesystem(Manager *m) {
×
841

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1014
        return NULL;
1015
}
1016

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

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

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

1034
        int status = can_unshare ? 0 : EXIT_NAMESPACE;
×
1035

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
1455
        finish = now(CLOCK_MONOTONIC);
×
1456

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

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

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

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

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

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

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

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

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

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

1516
                if (build_dir_mount) {
×
1517
                        int k;
×
1518

1519
                        ASSERT_OK_OR(k = RET_NERRNO(access(build_dir, F_OK)), -ENOENT);
×
1520

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

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

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

1538
        return r;
×
1539
}
1540

1541
TEST(run_tests_root) {
×
UNCOV
1542
        _cleanup_strv_free_ char **filters = NULL;
×
1543

1544
        if (!have_namespaces())
×
1545
                return (void) log_tests_skipped("unshare() is disabled");
×
1546

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

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

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

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

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

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

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

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

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

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

UNCOV
1596
TEST(run_tests_unprivileged) {
×
UNCOV
1597
        _cleanup_strv_free_ char **filters = NULL;
×
1598

1599
        if (!have_namespaces())
×
1600
                return (void) log_tests_skipped("unshare() is disabled");
×
1601

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

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

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

1614
static int intro(void) {
1✔
1615
        int r;
1✔
1616

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

1625
        if (running_in_chroot() != 0)
1✔
1626
                return log_tests_skipped("running in chroot");
×
1627

1628
        if (enter_cgroup_subroot(NULL) == -ENOMEDIUM)
1✔
1629
                return log_tests_skipped("cgroupfs not available");
×
1630

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

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

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

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

1647
        return EXIT_SUCCESS;
1648
}
1649

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

1656
        (void) rmdir(PRIVATE_UNIT_DIR);
1✔
1657

1658
        return EXIT_SUCCESS;
1✔
1659
}
1660

1661
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