Skip to contentSkip to conversation

net/softnic: fix memory leak in parsing arguments

PATCH In review [email protected] 4 participants 11 messages Jul 8, 2021 – Jul 23, 2021 1 message not in the archive Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/softnic: fix memory leak in parsing arguments

From: Dapeng Yu <[email protected]>

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")
---
 drivers/net/softnic/rte_eth_softnic.c           | 9 ++++++---
 drivers/net/softnic/rte_eth_softnic_internals.h | 4 ++--
 2 files changed, 8 insertions(+), 5 deletions(-)
drivers/net/softnic/rte_eth_softnic.c +6 -3
index f64023256d..4d2f93e9c6 100644
@@ -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,7 @@ pmd_parse_args(struct pmd_params *p, const char *params)
 
 	/* Set default values */
 	memset(p, 0, sizeof(*p));
-	p->firmware = SOFTNIC_FIRMWARE;
+	snprintf(p->firmware, sizeof(p->firmware), "%s", SOFTNIC_FIRMWARE);
 	p->cpu_id = SOFTNIC_CPU_ID;
 	p->sc = SOFTNIC_SC;
 	p->tm.n_queues = SOFTNIC_TM_N_QUEUES;
@@ -468,10 +469,12 @@ 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;
 	}
+	snprintf(p->firmware, sizeof(p->firmware), "%s", firmware);
+	free(firmware);
 
 	/* Connection listening port (optional) */
 	if (rte_kvargs_count(kvlist, PMD_PARAM_CONN_PORT) == 1) {
@@ -621,7 +624,7 @@ pmd_probe(struct rte_vdev_device *vdev)
 	if (status)
 		return status;
 
-	p.name = name;
+	snprintf(p.name, sizeof(p.name), "%s", name);
 
 	/* Allocate and initialize soft ethdev private data */
 	dev_private = pmd_init(&p);
drivers/net/softnic/rte_eth_softnic_internals.h +2 -2
index 1b3186ef0b..a13d67b7c1 100644
@@ -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. */
Signature · 1 line
2.27.0
#1 lore raw

@Jasvinder, @Cristian, could you the patch, please.

Quoted 33 lines · On 7/8/21 11:44 AM, [email protected] wrote:

From: Dapeng Yu <[email protected]>

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")
---
 drivers/net/softnic/rte_eth_softnic.c           | 9 ++++++---
 drivers/net/softnic/rte_eth_softnic_internals.h | 4 ++--
 2 files changed, 8 insertions(+), 5 deletions(-)
drivers/net/softnic/rte_eth_softnic.c +1 -0
index f64023256d..4d2f93e9c6 100644
@@ -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)

drivers/net/softnic/rte_eth_softnic.c pmd_parse_args(struct pmd_params *p, const char *params) · line 451

Quoted 5 lines
drivers/net/softnic/rte_eth_softnic.c +1 -1
@@ -447,7 +448,7 @@ pmd_parse_args(struct pmd_params *p, const char *params)
 
 	/* Set default values */
 	memset(p, 0, sizeof(*p));
-	p->firmware = SOFTNIC_FIRMWARE;
+	snprintf(p->firmware, sizeof(p->firmware), "%s", SOFTNIC_FIRMWARE);

Looking at strlcpy(), rte_strlcpy() and rte_strscpy() I think that rte_strscpy() is the best option here since it is easier to check return value to protect against buffer overflow and shortened result.

drivers/net/softnic/rte_eth_softnic.c pmd_parse_args(struct pmd_params *p, const char *params) · line 476

Quoted 13 lines
drivers/net/softnic/rte_eth_softnic.c +2 -1
 	p->cpu_id = SOFTNIC_CPU_ID;
 	p->sc = SOFTNIC_SC;
 	p->tm.n_queues = SOFTNIC_TM_N_QUEUES;
@@ -468,10 +469,12 @@ 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;
 	}
+	snprintf(p->firmware, sizeof(p->firmware), "%s", firmware);

Same here.

drivers/net/softnic/rte_eth_softnic.c pmd_probe(struct rte_vdev_device *vdev) · line 627

Quoted 10 lines
drivers/net/softnic/rte_eth_softnic.c +2 -1
+	free(firmware);
 
 	/* Connection listening port (optional) */
 	if (rte_kvargs_count(kvlist, PMD_PARAM_CONN_PORT) == 1) {
@@ -621,7 +624,7 @@ pmd_probe(struct rte_vdev_device *vdev)
 	if (status)
 		return status;
 
-	p.name = name;
+	snprintf(p.name, sizeof(p.name), "%s", name);

Same here.

Quoted 19 lines
drivers/net/softnic/rte_eth_softnic.c +0 -0
 	/* Allocate and initialize soft ethdev private data */
 	dev_private = pmd_init(&p);
drivers/net/softnic/rte_eth_softnic_internals.h +2 -2
index 1b3186ef0b..a13d67b7c1 100644
@@ -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 lore raw

PATCH v2 1/1 net/softnic: fix memory leak in parsing arguments

In reply to [email protected]

From: Dapeng Yu <[email protected]>

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")

---
V2:
* improve the patch according to maintainer's comment:
rte_strscpy() is the best option here.

---
 drivers/net/softnic/rte_eth_softnic.c           | 9 ++++++---
 drivers/net/softnic/rte_eth_softnic_internals.h | 4 ++--
 2 files changed, 8 insertions(+), 5 deletions(-)
drivers/net/softnic/rte_eth_softnic.c +6 -3
index f64023256d..8a49e83dce 100644
@@ -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,7 @@ pmd_parse_args(struct pmd_params *p, const char *params)
 
 	/* Set default values */
 	memset(p, 0, sizeof(*p));
-	p->firmware = SOFTNIC_FIRMWARE;
+	rte_strscpy(p->firmware, SOFTNIC_FIRMWARE, sizeof(p->firmware));
 	p->cpu_id = SOFTNIC_CPU_ID;
 	p->sc = SOFTNIC_SC;
 	p->tm.n_queues = SOFTNIC_TM_N_QUEUES;
@@ -468,10 +469,12 @@ 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;
 	}
+	rte_strscpy(p->firmware, firmware, sizeof(p->firmware));
+	free(firmware);
 
 	/* Connection listening port (optional) */
 	if (rte_kvargs_count(kvlist, PMD_PARAM_CONN_PORT) == 1) {
@@ -621,7 +624,7 @@ pmd_probe(struct rte_vdev_device *vdev)
 	if (status)
 		return status;
 
-	p.name = name;
+	rte_strscpy(p.name, name, sizeof(p.name));
 
 	/* Allocate and initialize soft ethdev private data */
 	dev_private = pmd_init(&p);
drivers/net/softnic/rte_eth_softnic_internals.h +2 -2
index 1b3186ef0b..a13d67b7c1 100644
@@ -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. */
Signature · 1 line
2.27.0
#3 lore raw
Quoted 37 lines · On 7/13/21 1:08 PM, [email protected] wrote:

From: Dapeng Yu <[email protected]>

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")

---
V2:
* improve the patch according to maintainer's comment:
rte_strscpy() is the best option here.

---
 drivers/net/softnic/rte_eth_softnic.c           | 9 ++++++---
 drivers/net/softnic/rte_eth_softnic_internals.h | 4 ++--
 2 files changed, 8 insertions(+), 5 deletions(-)
drivers/net/softnic/rte_eth_softnic.c +1 -0
index f64023256d..8a49e83dce 100644
@@ -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)

drivers/net/softnic/rte_eth_softnic.c pmd_parse_args(struct pmd_params *p, const char *params) · line 451

Quoted 5 lines
drivers/net/softnic/rte_eth_softnic.c +1 -1
@@ -447,7 +448,7 @@ pmd_parse_args(struct pmd_params *p, const char *params)
 
 	/* Set default values */
 	memset(p, 0, sizeof(*p));
-	p->firmware = SOFTNIC_FIRMWARE;
+	rte_strscpy(p->firmware, SOFTNIC_FIRMWARE, sizeof(p->firmware));

I still don't understand why return value is not checked here and in similar cases below.

Quoted 42 lines
drivers/net/softnic/rte_eth_softnic.c +4 -2
 	p->cpu_id = SOFTNIC_CPU_ID;
 	p->sc = SOFTNIC_SC;
 	p->tm.n_queues = SOFTNIC_TM_N_QUEUES;
@@ -468,10 +469,12 @@ 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;
 	}
+	rte_strscpy(p->firmware, firmware, sizeof(p->firmware));
+	free(firmware);
 
 	/* Connection listening port (optional) */
 	if (rte_kvargs_count(kvlist, PMD_PARAM_CONN_PORT) == 1) {
@@ -621,7 +624,7 @@ pmd_probe(struct rte_vdev_device *vdev)
 	if (status)
 		return status;
 
-	p.name = name;
+	rte_strscpy(p.name, name, sizeof(p.name));
 
 	/* Allocate and initialize soft ethdev private data */
 	dev_private = pmd_init(&p);
drivers/net/softnic/rte_eth_softnic_internals.h +2 -2
index 1b3186ef0b..a13d67b7c1 100644
@@ -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. */

A message that is not in the archive (sent off-list or not yet received): PH0PR11MB512563E3EA51977861DF77CF8C149@PH0PR11MB5125.namprd11.prod.outlook.com

#5 lore raw

Adding list and maintainers back.

Quoted 59 lines · DapengX
Quoted 51 lines

-----Original Message-----
From: Andrew Rybchenko <[email protected]>
Sent: Tuesday, July 13, 2021 6:11 PM
To: Yu, DapengX <[email protected]>; Singh, Jasvinder <[email protected]>; Dumitrescu, Cristian <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [dpdk-dev] [PATCH v2] net/softnic: fix memory leak in parsing arguments

Quoted 40 lines · On 7/13/21 1:08 PM, [email protected] wrote:

From: Dapeng Yu <[email protected]>

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")

---
V2:
* improve the patch according to maintainer's comment:
rte_strscpy() is the best option here.

---
 drivers/net/softnic/rte_eth_softnic.c           | 9 ++++++---
 drivers/net/softnic/rte_eth_softnic_internals.h | 4 ++--
 2 files changed, 8 insertions(+), 5 deletions(-)
drivers/net/softnic/rte_eth_softnic.c +1 -0
index f64023256d..8a49e83dce 100644
@@ -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,7 @@ pmd_parse_args(struct pmd_params *p, const char
 *params)
 
Quoted 8 lines

drivers/net/softnic/rte_eth_softnic.c pmd_parse_args(struct pmd_params *p, const char *params)

Quoted 5 lines
drivers/net/softnic/rte_eth_softnic.c +1 -1
 	/* Set default values */
 	memset(p, 0, sizeof(*p));
-	p->firmware = SOFTNIC_FIRMWARE;
+	rte_strscpy(p->firmware, SOFTNIC_FIRMWARE, sizeof(p-
 firmware));

I still don't understand why return value is not checked here and in similar cases below.

Quoted 3 lines

If the return value is not checked here, and the dst path string is not complete when the path string is too long, softnic_cli_script_process() will prompt error. So the fix will not cause more negative impact.
And for the similar case below, the src and dst string length is same always, so no negative impact too.

I see. For me it sounds a bit fragile, but not critical. Anyway since I'm not 100% sure I'll wait for maintainers review.

Quoted 47 lines
Quoted 46 lines

drivers/net/softnic/rte_eth_softnic.c

Quoted 4 lines
drivers/net/softnic/rte_eth_softnic.c +0 -0
 	p->cpu_id = SOFTNIC_CPU_ID;
drivers/net/softnic/rte_eth_softnic.c +0 -0
 	p->sc = SOFTNIC_SC;
 	p->tm.n_queues = SOFTNIC_TM_N_QUEUES; @@ -468,10 +469,12

@@

Quoted 4 lines

pmd_parse_args(struct pmd_params *p, const char *params)

drivers/net/softnic/rte_eth_softnic.c +0 -0
 	/* Firmware script (optional) */
 	if (rte_kvargs_count(kvlist, PMD_PARAM_FIRMWARE) == 1) {
 		ret = rte_kvargs_process(kvlist, PMD_PARAM_FIRMWARE,

drivers/net/softnic/rte_eth_softnic.c pmd_parse_args(struct pmd_params *p, const char *params)

Quoted 10 lines
drivers/net/softnic/rte_eth_softnic.c +3 -1
-			&get_string, &p->firmware);
+			&get_string, &firmware);
 		if (ret < 0)
 			goto out_free;
 	}
+	rte_strscpy(p->firmware, firmware, sizeof(p->firmware));
+	free(firmware);
 
 	/* Connection listening port (optional) */
 	if (rte_kvargs_count(kvlist, PMD_PARAM_CONN_PORT) == 1) { @@

-621,7

Quoted 26 lines

+624,7 @@ pmd_probe(struct rte_vdev_device *vdev)

drivers/net/softnic/rte_eth_softnic.c +1 -1
 	if (status)
 		return status;
 
-	p.name = name;
+	rte_strscpy(p.name, name, sizeof(p.name));
 
 	/* Allocate and initialize soft ethdev private data */
 	dev_private = pmd_init(&p);
drivers/net/softnic/rte_eth_softnic_internals.h +2 -2
index 1b3186ef0b..a13d67b7c1 100644
@@ -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. */
#6 lore raw

PATCH v3 1/1 net/softnic: fix memory leak in parsing arguments

In reply to [email protected]

From: Dapeng Yu <[email protected]>

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")

---
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(-)
drivers/net/softnic/rte_eth_softnic.c +26 -4
index f64023256d..0aa7147b13 100644
@@ -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);
drivers/net/softnic/rte_eth_softnic_internals.h +2 -2
index 1b3186ef0b..a13d67b7c1 100644
@@ -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. */
Signature · 1 line
2.27.0
#7 lore raw

<snip>

Review

Quoted 1 line
(quoted diff) +1 -0
+	free(firmware);

Memory for firmware is not allocated dynamically, so no need for this.

<snip>

drivers/net/softnic/rte_eth_softnic_internals.h line 37

Quoted 4 lines

struct pmd_params {

drivers/net/softnic/rte_eth_softnic_internals.h +1 -2
-	const char *name;
-	const char *firmware;
+	char name[RTE_DEV_NAME_MAX_LEN];

Please replace " RTE_DEV_NAME_MAX_LEN " with "NAME_SIZE" which is already defined in softnic_internals.h

drivers/net/softnic/rte_eth_softnic_internals.h line 38

Quoted 1 line
drivers/net/softnic/rte_eth_softnic_internals.h +1 -0
+	char firmware[PATH_MAX];

Also, instead of using PATH_MAX, define new macro "SOFTNIC_PATH_MAX 4096" in softnic_internals.h

Quoted 5 lines
drivers/net/softnic/rte_eth_softnic_internals.h +0 -1
 	uint16_t conn_port;
 	uint32_t cpu_id;
 	int sc; /**< Service cores. */
--

2.27.0

#8 lore raw
Quoted 10 lines

-----Original Message-----
From: Singh, Jasvinder <[email protected]>
Sent: Wednesday, July 14, 2021 7:08 PM
To: Yu, DapengX <[email protected]>; Dumitrescu, Cristian <[email protected]>
Cc: [email protected]; [email protected]
Subject: RE: [PATCH v3] net/softnic: fix memory leak in parsing arguments

Quoted 5 lines

<snip>

Review

Quoted 1 line
(quoted diff) +1 -0
+	free(firmware);

Memory for firmware is not allocated dynamically, so no need for this.

I have double checked it, at this code snippet, the memory block referenced by the "firmware" pointer is allocated by the strdup() in get_string() function. The free(firmware) is necessary, and it prevents memory leak.

Quoted 10 lines

<snip>

drivers/net/softnic/rte_eth_softnic_internals.h line 37

Quoted 4 lines

struct pmd_params {

drivers/net/softnic/rte_eth_softnic_internals.h +1 -2
-	const char *name;
-	const char *firmware;
+	char name[RTE_DEV_NAME_MAX_LEN];

Please replace " RTE_DEV_NAME_MAX_LEN " with "NAME_SIZE" which is already defined in softnic_internals.h

It will be modified in the next version.

Quoted 5 lines

drivers/net/softnic/rte_eth_softnic_internals.h line 38

Quoted 2 lines
drivers/net/softnic/rte_eth_softnic_internals.h +1 -0
+	char firmware[PATH_MAX];

Also, instead of using PATH_MAX, define new macro "SOFTNIC_PATH_MAX 4096" in softnic_internals.h

It will be modified in the next version.

Quoted 7 lines
Quoted 7 lines
drivers/net/softnic/rte_eth_softnic_internals.h +0 -1
 	uint16_t conn_port;
 	uint32_t cpu_id;
 	int sc; /**< Service cores. */
--

2.27.0

#9 lore raw

PATCH v4 1/1 net/softnic: fix memory leak in parsing arguments

In reply to [email protected]

From: Dapeng Yu <[email protected]>

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")

---
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 V4:
* replace RTE_DEV_NAME_MAX_LEN with NAME_SIZE
* replace PATH_MAX with SOFTNIC_PATH_MAX

---
 drivers/net/softnic/rte_eth_softnic.c         | 30 ++++++++++++++++---
 .../net/softnic/rte_eth_softnic_internals.h   |  5 ++--
 2 files changed, 29 insertions(+), 6 deletions(-)
drivers/net/softnic/rte_eth_softnic.c +26 -4
index f64023256d..0aa7147b13 100644
@@ -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);
drivers/net/softnic/rte_eth_softnic_internals.h +3 -2
index 1b3186ef0b..07285ca315 100644
@@ -28,14 +28,15 @@
 #include "conn.h"
 
 #define NAME_SIZE                                            64
+#define SOFTNIC_PATH_MAX                                     4096
 
 /**
  * PMD Parameters
  */
 
 struct pmd_params {
-	const char *name;
-	const char *firmware;
+	char name[NAME_SIZE];
+	char firmware[SOFTNIC_PATH_MAX];
 	uint16_t conn_port;
 	uint32_t cpu_id;
 	int sc; /**< Service cores. */
Signature · 1 line
2.27.0
Acked-by on v4 1/1
#10 lore raw
Quoted 20 lines

-----Original Message-----
From: Yu, DapengX <[email protected]>
Sent: Thursday, July 15, 2021 6:38 AM
To: Singh, Jasvinder <[email protected]>; Dumitrescu, Cristian <[email protected]>
Cc: [email protected]; Yu, DapengX <[email protected]>; [email protected]
Subject: [PATCH v4] net/softnic: fix memory leak in parsing arguments

From: Dapeng Yu <[email protected]>

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.

Quoted 5 lines
Fixes 7e68bc20f8c8 ("net/softnic: restructure")

---

#11 lore raw
Quoted 10 lines
Quoted 10 lines

From: Dapeng Yu <[email protected]>

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.

Quoted 6 lines
Quoted 4 lines
Fixes 7e68bc20f8c8 ("net/softnic: restructure")

Applied, thanks.