Skip to contentSkip to conversation

ALSA: pcm: Test for "silence" field in struct "pcm_format_data"

PATCH Applied Fabio M. De Francesco 3 participants 9 messages Apr 9, 2022 – Jun 14, 2022 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: pcm: Test for "silence" field in struct "pcm_format_data"

Syzbot reports "KASAN: null-ptr-deref Write in
snd_pcm_format_set_silence".[1]

It is due to missing validation of the "silence" field of struct "pcm_format_data" in "pcm_formats" array.

Add a test for valid "pat" and, if it is not so, return -EINVAL.

[1] https://lore.kernel.org/lkml/[email protected]/

---

I wasn't able to figure out the commit for the "Fixes:" tag. If this patch is good, can someone please help with providing this missing information?

 sound/core/pcm_misc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/pcm_misc.c +1 -1
index 4866aed97aac..5588b6a1ee8b 100644
@@ -433,7 +433,7 @@ int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int
 		return 0;
 	width = pcm_formats[(INT)format].phys; /* physical width */
 	pat = pcm_formats[(INT)format].silence;
-	if (! width)
+	if (!width || !pat)
 		return -EINVAL;
 	/* signed or 1 byte data */
 	if (pcm_formats[(INT)format].signd == 1 || width <= 8) {
Signature · 1 line
2.34.1
Applied
#1 lore raw
Quoted 7 lines · Fabio M. De Francesco

Syzbot reports "KASAN: null-ptr-deref Write in
snd_pcm_format_set_silence".[1]

It is due to missing validation of the "silence" field of struct "pcm_format_data" in "pcm_formats" array.

Quoted 6 lines

Add a test for valid "pat" and, if it is not so, return -EINVAL.

[1] https://lore.kernel.org/lkml/[email protected]/

Thanks, applied now.

Quoted 4 lines

---

I wasn't able to figure out the commit for the "Fixes:" tag. If this patch is good, can someone please help with providing this missing information?

That must be present from the very beginning.
I just add Cc-to-stable for allowing backport to all releases.

thanks,

Takashi

#2 lore raw
In reply to Fabio M. De Francesco

Hello Fabio, hello All,

Quoted 25 lines · Fabio M. De Francesco

Syzbot reports "KASAN: null-ptr-deref Write in
snd_pcm_format_set_silence".[1]

It is due to missing validation of the "silence" field of struct "pcm_format_data" in "pcm_formats" array.

Add a test for valid "pat" and, if it is not so, return -EINVAL.

[1] https://lore.kernel.org/lkml/[email protected]/

---

I wasn't able to figure out the commit for the "Fixes:" tag. If this patch is good, can someone please help with providing this missing information?

 sound/core/pcm_misc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/pcm_misc.c +0 -0
index 4866aed97aac..5588b6a1ee8b 100644

sound/core/pcm_misc.c int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int · line 439

Quoted 8 lines
sound/core/pcm_misc.c +1 -1
@@ -433,7 +433,7 @@ int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int
 		return 0;
 	width = pcm_formats[(INT)format].phys; /* physical width */
 	pat = pcm_formats[(INT)format].silence;
-	if (! width)
+	if (!width || !pat)
 		return -EINVAL;
 	/* signed or 1 byte data */
 	if (pcm_formats[(INT)format].signd == 1 || width <= 8) {

JFYI, PVS-Studio 7.19 reports:

sound/core/pcm_misc.c 409 warn V560 A part of conditional expression is always false: !pat.

I haven't fully validated the finding, but it appears to be legit, since the pointer variable (as opposed to the contents behind the pointer) is always non-null, hence !pat always evaluating to false.

If the above is true, then the patch likely hasn't introduced any regression, but also likely hasn't fixed the original KASAN problem.

Or are there alternative views?

BR, Eugeniu.

#3 lore raw
Quoted 42 lines · Eugeniu Rosca

Hello Fabio, hello All,

Quoted 25 lines · Fabio M. De Francesco

Syzbot reports "KASAN: null-ptr-deref Write in
snd_pcm_format_set_silence".[1]

It is due to missing validation of the "silence" field of struct "pcm_format_data" in "pcm_formats" array.

Add a test for valid "pat" and, if it is not so, return -EINVAL.

[1] https://lore.kernel.org/lkml/[email protected]/

---

I wasn't able to figure out the commit for the "Fixes:" tag. If this patch is good, can someone please help with providing this missing information?

 sound/core/pcm_misc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/pcm_misc.c +0 -0
index 4866aed97aac..5588b6a1ee8b 100644

sound/core/pcm_misc.c int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int · line 439

Quoted 8 lines
sound/core/pcm_misc.c +1 -1
@@ -433,7 +433,7 @@ int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int
 		return 0;
 	width = pcm_formats[(INT)format].phys; /* physical width */
 	pat = pcm_formats[(INT)format].silence;
-	if (! width)
+	if (!width || !pat)
 		return -EINVAL;
 	/* signed or 1 byte data */
 	if (pcm_formats[(INT)format].signd == 1 || width <= 8) {

JFYI, PVS-Studio 7.19 reports:

sound/core/pcm_misc.c 409 warn V560 A part of conditional expression is always false: !pat.

Quoted 8 lines

I haven't fully validated the finding, but it appears to be legit, since the pointer variable (as opposed to the contents behind the pointer) is always non-null, hence !pat always evaluating to false.

If the above is true, then the patch likely hasn't introduced any regression, but also likely hasn't fixed the original KASAN problem.

Or are there alternative views?

Indeed the fix looks bogus, and maybe better to revert.

Looking at the original syzkaller report again, it points rather to the *write* at the address 1, and it means not the source (silence[]) but the target pointer (data) is invalid; i.e. it's a problem in the caller side, likely some race between the OSS temporary buffer removal and other operation.

Thanks for checking this.

Takashi

#4 lore raw
In reply to Eugeniu Rosca
Quoted 12 lines · On martedì 14 giugno 2022 11:58:51 CEST Eugeniu Rosca wrote:

Hello Fabio, hello All,

Quoted 9 lines · Fabio M. De Francesco

Syzbot reports "KASAN: null-ptr-deref Write in
snd_pcm_format_set_silence".[1]

It is due to missing validation of the "silence" field of struct "pcm_format_data" in "pcm_formats" array.

Add a test for valid "pat" and, if it is not so, return -EINVAL.

[1] https://lore.kernel.org/lkml/

[email protected]/

Quoted 2 lines
Quoted 2 lines

Reported-and-tested-by:

[email protected]

Quoted 4 lines
Quoted 4 lines

---

I wasn't able to figure out the commit for the "Fixes:" tag. If this

patch

Quoted 1 line
Quoted 1 line

is good, can someone please help with providing this missing

information?

Quoted 9 lines

sound/core/pcm_misc.c int snd_pcm_format_set_silence(snd_pcm_format_t

Quoted 9 lines
 sound/core/pcm_misc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/pcm_misc.c +0 -0
index 4866aed97aac..5588b6a1ee8b 100644
@@ -433,7 +433,7 @@ int snd_pcm_format_set_silence(snd_pcm_format_t 

format, void *data, unsigned int

Quoted 11 lines

sound/core/pcm_misc.c int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int · line 439

Quoted 8 lines
sound/core/pcm_misc.c +1 -1
 		return 0;
 	width = pcm_formats[(INT)format].phys; /* physical width */
 	pat = pcm_formats[(INT)format].silence;
-	if (! width)
+	if (!width || !pat)
 		return -EINVAL;
 	/* signed or 1 byte data */
 	if (pcm_formats[(INT)format].signd == 1 || width <= 8) {

JFYI, PVS-Studio 7.19 reports:

Quoted 1 line

sound/core/pcm_misc.c 409 warn V560 A part of

conditional expression is always false: !pat.

Sorry, I assumed (wrongly!) that when we have

static const struct pcm_format_data

pcm_formats[(INT)SNDRV_PCM_FORMAT_LAST+1] = {
	[SNDRV_PCM_FORMAT_S8] = {
		.width = 8, .phys = 8, .le = -1, .signd = 1,
		.silence = {},
	},
	[snip]
	/* FIXME: the following two formats are not defined properly yet 
*/
	[SNDRV_PCM_FORMAT_MPEG] = {
		.le = -1, .signd = -1,
	},
	[SNDRV_PCM_FORMAT_GSM] = {
		.le = -1, .signd = -1,
	},

pointer "silence", and then "pat", must be NULL.

I'd better read again how fields of global struct variables are initialized :-(

Thanks for this finding,

Fabio

Quoted 12 lines

I haven't fully validated the finding, but it appears to be legit, since the pointer variable (as opposed to the contents behind the pointer) is always non-null, hence !pat always evaluating to false.

If the above is true, then the patch likely hasn't introduced any regression, but also likely hasn't fixed the original KASAN problem.

Or are there alternative views?

BR, Eugeniu.

#5 lore raw

Dear Takashi, dear Fabio,

Thank you for your prompt feedback.
Please, keep me in the loop in case a revert/fix is submitted to LKML.

BR, Eugeniu.

#6 lore raw
In reply to Fabio M. De Francesco
Quoted 67 lines · Fabio M. De Francesco
Quoted 12 lines · On martedì 14 giugno 2022 11:58:51 CEST Eugeniu Rosca wrote:

Hello Fabio, hello All,

Quoted 9 lines · Fabio M. De Francesco

Syzbot reports "KASAN: null-ptr-deref Write in
snd_pcm_format_set_silence".[1]

It is due to missing validation of the "silence" field of struct "pcm_format_data" in "pcm_formats" array.

Add a test for valid "pat" and, if it is not so, return -EINVAL.

[1] https://lore.kernel.org/lkml/

[email protected]/

Quoted 2 lines
Quoted 2 lines

Reported-and-tested-by:

[email protected]

Quoted 4 lines
Quoted 4 lines

---

I wasn't able to figure out the commit for the "Fixes:" tag. If this

patch

Quoted 1 line
Quoted 1 line

is good, can someone please help with providing this missing

information?

Quoted 9 lines

sound/core/pcm_misc.c int snd_pcm_format_set_silence(snd_pcm_format_t

Quoted 9 lines
 sound/core/pcm_misc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/pcm_misc.c +0 -0
index 4866aed97aac..5588b6a1ee8b 100644
@@ -433,7 +433,7 @@ int snd_pcm_format_set_silence(snd_pcm_format_t 

format, void *data, unsigned int

Quoted 11 lines

sound/core/pcm_misc.c int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int · line 439

Quoted 8 lines
sound/core/pcm_misc.c +1 -1
 		return 0;
 	width = pcm_formats[(INT)format].phys; /* physical width */
 	pat = pcm_formats[(INT)format].silence;
-	if (! width)
+	if (!width || !pat)
 		return -EINVAL;
 	/* signed or 1 byte data */
 	if (pcm_formats[(INT)format].signd == 1 || width <= 8) {

JFYI, PVS-Studio 7.19 reports:

Quoted 1 line

sound/core/pcm_misc.c 409 warn V560 A part of

conditional expression is always false: !pat.

Sorry, I assumed (wrongly!) that when we have

static const struct pcm_format_data

pcm_formats[(INT)SNDRV_PCM_FORMAT_LAST+1] = {
	[SNDRV_PCM_FORMAT_S8] = {
		.width = 8, .phys = 8, .le = -1, .signd = 1,
		.silence = {},
	},
	[snip]
	/* FIXME: the following two formats are not defined properly yet 
*/
	[SNDRV_PCM_FORMAT_MPEG] = {
		.le = -1, .signd = -1,
	},
	[SNDRV_PCM_FORMAT_GSM] = {
		.le = -1, .signd = -1,
	},
Quoted 1 line

pointer "silence", and then "pat", must be NULL.

Oh right, those are missing ones. I haven't realized that those formats are allowed by PCM OSS layer.

Practically seen, those formats have never been used in reality, and we may consider dropping them completely to plug such holes...

Takashi

#7 lore raw
Quoted 16 lines · On martedì 14 giugno 2022 12:49:38 CEST Takashi Iwai wrote:
Quoted 14 lines · Fabio M. De Francesco
Quoted 12 lines · On martedì 14 giugno 2022 11:58:51 CEST Eugeniu Rosca wrote:

Hello Fabio, hello All,

Quoted 9 lines · Fabio M. De Francesco

Syzbot reports "KASAN: null-ptr-deref Write in
snd_pcm_format_set_silence".[1]

It is due to missing validation of the "silence" field of struct "pcm_format_data" in "pcm_formats" array.

Add a test for valid "pat" and, if it is not so, return -EINVAL.

[1] https://lore.kernel.org/lkml/

Quoted 8 lines
Quoted 8 lines

[email protected]/

Quoted 2 lines
Quoted 2 lines

Reported-and-tested-by:

[email protected]

Quoted 4 lines
Quoted 4 lines

---

I wasn't able to figure out the commit for the "Fixes:" tag. If

this

Quoted 45 lines
Quoted 45 lines

patch

Quoted 1 line
Quoted 1 line

is good, can someone please help with providing this missing

information?

Quoted 9 lines

sound/core/pcm_misc.c int snd_pcm_format_set_silence(snd_pcm_format_t

Quoted 9 lines
 sound/core/pcm_misc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/pcm_misc.c +0 -0
index 4866aed97aac..5588b6a1ee8b 100644
@@ -433,7 +433,7 @@ int snd_pcm_format_set_silence(snd_pcm_format_t 

format, void *data, unsigned int

Quoted 11 lines

sound/core/pcm_misc.c int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int · line 439

Quoted 8 lines
sound/core/pcm_misc.c +1 -1
 		return 0;
 	width = pcm_formats[(INT)format].phys; /* physical width */
 	pat = pcm_formats[(INT)format].silence;
-	if (! width)
+	if (!width || !pat)
 		return -EINVAL;
 	/* signed or 1 byte data */
 	if (pcm_formats[(INT)format].signd == 1 || width <= 8) {

JFYI, PVS-Studio 7.19 reports:

Quoted 1 line

sound/core/pcm_misc.c 409 warn V560 A part of

conditional expression is always false: !pat.

Sorry, I assumed (wrongly!) that when we have

static const struct pcm_format_data

pcm_formats[(INT)SNDRV_PCM_FORMAT_LAST+1] = {
	[SNDRV_PCM_FORMAT_S8] = {
		.width = 8, .phys = 8, .le = -1, .signd = 1,
		.silence = {},
	},
	[snip]
	/* FIXME: the following two formats are not defined properly yet 
*/
	[SNDRV_PCM_FORMAT_MPEG] = {
		.le = -1, .signd = -1,
	},
	[SNDRV_PCM_FORMAT_GSM] = {
		.le = -1, .signd = -1,
	},
Quoted 8 lines
Quoted 1 line

pointer "silence", and then "pat", must be NULL.

Oh right, those are missing ones. I haven't realized that those formats are allowed by PCM OSS layer.

Practically seen, those formats have never been used in reality, and we may consider dropping them completely to plug such holes...

Does it imply that my argument is correct or my "fix" can't yet catch those missing ones?

Besides the question above, I want to notice that we have one more /* FIXME */ entry...

/* FIXME: the following format is not defined properly yet */

	[SNDRV_PCM_FORMAT_SPECIAL] = {
		.le = -1, .signd = -1,
	},

If you want I can get rid of those three entries if you confirm they can safely be deleted. In a second patch I can also remove that unnecessary check for valid "pat".

Please let me know.

Thanks,

Fabio

#8 lore raw
Quoted 74 lines · Fabio M. De Francesco
Quoted 16 lines · On martedì 14 giugno 2022 12:49:38 CEST Takashi Iwai wrote:
Quoted 14 lines · Fabio M. De Francesco
Quoted 12 lines · On martedì 14 giugno 2022 11:58:51 CEST Eugeniu Rosca wrote:

Hello Fabio, hello All,

Quoted 9 lines · Fabio M. De Francesco

Syzbot reports "KASAN: null-ptr-deref Write in
snd_pcm_format_set_silence".[1]

It is due to missing validation of the "silence" field of struct "pcm_format_data" in "pcm_formats" array.

Add a test for valid "pat" and, if it is not so, return -EINVAL.

[1] https://lore.kernel.org/lkml/

Quoted 8 lines
Quoted 8 lines

[email protected]/

Quoted 2 lines
Quoted 2 lines

Reported-and-tested-by:

[email protected]

Quoted 4 lines
Quoted 4 lines

---

I wasn't able to figure out the commit for the "Fixes:" tag. If

this

Quoted 47 lines
Quoted 46 lines

patch

Quoted 1 line
Quoted 1 line

is good, can someone please help with providing this missing

information?

Quoted 9 lines

sound/core/pcm_misc.c int snd_pcm_format_set_silence(snd_pcm_format_t

Quoted 9 lines
 sound/core/pcm_misc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/pcm_misc.c +0 -0
index 4866aed97aac..5588b6a1ee8b 100644
@@ -433,7 +433,7 @@ int snd_pcm_format_set_silence(snd_pcm_format_t 

format, void *data, unsigned int

Quoted 11 lines

sound/core/pcm_misc.c int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int · line 439

Quoted 8 lines
sound/core/pcm_misc.c +1 -1
 		return 0;
 	width = pcm_formats[(INT)format].phys; /* physical width */
 	pat = pcm_formats[(INT)format].silence;
-	if (! width)
+	if (!width || !pat)
 		return -EINVAL;
 	/* signed or 1 byte data */
 	if (pcm_formats[(INT)format].signd == 1 || width <= 8) {

JFYI, PVS-Studio 7.19 reports:

Quoted 1 line

sound/core/pcm_misc.c 409 warn V560 A part of

conditional expression is always false: !pat.

Sorry, I assumed (wrongly!) that when we have

static const struct pcm_format_data

pcm_formats[(INT)SNDRV_PCM_FORMAT_LAST+1] = {
	[SNDRV_PCM_FORMAT_S8] = {
		.width = 8, .phys = 8, .le = -1, .signd = 1,
		.silence = {},
	},
	[snip]
	/* FIXME: the following two formats are not defined properly yet 
*/
	[SNDRV_PCM_FORMAT_MPEG] = {
		.le = -1, .signd = -1,
	},
	[SNDRV_PCM_FORMAT_GSM] = {
		.le = -1, .signd = -1,
	},

pointer "silence", and then "pat", must be NULL.

Quoted 8 lines
Quoted 6 lines

Oh right, those are missing ones. I haven't realized that those formats are allowed by PCM OSS layer.

Practically seen, those formats have never been used in reality, and we may consider dropping them completely to plug such holes...

Does it imply that my argument is correct or my "fix" can't yet catch those missing ones?

Your fix should catch the case with a NULL pat pointer, I guess. PCM OSS layer allows this format, so this could be hit. However, whether it's really a fix for the given syzkaller code path, it's doubtful. Nevertheless, your check is still worth to keep.

Quoted 11 lines

Besides the question above, I want to notice that we have one more /* FIXME */ entry...

/* FIXME: the following format is not defined properly yet */

	[SNDRV_PCM_FORMAT_SPECIAL] = {
		.le = -1, .signd = -1,
	},

If you want I can get rid of those three entries if you confirm they can safely be deleted. In a second patch I can also remove that unnecessary check for valid "pat".

Well, you can't "delete" those entries so easiy. The formats themselves are still allowed for user-space, hence every caller needs to check. If any, we need to add the check in valid_format() for such unsupported formats, then drop the rest checks and entries.

thanks,

Takashi