Skip to contentSkip to conversation

net/mlx5e: Fix some static analysis warnings

PATCH In review Alex Dewar 2 participants 8 messages Sep 27, 2020 – Sep 29, 2020

Subsystems (5)
lore
Share & tools
lore

Alex Dewar

author
#0 lore raw

cover net/mlx5e: Fix some static analysis warnings

Hi,

Coverity has flagged up some warnings for this driver, which I address in this patch series. All the fixes are fairly trivial and have been build-tested.

Best,
Alex

author
#1 lore raw

PATCH 1/3 net/mlx5e: Fix possible null pointer dereference

In mlx5e_tc_unoffload_from_slow_path() a null check is performed for the variable slow_attr and a warning is issued if it is null. However, slow_attr is used later on in the function regardless. Fix this by returning if slow_attr is null.

Addresses-Coverity: CID 1497163: Null pointer dereferences (FORWARD_NULL)

---
 drivers/net/ethernet/mellanox/mlx5/core/en_tc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +3 -1
index f815b0c60a6c..b3c57b984a2a 100644
@@ -1238,8 +1238,10 @@ mlx5e_tc_unoffload_from_slow_path(struct mlx5_eswitch *esw,
 	struct mlx5_flow_attr *slow_attr;
 
 	slow_attr = mlx5_alloc_flow_attr(MLX5_FLOW_NAMESPACE_FDB);
-	if (!slow_attr)
+	if (!slow_attr) {
 		mlx5_core_warn(flow->priv->mdev, "Unable to unoffload slow path rule\n");
+		return;
+	}
 
 	memcpy(slow_attr, flow->attr, ESW_FLOW_ATTR_SZ);
 	slow_attr->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
Signature · 1 line
2.28.0
author
#2 lore raw

PATCH 2/3 net/mlx5e: Clean up error handling in mlx5e_alloc_flow()

In reply to Alex Dewar

The variable flow is used after being allocated but before being null-checked, which will cause a null pointer dereference if the allocation failed. Fix this and tidy up the error-checking logic in this function.

Addresses-Coverity: CID 1497154: Null pointer dereferences (REVERSE_INULL)

---
 .../net/ethernet/mellanox/mlx5/core/en_tc.c   | 20 ++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +11 -9
index b3c57b984a2a..ed308407be6f 100644
@@ -4536,20 +4536,22 @@ mlx5e_alloc_flow(struct mlx5e_priv *priv, int attr_size,
 	struct mlx5e_tc_flow_parse_attr *parse_attr;
 	struct mlx5_flow_attr *attr;
 	struct mlx5e_tc_flow *flow;
-	int out_index, err;
+	int out_index;
 
 	flow = kzalloc(sizeof(*flow), GFP_KERNEL);
+	if (!flow)
+		return -ENOMEM;
 	parse_attr = kvzalloc(sizeof(*parse_attr), GFP_KERNEL);
+	if (!parse_attr)
+		goto err_free_flow;
 
 	flow->flags = flow_flags;
 	flow->cookie = f->cookie;
 	flow->priv = priv;
 
 	attr = mlx5_alloc_flow_attr(get_flow_name_space(flow));
-	if (!parse_attr || !flow || !attr) {
-		err = -ENOMEM;
-		goto err_free;
-	}
+	if (!attr)
+		goto err_free_parse_attr;
 	flow->attr = attr;
 
 	for (out_index = 0; out_index < MLX5_MAX_FLOW_FWD_VPORTS; out_index++)
@@ -4564,11 +4566,11 @@ mlx5e_alloc_flow(struct mlx5e_priv *priv, int attr_size,
 
 	return 0;
 
-err_free:
-	kfree(flow);
+err_free_parse_attr:
 	kvfree(parse_attr);
-	kfree(attr);
-	return err;
+err_free_flow:
+	kfree(flow);
+	return -ENOMEM;
 }
 
 static void
Signature · 1 line
2.28.0
#3 lore raw

Re: [PATCH 2/3] net/mlx5e: Clean up error handling in mlx5e_alloc_flow()

Quoted 11 lines · Alex Dewar

The variable flow is used after being allocated but before being null-checked, which will cause a null pointer dereference if the allocation failed. Fix this and tidy up the error-checking logic in this function.

Addresses-Coverity: CID 1497154: Null pointer dereferences (REVERSE_INULL)

---
 .../net/ethernet/mellanox/mlx5/core/en_tc.c   | 20 ++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

Thanks, but Gustavo already sent a fix.
https://lore.kernel.org/lkml/20200925164913.GA18472@embeddedor

author
#4 lore raw

PATCH 3/3 net/mlx5e: Fix use of freed pointer

In reply to Alex Dewar

If the call to mlx5_fc_create() fails, then shared_counter will be freed before its member, shared_counter->counter, is accessed to retrieve the error code. Fix by using an intermediate variable.

Addresses-Coverity: CID 1497153: Memory - illegal accesses (USE_AFTER_FREE)

---
 drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c +5 -3
index b5f8ed30047b..5851a1dfe6e4 100644
@@ -738,6 +738,7 @@ mlx5_tc_ct_shared_counter_get(struct mlx5_tc_ct_priv *ct_priv,
 	struct mlx5_ct_shared_counter *shared_counter;
 	struct mlx5_core_dev *dev = ct_priv->dev;
 	struct mlx5_ct_entry *rev_entry;
+	struct mlx5_fc *counter;
 	__be16 tmp_port;
 
 	/* get the reversed tuple */
@@ -775,12 +776,13 @@ mlx5_tc_ct_shared_counter_get(struct mlx5_tc_ct_priv *ct_priv,
 	if (!shared_counter)
 		return ERR_PTR(-ENOMEM);
 
-	shared_counter->counter = mlx5_fc_create(dev, true);
-	if (IS_ERR(shared_counter->counter)) {
+	counter = mlx5_fc_create(dev, true);
+	if (IS_ERR(counter)) {
 		ct_dbg("Failed to create counter for ct entry");
 		kfree(shared_counter);
-		return ERR_PTR(PTR_ERR(shared_counter->counter));
+		return (struct mlx5_ct_shared_counter *)counter;
 	}
+	shared_counter->counter = counter;
 
 	refcount_set(&shared_counter->refcount, 1);
 	return shared_counter;
Signature · 1 line
2.28.0
#5 lore raw

Re: [PATCH 3/3] net/mlx5e: Fix use of freed pointer

Quoted 7 lines · Alex Dewar

If the call to mlx5_fc_create() fails, then shared_counter will be freed before its member, shared_counter->counter, is accessed to retrieve the error code. Fix by using an intermediate variable.

Addresses-Coverity: CID 1497153: Memory - illegal accesses (USE_AFTER_FREE)

---

Please add Fixes line.

Quoted 17 lines
 drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c +1 -0
index b5f8ed30047b..5851a1dfe6e4 100644
@@ -738,6 +738,7 @@ mlx5_tc_ct_shared_counter_get(struct mlx5_tc_ct_priv *ct_priv,
 	struct mlx5_ct_shared_counter *shared_counter;
 	struct mlx5_core_dev *dev = ct_priv->dev;
 	struct mlx5_ct_entry *rev_entry;
+	struct mlx5_fc *counter;
 	__be16 tmp_port;
 
 	/* get the reversed tuple */
@@ -775,12 +776,13 @@ mlx5_tc_ct_shared_counter_get(struct mlx5_tc_ct_priv *ct_priv,
 	if (!shared_counter)

drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c mlx5_tc_ct_shared_counter_get(struct mlx5_tc_ct_priv *ct_priv, · line 783

Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c +3 -3
 		return ERR_PTR(-ENOMEM);
 
-	shared_counter->counter = mlx5_fc_create(dev, true);
-	if (IS_ERR(shared_counter->counter)) {
+	counter = mlx5_fc_create(dev, true);
+	if (IS_ERR(counter)) {
 		ct_dbg("Failed to create counter for ct entry");
 		kfree(shared_counter);
-		return ERR_PTR(PTR_ERR(shared_counter->counter));
+		return (struct mlx5_ct_shared_counter *)counter;

return ERR_CAST(counter);

Quoted 8 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c +1 -1
 	}
+	shared_counter->counter = counter;
 
 	refcount_set(&shared_counter->refcount, 1);
 	return shared_counter;
--

2.28.0

author
#6 lore raw

PATCH v2 1/1 net/mlx5e: Fix use of freed pointer

If the call to mlx5_fc_create() fails, then shared_counter will be freed before its member, shared_counter->counter, is accessed to retrieve the error code. Fix by using an intermediate variable.

Addresses-Coverity: CID 1497153: Memory - illegal accesses (USE_AFTER_FREE)

Fixes 1edae2335adf ("net/mlx5e: CT: Use the same counter for both directions")

---
v2:
- Add Fixes tag (Leon)
- Use ERR_CAST (Leon)

Hi Leon,

I've made the suggested changes. Let me know if there's anything else you need :)

There is also this patch in the series which doesn't seem to have been reviewed yet: https://lore.kernel.org/lkml/[email protected]/

Best,
Alex

 drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c +5 -3
index b5f8ed30047b..1e80e7669995 100644
@@ -738,6 +738,7 @@ mlx5_tc_ct_shared_counter_get(struct mlx5_tc_ct_priv *ct_priv,
 	struct mlx5_ct_shared_counter *shared_counter;
 	struct mlx5_core_dev *dev = ct_priv->dev;
 	struct mlx5_ct_entry *rev_entry;
+	struct mlx5_fc *counter;
 	__be16 tmp_port;
 
 	/* get the reversed tuple */
@@ -775,12 +776,13 @@ mlx5_tc_ct_shared_counter_get(struct mlx5_tc_ct_priv *ct_priv,
 	if (!shared_counter)
 		return ERR_PTR(-ENOMEM);
 
-	shared_counter->counter = mlx5_fc_create(dev, true);
-	if (IS_ERR(shared_counter->counter)) {
+	counter = mlx5_fc_create(dev, true);
+	if (IS_ERR(counter)) {
 		ct_dbg("Failed to create counter for ct entry");
 		kfree(shared_counter);
-		return ERR_PTR(PTR_ERR(shared_counter->counter));
+		return ERR_CAST(counter);
 	}
+	shared_counter->counter = counter;
 
 	refcount_set(&shared_counter->refcount, 1);
 	return shared_counter;
Signature · 1 line
2.28.0
#7 lore raw

Re: [PATCH v2] net/mlx5e: Fix use of freed pointer

Quoted 12 lines · Alex Dewar

If the call to mlx5_fc_create() fails, then shared_counter will be freed before its member, shared_counter->counter, is accessed to retrieve the error code. Fix by using an intermediate variable.

Addresses-Coverity: CID 1497153: Memory - illegal accesses (USE_AFTER_FREE)

Fixes 1edae2335adf ("net/mlx5e: CT: Use the same counter for both directions")

---
v2:
- Add Fixes tag (Leon)
- Use ERR_CAST (Leon)

Quoted 4 lines

Hi Leon,

I've made the suggested changes. Let me know if there's anything else you need :)

Hi Alex,

Saeed already picked Dan's patch.
https://lore.kernel.org/linux-rdma/[email protected]/T/#t

Quoted 3 lines

There is also this patch in the series which doesn't seem to have been reviewed yet: https://lore.kernel.org/lkml/[email protected]/

Ariel is handling this internally.
https://lore.kernel.org/linux-rdma/[email protected]/T/#t

Thanks