[BUG] net/nfc: a 16-bit truncation in nfc_llcp_allocate_snl() overruns the SNL response skb and BUGs the kernel
From: Piyus Kedia
Date: Sun Nov 22 2026 - 21:37:34 EST
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