Commit Graph
344 Commits
Author SHA1 Message Date
Michael Vogt 18e70eec2f basic: make check-pointer-deref clean
Add the needed assert changes to make the code clean
for the new check-pointer-deref script.

Note that for `_cmp()` functions its a bit of a question
if we want `assert()` or `POINTER_MAY_BE_NULL()` here. The
former is more correct but it adds a (small) runtime hit. If
we use POINTER_MAY_BE_NULL() it is compiled out but strictly
speaking it is wrong (so at least we would need a comment).
2026-03-31 18:30:40 +02:00
Mike Yuan abffc0ef43 process-util: leave actual log fd closing to close_all_fds() if the latter will be done anyways 2026-03-01 13:18:19 +01:00
Daan De Meyer a12c5bf4f5 sd-bus: Make sure we can connect to user machines as well
Don't unconditionally look into /run/systemd/machines. If we're a
connected to a session bus, look at the machines for the current user
instead.
2026-02-17 17:52:20 +01:00
Daan De Meyer 4e40c2a515 namespace-util: Merge namespace_enter_delegated() into namespace_enter() (#40669)
There's no need to pass in a boolean to decide whether we use
namespace_enter_delegated() or not. Instead, we can just check if we
have CAP_SYS_ADMIN in our own user namespace. If we don't, then we have
to insist on a child user namespace being passed in and we have to enter
it first to get CAP_SYS_ADMIN as without CAP_SYS_ADMIN we wouldn't be
able
to call setns() in the first place. If we do have CAP_SYS_ADMIN, we can
always enter the other namespaces first before entering the user
namespace.

Additionally, we don't fail anymore if we can't reset the UID/GID since
a
root user might not always be available in every user namespace we might
enter.
2026-02-16 11:26:41 +01:00
Daan De Meyer 211c3c9d41 namespace-util: Merge namespace_enter_delegated() into namespace_enter()
There's no need to pass in a boolean to decide whether we use
namespace_enter_delegated() or not. Instead, we can just check if we
have CAP_SYS_ADMIN in our own user namespace. If we don't, then we have
to insist on a child user namespace being passed in and we have to enter
it first to get CAP_SYS_ADMIN as without CAP_SYS_ADMIN we wouldn't be able
to call setns() in the first place. If we do have CAP_SYS_ADMIN, we can
always enter the other namespaces first before entering the user namespace.

Additionally, we don't fail anymore if we can't reset the UID/GID since a
root user might not always be available in every user namespace we might
enter.
2026-02-16 10:27:02 +01:00
Mike Yuan 36f82366d0 process-util: group oom_score_adjust_is_valid() with getter/setter 2026-02-16 09:44:53 +01:00
Mike Yuan 8f89ed439d process-util: teach namespace_fork() to optionally use namespace_enter_delegated() 2026-02-10 21:54:12 +01:00
Yu Watanabe b7dd507cae tree-wide: use consistent format of short comment for constant argument
Follow-ups for recent changes after 93378148d4.
2026-01-20 00:22:15 +09:00
cyclopentane 465f6a6284 cryptenroll,cryptsetup,shutdown: only call mlockall if we have CAP_IPC_LOCK
Calling mlockall in an unprivileged process most notably had the effect
of making systemd-cryptenroll OOM while trying to open a normal-sized
argon2 keyslot due to it hitting RLIMIT_MEMLOCK.
2026-01-18 19:15:30 +01:00
Yu Watanabe fce7ad481b process-util: drop unnecessary E2BIG error handling
E2BIG is returned when the kernel does not support CLONE_INTO_CGROUP,
but the flag is supported since kernel v5.7.
2026-01-13 10:21:36 +09:00
Zbigniew Jędrzejewski-Szmek 4a4be1015b inhibit: fix borked double logging on error
Previously, if execution failed, we'd log at error level both from the
child and the parent, and we were using a bogus variable for the argument
name:
$ build/systemd-inhibit list
Failed to execute : No such file or directory
list failed with exit status 1.

In general, we can and should assume that the program the user is calling
is well behaved, so it'll log the error on its own if appropriate. So we
shouldn't log on "normal errors", but only if the child is terminated by
a signal.

And since the program name is controlled by the user, use quotes everywhere
to avoid ambiguity.

Now:
$ build/systemd-inhibit false
(nothing)
$ build/systemd-inhibit bash -c 'kill -SEGV $$'
src/basic/process-util.c:895: 'bash' terminated by signal SEGV.
2026-01-04 14:07:57 +01:00
Zbigniew Jędrzejewski-Szmek 22aa8c4879 basic/process-util: reduce scope of variables 2026-01-04 14:07:57 +01:00
Zbigniew Jędrzejewski-Szmek 1ee73c884e basic/process-util: use synthetic errno in two more places 2026-01-04 14:07:57 +01:00
Yu Watanabe 30b9fa495c clang-tidy: Enable more warnings (#39910) 2026-01-03 06:00:30 +09:00
Daan De Meyer 52ecf376fa tree-wide: Migrate to pidref_safe_fork()
Let's migrate all remaining callers of safe_fork()
to pidref_safe_fork() and get rid of safe_fork().
2026-01-02 20:08:41 +01:00
Daan De Meyer e6703a9aa4 process-util: Use ret as output parameter name
There's only one output parameter for all these
functions, so let's just name it ret following the
coding style.
2026-01-02 19:44:18 +01:00
Daan De Meyer 18842be4be tree-wide: Various coding style cleanups 2026-01-02 09:22:59 +01:00
Mike Yuan eb17c37df7 process-util: drop unused sched_priority_is_valid() 2025-12-20 18:38:56 +01:00
Matt Fleming 4dcbfbb1ad process-util: Add support SCHED_EXT scheduling policy
Allow CPUSchedulingPolicy to be set to "ext". SCHED_EXT is a new
scheduling policy in Linux v6.12 that allows processes to be scheduled
using custom BPF schedulers instead of the default in-kernel ones.

Selectively setting the SCHED_EXT policy is useful for systems running
in "partial mode" where not all processes are run using a custom
scheduler.

Fallback to SCHED_OTHER and print an error message for systems where
SCHED_EXT isn't available.
2025-12-20 18:31:55 +01:00
Daan De Meyer fbd276cb86 tree-wide: Use pidref_wait_for_terminate_and_check()
Let's use the pidref variants for waiting and
checking a process everywhere and get rid of the
non pidref variants.
2025-12-20 15:50:47 +01:00
Daan De Meyer e74b571004 pidref: Add pidref_wait_for_terminate_full()
We can use this to replace wait_for_terminate_with_timeout(). Because
we poll a pidfd, we don't need to block SIGCHLD anymore in umount.c.
2025-12-20 15:49:26 +01:00
Daan De Meyer 8c367868ee process-util: Use pidref_wait_for_terminate_and_check in pidref_safe_fork()
Note that we still have to block SIGCHLD so that
we can be certain the process is not reaped before
we get the pidfd to it. safe_fork() and friends are
used in libsystemd where we don't control how the
SIGCHLD signal is configured. Specifically, kernel
autoreaping could be enabled which is why we have
to block SIGCHLD until we get the pidfd so that the
kernel cannot autoreap the process before we get
the pidfd.
2025-12-20 15:44:20 +01:00
Mike Yuan 73c40aa1f6 process-util: synchronously wait for namespace_enter() before returning
Additionally, report actual errors using errno pipe too.
2025-12-20 14:27:46 +01:00
Mike Yuan ed0f6758c8 process-util: revamp flags handling in namespace_fork()
* Specifying all 3 of FORK_DEATHSIG_SIG{KILL,TERM,INT} for
  the middle man makes zero sense. Use SIGKILL only.
* Make sure operations on except_fds work sensibly - close/pack/
  de-ocloexecify fds only in the second level, so that the namespace
  fds remain usable across first safe_fork().
* Fire FORK_NEW_*NS after attaching to the desired namespaces,
  not already in the outer process.
* Insist on PDEATHSIG being enabled to ensure propagation of killing.
* Suppress more redundant flags.
2025-12-20 14:14:24 +01:00
Daan De MeyerandMike Yuan 8c007f68ff process-util: Migrate namespace_fork() to PidRef
Co-authored-by: Mike Yuan <me@yhndnzj.com>
2025-12-20 13:45:22 +01:00
Mike Yuan 3532691d72 process-util: trivial cleanup for read_errno() 2025-12-20 13:45:22 +01:00
Mike Yuan e48e098db1 process-util: dedup restore_sigsetp() with block_signals_reset() 2025-12-20 13:45:21 +01:00
Mike Yuan 29a823b3b6 process-util: add a flag for retaining dlopen()-ability
While blocking dlopen() in child generally makes sense, it does
also lead to misery - in the very case of extension/cred refreshing
it then enforces loading of libcryptsetup/libacl in pid1, which
are otherwise never used. Let's add a flag to opt out hence, but
still prohibit it in namespace_fork/enter().
2025-12-16 21:07:20 +01:00
Yu Watanabe 93378148d4 tree-wide: normalize comment style 2025-12-15 08:50:13 +09:00
Yu Watanabe 05afbcc377 process-util: fix typo
Follow-up for a08f2ea02d.
2025-11-25 22:52:10 +09:00
Lennart Poettering a08f2ea02d process-util: also disable dlopen() in safe_fork() 2025-11-24 11:56:47 +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
Yu Watanabe 0ab90015e0 tree-wide: replace cg_get_path_and_check() with cg_get_path()
We have dropped cgroup v1 support in v258. When running on cgroup v2,
cg_get_path_and_check() with SYSTEMD_CGROUP_CONTROLLER as controller is
equivalent with checking if we are running on cgroup v2 and then
cg_get_path(). As we can assume we are running on cgroup v2, then the
check is not necessary anymore, thus we can replace
cg_get_path_and_check() with cg_get_path().
2025-11-17 21:30:29 +09:00
Mike Yuan 686117e5b0 process-util: prefix FORK_PID_ONLY with _ to signify it is internal 2025-11-10 02:44:57 +01:00
Yu Watanabe 543a48b653 libc-wrapper: introduce a tiny libc wrapper
Then, move syscall definitions to the wrapper, and prototypes are moved
to relevant headers.

This also adds checks for add_key() and request_key(), as one day
glibc may be going to add some of them separatedly.

The check for fspick in meson.build is dropped, as it is currently
unused in our code.

This also moves
- basic/missing_bpf.h -> include/override/linux/bpf.h,
- basic/missing_keyctl.h -> include/override/linux/keyctl.h.
2025-07-11 13:05:46 +09:00
Yu Watanabe 237bdd2771 basic: move missing_sched.h -> include/sched.h 2025-07-01 12:29:15 +09:00
Daan De Meyer 0c15577abe basic + fundamental: Clean up includes
Split out of #37344.
2025-05-25 10:06:07 +02:00
Mike Yuan d6c8e0ce95 process-util: use procfs_file_get_field() where appropriate 2025-05-01 13:10:26 +09:00
Mike Yuan 15036f8555 fileio: modernize get_proc_field()
- Drop effectively unused "terminator" param, imply whitespace
- Make ret param optional
- Return ENODATA if the requested key is not found, rather than
  ENOENT
- Turn ENOENT -> ENOSYS if /proc/ is not mounted
- Don't skip whitespaces before ':', nothing needs this handling
  anyways
- Remove the special treatment for all "0"s. We don't actually
  use this for capabilities given pidref_get_capability() exists
- Switch away from read_full_virtual_file() - files using "field"
  scheme under /proc/ seem all to be "seq_file"s (refer to
  da65941c3e for details on file types)
2025-05-01 13:10:26 +09:00
Yu Watanabe 147f1d7b26 process-util: make wait_for_terminate() as trivial wrapper of its PidRef version 2025-04-03 00:11:27 +09:00
Lennart Poettering 4ecc87bf1c process-util: add pidref-based version of wait_for_terminate_and_check() 2025-04-03 00:11:27 +09:00
Yu Watanabe 3cf6a3a3d4 tree-wide: check more log message format in log_struct() and friends
This introduce LOG_ITEM() macro that checks arbitrary formats in
log_struct().
Then, drop _printf_ attribute from log_struct_internal(), as it does not
help so much, and compiler checked only the first format string.

Hopefully, this silences false-positive warnings by Coverity.
2025-03-19 01:56:48 +09:00
Mike Yuan 33db9f214b missing_syscall: drop raw_getpid()
This used to be relevant since in old versions of glibc an internal
cache is maintained, while we might sidestep their invalidation
with raw_clone(). After glibc 2.25 getpid() is a trivial wrapper
for the syscall, and hence there's no need to have a separate
raw_getpid().
2025-03-04 23:03:24 +01:00
Yu Watanabe e75372958d missing_threads.h: threads.h exists since glibc-2.28 2025-03-04 02:24:49 +09:00
Mike Yuan 3ddbc34e15 process-util: refuse FORK_WAIT + FORK_FREEZE combination 2025-02-21 21:35:05 +00:00
Daan De Meyer dc2f960b78 process-util: Allow setting ret_pid with FORK_DETACH in safe_fork()
Let's allow getting the pid even if the caller sets FORK_DETACH. We
do this via a socketpair() over which we send the inner child pid.
2025-02-20 21:00:52 +01:00
Daan De Meyer f48103ea61 process-util: Implement safe_fork_full() on top of pidref_safe_fork_full()
Let's switch things around, and move the internals of safe_fork_full() into
pidref_safe_fork_full() and make safe_fork_full() a trivial wrapper on top
of pidref_safe_fork_full().
2025-02-20 20:13:53 +01:00
Lennart Poettering 4ace93da8c pidref: now that we have the cached pidfdid of our own process, use it
Note that this drops a lot of "const" qualifiers on PidRef arguments.
That's because pidref_is_self() suddenly might end changing the PidRef
because it acquires the pidfd ID.

We had this previously already with pidfd_equal(), but this amplifies
the problem.

I guess we C's "const" doesn't really work for stuff that contains
caches, that is just conceptually constant, but not actually.
2025-01-20 21:51:40 +01:00
Yu Watanabe f0159e2b5b process-util: fix typo
Also rebreak comment.

Follow-up for 03b89cf213.
2025-01-19 04:24:08 +09:00
Lennart Poettering 277255e814 process-util: slightly update comment in freeze() 2025-01-16 11:55:21 +01:00