* [PATCH net v4 0/2] nfc: st21nfca: fix SE APDU request race condition
@ 2026-11-29 16:25 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
0 siblings, 2 replies; 4+ messages in thread
From: Joas Antonio dos Santos via B4 Relay @ 2026-11-29 16:25 UTC (permalink / raw)
To: david, krzk, joasantonio108; +Cc: oe-linux-nfc, netdev, linux-kernel, horms
The BWI timeout work and the EVT_TRANSMIT_DATA handler can both
complete the same SE APDU request. The callback frees its context, so
the second completion is a use-after-free followed by a double free.
1/2: Add a spinlock around bwi_active, cb and cb_context in the
paths that use them.
2/2: Use the lock to complete a request only once and to reject
overlapping requests.
The series is build-tested only (W=1, arm64); I do not have the
hardware.
Not addressed here, all older than this series and pointed out by the
Sashiko reviews of v2 and v3:
- st21nfca_se_deinit() can leave bwi_timer armed when info is freed
(it runs before nfc_hci_unregister_device() and only deletes the
timer when bwi_active is set), and it drops a pending request
without completing it. The remove ordering needs its own patch.
- EVT_TRANSMIT_DATA carries no request id, so a response that arrives
after its request timed out can still be taken for the answer to the
next request.
- When nfc_hci_send_event() fails in st21nfca_hci_se_io() the request
stays pending until the BWI timeout completes it with -ETIME.
- drivers/nfc/st-nci/se.c has the same double completion. I will send
a separate patch for it once the approach here is agreed on.
---
Changes in v4:
- 1/2: st21nfca_se_deinit() takes se_lock around its read and clear of
bwi_active (Simon Horman). The teardown ordering itself is still left
for a follow-up.
- Link to v3: https://lore.kernel.org/r/20261112-st21nfca-v2-v3-0-c81702269e01@gmail.com
Changes in v3:
- 2/2: free cb_context when a request is rejected with -EBUSY; it was
leaked (Sashiko).
- 2/2: still send EVT_SE_END_OF_APDU_TRANSFER for a response that
arrives after the timeout, as before the series; only the callback is
skipped (Sashiko).
- 1/2: comment on what se_lock protects (checkpatch), commit message
no longer claims every access is serialized.
- Rebased on v7.4-rc3+, target tree added to the subject.
- Link to v2: https://lore.kernel.org/r/20261110-st21nfca-v2-v2-0-cf9102b314cb@gmail.com
Changes in v2:
- Use a spinlock instead of plain bwi_active flag checks (Simon Horman).
- Split into two patches: locking, then the completion fix.
- Set bwi_active before mod_timer().
- Copy cb/cb_context under the lock, call it after unlocking.
- Reject overlapping se_io requests with -EBUSY.
- Drop the Assisted-by trailer.
- Link to v1: https://lore.kernel.org/r/179332800288.52644.8728400883396719965@gmail.com
---
Joas Antonio dos Santos (2):
nfc: st21nfca: add spinlock to serialize SE APDU request state
nfc: st21nfca: prevent double completion of SE APDU request
drivers/nfc/st21nfca/se.c | 48 ++++++++++++++++++++++++++++++++++++-----
drivers/nfc/st21nfca/st21nfca.h | 2 ++
2 files changed, 45 insertions(+), 5 deletions(-)
---
base-commit: 9d66f26fefacebfeed15de31c1123451a17ff119
change-id: 20261108-st21nfca-v2-f4c5f861e416
Best regards,
--
Joas Antonio dos Santos <joasantonio108@gmail.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH net v4 1/2] nfc: st21nfca: add spinlock to serialize SE APDU request state
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 ` 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
1 sibling, 0 replies; 4+ messages in thread
From: Joas Antonio dos Santos via B4 Relay @ 2026-11-29 16:25 UTC (permalink / raw)
To: david, krzk, joasantonio108; +Cc: oe-linux-nfc, netdev, linux-kernel, horms
From: Joas Antonio dos Santos <joasantonio108@gmail.com>
The bwi_active flag, callback pointer (cb), and callback context
(cb_context) in st21nfca_se_info are accessed concurrently from
multiple contexts without synchronization:
- st21nfca_hci_se_io() sets them from the netlink path
- st21nfca_se_wt_work() reads/clears them from the timeout workqueue
- st21nfca_apdu_reader_event_received() reads/clears them from the
HCI rx path
- st21nfca_se_deinit() reads/clears bwi_active on device removal
Add a spinlock (se_lock) and take it around the accesses in these
paths. The callback pointer and context are copied under the lock and
invoked after releasing it, since both the callback and
nfc_hci_send_event() may sleep.
Also move the bwi_active = true assignment before mod_timer() so that
the flag is always set before the timeout handler could observe it.
st21nfca_se_deinit() only gets the lock around its read and clear of
bwi_active. The teardown ordering has problems of its own which need
a separate fix.
Found by code review.
Fixes: 2130fb97fecf ("NFC: st21nfca: Adding support for secure element")
Cc: Simon Horman <horms@kernel.org>
Signed-off-by: Joas Antonio dos Santos <joasantonio108@gmail.com>
---
drivers/nfc/st21nfca/se.c | 34 +++++++++++++++++++++++++++++-----
drivers/nfc/st21nfca/st21nfca.h | 2 ++
2 files changed, 31 insertions(+), 5 deletions(-)
diff --git a/drivers/nfc/st21nfca/se.c b/drivers/nfc/st21nfca/se.c
index 7154bc1d644a..3726b00b7580 100644
--- a/drivers/nfc/st21nfca/se.c
+++ b/drivers/nfc/st21nfca/se.c
@@ -227,11 +227,13 @@ int st21nfca_hci_se_io(struct nfc_hci_dev *hdev, u32 se_idx,
switch (se_idx) {
case ST21NFCA_ESE_HOST_ID:
+ spin_lock(&info->se_info.se_lock);
info->se_info.cb = cb;
info->se_info.cb_context = cb_context;
+ info->se_info.bwi_active = true;
+ spin_unlock(&info->se_info.se_lock);
mod_timer(&info->se_info.bwi_timer, jiffies +
msecs_to_jiffies(info->se_info.wt_timeout));
- info->se_info.bwi_active = true;
return nfc_hci_send_event(hdev, ST21NFCA_APDU_READER_GATE,
ST21NFCA_EVT_TRANSMIT_DATA,
apdu, apdu_length);
@@ -260,11 +262,17 @@ static void st21nfca_se_wt_work(struct work_struct *work)
*/
/* hardware reset managed through VCC_UICC_OUT power supply */
u8 param = 0x01;
+ se_io_cb_t cb;
+ void *cb_context;
struct st21nfca_hci_info *info = container_of(work,
struct st21nfca_hci_info,
se_info.timeout_work);
+ spin_lock(&info->se_info.se_lock);
info->se_info.bwi_active = false;
+ cb = info->se_info.cb;
+ cb_context = info->se_info.cb_context;
+ spin_unlock(&info->se_info.se_lock);
if (!info->se_info.xch_error) {
info->se_info.xch_error = true;
@@ -275,7 +283,7 @@ static void st21nfca_se_wt_work(struct work_struct *work)
nfc_hci_send_event(info->hdev, ST21NFCA_DEVICE_MGNT_GATE,
ST21NFCA_EVT_SE_HARD_RESET, ¶m, 1);
}
- info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME);
+ cb(cb_context, NULL, 0, -ETIME);
}
static void st21nfca_se_wt_timeout(struct timer_list *t)
@@ -375,6 +383,8 @@ int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev,
u8 event, struct sk_buff *skb)
{
int r = 0;
+ se_io_cb_t cb;
+ void *cb_context;
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
pr_debug("apdu reader gate event: %x\n", event);
@@ -383,14 +393,19 @@ int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev,
case ST21NFCA_EVT_TRANSMIT_DATA:
timer_delete_sync(&info->se_info.bwi_timer);
cancel_work_sync(&info->se_info.timeout_work);
+
+ spin_lock(&info->se_info.se_lock);
info->se_info.bwi_active = false;
+ cb = info->se_info.cb;
+ cb_context = info->se_info.cb_context;
+ spin_unlock(&info->se_info.se_lock);
+
r = nfc_hci_send_event(hdev, ST21NFCA_DEVICE_MGNT_GATE,
ST21NFCA_EVT_SE_END_OF_APDU_TRANSFER, NULL, 0);
if (r < 0)
goto exit;
- info->se_info.cb(info->se_info.cb_context,
- skb->data, skb->len, 0);
+ cb(cb_context, skb->data, skb->len, 0);
break;
case ST21NFCA_EVT_WTX_REQUEST:
mod_timer(&info->se_info.bwi_timer, jiffies +
@@ -416,6 +431,7 @@ void st21nfca_se_init(struct nfc_hci_dev *hdev)
/* initialize timers */
timer_setup(&info->se_info.bwi_timer, st21nfca_se_wt_timeout, 0);
info->se_info.bwi_active = false;
+ spin_lock_init(&info->se_info.se_lock);
timer_setup(&info->se_info.se_active_timer,
st21nfca_se_activation_timeout, 0);
@@ -434,14 +450,22 @@ EXPORT_SYMBOL(st21nfca_se_init);
void st21nfca_se_deinit(struct nfc_hci_dev *hdev)
{
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
+ bool bwi_active;
+
+ spin_lock(&info->se_info.se_lock);
+ bwi_active = info->se_info.bwi_active;
+ spin_unlock(&info->se_info.se_lock);
- if (info->se_info.bwi_active)
+ if (bwi_active)
timer_delete_sync(&info->se_info.bwi_timer);
if (info->se_info.se_active)
timer_delete_sync(&info->se_info.se_active_timer);
cancel_work_sync(&info->se_info.timeout_work);
+
+ spin_lock(&info->se_info.se_lock);
info->se_info.bwi_active = false;
+ spin_unlock(&info->se_info.se_lock);
info->se_info.se_active = false;
}
EXPORT_SYMBOL(st21nfca_se_deinit);
diff --git a/drivers/nfc/st21nfca/st21nfca.h b/drivers/nfc/st21nfca/st21nfca.h
index ae6771cc9894..03e41fadb5ed 100644
--- a/drivers/nfc/st21nfca/st21nfca.h
+++ b/drivers/nfc/st21nfca/st21nfca.h
@@ -6,6 +6,7 @@
#ifndef __LOCAL_ST21NFCA_H_
#define __LOCAL_ST21NFCA_H_
+#include <linux/spinlock.h>
#include <net/nfc/hci.h>
#include <linux/skbuff.h>
#include <linux/workqueue.h>
@@ -131,6 +132,7 @@ struct st21nfca_se_info {
struct timer_list bwi_timer;
int wt_timeout; /* in msecs */
bool bwi_active;
+ spinlock_t se_lock; /* protects bwi_active, cb and cb_context */
struct timer_list se_active_timer;
bool se_active;
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH net v4 2/2] nfc: st21nfca: prevent double completion of SE APDU request
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 ` Joas Antonio dos Santos via B4 Relay
2026-11-30 16:44 ` netdev-bot+sashiko
1 sibling, 1 reply; 4+ messages in thread
From: Joas Antonio dos Santos via B4 Relay @ 2026-11-29 16:25 UTC (permalink / raw)
To: david, krzk, joasantonio108; +Cc: oe-linux-nfc, netdev, linux-kernel, horms
From: Joas Antonio dos Santos <joasantonio108@gmail.com>
Use the spinlock added in the previous commit to prevent completing an
SE APDU request more than once. Without this, the timeout work handler
(st21nfca_se_wt_work) and the EVT_TRANSMIT_DATA event handler can both
invoke the callback for the same request. The callback frees its
context, so the second call is a use-after-free followed by a double
free.
Check bwi_active under the lock in both completion paths and only
complete a request that is still pending. A response that arrives
after the timeout still gets its EVT_SE_END_OF_APDU_TRANSFER, as
before, but no callback.
Also reject a new SE APDU request in st21nfca_hci_se_io() with -EBUSY
while one is in progress, instead of overwriting the callback and
context of the pending request. The callback is not called in that
case, so free the context like the -ENODEV path does; nfc_se_io() does
not free it once the driver has been called.
Found by code review.
Fixes: 2130fb97fecf ("NFC: st21nfca: Adding support for secure element")
Cc: Simon Horman <horms@kernel.org>
Signed-off-by: Joas Antonio dos Santos <joasantonio108@gmail.com>
---
drivers/nfc/st21nfca/se.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/drivers/nfc/st21nfca/se.c b/drivers/nfc/st21nfca/se.c
index 3726b00b7580..33882f873909 100644
--- a/drivers/nfc/st21nfca/se.c
+++ b/drivers/nfc/st21nfca/se.c
@@ -228,6 +228,12 @@ int st21nfca_hci_se_io(struct nfc_hci_dev *hdev, u32 se_idx,
switch (se_idx) {
case ST21NFCA_ESE_HOST_ID:
spin_lock(&info->se_info.se_lock);
+ if (info->se_info.bwi_active) {
+ spin_unlock(&info->se_info.se_lock);
+ /* The callback is not called, see the comment below */
+ kfree(cb_context);
+ return -EBUSY;
+ }
info->se_info.cb = cb;
info->se_info.cb_context = cb_context;
info->se_info.bwi_active = true;
@@ -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;
@@ -383,6 +393,7 @@ int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev,
u8 event, struct sk_buff *skb)
{
int r = 0;
+ bool active;
se_io_cb_t cb;
void *cb_context;
struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
@@ -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;
- 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);
break;
case ST21NFCA_EVT_WTX_REQUEST:
mod_timer(&info->se_info.bwi_timer, jiffies +
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH net v4 2/2] nfc: st21nfca: prevent double completion of SE APDU request
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
0 siblings, 0 replies; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 16:44 UTC (permalink / raw)
To: joasantonio108
Cc: david, krzk, oe-linux-nfc, netdev, linux-kernel, horms, kuba
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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-30 16:44 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox