Skip to contentSkip to conversation

net/mlx5e: Fix return of a kfree'd object instead of NULL

PATCH Applied Colin Ian King 3 participants 3 messages Feb 24, 2022 – Mar 1, 2022

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

PATCH 1/1 net/mlx5e: Fix return of a kfree'd object instead of NULL

Currently in the case where parse_attr fails to be allocated the memory pointed to by attr2 is kfree'd but the non-null pointer attr2 is returned and a potential use of a kfree'd object can occur. Fix this by returning NULL to indicate a memory allocation error.

Addresses issue found by clang-scan:
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c:3401:3: warning: Use of memory after it is freed [unix.Malloc]

Fixes 8300f225268b ("net/mlx5e: Create new flow attr for multi table actions")
---
 drivers/net/ethernet/mellanox/mlx5/core/en_tc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +1 -1
index 76a015dfc5fc..c0776a4a3845 100644
@@ -3398,7 +3398,7 @@ mlx5e_clone_flow_attr_for_post_act(struct mlx5_flow_attr *attr,
 	if (!attr2 || !parse_attr) {
 		kvfree(parse_attr);
 		kfree(attr2);
-		return attr2;
+		return NULL;
 	}
 
 	memcpy(attr2, attr, attr_sz);
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines · Colin Ian King

Currently in the case where parse_attr fails to be allocated the memory pointed to by attr2 is kfree'd but the non-null pointer attr2 is returned and a potential use of a kfree'd object can occur. Fix this by returning NULL to indicate a memory allocation error.

Addresses issue found by clang-scan:
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c:3401:3: warning: Use of memory after it is freed [unix.Malloc]

Fixes 8300f225268b ("net/mlx5e: Create new flow attr for multi table actions")
---
  drivers/net/ethernet/mellanox/mlx5/core/en_tc.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +0 -0
index 76a015dfc5fc..c0776a4a3845 100644

drivers/net/ethernet/mellanox/mlx5/core/en_tc.c mlx5e_clone_flow_attr_for_post_act(struct mlx5_flow_attr *attr, · line 3404

Quoted 8 lines
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +1 -1
@@ -3398,7 +3398,7 @@ mlx5e_clone_flow_attr_for_post_act(struct mlx5_flow_attr *attr,
  	if (!attr2 || !parse_attr) {
  		kvfree(parse_attr);
  		kfree(attr2);
-		return attr2;
+		return NULL;
  	}
 
  	memcpy(attr2, attr, attr_sz);

thanks!

#2 lore raw
In reply to Colin Ian King
Quoted 11 lines · Colin Ian King

Currently in the case where parse_attr fails to be allocated the memory pointed to by attr2 is kfree'd but the non-null pointer attr2 is returned and a potential use of a kfree'd object can occur. Fix this by returning NULL to indicate a memory allocation error.

Addresses issue found by clang-scan:
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c:3401:3: warning: Use of memory after it is freed [unix.Malloc]

Fixes 8300f225268b ("net/mlx5e: Create new flow attr for multi table actions")

Applied to net-next-mlx5
Thanks.