diff --git a/src/libsystemd/sd-device/device-private.c b/src/libsystemd/sd-device/device-private.c index 48dce77689..a5638645c6 100644 --- a/src/libsystemd/sd-device/device-private.c +++ b/src/libsystemd/sd-device/device-private.c @@ -11,6 +11,7 @@ #include "device-private.h" #include "device-util.h" #include "errno-util.h" +#include "escape.h" #include "extract-word.h" #include "fd-util.h" #include "fileio.h" @@ -377,6 +378,14 @@ static int device_amend(sd_device *device, const char *key, const char *value) { return log_device_debug_errno(device, r, "sd-device: Failed to parse udev database version '%s': %m", value); } else { r = device_add_property_internal(device, key, value); + if (r == -EINVAL) { + _cleanup_free_ char *escaped_key = cescape(key), *escaped_value = cescape(value); + + log_device_debug_errno(device, r, + "sd-device: Failed to add property '%s=%s', ignoring: %m", + strnull(escaped_key), strnull(escaped_value)); + return 0; + } if (r < 0) return log_device_debug_errno(device, r, "sd-device: Failed to add property '%s=%s': %m", key, value); } diff --git a/src/libsystemd/sd-device/test-sd-device.c b/src/libsystemd/sd-device/test-sd-device.c index c53f0b0e88..dde2bc9c86 100644 --- a/src/libsystemd/sd-device/test-sd-device.c +++ b/src/libsystemd/sd-device/test-sd-device.c @@ -807,6 +807,62 @@ TEST(sd_device_new_from_nulstr) { } } +TEST(sd_device_new_from_nulstr_invalid_property) { + char nulstr[] = + "ACTION=add\0" + "DEVPATH=/devices/pci0000:00/80860F14:00/mmc_host/mmc0/mmc0:0001\0" + "SUBSYSTEM=mmc\0" + "MMC_NAME=H8G4a\x92\0" + "MODALIAS=mmc:block\0" + "SEQNUM=1\0"; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; + const char *value; + + ASSERT_OK(device_new_from_nulstr(&device, nulstr, sizeof(nulstr) - 1)); + ASSERT_ERROR(sd_device_get_property_value(device, "MMC_NAME", &value), ENOENT); + ASSERT_OK(sd_device_get_property_value(device, "MODALIAS", &value)); + ASSERT_STREQ(value, "mmc:block"); +} + +TEST(sd_device_new_from_nulstr_invalid_property_key) { + char nulstr[] = + "ACTION=add\0" + "DEVPATH=/devices/pci0000:00/80860F14:00/mmc_host/mmc0/mmc0:0001\0" + "SUBSYSTEM=mmc\0" + "INVALID-KEY=value\0" + "SEQNUM=1\0"; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; + const char *value; + + ASSERT_OK(device_new_from_nulstr(&device, nulstr, sizeof(nulstr) - 1)); + ASSERT_ERROR(sd_device_get_property_value(device, "INVALID-KEY", &value), ENOENT); +} + +TEST(sd_device_new_from_nulstr_invalid_required_property) { + char nulstr[] = + "ACTION=invalid\0" + "DEVPATH=/devices/pci0000:00/80860F14:00/mmc_host/mmc0/mmc0:0001\0" + "SUBSYSTEM=mmc\0" + "MODALIAS=mmc:block\0" + "SEQNUM=1\0"; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; + + ASSERT_ERROR(device_new_from_nulstr(&device, nulstr, sizeof(nulstr) - 1), EINVAL); +} + +TEST(sd_device_new_from_nulstr_invalid_typed_property) { + char nulstr[] = + "ACTION=add\0" + "DEVPATH=/devices/pci0000:00/80860F14:00/mmc_host/mmc0/mmc0:0001\0" + "SUBSYSTEM=mmc\0" + "IFINDEX=invalid\0" + "MODALIAS=mmc:block\0" + "SEQNUM=1\0"; + _cleanup_(sd_device_unrefp) sd_device *device = NULL; + + ASSERT_ERROR(device_new_from_nulstr(&device, nulstr, sizeof(nulstr) - 1), EINVAL); +} + TEST(sd_device_new_from_path) { _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; _cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL;