rnbd-srv: Fix sess_dev use-after-free in process_msg_close()
Subsystems (3)
PATCH 1/1 rnbd-srv: Fix sess_dev use-after-free in process_msg_close()
process_msg_close() drops the device reference with rnbd_put_sess_dev() before using sess_dev again in rnbd_srv_destroy_dev_session_sysfs(). If that put drops the last kref, e.g. when the device is concurrently being torn down through rnbd_srv_sess_dev_force_close(), the waiter in rnbd_destroy_sess_dev() is woken up and frees sess_dev, so the subsequent rnbd_srv_destroy_dev_session_sysfs() call operates on freed memory.
Move the rnbd_put_sess_dev() call after the sysfs teardown so it is the last access to sess_dev.
--- drivers/block/rnbd/rnbd-srv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/rnbd/rnbd-srv.c +1 -1
@@ -361,10 +361,10 @@ static void process_msg_close(struct rnbd_srv_session *srv_sess,if (IS_ERR (sess_dev))return ; -rnbd_put_sess_dev (sess_dev);mutex_lock (&srv_sess->lock);rnbd_srv_destroy_dev_session_sysfs (sess_dev);mutex_unlock (&srv_sess->lock); +rnbd_put_sess_dev (sess_dev); } static int process_msg_open(struct rnbd_srv_session *srv_sess,
Signature · 1 line
2.34.1
Quoted 24 lines · Wentao Liang
process_msg_close()drops the device reference withrnbd_put_sess_dev()before usingsess_devagain inrnbd_srv_destroy_dev_session_sysfs(). If that put drops the last kref, e.g. when the device is concurrently being torn down throughrnbd_srv_sess_dev_force_close(), the waiter inrnbd_destroy_sess_dev()is woken up and freessess_dev, so the subsequentrnbd_srv_destroy_dev_session_sysfs()call operates on freed memory.Move the
rnbd_put_sess_dev()call after the sysfs teardown so it is the last access tosess_dev.Fixes 2de6c8de192b ("block/rnbd: server: main functionality")Signed-off-by Wentao Liang <[email protected]>--- drivers/block/rnbd/rnbd-srv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/block/rnbd/rnbd-srv.c +0
-0
drivers/block/rnbd/rnbd-srv.c static void process_msg_close(struct rnbd_srv_session *srv_sess, · line 367
Quoted 8 lines
drivers/block/rnbd/rnbd-srv.c +1
-1@@ -361,10 +361,10 @@ static void process_msg_close(struct rnbd_srv_session *srv_sess,if (IS_ERR (sess_dev))return ; -rnbd_put_sess_dev (sess_dev);mutex_lock (&srv_sess->lock);rnbd_srv_destroy_dev_session_sysfs (sess_dev);mutex_unlock (&srv_sess->lock); +rnbd_put_sess_dev (sess_dev);
Thanks for the patch.
Sashiko has critical finding about this change.
https://sashiko.dev/#/patchset/20260916094817.2007756-1-vulab%40iscas.ac.cn
Can you please review that?
Quoted 6 lines
drivers/block/rnbd/rnbd-srv.c +0
-1} static int process_msg_open(struct rnbd_srv_session *srv_sess, --2.34.1
Signature · 2 lines
Regards -Haris
Hi Haris,
Sorry for the very late reply. Your message arrived when I was already behind on this thread, and then it sat far longer than it should have. That is my fault, not a sign that the report was unimportant.
I have now gone through the Sashiko report, and it is right: the patch is wrong in exactly the way it describes.
process_msg_close() takes a kref with rnbd_get_sess_dev() and then calls rnbd_srv_destroy_dev_session_sysfs(). That kobject_put() drops the last kobject reference, so rnbd_srv_sess_dev_release() -> rnbd_destroy_sess_dev() runs synchronously in the same thread, still under srv_sess->lock. That function does rnbd_put_sess_dev() and then waits on destroy_comp, but the reference this thread holds is only dropped after the wait returns. So a single CLOSE from a client hangs the teardown thread with the session lock held - I traded the race for a guaranteed deadlock, which is worse than what I set out to fix.
While checking this against the current tree I also found that the race itself has meanwhile been fixed upstream:
4912dab88baa ("rnbd-srv: fix use-after-free on two CLOSE messages")
It takes srv_sess->lock first and looks the ID up with xa_load() under it, so a second teardown of the same ID finds nothing. It is in v7.4-rc1, and it also covers the force_close() interleaving that my patch was trying to address. A v2 of my patch would therefore only duplicate work that is already merged, so please drop this one - there is nothing here for you to apply.
Thanks for pointing me at the Sashiko report.
Regards,
Wentao