Netdev List
 help / color / mirror / Atom feed
* [PATCH net v2 0/3] net/nfc: three peer-reachable skb bugs in LLCP and NCI
@ 2026-11-30  1:22 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
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Piyus Kedia @ 2026-11-30  1:22 UTC (permalink / raw)
  To: David Heidelberg, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Willem de Bruijn, Simon Horman
  Cc: oe-linux-nfc, netdev, linux-kernel, Kapil Vaswani

Three independent bugs in the NFC receive paths: a 16-bit truncation that
overruns an skb and panics, a use-after-free on a received frame, and a
NULL skb handed to the nfc core.  They touch three different files and have
three different causes, so they can be taken in any order; they are posted
as one series only because v1 went out as three numbered patches in three
threads, which is what the review objected to.

1/3 holds the SNL allocation size in a size_t instead of a u16, so a large
    SDREQ burst cannot wrap it and make skb_put() trip skb_over_panic().
2/3 takes the skb reference before sock_queue_rcv_skb() publishes the frame
    on the receive queue rather than after, and releases it on failure.
3/3 stops nci_add_rx_data_frag() forwarding a NULL skb to the nfc core
    after it has freed it on the reassembly error path.

Each was reproduced on net.git main under KASAN, and the patched kernel is
clean on the same reproducer with ordinary NFC traffic unaffected.  Found
by AI-driven fuzzing built on top of syzkaller; every analysis was then
checked by hand against the code, as each patch records.  The reproducers
are not posted - they go to anyone who asks for them.

Changes in v2, all of them from Willem de Bruijn's review:

 - a Fixes: tag on each patch.  v1 had none because the clone we were
   working from does not reach back that far; from a full one they are
     1/3  e0ae7bac06cc - gave nfc_llcp_allocate_snl() a size_t length to
          add into the u16 that was already there
     2/3  1727cf937401 - put skb_get() after sock_queue_rcv_skb() in both
          handlers
     3/3  122c195872e9 - added the target-mode exit that forwards the skb.
          The NULL itself dates to 2011, but until this commit the shared
          exit path only reached nci_data_exchange_complete(), which
          accepts one.
 - this cover letter, and one series rather than three numbered threads
 - the full get_maintainer.pl recipient list
 - a Link: to each bug report in the commit message, instead of threading
   the patch under the report with In-Reply-To

v1 of the three patches, each threaded under its own bug report:
  https://lore.kernel.org/netdev/20261123023548.3351531-2-piyus@iiitd.ac.in/
  https://lore.kernel.org/netdev/20261123023519.3350997-2-piyus@iiitd.ac.in/
  https://lore.kernel.org/netdev/20261123023446.3350387-2-piyus@iiitd.ac.in/

The review:
  https://lore.kernel.org/netdev/willemb.360aecefa3166@google.com/

Piyus Kedia (3):
  net/nfc: llcp: fix the 16-bit truncation that overruns the SNL
    response skb
  net/nfc: llcp: take the skb reference before queueing it to the socket
  net/nfc: nci: do not hand a NULL skb to the nfc core

 net/nfc/llcp_commands.c |  2 +-
 net/nfc/llcp_core.c     | 33 +++++++++++++++++++--------------
 net/nfc/nci/data.c      | 15 +++++++++++----
 3 files changed, 31 insertions(+), 19 deletions(-)


base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48
-- 
2.34.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH net v2 1/3] net/nfc: llcp: fix the 16-bit truncation that overruns the SNL response skb
  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 ` Piyus Kedia
  2026-12-01  2:15   ` netdev-bot+sashiko
  2026-11-30  1:22 ` [PATCH net v2 2/3] net/nfc: llcp: take the skb reference before queueing it to the socket Piyus Kedia
  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
  2 siblings, 1 reply; 7+ messages in thread
From: Piyus Kedia @ 2026-11-30  1:22 UTC (permalink / raw)
  To: David Heidelberg, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Willem de Bruijn, Simon Horman
  Cc: oe-linux-nfc, netdev, linux-kernel, Kapil Vaswani

nfc_llcp_allocate_snl() accumulates the allocation size in a u16 while the
length it is given is a size_t:

	u16 size = 0;
	...
	size += tlv_length;

	skb = alloc_skb(size, GFP_KERNEL);

nfc_llcp_recv_snl() builds one SDRES TLV for every SDREQ TLV of a received
SNL PDU and passes the total as tlv_length.  Each inbound SDREQ costs
three bytes - type, length, tid - and each SDRES it produces is four, so
the total is four thirds of the PDU and about 48 KiB of SDREQs drives it
past 65535.  The sum then wraps, alloc_skb() reserves a fraction of what
is needed, and nfc_llcp_send_snl_sdres() puts every entry of the list
into it anyway:

	hlist_for_each_entry_safe(sdp, n, tlv_list, node) {
		skb_put_data(skb, sdp->tlv, sdp->tlv_len);

skb_put() runs out of tailroom and trips skb_over_panic().  With 16400
SDREQs the accumulated length is 65600, which truncates to 64:

  skbuff: skb_over_panic: text:... len:382 put:4 head:... data:... tail:0
  kernel BUG at net/core/skbuff.c:215!
  RIP: 0010:skb_over_panic+0xcd/0xd0
   skb_put
   skb_put_data
   nfc_llcp_send_snl_sdres
   nfc_llcp_recv_snl
   nfc_llcp_rx_skb
   nfc_llcp_rx_work
   process_scheduled_works

Hold the size in a size_t so the value that reaches alloc_skb() is the one
the caller computed.  A request that large may now fail the allocation
instead, which the existing IS_ERR() path in both callers already handles.

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: e0ae7bac06cc ("NFC: llcp: Service Name Lookup SDRES aggregation")
Link: https://lore.kernel.org/netdev/20261123023548.3351531-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_commands.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/nfc/llcp_commands.c b/net/nfc/llcp_commands.c
index 80a00938c869..0a3d66e6cd59 100644
--- a/net/nfc/llcp_commands.c
+++ b/net/nfc/llcp_commands.c
@@ -542,7 +542,7 @@ static struct sk_buff *nfc_llcp_allocate_snl(struct nfc_llcp_local *local,
 {
 	struct sk_buff *skb;
 	struct nfc_dev *dev;
-	u16 size = 0;
+	size_t size = 0;
 
 	if (local == NULL)
 		return ERR_PTR(-ENODEV);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH net v2 2/3] net/nfc: llcp: take the skb reference before queueing it to the socket
  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-11-30  1:22 ` Piyus Kedia
  2026-12-01  2:15   ` 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
  2 siblings, 1 reply; 7+ messages in thread
From: Piyus Kedia @ 2026-11-30  1:22 UTC (permalink / raw)
  To: David Heidelberg, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Willem de Bruijn, Simon Horman
  Cc: oe-linux-nfc, netdev, linux-kernel, Kapil Vaswani

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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH net v2 3/3] net/nfc: nci: do not hand a NULL skb to the nfc core
  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-11-30  1:22 ` [PATCH net v2 2/3] net/nfc: llcp: take the skb reference before queueing it to the socket Piyus Kedia
@ 2026-11-30  1:22 ` Piyus Kedia
  2026-12-01  2:15   ` netdev-bot+sashiko
  2 siblings, 1 reply; 7+ messages in thread
From: Piyus Kedia @ 2026-11-30  1:22 UTC (permalink / raw)
  To: David Heidelberg, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Willem de Bruijn, Simon Horman
  Cc: oe-linux-nfc, netdev, linux-kernel, Kapil Vaswani

nci_add_rx_data_frag() frees the skb and clears the local pointer when it
cannot make room for the data accumulated so far, then falls through to the
exit path shared with the success case:

		if (skb_cow_head(skb, reassembly_len)) {
			pr_err("error adding room for accumulated rx data\n");

			kfree_skb(skb);
			skb = NULL;
			...
			err = -ENOMEM;
			goto exit;
		}
	...
exit:
	if (ndev->nfc_dev->rf_mode == NFC_RF_TARGET) {
		/* Data received in Target mode, forward to nfc core */
		err = nfc_tm_data_received(ndev->nfc_dev, skb);

In target mode that NULL reaches nfc_llcp_data_received(), whose own NULL
test is on the LLCP local and not on the skb, and __nfc_llcp_recv() then
dereferences it in pskb_may_pull():

  BUG: KASAN: null-ptr-deref in nfc_llcp_data_received
  Read of size 4 at addr 0000000000000070 by task kworker/u8:0/13
   skb_headlen
   pskb_may_pull_reason
   pskb_may_pull
   __nfc_llcp_recv
   nfc_llcp_data_received
   nfc_tm_data_received

Test the skb before forwarding it.  The other branch,
nci_data_exchange_complete(), already accepts a NULL skb and reports the
error to the pending data exchange, so only the target-mode call needs it -
and returning early instead would leave that exchange uncompleted.

A peer reaches the failing branch by growing the reassembly with PBF_CONT
fragments until the GFP_ATOMIC head expansion for the accumulated length
cannot be satisfied.

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: 122c195872e9 ("NFC: NCI: Forward data received in Target mode to nfc core")
Link: https://lore.kernel.org/netdev/20261123023446.3350387-1-piyus@iiitd.ac.in/
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
 net/nfc/nci/data.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/net/nfc/nci/data.c b/net/nfc/nci/data.c
index 4253edea5c8d..d7597fbd2a64 100644
--- a/net/nfc/nci/data.c
+++ b/net/nfc/nci/data.c
@@ -258,10 +258,17 @@ static void nci_add_rx_data_frag(struct nci_dev *ndev,
 
 exit:
 	if (ndev->nfc_dev->rf_mode == NFC_RF_TARGET) {
-		/* Data received in Target mode, forward to nfc core */
-		err = nfc_tm_data_received(ndev->nfc_dev, skb);
-		if (err)
-			pr_err("unable to handle received data\n");
+		/*
+		 * Data received in Target mode, forward to nfc core.  skb is
+		 * NULL when the reassembly above could not make room for the
+		 * accumulated data; there is nothing left to forward and the
+		 * core would dereference it.
+		 */
+		if (skb) {
+			err = nfc_tm_data_received(ndev->nfc_dev, skb);
+			if (err)
+				pr_err("unable to handle received data\n");
+		}
 	} else {
 		nci_data_exchange_complete(ndev, skb, conn_id, err);
 	}
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH net v2 1/3] net/nfc: llcp: fix the 16-bit truncation that overruns the SNL response skb
  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
  0 siblings, 0 replies; 7+ messages in thread
From: netdev-bot+sashiko @ 2026-12-01  2:15 UTC (permalink / raw)
  To: piyus
  Cc: david, davem, edumazet, kuba, pabeni, willemb, horms,
	oe-linux-nfc, netdev, linux-kernel, kapil

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 patch leaves a remotely triggerable leak on the path it now
  relies on.
- [Low] The Signed-off-by chain does not match the recorded authorship.

--

net/nfc: llcp: fix the 16-bit truncation that overruns the SNL response skb

This commit changes the size accumulator in nfc_llcp_allocate_snl() from
u16 to size_t. The SDRES length that nfc_llcp_recv_snl() computes is then
no longer truncated before it reaches alloc_skb(). This prevents the
skb_over_panic() in nfc_llcp_send_snl_sdres() when a peer sends an SNL PDU
with enough SDREQ TLVs to push the total past 65535.

Link: https://lore.kernel.org/netdev/20261123023548.3351531-1-piyus@iiitd.ac.in/

> Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
> Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>

[Severity: Low]
This isn't a bug, but the Signed-off-by chain doesn't match the recorded
authorship. The author is Piyus Kedia, but Kapil Vaswani's Signed-off-by
comes first and there is no Co-developed-by tag.

Documentation/process/submitting-patches.rst says the order of the
Signed-off-by tags should reflect the chronological history of the patch.
It also says every Co-developed-by must be immediately followed by the
co-author's Signed-off-by.

If Kapil Vaswani co-developed this, should there be a Co-developed-by tag
for him immediately before his Signed-off-by? If not, should the tags be
reordered, or that one dropped?

> diff --git a/net/nfc/llcp_commands.c b/net/nfc/llcp_commands.c
> index 80a00938c8696..0a3d66e6cd59f 100644
> --- a/net/nfc/llcp_commands.c
> +++ b/net/nfc/llcp_commands.c
> @@ -542,7 +542,7 @@ static struct sk_buff *nfc_llcp_allocate_snl(struct nfc_llcp_local *local,
>  {
>  	struct sk_buff *skb;
>  	struct nfc_dev *dev;
> -	u16 size = 0;
> +	size_t size = 0;

[Severity: High]
With size held in a size_t, the full peer-controlled tlv_length now
reaches alloc_skb(size, GFP_KERNEL). The commit message says:

  A request that large may now fail the allocation instead, which the
  existing IS_ERR() path in both callers already handles.

Is that true for the SDRES caller? If the allocation fails, does
nfc_llcp_send_snl_sdres() leak every entry on tlv_list?

net/nfc/llcp_commands.c:nfc_llcp_send_snl_sdres() {
	...
	skb = nfc_llcp_allocate_snl(local, tlvs_len);
	if (IS_ERR(skb))
		return PTR_ERR(skb);

	hlist_for_each_entry_safe(sdp, n, tlv_list, node) {
		skb_put_data(skb, sdp->tlv, sdp->tlv_len);

		hlist_del(&sdp->node);

		nfc_llcp_free_sdp_tlv(sdp);
	}
	...
}

That loop is the only place the entries are freed, and it only runs when
the allocation succeeds.

The caller ignores the return value. llc_sdres_list is a stack-local
HLIST_HEAD, so it goes out of scope with the entries still linked:

net/nfc/llcp_core.c:nfc_llcp_recv_snl() {
	...
exit:
	...
	if (!hlist_empty(&llc_sdres_list))
		nfc_llcp_send_snl_sdres(local, &llc_sdres_list, sdres_tlvs_len);
}

nfc_llcp_build_sdres_tlv() builds each entry. It allocates the
nfc_llcp_sdp_tlv struct with kzalloc_obj() and the TLV buffer with
nfc_llcp_build_tlv().

For the commit message's 16400-SDREQ example, that is two slab objects per
SDREQ, or roughly 1 MB per failed response. The 65600-byte skb head is a
costly-order allocation that can fail when memory is fragmented. A remote
peer can also send such SNL PDUs repeatedly.

The SDREQ caller does clean up. nfc_genl_llc_sdreq() in net/nfc/netlink.c
calls nfc_llcp_free_sdp_tlv_list() unconditionally at its free_list label,
after nfc_llcp_send_snl_sdreq() returns.

None of the later patches in this series change this path.

Could the IS_ERR() path in nfc_llcp_send_snl_sdres() free tlv_list?
Alternatively, nfc_llcp_recv_snl() could call
nfc_llcp_free_sdp_tlv_list(&llc_sdres_list) after the send. Either way, the
commit message would need updating to match.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130012208.1962491-1-piyus%40iiitd.ac.in

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH net v2 2/3] net/nfc: llcp: take the skb reference before queueing it to the socket
  2026-11-30  1:22 ` [PATCH net v2 2/3] net/nfc: llcp: take the skb reference before queueing it to the socket Piyus Kedia
@ 2026-12-01  2:15   ` netdev-bot+sashiko
  0 siblings, 0 replies; 7+ messages in thread
From: netdev-bot+sashiko @ 2026-12-01  2:15 UTC (permalink / raw)
  To: piyus
  Cc: david, davem, edumazet, kuba, pabeni, willemb, horms,
	oe-linux-nfc, netdev, linux-kernel, kapil

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

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

- [Critical] The patch does not close the teardown use-after-free that
  the commit message names ('a concurrent recvmsg() - or the socket's…
- [Low] The sign-off chain does not match the authorship.

--

net/nfc: llcp: take the skb reference before queueing it to the socket

This patch moves the skb_get() in nfc_llcp_recv_ui() and
nfc_llcp_recv_hdlc() so it runs before sock_queue_rcv_skb(). It also drops
the extra reference with kfree_skb() when queueing fails. The goal is to
stop a concurrent recvmsg() or socket teardown from freeing the skb before
the reference is taken.

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>

[Severity: Low]
This isn't a bug, but is the sign-off chain right here?

Piyus Kedia is the author and the submitter, so his Signed-off-by is
correctly last. Kapil Vaswani's Signed-off-by comes before it, and there is
no Co-developed-by tag.

Documentation/process/submitting-patches.rst makes two points here:

- SoBs that follow the author's SoB come from people who handled and
  transported the patch.
- Every Co-developed-by must be immediately followed by that co-author's
  Signed-off-by.

If Kapil Vaswani co-developed the fix, could a "Co-developed-by: Kapil
Vaswani <kapil@presceralabs.ai>" line go right before his Signed-off-by?
If he found or analysed the bug instead, would Reported-by or Suggested-by
fit better?

> diff --git a/net/nfc/llcp_core.c b/net/nfc/llcp_core.c
> index 74bf817007cf2..e988c95cc1722 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);

[Severity: Critical]
Is there still a use-after-free here, now on the struct sock instead of the
skb?

After skb_get() the skb has two users. sock_queue_rcv_skb() then calls
skb_set_owner_r() from __sock_queue_rcv_skb():

net/core/sock.c:__sock_queue_rcv_skb() {
    ...
	skb->dev = NULL;
	skb_set_owner_r(skb, sk);
    ...
}

skb_set_owner_r() sets skb->sk = sk and skb->destructor = sock_rfree, and
charges truesize to sk_rmem_alloc. It does not take a reference on sk.

The rx path keeps its own skb reference. It drops that reference only
after the handler has already called nfc_llcp_sock_put():

nfc_llcp_rx_work()
    nfc_llcp_rx_skb()
        nfc_llcp_recv_ui()
            skb_get(skb)
            sock_queue_rcv_skb()
            nfc_llcp_sock_put()
    ...
    kfree_skb(local->rx_pending)

nfc_llcp_recv_agf() follows the same pattern, calling kfree_skb(new_skb)
after nfc_llcp_rx_skb(). A remote peer can repeat this for every sub-frame
in one AGF PDU.

Suppose close() runs at the same time. llcp_sock_release() calls
sock_orphan() and then sock_put(). When the last socket reference is
dropped, sk_free() runs __sk_destruct() -> llcp_sock_destruct():

net/nfc/llcp_sock.c:llcp_sock_destruct() {
    ...
	skb_queue_purge(&sk->sk_receive_queue);
    ...
}

The purge only takes the skb from two users to one, so sock_rfree() does
not run. __sk_destruct() then goes on to sk_prot_free(). sk_free() waits
only on sk_wmem_alloc, not on sk_rmem_alloc.

Later the rx path calls kfree_skb() on its reference. That drops the last
reference, and sock_rfree() runs on the freed socket:

net/core/sock.c:sock_rfree() {
	struct sock *sk = skb->sk;
	unsigned int len = skb->truesize;

	atomic_sub(len, &sk->sk_rmem_alloc);
	sk_mem_uncharge(sk, len);
}

nfc_llcp_recv_ui() never takes lock_sock(). close() can therefore finish
at any point between nfc_llcp_sock_get() and nfc_llcp_sock_put().

The I frame path in nfc_llcp_recv_hdlc() has the same shape. The window
opens there once the handler calls release_sock() and
nfc_llcp_sock_put(), while nfc_llcp_rx_work() or nfc_llcp_recv_agf() still
holds the skb.

A second ordering ends the same way. recvmsg() dequeues and frees the skb
(two users down to one, so no destructor runs), and close() then frees the
socket.

The commit message cites the raw-socket path as the model:

    if (sock_queue_rcv_skb(sk, nskb))
            kfree_skb(nskb);

In that path nskb is a private skb_clone() and the caller keeps no
reference to it, so the queue really owns it. Here the caller keeps a
second reference to an skb that is now owned by the socket.

Would queueing a clone avoid this? Another option would be to hand the
single reference to the queue and not free it in the caller.

The commit message says it closes the race with "the socket's own
teardown" and that "the patched kernel is clean". Yet the same teardown
race still seems to reach freed memory through sock_rfree().

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130012208.1962491-1-piyus%40iiitd.ac.in

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH net v2 3/3] net/nfc: nci: do not hand a NULL skb to the nfc core
  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
  0 siblings, 0 replies; 7+ messages in thread
From: netdev-bot+sashiko @ 2026-12-01  2:15 UTC (permalink / raw)
  To: piyus
  Cc: david, davem, edumazet, kuba, pabeni, willemb, horms,
	oe-linux-nfc, netdev, linux-kernel, kapil

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The Signed-off-by chain in the commit "net/nfc: nci: do not hand
  a NULL skb to the nfc core" does not match the authorship.

--

net/nfc: nci: do not hand a NULL skb to the nfc core

This patch adds a NULL check on the skb in the target-mode exit path of
nci_add_rx_data_frag(). Without it, the skb that was freed and set to NULL
after a failed skb_cow_head() can be passed to nfc_tm_data_received(). That
call path then dereferences the NULL skb in __nfc_llcp_recv().

Link: https://lore.kernel.org/netdev/20261123023446.3350387-1-piyus@iiitd.ac.in/

> Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
> Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>

[Severity: Low]
This isn't a bug, but could the trailers be checked? The patch author is
Piyus Kedia, but the first Signed-off-by belongs to Kapil Vaswani, and the
patch has no Co-developed-by tag.

Documentation/process/submitting-patches.rst reads the Signed-off-by chain
in the order people handled the patch. With a non-author's SoB placed ahead
of the author's, the chain says Kapil handled the patch before its author
did, which doesn't match the From: line.

If Kapil co-developed the fix, should there be a
"Co-developed-by: Kapil Vaswani <kapil@presceralabs.ai>" line directly
before his Signed-off-by? If he only handled or forwarded the patch, should
his Signed-off-by go after the author's?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130012208.1962491-1-piyus%40iiitd.ac.in

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-12-01  2:15 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH net v2 2/3] net/nfc: llcp: take the skb reference before queueing it to the socket Piyus Kedia
2026-12-01  2:15   ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox