From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 71C9545BC5; Thu, 24 Oct 2024 17:19:58 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 3A15D434D4; Thu, 24 Oct 2024 17:19:55 +0200 (CEST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mails.dpdk.org (Postfix) with ESMTP id 61589434CE for ; Thu, 24 Oct 2024 17:19:54 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729783194; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+1g8ltvrrA24pEenzu7ym9/GbUipqZtV4Q7dHxiQUkg=; b=CCuGrGl2y0b+rmaX+U6SlUPBJmKV3UV/1V0Hdn/kVbnX7xodNi0Boy1v5ZLyfStFmzzyvs ua4Sl0MjRwjQGoThlGA97qR4SwgJ7a0hHyjMj6Ma5b0FZa01BIRobwvKP7MfMe+my6vcM4 5irPadbKTQ0yZ1jgadYjp/LMyRBpqrM= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-103-NlZDQEXdP7yDSigJR32XVw-1; Thu, 24 Oct 2024 11:19:52 -0400 X-MC-Unique: NlZDQEXdP7yDSigJR32XVw-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 796CC18D65C7 for ; Thu, 24 Oct 2024 15:19:33 +0000 (UTC) Received: from ringo.home (unknown [10.39.208.32]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D257119541B4; Thu, 24 Oct 2024 15:19:25 +0000 (UTC) From: Robin Jarry To: dev@dpdk.org Subject: [PATCH dpdk 1/2] net/ipv6: fix overflowed array index reads Date: Thu, 24 Oct 2024 17:19:21 +0200 Message-ID: <20241024151919.400878-5-rjarry@redhat.com> In-Reply-To: <20241024151919.400878-4-rjarry@redhat.com> References: <20241024151919.400878-4-rjarry@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Fix the following overflowed array index reads reported by Coverity: 107 static inline bool 108 rte_ipv6_addr_eq_prefix(const struct rte_ipv6_addr *a, const struct rte_ipv6_addr *b, uint8_t depth) 109 { 1. Condition depth < 128 /* 16 * 8 */, taking true branch. 110 if (depth < RTE_IPV6_MAX_DEPTH) { 2. cast_overflow: Truncation due to cast operation on depth / 8 from 32 to 8 bits. 3. overflow_assign: d is assigned from depth / 8. 111 uint8_t d = depth / 8; 112 uint8_t mask = ~(UINT8_MAX >> (depth % 8)); 113 CID 446756: (#1 of 1): Overflowed array index read 4. deref_overflow: d, which might have overflowed, is used in a pointer index in a->a[d]. 114 if ((a->a[d] ^ b->a[d]) & mask) 115 return false; 116 117 return memcmp(a, b, d) == 0; 118 } 119 return rte_ipv6_addr_eq(a, b); 120 } The same issue has been reported both in rte_ipv6_addr_eq_prefix() and rte_ipv6_addr_mask(). All arithmetic operations are made using regular integers and then truncated on assign if necessary (or if explicitly down cast to a smaller type). In this case, the result of (depth / 8) is assumed to be on 32 bits and is implicitly down cast 8 bits. This is causing a warning because it may result in unexpected behaviour. Change the type of the d variables to unsigned int (32bit by default) to avoid the overflow warning. Since depth is strictly lesser than RTE_IPV6_MAX_DEPTH, d will always be lesser than RTE_IPV6_ADDR_SIZE. Replace the magic 8 literals with CHAR_BIT to be consistent with the definition of RTE_IPV6_MAX_DEPTH. Coverity issue: 446756, 446758 Fixes: ca786def84ca ("net: add IPv6 address structure and utils") Signed-off-by: Robin Jarry --- lib/net/rte_ip6.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/net/rte_ip6.h b/lib/net/rte_ip6.h index 3ae38811b27c..c015c977573d 100644 --- a/lib/net/rte_ip6.h +++ b/lib/net/rte_ip6.h @@ -84,8 +84,8 @@ static inline void rte_ipv6_addr_mask(struct rte_ipv6_addr *ip, uint8_t depth) { if (depth < RTE_IPV6_MAX_DEPTH) { - uint8_t d = depth / 8; - uint8_t mask = ~(UINT8_MAX >> (depth % 8)); + unsigned int d = depth / CHAR_BIT; + uint8_t mask = ~(UINT8_MAX >> (depth % CHAR_BIT)); ip->a[d] &= mask; d++; memset(&ip->a[d], 0, sizeof(*ip) - d); @@ -108,8 +108,8 @@ static inline bool rte_ipv6_addr_eq_prefix(const struct rte_ipv6_addr *a, const struct rte_ipv6_addr *b, uint8_t depth) { if (depth < RTE_IPV6_MAX_DEPTH) { - uint8_t d = depth / 8; - uint8_t mask = ~(UINT8_MAX >> (depth % 8)); + unsigned int d = depth / CHAR_BIT; + uint8_t mask = ~(UINT8_MAX >> (depth % CHAR_BIT)); if ((a->a[d] ^ b->a[d]) & mask) return false; -- 2.47.0