Bluetooth: L2CAP: Fix use-after-free in l2cap_sock_new_connection_cb()
Subsystems (2)
v4 cover Bluetooth: L2CAP: Fix use-after-free in l2cap_sock_new_connection_cb()
This addresses v2 comments on https://sashiko.dev/#/patchset/20260415204842.2363950-1-oss%40fourdim.xyz .
Compared to v3, rebase against bluetooth-next.
Resend due to the missing version number.
Siwei Zhang (1):
Bluetooth: L2CAP: Fix use-after-free in l2cap_sock_new_connection_cb()
net/bluetooth/6lowpan.c | 5 +++++ net/bluetooth/l2cap_core.c | 12 ++++++++++++ net/bluetooth/l2cap_sock.c | 13 ++++++++++++- net/bluetooth/smp.c | 5 +++++ 4 files changed, 34 insertions(+), 1 deletion(-)
Signature · 1 line
2.54.0
PATCH RESEND v4 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.
Save the channel pointer into a local variable while the parent lock is still held to prevent this.
--- net/bluetooth/6lowpan.c | 5 +++++ net/bluetooth/l2cap_core.c | 12 ++++++++++++ net/bluetooth/l2cap_sock.c | 13 ++++++++++++- net/bluetooth/smp.c | 5 +++++ 4 files changed, 34 insertions(+), 1 deletion(-)
net/bluetooth/6lowpan.c +5 -0
@@ -755,6 +755,11 @@ static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)BT_DBG ("chan %p pchan %p" , chan, pchan); +/* Match the put that the caller of ops->new_connection() performs +* once it is done with the returned channel pointer. +*/ +l2cap_chan_hold (chan); +return chan; }
net/bluetooth/l2cap_core.c +12 -0
@@ -4071,6 +4071,9 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd,__l2cap_chan_add (conn, chan); +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); + dcid = chan->scid;__set_chan_timer (chan, chan->ops->get_sndtimeo (chan));
@@ -4970,6 +4973,9 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn,__l2cap_chan_add (conn, chan); +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); +l2cap_le_flowctl_init (chan,__le16_to_cpu (req->credits)); dcid = chan->scid;
@@ -5194,6 +5200,9 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,__l2cap_chan_add (conn, chan); +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); +l2cap_ecred_init (chan,__le16_to_cpu (req->credits));/* Init response */
@@ -7407,6 +7416,9 @@ static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) chan->dst_type = dst_type;__l2cap_chan_add (conn, chan); + +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); }l2cap_chan_unlock (pchan);
net/bluetooth/l2cap_sock.c +12 -1
@@ -1497,6 +1497,7 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)static struct l2cap_chan *l2cap_sock_new_connection_cb (struct l2cap_chan *chan) {struct sock *sk, *parent = chan->data; +struct l2cap_chan *child_chan;if (!parent)return NULL ;
@@ -1523,9 +1524,19 @@ static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)bt_accept_enqueue (parent, sk, false); + child_chan =l2cap_pi (sk)->chan; + +/* Pin the channel for the caller. Once release_sock(parent) returns, +* userspace can accept(2) and immediately close(2) the child socket, +* which would drop the socket's references on the channel and free +* it before the caller (e.g. l2cap_connect_req()) is done using the +* returned pointer. The matching put is the caller's responsibility. +*/ +l2cap_chan_hold (child_chan); +release_sock (parent); -return l2cap_pi (sk)->chan; +return child_chan; }static int l2cap_sock_recv_cb (struct l2cap_chan *chan,struct sk_buff *skb)
net/bluetooth/smp.c +5 -0
@@ -3231,6 +3231,11 @@ static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)BT_DBG ("created chan %p" , chan); +/* Match the put that the caller of ops->new_connection() performs +* once it is done with the returned channel pointer. +*/ +l2cap_chan_hold (chan); +return chan; }
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=1092984
---Test result---
Test Summary:
CheckPatch PASS 1.40 seconds GitLint FAIL 0.34 seconds SubjectPrefix PASS 0.13 seconds BuildKernel PASS 26.78 seconds CheckAllWarning PASS 27.90 seconds CheckSparse PASS 26.85 seconds BuildKernel32 PASS 24.71 seconds TestRunnerSetup PASS 524.97 seconds TestRunner_l2cap-tester PASS 376.41 seconds TestRunner_smp-tester PASS 18.41 seconds TestRunner_6lowpan-tester PASS 51.29 seconds IncrementalBuild PASS 23.95 seconds
Details
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
[RESEND,v4,1/1] Bluetooth: L2CAP: Fix use-after-free in l2cap_sock_new_connection_cb()
WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
1: T1 Title exceeds max length (86>80): "[RESEND,v4,1/1] Bluetooth: L2CAP: Fix use-after-free in l2cap_sock_new_connection_cb()"
https://github.com/bluez/bluetooth-next/pull/172
---
Regards,
Linux Bluetooth
Luiz Augusto von Dentz
maintainerHi,
Quoted 94 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.Save the channel pointer into a local variable while the parent lock is still held to prevent this.
Fixes 8ffb929098a5 ("Bluetooth: Remove parent socket usage from l2cap_core.c")Assisted-by Claude:claude-opus-4-7Signed-off-by Siwei Zhang <[email protected]>--- net/bluetooth/6lowpan.c | 5 +++++ net/bluetooth/l2cap_core.c | 12 ++++++++++++ net/bluetooth/l2cap_sock.c | 13 ++++++++++++- net/bluetooth/smp.c | 5 +++++ 4 files changed, 34 insertions(+), 1 deletion(-)net/bluetooth/6lowpan.c +5
-0@@ -755,6 +755,11 @@ static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)BT_DBG ("chan %p pchan %p" , chan, pchan); +/* Match the put that the caller of ops->new_connection() performs +* once it is done with the returned channel pointer. +*/ +l2cap_chan_hold (chan); +return chan; }net/bluetooth/l2cap_core.c +12
-0@@ -4071,6 +4071,9 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd,__l2cap_chan_add (conn, chan); +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); + dcid = chan->scid;__set_chan_timer (chan, chan->ops->get_sndtimeo (chan));@@ -4970,6 +4973,9 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn,__l2cap_chan_add (conn, chan); +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); +l2cap_le_flowctl_init (chan,__le16_to_cpu (req->credits)); dcid = chan->scid;@@ -5194,6 +5200,9 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,__l2cap_chan_add (conn, chan); +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); +l2cap_ecred_init (chan,__le16_to_cpu (req->credits));/* Init response */ @@ -7407,6 +7416,9 @@ static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) chan->dst_type = dst_type;__l2cap_chan_add (conn, chan); + +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); }l2cap_chan_unlock (pchan);net/bluetooth/l2cap_sock.c +1
-0@@ -1497,6 +1497,7 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)static struct l2cap_chan *l2cap_sock_new_connection_cb (struct l2cap_chan *chan) {struct sock *sk, *parent = chan->data; +struct l2cap_chan *child_chan;if (!parent)return NULL ;@@ -1523,9 +1524,19 @@ static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)bt_accept_enqueue (parent, sk, false);
net/bluetooth/l2cap_sock.c static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan) · line 1535
Quoted 10 lines
net/bluetooth/l2cap_sock.c +9
-0+ child_chan =l2cap_pi (sk)->chan; + +/* Pin the channel for the caller. Once release_sock(parent) returns, +* userspace can accept(2) and immediately close(2) the child socket, +* which would drop the socket's references on the channel and free +* it before the caller (e.g. l2cap_connect_req()) is done using the +* returned pointer. The matching put is the caller's responsibility. +*/ +l2cap_chan_hold (child_chan);
The entire problem might be solvable by not removing list_add from l2cap_create_chan. This way, it only allocates but does not attach to global_l until __l2cap_chan_add is called which then handles the addition. Alternatively, we could allocate it first, given the circular dependency involving l2cap_core.c->l2cap_sock.c) new_connection -> l2cap_chan_create(l2cap_sock.c->l2cap_core.c) which makes the code rather hard to follow.
Quoted 26 lines
net/bluetooth/l2cap_sock.c +1
-1release_sock (parent); -return l2cap_pi (sk)->chan; +return child_chan; }static int l2cap_sock_recv_cb (struct l2cap_chan *chan,struct sk_buff *skb)net/bluetooth/smp.c +5
-1@@ -3231,6 +3231,11 @@ static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)BT_DBG ("created chan %p" , chan); +/* Match the put that the caller of ops->new_connection() performs +* once it is done with the returned channel pointer. +*/ +l2cap_chan_hold (chan); +return chan; } --2.54.0
Signature · 1 line
Luiz Augusto von Dentz
Hi Luiz,
Quoted 98 lines · Luiz Augusto von Dentz
Hi,
Quoted 95 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.Save the channel pointer into a local variable while the parent lock is still held to prevent this.
Fixes 8ffb929098a5 ("Bluetooth: Remove parent socket usage from l2cap_core.c")Assisted-by Claude:claude-opus-4-7Signed-off-by Siwei Zhang <[email protected]>--- net/bluetooth/6lowpan.c | 5 +++++ net/bluetooth/l2cap_core.c | 12 ++++++++++++ net/bluetooth/l2cap_sock.c | 13 ++++++++++++- net/bluetooth/smp.c | 5 +++++ 4 files changed, 34 insertions(+), 1 deletion(-)net/bluetooth/6lowpan.c +5
-0@@ -755,6 +755,11 @@ static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)BT_DBG ("chan %p pchan %p" , chan, pchan); +/* Match the put that the caller of ops->new_connection() performs +* once it is done with the returned channel pointer. +*/ +l2cap_chan_hold (chan); +return chan; }net/bluetooth/l2cap_core.c +12
-0@@ -4071,6 +4071,9 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd,__l2cap_chan_add (conn, chan); +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); + dcid = chan->scid;__set_chan_timer (chan, chan->ops->get_sndtimeo (chan));@@ -4970,6 +4973,9 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn,__l2cap_chan_add (conn, chan); +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); +l2cap_le_flowctl_init (chan,__le16_to_cpu (req->credits)); dcid = chan->scid;@@ -5194,6 +5200,9 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,__l2cap_chan_add (conn, chan); +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); +l2cap_ecred_init (chan,__le16_to_cpu (req->credits));/* Init response */ @@ -7407,6 +7416,9 @@ static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) chan->dst_type = dst_type;__l2cap_chan_add (conn, chan); + +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); }l2cap_chan_unlock (pchan);net/bluetooth/l2cap_sock.c +1
-0@@ -1497,6 +1497,7 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)static struct l2cap_chan *l2cap_sock_new_connection_cb (struct l2cap_chan *chan) {struct sock *sk, *parent = chan->data; +struct l2cap_chan *child_chan;if (!parent)return NULL ;@@ -1523,9 +1524,19 @@ static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)bt_accept_enqueue (parent, sk, false);
Quoted 14 lines
net/bluetooth/l2cap_sock.c static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan) · line 1535
Quoted 9 lines
net/bluetooth/l2cap_sock.c +9
-0+ child_chan =l2cap_pi (sk)->chan; + +/* Pin the channel for the caller. Once release_sock(parent) returns, +* userspace can accept(2) and immediately close(2) the child socket, +* which would drop the socket's references on the channel and free +* it before the caller (e.g. l2cap_connect_req()) is done using the +* returned pointer. The matching put is the caller's responsibility. +*/ +l2cap_chan_hold (child_chan);The entire problem might be solvable by not removing
list_addfroml2cap_create_chan. This way, it only allocates but does not attach toglobal_luntil__l2cap_chan_addis called which then handles the addition.
Could you please clarify what is "not removing list_add from l2cap_chan_create"? I am not touching that part of code nor removing the list_add. Do you want me to correspond the chan lifetime to chan_list?
Quoted 3 lines
Alternatively, we could allocate it first, given the circular dependency involving
l2cap_core.c->l2cap_sock.c)new_connection->l2cap_chan_create(l2cap_sock.c->l2cap_core.c)which
This will change the signature of l2cap_ops.new_connection, which will be a large refactoring across multiple files.
Signature will be changed fromstruct l2cap_chan *(*new_connection) (struct l2cap_chan *chan);
tovoid (*new_connection) (struct l2cap_chan *chan, struct l2cap_chan *new_chan) Do you want me to this in this patch or in the follow up patch?
If in this patch, I will drop the backport cc to stable.
Quoted 2 lines
makes the code rather hard to follow.
I totally understand this will create a maintenance problem. I created this patch mainly because it can be easily to be backported to the stable branches and it is the safest fix (though ugly). I can send a follow up patch for this to refactor it according to your suggestions.
Quoted 23 lines
Quoted 23 lines
net/bluetooth/l2cap_sock.c +1
-1release_sock (parent); -return l2cap_pi (sk)->chan; +return child_chan; }static int l2cap_sock_recv_cb (struct l2cap_chan *chan,struct sk_buff *skb)net/bluetooth/smp.c +5
-0@@ -3231,6 +3231,11 @@ static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)BT_DBG ("created chan %p" , chan); +/* Match the put that the caller of ops->new_connection() performs +* once it is done with the returned channel pointer. +*/ +l2cap_chan_hold (chan); +return chan; }
Quoted 7 lines
Quoted 3 lines
--
2.54.0Signature · 1 line
Luiz Augusto von Dentz
Best,
Siwei
Luiz Augusto von Dentz
maintainerHi Siwei,
Quoted 135 lines · Siwei Zhang
Hi Luiz,
Quoted 98 lines · Luiz Augusto von Dentz
Hi,
Quoted 95 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.Save the channel pointer into a local variable while the parent lock is still held to prevent this.
Fixes 8ffb929098a5 ("Bluetooth: Remove parent socket usage from l2cap_core.c")Assisted-by Claude:claude-opus-4-7Signed-off-by Siwei Zhang <[email protected]>--- net/bluetooth/6lowpan.c | 5 +++++ net/bluetooth/l2cap_core.c | 12 ++++++++++++ net/bluetooth/l2cap_sock.c | 13 ++++++++++++- net/bluetooth/smp.c | 5 +++++ 4 files changed, 34 insertions(+), 1 deletion(-)net/bluetooth/6lowpan.c +5
-0@@ -755,6 +755,11 @@ static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)BT_DBG ("chan %p pchan %p" , chan, pchan); +/* Match the put that the caller of ops->new_connection() performs +* once it is done with the returned channel pointer. +*/ +l2cap_chan_hold (chan); +return chan; }net/bluetooth/l2cap_core.c +12
-0@@ -4071,6 +4071,9 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd,__l2cap_chan_add (conn, chan); +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); + dcid = chan->scid;__set_chan_timer (chan, chan->ops->get_sndtimeo (chan));@@ -4970,6 +4973,9 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn,__l2cap_chan_add (conn, chan); +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); +l2cap_le_flowctl_init (chan,__le16_to_cpu (req->credits)); dcid = chan->scid;@@ -5194,6 +5200,9 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,__l2cap_chan_add (conn, chan); +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); +l2cap_ecred_init (chan,__le16_to_cpu (req->credits));/* Init response */ @@ -7407,6 +7416,9 @@ static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) chan->dst_type = dst_type;__l2cap_chan_add (conn, chan); + +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); }l2cap_chan_unlock (pchan);net/bluetooth/l2cap_sock.c +1
-0@@ -1497,6 +1497,7 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)static struct l2cap_chan *l2cap_sock_new_connection_cb (struct l2cap_chan *chan) {struct sock *sk, *parent = chan->data; +struct l2cap_chan *child_chan;if (!parent)return NULL ;@@ -1523,9 +1524,19 @@ static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)bt_accept_enqueue (parent, sk, false);Quoted 14 lines
net/bluetooth/l2cap_sock.c static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan) · line 1535
Quoted 9 lines
net/bluetooth/l2cap_sock.c +9
-0+ child_chan =l2cap_pi (sk)->chan; + +/* Pin the channel for the caller. Once release_sock(parent) returns, +* userspace can accept(2) and immediately close(2) the child socket, +* which would drop the socket's references on the channel and free +* it before the caller (e.g. l2cap_connect_req()) is done using the +* returned pointer. The matching put is the caller's responsibility. +*/ +l2cap_chan_hold (child_chan);The entire problem might be solvable by not removing
list_addfroml2cap_create_chan. This way, it only allocates but does not attach toglobal_luntil__l2cap_chan_addis called which then handles the addition.Could you please clarify what is "not removing
list_addfroml2cap_chan_create"? I am not touching that part of code nor removing thelist_add. Do you want me to correspond thechanlifetime tochan_list?Quoted 3 lines
Alternatively, we could allocate it first, given the circular dependency involving
l2cap_core.c->l2cap_sock.c)new_connection->l2cap_chan_create(l2cap_sock.c->l2cap_core.c)whichThis will change the signature of
l2cap_ops.new_connection, which will be a large refactoring across multiple files.Signature will be changed from
struct l2cap_chan *(*new_connection) (struct l2cap_chan *chan);
tovoid (*new_connection) (struct l2cap_chan *chan, struct l2cap_chan *new_chan)Do you want me to this in this patch or in the follow up patch?
If in this patch, I will drop the backport cc to stable.
Quoted 7 lines
Quoted 2 lines
makes the code rather hard to follow.
I totally understand this will create a maintenance problem. I created this patch mainly because it can be easily to be backported to the stable branches and it is the safest fix (though ugly). I can send a follow up patch for this to refactor it according to your suggestions.
I don't think adding a new argument would be complicated. This argument could replace the current void return, and although it will affect a couple more places, it shouldn't be hard to backport if it really needs to be backported to begin with.
Quoted 34 lines
Quoted 24 lines
Quoted 24 lines
net/bluetooth/l2cap_sock.c +1
-1release_sock (parent); -return l2cap_pi (sk)->chan; +return child_chan; }static int l2cap_sock_recv_cb (struct l2cap_chan *chan,struct sk_buff *skb)net/bluetooth/smp.c +5
-0@@ -3231,6 +3231,11 @@ static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)BT_DBG ("created chan %p" , chan); +/* Match the put that the caller of ops->new_connection() performs +* once it is done with the returned channel pointer. +*/ +l2cap_chan_hold (chan); +return chan; }Quoted 7 lines
Quoted 3 lines
--
2.54.0--
Luiz Augusto von DentzBest,
Siwei
Signature · 1 line
Luiz Augusto von Dentz
Hi Luiz,
Quoted 146 lines · Luiz Augusto von Dentz
Hi Siwei,
Quoted 135 lines · Siwei Zhang
Hi Luiz,
Quoted 98 lines · Luiz Augusto von Dentz
Hi,
Quoted 95 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.Save the channel pointer into a local variable while the parent lock is still held to prevent this.
Fixes 8ffb929098a5 ("Bluetooth: Remove parent socket usage from l2cap_core.c")Assisted-by Claude:claude-opus-4-7Signed-off-by Siwei Zhang <[email protected]>--- net/bluetooth/6lowpan.c | 5 +++++ net/bluetooth/l2cap_core.c | 12 ++++++++++++ net/bluetooth/l2cap_sock.c | 13 ++++++++++++- net/bluetooth/smp.c | 5 +++++ 4 files changed, 34 insertions(+), 1 deletion(-)net/bluetooth/6lowpan.c +5
-0@@ -755,6 +755,11 @@ static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)BT_DBG ("chan %p pchan %p" , chan, pchan); +/* Match the put that the caller of ops->new_connection() performs +* once it is done with the returned channel pointer. +*/ +l2cap_chan_hold (chan); +return chan; }net/bluetooth/l2cap_core.c +12
-0@@ -4071,6 +4071,9 @@ static void l2cap_connect(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd,__l2cap_chan_add (conn, chan); +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); + dcid = chan->scid;__set_chan_timer (chan, chan->ops->get_sndtimeo (chan));@@ -4970,6 +4973,9 @@ static int l2cap_le_connect_req(struct l2cap_conn *conn,__l2cap_chan_add (conn, chan); +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); +l2cap_le_flowctl_init (chan,__le16_to_cpu (req->credits)); dcid = chan->scid;@@ -5194,6 +5200,9 @@ static inline int l2cap_ecred_conn_req(struct l2cap_conn *conn,__l2cap_chan_add (conn, chan); +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); +l2cap_ecred_init (chan,__le16_to_cpu (req->credits));/* Init response */ @@ -7407,6 +7416,9 @@ static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) chan->dst_type = dst_type;__l2cap_chan_add (conn, chan); + +/* Drop the ops->new_connection() ref; conn list now pins chan. */ +l2cap_chan_put (chan); }l2cap_chan_unlock (pchan);net/bluetooth/l2cap_sock.c +1
-0@@ -1497,6 +1497,7 @@ static void l2cap_sock_cleanup_listen(struct sock *parent)static struct l2cap_chan *l2cap_sock_new_connection_cb (struct l2cap_chan *chan) {struct sock *sk, *parent = chan->data; +struct l2cap_chan *child_chan;if (!parent)return NULL ;@@ -1523,9 +1524,19 @@ static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)bt_accept_enqueue (parent, sk, false);Quoted 14 lines
net/bluetooth/l2cap_sock.c static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan) · line 1535
Quoted 9 lines
net/bluetooth/l2cap_sock.c +9
-0+ child_chan =l2cap_pi (sk)->chan; + +/* Pin the channel for the caller. Once release_sock(parent) returns, +* userspace can accept(2) and immediately close(2) the child socket, +* which would drop the socket's references on the channel and free +* it before the caller (e.g. l2cap_connect_req()) is done using the +* returned pointer. The matching put is the caller's responsibility. +*/ +l2cap_chan_hold (child_chan);The entire problem might be solvable by not removing
list_addfroml2cap_create_chan. This way, it only allocates but does not attach toglobal_luntil__l2cap_chan_addis called which then handles the addition.Could you please clarify what is "not removing
list_addfroml2cap_chan_create"? I am not touching that part of code nor removing thelist_add. Do you want me to correspond thechanlifetime tochan_list?Quoted 3 lines
Alternatively, we could allocate it first, given the circular dependency involving
l2cap_core.c->l2cap_sock.c)new_connection->l2cap_chan_create(l2cap_sock.c->l2cap_core.c)whichThis will change the signature of
l2cap_ops.new_connection, which will be a large refactoring across multiple files.Signature will be changed from
struct l2cap_chan *(*new_connection) (struct l2cap_chan *chan);
tovoid (*new_connection) (struct l2cap_chan *chan, struct l2cap_chan *new_chan)Do you want me to this in this patch or in the follow up patch?
If in this patch, I will drop the backport cc to stable.Quoted 7 lines
Quoted 2 lines
makes the code rather hard to follow.
I totally understand this will create a maintenance problem. I created this patch mainly because it can be easily to be backported to the stable branches and it is the safest fix (though ugly). I can send a follow up patch for this to refactor it according to your suggestions.
Quoted 5 lines
I don't think adding a new argument would be complicated. This argument could replace the current void return, and although it will affect a couple more places, it shouldn't be hard to backport if it really needs to be backported to begin with.
OK, I will do that.
Also, could you please check emails titled with
[PATCH v7] Bluetooth: L2CAP: Fix slab-use-after-free in l2cap_sock_cleanup_listen() I sent it to you privately with CC to [email protected].
Quoted 32 lines
Quoted 32 lines
Quoted 31 lines
Quoted 25 lines
net/bluetooth/l2cap_sock.c +1
-1release_sock (parent); -return l2cap_pi (sk)->chan; +return child_chan; }static int l2cap_sock_recv_cb (struct l2cap_chan *chan,struct sk_buff *skb)net/bluetooth/smp.c +5
-1@@ -3231,6 +3231,11 @@ static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)BT_DBG ("created chan %p" , chan); +/* Match the put that the caller of ops->new_connection() performs +* once it is done with the returned channel pointer. +*/ +l2cap_chan_hold (chan); +return chan; } --Quoted 2 lines
2.54.0
--
Luiz Augusto von Dentz
Quoted 7 lines
Quoted 2 lines
Best,
SiweiSignature · 1 line
Luiz Augusto von Dentz
Best,
Siwei