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/2026…%40gmail.com