Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp2279020rwb; Mon, 19 Sep 2022 02:37:45 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5uW0D0ZFz3S3fZ3PnhVvgItrHaZgH48r/bzQ9BTybH743qbwB3o4uLuHnjruWRQ/Tm5wgi X-Received: by 2002:a17:907:25c7:b0:77b:c193:9230 with SMTP id ae7-20020a17090725c700b0077bc1939230mr12062254ejc.316.1663580265186; Mon, 19 Sep 2022 02:37:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663580265; cv=none; d=google.com; s=arc-20160816; b=GOtIaQNgAshiljNW0q6XFXD3XGjvvC2N4WtElVZHatbFtSMFWypeTAARI+o0g/CSWK TSNLZvKgw/ljtxRw4Hn3LLJEkTiUtT3Ja3fVP5x0fQuef8F1iAdup3BH6LbJIX7fJphu Juv1lE9hemCq2g+xLTXM6nxY3AqsNQsmYc8i8GRk+HoZyK2+Lz0x3RAfz/A8pPZshpkm v33JbaUJAzf55sD3ohYfNjOcMB0EVpty+gEchN+ZqIlC5Vw+k12r7+kMVRZwrfji4aYf SDuxIvrsNoFn1Duk4pU8RgT1Qh4gaV6vwTeXrFZGgg1N/dA2B8/BasteYHVjOLC628aN HL6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature:dkim-signature; bh=ZbT40zAkjhP9+n399AbT5L2GEVohnR2lNfZdDMkmB2I=; b=Fe0fqcjJDn5hljPwYQSYCeJ5byFVz4xM/V7ZXTIoXOK8C/QdRwoFhL1U5rL7i2/nrX uhwisQTqKKVQYNkl6rqPrgixOd296DXxLYg6gHFIFw2zTuiKP0422RF/KKrFqasK7zF2 LLoNhADC8Y/crgzIoLc4XlwAqPmJ679swRXycabhAc7fHCPDsaNwldNGB6HOk1y0gAVa 4he9rbb/ZwlE87BVRAj0Hmu4gV/SZ4+JCBSpB3nJ/DuVZGvPzKM/nsqYYqIcckzAIfve PnsX62BAPkvnTT7FogvvqRmMDkJnTDCWmYyA5jTWK/eaFExqn3Kzv5kO5Ebtp36OIUPA FyiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=s7BIrRKz; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=0MM+F95W; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y30-20020a50ce1e000000b0043e1c8af8c8si8570430edi.241.2022.09.19.02.37.19; Mon, 19 Sep 2022 02:37:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=s7BIrRKz; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=0MM+F95W; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230295AbiISJYi (ORCPT + 99 others); Mon, 19 Sep 2022 05:24:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230164AbiISJYb (ORCPT ); Mon, 19 Sep 2022 05:24:31 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB9F1338 for ; Mon, 19 Sep 2022 02:24:27 -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-out1.suse.de (Postfix) with ESMTPS id 516B1221E2; Mon, 19 Sep 2022 09:24:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1663579466; 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=ZbT40zAkjhP9+n399AbT5L2GEVohnR2lNfZdDMkmB2I=; b=s7BIrRKzRZ+RqyZh2vSJZm4djBMvyblnab40+wDxnhBKlf3MB4hBjij98pChd1+S+TzuRa MsteH+FOFgTLCu0kdyL0feWZ913GWoADpX0LuFj/yUQ150nRA+wZaxeF07l6ALfUhN2clr 6GA0quk9IMOYnSlX0qe6nUayb/9Jhfc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1663579466; 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=ZbT40zAkjhP9+n399AbT5L2GEVohnR2lNfZdDMkmB2I=; b=0MM+F95WPpXuJsELVkK2StvHIAoYnMOAq3u+fUYU4cuiMs/GOJvupOr/SOIu/K66OB/2fD IpnycQVdVkOjC6Cg== 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 2CEBE13A96; Mon, 19 Sep 2022 09:24:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id xO4ZCko1KGMTVAAAMHmgww (envelope-from ); Mon, 19 Sep 2022 09:24:26 +0000 Date: Mon, 19 Sep 2022 11:24:25 +0200 Message-ID: <87r107btti.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: <871qs7dav5.wl-tiwai@suse.de> References: <871qs7dav5.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 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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