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 Signed-off-by: Piyus Kedia --- 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