Blame SOURCES/0011-extensions-libebt_ip6-Use-xtables_ip6parse_any.patch

087bff
From 4ddde566b4af111536918b17e558c7bb4531335f Mon Sep 17 00:00:00 2001
087bff
From: Phil Sutter <phil@nwl.cc>
087bff
Date: Wed, 2 Jun 2021 14:04:43 +0200
087bff
Subject: [PATCH] extensions: libebt_ip6: Use xtables_ip6parse_any()
087bff
087bff
The code was almost identical and suffered from the same problem as
087bff
fixed in commit a76a5c997a235 ("libxtables: fix two off-by-one memory
087bff
corruption bugs").
087bff
087bff
The only functional change this involves is ebt_parse_ip6_address() will
087bff
now accept hostnames as well.
087bff
087bff
Signed-off-by: Phil Sutter <phil@nwl.cc>
087bff
(cherry picked from commit ca840c20b7b754d36a1abe7e597fd730dea142d4)
087bff
---
087bff
 extensions/libebt_ip6.c | 74 ++++++-----------------------------------
087bff
 1 file changed, 10 insertions(+), 64 deletions(-)
087bff
087bff
diff --git a/extensions/libebt_ip6.c b/extensions/libebt_ip6.c
087bff
index 301bed9aadefd..3cc39271d4658 100644
087bff
--- a/extensions/libebt_ip6.c
087bff
+++ b/extensions/libebt_ip6.c
087bff
@@ -247,73 +247,19 @@ static void brip6_init(struct xt_entry_match *match)
087bff
 	memset(ipinfo->dmsk.s6_addr, 0, sizeof(ipinfo->dmsk.s6_addr));
087bff
 }
087bff
 
087bff
-static struct in6_addr *numeric_to_addr(const char *num)
087bff
+/* wrap xtables_ip6parse_any(), ignoring any but the first returned address */
087bff
+static void ebt_parse_ip6_address(char *address,
087bff
+				  struct in6_addr *addr, struct in6_addr *msk)
087bff
 {
087bff
-	static struct in6_addr ap;
087bff
-
087bff
-	if (inet_pton(AF_INET6, num, &ap) == 1)
087bff
-		return ≈
087bff
-	return (struct in6_addr *)NULL;
087bff
-}
087bff
-
087bff
-static struct in6_addr *parse_ip6_mask(char *mask)
087bff
-{
087bff
-	static struct in6_addr maskaddr;
087bff
 	struct in6_addr *addrp;
087bff
-	unsigned int bits;
087bff
-
087bff
-	if (mask == NULL) {
087bff
-		/* no mask at all defaults to 128 bits */
087bff
-		memset(&maskaddr, 0xff, sizeof maskaddr);
087bff
-		return &maskaddr;
087bff
-	}
087bff
-	if ((addrp = numeric_to_addr(mask)) != NULL)
087bff
-		return addrp;
087bff
-	if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
087bff
-		xtables_error(PARAMETER_PROBLEM, "Invalid IPv6 Mask '%s' specified", mask);
087bff
-	if (bits != 0) {
087bff
-		char *p = (char *)&maskaddr;
087bff
-		memset(p, 0xff, bits / 8);
087bff
-		memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
087bff
-		p[bits / 8] = 0xff << (8 - (bits & 7));
087bff
-		return &maskaddr;
087bff
-	}
087bff
+	unsigned int naddrs;
087bff
 
087bff
-	memset(&maskaddr, 0, sizeof maskaddr);
087bff
-	return &maskaddr;
087bff
-}
087bff
-
087bff
-/* Set the ipv6 mask and address. Callers should check ebt_errormsg[0].
087bff
- * The string pointed to by address can be altered. */
087bff
-static void ebt_parse_ip6_address(char *address, struct in6_addr *addr, struct in6_addr *msk)
087bff
-{
087bff
-	struct in6_addr *tmp_addr;
087bff
-	char buf[256];
087bff
-	char *p;
087bff
-	int i;
087bff
-
087bff
-	strncpy(buf, address, sizeof(buf) - 1);
087bff
-	/* first the mask */
087bff
-	buf[sizeof(buf) - 1] = '\0';
087bff
-	if ((p = strrchr(buf, '/')) != NULL) {
087bff
-		*p = '\0';
087bff
-		tmp_addr = parse_ip6_mask(p + 1);
087bff
-	} else
087bff
-		tmp_addr = parse_ip6_mask(NULL);
087bff
-
087bff
-	*msk = *tmp_addr;
087bff
-
087bff
-	/* if a null mask is given, the name is ignored, like in "any/0" */
087bff
-	if (!memcmp(msk, &in6addr_any, sizeof(in6addr_any)))
087bff
-		strcpy(buf, "::");
087bff
-
087bff
-	if (inet_pton(AF_INET6, buf, addr) < 1) {
087bff
-		xtables_error(PARAMETER_PROBLEM, "Invalid IPv6 Address '%s' specified", buf);
087bff
-		return;
087bff
-	}
087bff
-
087bff
-	for (i = 0; i < 4; i++)
087bff
-		addr->s6_addr32[i] &= msk->s6_addr32[i];
087bff
+	xtables_ip6parse_any(address, &addrp, msk, &naddrs);
087bff
+	if (naddrs != 1)
087bff
+		xtables_error(PARAMETER_PROBLEM,
087bff
+			      "Invalid IPv6 Address '%s' specified", address);
087bff
+	memcpy(addr, addrp, sizeof(*addr));
087bff
+	free(addrp);
087bff
 }
087bff
 
087bff
 #define OPT_SOURCE 0x01
087bff
-- 
087bff
2.31.1
087bff