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

systemd / systemd / 13912360373

17 Mar 2025 10:34PM UTC coverage: 71.946% (+0.03%) from 71.915%
13912360373

push

github

web-flow
nsresourced,vmspawn: allow unpriv "tap" based networking in vmspawn (#36688)

This extends nsresourced to also allow delegation of a network tap
device (in addition to veth) to unpriv clients, with a strictly enforced
naming scheme.

also tightens security on a couple of things:

* enforces polkit on all nsresourced ops too (though by default still
everything is allowed)
* put a limit on delegated network devices
* forcibly clean up delegated network devices when the userns goes away

145 of 375 new or added lines in 14 files covered. (38.67%)

2324 existing lines in 47 files now uncovered.

296268 of 411794 relevant lines covered (71.95%)

711485.52 hits per line

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

71.98
/src/core/exec-invoke.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <linux/prctl.h>
4
#include <linux/sched.h>
5
#include <linux/securebits.h>
6
#include <sys/eventfd.h>
7
#include <sys/ioctl.h>
8
#include <sys/mount.h>
9
#include <sys/prctl.h>
10

11
#if HAVE_PAM
12
#include <security/pam_appl.h>
13
#include <security/pam_misc.h>
14
#endif
15

16
#include "sd-messages.h"
17

18
#include "apparmor-util.h"
19
#include "argv-util.h"
20
#include "ask-password-api.h"
21
#include "barrier.h"
22
#include "bitfield.h"
23
#include "bpf-dlopen.h"
24
#include "bpf-restrict-fs.h"
25
#include "btrfs-util.h"
26
#include "capability-util.h"
27
#include "cgroup-setup.h"
28
#include "chase.h"
29
#include "chattr-util.h"
30
#include "chown-recursive.h"
31
#include "copy.h"
32
#include "env-util.h"
33
#include "escape.h"
34
#include "exec-credential.h"
35
#include "exec-invoke.h"
36
#include "execute.h"
37
#include "exit-status.h"
38
#include "fd-util.h"
39
#include "hexdecoct.h"
40
#include "hostname-setup.h"
41
#include "io-util.h"
42
#include "ioprio-util.h"
43
#include "iovec-util.h"
44
#include "journal-send.h"
45
#include "memfd-util.h"
46
#include "missing_sched.h"
47
#include "missing_syscall.h"
48
#include "mkdir-label.h"
49
#include "osc-context.h"
50
#include "proc-cmdline.h"
51
#include "process-util.h"
52
#include "psi-util.h"
53
#include "rlimit-util.h"
54
#include "seccomp-util.h"
55
#include "selinux-util.h"
56
#include "signal-util.h"
57
#include "smack-util.h"
58
#include "socket-util.h"
59
#include "string-table.h"
60
#include "strv.h"
61
#include "terminal-util.h"
62
#include "utmp-wtmp.h"
63
#include "vpick.h"
64

65
#define IDLE_TIMEOUT_USEC (5*USEC_PER_SEC)
66
#define IDLE_TIMEOUT2_USEC (1*USEC_PER_SEC)
67

68
#define SNDBUF_SIZE (8*1024*1024)
69

70
static int flag_fds(
10,874✔
71
                const int fds[],
72
                size_t n_socket_fds,
73
                size_t n_fds,
74
                bool nonblock) {
75

76
        int r;
10,874✔
77

78
        assert(fds || n_fds == 0);
10,874✔
79

80
        /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags.
81
         * O_NONBLOCK only applies to socket activation though. */
82

83
        for (size_t i = 0; i < n_fds; i++) {
13,459✔
84

85
                if (i < n_socket_fds) {
2,585✔
86
                        r = fd_nonblock(fds[i], nonblock);
2,404✔
87
                        if (r < 0)
2,404✔
88
                                return r;
89
                }
90

91
                /* We unconditionally drop FD_CLOEXEC from the fds,
92
                 * since after all we want to pass these fds to our
93
                 * children */
94

95
                r = fd_cloexec(fds[i], false);
2,585✔
96
                if (r < 0)
2,585✔
97
                        return r;
98
        }
99

100
        return 0;
101
}
102

103
static bool is_terminal_input(ExecInput i) {
49,787✔
104
        return IN_SET(i,
49,787✔
105
                      EXEC_INPUT_TTY,
106
                      EXEC_INPUT_TTY_FORCE,
107
                      EXEC_INPUT_TTY_FAIL);
108
}
109

110
static bool is_terminal_output(ExecOutput o) {
46,549✔
111
        return IN_SET(o,
46,549✔
112
                      EXEC_OUTPUT_TTY,
113
                      EXEC_OUTPUT_KMSG_AND_CONSOLE,
114
                      EXEC_OUTPUT_JOURNAL_AND_CONSOLE);
115
}
116

117
static bool is_kmsg_output(ExecOutput o) {
12,059✔
118
        return IN_SET(o,
12,059✔
119
                      EXEC_OUTPUT_KMSG,
120
                      EXEC_OUTPUT_KMSG_AND_CONSOLE);
121
}
122

123
static bool exec_context_needs_term(const ExecContext *c) {
10,895✔
124
        assert(c);
10,895✔
125

126
        /* Return true if the execution context suggests we should set $TERM to something useful. */
127

128
        if (is_terminal_input(c->std_input))
10,895✔
129
                return true;
130

131
        if (is_terminal_output(c->std_output))
10,723✔
132
                return true;
133

134
        if (is_terminal_output(c->std_error))
10,463✔
135
                return true;
136

137
        return !!c->tty_path;
10,462✔
138
}
139

140
static int open_null_as(int flags, int nfd) {
12,836✔
141
        int fd;
12,836✔
142

143
        assert(nfd >= 0);
12,836✔
144

145
        fd = open("/dev/null", flags|O_NOCTTY);
12,836✔
146
        if (fd < 0)
12,836✔
147
                return -errno;
×
148

149
        return move_fd(fd, nfd, false);
12,836✔
150
}
151

152
static int connect_journal_socket(
12,059✔
153
                int fd,
154
                const char *log_namespace,
155
                uid_t uid,
156
                gid_t gid) {
157

158
        uid_t olduid = UID_INVALID;
12,059✔
159
        gid_t oldgid = GID_INVALID;
12,059✔
160
        const char *j;
12,059✔
161
        int r;
12,059✔
162

163
        assert(fd >= 0);
12,059✔
164

165
        j = journal_stream_path(log_namespace);
12,071✔
166
        if (!j)
2✔
167
                return -EINVAL;
×
168

169
        if (gid_is_valid(gid)) {
12,059✔
170
                oldgid = getgid();
2,449✔
171

172
                if (setegid(gid) < 0)
2,449✔
173
                        return -errno;
×
174
        }
175

176
        if (uid_is_valid(uid)) {
12,059✔
177
                olduid = getuid();
2,446✔
178

179
                if (seteuid(uid) < 0) {
2,446✔
180
                        r = -errno;
×
181
                        goto restore_gid;
×
182
                }
183
        }
184

185
        r = connect_unix_path(fd, AT_FDCWD, j);
12,059✔
186

187
        /* If we fail to restore the uid or gid, things will likely fail later on. This should only happen if
188
           an LSM interferes. */
189

190
        if (uid_is_valid(uid))
12,059✔
191
                (void) seteuid(olduid);
2,446✔
192

193
 restore_gid:
9,613✔
194
        if (gid_is_valid(gid))
12,059✔
195
                (void) setegid(oldgid);
2,449✔
196

197
        return r;
198
}
199

200
static int connect_logger_as(
12,059✔
201
                const ExecContext *context,
202
                const ExecParameters *params,
203
                ExecOutput output,
204
                const char *ident,
205
                int nfd,
206
                uid_t uid,
207
                gid_t gid) {
208

209
        _cleanup_close_ int fd = -EBADF;
12,059✔
210
        int r;
12,059✔
211

212
        assert(context);
12,059✔
213
        assert(params);
12,059✔
214
        assert(output < _EXEC_OUTPUT_MAX);
12,059✔
215
        assert(ident);
12,059✔
216
        assert(nfd >= 0);
12,059✔
217

218
        fd = socket(AF_UNIX, SOCK_STREAM, 0);
12,059✔
219
        if (fd < 0)
12,059✔
220
                return -errno;
×
221

222
        r = connect_journal_socket(fd, context->log_namespace, uid, gid);
12,059✔
223
        if (r < 0)
12,059✔
224
                return r;
225

226
        if (shutdown(fd, SHUT_RD) < 0)
12,059✔
227
                return -errno;
×
228

229
        (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
12,059✔
230

231
        if (dprintf(fd,
23,518✔
232
                "%s\n"
233
                "%s\n"
234
                "%i\n"
235
                "%i\n"
236
                "%i\n"
237
                "%i\n"
238
                "%i\n",
239
                context->syslog_identifier ?: ident,
12,059✔
240
                params->flags & EXEC_PASS_LOG_UNIT ? params->unit_id : "",
12,059✔
241
                context->syslog_priority,
12,059✔
242
                !!context->syslog_level_prefix,
12,059✔
243
                false,
244
                is_kmsg_output(output),
12,059✔
245
                is_terminal_output(output)) < 0)
12,059✔
246
                return -errno;
×
247

248
        return move_fd(TAKE_FD(fd), nfd, false);
12,059✔
249
}
250

251
static int open_terminal_as(const char *path, int flags, int nfd) {
32✔
252
        int fd;
32✔
253

254
        assert(path);
32✔
255
        assert(nfd >= 0);
32✔
256

257
        fd = open_terminal(path, flags | O_NOCTTY);
32✔
258
        if (fd < 0)
32✔
259
                return fd;
260

261
        return move_fd(fd, nfd, false);
32✔
262
}
263

264
static int acquire_path(const char *path, int flags, mode_t mode) {
11✔
265
        _cleanup_close_ int fd = -EBADF;
11✔
266
        int r;
11✔
267

268
        assert(path);
11✔
269

270
        if (IN_SET(flags & O_ACCMODE, O_WRONLY, O_RDWR))
11✔
271
                flags |= O_CREAT;
11✔
272

273
        fd = open(path, flags|O_NOCTTY, mode);
11✔
274
        if (fd >= 0)
11✔
275
                return TAKE_FD(fd);
11✔
276

277
        if (errno != ENXIO) /* ENXIO is returned when we try to open() an AF_UNIX file system socket on Linux */
×
278
                return -errno;
×
279

280
        /* So, it appears the specified path could be an AF_UNIX socket. Let's see if we can connect to it. */
281

282
        fd = socket(AF_UNIX, SOCK_STREAM, 0);
×
283
        if (fd < 0)
×
284
                return -errno;
×
285

286
        r = connect_unix_path(fd, AT_FDCWD, path);
×
287
        if (IN_SET(r, -ENOTSOCK, -EINVAL))
11✔
288
                /* Propagate initial error if we get ENOTSOCK or EINVAL, i.e. we have indication that this
289
                 * wasn't an AF_UNIX socket after all */
290
                return -ENXIO;
291
        if (r < 0)
×
292
                return r;
293

294
        if ((flags & O_ACCMODE) == O_RDONLY)
×
295
                r = shutdown(fd, SHUT_WR);
×
296
        else if ((flags & O_ACCMODE) == O_WRONLY)
×
297
                r = shutdown(fd, SHUT_RD);
×
298
        else
299
                r = 0;
300
        if (r < 0)
×
301
                return -errno;
×
302

303
        return TAKE_FD(fd);
304
}
305

306
static int fixup_input(
38,502✔
307
                const ExecContext *context,
308
                int socket_fd,
309
                bool apply_tty_stdin) {
310

311
        ExecInput std_input;
38,502✔
312

313
        assert(context);
38,502✔
314

315
        std_input = context->std_input;
38,502✔
316

317
        if (is_terminal_input(std_input) && !apply_tty_stdin)
38,502✔
318
                return EXEC_INPUT_NULL;
319

320
        if (std_input == EXEC_INPUT_SOCKET && socket_fd < 0)
38,502✔
321
                return EXEC_INPUT_NULL;
322

323
        if (std_input == EXEC_INPUT_DATA && context->stdin_data_size == 0)
38,502✔
324
                return EXEC_INPUT_NULL;
×
325

326
        return std_input;
327
}
328

329
static int fixup_output(ExecOutput output, int socket_fd) {
38,502✔
330

331
        if (output == EXEC_OUTPUT_SOCKET && socket_fd < 0)
38,502✔
332
                return EXEC_OUTPUT_INHERIT;
×
333

334
        return output;
335
}
336

337
static int setup_input(
13,305✔
338
                const ExecContext *context,
339
                const ExecParameters *params,
340
                int socket_fd,
341
                const int named_iofds[static 3]) {
342

343
        ExecInput i;
13,305✔
344
        int r;
13,305✔
345

346
        assert(context);
13,305✔
347
        assert(params);
13,305✔
348
        assert(named_iofds);
13,305✔
349

350
        if (params->stdin_fd >= 0) {
13,305✔
351
                if (dup2(params->stdin_fd, STDIN_FILENO) < 0)
471✔
352
                        return -errno;
×
353

354
                /* Try to make this our controlling tty, if it is a tty */
355
                if (isatty_safe(STDIN_FILENO) && ioctl(STDIN_FILENO, TIOCSCTTY, context->std_input == EXEC_INPUT_TTY_FORCE) < 0)
471✔
356
                        log_debug_errno(errno, "Failed to make standard input TTY our controlling terminal: %m");
2✔
357

358
                return STDIN_FILENO;
471✔
359
        }
360

361
        i = fixup_input(context, socket_fd, params->flags & EXEC_APPLY_TTY_STDIN);
12,834✔
362

363
        switch (i) {
12,834✔
364

365
        case EXEC_INPUT_NULL:
12,468✔
366
                return open_null_as(O_RDONLY, STDIN_FILENO);
12,468✔
367

368
        case EXEC_INPUT_TTY:
354✔
369
        case EXEC_INPUT_TTY_FORCE:
370
        case EXEC_INPUT_TTY_FAIL: {
371
                _cleanup_close_ int tty_fd = -EBADF;
13,659✔
372
                const char *tty_path;
354✔
373

374
                tty_path = ASSERT_PTR(exec_context_tty_path(context));
354✔
375

376
                tty_fd = acquire_terminal(tty_path,
708✔
377
                                          i == EXEC_INPUT_TTY_FAIL  ? ACQUIRE_TERMINAL_TRY :
354✔
378
                                          i == EXEC_INPUT_TTY_FORCE ? ACQUIRE_TERMINAL_FORCE :
379
                                                                      ACQUIRE_TERMINAL_WAIT,
380
                                          USEC_INFINITY);
381
                if (tty_fd < 0)
354✔
382
                        return tty_fd;
383

384
                r = move_fd(tty_fd, STDIN_FILENO, /* cloexec= */ false);
354✔
385
                if (r < 0)
354✔
386
                        return r;
×
387

388
                TAKE_FD(tty_fd);
389
                return r;
390
        }
391

392
        case EXEC_INPUT_SOCKET:
11✔
393
                assert(socket_fd >= 0);
11✔
394

395
                return RET_NERRNO(dup2(socket_fd, STDIN_FILENO));
11✔
396

397
        case EXEC_INPUT_NAMED_FD:
×
398
                assert(named_iofds[STDIN_FILENO] >= 0);
×
399

400
                (void) fd_nonblock(named_iofds[STDIN_FILENO], false);
×
401
                return RET_NERRNO(dup2(named_iofds[STDIN_FILENO], STDIN_FILENO));
13,305✔
402

403
        case EXEC_INPUT_DATA: {
1✔
404
                int fd;
1✔
405

406
                fd = memfd_new_and_seal("exec-input", context->stdin_data, context->stdin_data_size);
1✔
407
                if (fd < 0)
1✔
408
                        return fd;
409

410
                return move_fd(fd, STDIN_FILENO, false);
1✔
411
        }
412

413
        case EXEC_INPUT_FILE: {
×
414
                bool rw;
×
415
                int fd;
×
416

417
                assert(context->stdio_file[STDIN_FILENO]);
×
418

419
                rw = (context->std_output == EXEC_OUTPUT_FILE && streq_ptr(context->stdio_file[STDIN_FILENO], context->stdio_file[STDOUT_FILENO])) ||
×
420
                        (context->std_error == EXEC_OUTPUT_FILE && streq_ptr(context->stdio_file[STDIN_FILENO], context->stdio_file[STDERR_FILENO]));
×
421

422
                fd = acquire_path(context->stdio_file[STDIN_FILENO], rw ? O_RDWR : O_RDONLY, 0666 & ~context->umask);
×
423
                if (fd < 0)
×
424
                        return fd;
425

426
                return move_fd(fd, STDIN_FILENO, false);
×
427
        }
428

429
        default:
×
430
                assert_not_reached();
×
431
        }
432
}
433

434
static bool can_inherit_stderr_from_stdout(
12,834✔
435
                const ExecContext *context,
436
                ExecOutput o,
437
                ExecOutput e) {
438

439
        assert(context);
12,834✔
440

441
        /* Returns true, if given the specified STDERR and STDOUT output we can directly dup() the stdout fd to the
442
         * stderr fd */
443

444
        if (e == EXEC_OUTPUT_INHERIT)
12,834✔
445
                return true;
446
        if (e != o)
376✔
447
                return false;
448

449
        if (e == EXEC_OUTPUT_NAMED_FD)
373✔
450
                return streq_ptr(context->stdio_fdname[STDOUT_FILENO], context->stdio_fdname[STDERR_FILENO]);
×
451

452
        if (IN_SET(e, EXEC_OUTPUT_FILE, EXEC_OUTPUT_FILE_APPEND, EXEC_OUTPUT_FILE_TRUNCATE))
373✔
453
                return streq_ptr(context->stdio_file[STDOUT_FILENO], context->stdio_file[STDERR_FILENO]);
4✔
454

455
        return true;
456
}
457

458
static int setup_output(
26,610✔
459
                const ExecContext *context,
460
                const ExecParameters *params,
461
                int fileno,
462
                int socket_fd,
463
                const int named_iofds[static 3],
464
                const char *ident,
465
                uid_t uid,
466
                gid_t gid,
467
                dev_t *journal_stream_dev,
468
                ino_t *journal_stream_ino) {
469

470
        ExecOutput o;
26,610✔
471
        ExecInput i;
26,610✔
472
        int r;
26,610✔
473

474
        assert(context);
26,610✔
475
        assert(params);
26,610✔
476
        assert(ident);
26,610✔
477
        assert(journal_stream_dev);
26,610✔
478
        assert(journal_stream_ino);
26,610✔
479

480
        if (fileno == STDOUT_FILENO && params->stdout_fd >= 0) {
26,610✔
481

482
                if (dup2(params->stdout_fd, STDOUT_FILENO) < 0)
471✔
483
                        return -errno;
×
484

485
                return STDOUT_FILENO;
486
        }
487

488
        if (fileno == STDERR_FILENO && params->stderr_fd >= 0) {
26,139✔
489
                if (dup2(params->stderr_fd, STDERR_FILENO) < 0)
471✔
490
                        return -errno;
×
491

492
                return STDERR_FILENO;
493
        }
494

495
        i = fixup_input(context, socket_fd, params->flags & EXEC_APPLY_TTY_STDIN);
25,668✔
496
        o = fixup_output(context->std_output, socket_fd);
25,668✔
497

498
        // FIXME: we probably should spend some time here to verify that if we inherit an fd from stdin
499
        // (possibly indirect via inheritance from stdout) it is actually opened for write!
500

501
        if (fileno == STDERR_FILENO) {
25,668✔
502
                ExecOutput e;
12,834✔
503
                e = fixup_output(context->std_error, socket_fd);
12,834✔
504

505
                /* This expects the input and output are already set up */
506

507
                /* Don't change the stderr file descriptor if we inherit all
508
                 * the way and are not on a tty */
509
                if (e == EXEC_OUTPUT_INHERIT &&
12,834✔
510
                    o == EXEC_OUTPUT_INHERIT &&
8✔
511
                    i == EXEC_INPUT_NULL &&
×
512
                    !is_terminal_input(context->std_input) &&
×
513
                    getppid() != 1)
×
514
                        return fileno;
515

516
                /* Duplicate from stdout if possible */
517
                if (can_inherit_stderr_from_stdout(context, o, e))
12,834✔
518
                        return RET_NERRNO(dup2(STDOUT_FILENO, fileno));
12,827✔
519

520
                o = e;
521

522
        } else if (o == EXEC_OUTPUT_INHERIT) {
12,834✔
523
                /* If input got downgraded, inherit the original value */
524
                if (i == EXEC_INPUT_NULL && is_terminal_input(context->std_input))
8✔
525
                        return open_terminal_as(exec_context_tty_path(context), O_WRONLY, fileno);
×
526

527
                /* If the input is connected to anything that's not a /dev/null or a data fd, inherit that... */
528
                if (!IN_SET(i, EXEC_INPUT_NULL, EXEC_INPUT_DATA))
8✔
529
                        return RET_NERRNO(dup2(STDIN_FILENO, fileno));
8✔
530

531
                /* If we are not started from PID 1 we just inherit STDOUT from our parent process. */
532
                if (getppid() != 1)
×
533
                        return fileno;
534

535
                /* We need to open /dev/null here anew, to get the right access mode. */
536
                return open_null_as(O_WRONLY, fileno);
×
537
        }
538

539
        switch (o) {
12,833✔
540

541
        case EXEC_OUTPUT_NULL:
368✔
542
                return open_null_as(O_WRONLY, fileno);
368✔
543

544
        case EXEC_OUTPUT_TTY:
386✔
545
                if (is_terminal_input(i))
386✔
546
                        return RET_NERRNO(dup2(STDIN_FILENO, fileno));
354✔
547

548
                return open_terminal_as(exec_context_tty_path(context), O_WRONLY, fileno);
32✔
549

550
        case EXEC_OUTPUT_KMSG:
12,059✔
551
        case EXEC_OUTPUT_KMSG_AND_CONSOLE:
552
        case EXEC_OUTPUT_JOURNAL:
553
        case EXEC_OUTPUT_JOURNAL_AND_CONSOLE:
554
                r = connect_logger_as(context, params, o, ident, fileno, uid, gid);
12,059✔
555
                if (r < 0) {
12,059✔
556
                        log_exec_warning_errno(context,
×
557
                                               params,
558
                                               r,
559
                                               "Failed to connect %s to the journal socket, ignoring: %m",
560
                                               fileno == STDOUT_FILENO ? "stdout" : "stderr");
561
                        r = open_null_as(O_WRONLY, fileno);
×
562
                } else {
563
                        struct stat st;
12,059✔
564

565
                        /* If we connected this fd to the journal via a stream, patch the device/inode into the passed
566
                         * parameters, but only then. This is useful so that we can set $JOURNAL_STREAM that permits
567
                         * services to detect whether they are connected to the journal or not.
568
                         *
569
                         * If both stdout and stderr are connected to a stream then let's make sure to store the data
570
                         * about STDERR as that's usually the best way to do logging. */
571

572
                        if (fstat(fileno, &st) >= 0 &&
12,059✔
573
                            (*journal_stream_ino == 0 || fileno == STDERR_FILENO)) {
12,059✔
574
                                *journal_stream_dev = st.st_dev;
12,059✔
575
                                *journal_stream_ino = st.st_ino;
12,059✔
576
                        }
577
                }
578
                return r;
579

580
        case EXEC_OUTPUT_SOCKET:
9✔
581
                assert(socket_fd >= 0);
9✔
582

583
                return RET_NERRNO(dup2(socket_fd, fileno));
9✔
584

585
        case EXEC_OUTPUT_NAMED_FD:
×
586
                assert(named_iofds[fileno] >= 0);
×
587

588
                (void) fd_nonblock(named_iofds[fileno], false);
×
589
                return RET_NERRNO(dup2(named_iofds[fileno], fileno));
26,610✔
590

591
        case EXEC_OUTPUT_FILE:
11✔
592
        case EXEC_OUTPUT_FILE_APPEND:
593
        case EXEC_OUTPUT_FILE_TRUNCATE: {
594
                bool rw;
11✔
595
                int fd, flags;
11✔
596

597
                assert(context->stdio_file[fileno]);
11✔
598

599
                rw = context->std_input == EXEC_INPUT_FILE &&
11✔
600
                        streq_ptr(context->stdio_file[fileno], context->stdio_file[STDIN_FILENO]);
×
601

602
                if (rw)
11✔
603
                        return RET_NERRNO(dup2(STDIN_FILENO, fileno));
×
604

605
                flags = O_WRONLY;
11✔
606
                if (o == EXEC_OUTPUT_FILE_APPEND)
11✔
607
                        flags |= O_APPEND;
608
                else if (o == EXEC_OUTPUT_FILE_TRUNCATE)
9✔
609
                        flags |= O_TRUNC;
3✔
610

611
                fd = acquire_path(context->stdio_file[fileno], flags, 0666 & ~context->umask);
11✔
612
                if (fd < 0)
11✔
613
                        return fd;
614

615
                return move_fd(fd, fileno, 0);
11✔
616
        }
617

618
        default:
×
619
                assert_not_reached();
×
620
        }
621
}
622

623
static int chown_terminal(int fd, uid_t uid) {
2,713✔
624
        int r;
2,713✔
625

626
        assert(fd >= 0);
2,713✔
627

628
        /* Before we chown/chmod the TTY, let's ensure this is actually a tty */
629
        if (!isatty_safe(fd))
2,713✔
630
                return 0;
631

632
        /* This might fail. What matters are the results. */
633
        r = fchmod_and_chown(fd, TTY_MODE, uid, GID_INVALID);
7✔
634
        if (r < 0)
7✔
635
                return r;
×
636

637
        return 1;
638
}
639

640
static int setup_confirm_stdio(
×
641
                const ExecContext *context,
642
                const char *vc,
643
                int *ret_saved_stdin,
644
                int *ret_saved_stdout) {
645

646
        _cleanup_close_ int fd = -EBADF, saved_stdin = -EBADF, saved_stdout = -EBADF;
×
647
        int r;
×
648

649
        assert(context);
×
650
        assert(ret_saved_stdin);
×
651
        assert(ret_saved_stdout);
×
652

653
        saved_stdin = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, 3);
×
654
        if (saved_stdin < 0)
×
655
                return -errno;
×
656

657
        saved_stdout = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 3);
×
658
        if (saved_stdout < 0)
×
659
                return -errno;
×
660

661
        fd = acquire_terminal(vc, ACQUIRE_TERMINAL_WAIT, DEFAULT_CONFIRM_USEC);
×
662
        if (fd < 0)
×
663
                return fd;
664

665
        _cleanup_close_ int lock_fd = lock_dev_console();
×
666
        if (lock_fd < 0)
×
667
                log_debug_errno(lock_fd, "Failed to lock /dev/console, ignoring: %m");
×
668

669
        r = chown_terminal(fd, getuid());
×
670
        if (r < 0)
×
671
                return r;
672

673
        r = terminal_reset_defensive(fd, TERMINAL_RESET_SWITCH_TO_TEXT);
×
674
        if (r < 0)
×
675
                return r;
676

677
        r = exec_context_apply_tty_size(context, fd, fd, vc);
×
678
        if (r < 0)
×
679
                return r;
680

681
        r = rearrange_stdio(fd, fd, STDERR_FILENO); /* Invalidates 'fd' also on failure */
×
682
        TAKE_FD(fd);
×
683
        if (r < 0)
×
684
                return r;
685

686
        *ret_saved_stdin = TAKE_FD(saved_stdin);
×
687
        *ret_saved_stdout = TAKE_FD(saved_stdout);
×
688
        return 0;
×
689
}
690

691
static void write_confirm_error_fd(int err, int fd, const char *unit_id) {
×
692
        assert(err != 0);
×
693
        assert(fd >= 0);
×
694
        assert(unit_id);
×
695

696
        errno = abs(err);
×
697

698
        if (errno == ETIMEDOUT)
×
699
                dprintf(fd, "Confirmation question timed out for %s, assuming positive response.\n", unit_id);
×
700
        else
701
                dprintf(fd, "Couldn't ask confirmation for %s, assuming positive response: %m\n", unit_id);
×
702
}
×
703

704
static void write_confirm_error(int err, const char *vc, const char *unit_id) {
×
705
        _cleanup_close_ int fd = -EBADF;
×
706

707
        assert(vc);
×
708

709
        fd = open_terminal(vc, O_WRONLY|O_NOCTTY|O_CLOEXEC);
×
710
        if (fd < 0)
×
711
                return;
×
712

713
        write_confirm_error_fd(err, fd, unit_id);
×
714
}
715

716
static int restore_confirm_stdio(int *saved_stdin, int *saved_stdout) {
×
717
        int r = 0;
×
718

719
        assert(saved_stdin);
×
720
        assert(saved_stdout);
×
721

722
        release_terminal();
×
723

724
        if (*saved_stdin >= 0)
×
725
                if (dup2(*saved_stdin, STDIN_FILENO) < 0)
×
726
                        r = -errno;
×
727

728
        if (*saved_stdout >= 0)
×
729
                if (dup2(*saved_stdout, STDOUT_FILENO) < 0)
×
730
                        r = -errno;
×
731

732
        *saved_stdin = safe_close(*saved_stdin);
×
733
        *saved_stdout = safe_close(*saved_stdout);
×
734

735
        return r;
×
736
}
737

738
enum {
739
        CONFIRM_PRETEND_FAILURE = -1,
740
        CONFIRM_PRETEND_SUCCESS =  0,
741
        CONFIRM_EXECUTE = 1,
742
};
743

744
static bool confirm_spawn_disabled(void) {
×
745
        return access("/run/systemd/confirm_spawn_disabled", F_OK) >= 0;
×
746
}
747

748
static int ask_for_confirmation(const ExecContext *context, const ExecParameters *params, const char *cmdline) {
×
749
        int saved_stdout = -EBADF, saved_stdin = -EBADF, r;
×
750
        _cleanup_free_ char *e = NULL;
×
751
        char c;
×
752

753
        assert(context);
×
754
        assert(params);
×
755

756
        /* For any internal errors, assume a positive response. */
757
        r = setup_confirm_stdio(context, params->confirm_spawn, &saved_stdin, &saved_stdout);
×
758
        if (r < 0) {
×
759
                write_confirm_error(r, params->confirm_spawn, params->unit_id);
×
760
                return CONFIRM_EXECUTE;
761
        }
762

763
        /* confirm_spawn might have been disabled while we were sleeping. */
764
        if (!params->confirm_spawn || confirm_spawn_disabled()) {
×
765
                r = 1;
×
766
                goto restore_stdio;
×
767
        }
768

769
        e = ellipsize(cmdline, 60, 100);
×
770
        if (!e) {
×
771
                log_oom();
×
772
                r = CONFIRM_EXECUTE;
×
773
                goto restore_stdio;
×
774
        }
775

776
        for (;;) {
×
777
                r = ask_char(&c, "yfshiDjcn", "Execute %s? [y, f, s – h for help] ", e);
×
778
                if (r < 0) {
×
779
                        write_confirm_error_fd(r, STDOUT_FILENO, params->unit_id);
×
780
                        r = CONFIRM_EXECUTE;
×
781
                        goto restore_stdio;
×
782
                }
783

784
                switch (c) {
×
785
                case 'c':
×
786
                        printf("Resuming normal execution.\n");
×
787
                        manager_disable_confirm_spawn();
×
788
                        r = 1;
789
                        break;
790
                case 'D':
×
791
                        printf("  Unit: %s\n",
×
792
                               params->unit_id);
×
793
                        exec_context_dump(context, stdout, "  ");
×
794
                        exec_params_dump(params, stdout, "  ");
×
795
                        continue; /* ask again */
×
796
                case 'f':
×
797
                        printf("Failing execution.\n");
×
798
                        r = CONFIRM_PRETEND_FAILURE;
799
                        break;
800
                case 'h':
×
801
                        printf("  c - continue, proceed without asking anymore\n"
×
802
                               "  D - dump, show the state of the unit\n"
803
                               "  f - fail, don't execute the command and pretend it failed\n"
804
                               "  h - help\n"
805
                               "  i - info, show a short summary of the unit\n"
806
                               "  j - jobs, show jobs that are in progress\n"
807
                               "  s - skip, don't execute the command and pretend it succeeded\n"
808
                               "  y - yes, execute the command\n");
809
                        continue; /* ask again */
×
810
                case 'i':
×
811
                        printf("  Unit:        %s\n"
×
812
                               "  Command:     %s\n",
813
                               params->unit_id, cmdline);
×
814
                        continue; /* ask again */
×
815
                case 'j':
×
816
                        if (sigqueue(getppid(),
×
817
                                     SIGRTMIN+18,
×
818
                                     (const union sigval) { .sival_int = MANAGER_SIGNAL_COMMAND_DUMP_JOBS }) < 0)
×
819
                                return -errno;
×
820

821
                        continue; /* ask again */
×
822
                case 'n':
×
823
                        /* 'n' was removed in favor of 'f'. */
824
                        printf("Didn't understand 'n', did you mean 'f'?\n");
×
825
                        continue; /* ask again */
×
826
                case 's':
×
827
                        printf("Skipping execution.\n");
×
828
                        r = CONFIRM_PRETEND_SUCCESS;
829
                        break;
830
                case 'y':
831
                        r = CONFIRM_EXECUTE;
832
                        break;
833
                default:
×
834
                        assert_not_reached();
×
835
                }
836
                break;
837
        }
838

839
restore_stdio:
×
840
        restore_confirm_stdio(&saved_stdin, &saved_stdout);
×
841
        return r;
842
}
843

844
static int get_fixed_user(
10,944✔
845
                const char *user_or_uid,
846
                const char **ret_username,
847
                uid_t *ret_uid,
848
                gid_t *ret_gid,
849
                const char **ret_home,
850
                const char **ret_shell) {
851

852
        int r;
10,944✔
853

854
        assert(user_or_uid);
10,944✔
855
        assert(ret_username);
10,944✔
856

857
        r = get_user_creds(&user_or_uid, ret_uid, ret_gid, ret_home, ret_shell, USER_CREDS_CLEAN);
10,944✔
858
        if (r < 0)
10,944✔
859
                return r;
860

861
        /* user_or_uid is normalized by get_user_creds to username */
862
        *ret_username = user_or_uid;
10,942✔
863

864
        return 0;
10,942✔
865
}
866

867
static int get_fixed_group(
9✔
868
                const char *group_or_gid,
869
                const char **ret_groupname,
870
                gid_t *ret_gid) {
871

872
        int r;
9✔
873

874
        assert(group_or_gid);
9✔
875
        assert(ret_groupname);
9✔
876

877
        r = get_group_creds(&group_or_gid, ret_gid, /* flags = */ 0);
9✔
878
        if (r < 0)
9✔
879
                return r;
880

881
        /* group_or_gid is normalized by get_group_creds to groupname */
882
        *ret_groupname = group_or_gid;
9✔
883

884
        return 0;
9✔
885
}
886

887
static int get_supplementary_groups(
13,305✔
888
                const ExecContext *c,
889
                const char *user,
890
                gid_t gid,
891
                gid_t **ret_gids) {
892

893
        int r;
13,305✔
894

895
        assert(c);
13,305✔
896
        assert(ret_gids);
13,305✔
897

898
        /*
899
         * If user is given, then lookup GID and supplementary groups list.
900
         * We avoid NSS lookups for gid=0. Also we have to initialize groups
901
         * here and as early as possible so we keep the list of supplementary
902
         * groups of the caller.
903
         */
904
        bool keep_groups = false;
13,305✔
905
        if (user && gid_is_valid(gid) && gid != 0) {
16,018✔
906
                /* First step, initialize groups from /etc/groups */
907
                if (initgroups(user, gid) < 0)
2,611✔
908
                        return -errno;
13,305✔
909

910
                keep_groups = true;
911
        }
912

913
        if (strv_isempty(c->supplementary_groups)) {
13,305✔
914
                *ret_gids = NULL;
13,296✔
915
                return 0;
13,296✔
916
        }
917

918
        /*
919
         * If SupplementaryGroups= was passed then NGROUPS_MAX has to
920
         * be positive, otherwise fail.
921
         */
922
        errno = 0;
9✔
923
        int ngroups_max = (int) sysconf(_SC_NGROUPS_MAX);
9✔
924
        if (ngroups_max <= 0)
9✔
925
                return errno_or_else(EOPNOTSUPP);
×
926

927
        _cleanup_free_ gid_t *l_gids = new(gid_t, ngroups_max);
18✔
928
        if (!l_gids)
9✔
929
                return -ENOMEM;
930

931
        int k = 0;
9✔
932
        if (keep_groups) {
9✔
933
                /*
934
                 * Lookup the list of groups that the user belongs to, we
935
                 * avoid NSS lookups here too for gid=0.
936
                 */
937
                k = ngroups_max;
9✔
938
                if (getgrouplist(user, gid, l_gids, &k) < 0)
9✔
939
                        return -EINVAL;
940
        }
941

942
        STRV_FOREACH(i, c->supplementary_groups) {
18✔
943
                if (k >= ngroups_max)
9✔
944
                        return -E2BIG;
×
945

946
                const char *g = *i;
9✔
947
                r = get_group_creds(&g, l_gids + k, /* flags = */ 0);
9✔
948
                if (r < 0)
9✔
949
                        return r;
950

951
                k++;
9✔
952
        }
953

954
        if (k == 0) {
9✔
955
                *ret_gids = NULL;
×
956
                return 0;
×
957
        }
958

959
        /* Otherwise get the final list of supplementary groups */
960
        gid_t *groups = newdup(gid_t, l_gids, k);
9✔
961
        if (!groups)
9✔
962
                return -ENOMEM;
963

964
        *ret_gids = groups;
9✔
965
        return k;
9✔
966
}
967

968
static int enforce_groups(gid_t gid, const gid_t *supplementary_gids, int ngids) {
10,877✔
969
        int r;
10,877✔
970

971
        /* Handle SupplementaryGroups= if it is not empty */
972
        if (ngids > 0) {
10,877✔
973
                r = maybe_setgroups(ngids, supplementary_gids);
230✔
974
                if (r < 0)
230✔
975
                        return r;
976
        }
977

978
        if (gid_is_valid(gid)) {
10,877✔
979
                /* Then set our gids */
980
                if (setresgid(gid, gid, gid) < 0)
2,030✔
981
                        return -errno;
1✔
982
        }
983

984
        return 0;
985
}
986

987
static int set_securebits(unsigned bits, unsigned mask) {
845✔
988
        unsigned applied;
845✔
989
        int current;
845✔
990

991
        current = prctl(PR_GET_SECUREBITS);
845✔
992
        if (current < 0)
845✔
993
                return -errno;
×
994

995
        /* Clear all securebits defined in mask and set bits */
996
        applied = ((unsigned) current & ~mask) | bits;
845✔
997
        if ((unsigned) current == applied)
845✔
998
                return 0;
999

1000
        if (prctl(PR_SET_SECUREBITS, applied) < 0)
54✔
1001
                return -errno;
×
1002

1003
        return 1;
1004
}
1005

1006
static int enforce_user(
2,025✔
1007
                const ExecContext *context,
1008
                uid_t uid,
1009
                uint64_t capability_ambient_set) {
1010

1011
        int r;
2,025✔
1012

1013
        assert(context);
2,025✔
1014

1015
        if (!uid_is_valid(uid))
2,025✔
1016
                return 0;
1017

1018
        /* Sets (but doesn't look up) the UIS and makes sure we keep the capabilities while doing so. For
1019
         * setting secure bits the capability CAP_SETPCAP is required, so we also need keep-caps in this
1020
         * case. */
1021

1022
        if ((capability_ambient_set != 0 || context->secure_bits != 0) && uid != 0) {
2,025✔
1023

1024
                /* First step: If we need to keep capabilities but drop privileges we need to make sure we
1025
                 * keep our caps, while we drop privileges. Add KEEP_CAPS to the securebits */
1026
                r = set_securebits(1U << SECURE_KEEP_CAPS, 0);
845✔
1027
                if (r < 0)
845✔
1028
                        return r;
1029
        }
1030

1031
        /* Second step: actually set the uids */
1032
        if (setresuid(uid, uid, uid) < 0)
2,025✔
1033
                return -errno;
×
1034

1035
        /* At this point we should have all necessary capabilities but are otherwise a normal user. However,
1036
         * the caps might got corrupted due to the setresuid() so we need clean them up later. This is done
1037
         * outside of this call. */
1038
        return 0;
1039
}
1040

1041
#if HAVE_PAM
1042

1043
static void pam_response_free_array(struct pam_response *responses, size_t n_responses) {
×
1044
        assert(responses || n_responses == 0);
×
1045

1046
        FOREACH_ARRAY(resp, responses, n_responses)
×
1047
                erase_and_free(resp->resp);
×
1048

1049
        free(responses);
×
1050
}
×
1051

1052
typedef struct AskPasswordConvData {
1053
        const ExecContext *context;
1054
        const ExecParameters *params;
1055
} AskPasswordConvData;
1056

1057
static int ask_password_conv(
4✔
1058
                int num_msg,
1059
                const struct pam_message *msg[],
1060
                struct pam_response **ret,
1061
                void *userdata) {
1062

1063
        AskPasswordConvData *data = ASSERT_PTR(userdata);
4✔
1064
        bool set_credential_env_var = false;
4✔
1065
        int r;
4✔
1066

1067
        assert(num_msg >= 0);
4✔
1068
        assert(msg);
4✔
1069
        assert(data->context);
4✔
1070
        assert(data->params);
4✔
1071

1072
        size_t n = num_msg;
4✔
1073
        struct pam_response *responses = new0(struct pam_response, n);
4✔
1074
        if (!responses)
4✔
1075
                return PAM_BUF_ERR;
4✔
1076
        CLEANUP_ARRAY(responses, n, pam_response_free_array);
4✔
1077

1078
        for (size_t i = 0; i < n; i++) {
8✔
1079
                const struct pam_message *mi = *msg + i;
4✔
1080

1081
                switch (mi->msg_style) {
4✔
1082

1083
                case PAM_PROMPT_ECHO_ON:
1✔
1084
                case PAM_PROMPT_ECHO_OFF: {
1085

1086
                        /* Locally set the $CREDENTIALS_DIRECTORY to the credentials directory we just populated */
1087
                        if (!set_credential_env_var) {
1✔
1088
                                _cleanup_free_ char *creds_dir = NULL;
1✔
1089
                                r = exec_context_get_credential_directory(data->context, data->params, data->params->unit_id, &creds_dir);
1✔
1090
                                if (r < 0)
1✔
1091
                                        return log_exec_error_errno(data->context, data->params, r, "Failed to determine credentials directory: %m");
×
1092

1093
                                if (creds_dir) {
1✔
1094
                                        if (setenv("CREDENTIALS_DIRECTORY", creds_dir, /* overwrite= */ true) < 0)
1✔
1095
                                                return log_exec_error_errno(data->context, data->params, r, "Failed to set $CREDENTIALS_DIRECTORY: %m");
×
1096
                                } else
1097
                                        (void) unsetenv("CREDENTIALS_DIRECTORY");
×
1098

1099
                                set_credential_env_var = true;
1✔
1100
                        }
1101

1102
                        _cleanup_free_ char *credential_name = strjoin("pam.authtok.", data->context->pam_name);
2✔
1103
                        if (!credential_name)
1✔
1104
                                return log_oom();
×
1105

1106
                        AskPasswordRequest req = {
2✔
1107
                                .message = mi->msg,
1✔
1108
                                .credential = credential_name,
1109
                                .tty_fd = -EBADF,
1110
                                .hup_fd = -EBADF,
1111
                                .until = usec_add(now(CLOCK_MONOTONIC), 15 * USEC_PER_SEC),
1✔
1112
                        };
1113

1114
                        _cleanup_strv_free_erase_ char **acquired = NULL;
×
1115
                        r = ask_password_auto(
1✔
1116
                                        &req,
1117
                                        ASK_PASSWORD_ACCEPT_CACHED|
1118
                                        ASK_PASSWORD_NO_TTY|
1119
                                        (mi->msg_style == PAM_PROMPT_ECHO_ON ? ASK_PASSWORD_ECHO : 0),
1✔
1120
                                        &acquired);
1121
                        if (r < 0) {
1✔
1122
                                log_exec_error_errno(data->context, data->params, r, "Failed to query for password: %m");
×
1123
                                return PAM_CONV_ERR;
×
1124
                        }
1125

1126
                        responses[i].resp = strdup(ASSERT_PTR(acquired[0]));
1✔
1127
                        if (!responses[i].resp) {
1✔
1128
                                log_oom();
×
1129
                                return PAM_BUF_ERR;
1130
                        }
1131
                        break;
1✔
1132
                }
1133

1134
                case PAM_ERROR_MSG:
×
1135
                        log_exec_error(data->context, data->params, "PAM: %s", mi->msg);
×
1136
                        break;
×
1137

1138
                case PAM_TEXT_INFO:
3✔
1139
                        log_exec_info(data->context, data->params, "PAM: %s", mi->msg);
9✔
1140
                        break;
3✔
1141

1142
                default:
1143
                        return PAM_CONV_ERR;
1144
                }
1145
        }
1146

1147
        *ret = TAKE_PTR(responses);
4✔
1148
        n = 0;
4✔
1149

1150
        return PAM_SUCCESS;
4✔
1151
}
1152

1153
static int pam_close_session_and_delete_credentials(pam_handle_t *handle, int flags) {
178✔
1154
        int r, s;
178✔
1155

1156
        assert(handle);
178✔
1157

1158
        r = pam_close_session(handle, flags);
178✔
1159
        if (r != PAM_SUCCESS)
178✔
1160
                log_debug("pam_close_session() failed: %s", pam_strerror(handle, r));
39✔
1161

1162
        s = pam_setcred(handle, PAM_DELETE_CRED | flags);
178✔
1163
        if (s != PAM_SUCCESS)
178✔
1164
                log_debug("pam_setcred(PAM_DELETE_CRED) failed: %s", pam_strerror(handle, s));
123✔
1165

1166
        return r != PAM_SUCCESS ? r : s;
178✔
1167
}
1168
#endif
1169

1170
static int setup_pam(
320✔
1171
                const ExecContext *context,
1172
                ExecParameters *params,
1173
                const char *user,
1174
                uid_t uid,
1175
                gid_t gid,
1176
                char ***env, /* updated on success */
1177
                const int fds[], size_t n_fds,
1178
                int exec_fd) {
1179

1180
#if HAVE_PAM
1181
        AskPasswordConvData conv_data = {
320✔
1182
                .context = context,
1183
                .params = params,
1184
        };
1185

1186
        const struct pam_conv conv = {
320✔
1187
                .conv = ask_password_conv,
1188
                .appdata_ptr = &conv_data,
1189
        };
1190

1191
        _cleanup_(barrier_destroy) Barrier barrier = BARRIER_NULL;
320✔
1192
        _cleanup_strv_free_ char **e = NULL;
320✔
1193
        pam_handle_t *handle = NULL;
320✔
1194
        sigset_t old_ss;
320✔
1195
        int pam_code = PAM_SUCCESS, r;
320✔
1196
        bool close_session = false;
320✔
1197
        pid_t parent_pid;
320✔
1198
        int flags = 0;
320✔
1199

1200
        assert(context);
320✔
1201
        assert(params);
320✔
1202
        assert(user);
320✔
1203
        assert(uid_is_valid(uid));
320✔
1204
        assert(gid_is_valid(gid));
320✔
1205
        assert(fds || n_fds == 0);
320✔
1206
        assert(env);
320✔
1207

1208
        /* We set up PAM in the parent process, then fork. The child
1209
         * will then stay around until killed via PR_GET_PDEATHSIG or
1210
         * systemd via the cgroup logic. It will then remove the PAM
1211
         * session again. The parent process will exec() the actual
1212
         * daemon. We do things this way to ensure that the main PID
1213
         * of the daemon is the one we initially fork()ed. */
1214

1215
        r = barrier_create(&barrier);
320✔
1216
        if (r < 0)
320✔
1217
                goto fail;
×
1218

1219
        if (log_get_max_level() < LOG_DEBUG)
320✔
1220
                flags |= PAM_SILENT;
2✔
1221

1222
        pam_code = pam_start(context->pam_name, user, &conv, &handle);
320✔
1223
        if (pam_code != PAM_SUCCESS) {
320✔
1224
                handle = NULL;
×
1225
                goto fail;
×
1226
        }
1227

1228
        const char *tty = context->tty_path;
320✔
1229
        if (!tty) {
320✔
1230
                _cleanup_free_ char *q = NULL;
×
1231

1232
                /* Hmm, so no TTY was explicitly passed, but an fd passed to us directly might be a TTY. Let's figure
1233
                 * out if that's the case, and read the TTY off it. */
1234

1235
                if (getttyname_malloc(STDIN_FILENO, &q) >= 0)
313✔
1236
                        tty = strjoina("/dev/", q);
×
1237
        }
1238

1239
        if (tty) {
313✔
1240
                pam_code = pam_set_item(handle, PAM_TTY, tty);
7✔
1241
                if (pam_code != PAM_SUCCESS)
7✔
1242
                        goto fail;
×
1243
        }
1244

1245
        STRV_FOREACH(nv, *env) {
4,551✔
1246
                pam_code = pam_putenv(handle, *nv);
4,231✔
1247
                if (pam_code != PAM_SUCCESS)
4,231✔
1248
                        goto fail;
×
1249
        }
1250

1251
        pam_code = pam_acct_mgmt(handle, flags);
320✔
1252
        if (pam_code != PAM_SUCCESS)
320✔
1253
                goto fail;
×
1254

1255
        pam_code = pam_setcred(handle, PAM_ESTABLISH_CRED | flags);
320✔
1256
        if (pam_code != PAM_SUCCESS)
320✔
1257
                log_debug("pam_setcred(PAM_ESTABLISH_CRED) failed, ignoring: %s", pam_strerror(handle, pam_code));
260✔
1258

1259
        pam_code = pam_open_session(handle, flags);
320✔
1260
        if (pam_code != PAM_SUCCESS)
320✔
1261
                goto fail;
×
1262

1263
        close_session = true;
320✔
1264

1265
        e = pam_getenvlist(handle);
320✔
1266
        if (!e) {
320✔
1267
                pam_code = PAM_BUF_ERR;
×
1268
                goto fail;
×
1269
        }
1270

1271
        /* Block SIGTERM, so that we know that it won't get lost in the child */
1272

1273
        assert_se(sigprocmask_many(SIG_BLOCK, &old_ss, SIGTERM) >= 0);
320✔
1274

1275
        parent_pid = getpid_cached();
320✔
1276

1277
        r = safe_fork("(sd-pam)", 0, NULL);
320✔
1278
        if (r < 0)
499✔
1279
                goto fail;
×
1280
        if (r == 0) {
499✔
1281
                int ret = EXIT_PAM;
179✔
1282

1283
                /* The child's job is to reset the PAM session on termination */
1284
                barrier_set_role(&barrier, BARRIER_CHILD);
179✔
1285

1286
                /* Make sure we don't keep open the passed fds in this child. We assume that otherwise only
1287
                 * those fds are open here that have been opened by PAM. */
1288
                (void) close_many(fds, n_fds);
179✔
1289

1290
                /* Also close the 'exec_fd' in the child, since the service manager waits for the EOF induced
1291
                 * by the execve() to wait for completion, and if we'd keep the fd open here in the child
1292
                 * we'd never signal completion. */
1293
                exec_fd = safe_close(exec_fd);
179✔
1294

1295
                /* Drop privileges - we don't need any to pam_close_session and this will make
1296
                 * PR_SET_PDEATHSIG work in most cases.  If this fails, ignore the error - but expect sd-pam
1297
                 * threads to fail to exit normally */
1298

1299
                r = fully_set_uid_gid(uid, gid, /* supplementary_gids= */ NULL, /* n_supplementary_gids= */ 0);
179✔
1300
                if (r < 0)
179✔
1301
                        log_warning_errno(r, "Failed to drop privileges in sd-pam: %m");
×
1302

1303
                (void) ignore_signals(SIGPIPE);
179✔
1304

1305
                /* Wait until our parent died. This will only work if the above setresuid() succeeds,
1306
                 * otherwise the kernel will not allow unprivileged parents kill their privileged children
1307
                 * this way. We rely on the control groups kill logic to do the rest for us. */
1308
                if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
179✔
1309
                        goto child_finish;
×
1310

1311
                /* Tell the parent that our setup is done. This is especially important regarding dropping
1312
                 * privileges. Otherwise, unit setup might race against our setresuid(2) call.
1313
                 *
1314
                 * If the parent aborted, we'll detect this below, hence ignore return failure here. */
1315
                (void) barrier_place(&barrier);
179✔
1316

1317
                /* Check if our parent process might already have died? */
1318
                if (getppid() == parent_pid) {
179✔
1319
                        sigset_t ss;
179✔
1320
                        int sig;
179✔
1321

1322
                        assert_se(sigemptyset(&ss) >= 0);
179✔
1323
                        assert_se(sigaddset(&ss, SIGTERM) >= 0);
179✔
1324

1325
                        assert_se(sigwait(&ss, &sig) == 0);
179✔
1326
                        assert(sig == SIGTERM);
179✔
1327
                }
1328

1329
                /* If our parent died we'll end the session */
1330
                if (getppid() != parent_pid) {
179✔
1331
                        pam_code = pam_close_session_and_delete_credentials(handle, flags);
178✔
1332
                        if (pam_code != PAM_SUCCESS)
178✔
1333
                                goto child_finish;
123✔
1334
                }
1335

1336
                ret = 0;
1337

1338
        child_finish:
179✔
1339
                /* NB: pam_end() when called in child processes should set PAM_DATA_SILENT to let the module
1340
                 * know about this. See pam_end(3) */
1341
                (void) pam_end(handle, pam_code | flags | PAM_DATA_SILENT);
179✔
1342
                _exit(ret);
179✔
1343
        }
1344

1345
        barrier_set_role(&barrier, BARRIER_PARENT);
320✔
1346

1347
        /* If the child was forked off successfully it will do all the cleanups, so forget about the handle
1348
         * here. */
1349
        handle = NULL;
320✔
1350

1351
        /* Unblock SIGTERM again in the parent */
1352
        assert_se(sigprocmask(SIG_SETMASK, &old_ss, NULL) >= 0);
320✔
1353

1354
        /* We close the log explicitly here, since the PAM modules might have opened it, but we don't want
1355
         * this fd around. */
1356
        closelog();
320✔
1357

1358
        /* Synchronously wait for the child to initialize. We don't care for errors as we cannot
1359
         * recover. However, warn loudly if it happens. */
1360
        if (!barrier_place_and_sync(&barrier))
640✔
1361
                log_error("PAM initialization failed");
×
1362

1363
        return strv_free_and_replace(*env, e);
320✔
1364

1365
fail:
×
1366
        if (pam_code != PAM_SUCCESS) {
×
1367
                log_error("PAM failed: %s", pam_strerror(handle, pam_code));
×
1368
                r = -EPERM;  /* PAM errors do not map to errno */
1369
        } else
1370
                log_error_errno(r, "PAM failed: %m");
×
1371

1372
        if (handle) {
×
1373
                if (close_session)
×
1374
                        pam_code = pam_close_session_and_delete_credentials(handle, flags);
×
1375

1376
                (void) pam_end(handle, pam_code | flags);
×
1377
        }
1378

1379
        closelog();
×
1380
        return r;
1381
#else
1382
        return 0;
1383
#endif
1384
}
1385

1386
static void rename_process_from_path(const char *path) {
13,308✔
1387
        _cleanup_free_ char *buf = NULL;
13,308✔
1388
        const char *p;
13,308✔
1389

1390
        assert(path);
13,308✔
1391

1392
        /* This resulting string must fit in 10 chars (i.e. the length of "/sbin/init") to look pretty in
1393
         * /bin/ps */
1394

1395
        if (path_extract_filename(path, &buf) < 0) {
13,308✔
1396
                rename_process("(...)");
×
1397
                return;
×
1398
        }
1399

1400
        size_t l = strlen(buf);
13,308✔
1401
        if (l > 8) {
13,308✔
1402
                /* The end of the process name is usually more interesting, since the first bit might just be
1403
                 * "systemd-" */
1404
                p = buf + l - 8;
9,292✔
1405
                l = 8;
9,292✔
1406
        } else
1407
                p = buf;
1408

1409
        char process_name[11];
13,308✔
1410
        process_name[0] = '(';
13,308✔
1411
        memcpy(process_name+1, p, l);
13,308✔
1412
        process_name[1+l] = ')';
13,308✔
1413
        process_name[1+l+1] = 0;
13,308✔
1414

1415
        (void) rename_process(process_name);
13,308✔
1416
}
1417

1418
static bool context_has_address_families(const ExecContext *c) {
13,632✔
1419
        assert(c);
13,632✔
1420

1421
        return c->address_families_allow_list ||
13,632✔
1422
                !set_isempty(c->address_families);
11,820✔
1423
}
1424

1425
static bool context_has_syscall_filters(const ExecContext *c) {
13,596✔
1426
        assert(c);
13,596✔
1427

1428
        return c->syscall_allow_list ||
13,596✔
1429
                !hashmap_isempty(c->syscall_filter);
11,799✔
1430
}
1431

1432
static bool context_has_syscall_logs(const ExecContext *c) {
13,596✔
1433
        assert(c);
13,596✔
1434

1435
        return c->syscall_log_allow_list ||
13,596✔
1436
                !hashmap_isempty(c->syscall_log);
13,596✔
1437
}
1438

1439
static bool context_has_seccomp(const ExecContext *c) {
3,574✔
1440
        assert(c);
3,574✔
1441

1442
        /* We need NNP if we have any form of seccomp and are unprivileged */
1443
        return c->lock_personality ||
3,574✔
1444
                c->memory_deny_write_execute ||
2,758✔
1445
                c->private_devices ||
2,758✔
1446
                c->protect_clock ||
2,758✔
1447
                c->protect_hostname == PROTECT_HOSTNAME_YES ||
2,758✔
1448
                c->protect_kernel_tunables ||
1449
                c->protect_kernel_modules ||
2,758✔
1450
                c->protect_kernel_logs ||
2,758✔
1451
                context_has_address_families(c) ||
5,516✔
1452
                exec_context_restrict_namespaces_set(c) ||
2,758✔
1453
                c->restrict_realtime ||
2,758✔
1454
                c->restrict_suid_sgid ||
2,722✔
1455
                !set_isempty(c->syscall_archs) ||
5,444✔
1456
                context_has_syscall_filters(c) ||
9,018✔
1457
                context_has_syscall_logs(c);
2,722✔
1458
}
1459

1460
static bool context_has_no_new_privileges(const ExecContext *c) {
10,874✔
1461
        assert(c);
10,874✔
1462

1463
        if (c->no_new_privileges)
10,874✔
1464
                return true;
1465

1466
        if (have_effective_cap(CAP_SYS_ADMIN) > 0) /* if we are privileged, we don't need NNP */
9,188✔
1467
                return false;
1468

1469
        return context_has_seccomp(c);
1,549✔
1470
}
1471

1472
#if HAVE_SECCOMP
1473

1474
static bool seccomp_allows_drop_privileges(const ExecContext *c) {
852✔
1475
        void *id, *val;
852✔
1476
        bool has_capget = false, has_capset = false, has_prctl = false;
852✔
1477

1478
        assert(c);
852✔
1479

1480
        /* No syscall filter, we are allowed to drop privileges */
1481
        if (hashmap_isempty(c->syscall_filter))
852✔
1482
                return true;
852✔
1483

1484
        HASHMAP_FOREACH_KEY(val, id, c->syscall_filter) {
311,770✔
1485
                _cleanup_free_ char *name = NULL;
310,970✔
1486

1487
                name = seccomp_syscall_resolve_num_arch(SCMP_ARCH_NATIVE, PTR_TO_INT(id) - 1);
310,970✔
1488

1489
                if (streq(name, "capget"))
310,970✔
1490
                        has_capget = true;
1491
                else if (streq(name, "capset"))
310,170✔
1492
                        has_capset = true;
1493
                else if (streq(name, "prctl"))
309,370✔
1494
                        has_prctl = true;
800✔
1495
        }
1496

1497
        if (c->syscall_allow_list)
800✔
1498
                return has_capget && has_capset && has_prctl;
800✔
1499
        else
1500
                return !(has_capget || has_capset || has_prctl);
×
1501
}
1502

1503
static bool skip_seccomp_unavailable(const ExecContext *c, const ExecParameters *p, const char *msg) {
18,157✔
1504
        assert(c);
18,157✔
1505
        assert(p);
18,157✔
1506
        assert(msg);
18,157✔
1507

1508
        if (is_seccomp_available())
18,157✔
1509
                return false;
1510

1511
        log_exec_debug(c, p, "SECCOMP features not detected in the kernel, skipping %s", msg);
×
1512
        return true;
×
1513
}
1514

1515
static int apply_syscall_filter(const ExecContext *c, const ExecParameters *p) {
10,874✔
1516
        uint32_t negative_action, default_action, action;
10,874✔
1517
        int r;
10,874✔
1518

1519
        assert(c);
10,874✔
1520
        assert(p);
10,874✔
1521

1522
        if (!context_has_syscall_filters(c))
10,874✔
1523
                return 0;
1524

1525
        if (skip_seccomp_unavailable(c, p, "SystemCallFilter="))
1,798✔
1526
                return 0;
1527

1528
        negative_action = c->syscall_errno == SECCOMP_ERROR_NUMBER_KILL ? scmp_act_kill_process() : SCMP_ACT_ERRNO(c->syscall_errno);
1,798✔
1529

1530
        if (c->syscall_allow_list) {
1,798✔
1531
                default_action = negative_action;
1532
                action = SCMP_ACT_ALLOW;
1533
        } else {
1534
                default_action = SCMP_ACT_ALLOW;
1✔
1535
                action = negative_action;
1✔
1536
        }
1537

1538
        /* Sending over exec_fd or handoff_timestamp_fd requires write() syscall. */
1539
        if (p->exec_fd >= 0 || p->handoff_timestamp_fd >= 0) {
1,798✔
1540
                r = seccomp_filter_set_add_by_name(c->syscall_filter, c->syscall_allow_list, "write");
1,798✔
1541
                if (r < 0)
1,798✔
1542
                        return r;
1543
        }
1544

1545
        return seccomp_load_syscall_filter_set_raw(default_action, c->syscall_filter, action, false);
1,798✔
1546
}
1547

1548
static int apply_syscall_log(const ExecContext *c, const ExecParameters *p) {
10,874✔
1549
#ifdef SCMP_ACT_LOG
1550
        uint32_t default_action, action;
10,874✔
1551
#endif
1552

1553
        assert(c);
10,874✔
1554
        assert(p);
10,874✔
1555

1556
        if (!context_has_syscall_logs(c))
10,874✔
1557
                return 0;
1558

1559
#ifdef SCMP_ACT_LOG
1560
        if (skip_seccomp_unavailable(c, p, "SystemCallLog="))
×
1561
                return 0;
1562

1563
        if (c->syscall_log_allow_list) {
×
1564
                /* Log nothing but the ones listed */
1565
                default_action = SCMP_ACT_ALLOW;
1566
                action = SCMP_ACT_LOG;
1567
        } else {
1568
                /* Log everything but the ones listed */
1569
                default_action = SCMP_ACT_LOG;
×
1570
                action = SCMP_ACT_ALLOW;
×
1571
        }
1572

1573
        return seccomp_load_syscall_filter_set_raw(default_action, c->syscall_log, action, false);
×
1574
#else
1575
        /* old libseccomp */
1576
        log_exec_debug(c, p, "SECCOMP feature SCMP_ACT_LOG not available, skipping SystemCallLog=");
1577
        return 0;
1578
#endif
1579
}
1580

1581
static int apply_syscall_archs(const ExecContext *c, const ExecParameters *p) {
10,874✔
1582
        assert(c);
10,874✔
1583
        assert(p);
10,874✔
1584

1585
        if (set_isempty(c->syscall_archs))
10,874✔
1586
                return 0;
1587

1588
        if (skip_seccomp_unavailable(c, p, "SystemCallArchitectures="))
1,815✔
1589
                return 0;
1590

1591
        return seccomp_restrict_archs(c->syscall_archs);
1,815✔
1592
}
1593

1594
static int apply_address_families(const ExecContext *c, const ExecParameters *p) {
10,874✔
1595
        assert(c);
10,874✔
1596
        assert(p);
10,874✔
1597

1598
        if (!context_has_address_families(c))
10,874✔
1599
                return 0;
1600

1601
        if (skip_seccomp_unavailable(c, p, "RestrictAddressFamilies="))
1,812✔
1602
                return 0;
1603

1604
        return seccomp_restrict_address_families(c->address_families, c->address_families_allow_list);
1,812✔
1605
}
1606

1607
static int apply_memory_deny_write_execute(const ExecContext *c, const ExecParameters *p) {
10,874✔
1608
        int r;
10,874✔
1609

1610
        assert(c);
10,874✔
1611
        assert(p);
10,874✔
1612

1613
        if (!c->memory_deny_write_execute)
10,874✔
1614
                return 0;
1615

1616
        /* use prctl() if kernel supports it (6.3) */
1617
        r = prctl(PR_SET_MDWE, PR_MDWE_REFUSE_EXEC_GAIN, 0, 0, 0);
1,812✔
1618
        if (r == 0) {
1,812✔
1619
                log_exec_debug(c, p, "Enabled MemoryDenyWriteExecute= with PR_SET_MDWE");
5,436✔
1620
                return 0;
1,812✔
1621
        }
1622
        if (r < 0 && errno != EINVAL)
×
1623
                return log_exec_debug_errno(c,
×
1624
                                            p,
1625
                                            errno,
1626
                                            "Failed to enable MemoryDenyWriteExecute= with PR_SET_MDWE: %m");
1627
        /* else use seccomp */
1628
        log_exec_debug(c, p, "Kernel doesn't support PR_SET_MDWE: falling back to seccomp");
×
1629

1630
        if (skip_seccomp_unavailable(c, p, "MemoryDenyWriteExecute="))
×
1631
                return 0;
1632

1633
        return seccomp_memory_deny_write_execute();
×
1634
}
1635

1636
static int apply_restrict_realtime(const ExecContext *c, const ExecParameters *p) {
10,874✔
1637
        assert(c);
10,874✔
1638
        assert(p);
10,874✔
1639

1640
        if (!c->restrict_realtime)
10,874✔
1641
                return 0;
1642

1643
        if (skip_seccomp_unavailable(c, p, "RestrictRealtime="))
1,812✔
1644
                return 0;
1645

1646
        return seccomp_restrict_realtime();
1,812✔
1647
}
1648

1649
static int apply_restrict_suid_sgid(const ExecContext *c, const ExecParameters *p) {
10,874✔
1650
        assert(c);
10,874✔
1651
        assert(p);
10,874✔
1652

1653
        if (!c->restrict_suid_sgid)
10,874✔
1654
                return 0;
1655

1656
        if (skip_seccomp_unavailable(c, p, "RestrictSUIDSGID="))
1,731✔
1657
                return 0;
1658

1659
        return seccomp_restrict_suid_sgid();
1,731✔
1660
}
1661

1662
static int apply_protect_sysctl(const ExecContext *c, const ExecParameters *p) {
10,874✔
1663
        assert(c);
10,874✔
1664
        assert(p);
10,874✔
1665

1666
        /* Turn off the legacy sysctl() system call. Many distributions turn this off while building the kernel, but
1667
         * let's protect even those systems where this is left on in the kernel. */
1668

1669
        if (!c->protect_kernel_tunables)
10,874✔
1670
                return 0;
1671

1672
        if (skip_seccomp_unavailable(c, p, "ProtectKernelTunables="))
472✔
1673
                return 0;
1674

1675
        return seccomp_protect_sysctl();
472✔
1676
}
1677

1678
static int apply_protect_kernel_modules(const ExecContext *c, const ExecParameters *p) {
10,874✔
1679
        assert(c);
10,874✔
1680
        assert(p);
10,874✔
1681

1682
        /* Turn off module syscalls on ProtectKernelModules=yes */
1683

1684
        if (!c->protect_kernel_modules)
10,874✔
1685
                return 0;
1686

1687
        if (skip_seccomp_unavailable(c, p, "ProtectKernelModules="))
1,344✔
1688
                return 0;
1689

1690
        return seccomp_load_syscall_filter_set(SCMP_ACT_ALLOW, syscall_filter_sets + SYSCALL_FILTER_SET_MODULE, SCMP_ACT_ERRNO(EPERM), false);
1,344✔
1691
}
1692

1693
static int apply_protect_kernel_logs(const ExecContext *c, const ExecParameters *p) {
10,874✔
1694
        assert(c);
10,874✔
1695
        assert(p);
10,874✔
1696

1697
        if (!c->protect_kernel_logs)
10,874✔
1698
                return 0;
1699

1700
        if (skip_seccomp_unavailable(c, p, "ProtectKernelLogs="))
1,344✔
1701
                return 0;
1702

1703
        return seccomp_protect_syslog();
1,344✔
1704
}
1705

1706
static int apply_protect_clock(const ExecContext *c, const ExecParameters *p) {
10,874✔
1707
        assert(c);
10,874✔
1708
        assert(p);
10,874✔
1709

1710
        if (!c->protect_clock)
10,874✔
1711
                return 0;
1712

1713
        if (skip_seccomp_unavailable(c, p, "ProtectClock="))
943✔
1714
                return 0;
1715

1716
        return seccomp_load_syscall_filter_set(SCMP_ACT_ALLOW, syscall_filter_sets + SYSCALL_FILTER_SET_CLOCK, SCMP_ACT_ERRNO(EPERM), false);
943✔
1717
}
1718

1719
static int apply_private_devices(const ExecContext *c, const ExecParameters *p) {
10,874✔
1720
        assert(c);
10,874✔
1721
        assert(p);
10,874✔
1722

1723
        /* If PrivateDevices= is set, also turn off iopl and all @raw-io syscalls. */
1724

1725
        if (!c->private_devices)
10,874✔
1726
                return 0;
1727

1728
        if (skip_seccomp_unavailable(c, p, "PrivateDevices="))
944✔
1729
                return 0;
1730

1731
        return seccomp_load_syscall_filter_set(SCMP_ACT_ALLOW, syscall_filter_sets + SYSCALL_FILTER_SET_RAW_IO, SCMP_ACT_ERRNO(EPERM), false);
944✔
1732
}
1733

1734
static int apply_restrict_namespaces(const ExecContext *c, const ExecParameters *p) {
10,874✔
1735
        assert(c);
10,874✔
1736
        assert(p);
10,874✔
1737

1738
        if (!exec_context_restrict_namespaces_set(c))
10,874✔
1739
                return 0;
1740

1741
        if (skip_seccomp_unavailable(c, p, "RestrictNamespaces="))
1,453✔
1742
                return 0;
1743

1744
        return seccomp_restrict_namespaces(c->restrict_namespaces);
1,453✔
1745
}
1746

1747
static int apply_lock_personality(const ExecContext *c, const ExecParameters *p) {
10,874✔
1748
        unsigned long personality;
10,874✔
1749
        int r;
10,874✔
1750

1751
        assert(c);
10,874✔
1752
        assert(p);
10,874✔
1753

1754
        if (!c->lock_personality)
10,874✔
1755
                return 0;
10,874✔
1756

1757
        if (skip_seccomp_unavailable(c, p, "LockPersonality="))
1,812✔
1758
                return 0;
1759

1760
        personality = c->personality;
1,812✔
1761

1762
        /* If personality is not specified, use either PER_LINUX or PER_LINUX32 depending on what is currently set. */
1763
        if (personality == PERSONALITY_INVALID) {
1,812✔
1764

1765
                r = opinionated_personality(&personality);
1,812✔
1766
                if (r < 0)
1,812✔
1767
                        return r;
1768
        }
1769

1770
        return seccomp_lock_personality(personality);
1,812✔
1771
}
1772

1773
#endif
1774

1775
#if HAVE_LIBBPF
1776
static int apply_restrict_filesystems(const ExecContext *c, const ExecParameters *p) {
10,874✔
1777
        int r;
10,874✔
1778

1779
        assert(c);
10,874✔
1780
        assert(p);
10,874✔
1781

1782
        if (!exec_context_restrict_filesystems_set(c))
10,874✔
1783
                return 0;
1784

1785
        if (p->bpf_restrict_fs_map_fd < 0) {
×
1786
                /* LSM BPF is unsupported or lsm_bpf_setup failed */
1787
                log_exec_debug(c, p, "LSM BPF not supported, skipping RestrictFileSystems=");
×
1788
                return 0;
×
1789
        }
1790

1791
        /* We are in a new binary, so dl-open again */
1792
        r = dlopen_bpf();
×
1793
        if (r < 0)
×
1794
                return r;
1795

1796
        return bpf_restrict_fs_update(c->restrict_filesystems, p->cgroup_id, p->bpf_restrict_fs_map_fd, c->restrict_filesystems_allow_list);
×
1797
}
1798
#endif
1799

1800
static int apply_protect_hostname(const ExecContext *c, const ExecParameters *p, int *ret_exit_status) {
10,877✔
1801
        int r;
10,877✔
1802

1803
        assert(c);
10,877✔
1804
        assert(p);
10,877✔
1805
        assert(ret_exit_status);
10,877✔
1806

1807
        if (c->protect_hostname == PROTECT_HOSTNAME_NO)
10,877✔
1808
                return 0;
1809

1810
        if (ns_type_supported(NAMESPACE_UTS)) {
881✔
1811
                if (unshare(CLONE_NEWUTS) < 0) {
881✔
1812
                        if (!ERRNO_IS_NOT_SUPPORTED(errno) && !ERRNO_IS_PRIVILEGE(errno)) {
×
1813
                                *ret_exit_status = EXIT_NAMESPACE;
×
1814
                                return log_exec_error_errno(c, p, errno, "Failed to set up UTS namespacing: %m");
×
1815
                        }
1816

1817
                        log_exec_warning(c, p,
×
1818
                                         "ProtectHostname=%s is configured, but UTS namespace setup is prohibited (container manager?), ignoring namespace setup.",
1819
                                         protect_hostname_to_string(c->protect_hostname));
1820

1821
                } else if (c->private_hostname) {
881✔
1822
                        r = sethostname_idempotent(c->private_hostname);
4✔
1823
                        if (r < 0) {
4✔
1824
                                *ret_exit_status = EXIT_NAMESPACE;
×
1825
                                return log_exec_error_errno(c, p, r, "Failed to set private hostname '%s': %m", c->private_hostname);
×
1826
                        }
1827
                }
1828
        } else
1829
                log_exec_warning(c, p,
×
1830
                                 "ProtectHostname=%s is configured, but the kernel does not support UTS namespaces, ignoring namespace setup.",
1831
                                 protect_hostname_to_string(c->protect_hostname));
1832

1833
#if HAVE_SECCOMP
1834
        if (c->protect_hostname == PROTECT_HOSTNAME_YES) {
881✔
1835
                if (skip_seccomp_unavailable(c, p, "ProtectHostname="))
877✔
1836
                        return 0;
1837

1838
                r = seccomp_protect_hostname();
877✔
1839
                if (r < 0) {
877✔
1840
                        *ret_exit_status = EXIT_SECCOMP;
×
1841
                        return log_exec_error_errno(c, p, r, "Failed to apply hostname restrictions: %m");
×
1842
                }
1843
        }
1844
#endif
1845

1846
        return 0;
1847
}
1848

1849
static void do_idle_pipe_dance(int idle_pipe[static 4]) {
154✔
1850
        assert(idle_pipe);
154✔
1851

1852
        idle_pipe[1] = safe_close(idle_pipe[1]);
154✔
1853
        idle_pipe[2] = safe_close(idle_pipe[2]);
154✔
1854

1855
        if (idle_pipe[0] >= 0) {
154✔
1856
                int r;
154✔
1857

1858
                r = fd_wait_for_event(idle_pipe[0], POLLHUP, IDLE_TIMEOUT_USEC);
154✔
1859

1860
                if (idle_pipe[3] >= 0 && r == 0 /* timeout */) {
154✔
1861
                        ssize_t n;
110✔
1862

1863
                        /* Signal systemd that we are bored and want to continue. */
1864
                        n = write(idle_pipe[3], "x", 1);
110✔
1865
                        if (n > 0)
110✔
1866
                                /* Wait for systemd to react to the signal above. */
1867
                                (void) fd_wait_for_event(idle_pipe[0], POLLHUP, IDLE_TIMEOUT2_USEC);
110✔
1868
                }
1869

1870
                idle_pipe[0] = safe_close(idle_pipe[0]);
154✔
1871

1872
        }
1873

1874
        idle_pipe[3] = safe_close(idle_pipe[3]);
154✔
1875
}
154✔
1876

1877
static const char *exec_directory_env_name_to_string(ExecDirectoryType t);
1878

1879
/* And this table also maps ExecDirectoryType, to the environment variable we pass the selected directory to
1880
 * the service payload in. */
1881
static const char* const exec_directory_env_name_table[_EXEC_DIRECTORY_TYPE_MAX] = {
1882
        [EXEC_DIRECTORY_RUNTIME]       = "RUNTIME_DIRECTORY",
1883
        [EXEC_DIRECTORY_STATE]         = "STATE_DIRECTORY",
1884
        [EXEC_DIRECTORY_CACHE]         = "CACHE_DIRECTORY",
1885
        [EXEC_DIRECTORY_LOGS]          = "LOGS_DIRECTORY",
1886
        [EXEC_DIRECTORY_CONFIGURATION] = "CONFIGURATION_DIRECTORY",
1887
};
1888

1889
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(exec_directory_env_name, ExecDirectoryType);
2,700✔
1890

1891
static int build_environment(
10,896✔
1892
                const ExecContext *c,
1893
                const ExecParameters *p,
1894
                const CGroupContext *cgroup_context,
1895
                size_t n_fds,
1896
                const char *home,
1897
                const char *username,
1898
                const char *shell,
1899
                dev_t journal_stream_dev,
1900
                ino_t journal_stream_ino,
1901
                const char *memory_pressure_path,
1902
                bool needs_sandboxing,
1903
                char ***ret) {
1904

1905
        _cleanup_strv_free_ char **our_env = NULL;
10,896✔
1906
        size_t n_env = 0;
10,896✔
1907
        char *x;
10,896✔
1908
        int r;
10,896✔
1909

1910
        assert(c);
10,896✔
1911
        assert(p);
10,896✔
1912
        assert(cgroup_context);
10,896✔
1913
        assert(ret);
10,896✔
1914

1915
#define N_ENV_VARS 21
1916
        our_env = new0(char*, N_ENV_VARS + _EXEC_DIRECTORY_TYPE_MAX);
10,896✔
1917
        if (!our_env)
10,896✔
1918
                return -ENOMEM;
1919

1920
        if (n_fds > 0) {
10,896✔
1921
                _cleanup_free_ char *joined = NULL;
1,590✔
1922

1923
                if (asprintf(&x, "LISTEN_PID="PID_FMT, getpid_cached()) < 0)
1,590✔
1924
                        return -ENOMEM;
1925
                our_env[n_env++] = x;
1,590✔
1926

1927
                if (asprintf(&x, "LISTEN_FDS=%zu", n_fds) < 0)
1,590✔
1928
                        return -ENOMEM;
1929
                our_env[n_env++] = x;
1,590✔
1930

1931
                joined = strv_join(p->fd_names, ":");
1,590✔
1932
                if (!joined)
1,590✔
1933
                        return -ENOMEM;
1934

1935
                x = strjoin("LISTEN_FDNAMES=", joined);
1,590✔
1936
                if (!x)
1,590✔
1937
                        return -ENOMEM;
1938
                our_env[n_env++] = x;
1,590✔
1939
        }
1940

1941
        if ((p->flags & EXEC_SET_WATCHDOG) && p->watchdog_usec > 0) {
10,896✔
1942
                if (asprintf(&x, "WATCHDOG_PID="PID_FMT, getpid_cached()) < 0)
1,805✔
1943
                        return -ENOMEM;
1944
                our_env[n_env++] = x;
1,805✔
1945

1946
                if (asprintf(&x, "WATCHDOG_USEC="USEC_FMT, p->watchdog_usec) < 0)
1,805✔
1947
                        return -ENOMEM;
1948
                our_env[n_env++] = x;
1,805✔
1949
        }
1950

1951
        /* If this is D-Bus, tell the nss-systemd module, since it relies on being able to use blocking
1952
         * Varlink calls back to us for look up dynamic users in PID 1. Break the deadlock between D-Bus and
1953
         * PID 1 by disabling use of PID1' NSS interface for looking up dynamic users. */
1954
        if (p->flags & EXEC_NSS_DYNAMIC_BYPASS) {
10,896✔
1955
                x = strdup("SYSTEMD_NSS_DYNAMIC_BYPASS=1");
175✔
1956
                if (!x)
175✔
1957
                        return -ENOMEM;
1958
                our_env[n_env++] = x;
175✔
1959
        }
1960

1961
        /* We query "root" if this is a system unit and User= is not specified. $USER is always set. $HOME
1962
         * could cause problem for e.g. getty, since login doesn't override $HOME, and $LOGNAME and $SHELL don't
1963
         * really make much sense since we're not logged in. Hence we conditionalize the three based on
1964
         * SetLoginEnvironment= switch. */
1965
        if (!c->user && !c->dynamic_user && p->runtime_scope == RUNTIME_SCOPE_SYSTEM) {
10,896✔
1966
                r = get_fixed_user("root", &username, NULL, NULL, &home, &shell);
8,291✔
1967
                if (r < 0)
8,291✔
1968
                        return log_exec_debug_errno(c,
×
1969
                                                    p,
1970
                                                    r,
1971
                                                    "Failed to determine user credentials for root: %m");
1972
        }
1973

1974
        bool set_user_login_env = exec_context_get_set_login_environment(c);
10,896✔
1975

1976
        if (username) {
10,896✔
1977
                x = strjoin("USER=", username);
10,258✔
1978
                if (!x)
10,258✔
1979
                        return -ENOMEM;
1980
                our_env[n_env++] = x;
10,258✔
1981

1982
                if (set_user_login_env) {
10,258✔
1983
                        x = strjoin("LOGNAME=", username);
2,026✔
1984
                        if (!x)
2,026✔
1985
                                return -ENOMEM;
1986
                        our_env[n_env++] = x;
2,026✔
1987
                }
1988
        }
1989

1990
        /* Note that we don't set $HOME or $SHELL if they are not particularly enlightening anyway
1991
         * (i.e. are "/" or "/bin/nologin"). */
1992

1993
        if (home && set_user_login_env && !empty_or_root(home)) {
10,896✔
1994
                x = strjoin("HOME=", home);
332✔
1995
                if (!x)
332✔
1996
                        return -ENOMEM;
1997

1998
                path_simplify(x + 5);
332✔
1999
                our_env[n_env++] = x;
332✔
2000
        }
2001

2002
        if (shell && set_user_login_env && !shell_is_placeholder(shell)) {
10,896✔
2003
                x = strjoin("SHELL=", shell);
333✔
2004
                if (!x)
333✔
2005
                        return -ENOMEM;
2006

2007
                path_simplify(x + 6);
333✔
2008
                our_env[n_env++] = x;
333✔
2009
        }
2010

2011
        if (!sd_id128_is_null(p->invocation_id)) {
10,896✔
2012
                assert(p->invocation_id_string);
10,896✔
2013

2014
                x = strjoin("INVOCATION_ID=", p->invocation_id_string);
10,896✔
2015
                if (!x)
10,896✔
2016
                        return -ENOMEM;
2017

2018
                our_env[n_env++] = x;
10,896✔
2019
        }
2020

2021
        if (exec_context_needs_term(c)) {
10,896✔
2022
                _cleanup_free_ char *cmdline = NULL;
450✔
2023
                const char *tty_path, *term = NULL;
450✔
2024

2025
                tty_path = exec_context_tty_path(c);
450✔
2026

2027
                /* If we are forked off PID 1 and we are supposed to operate on /dev/console, then let's try
2028
                 * to inherit the $TERM set for PID 1. This is useful for containers so that the $TERM the
2029
                 * container manager passes to PID 1 ends up all the way in the console login shown. */
2030

2031
                if (path_equal(tty_path, "/dev/console") && getppid() == 1)
450✔
2032
                        term = getenv("TERM");
391✔
2033
                else if (tty_path && in_charset(skip_dev_prefix(tty_path), ALPHANUMERICAL)) {
59✔
2034
                        _cleanup_free_ char *key = NULL;
43✔
2035

2036
                        key = strjoin("systemd.tty.term.", skip_dev_prefix(tty_path));
43✔
2037
                        if (!key)
43✔
2038
                                return -ENOMEM;
×
2039

2040
                        r = proc_cmdline_get_key(key, 0, &cmdline);
43✔
2041
                        if (r < 0)
43✔
2042
                                log_exec_debug_errno(c,
×
2043
                                                     p,
2044
                                                     r,
2045
                                                     "Failed to read %s from kernel cmdline, ignoring: %m",
2046
                                                     key);
2047
                        else if (r > 0)
43✔
2048
                                term = cmdline;
×
2049
                }
2050

2051
                if (!term) {
434✔
2052
                        /* If no precise $TERM is known and we pick a fallback default, then let's also set
2053
                         * $COLORTERM=truecolor. That's because our fallback default is vt220, which is
2054
                         * generally a safe bet (as it supports PageUp/PageDown unlike vt100, and is quite
2055
                         * universally available in terminfo/termcap), except for the fact that real DEC
2056
                         * vt220 gear never actually supported color. Most tools these days generate color on
2057
                         * vt220 anyway, ignoring the physical capabilities of the real hardware, but some
2058
                         * tools actually believe in the historical truth. Which is unfortunate since *we*
2059
                         * *don't* care about the historical truth, we just want sane defaults if nothing
2060
                         * better is explicitly configured. It's 2025 after all, at the time of writing,
2061
                         * pretty much all terminal emulators actually *do* support color, hence if we don't
2062
                         * know any better let's explicitly claim color support via $COLORTERM. Or in other
2063
                         * words: we now explicitly claim to be connected to a franken-vt220 with true color
2064
                         * support. */
2065
                        x = strdup("COLORTERM=truecolor");
59✔
2066
                        if (!x)
59✔
2067
                                return -ENOMEM;
2068

2069
                        our_env[n_env++] = x;
59✔
2070

2071
                        term = default_term_for_tty(tty_path);
59✔
2072
                }
2073

2074
                x = strjoin("TERM=", term);
450✔
2075
                if (!x)
450✔
2076
                        return -ENOMEM;
2077
                our_env[n_env++] = x;
450✔
2078
        }
2079

2080
        if (journal_stream_dev != 0 && journal_stream_ino != 0) {
10,896✔
2081
                if (asprintf(&x, "JOURNAL_STREAM=" DEV_FMT ":" INO_FMT, journal_stream_dev, journal_stream_ino) < 0)
10,117✔
2082
                        return -ENOMEM;
2083

2084
                our_env[n_env++] = x;
10,117✔
2085
        }
2086

2087
        if (c->log_namespace) {
10,896✔
2088
                x = strjoin("LOG_NAMESPACE=", c->log_namespace);
2✔
2089
                if (!x)
2✔
2090
                        return -ENOMEM;
2091

2092
                our_env[n_env++] = x;
2✔
2093
        }
2094

2095
        for (ExecDirectoryType t = 0; t < _EXEC_DIRECTORY_TYPE_MAX; t++) {
65,376✔
2096
                _cleanup_free_ char *joined = NULL;
54,480✔
2097
                const char *n;
54,480✔
2098

2099
                if (!p->prefix[t])
54,480✔
UNCOV
2100
                        continue;
×
2101

2102
                if (c->directories[t].n_items == 0)
54,480✔
2103
                        continue;
51,780✔
2104

2105
                n = exec_directory_env_name_to_string(t);
2,700✔
2106
                if (!n)
2,700✔
UNCOV
2107
                        continue;
×
2108

2109
                for (size_t i = 0; i < c->directories[t].n_items; i++) {
5,896✔
2110
                        _cleanup_free_ char *prefixed = NULL;
3,196✔
2111

2112
                        prefixed = path_join(p->prefix[t], c->directories[t].items[i].path);
3,196✔
2113
                        if (!prefixed)
3,196✔
2114
                                return -ENOMEM;
2115

2116
                        if (!strextend_with_separator(&joined, ":", prefixed))
3,196✔
2117
                                return -ENOMEM;
2118
                }
2119

2120
                x = strjoin(n, "=", joined);
2,700✔
2121
                if (!x)
2,700✔
2122
                        return -ENOMEM;
2123

2124
                our_env[n_env++] = x;
2,700✔
2125
        }
2126

2127
        _cleanup_free_ char *creds_dir = NULL;
10,896✔
2128
        r = exec_context_get_credential_directory(c, p, p->unit_id, &creds_dir);
10,896✔
2129
        if (r < 0)
10,896✔
2130
                return r;
2131
        if (r > 0) {
10,896✔
2132
                x = strjoin("CREDENTIALS_DIRECTORY=", creds_dir);
2,399✔
2133
                if (!x)
2,399✔
2134
                        return -ENOMEM;
2135

2136
                our_env[n_env++] = x;
2,399✔
2137
        }
2138

2139
        if (asprintf(&x, "SYSTEMD_EXEC_PID=" PID_FMT, getpid_cached()) < 0)
10,896✔
2140
                return -ENOMEM;
2141

2142
        our_env[n_env++] = x;
10,896✔
2143

2144
        if (memory_pressure_path) {
10,896✔
2145
                x = strjoin("MEMORY_PRESSURE_WATCH=", memory_pressure_path);
10,540✔
2146
                if (!x)
10,540✔
2147
                        return -ENOMEM;
2148

2149
                our_env[n_env++] = x;
10,540✔
2150

2151
                if (!path_equal(memory_pressure_path, "/dev/null")) {
10,540✔
2152
                        _cleanup_free_ char *b = NULL, *e = NULL;
10,540✔
2153

2154
                        if (asprintf(&b, "%s " USEC_FMT " " USEC_FMT,
10,540✔
2155
                                     MEMORY_PRESSURE_DEFAULT_TYPE,
2156
                                     cgroup_context->memory_pressure_threshold_usec == USEC_INFINITY ? MEMORY_PRESSURE_DEFAULT_THRESHOLD_USEC :
10,540✔
2157
                                     CLAMP(cgroup_context->memory_pressure_threshold_usec, 1U, MEMORY_PRESSURE_DEFAULT_WINDOW_USEC),
10,540✔
2158
                                     MEMORY_PRESSURE_DEFAULT_WINDOW_USEC) < 0)
2159
                                return -ENOMEM;
2160

2161
                        if (base64mem(b, strlen(b) + 1, &e) < 0)
10,540✔
2162
                                return -ENOMEM;
2163

2164
                        x = strjoin("MEMORY_PRESSURE_WRITE=", e);
10,540✔
2165
                        if (!x)
10,540✔
2166
                                return -ENOMEM;
2167

2168
                        our_env[n_env++] = x;
10,540✔
2169
                }
2170
        }
2171

2172
        if (p->notify_socket) {
10,896✔
2173
                x = strjoin("NOTIFY_SOCKET=", exec_get_private_notify_socket_path(c, p, needs_sandboxing) ?: p->notify_socket);
2,216✔
2174
                if (!x)
2,216✔
2175
                        return -ENOMEM;
2176

2177
                our_env[n_env++] = x;
2,216✔
2178
        }
2179

2180
        assert(n_env < N_ENV_VARS + _EXEC_DIRECTORY_TYPE_MAX);
10,896✔
2181
#undef N_ENV_VARS
2182

2183
        *ret = TAKE_PTR(our_env);
10,896✔
2184

2185
        return 0;
10,896✔
2186
}
2187

2188
static int build_pass_environment(const ExecContext *c, char ***ret) {
10,896✔
2189
        _cleanup_strv_free_ char **pass_env = NULL;
10,896✔
2190
        size_t n_env = 0;
10,896✔
2191

2192
        assert(c);
10,896✔
2193
        assert(ret);
10,896✔
2194

2195
        STRV_FOREACH(i, c->pass_environment) {
11,216✔
UNCOV
2196
                _cleanup_free_ char *x = NULL;
×
2197
                char *v;
320✔
2198

2199
                v = getenv(*i);
320✔
2200
                if (!v)
320✔
UNCOV
2201
                        continue;
×
2202
                x = strjoin(*i, "=", v);
320✔
2203
                if (!x)
320✔
2204
                        return -ENOMEM;
2205

2206
                if (!GREEDY_REALLOC(pass_env, n_env + 2))
320✔
2207
                        return -ENOMEM;
2208

2209
                pass_env[n_env++] = TAKE_PTR(x);
320✔
2210
                pass_env[n_env] = NULL;
320✔
2211
        }
2212

2213
        *ret = TAKE_PTR(pass_env);
10,896✔
2214
        return 0;
10,896✔
2215
}
2216

2217
static int setup_private_users(PrivateUsers private_users, uid_t ouid, gid_t ogid, uid_t uid, gid_t gid, bool allow_setgroups) {
10,880✔
2218
        _cleanup_free_ char *uid_map = NULL, *gid_map = NULL;
10,880✔
2219
        _cleanup_close_pair_ int errno_pipe[2] = EBADF_PAIR;
10,880✔
2220
        _cleanup_close_ int unshare_ready_fd = -EBADF;
10,880✔
2221
        _cleanup_(sigkill_waitp) pid_t pid = 0;
10,880✔
2222
        uint64_t c = 1;
10,880✔
2223
        ssize_t n;
10,880✔
2224
        int r;
10,880✔
2225

2226
        /* Set up a user namespace and map the original UID/GID (IDs from before any user or group changes, i.e.
2227
         * the IDs from the user or system manager(s)) to itself, the selected UID/GID to itself, and everything else to
2228
         * nobody. In order to be able to write this mapping we need CAP_SETUID in the original user namespace, which
2229
         * we however lack after opening the user namespace. To work around this we fork() a temporary child process,
2230
         * which waits for the parent to create the new user namespace while staying in the original namespace. The
2231
         * child then writes the UID mapping, under full privileges. The parent waits for the child to finish and
2232
         * continues execution normally.
2233
         * For unprivileged users (i.e. without capabilities), the root to root mapping is excluded. As such, it
2234
         * does not need CAP_SETUID to write the single line mapping to itself. */
2235

2236
        if (private_users == PRIVATE_USERS_NO)
10,880✔
2237
                return 0;
2238

2239
        if (private_users == PRIVATE_USERS_IDENTITY) {
35✔
2240
                uid_map = strdup("0 0 65536\n");
2✔
2241
                if (!uid_map)
2✔
2242
                        return -ENOMEM;
2243
        } else if (private_users == PRIVATE_USERS_FULL) {
33✔
2244
                /* Map all UID/GID from original to new user namespace. We can't use `0 0 UINT32_MAX` because
2245
                 * this is the same UID/GID map as the init user namespace and systemd's running_in_userns()
2246
                 * checks whether its in a user namespace by comparing uid_map/gid_map to `0 0 UINT32_MAX`.
2247
                 * Thus, we still map all UIDs/GIDs but do it using two extents to differentiate the new user
2248
                 * namespace from the init namespace:
2249
                 *   0 0 1
2250
                 *   1 1 UINT32_MAX - 1
2251
                 *
2252
                 * systemd will remove the heuristic in running_in_userns() and use namespace inodes in version 258
2253
                 * (PR #35382). But some users may be running a container image with older systemd < 258 so we keep
2254
                 * this uid_map/gid_map hack until version 259 for version N-1 compatibility.
2255
                 *
2256
                 * TODO: Switch to `0 0 UINT32_MAX` in systemd v259.
2257
                 *
2258
                 * Note the kernel defines the UID range between 0 and UINT32_MAX so we map all UIDs even though
2259
                 * the UID range beyond INT32_MAX (e.g. i.e. the range above the signed 32-bit range) is
2260
                 * icky. For example, setfsuid() returns the old UID as signed integer. But units can decide to
2261
                 * use these UIDs/GIDs so we need to map them. */
2262
                r = asprintf(&uid_map, "0 0 1\n"
3✔
2263
                                       "1 1 " UID_FMT "\n", (uid_t) (UINT32_MAX - 1));
2264
                if (r < 0)
3✔
2265
                        return -ENOMEM;
2266
        /* Can only set up multiple mappings with CAP_SETUID. */
2267
        } else if (have_effective_cap(CAP_SETUID) > 0 && uid != ouid && uid_is_valid(uid)) {
30✔
UNCOV
2268
                r = asprintf(&uid_map,
×
2269
                             UID_FMT " " UID_FMT " 1\n"     /* Map $OUID → $OUID */
2270
                             UID_FMT " " UID_FMT " 1\n",    /* Map $UID → $UID */
2271
                             ouid, ouid, uid, uid);
UNCOV
2272
                if (r < 0)
×
2273
                        return -ENOMEM;
2274
        } else {
2275
                r = asprintf(&uid_map,
30✔
2276
                             UID_FMT " " UID_FMT " 1\n",    /* Map $OUID → $OUID */
2277
                             ouid, ouid);
2278
                if (r < 0)
30✔
2279
                        return -ENOMEM;
2280
        }
2281

2282
        if (private_users == PRIVATE_USERS_IDENTITY) {
35✔
2283
                gid_map = strdup("0 0 65536\n");
2✔
2284
                if (!gid_map)
2✔
2285
                        return -ENOMEM;
2286
        } else if (private_users == PRIVATE_USERS_FULL) {
33✔
2287
                r = asprintf(&gid_map, "0 0 1\n"
3✔
2288
                                       "1 1 " GID_FMT "\n", (gid_t) (UINT32_MAX - 1));
2289
                if (r < 0)
3✔
2290
                        return -ENOMEM;
2291
        /* Can only set up multiple mappings with CAP_SETGID. */
2292
        } else if (have_effective_cap(CAP_SETGID) > 0 && gid != ogid && gid_is_valid(gid)) {
36✔
UNCOV
2293
                r = asprintf(&gid_map,
×
2294
                             GID_FMT " " GID_FMT " 1\n"     /* Map $OGID → $OGID */
2295
                             GID_FMT " " GID_FMT " 1\n",    /* Map $GID → $GID */
2296
                             ogid, ogid, gid, gid);
UNCOV
2297
                if (r < 0)
×
2298
                        return -ENOMEM;
2299
        } else {
2300
                r = asprintf(&gid_map,
30✔
2301
                             GID_FMT " " GID_FMT " 1\n",    /* Map $OGID -> $OGID */
2302
                             ogid, ogid);
2303
                if (r < 0)
30✔
2304
                        return -ENOMEM;
2305
        }
2306

2307
        /* Create a communication channel so that the parent can tell the child when it finished creating the user
2308
         * namespace. */
2309
        unshare_ready_fd = eventfd(0, EFD_CLOEXEC);
35✔
2310
        if (unshare_ready_fd < 0)
35✔
UNCOV
2311
                return -errno;
×
2312

2313
        /* Create a communication channel so that the child can tell the parent a proper error code in case it
2314
         * failed. */
2315
        if (pipe2(errno_pipe, O_CLOEXEC) < 0)
35✔
UNCOV
2316
                return -errno;
×
2317

2318
        r = safe_fork("(sd-userns)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL, &pid);
35✔
2319
        if (r < 0)
72✔
2320
                return r;
2321
        if (r == 0) {
72✔
2322
                _cleanup_close_ int fd = -EBADF;
×
2323
                const char *a;
37✔
2324
                pid_t ppid;
37✔
2325

2326
                /* Child process, running in the original user namespace. Let's update the parent's UID/GID map from
2327
                 * here, after the parent opened its own user namespace. */
2328

2329
                ppid = getppid();
37✔
2330
                errno_pipe[0] = safe_close(errno_pipe[0]);
37✔
2331

2332
                /* Wait until the parent unshared the user namespace */
2333
                if (read(unshare_ready_fd, &c, sizeof(c)) < 0)
37✔
UNCOV
2334
                        report_errno_and_exit(errno_pipe[1], -errno);
×
2335

2336
                /* Disable the setgroups() system call in the child user namespace, for good, unless PrivateUsers=full
2337
                 * and using the system service manager. */
2338
                a = procfs_file_alloca(ppid, "setgroups");
37✔
2339
                fd = open(a, O_WRONLY|O_CLOEXEC);
37✔
2340
                if (fd < 0) {
37✔
2341
                        if (errno != ENOENT) {
×
2342
                                r = log_debug_errno(errno, "Failed to open %s: %m", a);
×
UNCOV
2343
                                report_errno_and_exit(errno_pipe[1], r);
×
2344
                        }
2345

2346
                        /* If the file is missing the kernel is too old, let's continue anyway. */
2347
                } else {
2348
                        const char *setgroups = allow_setgroups ? "allow\n" : "deny\n";
37✔
2349
                        if (write(fd, setgroups, strlen(setgroups)) < 0) {
37✔
UNCOV
2350
                                r = log_debug_errno(errno, "Failed to write '%s' to %s: %m", setgroups, a);
×
UNCOV
2351
                                report_errno_and_exit(errno_pipe[1], r);
×
2352
                        }
2353

2354
                        fd = safe_close(fd);
37✔
2355
                }
2356

2357
                /* First write the GID map */
2358
                a = procfs_file_alloca(ppid, "gid_map");
37✔
2359
                fd = open(a, O_WRONLY|O_CLOEXEC);
37✔
2360
                if (fd < 0) {
37✔
2361
                        r = log_debug_errno(errno, "Failed to open %s: %m", a);
×
2362
                        report_errno_and_exit(errno_pipe[1], r);
×
2363
                }
2364

2365
                if (write(fd, gid_map, strlen(gid_map)) < 0) {
37✔
UNCOV
2366
                        r = log_debug_errno(errno, "Failed to write GID map to %s: %m", a);
×
UNCOV
2367
                        report_errno_and_exit(errno_pipe[1], r);
×
2368
                }
2369

2370
                fd = safe_close(fd);
37✔
2371

2372
                /* The write the UID map */
2373
                a = procfs_file_alloca(ppid, "uid_map");
37✔
2374
                fd = open(a, O_WRONLY|O_CLOEXEC);
37✔
2375
                if (fd < 0) {
37✔
UNCOV
2376
                        r = log_debug_errno(errno, "Failed to open %s: %m", a);
×
UNCOV
2377
                        report_errno_and_exit(errno_pipe[1], r);
×
2378
                }
2379

2380
                if (write(fd, uid_map, strlen(uid_map)) < 0) {
37✔
UNCOV
2381
                        r = log_debug_errno(errno, "Failed to write UID map to %s: %m", a);
×
2382
                        report_errno_and_exit(errno_pipe[1], r);
×
2383
                }
2384

2385
                _exit(EXIT_SUCCESS);
37✔
2386
        }
2387

2388
        errno_pipe[1] = safe_close(errno_pipe[1]);
35✔
2389

2390
        if (unshare(CLONE_NEWUSER) < 0)
35✔
UNCOV
2391
                return log_debug_errno(errno, "Failed to unshare user namespace: %m");
×
2392

2393
        /* Let the child know that the namespace is ready now */
2394
        if (write(unshare_ready_fd, &c, sizeof(c)) < 0)
35✔
UNCOV
2395
                return -errno;
×
2396

2397
        /* Try to read an error code from the child */
2398
        n = read(errno_pipe[0], &r, sizeof(r));
35✔
2399
        if (n < 0)
35✔
2400
                return -errno;
×
2401
        if (n == sizeof(r)) { /* an error code was sent to us */
35✔
UNCOV
2402
                if (r < 0)
×
2403
                        return r;
UNCOV
2404
                return -EIO;
×
2405
        }
2406
        if (n != 0) /* on success we should have read 0 bytes */
35✔
2407
                return -EIO;
2408

2409
        r = wait_for_terminate_and_check("(sd-userns)", TAKE_PID(pid), 0);
35✔
2410
        if (r < 0)
35✔
2411
                return r;
2412
        if (r != EXIT_SUCCESS) /* If something strange happened with the child, let's consider this fatal, too */
35✔
UNCOV
2413
                return -EIO;
×
2414

2415
        return 1;
2416
}
2417

2418
static int can_mount_proc(const ExecContext *c, const ExecParameters *p) {
5✔
2419
        _cleanup_close_pair_ int errno_pipe[2] = EBADF_PAIR;
3✔
UNCOV
2420
        _cleanup_(sigkill_waitp) pid_t pid = 0;
×
2421
        ssize_t n;
5✔
2422
        int r;
5✔
2423

2424
        assert(c);
5✔
2425
        assert(p);
5✔
2426

2427
        /* If running via unprivileged user manager and /proc/ is masked (e.g. /proc/kmsg is over-mounted with tmpfs
2428
         * like systemd-nspawn does), then mounting /proc/ will fail with EPERM. This is due to a kernel restriction
2429
         * where unprivileged user namespaces cannot mount a less restrictive instance of /proc. */
2430

2431
        /* Create a communication channel so that the child can tell the parent a proper error code in case it
2432
         * failed. */
2433
        if (pipe2(errno_pipe, O_CLOEXEC) < 0)
5✔
UNCOV
2434
                return log_exec_debug_errno(c, p, errno, "Failed to create pipe for communicating with child process (sd-proc-check): %m");
×
2435

2436
        /* Fork a child process into its own mount and PID namespace. Note safe_fork() already remounts / as SLAVE
2437
         * with FORK_MOUNTNS_SLAVE. */
2438
        r = safe_fork("(sd-proc-check)",
5✔
2439
                      FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE|FORK_NEW_PIDNS, &pid);
2440
        if (r < 0)
5✔
2441
                return log_exec_debug_errno(c, p, r, "Failed to fork child process (sd-proc-check): %m");
×
2442
        if (r == 0) {
5✔
2443
                errno_pipe[0] = safe_close(errno_pipe[0]);
2✔
2444

2445
                /* Try mounting /proc on /dev/shm/. No need to clean up the mount since the mount
2446
                 * namespace will be cleaned up once the process exits. */
2447
                r = mount_follow_verbose(LOG_DEBUG, "proc", "/dev/shm/", "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
2✔
2448
                if (r < 0) {
2✔
2449
                        (void) write(errno_pipe[1], &r, sizeof(r));
×
UNCOV
2450
                        _exit(EXIT_FAILURE);
×
2451
                }
2452

2453
                _exit(EXIT_SUCCESS);
2✔
2454
        }
2455

2456
        errno_pipe[1] = safe_close(errno_pipe[1]);
3✔
2457

2458
        /* Try to read an error code from the child */
2459
        n = read(errno_pipe[0], &r, sizeof(r));
3✔
2460
        if (n < 0)
3✔
UNCOV
2461
                return log_exec_debug_errno(c, p, errno, "Failed to read errno from pipe with child process (sd-proc-check): %m");
×
2462
        if (n == sizeof(r)) { /* an error code was sent to us */
3✔
2463
                /* This is the expected case where proc cannot be mounted due to permissions. */
2464
                if (ERRNO_IS_NEG_PRIVILEGE(r))
3✔
2465
                        return 0;
UNCOV
2466
                if (r < 0)
×
2467
                        return r;
2468

UNCOV
2469
                return -EIO;
×
2470
        }
2471
        if (n != 0) /* on success we should have read 0 bytes */
2✔
2472
                return -EIO;
2473

2474
        r = wait_for_terminate_and_check("(sd-proc-check)", TAKE_PID(pid), 0 /* flags= */);
2✔
2475
        if (r < 0)
2✔
UNCOV
2476
                return log_exec_debug_errno(c, p, r, "Failed to wait for (sd-proc-check) child process to terminate: %m");
×
2477
        if (r != EXIT_SUCCESS) /* If something strange happened with the child, let's consider this fatal, too */
2✔
UNCOV
2478
                return log_exec_debug_errno(c, p, SYNTHETIC_ERRNO(EIO), "Child process (sd-proc-check) exited with unexpected exit status '%d'.", r);
×
2479

2480
        return 1;
2481
}
2482

2483
static int setup_private_pids(const ExecContext *c, ExecParameters *p) {
7✔
UNCOV
2484
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
×
2485
        _cleanup_close_pair_ int errno_pipe[2] = EBADF_PAIR;
6✔
2486
        ssize_t n;
7✔
2487
        int r, q;
7✔
2488

2489
        assert(c);
7✔
2490
        assert(p);
7✔
2491
        assert(p->pidref_transport_fd >= 0);
7✔
2492

2493
        /* The first process created after unsharing a pid namespace becomes PID 1 in the pid namespace, so
2494
         * we have to fork after unsharing the pid namespace to become PID 1. The parent sends the child
2495
         * pidref to the manager and exits while the child process continues with the rest of exec_invoke()
2496
         * and finally executes the actual payload. */
2497

2498
        /* Create a communication channel so that the parent can tell the child a proper error code in case it
2499
         * failed to send child pidref to the manager. */
2500
        if (pipe2(errno_pipe, O_CLOEXEC) < 0)
7✔
UNCOV
2501
                return log_exec_debug_errno(c, p, errno, "Failed to create pipe for communicating with parent process: %m");
×
2502

2503
        /* Set FORK_DETACH to immediately re-parent the child process to the invoking manager process. */
2504
        r = pidref_safe_fork("(sd-pidns-child)", FORK_NEW_PIDNS|FORK_DETACH, &pidref);
7✔
2505
        if (r < 0)
13✔
UNCOV
2506
                return log_exec_debug_errno(c, p, r, "Failed to fork child into new pid namespace: %m");
×
2507
        if (r > 0) {
13✔
2508
                errno_pipe[0] = safe_close(errno_pipe[0]);
7✔
2509

2510
                /* In the parent process, we send the child pidref to the manager and exit.
2511
                 * If PIDFD is not supported, only the child PID is sent. The server then
2512
                 * uses the child PID to set the new exec main process. */
2513
                q = send_one_fd_iov(
7✔
2514
                                p->pidref_transport_fd,
2515
                                pidref.fd,
2516
                                &IOVEC_MAKE(&pidref.pid, sizeof(pidref.pid)),
2517
                                /*iovlen=*/ 1,
2518
                                /*flags=*/ 0);
2519
                /* Send error code to child process. */
2520
                (void) write(errno_pipe[1], &q, sizeof(q));
7✔
2521
                /* Exit here so we only go through the destructors in exec_invoke only once - in the child - as
2522
                 * some destructors have external effects. The main codepaths continue in the child process. */
2523
                _exit(q < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
7✔
2524
        }
2525

2526
        errno_pipe[1] = safe_close(errno_pipe[1]);
6✔
2527
        p->pidref_transport_fd = safe_close(p->pidref_transport_fd);
6✔
2528

2529
        /* Try to read an error code from the parent. Note a child process cannot wait for the parent so we always
2530
         * receive an errno even on success. */
2531
        n = read(errno_pipe[0], &r, sizeof(r));
6✔
2532
        if (n < 0)
6✔
UNCOV
2533
                return log_exec_debug_errno(c, p, errno, "Failed to read errno from pipe with parent process: %m");
×
2534
        if (n != sizeof(r))
6✔
UNCOV
2535
                return log_exec_debug_errno(c, p, SYNTHETIC_ERRNO(EIO), "Failed to read enough bytes from pipe with parent process");
×
2536
        if (r < 0)
6✔
UNCOV
2537
                return log_exec_debug_errno(c, p, r, "Failed to send child pidref to manager: %m");
×
2538

2539
        /* NOTE! This function returns in the child process only. */
2540
        return r;
2541
}
2542

2543
static int create_many_symlinks(const char *root, const char *source, char **symlinks) {
1,634✔
2544
        _cleanup_free_ char *src_abs = NULL;
1,634✔
2545
        int r;
1,634✔
2546

2547
        assert(source);
1,634✔
2548

2549
        src_abs = path_join(root, source);
1,634✔
2550
        if (!src_abs)
1,634✔
2551
                return -ENOMEM;
2552

2553
        STRV_FOREACH(dst, symlinks) {
1,647✔
2554
                _cleanup_free_ char *dst_abs = NULL;
13✔
2555

2556
                dst_abs = path_join(root, *dst);
13✔
2557
                if (!dst_abs)
13✔
2558
                        return -ENOMEM;
2559

2560
                r = mkdir_parents_label(dst_abs, 0755);
13✔
2561
                if (r < 0)
13✔
2562
                        return r;
2563

2564
                r = symlink_idempotent(src_abs, dst_abs, true);
13✔
2565
                if (r < 0)
13✔
2566
                        return r;
2567
        }
2568

2569
        return 0;
2570
}
2571

2572
static int setup_exec_directory(
66,511✔
2573
                const ExecContext *context,
2574
                const ExecParameters *params,
2575
                uid_t uid,
2576
                gid_t gid,
2577
                ExecDirectoryType type,
2578
                bool needs_mount_namespace,
2579
                int *exit_status) {
2580

2581
        static const int exit_status_table[_EXEC_DIRECTORY_TYPE_MAX] = {
66,511✔
2582
                [EXEC_DIRECTORY_RUNTIME]       = EXIT_RUNTIME_DIRECTORY,
2583
                [EXEC_DIRECTORY_STATE]         = EXIT_STATE_DIRECTORY,
2584
                [EXEC_DIRECTORY_CACHE]         = EXIT_CACHE_DIRECTORY,
2585
                [EXEC_DIRECTORY_LOGS]          = EXIT_LOGS_DIRECTORY,
2586
                [EXEC_DIRECTORY_CONFIGURATION] = EXIT_CONFIGURATION_DIRECTORY,
2587
        };
2588
        int r;
66,511✔
2589

2590
        assert(context);
66,511✔
2591
        assert(params);
66,511✔
2592
        assert(type >= 0 && type < _EXEC_DIRECTORY_TYPE_MAX);
66,511✔
2593
        assert(exit_status);
66,511✔
2594

2595
        if (!params->prefix[type])
66,511✔
2596
                return 0;
2597

2598
        if (params->flags & EXEC_CHOWN_DIRECTORIES) {
66,511✔
2599
                if (!uid_is_valid(uid))
63,316✔
2600
                        uid = 0;
49,751✔
2601
                if (!gid_is_valid(gid))
63,316✔
2602
                        gid = 0;
49,731✔
2603
        }
2604

2605
        FOREACH_ARRAY(i, context->directories[type].items, context->directories[type].n_items) {
70,523✔
2606
                _cleanup_free_ char *p = NULL, *pp = NULL;
4,013✔
2607

2608
                p = path_join(params->prefix[type], i->path);
4,013✔
2609
                if (!p) {
4,013✔
UNCOV
2610
                        r = -ENOMEM;
×
UNCOV
2611
                        goto fail;
×
2612
                }
2613

2614
                r = mkdir_parents_label(p, 0755);
4,013✔
2615
                if (r < 0)
4,013✔
UNCOV
2616
                        goto fail;
×
2617

2618
                if (IN_SET(type, EXEC_DIRECTORY_STATE, EXEC_DIRECTORY_LOGS) && params->runtime_scope == RUNTIME_SCOPE_USER) {
4,013✔
2619

2620
                        /* If we are in user mode, and a configuration directory exists but a state directory
2621
                         * doesn't exist, then we likely are upgrading from an older systemd version that
2622
                         * didn't know the more recent addition to the xdg-basedir spec: the $XDG_STATE_HOME
2623
                         * directory. In older systemd versions EXEC_DIRECTORY_STATE was aliased to
2624
                         * EXEC_DIRECTORY_CONFIGURATION, with the advent of $XDG_STATE_HOME it is now
2625
                         * separated. If a service has both dirs configured but only the configuration dir
2626
                         * exists and the state dir does not, we assume we are looking at an update
2627
                         * situation. Hence, create a compatibility symlink, so that all expectations are
2628
                         * met.
2629
                         *
2630
                         * (We also do something similar with the log directory, which still doesn't exist in
2631
                         * the xdg basedir spec. We'll make it a subdir of the state dir.) */
2632

2633
                        /* this assumes the state dir is always created before the configuration dir */
2634
                        assert_cc(EXEC_DIRECTORY_STATE < EXEC_DIRECTORY_LOGS);
7✔
2635
                        assert_cc(EXEC_DIRECTORY_LOGS < EXEC_DIRECTORY_CONFIGURATION);
7✔
2636

2637
                        r = access_nofollow(p, F_OK);
7✔
2638
                        if (r == -ENOENT) {
3✔
2639
                                _cleanup_free_ char *q = NULL;
3✔
2640

2641
                                /* OK, we know that the state dir does not exist. Let's see if the dir exists
2642
                                 * under the configuration hierarchy. */
2643

2644
                                if (type == EXEC_DIRECTORY_STATE)
3✔
2645
                                        q = path_join(params->prefix[EXEC_DIRECTORY_CONFIGURATION], i->path);
3✔
UNCOV
2646
                                else if (type == EXEC_DIRECTORY_LOGS)
×
2647
                                        q = path_join(params->prefix[EXEC_DIRECTORY_CONFIGURATION], "log", i->path);
×
2648
                                else
UNCOV
2649
                                        assert_not_reached();
×
2650
                                if (!q) {
3✔
UNCOV
2651
                                        r = -ENOMEM;
×
UNCOV
2652
                                        goto fail;
×
2653
                                }
2654

2655
                                r = access_nofollow(q, F_OK);
3✔
2656
                                if (r >= 0) {
2✔
2657
                                        /* It does exist! This hence looks like an update. Symlink the
2658
                                         * configuration directory into the state directory. */
2659

2660
                                        r = symlink_idempotent(q, p, /* make_relative= */ true);
1✔
2661
                                        if (r < 0)
1✔
UNCOV
2662
                                                goto fail;
×
2663

2664
                                        log_exec_notice(context, params, "Unit state directory %s missing but matching configuration directory %s exists, assuming update from systemd 253 or older, creating compatibility symlink.", p, q);
1✔
2665
                                        continue;
1✔
2666
                                } else if (r != -ENOENT)
2✔
UNCOV
2667
                                        log_exec_warning_errno(context, params, r, "Unable to detect whether unit configuration directory '%s' exists, assuming not: %m", q);
×
2668

2669
                        } else if (r < 0)
4✔
UNCOV
2670
                                log_exec_warning_errno(context, params, r, "Unable to detect whether unit state directory '%s' is missing, assuming it is: %m", p);
×
2671
                }
2672

2673
                if (exec_directory_is_private(context, type)) {
4,012✔
2674
                        /* So, here's one extra complication when dealing with DynamicUser=1 units. In that
2675
                         * case we want to avoid leaving a directory around fully accessible that is owned by
2676
                         * a dynamic user whose UID is later on reused. To lock this down we use the same
2677
                         * trick used by container managers to prohibit host users to get access to files of
2678
                         * the same UID in containers: we place everything inside a directory that has an
2679
                         * access mode of 0700 and is owned root:root, so that it acts as security boundary
2680
                         * for unprivileged host code. We then use fs namespacing to make this directory
2681
                         * permeable for the service itself.
2682
                         *
2683
                         * Specifically: for a service which wants a special directory "foo/" we first create
2684
                         * a directory "private/" with access mode 0700 owned by root:root. Then we place
2685
                         * "foo" inside of that directory (i.e. "private/foo/"), and make "foo" a symlink to
2686
                         * "private/foo". This way, privileged host users can access "foo/" as usual, but
2687
                         * unprivileged host users can't look into it. Inside of the namespace of the unit
2688
                         * "private/" is replaced by a more liberally accessible tmpfs, into which the host's
2689
                         * "private/foo/" is mounted under the same name, thus disabling the access boundary
2690
                         * for the service and making sure it only gets access to the dirs it needs but no
2691
                         * others. Tricky? Yes, absolutely, but it works!
2692
                         *
2693
                         * Note that we don't do this for EXEC_DIRECTORY_CONFIGURATION as that's assumed not
2694
                         * to be owned by the service itself.
2695
                         *
2696
                         * Also, note that we don't do this for EXEC_DIRECTORY_RUNTIME as that's often used
2697
                         * for sharing files or sockets with other services. */
2698

2699
                        pp = path_join(params->prefix[type], "private");
13✔
2700
                        if (!pp) {
13✔
2701
                                r = -ENOMEM;
×
UNCOV
2702
                                goto fail;
×
2703
                        }
2704

2705
                        /* First set up private root if it doesn't exist yet, with access mode 0700 and owned by root:root */
2706
                        r = mkdir_safe_label(pp, 0700, 0, 0, MKDIR_WARN_MODE);
13✔
2707
                        if (r < 0)
13✔
UNCOV
2708
                                goto fail;
×
2709

2710
                        if (!path_extend(&pp, i->path)) {
13✔
UNCOV
2711
                                r = -ENOMEM;
×
UNCOV
2712
                                goto fail;
×
2713
                        }
2714

2715
                        /* Create all directories between the configured directory and this private root, and mark them 0755 */
2716
                        r = mkdir_parents_label(pp, 0755);
13✔
2717
                        if (r < 0)
13✔
UNCOV
2718
                                goto fail;
×
2719

2720
                        if (is_dir(p, false) > 0 &&
13✔
2721
                            (access_nofollow(pp, F_OK) == -ENOENT)) {
×
2722

2723
                                /* Hmm, the private directory doesn't exist yet, but the normal one exists? If so, move
2724
                                 * it over. Most likely the service has been upgraded from one that didn't use
2725
                                 * DynamicUser=1, to one that does. */
2726

UNCOV
2727
                                log_exec_info(context,
×
2728
                                              params,
2729
                                              "Found pre-existing public %s= directory %s, migrating to %s.\n"
2730
                                              "Apparently, service previously had DynamicUser= turned off, and has now turned it on.",
2731
                                              exec_directory_type_to_string(type), p, pp);
2732

UNCOV
2733
                                r = RET_NERRNO(rename(p, pp));
×
UNCOV
2734
                                if (r < 0)
×
UNCOV
2735
                                        goto fail;
×
2736
                        } else {
2737
                                /* Otherwise, create the actual directory for the service */
2738

2739
                                r = mkdir_label(pp, context->directories[type].mode);
13✔
2740
                                if (r < 0 && r != -EEXIST)
13✔
UNCOV
2741
                                        goto fail;
×
2742
                        }
2743

2744
                        if (!FLAGS_SET(i->flags, EXEC_DIRECTORY_ONLY_CREATE)) {
13✔
2745
                                /* And link it up from the original place.
2746
                                 * Notes
2747
                                 * 1) If a mount namespace is going to be used, then this symlink remains on
2748
                                 *    the host, and a new one for the child namespace will be created later.
2749
                                 * 2) It is not necessary to create this symlink when one of its parent
2750
                                 *    directories is specified and already created. E.g.
2751
                                 *        StateDirectory=foo foo/bar
2752
                                 *    In that case, the inode points to pp and p for "foo/bar" are the same:
2753
                                 *        pp = "/var/lib/private/foo/bar"
2754
                                 *        p = "/var/lib/foo/bar"
2755
                                 *    and, /var/lib/foo is a symlink to /var/lib/private/foo. So, not only
2756
                                 *    we do not need to create the symlink, but we cannot create the symlink.
2757
                                 *    See issue #24783. */
2758
                                r = symlink_idempotent(pp, p, true);
13✔
2759
                                if (r < 0)
13✔
UNCOV
2760
                                        goto fail;
×
2761
                        }
2762

2763
                } else {
2764
                        _cleanup_free_ char *target = NULL;
3,999✔
2765

2766
                        if (EXEC_DIRECTORY_TYPE_SHALL_CHOWN(type) &&
7,958✔
2767
                            readlink_and_make_absolute(p, &target) >= 0) {
3,959✔
2768
                                _cleanup_free_ char *q = NULL, *q_resolved = NULL, *target_resolved = NULL;
11✔
2769

2770
                                /* This already exists and is a symlink? Interesting. Maybe it's one created
2771
                                 * by DynamicUser=1 (see above)?
2772
                                 *
2773
                                 * We do this for all directory types except for ConfigurationDirectory=,
2774
                                 * since they all support the private/ symlink logic at least in some
2775
                                 * configurations, see above. */
2776

2777
                                r = chase(target, NULL, 0, &target_resolved, NULL);
11✔
2778
                                if (r < 0)
11✔
UNCOV
2779
                                        goto fail;
×
2780

2781
                                q = path_join(params->prefix[type], "private", i->path);
11✔
2782
                                if (!q) {
11✔
UNCOV
2783
                                        r = -ENOMEM;
×
2784
                                        goto fail;
×
2785
                                }
2786

2787
                                /* /var/lib or friends may be symlinks. So, let's chase them also. */
2788
                                r = chase(q, NULL, CHASE_NONEXISTENT, &q_resolved, NULL);
11✔
2789
                                if (r < 0)
11✔
UNCOV
2790
                                        goto fail;
×
2791

2792
                                if (path_equal(q_resolved, target_resolved)) {
11✔
2793

2794
                                        /* Hmm, apparently DynamicUser= was once turned on for this service,
2795
                                         * but is no longer. Let's move the directory back up. */
2796

2797
                                        log_exec_info(context,
24✔
2798
                                                      params,
2799
                                                      "Found pre-existing private %s= directory %s, migrating to %s.\n"
2800
                                                      "Apparently, service previously had DynamicUser= turned on, and has now turned it off.",
2801
                                                      exec_directory_type_to_string(type), q, p);
2802

2803
                                        r = RET_NERRNO(unlink(p));
8✔
UNCOV
2804
                                        if (r < 0)
×
UNCOV
2805
                                                goto fail;
×
2806

2807
                                        r = RET_NERRNO(rename(q, p));
11✔
UNCOV
2808
                                        if (r < 0)
×
UNCOV
2809
                                                goto fail;
×
2810
                                }
2811
                        }
2812

2813
                        r = mkdir_label(p, context->directories[type].mode);
3,999✔
2814
                        if (r < 0) {
3,999✔
2815
                                if (r != -EEXIST)
2,668✔
UNCOV
2816
                                        goto fail;
×
2817

2818
                                if (!EXEC_DIRECTORY_TYPE_SHALL_CHOWN(type)) {
2,668✔
2819
                                        struct stat st;
27✔
2820

2821
                                        /* Don't change the owner/access mode of the configuration directory,
2822
                                         * as in the common case it is not written to by a service, and shall
2823
                                         * not be writable. */
2824

2825
                                        r = RET_NERRNO(stat(p, &st));
27✔
UNCOV
2826
                                        if (r < 0)
×
UNCOV
2827
                                                goto fail;
×
2828

2829
                                        /* Still complain if the access mode doesn't match */
2830
                                        if (((st.st_mode ^ context->directories[type].mode) & 07777) != 0)
27✔
UNCOV
2831
                                                log_exec_warning(context,
×
2832
                                                                 params,
2833
                                                                 "%s \'%s\' already exists but the mode is different. "
2834
                                                                 "(File system: %o %sMode: %o)",
2835
                                                                 exec_directory_type_to_string(type), i->path,
2836
                                                                 st.st_mode & 07777, exec_directory_type_to_string(type), context->directories[type].mode & 07777);
2837

2838
                                        continue;
27✔
2839
                                }
2840
                        }
2841
                }
2842

2843
                /* Lock down the access mode (we use chmod_and_chown() to make this idempotent. We don't
2844
                 * specify UID/GID here, so that path_chown_recursive() can optimize things depending on the
2845
                 * current UID/GID ownership.) */
2846
                const char *target_dir = pp ?: p;
3,985✔
2847
                r = chmod_and_chown(target_dir, context->directories[type].mode, UID_INVALID, GID_INVALID);
3,985✔
2848
                if (r < 0)
3,985✔
UNCOV
2849
                        goto fail;
×
2850

2851
                /* Skip the rest (which deals with ownership) in user mode, since ownership changes are not
2852
                 * available to user code anyway */
2853
                if (params->runtime_scope != RUNTIME_SCOPE_SYSTEM)
3,985✔
2854
                        continue;
9✔
2855

2856
                int idmapping_supported = is_idmapping_supported(target_dir);
3,976✔
2857
                if (idmapping_supported < 0) {
3,976✔
UNCOV
2858
                        r = log_debug_errno(idmapping_supported, "Unable to determine if ID mapping is supported on mount '%s': %m", target_dir);
×
2859
                        goto fail;
×
2860
                }
2861

2862
                log_debug("ID-mapping is%ssupported for exec directory %s", idmapping_supported ? " " : " not ", target_dir);
4,035✔
2863

2864
                /* Change the ownership of the whole tree, if necessary. When dynamic users are used we
2865
                 * drop the suid/sgid bits, since we really don't want SUID/SGID files for dynamic UID/GID
2866
                 * assignments to exist. */
2867
                uid_t chown_uid = uid;
3,976✔
2868
                gid_t chown_gid = gid;
3,976✔
2869
                bool do_chown = false;
3,976✔
2870

2871
                if (uid == 0 || gid == 0 || !idmapping_supported) {
3,976✔
2872
                        do_chown = true;
1,592✔
2873
                        i->idmapped = false;
1,592✔
2874
                } else {
2875
                        /* Use 'nobody' uid/gid for exec directories if ID-mapping is supported. For backward compatibility,
2876
                         * continue doing chmod/chown if the directory was chmod/chowned before (if uid/gid is not 'nobody') */
2877
                        struct stat st;
2,384✔
2878
                        r = RET_NERRNO(stat(target_dir, &st));
2,384✔
UNCOV
2879
                        if (r < 0)
×
UNCOV
2880
                                goto fail;
×
2881

2882
                        if (st.st_uid == UID_NOBODY && st.st_gid == GID_NOBODY) {
2,384✔
2883
                                do_chown = false;
7✔
2884
                                i->idmapped = true;
7✔
2885
                       } else if (exec_directory_is_private(context, type) && st.st_uid == 0 && st.st_gid == 0) {
2,377✔
2886
                                chown_uid = UID_NOBODY;
6✔
2887
                                chown_gid = GID_NOBODY;
6✔
2888
                                do_chown = true;
6✔
2889
                                i->idmapped = true;
6✔
2890
                        } else {
2891
                                do_chown = true;
2,371✔
2892
                                i->idmapped = false;
2,371✔
2893
                        }
2894
                }
2895

2896
                if (do_chown) {
3,976✔
2897
                        r = path_chown_recursive(target_dir, chown_uid, chown_gid, context->dynamic_user ? 01777 : 07777, AT_SYMLINK_FOLLOW);
7,929✔
2898
                        if (r < 0)
3,969✔
2899
                                goto fail;
1✔
2900
                }
2901
        }
2902

2903
        /* If we are not going to run in a namespace, set up the symlinks - otherwise
2904
         * they are set up later, to allow configuring empty var/run/etc. */
2905
        if (!needs_mount_namespace)
66,510✔
2906
                FOREACH_ARRAY(i, context->directories[type].items, context->directories[type].n_items) {
53,274✔
2907
                        r = create_many_symlinks(params->prefix[type], i->path, i->symlinks);
1,634✔
2908
                        if (r < 0)
1,634✔
UNCOV
2909
                                goto fail;
×
2910
                }
2911

2912
        return 0;
2913

2914
fail:
1✔
2915
        *exit_status = exit_status_table[type];
1✔
2916
        return r;
1✔
2917
}
2918

2919
#if ENABLE_SMACK
UNCOV
2920
static int setup_smack(
×
2921
                const ExecContext *context,
2922
                const ExecParameters *params,
2923
                int executable_fd) {
UNCOV
2924
        int r;
×
2925

UNCOV
2926
        assert(context);
×
UNCOV
2927
        assert(params);
×
UNCOV
2928
        assert(executable_fd >= 0);
×
2929

UNCOV
2930
        if (context->smack_process_label) {
×
UNCOV
2931
                r = mac_smack_apply_pid(0, context->smack_process_label);
×
UNCOV
2932
                if (r < 0)
×
UNCOV
2933
                        return r;
×
UNCOV
2934
        } else if (params->fallback_smack_process_label) {
×
UNCOV
2935
                _cleanup_free_ char *exec_label = NULL;
×
2936

UNCOV
2937
                r = mac_smack_read_fd(executable_fd, SMACK_ATTR_EXEC, &exec_label);
×
UNCOV
2938
                if (r < 0 && !ERRNO_IS_XATTR_ABSENT(r))
×
2939
                        return r;
2940

UNCOV
2941
                r = mac_smack_apply_pid(0, exec_label ?: params->fallback_smack_process_label);
×
UNCOV
2942
                if (r < 0)
×
2943
                        return r;
2944
        }
2945

2946
        return 0;
2947
}
2948
#endif
2949

2950
static int compile_bind_mounts(
2,311✔
2951
                const ExecContext *context,
2952
                const ExecParameters *params,
2953
                uid_t exec_directory_uid, /* only used for id-mapped mounts Exec directories */
2954
                gid_t exec_directory_gid, /* only used for id-mapped mounts Exec directories */
2955
                BindMount **ret_bind_mounts,
2956
                size_t *ret_n_bind_mounts,
2957
                char ***ret_empty_directories) {
2958

2959
        _cleanup_strv_free_ char **empty_directories = NULL;
2,311✔
2960
        BindMount *bind_mounts = NULL;
2,311✔
2961
        size_t n, h = 0;
2,311✔
2962
        int r;
2,311✔
2963

2964
        assert(context);
2,311✔
2965
        assert(params);
2,311✔
2966
        assert(ret_bind_mounts);
2,311✔
2967
        assert(ret_n_bind_mounts);
2,311✔
2968
        assert(ret_empty_directories);
2,311✔
2969

2970
        CLEANUP_ARRAY(bind_mounts, h, bind_mount_free_many);
2,311✔
2971

2972
        n = context->n_bind_mounts;
2,311✔
2973
        for (ExecDirectoryType t = 0; t < _EXEC_DIRECTORY_TYPE_MAX; t++) {
13,866✔
2974
                if (!params->prefix[t])
11,555✔
UNCOV
2975
                        continue;
×
2976

2977
                FOREACH_ARRAY(i, context->directories[t].items, context->directories[t].n_items)
13,299✔
2978
                        n += !FLAGS_SET(i->flags, EXEC_DIRECTORY_ONLY_CREATE) || FLAGS_SET(i->flags, EXEC_DIRECTORY_READ_ONLY);
1,744✔
2979
        }
2980

2981
        if (n <= 0) {
2,311✔
2982
                *ret_bind_mounts = NULL;
1,287✔
2983
                *ret_n_bind_mounts = 0;
1,287✔
2984
                *ret_empty_directories = NULL;
1,287✔
2985
                return 0;
1,287✔
2986
        }
2987

2988
        bind_mounts = new(BindMount, n);
1,024✔
2989
        if (!bind_mounts)
1,024✔
2990
                return -ENOMEM;
2991

2992
        FOREACH_ARRAY(item, context->bind_mounts, context->n_bind_mounts) {
1,044✔
2993
                r = bind_mount_add(&bind_mounts, &h, item);
20✔
2994
                if (r < 0)
20✔
2995
                        return r;
2996
        }
2997

2998
        for (ExecDirectoryType t = 0; t < _EXEC_DIRECTORY_TYPE_MAX; t++) {
6,144✔
2999
                if (!params->prefix[t])
5,120✔
UNCOV
3000
                        continue;
×
3001

3002
                if (context->directories[t].n_items == 0)
5,120✔
3003
                        continue;
3,832✔
3004

3005
                if (exec_directory_is_private(context, t) &&
1,301✔
3006
                    !exec_context_with_rootfs(context)) {
13✔
3007
                        char *private_root;
13✔
3008

3009
                        /* So this is for a dynamic user, and we need to make sure the process can access its own
3010
                         * directory. For that we overmount the usually inaccessible "private" subdirectory with a
3011
                         * tmpfs that makes it accessible and is empty except for the submounts we do this for. */
3012

3013
                        private_root = path_join(params->prefix[t], "private");
13✔
3014
                        if (!private_root)
13✔
3015
                                return -ENOMEM;
3016

3017
                        r = strv_consume(&empty_directories, private_root);
13✔
3018
                        if (r < 0)
13✔
3019
                                return r;
3020
                }
3021

3022
                FOREACH_ARRAY(i, context->directories[t].items, context->directories[t].n_items) {
3,032✔
3023
                        _cleanup_free_ char *s = NULL, *d = NULL;
1,744✔
3024

3025
                        /* When one of the parent directories is in the list, we cannot create the symlink
3026
                         * for the child directory. See also the comments in setup_exec_directory().
3027
                         * But if it needs to be read only, then we have to create a bind mount anyway to
3028
                         * make it so. */
3029
                        if (FLAGS_SET(i->flags, EXEC_DIRECTORY_ONLY_CREATE) && !FLAGS_SET(i->flags, EXEC_DIRECTORY_READ_ONLY))
1,744✔
UNCOV
3030
                                continue;
×
3031

3032
                        if (exec_directory_is_private(context, t))
1,744✔
3033
                                s = path_join(params->prefix[t], "private", i->path);
13✔
3034
                        else
3035
                                s = path_join(params->prefix[t], i->path);
1,731✔
3036
                        if (!s)
1,744✔
3037
                                return -ENOMEM;
3038

3039
                        if (exec_directory_is_private(context, t) &&
1,757✔
3040
                            exec_context_with_rootfs(context))
13✔
3041
                                /* When RootDirectory= or RootImage= are set, then the symbolic link to the private
3042
                                 * directory is not created on the root directory. So, let's bind-mount the directory
3043
                                 * on the 'non-private' place. */
UNCOV
3044
                                d = path_join(params->prefix[t], i->path);
×
3045
                        else
3046
                                d = strdup(s);
1,744✔
3047
                        if (!d)
1,744✔
3048
                                return -ENOMEM;
3049

3050
                        bind_mounts[h++] = (BindMount) {
1,744✔
3051
                                .source = TAKE_PTR(s),
1,744✔
3052
                                .destination = TAKE_PTR(d),
1,744✔
3053
                                .nosuid = context->dynamic_user, /* don't allow suid/sgid when DynamicUser= is on */
1,744✔
3054
                                .recursive = true,
3055
                                .read_only = FLAGS_SET(i->flags, EXEC_DIRECTORY_READ_ONLY),
1,744✔
3056
                                .idmapped = i->idmapped,
1,744✔
3057
                                .uid = exec_directory_uid,
3058
                                .gid = exec_directory_gid,
3059
                        };
3060
                }
3061
        }
3062

3063
        assert(h == n);
1,024✔
3064

3065
        *ret_bind_mounts = TAKE_PTR(bind_mounts);
1,024✔
3066
        *ret_n_bind_mounts = n;
1,024✔
3067
        *ret_empty_directories = TAKE_PTR(empty_directories);
1,024✔
3068

3069
        return (int) n;
1,024✔
3070
}
3071

3072
/* ret_symlinks will contain a list of pairs src:dest that describes
3073
 * the symlinks to create later on. For example, the symlinks needed
3074
 * to safely give private directories to DynamicUser=1 users. */
3075
static int compile_symlinks(
2,311✔
3076
                const ExecContext *context,
3077
                const ExecParameters *params,
3078
                bool setup_os_release_symlink,
3079
                char ***ret_symlinks) {
3080

3081
        _cleanup_strv_free_ char **symlinks = NULL;
2,311✔
3082
        int r;
2,311✔
3083

3084
        assert(context);
2,311✔
3085
        assert(params);
2,311✔
3086
        assert(ret_symlinks);
2,311✔
3087

3088
        for (ExecDirectoryType dt = 0; dt < _EXEC_DIRECTORY_TYPE_MAX; dt++)
13,866✔
3089
                FOREACH_ARRAY(i, context->directories[dt].items, context->directories[dt].n_items) {
13,299✔
3090
                        _cleanup_free_ char *private_path = NULL, *path = NULL;
1,731✔
3091

3092
                        STRV_FOREACH(symlink, i->symlinks) {
1,870✔
3093
                                _cleanup_free_ char *src_abs = NULL, *dst_abs = NULL;
126✔
3094

3095
                                src_abs = path_join(params->prefix[dt], i->path);
126✔
3096
                                dst_abs = path_join(params->prefix[dt], *symlink);
126✔
3097
                                if (!src_abs || !dst_abs)
126✔
3098
                                        return -ENOMEM;
3099

3100
                                r = strv_consume_pair(&symlinks, TAKE_PTR(src_abs), TAKE_PTR(dst_abs));
126✔
3101
                                if (r < 0)
126✔
3102
                                        return r;
3103
                        }
3104

3105
                        if (!exec_directory_is_private(context, dt) ||
1,757✔
3106
                            exec_context_with_rootfs(context) ||
13✔
3107
                            FLAGS_SET(i->flags, EXEC_DIRECTORY_ONLY_CREATE))
13✔
3108
                                continue;
1,731✔
3109

3110
                        private_path = path_join(params->prefix[dt], "private", i->path);
13✔
3111
                        if (!private_path)
13✔
3112
                                return -ENOMEM;
3113

3114
                        path = path_join(params->prefix[dt], i->path);
13✔
3115
                        if (!path)
13✔
3116
                                return -ENOMEM;
3117

3118
                        r = strv_consume_pair(&symlinks, TAKE_PTR(private_path), TAKE_PTR(path));
13✔
3119
                        if (r < 0)
13✔
3120
                                return r;
3121
                }
3122

3123
        /* We make the host's os-release available via a symlink, so that we can copy it atomically
3124
         * and readers will never get a half-written version. Note that, while the paths specified here are
3125
         * absolute, when they are processed in namespace.c they will be made relative automatically, i.e.:
3126
         * 'os-release -> .os-release-stage/os-release' is what will be created. */
3127
        if (setup_os_release_symlink) {
2,311✔
3128
                r = strv_extend_many(
7✔
3129
                                &symlinks,
3130
                                "/run/host/.os-release-stage/os-release",
3131
                                "/run/host/os-release");
3132
                if (r < 0)
7✔
3133
                        return r;
3134
        }
3135

3136
        *ret_symlinks = TAKE_PTR(symlinks);
2,311✔
3137

3138
        return 0;
2,311✔
3139
}
3140

3141
static bool insist_on_sandboxing(
×
3142
                const ExecContext *context,
3143
                const char *root_dir,
3144
                const char *root_image,
3145
                const BindMount *bind_mounts,
3146
                size_t n_bind_mounts) {
3147

UNCOV
3148
        assert(context);
×
UNCOV
3149
        assert(n_bind_mounts == 0 || bind_mounts);
×
3150

3151
        /* Checks whether we need to insist on fs namespacing. i.e. whether we have settings configured that
3152
         * would alter the view on the file system beyond making things read-only or invisible, i.e. would
3153
         * rearrange stuff in a way we cannot ignore gracefully. */
3154

UNCOV
3155
        if (context->n_temporary_filesystems > 0)
×
3156
                return true;
3157

UNCOV
3158
        if (root_dir || root_image)
×
3159
                return true;
3160

UNCOV
3161
        if (context->n_mount_images > 0)
×
3162
                return true;
3163

UNCOV
3164
        if (context->dynamic_user)
×
3165
                return true;
3166

UNCOV
3167
        if (context->n_extension_images > 0 || !strv_isempty(context->extension_directories))
×
3168
                return true;
3169

3170
        /* If there are any bind mounts set that don't map back onto themselves, fs namespacing becomes
3171
         * essential. */
UNCOV
3172
        FOREACH_ARRAY(i, bind_mounts, n_bind_mounts)
×
UNCOV
3173
                if (!path_equal(i->source, i->destination))
×
3174
                        return true;
3175

UNCOV
3176
        if (context->log_namespace)
×
UNCOV
3177
                return true;
×
3178

3179
        return false;
3180
}
3181

3182
static int setup_ephemeral(
2,311✔
3183
                const ExecContext *context,
3184
                ExecRuntime *runtime,
3185
                char **root_image,            /* both input and output! modified if ephemeral logic enabled */
3186
                char **root_directory,        /* ditto */
3187
                char **reterr_path) {
3188

3189
        _cleanup_close_ int fd = -EBADF;
2,311✔
3190
        _cleanup_free_ char *new_root = NULL;
2,311✔
3191
        int r;
2,311✔
3192

3193
        assert(context);
2,311✔
3194
        assert(runtime);
2,311✔
3195
        assert(root_image);
2,311✔
3196
        assert(root_directory);
2,311✔
3197

3198
        if (!*root_image && !*root_directory)
2,311✔
3199
                return 0;
3200

3201
        if (!runtime->ephemeral_copy)
8✔
3202
                return 0;
3203

3204
        assert(runtime->ephemeral_storage_socket[0] >= 0);
×
3205
        assert(runtime->ephemeral_storage_socket[1] >= 0);
×
3206

3207
        new_root = strdup(runtime->ephemeral_copy);
×
UNCOV
3208
        if (!new_root)
×
3209
                return log_oom_debug();
×
3210

3211
        r = posix_lock(runtime->ephemeral_storage_socket[0], LOCK_EX);
×
UNCOV
3212
        if (r < 0)
×
UNCOV
3213
                return log_debug_errno(r, "Failed to lock ephemeral storage socket: %m");
×
3214

3215
        CLEANUP_POSIX_UNLOCK(runtime->ephemeral_storage_socket[0]);
×
3216

3217
        fd = receive_one_fd(runtime->ephemeral_storage_socket[0], MSG_PEEK|MSG_DONTWAIT);
×
UNCOV
3218
        if (fd >= 0)
×
3219
                /* We got an fd! That means ephemeral has already been set up, so nothing to do here. */
3220
                return 0;
UNCOV
3221
        if (fd != -EAGAIN)
×
UNCOV
3222
                return log_debug_errno(fd, "Failed to receive file descriptor queued on ephemeral storage socket: %m");
×
3223

UNCOV
3224
        if (*root_image) {
×
UNCOV
3225
                log_debug("Making ephemeral copy of %s to %s", *root_image, new_root);
×
3226

3227
                fd = copy_file(*root_image, new_root, O_EXCL, 0600,
×
3228
                               COPY_LOCK_BSD|COPY_REFLINK|COPY_CRTIME|COPY_NOCOW_AFTER);
UNCOV
3229
                if (fd < 0) {
×
UNCOV
3230
                        *reterr_path = strdup(*root_image);
×
UNCOV
3231
                        return log_debug_errno(fd, "Failed to copy image %s to %s: %m",
×
3232
                                               *root_image, new_root);
3233
                }
3234
        } else {
3235
                assert(*root_directory);
×
3236

3237
                log_debug("Making ephemeral snapshot of %s to %s", *root_directory, new_root);
×
3238

UNCOV
3239
                fd = btrfs_subvol_snapshot_at(
×
3240
                                AT_FDCWD, *root_directory,
3241
                                AT_FDCWD, new_root,
3242
                                BTRFS_SNAPSHOT_FALLBACK_COPY |
3243
                                BTRFS_SNAPSHOT_FALLBACK_DIRECTORY |
3244
                                BTRFS_SNAPSHOT_RECURSIVE |
3245
                                BTRFS_SNAPSHOT_LOCK_BSD);
UNCOV
3246
                if (fd < 0) {
×
UNCOV
3247
                        *reterr_path = strdup(*root_directory);
×
UNCOV
3248
                        return log_debug_errno(fd, "Failed to snapshot directory %s to %s: %m",
×
3249
                                               *root_directory, new_root);
3250
                }
3251
        }
3252

UNCOV
3253
        r = send_one_fd(runtime->ephemeral_storage_socket[1], fd, MSG_DONTWAIT);
×
UNCOV
3254
        if (r < 0)
×
UNCOV
3255
                return log_debug_errno(r, "Failed to queue file descriptor on ephemeral storage socket: %m");
×
3256

UNCOV
3257
        if (*root_image)
×
UNCOV
3258
                free_and_replace(*root_image, new_root);
×
3259
        else {
UNCOV
3260
                assert(*root_directory);
×
UNCOV
3261
                free_and_replace(*root_directory, new_root);
×
3262
        }
3263

3264
        return 1;
3265
}
3266

3267
static int verity_settings_prepare(
7✔
3268
                VeritySettings *verity,
3269
                const char *root_image,
3270
                const void *root_hash,
3271
                size_t root_hash_size,
3272
                const char *root_hash_path,
3273
                const void *root_hash_sig,
3274
                size_t root_hash_sig_size,
3275
                const char *root_hash_sig_path,
3276
                const char *verity_data_path) {
3277

3278
        int r;
7✔
3279

3280
        assert(verity);
7✔
3281

3282
        if (root_hash) {
7✔
3283
                void *d;
4✔
3284

3285
                d = memdup(root_hash, root_hash_size);
4✔
3286
                if (!d)
4✔
3287
                        return -ENOMEM;
7✔
3288

3289
                free_and_replace(verity->root_hash, d);
4✔
3290
                verity->root_hash_size = root_hash_size;
4✔
3291
                verity->designator = PARTITION_ROOT;
4✔
3292
        }
3293

3294
        if (root_hash_sig) {
7✔
UNCOV
3295
                void *d;
×
3296

UNCOV
3297
                d = memdup(root_hash_sig, root_hash_sig_size);
×
3298
                if (!d)
×
3299
                        return -ENOMEM;
7✔
3300

UNCOV
3301
                free_and_replace(verity->root_hash_sig, d);
×
UNCOV
3302
                verity->root_hash_sig_size = root_hash_sig_size;
×
UNCOV
3303
                verity->designator = PARTITION_ROOT;
×
3304
        }
3305

3306
        if (verity_data_path) {
7✔
UNCOV
3307
                r = free_and_strdup(&verity->data_path, verity_data_path);
×
UNCOV
3308
                if (r < 0)
×
3309
                        return r;
3310
        }
3311

3312
        r = verity_settings_load(
7✔
3313
                        verity,
3314
                        root_image,
3315
                        root_hash_path,
3316
                        root_hash_sig_path);
3317
        if (r < 0)
7✔
UNCOV
3318
                return log_debug_errno(r, "Failed to load root hash: %m");
×
3319

3320
        return 0;
3321
}
3322

3323
static int pick_versions(
2,313✔
3324
                const ExecContext *context,
3325
                const ExecParameters *params,
3326
                char **ret_root_image,
3327
                char **ret_root_directory,
3328
                char **reterr_path) {
3329

3330
        int r;
2,313✔
3331

3332
        assert(context);
2,313✔
3333
        assert(params);
2,313✔
3334
        assert(ret_root_image);
2,313✔
3335
        assert(ret_root_directory);
2,313✔
3336

3337
        if (context->root_image) {
2,313✔
3338
                _cleanup_(pick_result_done) PickResult result = PICK_RESULT_NULL;
8✔
3339

3340
                r = path_pick(/* toplevel_path= */ NULL,
16✔
3341
                              /* toplevel_fd= */ AT_FDCWD,
3342
                              context->root_image,
8✔
3343
                              &pick_filter_image_raw,
3344
                              PICK_ARCHITECTURE|PICK_TRIES|PICK_RESOLVE,
3345
                              &result);
3346
                if (r < 0) {
8✔
3347
                        *reterr_path = strdup(context->root_image);
1✔
3348
                        return r;
1✔
3349
                }
3350

3351
                if (!result.path) {
7✔
3352
                        *reterr_path = strdup(context->root_image);
×
UNCOV
3353
                        return log_exec_debug_errno(context, params, SYNTHETIC_ERRNO(ENOENT), "No matching entry in .v/ directory %s found.", context->root_image);
×
3354
                }
3355

3356
                *ret_root_image = TAKE_PTR(result.path);
7✔
3357
                *ret_root_directory = NULL;
7✔
3358
                return r;
7✔
3359
        }
3360

3361
        if (context->root_directory) {
2,305✔
3362
                _cleanup_(pick_result_done) PickResult result = PICK_RESULT_NULL;
2✔
3363

3364
                r = path_pick(/* toplevel_path= */ NULL,
4✔
3365
                              /* toplevel_fd= */ AT_FDCWD,
3366
                              context->root_directory,
2✔
3367
                              &pick_filter_image_dir,
3368
                              PICK_ARCHITECTURE|PICK_TRIES|PICK_RESOLVE,
3369
                              &result);
3370
                if (r < 0) {
2✔
UNCOV
3371
                        *reterr_path = strdup(context->root_directory);
×
UNCOV
3372
                        return r;
×
3373
                }
3374

3375
                if (!result.path) {
2✔
3376
                        *reterr_path = strdup(context->root_directory);
1✔
3377
                        return log_exec_debug_errno(context, params, SYNTHETIC_ERRNO(ENOENT), "No matching entry in .v/ directory %s found.", context->root_directory);
3✔
3378
                }
3379

3380
                *ret_root_image = NULL;
1✔
3381
                *ret_root_directory = TAKE_PTR(result.path);
1✔
3382
                return r;
1✔
3383
        }
3384

3385
        *ret_root_image = *ret_root_directory = NULL;
2,303✔
3386
        return 0;
2,303✔
3387
}
3388

3389
static int apply_mount_namespace(
2,313✔
3390
                ExecCommandFlags command_flags,
3391
                const ExecContext *context,
3392
                const ExecParameters *params,
3393
                ExecRuntime *runtime,
3394
                const char *memory_pressure_path,
3395
                bool needs_sandboxing,
3396
                char **reterr_path,
3397
                uid_t exec_directory_uid,
3398
                gid_t exec_directory_gid) {
3399

3400
        _cleanup_(verity_settings_done) VeritySettings verity = VERITY_SETTINGS_DEFAULT;
2,313✔
3401
        _cleanup_strv_free_ char **empty_directories = NULL, **symlinks = NULL,
2,313✔
UNCOV
3402
                        **read_write_paths_cleanup = NULL;
×
UNCOV
3403
        _cleanup_free_ char *creds_path = NULL, *incoming_dir = NULL, *propagate_dir = NULL,
×
3404
                *private_namespace_dir = NULL, *host_os_release_stage = NULL, *root_image = NULL, *root_dir = NULL;
2,313✔
3405
        const char *tmp_dir = NULL, *var_tmp_dir = NULL;
2,313✔
3406
        char **read_write_paths;
2,313✔
3407
        bool setup_os_release_symlink;
2,313✔
3408
        BindMount *bind_mounts = NULL;
2,313✔
3409
        size_t n_bind_mounts = 0;
2,313✔
3410
        int r;
2,313✔
3411

3412
        assert(context);
2,313✔
3413
        assert(params);
2,313✔
3414
        assert(runtime);
2,313✔
3415

3416
        CLEANUP_ARRAY(bind_mounts, n_bind_mounts, bind_mount_free_many);
2,313✔
3417

3418
        if (params->flags & EXEC_APPLY_CHROOT) {
2,313✔
3419
                r = pick_versions(
2,313✔
3420
                                context,
3421
                                params,
3422
                                &root_image,
3423
                                &root_dir,
3424
                                reterr_path);
3425
                if (r < 0)
2,313✔
3426
                        return r;
3427

3428
                r = setup_ephemeral(
2,311✔
3429
                                context,
3430
                                runtime,
3431
                                &root_image,
3432
                                &root_dir,
3433
                                reterr_path);
3434
                if (r < 0)
2,311✔
3435
                        return r;
3436
        }
3437

3438
        r = compile_bind_mounts(context, params, exec_directory_uid, exec_directory_gid, &bind_mounts, &n_bind_mounts, &empty_directories);
2,311✔
3439
        if (r < 0)
2,311✔
3440
                return r;
3441

3442
        /* We need to make the pressure path writable even if /sys/fs/cgroups is made read-only, as the
3443
         * service will need to write to it in order to start the notifications. */
3444
        if (exec_is_cgroup_mount_read_only(context, params) && memory_pressure_path && !streq(memory_pressure_path, "/dev/null")) {
2,311✔
3445
                read_write_paths_cleanup = strv_copy(context->read_write_paths);
1,351✔
3446
                if (!read_write_paths_cleanup)
1,351✔
3447
                        return -ENOMEM;
3448

3449
                r = strv_extend(&read_write_paths_cleanup, memory_pressure_path);
1,351✔
3450
                if (r < 0)
1,351✔
3451
                        return r;
3452

3453
                read_write_paths = read_write_paths_cleanup;
1,351✔
3454
        } else
3455
                read_write_paths = context->read_write_paths;
960✔
3456

3457
        if (needs_sandboxing) {
2,311✔
3458
                /* The runtime struct only contains the parent of the private /tmp, which is non-accessible
3459
                 * to world users. Inside of it there's a /tmp that is sticky, and that's the one we want to
3460
                 * use here.  This does not apply when we are using /run/systemd/empty as fallback. */
3461

3462
                if (context->private_tmp == PRIVATE_TMP_CONNECTED && runtime->shared) {
2,311✔
3463
                        if (streq_ptr(runtime->shared->tmp_dir, RUN_SYSTEMD_EMPTY))
381✔
3464
                                tmp_dir = runtime->shared->tmp_dir;
3465
                        else if (runtime->shared->tmp_dir)
381✔
3466
                                tmp_dir = strjoina(runtime->shared->tmp_dir, "/tmp");
1,905✔
3467

3468
                        if (streq_ptr(runtime->shared->var_tmp_dir, RUN_SYSTEMD_EMPTY))
381✔
3469
                                var_tmp_dir = runtime->shared->var_tmp_dir;
3470
                        else if (runtime->shared->var_tmp_dir)
381✔
3471
                                var_tmp_dir = strjoina(runtime->shared->var_tmp_dir, "/tmp");
1,905✔
3472
                }
3473
        }
3474

3475
        /* Symlinks (exec dirs, os-release) are set up after other mounts, before they are made read-only. */
3476
        setup_os_release_symlink = needs_sandboxing && exec_context_get_effective_mount_apivfs(context) && (root_dir || root_image);
2,311✔
3477
        r = compile_symlinks(context, params, setup_os_release_symlink, &symlinks);
2,311✔
3478
        if (r < 0)
2,311✔
3479
                return r;
3480

3481
        if (context->mount_propagation_flag == MS_SHARED)
2,311✔
UNCOV
3482
                log_exec_debug(context,
×
3483
                               params,
3484
                               "shared mount propagation hidden by other fs namespacing unit settings: ignoring");
3485

3486
        r = exec_context_get_credential_directory(context, params, params->unit_id, &creds_path);
2,311✔
3487
        if (r < 0)
2,311✔
3488
                return r;
3489

3490
        if (params->runtime_scope == RUNTIME_SCOPE_SYSTEM) {
2,311✔
3491
                propagate_dir = path_join("/run/systemd/propagate/", params->unit_id);
2,288✔
3492
                if (!propagate_dir)
2,288✔
3493
                        return -ENOMEM;
3494

3495
                incoming_dir = strdup("/run/systemd/incoming");
2,288✔
3496
                if (!incoming_dir)
2,288✔
3497
                        return -ENOMEM;
3498

3499
                private_namespace_dir = strdup("/run/systemd");
2,288✔
3500
                if (!private_namespace_dir)
2,288✔
3501
                        return -ENOMEM;
3502

3503
                /* If running under a different root filesystem, propagate the host's os-release. We make a
3504
                 * copy rather than just bind mounting it, so that it can be updated on soft-reboot. */
3505
                if (setup_os_release_symlink) {
2,288✔
3506
                        host_os_release_stage = strdup("/run/systemd/propagate/.os-release-stage");
7✔
3507
                        if (!host_os_release_stage)
7✔
3508
                                return -ENOMEM;
3509
                }
3510
        } else {
3511
                assert(params->runtime_scope == RUNTIME_SCOPE_USER);
23✔
3512

3513
                if (asprintf(&private_namespace_dir, "/run/user/" UID_FMT "/systemd", geteuid()) < 0)
23✔
3514
                        return -ENOMEM;
3515

3516
                if (setup_os_release_symlink) {
23✔
UNCOV
3517
                        if (asprintf(&host_os_release_stage,
×
3518
                                     "/run/user/" UID_FMT "/systemd/propagate/.os-release-stage",
3519
                                     geteuid()) < 0)
3520
                                return -ENOMEM;
3521
                }
3522
        }
3523

3524
        if (root_image) {
2,311✔
3525
                r = verity_settings_prepare(
14✔
3526
                        &verity,
3527
                        root_image,
3528
                        context->root_hash, context->root_hash_size, context->root_hash_path,
7✔
3529
                        context->root_hash_sig, context->root_hash_sig_size, context->root_hash_sig_path,
7✔
3530
                        context->root_verity);
7✔
3531
                if (r < 0)
7✔
3532
                        return r;
3533
        }
3534

UNCOV
3535
        NamespaceParameters parameters = {
×
3536
                .runtime_scope = params->runtime_scope,
2,311✔
3537

3538
                .root_directory = root_dir,
3539
                .root_image = root_image,
3540
                .root_image_options = context->root_image_options,
2,311✔
3541
                .root_image_policy = context->root_image_policy ?: &image_policy_service,
2,311✔
3542

3543
                .read_write_paths = read_write_paths,
3544
                .read_only_paths = needs_sandboxing ? context->read_only_paths : NULL,
2,311✔
3545
                .inaccessible_paths = needs_sandboxing ? context->inaccessible_paths : NULL,
2,311✔
3546

3547
                .exec_paths = needs_sandboxing ? context->exec_paths : NULL,
2,311✔
3548
                .no_exec_paths = needs_sandboxing ? context->no_exec_paths : NULL,
2,311✔
3549

3550
                .empty_directories = empty_directories,
3551
                .symlinks = symlinks,
3552

3553
                .bind_mounts = bind_mounts,
3554
                .n_bind_mounts = n_bind_mounts,
3555

3556
                .temporary_filesystems = context->temporary_filesystems,
2,311✔
3557
                .n_temporary_filesystems = context->n_temporary_filesystems,
2,311✔
3558

3559
                .mount_images = context->mount_images,
2,311✔
3560
                .n_mount_images = context->n_mount_images,
2,311✔
3561
                .mount_image_policy = context->mount_image_policy ?: &image_policy_service,
2,311✔
3562

3563
                .tmp_dir = tmp_dir,
3564
                .var_tmp_dir = var_tmp_dir,
3565

3566
                .creds_path = creds_path,
3567
                .log_namespace = context->log_namespace,
2,311✔
3568
                .mount_propagation_flag = context->mount_propagation_flag,
2,311✔
3569

3570
                .verity = &verity,
3571

3572
                .extension_images = context->extension_images,
2,311✔
3573
                .n_extension_images = context->n_extension_images,
2,311✔
3574
                .extension_image_policy = context->extension_image_policy ?: &image_policy_sysext,
2,311✔
3575
                .extension_directories = context->extension_directories,
2,311✔
3576

3577
                .propagate_dir = propagate_dir,
3578
                .incoming_dir = incoming_dir,
3579
                .private_namespace_dir = private_namespace_dir,
3580
                .host_notify_socket = params->notify_socket,
2,311✔
3581
                .notify_socket_path = exec_get_private_notify_socket_path(context, params, needs_sandboxing),
2,311✔
3582
                .host_os_release_stage = host_os_release_stage,
3583

3584
                /* If DynamicUser=no and RootDirectory= is set then lets pass a relaxed sandbox info,
3585
                 * otherwise enforce it, don't ignore protected paths and fail if we are enable to apply the
3586
                 * sandbox inside the mount namespace. */
3587
                .ignore_protect_paths = !needs_sandboxing && !context->dynamic_user && root_dir,
2,311✔
3588

3589
                .protect_control_groups = needs_sandboxing ? exec_get_protect_control_groups(context, params) : PROTECT_CONTROL_GROUPS_NO,
2,311✔
3590
                .protect_kernel_tunables = needs_sandboxing && context->protect_kernel_tunables,
2,311✔
3591
                .protect_kernel_modules = needs_sandboxing && context->protect_kernel_modules,
2,311✔
3592
                .protect_kernel_logs = needs_sandboxing && context->protect_kernel_logs,
2,311✔
3593

3594
                .private_dev = needs_sandboxing && context->private_devices,
2,311✔
3595
                .private_network = needs_sandboxing && exec_needs_network_namespace(context),
2,311✔
3596
                .private_ipc = needs_sandboxing && exec_needs_ipc_namespace(context),
2,311✔
3597
                .private_pids = needs_sandboxing && exec_needs_pid_namespace(context) ? context->private_pids : PRIVATE_PIDS_NO,
2,311✔
3598
                .private_tmp = needs_sandboxing ? context->private_tmp : PRIVATE_TMP_NO,
2,311✔
3599

3600
                .mount_apivfs = needs_sandboxing && exec_context_get_effective_mount_apivfs(context),
2,311✔
3601
                .bind_log_sockets = needs_sandboxing && exec_context_get_effective_bind_log_sockets(context),
2,311✔
3602

3603
                /* If NNP is on, we can turn on MS_NOSUID, since it won't have any effect anymore. */
3604
                .mount_nosuid = needs_sandboxing && context->no_new_privileges && !mac_selinux_use(),
2,311✔
3605

3606
                .protect_home = needs_sandboxing ? context->protect_home : PROTECT_HOME_NO,
2,311✔
3607
                .protect_hostname = needs_sandboxing ? context->protect_hostname : PROTECT_HOSTNAME_NO,
2,311✔
3608
                .protect_system = needs_sandboxing ? context->protect_system : PROTECT_SYSTEM_NO,
2,311✔
3609
                .protect_proc = needs_sandboxing ? context->protect_proc : PROTECT_PROC_DEFAULT,
2,311✔
3610
                .proc_subset = needs_sandboxing ? context->proc_subset : PROC_SUBSET_ALL,
2,311✔
3611
        };
3612

3613
        r = setup_namespace(&parameters, reterr_path);
2,311✔
3614
        /* If we couldn't set up the namespace this is probably due to a missing capability. setup_namespace() reports
3615
         * that with a special, recognizable error ENOANO. In this case, silently proceed, but only if exclusively
3616
         * sandboxing options were used, i.e. nothing such as RootDirectory= or BindMount= that would result in a
3617
         * completely different execution environment. */
3618
        if (r == -ENOANO) {
2,311✔
UNCOV
3619
                if (insist_on_sandboxing(
×
3620
                                    context,
3621
                                    root_dir, root_image,
3622
                                    bind_mounts,
3623
                                    n_bind_mounts))
UNCOV
3624
                        return log_exec_debug_errno(context,
×
3625
                                                    params,
3626
                                                    SYNTHETIC_ERRNO(EOPNOTSUPP),
3627
                                                    "Failed to set up namespace, and refusing to continue since "
3628
                                                    "the selected namespacing options alter mount environment non-trivially.\n"
3629
                                                    "Bind mounts: %zu, temporary filesystems: %zu, root directory: %s, root image: %s, dynamic user: %s",
3630
                                                    n_bind_mounts,
3631
                                                    context->n_temporary_filesystems,
3632
                                                    yes_no(root_dir),
3633
                                                    yes_no(root_image),
3634
                                                    yes_no(context->dynamic_user));
3635

UNCOV
3636
                log_exec_debug(context, params, "Failed to set up namespace, assuming containerized execution and ignoring.");
×
UNCOV
3637
                return 0;
×
3638
        }
3639

3640
        return r;
3641
}
3642

3643
static int apply_working_directory(
10,875✔
3644
                const ExecContext *context,
3645
                const ExecParameters *params,
3646
                ExecRuntime *runtime,
3647
                const char *pwent_home,
3648
                char * const *env) {
3649

3650
        const char *wd;
10,875✔
3651
        int r;
10,875✔
3652

3653
        assert(context);
10,875✔
3654
        assert(params);
10,875✔
3655
        assert(runtime);
10,875✔
3656

3657
        if (context->working_directory_home) {
10,875✔
3658
                /* Preferably use the data from $HOME, in case it was updated by a PAM module */
3659
                wd = strv_env_get(env, "HOME");
71✔
3660
                if (!wd) {
71✔
3661
                        /* If that's not available, use the data from the struct passwd entry: */
3662
                        if (!pwent_home)
1✔
3663
                                return -ENXIO;
3664

3665
                        wd = pwent_home;
3666
                }
3667
        } else
3668
                wd = empty_to_root(context->working_directory);
10,804✔
3669

3670
        if (params->flags & EXEC_APPLY_CHROOT)
10,875✔
3671
                r = RET_NERRNO(chdir(wd));
10,875✔
3672
        else {
UNCOV
3673
                _cleanup_close_ int dfd = -EBADF;
×
3674

UNCOV
3675
                r = chase(wd,
×
UNCOV
3676
                          runtime->ephemeral_copy ?: context->root_directory,
×
3677
                          CHASE_PREFIX_ROOT|CHASE_AT_RESOLVE_IN_ROOT,
3678
                          /* ret_path= */ NULL,
3679
                          &dfd);
3680
                if (r >= 0)
×
3681
                        r = RET_NERRNO(fchdir(dfd));
×
3682
        }
3683
        return context->working_directory_missing_ok ? 0 : r;
10,875✔
3684
}
3685

3686
static int apply_root_directory(
10,875✔
3687
                const ExecContext *context,
3688
                const ExecParameters *params,
3689
                ExecRuntime *runtime,
3690
                const bool needs_mount_ns,
3691
                int *exit_status) {
3692

3693
        assert(context);
10,875✔
3694
        assert(params);
10,875✔
3695
        assert(runtime);
10,875✔
3696
        assert(exit_status);
10,875✔
3697

3698
        if (params->flags & EXEC_APPLY_CHROOT)
10,875✔
3699
                if (!needs_mount_ns && context->root_directory)
10,875✔
UNCOV
3700
                        if (chroot(runtime->ephemeral_copy ?: context->root_directory) < 0) {
×
UNCOV
3701
                                *exit_status = EXIT_CHROOT;
×
UNCOV
3702
                                return -errno;
×
3703
                        }
3704

3705
        return 0;
3706
}
3707

3708
static int setup_keyring(
10,896✔
3709
                const ExecContext *context,
3710
                const ExecParameters *p,
3711
                uid_t uid,
3712
                gid_t gid) {
3713

3714
        key_serial_t keyring;
10,896✔
3715
        int r = 0;
10,896✔
3716
        uid_t saved_uid;
10,896✔
3717
        gid_t saved_gid;
10,896✔
3718

3719
        assert(context);
10,896✔
3720
        assert(p);
10,896✔
3721

3722
        /* Let's set up a new per-service "session" kernel keyring for each system service. This has the benefit that
3723
         * each service runs with its own keyring shared among all processes of the service, but with no hook-up beyond
3724
         * that scope, and in particular no link to the per-UID keyring. If we don't do this the keyring will be
3725
         * automatically created on-demand and then linked to the per-UID keyring, by the kernel. The kernel's built-in
3726
         * on-demand behaviour is very appropriate for login users, but probably not so much for system services, where
3727
         * UIDs are not necessarily specific to a service but reused (at least in the case of UID 0). */
3728

3729
        if (context->keyring_mode == EXEC_KEYRING_INHERIT)
10,896✔
3730
                return 0;
3731

3732
        /* Acquiring a reference to the user keyring is nasty. We briefly change identity in order to get things set up
3733
         * properly by the kernel. If we don't do that then we can't create it atomically, and that sucks for parallel
3734
         * execution. This mimics what pam_keyinit does, too. Setting up session keyring, to be owned by the right user
3735
         * & group is just as nasty as acquiring a reference to the user keyring. */
3736

3737
        saved_uid = getuid();
10,121✔
3738
        saved_gid = getgid();
10,121✔
3739

3740
        if (gid_is_valid(gid) && gid != saved_gid) {
10,121✔
3741
                if (setregid(gid, -1) < 0)
1,819✔
UNCOV
3742
                        return log_exec_error_errno(context,
×
3743
                                                    p,
3744
                                                    errno,
3745
                                                    "Failed to change GID for user keyring: %m");
3746
        }
3747

3748
        if (uid_is_valid(uid) && uid != saved_uid) {
10,121✔
3749
                if (setreuid(uid, -1) < 0) {
1,816✔
3750
                        r = log_exec_error_errno(context,
×
3751
                                                 p,
3752
                                                 errno,
3753
                                                 "Failed to change UID for user keyring: %m");
UNCOV
3754
                        goto out;
×
3755
                }
3756
        }
3757

3758
        keyring = keyctl(KEYCTL_JOIN_SESSION_KEYRING, 0, 0, 0, 0);
10,121✔
3759
        if (keyring == -1) {
10,121✔
UNCOV
3760
                if (errno == ENOSYS)
×
3761
                        log_exec_debug_errno(context,
×
3762
                                             p,
3763
                                             errno,
3764
                                             "Kernel keyring not supported, ignoring.");
UNCOV
3765
                else if (ERRNO_IS_PRIVILEGE(errno))
×
UNCOV
3766
                        log_exec_debug_errno(context,
×
3767
                                             p,
3768
                                             errno,
3769
                                             "Kernel keyring access prohibited, ignoring.");
3770
                else if (errno == EDQUOT)
×
UNCOV
3771
                        log_exec_debug_errno(context,
×
3772
                                             p,
3773
                                             errno,
3774
                                             "Out of kernel keyrings to allocate, ignoring.");
3775
                else
UNCOV
3776
                        r = log_exec_error_errno(context,
×
3777
                                                 p,
3778
                                                 errno,
3779
                                                 "Setting up kernel keyring failed: %m");
3780

3781
                goto out;
×
3782
        }
3783

3784
        /* When requested link the user keyring into the session keyring. */
3785
        if (context->keyring_mode == EXEC_KEYRING_SHARED) {
10,121✔
3786

3787
                if (keyctl(KEYCTL_LINK,
1,051✔
3788
                           KEY_SPEC_USER_KEYRING,
3789
                           KEY_SPEC_SESSION_KEYRING, 0, 0) < 0) {
UNCOV
3790
                        r = log_exec_error_errno(context,
×
3791
                                                 p,
3792
                                                 errno,
3793
                                                 "Failed to link user keyring into session keyring: %m");
UNCOV
3794
                        goto out;
×
3795
                }
3796
        }
3797

3798
        /* Restore uid/gid back */
3799
        if (uid_is_valid(uid) && uid != saved_uid) {
10,121✔
3800
                if (setreuid(saved_uid, -1) < 0) {
1,816✔
UNCOV
3801
                        r = log_exec_error_errno(context,
×
3802
                                                 p,
3803
                                                 errno,
3804
                                                 "Failed to change UID back for user keyring: %m");
UNCOV
3805
                        goto out;
×
3806
                }
3807
        }
3808

3809
        if (gid_is_valid(gid) && gid != saved_gid) {
10,121✔
3810
                if (setregid(saved_gid, -1) < 0)
1,819✔
UNCOV
3811
                        return log_exec_error_errno(context,
×
3812
                                                    p,
3813
                                                    errno,
3814
                                                    "Failed to change GID back for user keyring: %m");
3815
        }
3816

3817
        /* Populate they keyring with the invocation ID by default, as original saved_uid. */
3818
        if (!sd_id128_is_null(p->invocation_id)) {
10,121✔
3819
                key_serial_t key;
10,121✔
3820

3821
                key = add_key("user",
20,242✔
3822
                              "invocation_id",
3823
                              &p->invocation_id,
10,121✔
3824
                              sizeof(p->invocation_id),
3825
                              KEY_SPEC_SESSION_KEYRING);
3826
                if (key == -1)
10,121✔
UNCOV
3827
                        log_exec_debug_errno(context,
×
3828
                                             p,
3829
                                             errno,
3830
                                             "Failed to add invocation ID to keyring, ignoring: %m");
3831
                else {
3832
                        if (keyctl(KEYCTL_SETPERM, key,
10,121✔
3833
                                   KEY_POS_VIEW|KEY_POS_READ|KEY_POS_SEARCH|
3834
                                   KEY_USR_VIEW|KEY_USR_READ|KEY_USR_SEARCH, 0, 0) < 0)
UNCOV
3835
                                r = log_exec_error_errno(context,
×
3836
                                                         p,
3837
                                                         errno,
3838
                                                         "Failed to restrict invocation ID permission: %m");
3839
                }
3840
        }
3841

3842
out:
10,121✔
3843
        /* Revert back uid & gid for the last time, and exit */
3844
        /* no extra logging, as only the first already reported error matters */
3845
        if (getuid() != saved_uid)
10,121✔
UNCOV
3846
                (void) setreuid(saved_uid, -1);
×
3847

3848
        if (getgid() != saved_gid)
10,121✔
UNCOV
3849
                (void) setregid(saved_gid, -1);
×
3850

3851
        return r;
3852
}
3853

3854
static void append_socket_pair(int *array, size_t *n, const int pair[static 2]) {
40,045✔
3855
        assert(array);
40,045✔
3856
        assert(n);
40,045✔
3857
        assert(pair);
40,045✔
3858

3859
        if (pair[0] >= 0)
40,045✔
3860
                array[(*n)++] = pair[0];
181✔
3861
        if (pair[1] >= 0)
40,045✔
3862
                array[(*n)++] = pair[1];
181✔
3863
}
40,045✔
3864

3865
static int close_remaining_fds(
13,307✔
3866
                const ExecParameters *params,
3867
                const ExecRuntime *runtime,
3868
                int socket_fd,
3869
                const int *fds,
3870
                size_t n_fds) {
13,307✔
3871

3872
        size_t n_dont_close = 0;
13,307✔
3873
        int dont_close[n_fds + 17];
13,307✔
3874

3875
        assert(params);
13,307✔
3876
        assert(runtime);
13,307✔
3877

3878
        if (params->stdin_fd >= 0)
13,307✔
3879
                dont_close[n_dont_close++] = params->stdin_fd;
471✔
3880
        if (params->stdout_fd >= 0)
13,307✔
3881
                dont_close[n_dont_close++] = params->stdout_fd;
471✔
3882
        if (params->stderr_fd >= 0)
13,307✔
3883
                dont_close[n_dont_close++] = params->stderr_fd;
471✔
3884

3885
        if (socket_fd >= 0)
13,307✔
3886
                dont_close[n_dont_close++] = socket_fd;
17✔
3887
        if (n_fds > 0) {
13,307✔
3888
                memcpy(dont_close + n_dont_close, fds, sizeof(int) * n_fds);
13,307✔
3889
                n_dont_close += n_fds;
13,307✔
3890
        }
3891

3892
        append_socket_pair(dont_close, &n_dont_close, runtime->ephemeral_storage_socket);
13,307✔
3893

3894
        if (runtime->shared) {
13,307✔
3895
                append_socket_pair(dont_close, &n_dont_close, runtime->shared->netns_storage_socket);
13,307✔
3896
                append_socket_pair(dont_close, &n_dont_close, runtime->shared->ipcns_storage_socket);
13,307✔
3897
        }
3898

3899
        if (runtime->dynamic_creds) {
13,307✔
3900
                if (runtime->dynamic_creds->user)
13,307✔
3901
                        append_socket_pair(dont_close, &n_dont_close, runtime->dynamic_creds->user->storage_socket);
62✔
3902
                if (runtime->dynamic_creds->group)
13,307✔
3903
                        append_socket_pair(dont_close, &n_dont_close, runtime->dynamic_creds->group->storage_socket);
62✔
3904
        }
3905

3906
        if (params->user_lookup_fd >= 0)
13,307✔
3907
                dont_close[n_dont_close++] = params->user_lookup_fd;
13,307✔
3908

3909
        if (params->handoff_timestamp_fd >= 0)
13,307✔
3910
                dont_close[n_dont_close++] = params->handoff_timestamp_fd;
13,307✔
3911

3912
        if (params->pidref_transport_fd >= 0)
13,307✔
3913
                dont_close[n_dont_close++] = params->pidref_transport_fd;
12,175✔
3914

3915
        assert(n_dont_close <= ELEMENTSOF(dont_close));
13,307✔
3916

3917
        return close_all_fds(dont_close, n_dont_close);
13,307✔
3918
}
3919

3920
static int send_user_lookup(
13,305✔
3921
                const char *unit_id,
3922
                int user_lookup_fd,
3923
                uid_t uid,
3924
                gid_t gid) {
3925

3926
        assert(unit_id);
13,305✔
3927

3928
        /* Send the resolved UID/GID to PID 1 after we learnt it. We send a single datagram, containing the UID/GID
3929
         * data as well as the unit name. Note that we suppress sending this if no user/group to resolve was
3930
         * specified. */
3931

3932
        if (user_lookup_fd < 0)
13,305✔
3933
                return 0;
3934

3935
        if (!uid_is_valid(uid) && !gid_is_valid(gid))
13,305✔
3936
                return 0;
3937

3938
        if (writev(user_lookup_fd,
2,718✔
3939
               (struct iovec[]) {
5,436✔
3940
                           IOVEC_MAKE(&uid, sizeof(uid)),
3941
                           IOVEC_MAKE(&gid, sizeof(gid)),
3942
                           IOVEC_MAKE_STRING(unit_id) }, 3) < 0)
5,436✔
3943
                return -errno;
×
3944

3945
        return 0;
2,718✔
3946
}
3947

3948
static int acquire_home(const ExecContext *c, const char **home, char **ret_buf) {
13,305✔
3949
        int r;
13,305✔
3950

3951
        assert(c);
13,305✔
3952
        assert(home);
13,305✔
3953
        assert(ret_buf);
13,305✔
3954

3955
        /* If WorkingDirectory=~ is set, try to acquire a usable home directory. */
3956

3957
        if (*home) /* Already acquired from get_fixed_user()? */
13,305✔
3958
                return 0;
3959

3960
        if (!c->working_directory_home)
10,654✔
3961
                return 0;
3962

UNCOV
3963
        if (c->dynamic_user || (c->user && is_this_me(c->user) <= 0))
×
UNCOV
3964
                return -EADDRNOTAVAIL;
×
3965

UNCOV
3966
        r = get_home_dir(ret_buf);
×
UNCOV
3967
        if (r < 0)
×
3968
                return r;
3969

UNCOV
3970
        *home = *ret_buf;
×
UNCOV
3971
        return 1;
×
3972
}
3973

3974
static int compile_suggested_paths(const ExecContext *c, const ExecParameters *p, char ***ret) {
62✔
3975
        _cleanup_strv_free_ char ** list = NULL;
62✔
3976
        int r;
62✔
3977

3978
        assert(c);
62✔
3979
        assert(p);
62✔
3980
        assert(ret);
62✔
3981

3982
        assert(c->dynamic_user);
62✔
3983

3984
        /* Compile a list of paths that it might make sense to read the owning UID from to use as initial candidate for
3985
         * dynamic UID allocation, in order to save us from doing costly recursive chown()s of the special
3986
         * directories. */
3987

3988
        for (ExecDirectoryType t = 0; t < _EXEC_DIRECTORY_TYPE_MAX; t++) {
372✔
3989

3990
                if (!EXEC_DIRECTORY_TYPE_SHALL_CHOWN(t))
310✔
3991
                        continue;
62✔
3992

3993
                if (!p->prefix[t])
248✔
UNCOV
3994
                        continue;
×
3995

3996
                for (size_t i = 0; i < c->directories[t].n_items; i++) {
263✔
3997
                        char *e;
15✔
3998

3999
                        if (exec_directory_is_private(c, t))
15✔
4000
                                e = path_join(p->prefix[t], "private", c->directories[t].items[i].path);
13✔
4001
                        else
4002
                                e = path_join(p->prefix[t], c->directories[t].items[i].path);
2✔
4003
                        if (!e)
15✔
4004
                                return -ENOMEM;
4005

4006
                        r = strv_consume(&list, e);
15✔
4007
                        if (r < 0)
15✔
4008
                                return r;
4009
                }
4010
        }
4011

4012
        *ret = TAKE_PTR(list);
62✔
4013

4014
        return 0;
62✔
4015
}
4016

4017
static int exec_context_cpu_affinity_from_numa(const ExecContext *c, CPUSet *ret) {
2✔
4018
        _cleanup_(cpu_set_reset) CPUSet s = {};
2✔
4019
        int r;
2✔
4020

4021
        assert(c);
2✔
4022
        assert(ret);
2✔
4023

4024
        if (!c->numa_policy.nodes.set) {
2✔
UNCOV
4025
                log_debug("Can't derive CPU affinity mask from NUMA mask because NUMA mask is not set, ignoring");
×
UNCOV
4026
                return 0;
×
4027
        }
4028

4029
        r = numa_to_cpu_set(&c->numa_policy, &s);
2✔
4030
        if (r < 0)
2✔
4031
                return r;
4032

4033
        cpu_set_reset(ret);
2✔
4034

4035
        return cpu_set_add_all(ret, &s);
2✔
4036
}
4037

4038
static int add_shifted_fd(int *fds, size_t fds_size, size_t *n_fds, int *fd) {
50,796✔
4039
        int r;
50,796✔
4040

4041
        assert(fds);
50,796✔
4042
        assert(n_fds);
50,796✔
4043
        assert(*n_fds < fds_size);
50,796✔
4044
        assert(fd);
50,796✔
4045

4046
        if (*fd < 0)
50,796✔
4047
               return 0;
50,796✔
4048

4049
        if (*fd < 3 + (int) *n_fds) {
24,531✔
4050
                /* Let's move the fd up, so that it's outside of the fd range we will use to store
4051
                 * the fds we pass to the process (or which are closed only during execve). */
4052

4053
                r = fcntl(*fd, F_DUPFD_CLOEXEC, 3 + (int) *n_fds);
10,875✔
4054
                if (r < 0)
10,875✔
UNCOV
4055
                        return -errno;
×
4056

4057
                close_and_replace(*fd, r);
10,875✔
4058
        }
4059

4060
        fds[(*n_fds)++] = *fd;
24,531✔
4061
        return 1;
24,531✔
4062
}
4063

4064
static int connect_unix_harder(const ExecContext *c, const ExecParameters *p, const OpenFile *of, int ofd) {
1✔
4065
        static const int socket_types[] = { SOCK_DGRAM, SOCK_STREAM, SOCK_SEQPACKET };
1✔
4066

4067
        union sockaddr_union addr = {
1✔
4068
                .un.sun_family = AF_UNIX,
4069
        };
4070
        socklen_t sa_len;
1✔
4071
        int r;
1✔
4072

4073
        assert(c);
1✔
4074
        assert(p);
1✔
4075
        assert(of);
1✔
4076
        assert(ofd >= 0);
1✔
4077

4078
        r = sockaddr_un_set_path(&addr.un, FORMAT_PROC_FD_PATH(ofd));
1✔
4079
        if (r < 0)
1✔
UNCOV
4080
                return log_exec_debug_errno(c, p, r, "Failed to set sockaddr for '%s': %m", of->path);
×
4081
        sa_len = r;
1✔
4082

4083
        FOREACH_ELEMENT(i, socket_types) {
2✔
4084
                _cleanup_close_ int fd = -EBADF;
2✔
4085

4086
                fd = socket(AF_UNIX, *i|SOCK_CLOEXEC, 0);
2✔
4087
                if (fd < 0)
2✔
UNCOV
4088
                        return log_exec_debug_errno(c, p,
×
4089
                                                    errno, "Failed to create socket for '%s': %m",
4090
                                                    of->path);
4091

4092
                r = RET_NERRNO(connect(fd, &addr.sa, sa_len));
2✔
4093
                if (r >= 0)
1✔
4094
                        return TAKE_FD(fd);
1✔
4095
                if (r != -EPROTOTYPE)
1✔
UNCOV
4096
                        return log_exec_debug_errno(c, p,
×
4097
                                                    r, "Failed to connect to socket for '%s': %m",
4098
                                                    of->path);
4099
        }
4100

UNCOV
4101
        return log_exec_debug_errno(c, p,
×
4102
                                    SYNTHETIC_ERRNO(EPROTOTYPE), "No suitable socket type to connect to socket '%s'.",
4103
                                    of->path);
4104
}
4105

4106
static int get_open_file_fd(const ExecContext *c, const ExecParameters *p, const OpenFile *of) {
5✔
4107
        _cleanup_close_ int fd = -EBADF, ofd = -EBADF;
5✔
4108
        struct stat st;
5✔
4109

4110
        assert(c);
5✔
4111
        assert(p);
5✔
4112
        assert(of);
5✔
4113

4114
        ofd = open(of->path, O_PATH | O_CLOEXEC);
5✔
4115
        if (ofd < 0)
5✔
4116
                return log_exec_debug_errno(c, p, errno, "Failed to open '%s' as O_PATH: %m", of->path);
6✔
4117

4118
        if (fstat(ofd, &st) < 0)
3✔
UNCOV
4119
                return log_exec_debug_errno(c, p, errno, "Failed to stat '%s': %m", of->path);
×
4120

4121
        if (S_ISSOCK(st.st_mode)) {
3✔
4122
                fd = connect_unix_harder(c, p, of, ofd);
1✔
4123
                if (fd < 0)
1✔
4124
                        return fd;
4125

4126
                if (FLAGS_SET(of->flags, OPENFILE_READ_ONLY) && shutdown(fd, SHUT_WR) < 0)
1✔
UNCOV
4127
                        return log_exec_debug_errno(c, p,
×
4128
                                                    errno, "Failed to shutdown send for socket '%s': %m",
4129
                                                    of->path);
4130

4131
                log_exec_debug(c, p, "Opened socket '%s' as fd %d.", of->path, fd);
3✔
4132
        } else {
4133
                int flags = FLAGS_SET(of->flags, OPENFILE_READ_ONLY) ? O_RDONLY : O_RDWR;
2✔
4134
                if (FLAGS_SET(of->flags, OPENFILE_APPEND))
2✔
UNCOV
4135
                        flags |= O_APPEND;
×
4136
                else if (FLAGS_SET(of->flags, OPENFILE_TRUNCATE))
2✔
UNCOV
4137
                        flags |= O_TRUNC;
×
4138

4139
                fd = fd_reopen(ofd, flags|O_NOCTTY|O_CLOEXEC);
2✔
4140
                if (fd < 0)
2✔
UNCOV
4141
                        return log_exec_debug_errno(c, p, fd, "Failed to reopen file '%s': %m", of->path);
×
4142

4143
                log_exec_debug(c, p, "Opened file '%s' as fd %d.", of->path, fd);
6✔
4144
        }
4145

4146
        return TAKE_FD(fd);
4147
}
4148

4149
static int collect_open_file_fds(const ExecContext *c, ExecParameters *p, size_t *n_fds) {
13,308✔
4150
        assert(c);
13,308✔
4151
        assert(p);
13,308✔
4152
        assert(n_fds);
13,308✔
4153

4154
        LIST_FOREACH(open_files, of, p->open_files) {
13,308✔
4155
                _cleanup_close_ int fd = -EBADF;
13,313✔
4156

4157
                fd = get_open_file_fd(c, p, of);
5✔
4158
                if (fd < 0) {
5✔
4159
                        if (FLAGS_SET(of->flags, OPENFILE_GRACEFUL)) {
2✔
4160
                                log_exec_full_errno(c, p,
3✔
4161
                                                    fd == -ENOENT || ERRNO_IS_NEG_PRIVILEGE(fd) ? LOG_DEBUG : LOG_WARNING,
4162
                                                    fd,
4163
                                                    "Failed to get OpenFile= file descriptor for '%s', ignoring: %m",
4164
                                                    of->path);
4165
                                continue;
1✔
4166
                        }
4167

4168
                        return log_exec_error_errno(c, p, fd,
3✔
4169
                                                    "Failed to get OpenFile= file descriptor for '%s': %m",
4170
                                                    of->path);
4171
                }
4172

4173
                if (!GREEDY_REALLOC(p->fds, *n_fds + 1))
3✔
UNCOV
4174
                        return log_oom();
×
4175

4176
                if (strv_extend(&p->fd_names, of->fdname) < 0)
3✔
UNCOV
4177
                        return log_oom();
×
4178

4179
                p->fds[(*n_fds)++] = TAKE_FD(fd);
3✔
4180
        }
4181

4182
        return 0;
4183
}
4184

4185
static void log_command_line(
10,874✔
4186
                const ExecContext *context,
4187
                const ExecParameters *params,
4188
                const char *msg,
4189
                const char *executable,
4190
                char **argv) {
4191

4192
        assert(context);
10,874✔
4193
        assert(params);
10,874✔
4194
        assert(msg);
10,874✔
4195
        assert(executable);
10,874✔
4196

4197
        if (!DEBUG_LOGGING)
10,874✔
4198
                return;
10,874✔
4199

4200
        _cleanup_free_ char *cmdline = quote_command_line(argv, SHELL_ESCAPE_EMPTY);
21,146✔
4201

4202
        log_exec_struct(context, params, LOG_DEBUG,
40,390✔
4203
                        "EXECUTABLE=%s", executable,
4204
                        LOG_EXEC_MESSAGE(params, "%s: %s", msg, strnull(cmdline)),
4205
                        LOG_EXEC_INVOCATION_ID(params));
4206
}
4207

4208
static bool exec_context_need_unprivileged_private_users(
59,146✔
4209
                const ExecContext *context,
4210
                const ExecParameters *params) {
4211

4212
        assert(context);
59,146✔
4213
        assert(params);
59,146✔
4214

4215
        /* These options require PrivateUsers= when used in user units, as we need to be in a user namespace
4216
         * to have permission to enable them when not running as root. If we have effective CAP_SYS_ADMIN
4217
         * (system manager) then we have privileges and don't need this. */
4218
        if (params->runtime_scope != RUNTIME_SCOPE_USER)
59,146✔
4219
                return false;
4220

4221
        return context->private_users != PRIVATE_USERS_NO ||
3,232✔
4222
               context->private_tmp != PRIVATE_TMP_NO ||
4223
               context->private_devices ||
3,217✔
4224
               context->private_network ||
3,178✔
4225
               context->network_namespace_path ||
3,178✔
4226
               context->private_ipc ||
3,178✔
4227
               context->ipc_namespace_path ||
3,178✔
4228
               context->private_mounts > 0 ||
3,178✔
4229
               context->mount_apivfs > 0 ||
3,178✔
4230
               context->bind_log_sockets > 0 ||
3,178✔
4231
               context->n_bind_mounts > 0 ||
3,178✔
4232
               context->n_temporary_filesystems > 0 ||
3,171✔
4233
               context->root_directory ||
3,171✔
4234
               !strv_isempty(context->extension_directories) ||
3,171✔
4235
               context->protect_system != PROTECT_SYSTEM_NO ||
3,171✔
4236
               context->protect_home != PROTECT_HOME_NO ||
3,150✔
4237
               exec_needs_pid_namespace(context) ||
3,150✔
4238
               context->protect_kernel_tunables ||
4239
               context->protect_kernel_modules ||
3,123✔
4240
               context->protect_kernel_logs ||
3,109✔
4241
               exec_needs_cgroup_mount(context, params) ||
3,109✔
4242
               context->protect_clock ||
3,109✔
4243
               context->protect_hostname != PROTECT_HOSTNAME_NO ||
3,102✔
4244
               !strv_isempty(context->read_write_paths) ||
3,095✔
4245
               !strv_isempty(context->read_only_paths) ||
3,074✔
4246
               !strv_isempty(context->inaccessible_paths) ||
3,074✔
4247
               !strv_isempty(context->exec_paths) ||
3,074✔
4248
               !strv_isempty(context->no_exec_paths) ||
9,523✔
4249
               context->delegate_namespaces != NAMESPACE_FLAGS_INITIAL;
3,074✔
4250
}
4251

4252
static PrivateUsers exec_context_get_effective_private_users(
10,880✔
4253
                const ExecContext *context,
4254
                const ExecParameters *params) {
4255

4256
        assert(context);
10,880✔
4257
        assert(params);
10,880✔
4258

4259
        if (context->private_users != PRIVATE_USERS_NO)
10,880✔
4260
                return context->private_users;
4261

4262
        if (exec_context_need_unprivileged_private_users(context, params))
10,866✔
4263
                return PRIVATE_USERS_SELF;
4264

4265
        /* If any namespace is delegated with DelegateNamespaces=, always set up a user namespace. */
4266
        if (context->delegate_namespaces != NAMESPACE_FLAGS_INITIAL)
10,845✔
UNCOV
4267
                return PRIVATE_USERS_SELF;
×
4268

4269
        return PRIVATE_USERS_NO;
4270
}
4271

4272
static bool exec_namespace_is_delegated(
26,508✔
4273
                const ExecContext *context,
4274
                const ExecParameters *params,
4275
                unsigned long namespace) {
4276

4277
        assert(context);
26,508✔
4278
        assert(params);
26,508✔
4279
        assert(namespace != CLONE_NEWUSER);
26,508✔
4280

4281
        /* If we need unprivileged private users, we've already unshared a user namespace by the time we call
4282
         * setup_delegated_namespaces() for the first time so let's make sure we do all other namespace
4283
         * unsharing in the first call to setup_delegated_namespaces() by returning false here. */
4284
        if (exec_context_need_unprivileged_private_users(context, params))
26,508✔
4285
                return false;
4286

4287
        if (context->delegate_namespaces == NAMESPACE_FLAGS_INITIAL)
26,415✔
4288
                return false;
4289

UNCOV
4290
        return FLAGS_SET(context->delegate_namespaces, namespace);
×
4291
}
4292

4293
static int setup_delegated_namespaces(
21,772✔
4294
                const ExecContext *context,
4295
                ExecParameters *params,
4296
                ExecRuntime *runtime,
4297
                bool delegate,
4298
                const char *memory_pressure_path,
4299
                uid_t uid,
4300
                uid_t gid,
4301
                const ExecCommand *command,
4302
                bool needs_sandboxing,
4303
                bool has_cap_sys_admin,
4304
                int *reterr_exit_status) {
4305

4306
        int r;
21,772✔
4307

4308
        /* This function is called twice, once before unsharing the user namespace, and once after unsharing
4309
         * the user namespace. When called before unsharing the user namespace, "delegate" is set to "false".
4310
         * When called after unsharing the user namespace, "delegate" is set to "true". The net effect is
4311
         * that all namespaces that should not be delegated are unshared when this function is called the
4312
         * first time and all namespaces that should be delegated are unshared when this function is called
4313
         * the second time. */
4314

4315
        assert(context);
21,772✔
4316
        assert(params);
21,772✔
4317
        assert(runtime);
21,772✔
4318
        assert(reterr_exit_status);
21,772✔
4319

4320
        if (exec_needs_network_namespace(context) &&
21,881✔
4321
            exec_namespace_is_delegated(context, params, CLONE_NEWNET) == delegate &&
109✔
4322
            runtime->shared && runtime->shared->netns_storage_socket[0] >= 0) {
55✔
4323

4324
                /* Try to enable network namespacing if network namespacing is available and we have
4325
                 * CAP_NET_ADMIN in the current user namespace (either the system manager one or the unit's
4326
                 * own user namespace). We need CAP_NET_ADMIN to be able to configure the loopback device in
4327
                 * the new network namespace. And if we don't have that, then we could only create a network
4328
                 * namespace without the ability to set up "lo". Hence gracefully skip things then. */
4329
                if (ns_type_supported(NAMESPACE_NET) && have_effective_cap(CAP_NET_ADMIN) > 0) {
55✔
4330
                        r = setup_shareable_ns(runtime->shared->netns_storage_socket, CLONE_NEWNET);
55✔
4331
                        if (ERRNO_IS_NEG_PRIVILEGE(r))
55✔
UNCOV
4332
                                log_exec_notice_errno(context, params, r,
×
4333
                                                      "PrivateNetwork=yes is configured, but network namespace setup not permitted, proceeding without: %m");
4334
                        else if (r < 0) {
55✔
UNCOV
4335
                                *reterr_exit_status = EXIT_NETWORK;
×
UNCOV
4336
                                return log_exec_error_errno(context, params, r, "Failed to set up network namespacing: %m");
×
4337
                        } else
4338
                                log_exec_debug(context, params, "Set up %snetwork namespace", delegate ? "delegated " : "");
218✔
UNCOV
4339
                } else if (context->network_namespace_path) {
×
UNCOV
4340
                        *reterr_exit_status = EXIT_NETWORK;
×
4341
                        return log_exec_error_errno(context, params, SYNTHETIC_ERRNO(EOPNOTSUPP),
×
4342
                                                    "NetworkNamespacePath= is not supported, refusing.");
4343
                } else
UNCOV
4344
                        log_exec_notice(context, params, "PrivateNetwork=yes is configured, but the kernel does not support or we lack privileges for network namespace, proceeding without.");
×
4345
        }
4346

4347
        if (exec_needs_ipc_namespace(context) &&
21,775✔
4348
            exec_namespace_is_delegated(context, params, CLONE_NEWIPC) == delegate &&
3✔
4349
            runtime->shared && runtime->shared->ipcns_storage_socket[0] >= 0) {
2✔
4350

4351
                if (ns_type_supported(NAMESPACE_IPC)) {
2✔
4352
                        r = setup_shareable_ns(runtime->shared->ipcns_storage_socket, CLONE_NEWIPC);
2✔
4353
                        if (ERRNO_IS_NEG_PRIVILEGE(r))
2✔
UNCOV
4354
                                log_exec_warning_errno(context, params, r,
×
4355
                                                       "PrivateIPC=yes is configured, but IPC namespace setup failed, ignoring: %m");
4356
                        else if (r < 0) {
2✔
UNCOV
4357
                                *reterr_exit_status = EXIT_NAMESPACE;
×
UNCOV
4358
                                return log_exec_error_errno(context, params, r, "Failed to set up IPC namespacing: %m");
×
4359
                        } else
4360
                                log_exec_debug(context, params, "Set up %sIPC namespace", delegate ? "delegated " : "");
6✔
UNCOV
4361
                } else if (context->ipc_namespace_path) {
×
UNCOV
4362
                        *reterr_exit_status = EXIT_NAMESPACE;
×
UNCOV
4363
                        return log_exec_error_errno(context, params, SYNTHETIC_ERRNO(EOPNOTSUPP),
×
4364
                                                    "IPCNamespacePath= is not supported, refusing.");
4365
                } else
UNCOV
4366
                        log_exec_warning(context, params, "PrivateIPC=yes is configured, but the kernel does not support IPC namespaces, ignoring.");
×
4367
        }
4368

4369
        if (needs_sandboxing && exec_needs_cgroup_namespace(context, params) &&
21,789✔
4370
            exec_namespace_is_delegated(context, params, CLONE_NEWCGROUP) == delegate) {
17✔
4371
                if (unshare(CLONE_NEWCGROUP) < 0) {
9✔
UNCOV
4372
                        *reterr_exit_status = EXIT_NAMESPACE;
×
UNCOV
4373
                        return log_exec_error_errno(context, params, errno, "Failed to set up cgroup namespacing: %m");
×
4374
                }
4375

4376
                log_exec_debug(context, params, "Set up %scgroup namespace", delegate ? "delegated " : "");
36✔
4377
        }
4378

4379
        /* Unshare a new PID namespace before setting up mounts to ensure /proc/ is mounted with only processes in PID namespace visible.
4380
         * Note PrivatePIDs=yes implies MountAPIVFS=yes so we'll always ensure procfs is remounted. */
4381
        if (needs_sandboxing && exec_needs_pid_namespace(context) &&
21,788✔
4382
            exec_namespace_is_delegated(context, params, CLONE_NEWPID) == delegate) {
16✔
4383
                if (params->pidref_transport_fd < 0) {
10✔
UNCOV
4384
                        *reterr_exit_status = EXIT_NAMESPACE;
×
UNCOV
4385
                        return log_exec_error_errno(context, params, SYNTHETIC_ERRNO(ENOTCONN), "PidRef socket is not set up: %m");
×
4386
                }
4387

4388
                /* If we had CAP_SYS_ADMIN prior to joining the user namespace, then we are privileged and don't need
4389
                 * to check if we can mount /proc/.
4390
                 *
4391
                 * We need to check prior to entering the user namespace because if we're running unprivileged or in a
4392
                 * system without CAP_SYS_ADMIN, then we can have CAP_SYS_ADMIN in the current user namespace but not
4393
                 * once we unshare a mount namespace. */
4394
                if (!has_cap_sys_admin) {
10✔
4395
                        r = can_mount_proc(context, params);
5✔
4396
                        if (r < 0) {
3✔
UNCOV
4397
                                *reterr_exit_status = EXIT_NAMESPACE;
×
UNCOV
4398
                                return log_exec_error_errno(context, params, r, "Failed to detect if /proc/ can be remounted: %m");
×
4399
                        }
4400
                        if (r == 0) {
3✔
4401
                                *reterr_exit_status = EXIT_NAMESPACE;
1✔
4402
                                return log_exec_error_errno(context, params, SYNTHETIC_ERRNO(EPERM),
1✔
4403
                                                            "PrivatePIDs=yes is configured, but /proc/ cannot be re-mounted due to lack of privileges, refusing.");
4404
                        }
4405
                }
4406

4407
                r = setup_private_pids(context, params);
7✔
4408
                if (r < 0) {
6✔
UNCOV
4409
                        *reterr_exit_status = EXIT_NAMESPACE;
×
UNCOV
4410
                        return log_exec_error_errno(context, params, r, "Failed to set up pid namespace: %m");
×
4411
                }
4412

4413
                log_exec_debug(context, params, "Set up %spid namespace", delegate ? "delegated " : "");
20✔
4414
        }
4415

4416
        /* If PrivatePIDs= yes is configured, we're now running as pid 1 in a pid namespace! */
4417

4418
        if (exec_needs_mount_namespace(context, params, runtime) &&
26,378✔
4419
            exec_namespace_is_delegated(context, params, CLONE_NEWNS) == delegate) {
4,610✔
4420
                _cleanup_free_ char *error_path = NULL;
2,313✔
4421

4422
                r = apply_mount_namespace(command->flags,
2,313✔
4423
                                          context,
4424
                                          params,
4425
                                          runtime,
4426
                                          memory_pressure_path,
4427
                                          needs_sandboxing,
4428
                                          &error_path,
4429
                                          uid,
4430
                                          gid);
4431
                if (r < 0) {
2,313✔
4432
                        *reterr_exit_status = EXIT_NAMESPACE;
15✔
4433
                        return log_exec_error_errno(context, params, r, "Failed to set up mount namespacing%s%s: %m",
59✔
4434
                                                    error_path ? ": " : "", strempty(error_path));
4435
                }
4436

4437
                log_exec_debug(context, params, "Set up %smount namespace", delegate ? "delegated " : "");
9,144✔
4438
        }
4439

4440
        if (needs_sandboxing && exec_namespace_is_delegated(context, params, CLONE_NEWUTS) == delegate) {
21,753✔
4441
                r = apply_protect_hostname(context, params, reterr_exit_status);
10,877✔
4442
                if (r < 0)
10,877✔
4443
                        return r;
4444

4445
                log_exec_debug(context, params, "Set up %sUTS namespace", delegate ? "delegated " : "");
42,228✔
4446
        }
4447

4448
        return 0;
4449
}
4450

UNCOV
4451
static bool exec_context_shall_confirm_spawn(const ExecContext *context) {
×
UNCOV
4452
        assert(context);
×
4453

UNCOV
4454
        if (confirm_spawn_disabled())
×
4455
                return false;
4456

4457
        /* For some reasons units remaining in the same process group
4458
         * as PID 1 fail to acquire the console even if it's not used
4459
         * by any process. So skip the confirmation question for them. */
UNCOV
4460
        return !context->same_pgrp;
×
4461
}
4462

4463
static int exec_context_named_iofds(
13,308✔
4464
                const ExecContext *c,
4465
                const ExecParameters *p,
4466
                int named_iofds[static 3]) {
4467

4468
        size_t targets;
13,308✔
4469
        const char* stdio_fdname[3];
13,308✔
4470
        size_t n_fds;
13,308✔
4471

4472
        assert(c);
13,308✔
4473
        assert(p);
13,308✔
4474
        assert(named_iofds);
13,308✔
4475

4476
        targets = (c->std_input == EXEC_INPUT_NAMED_FD) +
13,308✔
4477
                  (c->std_output == EXEC_OUTPUT_NAMED_FD) +
13,308✔
4478
                  (c->std_error == EXEC_OUTPUT_NAMED_FD);
13,308✔
4479

4480
        for (size_t i = 0; i < 3; i++)
53,232✔
4481
                stdio_fdname[i] = exec_context_fdname(c, i);
39,924✔
4482

4483
        n_fds = p->n_storage_fds + p->n_socket_fds + p->n_extra_fds;
13,308✔
4484

4485
        for (size_t i = 0; i < n_fds  && targets > 0; i++)
13,308✔
UNCOV
4486
                if (named_iofds[STDIN_FILENO] < 0 &&
×
4487
                    c->std_input == EXEC_INPUT_NAMED_FD &&
×
4488
                    stdio_fdname[STDIN_FILENO] &&
×
UNCOV
4489
                    streq(p->fd_names[i], stdio_fdname[STDIN_FILENO])) {
×
4490

UNCOV
4491
                        named_iofds[STDIN_FILENO] = p->fds[i];
×
UNCOV
4492
                        targets--;
×
4493

UNCOV
4494
                } else if (named_iofds[STDOUT_FILENO] < 0 &&
×
UNCOV
4495
                           c->std_output == EXEC_OUTPUT_NAMED_FD &&
×
UNCOV
4496
                           stdio_fdname[STDOUT_FILENO] &&
×
UNCOV
4497
                           streq(p->fd_names[i], stdio_fdname[STDOUT_FILENO])) {
×
4498

UNCOV
4499
                        named_iofds[STDOUT_FILENO] = p->fds[i];
×
UNCOV
4500
                        targets--;
×
4501

UNCOV
4502
                } else if (named_iofds[STDERR_FILENO] < 0 &&
×
UNCOV
4503
                           c->std_error == EXEC_OUTPUT_NAMED_FD &&
×
UNCOV
4504
                           stdio_fdname[STDERR_FILENO] &&
×
UNCOV
4505
                           streq(p->fd_names[i], stdio_fdname[STDERR_FILENO])) {
×
4506

UNCOV
4507
                        named_iofds[STDERR_FILENO] = p->fds[i];
×
UNCOV
4508
                        targets--;
×
4509
                }
4510

4511
        return targets == 0 ? 0 : -ENOENT;
13,308✔
4512
}
4513

4514
static void exec_shared_runtime_close(ExecSharedRuntime *shared) {
10,875✔
4515
        if (!shared)
10,875✔
4516
                return;
4517

4518
        safe_close_pair(shared->netns_storage_socket);
10,875✔
4519
        safe_close_pair(shared->ipcns_storage_socket);
10,875✔
4520
}
4521

4522
static void exec_runtime_close(ExecRuntime *rt) {
10,875✔
4523
        if (!rt)
10,875✔
4524
                return;
4525

4526
        safe_close_pair(rt->ephemeral_storage_socket);
10,875✔
4527

4528
        exec_shared_runtime_close(rt->shared);
10,875✔
4529
        dynamic_creds_close(rt->dynamic_creds);
10,875✔
4530
}
4531

4532
static void exec_params_close(ExecParameters *p) {
10,875✔
4533
        if (!p)
10,875✔
4534
                return;
4535

4536
        p->stdin_fd = safe_close(p->stdin_fd);
10,875✔
4537
        p->stdout_fd = safe_close(p->stdout_fd);
10,875✔
4538
        p->stderr_fd = safe_close(p->stderr_fd);
10,875✔
4539
}
4540

4541
static int exec_fd_mark_hot(
10,877✔
4542
                const ExecContext *c,
4543
                ExecParameters *p,
4544
                bool hot,
4545
                int *reterr_exit_status) {
4546

4547
        assert(c);
10,877✔
4548
        assert(p);
10,877✔
4549

4550
        if (p->exec_fd < 0)
10,877✔
4551
                return 0;
10,877✔
4552

4553
        uint8_t x = hot;
252✔
4554

4555
        if (write(p->exec_fd, &x, sizeof(x)) < 0) {
252✔
UNCOV
4556
                if (reterr_exit_status)
×
UNCOV
4557
                        *reterr_exit_status = EXIT_EXEC;
×
UNCOV
4558
                return log_exec_error_errno(c, p, errno, "Failed to mark exec_fd as %s: %m", hot ? "hot" : "cold");
×
4559
        }
4560

4561
        return 1;
4562
}
4563

4564
static int send_handoff_timestamp(
10,874✔
4565
                const ExecContext *c,
4566
                ExecParameters *p,
4567
                int *reterr_exit_status) {
4568

4569
        assert(c);
10,874✔
4570
        assert(p);
10,874✔
4571

4572
        if (p->handoff_timestamp_fd < 0)
10,874✔
4573
                return 0;
10,874✔
4574

4575
        dual_timestamp dt;
10,874✔
4576
        dual_timestamp_now(&dt);
10,874✔
4577

4578
        if (write(p->handoff_timestamp_fd, (const usec_t[2]) { dt.realtime, dt.monotonic }, sizeof(usec_t) * 2) < 0) {
10,874✔
UNCOV
4579
                if (reterr_exit_status)
×
UNCOV
4580
                        *reterr_exit_status = EXIT_EXEC;
×
UNCOV
4581
                return log_exec_error_errno(c, p, errno, "Failed to send handoff timestamp: %m");
×
4582
        }
4583

4584
        return 1;
10,874✔
4585
}
4586

4587
static void prepare_terminal(
13,305✔
4588
                const ExecContext *context,
4589
                ExecParameters *p) {
4590

4591
        _cleanup_close_ int lock_fd = -EBADF;
13,305✔
4592

4593
        /* This is the "constructive" reset, i.e. is about preparing things for our invocation rather than
4594
         * cleaning up things from older invocations. */
4595

4596
        assert(context);
13,305✔
4597
        assert(p);
13,305✔
4598

4599
        /* We only try to reset things if we there's the chance our stdout points to a TTY */
4600
        if (!(is_terminal_output(context->std_output) ||
13,305✔
4601
              (context->std_output == EXEC_OUTPUT_INHERIT && is_terminal_input(context->std_input)) ||
12,691✔
4602
              context->std_output == EXEC_OUTPUT_NAMED_FD ||
4603
              p->stdout_fd >= 0))
12,691✔
4604
                return;
12,220✔
4605

4606
        /* Let's explicitly determine whether to reset via ANSI sequences or not, taking our ExecContext
4607
         * information into account */
4608
        bool use_ansi = exec_context_shall_ansi_seq_reset(context);
1,085✔
4609

4610
        if (context->tty_reset) {
1,085✔
4611
                /* When we are resetting the TTY, then let's create a lock first, to synchronize access. This
4612
                 * in particular matters as concurrent resets and the TTY size ANSI DSR logic done by the
4613
                 * exec_context_apply_tty_size() below might interfere */
4614
                lock_fd = lock_dev_console();
157✔
4615
                if (lock_fd < 0)
157✔
UNCOV
4616
                        log_exec_debug_errno(context, p, lock_fd, "Failed to lock /dev/console, ignoring: %m");
×
4617

4618
                /* We explicitly control whether to send ansi sequences or not here, since we want to consult
4619
                 * the env vars explicitly configured in the ExecContext, rather than our own environment
4620
                 * block. */
4621
                (void) terminal_reset_defensive(STDOUT_FILENO, use_ansi ? TERMINAL_RESET_FORCE_ANSI_SEQ : TERMINAL_RESET_AVOID_ANSI_SEQ);
160✔
4622
        }
4623

4624
        (void) exec_context_apply_tty_size(context, STDIN_FILENO, STDOUT_FILENO, /* tty_path= */ NULL);
1,085✔
4625

4626
        if (use_ansi)
1,085✔
4627
                (void) osc_context_open_service(p->unit_id, p->invocation_id, /* ret_seq= */ NULL);
154✔
4628
}
4629

4630
int exec_invoke(
13,308✔
4631
                const ExecCommand *command,
4632
                const ExecContext *context,
4633
                ExecParameters *params,
4634
                ExecRuntime *runtime,
4635
                const CGroupContext *cgroup_context,
4636
                int *exit_status) {
13,308✔
4637

4638
        _cleanup_strv_free_ char **our_env = NULL, **pass_env = NULL, **joined_exec_search_path = NULL, **accum_env = NULL, **replaced_argv = NULL;
28✔
4639
        int r;
13,308✔
4640
        const char *username = NULL, *groupname = NULL;
13,308✔
UNCOV
4641
        _cleanup_free_ char *home_buffer = NULL, *memory_pressure_path = NULL, *own_user = NULL;
×
4642
        const char *pwent_home = NULL, *shell = NULL;
13,308✔
4643
        char **final_argv = NULL;
13,308✔
4644
        dev_t journal_stream_dev = 0;
13,308✔
4645
        ino_t journal_stream_ino = 0;
13,308✔
4646
        bool needs_sandboxing,          /* Do we need to set up full sandboxing? (i.e. all namespacing, all MAC stuff, caps, yadda yadda */
13,308✔
4647
                needs_setuid,           /* Do we need to do the actual setresuid()/setresgid() calls? */
4648
                needs_mount_namespace;  /* Do we need to set up a mount namespace for this kernel? */
4649
        bool keep_seccomp_privileges = false;
13,308✔
4650
        bool has_cap_sys_admin = false;
13,308✔
4651
#if HAVE_SELINUX
4652
        _cleanup_free_ char *mac_selinux_context_net = NULL;
4653
        bool use_selinux = false;
4654
#endif
4655
#if ENABLE_SMACK
4656
        bool use_smack = false;
13,308✔
4657
#endif
4658
#if HAVE_APPARMOR
4659
        bool use_apparmor = false;
4660
#endif
4661
#if HAVE_SECCOMP
4662
        uint64_t saved_bset = 0;
13,308✔
4663
#endif
4664
        uid_t saved_uid = getuid();
13,308✔
4665
        gid_t saved_gid = getgid();
13,308✔
4666
        uid_t uid = UID_INVALID;
13,308✔
4667
        gid_t gid = GID_INVALID;
13,308✔
4668
        size_t n_fds, /* fds to pass to the child */
13,308✔
4669
               n_keep_fds; /* total number of fds not to close */
4670
        int secure_bits;
13,308✔
4671
        _cleanup_free_ gid_t *gids = NULL, *gids_after_pam = NULL;
28✔
4672
        int ngids = 0, ngids_after_pam = 0;
13,308✔
4673
        int socket_fd = -EBADF, named_iofds[3] = EBADF_TRIPLET;
13,308✔
4674
        size_t n_storage_fds, n_socket_fds, n_extra_fds;
13,308✔
4675

4676
        assert(command);
13,308✔
4677
        assert(context);
13,308✔
4678
        assert(params);
13,308✔
4679
        assert(runtime);
13,308✔
4680
        assert(cgroup_context);
13,308✔
4681
        assert(exit_status);
13,308✔
4682

4683
        /* This should be mostly redundant, as the log level is also passed as an argument of the executor,
4684
         * and is already applied earlier. Just for safety. */
4685
        if (params->debug_invocation)
13,308✔
4686
                log_set_max_level(LOG_PRI(LOG_DEBUG));
2✔
4687
        else if (context->log_level_max >= 0)
13,306✔
4688
                log_set_max_level(context->log_level_max);
5✔
4689

4690
        /* Explicitly test for CVE-2021-4034 inspired invocations */
4691
        if (!command->path || strv_isempty(command->argv)) {
13,308✔
UNCOV
4692
                *exit_status = EXIT_EXEC;
×
UNCOV
4693
                return log_exec_error_errno(
×
4694
                                context,
4695
                                params,
4696
                                SYNTHETIC_ERRNO(EINVAL),
4697
                                "Invalid command line arguments.");
4698
        }
4699

4700
        LOG_CONTEXT_PUSH_EXEC(context, params);
38,674✔
4701

4702
        if (context->std_input == EXEC_INPUT_SOCKET ||
13,308✔
4703
            context->std_output == EXEC_OUTPUT_SOCKET ||
13,297✔
4704
            context->std_error == EXEC_OUTPUT_SOCKET) {
13,291✔
4705

4706
                if (params->n_socket_fds > 1)
17✔
UNCOV
4707
                        return log_exec_error_errno(context, params, SYNTHETIC_ERRNO(EINVAL), "Got more than one socket.");
×
4708

4709
                if (params->n_socket_fds == 0)
17✔
UNCOV
4710
                        return log_exec_error_errno(context, params, SYNTHETIC_ERRNO(EINVAL), "Got no socket.");
×
4711

4712
                socket_fd = params->fds[0];
17✔
4713
                n_storage_fds = n_socket_fds = n_extra_fds = 0;
17✔
4714
        } else {
4715
                n_socket_fds = params->n_socket_fds;
13,291✔
4716
                n_storage_fds = params->n_storage_fds;
13,291✔
4717
                n_extra_fds = params->n_extra_fds;
13,291✔
4718
        }
4719
        n_fds = n_socket_fds + n_storage_fds + n_extra_fds;
13,308✔
4720

4721
        r = exec_context_named_iofds(context, params, named_iofds);
13,308✔
4722
        if (r < 0)
13,308✔
UNCOV
4723
                return log_exec_error_errno(context, params, r, "Failed to load a named file descriptor: %m");
×
4724

4725
        rename_process_from_path(command->path);
13,308✔
4726

4727
        /* We reset exactly these signals, since they are the only ones we set to SIG_IGN in the main
4728
         * daemon. All others we leave untouched because we set them to SIG_DFL or a valid handler initially,
4729
         * both of which will be demoted to SIG_DFL. */
4730
        (void) default_signals(SIGNALS_CRASH_HANDLER,
13,308✔
4731
                               SIGNALS_IGNORE);
4732

4733
        if (context->ignore_sigpipe)
13,308✔
4734
                (void) ignore_signals(SIGPIPE);
13,151✔
4735

4736
        r = reset_signal_mask();
13,308✔
4737
        if (r < 0) {
13,308✔
UNCOV
4738
                *exit_status = EXIT_SIGNAL_MASK;
×
UNCOV
4739
                return log_exec_error_errno(context, params, r, "Failed to set process signal mask: %m");
×
4740
        }
4741

4742
        if (params->idle_pipe)
13,308✔
4743
                do_idle_pipe_dance(params->idle_pipe);
154✔
4744

4745
        /* Close fds we don't need very early to make sure we don't block init reexecution because it cannot bind its
4746
         * sockets. Among the fds we close are the logging fds, and we want to keep them closed, so that we don't have
4747
         * any fds open we don't really want open during the transition. In order to make logging work, we switch the
4748
         * log subsystem into open_when_needed mode, so that it reopens the logs on every single log call. */
4749

4750
        log_forget_fds();
13,308✔
4751
        log_set_open_when_needed(true);
13,308✔
4752
        log_settle_target();
13,308✔
4753

4754
        /* In case anything used libc syslog(), close this here, too */
4755
        closelog();
13,308✔
4756

4757
        r = collect_open_file_fds(context, params, &n_fds);
13,308✔
4758
        if (r < 0) {
13,308✔
4759
                *exit_status = EXIT_FDS;
1✔
4760
                return log_exec_error_errno(context, params, r, "Failed to get OpenFile= file descriptors: %m");
3✔
4761
        }
4762

4763
        int keep_fds[n_fds + 4];
13,307✔
4764
        memcpy_safe(keep_fds, params->fds, n_fds * sizeof(int));
13,307✔
4765
        n_keep_fds = n_fds;
13,307✔
4766

4767
        r = add_shifted_fd(keep_fds, ELEMENTSOF(keep_fds), &n_keep_fds, &params->exec_fd);
13,307✔
4768
        if (r < 0) {
13,307✔
4769
                *exit_status = EXIT_FDS;
×
4770
                return log_exec_error_errno(context, params, r, "Failed to collect shifted fd: %m");
×
4771
        }
4772

4773
        r = add_shifted_fd(keep_fds, ELEMENTSOF(keep_fds), &n_keep_fds, &params->handoff_timestamp_fd);
13,307✔
4774
        if (r < 0) {
13,307✔
4775
                *exit_status = EXIT_FDS;
×
4776
                return log_exec_error_errno(context, params, r, "Failed to collect shifted fd: %m");
×
4777
        }
4778

4779
#if HAVE_LIBBPF
4780
        r = add_shifted_fd(keep_fds, ELEMENTSOF(keep_fds), &n_keep_fds, &params->bpf_restrict_fs_map_fd);
13,307✔
4781
        if (r < 0) {
13,307✔
UNCOV
4782
                *exit_status = EXIT_FDS;
×
UNCOV
4783
                return log_exec_error_errno(context, params, r, "Failed to collect shifted fd: %m");
×
4784
        }
4785
#endif
4786

4787
        r = close_remaining_fds(params, runtime, socket_fd, keep_fds, n_keep_fds);
13,307✔
4788
        if (r < 0) {
13,307✔
4789
                *exit_status = EXIT_FDS;
×
UNCOV
4790
                return log_exec_error_errno(context, params, r, "Failed to close unwanted file descriptors: %m");
×
4791
        }
4792

4793
        if (!context->same_pgrp &&
25,629✔
4794
            setsid() < 0) {
12,322✔
UNCOV
4795
                *exit_status = EXIT_SETSID;
×
UNCOV
4796
                return log_exec_error_errno(context, params, errno, "Failed to create new process session: %m");
×
4797
        }
4798

4799
        /* Now, reset the TTY associated to this service "destructively" (i.e. possibly even hang up or
4800
         * disallocate the VT), to get rid of any prior uses of the device. Note that we do not keep any fd
4801
         * open here, hence some of the settings made here might vanish again, depending on the TTY driver
4802
         * used. A 2nd ("constructive") initialization after we opened the input/output fds we actually want
4803
         * will fix this. Note that we pass a NULL invocation ID here – as exec_context_tty_reset() expects
4804
         * the invocation ID associated with the OSC 3008 context ID to close. But we don't want to close any
4805
         * OSC 3008 context here, and opening a fresh OSC 3008 context happens a bit further down. */
4806
        exec_context_tty_reset(context, params, /* invocation_id= */ SD_ID128_NULL);
13,307✔
4807

4808
        if (params->shall_confirm_spawn && exec_context_shall_confirm_spawn(context)) {
13,307✔
UNCOV
4809
                _cleanup_free_ char *cmdline = NULL;
×
4810

UNCOV
4811
                cmdline = quote_command_line(command->argv, SHELL_ESCAPE_EMPTY);
×
UNCOV
4812
                if (!cmdline) {
×
UNCOV
4813
                        *exit_status = EXIT_MEMORY;
×
UNCOV
4814
                        return log_oom();
×
4815
                }
4816

4817
                r = ask_for_confirmation(context, params, cmdline);
×
4818
                if (r != CONFIRM_EXECUTE) {
×
UNCOV
4819
                        if (r == CONFIRM_PRETEND_SUCCESS) {
×
UNCOV
4820
                                *exit_status = EXIT_SUCCESS;
×
UNCOV
4821
                                return 0;
×
4822
                        }
4823

UNCOV
4824
                        *exit_status = EXIT_CONFIRM;
×
UNCOV
4825
                        return log_exec_error_errno(context, params, SYNTHETIC_ERRNO(ECANCELED),
×
4826
                                                    "Execution cancelled by the user.");
4827
                }
4828
        }
4829

4830
        /* We are about to invoke NSS and PAM modules. Let's tell them what we are doing here, maybe they care. This is
4831
         * used by nss-resolve to disable itself when we are about to start systemd-resolved, to avoid deadlocks. Note
4832
         * that these env vars do not survive the execve(), which means they really only apply to the PAM and NSS
4833
         * invocations themselves. Also note that while we'll only invoke NSS modules involved in user management they
4834
         * might internally call into other NSS modules that are involved in hostname resolution, we never know. */
4835
        if (setenv("SYSTEMD_ACTIVATION_UNIT", params->unit_id, true) != 0 ||
26,614✔
4836
            setenv("SYSTEMD_ACTIVATION_SCOPE", runtime_scope_to_string(params->runtime_scope), true) != 0) {
13,307✔
UNCOV
4837
                *exit_status = EXIT_MEMORY;
×
UNCOV
4838
                return log_exec_error_errno(context, params, errno, "Failed to update environment: %m");
×
4839
        }
4840

4841
        if (context->dynamic_user && runtime->dynamic_creds) {
13,369✔
4842
                _cleanup_strv_free_ char **suggested_paths = NULL;
62✔
4843

4844
                /* On top of that, make sure we bypass our own NSS module nss-systemd comprehensively for any NSS
4845
                 * checks, if DynamicUser=1 is used, as we shouldn't create a feedback loop with ourselves here. */
4846
                if (putenv((char*) "SYSTEMD_NSS_DYNAMIC_BYPASS=1") != 0) {
62✔
4847
                        *exit_status = EXIT_USER;
×
4848
                        return log_exec_error_errno(context, params, errno, "Failed to update environment: %m");
×
4849
                }
4850

4851
                r = compile_suggested_paths(context, params, &suggested_paths);
62✔
4852
                if (r < 0) {
62✔
4853
                        *exit_status = EXIT_MEMORY;
×
UNCOV
4854
                        return log_oom();
×
4855
                }
4856

4857
                r = dynamic_creds_realize(runtime->dynamic_creds, suggested_paths, &uid, &gid);
62✔
4858
                if (r < 0) {
62✔
UNCOV
4859
                        *exit_status = EXIT_USER;
×
UNCOV
4860
                        if (r == -EILSEQ)
×
UNCOV
4861
                                return log_exec_error_errno(context, params, SYNTHETIC_ERRNO(EOPNOTSUPP),
×
4862
                                                            "Failed to update dynamic user credentials: User or group with specified name already exists.");
UNCOV
4863
                        return log_exec_error_errno(context, params, r, "Failed to update dynamic user credentials: %m");
×
4864
                }
4865

4866
                if (!uid_is_valid(uid)) {
62✔
UNCOV
4867
                        *exit_status = EXIT_USER;
×
4868
                        return log_exec_error_errno(context, params, SYNTHETIC_ERRNO(ESRCH), "UID validation failed for \""UID_FMT"\".", uid);
×
4869
                }
4870

4871
                if (!gid_is_valid(gid)) {
62✔
UNCOV
4872
                        *exit_status = EXIT_USER;
×
UNCOV
4873
                        return log_exec_error_errno(context, params, SYNTHETIC_ERRNO(ESRCH), "GID validation failed for \""GID_FMT"\".", gid);
×
4874
                }
4875

4876
                if (runtime->dynamic_creds->user)
62✔
4877
                        username = runtime->dynamic_creds->user->name;
62✔
4878

4879
        } else {
4880
                const char *u;
13,245✔
4881

4882
                if (context->user)
13,245✔
4883
                        u = context->user;
4884
                else if (context->pam_name) {
10,651✔
4885
                        /* If PAM is enabled but no user name is explicitly selected, then use our own one. */
4886
                        own_user = getusername_malloc();
59✔
4887
                        if (!own_user) {
59✔
UNCOV
4888
                                *exit_status = EXIT_USER;
×
UNCOV
4889
                                return log_exec_error_errno(context, params, r, "Failed to determine my own user ID: %m");
×
4890
                        }
4891
                        u = own_user;
4892
                } else
4893
                        u = NULL;
4894

4895
                if (u) {
4896
                        r = get_fixed_user(u, &username, &uid, &gid, &pwent_home, &shell);
2,653✔
4897
                        if (r < 0) {
2,653✔
4898
                                *exit_status = EXIT_USER;
2✔
4899
                                return log_exec_error_errno(context, params, r, "Failed to determine user credentials: %m");
6✔
4900
                        }
4901
                }
4902

4903
                if (context->group) {
13,243✔
4904
                        r = get_fixed_group(context->group, &groupname, &gid);
9✔
4905
                        if (r < 0) {
9✔
UNCOV
4906
                                *exit_status = EXIT_GROUP;
×
UNCOV
4907
                                return log_exec_error_errno(context, params, r, "Failed to determine group credentials: %m");
×
4908
                        }
4909
                }
4910
        }
4911

4912
        /* Initialize user supplementary groups and get SupplementaryGroups= ones */
4913
        ngids = get_supplementary_groups(context, username, gid, &gids);
13,305✔
4914
        if (ngids < 0) {
13,305✔
UNCOV
4915
                *exit_status = EXIT_GROUP;
×
UNCOV
4916
                return log_exec_error_errno(context, params, ngids, "Failed to determine supplementary groups: %m");
×
4917
        }
4918

4919
        r = send_user_lookup(params->unit_id, params->user_lookup_fd, uid, gid);
13,305✔
4920
        if (r < 0) {
13,305✔
UNCOV
4921
                *exit_status = EXIT_USER;
×
UNCOV
4922
                return log_exec_error_errno(context, params, r, "Failed to send user credentials to PID1: %m");
×
4923
        }
4924

4925
        params->user_lookup_fd = safe_close(params->user_lookup_fd);
13,305✔
4926

4927
        r = acquire_home(context, &pwent_home, &home_buffer);
13,305✔
4928
        if (r < 0) {
13,305✔
UNCOV
4929
                *exit_status = EXIT_CHDIR;
×
4930
                return log_exec_error_errno(context, params, r, "Failed to determine $HOME for the invoking user: %m");
×
4931
        }
4932

4933
        /* If a socket is connected to STDIN/STDOUT/STDERR, we must drop O_NONBLOCK */
4934
        if (socket_fd >= 0)
13,305✔
4935
                (void) fd_nonblock(socket_fd, false);
17✔
4936

4937
        /* Journald will try to look-up our cgroup in order to populate _SYSTEMD_CGROUP and _SYSTEMD_UNIT fields.
4938
         * Hence we need to migrate to the target cgroup from init.scope before connecting to journald */
4939
        if (params->cgroup_path) {
13,305✔
4940
                _cleanup_free_ char *p = NULL;
13,305✔
4941

4942
                r = exec_params_get_cgroup_path(params, cgroup_context, &p);
13,305✔
4943
                if (r < 0) {
13,305✔
4944
                        *exit_status = EXIT_CGROUP;
×
4945
                        return log_exec_error_errno(context, params, r, "Failed to acquire cgroup path: %m");
×
4946
                }
4947

4948
                r = cg_attach_everywhere(params->cgroup_supported, p, 0);
13,305✔
4949
                if (r == -EUCLEAN) {
13,305✔
UNCOV
4950
                        *exit_status = EXIT_CGROUP;
×
4951
                        return log_exec_error_errno(context, params, r,
×
4952
                                                    "Failed to attach process to cgroup '%s', "
4953
                                                    "because the cgroup or one of its parents or "
4954
                                                    "siblings is in the threaded mode.", p);
4955
                }
4956
                if (r < 0) {
13,305✔
UNCOV
4957
                        *exit_status = EXIT_CGROUP;
×
UNCOV
4958
                        return log_exec_error_errno(context, params, r, "Failed to attach to cgroup %s: %m", p);
×
4959
                }
4960
        }
4961

4962
        if (context->network_namespace_path && runtime->shared && runtime->shared->netns_storage_socket[0] >= 0) {
13,305✔
UNCOV
4963
                r = open_shareable_ns_path(runtime->shared->netns_storage_socket, context->network_namespace_path, CLONE_NEWNET);
×
UNCOV
4964
                if (r < 0) {
×
UNCOV
4965
                        *exit_status = EXIT_NETWORK;
×
UNCOV
4966
                        return log_exec_error_errno(context, params, r, "Failed to open network namespace path %s: %m", context->network_namespace_path);
×
4967
                }
4968
        }
4969

4970
        if (context->ipc_namespace_path && runtime->shared && runtime->shared->ipcns_storage_socket[0] >= 0) {
13,305✔
UNCOV
4971
                r = open_shareable_ns_path(runtime->shared->ipcns_storage_socket, context->ipc_namespace_path, CLONE_NEWIPC);
×
UNCOV
4972
                if (r < 0) {
×
4973
                        *exit_status = EXIT_NAMESPACE;
×
4974
                        return log_exec_error_errno(context, params, r, "Failed to open IPC namespace path %s: %m", context->ipc_namespace_path);
×
4975
                }
4976
        }
4977

4978
        r = setup_input(context, params, socket_fd, named_iofds);
13,305✔
4979
        if (r < 0) {
13,305✔
4980
                *exit_status = EXIT_STDIN;
×
UNCOV
4981
                return log_exec_error_errno(context, params, r, "Failed to set up standard input: %m");
×
4982
        }
4983

4984
        _cleanup_free_ char *fname = NULL;
25✔
4985
        r = path_extract_filename(command->path, &fname);
13,305✔
4986
        if (r < 0) {
13,305✔
UNCOV
4987
                *exit_status = EXIT_STDOUT;
×
UNCOV
4988
                return log_exec_error_errno(context, params, r, "Failed to extract filename from path %s: %m", command->path);
×
4989
        }
4990

4991
        r = setup_output(context, params, STDOUT_FILENO, socket_fd, named_iofds, fname, uid, gid, &journal_stream_dev, &journal_stream_ino);
13,305✔
4992
        if (r < 0) {
13,305✔
UNCOV
4993
                *exit_status = EXIT_STDOUT;
×
4994
                return log_exec_error_errno(context, params, r, "Failed to set up standard output: %m");
×
4995
        }
4996

4997
        r = setup_output(context, params, STDERR_FILENO, socket_fd, named_iofds, fname, uid, gid, &journal_stream_dev, &journal_stream_ino);
13,305✔
4998
        if (r < 0) {
13,305✔
UNCOV
4999
                *exit_status = EXIT_STDERR;
×
UNCOV
5000
                return log_exec_error_errno(context, params, r, "Failed to set up standard error output: %m");
×
5001
        }
5002

5003
        /* Now that stdin/stdout are definiely opened, properly initialize it with our desired
5004
         * settings. Note: this is a "constructive" reset, it prepares things for us to use. This is
5005
         * different from the "destructive" TTY reset further up. Also note: we apply this on stdin/stdout in
5006
         * case this is a tty, regardless if we opened it ourselves or got it passed in pre-opened. */
5007
        prepare_terminal(context, params);
13,305✔
5008

5009
        if (context->oom_score_adjust_set) {
13,305✔
5010
                /* When we can't make this change due to EPERM, then let's silently skip over it. User
5011
                 * namespaces prohibit write access to this file, and we shouldn't trip up over that. */
5012
                r = set_oom_score_adjust(context->oom_score_adjust);
1,451✔
5013
                if (ERRNO_IS_NEG_PRIVILEGE(r))
1,451✔
UNCOV
5014
                        log_exec_debug_errno(context, params, r,
×
5015
                                             "Failed to adjust OOM setting, assuming containerized execution, ignoring: %m");
5016
                else if (r < 0) {
1,451✔
5017
                        *exit_status = EXIT_OOM_ADJUST;
×
UNCOV
5018
                        return log_exec_error_errno(context, params, r, "Failed to adjust OOM setting: %m");
×
5019
                }
5020
        }
5021

5022
        if (context->coredump_filter_set) {
13,305✔
5023
                r = set_coredump_filter(context->coredump_filter);
2✔
5024
                if (ERRNO_IS_NEG_PRIVILEGE(r))
2✔
UNCOV
5025
                        log_exec_debug_errno(context, params, r, "Failed to adjust coredump_filter, ignoring: %m");
×
5026
                else if (r < 0) {
2✔
UNCOV
5027
                        *exit_status = EXIT_LIMITS;
×
UNCOV
5028
                        return log_exec_error_errno(context, params, r, "Failed to adjust coredump_filter: %m");
×
5029
                }
5030
        }
5031

5032
        if (context->cpu_sched_set) {
13,305✔
UNCOV
5033
                struct sched_attr attr = {
×
5034
                        .size = sizeof(attr),
UNCOV
5035
                        .sched_policy = context->cpu_sched_policy,
×
UNCOV
5036
                        .sched_priority = context->cpu_sched_priority,
×
5037
                        .sched_flags = context->cpu_sched_reset_on_fork ? SCHED_FLAG_RESET_ON_FORK : 0,
×
5038
                };
5039

UNCOV
5040
                r = sched_setattr(/* pid= */ 0, &attr, /* flags= */ 0);
×
UNCOV
5041
                if (r < 0) {
×
UNCOV
5042
                        *exit_status = EXIT_SETSCHEDULER;
×
UNCOV
5043
                        return log_exec_error_errno(context, params, errno, "Failed to set up CPU scheduling: %m");
×
5044
                }
5045
        }
5046

5047
        /*
5048
         * Set nice value _after_ the call to sched_setattr() because struct sched_attr includes sched_nice
5049
         * which we do not set, thus it will clobber any previously set nice value. Scheduling policy might
5050
         * be reasonably set together with nice value e.g. in case of SCHED_BATCH (see sched(7)).
5051
         * It would be ideal to set both with the same call, but we cannot easily do so because of all the
5052
         * extra logic in setpriority_closest().
5053
         */
5054
        if (context->nice_set) {
13,305✔
5055
                r = setpriority_closest(context->nice);
16✔
5056
                if (r < 0) {
16✔
UNCOV
5057
                        *exit_status = EXIT_NICE;
×
5058
                        return log_exec_error_errno(context, params, r, "Failed to set up process scheduling priority (nice level): %m");
×
5059
                }
5060
        }
5061

5062
        if (context->cpu_affinity_from_numa || context->cpu_set.set) {
13,305✔
5063
                _cleanup_(cpu_set_reset) CPUSet converted_cpu_set = {};
2✔
5064
                const CPUSet *cpu_set;
2✔
5065

5066
                if (context->cpu_affinity_from_numa) {
2✔
5067
                        r = exec_context_cpu_affinity_from_numa(context, &converted_cpu_set);
2✔
5068
                        if (r < 0) {
2✔
UNCOV
5069
                                *exit_status = EXIT_CPUAFFINITY;
×
UNCOV
5070
                                return log_exec_error_errno(context, params, r, "Failed to derive CPU affinity mask from NUMA mask: %m");
×
5071
                        }
5072

5073
                        cpu_set = &converted_cpu_set;
5074
                } else
5075
                        cpu_set = &context->cpu_set;
×
5076

5077
                if (sched_setaffinity(0, cpu_set->allocated, cpu_set->set) < 0) {
2✔
UNCOV
5078
                        *exit_status = EXIT_CPUAFFINITY;
×
UNCOV
5079
                        return log_exec_error_errno(context, params, errno, "Failed to set up CPU affinity: %m");
×
5080
                }
5081
        }
5082

5083
        if (mpol_is_valid(numa_policy_get_type(&context->numa_policy))) {
13,305✔
5084
                r = apply_numa_policy(&context->numa_policy);
19✔
5085
                if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
19✔
5086
                        log_exec_debug_errno(context, params, r, "NUMA support not available, ignoring.");
×
5087
                else if (r < 0) {
19✔
5088
                        *exit_status = EXIT_NUMA_POLICY;
2✔
5089
                        return log_exec_error_errno(context, params, r, "Failed to set NUMA memory policy: %m");
6✔
5090
                }
5091
        }
5092

5093
        if (context->ioprio_set)
13,303✔
5094
                if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) {
8✔
5095
                        *exit_status = EXIT_IOPRIO;
×
5096
                        return log_exec_error_errno(context, params, errno, "Failed to set up IO scheduling priority: %m");
×
5097
                }
5098

5099
        if (context->timer_slack_nsec != NSEC_INFINITY)
13,303✔
UNCOV
5100
                if (prctl(PR_SET_TIMERSLACK, context->timer_slack_nsec) < 0) {
×
5101
                        *exit_status = EXIT_TIMERSLACK;
×
5102
                        return log_exec_error_errno(context, params, errno, "Failed to set up timer slack: %m");
×
5103
                }
5104

5105
        if (context->personality != PERSONALITY_INVALID) {
13,303✔
UNCOV
5106
                r = safe_personality(context->personality);
×
UNCOV
5107
                if (r < 0) {
×
UNCOV
5108
                        *exit_status = EXIT_PERSONALITY;
×
UNCOV
5109
                        return log_exec_error_errno(context, params, r, "Failed to set up execution domain (personality): %m");
×
5110
                }
5111
        }
5112

5113
        if (context->memory_ksm >= 0)
13,303✔
5114
                if (prctl(PR_SET_MEMORY_MERGE, context->memory_ksm, 0, 0, 0) < 0) {
×
UNCOV
5115
                        if (ERRNO_IS_NOT_SUPPORTED(errno))
×
UNCOV
5116
                                log_exec_debug_errno(context,
×
5117
                                                     params,
5118
                                                     errno,
5119
                                                     "KSM support not available, ignoring.");
5120
                        else {
UNCOV
5121
                                *exit_status = EXIT_KSM;
×
UNCOV
5122
                                return log_exec_error_errno(context, params, errno, "Failed to set KSM: %m");
×
5123
                        }
5124
                }
5125

5126
#if ENABLE_UTMP
5127
        if (context->utmp_id) {
13,303✔
5128
                _cleanup_free_ char *username_alloc = NULL;
159✔
5129

5130
                if (!username && context->utmp_mode == EXEC_UTMP_USER) {
159✔
5131
                        username_alloc = uid_to_name(uid_is_valid(uid) ? uid : saved_uid);
1✔
5132
                        if (!username_alloc) {
1✔
5133
                                *exit_status = EXIT_USER;
×
5134
                                return log_oom();
×
5135
                        }
5136
                }
5137

5138
                const char *line = context->tty_path ?
318✔
5139
                        (path_startswith(context->tty_path, "/dev/") ?: context->tty_path) :
159✔
5140
                        NULL;
5141
                utmp_put_init_process(context->utmp_id, getpid_cached(), getsid(0),
159✔
5142
                                      line,
5143
                                      context->utmp_mode == EXEC_UTMP_INIT  ? INIT_PROCESS :
159✔
5144
                                      context->utmp_mode == EXEC_UTMP_LOGIN ? LOGIN_PROCESS :
7✔
5145
                                      USER_PROCESS,
5146
                                      username ?: username_alloc);
159✔
5147
        }
5148
#endif
5149

5150
        if (uid_is_valid(uid)) {
13,303✔
5151
                r = chown_terminal(STDIN_FILENO, uid);
2,713✔
5152
                if (r < 0) {
2,713✔
5153
                        *exit_status = EXIT_STDIN;
×
5154
                        return log_exec_error_errno(context, params, r, "Failed to change ownership of terminal: %m");
×
5155
                }
5156
        }
5157

5158
        /* We need sandboxing if the caller asked us to apply it and the command isn't explicitly excepted
5159
         * from it. */
5160
        needs_sandboxing = (params->flags & EXEC_APPLY_SANDBOXING) && !(command->flags & EXEC_COMMAND_FULLY_PRIVILEGED);
13,303✔
5161

5162
        if (params->cgroup_path) {
13,303✔
5163
                /* If delegation is enabled we'll pass ownership of the cgroup to the user of the new process. On cgroup v1
5164
                 * this is only about systemd's own hierarchy, i.e. not the controller hierarchies, simply because that's not
5165
                 * safe. On cgroup v2 there's only one hierarchy anyway, and delegation is safe there, hence in that case only
5166
                 * touch a single hierarchy too. */
5167

5168
                if (params->flags & EXEC_CGROUP_DELEGATE) {
13,303✔
5169
                        _cleanup_free_ char *p = NULL;
674✔
5170

5171
                        r = cg_set_access(SYSTEMD_CGROUP_CONTROLLER, params->cgroup_path, uid, gid);
674✔
5172
                        if (r < 0) {
674✔
UNCOV
5173
                                *exit_status = EXIT_CGROUP;
×
UNCOV
5174
                                return log_exec_error_errno(context, params, r, "Failed to adjust control group access: %m");
×
5175
                        }
5176

5177
                        r = exec_params_get_cgroup_path(params, cgroup_context, &p);
674✔
5178
                        if (r < 0) {
674✔
UNCOV
5179
                                *exit_status = EXIT_CGROUP;
×
UNCOV
5180
                                return log_exec_error_errno(context, params, r, "Failed to acquire cgroup path: %m");
×
5181
                        }
5182
                        if (r > 0) {
674✔
5183
                                r = cg_set_access_recursive(SYSTEMD_CGROUP_CONTROLLER, p, uid, gid);
338✔
5184
                                if (r < 0) {
338✔
UNCOV
5185
                                        *exit_status = EXIT_CGROUP;
×
UNCOV
5186
                                        return log_exec_error_errno(context, params, r, "Failed to adjust control subgroup access: %m");
×
5187
                                }
5188
                        }
5189
                }
5190

5191
                if (cg_unified() > 0 && is_pressure_supported() > 0) {
13,303✔
5192
                        if (cgroup_context_want_memory_pressure(cgroup_context)) {
13,303✔
5193
                                r = cg_get_path("memory", params->cgroup_path, "memory.pressure", &memory_pressure_path);
12,948✔
5194
                                if (r < 0) {
12,948✔
UNCOV
5195
                                        *exit_status = EXIT_MEMORY;
×
UNCOV
5196
                                        return log_oom();
×
5197
                                }
5198

5199
                                r = chmod_and_chown(memory_pressure_path, 0644, uid, gid);
12,948✔
5200
                                if (r < 0) {
12,948✔
5201
                                        log_exec_full_errno(context, params, r == -ENOENT || ERRNO_IS_PRIVILEGE(r) ? LOG_DEBUG : LOG_WARNING, r,
2✔
5202
                                                            "Failed to adjust ownership of '%s', ignoring: %m", memory_pressure_path);
5203
                                        memory_pressure_path = mfree(memory_pressure_path);
1✔
5204
                                }
5205
                                /* First we use the current cgroup path to chmod and chown the memory pressure path, then pass the path relative
5206
                                 * to the cgroup namespace to environment variables and mounts. If chown/chmod fails, we should not pass memory
5207
                                 * pressure path environment variable or read-write mount to the unit. This is why we check if
5208
                                 * memory_pressure_path != NULL in the conditional below. */
5209
                                if (memory_pressure_path && needs_sandboxing && exec_needs_cgroup_namespace(context, params)) {
12,948✔
5210
                                        memory_pressure_path = mfree(memory_pressure_path);
9✔
5211
                                        r = cg_get_path("memory", "", "memory.pressure", &memory_pressure_path);
9✔
5212
                                        if (r < 0) {
9✔
UNCOV
5213
                                                *exit_status = EXIT_MEMORY;
×
UNCOV
5214
                                                return log_oom();
×
5215
                                        }
5216
                                }
5217
                        } else if (cgroup_context->memory_pressure_watch == CGROUP_PRESSURE_WATCH_NO) {
355✔
5218
                                memory_pressure_path = strdup("/dev/null"); /* /dev/null is explicit indicator for turning of memory pressure watch */
×
UNCOV
5219
                                if (!memory_pressure_path) {
×
UNCOV
5220
                                        *exit_status = EXIT_MEMORY;
×
UNCOV
5221
                                        return log_oom();
×
5222
                                }
5223
                        }
5224
                }
5225
        }
5226

5227
        needs_mount_namespace = exec_needs_mount_namespace(context, params, runtime);
13,303✔
5228

5229
        for (ExecDirectoryType dt = 0; dt < _EXEC_DIRECTORY_TYPE_MAX; dt++) {
79,813✔
5230
                r = setup_exec_directory(context, params, uid, gid, dt, needs_mount_namespace, exit_status);
66,511✔
5231
                if (r < 0)
66,511✔
5232
                        return log_exec_error_errno(context, params, r, "Failed to set up special execution directory in %s: %m", params->prefix[dt]);
3✔
5233
        }
5234

5235
        r = exec_setup_credentials(context, params, params->unit_id, uid, gid);
13,302✔
5236
        if (r < 0) {
10,896✔
UNCOV
5237
                *exit_status = EXIT_CREDENTIALS;
×
UNCOV
5238
                return log_exec_error_errno(context, params, r, "Failed to set up credentials: %m");
×
5239
        }
5240

5241
        r = build_environment(
10,896✔
5242
                        context,
5243
                        params,
5244
                        cgroup_context,
5245
                        n_fds,
5246
                        pwent_home,
5247
                        username,
5248
                        shell,
5249
                        journal_stream_dev,
5250
                        journal_stream_ino,
5251
                        memory_pressure_path,
5252
                        needs_sandboxing,
5253
                        &our_env);
5254
        if (r < 0) {
10,896✔
UNCOV
5255
                *exit_status = EXIT_MEMORY;
×
UNCOV
5256
                return log_oom();
×
5257
        }
5258

5259
        r = build_pass_environment(context, &pass_env);
10,896✔
5260
        if (r < 0) {
10,896✔
UNCOV
5261
                *exit_status = EXIT_MEMORY;
×
UNCOV
5262
                return log_oom();
×
5263
        }
5264

5265
        /* The $PATH variable is set to the default path in params->environment. However, this is overridden
5266
         * if user-specified fields have $PATH set. The intention is to also override $PATH if the unit does
5267
         * not specify PATH but the unit has ExecSearchPath. */
5268
        if (!strv_isempty(context->exec_search_path)) {
10,896✔
UNCOV
5269
                _cleanup_free_ char *joined = NULL;
×
5270

5271
                joined = strv_join(context->exec_search_path, ":");
×
5272
                if (!joined) {
×
UNCOV
5273
                        *exit_status = EXIT_MEMORY;
×
UNCOV
5274
                        return log_oom();
×
5275
                }
5276

UNCOV
5277
                r = strv_env_assign(&joined_exec_search_path, "PATH", joined);
×
UNCOV
5278
                if (r < 0) {
×
UNCOV
5279
                        *exit_status = EXIT_MEMORY;
×
5280
                        return log_oom();
×
5281
                }
5282
        }
5283

5284
        accum_env = strv_env_merge(params->environment,
10,896✔
5285
                                   our_env,
5286
                                   joined_exec_search_path,
5287
                                   pass_env,
5288
                                   context->environment,
5289
                                   params->files_env);
5290
        if (!accum_env) {
10,896✔
UNCOV
5291
                *exit_status = EXIT_MEMORY;
×
UNCOV
5292
                return log_oom();
×
5293
        }
5294
        accum_env = strv_env_clean(accum_env);
10,896✔
5295

5296
        (void) umask(context->umask);
10,896✔
5297

5298
        r = setup_keyring(context, params, uid, gid);
10,896✔
5299
        if (r < 0) {
10,896✔
UNCOV
5300
                *exit_status = EXIT_KEYRING;
×
UNCOV
5301
                return log_exec_error_errno(context, params, r, "Failed to set up kernel keyring: %m");
×
5302
        }
5303

5304
        /* We need setresuid() if the caller asked us to apply sandboxing and the command isn't explicitly
5305
         * excepted from either whole sandboxing or just setresuid() itself. */
5306
        needs_setuid = (params->flags & EXEC_APPLY_SANDBOXING) && !(command->flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID));
10,896✔
5307

5308
        uint64_t capability_ambient_set = context->capability_ambient_set;
10,896✔
5309

5310
        /* Check CAP_SYS_ADMIN before we enter user namespace to see if we can mount /proc even though its masked. */
5311
        has_cap_sys_admin = have_effective_cap(CAP_SYS_ADMIN) > 0;
10,896✔
5312

5313
        if (needs_sandboxing) {
10,896✔
5314
                /* MAC enablement checks need to be done before a new mount ns is created, as they rely on
5315
                 * /sys being present. The actual MAC context application will happen later, as late as
5316
                 * possible, to avoid impacting our own code paths. */
5317

5318
#if HAVE_SELINUX
5319
                use_selinux = mac_selinux_use();
5320
#endif
5321
#if ENABLE_SMACK
5322
                use_smack = mac_smack_use();
10,896✔
5323
#endif
5324
#if HAVE_APPARMOR
5325
                if (mac_apparmor_use()) {
5326
                        r = dlopen_libapparmor();
5327
                        if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r))
5328
                                log_warning_errno(r, "Failed to load libapparmor, ignoring: %m");
5329
                        use_apparmor = r >= 0;
5330
                }
5331
#endif
5332
        }
5333

5334
        if (needs_sandboxing) {
10,896✔
5335
                int which_failed;
10,896✔
5336

5337
                /* Let's set the resource limits before we call into PAM, so that pam_limits wins over what
5338
                 * is set here. (See below.) */
5339

5340
                r = setrlimit_closest_all((const struct rlimit* const *) context->rlimit, &which_failed);
10,896✔
5341
                if (r < 0) {
10,896✔
UNCOV
5342
                        *exit_status = EXIT_LIMITS;
×
5343
                        return log_exec_error_errno(context, params, r, "Failed to adjust resource limit RLIMIT_%s: %m", rlimit_to_string(which_failed));
×
5344
                }
5345
        }
5346

5347
        if (needs_setuid && context->pam_name && username) {
10,896✔
5348
                /* Let's call into PAM after we set up our own idea of resource limits so that pam_limits
5349
                 * wins here. (See above.) */
5350

5351
                /* All fds passed in the fds array will be closed in the pam child process. */
5352
                r = setup_pam(context, params, username, uid, gid, &accum_env, params->fds, n_fds, params->exec_fd);
320✔
5353
                if (r < 0) {
320✔
UNCOV
5354
                        *exit_status = EXIT_PAM;
×
UNCOV
5355
                        return log_exec_error_errno(context, params, r, "Failed to set up PAM session: %m");
×
5356
                }
5357

5358
                /* PAM modules might have set some ambient caps. Query them here and merge them into
5359
                 * the caps we want to set in the end, so that we don't end up unsetting them. */
5360
                uint64_t ambient_after_pam;
320✔
5361
                r = capability_get_ambient(&ambient_after_pam);
320✔
5362
                if (r < 0) {
320✔
UNCOV
5363
                        *exit_status = EXIT_CAPABILITIES;
×
UNCOV
5364
                        return log_exec_error_errno(context, params, r, "Failed to query ambient caps: %m");
×
5365
                }
5366

5367
                capability_ambient_set |= ambient_after_pam;
320✔
5368

5369
                ngids_after_pam = getgroups_alloc(&gids_after_pam);
320✔
5370
                if (ngids_after_pam < 0) {
320✔
5371
                        *exit_status = EXIT_GROUP;
×
5372
                        return log_exec_error_errno(context, params, ngids_after_pam, "Failed to obtain groups after setting up PAM: %m");
×
5373
                }
5374
        }
5375

5376
        if (needs_sandboxing && exec_context_need_unprivileged_private_users(context, params)) {
10,896✔
5377
                /* If we're unprivileged, set up the user namespace first to enable use of the other namespaces.
5378
                 * Users with CAP_SYS_ADMIN can set up user namespaces last because they will be able to
5379
                 * set up all of the other namespaces (i.e. network, mount, UTS) without a user namespace. */
5380
                PrivateUsers pu = exec_context_get_effective_private_users(context, params);
24✔
5381

5382
                /* The kernel requires /proc/pid/setgroups be set to "deny" prior to writing /proc/pid/gid_map in
5383
                 * unprivileged user namespaces. */
5384
                r = setup_private_users(pu, saved_uid, saved_gid, uid, gid, /* allow_setgroups= */ false);
24✔
5385
                /* If it was requested explicitly and we can't set it up, fail early. Otherwise, continue and let
5386
                 * the actual requested operations fail (or silently continue). */
5387
                if (r < 0 && context->private_users != PRIVATE_USERS_NO) {
24✔
UNCOV
5388
                        *exit_status = EXIT_USER;
×
UNCOV
5389
                        return log_exec_error_errno(context, params, r, "Failed to set up user namespacing for unprivileged user: %m");
×
5390
                }
UNCOV
5391
                if (r < 0)
×
UNCOV
5392
                        log_exec_info_errno(context, params, r, "Failed to set up user namespacing for unprivileged user, ignoring: %m");
×
5393
                else {
5394
                        assert(r > 0);
24✔
5395
                        log_debug("Set up unprivileged user namespace");
24✔
5396
                }
5397
        }
5398

5399
        /* Call setup_delegated_namespaces() the first time to unshare all non-delegated namespaces. */
5400
        r = setup_delegated_namespaces(
10,896✔
5401
                        context,
5402
                        params,
5403
                        runtime,
5404
                        /* delegate= */ false,
5405
                        memory_pressure_path,
5406
                        uid,
5407
                        gid,
5408
                        command,
5409
                        needs_sandboxing,
5410
                        has_cap_sys_admin,
5411
                        exit_status);
5412
        if (r < 0)
10,893✔
5413
                return r;
5414

5415
        /* Drop groups as early as possible.
5416
         * This needs to be done after PrivateDevices=yes setup as device nodes should be owned by the host's root.
5417
         * For non-root in a userns, devices will be owned by the user/group before the group change, and nobody. */
5418
        if (needs_setuid) {
10,877✔
5419
                _cleanup_free_ gid_t *gids_to_enforce = NULL;
10,877✔
5420
                int ngids_to_enforce;
10,877✔
5421

5422
                ngids_to_enforce = merge_gid_lists(gids,
10,877✔
5423
                                                   ngids,
5424
                                                   gids_after_pam,
5425
                                                   ngids_after_pam,
5426
                                                   &gids_to_enforce);
5427
                if (ngids_to_enforce < 0) {
10,877✔
UNCOV
5428
                        *exit_status = EXIT_GROUP;
×
UNCOV
5429
                        return log_exec_error_errno(context, params,
×
5430
                                                    ngids_to_enforce,
5431
                                                    "Failed to merge group lists. Group membership might be incorrect: %m");
5432
                }
5433

5434
                r = enforce_groups(gid, gids_to_enforce, ngids_to_enforce);
10,877✔
5435
                if (r < 0) {
10,877✔
5436
                        *exit_status = EXIT_GROUP;
1✔
5437
                        return log_exec_error_errno(context, params, r, "Changing group credentials failed: %m");
1✔
5438
                }
5439
        }
5440

5441
        /* If the user namespace was not set up above, try to do it now.
5442
         * It's preferred to set up the user namespace later (after all other namespaces) so as not to be
5443
         * restricted by rules pertaining to combining user namespaces with other namespaces (e.g. in the
5444
         * case of mount namespaces being less privileged when the mount point list is copied from a
5445
         * different user namespace). */
5446

5447
        if (needs_sandboxing && !exec_context_need_unprivileged_private_users(context, params)) {
10,876✔
5448
                PrivateUsers pu = exec_context_get_effective_private_users(context, params);
10,856✔
5449

5450
                r = setup_private_users(pu, saved_uid, saved_gid, uid, gid,
10,856✔
5451
                                        /* allow_setgroups= */ pu == PRIVATE_USERS_FULL);
5452
                if (r < 0) {
10,856✔
UNCOV
5453
                        *exit_status = EXIT_USER;
×
UNCOV
5454
                        return log_exec_error_errno(context, params, r, "Failed to set up user namespacing: %m");
×
5455
                }
5456

5457
                log_debug("Set up privileged user namespace");
10,856✔
5458
        }
5459

5460
        /* Call setup_delegated_namespaces() the second time to unshare all delegated namespaces. */
5461
        r = setup_delegated_namespaces(
10,876✔
5462
                        context,
5463
                        params,
5464
                        runtime,
5465
                        /* delegate= */ true,
5466
                        memory_pressure_path,
5467
                        uid,
5468
                        gid,
5469
                        command,
5470
                        needs_sandboxing,
5471
                        has_cap_sys_admin,
5472
                        exit_status);
5473
        if (r < 0)
10,876✔
5474
                return r;
5475

5476
        /* Now that the mount namespace has been set up and privileges adjusted, let's look for the thing we
5477
         * shall execute. */
5478

5479
        _cleanup_free_ char *executable = NULL;
5✔
5480
        _cleanup_close_ int executable_fd = -EBADF;
5✔
5481
        r = find_executable_full(command->path, /* root= */ NULL, context->exec_search_path, false, &executable, &executable_fd);
10,876✔
5482
        if (r < 0) {
10,876✔
5483
                *exit_status = EXIT_EXEC;
1✔
5484
                log_exec_struct_errno(context, params, LOG_NOTICE, r,
3✔
5485
                                      "MESSAGE_ID=" SD_MESSAGE_SPAWN_FAILED_STR,
5486
                                      LOG_EXEC_MESSAGE(params,
5487
                                                       "Unable to locate executable '%s': %m",
5488
                                                       command->path),
5489
                                      "EXECUTABLE=%s", command->path);
5490
                /* If the error will be ignored by manager, tune down the log level here. Missing executable
5491
                 * is very much expected in this case. */
5492
                return r != -ENOMEM && FLAGS_SET(command->flags, EXEC_COMMAND_IGNORE_FAILURE) ? 1 : r;
1✔
5493
        }
5494

5495
        r = add_shifted_fd(keep_fds, ELEMENTSOF(keep_fds), &n_keep_fds, &executable_fd);
10,875✔
5496
        if (r < 0) {
10,875✔
UNCOV
5497
                *exit_status = EXIT_FDS;
×
UNCOV
5498
                return log_exec_error_errno(context, params, r, "Failed to collect shifted fd: %m");
×
5499
        }
5500

5501
#if HAVE_SELINUX
5502
        if (needs_sandboxing && use_selinux && params->selinux_context_net) {
5503
                int fd = -EBADF;
5504

5505
                if (socket_fd >= 0)
5506
                        fd = socket_fd;
5507
                else if (params->n_socket_fds == 1)
5508
                        /* If stdin is not connected to a socket but we are triggered by exactly one socket unit then we
5509
                         * use context from that fd to compute the label. */
5510
                        fd = params->fds[0];
5511

5512
                if (fd >= 0) {
5513
                        r = mac_selinux_get_child_mls_label(fd, executable, context->selinux_context, &mac_selinux_context_net);
5514
                        if (r < 0) {
5515
                                if (!context->selinux_context_ignore) {
5516
                                        *exit_status = EXIT_SELINUX_CONTEXT;
5517
                                        return log_exec_error_errno(context,
5518
                                                                    params,
5519
                                                                    r,
5520
                                                                    "Failed to determine SELinux context: %m");
5521
                                }
5522
                                log_exec_debug_errno(context,
5523
                                                     params,
5524
                                                     r,
5525
                                                     "Failed to determine SELinux context, ignoring: %m");
5526
                        }
5527
                }
5528
        }
5529
#endif
5530

5531
        /* We repeat the fd closing here, to make sure that nothing is leaked from the PAM modules. Note that
5532
         * we are more aggressive this time, since we don't need socket_fd and the netns and ipcns fds any
5533
         * more. We do keep exec_fd and handoff_timestamp_fd however, if we have it, since we need to keep
5534
         * them open until the final execve(). But first, close the remaining sockets in the context
5535
         * objects. */
5536

5537
        exec_runtime_close(runtime);
10,875✔
5538
        exec_params_close(params);
10,875✔
5539

5540
        r = close_all_fds(keep_fds, n_keep_fds);
10,875✔
5541
        if (r >= 0)
10,875✔
5542
                r = pack_fds(params->fds, n_fds);
10,875✔
5543
        if (r >= 0)
10,875✔
5544
                r = flag_fds(params->fds, n_socket_fds, n_fds, context->non_blocking);
10,875✔
5545
        if (r < 0) {
10,875✔
5546
                *exit_status = EXIT_FDS;
×
UNCOV
5547
                return log_exec_error_errno(context, params, r, "Failed to adjust passed file descriptors: %m");
×
5548
        }
5549

5550
        /* At this point, the fds we want to pass to the program are all ready and set up, with O_CLOEXEC turned off
5551
         * and at the right fd numbers. The are no other fds open, with one exception: the exec_fd if it is defined,
5552
         * and it has O_CLOEXEC set, after all we want it to be closed by the execve(), so that our parent knows we
5553
         * came this far. */
5554

5555
        secure_bits = context->secure_bits;
10,875✔
5556

5557
        if (needs_sandboxing) {
10,875✔
5558
                uint64_t bset;
10,875✔
5559

5560
                /* Set the RTPRIO resource limit to 0, but only if nothing else was explicitly requested.
5561
                 * (Note this is placed after the general resource limit initialization, see above, in order
5562
                 * to take precedence.) */
5563
                if (context->restrict_realtime && !context->rlimit[RLIMIT_RTPRIO]) {
10,875✔
5564
                        if (setrlimit(RLIMIT_RTPRIO, &RLIMIT_MAKE_CONST(0)) < 0) {
1,812✔
UNCOV
5565
                                *exit_status = EXIT_LIMITS;
×
UNCOV
5566
                                return log_exec_error_errno(context, params, errno, "Failed to adjust RLIMIT_RTPRIO resource limit: %m");
×
5567
                        }
5568
                }
5569

5570
#if ENABLE_SMACK
5571
                /* LSM Smack needs the capability CAP_MAC_ADMIN to change the current execution security context of the
5572
                 * process. This is the latest place before dropping capabilities. Other MAC context are set later. */
5573
                if (use_smack) {
10,875✔
5574
                        r = setup_smack(context, params, executable_fd);
×
5575
                        if (r < 0 && !context->smack_process_label_ignore) {
×
UNCOV
5576
                                *exit_status = EXIT_SMACK_PROCESS_LABEL;
×
UNCOV
5577
                                return log_exec_error_errno(context, params, r, "Failed to set SMACK process label: %m");
×
5578
                        }
5579
                }
5580
#endif
5581

5582
                bset = context->capability_bounding_set;
10,875✔
5583

5584
#if HAVE_SECCOMP
5585
                /* If the service has any form of a seccomp filter and it allows dropping privileges, we'll
5586
                 * keep the needed privileges to apply it even if we're not root. */
5587
                if (needs_setuid &&
21,750✔
5588
                    uid_is_valid(uid) &&
12,900✔
5589
                    context_has_seccomp(context) &&
2,877✔
5590
                    seccomp_allows_drop_privileges(context)) {
852✔
5591
                        keep_seccomp_privileges = true;
852✔
5592

5593
                        if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
852✔
UNCOV
5594
                                *exit_status = EXIT_USER;
×
UNCOV
5595
                                return log_exec_error_errno(context, params, errno, "Failed to enable keep capabilities flag: %m");
×
5596
                        }
5597

5598
                        /* Save the current bounding set so we can restore it after applying the seccomp
5599
                         * filter */
5600
                        saved_bset = bset;
852✔
5601
                        bset |= (UINT64_C(1) << CAP_SYS_ADMIN) |
852✔
5602
                                (UINT64_C(1) << CAP_SETPCAP);
5603
                }
5604
#endif
5605

5606
                if (!cap_test_all(bset)) {
10,875✔
5607
                        r = capability_bounding_set_drop(bset, /* right_now= */ false);
1,992✔
5608
                        if (r < 0) {
1,992✔
5609
                                *exit_status = EXIT_CAPABILITIES;
×
UNCOV
5610
                                return log_exec_error_errno(context, params, r, "Failed to drop capabilities: %m");
×
5611
                        }
5612
                }
5613

5614
                /* Ambient capabilities are cleared during setresuid() (in enforce_user()) even with
5615
                 * keep-caps set.
5616
                 *
5617
                 * To be able to raise the ambient capabilities after setresuid() they have to be added to
5618
                 * the inherited set and keep caps has to be set (done in enforce_user()).  After setresuid()
5619
                 * the ambient capabilities can be raised as they are present in the permitted and
5620
                 * inhertiable set. However it is possible that someone wants to set ambient capabilities
5621
                 * without changing the user, so we also set the ambient capabilities here.
5622
                 *
5623
                 * The requested ambient capabilities are raised in the inheritable set if the second
5624
                 * argument is true. */
5625
                if (capability_ambient_set != 0) {
10,875✔
5626
                        r = capability_ambient_set_apply(capability_ambient_set, /* also_inherit= */ true);
846✔
5627
                        if (r < 0) {
846✔
UNCOV
5628
                                *exit_status = EXIT_CAPABILITIES;
×
UNCOV
5629
                                return log_exec_error_errno(context, params, r, "Failed to apply ambient capabilities (before UID change): %m");
×
5630
                        }
5631
                }
5632
        }
5633

5634
        /* chroot to root directory first, before we lose the ability to chroot */
5635
        r = apply_root_directory(context, params, runtime, needs_mount_namespace, exit_status);
10,875✔
5636
        if (r < 0)
10,875✔
UNCOV
5637
                return log_exec_error_errno(context, params, r, "Chrooting to the requested root directory failed: %m");
×
5638

5639
        if (needs_setuid) {
10,875✔
5640
                if (uid_is_valid(uid)) {
10,875✔
5641
                        r = enforce_user(context, uid, capability_ambient_set);
2,025✔
5642
                        if (r < 0) {
2,025✔
UNCOV
5643
                                *exit_status = EXIT_USER;
×
5644
                                return log_exec_error_errno(context, params, r, "Failed to change UID to " UID_FMT ": %m", uid);
×
5645
                        }
5646

5647
                        if (keep_seccomp_privileges) {
2,025✔
5648
                                if (!BIT_SET(capability_ambient_set, CAP_SETUID)) {
852✔
5649
                                        r = drop_capability(CAP_SETUID);
852✔
5650
                                        if (r < 0) {
852✔
UNCOV
5651
                                                *exit_status = EXIT_USER;
×
UNCOV
5652
                                                return log_exec_error_errno(context, params, r, "Failed to drop CAP_SETUID: %m");
×
5653
                                        }
5654
                                }
5655

5656
                                r = keep_capability(CAP_SYS_ADMIN);
852✔
5657
                                if (r < 0) {
852✔
UNCOV
5658
                                        *exit_status = EXIT_USER;
×
UNCOV
5659
                                        return log_exec_error_errno(context, params, r, "Failed to keep CAP_SYS_ADMIN: %m");
×
5660
                                }
5661

5662
                                r = keep_capability(CAP_SETPCAP);
852✔
5663
                                if (r < 0) {
852✔
UNCOV
5664
                                        *exit_status = EXIT_USER;
×
UNCOV
5665
                                        return log_exec_error_errno(context, params, r, "Failed to keep CAP_SETPCAP: %m");
×
5666
                                }
5667
                        }
5668

5669
                        if (capability_ambient_set != 0) {
2,025✔
5670

5671
                                /* Raise the ambient capabilities after user change. */
5672
                                r = capability_ambient_set_apply(capability_ambient_set, /* also_inherit= */ false);
845✔
5673
                                if (r < 0) {
845✔
UNCOV
5674
                                        *exit_status = EXIT_CAPABILITIES;
×
UNCOV
5675
                                        return log_exec_error_errno(context, params, r, "Failed to apply ambient capabilities (after UID change): %m");
×
5676
                                }
5677
                        }
5678
                }
5679
        }
5680

5681
        /* Apply working directory here, because the working directory might be on NFS and only the user
5682
         * running this service might have the correct privilege to change to the working directory. Also, it
5683
         * is absolutely 💣 crucial 💣 we applied all mount namespacing rearrangements before this, so that
5684
         * the cwd cannot be used to pin directories outside of the sandbox. */
5685
        r = apply_working_directory(context, params, runtime, pwent_home, accum_env);
10,875✔
5686
        if (r < 0) {
10,875✔
5687
                *exit_status = EXIT_CHDIR;
1✔
5688
                return log_exec_error_errno(context, params, r, "Changing to the requested working directory failed: %m");
3✔
5689
        }
5690

5691
        if (needs_sandboxing) {
10,874✔
5692
                /* Apply other MAC contexts late, but before seccomp syscall filtering, as those should really be last to
5693
                 * influence our own codepaths as little as possible. Moreover, applying MAC contexts usually requires
5694
                 * syscalls that are subject to seccomp filtering, hence should probably be applied before the syscalls
5695
                 * are restricted. */
5696

5697
#if HAVE_SELINUX
5698
                if (use_selinux) {
5699
                        char *exec_context = mac_selinux_context_net ?: context->selinux_context;
5700

5701
                        if (exec_context) {
5702
                                r = setexeccon(exec_context);
5703
                                if (r < 0) {
5704
                                        if (!context->selinux_context_ignore) {
5705
                                                *exit_status = EXIT_SELINUX_CONTEXT;
5706
                                                return log_exec_error_errno(context, params, r, "Failed to change SELinux context to %s: %m", exec_context);
5707
                                        }
5708
                                        log_exec_debug_errno(context,
5709
                                                             params,
5710
                                                             r,
5711
                                                             "Failed to change SELinux context to %s, ignoring: %m",
5712
                                                             exec_context);
5713
                                }
5714
                        }
5715
                }
5716
#endif
5717

5718
#if HAVE_APPARMOR
5719
                if (use_apparmor && context->apparmor_profile) {
5720
                        r = ASSERT_PTR(sym_aa_change_onexec)(context->apparmor_profile);
5721
                        if (r < 0 && !context->apparmor_profile_ignore) {
5722
                                *exit_status = EXIT_APPARMOR_PROFILE;
5723
                                return log_exec_error_errno(context,
5724
                                                            params,
5725
                                                            errno,
5726
                                                            "Failed to prepare AppArmor profile change to %s: %m",
5727
                                                            context->apparmor_profile);
5728
                        }
5729
                }
5730
#endif
5731

5732
                /* PR_GET_SECUREBITS is not privileged, while PR_SET_SECUREBITS is. So to suppress potential
5733
                 * EPERMs we'll try not to call PR_SET_SECUREBITS unless necessary. Setting securebits
5734
                 * requires CAP_SETPCAP. */
5735
                if (prctl(PR_GET_SECUREBITS) != secure_bits) {
10,874✔
5736
                        /* CAP_SETPCAP is required to set securebits. This capability is raised into the
5737
                         * effective set here.
5738
                         *
5739
                         * The effective set is overwritten during execve() with the following values:
5740
                         *
5741
                         * - ambient set (for non-root processes)
5742
                         *
5743
                         * - (inheritable | bounding) set for root processes)
5744
                         *
5745
                         * Hence there is no security impact to raise it in the effective set before execve
5746
                         */
5747
                        r = capability_gain_cap_setpcap(/* ret_before_caps = */ NULL);
905✔
5748
                        if (r < 0) {
905✔
UNCOV
5749
                                *exit_status = EXIT_CAPABILITIES;
×
UNCOV
5750
                                return log_exec_error_errno(context, params, r, "Failed to gain CAP_SETPCAP for setting secure bits");
×
5751
                        }
5752
                        if (prctl(PR_SET_SECUREBITS, secure_bits) < 0) {
905✔
5753
                                *exit_status = EXIT_SECUREBITS;
×
5754
                                return log_exec_error_errno(context, params, errno, "Failed to set process secure bits: %m");
×
5755
                        }
5756
                }
5757

5758
                if (context_has_no_new_privileges(context))
10,874✔
5759
                        if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
1,686✔
5760
                                *exit_status = EXIT_NO_NEW_PRIVILEGES;
×
UNCOV
5761
                                return log_exec_error_errno(context, params, errno, "Failed to disable new privileges: %m");
×
5762
                        }
5763

5764
#if HAVE_SECCOMP
5765
                r = apply_address_families(context, params);
10,874✔
5766
                if (r < 0) {
10,874✔
UNCOV
5767
                        *exit_status = EXIT_ADDRESS_FAMILIES;
×
UNCOV
5768
                        return log_exec_error_errno(context, params, r, "Failed to restrict address families: %m");
×
5769
                }
5770

5771
                r = apply_memory_deny_write_execute(context, params);
10,874✔
5772
                if (r < 0) {
10,874✔
UNCOV
5773
                        *exit_status = EXIT_SECCOMP;
×
UNCOV
5774
                        return log_exec_error_errno(context, params, r, "Failed to disable writing to executable memory: %m");
×
5775
                }
5776

5777
                r = apply_restrict_realtime(context, params);
10,874✔
5778
                if (r < 0) {
10,874✔
UNCOV
5779
                        *exit_status = EXIT_SECCOMP;
×
UNCOV
5780
                        return log_exec_error_errno(context, params, r, "Failed to apply realtime restrictions: %m");
×
5781
                }
5782

5783
                r = apply_restrict_suid_sgid(context, params);
10,874✔
5784
                if (r < 0) {
10,874✔
UNCOV
5785
                        *exit_status = EXIT_SECCOMP;
×
UNCOV
5786
                        return log_exec_error_errno(context, params, r, "Failed to apply SUID/SGID restrictions: %m");
×
5787
                }
5788

5789
                r = apply_restrict_namespaces(context, params);
10,874✔
5790
                if (r < 0) {
10,874✔
UNCOV
5791
                        *exit_status = EXIT_SECCOMP;
×
UNCOV
5792
                        return log_exec_error_errno(context, params, r, "Failed to apply namespace restrictions: %m");
×
5793
                }
5794

5795
                r = apply_protect_sysctl(context, params);
10,874✔
5796
                if (r < 0) {
10,874✔
UNCOV
5797
                        *exit_status = EXIT_SECCOMP;
×
UNCOV
5798
                        return log_exec_error_errno(context, params, r, "Failed to apply sysctl restrictions: %m");
×
5799
                }
5800

5801
                r = apply_protect_kernel_modules(context, params);
10,874✔
5802
                if (r < 0) {
10,874✔
UNCOV
5803
                        *exit_status = EXIT_SECCOMP;
×
UNCOV
5804
                        return log_exec_error_errno(context, params, r, "Failed to apply module loading restrictions: %m");
×
5805
                }
5806

5807
                r = apply_protect_kernel_logs(context, params);
10,874✔
5808
                if (r < 0) {
10,874✔
UNCOV
5809
                        *exit_status = EXIT_SECCOMP;
×
UNCOV
5810
                        return log_exec_error_errno(context, params, r, "Failed to apply kernel log restrictions: %m");
×
5811
                }
5812

5813
                r = apply_protect_clock(context, params);
10,874✔
5814
                if (r < 0) {
10,874✔
UNCOV
5815
                        *exit_status = EXIT_SECCOMP;
×
UNCOV
5816
                        return log_exec_error_errno(context, params, r, "Failed to apply clock restrictions: %m");
×
5817
                }
5818

5819
                r = apply_private_devices(context, params);
10,874✔
5820
                if (r < 0) {
10,874✔
UNCOV
5821
                        *exit_status = EXIT_SECCOMP;
×
UNCOV
5822
                        return log_exec_error_errno(context, params, r, "Failed to set up private devices: %m");
×
5823
                }
5824

5825
                r = apply_syscall_archs(context, params);
10,874✔
5826
                if (r < 0) {
10,874✔
5827
                        *exit_status = EXIT_SECCOMP;
×
5828
                        return log_exec_error_errno(context, params, r, "Failed to apply syscall architecture restrictions: %m");
×
5829
                }
5830

5831
                r = apply_lock_personality(context, params);
10,874✔
5832
                if (r < 0) {
10,874✔
UNCOV
5833
                        *exit_status = EXIT_SECCOMP;
×
UNCOV
5834
                        return log_exec_error_errno(context, params, r, "Failed to lock personalities: %m");
×
5835
                }
5836

5837
                r = apply_syscall_log(context, params);
10,874✔
5838
                if (r < 0) {
10,874✔
UNCOV
5839
                        *exit_status = EXIT_SECCOMP;
×
UNCOV
5840
                        return log_exec_error_errno(context, params, r, "Failed to apply system call log filters: %m");
×
5841
                }
5842
#endif
5843

5844
#if HAVE_LIBBPF
5845
                r = apply_restrict_filesystems(context, params);
10,874✔
5846
                if (r < 0) {
10,874✔
5847
                        *exit_status = EXIT_BPF;
×
5848
                        return log_exec_error_errno(context, params, r, "Failed to restrict filesystems: %m");
×
5849
                }
5850
#endif
5851

5852
#if HAVE_SECCOMP
5853
                /* This really should remain as close to the execve() as possible, to make sure our own code is affected
5854
                 * by the filter as little as possible. */
5855
                r = apply_syscall_filter(context, params);
10,874✔
5856
                if (r < 0) {
10,874✔
5857
                        *exit_status = EXIT_SECCOMP;
×
5858
                        return log_exec_error_errno(context, params, r, "Failed to apply system call filters: %m");
×
5859
                }
5860

5861
                if (keep_seccomp_privileges) {
10,874✔
5862
                        /* Restore the capability bounding set with what's expected from the service + the
5863
                         * ambient capabilities hack */
5864
                        if (!cap_test_all(saved_bset)) {
851✔
5865
                                r = capability_bounding_set_drop(saved_bset, /* right_now= */ false);
816✔
5866
                                if (r < 0) {
816✔
5867
                                        *exit_status = EXIT_CAPABILITIES;
×
5868
                                        return log_exec_error_errno(context, params, r, "Failed to drop bset capabilities: %m");
×
5869
                                }
5870
                        }
5871

5872
                        /* Only drop CAP_SYS_ADMIN if it's not in the bounding set, otherwise we'll break
5873
                         * applications that use it. */
5874
                        if (!BIT_SET(saved_bset, CAP_SYS_ADMIN)) {
851✔
5875
                                r = drop_capability(CAP_SYS_ADMIN);
384✔
5876
                                if (r < 0) {
384✔
UNCOV
5877
                                        *exit_status = EXIT_USER;
×
UNCOV
5878
                                        return log_exec_error_errno(context, params, r, "Failed to drop CAP_SYS_ADMIN: %m");
×
5879
                                }
5880
                        }
5881

5882
                        /* Only drop CAP_SETPCAP if it's not in the bounding set, otherwise we'll break
5883
                         * applications that use it. */
5884
                        if (!BIT_SET(saved_bset, CAP_SETPCAP)) {
851✔
5885
                                r = drop_capability(CAP_SETPCAP);
584✔
5886
                                if (r < 0) {
584✔
UNCOV
5887
                                        *exit_status = EXIT_USER;
×
UNCOV
5888
                                        return log_exec_error_errno(context, params, r, "Failed to drop CAP_SETPCAP: %m");
×
5889
                                }
5890
                        }
5891

5892
                        if (prctl(PR_SET_KEEPCAPS, 0) < 0) {
851✔
UNCOV
5893
                                *exit_status = EXIT_USER;
×
UNCOV
5894
                                return log_exec_error_errno(context, params, errno, "Failed to drop keep capabilities flag: %m");
×
5895
                        }
5896
                }
5897
#endif
5898

5899
        }
5900

5901
        if (!strv_isempty(context->unset_environment)) {
10,874✔
5902
                char **ee = NULL;
43✔
5903

5904
                ee = strv_env_delete(accum_env, 1, context->unset_environment);
43✔
5905
                if (!ee) {
43✔
UNCOV
5906
                        *exit_status = EXIT_MEMORY;
×
5907
                        return log_oom();
5✔
5908
                }
5909

5910
                strv_free_and_replace(accum_env, ee);
43✔
5911
        }
5912

5913
        if (!FLAGS_SET(command->flags, EXEC_COMMAND_NO_ENV_EXPAND)) {
10,874✔
5914
                _cleanup_strv_free_ char **unset_variables = NULL, **bad_variables = NULL;
10,736✔
5915

5916
                r = replace_env_argv(command->argv, accum_env, &replaced_argv, &unset_variables, &bad_variables);
10,736✔
5917
                if (r < 0) {
10,736✔
UNCOV
5918
                        *exit_status = EXIT_MEMORY;
×
UNCOV
5919
                        return log_exec_error_errno(context,
×
5920
                                                    params,
5921
                                                    r,
5922
                                                    "Failed to replace environment variables: %m");
5923
                }
5924
                final_argv = replaced_argv;
10,736✔
5925

5926
                if (!strv_isempty(unset_variables)) {
10,736✔
5927
                        _cleanup_free_ char *ju = strv_join(unset_variables, ", ");
10✔
5928
                        log_exec_warning(context,
15✔
5929
                                         params,
5930
                                         "Referenced but unset environment variable evaluates to an empty string: %s",
5931
                                         strna(ju));
5932
                }
5933

5934
                if (!strv_isempty(bad_variables)) {
10,736✔
UNCOV
5935
                        _cleanup_free_ char *jb = strv_join(bad_variables, ", ");
×
UNCOV
5936
                        log_exec_warning(context,
×
5937
                                         params,
5938
                                         "Invalid environment variable name evaluates to an empty string: %s",
5939
                                         strna(jb));
5940
                }
5941
        } else
5942
                final_argv = command->argv;
138✔
5943

5944
        log_command_line(context, params, "Executing", executable, final_argv);
10,874✔
5945

5946
        /* We have finished with all our initializations. Let's now let the manager know that. From this
5947
         * point on, if the manager sees POLLHUP on the exec_fd, then execve() was successful. */
5948

5949
        r = exec_fd_mark_hot(context, params, /* hot= */ true, exit_status);
10,874✔
5950
        if (r < 0)
10,874✔
5951
                return r;
5952

5953
        /* As last thing before the execve(), let's send the handoff timestamp */
5954
        r = send_handoff_timestamp(context, params, exit_status);
10,874✔
5955
        if (r < 0) {
10,874✔
5956
                /* If this handoff timestamp failed, let's undo the marking as hot */
UNCOV
5957
                (void) exec_fd_mark_hot(context, params, /* hot= */ false, /* reterr_exit_status= */ NULL);
×
5958
                return r;
5959
        }
5960

5961
        /* NB: we leave executable_fd, exec_fd, handoff_timestamp_fd open here. This is safe, because they
5962
         * have O_CLOEXEC set, and the execve() below will thus automatically close them. In fact, for
5963
         * exec_fd this is pretty much the whole raison d'etre. */
5964

5965
        r = fexecve_or_execve(executable_fd, executable, final_argv, accum_env);
10,874✔
5966

5967
        /* The execve() failed, let's undo the marking as hot */
5968
        (void) exec_fd_mark_hot(context, params, /* hot= */ false, /* reterr_exit_status= */ NULL);
3✔
5969

5970
        *exit_status = EXIT_EXEC;
3✔
5971
        return log_exec_error_errno(context, params, r, "Failed to execute %s: %m", executable);
9✔
5972
}
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