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
Subject: Re: [PATCH net v2 2/3] nfc: nfcsim: stop link and works before unregistering digital device
Date: Sat, 05 Dec 2026 06:57:58 +0000 [thread overview]
Message-ID: <179645387869.3146.7132650635892113846@kernel.org> (raw)
In-Reply-To: <20261204060656.503-3-luob87709@gmail.com>
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 0 (1) · Medium: 2 · Low: 0
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] Clearing dev->up first in nfcsim_device_free() lets in-flight
digital commands be dropped silently, which leaks the caller's…
- [Medium] nfc: nfcsim: dev->up race before unregister stalls recv_work
teardown
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but
port100_disconnect() (drivers/nfc/port100.c:1615-1634) has the same…
--
nfc: nfcsim: stop link and works before unregistering digital device
This patch reorders nfcsim_device_free() so that dev->up is cleared,
link_in is shut down, and send_work/recv_work are disabled before
nfc_digital_unregister_device() drains the digital command queue. The goal
is to close a remaining use-after-free where nfcsim_recv_wq() calls
digital_send_cmd_complete() on a digital_cmd already freed by the drain
loop.
> diff --git a/drivers/nfc/nfcsim.c b/drivers/nfc/nfcsim.c
> index f8e5c4da3b13d..f3ddae6d41823 100644
> --- a/drivers/nfc/nfcsim.c
> +++ b/drivers/nfc/nfcsim.c
> @@ -411,14 +411,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);
> -
> dev->up = false;
[Severity: Medium]
If dev->up is cleared this early, can in-flight digital commands get dropped
without their completion callback being called?
The digital core is still running at this point. ddev->cmd_work,
cmd_complete_work and poll_work are only disabled later, inside
nfc_digital_unregister_device().
A new command can still be queued during that window. For example, say
nfcsim_recv_wq() passed the !dev->up check just before teardown and queued
cmd_complete_work. That work then runs after
disable_work_sync(&dev->recv_work) returns. Its cmd_cb can queue a DEP
ACK/RTOX via digital_in_send_cmd(). LLCP tx_work can also call
nfc_data_exchange()->digital_in_send(). shutting_down is not set yet at this
point.
digital_wq_cmd() then reaches nfcsim_send(), which now sees !dev->up:
drivers/nfc/nfcsim.c:nfcsim_send() {
...
if (!dev->up) {
NFCSIM_ERR(dev, "Device is down\n");
return -ENODEV;
}
...
}
The error path in digital_wq_cmd() then frees the cmd without calling
cmd->cmd_cb:
net/nfc/digital_core.c:digital_wq_cmd() {
...
mutex_lock(&ddev->cmd_lock);
list_del(&cmd->queue);
mutex_unlock(&ddev->cmd_lock);
kfree_skb(cmd->req);
kfree(cmd->mdaa_params);
kfree(cmd);
...
}
Would this leak the struct digital_data_exch allocated in digital_in_send()?
It only seems to be freed in digital_in_recv_dep_res().
ddev->saved_skb from digital_in_send_dep_req() also doesn't appear to be
freed, because nfc_digital_free_device() does not release it.
The upper layer would also never get an -ENODEV completion. That layer is
nfc_llcp_recv(), or rawsock_data_exchange_complete(), which drops a socket
reference.
Before this patch, dev->up stayed true until unregister had cancelled
cmd_work. nfcsim_send() then returned 0, and the command stayed queued until
the drain loop called cmd_cb(..., ERR_PTR(-ENODEV)).
Does the use-after-free fix need the early dev->up = false? It looks like
disable_work_sync(&dev->recv_work) before nfc_digital_unregister_device()
would be enough by itself. The commit message also doesn't mention this side
effect.
The window seems to remain after "nfc: digital: stop works and drain
commands before nfc_remove_device()" later in the series too. That change
disables cmd_work inside unregister, which is still after dev->up is
cleared.
>
> nfcsim_link_shutdown(dev->link_in);
>
> - cancel_delayed_work_sync(&dev->send_work);
> - cancel_work_sync(&dev->recv_work);
> + disable_delayed_work_sync(&dev->send_work);
> + disable_work_sync(&dev->recv_work);
> +
> + nfc_digital_unregister_device(dev->nfc_digital_dev);
[Severity: Medium]
Is dev->up = false actually one-way at this point? dev->up is a plain
bool that tracks RF state. The digital device stays registered until
nfc_digital_unregister_device() runs below, so ddev->poll_work and the
netlink dev_up/start_poll paths are still live.
nfcsim_in_configure_hw(), nfcsim_tg_configure_hw() and
nfcsim_switch_rf(ddev, true) all set dev->up = true unconditionally:
drivers/nfc/nfcsim.c:nfcsim_in_configure_hw() {
...
case NFC_DIGITAL_CONFIG_RF_TECH:
dev->up = true;
dev->mode = NFCSIM_MODE_INITIATOR;
dev->rf_tech = param;
break;
...
}
Say an in-flight nfcsim_recv_wq() is woken by nfcsim_link_shutdown()
and gets an error skb back. If digital_wq_poll() reconfigures the RF
tech before the !dev->up check, the worker sees dev->up == true again
and calls dev->cb().
That callback can move the poll on and send the next frame through
digital_wq_cmd()->nfcsim_send(). nfcsim_send() also sees dev->up ==
true and calls schedule_work(&dev->recv_work). This can happen while
this function is still waiting in
disable_delayed_work_sync(&dev->send_work).
The newly queued nfcsim_recv_wq() then sleeps in
nfcsim_link_recv_skb():
rc = wait_event_interruptible_timeout(link->recv_wait,
link->cond,
msecs_to_jiffies(timeout));
The wait condition only tests link->cond, not link->shutdown. The
previous pass already reset link->cond = 0 (outside link->lock) after
nfcsim_link_shutdown() had set it. Nothing wakes the new worker, so
disable_work_sync(&dev->recv_work) waits for the full receive timeout.
For an NFC-DEP initiator that can be up to 5049 ms from
digital_rwt_map[], which stalls module unload.
There is a second variant. If dev->up flips back to true after both
disable calls but before unregister cancels ddev->cmd_work,
nfcsim_send() stores the skb in dev->link_out and returns 0. Its
schedule_work() and schedule_delayed_work() calls silently fail on the
disabled works.
This doesn't seem to reopen the digital_send_cmd_complete()
use-after-free, because recv_work is still disabled before the drain
loop. But the teardown now depends on dev->up staying false while the
digital core can still set it back to true.
Before this patch, unregister stopped poll_work and cmd_work before
dev->up was cleared, so this window did not exist. Patch 3/3 doesn't
touch nfcsim.c, so the window remains at the end of the series.
Would it make sense to use a separate one-way teardown flag that
nfcsim_send() and nfcsim_recv_wq() check, rather than reusing dev->up?
Alternatively, the digital side could be quiesced before the link is
shut down. Should nfcsim_link_recv_skb() also wake on link->shutdown
so a late worker cannot sleep for the whole timeout?
[Severity: High]
This isn't a bug introduced by this patch, but port100_disconnect() in
drivers/nfc/port100.c seems to have the same kind of teardown ordering
problem. It never cancels dev->cmd_complete_work:
drivers/nfc/port100.c:port100_disconnect() {
...
nfc_digital_unregister_device(dev->nfc_digital_dev);
nfc_digital_free_device(dev->nfc_digital_dev);
usb_kill_urb(dev->in_urb);
usb_kill_urb(dev->out_urb);
usb_free_urb(dev->in_urb);
usb_free_urb(dev->out_urb);
kfree(dev->cmd);
...
}
On unbind, usb_unbind_interface() calls usb_disable_interface() before
->disconnect, because port100 doesn't set soft_unbind. That kills in_urb.
The completion handler, port100_recv_response() or port100_recv_ack(), then
runs with -ESHUTDOWN or -ENOENT and does:
schedule_work(&dev->cmd_complete_work);
port100_disconnect() then frees state in three places:
- the drain loop in nfc_digital_unregister_device() frees every
digital_cmd
- nfc_digital_free_device() frees ddev
- kfree(dev->cmd) frees the port100 command
If port100_wq_cmd_complete() runs after that, port100_send_async_complete()
reads dev->cmd. By then dev->cmd may already be freed, or it may end up
freed twice.
It then calls port100_in_comm_rf_complete()->digital_send_cmd_complete().
Can that write cmd->resp into a digital_cmd that the drain loop already
freed, and call schedule_work() on a freed ddev->cmd_complete_work?
struct port100 is devm-allocated. If the work is delayed past devm cleanup,
it would also touch a freed struct port100.
Should port100_disconnect() cancel or disable dev->cmd_complete_work before
freeing this state?
>
> nfc_digital_free_device(dev->nfc_digital_dev);
>
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204060656.503-1-luob87709%40gmail.com
next prev parent reply other threads:[~2026-12-05 6:57 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 6:06 [PATCH net v2 0/3] nfc: fix LLCP, nfcsim, and digital device teardown ordering Bin Luo via B4 Relay
2026-12-04 6:06 ` [PATCH net v2 1/3] nfc: llcp: fix work and timer teardown order in local_cleanup() Bin Luo via B4 Relay
2026-12-05 6:57 ` netdev-bot+sashiko
2026-12-04 6:06 ` [PATCH net v2 2/3] nfc: nfcsim: stop link and works before unregistering digital device Bin Luo via B4 Relay
2026-12-05 6:57 ` netdev-bot+sashiko [this message]
2026-12-04 6:06 ` [PATCH net v2 3/3] nfc: digital: stop works and drain commands before nfc_remove_device() Bin Luo via B4 Relay
2026-12-05 6:57 ` netdev-bot+sashiko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179645387869.3146.7132650635892113846@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=davem@davemloft.net \
--cc=david@ixit.cz \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=krzk@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luob87709@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=oe-linux-nfc@lists.linux.dev \
--cc=pabeni@redhat.com \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox