Skip to contentSkip to conversation

sched: fix potential use-after-free with cfs bandwidth

PATCH Superseded Josh Don 4 participants 5 messages Feb 10, 2025 – Feb 21, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Josh Don

author
#0 lore raw

PATCH 1/1 sched: fix potential use-after-free with cfs bandwidth

We remove the cfs_rq throttled_csd_list entry *before* doing the unthrottle. The problem with that is that destroy_bandwidth() does a lockless scan of the system for any non-empty CSD lists. As a result, it is possible that destroy_bandwidth() returns while we still have a cfs_rq from the task group about to be unthrottled.

For full correctness, we should avoid removal from the list until after we're done unthrottling in __cfsb_csd_unthrottle().

For consistency, we make the same change to distribute_cfs_runtime(), even though this should already be safe due to destroy_bandwidth() cancelling the bandwidth hrtimers.

---
 kernel/sched/fair.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/sched/fair.c +4 -4
index 34fe6e9490c2..78f542ab03cf 100644
@@ -5917,10 +5917,10 @@ static void __cfsb_csd_unthrottle(void *arg)
 
 	list_for_each_entry_safe(cursor, tmp, &rq->cfsb_csd_list,
 				 throttled_csd_list) {
-		list_del_init(&cursor->throttled_csd_list);
-
 		if (cfs_rq_throttled(cursor))
 			unthrottle_cfs_rq(cursor);
+
+		list_del_init(&cursor->throttled_csd_list);
 	}
 
 	rcu_read_unlock();
@@ -6034,11 +6034,11 @@ static bool distribute_cfs_runtime(struct cfs_bandwidth *cfs_b)
 
 		rq_lock_irqsave(rq, &rf);
 
-		list_del_init(&cfs_rq->throttled_csd_list);
-
 		if (cfs_rq_throttled(cfs_rq))
 			unthrottle_cfs_rq(cfs_rq);
 
+		list_del_init(&cfs_rq->throttled_csd_list);
+
 		rq_unlock_irqrestore(rq, &rf);
 	}
 	SCHED_WARN_ON(!list_empty(&local_unthrottle));
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
#1 lore raw

Hello Josh,

Quoted 6 lines · Josh Don

We remove the cfs_rq throttled_csd_list entry *before* doing the unthrottle. The problem with that is that destroy_bandwidth() does a lockless scan of the system for any non-empty CSD lists. As a result, it is possible that destroy_bandwidth() returns while we still have a cfs_rq from the task group about to be unthrottled.

Quoted 8 lines

For full correctness, we should avoid removal from the list until after we're done unthrottling in __cfsb_csd_unthrottle().

For consistency, we make the same change to distribute_cfs_runtime(), even though this should already be safe due to destroy_bandwidth() cancelling the bandwidth hrtimers.

Other than a small nit: s/destroy_bandwidth/destroy_cfs_bandwidth/g please feel free to add:

Signature · 2 lines
Thanks and Regards,
Prateek
Quoted 35 lines
---
  kernel/sched/fair.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
kernel/sched/fair.c +4 -4
index 34fe6e9490c2..78f542ab03cf 100644
@@ -5917,10 +5917,10 @@ static void __cfsb_csd_unthrottle(void *arg)
 
  	list_for_each_entry_safe(cursor, tmp, &rq->cfsb_csd_list,
  				 throttled_csd_list) {
-		list_del_init(&cursor->throttled_csd_list);
-
  		if (cfs_rq_throttled(cursor))
  			unthrottle_cfs_rq(cursor);
+
+		list_del_init(&cursor->throttled_csd_list);
  	}
 
  	rcu_read_unlock();
@@ -6034,11 +6034,11 @@ static bool distribute_cfs_runtime(struct cfs_bandwidth *cfs_b)
 
  		rq_lock_irqsave(rq, &rf);
 
-		list_del_init(&cfs_rq->throttled_csd_list);
-
  		if (cfs_rq_throttled(cfs_rq))
  			unthrottle_cfs_rq(cfs_rq);
 
+		list_del_init(&cfs_rq->throttled_csd_list);
+
  		rq_unlock_irqrestore(rq, &rf);
  	}
  	SCHED_WARN_ON(!list_empty(&local_unthrottle));
Reviewed-by on v1 1/1
#2 lore raw
In reply to Josh Don
Quoted 6 lines · Josh Don

We remove the cfs_rq throttled_csd_list entry *before* doing the unthrottle. The problem with that is that destroy_bandwidth() does a lockless scan of the system for any non-empty CSD lists. As a result, it is possible that destroy_bandwidth() returns while we still have a cfs_rq from the task group about to be unthrottled.

Quoted 8 lines

For full correctness, we should avoid removal from the list until after we're done unthrottling in __cfsb_csd_unthrottle().

For consistency, we make the same change to distribute_cfs_runtime(), even though this should already be safe due to destroy_bandwidth() cancelling the bandwidth hrtimers.

Good catch!

BTW, I just drew the cfs_rq UAF as below:

CPU0 CPU1
__cfsb_csd_unthrottle()
rq lock
for each cfs_rq on list
list_del_init from list
unregister_fair_sched_group()
destroy_cfs_bandwidth()
if (list_empty(&rq->cfsb_csd_list))
continue; // skip rq0
if (cfs_rq->on_list) // maybe false
unthrottle_cfs_rq()
add cfs_rq to list
rq unlock
cfs_rq freed after RCU grace period
cfs_rq UAF!

Thanks!

Quoted 35 lines
---
  kernel/sched/fair.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
kernel/sched/fair.c +4 -4
index 34fe6e9490c2..78f542ab03cf 100644
@@ -5917,10 +5917,10 @@ static void __cfsb_csd_unthrottle(void *arg)
 
  	list_for_each_entry_safe(cursor, tmp, &rq->cfsb_csd_list,
  				 throttled_csd_list) {
-		list_del_init(&cursor->throttled_csd_list);
-
  		if (cfs_rq_throttled(cursor))
  			unthrottle_cfs_rq(cursor);
+
+		list_del_init(&cursor->throttled_csd_list);
  	}
 
  	rcu_read_unlock();
@@ -6034,11 +6034,11 @@ static bool distribute_cfs_runtime(struct cfs_bandwidth *cfs_b)
 
  		rq_lock_irqsave(rq, &rf);
 
-		list_del_init(&cfs_rq->throttled_csd_list);
-
  		if (cfs_rq_throttled(cfs_rq))
  			unthrottle_cfs_rq(cfs_rq);
 
+		list_del_init(&cfs_rq->throttled_csd_list);
+
  		rq_unlock_irqrestore(rq, &rf);
  	}
  	SCHED_WARN_ON(!list_empty(&local_unthrottle));
author
#4 lore raw
Quoted 8 lines · Markus Elfring

…

Quoted 2 lines

For full correctness, we should avoid removal from the list until after we're done unthrottling in __cfsb_csd_unthrottle().

…

How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.14-rc3#n145

Thanks, yea I'll resend with the fixes tag and with all the additional review tags collected.

Best,
Josh