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=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202512 header.b=pkdWiD0s; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id CF8385A0773 for ; Wed, 07 Jan 2026 01:16:15 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202512; t=1767744971; bh=bOpYFCthDSmvNvdrAkyOvnNc30lAs+4x9cc4Xj/8KL0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=pkdWiD0s+IMiHr4HW99qMolawZk9OGZsBsYksGogRwL6tJUrOXXdPsgPAsW6LQr09 5NO9M7O8Zkm2V55Fg9FQ3VeVmYOq70voeX79zsXBGpmIM4eUJY92caeFd4Vd09Hpbk TKd59XUneqkPbR4eqIaZFmX14oLECs2HILkWrdGwnRuwwkfG4xMofdrXLTVkriR8k2 7mw6O2RW4Jt84ELkNDENbfJCSyp3Mmx173OnPtf04MRYt5w0JQq70xxByl/mq8NeFr r8xdfiekxM8wRGxCswUo1M/ZWYDJhrtwZ8WuALagp1EbnIhzbBoLWUvBhiOBng1pXS kvSlxkmY5T6Vw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4dm7qb15yLz4wQd; Wed, 07 Jan 2026 11:16:11 +1100 (AEDT) Date: Wed, 7 Jan 2026 11:11:50 +1100 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH 3/5] treewide: Don't rely on terminator records in ip[46].dns arrays Message-ID: References: <20260105075337.1724962-1-david@gibson.dropbear.id.au> <20260105075337.1724962-4-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="SBGyv1RvcMJNbOV7" Content-Disposition: inline In-Reply-To: Message-ID-Hash: O4FAUL4CFVE2NEQP37UWF7AVTB3RSZO6 X-Message-ID-Hash: O4FAUL4CFVE2NEQP37UWF7AVTB3RSZO6 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: passt-dev@passt.top, Stefano Brivio 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: --SBGyv1RvcMJNbOV7 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Jan 06, 2026 at 02:53:24PM +0100, Laurent Vivier wrote: > On 1/5/26 08:53, David Gibson wrote: > > In our arrays of DNS resolvers to pass to the guest we use a blank entry > > to indicate the end of the list. We rely on this when scanning the arr= ay, > > not having separate bounds checking. clang-tidy 21.1.7 has fancier > > checking for array overruns in loops, but it's not able to reason that > > there's always a terminating entry, so complains. > >=20 > > Indeed, it's correct to do so in this case. Although we allow space in= the > > arrays for the terminator (size MAXNS + 1), add_dns[46]() check only for > > idx >=3D ARRAY_SIZE() > > before adding an entry. This allows it to consume the last slot with a > > "real" entry, meaning the places where we scan really could overrun. > >=20 > > Fix the bug, and make it easier to reason about (for both clang-tidy and > > people) by using ARRAY_SIZE() base bounds checking. Treat the terminat= or > > explicitly as an early exit case using 'break'. > >=20 > > Signed-off-by: David Gibson > > --- > > conf.c | 8 ++++++-- > > dhcp.c | 4 +++- > > dhcpv6.c | 4 +++- > > ndp.c | 4 +++- > > passt.h | 4 ++-- > > 5 files changed, 17 insertions(+), 7 deletions(-) > >=20 > > diff --git a/conf.c b/conf.c > > index 84ae12b2..ceb9aa55 100644 > > --- a/conf.c > > +++ b/conf.c > > @@ -1159,7 +1159,9 @@ static void conf_print(const struct ctx *c) > > buf4, sizeof(buf4))); > > } > > - for (i =3D 0; !IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns[i]); i++) { > > + for (i =3D 0; i < ARRAY_SIZE(c->ip4.dns); i++) { > > + if (IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns[i])) > > + break; >=20 > It should be=A0"\t" rather than " ". Oops, not sure how that happened. Fixed. >=20 > Otherwise: >=20 > Reviewed-by: Laurent Vivier >=20 > > if (!i) > > info("DNS:"); > > inet_ntop(AF_INET, &c->ip4.dns[i], buf4, sizeof(buf4)); > > @@ -1197,7 +1199,9 @@ static void conf_print(const struct ctx *c) > > buf6, sizeof(buf6))); > > dns6: > > - for (i =3D 0; !IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns[i]); i++) { > > + for (i =3D 0; i < ARRAY_SIZE(c->ip6.dns); i++) { > > + if (IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns[i])) > > + break; > > if (!i) > > info("DNS:"); > > inet_ntop(AF_INET6, &c->ip6.dns[i], buf6, sizeof(buf6)); > > diff --git a/dhcp.c b/dhcp.c > > index 6b9c2e3b..1ff8cba9 100644 > > --- a/dhcp.c > > +++ b/dhcp.c > > @@ -430,7 +430,9 @@ int dhcp(const struct ctx *c, struct iov_tail *data) > > } > > for (i =3D 0, opts[6].slen =3D 0; > > - !c->no_dhcp_dns && !IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns[i]); i++= ) { > > + !c->no_dhcp_dns && i < ARRAY_SIZE(c->ip4.dns); i++) { > > + if (IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns[i])) > > + break; > > ((struct in_addr *)opts[6].s)[i] =3D c->ip4.dns[i]; > > opts[6].slen +=3D sizeof(uint32_t); > > } > > diff --git a/dhcpv6.c b/dhcpv6.c > > index e4df0db5..d94be23a 100644 > > --- a/dhcpv6.c > > +++ b/dhcpv6.c > > @@ -425,7 +425,9 @@ static size_t dhcpv6_dns_fill(const struct ctx *c, = char *buf, int offset) > > if (c->no_dhcp_dns) > > goto search; > > - for (i =3D 0; !IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns[i]); i++) { > > + for (i =3D 0; i < ARRAY_SIZE(c->ip6.dns); i++) { > > + if (IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns[i])) > > + break; > > if (!i) { > > srv =3D (struct opt_dns_servers *)(buf + offset); > > offset +=3D sizeof(struct opt_hdr); > > diff --git a/ndp.c b/ndp.c > > index eb9e3139..1f2bcb0c 100644 > > --- a/ndp.c > > +++ b/ndp.c > > @@ -285,7 +285,9 @@ static void ndp_ra(const struct ctx *c, const struc= t in6_addr *dst) > > size_t dns_s_len =3D 0; > > int i, n; > > - for (n =3D 0; !IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns[n]); n++); > > + for (n =3D 0; n < ARRAY_SIZE(c->ip6.dns); n++) > > + if (IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns[n])) > > + break; > > if (n) { > > struct opt_rdnss *rdnss =3D (struct opt_rdnss *)ptr; > > *rdnss =3D (struct opt_rdnss) { > > diff --git a/passt.h b/passt.h > > index 79d01ddb..87da76d3 100644 > > --- a/passt.h > > +++ b/passt.h > > @@ -91,7 +91,7 @@ struct ip4_ctx { > > struct in_addr guest_gw; > > struct in_addr map_host_loopback; > > struct in_addr map_guest_addr; > > - struct in_addr dns[MAXNS + 1]; > > + struct in_addr dns[MAXNS]; > > struct in_addr dns_match; > > struct in_addr our_tap_addr; > > @@ -132,7 +132,7 @@ struct ip6_ctx { > > struct in6_addr guest_gw; > > struct in6_addr map_host_loopback; > > struct in6_addr map_guest_addr; > > - struct in6_addr dns[MAXNS + 1]; > > + struct in6_addr dns[MAXNS]; > > struct in6_addr dns_match; > > struct in6_addr our_tap_ll; >=20 --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --SBGyv1RvcMJNbOV7 Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmldpMUACgkQzQJF27ox 2GcWxw//Qv2lboyQY+TZ6/F+nOMvuyqnjRuKya+A7lvZz4dmUupelAhdr30IwYMe ovl4PZ5DJU+iveT0OzgDXw4jdQMFoXVQvc/wXFk9wracCgTzTnIivo11DRmwdCsD wGg4l7bEFcG5NYU0oq5uxNtwt4oXzSJEGAWrrdBmK0YPQyYLoMCM6sCMfiANT5iQ Ul9qE165U8RHbOBeuNajc/TxHBNys/TkJbYutgbcjy6iD7059/5pqf9gzbNIwbF3 Qn+eBsjITXQRXkajjuU7yGgjOifqL6KNKCo2kMif7dcO1/ujB8nUtxGWOWkz6Ink cL5fDSNRcSxH3u780ESXu7YKwrym4WicQjY4X+K+OvLfmsLysnpTpS51xh+1Cows JQMUKKuceM3oyUK1An5k4GXSuU4MEqsmQszitfBxNuak+FCNdOazDHGYMpXRovU4 YeIgfbWvHirDdAmWLmQxBxRn6sXq1otx9EHOEs3jsMIxflxY8WraHpNIB9OOOxnp +AsiuKzXHc9cwMf6CBqzn2lXGulvIuSkBBU8VBeDY6s6eamItQHiZUgM7qvseSh3 at5463bf96AHDydNxf+umKDSy+zeUNV0B2ZQQMH8okZVaDExWhyC3HcGwuSK5ToS ZJR1C0edDXNqK8F0XIHjNqOTNPdOMzf7aduYyUDGRdVMSut2GjI= =oFxs -----END PGP SIGNATURE----- --SBGyv1RvcMJNbOV7--