nfc: llcp: reject PDUs shorter than the LLCP header
Subsystems (4)
PATCH v2 1/1 nfc: llcp: reject PDUs shorter than the LLCP header
Every LLCP PDU begins with a two-byte header (DSAP/SSAP + PTYPE), but the receive path never checked that a frame is at least LLCP_HEADER_SIZE bytes before parsing it.
A peer LLCP PDU travels: NFC-DEP frame -> nfc_tm_data_received() (target / NCI path) or nfc_llcp_recv() (initiator data-exchange callback) -> __nfc_llcp_recv() -> rx_work -> nfc_llcp_rx_skb() -> nfc_llcp_recv_connect(). For a CONNECT (or CC) PDU nfc_llcp_recv_connect() computes
tlv_array_len = skb->len - LLCP_HEADER_SIZE;
as a size_t and hands it to the TLV walk. When skb->len is 0 or 1 the subtraction wraps to a huge value and the walk runs far past the skb, causing an out-of-bounds read; nfc_llcp_ptype()/nfc_llcp_ssap() likewise read pdu->data[1] for such a short frame.
A nearby NFC device can reach this without authentication; LLCP link activation happens automatically after NFC-DEP.
Reject PDUs shorter than the LLCP header in __nfc_llcp_recv(), the common choke point shared by both the target (nfc_llcp_data_received()) and initiator (nfc_llcp_recv()) receive paths, so a short skb is freed before the rx_work worker is scheduled.
Reproduced with a KFENCE out-of-bounds read via /dev/virtual_nci on linux-next.
Found by 0sec (https://0sec.ai) using automated source analysis.
---
v2: move the check into __nfc_llcp_recv() so a short skb is dropped
before the rx_work worker is scheduled (Vadim Fedorenko), which also covers the initiator nfc_llcp_recv() path. Reword the commit message (drop the "same guard as AGF" wording) and add a KFENCE reproduction note.
net/nfc/llcp_core.c | 5 +++++ 1 file changed, 5 insertions(+)
net/nfc/llcp_core.c +5 -0
@@ -1565,6 +1565,11 @@ static void nfc_llcp_rx_work(struct work_struct *work)static void __nfc_llcp_recv (struct nfc_llcp_local *local,struct sk_buff *skb) { +if (skb->len <LLCP_HEADER_SIZE ) { +kfree_skb (skb); +return ; + } + local->rx_pending = skb;timer_delete (&local->link_timer);schedule_work (&local->rx_work);
Signature · 1 line
2.43.0
Quoted 3 lines · On Mon, 13 Jul 2026 17:58:48 +0200 Doruk Tan Ozturk <[email protected]> wrote:
Every LLCP PDU begins with a two-byte header (DSAP/SSAP + PTYPE), but the receive path never checked that a frame is at least
LLCP_HEADER_SIZEbytes before parsing it.
Is there a similar problem with non-linear skb?
Maybe they can't get into this code, but who knows what can happen with unusual configs.
David
Quoted 56 lines
A peer LLCP PDU travels: NFC-DEP frame ->
nfc_tm_data_received()(target / NCI path) ornfc_llcp_recv()(initiator data-exchange callback) ->__nfc_llcp_recv()->rx_work->nfc_llcp_rx_skb()->nfc_llcp_recv_connect(). For a CONNECT (or CC) PDUnfc_llcp_recv_connect()computestlv_array_len = skb->len - LLCP_HEADER_SIZE;as a
size_tand hands it to the TLV walk. Whenskb->lenis 0 or 1 the subtraction wraps to a huge value and the walk runs far past the skb, causing an out-of-bounds read; nfc_llcp_ptype()/nfc_llcp_ssap() likewise read pdu->data[1] for such a short frame.A nearby NFC device can reach this without authentication; LLCP link activation happens automatically after NFC-DEP.
Reject PDUs shorter than the LLCP header in
__nfc_llcp_recv(), the common choke point shared by both the target (nfc_llcp_data_received()) and initiator (nfc_llcp_recv()) receive paths, so a short skb is freed before therx_workworker is scheduled.Reproduced with a KFENCE out-of-bounds read via
/dev/virtual_ncion linux-next.Found by 0sec (https://0sec.ai) using automated source analysis.
Fixes d646960f7986 ("NFC: Initial LLCP support")Assisted-by 0sec:claude-opus-4-8Signed-off-by Doruk Tan Ozturk <[email protected]>---
v2: move the check into__nfc_llcp_recv()so a short skb is dropped
before therx_workworker is scheduled (Vadim Fedorenko), which also covers the initiatornfc_llcp_recv()path. Reword the commit message (drop the "same guard as AGF" wording) and add a KFENCE reproduction note.net/nfc/llcp_core.c | 5 +++++ 1 file changed, 5 insertions(+)net/nfc/llcp_core.c +5
-0@@ -1565,6 +1565,11 @@ static void nfc_llcp_rx_work(struct work_struct *work)static void __nfc_llcp_recv (struct nfc_llcp_local *local,struct sk_buff *skb) { +if (skb->len <LLCP_HEADER_SIZE ) { +kfree_skb (skb); +return ; + } + local->rx_pending = skb;timer_delete (&local->link_timer);schedule_work (&local->rx_work);
Quoted 3 lines
Is there a similar problem with non-linear skb?
Maybe they can't get into this code, but who knows what can happen with unusual configs.
Good question. Today every skb that reaches __nfc_llcp_recv() is
linear: the target path (nci_rx_data_packet -> nci_add_rx_data_frag -> nfc_tm_data_received) and the initiator path (nfc_data_exchange -> nfc_llcp_recv) both build the frame with alloc_skb()/nci_skb_alloc() plus skb_put()/skb_put_data(), and NCI reassembly uses skb_cow_head() and skb_push() into the linear area. Nothing on the NFC receive side attaches page frags or a frag_list, so skb->len == skb_headlen() and the v2 skb->len test was in fact sufficient for the in-tree drivers.
But relying on that is fragile: the parser reads the header out of the linear area (pdu->data[0]/data[1]) while skb->len is the total length, so a non-linear skb with a short linear head would slip past a skb->len test and still over-read the linear buffer. pskb_may_pull() is the right guard here -- it also covers the non-linear case, and it matches how the sibling NCI and HCI receive paths already validate their headers.
I will send a v3 that uses:
if (!pskb_may_pull(skb, LLCP_HEADER_SIZE)) {
kfree_skb(skb);
return;
}That is strictly stronger than the v2 check and does not reject any valid frame -- pskb_may_pull() pulls the two header bytes into the linear area when needed.
Thanks,
Doruk
Quoted 23 lines · On Tue, 14 Jul 2026 18:46:29 +0200 Doruk Tan Ozturk <[email protected]> wrote:
Quoted 3 lines
Is there a similar problem with non-linear skb?
Maybe they can't get into this code, but who knows what can happen with unusual configs.Good question. Today every skb that reaches
__nfc_llcp_recv()is
linear: the target path (nci_rx_data_packet->nci_add_rx_data_frag->nfc_tm_data_received) and the initiator path (nfc_data_exchange->nfc_llcp_recv) both build the frame with alloc_skb()/nci_skb_alloc() plus skb_put()/skb_put_data(), and NCI reassembly usesskb_cow_head()andskb_push()into the linear area. Nothing on the NFC receive side attaches page frags or afrag_list, soskb->len==skb_headlen()and the v2skb->lentest was in fact sufficient for the in-tree drivers.But relying on that is fragile: the parser reads the header out of the linear area (pdu->data[0]/data[1]) while
skb->lenis the total length, so a non-linear skb with a short linear head would slip past askb->lentest and still over-read the linear buffer.pskb_may_pull()is the right guard here -- it also covers the non-linear case, and it matches how the sibling NCI and HCI receive paths already validate their headers.I will send a v3 that uses:
Quoted 8 lines
if (!pskb_may_pull(skb, LLCP_HEADER_SIZE)) { kfree_skb(skb); return; }That is strictly stronger than the v2 check and does not reject any valid frame --
pskb_may_pull()pulls the two header bytes into the linear area when needed.
Does that help any code further down the rx path?
It might be better to drop non-linear packets with a comment that they aren't expected to happen.
David
Quoted 3 lines
Thanks,
Doruk