Skip to contentSkip to conversation

ALSA: check snd_ctl_new1() return before dereferencing

PATCH In review Ruoyu Wang 2 participants 10 messages Jun 7 Sound

lore
Share & tools
lore

Ruoyu Wang

author
#0 lore raw

cover ALSA: check snd_ctl_new1() return before dereferencing

Several ALSA drivers dereference the pointer returned by snd_ctl_new1() to adjust id.device, id.index, or store bookkeeping pointers before calling snd_ctl_add(). snd_ctl_new1() can return NULL on allocation failure, making these writes a NULL pointer dereference.

This series adds NULL checks to all affected call sites found by static analysis.

Ruoyu Wang (8):
ALSA: es18xx: check control allocation before private data setup
ALSA: gus: check PCM volume control allocation
ALSA: cmipci: check S/PDIF control allocations
ALSA: es1938: check control allocation before private data setup
ALSA: ice1712: aureon: check CS8415 control allocation
ALSA: ice1712: check pro S/PDIF control allocations
ALSA: ice1724: check S/PDIF control allocations
ALSA: ymfpci: check S/PDIF control allocations

 sound/isa/es18xx.c              | 4 ++++
 sound/isa/gus/gus_pcm.c        | 2 ++
 sound/pci/cmipci.c             | 6 ++++++
 sound/pci/es1938.c             | 2 ++
 sound/pci/ice1712/aureon.c     | 2 ++
 sound/pci/ice1712/ice1712.c    | 8 ++++++++
 sound/pci/ice1712/ice1724.c    | 6 ++++++
 sound/pci/ymfpci/ymfpci_main.c | 6 ++++++
 8 files changed, 36 insertions(+)

--
2.34.1

author
#1 lore raw

PATCH 1/1 ALSA: es18xx: check control allocation before private data setup

snd_es18xx_mixer() creates controls with snd_ctl_new1() and then stores hardware-volume bookkeeping in the returned control before calling snd_ctl_add(). snd_ctl_new1() can return NULL on allocation failure, so those private_free writes can dereference NULL before snd_ctl_add() gets a chance to reject the missing control.

Check the returned control pointers before using them and return -ENOMEM on allocation failure.

---
 sound/isa/es18xx.c | 4 ++++
 1 file changed, 4 insertions(+)
sound/isa/es18xx.c +4 -0
index 1da7b400a17b6..2f454f7797ce2 100644
@@ -1762,6 +1762,8 @@ static int snd_es18xx_mixer(struct snd_card *card)
 	for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
 		struct snd_kcontrol *kctl;
 		kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
+		if (!kctl)
+			return -ENOMEM;
 		if (chip->caps & ES18XX_HWV) {
 			switch (idx) {
 			case 0:
@@ -1823,6 +1825,8 @@ static int snd_es18xx_mixer(struct snd_card *card)
 		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
 			struct snd_kcontrol *kctl;
 			kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
+			if (!kctl)
+				return -ENOMEM;
 			if (idx == 0)
 				chip->hw_volume = kctl;
 			else
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 1/1 ALSA: gus: check PCM volume control allocation

In reply to Ruoyu Wang

snd_gf1_pcm_new() creates a PCM volume control and then updates kctl->id.index before adding it to the card. snd_ctl_new1() can return NULL on allocation failure, so the id update can dereference NULL before snd_ctl_add() can handle the error.

Return -ENOMEM when the control allocation fails.

---
 sound/isa/gus/gus_pcm.c | 2 ++
 1 file changed, 2 insertions(+)
sound/isa/gus/gus_pcm.c +2 -0
index a0757e1ede465..08ccb4d80adeb 100644
@@ -851,6 +851,8 @@ int snd_gf1_pcm_new(struct snd_gus_card *gus, int pcm_dev, int control_index)
 		kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control1, gus);
 	else
 		kctl = snd_ctl_new1(&snd_gf1_pcm_volume_control, gus);
+	if (!kctl)
+		return -ENOMEM;
 	kctl->id.index = control_index;
 	err = snd_ctl_add(card, kctl);
 	if (err < 0)
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 1/1 ALSA: cmipci: check S/PDIF control allocations

In reply to Ruoyu Wang

snd_cmipci_mixer_new() creates several S/PDIF controls and adjusts kctl->id.device before adding each control to the card. If snd_ctl_new1() returns NULL, the id update dereferences NULL before snd_ctl_add() can reject the missing control.

Check each S/PDIF control allocation before setting the device id.

---
 sound/pci/cmipci.c | 6 ++++++
 1 file changed, 6 insertions(+)
sound/pci/cmipci.c +6 -0
index cd73b68336394..ff4bfbf94b81b 100644
@@ -2637,16 +2637,22 @@ static int snd_cmipci_mixer_new(struct cmipci *cm, int pcm_spdif_device)
 		}
 		if (cm->can_ac3_hw) {
 			kctl = snd_ctl_new1(&snd_cmipci_spdif_default, cm);
+			if (!kctl)
+				return -ENOMEM;
 			kctl->id.device = pcm_spdif_device;
 			err = snd_ctl_add(card, kctl);
 			if (err < 0)
 				return err;
 			kctl = snd_ctl_new1(&snd_cmipci_spdif_mask, cm);
+			if (!kctl)
+				return -ENOMEM;
 			kctl->id.device = pcm_spdif_device;
 			err = snd_ctl_add(card, kctl);
 			if (err < 0)
 				return err;
 			kctl = snd_ctl_new1(&snd_cmipci_spdif_stream, cm);
+			if (!kctl)
+				return -ENOMEM;
 			kctl->id.device = pcm_spdif_device;
 			err = snd_ctl_add(card, kctl);
 			if (err < 0)
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH 1/1 ALSA: es1938: check control allocation before private data setup

In reply to Ruoyu Wang

snd_es1938_mixer() creates mixer controls with snd_ctl_new1() and then stores hardware-volume pointers and private_free hooks in the returned controls before calling snd_ctl_add(). snd_ctl_new1() can return NULL on allocation failure, so these setup writes can dereference NULL.

Check the allocation result before using the control pointer.

---
 sound/pci/es1938.c | 2 ++
 1 file changed, 2 insertions(+)
sound/pci/es1938.c +2 -0
index 280125eff3624..b6dcb721fffce 100644
@@ -1655,6 +1655,8 @@ static int snd_es1938_mixer(struct es1938 *chip)
 	for (idx = 0; idx < ARRAY_SIZE(snd_es1938_controls); idx++) {
 		struct snd_kcontrol *kctl;
 		kctl = snd_ctl_new1(&snd_es1938_controls[idx], chip);
+		if (!kctl)
+			return -ENOMEM;
 		switch (idx) {
 			case 0:
 				chip->master_volume = kctl;
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH 1/1 ALSA: ice1712: aureon: check CS8415 control allocation

In reply to Ruoyu Wang

aureon_add_controls() creates CS8415 controls and, for later entries, adjusts kctl->id.device before adding the control. snd_ctl_new1() can return NULL, so the device-id update can dereference NULL before the control core sees the pointer.

Return -ENOMEM when the control allocation fails.

---
 sound/pci/ice1712/aureon.c | 2 ++
 1 file changed, 2 insertions(+)
sound/pci/ice1712/aureon.c +2 -0
index 1191a2686dfd2..d6abff2978f3d 100644
@@ -1891,6 +1891,8 @@ static int aureon_add_controls(struct snd_ice1712 *ice)
 			for (i = 0; i < ARRAY_SIZE(cs8415_controls); i++) {
 				struct snd_kcontrol *kctl;
 				kctl = snd_ctl_new1(&cs8415_controls[i], ice);
+				if (!kctl)
+					return -ENOMEM;
 				if (i > 1)
 					kctl->id.device = ice->pcm->device;
 				err = snd_ctl_add(ice->card, kctl);
Signature · 1 line
2.34.1
author
#6 lore raw

PATCH 1/1 ALSA: ice1712: check pro S/PDIF control allocations

In reply to Ruoyu Wang

snd_ice1712_spdif_build_controls() creates pro S/PDIF controls and then sets kctl->id.device before calling snd_ctl_add(). snd_ctl_new1() can return NULL on allocation failure, making the device-id write a NULL pointer dereference.

Check each control allocation before using the returned pointer.

---
 sound/pci/ice1712/ice1712.c | 8 ++++++++
 1 file changed, 8 insertions(+)
sound/pci/ice1712/ice1712.c +8 -0
index 1e39b985bef26..4cec56769c0f6 100644
@@ -2346,21 +2346,29 @@ int snd_ice1712_spdif_build_controls(struct snd_ice1712 *ice)
 	if (snd_BUG_ON(!ice->pcm_pro))
 		return -EIO;
 	kctl = snd_ctl_new1(&snd_ice1712_spdif_default, ice);
+	if (!kctl)
+		return -ENOMEM;
 	kctl->id.device = ice->pcm_pro->device;
 	err = snd_ctl_add(ice->card, kctl);
 	if (err < 0)
 		return err;
 	kctl = snd_ctl_new1(&snd_ice1712_spdif_maskc, ice);
+	if (!kctl)
+		return -ENOMEM;
 	kctl->id.device = ice->pcm_pro->device;
 	err = snd_ctl_add(ice->card, kctl);
 	if (err < 0)
 		return err;
 	kctl = snd_ctl_new1(&snd_ice1712_spdif_maskp, ice);
+	if (!kctl)
+		return -ENOMEM;
 	kctl->id.device = ice->pcm_pro->device;
 	err = snd_ctl_add(ice->card, kctl);
 	if (err < 0)
 		return err;
 	kctl = snd_ctl_new1(&snd_ice1712_spdif_stream, ice);
+	if (!kctl)
+		return -ENOMEM;
 	kctl->id.device = ice->pcm_pro->device;
 	err = snd_ctl_add(ice->card, kctl);
 	if (err < 0)
Signature · 1 line
2.34.1
author
#7 lore raw

PATCH 1/1 ALSA: ice1724: check S/PDIF control allocations

In reply to Ruoyu Wang

snd_vt1724_spdif_build_controls() creates S/PDIF controls and then sets kctl->id.device before calling snd_ctl_add(). snd_ctl_new1() can return NULL on allocation failure, so these device-id writes can dereference NULL.

Check each live control allocation before using the returned pointer.

---
 sound/pci/ice1712/ice1724.c | 6 ++++++
 1 file changed, 6 insertions(+)
sound/pci/ice1712/ice1724.c +6 -0
index 65bf48647d089..b16c84983b81a 100644
@@ -2379,16 +2379,22 @@ static int snd_vt1724_spdif_build_controls(struct snd_ice1712 *ice)
 		return err;
 
 	kctl = snd_ctl_new1(&snd_vt1724_spdif_default, ice);
+	if (!kctl)
+		return -ENOMEM;
 	kctl->id.device = ice->pcm->device;
 	err = snd_ctl_add(ice->card, kctl);
 	if (err < 0)
 		return err;
 	kctl = snd_ctl_new1(&snd_vt1724_spdif_maskc, ice);
+	if (!kctl)
+		return -ENOMEM;
 	kctl->id.device = ice->pcm->device;
 	err = snd_ctl_add(ice->card, kctl);
 	if (err < 0)
 		return err;
 	kctl = snd_ctl_new1(&snd_vt1724_spdif_maskp, ice);
+	if (!kctl)
+		return -ENOMEM;
 	kctl->id.device = ice->pcm->device;
 	err = snd_ctl_add(ice->card, kctl);
 	if (err < 0)
Signature · 1 line
2.34.1
author
#8 lore raw

PATCH 1/1 ALSA: ymfpci: check S/PDIF control allocations

In reply to Ruoyu Wang

snd_ymfpci_mixer() creates S/PDIF controls and then sets kctl->id.device before adding each control. snd_ctl_new1() can return NULL on allocation failure, and snd_ctl_add() cannot protect these earlier id writes.

Check the control allocation results before using them.

---
 sound/pci/ymfpci/ymfpci_main.c | 6 ++++++
 1 file changed, 6 insertions(+)
sound/pci/ymfpci/ymfpci_main.c +6 -0
index b9a09568afc9e..2ccb976e68e0b 100644
@@ -1781,16 +1781,22 @@ int snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
 	if (snd_BUG_ON(!chip->pcm_spdif))
 		return -ENXIO;
 	kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip);
+	if (!kctl)
+		return -ENOMEM;
 	kctl->id.device = chip->pcm_spdif->device;
 	err = snd_ctl_add(chip->card, kctl);
 	if (err < 0)
 		return err;
 	kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip);
+	if (!kctl)
+		return -ENOMEM;
 	kctl->id.device = chip->pcm_spdif->device;
 	err = snd_ctl_add(chip->card, kctl);
 	if (err < 0)
 		return err;
 	kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip);
+	if (!kctl)
+		return -ENOMEM;
 	kctl->id.device = chip->pcm_spdif->device;
 	err = snd_ctl_add(chip->card, kctl);
 	if (err < 0)
Signature · 1 line
2.34.1
#9 lore raw
In reply to Ruoyu Wang
Quoted 9 lines · Ruoyu Wang

Several ALSA drivers dereference the pointer returned by snd_ctl_new1() to adjust id.device, id.index, or store bookkeeping pointers before calling snd_ctl_add(). snd_ctl_new1() can return NULL on allocation failure, making these writes a NULL pointer dereference.

This series adds NULL checks to all affected call sites found by static analysis.

Quoted 9 lines

Ruoyu Wang (8):
ALSA: es18xx: check control allocation before private data setup
ALSA: gus: check PCM volume control allocation
ALSA: cmipci: check S/PDIF control allocations
ALSA: es1938: check control allocation before private data setup
ALSA: ice1712: aureon: check CS8415 control allocation
ALSA: ice1712: check pro S/PDIF control allocations
ALSA: ice1724: check S/PDIF control allocations
ALSA: ymfpci: check S/PDIF control allocations

Some of them seems to have been already applied or conflicting. Please rebase on the latest for-next branch of sound.git tree and resubmit.

thanks,

Takashi