[PATCH] RDMA/mlx5: Fix memory leak in GET_DATA_DIRECT_SYSFS_PATH handler

Zilin Guan posted 1 patch 1 week, 6 days ago
drivers/infiniband/hw/mlx5/std_types.c | 1 +
1 file changed, 1 insertion(+)
[PATCH] RDMA/mlx5: Fix memory leak in GET_DATA_DIRECT_SYSFS_PATH handler
Posted by Zilin Guan 1 week, 6 days ago
The UVERBS_HANDLER(MLX5_IB_METHOD_GET_DATA_DIRECT_SYSFS_PATH) function
allocates memory for the device path using kobject_get_path(). If the
length of the device path exceeds the output buffer length, the function
returns -ENOSPC but does not free the allocated memory, resulting in a
memory leak.

Add a kfree() call to the error path to ensure the allocated memory is
properly freed.

Compile tested only. Issue found using a prototype static analysis tool
and code review.

Fixes: ec7ad6530909 ("RDMA/mlx5: Introduce GET_DATA_DIRECT_SYSFS_PATH ioctl")
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
 drivers/infiniband/hw/mlx5/std_types.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/infiniband/hw/mlx5/std_types.c b/drivers/infiniband/hw/mlx5/std_types.c
index 2fcf553044e1..ed4f5abb63c8 100644
--- a/drivers/infiniband/hw/mlx5/std_types.c
+++ b/drivers/infiniband/hw/mlx5/std_types.c
@@ -218,6 +218,7 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_GET_DATA_DIRECT_SYSFS_PATH)(
 	dev_path_len = strlen(dev_path) + 1;
 	if (dev_path_len > out_len) {
 		ret = -ENOSPC;
+		kfree(dev_path);
 		goto end;
 	}
 
-- 
2.34.1
Re: [PATCH] RDMA/mlx5: Fix memory leak in GET_DATA_DIRECT_SYSFS_PATH handler
Posted by Leon Romanovsky 1 week, 5 days ago
On Mon, 26 Jan 2026 07:48:01 +0000, Zilin Guan wrote:
> The UVERBS_HANDLER(MLX5_IB_METHOD_GET_DATA_DIRECT_SYSFS_PATH) function
> allocates memory for the device path using kobject_get_path(). If the
> length of the device path exceeds the output buffer length, the function
> returns -ENOSPC but does not free the allocated memory, resulting in a
> memory leak.
> 
> Add a kfree() call to the error path to ensure the allocated memory is
> properly freed.
> 
> [...]

Applied, thanks!

[1/1] RDMA/mlx5: Fix memory leak in GET_DATA_DIRECT_SYSFS_PATH handler
      (no commit info)

Best regards,
-- 
Leon Romanovsky <leon@kernel.org>
Re: [PATCH] RDMA/mlx5: Fix memory leak in GET_DATA_DIRECT_SYSFS_PATH handler
Posted by Markus Elfring 1 week, 6 days ago
…
> Add a kfree() call to the error path to ensure the allocated memory is
> properly freed.
…

How do you think about to use an additional label in the affected if branch instead?
https://elixir.bootlin.com/linux/v6.19-rc5/source/drivers/infiniband/hw/mlx5/std_types.c#L189-L231
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v6.19-rc7#n526

See also once more:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v6.19-rc7#n34

Regards,
Markus