Skip to contentSkip to conversation

ASoC: wcd934x: Fix a incorrect use of kstrndup

PATCH In review Miaoqian Lin 2 participants 3 messages Dec 14, 2021

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

PATCH 1/1 ASoC: wcd934x: Fix a incorrect use of kstrndup

In wcd934x_codec_enable_dec(), widget_name is allocated by kstrndup(). However, according to doc: "Note: Use kmemdup_nul() instead if the size is known exactly." So we should use kmemdup_nul() here instead of kstrndup(). It's similar to CVE-2019-12454.

---
 sound/soc/codecs/wcd934x.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/codecs/wcd934x.c +1 -1
index e63c6b723d76..c6677cfbce59 100644
@@ -5005,7 +5005,7 @@ static int wcd934x_codec_enable_dec(struct snd_soc_dapm_widget *w,
 	char *dec;
 	u8 hpf_coff_freq;
 
-	widget_name = kstrndup(w->name, 15, GFP_KERNEL);
+	widget_name = kmemdup_nul(w->name, 15, GFP_KERNEL);
 	if (!widget_name)
 		return -ENOMEM;
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 15 lines · Miaoqian Lin

In wcd934x_codec_enable_dec(), widget_name is allocated by kstrndup(). However, according to doc: "Note: Use kmemdup_nul() instead if the size is known exactly." So we should use kmemdup_nul() here instead of kstrndup(). It's similar to CVE-2019-12454.

---
  sound/soc/codecs/wcd934x.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/codecs/wcd934x.c +0 -0
index e63c6b723d76..c6677cfbce59 100644

sound/soc/codecs/wcd934x.c static int wcd934x_codec_enable_dec(struct snd_soc_dapm_widget *w, · line 5008

Quoted 5 lines
sound/soc/codecs/wcd934x.c +1 -1
@@ -5005,7 +5005,7 @@ static int wcd934x_codec_enable_dec(struct snd_soc_dapm_widget *w,
  	char *dec;
  	u8 hpf_coff_freq;
 
-	widget_name = kstrndup(w->name, 15, GFP_KERNEL);
+	widget_name = kmemdup_nul(w->name, 15, GFP_KERNEL);

I'm wondering if it isn't better to re-structure the code to not allocate any memory.

something like

ret = sscan(w->name, "ADC MUX%d", &decimator);
if (ret != 1)
    ...

#2 lore raw
In reply to Miaoqian Lin
Quoted 15 lines · Miaoqian Lin

In wcd934x_codec_enable_dec(), widget_name is allocated by kstrndup(). However, according to doc: "Note: Use kmemdup_nul() instead if the size is known exactly." So we should use kmemdup_nul() here instead of kstrndup(). It's similar to CVE-2019-12454.

---
  sound/soc/codecs/wcd934x.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/codecs/wcd934x.c +0 -0
index e63c6b723d76..c6677cfbce59 100644

sound/soc/codecs/wcd934x.c static int wcd934x_codec_enable_dec(struct snd_soc_dapm_widget *w, · line 5008

Quoted 5 lines
sound/soc/codecs/wcd934x.c +1 -1
@@ -5005,7 +5005,7 @@ static int wcd934x_codec_enable_dec(struct snd_soc_dapm_widget *w,
  	char *dec;
  	u8 hpf_coff_freq;
 
-	widget_name = kstrndup(w->name, 15, GFP_KERNEL);
+	widget_name = kmemdup_nul(w->name, 15, GFP_KERNEL);

Thinking a bit more about it, this is wrong. The source string is shorter than 15 character. So with this change you are copying past the end of the string, which depending on where in memory the string is placed can cause undefined behavior.