Bluetooth: L2CAP: Fix use-after-free in l2cap_sock_new_connection_cb()
Subsystems (2)
v9 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.
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 | 31 +++++++------ net/bluetooth/l2cap_core.c | 41 ++++++++++++----- net/bluetooth/l2cap_sock.c | 83 +++++++++++++++++++++-------------- net/bluetooth/smp.c | 17 ++++--- 5 files changed, 113 insertions(+), 69 deletions(-)
Signature · 1 line
2.54.0
PATCH v9 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 | 10 +++-- net/bluetooth/6lowpan.c | 31 +++++++------ net/bluetooth/l2cap_core.c | 41 ++++++++++++----- net/bluetooth/l2cap_sock.c | 83 +++++++++++++++++++++-------------- net/bluetooth/smp.c | 17 ++++--- 5 files changed, 113 insertions(+), 69 deletions(-)
include/net/bluetooth/l2cap.h +7 -3
@@ -620,7 +620,9 @@ struct l2cap_chan {struct l2cap_ops {char *name; -struct l2cap_chan *(*new_connection) (struct l2cap_chan *chan); +int (*new_connection)(struct l2cap_conn *conn, +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 +886,11 @@ 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_conn *conn, +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 +18 -13
@@ -624,6 +624,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;
@@ -632,11 +641,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; }
@@ -745,19 +750,19 @@ 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_conn *conn, +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; +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); +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 +29 -12
94 lines not on this page · raw message
net/bluetooth/l2cap_sock.c +51 -32
166 lines not on this page · raw message
net/bluetooth/smp.c +8 -9
@@ -3204,16 +3204,12 @@ 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_conn *conn, +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 +3225,12 @@ 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); +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); -return chan; +BT_DBG ("initialised chan %p" , 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=1105389
---Test result---
Test Summary:
CheckPatch PASS 1.55 seconds VerifyFixes PASS 0.10 seconds VerifySignedoff PASS 0.07 seconds GitLint PASS 0.32 seconds SubjectPrefix PASS 0.07 seconds BuildKernel PASS 26.94 seconds CheckAllWarning PASS 29.57 seconds CheckSparse PASS 27.88 seconds BuildKernel32 PASS 26.28 seconds TestRunnerSetup PASS 580.30 seconds TestRunner_l2cap-tester PASS 60.03 seconds TestRunner_smp-tester PASS 23.45 seconds TestRunner_6lowpan-tester PASS 22.67 seconds IncrementalBuild PASS 25.76 seconds
https://github.com/bluez/bluetooth-next/pull/283
---
Regards,
Linux Bluetooth
Luiz Augusto von Dentz
maintainerHi Siwei,
Quoted 128 lines · Siwei Zhang
l2cap_sock_new_connection_cb()accesses l2cap_pi(sk)->chan afterrelease_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.
Fixes 8ffb929098a5 ("Bluetooth: Remove parent socket usage from l2cap_core.c")Assisted-by Claude:claude-opus-4-8Signed-off-by Siwei Zhang <[email protected]>--- include/net/bluetooth/l2cap.h | 10 +++-- net/bluetooth/6lowpan.c | 31 +++++++------ net/bluetooth/l2cap_core.c | 41 ++++++++++++----- net/bluetooth/l2cap_sock.c | 83 +++++++++++++++++++++-------------- net/bluetooth/smp.c | 17 ++++--- 5 files changed, 113 insertions(+), 69 deletions(-)include/net/bluetooth/l2cap.h +7
-3@@ -620,7 +620,9 @@ struct l2cap_chan {struct l2cap_ops {char *name; -struct l2cap_chan *(*new_connection) (struct l2cap_chan *chan); +int (*new_connection)(struct l2cap_conn *conn, +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 +886,11 @@ 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_conn *conn, +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 +18
-13@@ -624,6 +624,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;@@ -632,11 +641,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; }@@ -745,19 +750,19 @@ 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_conn *conn, +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; +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); +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 +11
-0@@ -4007,6 +4007,31 @@ static inline int l2cap_command_rej(struct l2cap_conn *conn,return 0 ; } +/* Allocate and initialise a channel for an incoming connection. +* +* ->new_connection() initialises the channel and links it into @conn with +* __l2cap_chan_add(). The l2cap_chan_create() reference becomes the one owned +* by the parent subsystem (l2cap_pi(sk)->chan, conn->smp or peer->chan) and is +* released by its teardown callback; the conn list reference is released by +* l2cap_chan_del(). +*/ +static struct l2cap_chan *l2cap_new_connection (struct l2cap_conn *conn, +struct l2cap_chan *pchan) +{
net/bluetooth/l2cap_core.c static inline int l2cap_command_rej(struct l2cap_conn *conn, · line 4030
Quoted 10 lines
net/bluetooth/l2cap_core.c +10
-0+struct l2cap_chan *chan; + + chan =l2cap_chan_create (); +if (!chan) +return NULL ; + +if (pchan->ops->new_connection (conn, pchan, chan) <0 ) { +l2cap_chan_put (chan); +return NULL ; + }
I don't quite get why we can't just place __l2cap_chan_add here instead of having it called by new_connection callbacks?
Quoted 299 lines
net/bluetooth/l2cap_core.c +8
-12+ +return chan; +} +static void l2cap_connect (struct l2cap_conn *conn,struct l2cap_cmd_hdr *cmd,u8 *data,u8 rsp_code) {@@ -4053,7 +4078,7 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, goto response; } - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (!chan) goto response;@@ -4071,8 +4096,6 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, chan->psm = psm; chan->dcid = scid; -__l2cap_chan_add (conn, chan); - dcid = chan->scid;__set_chan_timer (chan, chan->ops->get_sndtimeo (chan));@@ -4955,7 +4978,7 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn, goto response_unlock; } - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (!chan) { result =L2CAP_CR_LE_NO_MEM ; goto response_unlock;@@ -4970,8 +4993,6 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn, chan->omtu = mtu; chan->remote_mps = mps; -__l2cap_chan_add (conn, chan); -l2cap_le_flowctl_init (chan,__le16_to_cpu (req->credits)); dcid = chan->scid;@@ -5179,7 +5200,7 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,continue ; } - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (!chan) { result =L2CAP_CR_LE_NO_MEM ;continue ;@@ -5194,8 +5215,6 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn, chan->omtu = mtu; chan->remote_mps = mps; -__l2cap_chan_add (conn, chan); -l2cap_ecred_init (chan,__le16_to_cpu (req->credits));/* Init response */ @@ -7470,14 +7489,12 @@ static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) goto next;l2cap_chan_lock (pchan); - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (chan) {bacpy (&chan->src, &hcon->src);bacpy (&chan->dst, &hcon->dst); chan->src_type =bdaddr_src_type (hcon); chan->dst_type = dst_type; - -__l2cap_chan_add (conn, chan); }l2cap_chan_unlock (pchan);net/bluetooth/l2cap_sock.c +51
-32@@ -46,7 +46,8 @@ static struct bt_sock_list l2cap_sk_list = {static const struct proto_ops l2cap_sock_ops;static void l2cap_sock_init (struct sock *sk,struct sock *parent);static struct sock *l2cap_sock_alloc (struct net *net,struct socket *sock, -int proto,gfp_t prio,int kern); +int proto,gfp_t prio,int kern, +struct l2cap_chan *chan);static void l2cap_sock_cleanup_listen (struct sock *parent);bool l2cap_is_socket (struct socket *sock)@@ -1287,6 +1288,23 @@ static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg,return err; } +/* Release the sock's ref on chan and clear the pointer so that the ref is +* dropped exactly once even if both l2cap_sock_kill() and +* l2cap_sock_destruct() run. Setting chan->data to NULL first stops any other +* task from dereferencing the now-dead sock pointer. +*/ +static void l2cap_sock_put_chan (struct sock *sk) +{ +struct l2cap_chan *chan =l2cap_pi (sk)->chan; + +if (!chan) +return ; + + chan->data =NULL ; +l2cap_pi (sk)->chan =NULL ; +l2cap_chan_put (chan); +} +/* Kill socket (only if zapped and orphan) * Must be called on unlocked socket, with l2cap channel lock. */ @@ -1297,13 +1315,9 @@ static void l2cap_sock_kill(struct sock *sk)BT_DBG ("sk %p state %s" , sk,state_to_string (sk->sk_state)); -/* Sock is dead, so set chan data to NULL, avoid other task use invalid -* sock pointer. -*/ -l2cap_pi (sk)->chan->data =NULL ; -/* Kill poor orphan */ +l2cap_sock_put_chan (sk); -l2cap_chan_put (l2cap_pi (sk)->chan); +/* Kill poor orphan */ sock_set_flag (sk,SOCK_DEAD );sock_put (sk); }@@ -1546,12 +1560,14 @@ static void l2cap_sock_cleanup_listen(struct sock *parent) } } -static struct l2cap_chan *l2cap_sock_new_connection_cb (struct l2cap_chan *chan) +static int l2cap_sock_new_connection_cb (struct l2cap_conn *conn, +struct l2cap_chan *chan, +struct l2cap_chan *new_chan) {struct sock *sk, *parent = chan->data;if (!parent) -return NULL ; +return -EINVAL ;lock_sock (parent);@@ -1559,25 +1575,33 @@ static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)if (sk_acceptq_is_full (parent)) {BT_DBG ("backlog full %d" , parent->sk_ack_backlog);release_sock (parent); -return NULL ; +return -ENOBUFS ; } sk =l2cap_sock_alloc (sock_net (parent),NULL ,BTPROTO_L2CAP , -GFP_ATOMIC ,0 ); +GFP_ATOMIC ,0 , new_chan);if (!sk) {release_sock (parent); -return NULL ; - } +return -ENOMEM ; + }bt_sock_reclassify_lock (sk,BTPROTO_L2CAP );l2cap_sock_init (sk, parent); +/* Link the channel into conn before exposing the new socket via the +* accept queue. Once release_sock() below drops the parent lock the +* socket may be freed by another task, dropping its reference on +* new_chan; the conn list reference taken here keeps new_chan alive so +* the caller can safely use it after ->new_connection() returns. +*/ +__l2cap_chan_add (conn, new_chan); +bt_accept_enqueue (parent, sk, false);release_sock (parent); -return l2cap_pi (sk)->chan; +return 0 ; }static int l2cap_sock_recv_cb (struct l2cap_chan *chan,struct sk_buff *skb)@@ -1874,10 +1898,7 @@ static void l2cap_sock_destruct(struct sock *sk)BT_DBG ("sk %p" , sk); -if (l2cap_pi (sk)->chan) { -l2cap_pi (sk)->chan->data =NULL ; -l2cap_chan_put (l2cap_pi (sk)->chan); - } +l2cap_sock_put_chan (sk);list_for_each_entry_safe (rx_busy, next, &l2cap_pi (sk)->rx_busy, list) {kfree_skb (rx_busy->skb);@@ -1978,10 +1999,10 @@ static struct proto l2cap_proto = { };static struct sock *l2cap_sock_alloc (struct net *net,struct socket *sock, -int proto,gfp_t prio,int kern) +int proto,gfp_t prio,int kern, +struct l2cap_chan *chan) {struct sock *sk; -struct l2cap_chan *chan; sk =bt_sock_alloc (net, sock, &l2cap_proto, proto, prio, kern);if (!sk)@@ -1992,16 +2013,7 @@ static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,INIT_LIST_HEAD (&l2cap_pi (sk)->rx_busy); - chan =l2cap_chan_create (); -if (!chan) { -sk_free (sk); -if (sock) - sock->sk =NULL ; -return NULL ; - } - -l2cap_chan_hold (chan); - +/* The sock takes ownership of the caller's reference on chan. */ l2cap_pi (sk)->chan = chan;return sk;@@ -2011,6 +2023,7 @@ static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, int kern) {struct sock *sk; +struct l2cap_chan *chan;BT_DBG ("sock %p" , sock);@@ -2025,10 +2038,16 @@ static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, sock->ops = &l2cap_sock_ops; - sk =l2cap_sock_alloc (net, sock, protocol,GFP_ATOMIC , kern); -if (!sk) + chan =l2cap_chan_create (); +if (!chan)return -ENOMEM ; + sk =l2cap_sock_alloc (net, sock, protocol,GFP_ATOMIC , kern, chan); +if (!sk) { +l2cap_chan_put (chan); +return -ENOMEM ; + } +l2cap_sock_init (sk,NULL );bt_sock_link (&l2cap_sk_list, sk);return 0 ;net/bluetooth/smp.c +8
-10@@ -3204,16 +3204,12 @@ 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_conn *conn, +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 +3225,12 @@ 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); +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); -return chan; +BT_DBG ("initialised chan %p" , chan); + +return 0 ; }static const struct l2cap_ops smp_root_chan_ops = { --2.54.0
Signature · 1 line
Luiz Augusto von Dentz
Hi Luiz,
Quoted 136 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 133 lines · Siwei Zhang
l2cap_sock_new_connection_cb()accesses l2cap_pi(sk)->chan afterrelease_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.
Fixes 8ffb929098a5 ("Bluetooth: Remove parent socket usage from l2cap_core.c")Assisted-by Claude:claude-opus-4-8Signed-off-by Siwei Zhang <[email protected]>--- include/net/bluetooth/l2cap.h | 10 +++-- net/bluetooth/6lowpan.c | 31 +++++++------ net/bluetooth/l2cap_core.c | 41 ++++++++++++----- net/bluetooth/l2cap_sock.c | 83 +++++++++++++++++++++-------------- net/bluetooth/smp.c | 17 ++++--- 5 files changed, 113 insertions(+), 69 deletions(-)include/net/bluetooth/l2cap.h +7
-3@@ -620,7 +620,9 @@ struct l2cap_chan {struct l2cap_ops {char *name; -struct l2cap_chan *(*new_connection) (struct l2cap_chan *chan); +int (*new_connection)(struct l2cap_conn *conn, +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 +886,11 @@ 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_conn *conn, +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 +18
-13@@ -624,6 +624,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;@@ -632,11 +641,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; }@@ -745,19 +750,19 @@ 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_conn *conn, +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; +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); +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 +16
-0@@ -4007,6 +4007,31 @@ static inline int l2cap_command_rej(struct l2cap_conn *conn,return 0 ; } +/* Allocate and initialise a channel for an incoming connection. +* +* ->new_connection() initialises the channel and links it into @conn with +* __l2cap_chan_add(). The l2cap_chan_create() reference becomes the one owned +* by the parent subsystem (l2cap_pi(sk)->chan, conn->smp or peer->chan) and is +* released by its teardown callback; the conn list reference is released by +* l2cap_chan_del(). +*/ +static struct l2cap_chan *l2cap_new_connection (struct l2cap_conn *conn, +struct l2cap_chan *pchan) +{ +struct l2cap_chan *chan; + + chan =l2cap_chan_create (); +if (!chan) +return NULL ;
Quoted 9 lines
net/bluetooth/l2cap_core.c static inline int l2cap_command_rej(struct l2cap_conn *conn, · line 4030
Quoted 5 lines
net/bluetooth/l2cap_core.c +5
-0+ +if (pchan->ops->new_connection (conn, pchan, chan) <0 ) { +l2cap_chan_put (chan); +return NULL ; + }I don't quite get why we can't just place
__l2cap_chan_addhere instead of having it called bynew_connectioncallbacks?
It's specifically the l2cap_sock_new_connection_cb() case - the very use-after-free this patch fixes. The __l2cap_chan_add() has to happen while the parent lock is still held, and only the callback holds that lock.
The reference counting on the new child chan starts at one ref, owned by the new socket:
/* l2cap_new_connection() */ chan = l2cap_chan_create(); /* refcount = 1 */ if (!chan) return NULL; pchan->ops->new_connection(conn, pchan, chan);
and inside the socket callback:
/* l2cap_sock_new_connection_cb() */ lock_sock(parent); ... sk = l2cap_sock_alloc(..., new_chan); /* sk owns the chan_create ref */ ... l2cap_sock_init(sk, parent); __l2cap_chan_add(conn, new_chan); /* (A) conn list takes a ref */ bt_accept_enqueue(parent, sk, false); /* (B) sk now on accept queue */ release_sock(parent); /* (C) parent lock dropped */ return 0;
The moment we hit (C), sk is reachable through the parent's accept queue, so another task can grab and tear it down:
accept() -> l2cap_sock_kill() -> l2cap_sock_put_chan() chan->data = NULL; l2cap_chan_put(chan); /* drops the sk's chan ref */
If __l2cap_chan_add() at (A) hadn't already taken the conn list reference, that put would drop the last ref and free new_chan. Control then returns up to l2cap_new_connection(), which hands the now-freed chan back to l2cap_connect():
/* l2cap_connect() - runs after the callback returns */ chan = l2cap_new_connection(conn, pchan); if (!chan) goto response; ... bacpy(&chan->src, &conn->hcon->src); /* <-- UAF on freed chan */ chan->psm = psm; chan->dcid = scid;
The conn list reference taken at (A), before (C), is what keeps new_chan alive across the release_sock() window so l2cap_connect() can keep using it.
So __l2cap_chan_add() can't move out to l2cap_new_connection(): by the time the callback returns, the parent lock is already dropped and chan may already be freed - which is exactly the race. It has to be taken inside the callback, under the parent lock, before the socket is exposed.
The other callbacks (6lowpan, smp) have no equivalent lock-drop window.
I kept __l2cap_chan_add() inside all of the ->new_connection() callbacks
just to keep the "callback links the channel into conn" contract uniform.
Quoted 295 lines
Quoted 295 lines
net/bluetooth/l2cap_core.c +8
-12+ +return chan; +} +static void l2cap_connect (struct l2cap_conn *conn,struct l2cap_cmd_hdr *cmd,u8 *data,u8 rsp_code) {@@ -4053,7 +4078,7 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, goto response; } - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (!chan) goto response;@@ -4071,8 +4096,6 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, chan->psm = psm; chan->dcid = scid; -__l2cap_chan_add (conn, chan); - dcid = chan->scid;__set_chan_timer (chan, chan->ops->get_sndtimeo (chan));@@ -4955,7 +4978,7 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn, goto response_unlock; } - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (!chan) { result =L2CAP_CR_LE_NO_MEM ; goto response_unlock;@@ -4970,8 +4993,6 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn, chan->omtu = mtu; chan->remote_mps = mps; -__l2cap_chan_add (conn, chan); -l2cap_le_flowctl_init (chan,__le16_to_cpu (req->credits)); dcid = chan->scid;@@ -5179,7 +5200,7 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,continue ; } - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (!chan) { result =L2CAP_CR_LE_NO_MEM ;continue ;@@ -5194,8 +5215,6 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn, chan->omtu = mtu; chan->remote_mps = mps; -__l2cap_chan_add (conn, chan); -l2cap_ecred_init (chan,__le16_to_cpu (req->credits));/* Init response */ @@ -7470,14 +7489,12 @@ static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) goto next;l2cap_chan_lock (pchan); - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (chan) {bacpy (&chan->src, &hcon->src);bacpy (&chan->dst, &hcon->dst); chan->src_type =bdaddr_src_type (hcon); chan->dst_type = dst_type; - -__l2cap_chan_add (conn, chan); }l2cap_chan_unlock (pchan);net/bluetooth/l2cap_sock.c +51
-32@@ -46,7 +46,8 @@ static struct bt_sock_list l2cap_sk_list = {static const struct proto_ops l2cap_sock_ops;static void l2cap_sock_init (struct sock *sk,struct sock *parent);static struct sock *l2cap_sock_alloc (struct net *net,struct socket *sock, -int proto,gfp_t prio,int kern); +int proto,gfp_t prio,int kern, +struct l2cap_chan *chan);static void l2cap_sock_cleanup_listen (struct sock *parent);bool l2cap_is_socket (struct socket *sock)@@ -1287,6 +1288,23 @@ static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg,return err; } +/* Release the sock's ref on chan and clear the pointer so that the ref is +* dropped exactly once even if both l2cap_sock_kill() and +* l2cap_sock_destruct() run. Setting chan->data to NULL first stops any other +* task from dereferencing the now-dead sock pointer. +*/ +static void l2cap_sock_put_chan (struct sock *sk) +{ +struct l2cap_chan *chan =l2cap_pi (sk)->chan; + +if (!chan) +return ; + + chan->data =NULL ; +l2cap_pi (sk)->chan =NULL ; +l2cap_chan_put (chan); +} +/* Kill socket (only if zapped and orphan) * Must be called on unlocked socket, with l2cap channel lock. */ @@ -1297,13 +1315,9 @@ static void l2cap_sock_kill(struct sock *sk)BT_DBG ("sk %p state %s" , sk,state_to_string (sk->sk_state)); -/* Sock is dead, so set chan data to NULL, avoid other task use invalid -* sock pointer. -*/ -l2cap_pi (sk)->chan->data =NULL ; -/* Kill poor orphan */ +l2cap_sock_put_chan (sk); -l2cap_chan_put (l2cap_pi (sk)->chan); +/* Kill poor orphan */ sock_set_flag (sk,SOCK_DEAD );sock_put (sk); }@@ -1546,12 +1560,14 @@ static void l2cap_sock_cleanup_listen(struct sock *parent) } } -static struct l2cap_chan *l2cap_sock_new_connection_cb (struct l2cap_chan *chan) +static int l2cap_sock_new_connection_cb (struct l2cap_conn *conn, +struct l2cap_chan *chan, +struct l2cap_chan *new_chan) {struct sock *sk, *parent = chan->data;if (!parent) -return NULL ; +return -EINVAL ;lock_sock (parent);@@ -1559,25 +1575,33 @@ static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)if (sk_acceptq_is_full (parent)) {BT_DBG ("backlog full %d" , parent->sk_ack_backlog);release_sock (parent); -return NULL ; +return -ENOBUFS ; } sk =l2cap_sock_alloc (sock_net (parent),NULL ,BTPROTO_L2CAP , -GFP_ATOMIC ,0 ); +GFP_ATOMIC ,0 , new_chan);if (!sk) {release_sock (parent); -return NULL ; - } +return -ENOMEM ; + }bt_sock_reclassify_lock (sk,BTPROTO_L2CAP );l2cap_sock_init (sk, parent); +/* Link the channel into conn before exposing the new socket via the +* accept queue. Once release_sock() below drops the parent lock the +* socket may be freed by another task, dropping its reference on +* new_chan; the conn list reference taken here keeps new_chan alive so +* the caller can safely use it after ->new_connection() returns. +*/ +__l2cap_chan_add (conn, new_chan); +bt_accept_enqueue (parent, sk, false);release_sock (parent); -return l2cap_pi (sk)->chan; +return 0 ; }static int l2cap_sock_recv_cb (struct l2cap_chan *chan,struct sk_buff *skb)@@ -1874,10 +1898,7 @@ static void l2cap_sock_destruct(struct sock *sk)BT_DBG ("sk %p" , sk); -if (l2cap_pi (sk)->chan) { -l2cap_pi (sk)->chan->data =NULL ; -l2cap_chan_put (l2cap_pi (sk)->chan); - } +l2cap_sock_put_chan (sk);list_for_each_entry_safe (rx_busy, next, &l2cap_pi (sk)->rx_busy, list) {kfree_skb (rx_busy->skb);@@ -1978,10 +1999,10 @@ static struct proto l2cap_proto = { };static struct sock *l2cap_sock_alloc (struct net *net,struct socket *sock, -int proto,gfp_t prio,int kern) +int proto,gfp_t prio,int kern, +struct l2cap_chan *chan) {struct sock *sk; -struct l2cap_chan *chan; sk =bt_sock_alloc (net, sock, &l2cap_proto, proto, prio, kern);if (!sk)@@ -1992,16 +2013,7 @@ static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,INIT_LIST_HEAD (&l2cap_pi (sk)->rx_busy); - chan =l2cap_chan_create (); -if (!chan) { -sk_free (sk); -if (sock) - sock->sk =NULL ; -return NULL ; - } - -l2cap_chan_hold (chan); - +/* The sock takes ownership of the caller's reference on chan. */ l2cap_pi (sk)->chan = chan;return sk;@@ -2011,6 +2023,7 @@ static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, int kern) {struct sock *sk; +struct l2cap_chan *chan;BT_DBG ("sock %p" , sock);@@ -2025,10 +2038,16 @@ static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, sock->ops = &l2cap_sock_ops; - sk =l2cap_sock_alloc (net, sock, protocol,GFP_ATOMIC , kern); -if (!sk) + chan =l2cap_chan_create (); +if (!chan)return -ENOMEM ; + sk =l2cap_sock_alloc (net, sock, protocol,GFP_ATOMIC , kern, chan); +if (!sk) { +l2cap_chan_put (chan); +return -ENOMEM ; + } +l2cap_sock_init (sk,NULL );bt_sock_link (&l2cap_sk_list, sk);return 0 ;net/bluetooth/smp.c +8
-9@@ -3204,16 +3204,12 @@ 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_conn *conn, +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 +3225,12 @@ 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); +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); -return chan; +BT_DBG ("initialised chan %p" , chan); + +return 0 ; }
Quoted 8 lines
Quoted 4 lines
net/bluetooth/smp.c +0
-1static const struct l2cap_ops smp_root_chan_ops = { --2.54.0
Signature · 1 line
Luiz Augusto von Dentz
Best,
Siwei
Hi Luiz,
Quoted 202 lines · Siwei Zhang
Hi Luiz,
Quoted 136 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 133 lines · Siwei Zhang
l2cap_sock_new_connection_cb()accesses l2cap_pi(sk)->chan afterrelease_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.
Fixes 8ffb929098a5 ("Bluetooth: Remove parent socket usage from l2cap_core.c")Assisted-by Claude:claude-opus-4-8Signed-off-by Siwei Zhang <[email protected]>--- include/net/bluetooth/l2cap.h | 10 +++-- net/bluetooth/6lowpan.c | 31 +++++++------ net/bluetooth/l2cap_core.c | 41 ++++++++++++----- net/bluetooth/l2cap_sock.c | 83 +++++++++++++++++++++-------------- net/bluetooth/smp.c | 17 ++++--- 5 files changed, 113 insertions(+), 69 deletions(-)include/net/bluetooth/l2cap.h +7
-3@@ -620,7 +620,9 @@ struct l2cap_chan {struct l2cap_ops {char *name; -struct l2cap_chan *(*new_connection) (struct l2cap_chan *chan); +int (*new_connection)(struct l2cap_conn *conn, +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 +886,11 @@ 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_conn *conn, +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 +18
-13@@ -624,6 +624,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;@@ -632,11 +641,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; }@@ -745,19 +750,19 @@ 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_conn *conn, +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; +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); +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 +16
-0@@ -4007,6 +4007,31 @@ static inline int l2cap_command_rej(struct l2cap_conn *conn,return 0 ; } +/* Allocate and initialise a channel for an incoming connection. +* +* ->new_connection() initialises the channel and links it into @conn with +* __l2cap_chan_add(). The l2cap_chan_create() reference becomes the one owned +* by the parent subsystem (l2cap_pi(sk)->chan, conn->smp or peer->chan) and is +* released by its teardown callback; the conn list reference is released by +* l2cap_chan_del(). +*/ +static struct l2cap_chan *l2cap_new_connection (struct l2cap_conn *conn, +struct l2cap_chan *pchan) +{ +struct l2cap_chan *chan; + + chan =l2cap_chan_create (); +if (!chan) +return NULL ;Quoted 9 lines
net/bluetooth/l2cap_core.c static inline int l2cap_command_rej(struct l2cap_conn *conn, · line 4030
Quoted 5 lines
net/bluetooth/l2cap_core.c +5
-0+ +if (pchan->ops->new_connection (conn, pchan, chan) <0 ) { +l2cap_chan_put (chan); +return NULL ; + }I don't quite get why we can't just place
__l2cap_chan_addhere instead of having it called bynew_connectioncallbacks?It's specifically the
l2cap_sock_new_connection_cb()case - the very use-after-free this patch fixes. The__l2cap_chan_add()has to happen while the parent lock is still held, and only the callback holds that lock.The reference counting on the new child chan starts at one ref, owned by the new socket:
/* l2cap_new_connection() */ chan = l2cap_chan_create(); /* refcount = 1 */ if (!chan) return NULL; pchan->ops->new_connection(conn, pchan, chan);and inside the socket callback:
/* l2cap_sock_new_connection_cb() */ lock_sock(parent); ... sk = l2cap_sock_alloc(..., new_chan); /* sk owns the chan_create ref */ ... l2cap_sock_init(sk, parent); __l2cap_chan_add(conn, new_chan); /* (A) conn list takes a ref */ bt_accept_enqueue(parent, sk, false); /* (B) sk now on accept queue */ release_sock(parent); /* (C) parent lock dropped */ return 0;The moment we hit (C), sk is reachable through the parent's accept queue, so another task can grab and tear it down:
accept() -> l2cap_sock_kill() -> l2cap_sock_put_chan() chan->data = NULL; l2cap_chan_put(chan); /* drops the sk's chan ref */If
__l2cap_chan_add()at (A) hadn't already taken the conn list reference, that put would drop the last ref and freenew_chan. Control then returns up tol2cap_new_connection(), which hands the now-freed chan back tol2cap_connect():/* l2cap_connect() - runs after the callback returns */ chan = l2cap_new_connection(conn, pchan); if (!chan) goto response; ... bacpy(&chan->src, &conn->hcon->src); /* <-- UAF on freed chan */ chan->psm = psm; chan->dcid = scid;The conn list reference taken at (A), before (C), is what keeps
new_chanalive across therelease_sock()window sol2cap_connect()can keep using it.
Quoted 9 lines
So
__l2cap_chan_add()can't move out tol2cap_new_connection(): by the time the callback returns, the parent lock is already dropped and chan may already be freed - which is exactly the race. It has to be taken inside the callback, under the parent lock, before the socket is exposed.The other callbacks (6lowpan, smp) have no equivalent lock-drop window.
I kept__l2cap_chan_add()inside all of the ->new_connection() callbacks
just to keep the "callback links the channel into conn" contract uniform.
Could you please check this?
Just remind you in case you miss this.
Quoted 304 lines
Quoted 295 lines
Quoted 295 lines
net/bluetooth/l2cap_core.c +8
-12+ +return chan; +} +static void l2cap_connect (struct l2cap_conn *conn,struct l2cap_cmd_hdr *cmd,u8 *data,u8 rsp_code) {@@ -4053,7 +4078,7 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, goto response; } - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (!chan) goto response;@@ -4071,8 +4096,6 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, chan->psm = psm; chan->dcid = scid; -__l2cap_chan_add (conn, chan); - dcid = chan->scid;__set_chan_timer (chan, chan->ops->get_sndtimeo (chan));@@ -4955,7 +4978,7 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn, goto response_unlock; } - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (!chan) { result =L2CAP_CR_LE_NO_MEM ; goto response_unlock;@@ -4970,8 +4993,6 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn, chan->omtu = mtu; chan->remote_mps = mps; -__l2cap_chan_add (conn, chan); -l2cap_le_flowctl_init (chan,__le16_to_cpu (req->credits)); dcid = chan->scid;@@ -5179,7 +5200,7 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,continue ; } - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (!chan) { result =L2CAP_CR_LE_NO_MEM ;continue ;@@ -5194,8 +5215,6 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn, chan->omtu = mtu; chan->remote_mps = mps; -__l2cap_chan_add (conn, chan); -l2cap_ecred_init (chan,__le16_to_cpu (req->credits));/* Init response */ @@ -7470,14 +7489,12 @@ static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) goto next;l2cap_chan_lock (pchan); - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (chan) {bacpy (&chan->src, &hcon->src);bacpy (&chan->dst, &hcon->dst); chan->src_type =bdaddr_src_type (hcon); chan->dst_type = dst_type; - -__l2cap_chan_add (conn, chan); }l2cap_chan_unlock (pchan);net/bluetooth/l2cap_sock.c +51
-32@@ -46,7 +46,8 @@ static struct bt_sock_list l2cap_sk_list = {static const struct proto_ops l2cap_sock_ops;static void l2cap_sock_init (struct sock *sk,struct sock *parent);static struct sock *l2cap_sock_alloc (struct net *net,struct socket *sock, -int proto,gfp_t prio,int kern); +int proto,gfp_t prio,int kern, +struct l2cap_chan *chan);static void l2cap_sock_cleanup_listen (struct sock *parent);bool l2cap_is_socket (struct socket *sock)@@ -1287,6 +1288,23 @@ static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg,return err; } +/* Release the sock's ref on chan and clear the pointer so that the ref is +* dropped exactly once even if both l2cap_sock_kill() and +* l2cap_sock_destruct() run. Setting chan->data to NULL first stops any other +* task from dereferencing the now-dead sock pointer. +*/ +static void l2cap_sock_put_chan (struct sock *sk) +{ +struct l2cap_chan *chan =l2cap_pi (sk)->chan; + +if (!chan) +return ; + + chan->data =NULL ; +l2cap_pi (sk)->chan =NULL ; +l2cap_chan_put (chan); +} +/* Kill socket (only if zapped and orphan) * Must be called on unlocked socket, with l2cap channel lock. */ @@ -1297,13 +1315,9 @@ static void l2cap_sock_kill(struct sock *sk)BT_DBG ("sk %p state %s" , sk,state_to_string (sk->sk_state)); -/* Sock is dead, so set chan data to NULL, avoid other task use invalid -* sock pointer. -*/ -l2cap_pi (sk)->chan->data =NULL ; -/* Kill poor orphan */ +l2cap_sock_put_chan (sk); -l2cap_chan_put (l2cap_pi (sk)->chan); +/* Kill poor orphan */ sock_set_flag (sk,SOCK_DEAD );sock_put (sk); }@@ -1546,12 +1560,14 @@ static void l2cap_sock_cleanup_listen(struct sock *parent) } } -static struct l2cap_chan *l2cap_sock_new_connection_cb (struct l2cap_chan *chan) +static int l2cap_sock_new_connection_cb (struct l2cap_conn *conn, +struct l2cap_chan *chan, +struct l2cap_chan *new_chan) {struct sock *sk, *parent = chan->data;if (!parent) -return NULL ; +return -EINVAL ;lock_sock (parent);@@ -1559,25 +1575,33 @@ static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)if (sk_acceptq_is_full (parent)) {BT_DBG ("backlog full %d" , parent->sk_ack_backlog);release_sock (parent); -return NULL ; +return -ENOBUFS ; } sk =l2cap_sock_alloc (sock_net (parent),NULL ,BTPROTO_L2CAP , -GFP_ATOMIC ,0 ); +GFP_ATOMIC ,0 , new_chan);if (!sk) {release_sock (parent); -return NULL ; - } +return -ENOMEM ; + }bt_sock_reclassify_lock (sk,BTPROTO_L2CAP );l2cap_sock_init (sk, parent); +/* Link the channel into conn before exposing the new socket via the +* accept queue. Once release_sock() below drops the parent lock the +* socket may be freed by another task, dropping its reference on +* new_chan; the conn list reference taken here keeps new_chan alive so +* the caller can safely use it after ->new_connection() returns. +*/ +__l2cap_chan_add (conn, new_chan); +bt_accept_enqueue (parent, sk, false);release_sock (parent); -return l2cap_pi (sk)->chan; +return 0 ; }static int l2cap_sock_recv_cb (struct l2cap_chan *chan,struct sk_buff *skb)@@ -1874,10 +1898,7 @@ static void l2cap_sock_destruct(struct sock *sk)BT_DBG ("sk %p" , sk); -if (l2cap_pi (sk)->chan) { -l2cap_pi (sk)->chan->data =NULL ; -l2cap_chan_put (l2cap_pi (sk)->chan); - } +l2cap_sock_put_chan (sk);list_for_each_entry_safe (rx_busy, next, &l2cap_pi (sk)->rx_busy, list) {kfree_skb (rx_busy->skb);@@ -1978,10 +1999,10 @@ static struct proto l2cap_proto = { };static struct sock *l2cap_sock_alloc (struct net *net,struct socket *sock, -int proto,gfp_t prio,int kern) +int proto,gfp_t prio,int kern, +struct l2cap_chan *chan) {struct sock *sk; -struct l2cap_chan *chan; sk =bt_sock_alloc (net, sock, &l2cap_proto, proto, prio, kern);if (!sk)@@ -1992,16 +2013,7 @@ static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,INIT_LIST_HEAD (&l2cap_pi (sk)->rx_busy); - chan =l2cap_chan_create (); -if (!chan) { -sk_free (sk); -if (sock) - sock->sk =NULL ; -return NULL ; - } - -l2cap_chan_hold (chan); - +/* The sock takes ownership of the caller's reference on chan. */ l2cap_pi (sk)->chan = chan;return sk;@@ -2011,6 +2023,7 @@ static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, int kern) {struct sock *sk; +struct l2cap_chan *chan;BT_DBG ("sock %p" , sock);@@ -2025,10 +2038,16 @@ static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, sock->ops = &l2cap_sock_ops; - sk =l2cap_sock_alloc (net, sock, protocol,GFP_ATOMIC , kern); -if (!sk) + chan =l2cap_chan_create (); +if (!chan)return -ENOMEM ; + sk =l2cap_sock_alloc (net, sock, protocol,GFP_ATOMIC , kern, chan); +if (!sk) { +l2cap_chan_put (chan); +return -ENOMEM ; + } +l2cap_sock_init (sk,NULL );bt_sock_link (&l2cap_sk_list, sk);return 0 ;net/bluetooth/smp.c +8
-9@@ -3204,16 +3204,12 @@ 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_conn *conn, +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 +3225,12 @@ 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); +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); -return chan; +BT_DBG ("initialised chan %p" , chan); + +return 0 ; }Quoted 8 lines
Quoted 4 lines
net/bluetooth/smp.c +0
-1static const struct l2cap_ops smp_root_chan_ops = { --2.54.0
Signature · 1 line
Luiz Augusto von Dentz
Quoted 2 lines
Best,
Siwei
Best,
Siwei
Luiz Augusto von Dentz
maintainerHi Siwei,
Quoted 210 lines · Siwei Zhang
Hi Luiz,
Quoted 206 lines · Siwei Zhang
Hi Luiz,
Quoted 136 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 133 lines · Siwei Zhang
l2cap_sock_new_connection_cb()accesses l2cap_pi(sk)->chan afterrelease_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.
Fixes 8ffb929098a5 ("Bluetooth: Remove parent socket usage from l2cap_core.c")Assisted-by Claude:claude-opus-4-8Signed-off-by Siwei Zhang <[email protected]>--- include/net/bluetooth/l2cap.h | 10 +++-- net/bluetooth/6lowpan.c | 31 +++++++------ net/bluetooth/l2cap_core.c | 41 ++++++++++++----- net/bluetooth/l2cap_sock.c | 83 +++++++++++++++++++++-------------- net/bluetooth/smp.c | 17 ++++--- 5 files changed, 113 insertions(+), 69 deletions(-)include/net/bluetooth/l2cap.h +7
-3@@ -620,7 +620,9 @@ struct l2cap_chan {struct l2cap_ops {char *name; -struct l2cap_chan *(*new_connection) (struct l2cap_chan *chan); +int (*new_connection)(struct l2cap_conn *conn, +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 +886,11 @@ 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_conn *conn, +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 +18
-13@@ -624,6 +624,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;@@ -632,11 +641,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; }@@ -745,19 +750,19 @@ 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_conn *conn, +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; +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); +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 +16
-0@@ -4007,6 +4007,31 @@ static inline int l2cap_command_rej(struct l2cap_conn *conn,return 0 ; } +/* Allocate and initialise a channel for an incoming connection. +* +* ->new_connection() initialises the channel and links it into @conn with +* __l2cap_chan_add(). The l2cap_chan_create() reference becomes the one owned +* by the parent subsystem (l2cap_pi(sk)->chan, conn->smp or peer->chan) and is +* released by its teardown callback; the conn list reference is released by +* l2cap_chan_del(). +*/ +static struct l2cap_chan *l2cap_new_connection (struct l2cap_conn *conn, +struct l2cap_chan *pchan) +{ +struct l2cap_chan *chan; + + chan =l2cap_chan_create (); +if (!chan) +return NULL ;Quoted 9 lines
net/bluetooth/l2cap_core.c static inline int l2cap_command_rej(struct l2cap_conn *conn, · line 4030
Quoted 5 lines
net/bluetooth/l2cap_core.c +5
-0+ +if (pchan->ops->new_connection (conn, pchan, chan) <0 ) { +l2cap_chan_put (chan); +return NULL ; + }I don't quite get why we can't just place
__l2cap_chan_addhere instead of having it called bynew_connectioncallbacks?It's specifically the
l2cap_sock_new_connection_cb()case - the very use-after-free this patch fixes. The__l2cap_chan_add()has to happen while the parent lock is still held, and only the callback holds that lock.The reference counting on the new child chan starts at one ref, owned by the new socket:
/* l2cap_new_connection() */ chan = l2cap_chan_create(); /* refcount = 1 */ if (!chan) return NULL; pchan->ops->new_connection(conn, pchan, chan);and inside the socket callback:
/* l2cap_sock_new_connection_cb() */ lock_sock(parent); ... sk = l2cap_sock_alloc(..., new_chan); /* sk owns the chan_create ref */ ... l2cap_sock_init(sk, parent); __l2cap_chan_add(conn, new_chan); /* (A) conn list takes a ref */ bt_accept_enqueue(parent, sk, false); /* (B) sk now on accept queue */ release_sock(parent); /* (C) parent lock dropped */ return 0;The moment we hit (C), sk is reachable through the parent's accept queue, so another task can grab and tear it down:
accept() -> l2cap_sock_kill() -> l2cap_sock_put_chan() chan->data = NULL; l2cap_chan_put(chan); /* drops the sk's chan ref */If
__l2cap_chan_add()at (A) hadn't already taken the conn list reference, that put would drop the last ref and freenew_chan. Control then returns up tol2cap_new_connection(), which hands the now-freed chan back tol2cap_connect():/* l2cap_connect() - runs after the callback returns */ chan = l2cap_new_connection(conn, pchan); if (!chan) goto response; ... bacpy(&chan->src, &conn->hcon->src); /* <-- UAF on freed chan */ chan->psm = psm; chan->dcid = scid;The conn list reference taken at (A), before (C), is what keeps
new_chanalive across therelease_sock()window sol2cap_connect()can keep using it.So
__l2cap_chan_add()can't move out tol2cap_new_connection(): by the time the callback returns, the parent lock is already dropped and chan may already be freed - which is exactly the race. It has to be taken inside the callback, under the parent lock, before the socket is exposed.
If you do after l2cap_new_connection, yes, but what if you do before you call ->new_connect:
net/bluetooth/l2cap_core.c +0 -0
@@ -4027,8 +4027,10 @@ static struct l2cap_chan
*l2cap_new_connection(struct l2cap_conn *conn,
if (!chan)
return NULL;+ __l2cap_chan_add(conn, chan);
+
if (pchan->ops->new_connection(conn, pchan, chan) < 0) {
- l2cap_chan_put(chan);
+ l2cap_chan_del(chan, 0);
return NULL;
}This means we have to call l2cap_chan_del if new_connection fails, but we can eliminate other functions having to call __l2cap_chan_add with this. If the issue is then having l2cap_chan_del called concurrently, we can add a l2cap_chan_hold; actually, we may need a reference anyway since sk can be released and call l2cap_chan_del, releasing both references.
Quoted 317 lines
Quoted 4 lines
The other callbacks (6lowpan, smp) have no equivalent lock-drop window.
I kept__l2cap_chan_add()inside all of the ->new_connection() callbacks
just to keep the "callback links the channel into conn" contract uniform.Could you please check this?
Just remind you in case you miss this.Quoted 304 lines
Quoted 295 lines
Quoted 295 lines
net/bluetooth/l2cap_core.c +8
-12+ +return chan; +} +static void l2cap_connect (struct l2cap_conn *conn,struct l2cap_cmd_hdr *cmd,u8 *data,u8 rsp_code) {@@ -4053,7 +4078,7 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, goto response; } - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (!chan) goto response;@@ -4071,8 +4096,6 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, chan->psm = psm; chan->dcid = scid; -__l2cap_chan_add (conn, chan); - dcid = chan->scid;__set_chan_timer (chan, chan->ops->get_sndtimeo (chan));@@ -4955,7 +4978,7 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn, goto response_unlock; } - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (!chan) { result =L2CAP_CR_LE_NO_MEM ; goto response_unlock;@@ -4970,8 +4993,6 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn, chan->omtu = mtu; chan->remote_mps = mps; -__l2cap_chan_add (conn, chan); -l2cap_le_flowctl_init (chan,__le16_to_cpu (req->credits)); dcid = chan->scid;@@ -5179,7 +5200,7 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,continue ; } - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (!chan) { result =L2CAP_CR_LE_NO_MEM ;continue ;@@ -5194,8 +5215,6 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn, chan->omtu = mtu; chan->remote_mps = mps; -__l2cap_chan_add (conn, chan); -l2cap_ecred_init (chan,__le16_to_cpu (req->credits));/* Init response */ @@ -7470,14 +7489,12 @@ static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) goto next;l2cap_chan_lock (pchan); - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (chan) {bacpy (&chan->src, &hcon->src);bacpy (&chan->dst, &hcon->dst); chan->src_type =bdaddr_src_type (hcon); chan->dst_type = dst_type; - -__l2cap_chan_add (conn, chan); }l2cap_chan_unlock (pchan);net/bluetooth/l2cap_sock.c +51
-32@@ -46,7 +46,8 @@ static struct bt_sock_list l2cap_sk_list = {static const struct proto_ops l2cap_sock_ops;static void l2cap_sock_init (struct sock *sk,struct sock *parent);static struct sock *l2cap_sock_alloc (struct net *net,struct socket *sock, -int proto,gfp_t prio,int kern); +int proto,gfp_t prio,int kern, +struct l2cap_chan *chan);static void l2cap_sock_cleanup_listen (struct sock *parent);bool l2cap_is_socket (struct socket *sock)@@ -1287,6 +1288,23 @@ static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg,return err; } +/* Release the sock's ref on chan and clear the pointer so that the ref is +* dropped exactly once even if both l2cap_sock_kill() and +* l2cap_sock_destruct() run. Setting chan->data to NULL first stops any other +* task from dereferencing the now-dead sock pointer. +*/ +static void l2cap_sock_put_chan (struct sock *sk) +{ +struct l2cap_chan *chan =l2cap_pi (sk)->chan; + +if (!chan) +return ; + + chan->data =NULL ; +l2cap_pi (sk)->chan =NULL ; +l2cap_chan_put (chan); +} +/* Kill socket (only if zapped and orphan) * Must be called on unlocked socket, with l2cap channel lock. */ @@ -1297,13 +1315,9 @@ static void l2cap_sock_kill(struct sock *sk)BT_DBG ("sk %p state %s" , sk,state_to_string (sk->sk_state)); -/* Sock is dead, so set chan data to NULL, avoid other task use invalid -* sock pointer. -*/ -l2cap_pi (sk)->chan->data =NULL ; -/* Kill poor orphan */ +l2cap_sock_put_chan (sk); -l2cap_chan_put (l2cap_pi (sk)->chan); +/* Kill poor orphan */ sock_set_flag (sk,SOCK_DEAD );sock_put (sk); }@@ -1546,12 +1560,14 @@ static void l2cap_sock_cleanup_listen(struct sock *parent) } } -static struct l2cap_chan *l2cap_sock_new_connection_cb (struct l2cap_chan *chan) +static int l2cap_sock_new_connection_cb (struct l2cap_conn *conn, +struct l2cap_chan *chan, +struct l2cap_chan *new_chan) {struct sock *sk, *parent = chan->data;if (!parent) -return NULL ; +return -EINVAL ;lock_sock (parent);@@ -1559,25 +1575,33 @@ static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)if (sk_acceptq_is_full (parent)) {BT_DBG ("backlog full %d" , parent->sk_ack_backlog);release_sock (parent); -return NULL ; +return -ENOBUFS ; } sk =l2cap_sock_alloc (sock_net (parent),NULL ,BTPROTO_L2CAP , -GFP_ATOMIC ,0 ); +GFP_ATOMIC ,0 , new_chan);if (!sk) {release_sock (parent); -return NULL ; - } +return -ENOMEM ; + }bt_sock_reclassify_lock (sk,BTPROTO_L2CAP );l2cap_sock_init (sk, parent); +/* Link the channel into conn before exposing the new socket via the +* accept queue. Once release_sock() below drops the parent lock the +* socket may be freed by another task, dropping its reference on +* new_chan; the conn list reference taken here keeps new_chan alive so +* the caller can safely use it after ->new_connection() returns. +*/ +__l2cap_chan_add (conn, new_chan); +bt_accept_enqueue (parent, sk, false);release_sock (parent); -return l2cap_pi (sk)->chan; +return 0 ; }static int l2cap_sock_recv_cb (struct l2cap_chan *chan,struct sk_buff *skb)@@ -1874,10 +1898,7 @@ static void l2cap_sock_destruct(struct sock *sk)BT_DBG ("sk %p" , sk); -if (l2cap_pi (sk)->chan) { -l2cap_pi (sk)->chan->data =NULL ; -l2cap_chan_put (l2cap_pi (sk)->chan); - } +l2cap_sock_put_chan (sk);list_for_each_entry_safe (rx_busy, next, &l2cap_pi (sk)->rx_busy, list) {kfree_skb (rx_busy->skb);@@ -1978,10 +1999,10 @@ static struct proto l2cap_proto = { };static struct sock *l2cap_sock_alloc (struct net *net,struct socket *sock, -int proto,gfp_t prio,int kern) +int proto,gfp_t prio,int kern, +struct l2cap_chan *chan) {struct sock *sk; -struct l2cap_chan *chan; sk =bt_sock_alloc (net, sock, &l2cap_proto, proto, prio, kern);if (!sk)@@ -1992,16 +2013,7 @@ static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,INIT_LIST_HEAD (&l2cap_pi (sk)->rx_busy); - chan =l2cap_chan_create (); -if (!chan) { -sk_free (sk); -if (sock) - sock->sk =NULL ; -return NULL ; - } - -l2cap_chan_hold (chan); - +/* The sock takes ownership of the caller's reference on chan. */ l2cap_pi (sk)->chan = chan;return sk;@@ -2011,6 +2023,7 @@ static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, int kern) {struct sock *sk; +struct l2cap_chan *chan;BT_DBG ("sock %p" , sock);@@ -2025,10 +2038,16 @@ static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, sock->ops = &l2cap_sock_ops; - sk =l2cap_sock_alloc (net, sock, protocol,GFP_ATOMIC , kern); -if (!sk) + chan =l2cap_chan_create (); +if (!chan)return -ENOMEM ; + sk =l2cap_sock_alloc (net, sock, protocol,GFP_ATOMIC , kern, chan); +if (!sk) { +l2cap_chan_put (chan); +return -ENOMEM ; + } +l2cap_sock_init (sk,NULL );bt_sock_link (&l2cap_sk_list, sk);return 0 ;net/bluetooth/smp.c +8
-9@@ -3204,16 +3204,12 @@ 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_conn *conn, +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 +3225,12 @@ 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); +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); -return chan; +BT_DBG ("initialised chan %p" , chan); + +return 0 ; }Quoted 8 lines
Quoted 4 lines
net/bluetooth/smp.c +0
-1static const struct l2cap_ops smp_root_chan_ops = { --2.54.0
--
Luiz Augusto von DentzQuoted 2 lines
Best,
SiweiBest,
Siwei
Signature · 1 line
Luiz Augusto von Dentz
Quoted 98 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 95 lines · Siwei Zhang
Hi Luiz,
Quoted 91 lines · Siwei Zhang
Hi Luiz,
Quoted 88 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 85 lines · Siwei Zhang
l2cap_sock_new_connection_cb()accesses l2cap_pi(sk)->chan afterrelease_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.
Fixes 8ffb929098a5 ("Bluetooth: Remove parent socket usage from l2cap_core.c")Assisted-by Claude:claude-opus-4-8Signed-off-by Siwei Zhang <[email protected]>--- include/net/bluetooth/l2cap.h | 10 +++-- net/bluetooth/6lowpan.c | 31 +++++++------ net/bluetooth/l2cap_core.c | 41 ++++++++++++----- net/bluetooth/l2cap_sock.c | 83 +++++++++++++++++++++-------------- net/bluetooth/smp.c | 17 ++++--- 5 files changed, 113 insertions(+), 69 deletions(-)include/net/bluetooth/l2cap.h +7
-3@@ -620,7 +620,9 @@ struct l2cap_chan {struct l2cap_ops {char *name; -struct l2cap_chan *(*new_connection) (struct l2cap_chan *chan); +int (*new_connection)(struct l2cap_conn *conn, +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 +886,11 @@ 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_conn *conn, +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 +10
-5@@ -624,6 +624,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;@@ -632,11 +641,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; }@@ -745,19 +750,19 @@ static inline void chan_ready_cb(struct l2cap_chan *chan)ifup (dev->netdev); }
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
net/bluetooth/6lowpan.c +3
-1-static inline struct l2cap_chan *chan_new_conn_cb (struct l2cap_chan *pchan) +static inline int chan_new_conn_cb (struct l2cap_conn *conn, +struct l2cap_chan *pchan, +struct l2cap_chan *chan)Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
net/bluetooth/6lowpan.c static inline void chan_ready_cb(struct l2cap_chan *chan) · line 757
Quoted 8 lines
net/bluetooth/6lowpan.c +1
-6{ -struct l2cap_chan *chan; - - chan =chan_create (); -if (!chan) -return NULL ; - +chan_init (chan);
ref1: chan->chan_type = L2CAP_CHAN_CONN_ORIENTED; set in chan_init.
Quoted 124 lines
Quoted 96 lines
Quoted 96 lines
Quoted 36 lines
Quoted 36 lines
net/bluetooth/6lowpan.c +4
-1chan->ops = pchan->ops; +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); +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 +16
-0@@ -4007,6 +4007,31 @@ static inline int l2cap_command_rej(struct l2cap_conn *conn,return 0 ; } +/* Allocate and initialise a channel for an incoming connection. +* +* ->new_connection() initialises the channel and links it into @conn with +* __l2cap_chan_add(). The l2cap_chan_create() reference becomes the one owned +* by the parent subsystem (l2cap_pi(sk)->chan, conn->smp or peer->chan) and is +* released by its teardown callback; the conn list reference is released by +* l2cap_chan_del(). +*/ +static struct l2cap_chan *l2cap_new_connection (struct l2cap_conn *conn, +struct l2cap_chan *pchan) +{ +struct l2cap_chan *chan; + + chan =l2cap_chan_create (); +if (!chan) +return NULL ;Quoted 9 lines
net/bluetooth/l2cap_core.c static inline int l2cap_command_rej(struct l2cap_conn *conn, · line 4030
Quoted 5 lines
net/bluetooth/l2cap_core.c +5
-0+ +if (pchan->ops->new_connection (conn, pchan, chan) <0 ) { +l2cap_chan_put (chan); +return NULL ; + }I don't quite get why we can't just place
__l2cap_chan_addhere instead of having it called bynew_connectioncallbacks?It's specifically the
l2cap_sock_new_connection_cb()case - the very use-after-free this patch fixes. The__l2cap_chan_add()has to happen while the parent lock is still held, and only the callback holds that lock.The reference counting on the new child chan starts at one ref, owned by the new socket:
/* l2cap_new_connection() */ chan = l2cap_chan_create(); /* refcount = 1 */ if (!chan) return NULL; pchan->ops->new_connection(conn, pchan, chan);and inside the socket callback:
/* l2cap_sock_new_connection_cb() */ lock_sock(parent); ... sk = l2cap_sock_alloc(..., new_chan); /* sk owns the chan_create ref */ ... l2cap_sock_init(sk, parent); __l2cap_chan_add(conn, new_chan); /* (A) conn list takes a ref */ bt_accept_enqueue(parent, sk, false); /* (B) sk now on accept queue */ release_sock(parent); /* (C) parent lock dropped */ return 0;The moment we hit (C), sk is reachable through the parent's accept queue, so another task can grab and tear it down:
accept() -> l2cap_sock_kill() -> l2cap_sock_put_chan() chan->data = NULL; l2cap_chan_put(chan); /* drops the sk's chan ref */If
__l2cap_chan_add()at (A) hadn't already taken the conn list reference, that put would drop the last ref and freenew_chan. Control then returns up tol2cap_new_connection(), which hands the now-freed chan back tol2cap_connect():/* l2cap_connect() - runs after the callback returns */ chan = l2cap_new_connection(conn, pchan); if (!chan) goto response; ... bacpy(&chan->src, &conn->hcon->src); /* <-- UAF on freed chan */ chan->psm = psm; chan->dcid = scid;Quoted 7 lines
Quoted 7 lines
The conn list reference taken at (A), before (C), is what keeps
new_chanalive across therelease_sock()window sol2cap_connect()can keep using it.So
__l2cap_chan_add()can't move out tol2cap_new_connection(): by the time the callback returns, the parent lock is already dropped and chan may already be freed - which is exactly the race. It has to be taken inside the callback, under the parent lock, before the socket is exposed.If you do after
l2cap_new_connection, yes, but what if you do before you call ->new_connect:net/bluetooth/l2cap_core.c +3
-1@@ -4027,8 +4027,10 @@ static struct l2cap_chan *l2cap_new_connection(struct l2cap_conn *conn, if (!chan) return NULL; + __l2cap_chan_add(conn, chan); +if (pchan->ops->new_connection (conn, pchan, chan) <0 ) { -l2cap_chan_put (chan); +l2cap_chan_del (chan,0 );return NULL ; }
Quoted 7 lines
This means we have to call
l2cap_chan_delifnew_connectionfails, but we can eliminate other functions having to call__l2cap_chan_addwith this. If the issue is then havingl2cap_chan_delcalled concurrently, we can add al2cap_chan_hold; actually, we may need a reference anyway since sk can be released and calll2cap_chan_del, releasing both references.
In:
void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
{
...
switch (chan->chan_type) {
case L2CAP_CHAN_CONN_ORIENTED:
...
break;
case L2CAP_CHAN_CONN_LESS:
...
break;
case L2CAP_CHAN_FIXED:
...
break;
default:
...
}
}Apparently it depends on chan->chan_type and it is set by ref1. It is only set in cb function.
Quoted 312 lines
Quoted 312 lines
Quoted 4 lines
The other callbacks (6lowpan, smp) have no equivalent lock-drop window.
I kept__l2cap_chan_add()inside all of the ->new_connection() callbacks
just to keep the "callback links the channel into conn" contract uniform.Could you please check this?
Just remind you in case you miss this.Quoted 304 lines
Quoted 303 lines
Quoted 297 lines
net/bluetooth/l2cap_core.c +8
-12+ +return chan; +} +static void l2cap_connect (struct l2cap_conn *conn,struct l2cap_cmd_hdr *cmd,u8 *data,u8 rsp_code) {@@ -4053,7 +4078,7 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, goto response; } - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (!chan) goto response;@@ -4071,8 +4096,6 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, chan->psm = psm; chan->dcid = scid; -__l2cap_chan_add (conn, chan); - dcid = chan->scid;__set_chan_timer (chan, chan->ops->get_sndtimeo (chan));@@ -4955,7 +4978,7 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn, goto response_unlock; } - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (!chan) { result =L2CAP_CR_LE_NO_MEM ; goto response_unlock;@@ -4970,8 +4993,6 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn, chan->omtu = mtu; chan->remote_mps = mps; -__l2cap_chan_add (conn, chan); -l2cap_le_flowctl_init (chan,__le16_to_cpu (req->credits)); dcid = chan->scid;@@ -5179,7 +5200,7 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,continue ; } - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (!chan) { result =L2CAP_CR_LE_NO_MEM ;continue ;@@ -5194,8 +5215,6 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn, chan->omtu = mtu; chan->remote_mps = mps; -__l2cap_chan_add (conn, chan); -l2cap_ecred_init (chan,__le16_to_cpu (req->credits));/* Init response */ @@ -7470,14 +7489,12 @@ static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) goto next;l2cap_chan_lock (pchan); - chan = pchan->ops->new_connection (pchan); + chan =l2cap_new_connection (conn, pchan);if (chan) {bacpy (&chan->src, &hcon->src);bacpy (&chan->dst, &hcon->dst); chan->src_type =bdaddr_src_type (hcon); chan->dst_type = dst_type; - -__l2cap_chan_add (conn, chan); }l2cap_chan_unlock (pchan);net/bluetooth/l2cap_sock.c +51
-32@@ -46,7 +46,8 @@ static struct bt_sock_list l2cap_sk_list = {static const struct proto_ops l2cap_sock_ops;static void l2cap_sock_init (struct sock *sk,struct sock *parent);static struct sock *l2cap_sock_alloc (struct net *net,struct socket *sock, -int proto,gfp_t prio,int kern); +int proto,gfp_t prio,int kern, +struct l2cap_chan *chan);static void l2cap_sock_cleanup_listen (struct sock *parent);bool l2cap_is_socket (struct socket *sock)@@ -1287,6 +1288,23 @@ static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg,return err; } +/* Release the sock's ref on chan and clear the pointer so that the ref is +* dropped exactly once even if both l2cap_sock_kill() and +* l2cap_sock_destruct() run. Setting chan->data to NULL first stops any other +* task from dereferencing the now-dead sock pointer. +*/ +static void l2cap_sock_put_chan (struct sock *sk) +{ +struct l2cap_chan *chan =l2cap_pi (sk)->chan; + +if (!chan) +return ; + + chan->data =NULL ; +l2cap_pi (sk)->chan =NULL ; +l2cap_chan_put (chan); +} +/* Kill socket (only if zapped and orphan) * Must be called on unlocked socket, with l2cap channel lock. */ @@ -1297,13 +1315,9 @@ static void l2cap_sock_kill(struct sock *sk)BT_DBG ("sk %p state %s" , sk,state_to_string (sk->sk_state)); -/* Sock is dead, so set chan data to NULL, avoid other task use invalid -* sock pointer. -*/ -l2cap_pi (sk)->chan->data =NULL ; -/* Kill poor orphan */ +l2cap_sock_put_chan (sk); -l2cap_chan_put (l2cap_pi (sk)->chan); +/* Kill poor orphan */ sock_set_flag (sk,SOCK_DEAD );sock_put (sk); }@@ -1546,12 +1560,14 @@ static void l2cap_sock_cleanup_listen(struct sock *parent) } } -static struct l2cap_chan *l2cap_sock_new_connection_cb (struct l2cap_chan *chan) +static int l2cap_sock_new_connection_cb (struct l2cap_conn *conn, +struct l2cap_chan *chan, +struct l2cap_chan *new_chan) {struct sock *sk, *parent = chan->data;if (!parent) -return NULL ; +return -EINVAL ;lock_sock (parent);@@ -1559,25 +1575,33 @@ static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)if (sk_acceptq_is_full (parent)) {BT_DBG ("backlog full %d" , parent->sk_ack_backlog);release_sock (parent); -return NULL ; +return -ENOBUFS ; } sk =l2cap_sock_alloc (sock_net (parent),NULL ,BTPROTO_L2CAP , -GFP_ATOMIC ,0 ); +GFP_ATOMIC ,0 , new_chan);if (!sk) {release_sock (parent); -return NULL ; - } +return -ENOMEM ; + }bt_sock_reclassify_lock (sk,BTPROTO_L2CAP );l2cap_sock_init (sk, parent); +/* Link the channel into conn before exposing the new socket via the +* accept queue. Once release_sock() below drops the parent lock the +* socket may be freed by another task, dropping its reference on +* new_chan; the conn list reference taken here keeps new_chan alive so +* the caller can safely use it after ->new_connection() returns. +*/ +__l2cap_chan_add (conn, new_chan); +bt_accept_enqueue (parent, sk, false);release_sock (parent); -return l2cap_pi (sk)->chan; +return 0 ; }static int l2cap_sock_recv_cb (struct l2cap_chan *chan,struct sk_buff *skb)@@ -1874,10 +1898,7 @@ static void l2cap_sock_destruct(struct sock *sk)BT_DBG ("sk %p" , sk); -if (l2cap_pi (sk)->chan) { -l2cap_pi (sk)->chan->data =NULL ; -l2cap_chan_put (l2cap_pi (sk)->chan); - } +l2cap_sock_put_chan (sk);list_for_each_entry_safe (rx_busy, next, &l2cap_pi (sk)->rx_busy, list) {kfree_skb (rx_busy->skb);@@ -1978,10 +1999,10 @@ static struct proto l2cap_proto = { };static struct sock *l2cap_sock_alloc (struct net *net,struct socket *sock, -int proto,gfp_t prio,int kern) +int proto,gfp_t prio,int kern, +struct l2cap_chan *chan) {struct sock *sk; -struct l2cap_chan *chan; sk =bt_sock_alloc (net, sock, &l2cap_proto, proto, prio, kern);if (!sk)@@ -1992,16 +2013,7 @@ static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,INIT_LIST_HEAD (&l2cap_pi (sk)->rx_busy); - chan =l2cap_chan_create (); -if (!chan) { -sk_free (sk); -if (sock) - sock->sk =NULL ; -return NULL ; - } - -l2cap_chan_hold (chan); - +/* The sock takes ownership of the caller's reference on chan. */ l2cap_pi (sk)->chan = chan;return sk;@@ -2011,6 +2023,7 @@ static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, int kern) {struct sock *sk; +struct l2cap_chan *chan;BT_DBG ("sock %p" , sock);@@ -2025,10 +2038,16 @@ static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, sock->ops = &l2cap_sock_ops; - sk =l2cap_sock_alloc (net, sock, protocol,GFP_ATOMIC , kern); -if (!sk) + chan =l2cap_chan_create (); +if (!chan)return -ENOMEM ; + sk =l2cap_sock_alloc (net, sock, protocol,GFP_ATOMIC , kern, chan); +if (!sk) { +l2cap_chan_put (chan); +return -ENOMEM ; + } +l2cap_sock_init (sk,NULL );bt_sock_link (&l2cap_sk_list, sk);return 0 ;net/bluetooth/smp.c +8
-10@@ -3204,16 +3204,12 @@ 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_conn *conn, +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 +3225,12 @@ 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); +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); -return chan; +BT_DBG ("initialised chan %p" , chan); + +return 0 ; }static const struct l2cap_ops smp_root_chan_ops = { --Quoted 2 lines
2.54.0
--
Luiz Augusto von Dentz
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
Best,
SiweiBest,
SiweiSignature · 1 line
Luiz Augusto von Dentz
Best,
Siwei
Luiz Augusto von Dentz
maintainerHi Siwei,
Quoted 270 lines · Siwei Zhang
Quoted 98 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 95 lines · Siwei Zhang
Hi Luiz,
Quoted 91 lines · Siwei Zhang
Hi Luiz,
Quoted 88 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 85 lines · Siwei Zhang
l2cap_sock_new_connection_cb()accesses l2cap_pi(sk)->chan afterrelease_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.
Fixes 8ffb929098a5 ("Bluetooth: Remove parent socket usage from l2cap_core.c")Assisted-by Claude:claude-opus-4-8Signed-off-by Siwei Zhang <[email protected]>--- include/net/bluetooth/l2cap.h | 10 +++-- net/bluetooth/6lowpan.c | 31 +++++++------ net/bluetooth/l2cap_core.c | 41 ++++++++++++----- net/bluetooth/l2cap_sock.c | 83 +++++++++++++++++++++-------------- net/bluetooth/smp.c | 17 ++++--- 5 files changed, 113 insertions(+), 69 deletions(-)include/net/bluetooth/l2cap.h +7
-3@@ -620,7 +620,9 @@ struct l2cap_chan {struct l2cap_ops {char *name; -struct l2cap_chan *(*new_connection) (struct l2cap_chan *chan); +int (*new_connection)(struct l2cap_conn *conn, +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 +886,11 @@ 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_conn *conn, +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 +10
-5@@ -624,6 +624,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;@@ -632,11 +641,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; }@@ -745,19 +750,19 @@ static inline void chan_ready_cb(struct l2cap_chan *chan)ifup (dev->netdev); }Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
net/bluetooth/6lowpan.c +3
-1-static inline struct l2cap_chan *chan_new_conn_cb (struct l2cap_chan *pchan) +static inline int chan_new_conn_cb (struct l2cap_conn *conn, +struct l2cap_chan *pchan, +struct l2cap_chan *chan)Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
net/bluetooth/6lowpan.c static inline void chan_ready_cb(struct l2cap_chan *chan) · line 757
Quoted 8 lines
net/bluetooth/6lowpan.c +1
-6{ -struct l2cap_chan *chan; - - chan =chan_create (); -if (!chan) -return NULL ; - +chan_init (chan);ref1:
chan->chan_type=L2CAP_CHAN_CONN_ORIENTED; set inchan_init.Quoted 124 lines
Quoted 96 lines
Quoted 96 lines
Quoted 36 lines
Quoted 36 lines
net/bluetooth/6lowpan.c +4
-1chan->ops = pchan->ops; +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); +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 +16
-0@@ -4007,6 +4007,31 @@ static inline int l2cap_command_rej(struct l2cap_conn *conn,return 0 ; } +/* Allocate and initialise a channel for an incoming connection. +* +* ->new_connection() initialises the channel and links it into @conn with +* __l2cap_chan_add(). The l2cap_chan_create() reference becomes the one owned +* by the parent subsystem (l2cap_pi(sk)->chan, conn->smp or peer->chan) and is +* released by its teardown callback; the conn list reference is released by +* l2cap_chan_del(). +*/ +static struct l2cap_chan *l2cap_new_connection (struct l2cap_conn *conn, +struct l2cap_chan *pchan) +{ +struct l2cap_chan *chan; + + chan =l2cap_chan_create (); +if (!chan) +return NULL ;Quoted 9 lines
net/bluetooth/l2cap_core.c static inline int l2cap_command_rej(struct l2cap_conn *conn, · line 4030
Quoted 5 lines
net/bluetooth/l2cap_core.c +5
-0+ +if (pchan->ops->new_connection (conn, pchan, chan) <0 ) { +l2cap_chan_put (chan); +return NULL ; + }I don't quite get why we can't just place
__l2cap_chan_addhere instead of having it called bynew_connectioncallbacks?It's specifically the
l2cap_sock_new_connection_cb()case - the very use-after-free this patch fixes. The__l2cap_chan_add()has to happen while the parent lock is still held, and only the callback holds that lock.The reference counting on the new child chan starts at one ref, owned by the new socket:
/* l2cap_new_connection() */ chan = l2cap_chan_create(); /* refcount = 1 */ if (!chan) return NULL; pchan->ops->new_connection(conn, pchan, chan);and inside the socket callback:
/* l2cap_sock_new_connection_cb() */ lock_sock(parent); ... sk = l2cap_sock_alloc(..., new_chan); /* sk owns the chan_create ref */ ... l2cap_sock_init(sk, parent); __l2cap_chan_add(conn, new_chan); /* (A) conn list takes a ref */ bt_accept_enqueue(parent, sk, false); /* (B) sk now on accept queue */ release_sock(parent); /* (C) parent lock dropped */ return 0;The moment we hit (C), sk is reachable through the parent's accept queue, so another task can grab and tear it down:
accept() -> l2cap_sock_kill() -> l2cap_sock_put_chan() chan->data = NULL; l2cap_chan_put(chan); /* drops the sk's chan ref */If
__l2cap_chan_add()at (A) hadn't already taken the conn list reference, that put would drop the last ref and freenew_chan. Control then returns up tol2cap_new_connection(), which hands the now-freed chan back tol2cap_connect():/* l2cap_connect() - runs after the callback returns */ chan = l2cap_new_connection(conn, pchan); if (!chan) goto response; ... bacpy(&chan->src, &conn->hcon->src); /* <-- UAF on freed chan */ chan->psm = psm; chan->dcid = scid;Quoted 7 lines
Quoted 7 lines
The conn list reference taken at (A), before (C), is what keeps
new_chanalive across therelease_sock()window sol2cap_connect()can keep using it.So
__l2cap_chan_add()can't move out tol2cap_new_connection(): by the time the callback returns, the parent lock is already dropped and chan may already be freed - which is exactly the race. It has to be taken inside the callback, under the parent lock, before the socket is exposed.If you do after
l2cap_new_connection, yes, but what if you do before you call ->new_connect:net/bluetooth/l2cap_core.c +3
-1@@ -4027,8 +4027,10 @@ static struct l2cap_chan *l2cap_new_connection(struct l2cap_conn *conn, if (!chan) return NULL; + __l2cap_chan_add(conn, chan); +if (pchan->ops->new_connection (conn, pchan, chan) <0 ) { -l2cap_chan_put (chan); +l2cap_chan_del (chan,0 );return NULL ; }Quoted 7 lines
This means we have to call
l2cap_chan_delifnew_connectionfails, but we can eliminate other functions having to call__l2cap_chan_addwith this. If the issue is then havingl2cap_chan_delcalled concurrently, we can add al2cap_chan_hold; actually, we may need a reference anyway since sk can be released and calll2cap_chan_del, releasing both references.In:
void
__l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan){ ... switch (chan->chan_type) { case L2CAP_CHAN_CONN_ORIENTED: ... break; case L2CAP_CHAN_CONN_LESS: ... break; case L2CAP_CHAN_FIXED: ... break; default: ... } }
Quoted 2 lines
Apparently it depends on
chan->chan_typeand it is set by ref1. It is only set in cb function.
Ok, this probably needs fixing in 6lowpan. These defaults should come from the parent channel, similar to how it's done in l2cap_sock_init. Perhaps we should promote that logic to l2cap_chan_set_defaults passing pchan so it can initialize the channel before calling __l2cap_chan_add.
Quoted 273 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 270 lines · Siwei Zhang
Quoted 98 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 95 lines · Siwei Zhang
Hi Luiz,
Quoted 91 lines · Siwei Zhang
Hi Luiz,
Quoted 88 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 85 lines · Siwei Zhang
l2cap_sock_new_connection_cb()accesses l2cap_pi(sk)->chan afterrelease_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.
Fixes 8ffb929098a5 ("Bluetooth: Remove parent socket usage from l2cap_core.c")Assisted-by Claude:claude-opus-4-8Signed-off-by Siwei Zhang <[email protected]>--- include/net/bluetooth/l2cap.h | 10 +++-- net/bluetooth/6lowpan.c | 31 +++++++------ net/bluetooth/l2cap_core.c | 41 ++++++++++++----- net/bluetooth/l2cap_sock.c | 83 +++++++++++++++++++++-------------- net/bluetooth/smp.c | 17 ++++--- 5 files changed, 113 insertions(+), 69 deletions(-)include/net/bluetooth/l2cap.h +7
-3@@ -620,7 +620,9 @@ struct l2cap_chan {struct l2cap_ops {char *name; -struct l2cap_chan *(*new_connection) (struct l2cap_chan *chan); +int (*new_connection)(struct l2cap_conn *conn, +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 +886,11 @@ 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_conn *conn, +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 +10
-5@@ -624,6 +624,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;@@ -632,11 +641,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; }@@ -745,19 +750,19 @@ static inline void chan_ready_cb(struct l2cap_chan *chan)ifup (dev->netdev); }Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
net/bluetooth/6lowpan.c +3
-1-static inline struct l2cap_chan *chan_new_conn_cb (struct l2cap_chan *pchan) +static inline int chan_new_conn_cb (struct l2cap_conn *conn, +struct l2cap_chan *pchan, +struct l2cap_chan *chan)Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
net/bluetooth/6lowpan.c static inline void chan_ready_cb(struct l2cap_chan *chan) · line 757
Quoted 8 lines
net/bluetooth/6lowpan.c +1
-6{ -struct l2cap_chan *chan; - - chan =chan_create (); -if (!chan) -return NULL ; - +chan_init (chan);ref1:
chan->chan_type=L2CAP_CHAN_CONN_ORIENTED; set inchan_init.Quoted 124 lines
Quoted 96 lines
Quoted 96 lines
Quoted 36 lines
Quoted 36 lines
net/bluetooth/6lowpan.c +4
-1chan->ops = pchan->ops; +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); +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 +16
-0@@ -4007,6 +4007,31 @@ static inline int l2cap_command_rej(struct l2cap_conn *conn,return 0 ; } +/* Allocate and initialise a channel for an incoming connection. +* +* ->new_connection() initialises the channel and links it into @conn with +* __l2cap_chan_add(). The l2cap_chan_create() reference becomes the one owned +* by the parent subsystem (l2cap_pi(sk)->chan, conn->smp or peer->chan) and is +* released by its teardown callback; the conn list reference is released by +* l2cap_chan_del(). +*/ +static struct l2cap_chan *l2cap_new_connection (struct l2cap_conn *conn, +struct l2cap_chan *pchan) +{ +struct l2cap_chan *chan; + + chan =l2cap_chan_create (); +if (!chan) +return NULL ;Quoted 9 lines
net/bluetooth/l2cap_core.c static inline int l2cap_command_rej(struct l2cap_conn *conn, · line 4030
Quoted 5 lines
net/bluetooth/l2cap_core.c +5
-0+ +if (pchan->ops->new_connection (conn, pchan, chan) <0 ) { +l2cap_chan_put (chan); +return NULL ; + }I don't quite get why we can't just place
__l2cap_chan_addhere instead of having it called bynew_connectioncallbacks?It's specifically the
l2cap_sock_new_connection_cb()case - the very use-after-free this patch fixes. The__l2cap_chan_add()has to happen while the parent lock is still held, and only the callback holds that lock.The reference counting on the new child chan starts at one ref, owned by the new socket:
/* l2cap_new_connection() */ chan = l2cap_chan_create(); /* refcount = 1 */ if (!chan) return NULL; pchan->ops->new_connection(conn, pchan, chan);and inside the socket callback:
/* l2cap_sock_new_connection_cb() */ lock_sock(parent); ... sk = l2cap_sock_alloc(..., new_chan); /* sk owns the chan_create ref */ ... l2cap_sock_init(sk, parent); __l2cap_chan_add(conn, new_chan); /* (A) conn list takes a ref */ bt_accept_enqueue(parent, sk, false); /* (B) sk now on accept queue */ release_sock(parent); /* (C) parent lock dropped */ return 0;The moment we hit (C), sk is reachable through the parent's accept queue, so another task can grab and tear it down:
accept() -> l2cap_sock_kill() -> l2cap_sock_put_chan() chan->data = NULL; l2cap_chan_put(chan); /* drops the sk's chan ref */If
__l2cap_chan_add()at (A) hadn't already taken the conn list reference, that put would drop the last ref and freenew_chan. Control then returns up tol2cap_new_connection(), which hands the now-freed chan back tol2cap_connect():/* l2cap_connect() - runs after the callback returns */ chan = l2cap_new_connection(conn, pchan); if (!chan) goto response; ... bacpy(&chan->src, &conn->hcon->src); /* <-- UAF on freed chan */ chan->psm = psm; chan->dcid = scid;Quoted 7 lines
Quoted 7 lines
The conn list reference taken at (A), before (C), is what keeps
new_chanalive across therelease_sock()window sol2cap_connect()can keep using it.So
__l2cap_chan_add()can't move out tol2cap_new_connection(): by the time the callback returns, the parent lock is already dropped and chan may already be freed - which is exactly the race. It has to be taken inside the callback, under the parent lock, before the socket is exposed.If you do after
l2cap_new_connection, yes, but what if you do before you call ->new_connect:net/bluetooth/l2cap_core.c +3
-1@@ -4027,8 +4027,10 @@ static struct l2cap_chan *l2cap_new_connection(struct l2cap_conn *conn, if (!chan) return NULL; + __l2cap_chan_add(conn, chan); +if (pchan->ops->new_connection (conn, pchan, chan) <0 ) { -l2cap_chan_put (chan); +l2cap_chan_del (chan,0 );return NULL ; }Quoted 7 lines
This means we have to call
l2cap_chan_delifnew_connectionfails, but we can eliminate other functions having to call__l2cap_chan_addwith this. If the issue is then havingl2cap_chan_delcalled concurrently, we can add al2cap_chan_hold; actually, we may need a reference anyway since sk can be released and calll2cap_chan_del, releasing both references.In:
void
__l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan){ ... switch (chan->chan_type) { case L2CAP_CHAN_CONN_ORIENTED: ... break; case L2CAP_CHAN_CONN_LESS: ... break; case L2CAP_CHAN_FIXED: ... break; default: ... } }
Quoted 8 lines
Quoted 2 lines
Apparently it depends on
chan->chan_typeand it is set by ref1. It is only set in cb function.Ok, this probably needs fixing in 6lowpan. These defaults should come from the parent channel, similar to how it's done in
l2cap_sock_init. Perhaps we should promote that logic tol2cap_chan_set_defaultspassing pchan so it can initialize the channel before calling__l2cap_chan_add.
I can fix that in a separate patch.
Could you please merge it first?
Best,
Siwei
Luiz Augusto von Dentz
maintainerHi Siwei,
Quoted 279 lines · Siwei Zhang
Quoted 276 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 272 lines · Siwei Zhang
Quoted 98 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 95 lines · Siwei Zhang
Hi Luiz,
Quoted 91 lines · Siwei Zhang
Hi Luiz,
Quoted 88 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 85 lines · Siwei Zhang
l2cap_sock_new_connection_cb()accesses l2cap_pi(sk)->chan afterrelease_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.
Fixes 8ffb929098a5 ("Bluetooth: Remove parent socket usage from l2cap_core.c")Assisted-by Claude:claude-opus-4-8Signed-off-by Siwei Zhang <[email protected]>--- include/net/bluetooth/l2cap.h | 10 +++-- net/bluetooth/6lowpan.c | 31 +++++++------ net/bluetooth/l2cap_core.c | 41 ++++++++++++----- net/bluetooth/l2cap_sock.c | 83 +++++++++++++++++++++-------------- net/bluetooth/smp.c | 17 ++++--- 5 files changed, 113 insertions(+), 69 deletions(-)include/net/bluetooth/l2cap.h +7
-3@@ -620,7 +620,9 @@ struct l2cap_chan {struct l2cap_ops {char *name; -struct l2cap_chan *(*new_connection) (struct l2cap_chan *chan); +int (*new_connection)(struct l2cap_conn *conn, +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 +886,11 @@ 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_conn *conn, +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 +10
-5@@ -624,6 +624,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;@@ -632,11 +641,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; }@@ -745,19 +750,19 @@ static inline void chan_ready_cb(struct l2cap_chan *chan)ifup (dev->netdev); }Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
net/bluetooth/6lowpan.c +3
-1-static inline struct l2cap_chan *chan_new_conn_cb (struct l2cap_chan *pchan) +static inline int chan_new_conn_cb (struct l2cap_conn *conn, +struct l2cap_chan *pchan, +struct l2cap_chan *chan)Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
net/bluetooth/6lowpan.c static inline void chan_ready_cb(struct l2cap_chan *chan) · line 757
Quoted 8 lines
net/bluetooth/6lowpan.c +1
-6{ -struct l2cap_chan *chan; - - chan =chan_create (); -if (!chan) -return NULL ; - +chan_init (chan);ref1:
chan->chan_type=L2CAP_CHAN_CONN_ORIENTED; set inchan_init.Quoted 124 lines
Quoted 96 lines
Quoted 96 lines
Quoted 36 lines
Quoted 36 lines
net/bluetooth/6lowpan.c +4
-1chan->ops = pchan->ops; +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); +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 +16
-0@@ -4007,6 +4007,31 @@ static inline int l2cap_command_rej(struct l2cap_conn *conn,return 0 ; } +/* Allocate and initialise a channel for an incoming connection. +* +* ->new_connection() initialises the channel and links it into @conn with +* __l2cap_chan_add(). The l2cap_chan_create() reference becomes the one owned +* by the parent subsystem (l2cap_pi(sk)->chan, conn->smp or peer->chan) and is +* released by its teardown callback; the conn list reference is released by +* l2cap_chan_del(). +*/ +static struct l2cap_chan *l2cap_new_connection (struct l2cap_conn *conn, +struct l2cap_chan *pchan) +{ +struct l2cap_chan *chan; + + chan =l2cap_chan_create (); +if (!chan) +return NULL ;Quoted 9 lines
net/bluetooth/l2cap_core.c static inline int l2cap_command_rej(struct l2cap_conn *conn, · line 4030
Quoted 5 lines
net/bluetooth/l2cap_core.c +5
-0+ +if (pchan->ops->new_connection (conn, pchan, chan) <0 ) { +l2cap_chan_put (chan); +return NULL ; + }I don't quite get why we can't just place
__l2cap_chan_addhere instead of having it called bynew_connectioncallbacks?It's specifically the
l2cap_sock_new_connection_cb()case - the very use-after-free this patch fixes. The__l2cap_chan_add()has to happen while the parent lock is still held, and only the callback holds that lock.The reference counting on the new child chan starts at one ref, owned by the new socket:
/* l2cap_new_connection() */ chan = l2cap_chan_create(); /* refcount = 1 */ if (!chan) return NULL; pchan->ops->new_connection(conn, pchan, chan);and inside the socket callback:
/* l2cap_sock_new_connection_cb() */ lock_sock(parent); ... sk = l2cap_sock_alloc(..., new_chan); /* sk owns the chan_create ref */ ... l2cap_sock_init(sk, parent); __l2cap_chan_add(conn, new_chan); /* (A) conn list takes a ref */ bt_accept_enqueue(parent, sk, false); /* (B) sk now on accept queue */ release_sock(parent); /* (C) parent lock dropped */ return 0;The moment we hit (C), sk is reachable through the parent's accept queue, so another task can grab and tear it down:
accept() -> l2cap_sock_kill() -> l2cap_sock_put_chan() chan->data = NULL; l2cap_chan_put(chan); /* drops the sk's chan ref */If
__l2cap_chan_add()at (A) hadn't already taken the conn list reference, that put would drop the last ref and freenew_chan. Control then returns up tol2cap_new_connection(), which hands the now-freed chan back tol2cap_connect():/* l2cap_connect() - runs after the callback returns */ chan = l2cap_new_connection(conn, pchan); if (!chan) goto response; ... bacpy(&chan->src, &conn->hcon->src); /* <-- UAF on freed chan */ chan->psm = psm; chan->dcid = scid;Quoted 7 lines
Quoted 7 lines
The conn list reference taken at (A), before (C), is what keeps
new_chanalive across therelease_sock()window sol2cap_connect()can keep using it.So
__l2cap_chan_add()can't move out tol2cap_new_connection(): by the time the callback returns, the parent lock is already dropped and chan may already be freed - which is exactly the race. It has to be taken inside the callback, under the parent lock, before the socket is exposed.If you do after
l2cap_new_connection, yes, but what if you do before you call ->new_connect:net/bluetooth/l2cap_core.c +3
-1@@ -4027,8 +4027,10 @@ static struct l2cap_chan *l2cap_new_connection(struct l2cap_conn *conn, if (!chan) return NULL; + __l2cap_chan_add(conn, chan); +if (pchan->ops->new_connection (conn, pchan, chan) <0 ) { -l2cap_chan_put (chan); +l2cap_chan_del (chan,0 );return NULL ; }Quoted 7 lines
This means we have to call
l2cap_chan_delifnew_connectionfails, but we can eliminate other functions having to call__l2cap_chan_addwith this. If the issue is then havingl2cap_chan_delcalled concurrently, we can add al2cap_chan_hold; actually, we may need a reference anyway since sk can be released and calll2cap_chan_del, releasing both references.In:
void
__l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan){ ... switch (chan->chan_type) { case L2CAP_CHAN_CONN_ORIENTED: ... break; case L2CAP_CHAN_CONN_LESS: ... break; case L2CAP_CHAN_FIXED: ... break; default: ... } }Apparently it depends on
chan->chan_typeand it is set by ref1. It is only set in cb function.
Quoted 8 lines
Quoted 5 lines
Ok, this probably needs fixing in 6lowpan. These defaults should come from the parent channel, similar to how it's done in
l2cap_sock_init. Perhaps we should promote that logic tol2cap_chan_set_defaultspassing pchan so it can initialize the channel before calling__l2cap_chan_add.I can fix that in a separate patch.
Could you please merge it first?
Merge it first, and then rework the order? Not sure what is the benefit of doing that, it is a somewhat substatial change either way so I don't think it will make it much easier to lets say backport.
Quoted 2 lines
Best,
Siwei
Signature · 1 line
Luiz Augusto von Dentz
Quoted 288 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 285 lines · Siwei Zhang
Quoted 281 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 270 lines · Siwei Zhang
Quoted 98 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 95 lines · Siwei Zhang
Hi Luiz,
Quoted 91 lines · Siwei Zhang
Hi Luiz,
Quoted 88 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 85 lines · Siwei Zhang
l2cap_sock_new_connection_cb()accesses l2cap_pi(sk)->chan afterrelease_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.
Fixes 8ffb929098a5 ("Bluetooth: Remove parent socket usage from l2cap_core.c")Assisted-by Claude:claude-opus-4-8Signed-off-by Siwei Zhang <[email protected]>--- include/net/bluetooth/l2cap.h | 10 +++-- net/bluetooth/6lowpan.c | 31 +++++++------ net/bluetooth/l2cap_core.c | 41 ++++++++++++----- net/bluetooth/l2cap_sock.c | 83 +++++++++++++++++++++-------------- net/bluetooth/smp.c | 17 ++++--- 5 files changed, 113 insertions(+), 69 deletions(-)include/net/bluetooth/l2cap.h +7
-3@@ -620,7 +620,9 @@ struct l2cap_chan {struct l2cap_ops {char *name; -struct l2cap_chan *(*new_connection) (struct l2cap_chan *chan); +int (*new_connection)(struct l2cap_conn *conn, +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 +886,11 @@ 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_conn *conn, +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 +10
-5@@ -624,6 +624,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;@@ -632,11 +641,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; }@@ -745,19 +750,19 @@ static inline void chan_ready_cb(struct l2cap_chan *chan)ifup (dev->netdev); }Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
net/bluetooth/6lowpan.c +3
-1-static inline struct l2cap_chan *chan_new_conn_cb (struct l2cap_chan *pchan) +static inline int chan_new_conn_cb (struct l2cap_conn *conn, +struct l2cap_chan *pchan, +struct l2cap_chan *chan)Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
net/bluetooth/6lowpan.c static inline void chan_ready_cb(struct l2cap_chan *chan) · line 757
Quoted 8 lines
net/bluetooth/6lowpan.c +1
-6{ -struct l2cap_chan *chan; - - chan =chan_create (); -if (!chan) -return NULL ; - +chan_init (chan);ref1:
chan->chan_type=L2CAP_CHAN_CONN_ORIENTED; set inchan_init.Quoted 124 lines
Quoted 96 lines
Quoted 96 lines
Quoted 36 lines
Quoted 36 lines
net/bluetooth/6lowpan.c +4
-1chan->ops = pchan->ops; +/* Take the conn list reference; see l2cap_new_connection(). */ +__l2cap_chan_add (conn, chan); +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 +16
-0@@ -4007,6 +4007,31 @@ static inline int l2cap_command_rej(struct l2cap_conn *conn,return 0 ; } +/* Allocate and initialise a channel for an incoming connection. +* +* ->new_connection() initialises the channel and links it into @conn with +* __l2cap_chan_add(). The l2cap_chan_create() reference becomes the one owned +* by the parent subsystem (l2cap_pi(sk)->chan, conn->smp or peer->chan) and is +* released by its teardown callback; the conn list reference is released by +* l2cap_chan_del(). +*/ +static struct l2cap_chan *l2cap_new_connection (struct l2cap_conn *conn, +struct l2cap_chan *pchan) +{ +struct l2cap_chan *chan; + + chan =l2cap_chan_create (); +if (!chan) +return NULL ;Quoted 9 lines
net/bluetooth/l2cap_core.c static inline int l2cap_command_rej(struct l2cap_conn *conn, · line 4030
Quoted 5 lines
net/bluetooth/l2cap_core.c +5
-0+ +if (pchan->ops->new_connection (conn, pchan, chan) <0 ) { +l2cap_chan_put (chan); +return NULL ; + }I don't quite get why we can't just place
__l2cap_chan_addhere instead of having it called bynew_connectioncallbacks?It's specifically the
l2cap_sock_new_connection_cb()case - the very use-after-free this patch fixes. The__l2cap_chan_add()has to happen while the parent lock is still held, and only the callback holds that lock.The reference counting on the new child chan starts at one ref, owned by the new socket:
/* l2cap_new_connection() */ chan = l2cap_chan_create(); /* refcount = 1 */ if (!chan) return NULL; pchan->ops->new_connection(conn, pchan, chan);and inside the socket callback:
/* l2cap_sock_new_connection_cb() */ lock_sock(parent); ... sk = l2cap_sock_alloc(..., new_chan); /* sk owns the chan_create ref */ ... l2cap_sock_init(sk, parent); __l2cap_chan_add(conn, new_chan); /* (A) conn list takes a ref */ bt_accept_enqueue(parent, sk, false); /* (B) sk now on accept queue */ release_sock(parent); /* (C) parent lock dropped */ return 0;The moment we hit (C), sk is reachable through the parent's accept queue, so another task can grab and tear it down:
accept() -> l2cap_sock_kill() -> l2cap_sock_put_chan() chan->data = NULL; l2cap_chan_put(chan); /* drops the sk's chan ref */If
__l2cap_chan_add()at (A) hadn't already taken the conn list reference, that put would drop the last ref and freenew_chan. Control then returns up tol2cap_new_connection(), which hands the now-freed chan back tol2cap_connect():/* l2cap_connect() - runs after the callback returns */ chan = l2cap_new_connection(conn, pchan); if (!chan) goto response; ... bacpy(&chan->src, &conn->hcon->src); /* <-- UAF on freed chan */ chan->psm = psm; chan->dcid = scid;Quoted 7 lines
Quoted 7 lines
The conn list reference taken at (A), before (C), is what keeps
new_chanalive across therelease_sock()window sol2cap_connect()can keep using it.So
__l2cap_chan_add()can't move out tol2cap_new_connection(): by the time the callback returns, the parent lock is already dropped and chan may already be freed - which is exactly the race. It has to be taken inside the callback, under the parent lock, before the socket is exposed.If you do after
l2cap_new_connection, yes, but what if you do before you call ->new_connect:net/bluetooth/l2cap_core.c +3
-1@@ -4027,8 +4027,10 @@ static struct l2cap_chan *l2cap_new_connection(struct l2cap_conn *conn, if (!chan) return NULL; + __l2cap_chan_add(conn, chan); +if (pchan->ops->new_connection (conn, pchan, chan) <0 ) { -l2cap_chan_put (chan); +l2cap_chan_del (chan,0 );return NULL ; }Quoted 7 lines
This means we have to call
l2cap_chan_delifnew_connectionfails, but we can eliminate other functions having to call__l2cap_chan_addwith this. If the issue is then havingl2cap_chan_delcalled concurrently, we can add al2cap_chan_hold; actually, we may need a reference anyway since sk can be released and calll2cap_chan_del, releasing both references.In:
void
__l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan){ ... switch (chan->chan_type) { case L2CAP_CHAN_CONN_ORIENTED: ... break; case L2CAP_CHAN_CONN_LESS: ... break; case L2CAP_CHAN_FIXED: ... break; default: ... } }Quoted 2 lines
Apparently it depends on
chan->chan_typeand it is set by ref1. It is only set in cb function.Ok, this probably needs fixing in 6lowpan. These defaults should come from the parent channel, similar to how it's done in
l2cap_sock_init. Perhaps we should promote that logic tol2cap_chan_set_defaultspassing pchan so it can initialize the channel before calling__l2cap_chan_add.
Quoted 7 lines
Quoted 2 lines
I can fix that in a separate patch.
Could you please merge it first?Merge it first, and then rework the order? Not sure what is the benefit of doing that, it is a somewhat substatial change either way so I don't think it will make it much easier to lets say backport.
Will do a patch v10.
Quoted 7 lines
Quoted 2 lines
Best,
SiweiSignature · 1 line
Luiz Augusto von Dentz
Best,
Siwei