Skip to contentSkip to conversation

ALSA: hwdep: fix NULL dereference on error path

PATCH In review Guangshuo Li 2 participants 3 messages Apr 12 – Apr 13

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

PATCH 1/1 ALSA: hwdep: fix NULL dereference on error path

snd_hwdep_new() allocates a hwdep instance first and then allocates hwdep->dev via snd_device_alloc().

When snd_device_alloc() fails, hwdep->dev remains NULL, because snd_device_alloc() clears *dev_p before attempting to allocate the device object. The error path then calls snd_hwdep_free(), which unconditionally invokes put_device(hwdep->dev).

This may lead to a NULL pointer dereference in put_device().

Fixes 897c8882df58 ("ALSA: hwdep: Don't embed device")
---
 sound/core/hwdep.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
sound/core/hwdep.c +2 -1
index 09200df2932c..aa35bee8da6b 100644
@@ -343,7 +343,8 @@ static void snd_hwdep_free(struct snd_hwdep *hwdep)
 		return;
 	if (hwdep->private_free)
 		hwdep->private_free(hwdep);
-	put_device(hwdep->dev);
+	if (hwdep->dev)
+		put_device(hwdep->dev);
 	kfree(hwdep);
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 10 lines · Guangshuo Li

snd_hwdep_new() allocates a hwdep instance first and then allocates hwdep->dev via snd_device_alloc().

When snd_device_alloc() fails, hwdep->dev remains NULL, because snd_device_alloc() clears *dev_p before attempting to allocate the device object. The error path then calls snd_hwdep_free(), which unconditionally invokes put_device(hwdep->dev).

This may lead to a NULL pointer dereference in put_device().

put_device() has a NULL check by itself, so it's safe to pass NULL there.

thanks,

Takashi

#2 lore raw

Hi Takashi,

Thanks for the correction.

I overlooked the NULL check in put_device(), so the reported NULL dereference on this error path is not valid.

Sorry for the noise. Please disregard this patch.

Thanks,
Guangshuo

Quoted 21 lines · Takashi Iwai <[email protected]> 于2026年4月13日周一 13:22写道:
Quoted 10 lines · Guangshuo Li

snd_hwdep_new() allocates a hwdep instance first and then allocates hwdep->dev via snd_device_alloc().

When snd_device_alloc() fails, hwdep->dev remains NULL, because snd_device_alloc() clears *dev_p before attempting to allocate the device object. The error path then calls snd_hwdep_free(), which unconditionally invokes put_device(hwdep->dev).

This may lead to a NULL pointer dereference in put_device().

put_device() has a NULL check by itself, so it's safe to pass NULL there.

thanks,

Takashi