alarmtimer: Fix use-after-free of timerqueue node in alarmtimer_suspend()
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
@@ -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
Thomas Gleixner
maintainerQuoted 8 lines · Zhan Xusheng
In
alarmtimer_suspend(), thetimerqueue_nodepointer 'next' is obtained underbase->lockviascoped_guard(), but its members (next->expires) are accessed after the lock has been released when thescoped_guardgoes 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