ALSA: timer: Forcibly close timer instances at closing
PATCH 1/2 ALSA: timer: Forcibly close timer instances at closing
When snd_timer object is freed via snd_timer_free() and still pending snd_timer_instance objects are assigned to the timer object, it tries to unlink all instances and just set NULL to each ti->timer, then releases the resources immediately. The problem is, however, when there are slave timer instances that are associated with a master instance linked to this timer: namely, those slave instances still point to the freed timer object although the master instance is unlinked, which may lead to user-after-free. The bug can be easily triggered particularly when a new userspace-driven timers (CONFIG_SND_UTIMER) is involved, since it can create and delete the timer object via a simple file open/close, while the other applications may keep accessing to that timer.
This patch is an attempt to paper over the problem above: now instead of just unlinking, call snd_timer_close[_locked]() forcibly for each pending timer instance, so that all assigned slave timer instances are properly detached, too. Since snd_timer_close() might be called later by the driver that created that instance, the check of SNDRV_TIMER_IFLG_DEAD is added at the beginning, too.
--- sound/core/timer.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
sound/core/timer.c +9 -7
@@ -430,6 +430,8 @@ static void snd_timer_close_locked(struct snd_timer_instance *timeri,if (timer) {guard (spinlock_irq)(&timer->lock); +if (timeri->flags &SNDRV_TIMER_IFLG_DEAD ) +return ;/* already closed */ timeri->flags |=SNDRV_TIMER_IFLG_DEAD ; }
@@ -975,18 +977,18 @@ EXPORT_SYMBOL(snd_timer_new);static int snd_timer_free (struct snd_timer *timer) { +struct snd_timer_instance *ti, *n; +if (!timer)return 0 ;guard (mutex)(®ister_mutex);if (!list_empty (&timer->open_list_head)) { -struct list_head *p, *n; -struct snd_timer_instance *ti; -pr_warn ("ALSA: timer %p is busy?\n" , timer); -list_for_each_safe (p, n, &timer->open_list_head) { -list_del_init (p); - ti = list_entry(p, struct snd_timer_instance, open_list); - ti->timer =NULL ; +list_for_each_entry_safe (ti, n, &timer->open_list_head, open_list) { +struct device *card_dev_to_put =NULL ; + +snd_timer_close_locked (ti, &card_dev_to_put); +put_device (card_dev_to_put); } }list_del (&timer->device_list);
Signature · 1 line
2.54.0
PATCH 2/2 ALSA: timer: Fix UAF at snd_timer_user_params()
At releasing a timer object, e.g. when a userspace timer (CONFIG_SND_UTIMER) gets closed and snd_timer_free() is called, it tries to detach the timer instances and release the resources. However, it's still possible that other in-flight tasks are holding the timer instance where the to-be-deleted timer object is associated, and this may lead to racy accesses.
Fortunately, most of ioctls dealing with the timer instance list already have the protection with register_mutex, and this also avoids such races. But, SNDRV_TIMER_IOCTL_PARAMS isn't protected, hence the concurrent ioctl may lead to use-after-free.
This patch just adds the guard with register_mutex to protect snd_timer_user_params() for covering the code path as a quick workaround. It's no hot-path but rather a rarely issued ioctl, so the performance penalty doesn't matter.
--- sound/core/timer.c | 1 + 1 file changed, 1 insertion(+)
sound/core/timer.c +1 -0
@@ -1811,6 +1811,7 @@ static int snd_timer_user_params(struct file *file,struct snd_timer *t;int err; +guard (mutex)(®ister_mutex); tu = file->private_data;if (!tu->timeri)return -EBADFD ;
Signature · 1 line
2.54.0