Commit Graph
262 Commits
Author SHA1 Message Date
Mike Yuan 32b7a07b8c core/exec-invoke: deduplicate needs_setuid with needs_sandboxing 2025-11-22 19:07:28 +01:00
Daan De Meyer 87fbd33372 clang-tidy: Fix all remaining misc-include-cleaner violations
- Remove unused includes
- Add common false positive headers to misc-include-cleaner.IgnoreHeaders
- Add IWYU pragma keep for uncommon false positive headers
2025-11-22 10:19:41 +01:00
Lennart Poettering c3b3eea2e5 apparmor: move dlopen() into mac_apparmor_use() check
This mirrors what we do for mac_selinux_use(), which also loads
libselinux.
2025-11-20 14:19:56 +01:00
Chris Down 171ceb4a00 core: Verify inherited FDs are writable for stdout/stderr
When inheriting file descriptors for stdout/stderr (either from stdin
or when making stderr inherit from stdout), we previously just assumed
they would be writable and dup'd them. This could lead to broken setups
if the inherited FD was actually opened read-only.

Before dup'ing any inherited FDs to stdout/stderr, verify they are
actually writable using the new fd_is_writable() helper. If not, fall
back to /dev/null (or reopen the terminal in the TTY case) with a
warning, rather than silently creating a broken setup where output
operations would fail.
2025-11-20 02:02:21 +08:00
Zbigniew Jędrzejewski-Szmek a50fdf611c core: improve messages about unknown users and groups
$ sudo build/systemd-run --uid=asdf whoami
$ journalctl -e
(whoami)[1007784]: run-p1007782-i5200512.service: Failed to determine user credentials: No such process
(whoami)[1007784]: run-p1007782-i5200512.service: Failed at step USER spawning /usr/sbin/whoami: No such process
systemd[1]: run-p1007782-i5200512.service: Main process exited, code=exited, status=217/USER
systemd[1]: run-p1007782-i5200512.service: Failed with result 'exit-code'.

Now:
(whoami)[1013204]: run-p1013202-i5205932.service: Failed to determine credentials for user 'asdf': Unknown user
(whoami)[1013204]: run-p1013202-i5205932.service: Failed at step USER spawning /usr/sbin/whoami: Invalid argument
systemd[1]: run-p1013202-i5205932.service: Main process exited, code=exited, status=217/USER
systemd[1]: run-p1013202-i5205932.service: Failed with result 'exit-code'.
2025-11-18 16:23:29 +01:00
Yu Watanabe a8156588fa cgroup-util: drop 'controller' argument from cg_get_path()
The argument is not used anymore. Let's drop it.
2025-11-17 21:31:50 +09:00
Lennart Poettering 08d75cfb99 dissect-image,execute: switch root hash/root hash sig storage to struct iovec
let's go one step further on our iovec'ification journey.

No change in behaviour, just rework to make struct iovec used
everywhere.
2025-11-16 23:36:11 +01:00
Yu Watanabe 9b55c4b859 tree-wide: drop redundant inclusion of linux/prctl.h
sys/prctl.h anyway includes linux/prctl.h and actually these .c files
includes sys/prctl.h. Hence, it is not necessary to explicitly include
linux/prctl.h.
2025-11-12 14:17:44 +08:00
Mike Yuan 1d73c596fa core/exec-invoke: do not check array being non-NULL
Addresses
https://download.copr.fedorainfracloud.org/results/packit/systemd-systemd-39680/fedora-rawhide-i386/09787959-systemd/builder-live.log.gz
2025-11-11 22:41:24 +00:00
Mike Yuan 89e74275ad core/execute: morph exec_output_is_* to _forward_to_*
Follow-up for 379d9ae222

After the commit, the functions are only used to determine
whether journals shall be forwarded to selected targets,
hence rename as such and remove effectively unused condition
on EXEC_OUTPUT_TTY.
2025-11-08 08:54:13 +09:00
Yu Watanabe f0fdb69a8d core: assign TTY to PAM context when TTYPath= is specified
Fixes #38486 again, which was fixed by
1405d46bf9, but regressed again by
f875a8026e.

This also make it use exec_input_is_terminal().

Follow-up for f875a8026e.
2025-11-08 06:12:20 +09:00
Mike Yuan 96d4750e7e core/exec-invoke: pass the correct pid (1) to processes in pidns 2025-11-05 17:47:42 +01:00
Chris Down 666cd35be4 core: Only apply unprivileged userns logic to user managers
Commit 38748596f0 ("core: Make DelegateNamespaces= work for user
managers with CAP_SYS_ADMIN") refactored the logic for when an
unprivileged process should create a new user namespace for sandboxing.

This refactor inadvertently removed a check (`params->runtime_scope !=
RUNTIME_SCOPE_USER`) that differentiated between system services and user
services.

This causes a regression in rootless containers where systemd runs
unprivileged. When starting a system service (like `dbus-broker`) that
uses sandboxing features (eg. with `PrivateTmp=yes`), systemd now
incorrectly creates a new, minimal `PRIVATE_USERS_SELF` namespace.

This new namespace only maps UID/GID 0. When dbus-broker attempts to
drop privileges to the `dbus` user (GID 81), the `setresgid(81, 81, 81)`
call fails because GID 81 is not mapped.

Restore the check to ensure that the special unprivileged sandboxing
logic is only applied to user services, as was the original intent.
System services in a rootless context will now correctly run in the
container's main user namespace, where all necessary UIDs/GIDs are
mapped.

Fixes: https://github.com/systemd/systemd/issues/39563
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2391343
2025-11-05 21:12:52 +08:00
Yu Watanabe d954161f9b core/exec-invoke: gracefully handle lack of privilege for initgroups() in user mode (#39039)
Fixes #39038
2025-11-05 02:53:05 +09:00
Quentin Deslandes 79dd24cf14 core: Add UserNamespacePath=
This allows a service to reuse the user namespace created for an
existing service, similarly to NetworkNamespacePath=. The configuration
is the initial user namespace (e.g. ID mapping) is preserved.
2025-11-04 10:55:04 +01:00
Yu Watanabe cbd8a12bce Several cleanups around strv_extendf() (#39508) 2025-11-01 18:10:42 +09:00
Daan De Meyer dd15dd1a58 core: Add RootDirectoryFileDescriptor= (#39480)
RootDirectory= but via a open_tree() file descriptor. This allows
setting up the execution environment for a service by the client in a
mount namespace and then starting a transient unit in that execution
environment using the new property.

We also add --root-directory= and --same-root-dir= to systemd-run to
have it run services within the given root directory. As systemd-run
might be invoked from a different mount namespace than what systemd is
running in, systemd-run opens the given path with open_tree() and then
sends it to systemd using the new RootDirectoryFileDescriptor= property.
2025-10-31 22:30:46 +01:00
Mike Yuan f8574e252a core/exec-invoke: correct fd array size for collect_open_file_fds()
Follow-up for f70346fb87
2025-10-31 17:32:34 +00:00
Yu Watanabe bafbcd3b27 core/exec-invoke: use heap allocation to build environment variables
We sometimes forget to increase the size N_ENV_VARS. Let's use heap
allocation.
2025-11-01 00:59:41 +09:00
Daan De Meyer 9e26ced980 core: Add RootDirectoryFileDescriptor=
RootDirectory= but via a open_tree() file descriptor. This allows
setting up the execution environment for a service by the client in
a mount namespace and then starting a transient unit in that execution
environment using the new property.

We also add --root-directory= and --same-root-dir= to systemd-run to
have it run services within the given root directory. As systemd-run
might be invoked from a different mount namespace than what systemd is
running in, systemd-run opens the given path with open_tree() and then
sends it to systemd using the new RootDirectoryFileDescriptor= property.
2025-10-31 13:09:51 +01:00
Mike Yuan a274cb0cff core/exec-invoke: switch keep_fds to heap allocation
Hardcoding total size of the array is error-prone, especially
considering the exeuctable_fd is added far below, so the '4' is
not entirely obvious. Also we seldomly do VLAs.
2025-10-30 17:47:30 +01:00
Mike Yuan f70346fb87 core/exec-invoke: store all stashed fds in ExecParameters, incl. OpenFile= ones
Keeping a half-detached counter around brings nothing
but confusion, and leads to fd leak in error paths.
2025-10-30 17:47:30 +01:00
Mike Yuan f4314f2fbc core/exec-invoke: do not discard stashed fds when stdio is connected to socket
This makes zero sense. Not sure how it got introduced...
2025-10-30 17:47:30 +01:00
Mike Yuan d85d98f406 core/execute: merge n_storage_fds and n_extra_fds into stashed_fds
The distinction between fdstore and extra fds is only meaningful
to struct Service. As far as executor is concerned they're just
some fds to pass to the service. Let's just merge it hence,
for the sake of simplicity.
2025-10-30 17:47:29 +01:00
Mike Yuan 75e05a9880 core/exec-invoke: rename process earlier
This is independent of any other setup stages, and should
happen as early as possible to make comm logged by journald
accurate.
2025-10-30 17:45:34 +01:00
Mike Yuan edb8fcd813 core/exec-invoke: set exit_status on exec_context_named_iofds() failure 2025-10-30 16:12:14 +01:00
Mike Yuan 04072ad9ed core/exec-invoke: do not attempt to use fdstore/extra fds for stdio
According to systemd.exec(5):

> The fd:name option connects standard input to a specific, named
> file descriptor provided *by a socket unit*. ...

Currently however we're looking at the whole fd array passed,
fix it.
2025-10-30 16:12:14 +01:00
Mike Yuan 234d8f8bc3 core/exec-invoke: drop redundant stdio_fdname checks
exec_context_fdname() would never return NULL if corresponding
stdio mode is set to named fd.
2025-10-30 16:12:14 +01:00
Daan De Meyer 35e03f6302 core: Don't setup mount propagation tunnel if not required
If we know we have mount_setattr(), then we don't need the mount
propagation tunnel, so don't set it up.
2025-10-29 12:01:02 +01:00
Mike Yuan c86914667a core/exec-invoke: gracefully handle lack of privilege for initgroups() in user mode
Otherwise specifying User=SELF also fails because we got
no privilege to call setgroups().

Fixes #39038
2025-10-25 21:18:11 +02:00
Mike Yuan a169a952b6 core/exec-invoke: use strnpcpy() where appropriate 2025-10-25 19:31:06 +02:00
Yu Watanabe f03b49b079 core/exec-invoke: relax restriction for process name length
Previously, we limit the length of process name by 8.
This relax the restriction then at least process comm or
program_invocation_name contains the untrucated process name.

Closes #38367.
2025-10-25 18:50:32 +02:00
Yu Watanabe 256d6f3f2f capability-util: rework capability_gain_cap_setpcap() and capability_bounding_set_drop()
This makes the functions use CapabilityQuintet, capability_get(), and
capability_apply().
2025-10-24 01:52:59 +09:00
Daniel Foster c7a444a9c1 tree-wide: extend $LISTEN_FDS protocol with $LISTEN_PIDFDID
Although extremely unlikely, there is a race present in solely checking the
$LISTEN_PID environment variable, due to PID recycling. Fix that by introducing
$LISTEN_PIDFDID, which contains the 64-bit ID of a pidfd for the child process
that is not subject to recycling.
2025-10-22 09:34:14 +02:00
Luca Boccassi ac9391c552 Support ExtensionImages=/MountImages= in user services via mountfsd and PrivateUsers=yes (#39341) 2025-10-16 15:49:46 +01:00
Luca Boccassi 68b476a298 core: also enable PrivateUsers= for user services when using images via mountfsd
RootDirectory= and other options already implicitly enable PrivateUsers=
since 6ef721cbc7 if they are set in user
units, so that they can work out of the box.
Now with mountfsd support we can do the same for the images settings,
so enable them and document them.
2025-10-16 12:58:59 +01:00
Daan De Meyer f875a8026e core: Don't use TTYPath= for PAM unless StandardInput=tty
Fixes #39334
2025-10-16 13:46:19 +02:00
Lennart Poettering 83b6ef9b62 libselinux: turn into dlopen() dep 2025-09-26 11:12:04 +02:00
Lennart Poettering aaca6bd5d9 libseccomp: turn into dlopen() dependency 2025-09-26 03:30:11 +09:00
Lennart Poettering 882c9ce040 pam: make libpam a dlopen() based dependency 2025-09-20 08:41:20 +02:00
Daan De Meyer 9adb4685df tree-wide: Remove unused includes 2025-09-19 14:46:55 +02:00
Yu Watanabe dac478c44c core: fix typo
Follow-up for b3415f5dae.
2025-08-26 04:54:19 +09:00
Yu WatanabeandLuca Boccassi 4279336618 chase: trigger automount only when explicitly requested
Since c5de7b14ae
file searching implies a new mount api syscall by default,
to trigger automounts.

But, this is not necessary in most cases, e.g. when chasing
syspath in sd-device (actually this causes regression in umockdev,
see https://github.com/martinpitt/umockdev/issues/271).
Another example is reading unit files, especially .network files,
as automount may trigger mounting network filesystems...

Also, when this is used in NSS plugins, programs that load the
plugins may fail because of spuriously configured seccomp. See #38565.

Let's not trigger automount by default, and do only when explicitly
requested.

This introduces CHASE_TRIGGER_AUTOFS, and use it in
- service manager,
- bootctl and finding ESP/xbootldr,
- sysupdate,
- mountfsd,
- systemd-mount.

There may be several more places we should trigger automount, but let's
do that later.

Follow-up for c5de7b14ae.
Fixes #38565.
Replaces #38569.

Co-authored-by: Luca Boccassi <luca.boccassi@gmail.com>
2025-08-20 02:32:02 +09:00
Yu Watanabe 2e7d7e929e Revert "chase: invert CHASE_NO_AUTOFS and only set it where needed"
This reverts commit 490aa05ca1.

As commented https://github.com/systemd/systemd/pull/38569#discussion_r2284978273,
the commit makes autofs check bypassed. Before the commit, when
CHASE_NO_AUTOFS is set, we did not shortcut chasing paths, and refused
any autofs mount points in the path. However, with the commit, the flag
was swapped but even when CHASE_AUTOFS is unset, the autofs check may be
skipped.

To fix the issue, rather than swapping the flag, we should introduce
another flag, say CHASE_TRIGGER_AUTOFS. This revert the commit, and in a
later commit, the new flag will be introduced.
2025-08-20 02:15:36 +09:00
Luca Boccassi 490aa05ca1 chase: invert CHASE_NO_AUTOFS and only set it where needed
Since https://github.com/systemd/systemd/commit/c5de7b14ae2e08d267d8d75bc88934ac6aa7dcd6
file searching implies a new mount api syscall by default,
to trigger automounts.

This is problematic in NSS plugins, as they are dlopen'ed inside
processes by glibc, for two reasons.

First of all, potentially searching on a networked filesystem
automount could lead to nasty surprises, such as the process
responsible for setting up the network filesystem trying to
search on that same filesystem.

More importantly, the new mount api syscall was never part of
the filesystem seccomp filter that we provide by default, and
given mounting/remounting/bind mounting is one of the possible
ways to bypass sandboxing it is very likely not allowed when
custom filters are used in sandboxed processes, if they don't
need to do these operations otherwise.
The filesystem seccomp mask we provide has been updated, however
this only takes effect on the next restart of a service. When
systemd is upgraded via a package upgrade, the new nss plugin is
installed and will be immediately dlopen'ed by glibc when needed,
without waiting for the process to restart, which means the existing
seccomp filter applies, causing the filter to trigger.
Given it's not really possible for any arbitrary program to
predict which NSS modules glibc will load, given programs do not
configure that and instead nsswitch is set up by the sysadmin,
it's impossible to handle at each process level. It's also not
possible to know when it will be triggered, given the plugin
is not linked in each binary tools like need-restart cannot
even pre-emptively restart services that may be affected.

This means in practice, upgrading from systemd << v258 to >= v258
requires a reboot to avoid either subtle or catastrophic system
failures.

By avoiding to trigger automounts in nss-systemd we can avoid
both issues.

userdb drop-ins are searched for in:

/etc/userdb/
/run/userdb/
/run/host/userdb/
/usr/local/lib/userdb/
/usr/lib/userdb/

none of which are supported as automounts anyway.

Note that this happens only when the userdbd service is not running,
as otherwise nss-systemd will go through the varlink IPC, rather than
doing the searches in-process.

So invert CHASE_NO_AUTOFS to CHASE_AUTOFS and set it in the places where
we do want to trigger automounts, like looking for the ESP.

Follow-up for https://github.com/systemd/systemd/commit/c5de7b14ae2e08d267d8d75bc88934ac6aa7dcd6
Fixes https://github.com/systemd/systemd/issues/38565
2025-08-19 16:48:13 +02:00
Yu Watanabe 379d9ae222 core: do not touch tty when StandardOutput=/StandardError=journal+console/kmsg+console 2025-08-13 23:44:42 +09:00
Yu Watanabe da34c27bb1 core: make is_terminal_input() and friends inline
Then, rename them to exec_input_is_terminal() and so on.
2025-08-13 23:43:52 +09:00
Matteo Croce 814bff5e28 core: suppress warning
Avoid definition of `exec_context_get_tty_for_pam` if pam support is
disabled, to avoid the following warning:
```
../src/core/exec-invoke.c:1231:12: warning: ‘exec_context_get_tty_for_pam’ defined but not used [-Wunused-function]
 1231 | static int exec_context_get_tty_for_pam(const ExecContext *context, char **ret) {
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
2025-08-12 19:45:04 +01:00
Yu Watanabe 1405d46bf9 core/exec-invoke: fallback to set TTY specified by TTYPath= to PAM
Follow-up for 2b0087e5b1.
Fixes #38486.
2025-08-11 10:26:51 +09:00
Yu Watanabe 1f4e9f6d60 core/exec-invoke: check size of read size
Even though we do not use the read data, it is better to check the size
to prevent something spurious going.

Fixes CID#1612155.
2025-07-26 03:44:49 +09:00