Skip to content

Commit c3afa2a

Browse files
committed
ALSA: core: Fix double-free at snd_card_new()
During the code change to add the support for devres-managed card instance, we put an explicit kfree(card) call at the error path in snd_card_new(). This is needed for the early error path before the card is initialized with the device, but is rather superfluous and causes a double-free at the error path after the card instance is initialized, as the destructor of the card object already contains a kfree() call. This patch fixes the double-free situation by removing the superfluous kfree(). Meanwhile we need to call kfree() explicitly for the early error path, so it's added there instead. Fixes: e8ad415 ("ALSA: core: Add managed card creation") Reported-by: Rondreis <[email protected]> Cc: <[email protected]> Link: https://lore.kernel.org/r/CAB7eexL1zBnB636hwS27d-LdPYZ_R1-5fJS_h=ZbCWYU=UPWJg@mail.gmail.com Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Takashi Iwai <[email protected]>
1 parent 4963223 commit c3afa2a

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

sound/core/init.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,8 @@ int snd_card_new(struct device *parent, int idx, const char *xid,
178178
return -ENOMEM;
179179

180180
err = snd_card_init(card, parent, idx, xid, module, extra_size);
181-
if (err < 0) {
182-
kfree(card);
183-
return err;
184-
}
181+
if (err < 0)
182+
return err; /* card is freed by error handler */
185183

186184
*card_ret = card;
187185
return 0;
@@ -233,7 +231,7 @@ int snd_devm_card_new(struct device *parent, int idx, const char *xid,
233231
card->managed = true;
234232
err = snd_card_init(card, parent, idx, xid, module, extra_size);
235233
if (err < 0) {
236-
devres_free(card);
234+
devres_free(card); /* in managed mode, we need to free manually */
237235
return err;
238236
}
239237

@@ -297,6 +295,8 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
297295
mutex_unlock(&snd_card_mutex);
298296
dev_err(parent, "cannot find the slot for index %d (range 0-%i), error: %d\n",
299297
idx, snd_ecards_limit - 1, err);
298+
if (!card->managed)
299+
kfree(card); /* manually free here, as no destructor called */
300300
return err;
301301
}
302302
set_bit(idx, snd_cards_lock); /* lock it */

0 commit comments

Comments
 (0)