From: Simon Horman <horms@kernel.org>
To: xuyuqiabc@gmail.com
Cc: Simon Horman <horms@kernel.org>,
netdev@vger.kernel.org, oe-linux-nfc@lists.linux.dev,
david@ixit.cz, davem@davemloft.net, edumazet@google.com,
kuba@kernel.org, pabeni@redhat.com, stable@vger.kernel.org,
vega@nebusec.ai, weir@nebusec.ai, xuyq21@lenovo.com,
mgreer@animalcreek.com, u.kleine-koenig@baylibre.com,
ian.ray@gehealthcare.com, dmitry.torokhov@gmail.com,
thierry.escande@linux.intel.com, sameo@linux.intel.com
Subject: Re: [PATCH net v3 1/1] nfc: digital: fix use-after-free in unregister
Date: Wed, 2 Dec 2026 15:31:36 +0000 [thread overview]
Message-ID: <20261202153135.109610-2-horms@kernel.org> (raw)
In-Reply-To: <20261121181620.57904-1-xuyuqiabc@gmail.com>
This is an AI-generated review of your patch. The human sending this
email says "This review does seem to be a legitimate concern, I'd
suggest dropping the duplicate call to cancel_delayed_work() in
trf7970a_abort_cmd()"
Full review at: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261121181620.57904-1-xuyuqiabc@gmail.com?part=1
---
nfc: digital: fix use-after-free in unregister
This patch makes nfc_digital_unregister_device() abort the in-flight
driver command and drain the completion works before it frees the queued
commands. It also stops new commands from being queued or submitted once
the device is shutting down. The port100, nfcsim, st95hf and trf7970a
drivers are updated so that no completion callback can run after abort or
removal.
> diff --git a/drivers/nfc/trf7970a.c b/drivers/nfc/trf7970a.c
> index 001748d91ec1c..bf3567c146fff 100644
> --- a/drivers/nfc/trf7970a.c
> +++ b/drivers/nfc/trf7970a.c
> @@ -652,6 +652,12 @@ static void trf7970a_send_upstream(struct trf7970a *trf)
> dev_kfree_skb_any(trf->tx_skb);
> trf->tx_skb = NULL;
>
> + /*
> + * A timeout handler may already be running and waiting for trf->lock.
> + * It must not deliver another completion after this one.
> + */
> + trf->ignore_timeout = true;
> +
[Severity: High]
This is a pre-existing issue, but with this change, can
trf7970a_issue_eof() leave the driver stuck with a stale cb_arg when
trf7970a_cmd() fails?
trf7970a_issue_eof() does not return after it completes the command on an
error:
drivers/nfc/trf7970a.c:trf7970a_issue_eof() {
ret = trf7970a_cmd(trf, TRF7970A_CMD_FIFO_RESET);
if (ret)
trf7970a_send_err_upstream(trf, ret);
ret = trf7970a_cmd(trf, TRF7970A_CMD_EOF);
if (ret)
trf7970a_send_err_upstream(trf, ret);
trf->state = TRF7970A_ST_WAIT_FOR_RX_DATA;
...
schedule_delayed_work(&trf->timeout_work,
msecs_to_jiffies(trf->timeout));
}
The first trf7970a_send_err_upstream() calls trf->cb(), which is
digital_send_cmd_complete(). That queues cmd_complete_work, which frees
the digital_cmd.
If the EOF command fails as well, the second call passes the same cb_arg
again. digital_send_cmd_complete() can then write cmd->resp into a freed
digital_cmd and queue cmd_complete_work again.
In both cases the state goes back to TRF7970A_ST_WAIT_FOR_RX_DATA and a
new timeout is armed for a command that has already completed.
trf->cb_arg is never cleared.
Before this patch, the re-armed timeout delivered that stale completion.
With the unconditional ignore_timeout = true above,
trf7970a_timeout_work_handler() now only clears the flag:
if (trf->ignore_timeout)
trf->ignore_timeout = false;
The device then stays in WAIT_FOR_RX_DATA. trf7970a_send_cmd() and
_trf7970a_tg_listen() reject every new command with -EIO ("Bogus state"),
so cb_arg is never refreshed.
What happens on the next event in that state? An SRX IRQ would reach
skb_tailroom(skb) in trf7970a_drain_fifo() with trf->rx_skb == NULL. An
error IRQ, or trf7970a_shutdown() on suspend or remove, would call
trf->cb() through trf7970a_send_err_upstream(trf, -ECANCELED) with the
freed digital_cmd as cb_arg.
Should trf7970a_issue_eof() return after each trf7970a_send_err_upstream()
call?
[ ... ]
> @@ -1903,7 +1909,9 @@ static void trf7970a_abort_cmd(struct nfc_digital_dev *ddev)
> trf->aborting = true;
> break;
> case TRF7970A_ST_LISTENING:
> - trf->ignore_timeout = !cancel_delayed_work(&trf->timeout_work);
> + case TRF7970A_ST_LISTENING_MD:
> + if (!cancel_delayed_work(&trf->timeout_work))
> + trf->ignore_timeout = true;
> trf7970a_send_err_upstream(trf, -ECANCELED);
[Severity: Low]
The commit message says:
Preserve an ignore_timeout already set by the IRQ path, and cancel the
timeout work synchronously after shutdown and before unregister
Does this conditional store change anything?
trf7970a_send_err_upstream() calls cancel_delayed_work() again and then
calls trf7970a_send_upstream(). That function now sets
trf->ignore_timeout = true unconditionally.
So ignore_timeout ends up true whichever branch is taken here. The same
is true of the new LISTENING_MD hunk in trf7970a_shutdown().
The store that actually stops a running timeout handler from completing
the command a second time is the one added to trf7970a_send_upstream().
This is tagged for stable. Could the commit message say that the abort and
shutdown hunks depend on the trf7970a_send_upstream() change, so a
backport does not take one without the other? Alternatively, could the
redundant cancel_delayed_work() and conditional store be dropped?
--
pw-bot: changes-requested
prev parent reply other threads:[~2026-12-02 15:32 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-21 10:16 [PATCH net v3 0/1] nfc: digital: fix use-after-free in unregister Yuqi Xu
2026-11-21 10:16 ` [PATCH net v3 1/1] " Yuqi Xu
2026-12-02 15:31 ` Simon Horman [this message]
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=20261202153135.109610-2-horms@kernel.org \
--to=horms@kernel.org \
--cc=davem@davemloft.net \
--cc=david@ixit.cz \
--cc=dmitry.torokhov@gmail.com \
--cc=edumazet@google.com \
--cc=ian.ray@gehealthcare.com \
--cc=kuba@kernel.org \
--cc=mgreer@animalcreek.com \
--cc=netdev@vger.kernel.org \
--cc=oe-linux-nfc@lists.linux.dev \
--cc=pabeni@redhat.com \
--cc=sameo@linux.intel.com \
--cc=stable@vger.kernel.org \
--cc=thierry.escande@linux.intel.com \
--cc=u.kleine-koenig@baylibre.com \
--cc=vega@nebusec.ai \
--cc=weir@nebusec.ai \
--cc=xuyq21@lenovo.com \
--cc=xuyuqiabc@gmail.com \
/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