From: Shubham Antil <shubham@octane.security>
To: netdev@vger.kernel.org
Cc: oe-linux-nfc@lists.linux.dev, david@ixit.cz, horms@kernel.org,
gio@octane.security, stable@vger.kernel.org
Subject: [PATCH net v8 2/4] nfc: reject overlapping firmware downloads
Date: Thu, 19 Nov 2026 09:32:51 +0530 [thread overview]
Message-ID: <20261119040253.15986-3-shubham@octane.security> (raw)
In-Reply-To: <20261119040253.15986-1-shubham@octane.security>
nfc_fw_download() records an asynchronous download before calling the
driver, but does not reject another request while that download is active.
If the driver rejects the second request with -EBUSY, the core clears
fw_download_in_progress even though the first request is still running.
Firmware frames can then be routed through the normal receive path and the
device can be brought up before the original download completes.
Check the core state while holding the NFC device lock and reject an
overlapping request without changing that state. Keep asynchronous
completion lockless because drivers can report completion while holding
their own locks, while the start path calls into those drivers with the NFC
device lock held. Nesting the device lock in both directions would create
an inverse lock order. Use one-copy accesses in the completion, receive,
and transmit callbacks, which cannot take the device lock, so all contexts
observe the state without a data race.
Discovered during an AI-assisted source audit using OpenAI Codex,
gpt-5.6-sol. Patch development and test planning used the same assistant.
Confirmed using an unmodified kernel and a pty-backed user-space controller
which issued two NFC_CMD_FW_DOWNLOAD requests and checked that the second
returned -EBUSY without disrupting the first; no error injection was used.
With the final v8 fix applied, 105 first/overlapping request pairs
passed under KASAN, UBSAN, and lockdep, and 105 pairs passed under
strict-mode KCSAN.
No in-tree selftest covers the NFC firmware-download path.
Fixes: 9674da8759df ("NFC: Add firmware upload netlink command")
Cc: stable@vger.kernel.org
Signed-off-by: Shubham Antil <shubham@octane.security>
---
drivers/nfc/nfcmrvl/main.c | 2 +-
drivers/nfc/nfcmrvl/uart.c | 4 ++--
net/nfc/core.c | 13 +++++++++----
3 files changed, 12 insertions(+), 7 deletions(-)
diff --git a/drivers/nfc/nfcmrvl/main.c b/drivers/nfc/nfcmrvl/main.c
index 6efa83219..b9b66127f 100644
--- a/drivers/nfc/nfcmrvl/main.c
+++ b/drivers/nfc/nfcmrvl/main.c
@@ -205,7 +205,7 @@ int nfcmrvl_nci_recv_frame(struct nfcmrvl_private *priv, struct sk_buff *skb)
}
}
- if (priv->ndev->nfc_dev->fw_download_in_progress) {
+ if (READ_ONCE(priv->ndev->nfc_dev->fw_download_in_progress)) {
nfcmrvl_fw_dnld_recv_frame(priv, skb);
return 0;
}
diff --git a/drivers/nfc/nfcmrvl/uart.c b/drivers/nfc/nfcmrvl/uart.c
index 9aedd1687..4b989fc4e 100644
--- a/drivers/nfc/nfcmrvl/uart.c
+++ b/drivers/nfc/nfcmrvl/uart.c
@@ -159,7 +159,7 @@ static void nfcmrvl_nci_uart_tx_start(struct nci_uart *nu)
{
struct nfcmrvl_private *priv = (struct nfcmrvl_private *)nu->drv_data;
- if (priv->ndev->nfc_dev->fw_download_in_progress)
+ if (READ_ONCE(priv->ndev->nfc_dev->fw_download_in_progress))
return;
/* Remove BREAK to wake up the NFCC */
@@ -173,7 +173,7 @@ static void nfcmrvl_nci_uart_tx_done(struct nci_uart *nu)
{
struct nfcmrvl_private *priv = (struct nfcmrvl_private *)nu->drv_data;
- if (priv->ndev->nfc_dev->fw_download_in_progress)
+ if (READ_ONCE(priv->ndev->nfc_dev->fw_download_in_progress))
return;
/*
diff --git a/net/nfc/core.c b/net/nfc/core.c
index f52166929..b03b34b40 100644
--- a/net/nfc/core.c
+++ b/net/nfc/core.c
@@ -48,15 +48,20 @@ int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
goto error;
}
+ if (READ_ONCE(dev->fw_download_in_progress)) {
+ rc = -EBUSY;
+ goto error;
+ }
+
if (!dev->ops->fw_download) {
rc = -EOPNOTSUPP;
goto error;
}
- dev->fw_download_in_progress = true;
+ WRITE_ONCE(dev->fw_download_in_progress, true);
rc = dev->ops->fw_download(dev, firmware_name);
if (rc)
- dev->fw_download_in_progress = false;
+ WRITE_ONCE(dev->fw_download_in_progress, false);
error:
device_unlock(&dev->dev);
@@ -73,7 +78,7 @@ int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
u32 result)
{
- dev->fw_download_in_progress = false;
+ WRITE_ONCE(dev->fw_download_in_progress, false);
return nfc_genl_fw_download_done(dev, firmware_name, result);
}
@@ -104,7 +109,7 @@ int nfc_dev_up(struct nfc_dev *dev)
goto error;
}
- if (dev->fw_download_in_progress) {
+ if (READ_ONCE(dev->fw_download_in_progress)) {
rc = -EBUSY;
goto error;
}
--
2.54.0
next prev parent reply other threads:[~2026-11-19 4:03 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 4:02 [PATCH net v8 0/4] nfc: order NCI UART and firmware teardown Shubham Antil
2026-11-19 4:02 ` [PATCH net v8 1/4] nfc: nci: uart: serialize driver selection Shubham Antil
2026-11-19 4:02 ` Shubham Antil [this message]
2026-11-19 4:02 ` [PATCH net v8 3/4] nfc: nci: uart: quiesce callbacks during teardown Shubham Antil
2026-11-19 4:02 ` [PATCH net v8 4/4] nfc: nfcmrvl: stop firmware work before unregistering Shubham Antil
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=20261119040253.15986-3-shubham@octane.security \
--to=shubham@octane.security \
--cc=david@ixit.cz \
--cc=gio@octane.security \
--cc=horms@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=oe-linux-nfc@lists.linux.dev \
--cc=stable@vger.kernel.org \
/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