Skip to contentSkip to conversation

alarmtimer: Fix use-after-free of timerqueue node in alarmtimer_suspend()

PATCH In review Zhan Xusheng 2 participants 2 messages Mar 30 – Apr 7 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 alarmtimer: Fix use-after-free of timerqueue node in alarmtimer_suspend()

In alarmtimer_suspend(), the timerqueue_node pointer 'next' is obtained under base->lock via scoped_guard(), but its members (next->expires) are accessed after the lock has been released when the scoped_guard goes out of scope.

Between the lock release and the dereference, a concurrent timer cancellation on another CPU could remove and free the timerqueue node, leading to a use-after-free.

Fix this by copying next->expires into a local ktime_t variable while still holding the lock.

---
 kernel/time/alarmtimer.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
kernel/time/alarmtimer.c +8 -4
index b64db405ba5c..6e173d70d825 100644
@@ -234,19 +234,23 @@ static int alarmtimer_suspend(struct device *dev)
 	if (!rtc)
 		return 0;
 
-	/* Find the soonest timer to expire*/
+	/* Find the soonest timer to expire */
 	for (i = 0; i < ALARM_NUMTYPE; i++) {
 		struct alarm_base *base = &alarm_bases[i];
 		struct timerqueue_node *next;
+		ktime_t next_expires;
 		ktime_t delta;
 
-		scoped_guard(spinlock_irqsave, &base->lock)
+		scoped_guard(spinlock_irqsave, &base->lock) {
 			next = timerqueue_getnext(&base->timerqueue);
+			if (next)
+				next_expires = next->expires;
+		}
 		if (!next)
 			continue;
-		delta = ktime_sub(next->expires, base->get_ktime());
+		delta = ktime_sub(next_expires, base->get_ktime());
 		if (!min || (delta < min)) {
-			expires = next->expires;
+			expires = next_expires;
 			min = delta;
 			type = i;
 		}
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 8 lines · Zhan Xusheng

In alarmtimer_suspend(), the timerqueue_node pointer 'next' is obtained under base->lock via scoped_guard(), but its members (next->expires) are accessed after the lock has been released when the scoped_guard goes out of scope.

Between the lock release and the dereference, a concurrent timer cancellation on another CPU could remove and free the timerqueue node, leading to a use-after-free.

That's correct in theory, but it's a made up scenario as this runs during suspend after all processes which could remove and free it have been frozen already.

That said, I have no objections against the patch per se, but I'm not accepting the unrealistic argument backing it up.

Using AI is fine, but taking the output at face value is not.

Thanks,

tglx