Skip to content

Commit 2a3f722

Browse files
committed
ALSA: core: Fix card races between register and disconnect
There 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. Reported-by: syzbot+48df349490c36f9f54ab@syzkaller.appspotmail.com Cc: <stable@vger.kernel.org> Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 8c2f870 commit 2a3f722

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

‎sound/core/init.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -382,14 +382,7 @@ int snd_card_disconnect(struct snd_card *card)
382382
card->shutdown = 1;
383383
spin_unlock(&card->files_lock);
384384

385-
/* phase 1: disable fops (user space) operations for ALSA API */
386-
mutex_lock(&snd_card_mutex);
387-
snd_cards[card->number] = NULL;
388-
clear_bit(card->number, snd_cards_lock);
389-
mutex_unlock(&snd_card_mutex);
390-
391-
/* phase 2: replace file->f_op with special dummy operations */
392-
385+
/* replace file->f_op with special dummy operations */
393386
spin_lock(&card->files_lock);
394387
list_for_each_entry(mfile, &card->files_list, list) {
395388
/* it's critical part, use endless loop */
@@ -405,7 +398,7 @@ int snd_card_disconnect(struct snd_card *card)
405398
}
406399
spin_unlock(&card->files_lock);
407400

408-
/* phase 3: notify all connected devices about disconnection */
401+
/* notify all connected devices about disconnection */
409402
/* at this point, they cannot respond to any calls except release() */
410403

411404
#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
@@ -421,6 +414,13 @@ int snd_card_disconnect(struct snd_card *card)
421414
device_del(&card->card_dev);
422415
card->registered = false;
423416
}
417+
418+
/* disable fops (user space) operations for ALSA API */
419+
mutex_lock(&snd_card_mutex);
420+
snd_cards[card->number] = NULL;
421+
clear_bit(card->number, snd_cards_lock);
422+
mutex_unlock(&snd_card_mutex);
423+
424424
#ifdef CONFIG_PM
425425
wake_up(&card->power_sleep);
426426
#endif

0 commit comments

Comments
 (0)