From patchwork Mon Jun 19 07:27:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Jarzmik X-Patchwork-Id: 9795403 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7F6C46020B for ; Mon, 19 Jun 2017 07:31:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7FC1B26E4A for ; Mon, 19 Jun 2017 07:31:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7443727FA3; Mon, 19 Jun 2017 07:31:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FROM autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 000E726E4A for ; Mon, 19 Jun 2017 07:31:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=YbP4rQGHP4u1kBNaYKFYGm9Dp5XiWP6IpzctbH+5cn0=; b=EYntXMMeUOxqrSqb+C5TgwY8eJ BXvdnlxZCrQf1qQ4im4O0RQET0W/3g0bEMgzra7ew7jzXwxdDZt31rF7kmgAxsKVh1q3ErcnsR/Wk vrGQ3cDcMBqMiFwphGxgAzaLaH9QwGdfZUg6x4vLm31lhu3lZx9E97WfmP+zy6BplxoHIyErC/Dar zLoVZPw8WeBW14NV3Mz6XfrzleNmr46DTAMvqMnVKqSwMfcrmVGVBOff74PBSKOpaFPmIO1wtOgl3 2IrDjHkvIrJp6KUjmjVOqpYHT9MfhgNeRIr3f10aTXbcXWI+3/L6hYRKFhiOmwrOi+SCqtGXRtsJk zyysEiXQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dMr9P-0006yW-LL; Mon, 19 Jun 2017 07:31:07 +0000 Received: from merlin.infradead.org ([2001:8b0:10b:1231::1]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dMr7Q-0003Im-Kn for linux-arm-kernel@bombadil.infradead.org; Mon, 19 Jun 2017 07:29:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=xnNEwMWBsCwjcal3Eu0fC5lTFuXivBBzv3na2fMgnmE=; b=VWp97FBqxf8xJPXDHn+5/2zC6 WT1/VenK8RF1/20WJwbyezBYdGgrT5E9onF32DKeMGCP84dQB5BgoDsbVAYuoaOZ+cS/S5XaBVmEw M5DmSq3RPcUuW5GrC5RAaMC8DRaOHcaDUyCaz08BwoMKXiJKKgUtI2gUePcaHBAJ8J4nQFiiSu2zX 463sdpVmpsHwUZDsXDsJhWAOccl7LZBgaLNJ5gc5rRz45HezpgsEa+BwUIKQxNoKb+h8KhF52iPPS /0uVmVLWuGR9EcVjSNvHKEVMtMhZRMj0NtD1XLuOoOyiPVXUHbyWQoGALC7oBzf9bubqyAfh/fhs4 sKzAgIHQw==; Received: from smtp03.smtpout.orange.fr ([80.12.242.125] helo=smtp.smtpout.orange.fr) by merlin.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dMr7N-0004Al-H6 for linux-arm-kernel@lists.infradead.org; Mon, 19 Jun 2017 07:29:02 +0000 Received: from belgarion.home ([109.220.219.63]) by mwinf5d26 with ME id aXUG1v00S1Ng3jh03XUZ51; Mon, 19 Jun 2017 09:28:35 +0200 X-ME-Helo: belgarion.home X-ME-Date: Mon, 19 Jun 2017 09:28:35 +0200 X-ME-IP: 109.220.219.63 From: Robert Jarzmik To: Dmitry Torokhov , Lee Jones , Jaroslav Kysela , Takashi Iwai , Daniel Mack , Haojian Zhuang , Robert Jarzmik , Liam Girdwood , Mark Brown , Lars-Peter Clausen , Charles Keepax Subject: [PATCH v2 12/12] ASoC: Fix use-after-free at card unregistration Date: Mon, 19 Jun 2017 09:27:09 +0200 Message-Id: <1497857229-12049-13-git-send-email-robert.jarzmik@free.fr> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1497857229-12049-1-git-send-email-robert.jarzmik@free.fr> References: <1497857229-12049-1-git-send-email-robert.jarzmik@free.fr> X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alsa-devel@alsa-project.org, Takashi Iwai , patches@opensource.wolfsonmicro.com, linux-kernel@vger.kernel.org, stable@vger.kernel.org, linux-input@vger.kernel.org, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Takashi Iwai soc_cleanup_card_resources() call snd_card_free() at the last of its procedure. This turned out to lead to a use-after-free. PCM runtimes have been already removed via soc_remove_pcm_runtimes(), while it's dereferenced later in soc_pcm_free() called via snd_card_free(). The fix is simple: just move the snd_card_free() call to the beginning of the whole procedure. This also gives another benefit: it guarantees that all operations have been shut down before actually releasing the resources, which was racy until now. Reported-and-tested-by: Robert Jarzmik Cc: Signed-off-by: Takashi Iwai --- sound/soc/soc-core.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sound/soc/soc-core.c b/sound/soc/soc-core.c index 2722bb0c5573..98d60f471c5d 100644 --- a/sound/soc/soc-core.c +++ b/sound/soc/soc-core.c @@ -2286,6 +2286,9 @@ static int soc_cleanup_card_resources(struct snd_soc_card *card) list_for_each_entry(rtd, &card->rtd_list, list) flush_delayed_work(&rtd->delayed_work); + /* free the ALSA card at first; this syncs with pending operations */ + snd_card_free(card->snd_card); + /* remove and free each DAI */ soc_remove_dai_links(card); soc_remove_pcm_runtimes(card); @@ -2300,9 +2303,7 @@ static int soc_cleanup_card_resources(struct snd_soc_card *card) if (card->remove) card->remove(card); - snd_card_free(card->snd_card); return 0; - } /* removes a socdev */