Skip to content

Commit f784beb

Browse files
committed
ALSA: timer: Fix link corruption due to double start or stop
Although ALSA timer code got hardening for races, it still causes use-after-free error. This is however rather a corrupted linked list, not actually the concurrent accesses. Namely, when timer start is triggered twice, list_add_tail() is called twice, too. This ends up with the link corruption and triggers KASAN error. The simplest fix would be replacing list_add_tail() with list_move_tail(), but fundamentally it's the problem that we don't check the double start/stop correctly. So, the right fix here is to add the proper checks to snd_timer_start() and snd_timer_stop() (and their variants). BugLink: http://lkml.kernel.org/r/CACT4Y+ZyPRoMQjmawbvmCEDrkBD2BQuH7R09=eOkf5ESK8kJAw@mail.gmail.com Reported-by: Dmitry Vyukov <[email protected]> Cc: <[email protected]> Signed-off-by: Takashi Iwai <[email protected]>
1 parent 2cdc7b6 commit f784beb

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

sound/core/timer.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,10 @@ static int snd_timer_start_slave(struct snd_timer_instance *timeri)
451451
unsigned long flags;
452452

453453
spin_lock_irqsave(&slave_active_lock, flags);
454+
if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
455+
spin_unlock_irqrestore(&slave_active_lock, flags);
456+
return -EBUSY;
457+
}
454458
timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
455459
if (timeri->master && timeri->timer) {
456460
spin_lock(&timeri->timer->lock);
@@ -475,7 +479,8 @@ int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
475479
return -EINVAL;
476480
if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
477481
result = snd_timer_start_slave(timeri);
478-
snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
482+
if (result >= 0)
483+
snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
479484
return result;
480485
}
481486
timer = timeri->timer;
@@ -484,11 +489,18 @@ int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
484489
if (timer->card && timer->card->shutdown)
485490
return -ENODEV;
486491
spin_lock_irqsave(&timer->lock, flags);
492+
if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
493+
SNDRV_TIMER_IFLG_START)) {
494+
result = -EBUSY;
495+
goto unlock;
496+
}
487497
timeri->ticks = timeri->cticks = ticks;
488498
timeri->pticks = 0;
489499
result = snd_timer_start1(timer, timeri, ticks);
500+
unlock:
490501
spin_unlock_irqrestore(&timer->lock, flags);
491-
snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
502+
if (result >= 0)
503+
snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
492504
return result;
493505
}
494506

@@ -502,6 +514,10 @@ static int _snd_timer_stop(struct snd_timer_instance *timeri, int event)
502514

503515
if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
504516
spin_lock_irqsave(&slave_active_lock, flags);
517+
if (!(timeri->flags & SNDRV_TIMER_IFLG_RUNNING)) {
518+
spin_unlock_irqrestore(&slave_active_lock, flags);
519+
return -EBUSY;
520+
}
505521
timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
506522
list_del_init(&timeri->ack_list);
507523
list_del_init(&timeri->active_list);
@@ -512,6 +528,11 @@ static int _snd_timer_stop(struct snd_timer_instance *timeri, int event)
512528
if (!timer)
513529
return -EINVAL;
514530
spin_lock_irqsave(&timer->lock, flags);
531+
if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
532+
SNDRV_TIMER_IFLG_START))) {
533+
spin_unlock_irqrestore(&timer->lock, flags);
534+
return -EBUSY;
535+
}
515536
list_del_init(&timeri->ack_list);
516537
list_del_init(&timeri->active_list);
517538
if (timer->card && timer->card->shutdown) {
@@ -581,10 +602,15 @@ int snd_timer_continue(struct snd_timer_instance *timeri)
581602
if (timer->card && timer->card->shutdown)
582603
return -ENODEV;
583604
spin_lock_irqsave(&timer->lock, flags);
605+
if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
606+
result = -EBUSY;
607+
goto unlock;
608+
}
584609
if (!timeri->cticks)
585610
timeri->cticks = 1;
586611
timeri->pticks = 0;
587612
result = snd_timer_start1(timer, timeri, timer->sticks);
613+
unlock:
588614
spin_unlock_irqrestore(&timer->lock, flags);
589615
snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
590616
return result;

0 commit comments

Comments
 (0)