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=202602 header.b=RTLjBQhT; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 9495B5A0624 for ; Fri, 27 Mar 2026 05:34:43 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1774586072; bh=VWkPmN+11mUEV59rE7eA7JR/ddGgNkWZxkAMhqzNEJA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RTLjBQhTYRfSmXtRdgb6SGxaE9PciiBKwGsRnqAWKm48DQ5yc2kqLUXda941G2op6 1j9pZmXeYYijh2T85zWc+42QpO4kiAXAe0uMOuF82B6EXxhxLrFXPNakLDsQOY5lBA IgiGz2sEHk+Y6AQ+gzZ+TGYnhzKXlYJDEL8I+ga/Lk6WiHLRSdIyH3mzv25/C5OARE RaYpvVda7O42FbhQnUp9oo89I/yD/pgi3gDtihJ4MAcvBAVc8lkR8WCF0PSPMH6hR5 UoePZgNrD9ghAsDE9nzp2BCygOZilSwEzaFHdYscEoqwc+dRovE1aWR2ijxu4Ngar0 o0G8TkxYLBv4g== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4fhnqD6Bp0z4wTD; Fri, 27 Mar 2026 15:34:32 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 16/18] ip: Define a bound for the string returned by ipproto_name() Date: Fri, 27 Mar 2026 15:34:28 +1100 Message-ID: <20260327043430.1785787-17-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260327043430.1785787-1-david@gibson.dropbear.id.au> References: <20260327043430.1785787-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: JSEPPF5AMC6KZBZFFK7ULUSRGJX5HVSE X-Message-ID-Hash: JSEPPF5AMC6KZBZFFK7ULUSRGJX5HVSE 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: ipproto_name() returns a static string of theoretically unbounded length. That's going to be inconvenient in future, so introduce IPPROTO_STRLEN giving an explicit bound on the length. Use static_assert() and some macros to ensure nothing we return can exceed this. Signed-off-by: David Gibson --- ip.c | 18 ++++++++++++------ ip.h | 2 ++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/ip.c b/ip.c index 0ea62998..25fa4073 100644 --- a/ip.c +++ b/ip.c @@ -12,6 +12,7 @@ * Author: Stefano Brivio */ +#include #include #include @@ -74,7 +75,7 @@ found: * ipproto_name() - Get IP protocol name from number * @proto: IP protocol number * - * Return: pointer to name of protocol @proto + * Return: pointer to name of protocol @proto (<= IPPROTO_STRLEN bytes) * * Usually this would be done with getprotobynumber(3) but that reads * /etc/protocols and might allocate, which isn't possible for us once @@ -83,16 +84,21 @@ found: const char *ipproto_name(uint8_t proto) { switch (proto) { +#define CASE(s) \ + static_assert(sizeof(s) <= IPPROTO_STRLEN, \ + "Increase IPPROTO_STRLEN to contain " #s); \ + return s; case IPPROTO_ICMP: - return "ICMP"; + CASE("ICMP"); case IPPROTO_TCP: - return "TCP"; + CASE("TCP"); case IPPROTO_UDP: - return "UDP"; + CASE("UDP"); case IPPROTO_ICMPV6: - return "ICMPv6"; + CASE("ICMPv6"); default: - return ""; + CASE(""); +#undef CASE } } diff --git a/ip.h b/ip.h index d0de6c8d..fb4119a7 100644 --- a/ip.h +++ b/ip.h @@ -118,6 +118,8 @@ static inline uint32_t ip6_get_flow_lbl(const struct ipv6hdr *ip6h) } bool ipv6_l4hdr(struct iov_tail *data, uint8_t *proto, size_t *dlen); + +#define IPPROTO_STRLEN (sizeof("")) const char *ipproto_name(uint8_t proto); /* IPv6 link-local all-nodes multicast address, ff02::1 */ -- 2.53.0