linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Zhen Lei <thunder.leizhen@huawei.com>
To: Ilya Dryomov <idryomov@gmail.com>,
	Dongsheng Yang <dongsheng.yang@easystack.cn>,
	Jens Axboe <axboe@kernel.dk>,
	Yehuda Sadeh <yehuda@hq.newdream.net>,
	Sage Weil <sage@newdream.net>,
	ceph-devel <ceph-devel@vger.kernel.org>,
	linux-block <linux-block@vger.kernel.org>,
	linux-kernel <linux-kernel@vger.kernel.org>
Cc: Zhen Lei <thunder.leizhen@huawei.com>
Subject: [PATCH] rbd: use kref_put_lock() to fix potential UAF
Date: Thu, 22 Jul 2021 12:02:16 +0800	[thread overview]
Message-ID: <20210722040216.4090-1-thunder.leizhen@huawei.com> (raw)

When the refcount is decreased to 0, the rbd_client_release() is called.
Before CPU0 reaches the race point (1), CPU1 may obtain the spinlock
and traverse the linked list to find 'rbdc'. Although CPU1 will call
kref_get() to increase the refcount, it is obviously too late. CPU0 will
release 'rbdc' directly, CPU1 then accesses 'rbdc' and triggers UAF.

Use kref_put_lock() to ensure that both the operations of decrease
refcount to 0 and link deletion are lock protected eliminates this risk.

     CPU0                      CPU1
rbd_client_release():
			    <-------- (1)
spin_lock(&rbd_client_list_lock);
list_del(&rbdc->node);
spin_unlock(&rbd_client_list_lock);

kfree(rbdc);
			    <-------- use-after-free

Fixes: 602adf400201 ("rbd: introduce rados block device (rbd), based on libceph")
Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
---
 drivers/block/rbd.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
index 531d390902dd..3a8989bf8e9c 100644
--- a/drivers/block/rbd.c
+++ b/drivers/block/rbd.c
@@ -875,11 +875,11 @@ static void rbd_client_release(struct kref *kref)
 {
 	struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
 
-	dout("%s: rbdc %p\n", __func__, rbdc);
-	spin_lock(&rbd_client_list_lock);
 	list_del(&rbdc->node);
 	spin_unlock(&rbd_client_list_lock);
 
+	dout("%s: rbdc %p\n", __func__, rbdc);
+
 	ceph_destroy_client(rbdc->client);
 	kfree(rbdc);
 }
@@ -891,7 +891,8 @@ static void rbd_client_release(struct kref *kref)
 static void rbd_put_client(struct rbd_client *rbdc)
 {
 	if (rbdc)
-		kref_put(&rbdc->kref, rbd_client_release);
+		kref_put_lock(&rbdc->kref,
+			      rbd_client_release, &rbd_client_list_lock);
 }
 
 /*
-- 
2.25.1


                 reply	other threads:[~2021-07-22  4:03 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210722040216.4090-1-thunder.leizhen@huawei.com \
    --to=thunder.leizhen@huawei.com \
    --cc=axboe@kernel.dk \
    --cc=ceph-devel@vger.kernel.org \
    --cc=dongsheng.yang@easystack.cn \
    --cc=idryomov@gmail.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sage@newdream.net \
    --cc=yehuda@hq.newdream.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).