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

systemd / systemd / 15175033720

21 May 2025 10:22PM UTC coverage: 72.079% (+0.03%) from 72.047%
15175033720

push

github

web-flow
Several minor follow-ups for #33995 (#37558)

3 of 3 new or added lines in 1 file covered. (100.0%)

34302 existing lines in 652 files now uncovered.

299232 of 415142 relevant lines covered (72.08%)

700018.81 hits per line

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

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

3
#include <fnmatch.h>
4
#include <linux/prctl.h>
5
#include <stdio.h>
6
#include <stdlib.h>
7
#include <sys/mount.h>
8
#include <sys/prctl.h>
9

10
#include "sd-event.h"
11

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

41
#define PRIVATE_UNIT_DIR "/run/test-execute-unit-dir"
42

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

49
STATIC_DESTRUCTOR_REGISTER(user_runtime_unit_dir, freep);
1✔
50

51
typedef void (*test_function_t)(Manager *m);
52

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

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

64
        log_error("Test timeout when testing %s", unit->id);
×
65
        r = unit_kill(unit, KILL_ALL, SIGKILL, SI_USER, 0, NULL);
×
66
        if (r < 0)
×
UNCOV
67
                log_error_errno(r, "Failed to kill %s, ignoring: %m", unit->id);
×
68

UNCOV
69
        abort();
×
70
}
71

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

UNCOV
76
        ASSERT_NOT_NULL(m);
×
77

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

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

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

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

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

98
        ASSERT_NOT_NULL(m);
×
UNCOV
99
        ASSERT_NOT_NULL(unit);
×
100

UNCOV
101
        wait_for_service_finish(m, unit);
×
102

103
        service = SERVICE(unit);
×
UNCOV
104
        exec_status_dump(&service->main_exec_status, stdout, "\t");
×
105

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

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

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

125
        ASSERT_NOT_NULL(m);
×
UNCOV
126
        ASSERT_NOT_NULL(unit);
×
127

UNCOV
128
        wait_for_service_finish(m, unit);
×
129

UNCOV
130
        service = SERVICE(unit);
×
131

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

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

146
        if (cache >= 0)
×
UNCOV
147
                return !!cache;
×
148

149
        if (!synthesize_nobody())
×
UNCOV
150
                goto invalid;
×
151

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

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

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

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

178
        cache = 1;
×
UNCOV
179
        return true;
×
180

181
invalid:
×
182
        cache = 0;
×
UNCOV
183
        return false;
×
184
}
185

186
static bool check_user_has_group_with_same_name(const char *name) {
×
187
        struct passwd *p;
×
UNCOV
188
        struct group *g;
×
189

UNCOV
190
        ASSERT_NOT_NULL(name);
×
191

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

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

202
        return true;
203
}
204

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

UNCOV
216
        return true;
×
217
}
218

219
static void start_parent_slices(Unit *unit) {
×
UNCOV
220
        Unit *slice;
×
221

222
        slice = UNIT_GET_SLICE(unit);
×
223
        if (slice) {
×
224
                start_parent_slices(slice);
×
225
                int r = unit_start(slice, NULL);
×
226
                if (r != -EALREADY)
×
UNCOV
227
                        ASSERT_OK(r);
×
228
        }
UNCOV
229
}
×
230

231
static bool apparmor_restrict_unprivileged_userns(void) {
×
232
        _cleanup_free_ char *v = NULL;
×
UNCOV
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)
×
UNCOV
240
                        log_debug_errno(r, "Failed to read kernel.apparmor_restrict_unprivileged_userns sysctl, ignoring: %m");
×
241

UNCOV
242
                return false;
×
243
        }
244

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

248
static bool have_userns_privileges(void) {
×
249
        pid_t pid;
×
UNCOV
250
        int r;
×
251

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

UNCOV
255
        r = safe_fork("(sd-test-check-userns)",
×
256
                      FORK_RESET_SIGNALS |
257
                      FORK_CLOSE_ALL_FDS |
258
                      FORK_DEATHSIG_SIGKILL,
259
                      &pid);
260
        ASSERT_OK(r);
×
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 = wait_for_terminate_and_check("(sd-test-check-userns)", pid, 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_reset) 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
        ASSERT_OK(mkdir_p("/tmp/test-exec_execsearchpath", 0755));
×
373

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

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

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

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

383
static void test_exec_execsearchpath_specifier(Manager *m) {
×
384
        test(m, "exec-execsearchpath-unit-specifier.service", 0, CLD_EXITED);
×
UNCOV
385
}
×
386

387
static void test_exec_execsearchpath_environment(Manager *m) {
×
388
        test(m, "exec-execsearchpath-environment.service", 0, CLD_EXITED);
×
389
        test(m, "exec-execsearchpath-environment-path-set.service", 0, CLD_EXITED);
×
UNCOV
390
}
×
391

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

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

UNCOV
418
        int r;
×
419

420
        r = write_string_file("/tmp/test-exec_execsearchpath_environmentfile.conf", path_not_set, WRITE_STRING_FILE_CREATE);
×
UNCOV
421
        ASSERT_OK(r);
×
422

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

UNCOV
425
        (void) unlink("/tmp/test-exec_environmentfile.conf");
×
426

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

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

432
        (void) unlink("/tmp/test-exec_environmentfile-set.conf");
×
UNCOV
433
}
×
434

435
static void test_exec_execsearchpath_passenvironment(Manager *m) {
×
436
        ASSERT_OK_ERRNO(setenv("VAR1", "word1 word2", 1));
×
437
        ASSERT_OK_ERRNO(setenv("VAR2", "word3", 1));
×
438
        ASSERT_OK_ERRNO(setenv("VAR3", "$word 5 6", 1));
×
439
        ASSERT_OK_ERRNO(setenv("VAR4", "new\nline", 1));
×
UNCOV
440
        ASSERT_OK_ERRNO(setenv("VAR5", "passwordwithbackslashes", 1));
×
441

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

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

447
        ASSERT_OK_ERRNO(unsetenv("VAR1"));
×
448
        ASSERT_OK_ERRNO(unsetenv("VAR2"));
×
449
        ASSERT_OK_ERRNO(unsetenv("VAR3"));
×
450
        ASSERT_OK_ERRNO(unsetenv("VAR4"));
×
451
        ASSERT_OK_ERRNO(unsetenv("VAR5"));
×
452
        ASSERT_OK_ERRNO(unsetenv("PATH"));
×
UNCOV
453
}
×
454

UNCOV
455
static void test_exec_personality(Manager *m) {
×
456
#if defined(__x86_64__)
UNCOV
457
        test(m, "exec-personality-x86-64.service", 0, CLD_EXITED);
×
458

459
#elif defined(__s390x__)
460
        test(m, "exec-personality-s390x.service", 0, CLD_EXITED);
461

462
#elif defined(__s390__)
463
        test(m, "exec-personality-s390.service", 0, CLD_EXITED);
464

465
#elif defined(__powerpc64__)
466
#  if __BYTE_ORDER == __BIG_ENDIAN
467
        test(m, "exec-personality-ppc64.service", 0, CLD_EXITED);
468
#  else
469
        test(m, "exec-personality-ppc64le.service", 0, CLD_EXITED);
470
#  endif
471

472
#elif defined(__aarch64__)
473
        test(m, "exec-personality-aarch64.service", 0, CLD_EXITED);
474

475
#elif defined(__i386__)
476
        test(m, "exec-personality-x86.service", 0, CLD_EXITED);
477
#elif defined(__loongarch_lp64)
478
        test(m, "exec-personality-loongarch64.service", 0, CLD_EXITED);
479
#else
480
        log_notice("Unknown personality, skipping %s", __func__);
481
#endif
UNCOV
482
}
×
483

484
static void test_exec_ignoresigpipe(Manager *m) {
×
485
        test(m, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED);
×
486
        test(m, "exec-ignoresigpipe-no.service", SIGPIPE, CLD_KILLED);
×
UNCOV
487
}
×
488

489
static void test_exec_privatetmp(Manager *m) {
×
UNCOV
490
        ASSERT_OK(touch("/tmp/test-exec_privatetmp"));
×
491

492
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) {
×
493
                test(m, "exec-privatetmp-yes.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
494
                test(m, "exec-privatetmp-disabled-by-prefix.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
495

UNCOV
496
                FOREACH_STRING(s,
×
497
                               "exec-privatetmp-disconnected.service",
498
                               "exec-privatetmp-disconnected-defaultdependencies-no.service",
499
                               "exec-privatetmp-disconnected-requires-mounts-for-var.service",
500
                               "exec-privatetmp-disconnected-wants-mounts-for-var.service",
501
                               "exec-privatetmp-disconnected-after-and-requires-for-var.service",
502
                               "exec-privatetmp-disconnected-after-and-wants-for-var.service") {
503
                        (void) unlink("/tmp/test-exec_privatetmp_disconnected");
×
504
                        (void) unlink("/var/tmp/test-exec_privatetmp_disconnected");
×
505
                        test(m, s, can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
506
                        ASSERT_FAIL(access("/tmp/test-exec_privatetmp_disconnected", F_OK));
×
UNCOV
507
                        ASSERT_FAIL(access("/var/tmp/test-exec_privatetmp_disconnected", F_OK));
×
508
                }
509
        }
510

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

513
        (void) unlink("/tmp/test-exec_privatetmp");
×
UNCOV
514
}
×
515

516
static void test_exec_privatedevices(Manager *m) {
×
UNCOV
517
        int r;
×
518

519
        if (detect_container() > 0) {
×
520
                log_notice("Testing in container, skipping %s", __func__);
×
UNCOV
521
                return;
×
522
        }
523
        if (!is_inaccessible_available()) {
×
524
                log_notice("Testing without inaccessible, skipping %s", __func__);
×
UNCOV
525
                return;
×
526
        }
527

528
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) {
×
529
                test(m, "exec-privatedevices-yes.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
530
                if (access("/dev/kmsg", F_OK) >= 0)
×
531
                        test(m, "exec-privatedevices-bind.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
532
                test(m, "exec-privatedevices-disabled-by-prefix.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
533
                test(m, "exec-privatedevices-yes-with-group.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
534
        }
535

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

538
        /* We use capsh to test if the capabilities are
539
         * properly set, so be sure that it exists */
540
        r = find_executable("capsh", NULL);
×
541
        if (r < 0) {
×
542
                log_notice_errno(r, "Could not find capsh binary, skipping remaining tests in %s: %m", __func__);
×
UNCOV
543
                return;
×
544
        }
545

546
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) {
×
547
                test(m, "exec-privatedevices-yes-capability-mknod.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
548
                test(m, "exec-privatedevices-yes-capability-sys-rawio.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
549
        }
550

551
        test(m, "exec-privatedevices-no-capability-mknod.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED);
×
UNCOV
552
        test(m, "exec-privatedevices-no-capability-sys-rawio.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED);
×
553
}
554

555
static void test_exec_protecthome(Manager *m) {
×
556
        if (!can_unshare) {
×
557
                log_notice("Cannot reliably unshare, skipping %s", __func__);
×
UNCOV
558
                return;
×
559
        }
560

UNCOV
561
        test(m, "exec-protecthome-tmpfs-vs-protectsystem-strict.service", 0, CLD_EXITED);
×
562
}
563

564
static void test_exec_protectkernelmodules(Manager *m) {
×
UNCOV
565
        int r;
×
566

567
        if (detect_container() > 0) {
×
568
                log_notice("Testing in container, skipping %s", __func__);
×
UNCOV
569
                return;
×
570
        }
571
        if (!is_inaccessible_available()) {
×
572
                log_notice("Testing without inaccessible, skipping %s", __func__);
×
UNCOV
573
                return;
×
574
        }
575

576
        r = find_executable("capsh", NULL);
×
577
        if (r < 0) {
×
578
                log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
×
UNCOV
579
                return;
×
580
        }
581

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

584
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) {
×
585
                test(m, "exec-protectkernelmodules-yes-capabilities.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
586
                test(m, "exec-protectkernelmodules-yes-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
587
        }
588
}
589

UNCOV
590
static void test_exec_readonlypaths(Manager *m) {
×
591

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

595
        if (path_is_read_only_fs("/var") > 0) {
×
596
                log_notice("Directory /var is readonly, skipping remaining tests in %s", __func__);
×
UNCOV
597
                return;
×
598
        }
599

600
        test(m, "exec-readonlypaths.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
601
        test(m, "exec-readonlypaths-with-bindpaths.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
602
        test(m, "exec-readonlypaths-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
603
}
604

UNCOV
605
static void test_exec_readwritepaths(Manager *m) {
×
606

607
        if (path_is_read_only_fs("/") > 0) {
×
608
                log_notice("Root directory is readonly, skipping %s", __func__);
×
UNCOV
609
                return;
×
610
        }
611

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

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

617
        if (!is_inaccessible_available()) {
×
618
                log_notice("Testing without inaccessible, skipping %s", __func__);
×
UNCOV
619
                return;
×
620
        }
621

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

625
        if (path_is_read_only_fs("/") > 0) {
×
626
                log_notice("Root directory is readonly, skipping remaining tests in %s", __func__);
×
UNCOV
627
                return;
×
628
        }
629

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

633
#if !HAS_FEATURE_ADDRESS_SANITIZER
634
static int on_spawn_io(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
×
635
        char **result = userdata;
×
636
        char buf[4096];
×
UNCOV
637
        ssize_t l;
×
638

639
        ASSERT_NOT_NULL(s);
×
UNCOV
640
        ASSERT_GT(fd, 0);
×
641

642
        l = read(fd, buf, sizeof(buf) - 1);
×
643
        if (l < 0) {
×
644
                if (errno == EAGAIN)
×
UNCOV
645
                        goto reenable;
×
646

UNCOV
647
                return 0;
×
648
        }
UNCOV
649
        if (l == 0)
×
650
                return 0;
651

652
        buf[l] = '\0';
×
653
        if (result)
×
UNCOV
654
                ASSERT_NOT_NULL(strextend(result, buf));
×
655
        else
UNCOV
656
                log_error("ldd: %s", buf);
×
657

UNCOV
658
reenable:
×
659
        /* Re-enable the event source if we did not encounter EOF */
UNCOV
660
        ASSERT_OK(sd_event_source_set_enabled(s, SD_EVENT_ONESHOT));
×
661
        return 0;
662
}
663

664
static int on_spawn_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
×
UNCOV
665
        pid_t *pid = userdata;
×
666

UNCOV
667
        ASSERT_NOT_NULL(pid);
×
668

UNCOV
669
        (void) kill(*pid, SIGKILL);
×
670

UNCOV
671
        return 1;
×
672
}
673

674
static int on_spawn_sigchld(sd_event_source *s, const siginfo_t *si, void *userdata) {
×
UNCOV
675
        int ret = -EIO;
×
676

UNCOV
677
        ASSERT_NOT_NULL(si);
×
678

679
        if (si->si_code == CLD_EXITED)
×
UNCOV
680
                ret = si->si_status;
×
681

682
        sd_event_exit(sd_event_source_get_event(s), ret);
×
UNCOV
683
        return 1;
×
684
}
685

686
static int find_libraries(const char *exec, char ***ret) {
×
687
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
×
688
        _cleanup_(sd_event_source_unrefp) sd_event_source *sigchld_source = NULL;
×
689
        _cleanup_(sd_event_source_unrefp) sd_event_source *stdout_source = NULL;
×
690
        _cleanup_(sd_event_source_unrefp) sd_event_source *stderr_source = NULL;
×
691
        _cleanup_close_pair_ int outpipe[2] = EBADF_PAIR, errpipe[2] = EBADF_PAIR;
×
692
        _cleanup_strv_free_ char **libraries = NULL;
×
693
        _cleanup_free_ char *result = NULL;
×
694
        pid_t pid;
×
UNCOV
695
        int r;
×
696

697
        ASSERT_NOT_NULL(exec);
×
UNCOV
698
        ASSERT_NOT_NULL(ret);
×
699

UNCOV
700
        ASSERT_OK(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD));
×
701

702
        ASSERT_OK_ERRNO(pipe2(outpipe, O_NONBLOCK|O_CLOEXEC));
×
UNCOV
703
        ASSERT_OK_ERRNO(pipe2(errpipe, O_NONBLOCK|O_CLOEXEC));
×
704

705
        r = safe_fork_full("(spawn-ldd)",
×
UNCOV
706
                           (int[]) { -EBADF, outpipe[1], errpipe[1] },
×
707
                           NULL, 0,
708
                           FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REARRANGE_STDIO|FORK_LOG, &pid);
709
        ASSERT_OK(r);
×
710
        if (r == 0) {
×
711
                execlp("ldd", "ldd", exec, NULL);
×
UNCOV
712
                _exit(EXIT_FAILURE);
×
713
        }
714

715
        outpipe[1] = safe_close(outpipe[1]);
×
UNCOV
716
        errpipe[1] = safe_close(errpipe[1]);
×
717

UNCOV
718
        ASSERT_OK(sd_event_new(&e));
×
719

UNCOV
720
        ASSERT_OK(sd_event_add_time_relative(e, NULL, CLOCK_MONOTONIC,
×
721
                                             10 * USEC_PER_SEC, USEC_PER_SEC, on_spawn_timeout, &pid));
722
        ASSERT_OK(sd_event_add_io(e, &stdout_source, outpipe[0], EPOLLIN, on_spawn_io, &result));
×
723
        ASSERT_OK(sd_event_source_set_enabled(stdout_source, SD_EVENT_ONESHOT));
×
724
        ASSERT_OK(sd_event_add_io(e, &stderr_source, errpipe[0], EPOLLIN, on_spawn_io, NULL));
×
725
        ASSERT_OK(sd_event_source_set_enabled(stderr_source, SD_EVENT_ONESHOT));
×
UNCOV
726
        ASSERT_OK(sd_event_add_child(e, &sigchld_source, pid, WEXITED, on_spawn_sigchld, NULL));
×
727
        /* SIGCHLD should be processed after IO is complete */
UNCOV
728
        ASSERT_OK(sd_event_source_set_priority(sigchld_source, SD_EVENT_PRIORITY_NORMAL + 1));
×
729

UNCOV
730
        ASSERT_OK(sd_event_loop(e));
×
731

732
        _cleanup_strv_free_ char **v = NULL;
×
UNCOV
733
        ASSERT_OK(strv_split_newlines_full(&v, result, 0));
×
734

735
        STRV_FOREACH(q, v) {
×
736
                _cleanup_free_ char *word = NULL;
×
UNCOV
737
                const char *p = *q;
×
738

739
                r = extract_first_word(&p, &word, NULL, 0);
×
740
                ASSERT_OK(r);
×
741
                if (r == 0)
×
UNCOV
742
                        continue;
×
743

744
                if (path_is_absolute(word)) {
×
745
                        ASSERT_OK(strv_consume(&libraries, TAKE_PTR(word)));
×
UNCOV
746
                        continue;
×
747
                }
748

749
                word = mfree(word);
×
750
                r = extract_first_word(&p, &word, NULL, 0);
×
751
                ASSERT_OK(r);
×
752
                if (r == 0)
×
UNCOV
753
                        continue;
×
754

755
                if (!streq_ptr(word, "=>"))
×
UNCOV
756
                        continue;
×
757

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

764
                if (path_is_absolute(word)) {
×
765
                        ASSERT_OK(strv_consume(&libraries, TAKE_PTR(word)));
×
UNCOV
766
                        continue;
×
767
                }
768
        }
769

770
        *ret = TAKE_PTR(libraries);
×
UNCOV
771
        return 0;
×
772
}
773
#endif
774

UNCOV
775
static void test_exec_mount_apivfs(Manager *m) {
×
776
#if !HAS_FEATURE_ADDRESS_SANITIZER
777
        _cleanup_free_ char *fullpath_touch = NULL, *fullpath_test = NULL, *data = NULL;
×
778
        _cleanup_strv_free_ char **libraries = NULL, **libraries_test = NULL;
×
UNCOV
779
        int r;
×
780

UNCOV
781
        ASSERT_NOT_NULL(user_runtime_unit_dir);
×
782

783
        r = find_executable("ldd", NULL);
×
784
        if (r < 0) {
×
785
                log_notice_errno(r, "Skipping %s, could not find 'ldd' command: %m", __func__);
×
UNCOV
786
                return;
×
787
        }
788
        r = find_executable("touch", &fullpath_touch);
×
789
        if (r < 0) {
×
790
                log_notice_errno(r, "Skipping %s, could not find 'touch' command: %m", __func__);
×
UNCOV
791
                return;
×
792
        }
793
        r = find_executable("test", &fullpath_test);
×
794
        if (r < 0) {
×
795
                log_notice_errno(r, "Skipping %s, could not find 'test' command: %m", __func__);
×
UNCOV
796
                return;
×
797
        }
798

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

802
        ASSERT_OK(find_libraries(fullpath_touch, &libraries));
×
803
        ASSERT_OK(find_libraries(fullpath_test, &libraries_test));
×
UNCOV
804
        ASSERT_OK(strv_extend_strv(&libraries, libraries_test, true));
×
805

806
        ASSERT_NOT_NULL(strextend(&data, "[Service]\n"));
×
807
        ASSERT_NOT_NULL((strextend(&data, "ExecStart=", fullpath_touch, " /aaa\n")));
×
808
        ASSERT_NOT_NULL((strextend(&data, "ExecStart=", fullpath_test, " -f /aaa\n")));
×
809
        ASSERT_NOT_NULL((strextend(&data, "BindReadOnlyPaths=", fullpath_touch, "\n")));
×
UNCOV
810
        ASSERT_NOT_NULL((strextend(&data, "BindReadOnlyPaths=", fullpath_test, "\n")));
×
811

812
        STRV_FOREACH(p, libraries)
×
UNCOV
813
                ASSERT_NOT_NULL((strextend(&data, "BindReadOnlyPaths=", *p, "\n")));
×
814

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

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

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

UNCOV
821
        (void) rm_rf("/tmp/test-exec-mount-apivfs-no/root", REMOVE_ROOT|REMOVE_PHYSICAL);
×
822
#endif
823
}
824

UNCOV
825
static void test_exec_noexecpaths(Manager *m) {
×
826

827
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges())
×
UNCOV
828
                test(m, "exec-noexecpaths-simple.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
829
        else
UNCOV
830
                return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__);
×
831
}
832

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

835
        test(m, "exec-temporaryfilesystem-options.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
836
        test(m, "exec-temporaryfilesystem-ro.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
837
        test(m, "exec-temporaryfilesystem-rw.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
838
        test(m, "exec-temporaryfilesystem-usr.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
839
}
×
840

UNCOV
841
static void test_exec_systemcallfilter(Manager *m) {
×
842
#if HAVE_SECCOMP && !HAS_FEATURE_ADDRESS_SANITIZER
UNCOV
843
        int r;
×
844

845
        if (!is_seccomp_available()) {
×
846
                log_notice("Seccomp not available, skipping %s", __func__);
×
UNCOV
847
                return;
×
848
        }
849

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

852
        test(m, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED);
×
853
        test(m, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED);
×
854
        test(m, "exec-systemcallfilter-not-failing3.service", 0, CLD_EXITED);
×
855
        test(m, "exec-systemcallfilter-failing.service", SIGSYS, CLD_KILLED);
×
856
        test(m, "exec-systemcallfilter-failing2.service", SIGSYS, CLD_KILLED);
×
UNCOV
857
        test(m, "exec-systemcallfilter-failing3.service", SIGSYS, CLD_KILLED);
×
858

859
        r = find_executable("python3", NULL);
×
860
        if (r < 0) {
×
861
                log_notice_errno(r, "Skipping remaining tests in %s, could not find python3 binary: %m", __func__);
×
UNCOV
862
                return;
×
863
        }
864

865
        test(m, "exec-systemcallfilter-with-errno-name.service", errno_from_name("EILSEQ"), CLD_EXITED);
×
866
        test(m, "exec-systemcallfilter-with-errno-number.service", 255, CLD_EXITED);
×
867
        test(m, "exec-systemcallfilter-with-errno-multi.service", errno_from_name("EILSEQ"), CLD_EXITED);
×
868
        test(m, "exec-systemcallfilter-with-errno-in-allow-list.service", errno_from_name("EILSEQ"), CLD_EXITED);
×
869
        test(m, "exec-systemcallfilter-override-error-action.service", SIGSYS, CLD_KILLED);
×
UNCOV
870
        test(m, "exec-systemcallfilter-override-error-action2.service", errno_from_name("EILSEQ"), CLD_EXITED);
×
871

872
        test(m, "exec-systemcallfilter-nonewprivileges.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
UNCOV
873
        test(m, "exec-systemcallfilter-nonewprivileges-protectclock.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
874

875
        r = find_executable("capsh", NULL);
×
876
        if (r < 0) {
×
877
                log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
×
UNCOV
878
                return;
×
879
        }
880

881
        test(m, "exec-systemcallfilter-nonewprivileges-bounding1.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
UNCOV
882
        test(m, "exec-systemcallfilter-nonewprivileges-bounding2.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
883
#endif
884
}
885

UNCOV
886
static void test_exec_systemcallerrornumber(Manager *m) {
×
887
#if HAVE_SECCOMP && !HAS_FEATURE_ADDRESS_SANITIZER
UNCOV
888
        int r;
×
889

890
        if (!is_seccomp_available()) {
×
891
                log_notice("Seccomp not available, skipping %s", __func__);
×
UNCOV
892
                return;
×
893
        }
894

895
        r = find_executable("python3", NULL);
×
896
        if (r < 0) {
×
897
                log_notice_errno(r, "Skipping %s, could not find python3 binary: %m", __func__);
×
UNCOV
898
                return;
×
899
        }
900

901
        test(m, "exec-systemcallerrornumber-name.service", errno_from_name("EACCES"), CLD_EXITED);
×
UNCOV
902
        test(m, "exec-systemcallerrornumber-number.service", 255, CLD_EXITED);
×
903
#endif
904
}
905

UNCOV
906
static void test_exec_restrictnamespaces(Manager *m) {
×
907
#if HAVE_SECCOMP
908
        if (!is_seccomp_available()) {
×
909
                log_notice("Seccomp not available, skipping %s", __func__);
×
UNCOV
910
                return;
×
911
        }
912

913
        test(m, "exec-restrictnamespaces-no.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
×
914
        test(m, "exec-restrictnamespaces-yes.service", 1, CLD_EXITED);
×
915
        test(m, "exec-restrictnamespaces-mnt.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
×
916
        test(m, "exec-restrictnamespaces-mnt-deny-list.service", 1, CLD_EXITED);
×
917
        test(m, "exec-restrictnamespaces-merge-and.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
×
918
        test(m, "exec-restrictnamespaces-merge-or.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
×
UNCOV
919
        test(m, "exec-restrictnamespaces-merge-all.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
×
920
#endif
921
}
922

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

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

937
        if (!check_nobody_user_and_group()) {
×
938
                log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
×
UNCOV
939
                return;
×
940
        }
941

942
        if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
×
943
                log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
×
UNCOV
944
                return;
×
945
        }
946

UNCOV
947
        test(m, "exec-systemcallfilter-system-user-" NOBODY_USER_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
948
#endif
949
}
950

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

954
        if (!check_nobody_user_and_group()) {
×
955
                log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
×
UNCOV
956
                return;
×
957
        }
958

959
        if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
×
960
                log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
×
UNCOV
961
                return;
×
962
        }
963

UNCOV
964
        test(m, "exec-user-" NOBODY_USER_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
965
}
966

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

970
        if (!check_nobody_user_and_group()) {
×
971
                log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
×
UNCOV
972
                return;
×
973
        }
974

975
        if (!STR_IN_SET(NOBODY_GROUP_NAME, "nobody", "nfsnobody", "nogroup")) {
×
976
                log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME, __func__);
×
UNCOV
977
                return;
×
978
        }
979

UNCOV
980
        test(m, "exec-group-" NOBODY_GROUP_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
981
}
982

983
static void test_exec_supplementarygroups(Manager *m) {
×
984
        int status = MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP;
×
985
        test(m, "exec-supplementarygroups.service", status, CLD_EXITED);
×
986
        test(m, "exec-supplementarygroups-single-group.service", status, CLD_EXITED);
×
987
        test(m, "exec-supplementarygroups-single-group-user.service", status, CLD_EXITED);
×
988
        test(m, "exec-supplementarygroups-multiple-groups-default-group-user.service", status, CLD_EXITED);
×
989
        test(m, "exec-supplementarygroups-multiple-groups-withgid.service", status, CLD_EXITED);
×
990
        test(m, "exec-supplementarygroups-multiple-groups-withuid.service", status, CLD_EXITED);
×
UNCOV
991
}
×
992

UNCOV
993
static char* private_directory_bad(Manager *m) {
×
994
        /* This mirrors setup_exec_directory(). */
995

996
        for (ExecDirectoryType dt = 0; dt < _EXEC_DIRECTORY_TYPE_MAX; dt++) {
×
997
                _cleanup_free_ char *p = NULL;
×
UNCOV
998
                struct stat st;
×
999

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

1002
                if (stat(p, &st) >= 0 &&
×
1003
                    (st.st_mode & (S_IRWXG|S_IRWXO)))
×
UNCOV
1004
                        return TAKE_PTR(p);
×
1005
        }
1006

1007
        return NULL;
1008
}
1009

1010
static void test_exec_dynamicuser(Manager *m) {
×
1011
        if (MANAGER_IS_USER(m)) {
×
1012
                log_notice("Skipping %s for user manager", __func__);
×
UNCOV
1013
                return;
×
1014
        }
1015

1016
        _cleanup_free_ char *bad = private_directory_bad(m);
×
1017
        if (bad) {
×
1018
                log_warning("%s: %s has bad permissions, skipping test.", __func__, bad);
×
UNCOV
1019
                return;
×
1020
        }
1021

1022
        if (strstr_ptr(ci_environment(), "github-actions")) {
×
1023
                log_notice("%s: skipping test on GH Actions because of systemd/systemd#10337", __func__);
×
UNCOV
1024
                return;
×
1025
        }
1026

UNCOV
1027
        int status = can_unshare ? 0 : EXIT_NAMESPACE;
×
1028

1029
        test(m, "exec-dynamicuser-fixeduser.service", status, CLD_EXITED);
×
1030
        if (check_user_has_group_with_same_name("adm"))
×
1031
                test(m, "exec-dynamicuser-fixeduser-adm.service", status, CLD_EXITED);
×
1032
        if (check_user_has_group_with_same_name("games"))
×
1033
                test(m, "exec-dynamicuser-fixeduser-games.service", status, CLD_EXITED);
×
1034
        test(m, "exec-dynamicuser-fixeduser-one-supplementarygroup.service", status, CLD_EXITED);
×
1035
        test(m, "exec-dynamicuser-supplementarygroups.service", status, CLD_EXITED);
×
UNCOV
1036
        test(m, "exec-dynamicuser-statedir.service", status, CLD_EXITED);
×
1037

1038
        (void) rm_rf("/var/lib/quux", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1039
        (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1040
        (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1041
        (void) rm_rf("/var/lib/waldo", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1042
        (void) rm_rf("/var/lib/private/quux", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1043
        (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1044
        (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
UNCOV
1045
        (void) rm_rf("/var/lib/private/waldo", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1046

1047
        test(m, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED);
×
1048
        test(m, "exec-dynamicuser-statedir-migrate-step2.service", status, CLD_EXITED);
×
UNCOV
1049
        test(m, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED);
×
1050

1051
        (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1052
        (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1053
        (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
×
UNCOV
1054
        (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1055

1056
        test(m, "exec-dynamicuser-runtimedirectory1.service", status, CLD_EXITED);
×
1057
        test(m, "exec-dynamicuser-runtimedirectory2.service", status, CLD_EXITED);
×
UNCOV
1058
        test(m, "exec-dynamicuser-runtimedirectory3.service", status, CLD_EXITED);
×
1059
}
1060

1061
static void test_exec_environment(Manager *m) {
×
1062
        test(m, "exec-environment-no-substitute.service", 0, CLD_EXITED);
×
1063
        test(m, "exec-environment.service", 0, CLD_EXITED);
×
1064
        test(m, "exec-environment-multiple.service", 0, CLD_EXITED);
×
1065
        test(m, "exec-environment-empty.service", 0, CLD_EXITED);
×
UNCOV
1066
}
×
1067

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

1082
        r = write_string_file("/tmp/test-exec_environmentfile.conf", e, WRITE_STRING_FILE_CREATE);
×
UNCOV
1083
        ASSERT_OK(r);
×
1084

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

1087
        (void) unlink("/tmp/test-exec_environmentfile.conf");
×
UNCOV
1088
}
×
1089

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

1118
static void test_exec_umask(Manager *m) {
×
1119
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) {
×
1120
                test(m, "exec-umask-default.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
1121
                test(m, "exec-umask-0177.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
1122
        } else
UNCOV
1123
                return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__);
×
1124
}
1125

1126
static void test_exec_runtimedirectory(Manager *m) {
×
1127
        (void) rm_rf("/run/test-exec_runtimedirectory2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1128
        test(m, "exec-runtimedirectory.service", 0, CLD_EXITED);
×
UNCOV
1129
        (void) rm_rf("/run/test-exec_runtimedirectory2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1130

1131
        test(m, "exec-runtimedirectory-mode.service", 0, CLD_EXITED);
×
UNCOV
1132
        test(m, "exec-runtimedirectory-owner.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
1133

1134
        if (!check_nobody_user_and_group()) {
×
1135
                log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
×
UNCOV
1136
                return;
×
1137
        }
1138

1139
        if (!STR_IN_SET(NOBODY_GROUP_NAME, "nobody", "nfsnobody", "nogroup")) {
×
1140
                log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME, __func__);
×
UNCOV
1141
                return;
×
1142
        }
1143

UNCOV
1144
        test(m, "exec-runtimedirectory-owner-" NOBODY_GROUP_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
1145
}
1146

1147
static void test_exec_capabilityboundingset(Manager *m) {
×
UNCOV
1148
        int r;
×
1149

1150
        r = find_executable("capsh", NULL);
×
1151
        if (r < 0) {
×
1152
                log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
×
UNCOV
1153
                return;
×
1154
        }
1155

1156
        if (have_effective_cap(CAP_CHOWN) <= 0 ||
×
1157
            have_effective_cap(CAP_FOWNER) <= 0 ||
×
1158
            have_effective_cap(CAP_KILL) <= 0) {
×
1159
                log_notice("Skipping %s, this process does not have enough capabilities", __func__);
×
UNCOV
1160
                return;
×
1161
        }
1162

1163
        test(m, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED);
×
1164
        test(m, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED);
×
1165
        test(m, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED);
×
UNCOV
1166
        test(m, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED);
×
1167
}
1168

1169
static void test_exec_basic(Manager *m) {
×
1170
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges())
×
UNCOV
1171
                test(m, "exec-basic.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
1172
        else
UNCOV
1173
                return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__);
×
1174
}
1175

1176
static void test_exec_ambientcapabilities(Manager *m) {
×
UNCOV
1177
        int r;
×
1178

1179
        /* Check if the kernel has support for ambient capabilities. Run
1180
         * the tests only if that's the case. Clearing all ambient
1181
         * capabilities is fine, since we are expecting them to be unset
1182
         * in the first place for the tests. */
1183
        r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
×
1184
        if (r < 0 && IN_SET(errno, EINVAL, EOPNOTSUPP, ENOSYS)) {
×
1185
                log_notice("Skipping %s, the kernel does not support ambient capabilities", __func__);
×
UNCOV
1186
                return;
×
1187
        }
1188

1189
        if (have_effective_cap(CAP_CHOWN) <= 0 ||
×
1190
            have_effective_cap(CAP_NET_RAW) <= 0) {
×
1191
                log_notice("Skipping %s, this process does not have enough capabilities", __func__);
×
UNCOV
1192
                return;
×
1193
        }
1194

1195
        test(m, "exec-ambientcapabilities.service", 0, CLD_EXITED);
×
UNCOV
1196
        test(m, "exec-ambientcapabilities-merge.service", 0, CLD_EXITED);
×
1197

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

1201
        if (!check_nobody_user_and_group()) {
×
1202
                log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
×
UNCOV
1203
                return;
×
1204
        }
1205

1206
        if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
×
1207
                log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
×
UNCOV
1208
                return;
×
1209
        }
1210

1211
        test(m, "exec-ambientcapabilities-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
×
UNCOV
1212
        test(m, "exec-ambientcapabilities-merge-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
×
1213
}
1214

1215
static void test_exec_privatenetwork(Manager *m) {
×
UNCOV
1216
        int r;
×
1217

1218
        if (!have_net_dummy)
×
UNCOV
1219
                return (void)log_notice("Skipping %s, dummy network interface not available", __func__);
×
1220

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

1224
        r = find_executable("ip", NULL);
×
1225
        if (r < 0) {
×
1226
                log_notice_errno(r, "Skipping %s, could not find ip binary: %m", __func__);
×
UNCOV
1227
                return;
×
1228
        }
1229

1230
        test(m, "exec-privatenetwork-yes-privatemounts-no.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NETWORK : EXIT_FAILURE, CLD_EXITED);
×
UNCOV
1231
        test(m, "exec-privatenetwork-yes-privatemounts-yes.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NETWORK : EXIT_NAMESPACE, CLD_EXITED);
×
1232
}
1233

1234
static void test_exec_networknamespacepath(Manager *m) {
×
UNCOV
1235
        int r;
×
1236

1237
        if (!have_net_dummy)
×
UNCOV
1238
                return (void)log_notice("Skipping %s, dummy network interface not available", __func__);
×
1239

1240
        if (!have_netns)
×
UNCOV
1241
                return (void)log_notice("Skipping %s, network namespace not available", __func__);
×
1242

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

1246
        r = find_executable("ip", NULL);
×
1247
        if (r < 0) {
×
1248
                log_notice_errno(r, "Skipping %s, could not find ip binary: %m", __func__);
×
UNCOV
1249
                return;
×
1250
        }
1251

1252
        test(m, "exec-networknamespacepath-privatemounts-no.service", MANAGER_IS_SYSTEM(m) ? EXIT_SUCCESS : EXIT_FAILURE, CLD_EXITED);
×
UNCOV
1253
        test(m, "exec-networknamespacepath-privatemounts-yes.service", can_unshare ? EXIT_SUCCESS : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
1254
}
1255

1256
static void test_exec_oomscoreadjust(Manager *m) {
×
UNCOV
1257
        test(m, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED);
×
1258

1259
        if (detect_container() > 0) {
×
1260
                log_notice("Testing in container, skipping remaining tests in %s", __func__);
×
UNCOV
1261
                return;
×
1262
        }
UNCOV
1263
        test(m, "exec-oomscoreadjust-negative.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED);
×
1264
}
1265

1266
static void test_exec_ioschedulingclass(Manager *m) {
×
1267
        test(m, "exec-ioschedulingclass-none.service", 0, CLD_EXITED);
×
1268
        test(m, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED);
×
UNCOV
1269
        test(m, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED);
×
1270

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

1278
static void test_exec_unsetenvironment(Manager *m) {
×
1279
        test(m, "exec-unsetenvironment.service", 0, CLD_EXITED);
×
UNCOV
1280
}
×
1281

1282
static void test_exec_specifier(Manager *m) {
×
1283
        test(m, "exec-specifier.service", 0, CLD_EXITED);
×
1284
        if (MANAGER_IS_SYSTEM(m))
×
UNCOV
1285
                test(m, "exec-specifier-system.service", 0, CLD_EXITED);
×
1286
        else
1287
                test(m, "exec-specifier-user.service", 0, CLD_EXITED);
×
1288
        test(m, "exec-specifier@foo-bar.service", 0, CLD_EXITED);
×
1289
        test(m, "exec-specifier-interpolation.service", 0, CLD_EXITED);
×
UNCOV
1290
}
×
1291

1292
static void test_exec_standardinput(Manager *m) {
×
1293
        test(m, "exec-standardinput-data.service", 0, CLD_EXITED);
×
UNCOV
1294
        test(m, "exec-standardinput-file.service", 0, CLD_EXITED);
×
1295

1296
        ExecOutput saved = m->defaults.std_output;
×
1297
        m->defaults.std_output = EXEC_OUTPUT_NULL;
×
1298
        test(m, "exec-standardinput-file-cat.service", 0, CLD_EXITED);
×
1299
        m->defaults.std_output = saved;
×
UNCOV
1300
}
×
1301

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

1306
static void test_exec_standardoutput_append(Manager *m) {
×
1307
        test(m, "exec-standardoutput-append.service", 0, CLD_EXITED);
×
UNCOV
1308
}
×
1309

1310
static void test_exec_standardoutput_truncate(Manager *m) {
×
1311
        test(m, "exec-standardoutput-truncate.service", 0, CLD_EXITED);
×
UNCOV
1312
}
×
1313

1314
static void test_exec_condition(Manager *m) {
×
1315
        test_service(m, "exec-condition-failed.service", SERVICE_FAILURE_EXIT_CODE);
×
1316
        test_service(m, "exec-condition-skip.service", SERVICE_SKIP_CONDITION);
×
UNCOV
1317
}
×
1318

UNCOV
1319
static void test_exec_umask_namespace(Manager *m) {
×
1320
        /* exec-specifier-credentials-dir.service creates /run/credentials and enables implicit
1321
         * InaccessiblePath= for the directory for all later services with mount namespace. */
1322
        if (!is_inaccessible_available()) {
×
1323
                log_notice("Testing without inaccessible, skipping %s", __func__);
×
UNCOV
1324
                return;
×
1325
        }
UNCOV
1326
        test(m, "exec-umask-namespace.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NAMESPACE : EXIT_GROUP, CLD_EXITED);
×
1327
}
1328

1329
typedef struct test_entry {
1330
        test_function_t f;
1331
        const char *name;
1332
} test_entry;
1333

1334
#define entry(x) {x, #x}
1335

1336
static void run_tests(RuntimeScope scope, char **patterns) {
×
1337
        _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
×
1338
        _cleanup_free_ char *unit_paths = NULL;
×
1339
        _cleanup_(manager_freep) Manager *m = NULL;
×
1340
        usec_t start, finish;
×
UNCOV
1341
        int r;
×
1342

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

1396
        ASSERT_OK_ERRNO(unsetenv("USER"));
×
1397
        ASSERT_OK_ERRNO(unsetenv("LOGNAME"));
×
1398
        ASSERT_OK_ERRNO(unsetenv("SHELL"));
×
1399
        ASSERT_OK_ERRNO(unsetenv("HOME"));
×
UNCOV
1400
        ASSERT_OK_ERRNO(unsetenv("TMPDIR"));
×
1401

1402
        /* Unset VARx, especially, VAR1, VAR2 and VAR3, which are used in the PassEnvironment test cases,
1403
         * otherwise (and if they are present in the environment), `manager_default_environment` will copy
1404
         * them into the default environment which is passed to each created job, which will make the tests
1405
         * that expect those not to be present to fail. */
1406
        ASSERT_OK_ERRNO(unsetenv("VAR1"));
×
1407
        ASSERT_OK_ERRNO(unsetenv("VAR2"));
×
1408
        ASSERT_OK_ERRNO(unsetenv("VAR3"));
×
1409
        ASSERT_OK_ERRNO(unsetenv("VAR4"));
×
UNCOV
1410
        ASSERT_OK_ERRNO(unsetenv("VAR5"));
×
1411

1412
        ASSERT_NOT_NULL((runtime_dir = setup_fake_runtime_dir()));
×
1413
        ASSERT_NOT_NULL((user_runtime_unit_dir = path_join(runtime_dir, "systemd/user")));
×
1414
        ASSERT_NOT_NULL((unit_paths = strjoin(PRIVATE_UNIT_DIR, ":", user_runtime_unit_dir)));
×
UNCOV
1415
        ASSERT_OK(setenv_unit_path(unit_paths));
×
1416

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

1421
        r = manager_new(scope, MANAGER_TEST_RUN_BASIC, &m);
×
1422
        if (manager_errno_skip_test(r))
×
1423
                return (void) log_tests_skipped_errno(r, "manager_new");
×
UNCOV
1424
        ASSERT_OK(r);
×
1425

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

1429
        /* Uncomment below if you want to make debugging logs stored to journal. */
1430
        //manager_override_log_target(m, LOG_TARGET_AUTO);
1431
        //manager_override_log_level(m, LOG_DEBUG);
1432

1433
        /* Measure and print the time that it takes to run tests, excluding startup of the manager object,
1434
         * to try and measure latency of spawning services */
1435
        n_ran_tests = 0;
×
UNCOV
1436
        start = now(CLOCK_MONOTONIC);
×
1437

1438
        for (const test_entry *test = tests; test->f; test++)
×
1439
                if (strv_fnmatch_or_empty(patterns, test->name, FNM_NOESCAPE)) {
×
1440
                        log_info("Starting %s.", test->name);
×
UNCOV
1441
                        test->f(m);
×
1442
                } else
UNCOV
1443
                        log_info("Skipping %s because it does not match any pattern.", test->name);
×
1444

UNCOV
1445
        finish = now(CLOCK_MONOTONIC);
×
1446

UNCOV
1447
        log_info("ran %u tests with %s manager + unshare=%s in: %s",
×
1448
                 n_ran_tests,
1449
                 scope == RUNTIME_SCOPE_SYSTEM ? "system" : "user",
1450
                 yes_no(can_unshare),
1451
                 FORMAT_TIMESPAN(finish - start, USEC_PER_MSEC));
1452
}
1453

1454
static int prepare_ns(const char *process_name) {
×
UNCOV
1455
        int r;
×
1456

UNCOV
1457
        r = safe_fork(process_name,
×
1458
                      FORK_RESET_SIGNALS |
1459
                      FORK_CLOSE_ALL_FDS |
1460
                      FORK_DEATHSIG_SIGTERM |
1461
                      FORK_WAIT |
1462
                      FORK_REOPEN_LOG |
1463
                      FORK_LOG |
1464
                      FORK_NEW_MOUNTNS |
1465
                      FORK_MOUNTNS_SLAVE,
1466
                      NULL);
1467
        ASSERT_OK(r);
×
1468
        if (r == 0) {
×
1469
                _cleanup_free_ char *unit_dir = NULL, *build_dir = NULL, *build_dir_mount = NULL;
×
UNCOV
1470
                int ret;
×
1471

1472
                const char *coverage = getenv("COVERAGE_BUILD_DIR");
×
UNCOV
1473
                if (!coverage)
×
1474
                        /* Make "/" read-only. */
UNCOV
1475
                        ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, NULL, "/", NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL));
×
1476

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

1482
                ASSERT_OK(mkdir_p(PRIVATE_UNIT_DIR, 0755));
×
UNCOV
1483
                ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, "tmpfs", PRIVATE_UNIT_DIR, "tmpfs", MS_NOSUID|MS_NODEV, NULL));
×
1484
                /* Mark our test "playground" as MS_SLAVE, so we can MS_MOVE mounts underneath it. */
UNCOV
1485
                ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, NULL, PRIVATE_UNIT_DIR, NULL, MS_SLAVE, NULL));
×
1486

1487
                /* Copy unit files to make them accessible even when unprivileged. */
1488
                ASSERT_OK(get_testdata_dir("test-execute/", &unit_dir));
×
UNCOV
1489
                ASSERT_OK(copy_directory_at(AT_FDCWD, unit_dir, AT_FDCWD, PRIVATE_UNIT_DIR, COPY_MERGE_EMPTY));
×
1490

1491
                /* Mount tmpfs on the following directories to make not StateDirectory= or friends disturb the host. */
1492
                ret = get_build_exec_dir(&build_dir);
×
1493
                if (ret != -ENOEXEC)
×
UNCOV
1494
                        ASSERT_OK(ret);
×
1495

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

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

1509
                if (build_dir_mount) {
×
1510
                        ret = RET_NERRNO(access(build_dir, F_OK));
×
1511
                        if (ret != -ENOENT)
×
UNCOV
1512
                                ASSERT_OK(ret);
×
1513

UNCOV
1514
                        if (ret == -ENOENT) {
×
1515
                                /* The build directory got overmounted by tmpfs, so let's use the "backup" bind mount to
1516
                                 * bring it back. */
1517
                                ASSERT_OK(mkdir_p(build_dir, 0755));
×
UNCOV
1518
                                ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, build_dir_mount, build_dir, NULL, MS_MOVE, NULL));
×
1519
                        }
1520
                }
1521

1522
                /* Prepare credstore like tmpfiles.d/credstore.conf for LoadCredential= tests. */
1523
                FOREACH_STRING(p, "/run/credstore", "/run/credstore.encrypted") {
×
1524
                        ASSERT_OK(mkdir_p(p, 0700));
×
UNCOV
1525
                        ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, "tmpfs", p, "tmpfs", MS_NOSUID|MS_NODEV, "mode=0700"));
×
1526
                }
1527

UNCOV
1528
                ASSERT_OK(write_string_file("/run/credstore/test-execute.load-credential", "foo", WRITE_STRING_FILE_CREATE));
×
1529
        }
1530

UNCOV
1531
        return r;
×
1532
}
1533

1534
TEST(run_tests_root) {
×
UNCOV
1535
        _cleanup_strv_free_ char **filters = NULL;
×
1536

1537
        if (!have_namespaces())
×
UNCOV
1538
                return (void) log_tests_skipped("unshare() is disabled");
×
1539

1540
        /* safe_fork() clears saved_argv in the child process. Let's copy it. */
UNCOV
1541
        ASSERT_NOT_NULL((filters = strv_copy(strv_skip(saved_argv, 1))));
×
1542

1543
        if (prepare_ns("(test-execute-root)") == 0) {
×
1544
                can_unshare = true;
×
1545
                run_tests(RUNTIME_SCOPE_SYSTEM, filters);
×
UNCOV
1546
                _exit(EXIT_SUCCESS);
×
1547
        }
1548
}
1549

1550
TEST(run_tests_without_unshare) {
×
UNCOV
1551
        if (!have_namespaces()) {
×
1552
                /* unshare() is already filtered. */
1553
                can_unshare = false;
×
1554
                run_tests(RUNTIME_SCOPE_SYSTEM, strv_skip(saved_argv, 1));
×
UNCOV
1555
                return;
×
1556
        }
1557

1558
#if HAVE_SECCOMP
1559
        _cleanup_strv_free_ char **filters = NULL;
×
UNCOV
1560
        int r;
×
1561

1562
        /* The following tests are for 1beab8b0d0ff2d7d1436b52d4a0c3d56dc908962. */
1563
        if (!is_seccomp_available())
×
UNCOV
1564
                return (void) log_tests_skipped("Seccomp not available, cannot run unshare() filtered tests");
×
1565

1566
        /* safe_fork() clears saved_argv in the child process. Let's copy it. */
UNCOV
1567
        ASSERT_NOT_NULL((filters = strv_copy(strv_skip(saved_argv, 1))));
×
1568

1569
        if (prepare_ns("(test-execute-without-unshare)") == 0) {
×
UNCOV
1570
                _cleanup_hashmap_free_ Hashmap *s = NULL;
×
1571

1572
                r = seccomp_syscall_resolve_name("unshare");
×
1573
                ASSERT_NE(r, __NR_SCMP_ERROR);
×
1574
                ASSERT_OK(hashmap_ensure_put(&s, NULL, UINT32_TO_PTR(r + 1), INT_TO_PTR(-1)));
×
UNCOV
1575
                ASSERT_OK(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EOPNOTSUPP), true));
×
1576

1577
                /* Check unshare() is actually filtered. */
UNCOV
1578
                ASSERT_ERROR_ERRNO(unshare(CLONE_NEWNS), EOPNOTSUPP);
×
1579

1580
                can_unshare = false;
×
1581
                run_tests(RUNTIME_SCOPE_SYSTEM, filters);
×
UNCOV
1582
                _exit(EXIT_SUCCESS);
×
1583
        }
1584
#else
1585
        log_tests_skipped("Built without seccomp support, cannot run unshare() filtered tests");
1586
#endif
1587
}
1588

1589
TEST(run_tests_unprivileged) {
×
UNCOV
1590
        _cleanup_strv_free_ char **filters = NULL;
×
1591

1592
        if (!have_namespaces())
×
UNCOV
1593
                return (void) log_tests_skipped("unshare() is disabled");
×
1594

1595
        /* safe_fork() clears saved_argv in the child process. Let's copy it. */
UNCOV
1596
        ASSERT_NOT_NULL((filters = strv_copy(strv_skip(saved_argv, 1))));
×
1597

1598
        if (prepare_ns("(test-execute-unprivileged)") == 0) {
×
UNCOV
1599
                ASSERT_OK(capability_bounding_set_drop(0, /* right_now = */ true));
×
1600

1601
                can_unshare = false;
×
1602
                run_tests(RUNTIME_SCOPE_USER, filters);
×
UNCOV
1603
                _exit(EXIT_SUCCESS);
×
1604
        }
1605
}
1606

1607
static int intro(void) {
1✔
1608
#if HAS_FEATURE_ADDRESS_SANITIZER
1609
        if (strstr_ptr(ci_environment(), "travis") || strstr_ptr(ci_environment(), "github-actions"))
1610
                return log_tests_skipped("Running on Travis CI/GH Actions under ASan, see https://github.com/systemd/systemd/issues/10696");
1611
#endif
1612
        /* It is needed otherwise cgroup creation fails */
1613
        if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0)
1✔
UNCOV
1614
                return log_tests_skipped("not privileged");
×
1615

1616
        if (enter_cgroup_subroot(NULL) == -ENOMEDIUM)
1✔
UNCOV
1617
                return log_tests_skipped("cgroupfs not available");
×
1618

1619
        if (path_is_read_only_fs("/sys") > 0)
1✔
1620
                return log_tests_skipped("/sys is mounted read-only");
1✔
1621

1622
        /* Create dummy network interface for testing PrivateNetwork=yes */
UNCOV
1623
        have_net_dummy = system("ip link add dummy-test-exec type dummy") == 0;
×
1624

UNCOV
1625
        if (have_net_dummy) {
×
1626
                /* Create a network namespace and a dummy interface in it for NetworkNamespacePath= */
1627
                have_netns = system("ip netns add test-execute-netns") == 0;
×
UNCOV
1628
                have_netns = have_netns && system("ip netns exec test-execute-netns ip link add dummy-test-ns type dummy") == 0;
×
1629
        }
1630

1631
        return EXIT_SUCCESS;
1632
}
1633

1634
static int outro(void) {
1✔
1635
        if (have_net_dummy) {
1✔
1636
                (void) system("ip link del dummy-test-exec");
×
UNCOV
1637
                (void) system("ip netns del test-execute-netns");
×
1638
        }
1639

1640
        (void) rmdir(PRIVATE_UNIT_DIR);
1✔
1641

1642
        return EXIT_SUCCESS;
1✔
1643
}
1644

1645
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