Linux kernel -stable discussions
 help / color / mirror / Atom feed
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,
	stable@vger.kernel.org, kuba@kernel.org
Subject: Re: [PATCH net v9 2/2] nfc: reject overlapping firmware downloads
Date: Tue, 01 Dec 2026 08:49:13 +0000	[thread overview]
Message-ID: <179611495350.3146.8701287594086522@kernel.org> (raw)
In-Reply-To: <20261130080645.60464-3-shubham@octane.security>

Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 3 (1) · Low: 2 (1)

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 new -EBUSY check in nfc_fw_download()
  (net/nfc/core.c:51-54) removes the only userspace way to recover a…
- [Medium] The new check only serializes writers that go through
  nfc_fw_download() under device_lock.
- [Medium] The series' earlier commit 'nfc: annotate firmware download
  state accesses' says it marks 'every lockless access with READ_ONCE()…
- [Low] The commit message understates what this patch prevents.
- [Low] The patch turns fw_download_in_progress into a lockless handoff
  of the driver's firmware-download state, from the completion context…

Pre-existing issues:
- [Medium] The kernel-doc of nfc_fw_download_done() (net/nfc/core.c:76)
  says @result is 'The positive value of a standard errno value'.
- [Low] For the same 'firmware download not supported' condition,
  nfc_fw_download() returns -EOPNOTSUPP when dev->ops->fw_download is…

--

nfc: reject overlapping firmware downloads

With this commit, nfc_fw_download() returns -EBUSY when
fw_download_in_progress is already set. A second NFC_CMD_FW_DOWNLOAD can
then no longer clear the flag of a running download. The check is done
under the NFC device lock, and the completion path stays lockless.

> If the driver rejects the second request, the core clears
> fw_download_in_progress and makes the first operation appear complete.

[ ... ]

> No production incident or pre-fix kernel warning was observed.

[Severity: Low]
Is this description complete? In some drivers the overlap looks like a
memory-safety problem rather than a flag-accounting problem.

In nfcmrvl_fw_dnld_start() in drivers/nfc/nfcmrvl/fw_dnld.c, a second
request calls request_firmware(&fw_dnld->fw, ...). That overwrites, and
leaks, the firmware of the running download. If the new image has a bad
magic, the error path frees it but leaves the pointer set:

	release_firmware(fw_dnld->fw);
	fw_dnld->header = NULL;
	return -EINVAL;

When the first download ends through fw_dnld_timeout() or rx_work,
fw_dnld_over() frees the same pointer again:

	if (priv->fw_dnld.fw) {
		release_firmware(priv->fw_dnld.fw);

Queued rx work can also read header->ref_clock (now NULL), or
priv->fw_dnld.fw->data (freed) in process_state_fw_dnld().

If the second image is valid, timer_setup() and mod_timer() run on
fw_dnld.timer while it is still queued from the first download.

In drivers/nfc/nxp-nci/firmware.c, nxp_nci_fw_download() overwrites
fw_info->fw, fw_info->data and fw_info->size while fw_info->work may
still be using them. It also leaks the first firmware.

The new check in nfc_fw_download() blocks all of these on the core path.
Could the commit message describe them?

> diff --git a/net/nfc/core.c b/net/nfc/core.c
> index 91b3274dad41a..b03b34b40e1c9 100644
> --- a/net/nfc/core.c
> +++ b/net/nfc/core.c
> @@ -48,6 +48,11 @@ 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;
> +	}

[Severity: Medium]
Can this lock out a pn544 i2c device for good if its firmware download
never completes?

pn544_hci_i2c_fw_download() always accepts the request, so the core
leaves fw_download_in_progress set:

	phy->fw_work_state = FW_WORK_STATE_START;

	schedule_work(&phy->fw_work);

	return 0;

pn544_hci_i2c_fw_work() then waits in one of these states:

	FW_WORK_STATE_WAIT_WRITE_ANSWER
	FW_WORK_STATE_WAIT_CHECK_ANSWER
	FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER

Only pn544_hci_i2c_irq_thread_fn() moves it forward, and
drivers/nfc/pn544/ has no timer or timeout. If the controller never
raises the answer IRQ (wedged bootloader, lost interrupt, I2C fault),
pn544_hci_i2c_fw_work_complete() and nfc_fw_download_done() never run.

Before this patch, a retried NFC_CMD_FW_DOWNLOAD reached
pn544_hci_i2c_fw_download(). That reset the state to FW_WORK_STATE_START
and restarted the download. The retry was racy and leaked phy->fw, but
the device recovered.

With this check every retry gets -EBUSY. nfc_dev_up() also returns
-EBUSY:

	if (READ_ONCE(dev->fw_download_in_progress)) {
		rc = -EBUSY;
		goto error;
	}

The only remaining way to clear the flag seems to be unbinding the
driver, through pn544_hci_i2c_remove():

	if (phy->fw_work_state != FW_WORK_STATE_IDLE)
		pn544_hci_i2c_fw_work_complete(phy, -ENODEV);

st-nci can set the same flag through vendor FWUPD_START, but vendor
DM_LOAD can still clear it there. So the lockout applies to pn544.

The missing pn544 timeout predates this patch, but the new check turns a
hang that could be retried into a lockout. Should the commit message
mention this change? It should also state the new rule that an accepted
->fw_download() must always end in nfc_fw_download_done().

[Severity: Medium]
Does this check cover every writer of fw_download_in_progress? st-nci also
writes the flag from the NFC_CMD_VENDOR path, and nfc_genl_vendor_cmd()
calls cmd->doit() without device_lock:

	dev->cur_cmd_info = info;
	err = cmd->doit(dev, data, data_len);

st_nci_hci_dm_fwupd_start() in drivers/nfc/st-nci/vendor_cmds.c uses the
same set-then-clear-on-error pattern that this patch removes from the
core:

	dev->fw_download_in_progress = true;
	r = nci_hci_send_cmd(ndev, ST_NCI_DEVICE_MGNT_GATE,
			ST_NCI_HCI_DM_FWUPD_START, data, data_len, NULL);
	if (r)
		dev->fw_download_in_progress = false;

st_nci_hci_dm_direct_load() also checks and clears the flag without a
lock.

With two overlapping vendor FWUPD_START requests, the second can still
clear the flag while the first update is running.

Also, st_nci_ops has no fw_download, so nci_fw_download() returns
-ENOTSUPP after the core has already set the flag. Consider:

CPU0 (NFC_CMD_FW_DOWNLOAD)           CPU1 (vendor FWUPD_START, no lock)
nfc_fw_download()
  READ_ONCE() sees false
                                     st_nci_hci_dm_fwupd_start()
                                       fw_download_in_progress = true
  WRITE_ONCE(..., true)
  nci_fw_download() returns -ENOTSUPP
  WRITE_ONCE(..., false)

The vendor update then looks finished and DM_LOAD returns -EPROTO.
nfc_dev_up() can also bring the controller up in the middle of a
firmware update.

Should these writers be covered too, or at least mentioned in the commit
message?

[Severity: Medium]
This check relies on the marked accesses from the earlier commit "nfc:
annotate firmware download state accesses". That commit says it marks
"every lockless access with READ_ONCE() or WRITE_ONCE()". Are some
accesses still unmarked?

At this commit these plain accesses remain:

drivers/nfc/st-nci/vendor_cmds.c:st_nci_hci_dm_fwupd_start()
	dev->fw_download_in_progress = true;
	...
		dev->fw_download_in_progress = false;

drivers/nfc/st-nci/vendor_cmds.c:st_nci_hci_dm_direct_load()
	if (dev->fw_download_in_progress) {
		dev->fw_download_in_progress = false;

drivers/nfc/nfcmrvl/main.c:nfcmrvl_nci_unregister_dev()
	if (priv->ndev->nfc_dev->fw_download_in_progress)
		nfcmrvl_fw_dnld_abort(priv);

The st-nci accesses run unlocked from nfc_genl_vendor_cmd(). The nfcmrvl
read can race with nfc_fw_download_done(), called from the fw_dnld timer
or from rx_work. The same nfcmrvl file already converted the read in
nfcmrvl_nci_recv_frame().

Won't KCSAN flag these against the READ_ONCE() and WRITE_ONCE() calls in
nfc_fw_download(), nfc_dev_up() and nfc_fw_download_done()? The KCSAN run
mentioned in the series used a pty-backed controller, which doesn't
exercise st-nci or nfcmrvl unregister.

[Severity: Low]
Is READ_ONCE() strong enough here? The flag now hands the download state
from the completion context, which takes no device_lock, to the next
nfc_fw_download() caller. Neither WRITE_ONCE() in nfc_fw_download_done()
nor READ_ONCE() here provides release or acquire ordering.

In pn544_hci_i2c_fw_work_complete(), nothing orders the teardown stores
before the flag store:

	phy->fw_work_state = FW_WORK_STATE_IDLE;

	if (phy->fw) {
		release_firmware(phy->fw);
		phy->fw = NULL;
	}

	nfc_fw_download_done(phy->hdev->ndev, phy->firmware_name, (u32) -result);

Under the LKMM, the starter can see the flag as false and write
fw_work_state = FW_WORK_STATE_START, and the stale IDLE store can still
land after it. The re-run fw_work would then see IDLE and do nothing,
and the flag would stay set.

The window is very small in practice. Would smp_store_release() in
nfc_fw_download_done(), paired with smp_load_acquire() here, be the right
primitives for this handoff?

> +
>  	if (!dev->ops->fw_download) {
>  		rc = -EOPNOTSUPP;
>  		goto error;

[Severity: Low]
This isn't a bug introduced by this patch, but the same "firmware
download not supported" condition gives different errnos. The core
returns -EOPNOTSUPP here, while nci_fw_download() and hci_fw_download()
return the kernel-internal -ENOTSUPP:

net/nfc/nci/core.c:nci_fw_download()
	if (!ndev->ops->fw_download)
		return -ENOTSUPP;

net/nfc/hci/core.c:hci_fw_download()
	if (!hdev->ops->fw_download)
		return -ENOTSUPP;

nci_nfc_ops and hci_nfc_ops always set .fw_download, so the check above
never fires for NCI and HCI devices. nfc_genl_fw_download() then passes
errno 524 to userspace.

Should these return -EOPNOTSUPP as well?

[Severity: Medium]
This is a pre-existing issue, but the status nfcmrvl reports through
nfc_fw_download_done() doesn't match the kernel-doc:

 * @result: The positive value of a standard errno value

nxp-nci and pn544 pass (u32)-result. nfcmrvl's fw_dnld_over() passes its
error argument through unchanged, and its callers pass negative errnos:

	fw_dnld_over(priv, -ETIMEDOUT);

nfcmrvl_fw_dnld_abort() (-EHOSTDOWN) and fw_dnld_rx_work() do the same.

nfc_genl_fw_download_done() writes this value to
NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS with nla_put_u32(). On a timeout,
userspace sees 4294967186 instead of 110.

Should fw_dnld_over() negate the value before reporting it?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130080645.60464-1-shubham%40octane.security

      reply	other threads:[~2026-12-01  8:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261130080645.60464-1-shubham@octane.security>
2026-11-30  8:06 ` [PATCH net v9 1/2] nfc: annotate firmware download state accesses Shubham Antil
2026-12-01  8:49   ` netdev-bot+sashiko
2026-11-30  8:06 ` [PATCH net v9 2/2] nfc: reject overlapping firmware downloads Shubham Antil
2026-12-01  8:49   ` netdev-bot+sashiko [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=179611495350.3146.8701287594086522@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 \
    --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