From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 2FEA15A031A for ; Wed, 14 Aug 2024 06:31:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1723609853; bh=OyMRblSXk5Y8lvQovbJ8L7frIG7Y9tpjoImjYCl5hj8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=INs6EnMbvT0lJJWpusquv8ZEymmfbuHBFg7WBBHcnzkSewTJ5icqYvDxd6irK3vNo 04KJgEf5tR/uZPj8SbOq0nvV/T+Qac0lwOJCOYCeCa6qrvL5OK2681Z2QldAiODGG2 EHPcpr0sCO3sCAfPA6Dbo3YnJQk/sNvei97q55T4+BK5xGg1Wo2BrVzEcbnyPz/crK qEKinHOl3BPMJBOUgGgPXleYCbzmvmfvkjY6p4T7h9OcVuvnOypjLSZgAC2T+Gobqp 1Ut+pyqtzQ3ejdPn8gGZocoVu41bBQqoEjrRz6g+pBYXsM9Uw8YT7vHysgTe/oCuhL G1bOWngJbpqNw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WkFgK22vTz4xCy; Wed, 14 Aug 2024 14:30:53 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 08/16] conf: Move DNS array bounds checks into add_dns[46] Date: Wed, 14 Aug 2024 14:30:42 +1000 Message-ID: <20240814043050.4177037-9-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240814043050.4177037-1-david@gibson.dropbear.id.au> References: <20240814043050.4177037-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: VGAY7AYSKEQMULM2MH22RDNMWRO66AZZ X-Message-ID-Hash: VGAY7AYSKEQMULM2MH22RDNMWRO66AZZ 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: 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 db036a75..a651cb91 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