ALSA: timer: Harden slave timer list handling
PATCH 1/2 ALSA: timer: Harden slave timer list handling
A slave timer instance might be still accessible in a racy way while operating the master instance as it lacks of locking. Since the master operation is mostly protected with timer->lock, we should cope with it while changing the slave instance, too. Also, some linked lists (active_list and ack_list) of slave instances aren't unlinked immediately at stopping or closing, and this may lead to unexpected accesses.
This patch tries to address these issues. It adds spin lock of timer->lock (either from master or slave, which is equivalent) in a few places. For avoiding a deadlock, we ensure that the global slave_active_lock is always locked at first before each timer lock.
Also, ack and active_list of slave instances are properly unlinked at snd_timer_stop() and snd_timer_close().
Last but not least, remove the superfluous call of _snd_timer_stop() at removing slave links. This is a noop, and calling it may confuse readers wrt locking. Further cleanup will follow in a later patch.
Actually we've got reports of use-after-free by syzkaller fuzzer, and this hopefully fixes these issues.
--- sound/core/timer.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
sound/core/timer.c +14 -4
@@ -215,11 +215,13 @@ static void snd_timer_check_master(struct snd_timer_instance *master) slave->slave_id == master->slave_id) {list_move_tail (&slave->open_list, &master->slave_list_head);spin_lock_irq (&slave_active_lock); +spin_lock (&master->timer->lock); slave->master = master; slave->timer = master->timer;if (slave->flags &SNDRV_TIMER_IFLG_RUNNING )list_add_tail (&slave->active_list, &master->slave_active_head); +spin_unlock (&master->timer->lock);spin_unlock_irq (&slave_active_lock); } }
@@ -346,15 +348,18 @@ int snd_timer_close(struct snd_timer_instance *timeri) timer->hw.close) timer->hw.close (timer);/* remove slave links */ +spin_lock_irq (&slave_active_lock); +spin_lock (&timer->lock);list_for_each_entry_safe (slave, tmp, &timeri->slave_list_head, open_list) { -spin_lock_irq (&slave_active_lock); -_snd_timer_stop (slave,1 ,SNDRV_TIMER_EVENT_RESOLUTION );list_move_tail (&slave->open_list, &snd_timer_slave_list); slave->master =NULL ; slave->timer =NULL ; -spin_unlock_irq (&slave_active_lock); +list_del_init (&slave->ack_list); +list_del_init (&slave->active_list); } +spin_unlock (&timer->lock); +spin_unlock_irq (&slave_active_lock);mutex_unlock (®ister_mutex); } out:
@@ -441,9 +446,12 @@ static int snd_timer_start_slave(struct snd_timer_instance *timeri)spin_lock_irqsave (&slave_active_lock, flags); timeri->flags |=SNDRV_TIMER_IFLG_RUNNING ; -if (timeri->master) +if (timeri->master && timeri->timer) { +spin_lock (&timeri->timer->lock);list_add_tail (&timeri->active_list, &timeri->master->slave_active_head); +spin_unlock (&timeri->timer->lock); + }spin_unlock_irqrestore (&slave_active_lock, flags);return 1 ;/* delayed start */ }
@@ -489,6 +497,8 @@ static int _snd_timer_stop(struct snd_timer_instance * timeri,if (!keep_flag) {spin_lock_irqsave (&slave_active_lock, flags); timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING ; +list_del_init (&timeri->ack_list); +list_del_init (&timeri->active_list);spin_unlock_irqrestore (&slave_active_lock, flags); } goto __end;
Signature · 1 line
2.7.0
PATCH 2/2 ALSA: timer: Code cleanup
This is a minor code cleanup without any functional changes:
- Kill keep_flag argument from _snd_timer_stop(), as all callers pass only it false.
- Remove redundant NULL check in _snd_timer_stop().
--- sound/core/timer.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-)
sound/core/timer.c +11 -17
@@ -301,8 +301,7 @@ int snd_timer_open(struct snd_timer_instance **ti,return 0 ; } -static int _snd_timer_stop (struct snd_timer_instance *timeri, -int keep_flag,int event); +static int _snd_timer_stop (struct snd_timer_instance *timeri,int event);/* * close a timer instance
@@ -344,7 +343,7 @@ int snd_timer_close(struct snd_timer_instance *timeri)spin_unlock_irq (&timer->lock);mutex_lock (®ister_mutex);list_del (&timeri->open_list); -if (timer &&list_empty (&timer->open_list_head) && +if (list_empty (&timer->open_list_head) && timer->hw.close) timer->hw.close (timer);/* remove slave links */
@@ -484,8 +483,7 @@ int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)return result; } -static int _snd_timer_stop (struct snd_timer_instance * timeri, -int keep_flag,int event) +static int _snd_timer_stop (struct snd_timer_instance *timeri,int event) {struct snd_timer *timer;unsigned long flags;
@@ -494,13 +492,11 @@ static int _snd_timer_stop(struct snd_timer_instance * timeri,return -ENXIO ;if (timeri->flags &SNDRV_TIMER_IFLG_SLAVE ) { -if (!keep_flag) { -spin_lock_irqsave (&slave_active_lock, flags); - timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING ; -list_del_init (&timeri->ack_list); -list_del_init (&timeri->active_list); -spin_unlock_irqrestore (&slave_active_lock, flags); - } +spin_lock_irqsave (&slave_active_lock, flags); + timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING ; +list_del_init (&timeri->ack_list); +list_del_init (&timeri->active_list); +spin_unlock_irqrestore (&slave_active_lock, flags); goto __end; } timer = timeri->timer;
@@ -521,9 +517,7 @@ static int _snd_timer_stop(struct snd_timer_instance * timeri, } } } -if (!keep_flag) - timeri->flags &= - ~(SNDRV_TIMER_IFLG_RUNNING |SNDRV_TIMER_IFLG_START ); + timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING |SNDRV_TIMER_IFLG_START );spin_unlock_irqrestore (&timer->lock, flags); __end:if (event !=SNDRV_TIMER_EVENT_RESOLUTION )
@@ -542,7 +536,7 @@ int snd_timer_stop(struct snd_timer_instance *timeri)unsigned long flags;int err; - err =_snd_timer_stop (timeri,0 ,SNDRV_TIMER_EVENT_STOP ); + err =_snd_timer_stop (timeri,SNDRV_TIMER_EVENT_STOP );if (err <0 )return err; timer = timeri->timer;
@@ -586,7 +580,7 @@ int snd_timer_continue(struct snd_timer_instance *timeri)*/ int snd_timer_pause (struct snd_timer_instance * timeri) { -return _snd_timer_stop (timeri,0 ,SNDRV_TIMER_EVENT_PAUSE ); +return _snd_timer_stop (timeri,SNDRV_TIMER_EVENT_PAUSE ); }/*
Signature · 1 line
2.7.0