Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6284222iob; Tue, 10 May 2022 14:53:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzcxGSD/1f4JKT4+zYJCaJ45DKlwH05wLWDoeBnF2ixXsTKcn6uHpOI+jnJed89bPV56Wh9 X-Received: by 2002:a17:902:bf45:b0:15c:df47:3d6 with SMTP id u5-20020a170902bf4500b0015cdf4703d6mr22148015pls.58.1652219634777; Tue, 10 May 2022 14:53:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652219634; cv=none; d=google.com; s=arc-20160816; b=fz3+l3zPzsFaTs5futSo0du8ctf3Hru5794Fvkxm1GRRB8xrWkpqNGc+wNrXvpVXr+ YXRguN7Xw7RS1f3yQylkaHaQUezLs6r8BMCrKSw5AFNOhIJNcSA/ZhXgyd2z8jSpHSjP neUSaqBTACwwt+wAducELoFo+3uZp5Zok6rMr12ygvH+rTV3a2yTKVH2yD9lVtfCc7qp TaC9Dtt2D1cxWr9II0AkMhAgHtvKVY08TmY0Wc3YulRzzOQGim3buBA7kBwYKjNenR9Z e/IdZdfTbBl5oVIy3mSEkQ/LKEX1mHjNODe3uBQMosj0mVKtPmhJlsI0pz2cPn3R/Q/p OYsw== 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=pQWxItXJOe8rMwpUJjgUaW2SBCSgkpF60Gytbov152s=; b=ZBoSx8ZSUP80jL3wR/HuW7mizLrnA4r+iadH3ngVX/nWvyWGOZ0eGUVgbyyYp7EPeO jsLD7h8ZCJcnz/aaANqqpaqLvbWz2y5uB3zPCQLgUUn2+aWqohc8M8ujqTx5gGEZwTRg ga3UeLyYALXiHoDvhrr50II14vHJaX+RccygP6XHHO/TiSZBlA2lNv4tJLEWZHY87Vam q7fuBNJIdeSqHmCmpB38AlYXzJ6vQq4h5b2kFDzL+ZtI3A1x0WNz3UiBMNx/4Xsn7P4q Cr9nV2mW9NtHJRSud+wCCPp2P0Oz5acMCcMgS3+EWzTaq4Dib/CXjqqiM6fbB2yW3OGc Qu3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=VjiV9jQu; 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 q4-20020a170902bd8400b0015ed25c4e18si186325pls.463.2022.05.10.14.53.38; Tue, 10 May 2022 14:53:54 -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=VjiV9jQu; 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 S1344150AbiEJOZr (ORCPT + 99 others); Tue, 10 May 2022 10:25:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244925AbiEJNvc (ORCPT ); Tue, 10 May 2022 09:51:32 -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 313312992F4; Tue, 10 May 2022 06:38:02 -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 344C7618C1; Tue, 10 May 2022 13:37:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 267EBC385A6; Tue, 10 May 2022 13:37:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1652189854; bh=9K72YIuqnFZ74bLMN4NZLheBjx01HBS0DoUJAc1ZgOQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VjiV9jQuntVHYVEuSKFgOG5xIbtpb9LqqtffpaoZnoNFvy4QMmTND2oaO9H+JOLqY W8qmp6tSf8MYfl23DY1h+Jlr0L7ptIwLIUqpvO3bPUc/VWgvfX/heUZkvQmmIjfmOB J/R6jFbr07RGZ58rACZUeMfXHYZ+Vy07Eh7P8bpQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Duoming Zhou , "David S. Miller" Subject: [PATCH 5.17 051/140] nfc: nfcmrvl: main: reorder destructive operations in nfcmrvl_nci_unregister_dev to avoid bugs Date: Tue, 10 May 2022 15:07:21 +0200 Message-Id: <20220510130743.081484314@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220510130741.600270947@linuxfoundation.org> References: <20220510130741.600270947@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 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,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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: Duoming Zhou commit d270453a0d9ec10bb8a802a142fb1b3601a83098 upstream. There are destructive operations such as nfcmrvl_fw_dnld_abort and gpio_free in nfcmrvl_nci_unregister_dev. The resources such as firmware, gpio and so on could be destructed while the upper layer functions such as nfcmrvl_fw_dnld_start and nfcmrvl_nci_recv_frame is executing, which leads to double-free, use-after-free and null-ptr-deref bugs. There are three situations that could lead to double-free bugs. The first situation is shown below: (Thread 1) | (Thread 2) nfcmrvl_fw_dnld_start | ... | nfcmrvl_nci_unregister_dev release_firmware() | nfcmrvl_fw_dnld_abort kfree(fw) //(1) | fw_dnld_over | release_firmware ... | kfree(fw) //(2) | ... The second situation is shown below: (Thread 1) | (Thread 2) nfcmrvl_fw_dnld_start | ... | mod_timer | (wait a time) | fw_dnld_timeout | nfcmrvl_nci_unregister_dev fw_dnld_over | nfcmrvl_fw_dnld_abort release_firmware | fw_dnld_over kfree(fw) //(1) | release_firmware ... | kfree(fw) //(2) The third situation is shown below: (Thread 1) | (Thread 2) nfcmrvl_nci_recv_frame | if(..->fw_download_in_progress)| nfcmrvl_fw_dnld_recv_frame | queue_work | | fw_dnld_rx_work | nfcmrvl_nci_unregister_dev fw_dnld_over | nfcmrvl_fw_dnld_abort release_firmware | fw_dnld_over kfree(fw) //(1) | release_firmware | kfree(fw) //(2) The firmware struct is deallocated in position (1) and deallocated in position (2) again. The crash trace triggered by POC is like below: BUG: KASAN: double-free or invalid-free in fw_dnld_over Call Trace: kfree fw_dnld_over nfcmrvl_nci_unregister_dev nci_uart_tty_close tty_ldisc_kill tty_ldisc_hangup __tty_hangup.part.0 tty_release ... What's more, there are also use-after-free and null-ptr-deref bugs in nfcmrvl_fw_dnld_start. If we deallocate firmware struct, gpio or set null to the members of priv->fw_dnld in nfcmrvl_nci_unregister_dev, then, we dereference firmware, gpio or the members of priv->fw_dnld in nfcmrvl_fw_dnld_start, the UAF or NPD bugs will happen. This patch reorders destructive operations after nci_unregister_device in order to synchronize between cleanup routine and firmware download routine. The nci_unregister_device is well synchronized. If the device is detaching, the firmware download routine will goto error. If firmware download routine is executing, nci_unregister_device will wait until firmware download routine is finished. Fixes: 3194c6870158 ("NFC: nfcmrvl: add firmware download support") Signed-off-by: Duoming Zhou Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- drivers/nfc/nfcmrvl/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/drivers/nfc/nfcmrvl/main.c +++ b/drivers/nfc/nfcmrvl/main.c @@ -183,6 +183,7 @@ void nfcmrvl_nci_unregister_dev(struct n { struct nci_dev *ndev = priv->ndev; + nci_unregister_device(ndev); if (priv->ndev->nfc_dev->fw_download_in_progress) nfcmrvl_fw_dnld_abort(priv); @@ -191,7 +192,6 @@ void nfcmrvl_nci_unregister_dev(struct n if (gpio_is_valid(priv->config.reset_n_io)) gpio_free(priv->config.reset_n_io); - nci_unregister_device(ndev); nci_free_device(ndev); kfree(priv); }