ALSA: some race fixes
cover ALSA: some race fixes
Hi,
syzkaller hilariously spotted some possible races via its usb fuzzing. I thought it were specific to USB-audio stuff, but no it's in the ALSA core side. So below is the resultant fixes.
Takashi
===
Takashi Iwai (2):
ALSA: info: Fix racy addition/deletion of nodes
ALSA: core: Fix card races between register and disconnect
sound/core/info.c | 12 ++++++++++-- sound/core/init.c | 18 +++++++++--------- 2 files changed, 19 insertions(+), 11 deletions(-)
Signature · 1 line
2.16.4
PATCH 1/2 ALSA: info: Fix racy addition/deletion of nodes
The ALSA proc helper manages the child nodes in a linked list, but its addition and deletion is done without any lock. This leads to a corruption if they are operated concurrently. Usually this isn't a problem because the proc entries are added sequentially in the driver probe procedure itself. But the card registrations are done often asynchronously, and the crash could be actually reproduced with syzkaller.
This patch papers over it by protecting the link addition and deletion with the parent's mutex. There is "access" mutex that is used for the file access, and this can be reused for this purpose as well.
--- sound/core/info.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
sound/core/info.c +10 -2
@@ -713,8 +713,11 @@ snd_info_create_entry(const char *name, struct snd_info_entry *parent,INIT_LIST_HEAD (&entry->list); entry->parent = parent; entry->module = module; -if (parent) +if (parent) { +mutex_lock (&parent->access);list_add_tail (&entry->list, &parent->children); +mutex_unlock (&parent->access); + }return entry; }
@@ -792,7 +795,12 @@ void snd_info_free_entry(struct snd_info_entry * entry)list_for_each_entry_safe (p, n, &entry->children, list)snd_info_free_entry (p); -list_del (&entry->list); + p = entry->parent; +if (p) { +mutex_lock (&p->access); +list_del (&entry->list); +mutex_unlock (&p->access); + }kfree (entry->name);if (entry->private_free) entry->private_free (entry);
Signature · 1 line
2.16.4
PATCH 2/2 ALSA: core: Fix card races between register and disconnect
In reply to Takashi IwaiThere is a small race window in the card disconnection code that allows the registration of another card with the very same card id. This leads to a warning in procfs creation as caught by syzkaller.
The problem is that we delete snd_cards and snd_cards_lock entries at the very beginning of the disconnection procedure. This makes the slot available to be assigned for another card object while the disconnection procedure is being processed. Then it becomes possible to issue a procfs registration with the existing file name although we check the conflict beforehand.
The fix is simply to move the snd_cards and snd_cards_lock clearances at the end of the disconnection procedure. The references to these entries are merely either from the global proc files like /proc/asound/cards or from the card registration / disconnection, so it should be fine to shift at the very end.
--- sound/core/init.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
sound/core/init.c +9 -9
@@ -382,14 +382,7 @@ int snd_card_disconnect(struct snd_card *card) card->shutdown =1 ;spin_unlock (&card->files_lock); -/* phase 1: disable fops (user space) operations for ALSA API */ -mutex_lock (&snd_card_mutex); - snd_cards[card->number] =NULL ; -clear_bit (card->number, snd_cards_lock); -mutex_unlock (&snd_card_mutex); - -/* phase 2: replace file->f_op with special dummy operations */ - +/* replace file->f_op with special dummy operations */ spin_lock (&card->files_lock);list_for_each_entry (mfile, &card->files_list, list) {/* it's critical part, use endless loop */
@@ -405,7 +398,7 @@ int snd_card_disconnect(struct snd_card *card) }spin_unlock (&card->files_lock); -/* phase 3: notify all connected devices about disconnection */ +/* notify all connected devices about disconnection */ /* at this point, they cannot respond to any calls except release() */ #if IS_ENABLED (CONFIG_SND_MIXER_OSS )
@@ -421,6 +414,13 @@ int snd_card_disconnect(struct snd_card *card)device_del (&card->card_dev); card->registered = false; } + +/* disable fops (user space) operations for ALSA API */ +mutex_lock (&snd_card_mutex); + snd_cards[card->number] =NULL ; +clear_bit (card->number, snd_cards_lock); +mutex_unlock (&snd_card_mutex); +#ifdef CONFIG_PM wake_up (&card->power_sleep);#endif
Signature · 1 line
2.16.4