From: Yu Watanabe Date: Wed, 11 Mar 2026 20:44:51 +0000 (+0900) Subject: sd-device: use device_get_sysattr_safe_string() X-Git-Tag: v261-rc1~125^2~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=13ce62caba10e0dfa715dfa5c3ab551dedb02095;p=thirdparty%2Fsystemd.git sd-device: use device_get_sysattr_safe_string() The read value are exposed by sd_device_get_subsystem() and friends. Hence, it is better to filter invalid characters. Of course, these should be always safe unless the kernel is buggy. But, just for safety. Note, even if uevent file contains invalid characters, then device_read_uevent_file() should succeed without parsing the contents. The caller should fail later with a proper error code if a necessary field is unset. E.g. sd_device_get_ifindex() should still return -ENOENT even when uevent file contains an invalid characters. --- diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index 09a48a74c5a..4d1693e74c3 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -837,7 +837,7 @@ int device_read_uevent_file(sd_device *device) { device->uevent_loaded = true; const char *uevent; - r = sd_device_get_sysattr_value(device, "uevent", &uevent); + r = device_get_sysattr_safe_string(device, "uevent", &uevent); if (ERRNO_IS_NEG_PRIVILEGE(r) || ERRNO_IS_NEG_DEVICE_ABSENT(r)) /* The uevent files may be write-only, the device may be already removed, or the device * may not have the uevent file. */ @@ -1258,7 +1258,7 @@ _public_ int sd_device_get_subsystem(sd_device *device, const char **ret) { if (!device->subsystem_set) { const char *subsystem; - r = sd_device_get_sysattr_value(device, "subsystem", &subsystem); + r = device_get_sysattr_safe_string(device, "subsystem", &subsystem); if (r < 0 && r != -ENOENT) return log_device_debug_errno(device, r, "sd-device: Failed to read subsystem for %s: %m", @@ -1398,7 +1398,7 @@ _public_ int sd_device_get_driver(sd_device *device, const char **ret) { if (!device->driver_set) { const char *driver = NULL; - r = sd_device_get_sysattr_value(device, "driver", &driver); + r = device_get_sysattr_safe_string(device, "driver", &driver); if (r < 0 && r != -ENOENT) return log_device_debug_errno(device, r, "sd-device: Failed to read driver: %m");