Commit Graph

6757 Commits

Author SHA1 Message Date
Yu Watanabe
db4b4e0cd3 core: Make DelegateNamespaces= work for user managers with CAP_SYS_ADMIN (#36771)
Currently DelegateNamespaces= only works for services spawned by the
system manager. User managers will always unshare the user namespace
first even if they're running with CAP_SYS_ADMIN.

Let's add support for DelegateNamespaces= for user managers if they're
running with CAP_SYS_ADMIN. By default, we'll still delegate all
namespaces
for user managers, but this can now be overridden by explicitly passing
DelegateNamespaces=.

If a user manager is running without CAP_SYS_ADMIN, the user manager is
still always unshared first just like before.
2025-03-20 06:28:18 +09:00
Daan De Meyer
f49b7404b2 capability-util: Ignore unknown capabilities instead of aborting
capability_ambient_set_apply() can be called with capability sets
containing unknown capabilities. Let's not crash when this is the
case but instead ignore the unknown capabilities.

This fixes a crash when running the following command:

"systemd-run -p "AmbientCapabilities=~" --wait --pipe id"

Fixes d5e12dc75e
2025-03-19 10:01:19 +01:00
Yu Watanabe
4b4181f4ac io-util: add one more assertion for Coverity
Fixes CID#1548067.
2025-03-19 11:41:25 +09:00
Yu Watanabe
c995855266 bitfield: check if specified index is non-negative
Hopefully silences false-positive warnings by Coverity e.g. CID#1590746.
2025-03-19 09:56:04 +09:00
Lennart Poettering
2d47f43b43 cgroup-util: remove cg_get_xattr(), it's apparently unused 2025-03-18 07:17:10 +01:00
Lennart Poettering
33cbda04eb xattr-util: rework getxattr_at_malloc()
Let's return the size in a return parameter instead of the return value.
And if NULL is specified this tells us the caller doesn't care about the
size and expects a NUL terminated string. In that case look for an
embedded NUL byte, and refuse in that case.

This should lock things down a bit, as we'll systematically refuse
embedded NUL strings now when we expect strings.
2025-03-18 07:17:10 +01:00
Lennart Poettering
783b40bd73 namespace-util: introduce userns_acquire_self_root()
This is a simple helper for creating a userns that just maps the
callers user to UID 0 in the namespace. This can be acquired unpriv,
which makes it useful for various purposes, for example for the logic in
is_idmapping_supported(), hence port it over.

(is_idmapping_supported() used a different mapping before, with the
nobody users, but there's no real reason for that, and we'll use
userns_acquire_self_root() elsewhere soon, where the root mapping is
important).
2025-03-17 16:03:18 +01:00
Lennart Poettering
6431c34b8a namespace-util: make "setgroups" users property writable via userns_acquire()
Unprivileged namespaces are only allowed if the "setgroups" file is set
to "deny" for processes. And we need to write it before writing the
gidmap. Hence add a parameter for that.

Then, also patch all current users to actually enable this. The usecase
generally don't need it (because they don't care about unprivileged
userns), but it doesn't hurt to enable the concept anyway in all current
users (none of them actually runs complex userspace in them, but they
mostly use userns_acquire() for idmapped mounts and similar).

Let's anyway make this option explicit in the function call, to indicate
that the concept exists and is applied.
2025-03-17 16:03:18 +01:00
Luca Boccassi
3d278519fa build: add C23 support (#35085)
To support C23, this introduces UTF8() macro to define UTF-8 literals,
as C23 changed char8_t from char to unsigned char.
This also makes pointer signedness warning critical, and updates C
standards table for tests.
2025-03-17 11:29:33 +00:00
Mike Yuan
b66fd12135 cgroup-util: drop is_cgroup_fs()
No need to bother with any cgroup v1 stuff anymore.
2025-03-16 18:02:08 +01:00
Yu Watanabe
e193378d32 macro: Introduce UTF8() macro to define UTF-8 string literal
C23 changed char8_t from char to unsigned char, hence assigning a u8 literal
to const char* emits pointer sign warning, e.g.
========
../src/shared/qrcode-util.c: In function ‘print_border’:
../src/shared/qrcode-util.c:16:34: warning: pointer targets in passing argument 1 of ‘fputs’ differ in signedness [-Wpointer-sign]
   16 | #define UNICODE_FULL_BLOCK       u8"█"
      |                                  ^~~~~
      |                                  |
      |                                  const unsigned char *
../src/shared/qrcode-util.c:65:39: note: in expansion of macro ‘UNICODE_FULL_BLOCK’
   65 |                                 fputs(UNICODE_FULL_BLOCK, output);
      |                                       ^~~~~~~~~~~~~~~~~~
========

This introduces UTF8() macro, which define u8 literal and casts to consth char*,
then rewrites all u8 literal definitions with the macro.
With this change, we can build systemd with C23.
2025-03-16 10:15:24 +09:00
Zbigniew Jędrzejewski-Szmek
75d51d77da basic/glyph-util: introduce optional_glyph() to wrap emoji_enabled() 2025-03-15 14:40:52 +01:00
Zbigniew Jędrzejewski-Szmek
1ae9b0cfa8 basic/glyph-util: rename "special glyph" to just "glyph"
Admittedly, some of our glyphs _are_ special, e.g. "O=" for SPECIAL_GLYPH_TOUCH ;)
But we don't need this in the name. The very long names make some invocations
very wordy, e.g. special_glyph(SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY).
Also, I want to add GLYPH_SPACE, which is not special at all.
2025-03-15 14:40:39 +01:00
Lennart Poettering
d810815ed4 firstboot: welcome user with emoji in firstboot wizard 🧙 2025-03-15 05:12:12 +09:00
Lennart Poettering
da65941c3e coredump,analyze: use read_full_file() for reading various top-level /proc/ files
Kernel API file systems typically use either "raw" or "seq_file" to
implement their various interface files. The former are really simple
(to point I'd call them broken), in that they have no understanding of
file offsets, and return their contents again and again on every read(),
and thus EOF is indicated by a short read, not by a zero read. The
latter otoh works like a typical file: you read until you get a
zero-sized read back.

We have read_virtual_file() to read the "raw" files, and can use regular
read_full_file() to read the "seq_file" ones.

Apparently all files in the top-level /proc/ directory use 'seq_file'.
but we accidentally used read_virtual_file() for them. Fix that.

Also clarify in a comment what the rules are.

Fixes: #36131
2025-03-13 14:18:46 +01:00
Lennart Poettering
af9c45d5b6 hostname: support that /etc/hostname contains ??? as wildcards to be replaced by hash value from /etc/machine-id 2025-03-11 18:01:42 +01:00
Lennart Poettering
98b7c5e2f2 basic: move gethostname_full() from basic/hostname-util.c → shared/hostname-setup.c
In one of the next commits we'd like to introduce a concept of
optionally hashing the hostname from the machine ID. For that we we need
to optionally back gethostname_full() by code involving sd-id128, hence
let's move it from src/basic/ to src/shared/, since only there we are
allowed to use our public APIs.
2025-03-11 17:54:44 +01:00
Lennart Poettering
a9178dbdd9 cgroup-util: Handle capsule@ paths like user@ paths (#36664)
The capsule instances are related to user instances, so treat them
equally to user@.service when handling cgroup paths. This also saves us
from polluting public libsystemd API with variant for capsules too.

Fix: https://github.com/systemd/systemd/issues/36098
2025-03-11 12:03:57 +01:00
Michal Koutný
6c5703f9bc cgroup-util: Handle capsule@ paths like user@ paths
The capsule instances are related to user instances, so treat them
equally to user@.service when handling cgroup paths. This also saves us
from polluting public libsystemd API with variant for capsules too.

Fix: #36098
2025-03-11 10:37:19 +01:00
Michal Koutný
afffd9ee26 capsule-util: Move from shared/ to basic/
...so that other code in basic can use it too (basic ⊆ shared).
2025-03-07 18:07:51 +01:00
Lennart Poettering
600e135a0b virt: detect "linux,dummy-virt" devicetree VMs
So apparently "linux,dummy-virt" is a devicetree in popular use by
various hypervisors, including crosvm:

e5d7a64d37/aarch64/src/fdt.rs (L692)

and qemu:

98c7362b1e/hw/arm/virt.c (L283)

and that's because the kernel ships support for that natively:

https://www.kernel.org/doc/Documentation/devicetree/bindings/arm/linux%2Cdummy-virt.yaml

It's explicitly for using in virtualization. Hence it's suitable for
detecting it as generic fallback.

This hence adds the check, similar to how we already look for one other
qemu-specific devicetree.

I ran into this while playing around with the new Pixel "Linux Terminal"
app from google which runs a Debian in a crosvm apparently. So far
systemd didn't recognize execution in it at all. Let's at least
recognize it as VM at all, even if this doesn't recognize it as
crosvm.
2025-03-07 15:18:08 +01:00
Lennart Poettering
4d53d76f5e virt: some modernizations
Reduce scope of certain allocations, add more debug logging, and improve
some log messages.
2025-03-07 15:11:09 +01:00
Lennart Poettering
4efd46c44b terminal-util: during terminal reset clear from beginning of line to end of screen
tianocore does some weird shit with its terminal emulation and regular
fills half the terminal with grey background and then invokes us with
this not cleared up. Hence let us clear this up for it: as part of the
ansi sequence based reset let's position the cursor explicitly at the
beginning of the current line, and erase everything till the end of the
screen. This makes boot output in tianocore vms much much cleaner.

Note that this does *not* erase any terminal output *before* the cursor
position where we take over, because that typically contains valuable
information still we should not erase.
2025-03-06 22:32:32 +00:00
Yu Watanabe
5c9feb2d6e tree-wide: drop unnecessary break in default branch 2025-03-06 11:42:20 +01: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
Mike Yuan
c133fcd5c0 locale-util: modernize is_locale_utf8() a bit 2025-03-04 23:03:23 +01:00
Mike Yuan
87838420aa basic/sys/mount: sort includes 2025-03-04 23:02:39 +01:00
Mike Yuan
c74380cceb missing_fs: drop FS_KEY_DESCRIPTOR_SIZE
We now directly import linux/fscrypt.h, so this def is duplicate
2025-03-04 23:02:37 +01:00
Yu Watanabe
c4eef17990 dirent-util: add several assertions in posix_getdents()
Follow-up for e86a492ff0.
2025-03-04 22:54:01 +01:00
Lennart Poettering
4f4c37bc07 log: explicitly size log_target_max_level()
We always validate that the target value is below _LOG_TARGET_SINGLE_MAX
before acessing it, but we don't actually size the array like that.
let's fix that.

This doesn#t effectively change anything, but it makes things more
explicit what the limit here is.
2025-03-04 18:07:00 +01:00
Daan De Meyer
5abf819a5f basic: remove unnecessary definition in missing_xyz.h (#36565) 2025-03-04 08:41:14 +01:00
Yu Watanabe
f342c2420a chattr-util: two trivial cleanups (#36593) 2025-03-04 13:13:25 +09:00
Yu Watanabe
42f6a96e6c fileio: move call of label_ops_post() before error handling of creating files
Fixes CID#1563946.
2025-03-04 05:18:15 +09:00
Yu Watanabe
e86a492ff0 dirent-util: introduce simple wrapper of posix_getdents()
glibc exports getdents64 syscall as is, but musl exports it as
posix_getdents(). Let's introduce a simple wrapper of posix_getdents().

Note, our baseline for glibc is 2.31. Hence, we can assume getdents64()
always defined when building with glibc.
2025-03-04 04:23:57 +09:00
Yu Watanabe
ec32732043 basic: introduce our own sys/mount.h implementation
To resolve conflict with sys/mount.h and linux/mount.h or linux/fs.h.

The conflict between sys/mount.h and linux/mount.h is resolved in
glibc-2.37 (774058d72942249f71d74e7f2b639f77184160a6), but our baseline
is still glibc-2.31. Also, even with the version or newer, still
sys/mount.h conflicts with linux/fs.h, which is included by
linux/btrfs.h.

This introduces our own implementation of sys/mount.h, that can be
simultaneously included with linux/mount.h and linux/fs.h. This also
imports linux/fs.h, linux/mount.h, and several other dependent headers.
The introduced sys/mount.h header itself may not be enough simple, but
by using the header, we can drop most of workarounds in other source files.
2025-03-04 02:24:49 +09:00
Yu Watanabe
bc2bb59bdd missing_audit: AUDIT_NLGRP_READLOG is defined since kernel v3.16
More specifically, since 451f921639fea4600dfb9ab2889332bdcc7b48d3.
Now, our kernel baseline is 5.4. Hence, we can drop the definition.
2025-03-04 02:24:49 +09:00
Yu Watanabe
aa14f19398 basic/linux: import capability.h from kernel 6.14-rc4 2025-03-04 02:24:49 +09:00
Yu Watanabe
b9b006c504 missing_wait: add short comment about P_PIDFD 2025-03-04 02:24:49 +09:00
Yu Watanabe
003b845cb2 missing_timerfd: TFD_TIMER_CANCEL_ON_SET is defined since glibc-2.26 2025-03-04 02:24:49 +09:00
Yu Watanabe
e75372958d missing_threads.h: threads.h exists since glibc-2.28 2025-03-04 02:24:49 +09:00
Yu Watanabe
75b101718c missing_socket: drop unnecessary definitions
All removed definitions are in glibc-2.31
2025-03-04 02:24:49 +09:00
Yu Watanabe
2c2d832eb0 missing_securebits: remove unnecessary header
Our kernel baseline is 5.4, hence all entries in the headers are defined
in linux/securebits.h.
2025-03-04 02:24:49 +09:00
Yu Watanabe
a7cb43d8d1 missing_resource.h: RLIMIT_RTTIME is defined since glibc-2.14
Now our baseline is glibc-2.31.
2025-03-04 02:24:49 +09:00
Yu Watanabe
8e091ec420 basic/linux: import prctl.h from linux 6.14-rc4 2025-03-04 02:24:49 +09:00
Yu Watanabe
6e338c25d8 missing_network: drop unnecessary definitions
They are already defined in glibc-2.31.
2025-03-04 02:24:49 +09:00
Yu Watanabe
b4ea2e7249 missing_namespace: drop unnecessary entries
Our kernel baseline is 5.4.
2025-03-04 02:24:49 +09:00
Yu Watanabe
dc1a78a462 basic/linux: import loop.h from kernel 6.14-rc4, and drop missing_loop.h 2025-03-04 02:24:49 +09:00
Yu Watanabe
fa716b6fc7 missing_keyctl: import keyctl.h from kernel 6.14-rc4 2025-03-04 02:24:49 +09:00
Yu Watanabe
e7e91769e8 basic/linux: import ioprio.h from kernel 6.14-rc4
This also fixes the maximum allowed ioprio class: 8 -> 7
2025-03-04 02:24:49 +09:00
Yu Watanabe
87fb62f598 missing_input: replace the header with genuine linux header 2025-03-04 02:24:49 +09:00