Commit Graph
4436 Commits
Author SHA1 Message Date
Zbigniew Jędrzejewski-Szmek 926167301c many: final set of coccinelle check-pointer-deref tweaks (#41426)
This is a followup to https://github.com/systemd/systemd/pull/41400 with
the final set of tweaks so that the new coccinelle `check-pinters-deref`
checker runs without failures.

It also includes a commit with some fixes for redundant asserts and the
final commit to make the assert() in the inner loop of qsort
POINTER_MAY_BE_NULL which will become a no-op - that is probably the
most controversial one, I hope the commit message explains the
trade-offs, I'm happy to drop it, I have no strong opinion either way.

Please see the individual commits/commit messages for more details, most
is (hopefully) relatively boring/mechanical.
2026-04-03 19:27:09 +02:00
Michael Vogt f180ff5983 sd-varlink: fix fd handling in upgrade code path
This commit fixes an issue with the fd handling in
sd_varlink_call_and_upgrade() when one direction of the output
FDs is unset.

Thanks to Lennart for spotting this and suggesting the fix.
2026-04-02 13:20:17 +02:00
Michael Vogt b50d7345a2 libsystemd: make check-pointer-deref clean
Add the needed assert changes to make the code clean
for the new check-pointer-deref script.
2026-03-31 18:30:40 +02:00
Michael Vogt 79db432ab2 sd-varlink: add sd_varlink_call_and_upgrade() for protocol upgrades
The varlink spec supports protocol upgrades and they are very
useful to e.g. transfer binary data directly via varlink. So
far sd-varlink was not supporting this.

This commit adds a new public sd_varlink_call_and_upgrade()
that sends a method call, waits for the reply, then steals
the connection fds for raw I/O. It returns separate input_fd
and output_fd to support both bidirectional sockets and pipe
pairs.

A helper is extracted and shared between sd_varlink_call_full()
and sd_varlink_call_and_upgrade(). A new `protocol_upgrade`
bool in `struct sd_varlink` ensures that on a protocol upgrade
request we only exactly read the varlink protocol bytes and
leave anything beyond that to the caller that speaks the upgraded
protocol.

Note that this is the client side of the library implementation
only for now. The server side needs work but this is already
useful as it allows to talk to varlink servers that speak protocol
upgrades (like the rust implemenations of varlink).
2026-03-31 18:25:09 +02:00
Luca Boccassi 4f5b28b72c sd-bus: add assert_cc for message allocation size
Use CONST_ALIGN_TO to express the compile-time overflow check for
the ALIGN(sizeof(sd_bus_message)) + sizeof(BusMessageHeader)
allocation, since ALIGN() is not constexpr.

CID#1548031

Follow-up for de1c301ed1
2026-03-30 09:37:32 +01:00
Luca Boccassi aa2cc18c76 sd-bus: use usec_add() for auth timeout calculation
Use the overflow-safe usec_add() instead of raw addition for
computing the authentication timeout.

CID#1548036

Follow-up for e3017af973
2026-03-30 09:37:32 +01:00
Luca Boccassi 55354d5930 sd-bus: add asserts for message size overflow safety
Coverity flags arithmetic in BUS_MESSAGE_SIZE(),
BUS_MESSAGE_BODY_BEGIN() and message_from_header() as potential
overflows. The values are validated at message creation time, but
add asserts to make the invariants explicit for static analyzers.

CID#1548023
CID#1548030
CID#1548046

Follow-up for 6629161f82
2026-03-30 09:37:31 +01:00
Luca Boccassi 56df6e5669 sd-daemon: add assert before CMSG_SPACE subtraction
Coverity flags the subtraction from msg_controllen as a potential
underflow. The CMSG_SPACE was added when send_ucred was set, and
the subtraction only runs when send_ucred was true, so it is safe.
Add an assert to document this invariant.

CID#1548074

Follow-up for 64144440a5
2026-03-30 09:37:31 +01:00
Luca Boccassi a702b9b6ea sd-json: silence false positive in sd_json_variant_filter
Same pattern as the fix for sd_json_variant_unset_field in
9b3715d529: cache the element
count in a local variable and assert it is at least 2 before
subtracting.

CID#1548029

Follow-up for f2ff34ff2a
2026-03-30 09:37:31 +01:00
Luca Boccassi 4ff8fb899b journal: add assert for max_size overflow safety
Coverity flags max_size*2 as a potential overflow. The value is
bounded by MAX_SIZE_UPPER (128 MiB) or JOURNAL_COMPACT_SIZE_MAX
(4 GiB), so doubling is safe within uint64_t. Add an assert to
document this.

CID#1548019

Follow-up for 8580d1f73d
2026-03-30 09:37:31 +01:00
Daan De Meyer ea79ac6f36 Assorted coverity fixes (#41406) 2026-03-29 21:02:55 +02:00
Zbigniew Jędrzejewski-Szmek ab3a2f375f sd-varlink: export sd_varlink_set_sentinel
I tried to implement a varlink service using sd-varlink, and
not being able to use the approach with sentinel is exteremely
painful. This is useful internally and likewise externally.
2026-03-29 20:32:15 +02:00
Luca Boccassi c34169e9d3 sd-bus: add asserts for rbuffer_size accumulation bounds
Coverity flags rbuffer_size += k as a potential overflow, but k is
always bounded by the iov size (which is the difference between the
allocated buffer and current rbuffer_size). Add asserts to make this
invariant explicit.

CID#1548044
CID#1548071

Follow-up for a7e3212d89
2026-03-28 20:09:40 +00:00
Luca Boccassi c8b53fcfd3 sd-event: add assert to help static analysis trace signal bounds
Coverity flags the signal_sources array access as a potential
out-of-bounds read because it cannot trace through the SIGNAL_VALID()
macro to know that ssi_signo < _NSIG. Add an explicit assert after
the runtime check to make the constraint visible to static analyzers.

CID#1548033

Follow-up for 7a64c5f23e
2026-03-28 19:56:31 +00:00
Michael Vogt 9f691ad3c0 varlink: comment that "more" flag IDL comment is API
External tools that use the systemd varlink ecosystem require
to know if a specific varlink method supports/requires the
"more" flag from the IDL. This is tracked upstream in
https://github.com/varlink/varlink.github.io/issues/26

As an intermediate step systemd adds the (very nice) comments
```
 # [Requires 'more' flag]
or
 # [Supports 'more' flag]
```
to the various methods.

This commit extends the comment around the code that adds the
comment to clarify that this should be considered API and that
the comment should not be changed as external tools (like e.g.
the varlink-http-bridge) rely on it.
2026-03-25 12:27:50 +01:00
Lennart Poettering 9eb207356c json-util: add json_dispatch_in6_addr() 2026-03-24 21:24:47 +01:00
Lennart Poettering 3839f5efec json-util: optionally accept string-based serialization for IPv4 addresses 2026-03-24 21:24:47 +01:00
Lennart Poettering e7a176fe46 Define options and verbs through "magic macros" (#40880)
This is an another alternative for #40656, based on @poettering's
suggestion to use an ELF section.

The output of --help is generated using format-table, but the details of
the formatting a bit off: different sections in the table (verbs, option
groups) are not aligned to the same column. Our current table formatting
doesn't make this easy. If somebody has an idea how to do this without
too much pain, please make suggestions.

Another thing that I didn't know how to do nicely, was to use the two
columns of separation. But maybe this doesn't matter. If we switch to
one column everywhere, I don't think people will care.

Example output:
```console
$ build/systemd-id128 -h | cat    
systemd-id128 [OPTIONS...] COMMAND

Generate and print 128-bit identifiers.

Commands:
  new                Generate a new ID
  machine-id         Print the ID of current machine
  boot-id            Print the ID of current boot
  invocation-id      Print the ID of current invocation
  var-partition-uuid Print the UUID for the /var/ partition
  show [NAME|UUID]   Print one or more UUIDs
  help               Show this help

Options:
  -h --help            Show this help
     --version         Show package version
     --no-pager        Do not start a pager
     --no-legend       Do not show headers and footers
     --json=FORMAT     Output inspection data in JSON (takes one of pretty,
                       short, off)
  -j                   Equivalent to --json=pretty (on TTY) or --json=short
                       (otherwise)
  -p --pretty          Generate samples of program code
  -P --value           Only print the value
  -a --app-specific=ID Generate app-specific IDs
  -u --uuid            Output in UUID format

See the systemd-id128(1) man page for details.
```

The output wraps automatically with terminal width. If the terminal is
extremely narrow, ellipsization occurs. I think this doesn't matter
and/or is actually a feature. Such narrow terminals mostly occur in
testing, so it doesn't matter what exactly we do, as long as it is
something somewhat reasonable.

Anyway, I think this is enough as PoC. Please compare this with the
previous approach. /cc @behrmann, @YHNdnzj, @poettering

The three binaries that are converted have options, verbs, optional
arguments, and options terminate option parsing, so most of the
functionality is there. One thing that I didn't implement that was
present in previous PRs is "namespaces", i.e. multiple parsers in the
same source file. I expect that we can handle this similarly to option
groups.
2026-03-23 22:17:05 +01:00
Frantisek Sumsal 924f5a3461 sd-varlink: gracefully reject arrays/maps with a null element
Follow-up for 799392286e.
2026-03-22 19:37:47 +01:00
Zbigniew Jędrzejewski-Szmek 57a1f1ab96 bus-error: fix typo 2026-03-22 16:52:22 +01:00
Zbigniew Jędrzejewski-Szmek 77549ac1c4 Stop disabling -Wattributes (#41174)
In one of the reviews one of the LLMs noticed that the pragma is set but
never unset, so it remains in effect for the rest of the translation
unit. From the comment, it's not clear how old those "old compilers"
were, so let's try if things work without this workaround.
2026-03-20 17:10:10 +01:00
Lennart Poettering 2c74a91cb8 sd-json: when parsing optionally insist top-level variant is object or array
Typically, the top-level JSON object has to be an object, in any json
document we parse, hence let's add a simple way to enforce that.

Make use of this in various places.

(Note, various other JSON parsers insist on this logic right from the
beginning, but I actually thinking making this insisting optional like
this patch does it is the cleaner approach)
2026-03-20 15:34:56 +01:00
Luca Boccassi 69c087bade test: skip D-Bus FD truncation test with dbus-daemon
dbus-daemon intentionally disconnects peers when FDs get
truncated. Detect it and skip it in that case, as the purpose
of the test is not to exercise the D-Bus implementation, but
our library.
When running with dbus-broker (Fedora, etc) we'll get full
coverage.

Fixes https://github.com/systemd/systemd/issues/41150

Follow-up for 744d589632
2026-03-20 15:25:26 +01:00
Zbigniew Jędrzejewski-Szmek a0eae2ab7b Stop disabling -Wattributes
In one of the reviews one of the LLMs noticed that the pragma is set but
never unset, so it remains in effect for the rest of the translation
unit. From the comment, it's not clear how old those "old compilers" were,
so let's try if things work without this workaround.
2026-03-19 11:46:30 +01:00
seaeunlee 59aed18d69 hostname: add API for getting custom fields from machine-info 2026-03-18 20:47:00 +00:00
Yu Watanabe a62cd5a153 sd-device: refuse spurious properties
Properties are set through uevent, udev rules, or program output by IMPORT.
They may contain spurious characters and udev database parsers may be confused.
Let's refuse spurious properties.
2026-03-10 06:32:30 +09:00
Yu Watanabe 460b580810 sd-device: do not register a property with an empty string
The function device_add_property() handles property with NULL value as
removing the property. Let's also make an empty value is handled as
same.

Note, ENV{hoge}=="" udev property handles both NULL value and an empty string
in the same way.
2026-03-10 03:33:23 +09:00
Yu Watanabe d3df01c4eb sd-device: use device_add_property() at one more place 2026-03-10 03:05:15 +09:00
Lennart Poettering dfb1fa0197 sd-varlink: pin error message while we invoke a reply callback
Let's make sure the parameters/error pointers into the message remain
valid as long as the reply callback is running, even if the reply
otherwise resets the pending message.
2026-03-06 14:51:35 +01:00
Yu Watanabe 8903e6b5f7 tree-wide: suppress misc-use-internal-linkage warnings
Suppress warnings like the following from clang tidy:
```
../src/boot/addon.c:11:19: error: function 'efi_main' can be made static to enforce internal linkage [misc-use-internal-linkage,-warnings-as-errors]
   11 | EFIAPI EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *system_table);
      |                   ^
```

Some warnings are suppressed simply by setting comments to ignore the warning,
some are by making global variables static, or include a suitable header.
2026-03-05 20:23:24 +01:00
Frantisek Sumsal 90f5d2a202 tree-wide: various fixlets suggested by Coccinelle 2026-03-05 10:27:58 +01:00
Frantisek Sumsal a8335fb91f tree-wide: use typed SD_JSON_BUILD_PAIR_* macros more 2026-03-05 10:27:58 +01:00
Mike Yuan f1f38e2fc1 sd-journal/catalog: make catalog_file_dirs static 2026-03-04 14:32:17 +01:00
Yu Watanabe 43116c5662 tree-wide: use DIGITS and friends 2026-03-03 22:18:26 +01:00
Yu Watanabe 4b4eba1666 tree-wide: use _contains() rather than _get()/_find() 2026-03-02 23:10:22 +09:00
Mike Yuan 8df624bfdd sd-varlink: add missing space 2026-02-28 12:01:06 +01:00
Mike Yuan 4e6e3b8707 sd-json: fix off-by-one issue when updating parent for array elements
Follow-up for 8525bb369a
2026-02-26 14:23:12 +01:00
Luca Boccassi a851c5118b test: avoid hanging forever waiting for udev if not a booted system
test_sd_device_enumerator_filter_subsystem hangs forever in OBS builds,
as it waits for the udev queue to be drained. But this is not a booted
system, with systemd and udev, so nothing will do that.
Skip the test in that case.
2026-02-25 04:53:20 +09:00
Lennart Poettering a69dffbf4f sd-varlink: add a bit of debug logging indicating when exit-on-idle triggers 2026-02-24 17:11:54 +01:00
Lennart Poettering a0276f6e12 sd-varlink: use correct error variable 2026-02-24 17:09:33 +01:00
Lennart Poettering 332ecf40f2 sd-netlink: pin reply slot while we execute callback
The callback might drop the last ref to the slot object, and we still
want to access it. Hence do what we usually do in these cases: keep an
extra reference while processing the callback.
2026-02-24 17:09:33 +01:00
Yu Watanabe e0a2369dd1 sd-device: do not try to remove previous tag indexes
The removed code in device_tag_index() in fact does nothing,
as sd_device.all_tags is never cleared. Moreover, not only the code
is meaningless, but it is theoretically/logically wrong, as the symlinks
in /run/udev/tags/ should be 'sticky', hence we should even not try to
remove them.
2026-02-23 05:46:42 +09:00
Yu Watanabe fc03d200c7 sd-device: do not clear sd_device.all_tags even on TAG="hoge"
The current tag concept has been introduced by
e77b146f82 (v247) to make symlinks in
/run/udev/tags/ are 'sticky'.

However, when TAG= (rather than TAG+=) is specified, then the tags
assigned in the previous events were also cleared.
This fixes the issue and now symlinks in /run/udev/tags/ are really
'sticky'.

Fortunately, TAG= is mostly unused. So, the issue should not affect
and the fix should not change anything on almost all systems.
2026-02-23 05:46:42 +09:00
Yu Watanabe 1c867dc71a sd-device: move copy_all_tags() from udev
No functional change, preparation for the next commit.
2026-02-23 05:46:42 +09:00
Yu Watanabe b65467b647 sd-device: shorten code a bit 2026-02-23 05:46:42 +09:00
Lennart Poettering a98fc72e29 json: add json_variant_compare() helper for comparint two json variants by order 2026-02-20 08:15:29 +01:00
Daan De Meyer e38fac0cc5 sd-bus: Make sure we can connect to user machines as well (#40698)
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 19:13:40 +01:00
Daan De Meyer 6119252f5a sd-bus: Don't fork unnecessarily to connect to container
Let's check if we're already in the right namespaces and call connect()
directly if that's the case. This can easily happen when the machine is
specified as .host or so.
2026-02-17 17:52:20 +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
Yu Watanabe b96f417ea6 xaccess: Rework from boolean into a list of tags (#40645)
Fixes: #40634
2026-02-18 01:24:59 +09:00