From 9e4ed37219b552bbf01b793f758d87afc2f6593e Mon Sep 17 00:00:00 2001 From: Xin LI Date: Thu, 25 Sep 2014 22:37:27 +0000 Subject: [PATCH] The strtol(3) family of functions would set errno when it hits one. Check errno and handle it as invalid input. Obtained from: HardenedBSD Submitted by: David CARLIER MFC after: 2 weeks --- sbin/sysctl/sysctl.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c index f9207f1de95..d7fe8e91356 100644 --- a/sbin/sysctl/sysctl.c +++ b/sbin/sysctl/sysctl.c @@ -305,7 +305,8 @@ parse(const char *string, int lineno) } else { intval = (int)strtol(newval, &endptr, 0); - if (endptr == newval || *endptr != '\0') { + if (errno != 0 || endptr == newval || + *endptr != '\0') { warnx("invalid integer '%s'%s", (char *)newval, line); return (1); @@ -316,7 +317,8 @@ parse(const char *string, int lineno) break; case CTLTYPE_UINT: uintval = (int) strtoul(newval, &endptr, 0); - if (endptr == newval || *endptr != '\0') { + if (errno != 0 || endptr == newval || + *endptr != '\0') { warnx("invalid unsigned integer '%s'%s", (char *)newval, line); return (1); @@ -326,7 +328,8 @@ parse(const char *string, int lineno) break; case CTLTYPE_LONG: longval = strtol(newval, &endptr, 0); - if (endptr == newval || *endptr != '\0') { + if (errno != 0 || endptr == newval || + *endptr != '\0') { warnx("invalid long integer '%s'%s", (char *)newval, line); return (1); @@ -336,7 +339,8 @@ parse(const char *string, int lineno) break; case CTLTYPE_ULONG: ulongval = strtoul(newval, &endptr, 0); - if (endptr == newval || *endptr != '\0') { + if (errno != 0 || endptr == newval || + *endptr != '\0') { warnx("invalid unsigned long integer" " '%s'%s", (char *)newval, line); return (1); @@ -348,7 +352,8 @@ parse(const char *string, int lineno) break; case CTLTYPE_S64: i64val = strtoimax(newval, &endptr, 0); - if (endptr == newval || *endptr != '\0') { + if (errno != 0 || endptr == newval || + *endptr != '\0') { warnx("invalid int64_t '%s'%s", (char *)newval, line); return (1); @@ -358,7 +363,8 @@ parse(const char *string, int lineno) break; case CTLTYPE_U64: u64val = strtoumax(newval, &endptr, 0); - if (endptr == newval || *endptr != '\0') { + if (errno != 0 || endptr == newval || + *endptr != '\0') { warnx("invalid uint64_t '%s'%s", (char *)newval, line); return (1); @@ -669,14 +675,16 @@ set_IK(const char *str, int *val) p = &str[len - 1]; if (*p == 'C' || *p == 'F') { temp = strtof(str, &endptr); - if (endptr == str || endptr != p) + if (errno != 0 || endptr == str || + endptr != p) return (0); if (*p == 'F') temp = (temp - 32) * 5 / 9; kelv = temp * 10 + 2732; } else { kelv = (int)strtol(str, &endptr, 10); - if (endptr == str || *endptr != '\0') + if (errno != 0 || endptr == str || + *endptr != '\0') return (0); } *val = kelv;