From 64f1a6dcf0a96c3637861f2db96a55b7eee010a1 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Fri, 17 Nov 2017 13:39:02 +0100 Subject: tools: tighten up strtoul parsing Reported-by: Cedric Buxin --- src/tools/ipc.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/tools/ipc.c') diff --git a/src/tools/ipc.c b/src/tools/ipc.c index 0f87d24..2d24287 100644 --- a/src/tools/ipc.c +++ b/src/tools/ipc.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -280,7 +281,7 @@ static int userspace_set_device(struct wgdevice *dev) #define NUM(max) ({ \ unsigned long long num; \ char *end; \ - if (!strlen(value)) \ + if (!isdigit(value[0])) \ break; \ num = strtoull(value, &end, 10); \ if (*end || num > max) \ @@ -398,11 +399,10 @@ static int userspace_get_device(struct wgdevice **out, const char *interface) peer->flags |= WGPEER_HAS_PERSISTENT_KEEPALIVE_INTERVAL; } else if (peer && !strcmp(key, "allowed_ip")) { struct wgallowedip *new_allowedip; - char *end, *cidr = strchr(value, '/'); + char *end, *mask = value, *ip = strsep(&mask, "/"); - if (!cidr || strlen(cidr) <= 1) + if (!mask || !isdigit(mask[0])) break; - *cidr++ = '\0'; new_allowedip = calloc(1, sizeof(struct wgallowedip)); if (!new_allowedip) { ret = -ENOMEM; @@ -414,14 +414,14 @@ static int userspace_get_device(struct wgdevice **out, const char *interface) peer->first_allowedip = new_allowedip; allowedip = new_allowedip; allowedip->family = AF_UNSPEC; - if (strchr(value, ':')) { - if (inet_pton(AF_INET6, value, &allowedip->ip6) == 1) + if (strchr(ip, ':')) { + if (inet_pton(AF_INET6, ip, &allowedip->ip6) == 1) allowedip->family = AF_INET6; } else { - if (inet_pton(AF_INET, value, &allowedip->ip4) == 1) + if (inet_pton(AF_INET, ip, &allowedip->ip4) == 1) allowedip->family = AF_INET; } - allowedip->cidr = strtoul(cidr, &end, 10); + allowedip->cidr = strtoul(mask, &end, 10); if (*end || allowedip->family == AF_UNSPEC || (allowedip->family == AF_INET6 && allowedip->cidr > 128) || (allowedip->family == AF_INET && allowedip->cidr > 32)) break; } else if (peer && !strcmp(key, "last_handshake_time_sec")) -- cgit v1.2.3-59-g8ed1b