Bluetooth: L2CAP: Fix use-after-free in l2cap_sock_new_connection_cb()
Subsystems (2)
v10 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.
Compared to v6, balance puts and holds on chans.
Compared to v7, rebase against bluetooth-next and refactor the chan refcounting.
Compared to v8, adopt the philosophy of one assignment one reference. Make refcounting easier to follow.
Compared to v9, rework on l2cap_chan_set_defaults so that new chan's default comes from pchan and can be used by __l2cap_chan_add.
Siwei Zhang (1):
Bluetooth: L2CAP: Fix use-after-free in l2cap_sock_new_connection_cb()
include/net/bluetooth/l2cap.h | 10 ++-- net/bluetooth/6lowpan.c | 18 +----- net/bluetooth/l2cap_core.c | 78 ++++++++++++++++++++----- net/bluetooth/l2cap_sock.c | 103 ++++++++++++++++------------------ net/bluetooth/smp.c | 27 ++------- 5 files changed, 127 insertions(+), 109 deletions(-)
Signature · 1 line
2.54.0
PATCH v10 1/1 Bluetooth: L2CAP: Fix use-after-free in l2cap_sock_new_connection_cb()
l2cap_sock_new_connection_cb() returned l2cap_pi(sk)->chan after release_sock(parent). Once the parent lock is dropped the newly enqueued child socket sk is reachable via the accept queue, so another task can accept and free it before the callback dereferences sk, resulting in a use-after-free.
Rework the ->new_connection() op so the core, rather than the callback, owns the child channel's lifetime. The op now receives a pre-allocated new_chan and returns an errno instead of allocating and returning a channel. l2cap_new_connection() allocates the child channel and links it into the conn list via __l2cap_chan_add() before invoking the callback, so the conn-list reference keeps the channel alive once release_sock(parent) exposes the socket to other tasks.
Channel configuration that was duplicated in l2cap_sock_init() and the various new_connection callbacks is consolidated into l2cap_chan_set_defaults(), which now inherits from the parent channel when one is supplied.
--- include/net/bluetooth/l2cap.h | 10 ++-- net/bluetooth/6lowpan.c | 18 +----- net/bluetooth/l2cap_core.c | 78 ++++++++++++++++++++----- net/bluetooth/l2cap_sock.c | 103 ++++++++++++++++------------------ net/bluetooth/smp.c | 27 ++------- 5 files changed, 127 insertions(+), 109 deletions(-)
include/net/bluetooth/l2cap.h +6 -4
@@ -620,7 +620,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);
@@ -884,9 +885,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)
@@ -963,7 +965,7 @@ int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len,void l2cap_chan_busy (struct l2cap_chan *chan,int busy);void l2cap_chan_rx_avail (struct l2cap_chan *chan,ssize_t rx_avail);int l2cap_chan_check_security (struct l2cap_chan *chan,bool initiator); -void l2cap_chan_set_defaults (struct l2cap_chan *chan); +void l2cap_chan_set_defaults (struct l2cap_chan *chan,struct l2cap_chan *pchan);int l2cap_ertm_init (struct l2cap_chan *chan);void l2cap_chan_add (struct l2cap_conn *conn,struct l2cap_chan *chan);void __l2cap_chan_add (struct l2cap_conn *conn,struct l2cap_chan *chan);
net/bluetooth/6lowpan.c +1 -17
@@ -632,7 +632,7 @@ static struct l2cap_chan *chan_create(void)if (!chan)return NULL ; -l2cap_chan_set_defaults (chan); +l2cap_chan_set_defaults (chan,NULL ); chan->chan_type =L2CAP_CHAN_CONN_ORIENTED ; chan->mode =L2CAP_MODE_LE_FLOWCTL ;
@@ -745,21 +745,6 @@ 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) -{ -struct l2cap_chan *chan; - - chan =chan_create (); -if (!chan) -return NULL ; - - chan->ops = pchan->ops; - -BT_DBG ("chan %p pchan %p" , chan, pchan); - -return chan; -} -static void unregister_dev (struct lowpan_btle_dev *dev) {struct hci_dev *hdev =READ_ONCE (dev->hdev);
@@ -901,7 +886,6 @@ static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)static const struct l2cap_ops bt_6lowpan_chan_ops = { .name ="L2CAP 6LoWPAN channel" , - .new_connection = chan_new_conn_cb, .recv = chan_recv_cb, .close = chan_close_cb, .state_change = chan_state_change_cb,
net/bluetooth/l2cap_core.c +65 -13
143 lines not on this page · raw message
net/bluetooth/l2cap_sock.c +49 -54
201 lines not on this page · raw message
net/bluetooth/smp.c +6 -21
@@ -3204,34 +3204,19 @@ 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 *chan, +struct l2cap_chan *new_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; - chan->dcid = chan->scid; - chan->imtu = pchan->imtu; - chan->omtu = pchan->omtu; - chan->mode = pchan->mode; + new_chan->ops = &smp_chan_ops;/* Other L2CAP channels may request SMP routines in order to * change the security level. This means that the SMP channel * lock must be considered in its own category to avoid lockdep * warnings. */ -atomic_set (&chan->nesting,L2CAP_NESTING_SMP ); - -BT_DBG ("created chan %p" , chan); +atomic_set (&new_chan->nesting,L2CAP_NESTING_SMP ); -return chan; +return 0 ; }static const struct l2cap_ops smp_root_chan_ops = {
@@ -3291,7 +3276,7 @@ static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)l2cap_add_scid (chan, cid); -l2cap_chan_set_defaults (chan); +l2cap_chan_set_defaults (chan,NULL );if (cid ==L2CAP_CID_SMP ) {u8 bdaddr_type;
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=1110137
---Test result---
Test Summary:
CheckPatch PASS 1.67 seconds VerifyFixes PASS 0.13 seconds VerifySignedoff PASS 0.12 seconds GitLint PASS 0.32 seconds SubjectPrefix PASS 0.12 seconds BuildKernel FAIL 25.18 seconds CheckAllWarning FAIL 28.90 seconds CheckSparse FAIL 26.64 seconds BuildKernel32 FAIL 24.50 seconds TestRunnerSetup FAIL 510.15 seconds TestRunner_l2cap-tester FAIL 0.13 seconds TestRunner_smp-tester FAIL 0.13 seconds TestRunner_6lowpan-tester FAIL 0.13 seconds IncrementalBuild PASS 23.83 seconds
Details
##############################
Test: BuildKernel - FAIL
Desc: Build Kernel for Bluetooth
Output:
net/bluetooth/6lowpan.c:768:13: error: redefinition of ‘unregister_dev’
768 | static void unregister_dev(struct lowpan_btle_dev *dev)
| ^~~~~~~~~~~~~~
net/bluetooth/6lowpan.c:748:13: note: previous definition of ‘unregister_dev’ was here
748 | static void unregister_dev(struct lowpan_btle_dev *dev)
| ^~~~~~~~~~~~~~net/bluetooth/6lowpan.c:748:13: warning: ‘unregister_dev’ defined but not used [-Wunused-function] make[4]: *** [scripts/Makefile.build:289: net/bluetooth/6lowpan.o] Error 1 make[3]: *** [scripts/Makefile.build:548: net/bluetooth] Error 2
make[2]: *** [scripts/Makefile.build:548: net] Error 2
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [/github/workspace/src/src/Makefile:2143: .] Error 2
make: *** [Makefile:248: __sub-make] Error 2
##############################
Test: CheckAllWarning - FAIL
Desc: Run linux kernel with all warning enabled
Output:
net/bluetooth/6lowpan.c:768:13: error: redefinition of ‘unregister_dev’
768 | static void unregister_dev(struct lowpan_btle_dev *dev)
| ^~~~~~~~~~~~~~
net/bluetooth/6lowpan.c:748:13: note: previous definition of ‘unregister_dev’ was here
748 | static void unregister_dev(struct lowpan_btle_dev *dev)
| ^~~~~~~~~~~~~~net/bluetooth/6lowpan.c:748:13: warning: ‘unregister_dev’ defined but not used [-Wunused-function] make[4]: *** [scripts/Makefile.build:289: net/bluetooth/6lowpan.o] Error 1 make[4]: *** Waiting for unfinished jobs....
make[3]: *** [scripts/Makefile.build:548: net/bluetooth] Error 2
make[2]: *** [scripts/Makefile.build:548: net] Error 2
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [/github/workspace/src/src/Makefile:2143: .] Error 2
make: *** [Makefile:248: __sub-make] Error 2
##############################
Test: CheckSparse - FAIL
Desc: Run sparse tool with linux kernel
Output:
/github/workspace/src/src/Makefile:1246: C=1 specified, but sparse is not available or not up to date /github/workspace/src/src/Makefile:1246: C=1 specified, but sparse is not available or not up to date net/bluetooth/6lowpan.c:768:13: error: redefinition of ‘unregister_dev’
768 | static void unregister_dev(struct lowpan_btle_dev *dev)
| ^~~~~~~~~~~~~~
net/bluetooth/6lowpan.c:748:13: note: previous definition of ‘unregister_dev’ was here
748 | static void unregister_dev(struct lowpan_btle_dev *dev)
| ^~~~~~~~~~~~~~net/bluetooth/6lowpan.c:748:13: warning: ‘unregister_dev’ defined but not used [-Wunused-function] make[4]: *** [scripts/Makefile.build:289: net/bluetooth/6lowpan.o] Error 1 make[4]: *** Waiting for unfinished jobs....
make[3]: *** [scripts/Makefile.build:548: net/bluetooth] Error 2
make[2]: *** [scripts/Makefile.build:548: net] Error 2
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [/github/workspace/src/src/Makefile:2143: .] Error 2
make: *** [Makefile:248: __sub-make] Error 2
##############################
Test: BuildKernel32 - FAIL
Desc: Build 32bit Kernel for Bluetooth
Output:
net/bluetooth/6lowpan.c:768:13: error: redefinition of ‘unregister_dev’
768 | static void unregister_dev(struct lowpan_btle_dev *dev)
| ^~~~~~~~~~~~~~
net/bluetooth/6lowpan.c:748:13: note: previous definition of ‘unregister_dev’ was here
748 | static void unregister_dev(struct lowpan_btle_dev *dev)
| ^~~~~~~~~~~~~~net/bluetooth/6lowpan.c:748:13: warning: ‘unregister_dev’ defined but not used [-Wunused-function] make[4]: *** [scripts/Makefile.build:289: net/bluetooth/6lowpan.o] Error 1 make[3]: *** [scripts/Makefile.build:548: net/bluetooth] Error 2
make[2]: *** [scripts/Makefile.build:548: net] Error 2
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [/github/workspace/src/src/Makefile:2143: .] Error 2
make: *** [Makefile:248: __sub-make] Error 2
##############################
Test: TestRunnerSetup - FAIL
Desc: Setup kernel and bluez for test-runner
Output:
Kernel:net/bluetooth/6lowpan.c:768:13: error: redefinition of ‘unregister_dev’
768 | static void unregister_dev(struct lowpan_btle_dev *dev)
| ^~~~~~~~~~~~~~
net/bluetooth/6lowpan.c:748:13: note: previous definition of ‘unregister_dev’ was here
748 | static void unregister_dev(struct lowpan_btle_dev *dev)
| ^~~~~~~~~~~~~~net/bluetooth/6lowpan.c:748:13: warning: ‘unregister_dev’ defined but not used [-Wunused-function] make[4]: *** [scripts/Makefile.build:289: net/bluetooth/6lowpan.o] Error 1 make[3]: *** [scripts/Makefile.build:548: net/bluetooth] Error 2
make[2]: *** [scripts/Makefile.build:548: net] Error 2
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [/github/workspace/src/src/Makefile:2143: .] Error 2
make: *** [Makefile:248: __sub-make] Error 2
##############################
Test: TestRunner_l2cap-tester - FAIL
Desc: Run l2cap-tester with test-runner
Output:
No kernel image found
##############################
Test: TestRunner_smp-tester - FAIL
Desc: Run smp-tester with test-runner
Output:
No kernel image found
##############################
Test: TestRunner_6lowpan-tester - FAIL
Desc: Run 6lowpan-tester with test-runner
Output:
No kernel image found
https://github.com/bluez/bluetooth-next/pull/304
---
Regards,
Linux Bluetooth