From patchwork Thu Apr 14 05:31:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Duoming Zhou X-Patchwork-Id: 12812917 X-Patchwork-Delegate: kuba@kernel.org 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 DC983C433FE for ; Thu, 14 Apr 2022 05:31:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239856AbiDNFeP (ORCPT ); Thu, 14 Apr 2022 01:34:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239866AbiDNFeM (ORCPT ); Thu, 14 Apr 2022 01:34:12 -0400 Received: from zju.edu.cn (mail.zju.edu.cn [61.164.42.155]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9CFD83BA6F; Wed, 13 Apr 2022 22:31:47 -0700 (PDT) Received: from ubuntu.localdomain (unknown [10.15.192.164]) by mail-app4 (Coremail) with SMTP id cS_KCgDHr_KqsVdi+oM1AQ--.59165S5; Thu, 14 Apr 2022 13:31:41 +0800 (CST) From: Duoming Zhou To: krzk@kernel.org, linux-kernel@vger.kernel.org Cc: davem@davemloft.net, gregkh@linuxfoundation.org, alexander.deucher@amd.com, akpm@linux-foundation.org, broonie@kernel.org, netdev@vger.kernel.org, linma@zju.edu.cn, Duoming Zhou Subject: [PATCH 3/3] drivers: nfc: nfcmrvl: fix use-after-free bug in nfcmrvl_fw_dnld_start() Date: Thu, 14 Apr 2022 13:31:22 +0800 Message-Id: <67e074f0c8e538caa2d2cd0eb49936e112249839.1649913521.git.duoming@zju.edu.cn> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-CM-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8JVW8JrUvcSsGvfC2KfnxnU UI43ZEXa7VUbXdbUUUUUU== X-CM-SenderInfo: qssqjiasttq6lmxovvfxof0/1tbiAgITAVZdtZLd3gAisf Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org There are potential use-after-free bug in nfcmrvl_fw_dnld_start(). The race between nfcmrvl_disconnect() and nfcmrvl_fw_dnld_start() can be shown as below: (USE) | (FREE) | nfcmrvl_disconnect | nfcmrvl_nci_unregister_dev | nfcmrvl_fw_dnld_abort | fw_dnld_over nfcmrvl_fw_dnld_start | release_firmware ... | kfree(fw) //(1) priv->fw_dnld.fw->data //(2)| ... ... | The firmware is deallocate in position (1), but it is used in position (2), which leads to UAF bug. This patch add spin_lock() and check in nfcmrvl_fw_dnld_start() in order to synchronize with other threads that could free firmware. Therefore, the UAF bug could be prevented. Fixes: 3194c6870158e3 ("NFC: nfcmrvl: add firmware download support") Signed-off-by: Duoming Zhou --- drivers/nfc/nfcmrvl/fw_dnld.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/nfc/nfcmrvl/fw_dnld.c b/drivers/nfc/nfcmrvl/fw_dnld.c index bb9e7e2bdec..910f6eaec65 100644 --- a/drivers/nfc/nfcmrvl/fw_dnld.c +++ b/drivers/nfc/nfcmrvl/fw_dnld.c @@ -511,7 +511,10 @@ int nfcmrvl_fw_dnld_start(struct nci_dev *ndev, const char *firmware_name) return -ENOENT; } - fw_dnld->header = (const struct nfcmrvl_fw *) priv->fw_dnld.fw->data; + spin_lock(&priv->fw_dnld.lock); + if (priv->fw_dnld.fw) + fw_dnld->header = (const struct nfcmrvl_fw *)priv->fw_dnld.fw->data; + spin_unlock(&priv->fw_dnld.lock); if (fw_dnld->header->magic != NFCMRVL_FW_MAGIC || fw_dnld->header->phy != priv->phy) {