The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: willemb@google.com
To: Joas Antonio dos Santos via B4 Relay
	<devnull+joasantonio108.gmail.com@kernel.org>,
	david@ixit.cz,  krzk@kernel.org, joasantonio108@gmail.com
Cc: oe-linux-nfc@lists.linux.dev, netdev@vger.kernel.org,
	 linux-kernel@vger.kernel.org, horms@kernel.org
Subject: Re: [PATCH net v4 0/2] nfc: st21nfca: fix SE APDU request race condition
Date: Sat, 05 Dec 2026 23:07:17 -0500	[thread overview]
Message-ID: <willemb.16359748911d6@google.com> (raw)
In-Reply-To: <20261129-st21nfca-v2-v4-0-7d18b104ad46@gmail.com>

Joas Antonio dos Santos via B4 Relay wrote:
> 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>



Also shared LLM analysis to the rebuttal, but perhaps more
readable and concise:

Please fold the three Medium Sashiko items into a v5 rather than
deferring them:

1) Roll back state when nfc_hci_send_event() fails in
st21nfca_hci_se_io() (drivers/nfc/st21nfca/se.c). With the new bwi_active
check in patch 2/2, a synchronous send failure (-ENOMEM, -EADDRNOTAVAIL,
-ESHUTDOWN) now rejects subsequent requests with -EBUSY until bwi_timer
expires (3.2 s default, up to 102.4 s for BWI=9), then triggers a
spurious SE reset and -ETIME callback. nfc_se_io() holds
device_lock(&dev->dev) across the call, so no second request can re-set
bwi_active concurrently; claiming and clearing bwi_active under se_lock
on error (then timer_delete_sync(), cancel_work_sync(), and
kfree(cb_context) if still owned — or arming bwi_timer under se_lock only
after nfc_hci_send_event() succeeds) decides ownership cleanly without
changing remove ordering.

2) Keep bwi_timer and bwi_active synchronized under se_lock
(st21nfca_se_wt_timeout() does not take se_lock): arm bwi_timer under
se_lock in st21nfca_hci_se_io(), guard the WTX mod_timer() under se_lock
with bwi_active, call timer_delete() under se_lock when clearing
bwi_active in st21nfca_apdu_reader_event_received() (not
timer_delete_sync() after unlocking, which could delete a new request's
timer), and drop the conditional if (bwi_active) before
timer_delete_sync() in st21nfca_se_deinit(). Reordering
st21nfca_hci_remove() can still stay in a separate patch, but leaving
bwi_timer armed with bwi_active == false also affects live operation
(stale timeout_work aborting a later request).

3) Reword the "no callback" sentence in patch 2/2's commit message so the
no-intervening-request caveat is recorded in git history, not just the
cover letter.

Since this is build-tested only without hardware and we are past -rc4,
please also target v5 at net-next (keep the Fixes tags and add Cc:
stable+noautosel@kernel.org # untested fix to unlikely driver error
path).

pw-bot: cr

      parent reply	other threads:[~2026-12-06  4:07 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
2026-12-06  4:07 ` willemb [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=willemb.16359748911d6@google.com \
    --to=willemb@google.com \
    --cc=david@ixit.cz \
    --cc=devnull+joasantonio108.gmail.com@kernel.org \
    --cc=horms@kernel.org \
    --cc=joasantonio108@gmail.com \
    --cc=krzk@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