From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A010AC54EE9 for ; Mon, 19 Sep 2022 12:34:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229942AbiISMez (ORCPT ); Mon, 19 Sep 2022 08:34:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229931AbiISMem (ORCPT ); Mon, 19 Sep 2022 08:34:42 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B12A310AC for ; Mon, 19 Sep 2022 05:34:38 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 703E41F897; Mon, 19 Sep 2022 12:34:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1663590877; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=n4DmpZOWDrU0D3NXR2Cd+6cBQKxt/l9K3qGbeLRvP20=; b=fX6T9Hxib+DPXhG/Hv83lhWYbOR0RcJSDJLnmC+Voz70E5gasG6mppv73qUWrWQj8t5Xa4 xPdJdfF0MfdvtiXhF/FKZ26WCH/jpEdABIm49dL1td8eOT4righz9mbFpEzEruu12s/VC5 /HAkUfIvpPl/FfDx06YthFpKg5ryvCg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1663590877; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=n4DmpZOWDrU0D3NXR2Cd+6cBQKxt/l9K3qGbeLRvP20=; b=QLr4Kb70gBT49BBjXfJc2C3kh173WpDiUt2SGxi8H5vXGK90EAiCo160ho4G4HKAstsenU LQI3ODlqvRmwv/CQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 5297D13A96; Mon, 19 Sep 2022 12:34:37 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id v4RnE91hKGMILQAAMHmgww (envelope-from ); Mon, 19 Sep 2022 12:34:37 +0000 Date: Mon, 19 Sep 2022 14:34:36 +0200 Message-ID: <87bkrbbl0j.wl-tiwai@suse.de> From: Takashi Iwai To: Rondreis Cc: perex@perex.cz, tiwai@suse.com, alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org Subject: Re: KASAN: invalid-free in snd_card_new In-Reply-To: References: <871qs7dav5.wl-tiwai@suse.de> <87r107btti.wl-tiwai@suse.de> User-Agent: Wanderlust/2.15.9 (Almost Unreal) Emacs/27.2 Mule/6.0 MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 19 Sep 2022 13:46:43 +0200, Rondreis wrote: > > Hello, > > I tested this patch with the reproducer and the crash did not trigger again. Thanks for quick testing. I'm going to submit and merge the fix, then. Takashi > > On Mon, Sep 19, 2022 at 5:24 PM Takashi Iwai wrote: > > > > On Mon, 19 Sep 2022 10:30:54 +0200, > > Takashi Iwai wrote: > > > > > > On Sun, 18 Sep 2022 15:01:11 +0200, > > > Rondreis wrote: > > > > > > > > Hello, > > > > > > > > When fuzzing the Linux kernel driver v6.0-rc4, the following crash was > > > > triggered. > > > > > > > > HEAD commit: 7e18e42e4b280c85b76967a9106a13ca61c16179 > > > > git tree: upstream > > > > > > > > kernel config: https://pastebin.com/raw/xtrgsXP3 > > > > C reproducer: https://pastebin.com/raw/w2sdQWYj > > > > console output: https://pastebin.com/raw/Yyf7zw2d > > > > > > > > Basically, in the c reproducer, we use the gadget module to emulate > > > > attaching a USB device(vendor id: 0x1bc7, product id: 0x1206, with the > > > > midi function) and executing some simple sequence of system calls. > > > > To reproduce this crash, we utilize a third-party library to emulate > > > > the attaching process: https://github.com/linux-usb-gadgets/libusbgx. > > > > Just clone this repository, install it, and compile the c > > > > reproducer with ``` gcc crash.c -lusbgx -lconfig -o crash ``` will do > > > > the trick. > > > > > > > > I would appreciate it if you have any idea how to solve this bug. > > > > > > Could you try the patch below? It looks like a simple double-free in > > > the code. > > > > A more proper patch is below. Please give it a try. > > > > > > thanks, > > > > Takashi > > > > -- 8< -- > > From: Takashi Iwai > > Subject: [PATCH] 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: e8ad415b7a55 ("ALSA: core: Add managed card creation") > > Reported-by: Rondreis > > Cc: > > Link: https://lore.kernel.org/r/CAB7eexL1zBnB636hwS27d-LdPYZ_R1-5fJS_h=ZbCWYU=UPWJg@mail.gmail.com > > Signed-off-by: Takashi Iwai > > --- > > sound/core/init.c | 10 +++++----- > > 1 file changed, 5 insertions(+), 5 deletions(-) > > > > diff --git a/sound/core/init.c b/sound/core/init.c > > index 193dae361fac..5377f94eb211 100644 > > --- a/sound/core/init.c > > +++ b/sound/core/init.c > > @@ -178,10 +178,8 @@ int snd_card_new(struct device *parent, int idx, const char *xid, > > return -ENOMEM; > > > > err = snd_card_init(card, parent, idx, xid, module, extra_size); > > - if (err < 0) { > > - kfree(card); > > - return err; > > - } > > + if (err < 0) > > + return err; /* card is freed by error handler */ > > > > *card_ret = card; > > return 0; > > @@ -233,7 +231,7 @@ int snd_devm_card_new(struct device *parent, int idx, const char *xid, > > card->managed = true; > > err = snd_card_init(card, parent, idx, xid, module, extra_size); > > if (err < 0) { > > - devres_free(card); > > + devres_free(card); /* in managed mode, we need to free manually */ > > return err; > > } > > > > @@ -297,6 +295,8 @@ static int snd_card_init(struct snd_card *card, struct device *parent, > > mutex_unlock(&snd_card_mutex); > > dev_err(parent, "cannot find the slot for index %d (range 0-%i), error: %d\n", > > idx, snd_ecards_limit - 1, err); > > + if (!card->managed) > > + kfree(card); /* manually free here, as no destructor called */ > > return err; > > } > > set_bit(idx, snd_cards_lock); /* lock it */ > > -- > > 2.35.3 > > >