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 4B56445A68; Mon, 30 Sep 2024 15:49:24 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id EF2F7402A3; Mon, 30 Sep 2024 15:49:23 +0200 (CEST) Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) by mails.dpdk.org (Postfix) with ESMTP id 1677B4014F for ; Mon, 30 Sep 2024 15:40:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1727703607; x=1759239607; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=12I8Ru/4gWqq9XyQbmcte1YEyCTdCCqvRbuFBK9TdGc=; b=fA6GAXBKHhNkVSE6FZ7htzF5nRhJq6dBGQzVwUUlF8Kj8dnXc6kXXXnJ 0I5w2M+rrt0LNTzXebrgXElAvyiuldPLi62gMHWbKV2AUNaP2K6UEANNI bK9i1ObEn1i48r1Z0lQG3SuHCLYqMPY4E2m+WoQuwGOZ4mt50RB046WVN xIn3pwSUCtLxIzyFOtYOUqKkkvnBDGu1Xb4UcBhi0XfZxsHUhigafDkAh e4EwyPW3UYLflUjkTLvpnW3fUkszM+uKFhMh8HPf2GuN2vNHhPXnCxzuI RPoQxI+7qCzK6MNHw2TTjnal5pEv+cmcHKw+Dvg7z+fJvzYBQWnBcx6xa g==; X-CSE-ConnectionGUID: 0v6zWEhzSZ6i4PZU3TY3PA== X-CSE-MsgGUID: 33lDKL98T72DVsUjsTmzsA== X-IronPort-AV: E=McAfee;i="6700,10204,11211"; a="37466693" X-IronPort-AV: E=Sophos;i="6.11,165,1725346800"; d="scan'208";a="37466693" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Sep 2024 06:40:06 -0700 X-CSE-ConnectionGUID: NuEK2LJlQ0CEOXowA44AjQ== X-CSE-MsgGUID: z3AjbKwNQvmuGzfO+z9Ymw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,165,1725346800"; d="scan'208";a="73612566" Received: from silpixa00401183.ir.intel.com ([10.55.128.236]) by fmviesa010.fm.intel.com with ESMTP; 30 Sep 2024 06:40:04 -0700 From: Niall Meade To: Thomas Monjalon , Ferruh Yigit , Andrew Rybchenko , Roman Zhukov Cc: dev@dpdk.org, Niall Meade Subject: [PATCH v3 1/1] ethdev: fix int overflow in descriptor count logic Date: Mon, 30 Sep 2024 13:40:02 +0000 Message-Id: <20240930134002.508717-1-niall.meade@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240927132329.296038-1-niall.meade@intel.com> References: <20240927132329.296038-1-niall.meade@intel.com> MIME-Version: 1.0 Organization: Intel Research and Development Ireland Ltd - Co. Reg. #308263 - Collinstown Industrial Park, Leixlip, County Kildare, Ireland Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailman-Approved-At: Mon, 30 Sep 2024 15:49:23 +0200 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 Addressed a specific overflow issue in the eth_dev_adjust_nb_desc() function where the uint16_t variable nb_desc would overflow when its value was greater than (2^16 - nb_align). This overflow caused nb_desc to incorrectly wrap around between 0 and nb_align-1, leading to the function setting nb_desc to nb_min instead of the expected nb_max. To give an example, let nb_desc=UINT16_MAX, nb_align=32, nb_max=4096 and nb_min=64. RTE_ALIGN_CEIL(nb_desc, nb_align) calls RTE_ALIGN_FLOOR(nb_desc + nb_align - 1, nb_align). This results in an overflow of nb_desc, leading to nb_desc being set to 30 and then 0 when the macros return. As a result of this, nb_desc is then set to nb_min later on. The resolution involves upcasting nb_desc to a uint32_t before the RTE_ALIGN_CEIL macro is applied. This change ensures that the subsequent call to RTE_ALIGN_FLOOR(nb_desc + (nb_align - 1), nb_align) does not result in an overflow, as it would when nb_desc is a uint16_t. By using a uint32_t for these operations, the correct behavior is maintained without the risk of overflow. Fixes: 0f67fc3baeb9 ("ethdev: add function to adjust number of descriptors") Signed-off-by: Niall Meade --- v3: * changed to explicit cast v2: * add example of issue to commit message --- .mailmap | 1 + lib/ethdev/rte_ethdev.c | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.mailmap b/.mailmap index 4a508bafad..c1941e78bb 100644 --- a/.mailmap +++ b/.mailmap @@ -1053,6 +1053,7 @@ Nelson Escobar Nemanja Marjanovic Netanel Belgazal Netanel Gonen +Niall Meade Niall Power Nicholas Pratte Nick Connolly diff --git a/lib/ethdev/rte_ethdev.c b/lib/ethdev/rte_ethdev.c index f1c658f49e..da1b831ff0 100644 --- a/lib/ethdev/rte_ethdev.c +++ b/lib/ethdev/rte_ethdev.c @@ -6577,13 +6577,19 @@ static void eth_dev_adjust_nb_desc(uint16_t *nb_desc, const struct rte_eth_desc_lim *desc_lim) { + /* Upcast to uint32 to avoid potential overflow with RTE_ALIGN_CEIL(). */ + uint32_t nb_desc_32 = (uint32_t)*nb_desc; + if (desc_lim->nb_align != 0) - *nb_desc = RTE_ALIGN_CEIL(*nb_desc, desc_lim->nb_align); + nb_desc_32 = RTE_ALIGN_CEIL(nb_desc_32, desc_lim->nb_align); if (desc_lim->nb_max != 0) - *nb_desc = RTE_MIN(*nb_desc, desc_lim->nb_max); + nb_desc_32 = RTE_MIN(nb_desc_32, desc_lim->nb_max); + + nb_desc_32 = RTE_MAX(nb_desc_32, desc_lim->nb_min); - *nb_desc = RTE_MAX(*nb_desc, desc_lim->nb_min); + /* Assign clipped u32 back to u16. */ + *nb_desc = (uint16_t)nb_desc_32; } int -- 2.34.1 -------------------------------------------------------------- Intel Research and Development Ireland Limited Registered in Ireland Registered Office: Collinstown Industrial Park, Leixlip, County Kildare Registered Number: 308263 This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.