* [PATCH] block/rnbd: Fix double free in process_msg_open
@ 2026-07-14 11:50 Guangshuo Li
2026-07-17 9:47 ` Jinpu Wang
0 siblings, 1 reply; 2+ messages in thread
From: Guangshuo Li @ 2026-07-14 11:50 UTC (permalink / raw)
To: Md. Haris Iqbal, Jack Wang, Jens Axboe, Danil Kipnis,
Jason Gunthorpe, Bart Van Assche, linux-block, linux-kernel
Cc: Guangshuo Li
process_msg_open() allocates srv_sess_dev with
rnbd_srv_create_set_sess_dev() and then initializes its embedded kobject
through rnbd_srv_create_dev_session_sysfs().
If sysfs creation fails, the helper calls kobject_put(). The final put
invokes rnbd_srv_sess_dev_release(), which reaches
rnbd_destroy_sess_dev() and frees srv_sess_dev. process_msg_open() then
jumps to free_srv_sess_dev and calls kfree() on the same object again,
resulting in a double free.
Add the session device to the list and release srv_dev->lock before
creating the sysfs entries. On failure, rely on the kobject release
callback as the sole owner of the cleanup instead of freeing the object
again.
This issue was found by a static analysis tool I am developing.
Fixes: 8cee532f469b ("block/rnbd: server: sysfs interface functions")
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
drivers/block/rnbd/rnbd-srv.c | 20 ++++++++++++--------
1 file changed, 12 insertions(+), 8 deletions(-)
diff --git a/drivers/block/rnbd/rnbd-srv.c b/drivers/block/rnbd/rnbd-srv.c
index 10e8c438bb43..5a991bfbf3ad 100644
--- a/drivers/block/rnbd/rnbd-srv.c
+++ b/drivers/block/rnbd/rnbd-srv.c
@@ -778,18 +778,22 @@ static int process_msg_open(struct rnbd_srv_session *srv_sess,
}
}
+ /*
+ * Add the session device to the list before initializing its
+ * kobject. If sysfs creation fails, kobject_put() invokes the
+ * release callback, which removes the object from this list and
+ * releases all resources associated with the session device.
+ */
+ list_add(&srv_sess_dev->dev_list, &srv_dev->sess_dev_list);
+ mutex_unlock(&srv_dev->lock);
+
ret = rnbd_srv_create_dev_session_sysfs(srv_sess_dev);
if (ret) {
- mutex_unlock(&srv_dev->lock);
- rnbd_srv_err(srv_sess_dev,
- "Opening device failed, failed to create dev client sysfs files, err: %d\n",
- ret);
- goto free_srv_sess_dev;
+ pr_err("Opening device '%s' on session %s failed, failed to create dev client sysfs files, err: %d\n",
+ full_path, srv_sess->sessname, ret);
+ goto free_path;
}
- list_add(&srv_sess_dev->dev_list, &srv_dev->sess_dev_list);
- mutex_unlock(&srv_dev->lock);
-
rnbd_srv_info(srv_sess_dev, "Opened device '%s'\n", srv_dev->name);
kfree(full_path);
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] block/rnbd: Fix double free in process_msg_open
2026-07-14 11:50 [PATCH] block/rnbd: Fix double free in process_msg_open Guangshuo Li
@ 2026-07-17 9:47 ` Jinpu Wang
0 siblings, 0 replies; 2+ messages in thread
From: Jinpu Wang @ 2026-07-17 9:47 UTC (permalink / raw)
To: Guangshuo Li
Cc: Md. Haris Iqbal, Jens Axboe, Danil Kipnis, Jason Gunthorpe,
Bart Van Assche, linux-block, linux-kernel
On Tue, Jul 14, 2026 at 1:53 PM Guangshuo Li <lgs201920130244@gmail.com> wrote:
>
> process_msg_open() allocates srv_sess_dev with
> rnbd_srv_create_set_sess_dev() and then initializes its embedded kobject
> through rnbd_srv_create_dev_session_sysfs().
>
> If sysfs creation fails, the helper calls kobject_put(). The final put
> invokes rnbd_srv_sess_dev_release(), which reaches
> rnbd_destroy_sess_dev() and frees srv_sess_dev. process_msg_open() then
> jumps to free_srv_sess_dev and calls kfree() on the same object again,
> resulting in a double free.
>
> Add the session device to the list and release srv_dev->lock before
> creating the sysfs entries. On failure, rely on the kobject release
> callback as the sole owner of the cleanup instead of freeing the object
> again.
>
> This issue was found by a static analysis tool I am developing.
>
> Fixes: 8cee532f469b ("block/rnbd: server: sysfs interface functions")
> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
lgtm.
Acked-by: Jack Wang <jinpu.wang@cloud.ionos.com>
> ---
> drivers/block/rnbd/rnbd-srv.c | 20 ++++++++++++--------
> 1 file changed, 12 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/block/rnbd/rnbd-srv.c b/drivers/block/rnbd/rnbd-srv.c
> index 10e8c438bb43..5a991bfbf3ad 100644
> --- a/drivers/block/rnbd/rnbd-srv.c
> +++ b/drivers/block/rnbd/rnbd-srv.c
> @@ -778,18 +778,22 @@ static int process_msg_open(struct rnbd_srv_session *srv_sess,
> }
> }
>
> + /*
> + * Add the session device to the list before initializing its
> + * kobject. If sysfs creation fails, kobject_put() invokes the
> + * release callback, which removes the object from this list and
> + * releases all resources associated with the session device.
> + */
> + list_add(&srv_sess_dev->dev_list, &srv_dev->sess_dev_list);
> + mutex_unlock(&srv_dev->lock);
> +
> ret = rnbd_srv_create_dev_session_sysfs(srv_sess_dev);
> if (ret) {
> - mutex_unlock(&srv_dev->lock);
> - rnbd_srv_err(srv_sess_dev,
> - "Opening device failed, failed to create dev client sysfs files, err: %d\n",
> - ret);
> - goto free_srv_sess_dev;
> + pr_err("Opening device '%s' on session %s failed, failed to create dev client sysfs files, err: %d\n",
> + full_path, srv_sess->sessname, ret);
> + goto free_path;
> }
>
> - list_add(&srv_sess_dev->dev_list, &srv_dev->sess_dev_list);
> - mutex_unlock(&srv_dev->lock);
> -
> rnbd_srv_info(srv_sess_dev, "Opened device '%s'\n", srv_dev->name);
>
> kfree(full_path);
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-07-17 9:47 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-14 11:50 [PATCH] block/rnbd: Fix double free in process_msg_open Guangshuo Li
2026-07-17 9:47 ` Jinpu Wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox