core: validate ref_uid before checking in AttachProcesses method

ref_uid is initialized to invalid, and is only set in some
circumstances. The AttachProcesses will attempt to check it,
and assert that it is valid. Check beforehand.

Reported as YWH-PGM9780-89

Follow-up for 59857b672c
This commit is contained in:
Luca Boccassi
2026-02-22 15:13:02 +01:00
committed by Lennart Poettering
parent bd7ba0a645
commit 05f5156ad1
2 changed files with 38 additions and 0 deletions
+4
View File
@@ -34,6 +34,7 @@
#include "strv.h"
#include "transaction.h" /* IWYU pragma: keep */
#include "unit-name.h"
#include "user-util.h"
#include "web-util.h"
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode, collect_mode, CollectMode);
@@ -1649,6 +1650,9 @@ int bus_unit_method_attach_processes(sd_bus_message *message, void *userdata, sd
if (r == 0)
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_ACCESS_DENIED, "Process " PID_FMT " not owned by client's UID. Refusing.", pidref->pid);
if (!uid_is_valid(u->ref_uid)) /* process_is_owned_by_uid() requires a valid uid */
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_ACCESS_DENIED, "Unit does not have a valid ref_uid, refusing to attach process " PID_FMT ".", pidref->pid);
r = process_is_owned_by_uid(pidref, u->ref_uid);
if (r < 0)
return sd_bus_error_set_errnof(reterr_error, r, "Failed to check if process " PID_FMT " is owned by target unit's UID: %m", pidref->pid);
+34
View File
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -eux
# Assert when calling AttachProcesses on a unit without ref_uid set
at_exit() {
set +e
systemctl stop attach_and_barf.service
rm -f /run/systemd/system/attach_and_barf.service
systemctl daemon-reload
}
trap at_exit EXIT
mkdir -p /run/systemd/system
cat >/run/systemd/system/attach_and_barf.service <<EOF
[Service]
Type=simple
Delegate=yes
ExecStart=sleep infinity
EOF
systemctl daemon-reload
systemctl start attach_and_barf.service
run0 -u testuser \
busctl --system call \
org.freedesktop.systemd1 \
/org/freedesktop/systemd1 \
org.freedesktop.systemd1.Manager \
AttachProcessesToUnit \
"ssau" "attach_and_barf.service" "" 1 0 |& grep -F "Access denied" &>/dev/null