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

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