Skip to contentSkip to conversation

blk-cgroup: fix UAF in __blkcg_rstat_flush()

PATCH Applied Ming Lei 3 participants 3 messages Feb 5 – Jun 22

Subsystems (3)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH v2 1/1 blk-cgroup: fix UAF in __blkcg_rstat_flush()

From: Michal Koutný <[email protected]>

When multiple blkgs in the same blkcg are released concurrently, a use-after-free can occur. The race happens when one blkg's __blkcg_rstat_flush() removes another blkg's iostat entries via llist_del_all(). The second blkg sees an empty list and proceeds to free itself while the first is still iterating over its entries.

Move the flush from __blkg_release() (RCU callback) to blkg_release() (before call_rcu). This ensures the RCU grace period waits for any concurrent flush's rcu_read_lock() section to complete before freeing.

Cc Jay Shin <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Waiman Long <[email protected]>
Fixes 20cb1c2fb756 ("blk-cgroup: Flush stats before releasing blkcg_gq")
Reported-by [email protected]

---
V2:
- take simpler approach from Michal Koutný

 block/blk-cgroup.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
block/blk-cgroup.c +11 -10
index 3cffb68ba5d8..ec079c2c404e 100644
@@ -163,20 +163,10 @@ static void blkg_free(struct blkcg_gq *blkg)
 static void __blkg_release(struct rcu_head *rcu)
 {
 	struct blkcg_gq *blkg = container_of(rcu, struct blkcg_gq, rcu_head);
-	struct blkcg *blkcg = blkg->blkcg;
-	int cpu;
 
 #ifdef CONFIG_BLK_CGROUP_PUNT_BIO
 	WARN_ON(!bio_list_empty(&blkg->async_bios));
 #endif
-	/*
-	 * Flush all the non-empty percpu lockless lists before releasing
-	 * us, given these stat belongs to us.
-	 *
-	 * blkg_stat_lock is for serializing blkg stat update
-	 */
-	for_each_possible_cpu(cpu)
-		__blkcg_rstat_flush(blkcg, cpu);
 
 	/* release the blkcg and parent blkg refs this blkg has been holding */
 	css_put(&blkg->blkcg->css);
@@ -194,6 +184,17 @@ static void __blkg_release(struct rcu_head *rcu)
 static void blkg_release(struct percpu_ref *ref)
 {
 	struct blkcg_gq *blkg = container_of(ref, struct blkcg_gq, refcnt);
+	struct blkcg *blkcg = blkg->blkcg;
+	int cpu;
+
+	/*
+	 * Flush all the non-empty percpu lockless lists before releasing
+	 * us, given these stat belongs to us.
+	 *
+	 * blkg_stat_lock is for serializing blkg stat update
+	 */
+	for_each_possible_cpu(cpu)
+		__blkcg_rstat_flush(blkcg, cpu);
 
 	call_rcu(&blkg->rcu_head, __blkg_release);
 }
Signature · 1 line
2.47.0
Tested-by on v2 1/1
#1 lore raw
Quoted 3 lines · Ming Lei

Move the flush from __blkg_release() (RCU callback) to blkg_release() (before call_rcu). This ensures the RCU grace period waits for any concurrent flush's rcu_read_lock() section to complete before freeing.

We started seeing this in the wild on a 6.18.35-based kernel as a NULL pointer dereference rather than a KASAN report. The freed blkg / percpu iostat slot gets reallocated and zeroed before the concurrent flusher reaches it, so bisc->blkg reads back as NULL:

  BUG: kernel NULL pointer dereference, address: 0000000000000030
  #PF: supervisor read access in kernel mode
  RIP: 0010:__blkcg_rstat_flush.isra.0+0x8d/0x1c0
  Code: ... 48 8b 1a 4c 8d 78 f8 31 c0 f3 48 ab <4c> 8b 73 30 ...
  RBX: 0000000000000000
  Call Trace:
   <IRQ>
   __blkg_release+0x2d/0xf0
   rcu_do_batch+0x1b8/0x570
   rcu_core+0x167/0x350
   handle_softirqs+0xda/0x330

The workload is container-heavy with frequent block-device add/remove, so multiple blkgs in the same blkcg routinely hit blkg_release() concurrently on different CPUs.

I can reproduce reliably under KASAN by inserting a udelay(2000) between llist_del_all() and raw_spin_lock_irqsave() in __blkcg_rstat_flush(), then driving direct I/O to N loop devices from one cgroup followed by parallel LOOP_CTL_REMOVE on each device. KASAN reports slab-use-after-free in __blkcg_rstat_flush() with the expected alloc=blkg_alloc / free=blkg_free_workfn stacks.

With this patch applied on top of the same udelay-widened tree, the same harness runs 150 rounds clean.

This doesn't appear to have been picked up after V1 was dropped; would be good to get it queued.

maintainer Applied
#2 lore raw
In reply to Ming Lei
Quoted 11 lines · Ming Lei

When multiple blkgs in the same blkcg are released concurrently, a use-after-free can occur. The race happens when one blkg's __blkcg_rstat_flush() removes another blkg's iostat entries via llist_del_all(). The second blkg sees an empty list and proceeds to free itself while the first is still iterating over its entries.

Move the flush from __blkg_release() (RCU callback) to blkg_release() (before call_rcu). This ensures the RCU grace period waits for any concurrent flush's rcu_read_lock() section to complete before freeing.

[...]

Applied, thanks!

[1/1] blk-cgroup: fix UAF in __blkcg_rstat_flush()

      commit: 0ab5ee5a1badb58cbb2242617cb01a4972b1f2a2

Best regards,

Signature · 1 line
Jens Axboe