From 0870906540506d0157f305df32b6b1f65b10ee85 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 16 May 2017 14:11:07 +0200 Subject: [PATCH] device: sanitze UTF-8 values for D-Bus ip link add name $'d\xccf\\c' type dummy Use nm_utils_str_utf8safe_escape() to sanitize non UTF-8 sequences before exposing them on D-Bus. The operation can be reverted client side via nm_utils_str_utf8safe_unescape() or simply g_strcompress(). Note that this preserves all valid UTF-8 sequences as-is, with exception of the backslash escape character and ASCII control characters. Thus, this is a change in behavior for strings that contain such characters. Note that nmcli is not changed to somehow unescape the string before printing. As the string is not valid UTF-8 (or contains ASCII characters that need escaping), they are not printable as-is, so unescaping before printing makes little sense. --- .../org.freedesktop.NetworkManager.Device.xml | 15 +++++++++ src/devices/nm-device.c | 31 ++++++++++++++----- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/introspection/org.freedesktop.NetworkManager.Device.xml b/introspection/org.freedesktop.NetworkManager.Device.xml index ee424101e6..be0a612cc4 100644 --- a/introspection/org.freedesktop.NetworkManager.Device.xml +++ b/introspection/org.freedesktop.NetworkManager.Device.xml @@ -21,6 +21,9 @@ each device in your application, use the object path. If you're looking for a way to track a specific piece of hardware across reboot or hotplug, use a MAC address or USB serial number. + + Note that non-UTF-8 characters are backslash escaped. Use g_strcompress() + to obtain the true (non-UTF-8) string. --> @@ -28,6 +31,9 @@ Interface: The name of the device's control (and often data) interface. + Note that non UTF-8 characters are backslash escaped, so the + resulting name may be longer then 15 characters. Use g_strcompress() + to revert the escaping. --> @@ -38,6 +44,9 @@ not refer to the actual data interface until the device has successfully established a data connection, indicated by the device's State becoming ACTIVATED. + Note that non UTF-8 characters are backslash escaped, so the + resulting name may be longer then 15 characters. Use g_strcompress() + to revert the escaping. --> @@ -45,6 +54,8 @@ Driver: The driver handling the device. + Non-UTF-8 sequences are backslash escaped. Use g_strcompress() + to revert. --> @@ -52,6 +63,8 @@ DriverVersion: The version of the driver handling the device. + Non-UTF-8 sequences are backslash escaped. Use g_strcompress() + to revert. --> @@ -59,6 +72,8 @@ FirmwareVersion: The firmware version for the device. + Non-UTF-8 sequences are backslash escaped. Use g_strcompress() + to revert. --> diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 48304c2660..af06a5d86a 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -13938,28 +13938,43 @@ get_property (GObject *object, guint prop_id, switch (prop_id) { case PROP_UDI: - g_value_set_string (value, priv->udi); + /* UDI is (depending on the device type) a path to sysfs and can contain + * non-UTF-8. + * ip link add name $'d\xccf\\c' type dummy */ + g_value_take_string (value, + nm_utils_str_utf8safe_escape_cp (priv->udi, + NM_UTILS_STR_UTF8_SAFE_FLAG_NONE)); break; case PROP_IFACE: - g_value_set_string (value, priv->iface); + g_value_take_string (value, + nm_utils_str_utf8safe_escape_cp (priv->iface, + NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL)); break; case PROP_IP_IFACE: - if (ip_config_valid (priv->state)) - g_value_set_string (value, nm_device_get_ip_iface (self)); - else + if (ip_config_valid (priv->state)) { + g_value_take_string (value, + nm_utils_str_utf8safe_escape_cp (nm_device_get_ip_iface (self), + NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL)); + } else g_value_set_string (value, NULL); break; case PROP_IFINDEX: g_value_set_int (value, priv->ifindex); break; case PROP_DRIVER: - g_value_set_string (value, priv->driver); + g_value_take_string (value, + nm_utils_str_utf8safe_escape_cp (priv->driver, + NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL)); break; case PROP_DRIVER_VERSION: - g_value_set_string (value, priv->driver_version); + g_value_take_string (value, + nm_utils_str_utf8safe_escape_cp (priv->driver_version, + NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL)); break; case PROP_FIRMWARE_VERSION: - g_value_set_string (value, priv->firmware_version); + g_value_take_string (value, + nm_utils_str_utf8safe_escape_cp (priv->firmware_version, + NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL)); break; case PROP_CAPABILITIES: g_value_set_uint (value, (priv->capabilities & ~NM_DEVICE_CAP_INTERNAL_MASK));