Skip to contentSkip to conversation

Bluetooth: L2CAP: Fix use-after-free in l2cap_sock_new_connection_cb()

PATCH Superseded Siwei Zhang 3 participants 4 messages Jun 2

Subsystems (2)

v8 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v8 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.

Siwei Zhang (1):
Bluetooth: L2CAP: Fix use-after-free in l2cap_sock_new_connection_cb()

 include/net/bluetooth/l2cap.h |  8 ++--
 net/bluetooth/6lowpan.c       | 32 +++++++++------
 net/bluetooth/l2cap_core.c    | 60 ++++++++++++++++++++-------
 net/bluetooth/l2cap_sock.c    | 76 ++++++++++++++++++++++-------------
 net/bluetooth/smp.c           | 18 ++++-----
 5 files changed, 126 insertions(+), 68 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH v8 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.

Fixes 8ffb929098a5 ("Bluetooth: Remove parent socket usage from l2cap_core.c")
Assisted-by Claude:claude-opus-4-6
---
 include/net/bluetooth/l2cap.h |  8 ++--
 net/bluetooth/6lowpan.c       | 32 +++++++++------
 net/bluetooth/l2cap_core.c    | 60 ++++++++++++++++++++-------
 net/bluetooth/l2cap_sock.c    | 76 ++++++++++++++++++++++-------------
 net/bluetooth/smp.c           | 18 ++++-----
 5 files changed, 126 insertions(+), 68 deletions(-)
include/net/bluetooth/l2cap.h +5 -3
index e0a1f2293679..ffd006187324 100644
@@ -620,7 +620,8 @@ struct l2cap_chan {
 struct l2cap_ops {
 	char			*name;
 
-	struct l2cap_chan	*(*new_connection) (struct l2cap_chan *chan);
+	int			(*new_connection)(struct l2cap_chan *chan,
+						  struct l2cap_chan *new_chan);
 	int			(*recv) (struct l2cap_chan * chan,
 					 struct sk_buff *skb);
 	void			(*teardown) (struct l2cap_chan *chan, int err);
@@ -884,9 +885,10 @@ static inline __u16 __next_seq(struct l2cap_chan *chan, __u16 seq)
 	return (seq + 1) % (chan->tx_win_max + 1);
 }
 
-static inline struct l2cap_chan *l2cap_chan_no_new_connection(struct l2cap_chan *chan)
+static inline int l2cap_chan_no_new_connection(struct l2cap_chan *chan,
+					       struct l2cap_chan *new_chan)
 {
-	return NULL;
+	return -EOPNOTSUPP;
 }
 
 static inline int l2cap_chan_no_recv(struct l2cap_chan *chan, struct sk_buff *skb)
net/bluetooth/6lowpan.c +19 -13
index cb1e329d66fd..40f118168015 100644
@@ -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,20 @@ static inline void chan_ready_cb(struct l2cap_chan *chan)
 	ifup(dev->netdev);
 }
 
-static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
+static inline int chan_new_conn_cb(struct l2cap_chan *pchan,
+				   struct l2cap_chan *chan)
 {
-	struct l2cap_chan *chan;
-
-	chan = chan_create();
-	if (!chan)
-		return NULL;
-
+	chan_init(chan);
 	chan->ops = pchan->ops;
 
+	/* Take a reference to match the put in chan_close_cb(). The caller
+	 * drops its own local reference after __l2cap_chan_add().
+	 */
+	l2cap_chan_hold(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 +45 -15

137 lines not on this page · raw message

net/bluetooth/l2cap_sock.c +48 -28

161 lines not on this page · raw message

net/bluetooth/smp.c +9 -9
index 1739c1989dbd..887a2fc34391 100644
@@ -3204,16 +3204,11 @@ static const struct l2cap_ops smp_chan_ops = {
 	.get_sndtimeo		= l2cap_chan_no_get_sndtimeo,
 };
 
-static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
+static inline int smp_new_conn_cb(struct l2cap_chan *pchan,
+				  struct l2cap_chan *chan)
 {
-	struct l2cap_chan *chan;
-
 	BT_DBG("pchan %p", pchan);
 
-	chan = l2cap_chan_create();
-	if (!chan)
-		return NULL;
-
 	chan->chan_type	= pchan->chan_type;
 	chan->ops	= &smp_chan_ops;
 	chan->scid	= pchan->scid;
@@ -3229,9 +3224,14 @@ 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 a reference to match the put in smp_teardown_cb(). The caller
+	 * drops its own local reference after __l2cap_chan_add().
+	 */
+	l2cap_chan_hold(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
#2 lore raw

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=1104625

---Test result---

Test Summary:

CheckPatch                    PASS      2.73 seconds
VerifyFixes                   PASS      0.12 seconds
VerifySignedoff               PASS      0.12 seconds
GitLint                       PASS      0.30 seconds
SubjectPrefix                 PASS      0.11 seconds
BuildKernel                   PASS      25.05 seconds
CheckAllWarning               PASS      27.86 seconds
CheckSparse                   PASS      26.23 seconds
BuildKernel32                 PASS      24.55 seconds
TestRunnerSetup               PASS      535.77 seconds
TestRunner_l2cap-tester       PASS      59.84 seconds
TestRunner_smp-tester         PASS      23.74 seconds
TestRunner_6lowpan-tester     PASS      23.40 seconds
IncrementalBuild              PASS      24.33 seconds

https://github.com/bluez/bluetooth-next/pull/273

---
Regards,
Linux Bluetooth

#3 lore raw
In reply to Siwei Zhang

Hi Siwei,

Quoted 90 lines · Siwei Zhang

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.

Fixes 8ffb929098a5 ("Bluetooth: Remove parent socket usage from l2cap_core.c")
Assisted-by Claude:claude-opus-4-6
---
 include/net/bluetooth/l2cap.h |  8 ++--
 net/bluetooth/6lowpan.c       | 32 +++++++++------
 net/bluetooth/l2cap_core.c    | 60 ++++++++++++++++++++-------
 net/bluetooth/l2cap_sock.c    | 76 ++++++++++++++++++++++-------------
 net/bluetooth/smp.c           | 18 ++++-----
 5 files changed, 126 insertions(+), 68 deletions(-)
include/net/bluetooth/l2cap.h +5 -3
index e0a1f2293679..ffd006187324 100644
@@ -620,7 +620,8 @@ struct l2cap_chan {
 struct l2cap_ops {
        char                    *name;
 
-       struct l2cap_chan       *(*new_connection) (struct l2cap_chan *chan);
+       int                     (*new_connection)(struct l2cap_chan *chan,
+                                                 struct l2cap_chan *new_chan);
        int                     (*recv) (struct l2cap_chan * chan,
                                         struct sk_buff *skb);
        void                    (*teardown) (struct l2cap_chan *chan, int err);
@@ -884,9 +885,10 @@ static inline __u16 __next_seq(struct l2cap_chan *chan, __u16 seq)
        return (seq + 1) % (chan->tx_win_max + 1);
 }
 
-static inline struct l2cap_chan *l2cap_chan_no_new_connection(struct l2cap_chan *chan)
+static inline int l2cap_chan_no_new_connection(struct l2cap_chan *chan,
+                                              struct l2cap_chan *new_chan)
 {
-       return NULL;
+       return -EOPNOTSUPP;
 }
 
 static inline int l2cap_chan_no_recv(struct l2cap_chan *chan, struct sk_buff *skb)
net/bluetooth/6lowpan.c +12 -9
index cb1e329d66fd..40f118168015 100644
@@ -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,20 @@ static inline void chan_ready_cb(struct l2cap_chan *chan)
        ifup(dev->netdev);
 }
 
-static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
+static inline int chan_new_conn_cb(struct l2cap_chan *pchan,
+                                  struct l2cap_chan *chan)
 {
-       struct l2cap_chan *chan;
-
-       chan = chan_create();

net/bluetooth/6lowpan.c static inline void chan_ready_cb(struct l2cap_chan *chan) · line 762

Quoted 10 lines
net/bluetooth/6lowpan.c +5 -3
-       if (!chan)
-               return NULL;
-
+       chan_init(chan);
        chan->ops = pchan->ops;
 
+       /* Take a reference to match the put in chan_close_cb(). The caller
+        * drops its own local reference after __l2cap_chan_add().
+        */
+       l2cap_chan_hold(chan);

If we are not going to assign to any variable here, I'm not sure I agree that taking a reference is the best thing to do.

Quoted 34 lines
net/bluetooth/6lowpan.c +1 -1
        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 +19 -0
index c4ccfbda9d78..31fb715fd830 100644
@@ -4007,6 +4007,36 @@ static inline int l2cap_command_rej(struct l2cap_conn *conn,
        return 0;
 }
 
+/* Allocate and initialise a channel for an incoming connection. The returned
+ * channel carries the caller's local reference, which must be dropped once
+ * __l2cap_chan_add() has pinned it via the conn list.
+ */
+static struct l2cap_chan *l2cap_new_connection(struct l2cap_chan *pchan)
+{
+       struct l2cap_chan *chan;
+
+       chan = l2cap_chan_create();
+       if (!chan)
+               return NULL;
+
+       if (pchan->ops->new_connection(pchan, chan) < 0) {
+               l2cap_chan_put(chan);
+               return NULL;
+       }
+
+       return chan;
+}

net/bluetooth/l2cap_core.c static inline int l2cap_command_rej(struct l2cap_conn *conn, · line 4038

Quoted 10 lines
net/bluetooth/l2cap_core.c +10 -0
+
+/* Link a channel from l2cap_new_connection() into conn and release the local
+ * reference it carried. __l2cap_chan_add() pins the channel via the conn list,
+ * so it remains valid after this returns.
+ */
+static void l2cap_chan_add_new(struct l2cap_conn *conn, struct l2cap_chan *chan)
+{
+       __l2cap_chan_add(conn, chan);
+       l2cap_chan_put(chan);
+}

Couldn't we just make __l2cap_chan_add take ownership of the reference, instead of doing multiple calls to l2cap_chan_hold/l2cap_chan_put?

Quoted 160 lines
net/bluetooth/l2cap_core.c +15 -15
 static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd,
                          u8 *data, u8 rsp_code)
 {
@@ -4053,7 +4083,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(pchan);
        if (!chan)
                goto response;
 
@@ -4071,7 +4101,7 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd,
        chan->psm  = psm;
        chan->dcid = scid;
 
-       __l2cap_chan_add(conn, chan);
+       l2cap_chan_add_new(conn, chan);
 
        dcid = chan->scid;
 
@@ -4883,6 +4913,7 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn,
        struct l2cap_le_conn_rsp rsp;
        struct l2cap_chan *chan, *pchan;
        u16 dcid, scid, credits, mtu, mps;
+       u16 rsp_mtu, rsp_mps;
        __le16 psm;
        u8 result;
 
@@ -4895,6 +4926,8 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn,
        psm  = req->psm;
        dcid = 0;
        credits = 0;
+       rsp_mtu = 0;
+       rsp_mps = 0;
 
        if (mtu < 23 || mps < 23)
                return -EPROTO;
@@ -4955,7 +4988,7 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn,
                goto response_unlock;
        }
 
-       chan = pchan->ops->new_connection(pchan);
+       chan = l2cap_new_connection(pchan);
        if (!chan) {
                result = L2CAP_CR_LE_NO_MEM;
                goto response_unlock;
@@ -4970,12 +5003,14 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn,
        chan->omtu = mtu;
        chan->remote_mps = mps;
 
-       __l2cap_chan_add(conn, chan);
+       l2cap_chan_add_new(conn, chan);
 
        l2cap_le_flowctl_init(chan, __le16_to_cpu(req->credits));
 
        dcid = chan->scid;
        credits = chan->rx_credits;
+       rsp_mtu = chan->imtu;
+       rsp_mps = chan->mps;
 
        __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
 
@@ -5003,13 +5038,8 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn,
                return 0;
 
 response:
-       if (chan) {
-               rsp.mtu = cpu_to_le16(chan->imtu);
-               rsp.mps = cpu_to_le16(chan->mps);
-       } else {
-               rsp.mtu = 0;
-               rsp.mps = 0;
-       }
+       rsp.mtu = cpu_to_le16(rsp_mtu);
+       rsp.mps = cpu_to_le16(rsp_mps);
 
        rsp.dcid    = cpu_to_le16(dcid);
        rsp.credits = cpu_to_le16(credits);
@@ -5179,7 +5209,7 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,
                        continue;
                }
 
-               chan = pchan->ops->new_connection(pchan);
+               chan = l2cap_new_connection(pchan);
                if (!chan) {
                        result = L2CAP_CR_LE_NO_MEM;
                        continue;
@@ -5194,7 +5224,7 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,
                chan->omtu = mtu;
                chan->remote_mps = mps;
 
-               __l2cap_chan_add(conn, chan);
+               l2cap_chan_add_new(conn, chan);
 
                l2cap_ecred_init(chan, __le16_to_cpu(req->credits));
 
@@ -7470,14 +7500,14 @@ 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(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_add_new(conn, chan);
                }
 
                l2cap_chan_unlock(pchan);
net/bluetooth/l2cap_sock.c +14 -6
index 025329636353..745b012890ee 100644
@@ -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)
@@ -1292,18 +1293,25 @@ static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg,
  */
 static void l2cap_sock_kill(struct sock *sk)
 {
+       struct l2cap_chan *chan = l2cap_pi(sk)->chan;
+
        if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
                return;
 
        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.
+       /* Release the sock's ref on chan and clear the pointer so that
+        * l2cap_sock_destruct() does not drop it a second time. Setting
+        * chan->data to NULL first stops any other task from dereferencing
+        * the now-dead sock pointer.
         */
-       l2cap_pi(sk)->chan->data = NULL;
-       /* Kill poor orphan */
+       if (chan) {
+               chan->data = NULL;
+               l2cap_pi(sk)->chan = NULL;
+               l2cap_chan_put(chan);
+       }
 
-       l2cap_chan_put(l2cap_pi(sk)->chan);
+       /* Kill poor orphan */
        sock_set_flag(sk, SOCK_DEAD);
        sock_put(sk);
 }

net/bluetooth/l2cap_sock.c static int l2cap_sock_shutdown(struct socket *sock, int how) · line 1396

Quoted 7 lines
net/bluetooth/l2cap_sock.c +3 -1
@@ -1383,7 +1391,9 @@ static int l2cap_sock_shutdown(struct socket *sock, int how)
        sock_hold(sk);
 
        /* prevent chan structure from being freed whilst unlocked */
-       chan = l2cap_chan_hold_unless_zero(l2cap_pi(sk)->chan);
+       chan = l2cap_pi(sk)->chan;
+       if (chan)
+               chan = l2cap_chan_hold_unless_zero(chan);

l2cap_chan_hold_unless_zero already handles !chan so no need to change anything here.

Quoted 110 lines
net/bluetooth/l2cap_sock.c +22 -21
        if (!chan)
                goto shutdown_already;
 
@@ -1546,12 +1556,13 @@ 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_chan *chan,
+                                       struct l2cap_chan *new_chan)
 {
        struct sock *sk, *parent = chan->data;
 
        if (!parent)
-               return NULL;
+               return -EINVAL;
 
        lock_sock(parent);
 
@@ -1559,15 +1570,15 @@ 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);
 
@@ -1577,7 +1588,7 @@ static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)
 
        release_sock(parent);
 
-       return l2cap_pi(sk)->chan;
+       return 0;
 }
 
 static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
@@ -1875,8 +1886,11 @@ 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);
+               struct l2cap_chan *chan = l2cap_pi(sk)->chan;
+
+               chan->data = NULL;
+               l2cap_pi(sk)->chan = NULL;
+               l2cap_chan_put(chan);
        }
 
        list_for_each_entry_safe(rx_busy, next, &l2cap_pi(sk)->rx_busy, list) {
@@ -1978,10 +1992,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,14 +2006,11 @@ 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;
-       }
-
+       /* The sock owns a single ref on chan. It is released by whichever of
+        * l2cap_sock_kill() or l2cap_sock_destruct() runs first; that path
+        * clears l2cap_pi(sk)->chan so the ref is dropped exactly once. The
+        * caller keeps its own ref independent of this one.
+        */
        l2cap_chan_hold(chan);
 
        l2cap_pi(sk)->chan = chan;
@@ -2011,6 +2022,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,9 +2037,17 @@ 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();

net/bluetooth/l2cap_sock.c static int l2cap_sock_create(struct net *net, struct socket *sock, int protocol, · line 2050

Quoted 10 lines
net/bluetooth/l2cap_sock.c +9 -0
+       if (!chan)
+               return -ENOMEM;
+
+       sk = l2cap_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern, chan);
+       if (!sk) {
+               l2cap_chan_put(chan);
                return -ENOMEM;
+       }
+       /* Sock has taken its own refs on chan; drop the chan_create() ref. */
+       l2cap_chan_put(chan);

We can probably eliminate another pair of l2cap_chan_hold/l2cap_chan_put here as well, just take the ownership when doing l2cap_pi(sk)->chan = chan;, except if we are trying to prevent use after free but until l2cap_chan is attached to a sk that should be probably safe.

Quoted 27 lines
net/bluetooth/l2cap_sock.c +0 -0
        l2cap_sock_init(sk, NULL);
        bt_sock_link(&l2cap_sk_list, sk);
net/bluetooth/smp.c +2 -7
index 1739c1989dbd..887a2fc34391 100644
@@ -3204,16 +3204,11 @@ static const struct l2cap_ops smp_chan_ops = {
        .get_sndtimeo           = l2cap_chan_no_get_sndtimeo,
 };
 
-static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
+static inline int smp_new_conn_cb(struct l2cap_chan *pchan,
+                                 struct l2cap_chan *chan)
 {
-       struct l2cap_chan *chan;
-
        BT_DBG("pchan %p", pchan);
 
-       chan = l2cap_chan_create();
-       if (!chan)
-               return NULL;
-
        chan->chan_type = pchan->chan_type;
        chan->ops       = &smp_chan_ops;
        chan->scid      = pchan->scid;

net/bluetooth/smp.c static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan) · line 3230

Quoted 8 lines
net/bluetooth/smp.c +4 -1
@@ -3229,9 +3224,14 @@ 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 a reference to match the put in smp_teardown_cb(). The caller
+        * drops its own local reference after __l2cap_chan_add().
+        */
+       l2cap_chan_hold(chan);

Ditto, if we are not attaching to a field of the sk then we shouldn't need to hold a reference.

Quoted 10 lines
net/bluetooth/smp.c +3 -2
-       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