From: Piyus Kedia <piyus@iiitd.ac.in>
To: David Heidelberg <david@ixit.cz>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Willem de Bruijn <willemb@google.com>,
Simon Horman <horms@kernel.org>
Cc: oe-linux-nfc@lists.linux.dev, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org,
Kapil Vaswani <kapil@presceralabs.ai>
Subject: [PATCH net v2 2/3] net/nfc: llcp: take the skb reference before queueing it to the socket
Date: Mon, 30 Nov 2026 06:52:07 +0530 [thread overview]
Message-ID: <20261130012208.1962491-3-piyus@iiitd.ac.in> (raw)
In-Reply-To: <20261130012208.1962491-1-piyus@iiitd.ac.in>
nfc_llcp_recv_ui() and nfc_llcp_recv_hdlc() queue a received frame to an
LLCP socket and only then take the reference meant to keep it alive:
if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
/*
* UI frames will be freed from the socket layer, so we
* need to keep them alive until someone receives them.
*/
skb_get(skb);
} else {
pr_err("Receive queue is full\n");
}
sock_queue_rcv_skb() takes over the caller's reference and publishes the
skb on sk_receive_queue at refcount one, so a concurrent recvmsg() - or
the socket's own teardown - can dequeue and free it before the skb_get()
runs. The increment then lands in freed memory:
refcount_t: addition on 0; use-after-free.
__refcount_add
skb_get
nfc_llcp_recv_hdlc
nfc_llcp_rx_skb
nfc_llcp_recv_agf
nfc_llcp_rx_skb
nfc_llcp_rx_work
BUG: KASAN: slab-use-after-free in nfc_llcp_rx_skb
Write of size 4 at addr ffff888055abe5e4 by task kworker/1:2/56
Both handlers are reached from nfc_llcp_rx_skb(), and the aggregated-frame
path widens the window by dispatching sub-frames through it in a loop.
Take the reference before queueing and release it again when the queueing
fails. That is the ownership rule the rest of the tree follows, including
the raw-socket copy in this same file:
if (sock_queue_rcv_skb(sk, nskb))
kfree_skb(nskb);
sock_queue_rcv_skb() does not free the skb on any of its failure paths, so
the explicit kfree_skb() balances only the reference taken here.
How this was found: by AI-driven fuzzing built on top of syzkaller. Its
analysis was then checked by hand against the code, the fault was
reproduced on net.git main under KASAN, and the patched kernel is clean on
the same reproducer with ordinary NFC LLCP traffic unaffected.
Fixes: 1727cf937401 ("NFC: llcp: Fix Rx memory leak")
Link: https://lore.kernel.org/netdev/20261123023519.3350997-1-piyus@iiitd.ac.in/
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
net/nfc/llcp_core.c | 33 +++++++++++++++++++--------------
1 file changed, 19 insertions(+), 14 deletions(-)
diff --git a/net/nfc/llcp_core.c b/net/nfc/llcp_core.c
index 74bf817007cf..e988c95cc172 100644
--- a/net/nfc/llcp_core.c
+++ b/net/nfc/llcp_core.c
@@ -922,14 +922,16 @@ static void nfc_llcp_recv_ui(struct nfc_llcp_local *local,
/* There is no sequence with UI frames */
skb_pull(skb, LLCP_HEADER_SIZE);
- if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
- /*
- * UI frames will be freed from the socket layer, so we
- * need to keep them alive until someone receives them.
- */
- skb_get(skb);
- } else {
+ /*
+ * UI frames will be freed from the socket layer, so we need to keep
+ * them alive until someone receives them. Take the reference before
+ * the skb is queued: once it is on the receive queue a concurrent
+ * recvmsg() may already have freed it.
+ */
+ skb_get(skb);
+ if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
pr_err("Receive queue is full\n");
+ kfree_skb(skb);
}
nfc_llcp_sock_put(llcp_sock);
@@ -1136,14 +1138,17 @@ static void nfc_llcp_recv_hdlc(struct nfc_llcp_local *local,
pr_err("Received out of sequence I PDU\n");
skb_pull(skb, LLCP_HEADER_SIZE + LLCP_SEQUENCE_SIZE);
- if (!sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
- /*
- * I frames will be freed from the socket layer, so we
- * need to keep them alive until someone receives them.
- */
- skb_get(skb);
- } else {
+ /*
+ * I frames will be freed from the socket layer, so we need to
+ * keep them alive until someone receives them. Take the
+ * reference before the skb is queued: once it is on the
+ * receive queue a concurrent recvmsg() may already have freed
+ * it.
+ */
+ skb_get(skb);
+ if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
pr_err("Receive queue is full\n");
+ kfree_skb(skb);
}
}
--
2.34.1
next prev parent reply other threads:[~2026-11-30 1:22 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 1:22 [PATCH net v2 0/3] net/nfc: three peer-reachable skb bugs in LLCP and NCI Piyus Kedia
2026-11-30 1:22 ` [PATCH net v2 1/3] net/nfc: llcp: fix the 16-bit truncation that overruns the SNL response skb Piyus Kedia
2026-12-01 2:15 ` netdev-bot+sashiko
2026-11-30 1:22 ` Piyus Kedia [this message]
2026-12-01 2:15 ` [PATCH net v2 2/3] net/nfc: llcp: take the skb reference before queueing it to the socket netdev-bot+sashiko
2026-11-30 1:22 ` [PATCH net v2 3/3] net/nfc: nci: do not hand a NULL skb to the nfc core Piyus Kedia
2026-12-01 2:15 ` netdev-bot+sashiko
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=20261130012208.1962491-3-piyus@iiitd.ac.in \
--to=piyus@iiitd.ac.in \
--cc=davem@davemloft.net \
--cc=david@ixit.cz \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=kapil@presceralabs.ai \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=oe-linux-nfc@lists.linux.dev \
--cc=pabeni@redhat.com \
--cc=willemb@google.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®