Skip to contentSkip to conversation

ALSA: hda: fix NULL pointer dereference during suspend

PATCH In review Ranjani Sridharan 3 participants 7 messages Jul 28, 2020 – Jul 30, 2020

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

PATCH 1/1 ALSA: hda: fix NULL pointer dereference during suspend

When the ASoC card registration fails and the codec component driver never probes, the codec device is not initialized and therefore memory for codec->wcaps is not allocated. This results in a NULL pointer dereference when the codec driver suspend callback is invoked during system suspend. Fix this by returning without performing any actions during codec suspend/resume if the card was not registered successfully.

---
 sound/pci/hda/hda_codec.c | 8 ++++++++
 1 file changed, 8 insertions(+)
sound/pci/hda/hda_codec.c +8 -0
index 3576e2d8452f..9b1f387d18e5 100644
@@ -2936,6 +2936,10 @@ static int hda_codec_runtime_suspend(struct device *dev)
 	struct hda_codec *codec = dev_to_hda_codec(dev);
 	unsigned int state;
 
+	/* Nothing to do if card registration fails and the component driver never probes */
+	if (!codec->card)
+		return 0;
+
 	cancel_delayed_work_sync(&codec->jackpoll_work);
 	state = hda_call_codec_suspend(codec);
 	if (codec->link_down_at_suspend ||
@@ -2950,6 +2954,10 @@ static int hda_codec_runtime_resume(struct device *dev)
 {
 	struct hda_codec *codec = dev_to_hda_codec(dev);
 
+	/* Nothing to do if card registration fails and the component driver never probes */
+	if (!codec->card)
+		return 0;
+
 	codec_display_power(codec, true);
 	snd_hdac_codec_link_up(&codec->core);
 	hda_call_codec_resume(codec);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 10 lines · Ranjani Sridharan

When the ASoC card registration fails and the codec component driver never probes, the codec device is not initialized and therefore memory for codec->wcaps is not allocated. This results in a NULL pointer dereference when the codec driver suspend callback is invoked during system suspend. Fix this by returning without performing any actions during codec suspend/resume if the card was not registered successfully.

The code changes look OK to apply, but I still wonder how the runtime PM gets invoked even if the device is not instantiated properly?

thanks,

Takashi

Quoted 33 lines
---
 sound/pci/hda/hda_codec.c | 8 ++++++++
 1 file changed, 8 insertions(+)
sound/pci/hda/hda_codec.c +8 -0
index 3576e2d8452f..9b1f387d18e5 100644
@@ -2936,6 +2936,10 @@ static int hda_codec_runtime_suspend(struct device *dev)
 	struct hda_codec *codec = dev_to_hda_codec(dev);
 	unsigned int state;
 
+	/* Nothing to do if card registration fails and the component driver never probes */
+	if (!codec->card)
+		return 0;
+
 	cancel_delayed_work_sync(&codec->jackpoll_work);
 	state = hda_call_codec_suspend(codec);
 	if (codec->link_down_at_suspend ||
@@ -2950,6 +2954,10 @@ static int hda_codec_runtime_resume(struct device *dev)
 {
 	struct hda_codec *codec = dev_to_hda_codec(dev);
 
+	/* Nothing to do if card registration fails and the component driver never probes */
+	if (!codec->card)
+		return 0;
+
 	codec_display_power(codec, true);
 	snd_hdac_codec_link_up(&codec->core);
 	hda_call_codec_resume(codec);
Signature · 1 line
2.25.1
maintainer
#2 lore raw
Quoted 2 lines · Takashi Iwai

The code changes look OK to apply, but I still wonder how the runtime PM gets invoked even if the device is not instantiated properly?

The components are registered and active at the device model level even if they never get incorporated into a card.

#3 lore raw
In reply to Takashi Iwai
Quoted 14 lines · Takashi Iwai
Quoted 12 lines · Ranjani Sridharan

When the ASoC card registration fails and the codec component driver
never probes, the codec device is not initialized and therefore memory for codec->wcaps is not allocated. This results in a NULL pointer
dereference when the codec driver suspend callback is invoked during
system suspend. Fix this by returning without performing any actions
during codec suspend/resume if the card was not registered successfully.

Quoted 7 lines
Quoted 4 lines

Reviewed-by: Pierre-Louis Bossart <
[email protected]>

Quoted 1 line

The code changes look OK to apply, but I still wonder how the runtime PM gets invoked even if the device is not instantiated properly?

Hi Takashi,

Its not runtime PM suspend but rather the system PM suspend callback that is invoked when the system is suspended that ends up callling the the runtime PM callback. So, the sequence is:
hda_codec_pm_suspend()
-> pm_runtime_force_suspend()

          -> hda_codec_runtime_suspend()

Thanks,
Ranjani

#4 lore raw
Quoted 23 lines · Ranjani Sridharan
Quoted 14 lines · Takashi Iwai
Quoted 12 lines · Ranjani Sridharan

When the ASoC card registration fails and the codec component driver
never probes, the codec device is not initialized and therefore memory for codec->wcaps is not allocated. This results in a NULL pointer
dereference when the codec driver suspend callback is invoked during
system suspend. Fix this by returning without performing any actions
during codec suspend/resume if the card was not registered successfully.

Quoted 7 lines
Quoted 4 lines

Reviewed-by: Pierre-Louis Bossart <
[email protected]>

Quoted 1 line

The code changes look OK to apply, but I still wonder how the runtime PM gets invoked even if the device is not instantiated properly?

Quoted 8 lines

Hi Takashi,

Its not runtime PM suspend but rather the system PM suspend callback that is invoked when the system is suspended that ends up callling the the runtime PM callback. So, the sequence is:
hda_codec_pm_suspend()
-> pm_runtime_force_suspend()

          -> hda_codec_runtime_suspend()

OK, but the problem is still same. The basic problem is that the hda_codec_driver_probe() is called for the hda_codec object that hasn't been initialized and bypasses to ext_ops.hdev_attach.

So, we can factor out the fundamental part of snd_hda_codec_device_new() that is irrelevant with the card object and call it in hdac_hda_dev_probe(). Most of the legacy HD-audio codec can work without the card object, including suspend/resume.

But this will be a more intensive surgery, and maybe not much worth for the corner case, so I already applied your patch as is.

thanks,

Takashi

#5 lore raw
Quoted 35 lines · Takashi Iwai
Quoted 23 lines · Ranjani Sridharan
Quoted 14 lines · Takashi Iwai
Quoted 12 lines · Ranjani Sridharan

When the ASoC card registration fails and the codec component driver
never probes, the codec device is not initialized and therefore memory for codec->wcaps is not allocated. This results in a NULL pointer
dereference when the codec driver suspend callback is invoked during
system suspend. Fix this by returning without performing any actions
during codec suspend/resume if the card was not registered successfully.

Quoted 7 lines
Quoted 4 lines

Reviewed-by: Pierre-Louis Bossart <
[email protected]>

Quoted 1 line

The code changes look OK to apply, but I still wonder how the runtime PM gets invoked even if the device is not instantiated properly?

Quoted 8 lines

Hi Takashi,

Its not runtime PM suspend but rather the system PM suspend callback that is invoked when the system is suspended that ends up callling the the runtime PM callback. So, the sequence is:
hda_codec_pm_suspend()
-> pm_runtime_force_suspend()

          -> hda_codec_runtime_suspend()
Quoted 7 lines

OK, but the problem is still same. The basic problem is that the hda_codec_driver_probe() is called for the hda_codec object that hasn't been initialized and bypasses to ext_ops.hdev_attach.

So, we can factor out the fundamental part of snd_hda_codec_device_new() that is irrelevant with the card object and call it in hdac_hda_dev_probe().

I meant something like below (totally untested)

Takashi

---

include/sound/hda_codec.h +5 -4
index f4cc364d837f..1f01e4d6b923 100644
@@ -303,10 +303,11 @@ struct hda_codec {
 /*
  * constructors
  */
-int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
-		      unsigned int codec_addr, struct hda_codec **codecp);
-int snd_hda_codec_device_new(struct hda_bus *bus, struct snd_card *card,
-		      unsigned int codec_addr, struct hda_codec *codec);
+int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
+		      struct hda_codec **codecp);
+int snd_hda_codec_device_init(struct hda_bus *bus, unsigned int codec_addr,
+			      struct hda_codec *codec);
+int snd_hda_codec_assign_card(struct hda_codec *codec);
 int snd_hda_codec_configure(struct hda_codec *codec);
 int snd_hda_codec_update_widgets(struct hda_codec *codec);
 
sound/pci/hda/hda_codec.c +43 -39

156 lines not on this page · raw message

sound/pci/hda/hda_controller.c +3 -2
index 80016b7b6849..e68ca57be30e 100644
@@ -1213,6 +1213,7 @@ EXPORT_SYMBOL_GPL(azx_bus_init);
 int azx_probe_codecs(struct azx *chip, unsigned int max_slots)
 {
 	struct hdac_bus *bus = azx_bus(chip);
+	struct hda_codec *codec;
 	int c, codecs, err;
 
 	codecs = 0;
@@ -1245,10 +1246,10 @@ int azx_probe_codecs(struct azx *chip, unsigned int max_slots)
 	/* Then create codec instances */
 	for (c = 0; c < max_slots; c++) {
 		if ((bus->codec_mask & (1 << c)) & chip->codec_probe_mask) {
-			struct hda_codec *codec;
-			err = snd_hda_codec_new(&chip->bus, chip->card, c, &codec);
+			err = snd_hda_codec_new(&chip->bus, c, &codec);
 			if (err < 0)
 				continue;
+
 			codec->jackpoll_interval = chip->jackpoll_interval;
 			codec->beep_mode = chip->beep_mode;
 			codecs++;
sound/soc/codecs/hdac_hda.c +15 -10
index 473efe9ef998..298d9c46d85d 100644
@@ -417,18 +417,12 @@ static int hdac_hda_codec_probe(struct snd_soc_component *component)
 		snd_hdac_display_power(hdev->bus,
 				       HDA_CODEC_IDX_CONTROLLER, true);
 
-	ret = snd_hda_codec_device_new(hcodec->bus, component->card->snd_card,
-				       hdev->addr, hcodec);
+	hcodec->bus->card = dapm->card->snd_card;
+	ret = snd_hda_codec_assign_card(hcodec);
 	if (ret < 0) {
 		dev_err(&hdev->dev, "failed to create hda codec %d\n", ret);
 		goto error_no_pm;
 	}
-	/*
-	 * Overwrite type to HDA_DEV_ASOC since it is a ASoC driver
-	 * hda_codec.c will check this flag to determine if unregister
-	 * device is needed.
-	 */
-	hdev->type = HDA_DEV_ASOC;
 
 	/*
 	 * snd_hda_codec_device_new decrements the usage count so call get pm
@@ -436,8 +430,6 @@ static int hdac_hda_codec_probe(struct snd_soc_component *component)
 	 */
 	pm_runtime_get_noresume(&hdev->dev);
 
-	hcodec->bus->card = dapm->card->snd_card;
-
 	ret = snd_hda_codec_set_name(hcodec, hcodec->preset->name);
 	if (ret < 0) {
 		dev_err(&hdev->dev, "name failed %s\n", hcodec->preset->name);
@@ -574,6 +566,7 @@ static int hdac_hda_dev_probe(struct hdac_device *hdev)
 {
 	struct hdac_ext_link *hlink;
 	struct hdac_hda_priv *hda_pvt;
+	struct hda_codec *hcodec;
 	int ret;
 
 	/* hold the ref while we probe */
@@ -588,6 +581,18 @@ static int hdac_hda_dev_probe(struct hdac_device *hdev)
 	if (!hda_pvt)
 		return -ENOMEM;
 
+	ret = snd_hda_codec_device_init(hcodec->bus, hdev->addr,
+					&hda_pvt->codec);
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * Overwrite type to HDA_DEV_ASOC since it is a ASoC driver
+	 * hda_codec.c will check this flag to determine if unregister
+	 * device is needed.
+	 */
+	hdev->type = HDA_DEV_ASOC;
+
 	/* ASoC specific initialization */
 	ret = devm_snd_soc_register_component(&hdev->dev,
 					 &hdac_hda_codec, hdac_hda_dais,
#6 lore raw
Quoted 45 lines · Takashi Iwai
Quoted 43 lines · Takashi Iwai
Quoted 28 lines · Ranjani Sridharan
Quoted 21 lines · Takashi Iwai
Quoted 14 lines · Ranjani Sridharan

When the ASoC card registration fails and the codec component driver
never probes, the codec device is not initialized and therefore
memory for codec->wcaps is not allocated. This results in a NULL
pointer
dereference when the codec driver suspend callback is invoked during
system suspend. Fix this by returning without performing any actions
during codec suspend/resume if the card was not registered successfully.

Quoted 4 lines

Reviewed-by: Pierre-Louis Bossart <
[email protected]>
Signed-off-by: Ranjani Sridharan <
[email protected]

Quoted 4 lines

The code changes look OK to apply, but I still wonder how the runtime
PM gets invoked even if the device is not instantiated properly?

Hi Takashi,

Quoted 8 lines

Its not runtime PM suspend but rather the system PM suspend callback
that is invoked when the system is suspended that ends up callling the
the runtime PM callback. So, the sequence is:
hda_codec_pm_suspend()
-> pm_runtime_force_suspend()

          -> hda_codec_runtime_suspend()

OK, but the problem is still same. The basic problem is that the hda_codec_driver_probe() is called for the hda_codec object that hasn't been initialized and bypasses to ext_ops.hdev_attach.

Quoted 6 lines
Quoted 4 lines

So, we can factor out the fundamental part of snd_hda_codec_device_new() that is irrelevant with the card object and
call it in hdac_hda_dev_probe().

I meant something like below (totally untested)

Thanks, Takashi. I can test this out and get back to you by next week.

Thanks,
Ranjani