Skip to contentSkip to conversation

rbd: use kref_put_lock() to fix potential UAF

PATCH New Zhen Lei 1 participant 1 message Jul 22, 2021

Subsystems (2)
lore
Share & tools
lore

Zhen Lei

author
#0 lore raw

PATCH 1/1 rbd: use kref_put_lock() to fix potential UAF

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")
---
 drivers/block/rbd.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/block/rbd.c +4 -3
index 531d390902dd..3a8989bf8e9c 100644
@@ -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);
 }
 
 /*
Signature · 1 line
2.25.1