Skip to contentSkip to conversation

RDMA/rtrs-srv: fix use-after-free on sysfs setup failure

PATCH In review Yuqiang Sun 3 participants 10 messages Oct 29 – Nov 4

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

cover RDMA/rtrs-srv: fix use-after-free on sysfs setup failure

These patches fix use-after-free bugs on the error paths of the rtrs server's sysfs setup, reached when a client's info request arrives and creating the sysfs entries for its path fails.

Patch 1 is about what remains after commit 5b7437339011 ("RDMA/rtrs:
Fix use-after-free in path file creation cleanup", CVE-2026-64033). That commit reordered the cleanup inside rtrs_srv_create_path_files(), but the final kobject_put() there still frees srv_path while the caller keeps using it and frees it again later; the stats kobject has the same problem.

Patch 2 balances the session device reference: on these failure paths it is dropped without having been taken, which frees the whole server session while it is still in use. Besides allocation failures, device_add() also fails when a client sends an empty pathname, which patch 3 rejects.

I first sent this to [email protected], since the empty pathname makes patch 2's bug reachable from the network. The conclusion there was that rtrs is deployed on trusted storage networks only, so this is an ordinary bug and the series should simply be posted here. Thanks to everyone who replied so quickly.

The bugs were found on v7.2.12 and are present in the rdma for-next branch, which this series is based on. They were reproduced with KASAN under KVM (soft-RoCE, rnbd mapping a brd device) by failing one allocation at a time with failslab, restricted by call stack to the sysfs setup, and the fixes were tested the same way, with lockdep and kmemleak, and also with CONFIG_DEBUG_KOBJECT_RELEASE. The empty pathname was tested with a small user-space rtrs client. The reproduction setup is available on request. Failures of the two sysfs_create_group() calls were not injected, and nothing was tested on RDMA hardware.

Two things seen while testing are not addressed here:

- The same pattern (a sysfs helper putting a kobject whose release frees an object its caller still owns) is present in the rtrs client (rtrs_clt_create_path_files() and both its callers) and in the rnbd server; separate reports and patches will follow.
- With CONFIG_DEBUG_KOBJECT_RELEASE, a normal map, I/O and unmap without fault injection triggers KASAN use-after-free reports from rnbd_destroy_sess_dev(), run as a delayed kobject release after the session it uses was freed by rnbd_srv_link_ev(); this happens with and without this series and will be reported with the rnbd issues.

The bugs were found with kbound, a research static analysis prototype, and the patches were prepared with the help of an AI assistant, as noted in the Assisted-by tags; I have reviewed and tested them as described.

Yuqiang Sun (3):
RDMA/rtrs-srv: Do not free the path on path file creation failure RDMA/rtrs-srv: Balance the session device reference in sysfs setup RDMA/rtrs-srv: Reject an empty pathname

 drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c | 37 +++++++++++---------
 drivers/infiniband/ulp/rtrs/rtrs-srv.c       | 19 ++++++++--
 2 files changed, 38 insertions(+), 18 deletions(-)

base-commit: c3e8e9451e6f1b69f685b75e0fbf14f3d0fb2539

Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/3 RDMA/rtrs-srv: Do not free the path on path file creation failure

When rtrs_srv_create_path_files() fails after kobject_init_and_add() of srv_path->kobj, its error path ends with kobject_put(&srv_path->kobj), and the release, rtrs_srv_release(), frees srv_path. The caller does not expect that: process_info_req() goes on to free its tx IU through srv_path->s.dev, and rtrs_srv_info_req_done() then closes the path, whose close work tears down the connections, puts the session and finally frees srv_path again in free_path().

Commit 5b7437339011 ("RDMA/rtrs: Fix use-after-free in path file creation cleanup") reordered the cleanup inside rtrs_srv_create_path_files() so that the helper runs before the put, but the final put still frees srv_path under the caller. The same pattern exists one level down: rtrs_srv_create_stats_files() puts srv_path->stats->kobj_stats on failure, whose release frees srv_path->stats, which free_path() frees again.

With one allocation failure injected into kobject_init_and_add() of the path kobject (failslab restricted to rtrs_srv_create_path_files), KASAN reports while a client connects:

  BUG: KASAN: slab-use-after-free in rtrs_srv_info_req_done+0xe43/0x1740
  Read of size 8 by task kworker/0:1H/84
  Call Trace:
   rtrs_srv_info_req_done
   __ib_process_cq
   ib_cq_poll_work
  Freed by task 84:
   kfree
   kobject_put
   rtrs_srv_create_path_files
   rtrs_srv_info_req_done

These paths run in the server's completion worker when a client sends its info request, and are reached when allocating or creating the sysfs entries for the new path fails.

Keep srv_path and its stats owned by the caller on failure: the error paths only delete what they added, and free_path(), which runs in every case, drops the kobjects that were initialized but never made it into sysfs, so their releases free the structures; otherwise it frees them as before. This keeps the kobject names from leaking, which is what the puts in the error paths were added for.

The problem was found with kbound, a static analysis prototype that checks whether callers rely on an object after a callee returns a value for which the callee may have released it, on v7.2.12; it is present in the rdma for-next branch.

Build-tested with x86_64 and arm64 allmodconfig (W=1, sparse): no new warnings. Run-tested under KVM (KASAN, lockdep, kmemleak), failing the path or the stats kobject allocation with failslab during a client's
connection: before this series KASAN reports use-after-free in rtrs_srv_info_req_done() and rtrs_srv_close_work() and a general protection fault follows; with this series nothing is reported, also with CONFIG_DEBUG_KOBJECT_RELEASE. Failures of the two sysfs_create_group() calls were not injected. With this patch alone, the close work still uses the session freed as described in the next patch. Not tested on RDMA hardware.

Fixes 424774c9f3fa ("RDMA/rtrs: Call kobject_put in the failure path")
Assisted-by Claude:claude-opus-5-5 kbound
---
 drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c | 16 +++++++---------
 drivers/infiniband/ulp/rtrs/rtrs-srv.c       | 14 +++++++++++++-
 2 files changed, 20 insertions(+), 10 deletions(-)
drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c +7 -9
index 9dd9141c86a5..45ee07a98ca8 100644
@@ -238,25 +238,24 @@ static int rtrs_srv_create_stats_files(struct rtrs_srv_path *srv_path)
 				   &srv_path->kobj, "stats");
 	if (err) {
 		rtrs_err(s, "kobject_init_and_add(): %pe\n", ERR_PTR(err));
-		kobject_put(&srv_path->stats->kobj_stats);
 		return err;
 	}
 	err = sysfs_create_group(&srv_path->stats->kobj_stats,
 				 &rtrs_srv_stats_attr_group);
 	if (err) {
 		rtrs_err(s, "sysfs_create_group(): %pe\n", ERR_PTR(err));
-		goto err;
+		kobject_del(&srv_path->stats->kobj_stats);
+		return err;
 	}
 
 	return 0;
-
-err:
-	kobject_del(&srv_path->stats->kobj_stats);
-	kobject_put(&srv_path->stats->kobj_stats);
-
-	return err;
 }
 
+/*
+ * On failure, the caller still owns srv_path and frees it in free_path(),
+ * which also drops the kobjects initialized here.  Do not put them here:
+ * their releases free srv_path and srv_path->stats.
+ */
 int rtrs_srv_create_path_files(struct rtrs_srv_path *srv_path)
 {
 	struct rtrs_srv_sess *srv = srv_path->srv;
@@ -296,7 +295,6 @@ int rtrs_srv_create_path_files(struct rtrs_srv_path *srv_path)
 	kobject_del(&srv_path->kobj);
 destroy_root:
 	rtrs_srv_destroy_once_sysfs_root_folders(srv_path);
-	kobject_put(&srv_path->kobj);
 
 	return err;
 }
drivers/infiniband/ulp/rtrs/rtrs-srv.c +13 -1
index 5314599d29fe..641a7bf5e02c 100644
@@ -1584,11 +1584,23 @@ static void free_path(struct rtrs_srv_path *srv_path)
 	if (srv_path->kobj.state_in_sysfs) {
 		kobject_del(&srv_path->kobj);
 		kobject_put(&srv_path->kobj);
+		return;
+	}
+
+	/*
+	 * rtrs_srv_create_path_files() may have failed after initializing
+	 * these kobjects; then their releases free the structures.
+	 */
+	if (srv_path->stats->kobj_stats.state_initialized) {
+		kobject_put(&srv_path->stats->kobj_stats);
 	} else {
 		free_percpu(srv_path->stats->rdma_stats);
 		kfree(srv_path->stats);
-		kfree(srv_path);
 	}
+	if (srv_path->kobj.state_initialized)
+		kobject_put(&srv_path->kobj);
+	else
+		kfree(srv_path);
 }
 
 static void rtrs_srv_close_work(struct work_struct *work)

base-commit: c3e8e9451e6f1b69f685b75e0fbf14f3d0fb2539

Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/3] RDMA/rtrs-srv: Do not free the path on path file creation failure

Sashiko has reviewed this patch and found no issues.
Quoted 17 lines

When rtrs_srv_create_path_files() fails after kobject_init_and_add() of srv_path->kobj, its error path ends with kobject_put(&srv_path->kobj), and the release, rtrs_srv_release(), frees srv_path. The caller does not expect that: process_info_req() goes on to free its tx IU through srv_path->s.dev, and rtrs_srv_info_req_done() then closes the path, whose close work tears down the connections, puts the session and finally frees srv_path again in free_path().

Commit 5b7437339011 ("RDMA/rtrs: Fix use-after-free in path file creation cleanup") reordered the cleanup inside rtrs_srv_create_path_files() so that the helper runs before the put, but the final put still frees srv_path under the caller. The same pattern exists one level down: rtrs_srv_create_stats_files() puts srv_path->stats->kobj_stats on failure, whose release frees srv_path->stats, which free_path() frees again.
[ ... ]

Quoted 4 lines
Fixes 424774c9f3fa ("RDMA/rtrs: Call kobject_put in the failure path")
Assisted-by Claude:claude-opus-5-5 kbound

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH 1/3] RDMA/rtrs-srv: Do not free the path on path file creation failure

In reply to Yuqiang Sun
Quoted 48 lines · Yuqiang Sun

When rtrs_srv_create_path_files() fails after kobject_init_and_add() of srv_path->kobj, its error path ends with kobject_put(&srv_path->kobj), and the release, rtrs_srv_release(), frees srv_path. The caller does not expect that: process_info_req() goes on to free its tx IU through srv_path->s.dev, and rtrs_srv_info_req_done() then closes the path, whose close work tears down the connections, puts the session and finally frees srv_path again in free_path().

Commit 5b7437339011 ("RDMA/rtrs: Fix use-after-free in path file creation cleanup") reordered the cleanup inside rtrs_srv_create_path_files() so that the helper runs before the put, but the final put still frees srv_path under the caller. The same pattern exists one level down: rtrs_srv_create_stats_files() puts srv_path->stats->kobj_stats on failure, whose release frees srv_path->stats, which free_path() frees again.

With one allocation failure injected into kobject_init_and_add() of the path kobject (failslab restricted to rtrs_srv_create_path_files), KASAN reports while a client connects:

  BUG: KASAN: slab-use-after-free in rtrs_srv_info_req_done+0xe43/0x1740
  Read of size 8 by task kworker/0:1H/84
  Call Trace:
   rtrs_srv_info_req_done
   __ib_process_cq
   ib_cq_poll_work
  Freed by task 84:
   kfree
   kobject_put
   rtrs_srv_create_path_files
   rtrs_srv_info_req_done

These paths run in the server's completion worker when a client sends its info request, and are reached when allocating or creating the sysfs entries for the new path fails.

Keep srv_path and its stats owned by the caller on failure: the error paths only delete what they added, and free_path(), which runs in every case, drops the kobjects that were initialized but never made it into sysfs, so their releases free the structures; otherwise it frees them as before. This keeps the kobject names from leaking, which is what the puts in the error paths were added for.

The problem was found with kbound, a static analysis prototype that checks whether callers rely on an object after a callee returns a value for which the callee may have released it, on v7.2.12; it is present in the rdma for-next branch.

Quoted 10 lines

Build-tested with x86_64 and arm64 allmodconfig (W=1, sparse): no new warnings. Run-tested under KVM (KASAN, lockdep, kmemleak), failing the path or the stats kobject allocation with failslab during a client's
connection: before this series KASAN reports use-after-free in rtrs_srv_info_req_done() and rtrs_srv_close_work() and a general protection fault follows; with this series nothing is reported, also with CONFIG_DEBUG_KOBJECT_RELEASE. Failures of the two sysfs_create_group() calls were not injected. With this patch alone, the close work still uses the session freed as described in the next patch. Not tested on RDMA hardware.

Interesting claim. How did you trigger the KASAN warning in the RDMA ULP?

Thanks

Quoted 89 lines
Fixes 424774c9f3fa ("RDMA/rtrs: Call kobject_put in the failure path")
Assisted-by Claude:claude-opus-5-5 kbound
---
 drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c | 16 +++++++---------
 drivers/infiniband/ulp/rtrs/rtrs-srv.c       | 14 +++++++++++++-
 2 files changed, 20 insertions(+), 10 deletions(-)
drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c +7 -9
index 9dd9141c86a5..45ee07a98ca8 100644
@@ -238,25 +238,24 @@ static int rtrs_srv_create_stats_files(struct rtrs_srv_path *srv_path)
 				   &srv_path->kobj, "stats");
 	if (err) {
 		rtrs_err(s, "kobject_init_and_add(): %pe\n", ERR_PTR(err));
-		kobject_put(&srv_path->stats->kobj_stats);
 		return err;
 	}
 	err = sysfs_create_group(&srv_path->stats->kobj_stats,
 				 &rtrs_srv_stats_attr_group);
 	if (err) {
 		rtrs_err(s, "sysfs_create_group(): %pe\n", ERR_PTR(err));
-		goto err;
+		kobject_del(&srv_path->stats->kobj_stats);
+		return err;
 	}
 
 	return 0;
-
-err:
-	kobject_del(&srv_path->stats->kobj_stats);
-	kobject_put(&srv_path->stats->kobj_stats);
-
-	return err;
 }
 
+/*
+ * On failure, the caller still owns srv_path and frees it in free_path(),
+ * which also drops the kobjects initialized here.  Do not put them here:
+ * their releases free srv_path and srv_path->stats.
+ */
 int rtrs_srv_create_path_files(struct rtrs_srv_path *srv_path)
 {
 	struct rtrs_srv_sess *srv = srv_path->srv;
@@ -296,7 +295,6 @@ int rtrs_srv_create_path_files(struct rtrs_srv_path *srv_path)
 	kobject_del(&srv_path->kobj);
 destroy_root:
 	rtrs_srv_destroy_once_sysfs_root_folders(srv_path);
-	kobject_put(&srv_path->kobj);
 
 	return err;
 }
drivers/infiniband/ulp/rtrs/rtrs-srv.c +13 -1
index 5314599d29fe..641a7bf5e02c 100644
@@ -1584,11 +1584,23 @@ static void free_path(struct rtrs_srv_path *srv_path)
 	if (srv_path->kobj.state_in_sysfs) {
 		kobject_del(&srv_path->kobj);
 		kobject_put(&srv_path->kobj);
+		return;
+	}
+
+	/*
+	 * rtrs_srv_create_path_files() may have failed after initializing
+	 * these kobjects; then their releases free the structures.
+	 */
+	if (srv_path->stats->kobj_stats.state_initialized) {
+		kobject_put(&srv_path->stats->kobj_stats);
 	} else {
 		free_percpu(srv_path->stats->rdma_stats);
 		kfree(srv_path->stats);
-		kfree(srv_path);
 	}
+	if (srv_path->kobj.state_initialized)
+		kobject_put(&srv_path->kobj);
+	else
+		kfree(srv_path);
 }
 
 static void rtrs_srv_close_work(struct work_struct *work)

base-commit: c3e8e9451e6f1b69f685b75e0fbf14f3d0fb2539

Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH 1/3] RDMA/rtrs-srv: Do not free the path on path file creation failure

Quoted 1 line · Leon Romanovsky

Interesting claim. How did you trigger the KASAN warning in the RDMA ULP?

With soft-RoCE in a KVM guest, with both ends of rtrs in the same guest, and failslab standing in for an allocation failure:

- Kernel: rdma for-next c3e8e9451e6f, x86_64, with KASAN, RDMA_RXE, INFINIBAND_RTRS_CLIENT/SERVER, BLK_DEV_RNBD_CLIENT/SERVER, BLK_DEV_RAM, FAILSLAB and FAULT_INJECTION_STACKTRACE_FILTER, all built in.
- In the guest: "rdma link add rxe0 type rxe netdev eth0", then failslab is limited to call stacks through rtrs_srv_create_path_files() (require-start/require-end set to its range from /proc/kallsyms, stacktrace-depth 32, probability 100, times 1, interval N), and /dev/ram0 is mapped through the rnbd client:
echo "sessname=s1 path=ip:10.0.2.15 device_path=/dev/ram0" \
> /sys/class/rnbd-client/ctl/map_device
- The server handles the client's info request in its completion worker, in rtrs_srv_info_req_done(). With N chosen so that the failing allocation is the one in kobject_init_and_add() of srv_path->kobj, rtrs_srv_create_path_files() fails, its kobject_put() frees srv_path through rtrs_srv_release(), and process_info_req() and rtrs_srv_info_req_done() go on using it: that is the KASAN report in the changelog, followed by more in rtrs_srv_close_work() and a general protection fault. Failing the stats kobject instead gives reports of the same kind.

The steps in the guest, after boot:

  ip link set eth0 up
  ip addr add 10.0.2.15/24 dev eth0
  rdma link add rxe0 type rxe netdev eth0
  f=rtrs_srv_create_path_files
  start=$(awk -v f=$f '$3 == f {print $1; exit}' /proc/kallsyms)
  end=$(sort /proc/kallsyms |
        awk -v s=$start 'found && $1 != s {print $1; exit} $1 == s {found=1}')
  F=/sys/kernel/debug/failslab
  echo 0x$start > $F/require-start
  echo 0x$end > $F/require-end
  echo 32 > $F/stacktrace-depth
  echo N > $F/task-filter
  echo N > $F/ignore-gfp-wait
  echo $N > $F/interval
  echo 1 > $F/times
  echo 2 > $F/verbose
  echo 100 > $F/probability
  echo "sessname=s1 path=ip:10.0.2.15 device_path=/dev/ram0" \
       > /sys/class/rnbd-client/ctl/map_device

With verbose=2 failslab prints the stack of the allocation it fails, so N is found by trying N=1, 2, ... until it is the one in kobject_init_and_add() for srv_path->kobj (21 in our build).

So it needs an allocation failure while a client connects; nothing else is unusual. I can send the full KASAN log, the init script and the kernel config if that helps. The client that triggers patch 2's use-after-free from the network by sending an empty pathname (fixed by patch 3) is not public; I can send it to you privately if you want it.

Thanks,
Yuqiang

#5 lore raw

Re: [PATCH 1/3] RDMA/rtrs-srv: Do not free the path on path file creation failure

Quoted 52 lines · Yuqiang Sun
Quoted 1 line · Leon Romanovsky

Interesting claim. How did you trigger the KASAN warning in the RDMA ULP?

With soft-RoCE in a KVM guest, with both ends of rtrs in the same guest, and failslab standing in for an allocation failure:

- Kernel: rdma for-next c3e8e9451e6f, x86_64, with KASAN, RDMA_RXE, INFINIBAND_RTRS_CLIENT/SERVER, BLK_DEV_RNBD_CLIENT/SERVER, BLK_DEV_RAM, FAILSLAB and FAULT_INJECTION_STACKTRACE_FILTER, all built in.
- In the guest: "rdma link add rxe0 type rxe netdev eth0", then failslab is limited to call stacks through rtrs_srv_create_path_files() (require-start/require-end set to its range from /proc/kallsyms, stacktrace-depth 32, probability 100, times 1, interval N), and /dev/ram0 is mapped through the rnbd client:
echo "sessname=s1 path=ip:10.0.2.15 device_path=/dev/ram0" \
> /sys/class/rnbd-client/ctl/map_device
- The server handles the client's info request in its completion worker, in rtrs_srv_info_req_done(). With N chosen so that the failing allocation is the one in kobject_init_and_add() of srv_path->kobj, rtrs_srv_create_path_files() fails, its kobject_put() frees srv_path through rtrs_srv_release(), and process_info_req() and rtrs_srv_info_req_done() go on using it: that is the KASAN report in the changelog, followed by more in rtrs_srv_close_work() and a general protection fault. Failing the stats kobject instead gives reports of the same kind.

The steps in the guest, after boot:

  ip link set eth0 up
  ip addr add 10.0.2.15/24 dev eth0
  rdma link add rxe0 type rxe netdev eth0
  f=rtrs_srv_create_path_files
  start=$(awk -v f=$f '$3 == f {print $1; exit}' /proc/kallsyms)
  end=$(sort /proc/kallsyms |
        awk -v s=$start 'found && $1 != s {print $1; exit} $1 == s {found=1}')
  F=/sys/kernel/debug/failslab
  echo 0x$start > $F/require-start
  echo 0x$end > $F/require-end
  echo 32 > $F/stacktrace-depth
  echo N > $F/task-filter
  echo N > $F/ignore-gfp-wait
  echo $N > $F/interval
  echo 1 > $F/times
  echo 2 > $F/verbose
  echo 100 > $F/probability
  echo "sessname=s1 path=ip:10.0.2.15 device_path=/dev/ram0" \
       > /sys/class/rnbd-client/ctl/map_device

With verbose=2 failslab prints the stack of the allocation it fails, so N is found by trying N=1, 2, ... until it is the one in kobject_init_and_add() for srv_path->kobj (21 in our build).

Quoted 5 lines

So it needs an allocation failure while a client connects; nothing else is unusual. I can send the full KASAN log, the init script and the kernel config if that helps. The client that triggers patch 2's use-after-free from the network by sending an empty pathname (fixed by patch 3) is not public; I can send it to you privately if you want it.

No, I don't need it. I was just curious what “no RDMA hardware” means.

Thanks

Quoted 3 lines

Thanks,
Yuqiang

#6 lore raw

PATCH 2/3 RDMA/rtrs-srv: Balance the session device reference in sysfs setup

In reply to Yuqiang Sun

The session device is initialized in get_or_create_srv(), and the reference that device_initialize() takes is meant to be dropped last, by free_srv(). Each path takes one more reference, which rtrs_srv_destroy_once_sysfs_root_folders() drops when the path goes away. On the failure paths of the path sysfs setup this does not add up:

- rtrs_srv_create_once_sysfs_root_folders() calls put_device() when device_add() or the creation of the "paths" kobject fails, then unlocks srv->paths_mutex;
- rtrs_srv_create_path_files() calls rtrs_srv_destroy_once_sysfs_root_folders() when a later step fails, but the per-path reference is only taken by process_info_req() after rtrs_srv_create_path_files() succeeds.

Both drop the base reference, so rtrs_srv_dev_release() frees the session while it is still locked, on ctx->srv_list and used by its
paths: the mutex_unlock() writes to freed memory, and the path's close work later uses and puts the freed session again. In addition, srv->dev_ref stays incremented when the first helper fails, so the next path of the session skips the setup although the device and the "paths" kobject do not exist.

With one allocation failure injected into device_add() (failslab restricted to rtrs_srv_create_path_files, into which the helper is inlined), KASAN reports while a client connects:

  BUG: KASAN: slab-use-after-free in __mutex_unlock_slowpath
  Call Trace:
   mutex_unlock
   rtrs_srv_create_path_files
   rtrs_srv_info_req_done
   __ib_process_cq
  Freed by task 25:
   kfree
   device_release
   kobject_put
   rtrs_srv_create_path_files
   rtrs_srv_info_req_done

and with the failure injected into kobject_init_and_add() of the path kobject instead, once the previous patch keeps srv_path alive:

  BUG: KASAN: slab-use-after-free in rtrs_srv_close_work
  Freed by task 88:
   kfree
   device_release
   kobject_put
   rtrs_srv_create_path_files
   rtrs_srv_info_req_done

This runs in the server's completion worker when a path sends its info request. Besides allocation failures in the sysfs setup, device_add() also fails when the client sends an empty pathname, which the next patch rejects.

Take the per-path reference in rtrs_srv_create_once_sysfs_root_folders() on success, instead of in process_info_req(), so that the put in rtrs_srv_destroy_once_sysfs_root_folders() is balanced on every path, and do not put the device when the helper itself fails. Undo the dev_ref increment on every failure, including dev_set_name(). The device is still released, together with its name, by the put_device() in free_srv().

The problem was found while checking at runtime, with the fault injection described above, a report of the kbound static analysis prototype for the same function, on v7.2.12; it is present in the rdma for-next branch.

Build-tested with x86_64 and arm64 allmodconfig (W=1, sparse): no new warnings. Run-tested under KVM (KASAN, lockdep, kmemleak) by failing, with failslab, each allocation of dev_set_name(), device_add() and the "paths" kobject, and the path kobject, during a client's connection:
before this series KASAN reports the use-after-free above, with this series nothing is reported, also with CONFIG_DEBUG_KOBJECT_RELEASE. Mapping, I/O and unmapping without fault injection still work. Not tested on RDMA hardware.

Fixes baa5b28b7a47 ("RDMA/rtrs-srv: Replace device_register with device_initialize and device_add")
Fixes e2853c49477d ("RDMA/rtrs-srv-sysfs: fix missing put_device")
Assisted-by Claude:claude-opus-5-5 kbound
---
 drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c | 21 +++++++++++++-------
 drivers/infiniband/ulp/rtrs/rtrs-srv.c       |  1 -
 2 files changed, 14 insertions(+), 8 deletions(-)
drivers/infiniband/ulp/rtrs/rtrs-srv-sysfs.c +14 -7
index 45ee07a98ca8..8de3963ca791 100644
@@ -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;
 	}
 	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,20 +177,27 @@ 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);
-unlock:
+get:
+	/* Dropped in rtrs_srv_destroy_once_sysfs_root_folders() */
+	get_device(&srv->dev);
+	mutex_unlock(&srv->paths_mutex);
+
+	return 0;
+
+dec_ref:
+	/* The reference from device_initialize() is dropped by free_srv() */
+	srv->dev_ref--;
 	mutex_unlock(&srv->paths_mutex);
 
 	return err;
drivers/infiniband/ulp/rtrs/rtrs-srv.c +0 -1
index 641a7bf5e02c..56f0ab5a7619 100644
@@ -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");
Signature · 1 line
2.53.0
#7 lore raw

Re: [PATCH 2/3] RDMA/rtrs-srv: Balance the session device reference in sysfs setup

Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

The session device is initialized in get_or_create_srv(), and the reference that device_initialize() takes is meant to be dropped last, by free_srv(). Each path takes one more reference, which rtrs_srv_destroy_once_sysfs_root_folders() drops when the path goes away. On the failure paths of the path sysfs setup this does not add up:

- rtrs_srv_create_once_sysfs_root_folders() calls put_device() when device_add() or the creation of the "paths" kobject fails, then unlocks srv->paths_mutex;
- rtrs_srv_create_path_files() calls rtrs_srv_destroy_once_sysfs_root_folders() when a later step fails, but the per-path reference is only taken by process_info_req() after rtrs_srv_create_path_files() succeeds.

Quoted 6 lines
[ ... ]
Fixes baa5b28b7a47 ("RDMA/rtrs-srv: Replace device_register with device_initialize and device_add")
Fixes e2853c49477d ("RDMA/rtrs-srv-sysfs: fix missing put_device")
Assisted-by Claude:claude-opus-5-5 kbound

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#8 lore raw

PATCH 3/3 RDMA/rtrs-srv: Reject an empty pathname

In reply to Yuqiang Sun

process_info_req() uses the pathname sent by the client as the name of the session device and rejects names containing '/' or '.', but it accepts an empty one. dev_set_name() succeeds for it, and device_add() then fails in kobject_add_internal(), which warns about the empty name.

Reject an empty pathname together with the other invalid ones.

Build-tested with x86_64 and arm64 allmodconfig (W=1, sparse): no new warnings. Run-tested under KVM with KASAN, lockdep and kmemleak, using a small user-space rtrs client that sends an empty pathname: the server now rejects it at the check above, nothing is reported, and an ordinary rnbd map, I/O and unmap afterwards still work. Not tested on RDMA hardware.

Fixes dea7bb3ad3e0 ("RDMA/rtrs: Do not allow sessname to contain special symbols / and .")
Assisted-by Claude:claude-opus-5-5 kbound
---
 drivers/infiniband/ulp/rtrs/rtrs-srv.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/infiniband/ulp/rtrs/rtrs-srv.c +4 -0
index 56f0ab5a7619..d34a177f7ba2 100644
@@ -823,6 +823,10 @@ static int process_info_req(struct rtrs_srv_con *con,
 		return err;
 	}
 
+	if (!msg->pathname[0]) {
+		rtrs_err(s, "pathname cannot be empty\n");
+		return -EINVAL;
+	}
 	if (strchr(msg->pathname, '/') || strchr(msg->pathname, '.')) {
 		rtrs_err(s, "pathname cannot contain / and .\n");
 		return -EINVAL;
Signature · 1 line
2.53.0
#9 lore raw

Re: [PATCH 3/3] RDMA/rtrs-srv: Reject an empty pathname

Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

process_info_req() uses the pathname sent by the client as the name of the session device and rejects names containing '/' or '.', but it accepts an empty one. dev_set_name() succeeds for it, and device_add() then fails in kobject_add_internal(), which warns about the empty name.

Reject an empty pathname together with the other invalid ones.

Build-tested with x86_64 and arm64 allmodconfig (W=1, sparse): no new warnings. Run-tested under KVM with KASAN, lockdep and kmemleak, using a small user-space rtrs client that sends an empty pathname: the server now rejects it at the check above, nothing is reported, and an ordinary rnbd map, I/O and unmap afterwards still work. Not tested on RDMA hardware.

Quoted 4 lines
Fixes dea7bb3ad3e0 ("RDMA/rtrs: Do not allow sessname to contain special symbols / and .")
Assisted-by Claude:claude-opus-5-5 kbound

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3