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 47BF1A0350 for ; Mon, 21 Feb 2022 16:41:04 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 4011F40DF6; Mon, 21 Feb 2022 16:41:04 +0100 (CET) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mails.dpdk.org (Postfix) with ESMTP id 7886D40DF6 for ; Mon, 21 Feb 2022 16:41:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645458063; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ihBT08vlYTN+V/RUo12lu59OSLlNT2bXIVzX88ims/E=; b=dY5CzcFRW9KC32MK5MTW0Z3NjdQYC7dalNlCejSRc2sfXouD+8ZOSJPcxwamsDMhwQAVwA 6W61RLyF66ShN5BtMjWfO+t3G+BkjCcdkC5W7O4N7oa9zCeW80jIct2fC43/WHlubBgsCf KaVQIOUzZ5Trkgs3RTOqrqvR0qCPbqQ= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-572-YL7q6qE7MuS1Xsct3F6atw-1; Mon, 21 Feb 2022 10:40:57 -0500 X-MC-Unique: YL7q6qE7MuS1Xsct3F6atw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B60581091DA1; Mon, 21 Feb 2022 15:40:56 +0000 (UTC) Received: from rh.Home (unknown [10.39.195.12]) by smtp.corp.redhat.com (Postfix) with ESMTP id AB9F77E131; Mon, 21 Feb 2022 15:40:55 +0000 (UTC) From: Kevin Traynor To: Huisong Li Cc: Min Hu , dpdk stable Subject: patch 'net/hns3: fix insecure way to query MAC statistics' has been queued to stable release 21.11.1 Date: Mon, 21 Feb 2022 15:35:20 +0000 Message-Id: <20220221153625.152324-131-ktraynor@redhat.com> In-Reply-To: <20220221153625.152324-1-ktraynor@redhat.com> References: <20220221153625.152324-1-ktraynor@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=ktraynor@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: stable-bounces@dpdk.org Hi, FYI, your patch has been queued to stable release 21.11.1 Note it hasn't been pushed to http://dpdk.org/browse/dpdk-stable yet. It will be pushed if I get no objections before 02/26/22. So please shout if anyone has objections. Also note that after the patch there's a diff of the upstream commit vs the patch applied to the branch. This will indicate if there was any rebasing needed to apply to the stable branch. If there were code changes for rebasing (ie: not only metadata diffs), please double check that the rebase was correctly done. Queued patches are on a temporary branch at: https://github.com/kevintraynor/dpdk-stable This queued commit can be viewed at: https://github.com/kevintraynor/dpdk-stable/commit/10342b22ae8f0d8f6b7971aa3e88a28562fd77ea Thanks. Kevin --- >From 10342b22ae8f0d8f6b7971aa3e88a28562fd77ea Mon Sep 17 00:00:00 2001 From: Huisong Li Date: Fri, 28 Jan 2022 10:07:05 +0800 Subject: [PATCH] net/hns3: fix insecure way to query MAC statistics [ upstream commit 6ee07e3cb589145d0f0826f918fd44eb3e160a08 ] The query way of MAC statistics in HNS3 PF driver is as following: 1) get MAC statistics register number and calculate descriptor number. 2) use above descriptor number to send command to firmware to query all MAC statistics and copy to hns3_mac_stats struct in driver. The preceding way does not verify the validity of the number of obtained register, which may cause memory out-of-bounds. Fixes: 8839c5e202f3 ("net/hns3: support device stats") Signed-off-by: Huisong Li Signed-off-by: Min Hu (Connor) --- drivers/net/hns3/hns3_ethdev.c | 4 ++ drivers/net/hns3/hns3_ethdev.h | 1 + drivers/net/hns3/hns3_stats.c | 120 ++++++++++++++++----------------- drivers/net/hns3/hns3_stats.h | 11 ++- 4 files changed, 67 insertions(+), 69 deletions(-) diff --git a/drivers/net/hns3/hns3_ethdev.c b/drivers/net/hns3/hns3_ethdev.c index ac0c1fb5ec..209ec75eac 100644 --- a/drivers/net/hns3/hns3_ethdev.c +++ b/drivers/net/hns3/hns3_ethdev.c @@ -2759,4 +2759,8 @@ hns3_get_capability(struct hns3_hw *hw) hw->revision = revision; + ret = hns3_query_mac_stats_reg_num(hw); + if (ret) + return ret; + if (revision < PCI_REVISION_ID_HIP09_A) { hns3_set_default_dev_specifications(hw); diff --git a/drivers/net/hns3/hns3_ethdev.h b/drivers/net/hns3/hns3_ethdev.h index 153e67337f..0e12d054e4 100644 --- a/drivers/net/hns3/hns3_ethdev.h +++ b/drivers/net/hns3/hns3_ethdev.h @@ -503,4 +503,5 @@ struct hns3_hw { /* Include Mac stats | Rx stats | Tx stats */ struct hns3_mac_stats mac_stats; + uint32_t mac_stats_reg_num; struct hns3_rx_missed_stats imissed_stats; uint64_t oerror_stats; diff --git a/drivers/net/hns3/hns3_stats.c b/drivers/net/hns3/hns3_stats.c index 606b72509a..806720faff 100644 --- a/drivers/net/hns3/hns3_stats.c +++ b/drivers/net/hns3/hns3_stats.c @@ -308,22 +308,19 @@ static const struct hns3_xstats_name_offset hns3_imissed_stats_strings[] = { static void hns3_tqp_stats_clear(struct hns3_hw *hw); -/* - * Query all the MAC statistics data of Network ICL command ,opcode id: 0x0034. - * This command is used before send 'query_mac_stat command', the descriptor - * number of 'query_mac_stat command' must match with reg_num in this command. - * @praram hw - * Pointer to structure hns3_hw. - * @return - * 0 on success. - */ static int -hns3_update_mac_stats(struct hns3_hw *hw, const uint32_t desc_num) +hns3_update_mac_stats(struct hns3_hw *hw) { +#define HNS3_MAC_STATS_REG_NUM_PER_DESC 4 + uint64_t *data = (uint64_t *)(&hw->mac_stats); struct hns3_cmd_desc *desc; + uint32_t stats_iterms; uint64_t *desc_data; - uint16_t i, k, n; + uint32_t desc_num; + uint16_t i; int ret; + /* The first desc has a 64-bit header, so need to consider it. */ + desc_num = hw->mac_stats_reg_num / HNS3_MAC_STATS_REG_NUM_PER_DESC + 1; desc = rte_malloc("hns3_mac_desc", desc_num * sizeof(struct hns3_cmd_desc), 0); @@ -341,19 +338,15 @@ hns3_update_mac_stats(struct hns3_hw *hw, const uint32_t desc_num) } - for (i = 0; i < desc_num; i++) { - /* For special opcode 0034, only the first desc has the head */ - if (i == 0) { - desc_data = (uint64_t *)(&desc[i].data[0]); - n = HNS3_RD_FIRST_STATS_NUM; - } else { - desc_data = (uint64_t *)(&desc[i]); - n = HNS3_RD_OTHER_STATS_NUM; - } - - for (k = 0; k < n; k++) { - *data += rte_le_to_cpu_64(*desc_data); - data++; - desc_data++; - } + stats_iterms = RTE_MIN(sizeof(hw->mac_stats) / sizeof(uint64_t), + hw->mac_stats_reg_num); + desc_data = (uint64_t *)(&desc[0].data[0]); + for (i = 0; i < stats_iterms; i++) { + /* + * Data memory is continuous and only the first descriptor has a + * header in this command. + */ + *data += rte_le_to_cpu_64(*desc_data); + data++; + desc_data++; } rte_free(desc); @@ -362,42 +355,52 @@ hns3_update_mac_stats(struct hns3_hw *hw, const uint32_t desc_num) } -/* - * Query Mac stat reg num command ,opcode id: 0x0033. - * This command is used before send 'query_mac_stat command', the descriptor - * number of 'query_mac_stat command' must match with reg_num in this command. - * @praram rte_stats - * Pointer to structure rte_eth_stats. - * @return - * 0 on success. - */ static int -hns3_mac_query_reg_num(struct rte_eth_dev *dev, uint32_t *desc_num) +hns3_mac_query_reg_num(struct hns3_hw *hw, uint32_t *reg_num) { - struct hns3_adapter *hns = dev->data->dev_private; - struct hns3_hw *hw = &hns->hw; +#define HNS3_MAC_STATS_RSV_REG_NUM_ON_HIP08_B 3 struct hns3_cmd_desc desc; - uint32_t *desc_data; - uint32_t reg_num; int ret; hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_MAC_REG_NUM, true); ret = hns3_cmd_send(hw, &desc, 1); + if (ret) { + hns3_err(hw, "failed to query MAC statistic reg number, ret = %d", + ret); + return ret; + } + + /* The number of MAC statistics registers are provided by firmware. */ + *reg_num = rte_le_to_cpu_32(desc.data[0]); + if (*reg_num == 0) { + hns3_err(hw, "MAC statistic reg number is invalid!"); + return -ENODATA; + } + + /* + * If driver doesn't request the firmware to report more MAC statistics + * iterms and the total number of MAC statistics registers by using new + * method, firmware will only reports the number of valid statistics + * registers. However, structure hns3_mac_stats in driver contains valid + * and reserved statistics iterms. In this case, the total register + * number must be added to three reserved statistics registers. + */ + *reg_num += HNS3_MAC_STATS_RSV_REG_NUM_ON_HIP08_B; + + return 0; +} + +int +hns3_query_mac_stats_reg_num(struct hns3_hw *hw) +{ + uint32_t mac_stats_reg_num = 0; + int ret; + + ret = hns3_mac_query_reg_num(hw, &mac_stats_reg_num); if (ret) return ret; - /* - * The num of MAC statistics registers that are provided by IMP in this - * version. - */ - desc_data = (uint32_t *)(&desc.data[0]); - reg_num = rte_le_to_cpu_32(*desc_data); - - /* - * The descriptor number of 'query_additional_mac_stat command' is - * '1 + (reg_num-3)/4 + ((reg_num-3)%4 !=0)'; - * This value is 83 in this version - */ - *desc_num = 1 + ((reg_num - 3) >> 2) + - (uint32_t)(((reg_num - 3) & 0x3) ? 1 : 0); + hw->mac_stats_reg_num = mac_stats_reg_num; + if (hw->mac_stats_reg_num > sizeof(hw->mac_stats) / sizeof(uint64_t)) + hns3_warn(hw, "MAC stats reg number from firmware is greater than stats iterms in driver."); return 0; @@ -409,13 +412,6 @@ hns3_query_update_mac_stats(struct rte_eth_dev *dev) struct hns3_adapter *hns = dev->data->dev_private; struct hns3_hw *hw = &hns->hw; - uint32_t desc_num; - int ret; - ret = hns3_mac_query_reg_num(dev, &desc_num); - if (ret == 0) - ret = hns3_update_mac_stats(hw, desc_num); - else - hns3_err(hw, "Query mac reg num fail : %d", ret); - return ret; + return hns3_update_mac_stats(hw); } diff --git a/drivers/net/hns3/hns3_stats.h b/drivers/net/hns3/hns3_stats.h index d1230f94cb..c81d351082 100644 --- a/drivers/net/hns3/hns3_stats.h +++ b/drivers/net/hns3/hns3_stats.h @@ -6,9 +6,4 @@ #define _HNS3_STATS_H_ -/* stats macro */ -#define HNS3_MAC_CMD_NUM 21 -#define HNS3_RD_FIRST_STATS_NUM 2 -#define HNS3_RD_OTHER_STATS_NUM 4 - /* TQP stats */ struct hns3_tqp_stats { @@ -23,4 +18,5 @@ struct hns3_mac_stats { uint64_t mac_tx_mac_pause_num; uint64_t mac_rx_mac_pause_num; + uint64_t rsv0; uint64_t mac_tx_pfc_pri0_pkt_num; uint64_t mac_tx_pfc_pri1_pkt_num; @@ -59,5 +55,5 @@ struct hns3_mac_stats { uint64_t mac_tx_2048_4095_oct_pkt_num; uint64_t mac_tx_4096_8191_oct_pkt_num; - uint64_t rsv0; + uint64_t rsv1; uint64_t mac_tx_8192_9216_oct_pkt_num; uint64_t mac_tx_9217_12287_oct_pkt_num; @@ -86,5 +82,5 @@ struct hns3_mac_stats { uint64_t mac_rx_2048_4095_oct_pkt_num; uint64_t mac_rx_4096_8191_oct_pkt_num; - uint64_t rsv1; + uint64_t rsv2; uint64_t mac_rx_8192_9216_oct_pkt_num; uint64_t mac_rx_9217_12287_oct_pkt_num; @@ -169,4 +165,5 @@ int hns3_tqp_stats_init(struct hns3_hw *hw); void hns3_tqp_stats_uninit(struct hns3_hw *hw); int hns3_update_imissed_stats(struct hns3_hw *hw, bool is_clear); +int hns3_query_mac_stats_reg_num(struct hns3_hw *hw); #endif /* _HNS3_STATS_H_ */ -- 2.34.1 --- Diff of the applied patch vs upstream commit (please double-check if non-empty: --- --- - 2022-02-21 15:22:47.432275095 +0000 +++ 0131-net-hns3-fix-insecure-way-to-query-MAC-statistics.patch 2022-02-21 15:22:44.267704522 +0000 @@ -1 +1 @@ -From 6ee07e3cb589145d0f0826f918fd44eb3e160a08 Mon Sep 17 00:00:00 2001 +From 10342b22ae8f0d8f6b7971aa3e88a28562fd77ea Mon Sep 17 00:00:00 2001 @@ -5,0 +6,2 @@ +[ upstream commit 6ee07e3cb589145d0f0826f918fd44eb3e160a08 ] + @@ -15 +16,0 @@ -Cc: stable@dpdk.org @@ -27 +28 @@ -index 73bf209717..57f1572340 100644 +index ac0c1fb5ec..209ec75eac 100644 @@ -30,2 +31,2 @@ -@@ -2734,4 +2734,8 @@ hns3_get_capability(struct hns3_hw *hw) - return ret; +@@ -2759,4 +2759,8 @@ hns3_get_capability(struct hns3_hw *hw) + hw->revision = revision; @@ -37 +38 @@ - if (hw->revision < PCI_REVISION_ID_HIP09_A) { + if (revision < PCI_REVISION_ID_HIP09_A) { @@ -40 +41 @@ -index cf6380ebb2..ef028a7b2c 100644 +index 153e67337f..0e12d054e4 100644 @@ -43 +44 @@ -@@ -501,4 +501,5 @@ struct hns3_hw { +@@ -503,4 +503,5 @@ struct hns3_hw {