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=1796057081; b=BJZSfeuB/S2Jwui53YChjrDjfgb2mE2Fom/akpwtAcsF3XC8yfh+XSs6sv/qzXaJS1YU aooX/TBT2EmDZLuXiY+IZ+G6ZPFl+QLbt8XveOK7nCJIuisQbnqviZux2d5gJ2tDaRzG1 o2ButHe3GKz9IquTJx3XcwtrQ9WgzJzcr/1pqL0WOQyJDjZ8q/GHbeHfDT4MvPvRN2SY0 gZjWUxzYmnhKJ+RP9oz8mILGXXunURnra/kIdZ7ZvHEAZqYuBcuNFbu0672l/BjfcJNG4 bFA2cb0WbtzzKypIZpmXMwk9Ns4lNNDoDWfcJz+mFLIQO4CcA2Q6UXE9ap5ns56X8GA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796057081; 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=OXelfkeb7imgcd4MqjJFHbRceIkgIrahFlvKtpOOS28=; b=oaXUV+C1pzKQYFqnsxZZPQ54wZ6W6qVtmL2Q70Uopm0Ewn6H1NdDEWqxVfEqJr/XDLTI oU+d8o3cC3UClMVktAb8PBtrSk0bRys2d2maqutaf7pH/hEpCJXeDYuesIlwRa38GGMai oTbk5U+ifRk7QYzwsCJKVf1g5lArF9x2f2yPKQQYbiZZcQq2//eL1sPbDg6qnQN/pYs1/ oIkEmwdJAvpGJ0HfV27YDpXp0FA73uK3HGFuswDDbwE4dTrjANheAuzA50EunLqHjqWrK 3ZW+MUYVDALaxCGvPX1HtRCXgrmMz6JSM9HXUt0TeleXcv2E3e4R8lS+16W+n9yzbHQ== 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=lLgYsRSZ; 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 B457A606A31C; Mon, 30 Nov 2026 16:44:41 +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=lLgYsRSZ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4D4121F0092F; Mon, 30 Nov 2026 16:44:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796057080; bh=OXelfkeb7imgcd4MqjJFHbRceIkgIrahFlvKtpOOS28=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=lLgYsRSZlNh95Q8qK/CAK9eow4xMJk5m16p8oKgKH4rIaIfL34JxM/gzyXzhY6kLs LKzj8uzvKxbRUJd+ZD6l9No1LQ8WRcNi4Si8Ahw1vbCcwy48bp5/3zOc9wLFoQYLuY R19eMq7PdvND9go/AZ2dBUvfu5uZUiSiPS3S1Gq9MP6pd9M7TK8/yoooMplG3xJmEI UbkxMkwhJiZ4n7N9i5wBcBrgoZ50d0ZudLRcY30P5+byd1ibYaXqTV98XE8Kv72Ltp jhomM7ghjAfRksDP7seNXVQWhemdSq/uT3WBREsA2u8P5tyRRgNVTDzEDlD3HM/yX0 XTTFv9fqB5P1Q== Subject: Re: [PATCH net v4 2/2] nfc: st21nfca: prevent double completion of SE APDU request From: netdev-bot+sashiko@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, horms@kernel.org, kuba@kernel.org Date: Mon, 30 Nov 2026 16:44:39 +0000 Message-ID: <179605707981.3146.15516824656082527187@kernel.org> In-Reply-To: <20261129-st21nfca-v2-v4-2-7d18b104ad46@gmail.com> References: <20261129-st21nfca-v2-v4-2-7d18b104ad46@gmail.com> X-sashiko-severity: Medium Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: linux-kernel@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 3 potential issue(s) to consider. Critical: 0 · High: 0 (2) · Medium: 3 (2) · 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). - [Medium] The commit message says a response arriving after the timeout gets "no callback". - [Medium] nfc: st21nfca: race on bwi_active and bwi_timer across SE APDU paths - [Medium] nfc: st21nfca: missing rollback on send failure in st21nfca_hci_se_io() Pre-existing issues: - [High] The double completion that this patch fixes in st21nfca is still present, unchanged, in the sibling ST driver drivers/nfc/st-nci/se.c. - [High] bwi_timer can still be armed after the driver is removed, which leads to a use-after-free on `info`. - [Medium] st21nfca_apdu_reader_event_received() leaks the callback context when sending EVT_SE_END_OF_APDU_TRANSFER fails. - [Medium] A stale timeout work item can complete a newly submitted request. -- nfc: st21nfca: prevent double completion of SE APDU request This patch checks bwi_active under se_lock in both the timeout work and the EVT_TRANSMIT_DATA handler, so an SE APDU request is completed at most once. It also makes st21nfca_hci_se_io() reject a new request with -EBUSY while one is pending, and frees cb_context in that case. > diff --git a/drivers/nfc/st21nfca/se.c b/drivers/nfc/st21nfca/se.c > index 3726b00b7580c..33882f8739099 100644 > --- a/drivers/nfc/st21nfca/se.c > +++ b/drivers/nfc/st21nfca/se.c [ ... ] > @@ -269,6 +275,10 @@ static void st21nfca_se_wt_work(struct work_struct *work) > se_info.timeout_work); > > spin_lock(&info->se_info.se_lock); > + if (!info->se_info.bwi_active) { > + spin_unlock(&info->se_info.se_lock); > + return; > + } > info->se_info.bwi_active = false; > cb = info->se_info.cb; > cb_context = info->se_info.cb_context; [Severity: Medium] This is a pre-existing issue, but can a stale timeout_work still complete a request that was just submitted? The new check only tests whether some request is pending. bwi_timer can be armed while nothing is pending in two ways: - the ST21NFCA_EVT_WTX_REQUEST case in st21nfca_apdu_reader_event_received() calls mod_timer() without looking at bwi_active. - st21nfca_hci_se_io() calls mod_timer() after dropping se_lock. Suppose that timer fires and st21nfca_se_wt_timeout() queues timeout_work. Then a new request is accepted before the work runs: st21nfca_hci_se_io() info->se_info.bwi_active = true; /* the queued timeout_work is not cancelled */ st21nfca_se_wt_work() if (!info->se_info.bwi_active) /* passes for the new request */ ... cb(cb_context, NULL, 0, -ETIME); The new request then completes with -ETIME before its own timeout has elapsed. The SE also gets a soft or hard reset and xch_error is toggled. The real response is dropped later, because active reads false. The pre-patch code had the same window with no guard at all. The new guard does close the case where a WTX arrives while idle and no new request follows. [ ... ] > @@ -395,6 +406,7 @@ int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev, > cancel_work_sync(&info->se_info.timeout_work); > > spin_lock(&info->se_info.se_lock); > + active = info->se_info.bwi_active; > info->se_info.bwi_active = false; > cb = info->se_info.cb; > cb_context = info->se_info.cb_context; > @@ -405,7 +417,9 @@ int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev, > if (r < 0) > goto exit; [Severity: Medium] What happens to the request state when nfc_hci_send_event() below fails? bwi_active stays true, bwi_timer stays armed and cb/cb_context stay installed. The error goes back to nfc_se_io(), which does not free cb_context once the driver has been called. nfc_hci_send_event() can fail synchronously with -EADDRNOTAVAIL when gate2pipe[ST21NFCA_APDU_READER_GATE] is NFC_HCI_INVALID_PIPE, with -ENOMEM from nfc_hci_hcp_message_tx(), or with -ESHUTDOWN when hdev->shutting_down is set. Before this patch the next request simply overwrote the stale state. With the new bwi_active check, every NFC_CMD_SE_IO request now fails with -EBUSY until bwi_timer expires. st21nfca_se_wt_work() then sends a soft or hard reset to an SE that never received the APDU, toggles xch_error, and calls cb(cb_context, NULL, 0, -ETIME) for a request that userspace was already told had failed. If st21nfca_se_deinit() runs before the timer expires, the callback is never called and cb_context is leaked. Should the send failure path roll this back? The timer may already have fired and queued timeout_work by then, so it looks like cb_context can only be freed here if this path is the one that clears bwi_active under se_lock. Is that what you had in mind, or is there a reason to leave this to the timeout? [Severity: Medium] Can this check tell a late response for a timed-out request apart from a new request that was accepted in the meantime? bwi_active is a single bit with no request identity, and both st21nfca_se_wt_work() and st21nfca_hci_se_io() drop se_lock before the timer and the SE are touched. Suppose request A times out while its EVT_TRANSMIT_DATA is in flight, or while this handler is waiting in cancel_work_sync(): st21nfca_se_wt_work() bwi_active = false; spin_unlock(&se_lock); cb(ctxA, NULL, 0, -ETIME); st21nfca_hci_se_io() /* request B, not -EBUSY */ cb_context = ctxB; bwi_active = true; spin_unlock(&se_lock); mod_timer(&bwi_timer, ...); st21nfca_apdu_reader_event_received() /* late response for A */ timer_delete_sync(&bwi_timer); active = bwi_active; /* true, but that is B */ bwi_active = false; cb(ctxB, skb->data, skb->len, 0); ctxB is completed and freed with A's data while B is still outstanding on the SE, and B's real response is dropped later. Because mod_timer() in st21nfca_hci_se_io() runs after spin_unlock(), the late response can also land between the two. Then timer_delete_sync() here has nothing to delete, bwi_active is cleared, and mod_timer() arms bwi_timer afterwards with bwi_active == false. The new check in st21nfca_se_wt_work() makes that timer harmless while the device is up, but st21nfca_se_deinit() only deletes bwi_timer when bwi_active is true. If the device is removed before the timer expires, st21nfca_se_wt_timeout() runs after kfree(info) and calls schedule_work() on freed memory. Would it make sense to arm bwi_timer under se_lock, and to delete the timer in st21nfca_se_deinit() unconditionally? The misattribution itself seems to need some notion of which request a response belongs to, or refusing new requests until the previous exchange has really finished. How do you want to handle that? [Severity: Medium] This isn't a bug introduced by this patch, but does this error path leak cb_context? At this point the request has already been claimed under se_lock. bwi_active is false, and cb/cb_context exist only in locals. nfc_hci_send_event() can fail here in three ways: - -EADDRNOTAVAIL for an invalid DEVICE_MGNT pipe - -ENOMEM from nfc_hci_hcp_message_tx() - -ESHUTDOWN when hdev->shutting_down is set In each case the exit label only frees the skb. nfc_se_io() in net/nfc/netlink.c frees cb_context only on its own error paths, before the driver is called. So the struct se_io_ctx is never freed, and userspace never gets a completion for the request. The pre-patch code also cleared bwi_active and skipped cb on this path. > > - cb(cb_context, skb->data, skb->len, 0); > + /* The request may have been completed by the timeout already */ > + if (active) > + cb(cb_context, skb->data, skb->len, 0); [Severity: Medium] The commit message says: A response that arrives after the timeout still gets its EVT_SE_END_OF_APDU_TRANSFER, as before, but no callback. Does that still hold if a new request was accepted in the meantime? active only says that some request is pending. It does not say that this response belongs to that request. Consider: st21nfca_se_wt_work() bwi_active = false; cb(ctxA, NULL, 0, -ETIME); st21nfca_hci_se_io() /* request B passes -EBUSY check */ cb_context = ctxB; bwi_active = true; mod_timer(&bwi_timer, ...); st21nfca_apdu_reader_event_received() /* late response for A */ timer_delete_sync(&bwi_timer); /* stops B's watchdog */ active = bwi_active; /* true, but that is B */ cb(ctxB, skb->data, skb->len, 0); /* A's data given to B */ When B's real response arrives, active is false and the response is silently dropped. If it never arrives, nothing times B out, because B's timer was deleted. Each context is still completed at most once, so the double free is fixed, and this misattribution predates the patch. But bwi_active is a single bit with no request identity. The statement in the commit message only holds when no new request is submitted before the late response. There is also a smaller variant. st21nfca_se_wt_work() clears bwi_active and drops se_lock before it queues the soft or hard reset. A new APDU accepted in that window can be queued ahead of the recovery reset meant for the previous exchange. [Severity: High] This is a pre-existing issue and was not introduced by this patch. The same double completion still seems to be present in the sibling driver drivers/nfc/st-nci/se.c, which the commit message doesn't mention. st_nci_se_wt_timeout() completes the request without checking bwi_active: drivers/nfc/st-nci/se.c:st_nci_se_wt_timeout() { info->se_info.bwi_active = false; ... info->se_info.cb(info->se_info.cb_context, NULL, 0, -ETIME); } A late ST_NCI_EVT_TRANSMIT_DATA then calls the callback again, with no check: drivers/nfc/st-nci/se.c:st_nci_hci_apdu_reader_event_received() { ... case ST_NCI_EVT_TRANSMIT_DATA: timer_delete_sync(&info->se_info.bwi_timer); info->se_info.bwi_active = false; info->se_info.cb(info->se_info.cb_context, skb->data, skb->len, 0); ... } se_io_cb() in net/nfc/netlink.c does kfree(ctx). Wouldn't the second call read ctx->dev_idx and ctx->se_idx from freed memory and then free ctx a second time? Two related problems exist in st-nci: - The ST_NCI_EVT_WTX_REQUEST case re-arms bwi_timer without checking that a request is pending. The timer can then call the stale cb/cb_context again. - st_nci_se_io() overwrites cb/cb_context of a pending request with no busy check. This patch now rejects that case with -EBUSY in st21nfca_hci_se_io(). > break; > case ST21NFCA_EVT_WTX_REQUEST: > mod_timer(&info->se_info.bwi_timer, jiffies + [Severity: High] This is a pre-existing issue, but can bwi_timer still be armed when info is freed during removal? This WTX case arms bwi_timer without checking or setting bwi_active. A WTX from the device while no APDU is pending leaves the timer armed with bwi_active == false. st21nfca_se_deinit() then skips the timer deletion: drivers/nfc/st21nfca/se.c:st21nfca_se_deinit() { spin_lock(&info->se_info.se_lock); bwi_active = info->se_info.bwi_active; spin_unlock(&info->se_info.se_lock); if (bwi_active) timer_delete_sync(&info->se_info.bwi_timer); ... cancel_work_sync(&info->se_info.timeout_work); ... } st21nfca_hci_remove() also calls st21nfca_se_deinit() before nfc_hci_unregister_device(). A concurrent st21nfca_hci_se_io() or another WTX event can therefore arm the timer after deinit has sampled the flag: st21nfca_hci_remove() st21nfca_se_deinit(hdev); nfc_hci_unregister_device(hdev); nfc_hci_free_device(hdev); kfree(info); When the timer expires, st21nfca_se_wt_timeout() runs on the freed info and calls schedule_work() on a freed work_struct. st21nfca_se_wt_work() can then read the freed bwi_active and call through a freed cb pointer. The new bwi_active check in st21nfca_se_wt_work() reads from the freed object, so it doesn't help here. The previous commit in this series, "nfc: st21nfca: add spinlock to serialize SE APDU request state", notes that the teardown ordering "need[s] a separate fix". No later patch in the series addresses it. -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261129-st21nfca-v2-v4-0-7d18b104ad46%40gmail.com