Bluetooth: L2CAP: Fix use-after-free in l2cap_sock_new_connection_cb()
Subsystems (2)
v6 cover Bluetooth: L2CAP: Fix use-after-free in l2cap_sock_new_connection_cb()
Compared to v2, addresses comments on https://sashiko.dev/#/patchset/20260415204842.2363950-1-oss%40fourdim.xyz .
Compared to v3, rebase against bluetooth-next.
Compared to v4, allocate the channel outside the function and pass it in as an argument to avoid the use-after-free.
Compared to v5, extract the channel init to a separate function.
Siwei Zhang (1):
Bluetooth: L2CAP: Fix use-after-free in l2cap_sock_new_connection_cb()
include/net/bluetooth/l2cap.h | 8 +++-- net/bluetooth/6lowpan.c | 27 ++++++++-------- net/bluetooth/l2cap_core.c | 58 ++++++++++++++++++++++++++++------- net/bluetooth/l2cap_sock.c | 48 +++++++++++++++++------------ net/bluetooth/smp.c | 13 +++----- 5 files changed, 98 insertions(+), 56 deletions(-)
Signature · 1 line
2.54.0
PATCH v6 1/1 Bluetooth: L2CAP: Fix use-after-free in l2cap_sock_new_connection_cb()
l2cap_sock_new_connection_cb() accesses l2cap_pi(sk)->chan after release_sock(parent). Once the parent lock is released, the child socket sk can be freed by another task.
Allocate the channel outside the func to prevent this.
--- include/net/bluetooth/l2cap.h | 8 +++-- net/bluetooth/6lowpan.c | 27 ++++++++-------- net/bluetooth/l2cap_core.c | 58 ++++++++++++++++++++++++++++------- net/bluetooth/l2cap_sock.c | 48 +++++++++++++++++------------ net/bluetooth/smp.c | 13 +++----- 5 files changed, 98 insertions(+), 56 deletions(-)
include/net/bluetooth/l2cap.h +5 -3
@@ -619,7 +619,8 @@ struct l2cap_chan {struct l2cap_ops {char *name; -struct l2cap_chan *(*new_connection) (struct l2cap_chan *chan); +int (*new_connection)(struct l2cap_chan *chan, +struct l2cap_chan *new_chan);int (*recv) (struct l2cap_chan * chan,struct sk_buff *skb);void (*teardown) (struct l2cap_chan *chan,int err);
@@ -883,9 +884,10 @@ static inline __u16 __next_seq(struct l2cap_chan *chan, __u16 seq)return (seq +1 ) % (chan->tx_win_max +1 ); } -static inline struct l2cap_chan *l2cap_chan_no_new_connection (struct l2cap_chan *chan) +static inline int l2cap_chan_no_new_connection (struct l2cap_chan *chan, +struct l2cap_chan *new_chan) { -return NULL ; +return -EOPNOTSUPP ; }static inline int l2cap_chan_no_recv (struct l2cap_chan *chan,struct sk_buff *skb)
net/bluetooth/6lowpan.c +14 -13
@@ -622,6 +622,15 @@ static bool is_bt_6lowpan(struct hci_conn *hcon)return true; } +static void chan_init (struct l2cap_chan *chan) +{ +l2cap_chan_set_defaults (chan); + + chan->chan_type =L2CAP_CHAN_CONN_ORIENTED ; + chan->mode =L2CAP_MODE_LE_FLOWCTL ; + chan->imtu =1280 ; +} +static struct l2cap_chan *chan_create (void ) {struct l2cap_chan *chan;
@@ -630,11 +639,7 @@ static struct l2cap_chan *chan_create(void)if (!chan)return NULL ; -l2cap_chan_set_defaults (chan); - - chan->chan_type =L2CAP_CHAN_CONN_ORIENTED ; - chan->mode =L2CAP_MODE_LE_FLOWCTL ; - chan->imtu =1280 ; +chan_init (chan);return chan; }
@@ -743,19 +748,15 @@ static inline void chan_ready_cb(struct l2cap_chan *chan)ifup (dev->netdev); } -static inline struct l2cap_chan *chan_new_conn_cb (struct l2cap_chan *pchan) +static inline int chan_new_conn_cb (struct l2cap_chan *pchan, +struct l2cap_chan *chan) { -struct l2cap_chan *chan; - - chan =chan_create (); -if (!chan) -return NULL ; - +chan_init (chan); chan->ops = pchan->ops;BT_DBG ("chan %p pchan %p" , chan, pchan); -return chan; +return 0 ; }static void unregister_dev (struct lowpan_btle_dev *dev)
net/bluetooth/l2cap_core.c +47 -11
143 lines not on this page · raw message
net/bluetooth/l2cap_sock.c +28 -20
109 lines not on this page · raw message
net/bluetooth/smp.c +4 -9
@@ -3204,16 +3204,11 @@ static const struct l2cap_ops smp_chan_ops = { .get_sndtimeo = l2cap_chan_no_get_sndtimeo, }; -static inline struct l2cap_chan *smp_new_conn_cb (struct l2cap_chan *pchan) +static inline int smp_new_conn_cb (struct l2cap_chan *pchan, +struct l2cap_chan *chan) { -struct l2cap_chan *chan; -BT_DBG ("pchan %p" , pchan); - chan =l2cap_chan_create (); -if (!chan) -return NULL ; - chan->chan_type = pchan->chan_type; chan->ops = &smp_chan_ops; chan->scid = pchan->scid;
@@ -3229,9 +3224,9 @@ static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)*/ atomic_set (&chan->nesting,L2CAP_NESTING_SMP ); -BT_DBG ("created chan %p" , chan); +BT_DBG ("initialised chan %p" , chan); -return chan; +return 0 ; }static const struct l2cap_ops smp_root_chan_ops = {
Signature · 1 line
2.54.0
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1103029
---Test result---
Test Summary:
CheckPatch PASS 1.68 seconds VerifyFixes PASS 0.14 seconds VerifySignedoff PASS 0.14 seconds GitLint PASS 0.35 seconds SubjectPrefix PASS 0.13 seconds BuildKernel PASS 25.47 seconds CheckAllWarning PASS 27.77 seconds CheckSparse PASS 26.54 seconds BuildKernel32 PASS 24.52 seconds TestRunnerSetup PASS 530.14 seconds TestRunner_l2cap-tester FAIL 55.83 seconds TestRunner_smp-tester FAIL 23.82 seconds TestRunner_6lowpan-tester FAIL 22.93 seconds IncrementalBuild PASS 25.60 seconds
Details
##############################
Test: TestRunner_l2cap-tester - FAIL
Desc: Run l2cap-tester with test-runner
Output:
WARNING: held lock freed!
7.1.0-rc1-g5f2fcd0833d0 #1 Not tainted
-------------------------
memcheck-amd64-/34 is freeing memory ffff888002762000-ffff8880027627ff, with a lock still held there! ffff888002762508 (&chan->lock#4){+.+.}-{4:4}, at: l2cap_conn_del+0x33b/0x660
5 locks held by memcheck-amd64-/34:
#0: ffff8880026e0e80 (&hdev->req_lock){+.+.}-{4:4}, at: hci_dev_do_close+0x5d/0x90 #1: ffff8880026e00b0 (&hdev->lock){+.+.}-{4:4}, at: hci_dev_close_sync+0x2ca/0xfa0 #2: ffffffffb92bc278 (hci_cb_list_lock){+.+.}-{4:4}, at: hci_conn_hash_flush+0xed/0x230 #3: ffff8880022ec2f0 (&conn->lock){+.+.}-{4:4}, at: l2cap_conn_del+0x9b/0x660
#4: ffff888002762508 (&chan->lock#4){+.+.}-{4:4}, at: l2cap_conn_del+0x33b/0x660
stack backtrace: CPU: 0 UID: 0 PID: 34 Comm: memcheck-amd64- Not tainted 7.1.0-rc1-g5f2fcd0833d0 #1 PREEMPT(lazy) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.13.0-1ubuntu1.1 04/01/2014 Call Trace: <TASK> debug_check_no_locks_freed+0x102/0x140 ? l2cap_chan_del+0x220/0x770 kfree+0x22b/0x4c0 ? _raw_write_unlock+0x1e/0x40 l2cap_chan_del+0x220/0x770 l2cap_conn_del+0x346/0x660 hci_conn_hash_flush+0x135/0x230 hci_dev_close_sync+0x4f8/0xfa0 hci_dev_do_close+0x65/0x90 hci_unregister_dev+0x254/0x510 vhci_release+0x183/0x240 __fput+0x356/0x9c0 ? lock_is_held_type+0x9b/0x110 fput_close_sync+0xd6/0x180 ? __pfx_fput_close_sync+0x10/0x10 __x64_sys_close+0x78/0xd0 do_syscall_64+0x9f/0x560 entry_SYSCALL_64_after_hwframe+0x74/0x7c RIP: 0033:0x5805ccc9 Code: 00 e8 eb a4 fe ff 66 2e 0f 1f 84 00 00 00 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <c3> 66 0f 1f 44 00 00 f3 0f 1e fa 8d 87 ff 0f 00 00 89 fa 89 ff 3d RSP: 002b:0000001002db9d08 EFLAGS: 00000206 ORIG_RAX: 0000000000000003 RAX: ffffffffffffffda RBX: 0000001002386d90 RCX: 000000005805ccc9 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000007 ... BUG: KASAN: slab-use-after-free in l2cap_chan_del+0x6ee/0x770 Write of size 8 at addr ffff888002762000 by task memcheck-amd64-/34 CPU: 0 UID: 0 PID: 34 Comm: memcheck-amd64- Not tainted 7.1.0-rc1-g5f2fcd0833d0 #1 PREEMPT(lazy) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.13.0-1ubuntu1.1 04/01/2014 Call Trace: <TASK> print_report+0x108/0x5c0 ? __virt_addr_valid+0x21c/0x3f0 ? l2cap_chan_del+0x6ee/0x770 kasan_report+0x94/0xc0 ? l2cap_chan_del+0x6ee/0x770 l2cap_chan_del+0x6ee/0x770 l2cap_conn_del+0x346/0x660 hci_conn_hash_flush+0x135/0x230 hci_dev_close_sync+0x4f8/0xfa0 hci_dev_do_close+0x65/0x90 hci_unregister_dev+0x254/0x510 vhci_release+0x183/0x240 __fput+0x356/0x9c0 ? lock_is_held_type+0x9b/0x110 fput_close_sync+0xd6/0x180 ? __pfx_fput_close_sync+0x10/0x10 __x64_sys_close+0x78/0xd0 do_syscall_64+0x9f/0x560 entry_SYSCALL_64_after_hwframe+0x74/0x7c RIP: 0033:0x5805ccc9 Code: 00 e8 eb a4 fe ff 66 2e 0f 1f 84 00 00 00 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <c3> 66 0f 1f 44 00 00 f3 0f 1e fa 8d 87 ff 0f 00 00 89 fa 89 ff 3d RSP: 002b:0000001002db9d08 EFLAGS: 00000206 ORIG_RAX: 0000000000000003 RAX: ffffffffffffffda RBX: 0000001002386d90 RCX: 000000005805ccc9 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000007 RBP: 0000000000000001 R08: 0000000005229e80 R09: 0000000000000040 R10: 00000000000002fa R11: 0000000000000206 R12: 0000001002386e20 R13: 0000001002008360 R14: 0000000000000003 R15: 0000001002db9d70
Total: 96, Passed: 96 (100.0%), Failed: 0, Not Run: 0
##############################
Test: TestRunner_smp-tester - FAIL
Desc: Run smp-tester with test-runner
Output:
WARNING: held lock freed!
7.1.0-rc1-g5f2fcd0833d0 #1 Not tainted
-------------------------
memcheck-amd64-/34 is freeing memory ffff8880021b6000-ffff8880021b67ff, with a lock still held there! ffff8880021b6508 (&chan->lock#3){+.+.}-{4:4}, at: l2cap_conn_del+0x33b/0x660
5 locks held by memcheck-amd64-/34:
#0: ffff8880024e4e80 (&hdev->req_lock){+.+.}-{4:4}, at: hci_dev_do_close+0x5d/0x90 #1: ffff8880024e40b0 (&hdev->lock){+.+.}-{4:4}, at: hci_dev_close_sync+0x2ca/0xfa0 #2: ffffffffb6ebc278 (hci_cb_list_lock){+.+.}-{4:4}, at: hci_conn_hash_flush+0xed/0x230 #3: ffff8880022fb2f0 (&conn->lock){+.+.}-{4:4}, at: l2cap_conn_del+0x9b/0x660
#4: ffff8880021b6508 (&chan->lock#3){+.+.}-{4:4}, at: l2cap_conn_del+0x33b/0x660
stack backtrace: CPU: 0 UID: 0 PID: 34 Comm: memcheck-amd64- Not tainted 7.1.0-rc1-g5f2fcd0833d0 #1 PREEMPT(lazy) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.13.0-1ubuntu1.1 04/01/2014 Call Trace: <TASK> debug_check_no_locks_freed+0x102/0x140 ? l2cap_chan_del+0x220/0x770 kfree+0x22b/0x4c0 ? _raw_write_unlock+0x1e/0x40 l2cap_chan_del+0x220/0x770 l2cap_conn_del+0x346/0x660 hci_conn_hash_flush+0x135/0x230 hci_dev_close_sync+0x4f8/0xfa0 hci_dev_do_close+0x65/0x90 hci_unregister_dev+0x254/0x510 vhci_release+0x183/0x240 __fput+0x356/0x9c0 ? lock_is_held_type+0x9b/0x110 fput_close_sync+0xd6/0x180 ? __pfx_fput_close_sync+0x10/0x10 __x64_sys_close+0x78/0xd0 do_syscall_64+0x9f/0x560 entry_SYSCALL_64_after_hwframe+0x74/0x7c RIP: 0033:0x5805ccc9 Code: 00 e8 eb a4 fe ff 66 2e 0f 1f 84 00 00 00 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <c3> 66 0f 1f 44 00 00 f3 0f 1e fa 8d 87 ff 0f 00 00 89 fa 89 ff 3d RSP: 002b:0000001002db5d08 EFLAGS: 00000206 ORIG_RAX: 0000000000000003 RAX: ffffffffffffffda RBX: 0000001002386d90 RCX: 000000005805ccc9 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 000000000000000a ... BUG: KASAN: slab-use-after-free in l2cap_chan_del+0x6ee/0x770 Write of size 8 at addr ffff8880021b6000 by task memcheck-amd64-/34 CPU: 0 UID: 0 PID: 34 Comm: memcheck-amd64- Not tainted 7.1.0-rc1-g5f2fcd0833d0 #1 PREEMPT(lazy) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.13.0-1ubuntu1.1 04/01/2014 Call Trace: <TASK> print_report+0x108/0x5c0 ? __virt_addr_valid+0x21c/0x3f0 ? l2cap_chan_del+0x6ee/0x770 kasan_report+0x94/0xc0 ? l2cap_chan_del+0x6ee/0x770 l2cap_chan_del+0x6ee/0x770 l2cap_conn_del+0x346/0x660 hci_conn_hash_flush+0x135/0x230 hci_dev_close_sync+0x4f8/0xfa0 hci_dev_do_close+0x65/0x90 hci_unregister_dev+0x254/0x510 vhci_release+0x183/0x240 __fput+0x356/0x9c0 ? lock_is_held_type+0x9b/0x110 fput_close_sync+0xd6/0x180 ? __pfx_fput_close_sync+0x10/0x10 __x64_sys_close+0x78/0xd0 do_syscall_64+0x9f/0x560 entry_SYSCALL_64_after_hwframe+0x74/0x7c RIP: 0033:0x5805ccc9 Code: 00 e8 eb a4 fe ff 66 2e 0f 1f 84 00 00 00 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <c3> 66 0f 1f 44 00 00 f3 0f 1e fa 8d 87 ff 0f 00 00 89 fa 89 ff 3d RSP: 002b:0000001002db5d08 EFLAGS: 00000206 ORIG_RAX: 0000000000000003 RAX: ffffffffffffffda RBX: 0000001002386d90 RCX: 000000005805ccc9 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 000000000000000a RBP: 0000000000000001 R08: 0000000004c198e0 R09: 0000000000000040 R10: 0000000000000002 R11: 0000000000000206 R12: 0000001002386e20 R13: 0000001002008360 R14: 0000000000000003 R15: 0000001002db5d70
Total: 8, Passed: 8 (100.0%), Failed: 0, Not Run: 0
##############################
Test: TestRunner_6lowpan-tester - FAIL
Desc: Run 6lowpan-tester with test-runner
Output:
WARNING: held lock freed!
7.1.0-rc1-g5f2fcd0833d0 #1 Not tainted
-------------------------
memcheck-amd64-/34 is freeing memory ffff8880026af000-ffff8880026af7ff, with a lock still held there! ffff8880026af508 (&chan->lock#4){+.+.}-{4:4}, at: l2cap_conn_del+0x33b/0x660
5 locks held by memcheck-amd64-/34:
#0: ffff8880025ece80 (&hdev->req_lock){+.+.}-{4:4}, at: hci_dev_do_close+0x5d/0x90 #1: ffff8880025ec0b0 (&hdev->lock){+.+.}-{4:4}, at: hci_dev_close_sync+0x2ca/0xfa0 #2: ffffffff84ebc278 (hci_cb_list_lock){+.+.}-{4:4}, at: hci_conn_hash_flush+0xed/0x230 #3: ffff888001fcbaf0 (&conn->lock){+.+.}-{4:4}, at: l2cap_conn_del+0x9b/0x660
#4: ffff8880026af508 (&chan->lock#4){+.+.}-{4:4}, at: l2cap_conn_del+0x33b/0x660
stack backtrace: CPU: 0 UID: 0 PID: 34 Comm: memcheck-amd64- Not tainted 7.1.0-rc1-g5f2fcd0833d0 #1 PREEMPT(lazy) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.13.0-1ubuntu1.1 04/01/2014 Call Trace: <TASK> debug_check_no_locks_freed+0x102/0x140 ? l2cap_chan_del+0x220/0x770 kfree+0x22b/0x4c0 ? _raw_write_unlock+0x1e/0x40 l2cap_chan_del+0x220/0x770 l2cap_conn_del+0x346/0x660 hci_conn_hash_flush+0x135/0x230 hci_dev_close_sync+0x4f8/0xfa0 hci_dev_do_close+0x65/0x90 hci_unregister_dev+0x254/0x510 vhci_release+0x183/0x240 __fput+0x356/0x9c0 ? lock_is_held_type+0x9b/0x110 fput_close_sync+0xd6/0x180 ? __pfx_fput_close_sync+0x10/0x10 __x64_sys_close+0x78/0xd0 do_syscall_64+0x9f/0x560 entry_SYSCALL_64_after_hwframe+0x74/0x7c RIP: 0033:0x5805ccc9 Code: 00 e8 eb a4 fe ff 66 2e 0f 1f 84 00 00 00 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <c3> 66 0f 1f 44 00 00 f3 0f 1e fa 8d 87 ff 0f 00 00 89 fa 89 ff 3d RSP: 002b:0000001002cb5d08 EFLAGS: 00000206 ORIG_RAX: 0000000000000003 RAX: ffffffffffffffda RBX: 0000001002386d90 RCX: 000000005805ccc9 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000007 ... BUG: KASAN: slab-use-after-free in l2cap_chan_del+0x6ee/0x770 Write of size 8 at addr ffff8880026af000 by task memcheck-amd64-/34 CPU: 0 UID: 0 PID: 34 Comm: memcheck-amd64- Not tainted 7.1.0-rc1-g5f2fcd0833d0 #1 PREEMPT(lazy) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.13.0-1ubuntu1.1 04/01/2014 Call Trace: <TASK> print_report+0x108/0x5c0 ? __virt_addr_valid+0x21c/0x3f0 ? l2cap_chan_del+0x6ee/0x770 kasan_report+0x94/0xc0 ? l2cap_chan_del+0x6ee/0x770 l2cap_chan_del+0x6ee/0x770 l2cap_conn_del+0x346/0x660 hci_conn_hash_flush+0x135/0x230 hci_dev_close_sync+0x4f8/0xfa0 hci_dev_do_close+0x65/0x90 hci_unregister_dev+0x254/0x510 vhci_release+0x183/0x240 __fput+0x356/0x9c0 ? lock_is_held_type+0x9b/0x110 fput_close_sync+0xd6/0x180 ? __pfx_fput_close_sync+0x10/0x10 __x64_sys_close+0x78/0xd0 do_syscall_64+0x9f/0x560 entry_SYSCALL_64_after_hwframe+0x74/0x7c RIP: 0033:0x5805ccc9 Code: 00 e8 eb a4 fe ff 66 2e 0f 1f 84 00 00 00 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <c3> 66 0f 1f 44 00 00 f3 0f 1e fa 8d 87 ff 0f 00 00 89 fa 89 ff 3d RSP: 002b:0000001002cb5d08 EFLAGS: 00000206 ORIG_RAX: 0000000000000003 RAX: ffffffffffffffda RBX: 0000001002386d90 RCX: 000000005805ccc9 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000007 RBP: 0000000000000001 R08: 0000000004c205f0 R09: 0000000000000040 R10: 0000000000000002 R11: 0000000000000206 R12: 0000001002386e20 R13: 0000001002008360 R14: 0000000000000003 R15: 0000001002cb5d70
Total: 8, Passed: 8 (100.0%), Failed: 0, Not Run: 0
https://github.com/bluez/bluetooth-next/pull/256
---
Regards,
Linux Bluetooth