From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796114954; b=OmSYGCXSFEVTabm0IwvkLk5r86v7+jWexYmTX7/mkhA9uvpjGOdUW+Z4LhfFQ1p3I2E/ bUhz4uj0LpgPXgpxroHnomqdBVNV5kZY6qMRpPho/u0QmQVmkfY32wRH3uDnQk5Oppkba JwYWrXLn5isVYyOv64zJYK2vquwtS3nxkhl4ltqRoDyZSaM0JF8LP8/QkglJP6/X7C5qU JFvP5qjfDFmgYCoAC0XkbIx774OMZ3Cj8L9FQP8+pieliQhsdLIPpDlQk6IAC/BxOCSX9 11H/rUrOaJ/Y/4CLD9Kob65P6ANID1ZWnJN0ifa9Cf4YroUTxqFbEXdznLmFuXjKc3w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796114954; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=XoLsn9mr51zmApEqIUdh+GIG3EfXWYPcTTFmile6aLk=; b=fv4mHz9yXjZf1k0hZPjlRnA3z/nnGb7M0DjfFfaeG+rYtKsZSA5tT6CMXhlwcxNS19oy DxRTifijYTTRbPX55hiCp/sANMWStH+z+QV2jqnQugCWyRIV7HhyKX83DIbAAtbMV1CJu Nb1sAygajK+TDZ/U+q1J3yBbiJTOBPey55usK5nBadHR+6UtweUWQURBXI3GCiM6FyxH0 HCXNr+xgsAUe/pgl3A04NZ76+gTAUrenB5pbYT+FsNY2iormJtPR24xS9JZhtzJ5sszYM h6RUztFm6+JqwUfNa2JhJjovomIHVny1uSyGgZtwfAP7Lefx/LRrVbvlvrUka0x5DTw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="gVX/GYWS"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AAC73606A330; Tue, 1 Dec 2026 08:49:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=gVX/GYWS Received: by smtp.kernel.org (Postfix) with ESMTPSA id EAE411F00893; Tue, 1 Dec 2026 08:49:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796114954; bh=XoLsn9mr51zmApEqIUdh+GIG3EfXWYPcTTFmile6aLk=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=gVX/GYWSSdWfWOnRC7OgAv8e+4bL++yr9LsJD4pnMcXR4noDYUNiw3/VGtVDeXrJ5 L3w3LYS16ngLhoXBC2Cf+EE8ge23p2eAP+b1wLPem/eKDx5MWB18uN3AncKcvwURzA wqhsFVhzllbjIFy2gemlpA8btyl+g6AX2gTWJnSc4D+JBA/iCtJWX5bMRU7vNdiEdp gCY2vXupt4PuzE/rIOz4djv5mZxt8uaoDxd2/wj81xYIBWf6VHuzrTbqA0aUE6CTyz L8+ZqPX7bcllWBem1bUmXOYNQk2+9z+UBwD42qqetsQP8XpKRiAHIsosqMfecVBdRa tnj0Aa59UrWgQ== Subject: Re: [PATCH net v9 2/2] nfc: reject overlapping firmware downloads 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 Date: Tue, 01 Dec 2026 08:49:13 +0000 Message-ID: <179611495350.3146.8701287594086522@kernel.org> In-Reply-To: <20261130080645.60464-3-shubham@octane.security> References: <20261130080645.60464-3-shubham@octane.security> X-sashiko-severity: Medium Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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