All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Horman <horms@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
Subject: Re: [PATCH net v3 1/2] nfc: st21nfca: add spinlock to serialize SE APDU request state
Date: Wed, 18 Nov 2026 10:05:06 +0000	[thread overview]
Message-ID: <20261118100506.GB860048@horms.kernel.org> (raw)
In-Reply-To: <20261112-st21nfca-v2-v3-1-c81702269e01@gmail.com>

On Thu, Nov 12, 2026 at 01:39:29PM +0000, Joas Antonio dos Santos via B4 Relay wrote:
> 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
> 
> Add a spinlock (se_lock) and take it around the accesses in these
> three 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() is not changed here.  It still accesses
> bwi_active without the lock; the teardown ordering has problems of its
> own which need a separate fix.
> 
> Found by LLM-assisted manual 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>

Hi Joas,

sashiko.dev has provided an AI-generated review of this patch,
which seems to raise issues not covered so far.

  Can releasing se_lock before mod_timer() leave bwi_timer armed while
  bwi_active is false?

  CPU0                                    CPU1
  st21nfca_hci_se_io()
    spin_lock(&se_lock)
    bwi_active = true
    spin_unlock(&se_lock)
                                          st21nfca_apdu_reader_event_received()
                                            timer_delete_sync(&bwi_timer)
                                            ...
                                            spin_lock(&se_lock)
                                            bwi_active = false
                                            spin_unlock(&se_lock)
    mod_timer(&bwi_timer, ...)
                                          st21nfca_hci_remove()
                                            st21nfca_se_deinit()
                                              /* bwi_active is false, skips
                                               * timer_delete_sync() */
                                            kfree(info)
  <softirq>
    st21nfca_se_wt_timeout()
      schedule_work(&info->...)  <-- use-after-free

  The ST21NFCA_EVT_WTX_REQUEST branch in st21nfca_apdu_reader_event_received()
  also calls mod_timer(&info->se_info.bwi_timer, ...) without holding se_lock or
  checking bwi_active, which likewise re-arms bwi_timer after a timeout or on an
  unsolicited event while bwi_active is false.

  In addition, st21nfca_se_deinit() reads and clears bwi_active without se_lock,
  only calls timer_delete_sync(&info->se_info.bwi_timer) when bwi_active is
  true, and leaks cb_context if it cancels an in-flight request:

  drivers/nfc/st21nfca/se.c:st21nfca_se_deinit() {
  	if (info->se_info.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);
  	info->se_info.bwi_active = false;
  	info->se_info.se_active = false;
  }

  These gaps are not addressed in the follow-up commit "nfc: st21nfca: prevent
  double completion of SE APDU request" either.

  Link: https://sashiko.dev/#/patchset/20261112-st21nfca-v2-v3-0-c81702269e01%40gmail.com?part=1

I actually think there are two issues here.

1. The headline issue relating to races in teardown. This strikes me as
   a pre-existing problem. And suitable for follow-up.

2. This part: " In addition, st21nfca_se_deinit() reads and clears
   bwi_active without se_lock". And it seems to me it would be worth
   addressing that in an updated version of this patch for completeness.


  reply	other threads:[~2026-11-18 10:05 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 13:39 [PATCH net v3 0/2] nfc: st21nfca: fix SE APDU request race condition Joas Antonio dos Santos
2026-11-12 13:39 ` Joas Antonio dos Santos via B4 Relay
2026-11-12 13:39 ` [PATCH net v3 1/2] nfc: st21nfca: add spinlock to serialize SE APDU request state Joas Antonio dos Santos
2026-11-12 13:39   ` Joas Antonio dos Santos via B4 Relay
2026-11-18 10:05   ` Simon Horman [this message]
2026-11-12 13:39 ` [PATCH net v3 2/2] nfc: st21nfca: prevent double completion of SE APDU request Joas Antonio dos Santos
2026-11-12 13:39   ` Joas Antonio dos Santos via B4 Relay
2026-11-18 10:05   ` Simon Horman

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=20261118100506.GB860048@horms.kernel.org \
    --to=horms@kernel.org \
    --cc=david@ixit.cz \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.