Skip to contentSkip to conversation

net/bonding: fix socket id check

PATCH Superseded Min Hu (Connor) 3 participants 8 messages Apr 22, 2021 – Apr 27, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/bonding: fix socket id check

From: Chengchang Tang <[email protected]>

The socket ID entered by user is cast to an unsigned integer. However, the value may be an illegal negative value, which may cause some problems. In this case, an error should be returned.

In addition, the socket ID may be an invalid positive number, which is also processed in this patch.

Fixes 2efb58cbab6e ("bond: new link bonding library")
---
 drivers/net/bonding/rte_eth_bond_args.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/bonding/rte_eth_bond_args.c +2 -2
index 8c5f90d..bcc0fe3 100644
@@ -207,12 +207,12 @@ bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,
 		return -1;
 
 	errno = 0;
-	socket_id = (uint8_t)strtol(value, &endptr, 10);
+	socket_id = strtol(value, &endptr, 10);
 	if (*endptr != 0 || errno != 0)
 		return -1;
 
 	/* validate socket id value */
-	if (socket_id >= 0) {
+	if (socket_id >= 0 && socket_id < RTE_MAX_NUMA_NODES) {
 		*(uint8_t *)extra_args = (uint8_t)socket_id;
 		return 0;
 	}
Signature · 1 line
2.7.4
#1 lore raw
Quoted 6 lines · Min Hu (Connor)

From: Chengchang Tang <[email protected]>

The socket ID entered by user is cast to an unsigned integer. However, the value may be an illegal negative value, which may cause some problems. In this case, an error should be returned.

+1 to fix

Quoted 17 lines

In addition, the socket ID may be an invalid positive number, which is also processed in this patch.

Fixes 2efb58cbab6e ("bond: new link bonding library")
---
 drivers/net/bonding/rte_eth_bond_args.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/bonding/rte_eth_bond_args.c +0 -0
index 8c5f90d..bcc0fe3 100644

drivers/net/bonding/rte_eth_bond_args.c bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused, · line 210

Quoted 5 lines
drivers/net/bonding/rte_eth_bond_args.c +1 -1
@@ -207,12 +207,12 @@ bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,
 		return -1;
 
 	errno = 0;
-	socket_id = (uint8_t)strtol(value, &endptr, 10);
+	socket_id = strtol(value, &endptr, 10);

'strtol()' returns 'long int', but implicitly casting it to 'int'. My concern is if this cause a static analysis tool warning.
What do you think to have 'socket_id' type as 'long int'?

drivers/net/bonding/rte_eth_bond_args.c bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,

Quoted 6 lines
drivers/net/bonding/rte_eth_bond_args.c +1 -1
 	if (*endptr != 0 || errno != 0)
 		return -1;
 
 	/* validate socket id value */
-	if (socket_id >= 0) {
+	if (socket_id >= 0 && socket_id < RTE_MAX_NUMA_NODES) {>  		*(uint8_t *)extra_args = (uint8_t)socket_id;

Here there is an assumption that RTE_MAX_NUMA_NODES will be less than 'UCHAR_MAX', perhaps it can be good to add a check to verify this assumption.

Quoted 3 lines
drivers/net/bonding/rte_eth_bond_args.c +0 -0
 		return 0;
 	}
 
#2 lore raw
Quoted 29 lines · Ferruh Yigit
Quoted 6 lines · Min Hu (Connor)

From: Chengchang Tang <[email protected]>

The socket ID entered by user is cast to an unsigned integer. However, the value may be an illegal negative value, which may cause some problems. In this case, an error should be returned.

+1 to fix

Quoted 19 lines

In addition, the socket ID may be an invalid positive number, which is also processed in this patch.

Fixes 2efb58cbab6e ("bond: new link bonding library")
---
 drivers/net/bonding/rte_eth_bond_args.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/bonding/rte_eth_bond_args.c +0 -0
index 8c5f90d..bcc0fe3 100644
@@ -207,12 +207,12 @@ bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,
 		return -1;
 
Quoted 8 lines

drivers/net/bonding/rte_eth_bond_args.c bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused, · line 210

Quoted 3 lines
drivers/net/bonding/rte_eth_bond_args.c +1 -1
 	errno = 0;
-	socket_id = (uint8_t)strtol(value, &endptr, 10);
+	socket_id = strtol(value, &endptr, 10);

'strtol()' returns 'long int', but implicitly casting it to 'int'. My concern is if this cause a static analysis tool warning.
What do you think to have 'socket_id' type as 'long int'?

I think it would be better to cast to the 'int' here, for reasons below.

Quoted 9 lines

drivers/net/bonding/rte_eth_bond_args.c bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,

Quoted 6 lines
drivers/net/bonding/rte_eth_bond_args.c +1 -1
 	if (*endptr != 0 || errno != 0)
 		return -1;
 
 	/* validate socket id value */
-	if (socket_id >= 0) {
+	if (socket_id >= 0 && socket_id < RTE_MAX_NUMA_NODES) {>  		*(uint8_t *)extra_args = (uint8_t)socket_id;

Here there is an assumption that RTE_MAX_NUMA_NODES will be less than 'UCHAR_MAX', perhaps it can be good to add a check to verify this assumption.

Currently, it is unlikely that RTE_MAX_NUMA_NODES will be greater than 256. Therefore, adding such check will not cause any problems. But I don't think it's necessary to put such restrictions on it (i.e. RTE_MAX_NUMA_NODES should be less than UCHAR_MAX). I checked all references to RTE_MAX_NUMA_NODES, and usually socket_id is of type 'int' or 'unsigned int' (Only the efd, node, and bonding specify 'unsigned char' for socket IDs.). And for that, I think it will be better to change the socket id type to 'int' in this patch. For the type of socket id in efd and node, I will send new patches to modify it.

Quoted 8 lines

drivers/net/bonding/rte_eth_bond_args.c bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,

Quoted 4 lines
drivers/net/bonding/rte_eth_bond_args.c +0 -0
 		return 0;
 	}
 

.

#3 lore raw
Quoted 40 lines · Chengchang Tang
Quoted 29 lines · Ferruh Yigit
Quoted 6 lines · Min Hu (Connor)

From: Chengchang Tang <[email protected]>

The socket ID entered by user is cast to an unsigned integer. However, the value may be an illegal negative value, which may cause some problems. In this case, an error should be returned.

+1 to fix

Quoted 19 lines

In addition, the socket ID may be an invalid positive number, which is also processed in this patch.

Fixes 2efb58cbab6e ("bond: new link bonding library")
---
 drivers/net/bonding/rte_eth_bond_args.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/bonding/rte_eth_bond_args.c +0 -0
index 8c5f90d..bcc0fe3 100644
@@ -207,12 +207,12 @@ bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,
 		return -1;
 
Quoted 8 lines

drivers/net/bonding/rte_eth_bond_args.c bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused, · line 210

Quoted 3 lines
drivers/net/bonding/rte_eth_bond_args.c +1 -1
 	errno = 0;
-	socket_id = (uint8_t)strtol(value, &endptr, 10);
+	socket_id = strtol(value, &endptr, 10);

'strtol()' returns 'long int', but implicitly casting it to 'int'. My concern is if this cause a static analysis tool warning.
What do you think to have 'socket_id' type as 'long int'?

Quoted 2 lines

I think it would be better to cast to the 'int' here, for reasons below.

Independent from below reasons, converting from user provided "long int" to 'int' will cause losing value and may lead wrong checks,

Like if user provided '-4294967281' (0xffffffff0000000f), when you cast to 'int', it will become '15' (0xf) and will pass from validation checks.

So I think better to verify the value first as "long int", later cast it to 'int'.

Quoted 10 lines
Quoted 9 lines

drivers/net/bonding/rte_eth_bond_args.c bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,

Quoted 6 lines
drivers/net/bonding/rte_eth_bond_args.c +1 -1
 	if (*endptr != 0 || errno != 0)
 		return -1;
 
 	/* validate socket id value */
-	if (socket_id >= 0) {
+	if (socket_id >= 0 && socket_id < RTE_MAX_NUMA_NODES) {>  		*(uint8_t *)extra_args = (uint8_t)socket_id;

Here there is an assumption that RTE_MAX_NUMA_NODES will be less than 'UCHAR_MAX', perhaps it can be good to add a check to verify this assumption.

Quoted 3 lines

Currently, it is unlikely that RTE_MAX_NUMA_NODES will be greater than 256. Therefore, adding such check will not cause any problems. But I don't think it's necessary to put such restrictions on it (i.e. RTE_MAX_NUMA_NODES should be less than UCHAR_MAX).

Restriction comes from provided 'extra_args' being 'uint8_t', I just suggest checking this.

Quoted 14 lines

I checked all references to RTE_MAX_NUMA_NODES, and usually socket_id is of type 'int' or 'unsigned int' (Only the efd, node, and bonding specify 'unsigned char' for socket IDs.). And for that, I think it will be better to change the socket id type to 'int' in this patch. For the type of socket id in efd and node, I will send new patches to modify it.

Quoted 8 lines

drivers/net/bonding/rte_eth_bond_args.c bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,

Quoted 4 lines
drivers/net/bonding/rte_eth_bond_args.c +0 -0
 		return 0;
 	}
 

.

#4 lore raw

PATCH v2 1/1 net/bonding: fix socket id check

In reply to Min Hu (Connor)

From: Chengchang Tang <[email protected]>

The socket ID entered by user is cast to an unsigned integer. However, the value may be an illegal negative value, which may cause some problems. In this case, an error should be returned.

In addition, the socket ID may be an invalid positive number, which is also processed in this patch.

Fixes 2efb58cbab6e ("bond: new link bonding library")

---
v2:
* fixed socket id type.

---
 drivers/net/bonding/rte_eth_bond_args.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/bonding/rte_eth_bond_args.c +3 -3
index 8c5f90d..977f3fe 100644
@@ -207,13 +207,13 @@ bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,
 		return -1;
 
 	errno = 0;
-	socket_id = (uint8_t)strtol(value, &endptr, 10);
+	socket_id = (int)strtol(value, &endptr, 10);
 	if (*endptr != 0 || errno != 0)
 		return -1;
 
 	/* validate socket id value */
-	if (socket_id >= 0) {
-		*(uint8_t *)extra_args = (uint8_t)socket_id;
+	if (socket_id >= 0 && socket_id < RTE_MAX_NUMA_NODES) {
+		*(int *)extra_args = socket_id;
 		return 0;
 	}
 	return -1;
Signature · 1 line
2.7.4
#5 lore raw
Quoted 26 lines · Min Hu (Connor)

From: Chengchang Tang <[email protected]>

The socket ID entered by user is cast to an unsigned integer. However, the value may be an illegal negative value, which may cause some problems. In this case, an error should be returned.

In addition, the socket ID may be an invalid positive number, which is also processed in this patch.

Fixes 2efb58cbab6e ("bond: new link bonding library")

---
v2:
* fixed socket id type.

---
 drivers/net/bonding/rte_eth_bond_args.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/bonding/rte_eth_bond_args.c +0 -0
index 8c5f90d..977f3fe 100644

drivers/net/bonding/rte_eth_bond_args.c bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused, · line 210

Quoted 5 lines
drivers/net/bonding/rte_eth_bond_args.c +1 -1
@@ -207,13 +207,13 @@ bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,
 		return -1;
 
 	errno = 0;
-	socket_id = (uint8_t)strtol(value, &endptr, 10);
+	socket_id = (int)strtol(value, &endptr, 10);

Already provided some comment to v1, why it is better to do checks first and cast later.

drivers/net/bonding/rte_eth_bond_args.c bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused, · line 216

Quoted 8 lines
drivers/net/bonding/rte_eth_bond_args.c +2 -2
 	if (*endptr != 0 || errno != 0)
 		return -1;
 
 	/* validate socket id value */
-	if (socket_id >= 0) {
-		*(uint8_t *)extra_args = (uint8_t)socket_id;
+	if (socket_id >= 0 && socket_id < RTE_MAX_NUMA_NODES) {
+		*(int *)extra_args = socket_id;

The provided 'extra_args' is 'uint8_t', we can't just cast it to "int *" and assign to it.

#6 lore raw

PATCH v4 1/1 net/bonding: fix socket id check

In reply to Min Hu (Connor)

From: Chengchang Tang <[email protected]>

The socket ID entered by user is cast to an unsigned integer. However, the value may be an illegal negative value, which may cause some problems. In this case, an error should be returned.

In addition, the socket ID may be an invalid positive number, which is also processed in this patch.

Fixes 2efb58cbab6e ("bond: new link bonding library")

---
v4:
* changed type of socket id getting from 'strtol'.
* delete unused comments.

v3:
* changed type of socket id.

v2:
* fixed socket id type.

---
 drivers/net/bonding/rte_eth_bond_args.c | 8 ++++----
 drivers/net/bonding/rte_eth_bond_pmd.c  | 5 +++--
 2 files changed, 7 insertions(+), 6 deletions(-)
drivers/net/bonding/rte_eth_bond_args.c +4 -4
index 8c5f90d..dc68e52 100644
@@ -200,20 +200,20 @@ int
 bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,
 		const char *value, void *extra_args)
 {
-	int socket_id;
+	long socket_id;
 	char *endptr;
 
 	if (value == NULL || extra_args == NULL)
 		return -1;
 
 	errno = 0;
-	socket_id = (uint8_t)strtol(value, &endptr, 10);
+	socket_id = strtol(value, &endptr, 10);
 	if (*endptr != 0 || errno != 0)
 		return -1;
 
 	/* validate socket id value */
-	if (socket_id >= 0) {
-		*(uint8_t *)extra_args = (uint8_t)socket_id;
+	if (socket_id >= 0 && socket_id < RTE_MAX_NUMA_NODES) {
+		*(int *)extra_args = (int)socket_id;
 		return 0;
 	}
 	return -1;
drivers/net/bonding/rte_eth_bond_pmd.c +3 -2
index 2e9cea5..2f7d6ad 100644
@@ -3333,8 +3333,9 @@ bond_probe(struct rte_vdev_device *dev)
 	const char *name;
 	struct bond_dev_private *internals;
 	struct rte_kvargs *kvlist;
-	uint8_t bonding_mode, socket_id/*, agg_mode*/;
-	int  arg_count, port_id;
+	uint8_t bonding_mode;
+	int arg_count, port_id;
+	int socket_id;
 	uint8_t agg_mode;
 	struct rte_eth_dev *eth_dev;
 
Signature · 1 line
2.7.4
Reviewed-by on v4 1/1 Applied
#7 lore raw
Quoted 6 lines · Min Hu (Connor)

From: Chengchang Tang <[email protected]>

The socket ID entered by user is cast to an unsigned integer. However, the value may be an illegal negative value, which may cause some problems. In this case, an error should be returned.

Quoted 8 lines

In addition, the socket ID may be an invalid positive number, which is also processed in this patch.

Fixes 2efb58cbab6e ("bond: new link bonding library")

Applied to dpdk-next-net/main, thanks.