Re: [PATCH net v2 1/3] nfc: llcp: fix work and timer teardown order in local_cleanup()

From: netdev-bot+sashiko

Date: Sat Dec 05 2026 - 01:58:12 EST


Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit author has no real name.
- [Low] nfc: llcp: Fixes tag cites c7aa12252f51 instead of d646960f7986

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but
local_cleanup() (net/nfc/llcp_core.c:185) frees local->pending_sdreqs…
- [High] This problem wasn't introduced by this patch, but the data-
exchange completion context is a raw, unreferenced struct…

--

nfc: llcp: fix work and timer teardown order in local_cleanup()

This patch reorders local_cleanup(). rx_work and tx_work are now disabled
before tx_queue is purged and before link_timer and timeout_work are shut
down. It also switches to disable_work_sync() and timer_shutdown_sync(), so
the works and timers cannot be re-queued or re-armed once cleanup has
started.

> Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) <luob87709@xxxxxxxxx>

[Severity: Low]
This isn't a bug, but the author name on this commit is only the email
address:

Author: luob87709@xxxxxxxxx <luob87709@xxxxxxxxx>

The Signed-off-by uses "Bin Luo (University of Electronic Science and
Technology of China)" as the name. checkpatch reports this as
FROM_SIGN_OFF_MISMATCH. If the patch is applied as sent, git history will
record the email address as the author name.

The other two patches in the series have the same metadata:

"nfc: nfcsim: stop link and works before unregistering digital device"
"nfc: digital: stop works and drain commands before nfc_remove_device()"

Could the git author identity be fixed, or a "From: Bin Luo
<luob87709@xxxxxxxxx>" line be added to the body?

> diff --git a/net/nfc/llcp_core.c b/net/nfc/llcp_core.c
> index 74bf817007cf2..32530a29fb0cd 100644
> --- a/net/nfc/llcp_core.c
> +++ b/net/nfc/llcp_core.c
> @@ -173,15 +173,15 @@ static struct nfc_llcp_local *nfc_llcp_local_get(struct nfc_llcp_local *local)
> static void local_cleanup(struct nfc_llcp_local *local)
> {
> nfc_llcp_socket_release(local, false, ENXIO);
> - timer_delete_sync(&local->link_timer);
> + disable_work_sync(&local->rx_work);
> + disable_work_sync(&local->tx_work);

[Severity: Low]
This isn't a bug, but is the first Fixes tag pointing at the right
commit?

c7aa12252f51 ("NFC: Take a reference on the LLCP local pointer when
creating a socket") did not create this teardown order. It moved the
existing sequence out of nfc_llcp_unregister_device() and into
local_release(). The order it moved was already
del_timer_sync(&local->link_timer), then
skb_queue_purge(&local->tx_queue), then destroy_workqueue() on tx_wq
and then on rx_wq.

That ordering, and the rx_work -> tx_work -> link_timer dependency
this patch fixes, go back to d646960f7986 ("NFC: Initial LLCP
support"). 474fee3db16c later replaced the destroy_workqueue() calls
with cancel_work_sync() and kept the same order.

The patch is Cc: stable, and the stable tooling uses the Fixes tag to
decide how far back to apply it. Should the first tag be:

Fixes: d646960f7986 ("NFC: Initial LLCP support")

The second tag, 40213fa8513c, looks correct for the sdreq_timer and
sdreq_timeout_work part.

[Severity: High]
This is a pre-existing issue, but is local protected from driver
data-exchange callbacks that are still in flight when it gets freed?

nfc_llcp_tx_work() passes the bare local pointer to the driver as the
callback context and does not take a kref:

net/nfc/llcp_core.c:nfc_llcp_tx_work() {
...
ret = nfc_data_exchange(local->dev, local->target_idx,
skb, nfc_llcp_recv, local);
...
}

When the exchange completes successfully, nfc_llcp_recv()->__nfc_llcp_recv()
writes into local:

local->rx_pending = skb;
timer_delete(&local->link_timer);
schedule_work(&local->rx_work);

pn533 unregisters the LLCP device before it drains its workqueue and before
it kills the URBs:

drivers/nfc/pn533/usb.c:pn533_usb_disconnect() {
...
pn53x_unregister_nfc(phy->priv);
pn53x_common_clean(phy->priv);
...
usb_kill_urb(phy->in_urb);
...
}

The free happens on this path:

pn53x_unregister_nfc()
nfc_unregister_device()
nfc_llcp_unregister_device()
local_cleanup()
nfc_llcp_local_put()

If no socket holds a reference, local_release_work() then runs
local_cleanup() again and calls kfree(local).

After that, destroy_workqueue(priv->wq) in pn53x_common_clean() can run a
pending cmd_complete_work. pn533_data_exchange_complete() then calls
arg->cb(arg->cb_context, skb, 0), where cb_context is the freed local.

On an active LLCP link a DATA_EXCHANGE is almost always in flight, because
tx_work keeps sending SYMM. Error completions are safe: nfc_llcp_recv()
returns before touching local when err < 0. A successful response is
needed, for example a driver unbind or rmmod while the peer is still in the
field.

The disable_work_sync(&local->rx_work) added here makes schedule_work() a
no-op while the memory is still valid. It does not stop the stores to
rx_pending and link_timer after kfree(local).

There is a second case. If the callback lands between kfree_skb(
local->rx_pending) in the second local_cleanup() and kfree(local), the skb
leaks.

The "nfc: digital: stop works and drain commands before
nfc_remove_device()" patch later in this series fixes the ordering only for
Digital-based drivers. Non-digital drivers like pn533 still have this
window.

> skb_queue_purge(&local->tx_queue);
> - cancel_work_sync(&local->tx_work);
> - cancel_work_sync(&local->rx_work);
> - cancel_work_sync(&local->timeout_work);
> + timer_shutdown_sync(&local->link_timer);
> + disable_work_sync(&local->timeout_work);
> kfree_skb(local->rx_pending);
> local->rx_pending = NULL;
> - timer_delete_sync(&local->sdreq_timer);
> - cancel_work_sync(&local->sdreq_timeout_work);
> + timer_shutdown_sync(&local->sdreq_timer);
> + disable_work_sync(&local->sdreq_timeout_work);
> nfc_llcp_free_sdp_tlv_list(&local->pending_sdreqs);

[Severity: High]
This isn't a bug introduced by this patch, but can this free race with a
concurrent NFC_CMD_LLC_SDREQ request?

local_cleanup() walks pending_sdreqs and frees it with hlist_del() and
kfree() without holding local->sdreq_lock. nfc_llcp_send_snl_sdreq() adds
entries to the same list under sdreq_lock:

net/nfc/llcp_commands.c:nfc_llcp_send_snl_sdreq() {
...
mutex_lock(&local->sdreq_lock);
...
hlist_add_head(&sdreq->node, &local->pending_sdreqs);
...
}

nfc_genl_llc_sdreq() takes device_lock but only checks dep_link_up. It
never checks dev->shutting_down:

device_lock(&dev->dev);

if (dev->dep_link_up == false) {
rc = -ENOLINK;
goto exit;
}

local = nfc_llcp_find_local(dev);

On the unregister side, nfc_unregister_device() calls
nfc_unregister_rfkill(), which sets shutting_down and drops device_lock.
It then reaches local_cleanup() without holding device_lock:

nfc_remove_device()
nfc_llcp_unregister_device()
local_cleanup()

So a request can take device_lock after nfc_unregister_rfkill() and call
nfc_llcp_find_local() before nfc_llcp_remove_local(). It then holds a valid
local reference while local_cleanup() runs. One interleaving:

nfc_llcp_send_snl_sdreq() local_cleanup()
hlist_add_head()
N->next = head->first (A)
nfc_llcp_free_sdp_tlv_list()
hlist_del(A); kfree(A)
head->first = N

N->next now points at freed memory.

When the netlink caller drops its reference, local_release_work() runs
local_cleanup() a second time. That walk reaches A, which is a
use-after-free, and frees it a second time.

The timer_shutdown_sync(&local->sdreq_timer) in this patch turns the
mod_timer() call in that path into a harmless no-op. It does not affect the
list race. NFC_CMD_LLC_SDREQ is GENL_ADMIN_PERM, so CAP_NET_ADMIN is
required.

[ ... ]

--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204060656.503-1-luob87709%40gmail.com