Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp816750rwb; Mon, 26 Sep 2022 06:17:35 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5UsjqhtzguewYtysBm9tcSM3lTBkdfYtlgjxZJmKG0cz5pcrebcgTr2I0+XjDGSqGsYA5p X-Received: by 2002:a05:6a00:4c97:b0:54d:3cf5:781 with SMTP id eb23-20020a056a004c9700b0054d3cf50781mr23668038pfb.73.1664198255765; Mon, 26 Sep 2022 06:17:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664198255; cv=none; d=google.com; s=arc-20160816; b=Q0MxOo3Gb92GszU3rLes2bfzVD8qfjA8xQWtxeNWTbPm+e7xX8UrperIve46VkKnWB /hTeGBogedIYvGOt2xDnjyCpqXk6wIcQ/9kG4ZPvY1afQFzlkPvcmL4y3FLTqQ31oSRw Zpse2RNrkl3v20Mgss55jpwG/cAatOsHcMd/p84JKg9QSP4IU8swn4Hg1iR/u0aamjWo oaKo0HShI48hiB/T6iPnKn/chDF8Dz+5gN0Z5Fr83elBskNe8Z6rQvSNkFm0RI6EK/dc N+I7Xa+DKPz+nmM2UUW2oo1Qz3mIuA9cKouOYiM9ROP8pQFPyBe15b4F/kHljWNYlZ7A ifZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=0c/RHbOLzx058LfDnVbbnyM/EdtdF3CVvldBW/rCmrQ=; b=ReXrjn0/+Sno1gWUE+5sGG8qlC+go/f8nL2CmIen1n4fCq88rVc4OqTO7uNjPZBwmW BPiU6EDNS0ntFVcIrAbh11ncrgS6WhReMehNXuQFJ83Swg6I6PbtkmhpN+lOhpFehDSR RI9AKlA1UI4vsg0CgCsPoR0e4HGkD8zUxyoa3Hi4mAh+a6sNxzgvh7ZeCspXhppfc0rl JcCBpvTStrx08Ci74GroGzoWOmsLL+VeoR0aYcIyqvmqFkJV3pVAOpr3wuvXAWayHeQG hgpc38W8jwOqAxTdQS/QXQ04vo/yta3wZB2i/Bl7AQMaNIvf+j9JsJILZ+KS3u0YDzuN nXjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BUhFF6zj; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h71-20020a63834a000000b0043c87ece306si1361493pge.534.2022.09.26.06.17.22; Mon, 26 Sep 2022 06:17:35 -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=@linuxfoundation.org header.s=korg header.b=BUhFF6zj; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239414AbiIZMH0 (ORCPT + 99 others); Mon, 26 Sep 2022 08:07:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238793AbiIZMDJ (ORCPT ); Mon, 26 Sep 2022 08:03:09 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0816A7D1F9; Mon, 26 Sep 2022 03:53:50 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C193F60769; Mon, 26 Sep 2022 10:42:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BBEE2C433C1; Mon, 26 Sep 2022 10:42:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1664188955; bh=ekEYV8am7I3UA39OEA5d3IcLfPZlN6rAm5VHcwf7ZNQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BUhFF6zjLocop8E8hgXl6xf7chthZsoTOgfo5hAgDa5cSrCsMu7hMStxdqMRMEKCI J6K080h/tICIG770m8FJ7EWCMImjnju+7G5rzjcfywkklcHxEArBXX+ay7ETzfkXMO h9lxtELdvNYWGu9m9lpCjoncooY8qnXLdsY6XD+A= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Rondreis , Takashi Iwai Subject: [PATCH 5.19 026/207] ALSA: core: Fix double-free at snd_card_new() Date: Mon, 26 Sep 2022 12:10:15 +0200 Message-Id: <20220926100807.648115115@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220926100806.522017616@linuxfoundation.org> References: <20220926100806.522017616@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 From: Takashi Iwai commit c3afa2a402d1ecefa59f88d55d9e765f52f75bd9 upstream. 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 Link: https://lore.kernel.org/r/20220919123516.28222-1-tiwai@suse.de Signed-off-by: Takashi Iwai Signed-off-by: Greg Kroah-Hartman --- sound/core/init.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) --- a/sound/core/init.c +++ b/sound/core/init.c @@ -178,10 +178,8 @@ int snd_card_new(struct device *parent, 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; @@ -231,7 +229,7 @@ int snd_devm_card_new(struct device *par 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; } @@ -293,6 +291,8 @@ static int snd_card_init(struct snd_card 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 */