Received: from eggs.gnu.org ([2001:4830:134:3::10]:39391) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vlx83-00089o-US for qemu-devel@nongnu.org; Thu, 28 Nov 2013 03:39:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Vlx7x-0000AT-Mq for qemu-devel@nongnu.org; Thu, 28 Nov 2013 03:39:19 -0500 Received: from cantor2.suse.de ([195.135.220.15]:60077 helo=mx2.suse.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vlx7x-0000A7-Fq for qemu-devel@nongnu.org; Thu, 28 Nov 2013 03:39:13 -0500 Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id CCD89A7CF4 for ; Thu, 28 Nov 2013 09:39:11 +0100 (CET) From: Hannes Reinecke Date: Thu, 28 Nov 2013 09:39:07 +0100 Message-Id: <1385627947-23147-1-git-send-email-hare@suse.de> Subject: [Qemu-devel] [PATCHv3] qdev: Validate hex properties List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Andreas Faerber Cc: Hannes Reinecke , qemu-devel@nongnu.org, Alexander Graf 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. Signed-off-by: Hannes Reinecke --- hw/core/qdev-properties.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c index dc8ae69..4891a01 100644 --- a/hw/core/qdev-properties.c +++ b/hw/core/qdev-properties.c @@ -191,6 +191,7 @@ PropertyInfo qdev_prop_uint8 = { static int parse_hex8(DeviceState *dev, Property *prop, const char *str) { + unsigned long val; uint8_t *ptr = qdev_get_prop_ptr(dev, prop); char *end; @@ -198,11 +199,18 @@ static int parse_hex8(DeviceState *dev, Property *prop, const char *str) return -EINVAL; } - *ptr = strtoul(str, &end, 16); + errno = 0; + val = strtoul(str, &end, 16); + if (errno) { + return -errno; + } + if (val > 255) { + return -ERANGE; + } if ((*end != '\0') || (end == str)) { return -EINVAL; } - + *ptr = val; return 0; } @@ -329,7 +337,11 @@ 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 +408,11 @@ 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; } -- 1.8.1.4