net/mlx5: Fix NULL dereference and memory leak in ttc_table creation
Subsystems (4)
v6 cover net/mlx5: Fix NULL dereference and memory leak in ttc_table creation
This patch series addresses two issues in the mlx5_create_inner_ttc_table() and mlx5_create_ttc_table() functions:
1. A potential NULL pointer dereference if mlx5_get_flow_namespace() returns NULL.
2. A memory leak in the error path when ttc_type is invalid (default:
switch case).
Henry Martin (2):
net/mlx5: Fix null-ptr-deref in mlx5_create_{inner_,}ttc_table() net/mlx5: Move ttc allocation after switch case to prevent leaks
.../ethernet/mellanox/mlx5/core/lib/fs_ttc.c | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-)
Signature · 1 line
2.34.1
PATCH v6 1/2 net/mlx5: Fix null-ptr-deref in mlx5_create_{inner_,}ttc_table()
Add NULL check for mlx5_get_flow_namespace() returns in mlx5_create_inner_ttc_table() and mlx5_create_ttc_table() to prevent NULL pointer dereference.
--- drivers/net/ethernet/mellanox/mlx5/core/lib/fs_ttc.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/net/ethernet/mellanox/mlx5/core/lib/fs_ttc.c +10 -0
@@ -655,6 +655,11 @@ struct mlx5_ttc_table *mlx5_create_inner_ttc_table(struct mlx5_core_dev *dev, } ns =mlx5_get_flow_namespace (dev, params->ns_type); +if (!ns) { +kvfree (ttc); +return ERR_PTR (-EOPNOTSUPP ); + } + groups = use_l4_type ? &inner_ttc_groups[TTC_GROUPS_USE_L4_TYPE ] : &inner_ttc_groups[TTC_GROUPS_DEFAULT ];
@@ -728,6 +733,11 @@ struct mlx5_ttc_table *mlx5_create_ttc_table(struct mlx5_core_dev *dev, } ns =mlx5_get_flow_namespace (dev, params->ns_type); +if (!ns) { +kvfree (ttc); +return ERR_PTR (-EOPNOTSUPP ); + } + groups = use_l4_type ? &ttc_groups[TTC_GROUPS_USE_L4_TYPE ] : &ttc_groups[TTC_GROUPS_DEFAULT ];
Signature · 1 line
2.34.1
Re: [PATCH v6 1/2] net/mlx5: Fix null-ptr-deref in mlx5_create_{inner_,}ttc_table()
Quoted 3 lines
Add NULL check for
mlx5_get_flow_namespace()returns inmlx5_create_inner_ttc_table()andmlx5_create_ttc_table()to prevent NULL pointer dereference.
* Can an other summary phrase variant become more desirable accordingly?
* Please avoid duplicate source code.
Regards,
Markus
PATCH v6 2/2 net/mlx5: Move ttc allocation after switch case to prevent leaks
In reply to Henry MartinRelocate the memory allocation for ttc table after the switch statement that validates params->ns_type in both mlx5_create_inner_ttc_table() and mlx5_create_ttc_table(). This ensures memory is only allocated after confirming valid input, eliminating potential memory leaks when invalid ns_type cases occur.
--- .../net/ethernet/mellanox/mlx5/core/lib/fs_ttc.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/lib/fs_ttc.c +8 -8
@@ -637,10 +637,6 @@ struct mlx5_ttc_table *mlx5_create_inner_ttc_table(struct mlx5_core_dev *dev,bool use_l4_type;int err; - ttc =kvzalloc (sizeof (*ttc),GFP_KERNEL ); -if (!ttc) -return ERR_PTR (-ENOMEM ); -switch (params->ns_type) {case MLX5_FLOW_NAMESPACE_PORT_SEL : use_l4_type = MLX5_CAP_GEN_2(dev, pcc_ifa2) &&
@@ -654,6 +650,10 @@ struct mlx5_ttc_table *mlx5_create_inner_ttc_table(struct mlx5_core_dev *dev,return ERR_PTR (-EINVAL ); } + ttc =kvzalloc (sizeof (*ttc),GFP_KERNEL ); +if (!ttc) +return ERR_PTR (-ENOMEM ); + ns =mlx5_get_flow_namespace (dev, params->ns_type);if (!ns) {kvfree (ttc);
@@ -715,10 +715,6 @@ struct mlx5_ttc_table *mlx5_create_ttc_table(struct mlx5_core_dev *dev,bool use_l4_type;int err; - ttc =kvzalloc (sizeof (*ttc),GFP_KERNEL ); -if (!ttc) -return ERR_PTR (-ENOMEM ); -switch (params->ns_type) {case MLX5_FLOW_NAMESPACE_PORT_SEL : use_l4_type = MLX5_CAP_GEN_2(dev, pcc_ifa2) &&
@@ -732,6 +728,10 @@ struct mlx5_ttc_table *mlx5_create_ttc_table(struct mlx5_core_dev *dev,return ERR_PTR (-EINVAL ); } + ttc =kvzalloc (sizeof (*ttc),GFP_KERNEL ); +if (!ttc) +return ERR_PTR (-ENOMEM ); + ns =mlx5_get_flow_namespace (dev, params->ns_type);if (!ns) {kvfree (ttc);
Signature · 1 line
2.34.1
Re: [PATCH v6 2/2] net/mlx5: Move ttc allocation after switch case to prevent leaks
Quoted 49 lines · Henry Martin
Relocate the memory allocation for ttc table after the switch statement that validates
params->ns_typein bothmlx5_create_inner_ttc_table()andmlx5_create_ttc_table(). This ensures memory is only allocated after confirming valid input, eliminating potential memory leaks when invalidns_typecases occur.Signed-off-by Henry Martin <[email protected]>--- .../net/ethernet/mellanox/mlx5/core/lib/fs_ttc.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)drivers/net/ethernet/mellanox/mlx5/core/lib/fs_ttc.c +4
-8@@ -637,10 +637,6 @@ struct mlx5_ttc_table *mlx5_create_inner_ttc_table(struct mlx5_core_dev *dev,bool use_l4_type;int err; - ttc =kvzalloc (sizeof (*ttc),GFP_KERNEL ); -if (!ttc) -return ERR_PTR (-ENOMEM ); -switch (params->ns_type) {case MLX5_FLOW_NAMESPACE_PORT_SEL : use_l4_type = MLX5_CAP_GEN_2(dev, pcc_ifa2) &&@@ -654,6 +650,10 @@ struct mlx5_ttc_table *mlx5_create_inner_ttc_table(struct mlx5_core_dev *dev,return ERR_PTR (-EINVAL ); } + ttc =kvzalloc (sizeof (*ttc),GFP_KERNEL ); +if (!ttc) +return ERR_PTR (-ENOMEM ); + ns =mlx5_get_flow_namespace (dev, params->ns_type);if (!ns) {kvfree (ttc);@@ -715,10 +715,6 @@ struct mlx5_ttc_table *mlx5_create_ttc_table(struct mlx5_core_dev *dev,bool use_l4_type;int err; - ttc =kvzalloc (sizeof (*ttc),GFP_KERNEL ); -if (!ttc) -return ERR_PTR (-ENOMEM ); -switch (params->ns_type) {case MLX5_FLOW_NAMESPACE_PORT_SEL : use_l4_type = MLX5_CAP_GEN_2(dev, pcc_ifa2) &&
drivers/net/ethernet/mellanox/mlx5/core/lib/fs_ttc.c struct mlx5_ttc_table *mlx5_create_ttc_table(struct mlx5_core_dev *dev, · line 737
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/lib/fs_ttc.c +4
-0@@ -732,6 +728,10 @@ struct mlx5_ttc_table *mlx5_create_ttc_table(struct mlx5_core_dev *dev,return ERR_PTR (-EINVAL ); } + ttc =kvzalloc (sizeof (*ttc),GFP_KERNEL ); +if (!ttc) +return ERR_PTR (-ENOMEM ); + ns =mlx5_get_flow_namespace (dev, params->ns_type);if (!ns) {kvfree (ttc);
Thanks for fixing
Quoted 3 lines
Signature · 1 line
2.34.1
Re: [PATCH v6 2/2] net/mlx5: Move ttc allocation after switch case to prevent leaks
In reply to Henry MartinQuoted 49 lines · Henry Martin
Relocate the memory allocation for ttc table after the switch statement that validates
params->ns_typein bothmlx5_create_inner_ttc_table()andmlx5_create_ttc_table(). This ensures memory is only allocated after confirming valid input, eliminating potential memory leaks when invalidns_typecases occur.Signed-off-by Henry Martin <[email protected]>--- .../net/ethernet/mellanox/mlx5/core/lib/fs_ttc.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)drivers/net/ethernet/mellanox/mlx5/core/lib/fs_ttc.c +4
-8@@ -637,10 +637,6 @@ struct mlx5_ttc_table *mlx5_create_inner_ttc_table(struct mlx5_core_dev *dev,bool use_l4_type;int err; - ttc =kvzalloc (sizeof (*ttc),GFP_KERNEL ); -if (!ttc) -return ERR_PTR (-ENOMEM ); -switch (params->ns_type) {case MLX5_FLOW_NAMESPACE_PORT_SEL : use_l4_type = MLX5_CAP_GEN_2(dev, pcc_ifa2) &&@@ -654,6 +650,10 @@ struct mlx5_ttc_table *mlx5_create_inner_ttc_table(struct mlx5_core_dev *dev,return ERR_PTR (-EINVAL ); } + ttc =kvzalloc (sizeof (*ttc),GFP_KERNEL ); +if (!ttc) +return ERR_PTR (-ENOMEM ); + ns =mlx5_get_flow_namespace (dev, params->ns_type);if (!ns) {kvfree (ttc);@@ -715,10 +715,6 @@ struct mlx5_ttc_table *mlx5_create_ttc_table(struct mlx5_core_dev *dev,bool use_l4_type;int err; - ttc =kvzalloc (sizeof (*ttc),GFP_KERNEL ); -if (!ttc) -return ERR_PTR (-ENOMEM ); -switch (params->ns_type) {case MLX5_FLOW_NAMESPACE_PORT_SEL : use_l4_type = MLX5_CAP_GEN_2(dev, pcc_ifa2) &&
drivers/net/ethernet/mellanox/mlx5/core/lib/fs_ttc.c struct mlx5_ttc_table *mlx5_create_ttc_table(struct mlx5_core_dev *dev, · line 737
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/lib/fs_ttc.c +4
-0@@ -732,6 +728,10 @@ struct mlx5_ttc_table *mlx5_create_ttc_table(struct mlx5_core_dev *dev,return ERR_PTR (-EINVAL ); } + ttc =kvzalloc (sizeof (*ttc),GFP_KERNEL ); +if (!ttc) +return ERR_PTR (-ENOMEM ); + ns =mlx5_get_flow_namespace (dev, params->ns_type);if (!ns) {kvfree (ttc);
Mark
Jakub Kicinski
maintainerRe: [PATCH v6 2/2] net/mlx5: Move ttc allocation after switch case to prevent leaks
Quoted 8 lines · On Wed, 16 Apr 2025 15:02:13 +0300 Mark Bloch wrote:
Quoted 5 lines · Henry Martin
Relocate the memory allocation for ttc table after the switch statement that validates
params->ns_typein bothmlx5_create_inner_ttc_table()andmlx5_create_ttc_table(). This ensures memory is only allocated after confirming valid input, eliminating potential memory leaks when invalidns_typecases occur.Reviewed-by Mark Bloch <[email protected]>
A bit hard to see from the context but I'm guessing this fixes a memory leak? We need a Fixes tag..
reminder: please trim your replies
Signature · 1 line
pw-bot: cr
Re: [PATCH v6 2/2] net/mlx5: Move ttc allocation after switch case to prevent leaks
Quoted 2 lines
A bit hard to see from the context but I'm guessing this fixes a memory leak? We need a Fixes tag..
Thanks for the reminder. I've added the Fixes tag in v7.
Regards,
Henry
Quoted 1 line
This patch series addresses two issues in …
It would have been helpful to extend patch version descriptions accordingly.
https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.15-rc2#n310
Regards,
Markus