Skip to contentSkip to conversation

ALSA: ump: Fix double-free of ump->out_cvts on legacy rawmidi attach failure

PATCH In review Deepanshu Kartikey 2 participants 2 messages Jul 29 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: ump: Fix double-free of ump->out_cvts on legacy rawmidi attach failure

If snd_rawmidi_new() fails inside snd_ump_attach_legacy_rawmidi(), the error path frees ump->out_cvts but leaves the pointer dangling. Since ump->out_cvts is a field of the long-lived struct snd_ump_endpoint (not the rawmidi device that failed to be created), it gets freed a second time later during normal endpoint teardown, in snd_ump_endpoint_free(), invoked via snd_rawmidi_free()'s private_free callback when the sound card is released. This results in a KASAN double-free/invalid-free.

Clear ump->out_cvts to NULL after freeing it on the error path, so the later unconditional kfree() in snd_ump_endpoint_free() becomes a harmless no-op.

---
 sound/core/ump.c | 1 +
 1 file changed, 1 insertion(+)
sound/core/ump.c +1 -0
index 70520c7ca293..632c13baf21e 100644
@@ -1365,6 +1365,7 @@ int snd_ump_attach_legacy_rawmidi(struct snd_ump_endpoint *ump,
 			      &rmidi);
 	if (err < 0) {
 		kfree(ump->out_cvts);
+		ump->out_cvts = NULL;
 		return err;
 	}
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 10 lines · Deepanshu Kartikey

If snd_rawmidi_new() fails inside snd_ump_attach_legacy_rawmidi(), the error path frees ump->out_cvts but leaves the pointer dangling. Since ump->out_cvts is a field of the long-lived struct snd_ump_endpoint (not the rawmidi device that failed to be created), it gets freed a second time later during normal endpoint teardown, in snd_ump_endpoint_free(), invoked via snd_rawmidi_free()'s private_free callback when the sound card is released. This results in a KASAN double-free/invalid-free.

Quoted 7 lines

Clear ump->out_cvts to NULL after freeing it on the error path, so the later unconditional kfree() in snd_ump_endpoint_free() becomes a harmless no-op.

The very same fix has been already merged in commit 70c977815af0d997feb2d0c5d284d55689bf7051 on sound git tree.

thanks,

Takashi