From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796554339; b=X52NJrt5XmqeGfjyLoSx+zppaj7InpOuGcIECMotgFNC6XZHSpqHGY+2JcH4pU1B1X3K f9254GKW6iPL32wumH95RI28zcRuAP3QMvjupj78Y+SeH3bH1UQyjxwHOdwQ/4IqdAMy3 OWww8pQcsl6r3O1RHJpqiybEs9D4bLaW8ubctxgWe5LW9GHweiyEIk3QaxmB4BESFmd0M JekpyMQUuRx5a98yMqQi41HxhsRtyHDBvEjzumorABdWjTZleCP1prohVD12ApqB+MXfo z2oAxbR59cnklWP5/1ils7Y0p12USxYpjP2Xss7suIl1pPe0uXQqF30O8v72uld4Z8A== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796554339; h=Received:DKIM-Signature:Received:From:To:Cc:Subject:Date:Message-ID: In-Reply-To:References:Content-Type:Content-Transfer-Encoding: MIME-Version:X-Mailer:X-Developer-Signature:X-Developer-Key: X-Endpoint-Received:X-Original-From:Reply-To; bh=HeSxZe4u/jNi1LSBO4qO9cc0DpjYNx/smex/3zpgKlY=; b=ZUmFU21yZYFK0hGtBIGtBtL8hT7Cj6h610Ex9kVOcMfKurQJlY3L1catUQfF2k82pneE eUW13ToLtdAVeX4ScxlwPdgdTZmfXL02X20aAZGQMFGqzJdfar5VoWOvOE5g5VlQ22yBB ejWWPmuiIjPNhsePZtZhDkp/EnSgPiWkU7tzZmcdveCd7N7W08NYYwW8Ho1q0wywUC7mE XXhq4xhHWfLHpyj+VQh0qPV1mnI+udiHQLfC9lvpCQ5SVVwJYU+v3uNDoHW9Pw3KFSmXX 89BGgYcRFpyDMDaWa8JfqA3A6Yh5xo1TSq5RFfcbtaVCZ69LoGL67Bjkp/61s8Rew1A== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=nv4vH4Ir; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35630606A696; Sun, 6 Dec 2026 10:52:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=nv4vH4Ir Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796554339; b=ISw+Rnqe/aI8Z/LH9Jnv/L42PoY71S6hf1Cr04ACDUTUnsXdAZcQ9uvkUjd6sIcLT5RH db2C/KabV/wfSnYF80YQcgVFCP0OAoLIKKCzOfhIJvbQetrSgSIBManjqanaPD9qppioC nOCd4cL78oWI73JXFa92d6nlKShV6AqDew67kuJKiU/Ey48Y2U12cvhT2lyX7v5iP0nNB eQ2dtA/GWn/FKkQ7eJ8lVmtbAflWpg+trY1TniJhEKL2+yCNwjKJ+xoqZ5FipPR9zOTWv co2DhRbDjRgNfOktbhXGSJviXmVrBh5jDLePKQehldDY3PpfN0p+TdJyBrqGv2VyoMA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796554339; h=DKIM-Signature:Received:From:To:Cc:Subject:Date:Message-ID: In-Reply-To:References:Content-Type:Content-Transfer-Encoding: MIME-Version:X-Mailer:X-Developer-Signature:X-Developer-Key: X-Endpoint-Received:X-Original-From:Reply-To; bh=HeSxZe4u/jNi1LSBO4qO9cc0DpjYNx/smex/3zpgKlY=; b=lHg/+Sq6egmDAmeIYMA66lCgmAjeHeW+5KqekUCW0zlq4dMNdHOpRviW1HFQpvC1ZiFf Q7XdrpMEcuxyzlNUjSGblStBRXw9/du/ByOVT53L7gT8tcOY7Ur0LyYjk5VS3E/rtYp4g zuQ1pVVAxPqoF+63Ez5nbId6cp8+tann0gvOfxeBsTUr4NNsF41mhtItKzk3Izw9QyNb1 oR6W2lbI4tdU9prPoUQ6x6U9x+QTTD/fP+hYXSQzR5RViVetfSK8ev5t9ThsK+s/2VUkG +G7dB0b1MT6VBlE/93b7Q+kEEHlNeA6DwRriizaEm7ehLTOw22XGRF3wzFnDeJQb4kg== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 7B8601F00A15; Sun, 6 Dec 2026 10:52:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796554339; bh=HeSxZe4u/jNi1LSBO4qO9cc0DpjYNx/smex/3zpgKlY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:Reply-To; b=nv4vH4IrHqCrxfUkSI8dChr7CrWAqVLtQkc+A2XZ971IdtMTXXgYyK7JKQ0eVmw2d tAKvwJDdqwVVL5WkwkormfO/2KBaSG42L8sCJWXBd3Ml1KHUjLxl+V3PijlilHTkQ/ GAUw+hHh/k0b+cqgsVCwVwUvRbBfclcqnrLIuWb9fKM9CYK9AnPl9HOont/PTl1v58 Vr1Uifzvq9jNIvbF6CIdQHzhRSwY4uF9tAGJlCxF3xN/BqCMgFaWidLTjZfVeZkdyU gcsGupWSFU4EFf8deKLHSHUETrvKmA38x8SiK55rGwL+++4IHyVc10HrafEh/13dBh kdHf0THOYzupw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 0FB2550720; Sun, 6 Dec 2026 10:52:17 +0000 (UTC) From: Bin Luo via B4 Relay To: David Heidelberg , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman Cc: Krzysztof Kozlowski , oe-linux-nfc@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH net v3 2/3] nfc: nfcsim: stop link and works before unregistering digital device Date: Sun, 06 Dec 2026 10:52:15 -0000 Message-ID: <20261206105215.479-3-luob87709@gmail.com> In-Reply-To: <20261206105215.479-1-luob87709@gmail.com> References: <20261206105215.479-1-luob87709@gmail.com> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796554335; l=4721; i=luob87709@gmail.com; h=from:subject:message-id; bh=rJ5J83wlp5Z79rNygt+oKO2gTygktHYY97CGg/afyDo=; b=PsxodhUq+tqxUMKiCqgvRAKVn9MG86eR9iukbeCxZPw6jb1A7FG7QtPaEc2c7I4gChLzDyG1f VzKOBsR69XvAH9I8TwrpW0ceHH3PYfeIjI3jw5hpI5etYfTn3t2Tzwp X-Developer-Key: i=luob87709@gmail.com; a=ed25519; pk=qac/JyfPpjPD91IsbcZY5AvHG2LdM5meIPeVzh4kuN0= X-Endpoint-Received: by B4 Relay for luob87709@gmail.com/default with auth_id=1739 X-Original-From: Bin Luo Reply-To: luob87709@gmail.com From: Bin Luo Commit 5e7b30d24a5b ("nfc: nfcsim: fix use after free during module unload") partially addressed a use-after-free in digital_send_cmd_complete() by returning early in nfcsim_recv_wq() when !dev->up, but it did not close the race completely: nfcsim_device_free() still calls nfc_digital_unregister_device() before shutting down link_in and stopping recv_work. While nfc_digital_unregister_device() drains and frees every pending struct digital_cmd from ddev->cmd_queue, nfcsim_recv_wq() can still wake up on an incoming frame from the peer device (or timeout) and invoke dev->cb() -> digital_send_cmd_complete() on the freed struct digital_cmd: BUG: KASAN: slab-use-after-free in digital_send_cmd_complete+0x76/0xa0 Write of size 8 at addr ffff888007d9bb20 by task kworker/3:4/66 Workqueue: events nfcsim_recv_wq [nfcsim] Call Trace: digital_send_cmd_complete+0x76/0xa0 nfcsim_recv_wq+0x30a/0x6c0 [nfcsim] Freed by task 64: kfree+0x308/0x530 nfc_digital_unregister_device+0x1e9/0x2e0 nfcsim_device_free+0x2d/0x1c0 [nfcsim] Shut down dev->link_in and permanently stop send_work and recv_work with disable_delayed_work_sync() and disable_work_sync() before calling nfc_digital_unregister_device(). Keep dev->up = false after nfc_digital_unregister_device() so any digital_wq_cmd() invocation that races before unregister stops cmd_work still receives rc = 0 and stays on ddev->cmd_queue to be properly completed with -ENODEV by the drain loop (avoiding digital_data_exch and upper-layer callback leaks). Also update nfcsim_link_recv_skb() to wake on READ_ONCE(link->shutdown) and check link->shutdown before validating skb, so a late recv_work instance cannot sleep for the full receive timeout (up to 5049 ms) during module unload. Triggered in QEMU KVM without kernel delay injection while unloading nfcsim during active LLCP traffic, and verified fixed across 200 nfcsim load/unload cycles with KASAN, LOCKDEP, and DEBUG_OBJECTS enabled (zero warnings, zero stalls). Fixes: 5e7b30d24a5b ("nfc: nfcsim: fix use after free during module unload") Fixes: 204bddcb508f ("NFC: nfcsim: Make use of the Digital layer") Cc: stable@vger.kernel.org Signed-off-by: Bin Luo --- Changes in v3: - Keep dev->up = false after nfc_digital_unregister_device() so in-flight digital_wq_cmd() requests are not silently dropped without invoking cmd_cb(). - Make nfcsim_link_recv_skb() wake on READ_ONCE(link->shutdown) (set via WRITE_ONCE() in nfcsim_link_shutdown()) and return -ENODEV immediately so disable_work_sync(&dev->recv_work) cannot stall for a full receive timeout. - Normalize From:/Signed-off-by: author metadata. Changes in v2: - Resend cleanly via b4 web relay and include all three NFC teardown fixes as a threaded series. drivers/nfc/nfcsim.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/drivers/nfc/nfcsim.c b/drivers/nfc/nfcsim.c index f8e5c4d..a4e653a 100644 --- a/drivers/nfc/nfcsim.c +++ b/drivers/nfc/nfcsim.c @@ -116,7 +116,7 @@ static void nfcsim_link_shutdown(struct nfcsim_link *link) { mutex_lock(&link->lock); - link->shutdown = 1; + WRITE_ONCE(link->shutdown, 1); link->mode = NFCSIM_MODE_NONE; mutex_unlock(&link->lock); @@ -131,7 +131,8 @@ static struct sk_buff *nfcsim_link_recv_skb(struct nfcsim_link *link, struct sk_buff *skb; rc = wait_event_interruptible_timeout(link->recv_wait, - link->cond, + link->cond || + READ_ONCE(link->shutdown), msecs_to_jiffies(timeout)); mutex_lock(&link->lock); @@ -144,13 +145,13 @@ static struct sk_buff *nfcsim_link_recv_skb(struct nfcsim_link *link, goto done; } - if (!skb || link->rf_tech != rf_tech || link->mode = mode) { - rc = -EINVAL; + if (link->shutdown) { + rc = -ENODEV; goto done; } - if (link->shutdown) { - rc = -ENODEV; + if (!skb || link->rf_tech != rf_tech || link->mode = mode) { + rc = -EINVAL; goto done; } @@ -411,14 +412,14 @@ static struct nfcsim *nfcsim_device_new(struct nfcsim_link *link_in, static void nfcsim_device_free(struct nfcsim *dev) { - nfc_digital_unregister_device(dev->nfc_digital_dev); + nfcsim_link_shutdown(dev->link_in); - dev->up = false; + disable_delayed_work_sync(&dev->send_work); + disable_work_sync(&dev->recv_work); - nfcsim_link_shutdown(dev->link_in); + nfc_digital_unregister_device(dev->nfc_digital_dev); - cancel_delayed_work_sync(&dev->send_work); - cancel_work_sync(&dev->recv_work); + dev->up = false; nfc_digital_free_device(dev->nfc_digital_dev); -- 2.43.0