Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH] RDMA/rtrs-srv: Fix potential session use-after-free on sysfs failure
@ 2026-12-03 11:55 Guangshuo Li
  2026-12-03 12:14 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Guangshuo Li @ 2026-12-03 11:55 UTC (permalink / raw)
  To: haris.iqbal, jinpu.wang, jgg, leon; +Cc: danil.kipnis, linux-rdma

Sysfs setup can drop the session device reference on failure before
process_info_req() has acquired the path reference. This can free the
session while cleanup still holds its mutex and needs the session.

Acquire each path reference when its root-folder setup succeeds, and
roll back dev_ref on failure without dropping the session-owned initial
reference. Release path references after unlocking the session mutex.

Fixes: 91b11610af8d ("RDMA/rtrs: server: sysfs interface functions")
Signed-off-by: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>
---
 drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c | 17 ++++++++++-------
 drivers/infiniband/ulp/rtrs/rtrs-srv.c       |  1 -
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
index 9dd9141c86a5..938082369b15 100644
--- a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
+++ b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
@@ -162,12 +162,12 @@ static int rtrs_srv_create_once_sysfs_root_folders(struct rtrs_srv_path *srv_pat
 		/*
 		 * Device needs to be registered only on the first session
 		 */
-		goto unlock;
+		goto get_ref;
 	}
 	srv->dev.class = &rtrs_dev_class;
 	err = dev_set_name(&srv->dev, "%s", srv_path->s.sessname);
 	if (err)
-		goto unlock;
+		goto dec_ref;
 
 	/*
 	 * Suppress user space notification until
@@ -177,19 +177,22 @@ static int rtrs_srv_create_once_sysfs_root_folders(struct rtrs_srv_path *srv_pat
 	err = device_add(&srv->dev);
 	if (err) {
 		pr_err("device_add(): %pe\n", ERR_PTR(err));
-		put_device(&srv->dev);
-		goto unlock;
+		goto dec_ref;
 	}
 	srv->kobj_paths = kobject_create_and_add("paths", &srv->dev.kobj);
 	if (!srv->kobj_paths) {
 		err = -ENOMEM;
 		pr_err("kobject_create_and_add(): %pe\n", ERR_PTR(err));
 		device_del(&srv->dev);
-		put_device(&srv->dev);
-		goto unlock;
+		goto dec_ref;
 	}
 	dev_set_uevent_suppress(&srv->dev, false);
 	kobject_uevent(&srv->dev.kobj, KOBJ_ADD);
+get_ref:
+	get_device(&srv->dev);
+	goto unlock;
+dec_ref:
+	srv->dev_ref--;
 unlock:
 	mutex_unlock(&srv->paths_mutex);
 
@@ -208,8 +211,8 @@ rtrs_srv_destroy_once_sysfs_root_folders(struct rtrs_srv_path *srv_path)
 		device_del(&srv->dev);
 		put_device(&srv->dev);
 	} else {
-		put_device(&srv->dev);
 		mutex_unlock(&srv->paths_mutex);
+		put_device(&srv->dev);
 	}
 }
 
diff --git a/drivers/infiniband/ulp/rtrs/rtrs-srv.c b/drivers/infiniband/ulp/rtrs/rtrs-srv.c
index 5314599d29fe..d9e09a1c4ed7 100644
--- a/drivers/infiniband/ulp/rtrs/rtrs-srv.c
+++ b/drivers/infiniband/ulp/rtrs/rtrs-srv.c
@@ -879,7 +879,6 @@ static int process_info_req(struct rtrs_srv_con *con,
 	if (err)
 		goto iu_free;
 	kobject_get(&srv_path->kobj);
-	get_device(&srv_path->srv->dev);
 	err = rtrs_srv_change_state(srv_path, RTRS_SRV_CONNECTED);
 	if (!err) {
 		rtrs_err(s, "rtrs_srv_change_state() failed\n");

base-commit: d06ce6f1eb5eeffd4ebcfde345dc34a98131d106
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] RDMA/rtrs-srv: Fix potential session use-after-free on sysfs failure
  2026-12-03 11:55 [PATCH] RDMA/rtrs-srv: Fix potential session use-after-free on sysfs failure Guangshuo Li
@ 2026-12-03 12:14 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03 12:14 UTC (permalink / raw)
  To: Guangshuo Li; +Cc: linux-rdma

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] RDMA/rtrs-srv: dec_ref rollback allows re-adding deleted
  srv->dev
--

commit be8058b540e2d69d724eccc0f72faf8f78caad2c
Author: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>

RDMA/rtrs-srv: Fix potential session use-after-free on sysfs failure

This commit moves the per-path get_device(&srv->dev) into
rtrs_srv_create_once_sysfs_root_folders(), removes put_device() from its
error paths while decrementing srv->dev_ref on failure, and unlocks
srv->paths_mutex before put_device() on teardown.

> diff --git a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
> index 9dd9141c86a54..938082369b158 100644
> --- a/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c
> +++ b/drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c

[ ... ]

> @@ -177,19 +177,22 @@ static int rtrs_srv_create_once_sysfs_root_folders(struct rtrs_srv_path *srv_pat
>  	err = device_add(&srv->dev);
>  	if (err) {
>  		pr_err("device_add(): %pe\n", ERR_PTR(err));
> -		put_device(&srv->dev);
> -		goto unlock;
> +		goto dec_ref;
>  	}
>  	srv->kobj_paths = kobject_create_and_add("paths", &srv->dev.kobj);
>  	if (!srv->kobj_paths) {
>  		err = -ENOMEM;
>  		pr_err("kobject_create_and_add(): %pe\n", ERR_PTR(err));
>  		device_del(&srv->dev);
> -		put_device(&srv->dev);
> -		goto unlock;
> +		goto dec_ref;
>  	}
>  	dev_set_uevent_suppress(&srv->dev, false);
>  	kobject_uevent(&srv->dev.kobj, KOBJ_ADD);
> +get_ref:
> +	get_device(&srv->dev);
> +	goto unlock;
> +dec_ref:
> +	srv->dev_ref--;
>  unlock:
>  	mutex_unlock(&srv->paths_mutex);

[Severity: Medium]
Can rolling back srv->dev_ref here cause device_add() to be called more
than once on the same srv->dev?

When kobject_create_and_add() fails, device_del(&srv->dev) runs and
dec_ref resets srv->dev_ref to 0.  Because srv is shared across all
paths of a multi-path session, another path entering this function will
see srv->dev_ref == 0 and call device_add(&srv->dev) a second time.

The driver core forbids calling device_add() more than once on a struct
device: device_del() leaves dev->p allocated with dev->p->dead set and
unlinks dev->p->knode_class asynchronously, so re-adding srv->dev leaves
it in a broken state and can corrupt the class klist on klist_add_tail().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203115553.2318954-1-liguangshuo@mail.sdu.edu.cn?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03 12:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 11:55 [PATCH] RDMA/rtrs-srv: Fix potential session use-after-free on sysfs failure Guangshuo Li
2026-12-03 12:14 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox