Skip to contentSkip to conversation

net/nfc: fix skb ownership on data exchange errors

PATCH Superseded Bin Luo 3 participants 4 messages Nov 18 – Nov 22

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Bin Luo

author
#0 lore raw

PATCH v2 1/1 net/nfc: fix skb ownership on data exchange errors

NFC data exchange callbacks may receive a non-NULL response skb together with an error. The ownership of the skb is not documented, causing the rawsock and LLCP error paths to return without freeing it. Digital DEP also frees the response after invoking the callback, which conflicts with callback ownership.

Document that the callback takes ownership of a non-NULL skb regardless of err. Free it on the rawsock and LLCP error paths, clear the Digital DEP response pointer after the callback, and release the skb when the NCI loopback connection lookup fails.

---
Built on Linux v7.4-rc1 commit 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4 with NFC_VIRTUAL_NCI, KASAN, and kmemleak enabled. A QEMU TCG guest ran 32 NCI 1.0 MIFARE exchanges with a nonzero data status. The root harness provisioned the virtual controller and polling session; each rawsock exchange ran with effective UID 65534. The baseline reported 48 kmemleak suspects; the fixed run reported no unreferenced objects and no KASAN report.

The runtime test covers NCI-to-rawsock only. It did not trigger the LLCP, Digital DEP, or NCI loopback connection-lookup failure paths.

---
 include/net/nfc/nfc.h | 2 ++
 net/nfc/digital_dep.c | 1 +
 net/nfc/llcp_core.c   | 2 ++
 net/nfc/nci/core.c    | 1 +
 net/nfc/rawsock.c     | 5 ++++-
 5 files changed, 10 insertions(+), 1 deletion(-)
include/net/nfc/nfc.h +2 -0
index bcafab5c5..d3c2d6735 100644
@@ -37,6 +37,8 @@ struct nfc_dev;
  *
  * When a rx or tx package is lost or corrupted or the target gets out
  * of the operating field, err is -EIO.
+ * If skb is non-NULL, ownership is transferred to the callback regardless
+ * of err. Providers must not access or free skb after invoking the callback.
  */
 typedef void (*data_exchange_cb_t)(void *context, struct sk_buff *skb,
 								int err);
net/nfc/digital_dep.c +1 -0
index 968547c30..c7f5378cd 100644
@@ -882,6 +882,7 @@ static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
 
 exit:
 	data_exch->cb(data_exch->cb_context, resp, rc);
+	resp = NULL;
 
 error:
 	kfree(data_exch);
net/nfc/llcp_core.c +2 -0
index 74bf81700..3619e12b3 100644
@@ -1638,6 +1638,8 @@ void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
 
 	if (err < 0) {
 		pr_err("LLCP PDU receive err %d\n", err);
+		if (skb)
+			kfree_skb(skb);
 		return;
 	}
 
net/nfc/nci/core.c +1 -0
index 73e3a9647..678f744a2 100644
@@ -413,6 +413,7 @@ static void nci_nfcc_loopback_cb(void *context, struct sk_buff *skb, int err)
 
 	conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
 	if (!conn_info) {
+		kfree_skb(skb);
 		nci_req_complete(ndev, NCI_STATUS_REJECTED);
 		return;
 	}
net/nfc/rawsock.c +4 -1
index f7d7a599f..3bf65b1fb 100644
@@ -156,8 +156,11 @@ static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb,
 
 	pr_debug("sk=%p err=%d\n", sk, err);
 
-	if (err)
+	if (err) {
+		if (skb)
+			kfree_skb(skb);
 		goto error;
+	}
 
 	err = rawsock_add_header(skb);
 	if (err)
Signature · 1 line
2.51.0.windows.1
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.

Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
author
#2 lore raw

Hi,

The issue was first identified through manual code inspection during our NFC callback-ownership audit. I traced the NCI data-exchange completion path into rawsock and found that a non-NULL skb could bypass cleanup on the error path. I then reproduced the behavior with a QEMU NFC_VIRTUAL_NCI test.

Best regards,
Bin Luo

#3 lore raw
In reply to Bin Luo
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

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

Critical: 0 · High: 1 · Medium: 0 · Low: 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).

- [High] The fix is incomplete.
- [Low] The commit fixes a user-reachable skb leak in rawsock_data_exchange_complete() (net/nfc/rawsock.c) and changes…

--

net/nfc: fix skb ownership on data exchange errors

The commit documents that a data_exchange_cb_t callback takes ownership of a non-NULL skb regardless of err. It frees the skb on the rawsock, LLCP and NCI loopback error paths. It also stops digital DEP from freeing the response after the callback has been invoked.

include/net/nfc/nfc.h struct nfc_dev; · line 44

Quoted 13 lines
include/net/nfc/nfc.h +2 -0
index bcafab5c53e51..d3c2d67357f1b 100644
@@ -37,6 +37,8 @@ struct nfc_dev;
  *
  * When a rx or tx package is lost or corrupted or the target gets out
  * of the operating field, err is -EIO.
+ * If skb is non-NULL, ownership is transferred to the callback regardless
+ * of err. Providers must not access or free skb after invoking the callback.
  */
 typedef void (*data_exchange_cb_t)(void *context, struct sk_buff *skb,
 								int err);

[Severity: High]
This rule now covers every data_exchange_cb_t implementer. Several in-tree callbacks still return on error without freeing the skb. Is the fix complete without them?

In net/nfc/nci/hci.c, nci_hci_data_received_cb() is installed as conn_info->data_exchange_cb and does:

	if (err) {
		nci_req_complete(ndev, err);
		return;
	}

nci_rx_data_packet() strips a status byte for every conn_id, based on the device-wide ndev->target_active_prot. That status ends up in:

nci_rx_data_packet()
nci_add_rx_data_frag(..., nci_to_errno(status))
nci_data_exchange_complete(ndev, skb, conn_id, err)
cb(cb_context, skb, err);

Suppose an NFCC sends an error-status data packet on the HCI conn_id while a frame-I/F target is active and an RF transceive is pending. Wouldn't that skb leak? This is the same provider path that was tested for rawsock.

On the HCI side, nfc_hci_resp_received() calls:

	__nfc_hci_cmd_completion(hdev, nfc_hci_result_to_errno(result), skb);

As a result, every non-OK HCI response reaches the callback registered by nfc_hci_send_cmd_async() with both a non-NULL skb and a nonzero err. In drivers/nfc/st21nfca/dep.c, st21nfca_im_recv_atr_res_cb() and st21nfca_im_recv_dep_res_cb() are registered directly as that callback. Both start with:

	if (err != 0)
		return;

Would every HCI error response during NFC-DEP leak the response skb here? For example, a timeout when the peer leaves the field.

Separately, the ST21NFCA_CB_TYPE_READER_F success branch of st21nfca_im_recv_atr_res_cb() never frees skb at all.

Four default branches only free on success:

	default:
		if (err == 0)
			kfree_skb(skb);
		break;

They are hci_transceive_cb() in net/nfc/hci/core.c, pn544_hci_data_exchange_cb() in drivers/nfc/pn544/pn544.c, st21nfca_hci_data_exchange_cb() in drivers/nfc/st21nfca/core.c and microread_im_transceive_cb() in drivers/nfc/microread/microread.c. Under the new contract, don't these leak skb when err != 0?

nfc_hci_execute_cb() already frees skb on error, so the in-tree callbacks end up inconsistent with each other and with the documented rule. Should these callbacks be updated in the same patch, or at least mentioned in the commit message?

net/nfc/digital_dep.c static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg, · line 888

Quoted 12 lines
net/nfc/digital_dep.c +1 -0
index 968547c306a5d..c7f5378cd12e2 100644
@@ -882,6 +882,7 @@ static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
 
 exit:
 	data_exch->cb(data_exch->cb_context, resp, rc);
+	resp = NULL;
 
 error:
 	kfree(data_exch);

[Severity: Low]
This isn't a bug, but should this carry a Fixes: tag and Cc: stable? The rawsock_data_exchange_complete() leak can be reached by an unprivileged user through an NFC raw socket.

The hunks also depend on each other. The resp = NULL in digital_in_recv_dep_res() stops the provider from freeing resp, and the rawsock and LLCP hunks make the consumers free it.

A partial backport would either leak or double free the skb. Fixes:
tags would help stable take the change as one unit.

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118081530.32-1-luob87709%40gmail.com