Skip to contentSkip to conversation

ALSA: aoa: i2sbus: serialize codec_list and pcm check-and-free sequences

PATCH New Hanxiang Xu 2 participants 2 messages Dec 2

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: aoa: i2sbus: serialize codec_list and pcm check-and-free sequences

i2sbus_detach_codec() manipulates dev->codec_list without any lock and ends in an unlocked check-then-free:
"if (list_empty(&dev->codec_list) && dev->pcm) snd_device_free(dev->pcm->card, dev->pcm)". The callers hold device locks that do not serialize against each other: codec drivers detach from i2c driver context (each holding its own i2c client lock) while bus removal runs from soundbus/macio context, and a bus can host two codecs (MAX_CODECS_PER_BUS). Two concurrent codec detaches therefore both pass the dev->pcm check before either clears it and both run snd_device_free() on the same pcm, double-freeing it (the second call also walks card->devices while the first is freeing the snd_device wrapper).

Serialize attach/detach with a mutex and claim dev->pcm before calling snd_device_free() so a concurrent detach cannot re-enter the free.

---
 sound/aoa/soundbus/i2sbus/pcm.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
sound/aoa/soundbus/i2sbus/pcm.c +19 -3
index 63004ece94f9333eaacdeb9ed2fb7298743d2e45..3fbc34a247988d7a0de7edb7dcd16b266880653d 100644
@@ -862,6 +862,8 @@
 	struct i2sbus_dev *i2sdev = soundbus_dev_to_i2sbus_dev(dev);
 	struct codec_info_item *cii;
 
+	guard(mutex)(&i2sbus_pcm_mutex);
+
 	if (!dev->pcmname || dev->pcmid == -1) {
 		printk(KERN_ERR "i2sbus: pcm name and id must be set!\n");
 		return -EINVAL;
@@ -1018,10 +1020,18 @@ static void i2sbus_private_free(struct snd_pcm *pcm)
 	return err;
 }
 
+/*
+ * codec attach/detach (i2c driver context) and bus removal
+ * (soundbus/macio context) hold different device locks; serialize the
+ * codec_list and dev->pcm sequences between them.
+ */
+static DEFINE_MUTEX(i2sbus_pcm_mutex);
+
 void i2sbus_detach_codec(struct soundbus_dev *dev, void *data)
 {
 	struct codec_info_item *cii = NULL, *i;
 
+	guard(mutex)(&i2sbus_pcm_mutex);
 	list_for_each_entry(i, &dev->codec_list, list) {
 		if (i->codec_data == data) {
 			cii = i;
@@ -1035,7 +1045,13 @@ void i2sbus_detach_codec(struct soundbus_dev *dev, void *data)
 	}
 	/* no more codecs, but still a pcm? */
 	if (list_empty(&dev->codec_list) && dev->pcm) {
+		struct snd_pcm *pcm = dev->pcm;
+		struct snd_card *card = pcm->card;
+
+		/* claim before freeing: a concurrent detach_codec() must not
+		 * re-enter snd_device_free() on the same pcm */
+		dev->pcm = NULL;
 		/* the actual cleanup is done by the callback above! */
-		snd_device_free(dev->pcm->card, dev->pcm);
-	}
-}
+		snd_device_free(card, pcm);
+	}
+}
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] ALSA: aoa: i2sbus: undeclared i2sbus_pcm_mutex in i2sbus_attach_codec() - [Low] ALSA: aoa: i2sbus…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] ALSA: aoa: i2sbus: undeclared i2sbus_pcm_mutex in i2sbus_attach_codec()
- [Low] ALSA: aoa: i2sbus: missing Fixes trailer on bug-fix commit --

commit 0568c140cfaf58b5209a2f736b6288cd639d9e34
Author: Hanxiang Xu <[email protected]>

Quoted 15 lines

ALSA: aoa: i2sbus: serialize codec_list and pcm check-and-free sequences

i2sbus_detach_codec() manipulates dev->codec_list without any lock and ends in an unlocked check-then-free:
"if (list_empty(&dev->codec_list) && dev->pcm) snd_device_free(dev->pcm->card, dev->pcm)". The callers hold device locks that do not serialize against each other: codec drivers detach from i2c driver context (each holding its own i2c client lock) while bus removal runs from soundbus/macio context, and a bus can host two codecs (MAX_CODECS_PER_BUS). Two concurrent codec detaches therefore both pass the dev->pcm check before either clears it and both run snd_device_free() on the same pcm, double-freeing it (the second call also walks card->devices while the first is freeing the snd_device wrapper).

Quoted 6 lines

Serialize attach/detach with a mutex and claim dev->pcm before calling snd_device_free() so a concurrent detach cannot re-enter the free.

[Severity: Low]
Should this bug fix include a Fixes: tag?

Fixes f3d9478b2ce4 ("[ALSA] snd-aoa: add snd-aoa")
Quoted 14 lines
sound/aoa/soundbus/i2sbus/pcm.c +2 -0
index 63004ece94f93..3fbc34a247988 100644
@@ -862,6 +862,8 @@ i2sbus_attach_codec(struct soundbus_dev *dev, struct snd_card *card,
 	struct i2sbus_dev *i2sdev = soundbus_dev_to_i2sbus_dev(dev);
 	struct codec_info_item *cii;
 
+	guard(mutex)(&i2sbus_pcm_mutex);
+
 	if (!dev->pcmname || dev->pcmid == -1) {
 		printk(KERN_ERR "i2sbus: pcm name and id must be set!\n");
 		return -EINVAL;

sound/aoa/soundbus/i2sbus/pcm.c i2sbus_attach_codec(struct soundbus_dev *dev, struct snd_card *card, · line 1028

Quoted 9 lines
sound/aoa/soundbus/i2sbus/pcm.c +6 -0
@@ -1018,10 +1020,18 @@ i2sbus_attach_codec(struct soundbus_dev *dev, struct snd_card *card,
 	return err;
 }
 
+/*
+ * codec attach/detach (i2c driver context) and bus removal
+ * (soundbus/macio context) hold different device locks; serialize the
+ * codec_list and dev->pcm sequences between them.
+ */
+static DEFINE_MUTEX(i2sbus_pcm_mutex);

[Severity: High]
Does this fail to build because i2sbus_pcm_mutex is defined below i2sbus_attach_codec(), where it is already used without a prior declaration?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1