LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] ALSA: hda - Handle kzalloc() failure in snd_hda_attach_pcm_stream()
@ 2018-05-31 22:35 Bo Chen
  2018-06-01  7:39 ` Takashi Iwai
  0 siblings, 1 reply; 2+ messages in thread
From: Bo Chen @ 2018-05-31 22:35 UTC (permalink / raw)
  To: tiwai; +Cc: perex, alsa-devel, linux-kernel, Bo Chen

When 'kzalloc()' fails in 'snd_hda_attach_pcm_stream()', a new pcm instance is
created without setting its operators via 'snd_pcm_set_ops()'. Following
operations on the new pcm instance can trigger kernel null pointer dereferences
and cause kernel oops.

This bug was found with my work on building a gray-box fault-injection tool for
linux-kernel-module binaries. A kernel null pointer dereference was confirmed
from line 'substream->ops->open()' in function 'snd_pcm_open_substream()' in
file 'sound/core/pcm_native.c'.

This patch fixes the bug by calling 'snd_device_free()' in the error handling
path of 'kzalloc()', which removes the new pcm instance from the snd card before
returns with an error code.

Signed-off-by: Bo Chen <chenbo@pdx.edu>
---
 sound/pci/hda/hda_controller.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/sound/pci/hda/hda_controller.c b/sound/pci/hda/hda_controller.c
index d1eb14842340..a12e594d4e3b 100644
--- a/sound/pci/hda/hda_controller.c
+++ b/sound/pci/hda/hda_controller.c
@@ -748,8 +748,10 @@ int snd_hda_attach_pcm_stream(struct hda_bus *_bus, struct hda_codec *codec,
 		return err;
 	strlcpy(pcm->name, cpcm->name, sizeof(pcm->name));
 	apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
-	if (apcm == NULL)
+	if (apcm == NULL) {
+		snd_device_free(chip->card, pcm);
 		return -ENOMEM;
+	}
 	apcm->chip = chip;
 	apcm->pcm = pcm;
 	apcm->codec = codec;
-- 
2.17.0

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] ALSA: hda - Handle kzalloc() failure in snd_hda_attach_pcm_stream()
  2018-05-31 22:35 [PATCH] ALSA: hda - Handle kzalloc() failure in snd_hda_attach_pcm_stream() Bo Chen
@ 2018-06-01  7:39 ` Takashi Iwai
  0 siblings, 0 replies; 2+ messages in thread
From: Takashi Iwai @ 2018-06-01  7:39 UTC (permalink / raw)
  To: Bo Chen; +Cc: alsa-devel, perex, linux-kernel

On Fri, 01 Jun 2018 00:35:18 +0200,
Bo Chen wrote:
> 
> When 'kzalloc()' fails in 'snd_hda_attach_pcm_stream()', a new pcm instance is
> created without setting its operators via 'snd_pcm_set_ops()'. Following
> operations on the new pcm instance can trigger kernel null pointer dereferences
> and cause kernel oops.
> 
> This bug was found with my work on building a gray-box fault-injection tool for
> linux-kernel-module binaries. A kernel null pointer dereference was confirmed
> from line 'substream->ops->open()' in function 'snd_pcm_open_substream()' in
> file 'sound/core/pcm_native.c'.
> 
> This patch fixes the bug by calling 'snd_device_free()' in the error handling
> path of 'kzalloc()', which removes the new pcm instance from the snd card before
> returns with an error code.
> 
> Signed-off-by: Bo Chen <chenbo@pdx.edu>

Applied now.  Thanks.


Takashi

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-06-01  7:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-31 22:35 [PATCH] ALSA: hda - Handle kzalloc() failure in snd_hda_attach_pcm_stream() Bo Chen
2018-06-01  7:39 ` Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).