From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796453881; b=SFHi4tV7prVgEGHO1iYYy3aeAr6ZrgC0MJan9B5+Xvv9kSg/aUXKYqBa3Ikr93p3Dimv cKkVXwiBVY8sKm+NqoIBoOxlWJevMrVsxv/KaOXLzIklgVoI9J2zFllq06O3U1ZB5nyFq NDE5DSQRk+5RWW6YjP46VRzrQhIgztUOPL+Vioa9BWjAipD63lecKShnoc4fTzMsfQhtQ CqFqAfMTKJ1ZkK5bwMv+j4OaktSmMhF0yqmSAnpFu/ZUg0enkxJMC7IZ0dTKk7TYkI3+X HcxDFz0m/BT76PL5PrcE5sTj3BoIczB4lkdXJluT5lMRFCxhMvzhJDex0KG0Imk5nKQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796453881; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=eHXpU5vkjCkYWddtaoBHez6gy0g34shaSSk26RRAxLI=; b=ykkofiYp0lK0DOcfVajuZ3jldFHxC9uEeS5oOvodCoQ1pE4jc5nOau49H1yo1YdEmnm8 wiPIM1JLWXLhA98LJNWbuDbCcfUsykiuq36obL5Hm6bPjASiSzqW2blS/TCpW5X8HjDjy iU+BcyQpXinwDpIlvtZ/eytHL3EBOS5EpFh7o7sUXAQR91n3ATbgqkbpetIYvrpd3uH4g 3I5oXZ/TkKFHphsYEAMgRhK5WA3v9Lk1H93N7wvLY7dqSROYwvnn6RS6+FRQ0QtRaUKXK o/QV8cAPz6M1JYUR7+ecNa1k2ZXgykkVH3d2I4cuams3ywLLrz67mpRTPXHf4VAfU4w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="IK/SGGtK"; arc=none 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 68163606A680; Sat, 5 Dec 2026 06:58:01 +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=IK/SGGtK Received: by smtp.kernel.org (Postfix) with ESMTPSA id 726151F0092B; Sat, 5 Dec 2026 06:58:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796453881; bh=eHXpU5vkjCkYWddtaoBHez6gy0g34shaSSk26RRAxLI=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=IK/SGGtKz5bzGVAg1IJovcHzQo1dBL7eWCVBegGSb3nmauToQPem5eX0qbKFsn1eb D89QbyWexM5o0yK/loBXelzugblVNysLTIBLIeyxVf7u83YErG4lhOfUH7DSFha7G3 gvGWOjBLAxyvrOApRKIDr166Pm/oluboTVHZGAVzc/W1H1zr4P0VSseeN0WWcJF6pv rimtwbLTKxEnZ0JUy2Rz7aZxq0f+vyBxdMXcXYceOT7VrGAYiFX1a/rv8h5oHJK7hr SBvIDZaWplCE26WyYb8yydRGyhS8JhtrmrClQXE9AhSBYj0MMYBK/O0PErmyEM2FgC BvsKCzfwBja1g== Subject: Re: [PATCH net v2 3/3] nfc: digital: stop works and drain commands before nfc_remove_device() From: netdev-bot+sashiko@kernel.org To: luob87709@gmail.com Cc: david@ixit.cz, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, krzk@kernel.org, oe-linux-nfc@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Date: Sat, 05 Dec 2026 06:57:59 +0000 Message-ID: <179645387999.3146.387259976163135946@kernel.org> In-Reply-To: <20261204060656.503-4-luob87709@gmail.com> References: <20261204060656.503-4-luob87709@gmail.com> X-sashiko-severity: Medium Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit 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 Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider. Critical: 0 · High: 0 (2) · Medium: 1 · Low: 2 If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough). - [Medium] The drain loop in nfc_digital_unregister_device() (net/nfc/digital_core.c:849-861) frees only cmd->mdaa_params and cmd. - [Low] The commit message names two routes to a use-after-free of nfc_llcp_local: a cmd_complete_work that is still running, and the… - [Low] nfc: digital: Fixes trailer cites stub commit 4b10884eb428 Pre-existing issues: - [High] This problem wasn't introduced by this patch, but the pn533 driver has the same bug this patch fixes in the digital layer: a… - [High] This problem wasn't introduced by this patch, but nfc_digital_unregister_device() frees the head digital_cmd even when… -- nfc: digital: stop works and drain commands before nfc_remove_device() This patch reorders nfc_digital_unregister_device(). It now calls nfc_unregister_rfkill() first, permanently disables poll_work, cmd_work and cmd_complete_work, and drains cmd_queue while LLCP is still registered. nfc_remove_device() is called last, mirroring nci_unregister_device(). > nfc_unregister_device() calls nfc_llcp_unregister_device(), which > tears down and releases struct nfc_llcp_local (dropping its reference > and scheduling local_release_work() to kfree(local)). Afterwards, > still-running cmd_complete_work or the cmd_queue drain loop invokes > cmd->cmd_cb(ddev, cmd->cb_context, ...) -> nfc_llcp_recv(local, ...), > passing a struct nfc_llcp_local pointer that has already been > unregistered and may already be freed. [Severity: Low] Is the drain loop really a use-after-free route? The drain in nfc_digital_unregister_device() always passes an error: cmd->cmd_cb(ddev, cmd->cb_context, ERR_PTR(-ENODEV)); digital_in_recv_dep_res() turns that into rc = -ENODEV and jumps to the exit label. That calls the callback with a NULL resp: exit: data_exch->cb(data_exch->cb_context, resp, rc); nfc_llcp_recv() then returns before it reaches __nfc_llcp_recv(local, ...): if (err < 0) { pr_err("LLCP PDU receive err %d\n", err); return; } So on the drain path the stale local pointer is passed but never dereferenced. The route that can actually dereference local is a cmd_complete_work that is still running and delivers a valid resp. Could the commit message name the in-flight cmd_complete_work as the real trigger, and describe moving the drain earlier as defensive? That would help stable backporters judge the fix. > diff --git a/net/nfc/digital_core.c b/net/nfc/digital_core.c > index 18236221d8988..7b8d72f917811 100644 > --- a/net/nfc/digital_core.c > +++ b/net/nfc/digital_core.c > @@ -836,15 +836,15 @@ void nfc_digital_unregister_device(struct nfc_digital_dev *ddev) > { > struct digital_cmd *cmd, *n; > > - nfc_unregister_device(ddev->nfc_dev); > + nfc_unregister_rfkill(ddev->nfc_dev); > > mutex_lock(&ddev->poll_lock); > ddev->poll_tech_count = 0; > mutex_unlock(&ddev->poll_lock); > > - cancel_delayed_work_sync(&ddev->poll_work); > - cancel_work_sync(&ddev->cmd_work); > - cancel_work_sync(&ddev->cmd_complete_work); > + disable_delayed_work_sync(&ddev->poll_work); > + disable_work_sync(&ddev->cmd_work); > + disable_work_sync(&ddev->cmd_complete_work); [Severity: Low] Is 4b10884eb428 the right commit for the first Fixes tag? As far as I can tell, 4b10884eb428 only added the skeleton of the digital stack. All of the digital_nfc_ops returned -EOPNOTSUPP, and nfc_digital_unregister_device() was just a call to nfc_unregister_device(ddev->nfc_dev). At that point there was no poll_work, cmd_work, cmd_complete_work or ddev->cmd_queue, so the teardown could not run in the wrong order. Those pieces arrived with 59ee2361c924 ("NFC Digital: Implement driver commands mechanism"). That commit added the work items and ddev->cmd_queue. It also made digital_wq_cmd_complete() re-schedule cmd_work. Its unregister sequence calls nfc_unregister_device() before cancelling the works and draining the queue, which is the ordering this patch changes. The second tag, 82e57952869f, looks correct to me. It is the commit that added the cmd->cmd_cb() call in the drain loop. Stable and bisect tooling rely on these tags to decide which trees need the fix. Could the first one be replaced with: Fixes: 59ee2361c924 ("NFC Digital: Implement driver commands mechanism") [Severity: Medium] Can this ordering leak the req skb of a chained command? cmd_work is disabled before cmd_complete_work. So disable_work_sync(&ddev->cmd_complete_work) waits for a handler that may still be running. That handler's cmd_cb can chain the next command, for example digital_in_recv_sens_res() sending SDD_REQ, or a DEP ACK, RTOX or next fragment. digital_send_cmd() then queues it: cmd->req = skb; ... list_add_tail(&cmd->queue, &ddev->cmd_queue); mutex_unlock(&ddev->cmd_lock); schedule_work(&ddev->cmd_work); With cmd_work disabled, schedule_work() is now a no-op. The command stays non-pending and ends up in the drain loop below, which only does: kfree(cmd->mdaa_params); kfree(cmd); The drain never calls kfree_skb(cmd->req). The error path in digital_wq_cmd() shows that the cmd owns req until the driver accepts it: kfree_skb(cmd->req); kfree(cmd->mdaa_params); kfree(cmd); cmd->resp has the same problem. A driver completion can arrive after cmd_complete_work has been disabled, or a pending cmd_complete_work can be cancelled. In both cases cmd->resp holds a valid skb, but the drain passes ERR_PTR(-ENODEV) to cmd_cb and nothing frees cmd->resp. The missing kfree_skb() in the drain dates back to 82e57952869f. With disable_work_sync(), though, these commands now always end up there. Should the drain call kfree_skb(cmd->req) when !cmd->pending, and kfree_skb(cmd->resp) when it holds a valid skb? > > list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) { > list_del(&cmd->queue); > @@ -859,6 +859,8 @@ void nfc_digital_unregister_device(struct nfc_digital_dev *ddev) > kfree(cmd->mdaa_params); > kfree(cmd); > } [Severity: High] This isn't a bug introduced by this patch, but can a late driver completion write into a digital_cmd that is freed here? The head command may already have been handed to the driver (cmd->pending == 1). Nothing aborts the driver's asynchronous completion or waits for it, because the disable_work_sync() calls only wait for digital's own work items. After the drain frees cmd, the driver can still call: net/nfc/digital_core.c:digital_send_cmd_complete() { struct digital_cmd *cmd = arg; cmd->resp = resp; schedule_work(&ddev->cmd_complete_work); } That writes into the freed cmd. The schedule_work() may also act on a ddev that the caller has already freed with nfc_digital_free_device(). port100 looks like it can reach this. port100 does not set soft_unbind, so usb_unbind_interface() kills the URBs before ->disconnect: if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED) usb_disable_interface(udev, intf, false); driver->disconnect(intf); port100_recv_response() still queues the completion on system_wq for killed URBs: sched_wq: schedule_work(&dev->cmd_complete_work); port100_disconnect() never cancels that work: nfc_digital_unregister_device(dev->nfc_digital_dev); nfc_digital_free_device(dev->nfc_digital_dev); usb_kill_urb(dev->in_urb); So this chain can run after the drain has freed cmd: port100_wq_cmd_complete() port100_send_async_complete() port100_in_comm_rf_complete() digital_send_cmd_complete() The same window existed with cancel_work_sync(). This patch narrows part of it by preventing cmd_complete_work from being re-queued. As noted above, a valid resp delivered that way is now leaked instead. nci_unregister_device(), which the commit message says this mirrors, stops the device before cleanup. There is no equivalent step here. Would it make sense to abort the pending command through ddev->ops->abort_cmd and wait for the driver? Another option would be to require drivers to quiesce their I/O before calling nfc_digital_unregister_device(). > + > + nfc_remove_device(ddev->nfc_dev); > } > EXPORT_SYMBOL(nfc_digital_unregister_device); [Severity: High] This isn't a bug introduced by this patch, but does pn533 have the same LLCP teardown problem that this patch fixes in the digital layer? pn533_usb_disconnect() unregisters before it drains the driver workqueue: pn53x_unregister_nfc(phy->priv); pn53x_common_clean(phy->priv); pn53x_unregister_nfc() calls nfc_unregister_device(). That goes through nfc_remove_device()->nfc_llcp_unregister_device(), which calls local_cleanup() and nfc_llcp_local_put(). When the last reference is dropped, local_release_work() kfree()s local. Only after that does pn53x_common_clean() run: destroy_workqueue(priv->wq); LLCP sends via nfc_data_exchange(..., nfc_llcp_recv, local) without taking a reference on local. Suppose a successful data exchange response is already queued on priv->wq when unbind starts. Then pn533_data_exchange_complete() calls: arg->cb(arg->cb_context, skb, 0); That is nfc_llcp_recv(local, skb, 0)->__nfc_llcp_recv(), which does: local->rx_pending = skb; timer_delete(&local->link_timer); schedule_work(&local->rx_work); Can this write into a freed nfc_llcp_local? Killed URBs complete with an error, and nfc_llcp_recv() ignores errors, so only a successful completion queued before unbind would hit this. -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204060656.503-1-luob87709%40gmail.com