[BUG] net/nfc: LLCP takes the skb reference after queueing it to the socket, so a concurrent recvmsg() frees it first

From: Piyus Kedia

Date: Sun Nov 22 2026 - 21:36:10 EST


Hello,

nfc_llcp_recv_ui() and nfc_llcp_recv_hdlc() queue a received frame to an
LLCP socket and only then call skb_get() on it. Between those two steps the
skb is on sk_receive_queue at refcount one and a concurrent recvmsg() can
dequeue and free it, so the skb_get() increments freed memory.

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


1. Root cause

net/nfc/llcp_core.c, nfc_llcp_recv_ui() (and the same shape in
nfc_llcp_recv_hdlc()):

/* 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 {
pr_err("Receive queue is full\n");
}

sock_queue_rcv_skb() does not take a reference of its own: __sock_queue_rcv_skb()
calls skb_set_owner_r() and skb_queue_tail(), taking over the reference the
caller holds. The moment it returns, the skb is visible on the receive
queue and whoever reads the socket owns it. A reader that gets there first
dequeues and frees it, and the skb_get() that was meant to keep it alive
then operates on a freed object:

refcount_t: addition on 0; use-after-free.

The comment above the call describes the intent correctly - the socket layer
will free it, so a reference is needed - but the reference has to exist
before the skb is published, not after.

The intended final state is unchanged by taking it earlier: the caller
(nfc_llcp_rx_skb(), via nfc_llcp_rx_work() or nfc_llcp_recv_agf()) drops its
own reference with kfree_skb() when the dispatch returns, leaving the socket
queue holding the last one.


2. Both sites, and why the aggregated-frame path finds it so easily

nfc_llcp_recv_ui() net/nfc/llcp_core.c:930 (SOCK_DGRAM, UI frames)
nfc_llcp_recv_hdlc() net/nfc/llcp_core.c:1144 (SOCK_STREAM, I frames)

Both are reached from nfc_llcp_rx_skb(). nfc_llcp_recv_agf() splits an
aggregated frame and dispatches each sub-PDU through nfc_llcp_rx_skb() in a
loop, freeing each one immediately afterwards:

new_skb = nfc_alloc_recv_skb(pdu_len, GFP_KERNEL);
...
skb_put_data(new_skb, skb->data, pdu_len);

nfc_llcp_rx_skb(local, new_skb);

kfree_skb(new_skb);

so one AGF PDU delivers many frames to the socket back to back, which is
what makes the window easy to hit from userspace.

The same file already uses the correct ownership rule for the raw-socket
copy a few hundred lines above:

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


3. KASAN reports

net.git main, 7.4.0-rc3-01164-gaf6737b90668, KASAN.

The I-frame site (kasan-nfc-llcp-hdlc-skb-uaf-mainline-7.4-rc3.txt):

BUG: KASAN: slab-use-after-free in nfc_llcp_rx_skb+0x17b3/0x34a0
Write of size 4 at addr ffff888055abe5e4 by task kworker/1:2/56
kasan_check_range
nfc_llcp_rx_skb <-- __refcount_inc, skb_get, nfc_llcp_recv_hdlc
nfc_llcp_rx_skb <-- the AGF loop's inner dispatch
process_scheduled_works

(rx_skb+0x17b3 resolves to skb_get at include/linux/skbuff.h:2004,
inlined by nfc_llcp_recv_hdlc at net/nfc/llcp_core.c:1144.)

The UI-frame site (kasan-nfc-llcp-ui-skb-uaf-mainline-7.4-rc3.txt):

BUG: KASAN: slab-use-after-free in nfc_llcp_rx_skb+0x3a5/0x34a0
Write of size 4 at addr ffff888055973724 by task kworker/1:3/2203

(rx_skb+0x3a5 resolves to skb_get at include/linux/skbuff.h:2004,
inlined by nfc_llcp_recv_ui at net/nfc/llcp_core.c:930.)

On 7.3.1, where net/ is built without inlining, the same two reproducers
name the refcount helpers directly - once on the increment and once on the
matching decrement, depending on which side of the race loses:

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 __refcount_sub_and_test

(kasan-nfc-llcp-{hdlc,ui}-skb-uaf-7.3.1.txt.)


4. Impact

A four-byte increment on freed slab memory, with the value and the timing
controlled by an unprivileged reader of the LLCP socket: it is the same
primitive as any refcount-on-freed-object bug, and the freed skb can be
reallocated between the free and the increment. The decrement side can also
land on a reallocated object, which is what the __refcount_sub_and_test
report above shows.

Reachable by whoever can deliver LLCP frames to the device (an NFC peer, or
a local process owning an NFC device) together with any local process able
to read the corresponding LLCP socket.


5. Reproducers

nfc_llcp_hdlc_skb_uaf.c and nfc_llcp_ui_skb_uaf.c in this directory (sent
off-list on request). Each brings up a virtual NCI device, binds an LLCP
socket, and then races frame delivery against recvmsg(). Both fault within
a few seconds on both trees.


6. Suggested fix

Take the reference before the skb is published, and drop it again if the
queueing fails - sock_queue_rcv_skb() frees nothing on any of its failure
paths, so the extra kfree_skb() balances exactly the reference taken here:

+ skb_get(skb);
+ if (sock_queue_rcv_skb(&llcp_sock->sk, skb)) {
+ pr_err("Receive queue is full\n");
+ kfree_skb(skb);
+ }

at both sites. The patch follows in this thread. Checkpatch is clean.


7. What was and was not checked

Checked: both sites fault on net.git main under KASAN; the patched kernel is
clean on both reproducers; the refcount accounting with the patch applied
ends in the same state as the original intent (socket queue holds the last
reference, the dispatcher's kfree_skb() drops its own); ordinary LLCP
datagram and stream traffic still delivers with the patch applied;
net/nfc/llcp_core.c is byte-identical between 7.3.1 and af6737b90668.

Not checked: real hardware; an over-the-air peer.

No Fixes: tag is given: both sites date from the original LLCP receive
implementation and our clone of net.git does not reach that commit. Happy
to add exact tags if you would like them.

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