From: Bin Luo <luob87709@gmail.com>
To: oe-linux-nfc@lists.linux.dev
Cc: johan@kernel.org, david+nfc@ixit.cz, netdev@vger.kernel.org,
Bin Luo <luob87709@gmail.com>
Subject: [PATCH v2] net/nfc: fix skb ownership on data exchange errors
Date: Wed, 18 Nov 2026 16:15:30 +0800 [thread overview]
Message-ID: <20261118081530.32-1-luob87709@gmail.com> (raw)
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.
Signed-off-by: Bin Luo <luob87709@gmail.com>
---
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(-)
diff --git a/include/net/nfc/nfc.h b/include/net/nfc/nfc.h
index bcafab5c5..d3c2d6735 100644
--- a/include/net/nfc/nfc.h
+++ b/include/net/nfc/nfc.h
@@ -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);
diff --git a/net/nfc/digital_dep.c b/net/nfc/digital_dep.c
index 968547c30..c7f5378cd 100644
--- a/net/nfc/digital_dep.c
+++ b/net/nfc/digital_dep.c
@@ -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);
diff --git a/net/nfc/llcp_core.c b/net/nfc/llcp_core.c
index 74bf81700..3619e12b3 100644
--- a/net/nfc/llcp_core.c
+++ b/net/nfc/llcp_core.c
@@ -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;
}
diff --git a/net/nfc/nci/core.c b/net/nfc/nci/core.c
index 73e3a9647..678f744a2 100644
--- a/net/nfc/nci/core.c
+++ b/net/nfc/nci/core.c
@@ -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;
}
diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
index f7d7a599f..3bf65b1fb 100644
--- a/net/nfc/rawsock.c
+++ b/net/nfc/rawsock.c
@@ -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)
--
2.51.0.windows.1
next reply other threads:[~2026-11-18 8:15 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 8:15 Bin Luo [this message]
2026-11-18 8:18 ` [PATCH v2] net/nfc: fix skb ownership on data exchange errors netdev-bot+sinfo
2026-11-18 8:23 ` bin luo
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=20261118081530.32-1-luob87709@gmail.com \
--to=luob87709@gmail.com \
--cc=david+nfc@ixit.cz \
--cc=johan@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=oe-linux-nfc@lists.linux.dev \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.