Skip to contentSkip to conversation

net/nfc: fix skb ownership on data exchange errors

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

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Bin Luo

author
#0 lore raw

PATCH 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.

---
 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)
#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.

- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.

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.
#2 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 (2) · 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 skb leaks that users can hit, but it has no Fixes: tag and no Cc: [email protected].

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it runs through the rawsock error branch that the patch modifies.
- [High] This problem wasn't introduced by this patch, but it is in the function the patch changes.

--

net/nfc: fix skb ownership on data exchange errors

This patch 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 and LLCP error paths and on the NCI loopback lookup failure, and clears the Digital DEP response pointer after the callback is 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]
With this rule the callback owns a non-NULL skb whatever err is. Are there other data_exchange_cb_t implementations that still return early on error without freeing it?

In drivers/nfc/st21nfca/dep.c, st21nfca_im_recv_atr_res_cb() and st21nfca_im_recv_dep_res_cb() both start with:

	if (err != 0)
		return;

They are registered as the callback to nfc_hci_send_cmd_async() from st21nfca_im_send_atr_req() and st21nfca_im_send_dep_req(). When the chip answers with a non-OK HCI result, the path is:

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

    st21nfca_im_recv_atr_res_cb(ctx, skb, err)  /* or _dep_res_cb */
      return;

Nothing on that path seems to free the response skb. Is it leaked on every failed ATR_REQ or DEP_REQ? In the DEP_REQ case the upper layer also never gets a completion.

In net/nfc/nci/hci.c, nci_hci_data_received_cb() has:

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

nci_add_rx_data_frag() can hand the original skb plus a non-zero err to nci_data_exchange_complete() through its "if (status) { err = status; goto exit; }" path. This needs NCI_DATA_EXCHANGE to be set when the frame arrives, because nci_data_exchange_complete() only calls the callback after test_and_clear_bit(NCI_DATA_EXCHANGE) succeeds.

Should this branch call kfree_skb(skb) as well?

The HCI wrapper callbacks hci_transceive_cb() in net/nfc/hci/core.c, pn544_hci_data_exchange_cb(), st21nfca_hci_data_exchange_cb() and microread_im_transceive_cb() all have a default branch like:

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

That branch only runs with an unexpected async_cb_type. Under the new rule, though, wouldn't it leak the skb when err is non-zero?

The subject says this fixes skb ownership on data exchange errors. Should these callbacks be converted too?

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

Quoted 9 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;

[Severity: Low]
This fixes skb leaks that users can reach: rawsock_data_exchange_complete() and nfc_llcp_recv() on NCI or HCI error completions, and nci_nfcc_loopback_cb() when the connection lookup fails. Should the commit carry Fixes: tags and Cc: [email protected]?

The hunks also depend on each other. Before this patch, the exit: path in digital_in_recv_dep_res() with rc != 0 freed resp after the callback:

	if (rc)
		kfree_skb(resp);

A backport that took only the rawsock and LLCP hunks would turn Digital error paths into a double free, for example the NACK bit case (rc = -EIO; goto exit;).

Could the commit message say that the resp = NULL change has to go together with the rawsock and LLCP frees?

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

Quoted 3 lines
net/nfc/digital_dep.c +0 -0
 error:
 	kfree(data_exch);

[Severity: High]
This is a pre-existing issue and wasn't introduced by this patch, but it is in the function being changed. Several error paths in digital_in_recv_dep_res() jump straight to error: and free data_exch without ever calling data_exch->cb(). For example:

	if (resp->len < size || dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN ||
	    dep_res->cmd != DIGITAL_CMD_DEP_RES) {
		rc = -EIO;
		goto error;
	}

The CRC failure path does the same once the NACK retries run out, or when the frame is shorter than 4 bytes:

		PROTOCOL_ERR("14.4.1.6");
		goto error;

Other cases:

- the DID bit is set
- the I-PDU PNI does not match
- digital_in_send_nack(), digital_in_send_atn(), digital_in_send_dep_req(), digital_in_send_saved_skb() or digital_in_send_rtox() fails
- the ATN/NACK count or the RTOX value is invalid

Take a raw socket connected to an NFC_DEP target on a Digital driver. rawsock_tx_work() does:

	sock_hold(sk);
	rc = nfc_data_exchange(dev, target_idx, skb,
			       rawsock_data_exchange_complete, sk);

Only rawsock_data_exchange_complete() drops that reference and clears tx_work_scheduled. If the callback never runs, does the socket reference leak? If so, rawsock_destruct() never calls nfc_deactivate_target() or nfc_put_device(), the target stays active, and later nfc_activate_target() calls fail with -EBUSY.

tx_work_scheduled would also stay true, so later sendmsg() calls queue data that is never sent.

A remote peer can reach these paths by sending a malformed DEP_RES. The resp = NULL added here sits between exit: and error: and does not cover them.

[ ... ]

Quoted 5 lines
net/nfc/rawsock.c +0 -0
index f7d7a599fade7..3bf65b1fbe255 100644

net/nfc/rawsock.c static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb, · line 163

Quoted 9 lines
net/nfc/rawsock.c +4 -1
@@ -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;
+	}

[Severity: High]
This is a pre-existing issue and wasn't introduced by this patch, but this error branch can get a positive err from the NCI path. nci_rx_data_packet() does:

	nci_add_rx_data_frag(ndev, skb, pbf, conn_id, nci_to_errno(status));

but nci_add_rx_data_frag() declares the last parameter as __u8:

static void nci_add_rx_data_frag(struct nci_dev *ndev,

				 struct sk_buff *skb,
				 __u8 pbf, __u8 conn_id, __u8 status)

So a negative errno is truncated to a positive byte. For example, -ETIMEDOUT (-110) becomes 146 and is passed on as err.

rawsock_report_error() then does:

	sk->sk_err = -err;

That leaves sk_err at -146, so sock_error() returns +146. In rawsock_recvmsg() that value comes back as rc:

	skb = skb_recv_datagram(sk, flags, &rc);
	if (!skb)
		return rc;

Doesn't this make an error look like a 146-byte read with no data copied?

This affects MIFARE, Jewel, FeliCa and ISO15693 targets on NCI controllers whenever the frame status byte is non-zero, for example when the tag leaves the field.

Should the status parameter of nci_add_rx_data_frag() be an int?

Quoted 3 lines
net/nfc/rawsock.c +0 -0
 	err = rawsock_add_header(skb);
 	if (err)
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118081341.1514-1-luob87709%40gmail.com