From 7359f104c202a6e36212324cdd5aba7964737e9d Mon Sep 17 00:00:00 2001 From: Ben Hutchings Date: Thu, 21 Mar 2024 23:12:47 +0100 Subject: [klibc] inet: Stricter IPv6 field parsing in inet_pton() We currently don't range-check the fields of an IPv6 address, so the following strings are wrongly accepted: "10000::" "::10000" Since we currently only support hexadecimal fields, implement the range check by limiting the number of digits to 4. Signed-off-by: Ben Hutchings --- usr/klibc/inet/inet_pton.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/usr/klibc/inet/inet_pton.c b/usr/klibc/inet/inet_pton.c index a319506abd390..19706ce03646b 100644 --- a/usr/klibc/inet/inet_pton.c +++ b/usr/klibc/inet/inet_pton.c @@ -32,7 +32,7 @@ int inet_pton(int af, const char *src, void *dst) case AF_INET6: { struct in6_addr *d = (struct in6_addr *)dst; - int colons = 0, dcolons = 0; + int colons = 0, dcolons = 0, digits = 0; int i; const char *p; @@ -43,7 +43,9 @@ int inet_pton(int af, const char *src, void *dst) colons++; if (p[1] == ':') dcolons++; - } else if (!isxdigit((unsigned char)*p)) + digits = 0; + } else if (!isxdigit((unsigned char)*p) + || ++digits > 4) return 0; /* Invalid address */ } -- cgit 1.2.3-korg