Skip to contentSkip to conversation

ALSA: i2c: ak4xxx-adda: Fix a possible null pointer dereference in build_adc_controls()

PATCH Superseded Jia-Ju Bai 2 participants 3 messages Jul 25, 2019 – Jul 26, 2019 Sound

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH 1/1 ALSA: i2c: ak4xxx-adda: Fix a possible null pointer dereference in build_adc_controls()

In build_adc_controls(), there is an if statement on line 773 to check whether ak->adc_info is NULL:

	if (! ak->adc_info || 
		! ak->adc_info[mixer_ch].switch_name)

When ak->adc_info is NULL, it is used on line 792:

    knew.name = ak->adc_info[mixer_ch].selector_name;

Thus, a possible null-pointer dereference may occur.

To fix this bug, referring to lines 773 and 774, ak->adc_info and ak->adc_info[mixer_ch].selector_name are checked before being used.

This bug is found by a static analysis tool STCheck written by us.

---
 sound/i2c/other/ak4xxx-adda.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
sound/i2c/other/ak4xxx-adda.c +4 -2
index 5f59316f982a..9a891470e84a 100644
@@ -775,11 +775,13 @@ static int build_adc_controls(struct snd_akm4xxx *ak)
 				return err;
 
 			memset(&knew, 0, sizeof(knew));
-			knew.name = ak->adc_info[mixer_ch].selector_name;
-			if (!knew.name) {
+			if (! ak->adc_info ||
+				! ak->adc_info[mixer_ch].selector_name) {
 				knew.name = "Capture Channel";
 				knew.index = mixer_ch + ak->idx_offset * 2;
 			}
+			else
+				knew.name = ak->adc_info[mixer_ch].selector_name;
 
 			knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
 			knew.info = ak4xxx_capture_source_info;
Signature · 1 line
2.17.0
#1 lore raw
Quoted 31 lines · Jia-Ju Bai

In build_adc_controls(), there is an if statement on line 773 to check whether ak->adc_info is NULL:

	if (! ak->adc_info || 
		! ak->adc_info[mixer_ch].switch_name)

When ak->adc_info is NULL, it is used on line 792:

    knew.name = ak->adc_info[mixer_ch].selector_name;

Thus, a possible null-pointer dereference may occur.

To fix this bug, referring to lines 773 and 774, ak->adc_info and ak->adc_info[mixer_ch].selector_name are checked before being used.

This bug is found by a static analysis tool STCheck written by us.

---
 sound/i2c/other/ak4xxx-adda.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
sound/i2c/other/ak4xxx-adda.c +0 -2
index 5f59316f982a..9a891470e84a 100644
@@ -775,11 +775,13 @@ static int build_adc_controls(struct snd_akm4xxx *ak)
 				return err;
 
 			memset(&knew, 0, sizeof(knew));
-			knew.name = ak->adc_info[mixer_ch].selector_name;
-			if (!knew.name) {

sound/i2c/other/ak4xxx-adda.c static int build_adc_controls(struct snd_akm4xxx *ak) · line 787

Quoted 10 lines
sound/i2c/other/ak4xxx-adda.c +4 -0
+			if (! ak->adc_info ||
+				! ak->adc_info[mixer_ch].selector_name) {
 				knew.name = "Capture Channel";
 				knew.index = mixer_ch + ak->idx_offset * 2;
 			}
+			else
+				knew.name = ak->adc_info[mixer_ch].selector_name;
 
 			knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
 			knew.info = ak4xxx_capture_source_info;

The code change itself looks good, but please follow the standard coding style. In short: please run checkpatch.pl, fix errors (some warnings may be ignored) and resubmit.

thanks,

Takashi

author
#2 lore raw
Quoted 40 lines · Takashi Iwai
Quoted 38 lines · Jia-Ju Bai

In build_adc_controls(), there is an if statement on line 773 to check whether ak->adc_info is NULL:

	if (! ak->adc_info ||
		! ak->adc_info[mixer_ch].switch_name)

When ak->adc_info is NULL, it is used on line 792:

     knew.name = ak->adc_info[mixer_ch].selector_name;

Thus, a possible null-pointer dereference may occur.

To fix this bug, referring to lines 773 and 774, ak->adc_info and ak->adc_info[mixer_ch].selector_name are checked before being used.

This bug is found by a static analysis tool STCheck written by us.

---
  sound/i2c/other/ak4xxx-adda.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
sound/i2c/other/ak4xxx-adda.c +4 -2
index 5f59316f982a..9a891470e84a 100644
@@ -775,11 +775,13 @@ static int build_adc_controls(struct snd_akm4xxx *ak)
  				return err;
 
  			memset(&knew, 0, sizeof(knew));
-			knew.name = ak->adc_info[mixer_ch].selector_name;
-			if (!knew.name) {
+			if (! ak->adc_info ||
+				! ak->adc_info[mixer_ch].selector_name) {
  				knew.name = "Capture Channel";
  				knew.index = mixer_ch + ak->idx_offset * 2;
  			}
+			else
+				knew.name = ak->adc_info[mixer_ch].selector_name;
 
Quoted 5 lines

sound/i2c/other/ak4xxx-adda.c static int build_adc_controls(struct snd_akm4xxx *ak) · line 787

Quoted 2 lines
sound/i2c/other/ak4xxx-adda.c +0 -0
  			knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  			knew.info = ak4xxx_capture_source_info;

The code change itself looks good, but please follow the standard coding style. In short: please run checkpatch.pl, fix errors (some warnings may be ignored) and resubmit.

Okay, thanks for the advice.
I will send a v2 patch.

Best wishes,
Jia-Ju Bai