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

systemd / systemd / 28272947092

26 Jun 2026 08:38PM UTC coverage: 72.893% (+0.2%) from 72.703%
28272947092

push

github

poettering
sysupdate: Address review feedback on CheckNew varlink scaffolding

Follow-up to #42422:

 - Rename process_image() to context_process_image(), since it now
   operates on a Context object.
 - Use IN_SET() in image_type_can_sysupdate() instead of a switch.
 - Name the return parameters of context_list_components() ret_xyz, per
   our coding style.
 - Drop a redundant "else" after a return in vl_method_check_new().

9 of 11 new or added lines in 1 file covered. (81.82%)

12567 existing lines in 144 files now uncovered.

341026 of 467845 relevant lines covered (72.89%)

1339355.33 hits per line

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

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

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

11
#include "sd-event.h"
12

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

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

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

52
STATIC_DESTRUCTOR_REGISTER(user_runtime_unit_dir, freep);
1✔
53

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

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

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

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

72
        abort();
×
73
}
74

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

79
        ASSERT_NOT_NULL(m);
×
80

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

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

89
        /* Use a per-Exec timeout rather than a service timeout, as especially under sanitizers some test
90
         * units running many commands can hit the service timeout. */
UNCOV
91
        _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
×
UNCOV
92
        ASSERT_OK(sd_event_add_time_relative(m->event, &s, CLOCK_MONOTONIC, timeout, 0, time_handler, unit));
×
93

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

99
                if (service->main_command != last_command) {
×
UNCOV
100
                        last_command = service->main_command;
×
101
                        ASSERT_OK(sd_event_source_set_time_relative(s, timeout));
×
102
                }
103
        }
104
}
×
105

106
static void check_main_result(const char *file, unsigned line, const char *func,
×
107
                              Manager *m, Unit *unit, int status_expected, int code_expected) {
UNCOV
108
        Service *service = NULL;
×
109

110
        ASSERT_NOT_NULL(m);
×
UNCOV
111
        ASSERT_NOT_NULL(unit);
×
112

113
        wait_for_service_finish(m, unit);
×
114

UNCOV
115
        service = SERVICE(unit);
×
116
        exec_status_dump(&service->main_exec_status, stdout, "\t");
×
117

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

UNCOV
125
        if (service->main_exec_status.status != status_expected) {
×
126
                log_error("%s:%u:%s: %s: can_unshare=%s: exit status %d, expected %d",
×
127
                          file, line, func, unit->id, yes_no(can_unshare),
128
                          service->main_exec_status.status, status_expected);
129
                abort();
×
130
        }
131
}
×
132

133
static void check_service_result(const char *file, unsigned line, const char *func,
×
134
                                 Manager *m, Unit *unit, ServiceResult result_expected) {
135
        Service *service = NULL;
×
136

UNCOV
137
        ASSERT_NOT_NULL(m);
×
UNCOV
138
        ASSERT_NOT_NULL(unit);
×
139

140
        wait_for_service_finish(m, unit);
×
141

142
        service = SERVICE(unit);
×
143

144
        if (service->result != result_expected) {
×
145
                log_error("%s:%u:%s: %s: can_unshare=%s: service end result %s, expected %s",
×
146
                          file, line, func, unit->id, yes_no(can_unshare),
147
                          service_result_to_string(service->result),
148
                          service_result_to_string(result_expected));
149
                abort();
×
150
        }
UNCOV
151
}
×
152

153
static bool check_nobody_user_and_group(void) {
×
UNCOV
154
        static int cache = -1;
×
155
        struct passwd *p;
×
156
        struct group *g;
×
157

158
        if (cache >= 0)
×
159
                return !!cache;
×
160

UNCOV
161
        if (!synthesize_nobody())
×
162
                goto invalid;
×
163

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

171
        p = getpwuid(UID_NOBODY);
×
172
        if (!p ||
×
173
            !streq(p->pw_name, NOBODY_USER_NAME) ||
×
UNCOV
174
            p->pw_uid != UID_NOBODY ||
×
175
            p->pw_gid != GID_NOBODY)
×
176
                goto invalid;
×
177

178
        g = getgrnam(NOBODY_GROUP_NAME);
×
179
        if (!g ||
×
UNCOV
180
            !streq(g->gr_name, NOBODY_GROUP_NAME) ||
×
181
            g->gr_gid != GID_NOBODY)
×
182
                goto invalid;
×
183

184
        g = getgrgid(GID_NOBODY);
×
185
        if (!g ||
×
186
            !streq(g->gr_name, NOBODY_GROUP_NAME) ||
×
UNCOV
187
            g->gr_gid != GID_NOBODY)
×
UNCOV
188
                goto invalid;
×
189

190
        cache = 1;
×
191
        return true;
×
192

193
invalid:
×
UNCOV
194
        cache = 0;
×
195
        return false;
×
196
}
197

UNCOV
198
static bool check_user_has_group_with_same_name(const char *name) {
×
UNCOV
199
        struct passwd *p;
×
200
        struct group *g;
×
201

202
        ASSERT_NOT_NULL(name);
×
203

UNCOV
204
        p = getpwnam(name);
×
UNCOV
205
        if (!p ||
×
UNCOV
206
            !streq(p->pw_name, name))
×
207
                return false;
208

209
        g = getgrgid(p->pw_gid);
×
UNCOV
210
        if (!g ||
×
UNCOV
211
            !streq(g->gr_name, name))
×
UNCOV
212
                return false;
×
213

214
        return true;
215
}
216

217
static bool is_inaccessible_available(void) {
×
UNCOV
218
        FOREACH_STRING(p,
×
219
                       "/run/systemd/inaccessible/reg",
220
                       "/run/systemd/inaccessible/dir",
221
                       "/run/systemd/inaccessible/chr",
222
                       "/run/systemd/inaccessible/blk",
223
                       "/run/systemd/inaccessible/fifo",
224
                       "/run/systemd/inaccessible/sock")
225
                if (access(p, F_OK) < 0)
×
226
                        return false;
×
227

228
        return true;
×
229
}
230

UNCOV
231
static void start_parent_slices(Unit *unit) {
×
232
        Unit *slice;
×
233

234
        slice = UNIT_GET_SLICE(unit);
×
UNCOV
235
        if (slice) {
×
UNCOV
236
                start_parent_slices(slice);
×
UNCOV
237
                ASSERT_OK_OR(unit_start(slice, NULL), -EALREADY);
×
238
        }
239
}
×
240

241
static bool apparmor_restrict_unprivileged_userns(void) {
×
UNCOV
242
        _cleanup_free_ char *v = NULL;
×
UNCOV
243
        int r;
×
244

245
        /* If kernel.apparmor_restrict_unprivileged_userns=1, then we cannot
246
         * use unprivileged user namespaces. */
UNCOV
247
        r = sysctl_read("kernel/apparmor_restrict_unprivileged_userns", &v);
×
UNCOV
248
        if (r < 0) {
×
249
                if (r != -ENOENT)
×
250
                        log_debug_errno(r, "Failed to read kernel.apparmor_restrict_unprivileged_userns sysctl, ignoring: %m");
×
251

252
                return false;
253
        }
254

UNCOV
255
        return streq(v, "1");
×
256
}
257

UNCOV
258
static bool have_userns_privileges(void) {
×
UNCOV
259
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
×
UNCOV
260
        int r;
×
261

UNCOV
262
        if (apparmor_restrict_unprivileged_userns())
×
263
                return false;
264

UNCOV
265
        r = ASSERT_OK(pidref_safe_fork(
×
266
                        "(sd-test-check-userns)",
267
                        FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGKILL,
268
                        &pidref));
269

270
        if (r == 0) {
×
271
                /* Keep CAP_SYS_ADMIN if we have it to ensure we give an
272
                 * accurate result to the caller. Some kernels have a
273
                 * kernel.unprivileged_userns_clone sysctl which can be
274
                 * configured to make CLONE_NEWUSER require CAP_SYS_ADMIN.
275
                 * Additionally, AppArmor may restrict unprivileged user
276
                 * namespace creation. */
UNCOV
277
                r = capability_bounding_set_drop(UINT64_C(1) << CAP_SYS_ADMIN, /* right_now= */ true);
×
278
                if (r < 0) {
×
UNCOV
279
                        log_debug_errno(r, "Failed to drop capabilities: %m");
×
UNCOV
280
                        _exit(2);
×
281
                }
282

UNCOV
283
                r = RET_NERRNO(unshare(CLONE_NEWUSER));
×
UNCOV
284
                if (r < 0 && !ERRNO_IS_NEG_PRIVILEGE(r))
×
285
                        log_debug_errno(r, "Failed to create user namespace: %m");
×
286

287
                _exit(r >= 0 ? EXIT_SUCCESS : ERRNO_IS_NEG_PRIVILEGE(r) ? EXIT_FAILURE : 2);
×
288
        }
289

290
        /* The exit code records the result of the check:
291
         *  EXIT_SUCCESS => we can use user namespaces
292
         *  EXIT_FAILURE => we can NOT use user namespaces
293
         *  2            => some other error occurred */
294
        r = pidref_wait_for_terminate_and_check("(sd-test-check-userns)", &pidref, 0);
×
UNCOV
295
        if (!IN_SET(r, EXIT_SUCCESS, EXIT_FAILURE))
×
296
                log_debug("Failed to check if user namespaces can be used, assuming not.");
×
297

298
        return r == EXIT_SUCCESS;
×
299
}
300

301
static void _test(const char *file, unsigned line, const char *func,
×
302
                  Manager *m, const char *unit_name, int status_expected, int code_expected) {
303
        Unit *unit;
×
304

305
        ASSERT_NOT_NULL(unit_name);
×
306

UNCOV
307
        ASSERT_OK(manager_load_startable_unit_or_warn(m, unit_name, NULL, &unit));
×
308
        /* We need to start the slices as well otherwise the slice cgroups might be pruned
309
         * in on_cgroup_empty_event. */
310
        start_parent_slices(unit);
×
UNCOV
311
        ASSERT_OK(unit_start(unit, NULL));
×
312
        check_main_result(file, line, func, m, unit, status_expected, code_expected);
×
313

314
        ++n_ran_tests;
×
UNCOV
315
}
×
316
#define test(m, unit_name, status_expected, code_expected) \
317
        _test(PROJECT_FILE, __LINE__, __func__, m, unit_name, status_expected, code_expected)
318

319
static void _test_service(const char *file, unsigned line, const char *func,
×
320
                          Manager *m, const char *unit_name, ServiceResult result_expected) {
UNCOV
321
        Unit *unit;
×
322

323
        ASSERT_NOT_NULL(unit_name);
×
324

325
        ASSERT_OK(manager_load_startable_unit_or_warn(m, unit_name, NULL, &unit));
×
UNCOV
326
        ASSERT_OK(unit_start(unit, NULL));
×
327
        check_service_result(file, line, func, m, unit, result_expected);
×
UNCOV
328
}
×
329
#define test_service(m, unit_name, result_expected) \
330
        _test_service(PROJECT_FILE, __LINE__, __func__, m, unit_name, result_expected)
331

UNCOV
332
static void test_exec_bindpaths(Manager *m) {
×
333
        ASSERT_OK(mkdir_p("/tmp/test-exec-bindpaths", 0755));
×
334
        ASSERT_OK(mkdir_p("/tmp/test-exec-bindreadonlypaths", 0755));
×
335

336
        test(m, "exec-bindpaths.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
337

UNCOV
338
        (void) rm_rf("/tmp/test-exec-bindpaths", REMOVE_ROOT|REMOVE_PHYSICAL);
×
339
        (void) rm_rf("/tmp/test-exec-bindreadonlypaths", REMOVE_ROOT|REMOVE_PHYSICAL);
×
340
}
×
341

UNCOV
342
static void test_exec_cpuaffinity(Manager *m) {
×
UNCOV
343
        _cleanup_(cpu_set_done) CPUSet c = {};
×
344

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

348
        if (!CPU_ISSET_S(0, c.allocated, c.set)) {
×
349
                log_notice("Cannot use CPU 0, skipping %s", __func__);
×
350
                return;
351
        }
352

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

356
        if (!CPU_ISSET_S(1, c.allocated, c.set) ||
×
357
            !CPU_ISSET_S(2, c.allocated, c.set)) {
×
358
                log_notice("Cannot use CPU 1 or 2, skipping remaining tests in %s", __func__);
×
359
                return;
360
        }
361

362
        test(m, "exec-cpuaffinity3.service", 0, CLD_EXITED);
×
363
}
364

365
static void test_exec_credentials(Manager *m) {
×
366
        test(m, "exec-set-credential.service", 0, CLD_EXITED);
×
UNCOV
367
        test(m, "exec-load-credential.service", 0, CLD_EXITED);
×
368
        test(m, "exec-credentials-dir-specifier.service", 0, CLD_EXITED);
×
369
}
×
370

371
static void test_exec_workingdirectory(Manager *m) {
×
372
        ASSERT_OK(mkdir_p("/tmp/test-exec_workingdirectory", 0755));
×
373

374
        test(m, "exec-workingdirectory.service", 0, CLD_EXITED);
×
375
        test(m, "exec-workingdirectory-trailing-dot.service", 0, CLD_EXITED);
×
376

UNCOV
377
        (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT|REMOVE_PHYSICAL);
×
378
}
×
379

380
static void test_exec_execsearchpath(Manager *m) {
×
UNCOV
381
        int r;
×
382

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

UNCOV
387
        ASSERT_OK(mkdir_p("/tmp/test-exec_execsearchpath", 0755));
×
388

389
        ASSERT_OK(copy_file("/bin/ls", "/tmp/test-exec_execsearchpath/ls_temp", 0,  0777, COPY_REPLACE));
×
390

391
        test(m, "exec-execsearchpath.service", 0, CLD_EXITED);
×
392

393
        ASSERT_OK(rm_rf("/tmp/test-exec_execsearchpath", REMOVE_ROOT|REMOVE_PHYSICAL));
×
394

395
        test(m, "exec-execsearchpath.service", EXIT_EXEC, CLD_EXITED);
×
396
}
397

398
static void test_exec_execsearchpath_specifier(Manager *m) {
×
399
        test(m, "exec-execsearchpath-unit-specifier.service", 0, CLD_EXITED);
×
UNCOV
400
}
×
401

UNCOV
402
static void test_exec_execsearchpath_environment(Manager *m) {
×
UNCOV
403
        test(m, "exec-execsearchpath-environment.service", 0, CLD_EXITED);
×
UNCOV
404
        test(m, "exec-execsearchpath-environment-path-set.service", 0, CLD_EXITED);
×
UNCOV
405
}
×
406

UNCOV
407
static void test_exec_execsearchpath_environment_files(Manager *m) {
×
UNCOV
408
        static const char path_not_set[] =
×
409
                "VAR1='word1 word2'\n"
410
                "VAR2=word3 \n"
411
                "# comment1\n"
412
                "\n"
413
                "; comment2\n"
414
                " ; # comment3\n"
415
                "line without an equal\n"
416
                "VAR3='$word 5 6'\n"
417
                "VAR4='new\nline'\n"
418
                "VAR5=password\\with\\backslashes";
419

UNCOV
420
        static const char path_set[] =
×
421
                "VAR1='word1 word2'\n"
422
                "VAR2=word3 \n"
423
                "# comment1\n"
424
                "\n"
425
                "; comment2\n"
426
                " ; # comment3\n"
427
                "line without an equal\n"
428
                "VAR3='$word 5 6'\n"
429
                "VAR4='new\nline'\n"
430
                "VAR5=password\\with\\backslashes\n"
431
                "PATH=/usr";
432

433
        int r;
×
434

UNCOV
435
        r = write_string_file("/tmp/test-exec_execsearchpath_environmentfile.conf", path_not_set, WRITE_STRING_FILE_CREATE);
×
436
        ASSERT_OK(r);
×
437

438
        test(m, "exec-execsearchpath-environmentfile.service", 0, CLD_EXITED);
×
439

UNCOV
440
        (void) unlink("/tmp/test-exec_environmentfile.conf");
×
441

442
        r = write_string_file("/tmp/test-exec_execsearchpath_environmentfile-set.conf", path_set, WRITE_STRING_FILE_CREATE);
×
443
        ASSERT_OK(r);
×
444

445
        test(m, "exec-execsearchpath-environmentfile-set.service", 0, CLD_EXITED);
×
446

UNCOV
447
        (void) unlink("/tmp/test-exec_environmentfile-set.conf");
×
448
}
×
449

450
static void test_exec_execsearchpath_passenvironment(Manager *m) {
×
451
        ASSERT_OK_ERRNO(setenv("VAR1", "word1 word2", 1));
×
UNCOV
452
        ASSERT_OK_ERRNO(setenv("VAR2", "word3", 1));
×
453
        ASSERT_OK_ERRNO(setenv("VAR3", "$word 5 6", 1));
×
454
        ASSERT_OK_ERRNO(setenv("VAR4", "new\nline", 1));
×
455
        ASSERT_OK_ERRNO(setenv("VAR5", "passwordwithbackslashes", 1));
×
456

457
        test(m, "exec-execsearchpath-passenvironment.service", 0, CLD_EXITED);
×
458

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

UNCOV
462
        ASSERT_OK_ERRNO(unsetenv("VAR1"));
×
463
        ASSERT_OK_ERRNO(unsetenv("VAR2"));
×
UNCOV
464
        ASSERT_OK_ERRNO(unsetenv("VAR3"));
×
UNCOV
465
        ASSERT_OK_ERRNO(unsetenv("VAR4"));
×
UNCOV
466
        ASSERT_OK_ERRNO(unsetenv("VAR5"));
×
UNCOV
467
        ASSERT_OK_ERRNO(unsetenv("PATH"));
×
UNCOV
468
}
×
469

UNCOV
470
static void test_exec_personality(Manager *m) {
×
471
#if defined(__x86_64__)
UNCOV
472
        test(m, "exec-personality-x86-64.service", 0, CLD_EXITED);
×
473

474
#elif defined(__s390x__)
475
        test(m, "exec-personality-s390x.service", 0, CLD_EXITED);
476

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

480
#elif defined(__powerpc64__)
481
#  if __BYTE_ORDER == __BIG_ENDIAN
482
        test(m, "exec-personality-ppc64.service", 0, CLD_EXITED);
483
#  else
484
        test(m, "exec-personality-ppc64le.service", 0, CLD_EXITED);
485
#  endif
486

487
#elif defined(__aarch64__)
488
        test(m, "exec-personality-aarch64.service", 0, CLD_EXITED);
489

490
#elif defined(__i386__)
491
        test(m, "exec-personality-x86.service", 0, CLD_EXITED);
492
#elif defined(__loongarch_lp64)
493
        test(m, "exec-personality-loongarch64.service", 0, CLD_EXITED);
494
#else
495
        log_notice("Unknown personality, skipping %s", __func__);
496
#endif
UNCOV
497
}
×
498

499
static void test_exec_ignoresigpipe(Manager *m) {
×
500
        test(m, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED);
×
UNCOV
501
        test(m, "exec-ignoresigpipe-no.service", SIGPIPE, CLD_KILLED);
×
502
}
×
503

504
static void test_exec_privatetmp(Manager *m) {
×
UNCOV
505
        ASSERT_OK(touch("/tmp/test-exec_privatetmp"));
×
506

UNCOV
507
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) {
×
UNCOV
508
                test(m, "exec-privatetmp-yes.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
509
                test(m, "exec-privatetmp-disabled-by-prefix.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
510

UNCOV
511
                (void) unlink("/tmp/test-exec_privatetmp_disconnected");
×
UNCOV
512
                test(m, "exec-privatetmp-disconnected-nodefaultdeps-nor-sandboxing.service", 0, CLD_EXITED);
×
513
                ASSERT_OK_ERRNO(access("/tmp/test-exec_privatetmp_disconnected", F_OK));
×
514

515
                FOREACH_STRING(s,
×
516
                               "exec-privatetmp-disconnected.service",
517
                               "exec-privatetmp-disconnected-defaultdependencies-no.service",
518
                               "exec-privatetmp-disconnected-requires-mounts-for-var.service",
519
                               "exec-privatetmp-disconnected-wants-mounts-for-var.service",
520
                               "exec-privatetmp-disconnected-after-and-requires-for-var.service",
521
                               "exec-privatetmp-disconnected-after-and-wants-for-var.service") {
UNCOV
522
                        (void) unlink("/tmp/test-exec_privatetmp_disconnected");
×
523
                        (void) unlink("/var/tmp/test-exec_privatetmp_disconnected");
×
524
                        test(m, s, can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
525
                        ASSERT_FAIL(access("/tmp/test-exec_privatetmp_disconnected", F_OK));
×
526
                        ASSERT_FAIL(access("/var/tmp/test-exec_privatetmp_disconnected", F_OK));
×
527
                }
528
        }
529

530
        test(m, "exec-privatetmp-no.service", 0, CLD_EXITED);
×
531

UNCOV
532
        (void) unlink("/tmp/test-exec_privatetmp");
×
533
}
×
534

UNCOV
535
static void test_exec_privatedevices(Manager *m) {
×
UNCOV
536
        int r;
×
537

538
        if (detect_container() > 0) {
×
539
                log_notice("Testing in container, skipping %s", __func__);
×
540
                return;
541
        }
542
        if (!is_inaccessible_available()) {
×
543
                log_notice("Testing without inaccessible, skipping %s", __func__);
×
544
                return;
545
        }
546

UNCOV
547
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) {
×
UNCOV
548
                test(m, "exec-privatedevices-yes.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
549
                if (access("/dev/kmsg", F_OK) >= 0)
×
550
                        test(m, "exec-privatedevices-bind.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
551
                test(m, "exec-privatedevices-disabled-by-prefix.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
552
                test(m, "exec-privatedevices-yes-with-group.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
553
        }
554

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

557
        /* We use capsh to test if the capabilities are
558
         * properly set, so be sure that it exists */
UNCOV
559
        r = find_executable("capsh", NULL);
×
UNCOV
560
        if (r < 0) {
×
561
                log_notice_errno(r, "Could not find capsh binary, skipping remaining tests in %s: %m", __func__);
×
562
                return;
563
        }
564

565
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) {
×
566
                test(m, "exec-privatedevices-yes-capability-mknod.service", can_unshare || MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
567
                test(m, "exec-privatedevices-yes-capability-sys-rawio.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
568
        }
569

UNCOV
570
        test(m, "exec-privatedevices-no-capability-mknod.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED);
×
571
        test(m, "exec-privatedevices-no-capability-sys-rawio.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_FAILURE, CLD_EXITED);
×
572
}
573

574
static void test_exec_protecthome(Manager *m) {
×
575
        if (!can_unshare) {
×
UNCOV
576
                log_notice("Cannot reliably unshare, skipping %s", __func__);
×
577
                return;
578
        }
579

UNCOV
580
        test(m, "exec-protecthome-tmpfs-vs-protectsystem-strict.service", 0, CLD_EXITED);
×
581
}
582

UNCOV
583
static void test_exec_protectkernelmodules(Manager *m) {
×
UNCOV
584
        int r;
×
585

586
        if (detect_container() > 0) {
×
587
                log_notice("Testing in container, skipping %s", __func__);
×
588
                return;
589
        }
UNCOV
590
        if (!is_inaccessible_available()) {
×
UNCOV
591
                log_notice("Testing without inaccessible, skipping %s", __func__);
×
592
                return;
593
        }
594

595
        r = find_executable("capsh", NULL);
×
596
        if (r < 0) {
×
UNCOV
597
                log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
×
598
                return;
599
        }
600

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

603
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges()) {
×
UNCOV
604
                test(m, "exec-protectkernelmodules-yes-capabilities.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
605
                test(m, "exec-protectkernelmodules-yes-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
606
        }
607
}
608

UNCOV
609
static void test_exec_readonlypaths(Manager *m) {
×
610

611
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges())
×
612
                test(m, "exec-readonlypaths-simple.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
613

UNCOV
614
        if (path_is_read_only_fs("/var") > 0) {
×
615
                log_notice("Directory /var is readonly, skipping remaining tests in %s", __func__);
×
616
                return;
617
        }
618

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

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

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

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

UNCOV
634
static void test_exec_inaccessiblepaths(Manager *m) {
×
635

636
        if (!is_inaccessible_available()) {
×
UNCOV
637
                log_notice("Testing without inaccessible, skipping %s", __func__);
×
638
                return;
639
        }
640

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

644
        if (path_is_read_only_fs("/") > 0) {
×
645
                log_notice("Root directory is readonly, skipping remaining tests in %s", __func__);
×
646
                return;
647
        }
648

649
        test(m, "exec-inaccessiblepaths-mount-propagation.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
650
}
651

652
#if !HAS_FEATURE_ADDRESS_SANITIZER
653
static int on_spawn_io(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
×
654
        char **result = userdata;
×
655
        char buf[4096];
×
UNCOV
656
        ssize_t l;
×
657

UNCOV
658
        ASSERT_NOT_NULL(s);
×
659
        ASSERT_GT(fd, 0);
×
660

UNCOV
661
        l = read(fd, buf, sizeof(buf) - 1);
×
662
        if (l < 0) {
×
663
                if (errno == EAGAIN)
×
664
                        goto reenable;
×
665

666
                return 0;
×
667
        }
668
        if (l == 0)
×
669
                return 0;
670

UNCOV
671
        buf[l] = '\0';
×
UNCOV
672
        if (result)
×
UNCOV
673
                ASSERT_NOT_NULL(strextend(result, buf));
×
674
        else
675
                log_error("ldd: %s", buf);
×
676

677
reenable:
×
678
        /* Re-enable the event source if we did not encounter EOF */
679
        ASSERT_OK(sd_event_source_set_enabled(s, SD_EVENT_ONESHOT));
×
680
        return 0;
681
}
682

683
static int on_spawn_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
×
UNCOV
684
        PidRef *pidref = ASSERT_PTR(userdata);
×
685

UNCOV
686
        (void) pidref_kill(pidref, SIGKILL);
×
687

688
        return 1;
×
689
}
690

691
static int on_spawn_exit(sd_event_source *s, const siginfo_t *si, void *userdata) {
×
UNCOV
692
        int ret = -EIO;
×
693

694
        ASSERT_NOT_NULL(si);
×
695

696
        if (si->si_code == CLD_EXITED)
×
697
                ret = si->si_status;
×
698

699
        sd_event_exit(sd_event_source_get_event(s), ret);
×
700
        return 1;
×
701
}
702

UNCOV
703
static int find_libraries(const char *exec, char ***ret) {
×
704
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
×
705
        _cleanup_(sd_event_source_unrefp) sd_event_source *sigchld_source = NULL;
×
UNCOV
706
        _cleanup_(sd_event_source_unrefp) sd_event_source *stdout_source = NULL;
×
707
        _cleanup_(sd_event_source_unrefp) sd_event_source *stderr_source = NULL;
×
708
        _cleanup_close_pair_ int outpipe[2] = EBADF_PAIR, errpipe[2] = EBADF_PAIR;
×
UNCOV
709
        _cleanup_strv_free_ char **libraries = NULL;
×
710
        _cleanup_free_ char *result = NULL;
×
711
        int r;
×
712

713
        ASSERT_NOT_NULL(exec);
×
UNCOV
714
        ASSERT_NOT_NULL(ret);
×
715

UNCOV
716
        ASSERT_OK_ERRNO(pipe2(outpipe, O_NONBLOCK|O_CLOEXEC));
×
717
        ASSERT_OK_ERRNO(pipe2(errpipe, O_NONBLOCK|O_CLOEXEC));
×
718

719
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
×
720
        r = pidref_safe_fork_full(
×
721
                        "(spawn-ldd)",
UNCOV
722
                        (int[]) { -EBADF, outpipe[1], errpipe[1] },
×
723
                        NULL, 0,
724
                        FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REARRANGE_STDIO|FORK_LOG,
725
                        &pidref);
726
        ASSERT_OK(r);
×
UNCOV
727
        if (r == 0) {
×
728
                execlp("ldd", "ldd", exec, NULL);
×
UNCOV
729
                _exit(EXIT_FAILURE);
×
730
        }
731

732
        outpipe[1] = safe_close(outpipe[1]);
×
733
        errpipe[1] = safe_close(errpipe[1]);
×
734

UNCOV
735
        ASSERT_OK(sd_event_new(&e));
×
736

UNCOV
737
        ASSERT_OK(sd_event_add_time_relative(e, NULL, CLOCK_MONOTONIC,
×
738
                                             10 * USEC_PER_SEC, USEC_PER_SEC, on_spawn_timeout, &pidref));
UNCOV
739
        ASSERT_OK(sd_event_add_io(e, &stdout_source, outpipe[0], EPOLLIN, on_spawn_io, &result));
×
740
        ASSERT_OK(sd_event_source_set_enabled(stdout_source, SD_EVENT_ONESHOT));
×
741
        ASSERT_OK(sd_event_add_io(e, &stderr_source, errpipe[0], EPOLLIN, on_spawn_io, NULL));
×
UNCOV
742
        ASSERT_OK(sd_event_source_set_enabled(stderr_source, SD_EVENT_ONESHOT));
×
743
        ASSERT_OK(event_add_child_pidref(e, &sigchld_source, &pidref, WEXITED, on_spawn_exit, NULL));
×
744
        /* Child exit should be processed after IO is complete */
745
        ASSERT_OK(sd_event_source_set_priority(sigchld_source, SD_EVENT_PRIORITY_NORMAL + 1));
×
746

747
        ASSERT_OK(sd_event_loop(e));
×
748

749
        _cleanup_strv_free_ char **v = NULL;
×
750
        ASSERT_OK(strv_split_newlines_full(&v, result, 0));
×
751

752
        STRV_FOREACH(q, v) {
×
753
                _cleanup_free_ char *word = NULL;
×
754
                const char *p = *q;
×
755

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

761
                if (path_is_absolute(word)) {
×
UNCOV
762
                        ASSERT_OK(strv_consume(&libraries, TAKE_PTR(word)));
×
763
                        continue;
×
764
                }
765

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

772
                if (!streq_ptr(word, "=>"))
×
773
                        continue;
×
774

UNCOV
775
                word = mfree(word);
×
UNCOV
776
                r = extract_first_word(&p, &word, NULL, 0);
×
UNCOV
777
                ASSERT_OK(r);
×
778
                if (r == 0)
×
779
                        continue;
×
780

UNCOV
781
                if (path_is_absolute(word)) {
×
UNCOV
782
                        ASSERT_OK(strv_consume(&libraries, TAKE_PTR(word)));
×
783
                        continue;
×
784
                }
785
        }
786

787
        *ret = TAKE_PTR(libraries);
×
UNCOV
788
        return 0;
×
789
}
790
#endif
791

792
static void test_exec_mount_apivfs(Manager *m) {
×
793
#if !HAS_FEATURE_ADDRESS_SANITIZER
UNCOV
794
        _cleanup_free_ char *fullpath_touch = NULL, *fullpath_test = NULL, *data = NULL;
×
UNCOV
795
        _cleanup_strv_free_ char **libraries = NULL, **libraries_test = NULL;
×
796
        int r;
×
797

798
        ASSERT_NOT_NULL(user_runtime_unit_dir);
×
799

UNCOV
800
        r = find_executable("ldd", NULL);
×
801
        if (r < 0) {
×
802
                log_notice_errno(r, "Skipping %s, could not find 'ldd' command: %m", __func__);
×
803
                return;
804
        }
UNCOV
805
        r = find_executable("touch", &fullpath_touch);
×
UNCOV
806
        if (r < 0) {
×
807
                log_notice_errno(r, "Skipping %s, could not find 'touch' command: %m", __func__);
×
808
                return;
809
        }
810
        r = find_executable("test", &fullpath_test);
×
811
        if (r < 0) {
×
812
                log_notice_errno(r, "Skipping %s, could not find 'test' command: %m", __func__);
×
813
                return;
814
        }
815

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

UNCOV
819
        ASSERT_OK(find_libraries(fullpath_touch, &libraries));
×
820
        ASSERT_OK(find_libraries(fullpath_test, &libraries_test));
×
821
        ASSERT_OK(strv_extend_strv(&libraries, libraries_test, true));
×
822

823
        ASSERT_NOT_NULL(strextend(&data, "[Service]\n"));
×
UNCOV
824
        ASSERT_NOT_NULL((strextend(&data, "ExecStart=", fullpath_touch, " /aaa\n")));
×
825
        ASSERT_NOT_NULL((strextend(&data, "ExecStart=", fullpath_test, " -f /aaa\n")));
×
UNCOV
826
        ASSERT_NOT_NULL((strextend(&data, "BindReadOnlyPaths=", fullpath_touch, "\n")));
×
827
        ASSERT_NOT_NULL((strextend(&data, "BindReadOnlyPaths=", fullpath_test, "\n")));
×
828

829
        STRV_FOREACH(p, libraries)
×
UNCOV
830
                ASSERT_NOT_NULL((strextend(&data, "BindReadOnlyPaths=", *p, "\n")));
×
831

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

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

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

838
        (void) rm_rf("/tmp/test-exec-mount-apivfs-no/root", REMOVE_ROOT|REMOVE_PHYSICAL);
×
839
#endif
840
}
841

UNCOV
842
static void test_exec_noexecpaths(Manager *m) {
×
843

844
        if (MANAGER_IS_SYSTEM(m) || have_userns_privileges())
×
845
                test(m, "exec-noexecpaths-simple.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
846
        else
847
                return (void)log_notice("Skipping %s, do not have user namespace privileges", __func__);
×
848
}
849

UNCOV
850
static void test_exec_temporaryfilesystem(Manager *m) {
×
851

UNCOV
852
        test(m, "exec-temporaryfilesystem-options.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
853
        test(m, "exec-temporaryfilesystem-ro.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
854
        test(m, "exec-temporaryfilesystem-rw.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
855
        test(m, "exec-temporaryfilesystem-usr.service", can_unshare ? 0 : EXIT_NAMESPACE, CLD_EXITED);
×
UNCOV
856
}
×
857

858
static void test_exec_systemcallfilter(Manager *m) {
×
859
#if HAVE_SECCOMP && !HAS_FEATURE_ADDRESS_SANITIZER
860
        int r;
×
861

862
        if (!is_seccomp_available()) {
×
863
                log_notice("Seccomp not available, skipping %s", __func__);
×
864
                return;
865
        }
866

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

869
        test(m, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED);
×
UNCOV
870
        test(m, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED);
×
UNCOV
871
        test(m, "exec-systemcallfilter-not-failing3.service", 0, CLD_EXITED);
×
UNCOV
872
        test(m, "exec-systemcallfilter-failing.service", SIGSYS, CLD_KILLED);
×
873
        test(m, "exec-systemcallfilter-failing2.service", SIGSYS, CLD_KILLED);
×
874
        test(m, "exec-systemcallfilter-failing3.service", SIGSYS, CLD_KILLED);
×
875

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

UNCOV
882
        test(m, "exec-systemcallfilter-with-errno-name.service", errno_from_name("EILSEQ"), CLD_EXITED);
×
883
        test(m, "exec-systemcallfilter-with-errno-number.service", 255, CLD_EXITED);
×
884
        test(m, "exec-systemcallfilter-with-errno-multi.service", errno_from_name("EILSEQ"), CLD_EXITED);
×
885
        test(m, "exec-systemcallfilter-with-errno-in-allow-list.service", errno_from_name("EILSEQ"), CLD_EXITED);
×
UNCOV
886
        test(m, "exec-systemcallfilter-override-error-action.service", SIGSYS, CLD_KILLED);
×
UNCOV
887
        test(m, "exec-systemcallfilter-override-error-action2.service", errno_from_name("EILSEQ"), CLD_EXITED);
×
888

889
        test(m, "exec-systemcallfilter-nonewprivileges.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
890
        test(m, "exec-systemcallfilter-nonewprivileges-protectclock.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
891

UNCOV
892
        r = find_executable("capsh", NULL);
×
UNCOV
893
        if (r < 0) {
×
894
                log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
×
895
                return;
896
        }
897

898
        test(m, "exec-systemcallfilter-nonewprivileges-bounding1.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
899
        test(m, "exec-systemcallfilter-nonewprivileges-bounding2.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
900
#endif
901
}
902

903
static void test_exec_systemcallerrornumber(Manager *m) {
×
904
#if HAVE_SECCOMP && !HAS_FEATURE_ADDRESS_SANITIZER
905
        int r;
×
906

UNCOV
907
        if (!is_seccomp_available()) {
×
UNCOV
908
                log_notice("Seccomp not available, skipping %s", __func__);
×
909
                return;
910
        }
911

UNCOV
912
        r = find_executable("python3", NULL);
×
UNCOV
913
        if (r < 0) {
×
914
                log_notice_errno(r, "Skipping %s, could not find python3 binary: %m", __func__);
×
915
                return;
916
        }
917

UNCOV
918
        test(m, "exec-systemcallerrornumber-name.service", errno_from_name("EACCES"), CLD_EXITED);
×
UNCOV
919
        test(m, "exec-systemcallerrornumber-number.service", 255, CLD_EXITED);
×
920
#endif
921
}
922

923
static void test_exec_restrictnamespaces(Manager *m) {
×
924
#if HAVE_SECCOMP
925
        if (!is_seccomp_available()) {
×
926
                log_notice("Seccomp not available, skipping %s", __func__);
×
927
                return;
928
        }
929

UNCOV
930
        test(m, "exec-restrictnamespaces-no.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
×
931
        test(m, "exec-restrictnamespaces-yes.service", 1, CLD_EXITED);
×
UNCOV
932
        test(m, "exec-restrictnamespaces-mnt.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
×
UNCOV
933
        test(m, "exec-restrictnamespaces-mnt-deny-list.service", 1, CLD_EXITED);
×
UNCOV
934
        test(m, "exec-restrictnamespaces-merge-and.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
×
UNCOV
935
        test(m, "exec-restrictnamespaces-merge-or.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
×
UNCOV
936
        test(m, "exec-restrictnamespaces-merge-all.service", can_unshare ? 0 : EXIT_FAILURE, CLD_EXITED);
×
937
#endif
938
}
939

UNCOV
940
static void test_exec_systemcallfilter_system(Manager *m) {
×
941
/* Skip this particular test case when running under ASan, as
942
 * LSan intermittently segfaults when accessing memory right
943
 * after the test finishes. Generally, ASan & LSan don't like
944
 * the seccomp stuff.
945
 */
946
#if HAVE_SECCOMP && !HAS_FEATURE_ADDRESS_SANITIZER
UNCOV
947
        if (!is_seccomp_available()) {
×
UNCOV
948
                log_notice("Seccomp not available, skipping %s", __func__);
×
949
                return;
950
        }
951

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

UNCOV
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__);
×
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-systemcallfilter-system-user-" NOBODY_USER_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
965
#endif
966
}
967

968
static void test_exec_user(Manager *m) {
×
969
        test(m, "exec-user.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
970

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

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

UNCOV
981
        test(m, "exec-user-" NOBODY_USER_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
982
}
983

984
static void test_exec_group(Manager *m) {
×
985
        test(m, "exec-group.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
986

UNCOV
987
        if (!check_nobody_user_and_group()) {
×
988
                log_notice("nobody user/group is not synthesized or may conflict to other entries, skipping remaining tests in %s", __func__);
×
989
                return;
990
        }
991

992
        if (!STR_IN_SET(NOBODY_GROUP_NAME, "nobody", "nfsnobody", "nogroup")) {
×
993
                log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME, __func__);
×
994
                return;
×
995
        }
996

997
        test(m, "exec-group-" NOBODY_GROUP_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
998
}
999

UNCOV
1000
static void test_exec_supplementarygroups(Manager *m) {
×
1001
        int status = MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP;
×
UNCOV
1002
        test(m, "exec-supplementarygroups.service", status, CLD_EXITED);
×
UNCOV
1003
        test(m, "exec-supplementarygroups-single-group.service", status, CLD_EXITED);
×
1004
        test(m, "exec-supplementarygroups-single-group-user.service", status, CLD_EXITED);
×
1005
        test(m, "exec-supplementarygroups-multiple-groups-default-group-user.service", status, CLD_EXITED);
×
1006
        test(m, "exec-supplementarygroups-multiple-groups-withgid.service", status, CLD_EXITED);
×
UNCOV
1007
        test(m, "exec-supplementarygroups-multiple-groups-withuid.service", status, CLD_EXITED);
×
1008
}
×
1009

1010
static char* private_directory_bad(Manager *m) {
×
1011
        /* This mirrors setup_exec_directory(). */
1012

UNCOV
1013
        for (ExecDirectoryType dt = 0; dt < _EXEC_DIRECTORY_TYPE_MAX; dt++) {
×
UNCOV
1014
                _cleanup_free_ char *p = NULL;
×
UNCOV
1015
                struct stat st;
×
1016

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

1019
                if (stat(p, &st) >= 0 &&
×
1020
                    (st.st_mode & (S_IRWXG|S_IRWXO)))
×
1021
                        return TAKE_PTR(p);
×
1022
        }
1023

1024
        return NULL;
1025
}
1026

1027
static void test_exec_dynamicuser(Manager *m) {
×
UNCOV
1028
        if (MANAGER_IS_USER(m)) {
×
UNCOV
1029
                log_notice("Skipping %s for user manager", __func__);
×
1030
                return;
×
1031
        }
1032

1033
        _cleanup_free_ char *bad = private_directory_bad(m);
×
1034
        if (bad) {
×
1035
                log_warning("%s: %s has bad permissions, skipping test.", __func__, bad);
×
1036
                return;
×
1037
        }
1038

1039
        int status = can_unshare ? 0 : EXIT_NAMESPACE;
×
1040

1041
        test(m, "exec-dynamicuser-fixeduser.service", status, CLD_EXITED);
×
1042
        if (check_user_has_group_with_same_name("adm"))
×
1043
                test(m, "exec-dynamicuser-fixeduser-adm.service", status, CLD_EXITED);
×
1044
        if (check_user_has_group_with_same_name("games"))
×
1045
                test(m, "exec-dynamicuser-fixeduser-games.service", status, CLD_EXITED);
×
1046
        test(m, "exec-dynamicuser-fixeduser-one-supplementarygroup.service", status, CLD_EXITED);
×
1047
        test(m, "exec-dynamicuser-supplementarygroups.service", status, CLD_EXITED);
×
1048
        test(m, "exec-dynamicuser-statedir.service", status, CLD_EXITED);
×
1049

1050
        (void) rm_rf("/var/lib/quux", REMOVE_ROOT|REMOVE_PHYSICAL);
×
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);
×
UNCOV
1053
        (void) rm_rf("/var/lib/waldo", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1054
        (void) rm_rf("/var/lib/private/quux", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1055
        (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1056
        (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1057
        (void) rm_rf("/var/lib/private/waldo", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1058

1059
        test(m, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED);
×
1060
        test(m, "exec-dynamicuser-statedir-migrate-step2.service", status, CLD_EXITED);
×
1061
        test(m, "exec-dynamicuser-statedir-migrate-step1.service", 0, CLD_EXITED);
×
1062

UNCOV
1063
        (void) rm_rf("/var/lib/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1064
        (void) rm_rf("/var/lib/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1065
        (void) rm_rf("/var/lib/private/test-dynamicuser-migrate", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1066
        (void) rm_rf("/var/lib/private/test-dynamicuser-migrate2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1067

1068
        test(m, "exec-dynamicuser-runtimedirectory1.service", status, CLD_EXITED);
×
1069
        test(m, "exec-dynamicuser-runtimedirectory2.service", status, CLD_EXITED);
×
UNCOV
1070
        test(m, "exec-dynamicuser-runtimedirectory3.service", status, CLD_EXITED);
×
1071
}
1072

UNCOV
1073
static void test_exec_environment(Manager *m) {
×
UNCOV
1074
        test(m, "exec-environment-no-substitute.service", 0, CLD_EXITED);
×
UNCOV
1075
        test(m, "exec-environment.service", 0, CLD_EXITED);
×
UNCOV
1076
        test(m, "exec-environment-multiple.service", 0, CLD_EXITED);
×
UNCOV
1077
        test(m, "exec-environment-empty.service", 0, CLD_EXITED);
×
UNCOV
1078
}
×
1079

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

UNCOV
1094
        r = write_string_file("/tmp/test-exec_environmentfile.conf", e, WRITE_STRING_FILE_CREATE);
×
UNCOV
1095
        ASSERT_OK(r);
×
1096

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

UNCOV
1099
        (void) unlink("/tmp/test-exec_environmentfile.conf");
×
UNCOV
1100
}
×
1101

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

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

1138
static void test_exec_runtimedirectory(Manager *m) {
×
UNCOV
1139
        (void) rm_rf("/run/test-exec_runtimedirectory2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
UNCOV
1140
        test(m, "exec-runtimedirectory.service", 0, CLD_EXITED);
×
UNCOV
1141
        (void) rm_rf("/run/test-exec_runtimedirectory2", REMOVE_ROOT|REMOVE_PHYSICAL);
×
1142

1143
        test(m, "exec-runtimedirectory-mode.service", 0, CLD_EXITED);
×
1144
        test(m, "exec-runtimedirectory-owner.service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
1145

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

1151
        if (!STR_IN_SET(NOBODY_GROUP_NAME, "nobody", "nfsnobody", "nogroup")) {
×
UNCOV
1152
                log_notice("Unsupported nobody group name '%s', skipping remaining tests in %s", NOBODY_GROUP_NAME, __func__);
×
1153
                return;
×
1154
        }
1155

UNCOV
1156
        test(m, "exec-runtimedirectory-owner-" NOBODY_GROUP_NAME ".service", MANAGER_IS_SYSTEM(m) ? 0 : EXIT_GROUP, CLD_EXITED);
×
1157
}
1158

1159
static void test_exec_capabilityboundingset(Manager *m) {
×
1160
        int r;
×
1161

1162
        r = find_executable("capsh", NULL);
×
UNCOV
1163
        if (r < 0) {
×
UNCOV
1164
                log_notice_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
×
1165
                return;
1166
        }
1167

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

UNCOV
1175
        test(m, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED);
×
1176
        test(m, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED);
×
1177
        test(m, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED);
×
UNCOV
1178
        test(m, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED);
×
1179
}
1180

UNCOV
1181
static void test_exec_basic(Manager *m) {
×
1182
        if (isempty(gnu_get_libc_version()))
×
1183
                return (void) log_tests_skipped("ConditionVersion=glibc will not pass under musl");
×
1184

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

1191
static void test_exec_ambientcapabilities(Manager *m) {
×
UNCOV
1192
        int r;
×
1193

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

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

UNCOV
1210
        test(m, "exec-ambientcapabilities.service", 0, CLD_EXITED);
×
UNCOV
1211
        test(m, "exec-ambientcapabilities-merge.service", 0, CLD_EXITED);
×
1212

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

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

1221
        if (!STR_IN_SET(NOBODY_USER_NAME, "nobody", "nfsnobody")) {
×
1222
                log_notice("Unsupported nobody user name '%s', skipping remaining tests in %s", NOBODY_USER_NAME, __func__);
×
UNCOV
1223
                return;
×
1224
        }
1225

UNCOV
1226
        test(m, "exec-ambientcapabilities-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
×
1227
        test(m, "exec-ambientcapabilities-merge-" NOBODY_USER_NAME ".service", 0, CLD_EXITED);
×
1228
}
1229

1230
static void test_exec_privatenetwork(Manager *m) {
×
1231
        int r;
×
1232

UNCOV
1233
        if (!have_net_dummy)
×
UNCOV
1234
                return (void)log_notice("Skipping %s, dummy network interface not available", __func__);
×
1235

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

UNCOV
1239
        r = find_executable("ip", NULL);
×
1240
        if (r < 0) {
×
1241
                log_notice_errno(r, "Skipping %s, could not find ip binary: %m", __func__);
×
1242
                return;
1243
        }
1244

UNCOV
1245
        test(m, "exec-privatenetwork-yes-privatemounts-no.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NETWORK : EXIT_FAILURE, CLD_EXITED);
×
1246
        test(m, "exec-privatenetwork-yes-privatemounts-yes.service", can_unshare ? 0 : MANAGER_IS_SYSTEM(m) ? EXIT_NETWORK : EXIT_NAMESPACE, CLD_EXITED);
×
1247
}
1248

1249
static void test_exec_networknamespacepath(Manager *m) {
×
1250
        int r;
×
1251

1252
        if (!have_net_dummy)
×
1253
                return (void)log_notice("Skipping %s, dummy network interface not available", __func__);
×
1254

UNCOV
1255
        if (!have_netns)
×
UNCOV
1256
                return (void)log_notice("Skipping %s, network namespace not available", __func__);
×
1257

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

UNCOV
1261
        r = find_executable("ip", NULL);
×
1262
        if (r < 0) {
×
1263
                log_notice_errno(r, "Skipping %s, could not find ip binary: %m", __func__);
×
1264
                return;
1265
        }
1266

UNCOV
1267
        test(m, "exec-networknamespacepath-privatemounts-no.service", MANAGER_IS_SYSTEM(m) ? EXIT_SUCCESS : EXIT_FAILURE, CLD_EXITED);
×
UNCOV
1268
        test(m, "exec-networknamespacepath-privatemounts-yes.service", can_unshare ? EXIT_SUCCESS : MANAGER_IS_SYSTEM(m) ? EXIT_FAILURE : EXIT_NAMESPACE, CLD_EXITED);
×
1269
}
1270

UNCOV
1271
static void test_exec_oomscoreadjust(Manager *m) {
×
1272
        test(m, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED);
×
1273

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

1281
static void test_exec_ioschedulingclass(Manager *m) {
×
UNCOV
1282
        test(m, "exec-ioschedulingclass-none.service", 0, CLD_EXITED);
×
UNCOV
1283
        test(m, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED);
×
1284
        test(m, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED);
×
1285

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

1293
static void test_exec_unsetenvironment(Manager *m) {
×
1294
        test(m, "exec-unsetenvironment.service", 0, CLD_EXITED);
×
1295
}
×
1296

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

UNCOV
1307
static void test_exec_standardinput(Manager *m) {
×
1308
        test(m, "exec-standardinput-data.service", 0, CLD_EXITED);
×
1309
        test(m, "exec-standardinput-file.service", 0, CLD_EXITED);
×
1310

UNCOV
1311
        ExecOutput saved = m->defaults.std_output;
×
1312
        m->defaults.std_output = EXEC_OUTPUT_NULL;
×
1313
        test(m, "exec-standardinput-file-cat.service", 0, CLD_EXITED);
×
1314
        m->defaults.std_output = saved;
×
UNCOV
1315
}
×
1316

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

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

1325
static void test_exec_standardoutput_truncate(Manager *m) {
×
UNCOV
1326
        test(m, "exec-standardoutput-truncate.service", 0, CLD_EXITED);
×
UNCOV
1327
}
×
1328

1329
static void test_exec_condition(Manager *m) {
×
UNCOV
1330
        test_service(m, "exec-condition-failed.service", SERVICE_FAILURE_EXIT_CODE);
×
UNCOV
1331
        test_service(m, "exec-condition-skip.service", SERVICE_SKIP_CONDITION);
×
1332
}
×
1333

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

1344
typedef struct test_entry {
1345
        test_function_t f;
1346
        const char *name;
1347
} test_entry;
1348

1349
#define entry(x) {x, #x}
1350

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

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

UNCOV
1411
        ASSERT_OK_ERRNO(unsetenv("USER"));
×
1412
        ASSERT_OK_ERRNO(unsetenv("LOGNAME"));
×
1413
        ASSERT_OK_ERRNO(unsetenv("SHELL"));
×
1414
        ASSERT_OK_ERRNO(unsetenv("HOME"));
×
1415
        ASSERT_OK_ERRNO(unsetenv("TMPDIR"));
×
1416

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

1427
        ASSERT_NOT_NULL((runtime_dir = setup_fake_runtime_dir()));
×
1428
        ASSERT_NOT_NULL((user_runtime_unit_dir = path_join(runtime_dir, "systemd/user")));
×
1429
        ASSERT_NOT_NULL((unit_paths = strjoin(PRIVATE_UNIT_DIR, ":", user_runtime_unit_dir)));
×
1430
        ASSERT_OK(setenv_unit_path(unit_paths));
×
1431

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

UNCOV
1436
        r = manager_new(scope, MANAGER_TEST_RUN_BASIC, &m);
×
UNCOV
1437
        if (manager_errno_skip_test(r))
×
UNCOV
1438
                return (void) log_tests_skipped_errno(r, "manager_new");
×
UNCOV
1439
        ASSERT_OK(r);
×
1440

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

1444
        /* Uncomment below if you want to make debugging logs stored to journal. */
1445
        //manager_override_log_target(m, LOG_TARGET_AUTO);
1446
        //manager_override_log_level(m, LOG_DEBUG);
1447

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

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

1460
        finish = now(CLOCK_MONOTONIC);
×
1461

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

UNCOV
1469
static int prepare_ns(const char *process_name) {
×
UNCOV
1470
        int r;
×
1471

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

UNCOV
1487
                const char *coverage = getenv("COVERAGE_BUILD_DIR");
×
1488
                if (!coverage)
×
1489
                        /* Make "/" read-only. */
UNCOV
1490
                        ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, NULL, "/", NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL));
×
1491

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

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

1502
                /* Copy unit files to make them accessible even when unprivileged. */
UNCOV
1503
                ASSERT_OK(get_testdata_dir("test-execute/", &unit_dir));
×
1504
                ASSERT_OK(copy_directory_at(AT_FDCWD, unit_dir, AT_FDCWD, PRIVATE_UNIT_DIR, UID_INVALID, GID_INVALID, COPY_MERGE_EMPTY));
×
1505

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

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

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

1522
                if (build_dir_mount) {
×
UNCOV
1523
                        int k;
×
1524

UNCOV
1525
                        ASSERT_OK_OR(k = RET_NERRNO(access(build_dir, F_OK)), -ENOENT);
×
1526

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

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

1541
                ASSERT_OK(write_string_file("/run/credstore/test-execute.load-credential", "foo", WRITE_STRING_FILE_CREATE));
×
1542
        }
1543

UNCOV
1544
        return r;
×
1545
}
1546

1547
TEST(run_tests_root) {
×
1548
        _cleanup_strv_free_ char **filters = NULL;
×
1549

1550
        if (!have_namespaces())
×
UNCOV
1551
                return (void) log_tests_skipped("unshare() is disabled");
×
1552

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

UNCOV
1556
        if (prepare_ns("(test-execute-root)") == 0) {
×
1557
                can_unshare = true;
×
1558
                run_tests(RUNTIME_SCOPE_SYSTEM, filters);
×
1559
                _exit(EXIT_SUCCESS);
×
1560
        }
1561
}
1562

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

1571
#if HAVE_SECCOMP
UNCOV
1572
        _cleanup_strv_free_ char **filters = NULL;
×
1573
        int r;
×
1574

1575
        /* The following tests are for 1beab8b0d0ff2d7d1436b52d4a0c3d56dc908962. */
1576
        if (!is_seccomp_available())
×
1577
                return (void) log_tests_skipped("Seccomp not available, cannot run unshare() filtered tests");
×
1578

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

1582
        if (prepare_ns("(test-execute-without-unshare)") == 0) {
×
UNCOV
1583
                _cleanup_hashmap_free_ Hashmap *s = NULL;
×
1584

1585
                r = sym_seccomp_syscall_resolve_name("unshare");
×
1586
                ASSERT_NE(r, __NR_SCMP_ERROR);
×
UNCOV
1587
                ASSERT_OK(hashmap_ensure_put(&s, NULL, UINT32_TO_PTR(r + 1), INT_TO_PTR(-1)));
×
UNCOV
1588
                ASSERT_OK(seccomp_load_syscall_filter_set_raw(SCMP_ACT_ALLOW, s, SCMP_ACT_ERRNO(EOPNOTSUPP), true));
×
1589

1590
                /* Check unshare() is actually filtered. */
UNCOV
1591
                ASSERT_ERROR_ERRNO(unshare(CLONE_NEWNS), EOPNOTSUPP);
×
1592

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

1602
TEST(run_tests_unprivileged) {
×
1603
        _cleanup_strv_free_ char **filters = NULL;
×
1604

1605
        if (!have_namespaces())
×
1606
                return (void) log_tests_skipped("unshare() is disabled");
×
1607

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

UNCOV
1611
        if (prepare_ns("(test-execute-unprivileged)") == 0) {
×
UNCOV
1612
                ASSERT_OK(capability_bounding_set_drop(0, /* right_now= */ true));
×
1613

UNCOV
1614
                can_unshare = false;
×
UNCOV
1615
                run_tests(RUNTIME_SCOPE_USER, filters);
×
1616
                _exit(EXIT_SUCCESS);
×
1617
        }
1618
}
1619

1620
static int intro(void) {
1✔
1621
        int r;
1✔
1622

1623
        /* It is needed otherwise cgroup creation fails */
1624
        if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0)
1✔
UNCOV
1625
                return log_tests_skipped("not privileged");
×
1626

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

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

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

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

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

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

1649
        return EXIT_SUCCESS;
1650
}
1651

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

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

1660
        return EXIT_SUCCESS;
1✔
1661
}
1662

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

© 2026 Coveralls, Inc