The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [BUG] net/nfc: a 16-bit truncation in nfc_llcp_allocate_snl() overruns the SNL response skb and BUGs the kernel
@ 2026-11-23  2:35 Piyus Kedia
  2026-11-23  2:35 ` [PATCH net 1/3] net/nfc: llcp: fix the 16-bit truncation that overruns the SNL response skb Piyus Kedia
  0 siblings, 1 reply; 3+ messages in thread
From: Piyus Kedia @ 2026-11-23  2:35 UTC (permalink / raw)
  To: David Heidelberg, netdev; +Cc: oe-linux-nfc, linux-kernel, Kapil Vaswani

Hello,

nfc_llcp_allocate_snl() accumulates its allocation size in a u16 while the
length it is given is a size_t.  An SNL PDU carrying enough SDREQ TLVs makes
that sum wrap, the response skb is allocated far too small, and
nfc_llcp_send_snl_sdres() then puts the full TLV list into it: skb_put()
trips skb_over_panic() and the kernel BUGs.

Reproduced on net.git main at af6737b90668 ("net: af_key: fix use-after-free
in dump_sp()"), 7.4.0-rc3-01164, with KASAN.  A patch is in the next mail of
this thread.


1. Root cause

net/nfc/llcp_commands.c, nfc_llcp_allocate_snl():

	struct sk_buff *skb;
	struct nfc_dev *dev;
	u16 size = 0;                           <-- u16
	...
	size += LLCP_HEADER_SIZE;
	size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
	size += tlv_length;                     <-- tlv_length is a size_t

	skb = alloc_skb(size, GFP_KERNEL);

The caller is net/nfc/llcp_core.c, nfc_llcp_recv_snl(), which walks the TLVs
of a received SNL PDU and builds one SDRES TLV for every SDREQ it finds:

	case LLCP_TLV_SDREQ:
		...
add_snl:
		sdp = nfc_llcp_build_sdres_tlv(tid, sap);
		if (sdp == NULL)
			goto exit;

		sdres_tlvs_len += sdp->tlv_len;
		hlist_add_head(&sdp->node, &llc_sdres_list);

and at the end passes the accumulated length:

	if (!hlist_empty(&llc_sdres_list))
		nfc_llcp_send_snl_sdres(local, &llc_sdres_list, sdres_tlvs_len);

The accounting on this side is exact - every entry added to the list also
adds its length - so the list and sdres_tlvs_len always agree.  What does
not agree is the u16 inside the allocator.

An inbound SDREQ TLV costs three bytes (type, length, tid, with an empty
service name), and the SDRES it produces is four, so sdres_tlvs_len is four
thirds of the PDU's TLV area.  About 48 KiB of SDREQs therefore pushes it
past 65535.  nfc_llcp_send_snl_sdres() then walks the list regardless:

	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);
	}

With 16400 SDREQs the accumulated length is 65600, which truncates to 64,
and the skb that should have held 64 KiB holds none of it.


2. How the PDU gets large enough

The NCI data path reassembles fragments before the LLCP layer sees them
(net/nfc/nci/data.c, nci_add_rx_data_frag()), so the PDU that reaches
nfc_llcp_recv_snl() is not limited to one NCI packet: a sequence of
PBF_CONT fragments accumulates into one skb, and the final fragment
delivers it.  The reproducer uses 64 KiB fragments.

The LLCP MIU negotiated for I-frames does not bound this path; SNL is
handled directly out of the receive skb by nfc_llcp_rx_skb(), and
nfc_llcp_recv_snl() walks whatever linear area that skb has.


3. KASAN reports

net.git main, 7.4.0-rc3-01164-gaf6737b90668, KASAN (full log attached as
kasan-nfc-snl-skb-over-panic-mainline-7.4-rc3.txt):

  skbuff: skb_over_panic: text:ffffffff87e134e6 len:382 put:4
          head:ffff88801d2016c0 data:ffff88801d2016c4 tail:0
  kernel BUG at net/core/skbuff.c:215!
  Oops: invalid opcode: 0000 [#1] SMP KASAN PTI
  Workqueue: events nfc_llcp_rx_work
  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

"put:4" is one SDRES TLV; the skb had no tailroom left for it.

The same reproducer on 7.3.1 gives the identical BUG at the line that
skb_over_panic() occupies in that tree (skbuff.c:214,
kasan-nfc-snl-skb-over-panic-7.3.1.txt).


4. Impact

A kernel BUG() in a workqueue context: the machine panics with
panic_on_oops, and otherwise the worker thread is killed with the LLCP
local's locks still held, which wedges the NFC stack.

The overflow itself never writes past the allocation, because skb_put()
checks before copying - this is a denial of service rather than memory
corruption.  It is reachable by whoever can deliver an SNL PDU to an NFC
device: an NFC peer over the air, or a local process that owns an NFC
device able to inject target-mode data.  No capability check stands between
a peer's PDU and this path.


5. Reproducer

nfc_llcp_snl_sdres_overflow.c in this directory (sent off-list on request;
see the note at the end).  It brings up a virtual NCI device, answers the
kernel's bring-up commands, activates target mode, then delivers an SNL PDU
of 16400 SDREQ TLVs across PBF_CONT fragments.  It BUGs the kernel within a
second on both trees.


6. Suggested fix

Hold the size in a size_t, so the value that reaches alloc_skb() is the one
the caller computed:

	-	u16 size = 0;
	+	size_t size = 0;

A request that large can now fail the allocation instead of wrapping, and
both callers of nfc_llcp_allocate_snl() already handle the IS_ERR() return.

The patch follows in this thread.  Checkpatch is clean.


7. What was and was not checked

Checked: the fault reproduces on net.git main under KASAN; the patched
kernel is clean on the same reproducer; ordinary LLCP traffic (SYMM, CONNECT,
I-frames, an SDREQ/SDRES exchange of a few TLVs) still works with the patch
applied; net/nfc/llcp_commands.c and net/nfc/llcp_core.c are byte-identical
between 7.3.1 and af6737b90668, so nothing in between changes this path.

Not checked: real hardware - all of this is on a virtual NCI device under
QEMU.  We have not tried to drive the path from an over-the-air peer.

No Fixes: tag is given: the u16 has been in nfc_llcp_allocate_snl() since
the SNL support was added, and our clone of net.git does not reach that
commit.  Happy to add the exact tag if you would like one.

This was found by AI-driven fuzzing built on top of syzkaller; its analysis
was then checked by hand against the code and every claim above was
reproduced.

Kapil Vaswani
Piyus Kedia

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

* [PATCH net 1/3] net/nfc: llcp: fix the 16-bit truncation that overruns the SNL response skb
  2026-11-23  2:35 [BUG] net/nfc: a 16-bit truncation in nfc_llcp_allocate_snl() overruns the SNL response skb and BUGs the kernel Piyus Kedia
@ 2026-11-23  2:35 ` Piyus Kedia
  2026-11-29 14:24   ` willemb
  0 siblings, 1 reply; 3+ messages in thread
From: Piyus Kedia @ 2026-11-23  2:35 UTC (permalink / raw)
  To: David Heidelberg, netdev
  Cc: oe-linux-nfc, linux-kernel, Kapil Vaswani, Piyus Kedia

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.

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 80a00938c..0a3d66e6c 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] 3+ messages in thread

* Re: [PATCH net 1/3] net/nfc: llcp: fix the 16-bit truncation that overruns the SNL response skb
  2026-11-23  2:35 ` [PATCH net 1/3] net/nfc: llcp: fix the 16-bit truncation that overruns the SNL response skb Piyus Kedia
@ 2026-11-29 14:24   ` willemb
  0 siblings, 0 replies; 3+ messages in thread
From: willemb @ 2026-11-29 14:24 UTC (permalink / raw)
  To: Piyus Kedia, David Heidelberg, netdev
  Cc: oe-linux-nfc, linux-kernel, Kapil Vaswani, Piyus Kedia

Piyus Kedia wrote:
> 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.

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

For all three patches in this series:

https://patchwork.kernel.org/project/netdevbpf/list/?series=1219102

Patches targeting net must include valid Fixes tags; please add Fixes tags  
to all three patches, include a cover letter for the series, and CC the  
maintainers listed by get_maintainer.pl before respinning.

Also do not play games with the In-Reply-To. Use a Closes: (or Link:) tag  
to point back to bug reports.

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

end of thread, other threads:[~2026-11-29 14:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23  2:35 [BUG] net/nfc: a 16-bit truncation in nfc_llcp_allocate_snl() overruns the SNL response skb and BUGs the kernel Piyus Kedia
2026-11-23  2:35 ` [PATCH net 1/3] net/nfc: llcp: fix the 16-bit truncation that overruns the SNL response skb Piyus Kedia
2026-11-29 14:24   ` willemb

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