From: willemb@google.com
To: Joas Antonio <joasantonio108@gmail.com>, netdev-bot+sashiko@kernel.org
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: Sat, 05 Dec 2026 23:04:48 -0500 [thread overview]
Message-ID: <willemb.126fad54666f2@google.com> (raw)
In-Reply-To: <CAOCBhwF4+0ZBrWLX6Eri-986xfJUi+xZzeb+nJp7-z8_7tnTiA@mail.gmail.com>
Joas Antonio wrote:
> Hi,
> Thanks for the review. Answering the three Medium items:
> 1) Commit message, "a response that arrives after the timeout gets no
> callback".
> Right, that only holds if no new request was accepted in between.
> EVT_TRANSMIT_DATA carries no request id, so the driver cannot tell a
> late response from the answer to the next request. This is the
> second item in the "not addressed here" list of the cover letter. I
> can reword that sentence in the commit message if a new version is
> needed.
> 2) Race on bwi_active and bwi_timer (mod_timer() outside se_lock, timer
> left armed with bwi_active false, st21nfca_se_deinit() only deleting
> it when bwi_active is set).
> Agreed that this exists. It is the teardown problem Simon looked at
> in v3 and considered suitable for a follow-up, and it is the first
> item of the list in the cover letter. The fix needs the remove
> ordering changed (deinit runs before nfc_hci_unregister_device()), so
> I would rather do it as its own patch than grow this one.
This is not solely a remove-ordering issue:
- Patch 1/2 moved bwi_active = true before mod_timer(), but drops
se_lock at
se.c:240 before calling mod_timer(&info->se_info.bwi_timer, ...) at
se.c:241-242. A concurrent ST21NFCA_EVT_TRANSMIT_DATA landing between
lines
240 and 241 runs timer_delete_sync() (se.c:405, no-op) and clears
bwi_active = false (se.c:410), after which mod_timer() arms bwi_timer
with
bwi_active == false. Likewise, se.c:405 runs timer_delete_sync() before
taking se_lock at se.c:408, so st21nfca_hci_se_io() can still arm the
timer
between lines 405 and 408 even once mod_timer() is locked.
- Leaving bwi_timer armed with bwi_active == false causes a runtime bug
even
without device removal: if the orphaned timer expires and
st21nfca_se_wt_timeout() (se.c:299-305) schedules timeout_work, a newly
accepted request submitted before timeout_work runs passes the
if (!info->se_info.bwi_active) check (se.c:278) and is prematurely
failed
with -ETIME plus a spurious SE reset.
- It also triggers the teardown UAF because st21nfca_se_deinit()
(se.c:469-474, already modified in v4) still guards timer_delete_sync()
with if (bwi_active).
- Locking analysis: se_lock is taken only in process context (genl, wq,
remove); st21nfca_se_wt_timeout() (se.c:299-305) only calls
schedule_work()
and never takes se_lock. Plain spin_lock() is safe, and mod_timer() /
non-blocking timer_delete() can be called directly under se_lock (note:
do not call timer_delete_sync() after unlocking se_lock in the RX path,
or it could delete a subsequent request's timer). Calling mod_timer()
under
se_lock in st21nfca_hci_se_io(), deleting the timer under se_lock (via
timer_delete()) when clearing bwi_active in the RX path, guarding
ST21NFCA_EVT_WTX_REQUEST's mod_timer() (se.c:425) with if (bwi_active)
under se_lock, and dropping the if (bwi_active) guard before
timer_delete_sync() in st21nfca_se_deinit() (se.c:473-474) are all
small
changes that do not require reordering st21nfca_hci_remove()
(core.c:1027-1028).
> 3) No rollback when nfc_hci_send_event() fails in st21nfca_hci_se_io().
> Also real, and also discussed in v3: the request stays pending until
> the BWI timeout completes it with -ETIME. With this patch new
> requests get -EBUSY during that time instead of overwriting the
> pending one. Rolling it back safely means deciding who owns
> cb_context once the timer may have fired, which I would like to
> handle together with 2).
- nfc_hci_send_event() fails synchronously before queuing onto
msg_tx_queue
with -EADDRNOTAVAIL (net/nfc/hci/command.c:99-100), -ENOMEM
(net/nfc/hci/hcp.c:35, 63), or -ESHUTDOWN (net/nfc/hci/hcp.c:94).
nfc_se_io() (net/nfc/netlink.c:1458-1462) does not free cb_context once
dev->ops->se_io() is entered.
- Behavioral regression from patch 2/2: previously a retry overwrote the
stale state; with the new if (info->se_info.bwi_active) guard
(se.c:231-236), every subsequent NFC_CMD_SE_IO fails with -EBUSY until
bwi_timer expires. This is NOT permanent while timers run, but
wt_timeout
(ST21NFCA_BWI_TO_TIMEOUT(bwi) = (1 << bwi) * 200 ms, se.c:40, 80-81,
459-460) is 3.2 s by default (BWI=4), up to 102.4 s for valid ISO7816-3
max BWI=9, and up to ~109 min for an unclamped 4-bit BWI=15 from ATR
(se.c:61). When the timer expires, st21nfca_se_wt_work() sends a
spurious
soft/hard SE reset (toggling xch_error; on alternate -EADDRNOTAVAIL
errors
for APDU_READER_GATE, HARD_RESET goes to DEVICE_MGNT_GATE,
se.c:287-295)
and invokes se_io_cb(cb_context, NULL, 0, -ETIME) (se.c:296,
net/nfc/netlink.c:1475-1502) for a command that already returned an
error.
On -ESHUTDOWN after st21nfca_se_deinit() has already run
(core.c:1027-1028, net/nfc/hci/core.c:1055), failing to roll back also
leaves bwi_timer armed across kfree(info) (core.c:1030).
- Ownership is already trivial and independent of teardown ordering:
nfc_se_io() (net/nfc/netlink.c:1430-1461) is the sole caller of
dev->ops->se_io() and holds device_lock(&dev->dev) throughout, so no
second
st21nfca_hci_se_io() can re-set bwi_active while the failing call is
still
active. On r < 0, taking se_lock, claiming and clearing bwi_active,
unlocking, and (if owned) calling timer_delete_sync(&bwi_timer),
cancel_work_sync(&timeout_work) (safe: st21nfca_se_wt_work() never
takes
device_lock), and kfree(cb_context) resolves ownership cleanly — or
alternatively bwi_timer can be armed under se_lock (if bwi_active is
still
true) only after nfc_hci_send_event() returns 0.
> The pre-existing items (st-nci has the same double completion, the
> cb_context leak when EVT_SE_END_OF_APDU_TRANSFER fails) are not touched
> by this series. I plan to send the st-nci patch once the approach here
> is agreed on.
> Simon, does that split still work for you, or would you prefer the
> timer handling folded into this series?
> Thanks,
> Joas
> Em seg., 30 de nov. de 2026 às 13:44, <netdev-bot+sashiko@kernel.org>
> escreveu:
> >
> > 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
> --
> Joas Antonio dos Santos
> Cyber Security Specialist and Ethical Hacker
> joasantonio108@gmail.com
> LinkedIn
next prev parent reply other threads:[~2026-12-06 4:04 UTC|newest]
Thread overview: 7+ 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
2026-12-01 15:25 ` Joas Antonio
2026-12-06 4:04 ` willemb [this message]
2026-12-06 4:07 ` [PATCH net v4 0/2] nfc: st21nfca: fix SE APDU request race condition willemb
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=willemb.126fad54666f2@google.com \
--to=willemb@google.com \
--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-bot+sashiko@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