* [BUG] Bluetooth: L2CAP: slab-use-after-free reading chan->sdu->len after the receive callback has consumed the SDU (seen on 7.3.1)
@ 2026-11-12 17:38 Piyus Kedia
2026-11-12 17:38 ` [PATCH] Bluetooth: L2CAP: clear the SDU before the receive callback Piyus Kedia
0 siblings, 1 reply; 7+ messages in thread
From: Piyus Kedia @ 2026-11-12 17:38 UTC (permalink / raw)
To: Marcel Holtmann, Luiz Augusto von Dentz, linux-bluetooth
Cc: linux-kernel, Kapil Vaswani
Hi Marcel and Luiz,
We would like to report a bug in l2cap_le_rx_credits.
Summary
=======
Affected: reproduced directly on net/main at a2fd526d6442, i.e. on
7.4.0-rc1, as well as on the 7.3.1 tree it was first seen on. The four
relevant code regions are byte-identical in both and in mainline at
79eb6679ef6a, and the code is present unchanged in every v7.3-rc tag we
have.
Problem: l2cap_ecred_data_rcv() hands a completed multi-frame SDU to the
channel's receive callback while chan->sdu still points at it, and only
clears chan->sdu after the call returns. The callback takes ownership and
may free the skb; l2cap_ecred_recv() then updates the credits, and
l2cap_le_rx_credits() reads chan->sdu->len.
BUG: KASAN: slab-use-after-free in l2cap_le_rx_credits
net/bluetooth/l2cap_core.c:576 [inline]
BUG: KASAN: slab-use-after-free in l2cap_chan_le_send_credits+0xa8/0x3e0
net/bluetooth/l2cap_core.c:6811
Read of size 4 at addr ffff88800e134070 by task kworker/u21:2/11352
Workqueue: hci0 hci_rx_work
The read is 112 bytes into a freed 240-byte skbuff_head_cache object.
Offset 112 is sk_buff.len and sizeof(struct sk_buff) is 240, so the read is
exactly chan->sdu->len.
Conditions: a 6LoWPAN channel, because its receive callback frees the skb
outright. An administrator enables 6LoWPAN through
/sys/kernel/debug/bluetooth/6lowpan_enable; after that bt_6lowpan_listen()
listens on L2CAP_PSM_IPSP and a connected LE peer triggers this by sending
one SDU across more than one K-frame.
Reproducer: tested, not included here - see section 5.
Fix: sent as a separate patch in this thread, verified at runtime on
net/main - the reproducer faults on the unpatched kernel, three runs, and
runs clean on the patched one, two runs. Section 7 has the detail.
This bug was found with AI assistance.
1. Root cause
=============
Three functions are involved. The reassembly completes here:
/* net/bluetooth/l2cap_core.c, l2cap_ecred_data_rcv() */
append_skb_frag(chan->sdu, skb, &chan->sdu_last_frag);
skb = NULL;
if (chan->sdu->len == chan->sdu_len) {
err = l2cap_ecred_recv(chan, chan->sdu);
if (!err) {
chan->sdu = NULL;
chan->sdu_last_frag = NULL;
chan->sdu_len = 0;
}
}
chan->sdu is still set when l2cap_ecred_recv() is called, and is cleared
only after it returns. Inside:
/* l2cap_ecred_recv() */
/* Wait recv to confirm reception before updating the credits */
err = chan->ops->recv(chan, skb);
if (err < 0 && chan->rx_avail != -1) {
...
return err;
}
/* Update credits whenever an SDU is received */
l2cap_chan_le_send_credits(chan);
and l2cap_chan_le_send_credits() calls
/* l2cap_le_rx_credits() */
size_t sdu_len = chan->sdu ? chan->sdu->len : 0;
So in the window between chan->ops->recv() taking the skb and the caller
clearing chan->sdu, chan->sdu is dereferenced.
Only the multi-frame path is affected. The single-frame path returns
l2cap_ecred_recv(chan, skb) with chan->sdu NULL, so l2cap_le_rx_credits()
reads a zero sdu_len there. The fix makes the fragmented path behave the
same way.
2. Why it is a use-after-free and not just a stale read
=======================================================
The 6LoWPAN callback frees the skb on the success path:
/* net/bluetooth/6lowpan.c, recv_pkt() */
if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) {
kfree_skb(local_skb);
goto drop;
}
...
consume_skb(local_skb);
consume_skb(skb);
...
return NET_RX_SUCCESS;
chan_recv_cb() returns that unchanged, i.e. 0, so l2cap_ecred_recv() takes
neither the error branch nor any ownership back, and goes straight on to the
credit update. That makes the read unconditional and deterministic rather
than a race, which is why KASAN catches it every time on this path.
The ownership rule the code relies on is consistent across the callbacks:
on success the callback owns the skb, on failure the caller still does.
l2cap_sock_recv_cb() queues the skb with __sock_queue_rcv_skb() and leaves
it alone on error, which is why l2cap_ecred_data_rcv()'s error path can do
kfree_skb(chan->sdu). The fix keeps that rule.
We note, without having demonstrated it, that the socket path looks exposed
to the same bug as a race rather than a certainty: l2cap_sock_recv_cb()
releases the socket lock before returning, so once the SDU is on
sk_receive_queue a concurrent recvmsg() can free it before
l2cap_le_rx_credits() reads chan->sdu->len. KASAN would not usually catch
that because the skb is normally still queued. The fix covers it either
way, since it stops the caller from holding a pointer it has given away.
3. KASAN reports
================
Quoted below, not attached - this mail carries no attachments. We have
four logs in all, from four kernels; the two that matter are here. The
timestamps and KASAN's speculative "?" frames are dropped for width,
nothing else is edited, and the unedited logs are available on request.
First, net/main at a2fd526d6442 - the tree this patch is against. The
LOWRUN line is the reproducer's own marker, printed 185 ms before the
report:
LOWRUN sdu
==================================================================
BUG: KASAN: slab-use-after-free in l2cap_chan_le_send_credits+0x86/0x1d0
Read of size 4 at addr ffff888011a76a70 by task kworker/u21:0/352
CPU: 1 UID: 0 PID: 352 Comm: kworker/u21:0 Not tainted
7.4.0-rc1-ga2fd526d6442 #2 PREEMPT(lazy)
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1
04/01/2014
Workqueue: hci0 hci_rx_work
Call Trace:
<TASK>
dump_stack_lvl+0x55/0x70
print_report+0xcb/0x5e0
kasan_report+0xb8/0xf0
l2cap_chan_le_send_credits+0x86/0x1d0
l2cap_ecred_recv+0x5a/0x90
l2cap_recv_frame+0xd84/0x3f30
l2cap_recv_acldata+0x702/0x710
hci_rx_work+0x1d8/0x430
process_one_work+0x3de/0x780
worker_thread+0x287/0x4c0
kthread+0x1af/0x1f0
ret_from_fork+0x2ea/0x410
Second, and the one to read closely: the reproducer on an unmodified 7.3.1
kernel, "Not tainted 7.3.1 #2", with no local changes to net/bluetooth at
all - no modified sources and no added compiler flags. It carries the
freeing stack as well, and shows the inline chain explicitly:
==================================================================
BUG: KASAN: slab-use-after-free in l2cap_le_rx_credits
net/bluetooth/l2cap_core.c:576 [inline]
BUG: KASAN: slab-use-after-free in l2cap_chan_le_send_credits+0xa8/0x3e0
net/bluetooth/l2cap_core.c:6811
Read of size 4 at addr ffff88800e134070 by task kworker/u21:2/11352
CPU: 1 UID: 0 PID: 11352 Comm: kworker/u21:2 Not tainted 7.3.1 #2
Workqueue: hci0 hci_rx_work
Call Trace:
l2cap_le_rx_credits net/bluetooth/l2cap_core.c:576 [inline]
l2cap_chan_le_send_credits+0xa8/0x3e0 net/bluetooth/l2cap_core.c:6811
l2cap_ecred_recv+0x63/0x120 net/bluetooth/l2cap_core.c:6873
l2cap_ecred_data_rcv net/bluetooth/l2cap_core.c:6976 [inline]
l2cap_data_channel net/bluetooth/l2cap_core.c:7028 [inline]
l2cap_recv_frame+0x6c3c/0x9650 net/bluetooth/l2cap_core.c:7162
l2cap_recv_acldata+0x6fd/0xe60 net/bluetooth/l2cap_core.c:7902
hci_acldata_packet net/bluetooth/hci_core.c:3865 [inline]
hci_rx_work+0x3c0/0xbe0 net/bluetooth/hci_core.c:4092
Freed by task 11352:
recv_pkt net/bluetooth/6lowpan.c
chan_recv_cb+0x88/0x150 net/bluetooth/6lowpan.c:367
l2cap_ecred_recv+0x61/0x160 net/bluetooth/l2cap_core.c:6864
l2cap_ecred_data_rcv net/bluetooth/l2cap_core.c:6976 [inline]
The buggy address belongs to the object at ffff88800e134000
which belongs to the cache skbuff_head_cache of size 240
The buggy address is located 112 bytes inside of
freed 240-byte region
==================================================================
The freeing task and the reading task are the same rx worker, two frames
apart in the same call chain, which is the whole bug.
The first report comes from a kernel where our coverage harness appends
compiler flags to net/bluetooth/Makefile -
subdir-ccflags-y += -fno-inline -fno-inline-functions \
-finstrument-functions-after-inlining \
-mllvm -sanitizer-coverage-prune-blocks=0
- so there l2cap_le_rx_credits() is a real frame at :576 rather than
inlined. No .c or .h file under net/bluetooth is modified in either
kernel. That the same bug appears with and without those flags is itself
evidence that they change inlining and not semantics.
4. Impact
=========
Stating only what we verified, and our reading of
Documentation/process/threat-model.rst - please correct it:
- Enabling 6LoWPAN requires root, through a debugfs file. By
threat-model.rst that alone is "excess of initial privileges", so our
reproducer's own path does not cross a boundary.
- What we think does: once an administrator has enabled it - which is how
IPv6-over-BLE is meant to be used, not a debug-only setting -
bt_6lowpan_listen() listens on L2CAP_PSM_IPSP, and the trigger is then
an ordinary inbound SDU from a connected LE peer. Fragmenting an SDU
across two K-frames is normal protocol behaviour, needs no local
privilege, and is the remote's choice. So on a configured system this
is reachable over the air.
- The read itself is 4 bytes of a freed 240-byte skbuff_head_cache
object, and its value feeds a credit calculation, so the visible
consequence is a wrong credit count rather than a leak of the bytes.
We have not attempted to turn the freed-object reuse into anything
further, and we are not claiming an exploit.
We did not observe a crash without KASAN; the read is small and lands in
recycled slab memory.
5. Reproducer
=============
Tested, and deliberately not included. security-bugs.rst says an
AI-assisted finding must be treated as public but its reproducer must not
be, so we are happy to send it to any maintainer who asks.
So you can judge whether to ask for it: a single C program, no external
dependencies and no Bluetooth hardware - it stands up a virtual controller
and answers it, so it runs on any KASAN kernel with CONFIG_BT_6LOWPAN=y and
CONFIG_BT_HCIVHCI=y. KASAN fires inside hci_rx_work.
We have deliberately left out the sequence it drives. Section 1 gives the
three functions and the window between them, and section 2 gives the
ownership rule they break, which is what the fix has to be judged against.
What does bound the scope, because it limits who is affected: the channel's
receive callback has to be one that frees the skb, which is the 6LoWPAN one;
and the SDU has to arrive in more than one frame, since the single-frame
path already passes chan->sdu NULL.
In fairness about what it is: this is the harness's own exploratory program,
not a minimised reproducer. It is about 1100 lines and still carries some
dead scaffolding from the search - a few unused helpers and a table of SDU
shapes it no longer iterates - which is why it compiles with warnings. We
kept it exactly as it ran rather than restructure a program we had already
verified. If you would rather have a minimised one, say so and we will cut
it down and re-verify.
6. Suggested fix
================
Clear the reassembly state before handing the SDU to the callback, and
release the SDU on the spot if the callback refused it - which is what the
error path's kfree_skb(chan->sdu) did before. The completion path then
returns rather than falling into that error path, because the reassembly
state is already clear there and the kfree_skb(chan->sdu) it would reach
has nothing left to free. The patch is sent separately, against net/main
at a2fd526d6442.
This also makes l2cap_le_rx_credits() see chan->sdu == NULL and so compute
sdu_len 0 during the credit update, which is the correct value: the SDU has
just been delivered and no partial SDU is pending. It is what the
single-frame path already produces.
We considered instead making l2cap_ecred_recv() not update the credits, or
passing the length down rather than re-reading it from chan->sdu, and
preferred this because it keeps the ownership rule in one place and does not
change the credit arithmetic.
7. What was and was not checked
===============================
Verified at runtime on net/main itself. The kernel was built from
a2fd526d6442 with x86_64 defconfig plus KASAN and the Bluetooth, 6LoWPAN,
vhci and configfs options the test needs - 1664 =y options, gcc, and no
instrumentation of any kind, so net/bluetooth is exactly as shipped. It was
booted by hand under qemu against the same unmodified rootfs image each
time, with the image opened read-only (-snapshot). The only difference
between the two kernels is net/bluetooth/l2cap_core.c:
unpatched 7.4.0-rc1-ga2fd526d6442 run 1: KASAN run 2: KASAN
patched 7.4.0-rc1-gc0de89ec9d04 run 1: no report run 2: no report
The clean runs are not setup failures, which is the thing worth checking:
the reproducer prints each step, and on both the unpatched and the patched
kernel it reported the same L2CAP LE channel - the same scid, the same MTU,
MPS and credits - sent the same frames, and exited 0. The only difference
is the report. On the unpatched kernel the console shows the program's own
marker immediately before the trace:
[ 47.223483] LOWRUN sdu
[ 47.408129] BUG: KASAN: slab-use-after-free in
l2cap_chan_le_send_credits+0x86/0x1d0
[ 47.408176] Read of size 4 at addr ffff888011a76a70 by task
kworker/u21:0/352
[ 47.408280] Workqueue: hci0 hci_rx_work
Why freeing the SDU in the completion block cannot double-free. The
explicit kfree_skb() runs when the callback returns non-zero, and one
callback - 6LoWPAN's chan_recv_cb() -> recv_pkt() - returns a POSITIVE
NET_RX_DROP on paths that do not free the skb, so the caller must. The one
path in recv_pkt() that does free it is skb_share_check() returning NULL,
which needs the skb to be shared. It cannot be: net/bluetooth/l2cap_core.c
contains no skb_get() at all, its three skb_clone() calls are all on the
transmit side (l2cap_ertm_resend() and the broadcast loop),
net/bluetooth/6lowpan.c has none, and the only skb_get() in
net/bluetooth/hci_core.c is hdev->req_skb = skb_get(hdev->sent_cmd) on the
command path, which no ACL data frame reaches. append_skb_frag() links
fragments and adjusts lengths without touching skb->users. So a reassembly
head reaches the callback with users == 1, skb_shared() is false, and
skb_share_check() returns it untouched. Freeing on a non-zero return is
also what the three other ops->recv() call sites in this file already do.
Earlier runs, for completeness: the same reproducer faults the same way on
an unmodified 7.3.1 kernel, and the harness hit the signature 19 times over
eight hours on a third kernel. The patch also compiles warning-free in the
7.3.1 tree, where l2cap_core.o's .text goes from 147425 to 147441 bytes.
Affected versions. The four code regions that matter -
l2cap_le_rx_credits(), l2cap_ecred_recv(), l2cap_chan_le_send_credits() and
l2cap_ecred_data_rcv()'s completion block - are byte-identical in the 7.3.1
tree we crashed and in net/main at a2fd526d6442, and the pattern is present
in mainline at 79eb6679ef6a. net/bluetooth/6lowpan.c's recv_pkt() has the
same two consume_skb(skb) calls in both trees. The code appears unchanged
in every v7.3-rc tag available to us.
Not determined: the commit that introduced it, hence no Fixes: tag. Our
clone is shallow - 27 commits of l2cap_core.c history are reachable and
git blame lands on the shallow boundary - so we can give the bounds above
but not a SHA. Please add the tag if you can resolve it.
Not demonstrated: the socket-path race described at the end of section 2,
and any consequence beyond the KASAN report itself.
Thanks,
Piyus Kedia
Kapil Vaswani
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH] Bluetooth: L2CAP: clear the SDU before the receive callback
2026-11-12 17:38 [BUG] Bluetooth: L2CAP: slab-use-after-free reading chan->sdu->len after the receive callback has consumed the SDU (seen on 7.3.1) Piyus Kedia
@ 2026-11-12 17:38 ` Piyus Kedia
2026-11-12 19:54 ` Luiz Augusto von Dentz
2026-11-16 19:50 ` patchwork-bot+bluetooth
0 siblings, 2 replies; 7+ messages in thread
From: Piyus Kedia @ 2026-11-12 17:38 UTC (permalink / raw)
To: Marcel Holtmann, Luiz Augusto von Dentz, linux-bluetooth
Cc: linux-kernel, Kapil Vaswani, Piyus Kedia
l2cap_ecred_data_rcv() completes a reassembled SDU by calling
l2cap_ecred_recv() with chan->sdu still set, and only clears the
reassembly state after it returns:
if (chan->sdu->len == chan->sdu_len) {
err = l2cap_ecred_recv(chan, chan->sdu);
if (!err) {
chan->sdu = NULL;
...
l2cap_ecred_recv() hands the SDU to the channel's receive callback,
which takes ownership of it on success, and then updates the credits:
err = chan->ops->recv(chan, skb);
...
l2cap_chan_le_send_credits(chan);
l2cap_chan_le_send_credits() calls l2cap_le_rx_credits(), which reads
size_t sdu_len = chan->sdu ? chan->sdu->len : 0;
so between the callback consuming the skb and the assignment above,
chan->sdu is a dangling pointer that is dereferenced.
For the 6LoWPAN channel the callback frees the skb outright -
chan_recv_cb() -> recv_pkt() ends in consume_skb(skb) and returns
NET_RX_SUCCESS - so the read is a straightforward use-after-free that
KASAN reports:
BUG: KASAN: slab-use-after-free in l2cap_le_rx_credits
net/bluetooth/l2cap_core.c:576 [inline]
BUG: KASAN: slab-use-after-free in l2cap_chan_le_send_credits+0xa8/0x3e0
net/bluetooth/l2cap_core.c:6811
Read of size 4 at addr ffff88800e134070 by task kworker/u21:2/11352
Workqueue: hci0 hci_rx_work
l2cap_le_rx_credits net/bluetooth/l2cap_core.c:576 [inline]
l2cap_chan_le_send_credits+0xa8/0x3e0 net/bluetooth/l2cap_core.c:6811
l2cap_ecred_recv+0x63/0x120 net/bluetooth/l2cap_core.c:6873
l2cap_ecred_data_rcv net/bluetooth/l2cap_core.c:6976 [inline]
l2cap_data_channel net/bluetooth/l2cap_core.c:7028 [inline]
l2cap_recv_frame+0x6c3c/0x9650 net/bluetooth/l2cap_core.c:7162
l2cap_recv_acldata+0x6fd/0xe60 net/bluetooth/l2cap_core.c:7902
hci_acldata_packet net/bluetooth/hci_core.c:3865 [inline]
hci_rx_work+0x3c0/0xbe0 net/bluetooth/hci_core.c:4092
Freed by task 11352:
recv_pkt net/bluetooth/6lowpan.c
chan_recv_cb+0x88/0x150 net/bluetooth/6lowpan.c:367
l2cap_ecred_recv+0x61/0x160 net/bluetooth/l2cap_core.c:6864
The buggy address belongs to the object at ffff88800e134000
which belongs to the cache skbuff_head_cache of size 240
The buggy address is located 112 bytes inside of
freed 240-byte region
Offset 112 is sk_buff.len.
Only a multi-frame SDU is affected: the single-frame path already calls
l2cap_ecred_recv() with chan->sdu NULL, so l2cap_le_rx_credits() reads a
zero sdu_len there.
l2cap_reassemble_sdu() completes a segmented SDU the same way and has the
same defect. On an ERTM or streaming channel l2cap_sock_recv_cb() calls
l2cap_publish_rx_avail(), which reaches l2cap_chan_le_send_credits()
through l2cap_chan_rx_avail(), and that function evaluates
u16 return_credits = l2cap_le_rx_credits(chan);
in its initializer - before the mode test that makes the rest of it a
no-op for a non-LE channel. So chan->sdu is dereferenced on that path
too, and the result is discarded. What makes it a use-after-free rather
than a stale read is a consumer that frees the skb from another thread:
BNEP's session kthread takes it off the socket with a lockless
skb_dequeue() (net/bluetooth/bnep/core.c:530, freed at :533) and never
takes the socket lock, so lock_sock() is no defence.
Fix both sites.
Clear the reassembly state before handing the SDU to the callback, which
makes the fragmented path behave like the single-frame one, and free the
SDU on the spot if the callback refused it - the error path's
kfree_skb(chan->sdu) used to do that, and the callbacks leave the skb to
the caller when they fail.
Clearing it first is also the arithmetically correct input, not merely a
safe one. l2cap_le_rx_credits() subtracts sdu_len from chan->rx_avail
because a partially reassembled SDU will claim that much receive buffer
once it completes:
if (chan->rx_avail <= sdu_len)
return 0;
return DIV_ROUND_UP(chan->rx_avail - sdu_len, chan->mps);
A completed SDU is no longer waiting for that space. If the callback
queued it, l2cap_sock_recv_cb() has already called
l2cap_publish_rx_avail(), which recomputes rx_avail from
sk_rcvbuf - sk_rmem_alloc with that skb included in sk_rmem_alloc; if the
callback consumed it, as the 6LoWPAN one does, it occupies nothing at
all. Either way the reassembly buffer is empty, so zero is the right
sdu_len, and a NULL chan->sdu is how l2cap_le_rx_credits() expresses
that - the same value l2cap_le_flowctl_init() depends on when it seeds
chan->rx_credits on an idle channel. Carrying the old length into the
calculation instead would subtract the SDU a second time and under-grant
credits on every delivery, and once rx_avail fell to the size of one SDU
the channel would be granted none at all.
Per Documentation/process/generated-content.rst: the use-after-free was
found by an AI-driven fuzzing harness built on syzkaller, developed
jointly by Kapil Vaswani and myself, which drove a virtual HCI
controller and a 6LoWPAN channel under KASAN on x86_64. The root-cause
analysis, this changelog and the patch were written with the assistance
of a large language model over an extended session, and reviewed by
hand; the before/after testing below the --- was run by us.
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
Tested on net/main itself, before and after. The kernel was built from
a2fd526d6442 with x86_64 defconfig plus KASAN and the Bluetooth, 6LoWPAN,
vhci and configfs options the test needs, gcc, no instrumentation, and
booted by hand under qemu against the same unmodified rootfs each time
(-snapshot, so the image is never written). The only difference between the
two kernels is this patch:
unpatched 7.4.0-rc1-ga2fd526d6442 KASAN on every run (4 runs today)
patched 7.4.0-rc1-g63fe746a609a no report (2 runs)
WHICH SITE THAT COVERS, because the patch changes two. The reproducer
drives the ECRED path, so l2cap_ecred_data_rcv() is the hunk those runs
exercise. For the l2cap_reassemble_sdu() hunk, the same patched commit was
rebuilt with CONFIG_BT_BNEP=y and driven with a segmented SDU on an ERTM
channel with a BNEP session attached, under two kprobes:
l2cap_reassemble_sdu 14 hits
l2cap_sock_recv_cb 6 hits
interleaved rr-C rr-C: two entries into the reassembly function per
callback, which is a START frame installing chan->sdu followed by an END
frame completing it - the branch this patch changes - and not the
unsegmented path, which would call back on every entry
with no KASAN report. So the changed line runs and reassembly still
completes. What that run does NOT do is reproduce the use-after-free on
that path: it needs the freeing thread to win, and the harness that found
this bug did win it with BNEP's lockless skb_dequeue(), where we did not.
The argument for why that site is affected is the ordering one above, from
the source.
The clean runs are not setup failures: on both kernels the reproducer
reported the same L2CAP LE channel and the same frames -
SUMMARY mtu=1280 mps=1017 credits=2 scid=64 chan_gone=0
- and exited 0. Only the report differs. On the unpatched kernel the
console shows the program's own marker immediately before the trace:
[ 45.878309] LOWRUN sdu
[ 46.063560] BUG: KASAN: slab-use-after-free in
l2cap_chan_le_send_credits+0x86/0x1d0
[ 46.063711] Read of size 4 at addr ffff88800c9a82f0 by task
kworker/u21:0/353
[ 46.063838] Workqueue: hci0 hci_rx_work
Offset 112 of the freed 240-byte skbuff_head_cache object is sk_buff.len.
On the patched kernel the same marker is printed and nothing follows it.
The same reproducer also faults on an unmodified 7.3.1 kernel, and the
harness hit the signature 19 times over eight hours on a third kernel.
No Fixes: tag: our clone is shallow, only 27 commits of l2cap_core.c history
are reachable and git blame lands on the shallow boundary, so we cannot name
the introducing commit. The code is byte-identical in net/main at
a2fd526d6442, in mainline at 79eb6679ef6a and in the 7.3.1 tree this was
found on, and is present in every v7.3-rc tag available to us. Please add
the tag if you can resolve it against full history.
Reproducer: not included here. This bug was found with AI assistance, which
Documentation/process/security-bugs.rst says to treat as public; the same
paragraph says not to post the reproducer publicly. It is one C program
needing no Bluetooth hardware, it is tested, and we will send it on request.
What bounds the scope: the channel's receive callback has to be one that
frees the skb, which is the 6LoWPAN one, and the SDU has to arrive in more
than one frame. An administrator enables 6LoWPAN through debugfs; after
that bt_6lowpan_listen() listens on L2CAP_PSM_IPSP and a connected LE peer
reaches this with an ordinary fragmented SDU, needing no local privilege.
net/bluetooth/l2cap_core.c | 53 ++++++++++++++++++++++++++++----------
1 file changed, 39 insertions(+), 14 deletions(-)
diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 7dcb0586a10b..fb965e4d75f2 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -6186,6 +6186,7 @@ static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag,
static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb,
struct l2cap_ctrl *control)
{
+ struct sk_buff *sdu;
int err = -EINVAL;
switch (control->sar) {
@@ -6246,15 +6247,25 @@ static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb,
if (chan->sdu->len != chan->sdu_len)
break;
- err = chan->ops->recv(chan, chan->sdu);
+ /* Reassembly complete. Hand the SDU over before the
+ * callback, for the reason given in l2cap_ecred_data_rcv():
+ * the callback takes ownership and may free it, while
+ * l2cap_chan_rx_avail() -> l2cap_chan_le_send_credits() reads
+ * chan->sdu->len on the way out - it does so before the mode
+ * test that makes the rest of that function a no-op here.
+ * Return directly, since the state this leaves is what the
+ * trailing error block would otherwise set again.
+ */
+ sdu = chan->sdu;
+ chan->sdu = NULL;
+ chan->sdu_last_frag = NULL;
+ chan->sdu_len = 0;
- if (!err) {
- /* Reassembly complete */
- chan->sdu = NULL;
- chan->sdu_last_frag = NULL;
- chan->sdu_len = 0;
- }
- break;
+ err = chan->ops->recv(chan, sdu);
+ if (err)
+ kfree_skb(sdu);
+
+ return err;
}
if (err) {
@@ -7245,12 +7256,26 @@ static int l2cap_ecred_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
skb = NULL;
if (chan->sdu->len == chan->sdu_len) {
- err = l2cap_ecred_recv(chan, chan->sdu);
- if (!err) {
- chan->sdu = NULL;
- chan->sdu_last_frag = NULL;
- chan->sdu_len = 0;
- }
+ struct sk_buff *sdu = chan->sdu;
+
+ /* The receive callback takes ownership of a reassembled SDU and
+ * may free it, while l2cap_ecred_recv() goes on to update the
+ * credits - which reads chan->sdu->len. Drop the reassembly
+ * state before handing the SDU over, and release it here if the
+ * callback refused it, so that it is still freed exactly once.
+ * The reassembly state is already clear at that point, so
+ * return instead of falling into the failed: block, whose
+ * kfree_skb(chan->sdu) would have nothing left to free.
+ */
+ chan->sdu = NULL;
+ chan->sdu_last_frag = NULL;
+ chan->sdu_len = 0;
+
+ err = l2cap_ecred_recv(chan, sdu);
+ if (err)
+ kfree_skb(sdu);
+
+ return 0;
}
failed:
base-commit: a2fd526d644233d6a892c28173f07c61e21e4c93
--
2.34.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Bluetooth: L2CAP: clear the SDU before the receive callback
2026-11-12 17:38 ` [PATCH] Bluetooth: L2CAP: clear the SDU before the receive callback Piyus Kedia
@ 2026-11-12 19:54 ` Luiz Augusto von Dentz
2026-11-13 16:20 ` Piyus Kedia
2026-11-16 19:50 ` patchwork-bot+bluetooth
1 sibling, 1 reply; 7+ messages in thread
From: Luiz Augusto von Dentz @ 2026-11-12 19:54 UTC (permalink / raw)
To: Piyus Kedia; +Cc: Marcel Holtmann, linux-bluetooth, linux-kernel, Kapil Vaswani
Hi Piyus,
On Thu, Nov 12, 2026 at 12:38 PM Piyus Kedia <piyus@iiitd.ac.in> wrote:
>
> l2cap_ecred_data_rcv() completes a reassembled SDU by calling
> l2cap_ecred_recv() with chan->sdu still set, and only clears the
> reassembly state after it returns:
>
> if (chan->sdu->len == chan->sdu_len) {
> err = l2cap_ecred_recv(chan, chan->sdu);
> if (!err) {
> chan->sdu = NULL;
> ...
>
> l2cap_ecred_recv() hands the SDU to the channel's receive callback,
> which takes ownership of it on success, and then updates the credits:
>
> err = chan->ops->recv(chan, skb);
> ...
> l2cap_chan_le_send_credits(chan);
>
> l2cap_chan_le_send_credits() calls l2cap_le_rx_credits(), which reads
>
> size_t sdu_len = chan->sdu ? chan->sdu->len : 0;
>
> so between the callback consuming the skb and the assignment above,
> chan->sdu is a dangling pointer that is dereferenced.
>
> For the 6LoWPAN channel the callback frees the skb outright -
> chan_recv_cb() -> recv_pkt() ends in consume_skb(skb) and returns
Looks like this comment doesn't hold:
https://sashiko.dev/#/patchset/20261112173812.18696-2-piyus%40iiitd.ac.in
The 6LoWPAN does actually free on error as well.
> NET_RX_SUCCESS - so the read is a straightforward use-after-free that
> KASAN reports:
>
> BUG: KASAN: slab-use-after-free in l2cap_le_rx_credits
> net/bluetooth/l2cap_core.c:576 [inline]
> BUG: KASAN: slab-use-after-free in l2cap_chan_le_send_credits+0xa8/0x3e0
> net/bluetooth/l2cap_core.c:6811
> Read of size 4 at addr ffff88800e134070 by task kworker/u21:2/11352
> Workqueue: hci0 hci_rx_work
> l2cap_le_rx_credits net/bluetooth/l2cap_core.c:576 [inline]
> l2cap_chan_le_send_credits+0xa8/0x3e0 net/bluetooth/l2cap_core.c:6811
> l2cap_ecred_recv+0x63/0x120 net/bluetooth/l2cap_core.c:6873
> l2cap_ecred_data_rcv net/bluetooth/l2cap_core.c:6976 [inline]
> l2cap_data_channel net/bluetooth/l2cap_core.c:7028 [inline]
> l2cap_recv_frame+0x6c3c/0x9650 net/bluetooth/l2cap_core.c:7162
> l2cap_recv_acldata+0x6fd/0xe60 net/bluetooth/l2cap_core.c:7902
> hci_acldata_packet net/bluetooth/hci_core.c:3865 [inline]
> hci_rx_work+0x3c0/0xbe0 net/bluetooth/hci_core.c:4092
> Freed by task 11352:
> recv_pkt net/bluetooth/6lowpan.c
> chan_recv_cb+0x88/0x150 net/bluetooth/6lowpan.c:367
> l2cap_ecred_recv+0x61/0x160 net/bluetooth/l2cap_core.c:6864
> The buggy address belongs to the object at ffff88800e134000
> which belongs to the cache skbuff_head_cache of size 240
> The buggy address is located 112 bytes inside of
> freed 240-byte region
>
> Offset 112 is sk_buff.len.
>
> Only a multi-frame SDU is affected: the single-frame path already calls
> l2cap_ecred_recv() with chan->sdu NULL, so l2cap_le_rx_credits() reads a
> zero sdu_len there.
>
> l2cap_reassemble_sdu() completes a segmented SDU the same way and has the
> same defect. On an ERTM or streaming channel l2cap_sock_recv_cb() calls
> l2cap_publish_rx_avail(), which reaches l2cap_chan_le_send_credits()
> through l2cap_chan_rx_avail(), and that function evaluates
>
> u16 return_credits = l2cap_le_rx_credits(chan);
>
> in its initializer - before the mode test that makes the rest of it a
> no-op for a non-LE channel. So chan->sdu is dereferenced on that path
> too, and the result is discarded. What makes it a use-after-free rather
> than a stale read is a consumer that frees the skb from another thread:
> BNEP's session kthread takes it off the socket with a lockless
> skb_dequeue() (net/bluetooth/bnep/core.c:530, freed at :533) and never
> takes the socket lock, so lock_sock() is no defence.
>
> Fix both sites.
>
> Clear the reassembly state before handing the SDU to the callback, which
> makes the fragmented path behave like the single-frame one, and free the
> SDU on the spot if the callback refused it - the error path's
> kfree_skb(chan->sdu) used to do that, and the callbacks leave the skb to
> the caller when they fail.
>
> Clearing it first is also the arithmetically correct input, not merely a
> safe one. l2cap_le_rx_credits() subtracts sdu_len from chan->rx_avail
> because a partially reassembled SDU will claim that much receive buffer
> once it completes:
>
> if (chan->rx_avail <= sdu_len)
> return 0;
>
> return DIV_ROUND_UP(chan->rx_avail - sdu_len, chan->mps);
>
> A completed SDU is no longer waiting for that space. If the callback
> queued it, l2cap_sock_recv_cb() has already called
> l2cap_publish_rx_avail(), which recomputes rx_avail from
> sk_rcvbuf - sk_rmem_alloc with that skb included in sk_rmem_alloc; if the
> callback consumed it, as the 6LoWPAN one does, it occupies nothing at
> all. Either way the reassembly buffer is empty, so zero is the right
> sdu_len, and a NULL chan->sdu is how l2cap_le_rx_credits() expresses
> that - the same value l2cap_le_flowctl_init() depends on when it seeds
> chan->rx_credits on an idle channel. Carrying the old length into the
> calculation instead would subtract the SDU a second time and under-grant
> credits on every delivery, and once rx_avail fell to the size of one SDU
> the channel would be granted none at all.
>
> Per Documentation/process/generated-content.rst: the use-after-free was
> found by an AI-driven fuzzing harness built on syzkaller, developed
> jointly by Kapil Vaswani and myself, which drove a virtual HCI
> controller and a 6LoWPAN channel under KASAN on x86_64. The root-cause
> analysis, this changelog and the patch were written with the assistance
> of a large language model over an extended session, and reviewed by
> hand; the before/after testing below the --- was run by us.
>
> Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
> Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
> ---
>
> Tested on net/main itself, before and after. The kernel was built from
> a2fd526d6442 with x86_64 defconfig plus KASAN and the Bluetooth, 6LoWPAN,
> vhci and configfs options the test needs, gcc, no instrumentation, and
> booted by hand under qemu against the same unmodified rootfs each time
> (-snapshot, so the image is never written). The only difference between the
> two kernels is this patch:
>
> unpatched 7.4.0-rc1-ga2fd526d6442 KASAN on every run (4 runs today)
> patched 7.4.0-rc1-g63fe746a609a no report (2 runs)
>
> WHICH SITE THAT COVERS, because the patch changes two. The reproducer
> drives the ECRED path, so l2cap_ecred_data_rcv() is the hunk those runs
> exercise. For the l2cap_reassemble_sdu() hunk, the same patched commit was
> rebuilt with CONFIG_BT_BNEP=y and driven with a segmented SDU on an ERTM
> channel with a BNEP session attached, under two kprobes:
>
> l2cap_reassemble_sdu 14 hits
> l2cap_sock_recv_cb 6 hits
> interleaved rr-C rr-C: two entries into the reassembly function per
> callback, which is a START frame installing chan->sdu followed by an END
> frame completing it - the branch this patch changes - and not the
> unsegmented path, which would call back on every entry
>
> with no KASAN report. So the changed line runs and reassembly still
> completes. What that run does NOT do is reproduce the use-after-free on
> that path: it needs the freeing thread to win, and the harness that found
> this bug did win it with BNEP's lockless skb_dequeue(), where we did not.
> The argument for why that site is affected is the ordering one above, from
> the source.
>
> The clean runs are not setup failures: on both kernels the reproducer
> reported the same L2CAP LE channel and the same frames -
>
> SUMMARY mtu=1280 mps=1017 credits=2 scid=64 chan_gone=0
>
> - and exited 0. Only the report differs. On the unpatched kernel the
> console shows the program's own marker immediately before the trace:
>
> [ 45.878309] LOWRUN sdu
> [ 46.063560] BUG: KASAN: slab-use-after-free in
> l2cap_chan_le_send_credits+0x86/0x1d0
> [ 46.063711] Read of size 4 at addr ffff88800c9a82f0 by task
> kworker/u21:0/353
> [ 46.063838] Workqueue: hci0 hci_rx_work
>
> Offset 112 of the freed 240-byte skbuff_head_cache object is sk_buff.len.
> On the patched kernel the same marker is printed and nothing follows it.
>
> The same reproducer also faults on an unmodified 7.3.1 kernel, and the
> harness hit the signature 19 times over eight hours on a third kernel.
>
> No Fixes: tag: our clone is shallow, only 27 commits of l2cap_core.c history
> are reachable and git blame lands on the shallow boundary, so we cannot name
> the introducing commit. The code is byte-identical in net/main at
> a2fd526d6442, in mainline at 79eb6679ef6a and in the 7.3.1 tree this was
> found on, and is present in every v7.3-rc tag available to us. Please add
> the tag if you can resolve it against full history.
>
> Reproducer: not included here. This bug was found with AI assistance, which
> Documentation/process/security-bugs.rst says to treat as public; the same
> paragraph says not to post the reproducer publicly. It is one C program
> needing no Bluetooth hardware, it is tested, and we will send it on request.
>
> What bounds the scope: the channel's receive callback has to be one that
> frees the skb, which is the 6LoWPAN one, and the SDU has to arrive in more
> than one frame. An administrator enables 6LoWPAN through debugfs; after
> that bt_6lowpan_listen() listens on L2CAP_PSM_IPSP and a connected LE peer
> reaches this with an ordinary fragmented SDU, needing no local privilege.
>
> net/bluetooth/l2cap_core.c | 53 ++++++++++++++++++++++++++++----------
> 1 file changed, 39 insertions(+), 14 deletions(-)
>
> diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
> index 7dcb0586a10b..fb965e4d75f2 100644
> --- a/net/bluetooth/l2cap_core.c
> +++ b/net/bluetooth/l2cap_core.c
> @@ -6186,6 +6186,7 @@ static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag,
> static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb,
> struct l2cap_ctrl *control)
> {
> + struct sk_buff *sdu;
> int err = -EINVAL;
>
> switch (control->sar) {
> @@ -6246,15 +6247,25 @@ static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb,
> if (chan->sdu->len != chan->sdu_len)
> break;
>
> - err = chan->ops->recv(chan, chan->sdu);
> + /* Reassembly complete. Hand the SDU over before the
> + * callback, for the reason given in l2cap_ecred_data_rcv():
> + * the callback takes ownership and may free it, while
> + * l2cap_chan_rx_avail() -> l2cap_chan_le_send_credits() reads
> + * chan->sdu->len on the way out - it does so before the mode
> + * test that makes the rest of that function a no-op here.
> + * Return directly, since the state this leaves is what the
> + * trailing error block would otherwise set again.
> + */
> + sdu = chan->sdu;
> + chan->sdu = NULL;
> + chan->sdu_last_frag = NULL;
> + chan->sdu_len = 0;
>
> - if (!err) {
> - /* Reassembly complete */
> - chan->sdu = NULL;
> - chan->sdu_last_frag = NULL;
> - chan->sdu_len = 0;
> - }
> - break;
> + err = chan->ops->recv(chan, sdu);
> + if (err)
> + kfree_skb(sdu);
> +
> + return err;
> }
>
> if (err) {
> @@ -7245,12 +7256,26 @@ static int l2cap_ecred_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
> skb = NULL;
>
> if (chan->sdu->len == chan->sdu_len) {
> - err = l2cap_ecred_recv(chan, chan->sdu);
> - if (!err) {
> - chan->sdu = NULL;
> - chan->sdu_last_frag = NULL;
> - chan->sdu_len = 0;
> - }
> + struct sk_buff *sdu = chan->sdu;
> +
> + /* The receive callback takes ownership of a reassembled SDU and
> + * may free it, while l2cap_ecred_recv() goes on to update the
> + * credits - which reads chan->sdu->len. Drop the reassembly
> + * state before handing the SDU over, and release it here if the
> + * callback refused it, so that it is still freed exactly once.
> + * The reassembly state is already clear at that point, so
> + * return instead of falling into the failed: block, whose
> + * kfree_skb(chan->sdu) would have nothing left to free.
> + */
> + chan->sdu = NULL;
> + chan->sdu_last_frag = NULL;
> + chan->sdu_len = 0;
> +
> + err = l2cap_ecred_recv(chan, sdu);
> + if (err)
> + kfree_skb(sdu);
> +
> + return 0;
> }
>
> failed:
>
> base-commit: a2fd526d644233d6a892c28173f07c61e21e4c93
> --
> 2.34.1
>
--
Luiz Augusto von Dentz
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Bluetooth: L2CAP: clear the SDU before the receive callback
2026-11-12 19:54 ` Luiz Augusto von Dentz
@ 2026-11-13 16:20 ` Piyus Kedia
2026-11-13 16:23 ` Luiz Augusto von Dentz
0 siblings, 1 reply; 7+ messages in thread
From: Piyus Kedia @ 2026-11-13 16:20 UTC (permalink / raw)
To: Luiz Augusto von Dentz
Cc: Marcel Holtmann, linux-bluetooth, linux-kernel, Kapil Vaswani
Hi Luiz,
Thanks for sharing this!
On the double free: that free isn't new. The block this patch replaces
already did it,
failed:
if (err) {
kfree_skb(skb);
kfree_skb(chan->sdu);
with skb NULL by then, and l2cap_reassemble_sdu() has the same block.
So if a shared skb can reach the callback, the existing code
double-frees in exactly the same way - this patch neither adds that
nor fixes it. What it fixes is the use-after-free on chan->sdu.
As far as we can tell, it can't be shared here: skb_share_check() only
acts when skb->users != 1, and nothing on the ACL receive path takes a
reference - the only two skb_get() calls in net/bluetooth are req_skb
in hci_core.c and req_rsp in hci_sync.c, both on the HCI command path,
and
append_skb_frag() doesn't touch skb->users. We may be wrong about
that, and if we are, the fix belongs in recv_pkt() rather than in each
caller - it frees the caller's skb on one drop path and leaves it on
the others. Happy to send a patch for that separately.
You're also right that "frees the skb outright" was wrong: recv_pkt()
consumes the skb on its success paths and leaves it to the caller on
most of the drop paths. I have a v2 ready with that sentence fixed,
but the diff is unchanged from v1. Please let us know if you want it
posted.
Best,
Piyus Kedia
Kapil Vaswani
On Fri, Nov 13, 2026 at 1:24 AM Luiz Augusto von Dentz
<luiz.dentz@gmail.com> wrote:
>
> Hi Piyus,
>
> On Thu, Nov 12, 2026 at 12:38 PM Piyus Kedia <piyus@iiitd.ac.in> wrote:
> >
> > l2cap_ecred_data_rcv() completes a reassembled SDU by calling
> > l2cap_ecred_recv() with chan->sdu still set, and only clears the
> > reassembly state after it returns:
> >
> > if (chan->sdu->len == chan->sdu_len) {
> > err = l2cap_ecred_recv(chan, chan->sdu);
> > if (!err) {
> > chan->sdu = NULL;
> > ...
> >
> > l2cap_ecred_recv() hands the SDU to the channel's receive callback,
> > which takes ownership of it on success, and then updates the credits:
> >
> > err = chan->ops->recv(chan, skb);
> > ...
> > l2cap_chan_le_send_credits(chan);
> >
> > l2cap_chan_le_send_credits() calls l2cap_le_rx_credits(), which reads
> >
> > size_t sdu_len = chan->sdu ? chan->sdu->len : 0;
> >
> > so between the callback consuming the skb and the assignment above,
> > chan->sdu is a dangling pointer that is dereferenced.
> >
> > For the 6LoWPAN channel the callback frees the skb outright -
> > chan_recv_cb() -> recv_pkt() ends in consume_skb(skb) and returns
>
> Looks like this comment doesn't hold:
>
> https://sashiko.dev/#/patchset/20261112173812.18696-2-piyus%40iiitd.ac.in
>
> The 6LoWPAN does actually free on error as well.
>
> > NET_RX_SUCCESS - so the read is a straightforward use-after-free that
> > KASAN reports:
> >
> > BUG: KASAN: slab-use-after-free in l2cap_le_rx_credits
> > net/bluetooth/l2cap_core.c:576 [inline]
> > BUG: KASAN: slab-use-after-free in l2cap_chan_le_send_credits+0xa8/0x3e0
> > net/bluetooth/l2cap_core.c:6811
> > Read of size 4 at addr ffff88800e134070 by task kworker/u21:2/11352
> > Workqueue: hci0 hci_rx_work
> > l2cap_le_rx_credits net/bluetooth/l2cap_core.c:576 [inline]
> > l2cap_chan_le_send_credits+0xa8/0x3e0 net/bluetooth/l2cap_core.c:6811
> > l2cap_ecred_recv+0x63/0x120 net/bluetooth/l2cap_core.c:6873
> > l2cap_ecred_data_rcv net/bluetooth/l2cap_core.c:6976 [inline]
> > l2cap_data_channel net/bluetooth/l2cap_core.c:7028 [inline]
> > l2cap_recv_frame+0x6c3c/0x9650 net/bluetooth/l2cap_core.c:7162
> > l2cap_recv_acldata+0x6fd/0xe60 net/bluetooth/l2cap_core.c:7902
> > hci_acldata_packet net/bluetooth/hci_core.c:3865 [inline]
> > hci_rx_work+0x3c0/0xbe0 net/bluetooth/hci_core.c:4092
> > Freed by task 11352:
> > recv_pkt net/bluetooth/6lowpan.c
> > chan_recv_cb+0x88/0x150 net/bluetooth/6lowpan.c:367
> > l2cap_ecred_recv+0x61/0x160 net/bluetooth/l2cap_core.c:6864
> > The buggy address belongs to the object at ffff88800e134000
> > which belongs to the cache skbuff_head_cache of size 240
> > The buggy address is located 112 bytes inside of
> > freed 240-byte region
> >
> > Offset 112 is sk_buff.len.
> >
> > Only a multi-frame SDU is affected: the single-frame path already calls
> > l2cap_ecred_recv() with chan->sdu NULL, so l2cap_le_rx_credits() reads a
> > zero sdu_len there.
> >
> > l2cap_reassemble_sdu() completes a segmented SDU the same way and has the
> > same defect. On an ERTM or streaming channel l2cap_sock_recv_cb() calls
> > l2cap_publish_rx_avail(), which reaches l2cap_chan_le_send_credits()
> > through l2cap_chan_rx_avail(), and that function evaluates
> >
> > u16 return_credits = l2cap_le_rx_credits(chan);
> >
> > in its initializer - before the mode test that makes the rest of it a
> > no-op for a non-LE channel. So chan->sdu is dereferenced on that path
> > too, and the result is discarded. What makes it a use-after-free rather
> > than a stale read is a consumer that frees the skb from another thread:
> > BNEP's session kthread takes it off the socket with a lockless
> > skb_dequeue() (net/bluetooth/bnep/core.c:530, freed at :533) and never
> > takes the socket lock, so lock_sock() is no defence.
> >
> > Fix both sites.
> >
> > Clear the reassembly state before handing the SDU to the callback, which
> > makes the fragmented path behave like the single-frame one, and free the
> > SDU on the spot if the callback refused it - the error path's
> > kfree_skb(chan->sdu) used to do that, and the callbacks leave the skb to
> > the caller when they fail.
> >
> > Clearing it first is also the arithmetically correct input, not merely a
> > safe one. l2cap_le_rx_credits() subtracts sdu_len from chan->rx_avail
> > because a partially reassembled SDU will claim that much receive buffer
> > once it completes:
> >
> > if (chan->rx_avail <= sdu_len)
> > return 0;
> >
> > return DIV_ROUND_UP(chan->rx_avail - sdu_len, chan->mps);
> >
> > A completed SDU is no longer waiting for that space. If the callback
> > queued it, l2cap_sock_recv_cb() has already called
> > l2cap_publish_rx_avail(), which recomputes rx_avail from
> > sk_rcvbuf - sk_rmem_alloc with that skb included in sk_rmem_alloc; if the
> > callback consumed it, as the 6LoWPAN one does, it occupies nothing at
> > all. Either way the reassembly buffer is empty, so zero is the right
> > sdu_len, and a NULL chan->sdu is how l2cap_le_rx_credits() expresses
> > that - the same value l2cap_le_flowctl_init() depends on when it seeds
> > chan->rx_credits on an idle channel. Carrying the old length into the
> > calculation instead would subtract the SDU a second time and under-grant
> > credits on every delivery, and once rx_avail fell to the size of one SDU
> > the channel would be granted none at all.
> >
> > Per Documentation/process/generated-content.rst: the use-after-free was
> > found by an AI-driven fuzzing harness built on syzkaller, developed
> > jointly by Kapil Vaswani and myself, which drove a virtual HCI
> > controller and a 6LoWPAN channel under KASAN on x86_64. The root-cause
> > analysis, this changelog and the patch were written with the assistance
> > of a large language model over an extended session, and reviewed by
> > hand; the before/after testing below the --- was run by us.
> >
> > Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
> > Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
> > ---
> >
> > Tested on net/main itself, before and after. The kernel was built from
> > a2fd526d6442 with x86_64 defconfig plus KASAN and the Bluetooth, 6LoWPAN,
> > vhci and configfs options the test needs, gcc, no instrumentation, and
> > booted by hand under qemu against the same unmodified rootfs each time
> > (-snapshot, so the image is never written). The only difference between the
> > two kernels is this patch:
> >
> > unpatched 7.4.0-rc1-ga2fd526d6442 KASAN on every run (4 runs today)
> > patched 7.4.0-rc1-g63fe746a609a no report (2 runs)
> >
> > WHICH SITE THAT COVERS, because the patch changes two. The reproducer
> > drives the ECRED path, so l2cap_ecred_data_rcv() is the hunk those runs
> > exercise. For the l2cap_reassemble_sdu() hunk, the same patched commit was
> > rebuilt with CONFIG_BT_BNEP=y and driven with a segmented SDU on an ERTM
> > channel with a BNEP session attached, under two kprobes:
> >
> > l2cap_reassemble_sdu 14 hits
> > l2cap_sock_recv_cb 6 hits
> > interleaved rr-C rr-C: two entries into the reassembly function per
> > callback, which is a START frame installing chan->sdu followed by an END
> > frame completing it - the branch this patch changes - and not the
> > unsegmented path, which would call back on every entry
> >
> > with no KASAN report. So the changed line runs and reassembly still
> > completes. What that run does NOT do is reproduce the use-after-free on
> > that path: it needs the freeing thread to win, and the harness that found
> > this bug did win it with BNEP's lockless skb_dequeue(), where we did not.
> > The argument for why that site is affected is the ordering one above, from
> > the source.
> >
> > The clean runs are not setup failures: on both kernels the reproducer
> > reported the same L2CAP LE channel and the same frames -
> >
> > SUMMARY mtu=1280 mps=1017 credits=2 scid=64 chan_gone=0
> >
> > - and exited 0. Only the report differs. On the unpatched kernel the
> > console shows the program's own marker immediately before the trace:
> >
> > [ 45.878309] LOWRUN sdu
> > [ 46.063560] BUG: KASAN: slab-use-after-free in
> > l2cap_chan_le_send_credits+0x86/0x1d0
> > [ 46.063711] Read of size 4 at addr ffff88800c9a82f0 by task
> > kworker/u21:0/353
> > [ 46.063838] Workqueue: hci0 hci_rx_work
> >
> > Offset 112 of the freed 240-byte skbuff_head_cache object is sk_buff.len.
> > On the patched kernel the same marker is printed and nothing follows it.
> >
> > The same reproducer also faults on an unmodified 7.3.1 kernel, and the
> > harness hit the signature 19 times over eight hours on a third kernel.
> >
> > No Fixes: tag: our clone is shallow, only 27 commits of l2cap_core.c history
> > are reachable and git blame lands on the shallow boundary, so we cannot name
> > the introducing commit. The code is byte-identical in net/main at
> > a2fd526d6442, in mainline at 79eb6679ef6a and in the 7.3.1 tree this was
> > found on, and is present in every v7.3-rc tag available to us. Please add
> > the tag if you can resolve it against full history.
> >
> > Reproducer: not included here. This bug was found with AI assistance, which
> > Documentation/process/security-bugs.rst says to treat as public; the same
> > paragraph says not to post the reproducer publicly. It is one C program
> > needing no Bluetooth hardware, it is tested, and we will send it on request.
> >
> > What bounds the scope: the channel's receive callback has to be one that
> > frees the skb, which is the 6LoWPAN one, and the SDU has to arrive in more
> > than one frame. An administrator enables 6LoWPAN through debugfs; after
> > that bt_6lowpan_listen() listens on L2CAP_PSM_IPSP and a connected LE peer
> > reaches this with an ordinary fragmented SDU, needing no local privilege.
> >
> > net/bluetooth/l2cap_core.c | 53 ++++++++++++++++++++++++++++----------
> > 1 file changed, 39 insertions(+), 14 deletions(-)
> >
> > diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
> > index 7dcb0586a10b..fb965e4d75f2 100644
> > --- a/net/bluetooth/l2cap_core.c
> > +++ b/net/bluetooth/l2cap_core.c
> > @@ -6186,6 +6186,7 @@ static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag,
> > static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb,
> > struct l2cap_ctrl *control)
> > {
> > + struct sk_buff *sdu;
> > int err = -EINVAL;
> >
> > switch (control->sar) {
> > @@ -6246,15 +6247,25 @@ static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb,
> > if (chan->sdu->len != chan->sdu_len)
> > break;
> >
> > - err = chan->ops->recv(chan, chan->sdu);
> > + /* Reassembly complete. Hand the SDU over before the
> > + * callback, for the reason given in l2cap_ecred_data_rcv():
> > + * the callback takes ownership and may free it, while
> > + * l2cap_chan_rx_avail() -> l2cap_chan_le_send_credits() reads
> > + * chan->sdu->len on the way out - it does so before the mode
> > + * test that makes the rest of that function a no-op here.
> > + * Return directly, since the state this leaves is what the
> > + * trailing error block would otherwise set again.
> > + */
> > + sdu = chan->sdu;
> > + chan->sdu = NULL;
> > + chan->sdu_last_frag = NULL;
> > + chan->sdu_len = 0;
> >
> > - if (!err) {
> > - /* Reassembly complete */
> > - chan->sdu = NULL;
> > - chan->sdu_last_frag = NULL;
> > - chan->sdu_len = 0;
> > - }
> > - break;
> > + err = chan->ops->recv(chan, sdu);
> > + if (err)
> > + kfree_skb(sdu);
> > +
> > + return err;
> > }
> >
> > if (err) {
> > @@ -7245,12 +7256,26 @@ static int l2cap_ecred_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
> > skb = NULL;
> >
> > if (chan->sdu->len == chan->sdu_len) {
> > - err = l2cap_ecred_recv(chan, chan->sdu);
> > - if (!err) {
> > - chan->sdu = NULL;
> > - chan->sdu_last_frag = NULL;
> > - chan->sdu_len = 0;
> > - }
> > + struct sk_buff *sdu = chan->sdu;
> > +
> > + /* The receive callback takes ownership of a reassembled SDU and
> > + * may free it, while l2cap_ecred_recv() goes on to update the
> > + * credits - which reads chan->sdu->len. Drop the reassembly
> > + * state before handing the SDU over, and release it here if the
> > + * callback refused it, so that it is still freed exactly once.
> > + * The reassembly state is already clear at that point, so
> > + * return instead of falling into the failed: block, whose
> > + * kfree_skb(chan->sdu) would have nothing left to free.
> > + */
> > + chan->sdu = NULL;
> > + chan->sdu_last_frag = NULL;
> > + chan->sdu_len = 0;
> > +
> > + err = l2cap_ecred_recv(chan, sdu);
> > + if (err)
> > + kfree_skb(sdu);
> > +
> > + return 0;
> > }
> >
> > failed:
> >
> > base-commit: a2fd526d644233d6a892c28173f07c61e21e4c93
> > --
> > 2.34.1
> >
>
>
> --
> Luiz Augusto von Dentz
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Bluetooth: L2CAP: clear the SDU before the receive callback
2026-11-13 16:20 ` Piyus Kedia
@ 2026-11-13 16:23 ` Luiz Augusto von Dentz
2026-11-13 16:32 ` Piyus Kedia
0 siblings, 1 reply; 7+ messages in thread
From: Luiz Augusto von Dentz @ 2026-11-13 16:23 UTC (permalink / raw)
To: Piyus Kedia; +Cc: Marcel Holtmann, linux-bluetooth, linux-kernel, Kapil Vaswani
Hi Piyus,
On Fri, Nov 13, 2026 at 11:20 AM Piyus Kedia <piyus@iiitd.ac.in> wrote:
>
> Hi Luiz,
>
> Thanks for sharing this!
>
> On the double free: that free isn't new. The block this patch replaces
> already did it,
>
> failed:
> if (err) {
> kfree_skb(skb);
> kfree_skb(chan->sdu);
>
> with skb NULL by then, and l2cap_reassemble_sdu() has the same block.
> So if a shared skb can reach the callback, the existing code
> double-frees in exactly the same way - this patch neither adds that
> nor fixes it. What it fixes is the use-after-free on chan->sdu.
>
> As far as we can tell, it can't be shared here: skb_share_check() only
> acts when skb->users != 1, and nothing on the ACL receive path takes a
> reference - the only two skb_get() calls in net/bluetooth are req_skb
> in hci_core.c and req_rsp in hci_sync.c, both on the HCI command path,
> and
> append_skb_frag() doesn't touch skb->users. We may be wrong about
> that, and if we are, the fix belongs in recv_pkt() rather than in each
> caller - it frees the caller's skb on one drop path and leaves it on
> the others. Happy to send a patch for that separately.
>
> You're also right that "frees the skb outright" was wrong: recv_pkt()
> consumes the skb on its success paths and leaves it to the caller on
> most of the drop paths. I have a v2 ready with that sentence fixed,
> but the diff is unchanged from v1. Please let us know if you want it
> posted.
Ive sent a fix the 6lowpan, so no need to respin this, I will add the
missing Fixes tag that sashiko flagged.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Bluetooth: L2CAP: clear the SDU before the receive callback
2026-11-13 16:23 ` Luiz Augusto von Dentz
@ 2026-11-13 16:32 ` Piyus Kedia
0 siblings, 0 replies; 7+ messages in thread
From: Piyus Kedia @ 2026-11-13 16:32 UTC (permalink / raw)
To: Luiz Augusto von Dentz
Cc: Marcel Holtmann, linux-bluetooth, linux-kernel, Kapil Vaswani
Sounds good, thanks for taking care of both!
Best,
Piyus
On Fri, Nov 13, 2026 at 9:53 PM Luiz Augusto von Dentz
<luiz.dentz@gmail.com> wrote:
>
> Hi Piyus,
>
> On Fri, Nov 13, 2026 at 11:20 AM Piyus Kedia <piyus@iiitd.ac.in> wrote:
> >
> > Hi Luiz,
> >
> > Thanks for sharing this!
> >
> > On the double free: that free isn't new. The block this patch replaces
> > already did it,
> >
> > failed:
> > if (err) {
> > kfree_skb(skb);
> > kfree_skb(chan->sdu);
> >
> > with skb NULL by then, and l2cap_reassemble_sdu() has the same block.
> > So if a shared skb can reach the callback, the existing code
> > double-frees in exactly the same way - this patch neither adds that
> > nor fixes it. What it fixes is the use-after-free on chan->sdu.
> >
> > As far as we can tell, it can't be shared here: skb_share_check() only
> > acts when skb->users != 1, and nothing on the ACL receive path takes a
> > reference - the only two skb_get() calls in net/bluetooth are req_skb
> > in hci_core.c and req_rsp in hci_sync.c, both on the HCI command path,
> > and
> > append_skb_frag() doesn't touch skb->users. We may be wrong about
> > that, and if we are, the fix belongs in recv_pkt() rather than in each
> > caller - it frees the caller's skb on one drop path and leaves it on
> > the others. Happy to send a patch for that separately.
> >
> > You're also right that "frees the skb outright" was wrong: recv_pkt()
> > consumes the skb on its success paths and leaves it to the caller on
> > most of the drop paths. I have a v2 ready with that sentence fixed,
> > but the diff is unchanged from v1. Please let us know if you want it
> > posted.
>
> Ive sent a fix the 6lowpan, so no need to respin this, I will add the
> missing Fixes tag that sashiko flagged.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Bluetooth: L2CAP: clear the SDU before the receive callback
2026-11-12 17:38 ` [PATCH] Bluetooth: L2CAP: clear the SDU before the receive callback Piyus Kedia
2026-11-12 19:54 ` Luiz Augusto von Dentz
@ 2026-11-16 19:50 ` patchwork-bot+bluetooth
1 sibling, 0 replies; 7+ messages in thread
From: patchwork-bot+bluetooth @ 2026-11-16 19:50 UTC (permalink / raw)
To: Piyus Kedia; +Cc: marcel, luiz.dentz, linux-bluetooth, linux-kernel, kapil
Hello:
This patch was applied to bluetooth/bluetooth-next.git (master)
by Luiz Augusto von Dentz <luiz.von.dentz@intel.com>:
On Thu, 12 Nov 2026 23:08:12 +0530 you wrote:
> l2cap_ecred_data_rcv() completes a reassembled SDU by calling
> l2cap_ecred_recv() with chan->sdu still set, and only clears the
> reassembly state after it returns:
>
> if (chan->sdu->len == chan->sdu_len) {
> err = l2cap_ecred_recv(chan, chan->sdu);
> if (!err) {
> chan->sdu = NULL;
> ...
>
> [...]
Here is the summary with links:
- Bluetooth: L2CAP: clear the SDU before the receive callback
https://git.kernel.org/bluetooth/bluetooth-next/c/76248359ac32
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-16 19:50 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-12 17:38 [BUG] Bluetooth: L2CAP: slab-use-after-free reading chan->sdu->len after the receive callback has consumed the SDU (seen on 7.3.1) Piyus Kedia
2026-11-12 17:38 ` [PATCH] Bluetooth: L2CAP: clear the SDU before the receive callback Piyus Kedia
2026-11-12 19:54 ` Luiz Augusto von Dentz
2026-11-13 16:20 ` Piyus Kedia
2026-11-13 16:23 ` Luiz Augusto von Dentz
2026-11-13 16:32 ` Piyus Kedia
2026-11-16 19:50 ` patchwork-bot+bluetooth
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®