From: netdev-bot+sashiko@kernel.org
To: joasantonio108@gmail.com
Cc: david@ixit.cz, krzk@kernel.org, oe-linux-nfc@lists.linux.dev,
netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
horms@kernel.org, kuba@kernel.org
Subject: Re: [PATCH net v4 2/2] nfc: st21nfca: prevent double completion of SE APDU request
Date: Mon, 30 Nov 2026 16:44:39 +0000 [thread overview]
Message-ID: <179605707981.3146.15516824656082527187@kernel.org> (raw)
In-Reply-To: <20261129-st21nfca-v2-v4-2-7d18b104ad46@gmail.com>
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 (2) · Medium: 3 (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] The commit message says a response arriving after the timeout
gets "no callback".
- [Medium] nfc: st21nfca: race on bwi_active and bwi_timer across SE APDU
paths
- [Medium] nfc: st21nfca: missing rollback on send failure in
st21nfca_hci_se_io()
Pre-existing issues:
- [High] The double completion that this patch fixes in st21nfca is still
present, unchanged, in the sibling ST driver drivers/nfc/st-nci/se.c.
- [High] bwi_timer can still be armed after the driver is removed, which
leads to a use-after-free on `info`.
- [Medium] st21nfca_apdu_reader_event_received() leaks the callback
context when sending EVT_SE_END_OF_APDU_TRANSFER fails.
- [Medium] A stale timeout work item can complete a newly submitted
request.
--
nfc: st21nfca: prevent double completion of SE APDU request
This patch checks bwi_active under se_lock in both the timeout work and
the EVT_TRANSMIT_DATA handler, so an SE APDU request is completed at most
once. It also makes st21nfca_hci_se_io() reject a new request with -EBUSY
while one is pending, and frees cb_context in that case.
> diff --git a/drivers/nfc/st21nfca/se.c b/drivers/nfc/st21nfca/se.c
> index 3726b00b7580c..33882f8739099 100644
> --- a/drivers/nfc/st21nfca/se.c
> +++ b/drivers/nfc/st21nfca/se.c
[ ... ]
> @@ -269,6 +275,10 @@ static void st21nfca_se_wt_work(struct work_struct *work)
> se_info.timeout_work);
>
> spin_lock(&info->se_info.se_lock);
> + if (!info->se_info.bwi_active) {
> + spin_unlock(&info->se_info.se_lock);
> + return;
> + }
> info->se_info.bwi_active = false;
> cb = info->se_info.cb;
> cb_context = info->se_info.cb_context;
[Severity: Medium]
This is a pre-existing issue, but can a stale timeout_work still complete
a request that was just submitted?
The new check only tests whether some request is pending. bwi_timer can
be armed while nothing is pending in two ways:
- the ST21NFCA_EVT_WTX_REQUEST case in
st21nfca_apdu_reader_event_received() calls mod_timer() without looking
at bwi_active.
- st21nfca_hci_se_io() calls mod_timer() after dropping se_lock.
Suppose that timer fires and st21nfca_se_wt_timeout() queues
timeout_work. Then a new request is accepted before the work runs:
st21nfca_hci_se_io()
info->se_info.bwi_active = true;
/* the queued timeout_work is not cancelled */
st21nfca_se_wt_work()
if (!info->se_info.bwi_active) /* passes for the new request */
...
cb(cb_context, NULL, 0, -ETIME);
The new request then completes with -ETIME before its own timeout has
elapsed. The SE also gets a soft or hard reset and xch_error is toggled.
The real response is dropped later, because active reads false.
The pre-patch code had the same window with no guard at all. The new
guard does close the case where a WTX arrives while idle and no new
request follows.
[ ... ]
> @@ -395,6 +406,7 @@ int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev,
> cancel_work_sync(&info->se_info.timeout_work);
>
> spin_lock(&info->se_info.se_lock);
> + active = info->se_info.bwi_active;
> info->se_info.bwi_active = false;
> cb = info->se_info.cb;
> cb_context = info->se_info.cb_context;
> @@ -405,7 +417,9 @@ int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev,
> if (r < 0)
> goto exit;
[Severity: Medium]
What happens to the request state when nfc_hci_send_event() below
fails? bwi_active stays true, bwi_timer stays armed and cb/cb_context
stay installed. The error goes back to nfc_se_io(), which does not
free cb_context once the driver has been called.
nfc_hci_send_event() can fail synchronously with -EADDRNOTAVAIL when
gate2pipe[ST21NFCA_APDU_READER_GATE] is NFC_HCI_INVALID_PIPE, with
-ENOMEM from nfc_hci_hcp_message_tx(), or with -ESHUTDOWN when
hdev->shutting_down is set.
Before this patch the next request simply overwrote the stale state.
With the new bwi_active check, every NFC_CMD_SE_IO request now fails
with -EBUSY until bwi_timer expires. st21nfca_se_wt_work() then sends
a soft or hard reset to an SE that never received the APDU, toggles
xch_error, and calls cb(cb_context, NULL, 0, -ETIME) for a request
that userspace was already told had failed. If st21nfca_se_deinit()
runs before the timer expires, the callback is never called and
cb_context is leaked.
Should the send failure path roll this back? The timer may already
have fired and queued timeout_work by then, so it looks like
cb_context can only be freed here if this path is the one that clears
bwi_active under se_lock. Is that what you had in mind, or is there a
reason to leave this to the timeout?
[Severity: Medium]
Can this check tell a late response for a timed-out request apart from
a new request that was accepted in the meantime? bwi_active is a single
bit with no request identity, and both st21nfca_se_wt_work() and
st21nfca_hci_se_io() drop se_lock before the timer and the SE are
touched.
Suppose request A times out while its EVT_TRANSMIT_DATA is in flight,
or while this handler is waiting in cancel_work_sync():
st21nfca_se_wt_work()
bwi_active = false;
spin_unlock(&se_lock);
cb(ctxA, NULL, 0, -ETIME);
st21nfca_hci_se_io() /* request B, not -EBUSY */
cb_context = ctxB;
bwi_active = true;
spin_unlock(&se_lock);
mod_timer(&bwi_timer, ...);
st21nfca_apdu_reader_event_received() /* late response for A */
timer_delete_sync(&bwi_timer);
active = bwi_active; /* true, but that is B */
bwi_active = false;
cb(ctxB, skb->data, skb->len, 0);
ctxB is completed and freed with A's data while B is still outstanding
on the SE, and B's real response is dropped later.
Because mod_timer() in st21nfca_hci_se_io() runs after spin_unlock(),
the late response can also land between the two. Then
timer_delete_sync() here has nothing to delete, bwi_active is cleared,
and mod_timer() arms bwi_timer afterwards with bwi_active == false.
The new check in st21nfca_se_wt_work() makes that timer harmless while
the device is up, but st21nfca_se_deinit() only deletes bwi_timer when
bwi_active is true. If the device is removed before the timer expires,
st21nfca_se_wt_timeout() runs after kfree(info) and calls
schedule_work() on freed memory.
Would it make sense to arm bwi_timer under se_lock, and to delete the
timer in st21nfca_se_deinit() unconditionally? The misattribution
itself seems to need some notion of which request a response belongs
to, or refusing new requests until the previous exchange has really
finished. How do you want to handle that?
[Severity: Medium]
This isn't a bug introduced by this patch, but does this error path leak
cb_context?
At this point the request has already been claimed under se_lock.
bwi_active is false, and cb/cb_context exist only in locals.
nfc_hci_send_event() can fail here in three ways:
- -EADDRNOTAVAIL for an invalid DEVICE_MGNT pipe
- -ENOMEM from nfc_hci_hcp_message_tx()
- -ESHUTDOWN when hdev->shutting_down is set
In each case the exit label only frees the skb. nfc_se_io() in
net/nfc/netlink.c frees cb_context only on its own error paths, before
the driver is called. So the struct se_io_ctx is never freed, and
userspace never gets a completion for the request.
The pre-patch code also cleared bwi_active and skipped cb on this path.
>
> - cb(cb_context, skb->data, skb->len, 0);
> + /* The request may have been completed by the timeout already */
> + if (active)
> + cb(cb_context, skb->data, skb->len, 0);
[Severity: Medium]
The commit message says:
A response that arrives after the timeout still gets its
EVT_SE_END_OF_APDU_TRANSFER, as before, but no callback.
Does that still hold if a new request was accepted in the meantime?
active only says that some request is pending. It does not say that this
response belongs to that request.
Consider:
st21nfca_se_wt_work()
bwi_active = false;
cb(ctxA, NULL, 0, -ETIME);
st21nfca_hci_se_io() /* request B passes -EBUSY check */
cb_context = ctxB;
bwi_active = true;
mod_timer(&bwi_timer, ...);
st21nfca_apdu_reader_event_received() /* late response for A */
timer_delete_sync(&bwi_timer); /* stops B's watchdog */
active = bwi_active; /* true, but that is B */
cb(ctxB, skb->data, skb->len, 0); /* A's data given to B */
When B's real response arrives, active is false and the response is
silently dropped. If it never arrives, nothing times B out, because B's
timer was deleted.
Each context is still completed at most once, so the double free is
fixed, and this misattribution predates the patch. But bwi_active is a
single bit with no request identity. The statement in the commit message
only holds when no new request is submitted before the late response.
There is also a smaller variant. st21nfca_se_wt_work() clears bwi_active
and drops se_lock before it queues the soft or hard reset. A new APDU
accepted in that window can be queued ahead of the recovery reset meant
for the previous exchange.
[Severity: High]
This is a pre-existing issue and was not introduced by this patch. The
same double completion still seems to be present in the sibling driver
drivers/nfc/st-nci/se.c, which the commit message doesn't mention.
st_nci_se_wt_timeout() completes the request without checking
bwi_active:
drivers/nfc/st-nci/se.c:st_nci_se_wt_timeout() {
info->se_info.bwi_active = false;
...
info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME);
}
A late ST_NCI_EVT_TRANSMIT_DATA then calls the callback again, with no
check:
drivers/nfc/st-nci/se.c:st_nci_hci_apdu_reader_event_received() {
...
case ST_NCI_EVT_TRANSMIT_DATA:
timer_delete_sync(&info->se_info.bwi_timer);
info->se_info.bwi_active = false;
info->se_info.cb(info->se_info.cb_context,
skb->data, skb->len, 0);
...
}
se_io_cb() in net/nfc/netlink.c does kfree(ctx). Wouldn't the second
call read ctx->dev_idx and ctx->se_idx from freed memory and then free
ctx a second time?
Two related problems exist in st-nci:
- The ST_NCI_EVT_WTX_REQUEST case re-arms bwi_timer without checking
that a request is pending. The timer can then call the stale
cb/cb_context again.
- st_nci_se_io() overwrites cb/cb_context of a pending request with no
busy check. This patch now rejects that case with -EBUSY in
st21nfca_hci_se_io().
> break;
> case ST21NFCA_EVT_WTX_REQUEST:
> mod_timer(&info->se_info.bwi_timer, jiffies +
[Severity: High]
This is a pre-existing issue, but can bwi_timer still be armed when info
is freed during removal?
This WTX case arms bwi_timer without checking or setting bwi_active. A
WTX from the device while no APDU is pending leaves the timer armed with
bwi_active == false. st21nfca_se_deinit() then skips the timer deletion:
drivers/nfc/st21nfca/se.c:st21nfca_se_deinit() {
spin_lock(&info->se_info.se_lock);
bwi_active = info->se_info.bwi_active;
spin_unlock(&info->se_info.se_lock);
if (bwi_active)
timer_delete_sync(&info->se_info.bwi_timer);
...
cancel_work_sync(&info->se_info.timeout_work);
...
}
st21nfca_hci_remove() also calls st21nfca_se_deinit() before
nfc_hci_unregister_device(). A concurrent st21nfca_hci_se_io() or
another WTX event can therefore arm the timer after deinit has sampled
the flag:
st21nfca_hci_remove()
st21nfca_se_deinit(hdev);
nfc_hci_unregister_device(hdev);
nfc_hci_free_device(hdev);
kfree(info);
When the timer expires, st21nfca_se_wt_timeout() runs on the freed info
and calls schedule_work() on a freed work_struct. st21nfca_se_wt_work()
can then read the freed bwi_active and call through a freed cb pointer.
The new bwi_active check in st21nfca_se_wt_work() reads from the freed
object, so it doesn't help here.
The previous commit in this series, "nfc: st21nfca: add spinlock to
serialize SE APDU request state", notes that the teardown ordering
"need[s] a separate fix". No later patch in the series addresses it.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129-st21nfca-v2-v4-0-7d18b104ad46%40gmail.com
prev parent reply other threads:[~2026-11-30 16:44 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 16:25 [PATCH net v4 0/2] nfc: st21nfca: fix SE APDU request race condition Joas Antonio dos Santos via B4 Relay
2026-11-29 16:25 ` [PATCH net v4 1/2] nfc: st21nfca: add spinlock to serialize SE APDU request state Joas Antonio dos Santos via B4 Relay
2026-11-29 16:25 ` [PATCH net v4 2/2] nfc: st21nfca: prevent double completion of SE APDU request Joas Antonio dos Santos via B4 Relay
2026-11-30 16:44 ` netdev-bot+sashiko [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=179605707981.3146.15516824656082527187@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=david@ixit.cz \
--cc=horms@kernel.org \
--cc=joasantonio108@gmail.com \
--cc=krzk@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=oe-linux-nfc@lists.linux.dev \
/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
all inboxes | Powered by JetHome®