linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ALSA: control: Fix an out-of-bounds bug in get_ctl_id_hash()
@ 2022-08-24  8:16 Zheyu Ma
  2022-08-24  9:42 ` Takashi Iwai
  0 siblings, 1 reply; 2+ messages in thread
From: Zheyu Ma @ 2022-08-24  8:16 UTC (permalink / raw)
  To: Jaroslav Kysela, Takashi Iwai
  Cc: Zheyu Ma, Takashi Iwai, alsa-devel, linux-kernel

Since the user can control the arguments provided to the kernel by the
ioctl() system call, an out-of-bounds bug occurs when the 'id->name'
provided by the user does not end with '\0'.

The following log can reveal it:

[    10.002313] BUG: KASAN: stack-out-of-bounds in snd_ctl_find_id+0x36c/0x3a0
[    10.002895] Read of size 1 at addr ffff888109f5fe28 by task snd/439
[    10.004934] Call Trace:
[    10.007140]  snd_ctl_find_id+0x36c/0x3a0
[    10.007489]  snd_ctl_ioctl+0x6cf/0x10e0

Fix this by checking the bound of 'id->name' in the loop.

Fixes: c27e1efb61c5 ("ALSA: control: Use xarray for faster lookups")
Signed-off-by: Zheyu Ma <zheyuma97@gmail.com>
---
 sound/core/control.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/sound/core/control.c b/sound/core/control.c
index f3e893715369..e8fc4c511e5f 100644
--- a/sound/core/control.c
+++ b/sound/core/control.c
@@ -385,14 +385,14 @@ static bool elem_id_matches(const struct snd_kcontrol *kctl,
 #define MULTIPLIER	37
 static unsigned long get_ctl_id_hash(const struct snd_ctl_elem_id *id)
 {
+	int i;
 	unsigned long h;
-	const unsigned char *p;
 
 	h = id->iface;
 	h = MULTIPLIER * h + id->device;
 	h = MULTIPLIER * h + id->subdevice;
-	for (p = id->name; *p; p++)
-		h = MULTIPLIER * h + *p;
+	for (i = 0; id->name[i] && i < SNDRV_CTL_ELEM_ID_NAME_MAXLEN; i++)
+		h = MULTIPLIER * h + id->name[i];
 	h = MULTIPLIER * h + id->index;
 	h &= LONG_MAX;
 	return h;
-- 
2.25.1


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

* Re: [PATCH] ALSA: control: Fix an out-of-bounds bug in get_ctl_id_hash()
  2022-08-24  8:16 [PATCH] ALSA: control: Fix an out-of-bounds bug in get_ctl_id_hash() Zheyu Ma
@ 2022-08-24  9:42 ` Takashi Iwai
  0 siblings, 0 replies; 2+ messages in thread
From: Takashi Iwai @ 2022-08-24  9:42 UTC (permalink / raw)
  To: Zheyu Ma; +Cc: Jaroslav Kysela, Takashi Iwai, alsa-devel, linux-kernel

On Wed, 24 Aug 2022 10:16:54 +0200,
Zheyu Ma wrote:
> 
> Since the user can control the arguments provided to the kernel by the
> ioctl() system call, an out-of-bounds bug occurs when the 'id->name'
> provided by the user does not end with '\0'.
> 
> The following log can reveal it:
> 
> [    10.002313] BUG: KASAN: stack-out-of-bounds in snd_ctl_find_id+0x36c/0x3a0
> [    10.002895] Read of size 1 at addr ffff888109f5fe28 by task snd/439
> [    10.004934] Call Trace:
> [    10.007140]  snd_ctl_find_id+0x36c/0x3a0
> [    10.007489]  snd_ctl_ioctl+0x6cf/0x10e0
> 
> Fix this by checking the bound of 'id->name' in the loop.
> 
> Fixes: c27e1efb61c5 ("ALSA: control: Use xarray for faster lookups")
> Signed-off-by: Zheyu Ma <zheyuma97@gmail.com>

Thanks, applied now.


Takashi

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

end of thread, other threads:[~2022-08-24  9:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-24  8:16 [PATCH] ALSA: control: Fix an out-of-bounds bug in get_ctl_id_hash() Zheyu Ma
2022-08-24  9:42 ` 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).