tree-wide: use device_get_sysattr_safe_string()

The obtained strings are passed to another function, e,g, handled as a
path and opened, printed to the terminal, written to a file, saved to
udev database as udev property, exposed through DBus, passed to logger,
and so on. Hence, these should not contain any malicious characters.
This commit is contained in:
Yu Watanabe
2026-05-18 02:55:56 +09:00
parent fdce4d4687
commit 06d687ffac
14 changed files with 47 additions and 35 deletions
+3 -3
View File
@@ -43,7 +43,7 @@ static int device_is_power_sink(sd_device *device) {
FOREACH_DEVICE(e, d) {
const char *val;
r = sd_device_get_sysattr_value(d, "power_role", &val);
r = device_get_sysattr_safe_string(d, "power_role", &val);
if (r < 0) {
if (r != -ENOENT)
log_device_debug_errno(d, r, "Failed to read 'power_role' sysfs attribute, ignoring: %m");
@@ -93,7 +93,7 @@ static bool battery_is_discharging(sd_device *d) {
}
/* Possible values: "Unknown", "Charging", "Discharging", "Not charging", "Full" */
r = sd_device_get_sysattr_value(d, "status", &val);
r = device_get_sysattr_safe_string(d, "status", &val);
if (r < 0) {
log_device_debug_errno(d, r, "Failed to read 'status' sysfs attribute, assuming the battery is discharging: %m");
return true;
@@ -130,7 +130,7 @@ int on_ac_power(void) {
* https://docs.kernel.org/admin-guide/abi-testing.html#abi-file-testing-sysfs-class-power */
const char *val;
r = sd_device_get_sysattr_value(d, "type", &val);
r = device_get_sysattr_safe_string(d, "type", &val);
if (r < 0) {
log_device_debug_errno(d, r, "Failed to read 'type' sysfs attribute, ignoring device: %m");
continue;
+2 -1
View File
@@ -15,6 +15,7 @@
#include "alloc-util.h"
#include "blockdev-util.h"
#include "data-fd-util.h"
#include "device-private.h"
#include "device-util.h"
#include "devnum-util.h"
#include "dissect-image.h"
@@ -1027,7 +1028,7 @@ int loop_device_open(
#endif
nr = info.lo_number;
if (sd_device_get_sysattr_value(dev, "loop/backing_file", &s) >= 0) {
if (device_get_sysattr_safe_string(dev, "loop/backing_file", &s) >= 0) {
backing_file = strdup(s);
if (!backing_file)
return -ENOMEM;