From: netdev-bot+sashiko@kernel.org
To: shubham@octane.security
Cc: netdev@vger.kernel.org, horms@kernel.org, gio@octane.security,
david@ixit.cz, oe-linux-nfc@lists.linux.dev, kuba@kernel.org
Subject: Re: [PATCH net v9 2/3] nfc: nfcmrvl: serialize firmware completion and teardown
Date: Tue, 01 Dec 2026 14:04:45 +0000 [thread overview]
Message-ID: <179613388583.3146.2330494470837938376@kernel.org> (raw)
In-Reply-To: <20261130130240.65411-3-shubham@octane.security>
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 1 (2) · Medium: 0 · 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).
- [High] nfcmrvl_fw_dnld_start() (drivers/nfc/nfcmrvl/fw_dnld.c) checks
only fw_dnld->stopping.
Pre-existing issues:
- [High] process_state_fw_dnld() (drivers/nfc/nfcmrvl/fw_dnld.c) trusts
the 16-bit chunk length that the NFC controller sends.
- [High] On the I2C and SPI PHYs, the NFCC interrupt is requested with
devm_request_threaded_irq() in probe.
--
nfc: nfcmrvl: serialize firmware completion and teardown
This adds spinlock-protected active, stopping and starting state, plus an
rx_active counter, to the nfcmrvl firmware download code. The goal is to
serialize receive work, timeout completion and teardown. Timeout handling
moves to a work item, and fw_dnld_over() drains receivers, stops the timer
and work items, and cancels pending NCI commands before releasing the
firmware.
> diff --git a/drivers/nfc/nfcmrvl/fw_dnld.c b/drivers/nfc/nfcmrvl/fw_dnld.c
> index 8b9d5257320dc..00c0e9e3e548b 100644
> --- a/drivers/nfc/nfcmrvl/fw_dnld.c
> +++ b/drivers/nfc/nfcmrvl/fw_dnld.c
[ ... ]
> @@ -395,62 +420,80 @@ static int process_state_boot(struct nfcmrvl_private *priv,
>
> static void fw_dnld_rx_work(struct work_struct *work)
> {
[ ... ]
> + spin_lock_irqsave(&fw_dnld->rx_lock, flags);
> + if (fw_dnld->stopping || fw_dnld->starting || !fw_dnld->active) {
> + spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
> + return;
> + }
> + skb = skb_dequeue(&fw_dnld->rx_q);
> + spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
> + if (!skb)
> + return;
[ ... ]
> + case STATE_FW_DNLD:
> + ret = process_state_fw_dnld(priv, skb);
> + break;
[Severity: High]
This isn't a bug introduced by this patch, but this path is being reworked,
so it seemed worth asking. Does process_state_fw_dnld() trust the chunk
length from the controller too much?
In SUBSTATE_WAIT_COMMAND the length comes straight from the device. The only
check is against a complement that the device also supplies:
len = get_unaligned_le16(skb->data);
...
if (((~len) & 0xFFFF) != comp_len) {
...
priv->fw_dnld.chunk_len = len;
Then in SUBSTATE_WAIT_ACK_CREDIT:
out_skb = alloc_lc_skb(priv, priv->fw_dnld.chunk_len);
...
skb_put_data(out_skb,
((uint8_t *)priv->fw_dnld.fw->data) + priv->fw_dnld.offset,
priv->fw_dnld.chunk_len);
alloc_lc_skb() takes a uint8_t plen. A chunk_len such as 0xFF00 therefore
sizes the skb for only len & 0xff payload bytes, and hdr->plen is also
wrong.
Can skb_put_data() then hit skb_over_panic() once chunk_len exceeds the
tailroom?
Separately, fw_dnld.offset starts from binary_config->offset in
process_state_open_lc() and grows by chunk_len on every data credit. It is
never compared against fw->size.
Can a controller make the driver read past the end of fw->data and send that
memory to the device?
nfcmrvl_fw_dnld_start() also casts fw->data to struct nfcmrvl_fw and reads
magic, phy, helper and firmware without checking that fw->size is at least
sizeof(struct nfcmrvl_fw). Would a truncated firmware file cause an
out-of-bounds read there?
[ ... ]
> void nfcmrvl_fw_dnld_recv_frame(struct nfcmrvl_private *priv,
> struct sk_buff *skb)
> {
> + unsigned long flags;
> +
> + spin_lock_irqsave(&priv->fw_dnld.rx_lock, flags);
> + if (priv->fw_dnld.stopping || !priv->fw_dnld.active) {
> + spin_unlock_irqrestore(&priv->fw_dnld.rx_lock, flags);
> + kfree_skb(skb);
> + return;
> + }
> + priv->fw_dnld.rx_active++;
> + spin_unlock_irqrestore(&priv->fw_dnld.rx_lock, flags);
[Severity: High]
This is a pre-existing issue, but does this gate cover receive entries that
come from the I2C and SPI interrupt threads after teardown?
Both PHYs request the NFCC interrupt with devm_request_threaded_irq(). The
IRQ stays requested and enabled until devres runs after remove() returns.
nfcmrvl_i2c_remove() and nfcmrvl_spi_remove() only call
nfcmrvl_nci_unregister_dev(), which does:
nci_unregister_device(ndev);
nfcmrvl_fw_dnld_deinit(priv);
nci_free_device(ndev);
kfree(priv);
Nothing disables or frees the IRQ first, and drv_data->priv is never
cleared. So this guard in nfcmrvl_i2c_int_irq_thread_fn() still passes:
if (!drv_data->priv)
return IRQ_HANDLED;
if (test_bit(NFCMRVL_PHY_ERROR, &drv_data->priv->flags))
Suppose the controller raises an interrupt after kfree(priv). Can the
thread handler then read the freed priv and ndev through nci_skb_alloc()
and nfcmrvl_nci_recv_frame(), and possibly enter
nfcmrvl_fw_dnld_recv_frame()?
The SPI thread handler calls nfcmrvl_nci_recv_frame(drv_data->priv, ...) in
the same way.
The stopping and rx_active state lives inside the freed priv, so it can't
reject entries that begin after the free. No patch in this series quiesces
the IRQ either.
[ ... ]
> @@ -497,6 +582,13 @@ int nfcmrvl_fw_dnld_start(struct nci_dev *ndev, const char *firmware_name)
> if (!firmware_name || !firmware_name[0])
> return -EINVAL;
>
> + spin_lock_irqsave(&fw_dnld->rx_lock, flags);
> + if (fw_dnld->stopping) {
> + spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
> + return -ENODEV;
> + }
> + spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
> +
> strscpy(fw_dnld->name, firmware_name);
[Severity: High]
Should this also reject a start while fw_dnld->active is set, or while a
fw_dnld_over() is still running?
nfc_fw_download() in net/nfc/core.c only rejects shutting_down and dev_up.
It does not check fw_download_in_progress, unlike nfc_dev_up().
So a second NFC_CMD_FW_DOWNLOAD during an active download reaches this
point. It then overwrites fw_dnld->name, fw, header, binary_config and
state while rx_work, timeout_work or abort still own them.
For example, take download A, active with its timer armed. A second request
names an existing file with the wrong magic or phy:
nfcmrvl_fw_dnld_start()
request_firmware(&fw_dnld->fw, ...) <- A's firmware leaks
release_firmware(fw_dnld->fw);
fw_dnld->header = NULL;
return -EINVAL; <- fw_dnld->fw left dangling
nfc_fw_download()
dev->fw_download_in_progress = false;
fw_dnld->active is still true, so when A's timer fires:
fw_dnld_timeout_work()
fw_dnld_over()
release_firmware(fw_dnld->fw); <- second release
Can this double free the struct firmware?
If a queued frame is processed before the timer fires,
process_state_init() would dereference the NULL header. Alternatively,
process_state_fw_dnld() would read the freed fw->data.
If the second request names a file that doesn't exist, request_firmware()
sets fw_dnld->fw to NULL. Would A's state machine then dereference
fw_dnld->fw->data with fw_dnld->fw NULL?
There is also a window in fw_dnld_over() itself:
fw_dnld->active = false;
spin_unlock_irqrestore(&fw_dnld->rx_lock, flags);
strscpy(name, fw_dnld->name);
wait_event(fw_dnld->rx_wait, !READ_ONCE(fw_dnld->rx_active));
timer_delete_sync(&fw_dnld->timer);
...
nci_cancel_pending_cmds(priv->ndev);
skb_queue_purge(&fw_dnld->rx_q);
if (fw_dnld->fw) {
release_firmware(fw_dnld->fw);
fw_dnld_over() runs from the work items and from abort without
device_lock. If start() runs during these sleeping steps, could
fw_dnld_over() release the new download's firmware, purge its rx_q,
delete its timer and clear fw_download_in_progress?
The commit message says firmware state is released "exactly once". Does
that hold while start() does not check the new active flag?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130130240.65411-1-shubham%40octane.security
next prev parent reply other threads:[~2026-12-01 14:04 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 13:02 [PATCH net v9 0/3] nfc: nfcmrvl: split firmware teardown fixes Shubham Antil
2026-11-30 13:02 ` [PATCH net v9 1/3] nfc: nci: add a pre-unregister shutdown stage Shubham Antil
2026-12-01 14:04 ` netdev-bot+sashiko
2026-11-30 13:02 ` [PATCH net v9 2/3] nfc: nfcmrvl: serialize firmware completion and teardown Shubham Antil
2026-12-01 14:04 ` netdev-bot+sashiko [this message]
2026-11-30 13:02 ` [PATCH net v9 3/3] nfc: nfcmrvl: abort 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=179613388583.3146.2330494470837938376@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=david@ixit.cz \
--cc=gio@octane.security \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=oe-linux-nfc@lists.linux.dev \
--cc=shubham@octane.security \
/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