From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=fail reason="key not found in DNS" header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202312 header.b=eOomwKqe; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id DCF875A027B for ; Wed, 21 Aug 2024 06:20:33 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1724214023; bh=RGpp2KOU4/qksL9l0Q3D6+yXb9e3LUxkg+ik4IZSQw8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eOomwKqeiK4SNUrbTnTXr66xtMeQpEv3nO9suzO/0IlD+XHp3u3gGVdYScEk/CydF dMh+j6v8e9zyhe3bP+cxwIL4f2RsxgFOAa/67/qxaM6eF6gFuU9OyNN5HU7bSc7GpY KIKkuGl/qIqZs9x8rR6hNZLESDR6UniRj8N7Se+2IhOAQ4IBvglSP4b79Tx9Y8FUFM 4rz980r1wIMas37hvBEMQF1PifBU2qDgic+5+ux2cCN5gxkPhk6Sv8UWkZICJJxw8a GesJbsSyopnsTs+pS7q4wBKDEi56/p/HpKYFxBXL4H3RLCB2zDAPS67cAEWcZhp4Hs bab5L0y9RkZ5w== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WpY5z25cDz4x8H; Wed, 21 Aug 2024 14:20:23 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 07/23] conf: Move DNS array bounds checks into add_dns[46] Date: Wed, 21 Aug 2024 14:20:03 +1000 Message-ID: <20240821042020.718422-8-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240821042020.718422-1-david@gibson.dropbear.id.au> References: <20240821042020.718422-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 27FMR47GKNHC47K3BO5NZCHMBDEUIIGB X-Message-ID-Hash: 27FMR47GKNHC47K3BO5NZCHMBDEUIIGB X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Paul Holzinger , David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Every time we call add_dns[46] we need to first check if there's space in the c->ip[46].dns array for the new entry. We might as well make that check in add_dns[46]() itself. In fact it looks like the calls in get_dns() had an off by one error, not allowing the last entry of the array to be filled. So, that bug is also fixed by the change. Signed-off-by: David Gibson --- conf.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/conf.c b/conf.c index d19013c1..dfe417b4 100644 --- a/conf.c +++ b/conf.c @@ -363,6 +363,9 @@ static unsigned add_dns4(struct ctx *c, const struct in_addr *addr, { unsigned added = 0; + if (idx >= ARRAY_SIZE(c->ip4.dns)) + return 0; + /* Guest or container can only access local addresses via redirect */ if (IN4_IS_ADDR_LOOPBACK(addr)) { if (!c->no_map_gw) { @@ -395,6 +398,9 @@ static unsigned add_dns6(struct ctx *c, struct in6_addr *addr, unsigned idx) { unsigned added = 0; + if (idx >= ARRAY_SIZE(c->ip6.dns)) + return 0; + /* Guest or container can only access local addresses via redirect */ if (IN6_IS_ADDR_LOOPBACK(addr)) { if (!c->no_map_gw) { @@ -453,12 +459,10 @@ static void get_dns(struct ctx *c) if (end) *end = 0; - if (!dns4_set && dns4_idx < ARRAY_SIZE(c->ip4.dns) - 1 - && inet_pton(AF_INET, p + 1, &dns4_tmp)) + if (!dns4_set && inet_pton(AF_INET, p + 1, &dns4_tmp)) dns4_idx += add_dns4(c, &dns4_tmp, dns4_idx); - if (!dns6_set && dns6_idx < ARRAY_SIZE(c->ip6.dns) - 1 - && inet_pton(AF_INET6, p + 1, &dns6_tmp)) + if (!dns6_set && inet_pton(AF_INET6, p + 1, &dns6_tmp)) dns6_idx += add_dns6(c, &dns6_tmp, dns6_idx); } else if (!dnss_set && strstr(line, "search ") == line && s == c->dns_search) { @@ -1682,14 +1686,12 @@ void conf(struct ctx *c, int argc, char **argv) c->no_dns = 0; - if (dns4_idx < ARRAY_SIZE(c->ip4.dns) && - inet_pton(AF_INET, optarg, &dns4_tmp)) { + if (inet_pton(AF_INET, optarg, &dns4_tmp)) { dns4_idx += add_dns4(c, &dns4_tmp, dns4_idx); continue; } - if (dns6_idx < ARRAY_SIZE(c->ip6.dns) && - inet_pton(AF_INET6, optarg, &dns6_tmp)) { + if (inet_pton(AF_INET6, optarg, &dns6_tmp)) { dns6_idx += add_dns6(c, &dns6_tmp, dns6_idx); continue; } -- 2.46.0