Commit Graph
9513 Commits
Author SHA1 Message Date
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
Mike Yuan e153cf36b0 core: address review feedback from Mike (#41354)
This commit addresses the review feedback from @YHNdnzj (many thanks)

Closes: https://github.com/systemd/systemd/issues/41324
2026-03-26 18:18:01 +01:00
Michael Vogt 31b9f451d7 core: always call manager_log_caller() even without pidref
Its fine if `manager_log_caller()` with an empty pidref,
it will log an unknown caller.

Thanks to @YHNdnzj for suggesting this.
2026-03-26 15:27:50 +01:00
Michael Vogt 1e9f0d76fb core: drop incorrect comment about SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)
The comment about `SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)` is
incorrect. To quote @YHNdnzj:
```
nah, the capability-based permission model is a legacy from kdbus. We cannot do it race-freely without it. Please simply drop the comment.
```

Thanks to @YHNdnzj for suggesting this.
2026-03-26 15:21:10 +01:00
Michael Vogt 21b25d8470 core: add assert(selinux_permission) to manager_do_set_objective
Thanks to @YHNdnzj for suggesting this.
2026-03-26 15:21:10 +01:00
Michael Vogt 34eec125d6 core: simplify manager_do_set_objective root path handling
The manager_do_set_objective() was doing a bunch of work to
check the `root` path that can already be done via
`json_dispatch_path` so instead of duplicating use the helper.

Thanks to @YHNdnzj for suggesting this.
2026-03-26 15:21:10 +01:00
Michael Vogt 1bdb3625c7 core: drop _manager() from new vl_method to shutdown
Drop the _manager suffix for vl_method_{poweroff,{soft,}reboot,
halt,kexec}_manager.

They're unambiguous enough and the power mgmt operations are
not manager-wide but system-wide.

Thanks to @YHNdnzj for suggesting this.
2026-03-26 15:21:10 +01:00
Daan De Meyer 6fb5ec3dd1 ci: Replace codeql PotentiallyDangerousFunction query with clang-tidy (#41246) 2026-03-23 11:30:47 +01:00
Luca Boccassi aaac2d3eda core: also set iov_len when deserializing LogExtraFields=
This is not actually used so it doesn't really matter in
practice and the fields are used anyway, but for cleanliness
fix it

Reported on yeswehack.com as YWH-PGM9780-165

Follow-up for 5699a1689b
2026-03-23 00:54:23 +00:00
Daan De Meyer 2bc0b826dc [RFC] core: add io.systemd.Manager.{PowerOff,SoftReboot,Halt,Kexec} (#41236) 2026-03-22 20:06:34 +01:00
Michael Vogt 114c89cc94 core: allow unset pidref in manager_log_caller
This commit allows unset pidref when calling manager_log_caller().
With that we can log manager calls even if we cannot resolve the
caller. Currently when we cannot resolve the caller we are just
not logging anything. With this commit we at least log the call
(even though we don't know what caller it was).

Thanks to keszybz for the suggestion.
2026-03-21 22:40:52 +01:00
Michael Vogt 955115546c core: extract varlink_log_caller() helper
Extract a common helper varlink_log_caller() and use in the varlink
code when logging the caller of a method. It also logs the method
now that was tried (but failed) to be logged with log_notice just
like manager_log_caller() would do.

I was looking into modifying `manager_log_caller` instead and
accept a NULL pidref but could not log more than the method without
pidref and would make the manager_log_caller slightly less nice.

Thanks to keszybz for suggesting this.
2026-03-21 22:28:19 +01:00
Michael Vogt 6215147624 core: add io.systemd.Manager.{PowerOff,Reboot,SoftReboot,Halt,Kexec}
This adds the low-level io.systemd.Manager shutdown support. This
is (much) simpler than the logind one. It mimics dbus but uses
a shared helper for the simple cases.

Note that this is more restrictive than the dbus version. The
dbus version uses SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT) but
the varlink version uses varlink_check_privileged_peer(link).
This is mostly because I'm not sure how to do the equivalent
in a race-free way.

Thanks to Daan for suggesting this.
2026-03-21 22:01:17 +01:00
Daan De Meyer 5227767021 core: Add two more IWYU pragmas
If selinux isn't enabled, these are reported as unused, so let's
add pragmas to tell clang-tidy to keep these.
2026-03-20 21:51:45 +01:00
Daan De Meyer 0b2432438c core: Only build selinux-setup if we have selinux 2026-03-20 21:38:08 +01:00
vlefebvre 7c1075fb8f kmod-setup: load vsock_loopback alongside vsock
Loading vmw_vsock_virtio_transport early at boot causes vsock to be
resident before any application opens an AF_VSOCK socket. Because the
kernel skips autoloading when the vsock module is already present,
vsock_loopback never gets loaded automatically, and any subsequent
bind() to VMADDR_CID_LOCAL fails with EADDRNOTAVAIL.

Fix this by explicitly loading vsock_loopback on virtio or VMWare
machines via the new may_have_vsock_looopback() helper, wich covers both
vmw_vsock_virtio_transport and vmware_vsock_vmci_transport case.
vsock_loopback is the only module that registers a transport for
VMADDR_CID_LOCAL (CID 1) and has no hard dependency from any of the
vsock transport modules.

Fixes: #41100
Follow-up for 381c78db491a7c5fad8697543dd36ebe9b848718
2026-03-20 19:25:55 +00:00
Luca Boccassi 415663a13a core: limit number of LogExtraFields (#41044)
We have two places where those fields can be set: config and the dbus
interface. Let's clamp down on the number in both places. But in
principle, we could also be upgrading (through
serialization/deserialization) from an older systemd which didn't
enforce this limit, so also check on deserialization. A user could have
a unit with lots and lots of ExtraFields, but not enough to cause the
issue in #40916. To handle this gracefully, ignore the extra fields,
like we do in the parser. Where the field is used, assert that we are
within the expected bounds.

Fixes #40916.

Reproducer:
```console
$ python3 -c 'from pydbus import SystemBus; from gi.repository import GLib; SystemBus().get("org.freedesktop.systemd1", "/org/freedesktop/systemd1").StartTransientUnit("crash.service", "fail", [("ExecStart", GLib.Variant("a(sasb)", [("/bin/true", ["/bin/true"], False)])), ("LogExtraFields", GLib.Variant("aay", [b"F%d=x" % i for i in range(140000)]))], [])' Traceback (most recent call last):
  File "<string>", line 1, in <module>
    from pydbus import SystemBus; from gi.repository import GLib; SystemBus().get("org.freedesktop.systemd1", "/org/freedesktop/systemd1").StartTransientUnit("crash.service", "fail", [("ExecStart", GLib.Variant("a(sasb)", [("/bin/true", ["/bin/true"], False)])), ("LogExtraFields", GLib.Variant("aay", [b"F%d=x" % i for i in range(140000)]))], [])
                                                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.14/site-packages/pydbus/proxy_method.py", line 102, in __call__
    raise error
  File "/usr/lib/python3.14/site-packages/pydbus/proxy_method.py", line 97, in __call__
    result = instance._bus.con.call_sync(*call_args)
gi.repository.GLib.GError: g-dbus-error-quark: GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Too many extra log fields. (16)
```
2026-03-11 19:40:43 +00:00
Zbigniew Jędrzejewski-Szmek 3105286f25 core/unit: shorten code
I wanted to use _cleanup_(unlink_tempfilep), but the type doesn't
match (char ** vs. char (*)[]), so the goto remains.
2026-03-11 18:09:27 +01:00
Zbigniew Jędrzejewski-Szmek 456a277d3f core: limit number of LogExtraFields
We have two places where those fields can be set: config and the dbus
interface. Let's clamp down on the number in both places. But in principle, we
could also be upgrading (through serialization/deserialization) from an older
systemd which didn't enforce this limit, so also check on deserialization. A
user could have a unit with lots and lots of ExtraFields, but not enough to
cause the issue in #40916. To handle this gracefully, ignore the extra fields,
like we do in the parser. Where the field is used, assert that we are within
the expected bounds.

Fixes #40916.

Reproducer:
$ python3 -c 'from pydbus import SystemBus; from gi.repository import GLib; SystemBus().get("org.freedesktop.systemd1", "/org/freedesktop/systemd1").StartTransientUnit("crash.service", "fail", [("ExecStart", GLib.Variant("a(sasb)", [("/bin/true", ["/bin/true"], False)])), ("LogExtraFields", GLib.Variant("aay", [b"F%d=x" % i for i in range(140000)]))], [])'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    from pydbus import SystemBus; from gi.repository import GLib; SystemBus().get("org.freedesktop.systemd1", "/org/freedesktop/systemd1").StartTransientUnit("crash.service", "fail", [("ExecStart", GLib.Variant("a(sasb)", [("/bin/true", ["/bin/true"], False)])), ("LogExtraFields", GLib.Variant("aay", [b"F%d=x" % i for i in range(140000)]))], [])
                                                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.14/site-packages/pydbus/proxy_method.py", line 102, in __call__
    raise error
  File "/usr/lib/python3.14/site-packages/pydbus/proxy_method.py", line 97, in __call__
    result = instance._bus.con.call_sync(*call_args)
gi.repository.GLib.GError: g-dbus-error-quark: GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Too many extra log fields. (16)
2026-03-11 18:09:27 +01:00
Luca Boccassi 0c67b8adcb core: fix reloading multiple confexts at the same time
[] has higher precedence than pointer dereference, hence hilarity
ensues as soon as there are multuple images

Originally reported on yeswehack.com as:
YWH-PGM9780-122

Follow-up for dfdeb0b1cb
2026-03-11 16:54:33 +00:00
Daan De Meyer 2f5cecd7de dbus-cgroup: Fix copy paste error
Let's set the appropriate field for ManagedOOMMemoryPressureDurationUSec=
and not a totally different one.
2026-03-11 16:53:36 +00:00
Cyrus Xi 9d15e09d5c core/cgroup: fix TasksMaxScale percentage serialization (#41011)
bus_cgroup_set_tasks_max_scale() used a hand-rolled percentage format
that produced values ~10x too small (e.g., "TasksMax=4.0%" instead of
"TasksMax=40.00%").

On daemon-reload, the incorrect value was re-read, silently reducing
the effective TasksMax by ~10x and causing fork rejections on systems
with high thread counts.

Fix by using the existing PERMYRIAD macros, consistent with memory
property handlers (MemoryMax, MemoryHigh, MemoryLow, etc.).

Fixes: #41009
2026-03-11 03:36:21 +09:00
Zbigniew Jędrzejewski-Szmek dc0b438836 various: use asprintf_safe in more places 2026-03-06 17:46:59 +01:00
Mike Yuan e65a6e26ad core/varlink-unit: distinguish PIDREF_AUTOMATIC from unset
Follow-up for ab73333c43

Methods that take numeric pid values use 0 to denote the peer,
hence let's log about 0 on PIDREF_AUTOMATIC, -1 if truly unset.
2026-03-06 04:05:28 +09:00
Mike Yuan 909108163f tree-wide: various Coccinelle-suggested tweaks and related fixes (#40950) 2026-03-05 17:07:39 +01:00
Michal Sekletar 0c98e432d1 core: cleanup unit's dropin directories from global cache
When user creates dropin files via API (e.g. systemctl set-property ...)
we put the dropin directory path into unit_path_cache. Drop those
directories from the cache in unit_free() and prevent memory leak.

Follow-up for fce94c5c56.
2026-03-05 16:50:25 +01:00
Frantisek Sumsal ab73333c43 core: drop unused errno from debug message
And properly guard unset parameters.
2026-03-05 10:27:59 +01:00
Mike Yuan 5aeda638bb core/varlink-metrics: make metric_family_table static 2026-03-04 14:32:16 +01:00
Mike Yuan 452c576927 core/varlink-unit: use VARLINK_ERROR_UNIT_NO_SUCH_UNIT macro
Follow-up for 1fc868ac6b
2026-03-02 10:49:51 +01:00
noxiouz 1fc868ac6b core: fix typo in Unit.List sentinel error name 2026-03-02 13:19:33 +09:00
Mike Yuan eb8df25a37 core/exec-invoke: make fd array sizing less error-prone
History has clearly shown that we're terrible at keeping
the size of the dont_close array up-to-date. Hence let's
step away from a hardcoded max size for that, instead
always collect all fds in the array initializer and
let compiler figure it out, taking advantage of the fact
that close_all_fds() gracefully handles invalid fds in
the 'except' array.
2026-03-01 13:55:39 +01:00
Mike Yuan f2e7ba1074 core/exec-invoke: drop duplicate addition of handoff_timestamp_fd to dont_close array
Follow-up for 12001b1bf0

It is part of the keep_fds array already, which is intended
for fds that need to survive until the very end of executor
runtime, i.e. across the second close_all_fds() call.
2026-03-01 13:55:39 +01:00
Mike Yuan 0aa70bef5e core/exec-invoke: correct size for dont_close array in close_remaining_fds()
Follow-up for 79dd24cf14
2026-03-01 13:55:39 +01:00
Mike Yuan ec93edc4ca core/exec-invoke: fix potential double close of socket/bpffs fds on failure
The owned fds should be closed before close_all_fds() along the lines
of exec_params/runtime_close(), otherwise they would be left dangling
afterwards.
2026-03-01 13:55:39 +01:00
Mike Yuan b702bbc31c core/execute: drop redundant close of root_directory_fd
exec_params_shallow_clear() will take care of it.
2026-03-01 13:55:30 +01:00
Mike Yuan efa6ba2ab6 core: validate input cgroup path more prudently 2026-02-26 14:23:25 +01:00
Mike Yuan 42aee39107 core/cgroup: avoid one unnecessary strjoina() 2026-02-26 14:23:23 +01:00
Mike Yuan 679e738bf4 core/dbus-unit: query sender uid only once, validate unit uid early
Follow-up for 05f5156ad1
2026-02-26 14:23:23 +01:00
Zbigniew Jędrzejewski-Szmek 13167bfc2f core/socket: fix reversed symlink direction in error message
After the update to 260-rc1 in Fedora Rawhide, we get a lot of messages like
this:
  systemd[1]: systemd-resolved-monitor.socket:
  Failed to create symlink /run/systemd/resolve/io.systemd.Resolve.Monitor →
  /run/varlink/registry/io.systemd.Resolve.Monitor, ignoring: Permission denied

The actual issue will need to be fixed too, but let's fix the message first.
While at it, let's rename the variables to be meaningful.
2026-02-26 11:34:22 +01:00
Mike Yuan 2b67122cdf core/namespace: clean up append_private_tmp() a bit
With the previous changes the modes for /tmp/ and /var/tmp/
may come in arbitrary directions, i.e. we may downgrade one
and upgrade another. Hence let's refactor the setup logic
a bit so they're truly independent.
2026-02-25 12:38:12 +01:00
Zbigniew Jędrzejewski-Szmek 0a17bb5c34 core: simplify requirements in unit_get_private_var_tmp() to just After=
As in the previous commit, checking for both requirements and ordering seems
unnecessary. In practical cases, the mount will be pulled in by the rest
of the transaction, so ordering is the part that matters. (The setup is
racy without the ordering.)  If we drop the second check, the admin can
just use After=tmp.mount to achieve the desired behaviour, without needing
to explicitly pull in the unit. This is easier to configure and more robust.

This changes the implementation introduced in
6156bec7a4.

Also actually describe the implemented behaviour in the man page.
2026-02-25 12:38:11 +01:00
Zbigniew Jędrzejewski-Szmek fa33eef344 core: upgrade /tmp when PrivateTmp=yes/DefaultDeps=no to disconnected
In https://github.com/systemd/systemd/issues/28515, multiple people report that
services that have PrivateTmp=yes and DefaultDependencies=no fail to create the
temporary directories under /tmp, when /tmp is e.g. a bind mount or some other
kind of mount that takes more time.

Before PrivateTmp=disconnected was added, we didn't have a nice solution:
DefaultDependencies=no is used to start services very early, so we wouldn't
want to add a dependency on /tmp automatically. With PrivateTmp=disconnected we
have a fairly nice solution. Let's "upgrade" to this mode automatically.
Strictly speaking, it is a small compat break, but in practice it's unlikely to
matter for early-boot services whether their /tmp is private or disconnected.

The dependency on /tmp that is checked is After. I think this is enough,
since any tmp.mount would be pulled in by local-fs.target and the rest of
the transaction anyway, so we don't need to check more than After.

The asserts are relaxed, because now the two settings can now diverge
in either way.

Resolves https://github.com/systemd/systemd/issues/28515.

[yhndnzj: fix unit_add_exec_dependencies() to handle the new
          combination; add a comment in exec_needs_sys_admin()]
2026-02-25 12:38:11 +01:00
Mike Yuan 9984a35d65 core/execute: stop looking at ExecSharedRuntime to determine PrivateTmp= state
The only reason why ExecSharedRuntime.(var_)tmp_dir might
be suppressed is when (/var)/tmp/ is listed in InaccessiblePaths=,
in which case mount namespace is needed anyways. Moreover,
the current logic imposes a chicken-and-egg problem:
at various places in pid1 we'd need to check whether
the unit runs in a mountns, but the ExecSharedRuntime
may not be allocated for it just yet.
2026-02-25 12:38:11 +01:00
Mike Yuan 0d10a125f6 core/execute: make exec_shared_runtime_make() handle distinct PrivateTmp= modes properly 2026-02-25 12:38:11 +01:00
Mike Yuan e438e5923d core/namespace: two fixes for namespace_cleanup_tmpdir()
* Make sure we're not passing NULL to rmdir()
* Remove the inner "tmp" subdir as well, so that callers
  can handle this transparently (e.g. exec_shared_runtime_make()
  is not aware of the nested rmdir() requirements)

While at it, remove unneeded PROTECT_ERRNO.
2026-02-25 12:38:11 +01:00
Mike Yuan a45fb074fe core/namespace: rename() might return ENOTEMPTY if target dir exists 2026-02-25 12:36:45 +01:00
Mike Yuan 2e71d0a2bd core/unit: refuse UserNamespacePath= + PrivateUsers=
Such setup makes zero sense, as if we unshare userns ourselves
we'd immediately lose CAP_SYS_ADMIN over the userns specified.
2026-02-25 12:36:44 +01:00
Mike Yuan 457bd2345c core/execute: do not suppress ExecSharedRuntime if userns path is specified
Follow-up for 79dd24cf14
2026-02-25 12:36:44 +01:00
Zbigniew Jędrzejewski-Szmek 92a0761d55 core/exec-invoke: add missing char in comment 2026-02-25 12:36:44 +01:00
Luca Boccassi 882cf2d943 core: check selinux/polkit access on varlink SetProperty
Reported on yeswehack.com as:
YWH-PGM9780-92

Follow-up for 0e1c4de235
2026-02-24 18:54:44 +00:00