[PATCH v2] IB/mlx4: Fix refcount leak in add_port() error path

Guangshuo Li posted 1 patch 2 months, 1 week ago
There is a newer version of this series
drivers/infiniband/hw/mlx4/sysfs.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
[PATCH v2] IB/mlx4: Fix refcount leak in add_port() error path
Posted by Guangshuo Li 2 months, 1 week ago
After kobject_init_and_add(), the lifetime of the embedded struct
kobject is expected to be managed through the kobject core reference
counting.

In add_port(), if kobject_init_and_add() fails, the error path frees p
directly instead of releasing the kobject reference with kobject_put().
This may leave the reference count of the embedded struct kobject
unbalanced, resulting in a refcount leak and potentially leading to a
use-after-free.

The issue was identified by a static analysis tool I developed and
confirmed by manual review.

Fix this by using kobject_put(&p->kobj) in the kobject_init_and_add()
failure path.

Fixes: c1e7e466120b ("IB/mlx4: Add iov directory in sysfs under the ib device")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
v2:
  - note that the issue was identified by my static analysis tool
  - and confirmed by manual review

 drivers/infiniband/hw/mlx4/sysfs.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/infiniband/hw/mlx4/sysfs.c b/drivers/infiniband/hw/mlx4/sysfs.c
index 88f534cf690e..15b36b9e4bd6 100644
--- a/drivers/infiniband/hw/mlx4/sysfs.c
+++ b/drivers/infiniband/hw/mlx4/sysfs.c
@@ -642,7 +642,7 @@ static int add_port(struct mlx4_ib_dev *dev, int port_num, int slave)
 				   kobject_get(dev->dev_ports_parent[slave]),
 				   "%d", port_num);
 	if (ret)
-		goto err_alloc;
+		goto err_kobj;
 
 	p->pkey_group.name  = "pkey_idx";
 	p->pkey_group.attrs =
@@ -689,6 +689,11 @@ static int add_port(struct mlx4_ib_dev *dev, int port_num, int slave)
 	kobject_put(dev->dev_ports_parent[slave]);
 	kfree(p);
 	return ret;
+
+err_kobj:
+	kobject_put(&p->kobj);
+	return ret;
+
 }
 
 static int register_one_pkey_tree(struct mlx4_ib_dev *dev, int slave)
-- 
2.43.0
Re: [PATCH v2] IB/mlx4: Fix refcount leak in add_port() error path
Posted by Jason Gunthorpe 1 month, 3 weeks ago
On Mon, Apr 13, 2026 at 07:59:48PM +0800, Guangshuo Li wrote:
> @@ -642,7 +642,7 @@ static int add_port(struct mlx4_ib_dev *dev, int port_num, int slave)
>  				   kobject_get(dev->dev_ports_parent[slave]),
>  				   "%d", port_num);
>  	if (ret)
> -		goto err_alloc;
> +		goto err_kobj;
>  
>  	p->pkey_group.name  = "pkey_idx";
>  	p->pkey_group.attrs =
> @@ -689,6 +689,11 @@ static int add_port(struct mlx4_ib_dev *dev, int port_num, int slave)
>  	kobject_put(dev->dev_ports_parent[slave]);
>  	kfree(p);
>  	return ret;
> +
> +err_kobj:
> +	kobject_put(&p->kobj);

Sashiko says this will crash because this was skipped:

	p->pkey_group.attrs =
		alloc_group_attrs(show_port_pkey,
				  is_eth ? NULL : store_port_pkey,
				  dev->dev->caps.pkey_table_len[port_num]);

Along with other problems.

Jason
Re: [PATCH v2] IB/mlx4: Fix refcount leak in add_port() error path
Posted by Guangshuo Li 1 month, 3 weeks ago
Hi Jason,

Thanks for reviewing.

On Tue, 28 Apr 2026 at 22:35, Jason Gunthorpe <jgg@nvidia.com> wrote:
>
>
> Sashiko says this will crash because this was skipped:
>
>         p->pkey_group.attrs =
>                 alloc_group_attrs(show_port_pkey,
>                                   is_eth ? NULL : store_port_pkey,
>                                   dev->dev->caps.pkey_table_len[port_num]);
>
> Along with other problems.
>
> Jason

You are right! I missed that mlx4_port_release()
currently assumes pkey_group.attrs and gid_group.attrs are already
allocated. On the kobject_init_and_add() failure path they are still
NULL, so kobject_put(&p->kobj) can crash in the release callback.

I will respin v3 by making mlx4_port_release() tolerate NULL attribute
arrays and by dropping the parent reference taken before
kobject_init_and_add() before putting the embedded kobject.