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 EE291A0C4A for ; Wed, 14 Jul 2021 07:47:17 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id D3378410F4; Wed, 14 Jul 2021 07:47:17 +0200 (CEST) Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mails.dpdk.org (Postfix) with ESMTP id 477214003E; Wed, 14 Jul 2021 07:47:15 +0200 (CEST) X-IronPort-AV: E=McAfee;i="6200,9189,10044"; a="189974512" X-IronPort-AV: E=Sophos;i="5.84,238,1620716400"; d="scan'208";a="189974512" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jul 2021 22:47:13 -0700 X-IronPort-AV: E=Sophos;i="5.84,238,1620716400"; d="scan'208";a="494033271" Received: from unknown (HELO localhost.localdomain) ([10.240.183.93]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Jul 2021 22:47:11 -0700 From: dapengx.yu@intel.com To: Jasvinder Singh , Cristian Dumitrescu Cc: dev@dpdk.org, Dapeng Yu , stable@dpdk.org Date: Wed, 14 Jul 2021 13:47:01 +0800 Message-Id: <20210714054702.764366-1-dapengx.yu@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210713100801.597956-1-dapengx.yu@intel.com> References: <20210713100801.597956-1-dapengx.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-stable] [PATCH v3] net/softnic: fix memory leak in parsing arguments 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 Sender: "stable" From: Dapeng Yu In function pmd_parse_args(), firmware path is duplicated from device arguments as character string, but is never freed, which cause memory leak. This patch changes the type of firmware member of struct pmd_params to character array, to make memory resource release unnecessary, and changes the type of name member to character array, to keep the consistency of character string handling in struct pmd_params. Fixes: 7e68bc20f8c8 ("net/softnic: restructure") Cc: stable@dpdk.org Signed-off-by: Dapeng Yu --- V2: * improve the patch according to maintainer's comment: rte_strscpy() is the best option here. V3: * improve the handling of string copy, to make the code more robust --- drivers/net/softnic/rte_eth_softnic.c | 30 ++++++++++++++++--- .../net/softnic/rte_eth_softnic_internals.h | 4 +-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/drivers/net/softnic/rte_eth_softnic.c b/drivers/net/softnic/rte_eth_softnic.c index f64023256d..0aa7147b13 100644 --- a/drivers/net/softnic/rte_eth_softnic.c +++ b/drivers/net/softnic/rte_eth_softnic.c @@ -440,6 +440,7 @@ pmd_parse_args(struct pmd_params *p, const char *params) { struct rte_kvargs *kvlist; int ret = 0; + char *firmware = NULL; kvlist = rte_kvargs_parse(params, pmd_valid_args); if (kvlist == NULL) @@ -447,7 +448,14 @@ pmd_parse_args(struct pmd_params *p, const char *params) /* Set default values */ memset(p, 0, sizeof(*p)); - p->firmware = SOFTNIC_FIRMWARE; + if (rte_strscpy(p->firmware, SOFTNIC_FIRMWARE, + sizeof(p->firmware)) < 0) { + PMD_LOG(WARNING, + "\"%s\": firmware path should be shorter than %zu", + SOFTNIC_FIRMWARE, sizeof(p->firmware)); + ret = -EINVAL; + goto out_free; + } p->cpu_id = SOFTNIC_CPU_ID; p->sc = SOFTNIC_SC; p->tm.n_queues = SOFTNIC_TM_N_QUEUES; @@ -468,11 +476,20 @@ pmd_parse_args(struct pmd_params *p, const char *params) /* Firmware script (optional) */ if (rte_kvargs_count(kvlist, PMD_PARAM_FIRMWARE) == 1) { ret = rte_kvargs_process(kvlist, PMD_PARAM_FIRMWARE, - &get_string, &p->firmware); + &get_string, &firmware); if (ret < 0) goto out_free; } - + if (rte_strscpy(p->firmware, firmware, + sizeof(p->firmware)) < 0) { + PMD_LOG(WARNING, + "\"%s\": firmware path should be shorter than %zu", + firmware, sizeof(p->firmware)); + free(firmware); + ret = -EINVAL; + goto out_free; + } + free(firmware); /* Connection listening port (optional) */ if (rte_kvargs_count(kvlist, PMD_PARAM_CONN_PORT) == 1) { ret = rte_kvargs_process(kvlist, PMD_PARAM_CONN_PORT, @@ -621,7 +638,12 @@ pmd_probe(struct rte_vdev_device *vdev) if (status) return status; - p.name = name; + if (rte_strscpy(p.name, name, sizeof(p.name)) < 0) { + PMD_LOG(WARNING, + "\"%s\": device name should be shorter than %zu", + name, sizeof(p.name)); + return -EINVAL; + } /* Allocate and initialize soft ethdev private data */ dev_private = pmd_init(&p); diff --git a/drivers/net/softnic/rte_eth_softnic_internals.h b/drivers/net/softnic/rte_eth_softnic_internals.h index 1b3186ef0b..a13d67b7c1 100644 --- a/drivers/net/softnic/rte_eth_softnic_internals.h +++ b/drivers/net/softnic/rte_eth_softnic_internals.h @@ -34,8 +34,8 @@ */ struct pmd_params { - const char *name; - const char *firmware; + char name[RTE_DEV_NAME_MAX_LEN]; + char firmware[PATH_MAX]; uint16_t conn_port; uint32_t cpu_id; int sc; /**< Service cores. */ -- 2.27.0