Commit Graph
270 Commits
Author SHA1 Message Date
Mike Yuan b34aee0440 core/exec-invoke: socket_fd must be set if any of stdio requests it
This is checked as one of the very first things in exec_invoke(),
hence remove the condition in fixup_input() and the whole
fixup_output().
2025-12-17 23:23:22 +00:00
Mike Yuan c8a6c42e53 core/exec-invoke: use path_equal() on for stdio files 2025-12-17 23:23:22 +00:00
Mike Yuan eac3867764 core/exec-invoke: fix stdout and stderr sharing the same named fd
Previously such usage would be refused by exec_context_named_iofds()
with -ENOENT, however setup_output() carried relevant logic.
2025-12-17 23:23:22 +00:00
Yu Watanabe 93378148d4 tree-wide: normalize comment style 2025-12-15 08:50:13 +09:00
Lennart Poettering 81e79ee499 exec-invoke: fix type for gid 2025-12-02 13:34:05 +09:00
Daan De Meyer 9a6a8f35eb tree-wide: Small fixes for various clang-tidy warnings 2025-11-26 16:22:00 +09:00
Lennart Poettering efaf5a763d execute: load a bunch of libs before we disable dlopen() 2025-11-24 11:56:35 +01:00
Lennart Poettering 2c7bdaf9f1 dlfcn-util: let's make our dlopen() code fail if we enter a container namespace
Now that we dlopen() so many deps, it might happen by accident that we
end up dlopen()ening stuff when we entered a container, which we should
really avoid, to not mix host and container libraries.

Let's add a global variable we can set when we want to block dlopen() to
ever succeed. This is then checked primarily in
dlopen_many_sym_or_warn(), where we'll generate EPERM plus a log
message.

There are a couple of other places we invoke dlopen(), without going
through dlopen_many_sym_or_warn(). This adds the same check there.
2025-11-24 09:19:33 +01:00
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