From patchwork Mon Mar 13 16:57:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zheng Wang X-Patchwork-Id: 68949 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1300275wrd; Mon, 13 Mar 2023 10:04:37 -0700 (PDT) X-Google-Smtp-Source: AK7set9z/bQ7nH07GKXc1zUshffGAFj8SI5edod3cpmhysef/RF7mK5iYXCofGD3QE5meMrRcbB4 X-Received: by 2002:a05:6a20:baa2:b0:d4:4fbe:6d1f with SMTP id fb34-20020a056a20baa200b000d44fbe6d1fmr3979159pzb.53.1678727076850; Mon, 13 Mar 2023 10:04:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678727076; cv=none; d=google.com; s=arc-20160816; b=CGCv3SXcguGY/e6/E7iRSeXqy6Tt6FwEF3sbkvvb2n6np/D1XC+hxoVPFnKrXPvSoS jOljHcPOx4Byk3hh5Vbx2pM7mNwwCkwOSA+hYPxRhOW9//N0e72cLZ50QIoK68kW23mb BzmRtFaH1cM+l0cdgbcd7TYuge9Z+0UbBLZ2d6aX+/E+hgjpE0Iv9Q+FcNkq+G+2u2kN MSTuSNxUeobrmBO6fwnlzO4kZVjPL50JuShDKfivMdQwFMPMCrgYzEE2NZnUobNWnRJC N1kql/DUpvOD7/DxNrxVBxTXVsdyH9E8WccMC6llCvm3SezW6qpRSxbjAfTnpyRPtM6X RpEQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=LPnq8bkoN5jsFP/Rx6L5bokstFhujUhLLbUiFn+wSu0=; b=X3FgCUujUJbZ6+Jk1HBDLR7JO+UIwFKr3il6lGrgJXv+mtqXlOLlCfzu5aUNyMH0cf stBXb5Lh3tEV9wuzMiFFSmbVQrDthSCU3b7FvOVST9s0Lr3Y6vdys/lYxcnsXptUaG5p z0C1hD/uAla9BW+M5KZIBo/1d5W97S65ev3KZhQBsBfKiE1uDOXbOs+lWB8geyyni+In Ob64VJg0rtvd/ptRWba6qjOQ5+5Bv0/azvxV14x2u2H7QfyRuCjuR9W3n8iucKZDxlUW GmJXQrkLLIUMJlkXwtJt64L4vlNib4Hct6HBdCkE9zrPnMTrBGm1amXpBUGan3maUgKh CEpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=hN9UR0vg; 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=163.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 143-20020a630595000000b004fb921d0184si6771136pgf.146.2023.03.13.10.04.21; Mon, 13 Mar 2023 10:04:36 -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=@163.com header.s=s110527 header.b=hN9UR0vg; 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=163.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230084AbjCMQ7E (ORCPT + 99 others); Mon, 13 Mar 2023 12:59:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229958AbjCMQ7C (ORCPT ); Mon, 13 Mar 2023 12:59:02 -0400 Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.199]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1EE454C26; Mon, 13 Mar 2023 09:58:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=LPnq8 bkoN5jsFP/Rx6L5bokstFhujUhLLbUiFn+wSu0=; b=hN9UR0vgXxAf25NbGUdd/ 2nV1L/WAiDZIPp8LJPjnwGdNLMxqyKsUB5SVc6Co33mXFI+rmFDTA6GaojngyKdT X8AAZK5aLaPrS1WT889NPdTm2U0YeyQOFne1AK2kmX/hPoOWm8YyA/BN9dsbZnWQ KQA2ISmN8fErV1Xc9VG68g= Received: from leanderwang-LC2.localdomain (unknown [111.206.145.21]) by zwqz-smtp-mta-g2-3 (Coremail) with SMTP id _____wCX6mgSVg9kk1wSAA--.12738S2; Tue, 14 Mar 2023 00:57:54 +0800 (CST) From: Zheng Wang To: krzysztof.kozlowski@linaro.org Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, hackerzheng666@gmail.com, 1395428693sheep@gmail.com, alex000young@gmail.com, Zheng Wang Subject: [PATCH] nfc: st-nci: Fix use after free bug in ndlc_remove due to race condition Date: Tue, 14 Mar 2023 00:57:52 +0800 Message-Id: <20230313165752.3771096-1-zyytlz.wz@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-CM-TRANSID: _____wCX6mgSVg9kk1wSAA--.12738S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7WFyrJFyrKw4xJw1fWw4Uurg_yoW8WF18pr ZxWryfuFyrGF4FvFZ7WF4UGF1YkwsrtryDt3y3W343ZFnayrs0qr92yFW5uF1IqF4IqFWI y3yjq3Z8J3WkCa7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0ziHmh7UUUUU= X-Originating-IP: [111.206.145.21] X-CM-SenderInfo: h2113zf2oz6qqrwthudrp/1tbiXAwxU1Xl56QT-QABsu X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760272922837956795?= X-GMAIL-MSGID: =?utf-8?q?1760272922837956795?= This bug influences both st_nci_i2c_remove and st_nci_spi_remove. Take st_nci_i2c_remove as an example. In st_nci_i2c_probe, it called ndlc_probe and bound &ndlc->sm_work with llt_ndlc_sm_work. When it calls ndlc_recv or timeout handler, it will finally call schedule_work to start the work. When we call st_nci_i2c_remove to remove the driver, there may be a sequence as follows: Fix it by finishing the work before cleanup in ndlc_remove CPU0 CPU1 |llt_ndlc_sm_work st_nci_i2c_remove | ndlc_remove | st_nci_remove | nci_free_device| kfree(ndev) | //free ndlc->ndev | |llt_ndlc_rcv_queue |nci_recv_frame |//use ndlc->ndev Fixes: 35630df68d60 ("NFC: st21nfcb: Add driver for STMicroelectronics ST21NFCB NFC chip") Reviewed-by: Krzysztof Kozlowski Signed-off-by: Zheng Wang --- drivers/nfc/st-nci/ndlc.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/nfc/st-nci/ndlc.c b/drivers/nfc/st-nci/ndlc.c index 755460a73c0d..d2aa9f766738 100644 --- a/drivers/nfc/st-nci/ndlc.c +++ b/drivers/nfc/st-nci/ndlc.c @@ -282,13 +282,15 @@ EXPORT_SYMBOL(ndlc_probe); void ndlc_remove(struct llt_ndlc *ndlc) { - st_nci_remove(ndlc->ndev); - /* cancel timers */ del_timer_sync(&ndlc->t1_timer); del_timer_sync(&ndlc->t2_timer); ndlc->t2_active = false; ndlc->t1_active = false; + /* cancel work */ + cancel_work_sync(&ndlc->sm_work); + + st_nci_remove(ndlc->ndev); skb_queue_purge(&ndlc->rcv_q); skb_queue_purge(&ndlc->send_q);