* [PATCH v4 0/2] net/nfc: fix skb ownership and NCI status errors
@ 2026-11-26 6:39 Bin Luo (University of Electronic Science and Technology of China)
2026-11-26 6:39 ` [PATCH v4 1/2] net/nfc: fix skb ownership on data exchange errors Bin Luo (University of Electronic Science and Technology of China)
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Bin Luo (University of Electronic Science and Technology of China) @ 2026-11-26 6:39 UTC (permalink / raw)
To: oe-linux-nfc; +Cc: johan, david, kuba, netdev
Hi,
This v4 series addresses the review findings on the v3 NFC callback
ownership and NCI status-error patches:
Changes in v4:
- Patch 1 (net/nfc/hci/core.c, net/nfc/nci/hci.c): synchronize fragment
purging with fragment reassembly by splicing queued rx_hcp_frags onto
a local sk_buff_head under rx_hcp_frags.lock before walking and
allocating the reassembled skb, preventing concurrent purges from
racing with skb_queue_walk().
- Patch 1 (net/nfc/nci/core.c): handle nonzero err in
nci_nfcc_loopback_cb() by freeing skb and failing the pending NCI
request instead of reporting NCI_STATUS_OK.
- Patch 1 (net/nfc/nci/core.c, net/nfc/nci/ntf.c): free and clear
stale ndev->rx_data_reassembly on NCI data-exchange timeouts and
interface error notifications (serialized on ndev->rx_wq).
- Patch 1 & 2: expand commit messages (including user-visible SO_ERROR
impact in Patch 2), add Fixes: tags covering loopback and NCI timer
paths, and place Fixes:/Cc:/Signed-off-by: in a single trailer block.
Validation:
- Both patches apply cleanly with git am on v7.4-rc1 (6628e7d4c6b2) and
pass full-tree scripts/checkpatch.pl --strict with 0 errors, 0 warnings,
and 0 checks.
- Full x86_64 KASAN + kmemleak bzImage build passed with all modified NFC
drivers built in (NFC, Digital, NCI, HCI/SHDLC, NFC-SIM, virtual NCI,
PN533, PN544, Microread, ST21NFCA), and W=1 compilation of all 13
modified NFC object files produced 0 warnings and 0 errors.
- Real QEMU TCG verification ran 32 NCI 1.0 MIFARE status-error (0x01)
exchanges as euid 65534: unfixed baseline returned SO_ERROR=-240 with
47 kmemleak unreferenced objects; v4 returned SO_ERROR=16 on all 32
iterations with SELFTEST_STATUS=0 and 0 kmemleak / KASAN reports.
- Real PN533/PN544/Microread/ST21NFCA hardware was not exercised.
Best regards,
Bin Luo
Bin Luo (University of Electronic Science and Technology of China) (2):
net/nfc: fix skb ownership on data exchange errors
nfc: preserve NCI data status error codes
drivers/nfc/microread/microread.c | 3 +--
drivers/nfc/pn533/pn533.c | 1 +
drivers/nfc/pn544/pn544.c | 3 +--
drivers/nfc/st21nfca/core.c | 3 +--
drivers/nfc/st21nfca/dep.c | 26 ++++++++++++-------------
include/net/nfc/nfc.h | 5 ++++-
net/nfc/digital_dep.c | 32 ++++++++++++++-----------------
net/nfc/hci/core.c | 27 +++++++++++++++++---------
net/nfc/llcp_core.c | 4 +++-
net/nfc/nci/core.c | 18 +++++++++++++++---
net/nfc/nci/data.c | 8 ++++----
net/nfc/nci/hci.c | 25 ++++++++++++++++--------
net/nfc/nci/ntf.c | 5 ++++-
net/nfc/rawsock.c | 5 ++++-
14 files changed, 102 insertions(+), 63 deletions(-)
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v4 1/2] net/nfc: fix skb ownership on data exchange errors
2026-11-26 6:39 [PATCH v4 0/2] net/nfc: fix skb ownership and NCI status errors Bin Luo (University of Electronic Science and Technology of China)
@ 2026-11-26 6:39 ` Bin Luo (University of Electronic Science and Technology of China)
2026-11-27 7:35 ` netdev-bot+sashiko
2026-11-26 6:39 ` [PATCH v4 2/2] nfc: preserve NCI data status error codes Bin Luo (University of Electronic Science and Technology of China)
2026-11-26 6:42 ` [PATCH v4 0/2] net/nfc: fix skb ownership and NCI status errors netdev-bot+sinfo
2 siblings, 1 reply; 6+ messages in thread
From: Bin Luo (University of Electronic Science and Technology of China) @ 2026-11-26 6:39 UTC (permalink / raw)
To: oe-linux-nfc; +Cc: johan, david, kuba, netdev, stable
NFC data_exchange_cb_t callbacks may receive a non-NULL response skb
together with a nonzero error. Because callback ownership was not
documented, several consumers and wrapper callbacks return on error
without freeing that skb, leaking the response buffer on failed
exchanges (including rawsock, LLCP, HCI wrappers, ST21NFCA, Microread,
PN544, and NCI loopback/HCI callbacks).
Document the data_exchange_cb_t ownership contract so callbacks always
own a non-NULL skb regardless of err, and update affected paths:
- Free non-NULL skbs on error paths in rawsock, LLCP, HCI/NCI-HCI
wrappers, ST21NFCA, Microread, and PN544.
- Handle nonzero errors in nci_nfcc_loopback_cb() by freeing skb and
failing the pending NCI request instead of reporting NCI_STATUS_OK.
- Route terminal Digital DEP errors through the registered completion
callback so callers always receive completion and own resp.
- Drop partial NCI rx_data_reassembly fragments on data-status errors,
interface errors, and data-exchange timeouts so stale fragments are
neither leaked nor prepended to later responses.
- Splice queued HCI/NCI-HCI rx_hcp_frags onto a local sk_buff_head
under rx_hcp_frags.lock before walking/reassembling so concurrent
error purges cannot race with skb_queue_walk(), and purge partial
fragments on error.
- Clear resp after queueing it to dev->resp_q in PN533 so subsequent
error handling cannot free an already queued skb.
Fixes: 98ff416f97c5 ("NFC: nci: Add status byte management in case of error.")
Fixes: 1c53855f6be2 ("nfc: nci: Add nci_nfcc_loopback to the nci core")
Fixes: c4bf98b220cb ("NFC: Add NCI data exchange timer")
Cc: stable@vger.kernel.org
Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) <luob87709@gmail.com>
---
drivers/nfc/microread/microread.c | 3 +--
drivers/nfc/pn533/pn533.c | 1 +
drivers/nfc/pn544/pn544.c | 3 +--
drivers/nfc/st21nfca/core.c | 3 +--
drivers/nfc/st21nfca/dep.c | 26 ++++++++++++-------------
include/net/nfc/nfc.h | 5 ++++-
net/nfc/digital_dep.c | 32 ++++++++++++++-----------------
net/nfc/hci/core.c | 25 +++++++++++++++++-------
net/nfc/llcp_core.c | 4 +++-
net/nfc/nci/core.c | 15 ++++++++++++++-
net/nfc/nci/data.c | 2 ++
net/nfc/nci/hci.c | 23 +++++++++++++++++-----
net/nfc/nci/ntf.c | 5 ++++-
net/nfc/rawsock.c | 5 ++++-
14 files changed, 98 insertions(+), 54 deletions(-)
diff --git a/drivers/nfc/microread/microread.c b/drivers/nfc/microread/microread.c
index 2bfafa9..f221ec7 100644
--- a/drivers/nfc/microread/microread.c
+++ b/drivers/nfc/microread/microread.c
@@ -383,8 +383,7 @@ static void microread_im_transceive_cb(void *context, struct sk_buff *skb,
info->async_cb(info->async_cb_context, skb, err);
break;
default:
- if (err == 0)
- kfree_skb(skb);
+ kfree_skb(skb);
break;
}
}
diff --git a/drivers/nfc/pn533/pn533.c b/drivers/nfc/pn533/pn533.c
index b0133e5..f7bdc07 100644
--- a/drivers/nfc/pn533/pn533.c
+++ b/drivers/nfc/pn533/pn533.c
@@ -2138,6 +2138,7 @@ static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
}
skb_queue_tail(&dev->resp_q, resp);
+ resp = NULL;
if (mi) {
dev->cmd_complete_mi_arg = arg;
diff --git a/drivers/nfc/pn544/pn544.c b/drivers/nfc/pn544/pn544.c
index c4fa70e..82f7107 100644
--- a/drivers/nfc/pn544/pn544.c
+++ b/drivers/nfc/pn544/pn544.c
@@ -581,8 +581,7 @@ static void pn544_hci_data_exchange_cb(void *context, struct sk_buff *skb,
info->async_cb(info->async_cb_context, skb, err);
break;
default:
- if (err == 0)
- kfree_skb(skb);
+ kfree_skb(skb);
break;
}
}
diff --git a/drivers/nfc/st21nfca/core.c b/drivers/nfc/st21nfca/core.c
index b5c1ca3..68327ca 100644
--- a/drivers/nfc/st21nfca/core.c
+++ b/drivers/nfc/st21nfca/core.c
@@ -743,8 +743,7 @@ static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb,
info->async_cb(info->async_cb_context, skb, err);
break;
default:
- if (err == 0)
- kfree_skb(skb);
+ kfree_skb(skb);
break;
}
}
diff --git a/drivers/nfc/st21nfca/dep.c b/drivers/nfc/st21nfca/dep.c
index a5fab4f..c907b6e 100644
--- a/drivers/nfc/st21nfca/dep.c
+++ b/drivers/nfc/st21nfca/dep.c
@@ -449,11 +449,10 @@ static void st21nfca_im_recv_atr_res_cb(void *context, struct sk_buff *skb,
struct st21nfca_atr_res *atr_res;
int r;
- if (err != 0)
- return;
-
if (!skb)
return;
+ if (err)
+ goto out;
switch (info->async_cb_type) {
case ST21NFCA_CB_TYPE_READER_F:
@@ -463,7 +462,7 @@ static void st21nfca_im_recv_atr_res_cb(void *context, struct sk_buff *skb,
atr_res->gbi,
skb->len - sizeof(struct st21nfca_atr_res));
if (r < 0)
- return;
+ goto out;
if (atr_res->to >= 0x0e)
info->dep_info.to = 0x0e;
@@ -475,7 +474,7 @@ static void st21nfca_im_recv_atr_res_cb(void *context, struct sk_buff *skb,
r = nfc_dep_link_is_up(info->hdev->ndev, info->dep_info.idx,
NFC_COMM_PASSIVE, NFC_RF_INITIATOR);
if (r < 0)
- return;
+ goto out;
info->dep_info.curr_nfc_dep_pni = 0;
if (ST21NFCA_PP2LRI(atr_res->ppi) != info->dep_info.lri)
@@ -484,9 +483,11 @@ static void st21nfca_im_recv_atr_res_cb(void *context, struct sk_buff *skb,
ST21NFCA_PP2LRI(atr_res->ppi));
break;
default:
- kfree_skb(skb);
break;
}
+
+out:
+ kfree_skb(skb);
}
int st21nfca_im_send_atr_req(struct nfc_hci_dev *hdev, u8 *gb, size_t gb_len)
@@ -561,11 +562,10 @@ static void st21nfca_im_recv_dep_res_cb(void *context, struct sk_buff *skb,
int size;
- if (err != 0)
- return;
-
if (!skb)
return;
+ if (err)
+ goto exit;
switch (info->async_cb_type) {
case ST21NFCA_CB_TYPE_READER_F:
@@ -596,7 +596,7 @@ static void st21nfca_im_recv_dep_res_cb(void *context, struct sk_buff *skb,
size++;
skb_pull(skb, size);
nfc_tm_data_received(info->hdev->ndev, skb);
- break;
+ return;
case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
pr_err("Received a SUPERVISOR PDU\n");
skb_pull(skb, size);
@@ -606,10 +606,10 @@ static void st21nfca_im_recv_dep_res_cb(void *context, struct sk_buff *skb,
*(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
st21nfca_im_send_pdu(info, skb);
- break;
+ return;
+ default:
+ goto exit;
}
-
- return;
default:
break;
}
diff --git a/include/net/nfc/nfc.h b/include/net/nfc/nfc.h
index bcafab5..bdf2ac3 100644
--- a/include/net/nfc/nfc.h
+++ b/include/net/nfc/nfc.h
@@ -31,12 +31,15 @@ struct nfc_dev;
* data_exchange_cb_t - Definition of nfc_data_exchange callback
*
* @context: nfc_data_exchange cb_context parameter
- * @skb: response data
+ * @skb: response data, owned by the callback if non-NULL
* @err: If an error has occurred during data exchange, it is the
* error number. Zero means no error.
*
* 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, the callback owns it regardless of err and must free
+ * or otherwise consume it. Providers must not access or free it after the
+ * callback returns.
*/
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 968547c..cd6eff0 100644
--- a/net/nfc/digital_dep.c
+++ b/net/nfc/digital_dep.c
@@ -703,7 +703,7 @@ static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
rc = digital_in_send_nack(ddev, data_exch);
if (rc)
- goto error;
+ goto exit;
return;
} else if ((rc == -ETIMEDOUT) &&
@@ -712,7 +712,7 @@ static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
rc = digital_in_send_atn(ddev, data_exch);
if (rc)
- goto error;
+ goto exit;
return;
}
@@ -734,7 +734,7 @@ static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
rc = digital_in_send_nack(ddev, data_exch);
if (rc)
- goto error;
+ goto exit;
kfree_skb(resp);
@@ -742,7 +742,7 @@ static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
}
PROTOCOL_ERR("14.4.1.6");
- goto error;
+ goto exit;
}
ddev->atn_count = 0;
@@ -759,7 +759,7 @@ static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
if (resp->len < size || dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN ||
dep_res->cmd != DIGITAL_CMD_DEP_RES) {
rc = -EIO;
- goto error;
+ goto exit;
}
pfb = dep_res->pfb;
@@ -767,7 +767,7 @@ static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
PROTOCOL_ERR("14.8.2.1");
rc = -EIO;
- goto error;
+ goto exit;
}
if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
@@ -777,7 +777,7 @@ static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
if (size > resp->len) {
rc = -EIO;
- goto error;
+ goto exit;
}
skb_pull(resp, size);
@@ -787,7 +787,7 @@ static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
PROTOCOL_ERR("14.12.3.3");
rc = -EIO;
- goto error;
+ goto exit;
}
ddev->curr_nfc_dep_pni =
@@ -802,7 +802,7 @@ static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
if (IS_ERR(resp)) {
rc = PTR_ERR(resp);
resp = NULL;
- goto error;
+ goto exit;
}
/* If resp is NULL then we're still chaining so return and
@@ -847,7 +847,7 @@ static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
ddev->chaining_skb,
ddev->data_exch);
if (rc)
- goto error;
+ goto exit;
goto free_resp;
@@ -855,7 +855,7 @@ static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) { /* ATN */
rc = digital_in_send_saved_skb(ddev, data_exch);
if (rc)
- goto error;
+ goto exit;
goto free_resp;
}
@@ -863,19 +863,19 @@ static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
if (ddev->atn_count || ddev->nack_count) {
PROTOCOL_ERR("14.12.4.4");
rc = -EIO;
- goto error;
+ goto exit;
}
rtox = DIGITAL_NFC_DEP_RTOX_VALUE(resp->data[0]);
if (!rtox || rtox > DIGITAL_NFC_DEP_RTOX_MAX) {
PROTOCOL_ERR("14.8.4.1");
rc = -EIO;
- goto error;
+ goto exit;
}
rc = digital_in_send_rtox(ddev, data_exch, rtox);
if (rc)
- goto error;
+ goto exit;
goto free_resp;
}
@@ -883,7 +883,6 @@ static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
exit:
data_exch->cb(data_exch->cb_context, resp, rc);
-error:
kfree(data_exch);
kfree_skb(ddev->chaining_skb);
@@ -892,9 +891,6 @@ error:
kfree_skb(ddev->saved_skb);
ddev->saved_skb = NULL;
- if (rc)
- kfree_skb(resp);
-
return;
free_resp:
diff --git a/net/nfc/hci/core.c b/net/nfc/hci/core.c
index ba6f031..1b43f1e 100644
--- a/net/nfc/hci/core.c
+++ b/net/nfc/hci/core.c
@@ -713,8 +713,7 @@ static void hci_transceive_cb(void *context, struct sk_buff *skb, int err)
hdev->async_cb(hdev->async_cb_context, skb, err);
break;
default:
- if (err == 0)
- kfree_skb(skb);
+ kfree_skb(skb);
break;
}
}
@@ -833,6 +832,7 @@ static int hci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
static void nfc_hci_failure(struct nfc_hci_dev *hdev, int err)
{
+ skb_queue_purge(&hdev->rx_hcp_frags);
mutex_lock(&hdev->msg_tx_mutex);
if (hdev->cmd_pending_msg == NULL) {
@@ -859,9 +859,12 @@ static void nfc_hci_recv_from_llc(struct nfc_hci_dev *hdev, struct sk_buff *skb)
struct sk_buff *hcp_skb;
u8 pipe;
struct sk_buff *frag_skb;
+ struct sk_buff_head frags;
+ unsigned long flags;
int msg_len;
if (!pskb_may_pull(skb, NFC_HCI_HCP_PACKET_HEADER_LEN)) {
+ skb_queue_purge(&hdev->rx_hcp_frags);
kfree_skb(skb);
return;
}
@@ -873,12 +876,19 @@ static void nfc_hci_recv_from_llc(struct nfc_hci_dev *hdev, struct sk_buff *skb)
}
/* it's the last fragment. Does it need re-aggregation? */
- if (skb_queue_len(&hdev->rx_hcp_frags)) {
+ __skb_queue_head_init(&frags);
+ spin_lock_irqsave(&hdev->rx_hcp_frags.lock, flags);
+ if (!skb_queue_empty(&hdev->rx_hcp_frags)) {
+ __skb_queue_tail(&hdev->rx_hcp_frags, skb);
+ skb_queue_splice_init(&hdev->rx_hcp_frags, &frags);
+ }
+ spin_unlock_irqrestore(&hdev->rx_hcp_frags.lock, flags);
+
+ if (!skb_queue_empty(&frags)) {
pipe = packet->header & NFC_HCI_FRAGMENT;
- skb_queue_tail(&hdev->rx_hcp_frags, skb);
msg_len = 0;
- skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
+ skb_queue_walk(&frags, frag_skb) {
msg_len += (frag_skb->len -
NFC_HCI_HCP_PACKET_HEADER_LEN);
}
@@ -886,20 +896,21 @@ static void nfc_hci_recv_from_llc(struct nfc_hci_dev *hdev, struct sk_buff *skb)
hcp_skb = nfc_alloc_recv_skb(NFC_HCI_HCP_PACKET_HEADER_LEN +
msg_len, GFP_KERNEL);
if (hcp_skb == NULL) {
+ __skb_queue_purge(&frags);
nfc_hci_failure(hdev, -ENOMEM);
return;
}
skb_put_u8(hcp_skb, pipe);
- skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
+ skb_queue_walk(&frags, frag_skb) {
msg_len = frag_skb->len - NFC_HCI_HCP_PACKET_HEADER_LEN;
skb_put_data(hcp_skb,
frag_skb->data + NFC_HCI_HCP_PACKET_HEADER_LEN,
msg_len);
}
- skb_queue_purge(&hdev->rx_hcp_frags);
+ __skb_queue_purge(&frags);
} else {
packet->header &= NFC_HCI_FRAGMENT;
hcp_skb = skb;
diff --git a/net/nfc/llcp_core.c b/net/nfc/llcp_core.c
index 74bf817..0736a0e 100644
--- a/net/nfc/llcp_core.c
+++ b/net/nfc/llcp_core.c
@@ -1636,8 +1636,10 @@ void nfc_llcp_recv(void *data, struct sk_buff *skb, int err)
{
struct nfc_llcp_local *local = (struct nfc_llcp_local *) data;
- if (err < 0) {
+ if (err) {
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 73e3a96..f2b3137 100644
--- a/net/nfc/nci/core.c
+++ b/net/nfc/nci/core.c
@@ -411,8 +411,18 @@ static void nci_nfcc_loopback_cb(void *context, struct sk_buff *skb, int err)
struct nci_dev *ndev = (struct nci_dev *)context;
struct nci_conn_info *conn_info;
+ if (err) {
+ kfree_skb(skb);
+ if (err < 0)
+ nci_req_cancel(ndev, -err);
+ else
+ nci_req_complete(ndev, NCI_STATUS_FAILED);
+ return;
+ }
+
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;
}
@@ -1597,10 +1607,13 @@ static void nci_rx_work(struct work_struct *work)
/* check if a data exchange timeout has occurred */
if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
/* complete the data exchange transaction, if exists */
- if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
+ if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) {
+ kfree_skb(ndev->rx_data_reassembly);
+ ndev->rx_data_reassembly = NULL;
nci_data_exchange_complete(ndev, NULL,
ndev->cur_conn_id,
-ETIMEDOUT);
+ }
clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
}
diff --git a/net/nfc/nci/data.c b/net/nfc/nci/data.c
index 4253ede..3bbebb9 100644
--- a/net/nfc/nci/data.c
+++ b/net/nfc/nci/data.c
@@ -220,6 +220,8 @@ static void nci_add_rx_data_frag(struct nci_dev *ndev,
if (status) {
err = status;
+ kfree_skb(ndev->rx_data_reassembly);
+ ndev->rx_data_reassembly = NULL;
goto exit;
}
diff --git a/net/nfc/nci/hci.c b/net/nfc/nci/hci.c
index c03e8a0..aca05ca 100644
--- a/net/nfc/nci/hci.c
+++ b/net/nfc/nci/hci.c
@@ -432,14 +432,19 @@ void nci_hci_data_received_cb(void *context,
u8 pipe, type;
struct sk_buff *hcp_skb;
struct sk_buff *frag_skb;
+ struct sk_buff_head frags;
+ unsigned long flags;
int msg_len;
if (err) {
+ skb_queue_purge(&ndev->hci_dev->rx_hcp_frags);
+ kfree_skb(skb);
nci_req_complete(ndev, err);
return;
}
if (!pskb_may_pull(skb, NCI_HCI_HCP_PACKET_HEADER_LEN)) {
+ skb_queue_purge(&ndev->hci_dev->rx_hcp_frags);
kfree_skb(skb);
return;
}
@@ -451,12 +456,19 @@ void nci_hci_data_received_cb(void *context,
}
/* it's the last fragment. Does it need re-aggregation? */
- if (skb_queue_len(&ndev->hci_dev->rx_hcp_frags)) {
+ __skb_queue_head_init(&frags);
+ spin_lock_irqsave(&ndev->hci_dev->rx_hcp_frags.lock, flags);
+ if (!skb_queue_empty(&ndev->hci_dev->rx_hcp_frags)) {
+ __skb_queue_tail(&ndev->hci_dev->rx_hcp_frags, skb);
+ skb_queue_splice_init(&ndev->hci_dev->rx_hcp_frags, &frags);
+ }
+ spin_unlock_irqrestore(&ndev->hci_dev->rx_hcp_frags.lock, flags);
+
+ if (!skb_queue_empty(&frags)) {
pipe = NCI_HCP_MSG_GET_PIPE(packet->header);
- skb_queue_tail(&ndev->hci_dev->rx_hcp_frags, skb);
msg_len = 0;
- skb_queue_walk(&ndev->hci_dev->rx_hcp_frags, frag_skb) {
+ skb_queue_walk(&frags, frag_skb) {
msg_len += (frag_skb->len -
NCI_HCI_HCP_PACKET_HEADER_LEN);
}
@@ -464,20 +476,21 @@ void nci_hci_data_received_cb(void *context,
hcp_skb = nfc_alloc_recv_skb(NCI_HCI_HCP_PACKET_HEADER_LEN +
msg_len, GFP_KERNEL);
if (!hcp_skb) {
+ __skb_queue_purge(&frags);
nci_req_complete(ndev, -ENOMEM);
return;
}
skb_put_u8(hcp_skb, pipe);
- skb_queue_walk(&ndev->hci_dev->rx_hcp_frags, frag_skb) {
+ skb_queue_walk(&frags, frag_skb) {
msg_len = frag_skb->len - NCI_HCI_HCP_PACKET_HEADER_LEN;
skb_put_data(hcp_skb,
frag_skb->data + NCI_HCI_HCP_PACKET_HEADER_LEN,
msg_len);
}
- skb_queue_purge(&ndev->hci_dev->rx_hcp_frags);
+ __skb_queue_purge(&frags);
} else {
packet->header &= NCI_HCI_FRAGMENT;
hcp_skb = skb;
diff --git a/net/nfc/nci/ntf.c b/net/nfc/nci/ntf.c
index f5c9a8a..2149383 100644
--- a/net/nfc/nci/ntf.c
+++ b/net/nfc/nci/ntf.c
@@ -133,8 +133,11 @@ static int nci_core_conn_intf_error_ntf_packet(struct nci_dev *ndev,
pr_debug("status 0x%x, conn_id %d\n", ntf->status, ntf->conn_id);
/* complete the data exchange transaction, if exists */
- if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
+ if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) {
+ kfree_skb(ndev->rx_data_reassembly);
+ ndev->rx_data_reassembly = NULL;
nci_data_exchange_complete(ndev, NULL, ntf->conn_id, -EIO);
+ }
return 0;
}
diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
index f7d7a59..3bf65b1 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.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v4 2/2] nfc: preserve NCI data status error codes
2026-11-26 6:39 [PATCH v4 0/2] net/nfc: fix skb ownership and NCI status errors Bin Luo (University of Electronic Science and Technology of China)
2026-11-26 6:39 ` [PATCH v4 1/2] net/nfc: fix skb ownership on data exchange errors Bin Luo (University of Electronic Science and Technology of China)
@ 2026-11-26 6:39 ` Bin Luo (University of Electronic Science and Technology of China)
2026-11-26 6:42 ` [PATCH v4 0/2] net/nfc: fix skb ownership and NCI status errors netdev-bot+sinfo
2 siblings, 0 replies; 6+ messages in thread
From: Bin Luo (University of Electronic Science and Technology of China) @ 2026-11-26 6:39 UTC (permalink / raw)
To: oe-linux-nfc; +Cc: johan, david, kuba, netdev, stable
nci_to_errno() returns negative errno values for nonzero data-status
bytes. Passing that value to nci_add_rx_data_frag() through a __u8
parameter truncates the signed errno into an unrelated positive value
before the completion callback receives it.
For userspace AF_NFC raw sockets, this corrupts the reported error: for
example, an NCI status error mapped to -EBUSY (-16) is truncated to
240 and negated to SO_ERROR=-240 instead of reporting EBUSY (16).
Keep the error parameter as int so the original errno reaches the
completion path and userspace unchanged.
Fixes: 98ff416f97c5 ("NFC: nci: Add status byte management in case of error.")
Cc: stable@vger.kernel.org
Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) <luob87709@gmail.com>
---
net/nfc/nci/data.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/net/nfc/nci/data.c b/net/nfc/nci/data.c
index 3bbebb9..4a2326a 100644
--- a/net/nfc/nci/data.c
+++ b/net/nfc/nci/data.c
@@ -213,13 +213,11 @@ EXPORT_SYMBOL(nci_send_data);
static void nci_add_rx_data_frag(struct nci_dev *ndev,
struct sk_buff *skb,
- __u8 pbf, __u8 conn_id, __u8 status)
+ __u8 pbf, __u8 conn_id, int err)
{
int reassembly_len;
- int err = 0;
- if (status) {
- err = status;
+ if (err) {
kfree_skb(ndev->rx_data_reassembly);
ndev->rx_data_reassembly = NULL;
goto exit;
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v4 0/2] net/nfc: fix skb ownership and NCI status errors
2026-11-26 6:39 [PATCH v4 0/2] net/nfc: fix skb ownership and NCI status errors Bin Luo (University of Electronic Science and Technology of China)
2026-11-26 6:39 ` [PATCH v4 1/2] net/nfc: fix skb ownership on data exchange errors Bin Luo (University of Electronic Science and Technology of China)
2026-11-26 6:39 ` [PATCH v4 2/2] nfc: preserve NCI data status error codes Bin Luo (University of Electronic Science and Technology of China)
@ 2026-11-26 6:42 ` netdev-bot+sinfo
2 siblings, 0 replies; 6+ messages in thread
From: netdev-bot+sinfo @ 2026-11-26 6:42 UTC (permalink / raw)
To: Bin Luo (University of Electronic Science and Technology of China)
Cc: oe-linux-nfc, johan, david, kuba, netdev
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.
--
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v4 1/2] net/nfc: fix skb ownership on data exchange errors
2026-11-26 6:39 ` [PATCH v4 1/2] net/nfc: fix skb ownership on data exchange errors Bin Luo (University of Electronic Science and Technology of China)
@ 2026-11-27 7:35 ` netdev-bot+sashiko
2026-11-27 13:40 ` Bin Luo (University of Electronic Science and Technology of China)
0 siblings, 1 reply; 6+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 7:35 UTC (permalink / raw)
To: luob87709; +Cc: oe-linux-nfc, johan, david, kuba, netdev, stable
Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 3 · 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).
- [Medium] The commit message says the NCI loopback and NCI-HCI callbacks
leak the response skb on failed exchanges, and adds 'Fixes:…
- [Medium] The patch says it drops partial ndev->rx_data_reassembly
fragments 'so stale fragments are neither leaked nor prepended to…
- [Medium] The commit says it drops partial NCI rx_data_reassembly
fragments on interface errors.
- [Low] The commit message calls the patch a fix for skb leaks on error
paths, but several hunks fix different and in one case more serious…
--
net/nfc: fix skb ownership on data exchange errors
This patch documents that a data_exchange_cb_t callback owns any
non-NULL skb regardless of err. It updates the rawsock, LLCP, HCI,
NCI, Digital DEP and several driver callbacks to free or complete on
error. It also drops partial NCI rx_data_reassembly fragments on
several error paths and reworks HCI fragment reassembly to use a
local queue.
> - Clear resp after queueing it to dev->resp_q in PN533 so subsequent
> error handling cannot free an already queued skb.
[Severity: Low]
Is the PN533 case a double free rather than an ownership cleanup?
In pn533_data_exchange_complete(), resp has already been queued on
dev->resp_q. If pn533_build_response() then fails its alloc_skb(), it
purges that queue:
drivers/nfc/pn533/pn533.c:pn533_build_response() {
...
skb = alloc_skb(skb_len, GFP_KERNEL);
if (skb == NULL)
goto out;
...
out:
skb_queue_purge(&dev->resp_q);
...
}
That frees resp, and the old error path then called dev_kfree_skb(resp)
on it a second time.
Some other hunks also fix bugs other than skb leaks. The old goto error
sites in digital_in_recv_dep_res() skipped data_exch->cb(), so rawsock
never got a completion. It kept its sock reference and left
tx_work_scheduled set. That is a hang plus a refcount leak.
The st21nfca changes also fix success-path leaks.
st21nfca_im_recv_atr_res_cb() did not free the skb on the READER_F path
or on the r < 0 returns. st21nfca_im_recv_dep_res_cb() leaked it on an
unknown PFB type.
> Fixes: 98ff416f97c5 ("NFC: nci: Add status byte management in case of error.")
> Fixes: 1c53855f6be2 ("nfc: nci: Add nci_nfcc_loopback to the nci core")
> Fixes: c4bf98b220cb ("NFC: Add NCI data exchange timer")
> Cc: stable@vger.kernel.org
All three Fixes tags point at NCI commits. Could tags be added for the
commits that introduced the PN533, digital DEP, HCI, st21nfca, microread
and pn544 issues? Most of the diff lands there, and the patch is going
to stable.
> diff --git a/net/nfc/nci/core.c b/net/nfc/nci/core.c
> index 73e3a96470ace..f2b3137cb121e 100644
> --- a/net/nfc/nci/core.c
> +++ b/net/nfc/nci/core.c
> @@ -411,8 +411,18 @@ static void nci_nfcc_loopback_cb(void *context, struct sk_buff *skb, int err)
> struct nci_dev *ndev = (struct nci_dev *)context;
> struct nci_conn_info *conn_info;
>
> + if (err) {
> + kfree_skb(skb);
> + if (err < 0)
> + nci_req_cancel(ndev, -err);
> + else
> + nci_req_complete(ndev, NCI_STATUS_FAILED);
> + return;
> + }
> +
[Severity: Medium]
Can this new error path in nci_nfcc_loopback_cb() actually run?
nci_data_exchange_complete() only calls the callback if it can claim
NCI_DATA_EXCHANGE:
net/nfc/nci/data.c:nci_data_exchange_complete() {
...
if (!test_and_clear_bit(NCI_DATA_EXCHANGE, &ndev->flags)) {
kfree_skb(skb);
return;
}
...
}
The only place that sets the bit appears to be nci_transceive():
if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags)) {
Neither nci_nfcc_loopback() nor the NCI-HCI send path sets it. The
NCI-HCI path is nci_hci_send_cmd()->nci_request(nci_hci_send_data_req)
->nci_send_data(). So loopback and NCI-HCI responses seem to be dropped
inside nci_data_exchange_complete() before nci_nfcc_loopback_cb() or
nci_hci_data_received_cb() is reached. That makes the new err branches
in both callbacks unreachable.
If so, loopback and NCI-HCI requests on this tree would always time out.
The commit message does not mention this.
The commit message also says the loopback callback leaked the response
skb. The old nci_nfcc_loopback_cb() ignored err, stored the skb in
conn_info->rx_skb and completed with NCI_STATUS_OK. A non-NULL skb was
handed to the caller and freed there, not leaked.
The old defect looks like a different one. An -EIO or -ETIMEDOUT
completion with a NULL skb was reported as success with a NULL *resp.
st_nci_loopback() then dereferences skb->len on it.
Should the commit message and the Fixes: 1c53855f6be2 tag describe this
as a NULL pointer dereference instead of a leak?
> 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;
> }
> @@ -1597,10 +1607,13 @@ static void nci_rx_work(struct work_struct *work)
> /* check if a data exchange timeout has occurred */
> if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
> /* complete the data exchange transaction, if exists */
> - if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
> + if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) {
> + kfree_skb(ndev->rx_data_reassembly);
> + ndev->rx_data_reassembly = NULL;
> nci_data_exchange_complete(ndev, NULL,
> ndev->cur_conn_id,
> -ETIMEDOUT);
> + }
[Severity: Medium]
The commit message says partial fragments are now dropped "so stale
fragments are neither leaked nor prepended to later responses". Does
nci_close_device() still keep them?
Both close paths complete the exchange and clear the flags, but neither
touches ndev->rx_data_reassembly:
net/nfc/nci/core.c:nci_close_device() {
...
if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
nci_data_exchange_complete(ndev, NULL, ndev->cur_conn_id,
-ENODEV);
/* Clear flags except NCI_UNREG */
ndev->flags &= BIT(NCI_UNREG);
...
}
nci_open_device() does not reset it either. The only other place it is
freed is nci_free_device().
In listen/target mode this looks reachable:
1. nfc_tm_activated() clears dev->polling and never sets
dev->active_target.
2. nfc_dev_down() only checks those two fields, so the device can be
closed while an NCI_PBF_CONT segment from the remote initiator is
buffered.
3. The device is brought back up and activated again.
4. The first data packet in nci_add_rx_data_frag() finds
rx_data_reassembly non-NULL.
5. The previous session's bytes are prepended before the packet goes
to nfc_tm_data_received(), or to the transceive callback in poll
mode.
Should nci_close_device() free rx_data_reassembly as well? It would need
to come after the flush_workqueue(ndev->rx_wq) at the end of the
function, because rx_work may still be running before that point.
>
> clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
> }
[ ... ]
> diff --git a/net/nfc/nci/ntf.c b/net/nfc/nci/ntf.c
> index f5c9a8ab7ec10..2149383889338 100644
> --- a/net/nfc/nci/ntf.c
> +++ b/net/nfc/nci/ntf.c
> @@ -133,8 +133,11 @@ static int nci_core_conn_intf_error_ntf_packet(struct nci_dev *ndev,
> pr_debug("status 0x%x, conn_id %d\n", ntf->status, ntf->conn_id);
>
> /* complete the data exchange transaction, if exists */
> - if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
> + if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) {
> + kfree_skb(ndev->rx_data_reassembly);
> + ndev->rx_data_reassembly = NULL;
> nci_data_exchange_complete(ndev, NULL, ntf->conn_id, -EIO);
> + }
[Severity: Medium]
Should the rx_data_reassembly drop sit outside the test_bit() check?
NCI_DATA_EXCHANGE is only set by nci_transceive(). In listen/target
mode (rf_mode == NFC_RF_TARGET) the bit is never set, but
nci_add_rx_data_frag() still stores NCI_PBF_CONT segments in
ndev->rx_data_reassembly.
Suppose the NFCC sends a CORE_INTERFACE_ERROR_NTF in target mode after
forwarding a partial segment. The stale partial stays in place. The
next segment from the remote initiator gets those bytes prepended
before it reaches nfc_tm_data_received().
nci_rf_deactivate_ntf_packet() already drops the reassembly without
any condition:
/* drop partial rx data packet */
if (ndev->rx_data_reassembly) {
kfree_skb(ndev->rx_data_reassembly);
ndev->rx_data_reassembly = NULL;
}
/* complete the data exchange transaction, if exists */
if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
>
> return 0;
> }
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126063911.353-1-luob87709%40gmail.com
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v4 1/2] net/nfc: fix skb ownership on data exchange errors
2026-11-27 7:35 ` netdev-bot+sashiko
@ 2026-11-27 13:40 ` Bin Luo (University of Electronic Science and Technology of China)
0 siblings, 0 replies; 6+ messages in thread
From: Bin Luo (University of Electronic Science and Technology of China) @ 2026-11-27 13:40 UTC (permalink / raw)
To: netdev-bot+sashiko; +Cc: oe-linux-nfc, johan, david, kuba, netdev
Hi,
Thank you for the review. All four findings are valid and will be
addressed in v5:
1. nci_nfcc_loopback_cb() and NCI_DATA_EXCHANGE:
Agreed. The old nci_nfcc_loopback_cb() defect on nonzero err with a
NULL skb was reporting NCI_STATUS_OK with a NULL *resp (risking a
NULL pointer dereference in st_nci_loopback()), plus leaking skb
when conn_info lookup fails, rather than leaking skb on the normal
error path. Also agreed that current mainline
nci_data_exchange_complete() requires NCI_DATA_EXCHANGE to be set
(which loopback and NCI-HCI send paths do not currently set). In v5
we update the commit description to accurately state both the NULL
dereference / !conn_info leak and this mainline gating nuance while
keeping the callbacks consistent with the data_exchange_cb_t
contract for stable trees and future fixes.
2. nci_close_device() and stale ndev->rx_data_reassembly:
Agreed. In v5 we free and clear ndev->rx_data_reassembly after
flush_workqueue(ndev->rx_wq) on both close paths in
nci_close_device() (guarded by req_lock and !NCI_UP).
3. nci_core_conn_intf_error_ntf_packet() in target mode:
Agreed. In v5 we move the ndev->rx_data_reassembly drop outside the
test_bit(NCI_DATA_EXCHANGE, &ndev->flags) check, matching
nci_rf_deactivate_ntf_packet().
4. Patch scope, accurate descriptions, and Fixes tags:
In v5 we split the series cleanly so each bug class has its own
patch and accurate Fixes tags:
- Patch 1/5: data_exchange_cb_t contract and callback skb leaks
(explicitly calling out ST21NFCA success-path leaks and the
nci_nfcc_loopback_cb() NULL-dereference / !conn_info leak).
- Patch 2/5: PN533 double-free after queueing resp to dev->resp_q
(in both pn533_data_exchange_complete() and
pn533_tm_get_data_complete()).
- Patch 3/5: Digital DEP missing completion callback on terminal
errors (hang + socket refcount leak).
- Patch 4/5: NCI rx_data_reassembly cleanup on data-status errors,
interface errors (unconditional), timeouts, and device close.
- Patch 5/5: Preserve signed errno in nci_add_rx_data_frag().
I will respin and post v5 as a new thread shortly.
Best regards,
Bin Luo
pw-bot: cr
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-27 13:40 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 6:39 [PATCH v4 0/2] net/nfc: fix skb ownership and NCI status errors Bin Luo (University of Electronic Science and Technology of China)
2026-11-26 6:39 ` [PATCH v4 1/2] net/nfc: fix skb ownership on data exchange errors Bin Luo (University of Electronic Science and Technology of China)
2026-11-27 7:35 ` netdev-bot+sashiko
2026-11-27 13:40 ` Bin Luo (University of Electronic Science and Technology of China)
2026-11-26 6:39 ` [PATCH v4 2/2] nfc: preserve NCI data status error codes Bin Luo (University of Electronic Science and Technology of China)
2026-11-26 6:42 ` [PATCH v4 0/2] net/nfc: fix skb ownership and NCI status errors netdev-bot+sinfo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox