From patchwork Tue Apr 12 10:56:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Duoming Zhou X-Patchwork-Id: 12810645 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 3466EC4167B for ; Tue, 12 Apr 2022 12:03:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348854AbiDLMFo (ORCPT ); Tue, 12 Apr 2022 08:05:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343909AbiDLMCK (ORCPT ); Tue, 12 Apr 2022 08:02:10 -0400 Received: from zju.edu.cn (spam.zju.edu.cn [61.164.42.155]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7156C673D3; Tue, 12 Apr 2022 03:57:09 -0700 (PDT) Received: from ubuntu.localdomain (unknown [10.15.192.164]) by mail-app2 (Coremail) with SMTP id by_KCgBn3hX1WlVixrabAQ--.10506S3; Tue, 12 Apr 2022 18:57:00 +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, broonie@kernel.org, akpm@linux-foundation.org, netdev@vger.kernel.org, pabeni@redhat.com, Duoming Zhou Subject: [PATCH V3 1/2] drivers: nfc: nfcmrvl: fix double free bugs caused by fw_dnld_over() Date: Tue, 12 Apr 2022 18:56:52 +0800 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-CM-TRANSID: by_KCgBn3hX1WlVixrabAQ--.10506S3 X-Coremail-Antispam: 1UD129KBjvJXo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AgwSAVZdtZJwywAFsP Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org There are potential double free bugs in nfcmrvl usb driver among fw_dnld_rx_work(), fw_dnld_timeout() and nfcmrvl_nci_unregister_dev(). All these three functions will call fw_dnld_over(). The fw_dnld_rx_work() is a work item, the fw_dnld_timeout() is a timer handler and the nfcmrvl_nci_unregister_dev() is called when nfcmrvl_nci device is detaching. So these three functions could execute concurrently. The race between fw_dnld_rx_work() and nfcmrvl_nci_unregister_dev() can be shown as below: (Thread 1) | (Thread 2) | fw_dnld_rx_work | fw_dnld_over | release_firmware | kfree(fw); //(1) nfcmrvl_disconnect | nfcmrvl_nci_unregister_dev | nfcmrvl_fw_dnld_abort | fw_dnld_over | ... if (priv->fw_dnld.fw) | release_firmware | kfree(fw); //(2) | ... | priv->fw_dnld.fw = NULL; When the fw_dnld_rx_work work item is executing, we detach the device. The release_firmware() will deallocate firmware in position (1), but firmware will be deallocated again in position (2), which leads to double free. The race between fw_dnld_rx_work() and fw_dnld_timeout() can be shown as below: (Thread 1) | (Thread 2) | fw_dnld_rx_work | fw_dnld_over | release_firmware | kfree(fw); //(1) fw_dnld_timeout | fw_dnld_over | ... if (priv->fw_dnld.fw) | release_firmware | kfree(fw); //(2) | ... | priv->fw_dnld.fw = NULL; The release_firmware() will deallocate firmware in position (1), but firmware will be deallocated again in position (2), which leads to double free. The race between fw_dnld_timeout() and nfcmrvl_nci_unregister_dev() can be shown as below. (Thread 1) | (Thread 2) | nfcmrvl_disconnect | nfcmrvl_nci_unregister_dev | nfcmrvl_fw_dnld_abort | fw_dnld_over | release_firmware | kfree(fw); //(1) fw_dnld_timeout | fw_dnld_over | ... if (priv->fw_dnld.fw) | release_firmware | kfree(fw); //(2) | ... | priv->fw_dnld.fw = NULL; The release_firmware() will deallocate firmware in position (1), but firmware will be deallocated again in position (2), which leads to double free. This patch adds spin_lock_irq in fw_dnld_over() in order to synchronize among different threads that call fw_dnld_over(). The priv->fw_dnld.fw will be set to NULL after work item is finished and fw_dnld_over() called by other threads will check whether priv->fw_dnld.fw is NULL. So the double free bug could be prevented. Fixes: 3194c6870158e3 ("NFC: nfcmrvl: add firmware download support") Signed-off-by: Duoming Zhou Reviewed-by: Lin Ma --- Changes in V3: - Make commit message more clearer. - Use spin_lock_irq to synchronize. drivers/nfc/nfcmrvl/fw_dnld.c | 3 +++ drivers/nfc/nfcmrvl/fw_dnld.h | 2 ++ 2 files changed, 5 insertions(+) diff --git a/drivers/nfc/nfcmrvl/fw_dnld.c b/drivers/nfc/nfcmrvl/fw_dnld.c index e83f65596a8..c22a4556db5 100644 --- a/drivers/nfc/nfcmrvl/fw_dnld.c +++ b/drivers/nfc/nfcmrvl/fw_dnld.c @@ -92,12 +92,14 @@ static struct sk_buff *alloc_lc_skb(struct nfcmrvl_private *priv, uint8_t plen) static void fw_dnld_over(struct nfcmrvl_private *priv, u32 error) { + spin_lock_irq(&priv->fw_dnld.lock); if (priv->fw_dnld.fw) { release_firmware(priv->fw_dnld.fw); priv->fw_dnld.fw = NULL; priv->fw_dnld.header = NULL; priv->fw_dnld.binary_config = NULL; } + spin_unlock_irq(&priv->fw_dnld.lock); atomic_set(&priv->ndev->cmd_cnt, 0); @@ -451,6 +453,7 @@ int nfcmrvl_fw_dnld_init(struct nfcmrvl_private *priv) if (!priv->fw_dnld.rx_wq) return -ENOMEM; skb_queue_head_init(&priv->fw_dnld.rx_q); + spin_lock_init(&priv->fw_dnld.lock); return 0; } diff --git a/drivers/nfc/nfcmrvl/fw_dnld.h b/drivers/nfc/nfcmrvl/fw_dnld.h index 7c4d91b0191..e4fe0fb8aff 100644 --- a/drivers/nfc/nfcmrvl/fw_dnld.h +++ b/drivers/nfc/nfcmrvl/fw_dnld.h @@ -75,6 +75,8 @@ struct nfcmrvl_fw_dnld { struct sk_buff_head rx_q; struct timer_list timer; + /* To synchronize among different threads that call fw_dnld_over.*/ + spinlock_t lock; }; int nfcmrvl_fw_dnld_init(struct nfcmrvl_private *priv); From patchwork Tue Apr 12 10:56:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Duoming Zhou X-Patchwork-Id: 12810626 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 3688BC4332F for ; Tue, 12 Apr 2022 12:00:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344538AbiDLMCT (ORCPT ); Tue, 12 Apr 2022 08:02:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244281AbiDLMCK (ORCPT ); Tue, 12 Apr 2022 08:02:10 -0400 Received: from zju.edu.cn (spam.zju.edu.cn [61.164.42.155]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B43EE673DD; Tue, 12 Apr 2022 03:57:10 -0700 (PDT) Received: from ubuntu.localdomain (unknown [10.15.192.164]) by mail-app2 (Coremail) with SMTP id by_KCgBn3hX1WlVixrabAQ--.10506S4; Tue, 12 Apr 2022 18:57:03 +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, broonie@kernel.org, akpm@linux-foundation.org, netdev@vger.kernel.org, pabeni@redhat.com, Duoming Zhou Subject: [PATCH 2/2] drivers: nfc: nfcmrvl: fix double free bug in nfc_fw_download_done() Date: Tue, 12 Apr 2022 18:56:53 +0800 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-CM-TRANSID: by_KCgBn3hX1WlVixrabAQ--.10506S4 X-Coremail-Antispam: 1UD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xof0/1tbiAgwSAVZdtZJwywAHsN Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org There are potential double free bug in nfc_fw_download_done(). The timer handler fw_dnld_timeout() and work item fw_dnld_rx_work() could both reach in fw_dnld_over() and nfc_fw_download_done() is not protected by any lock, which leads to double free. The race between fw_dnld_rx_work() and fw_dnld_timeout() can be shown as below: (Thread 1) | (Thread 2) fw_dnld_timeout | fw_dnld_rx_work | fw_dnld_over fw_dnld_over | nfc_fw_download_done nfc_fw_download_done | nfc_genl_fw_download_done nfc_genl_fw_download_done| nlmsg_free(msg) //(1) nlmsg_free(msg) //(2) | ... ... | The nlmsg_free() will deallocate sk_buff in position (1), but nlmsg_free will be deallocated again in position (2), which leads to double free. This patch adds spin_lock_irq() and check in fw_dnld_over() in order to synchronize among different threads that call fw_dnld_over(). So the double free bug could be prevented. Fixes: 3194c6870158e3 ("NFC: nfcmrvl: add firmware download support") Signed-off-by: Duoming Zhou Reviewed-by: Lin Ma Reported-by: kernel test robot Reported-by: kernel test robot --- 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 c22a4556db5..7586a2f678d 100644 --- a/drivers/nfc/nfcmrvl/fw_dnld.c +++ b/drivers/nfc/nfcmrvl/fw_dnld.c @@ -116,7 +116,10 @@ static void fw_dnld_over(struct nfcmrvl_private *priv, u32 error) nfcmrvl_chip_halt(priv); } - nfc_fw_download_done(priv->ndev->nfc_dev, priv->fw_dnld.name, error); + spin_lock_irq(&priv->fw_dnld.lock); + if (dev->fw_download_in_progress) + nfc_fw_download_done(priv->ndev->nfc_dev, priv->fw_dnld.name, error); + spin_unlock_irq(&priv->fw_dnld.lock); } static void fw_dnld_timeout(struct timer_list *t)