Skip to contentSkip to conversation

qdev: Validate hex properties

PATCH Superseded Hannes Reinecke 3 participants 3 messages Nov 27, 2013

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 qdev: Validate hex properties

strtoul(l) might overflow, in which case it'll return '-1' and set the appropriate error code. So update the calls to strtoul(l) when parsing hex properties to avoid silent overflows.

Cc Peter Maydell <[email protected]>
Cc Eric Blake <[email protected]>
---
 hw/core/qdev-properties.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/core/qdev-properties.c +9 -0
index dc8ae69..5a94c04 100644
@@ -198,7 +198,10 @@ static int parse_hex8(DeviceState *dev, Property *prop, const char *str)
         return -EINVAL;
     }
 
+    errno = 0;
     *ptr = strtoul(str, &end, 16);
+    if (errno)
+        return -errno;
     if ((*end != '\0') || (end == str)) {
         return -EINVAL;
     }
@@ -329,7 +332,10 @@ static int parse_hex32(DeviceState *dev, Property *prop, const char *str)
         return -EINVAL;
     }
 
+    errno = 0;
     *ptr = strtoul(str, &end, 16);
+    if (errno)
+        return -errno;
     if ((*end != '\0') || (end == str)) {
         return -EINVAL;
     }
@@ -396,7 +402,10 @@ static int parse_hex64(DeviceState *dev, Property *prop, const char *str)
         return -EINVAL;
     }
 
+    errno = 0;
     *ptr = strtoull(str, &end, 16);
+    if (errno)
+        return -errno;
     if ((*end != '\0') || (end == str)) {
         return -EINVAL;
     }
Signature · 1 line
1.8.1.4
#1 lore raw

Am 27.11.2013 08:52, schrieb Hannes Reinecke:

Quoted 16 lines

strtoul(l) might overflow, in which case it'll return '-1' and set the appropriate error code. So update the calls to strtoul(l) when parsing hex properties to avoid silent overflows.

Cc Peter Maydell <[email protected]>
Cc Eric Blake <[email protected]>
---
 hw/core/qdev-properties.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/core/qdev-properties.c +0 -0
index dc8ae69..5a94c04 100644

hw/core/qdev-properties.c static int parse_hex8(DeviceState *dev, Property *prop, const char *str) · line 207

Quoted 10 lines
hw/core/qdev-properties.c +3 -0
@@ -198,7 +198,10 @@ static int parse_hex8(DeviceState *dev, Property *prop, const char *str)
         return -EINVAL;
     }
 
+    errno = 0;
     *ptr = strtoul(str, &end, 16);
+    if (errno)
+        return -errno;
     if ((*end != '\0') || (end == str)) {
         return -EINVAL;
     }

Thanks, this seems to match the requested logic. But please run checkpatch.pl and add the missing braces. I'll queue it then.

Please add Cc: [email protected] to the commit message while at it since this seems a bug fix that spans multiple releases.

Regards,
Andreas

Quoted 23 lines
hw/core/qdev-properties.c +6 -0
@@ -329,7 +332,10 @@ static int parse_hex32(DeviceState *dev, Property *prop, const char *str)
         return -EINVAL;
     }
 
+    errno = 0;
     *ptr = strtoul(str, &end, 16);
+    if (errno)
+        return -errno;
     if ((*end != '\0') || (end == str)) {
         return -EINVAL;
     }
@@ -396,7 +402,10 @@ static int parse_hex64(DeviceState *dev, Property *prop, const char *str)
         return -EINVAL;
     }
 
+    errno = 0;
     *ptr = strtoull(str, &end, 16);
+    if (errno)
+        return -errno;
     if ((*end != '\0') || (end == str)) {
         return -EINVAL;
     }
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#2 lore raw
In reply to Hannes Reinecke
Quoted 16 lines · Hannes Reinecke

strtoul(l) might overflow, in which case it'll return '-1' and set the appropriate error code. So update the calls to strtoul(l) when parsing hex properties to avoid silent overflows.

Cc Peter Maydell <[email protected]>
Cc Eric Blake <[email protected]>
---
 hw/core/qdev-properties.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/core/qdev-properties.c +0 -0
index dc8ae69..5a94c04 100644

hw/core/qdev-properties.c static int parse_hex8(DeviceState *dev, Property *prop, const char *str) · line 207

Quoted 10 lines
hw/core/qdev-properties.c +3 -0
@@ -198,7 +198,10 @@ static int parse_hex8(DeviceState *dev, Property *prop, const char *str)
         return -EINVAL;
     }
 
+    errno = 0;
     *ptr = strtoul(str, &end, 16);
+    if (errno)
+        return -errno;
     if ((*end != '\0') || (end == str)) {
         return -EINVAL;
     }

Still incomplete. You still have silent overflows. Consider a string of "0x100". strtoul() does not set errno, but *ptr is set to 0 because 0x100 is bigger than a uint8_t. You HAVE to parse the results into an unsigned long, then manually check that the resulting unsigned long value does not overflow *ptr.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org