Skip to contentSkip to conversation

Bluetooth: L2CAP: Fix potential user-after-free

PATCH Applied Luiz Augusto von Dentz 4 participants 6 messages Feb 1, 2023 – Feb 15, 2023

Subsystems (2)
lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/1 Bluetooth: L2CAP: Fix potential user-after-free

From: Luiz Augusto von Dentz <[email protected]>

This fixes all instances of which requires to allocate a buffer calling alloc_skb which may release the chan lock and reacquire later which makes it possible that the chan is disconnected in the meantime.

Fixes a6a5568c03c4 ("Bluetooth: Lock the L2CAP channel when sending")
---
 net/bluetooth/l2cap_core.c | 24 ------------------------
 net/bluetooth/l2cap_sock.c |  8 ++++++++
 2 files changed, 8 insertions(+), 24 deletions(-)
net/bluetooth/l2cap_core.c +0 -24
index a3e0dc6a6e73..adfc3ea06d08 100644
@@ -2683,14 +2683,6 @@ int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
 		if (IS_ERR(skb))
 			return PTR_ERR(skb);
 
-		/* Channel lock is released before requesting new skb and then
-		 * reacquired thus we need to recheck channel state.
-		 */
-		if (chan->state != BT_CONNECTED) {
-			kfree_skb(skb);
-			return -ENOTCONN;
-		}
-
 		l2cap_do_send(chan, skb);
 		return len;
 	}
@@ -2735,14 +2727,6 @@ int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
 		if (IS_ERR(skb))
 			return PTR_ERR(skb);
 
-		/* Channel lock is released before requesting new skb and then
-		 * reacquired thus we need to recheck channel state.
-		 */
-		if (chan->state != BT_CONNECTED) {
-			kfree_skb(skb);
-			return -ENOTCONN;
-		}
-
 		l2cap_do_send(chan, skb);
 		err = len;
 		break;
@@ -2763,14 +2747,6 @@ int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
 		 */
 		err = l2cap_segment_sdu(chan, &seg_queue, msg, len);
 
-		/* The channel could have been closed while segmenting,
-		 * check that it is still connected.
-		 */
-		if (chan->state != BT_CONNECTED) {
-			__skb_queue_purge(&seg_queue);
-			err = -ENOTCONN;
-		}
-
 		if (err)
 			break;
 
net/bluetooth/l2cap_sock.c +8 -0
index ca8f07f3542b..eebe256104bc 100644
@@ -1624,6 +1624,14 @@ static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,
 	if (!skb)
 		return ERR_PTR(err);
 
+	/* Channel lock is released before requesting new skb and then
+	 * reacquired thus we need to recheck channel state.
+	 */
+	if (chan->state != BT_CONNECTED) {
+		kfree_skb(skb);
+		return ERR_PTR(-ENOTCONN);
+	}
+
 	skb->priority = sk->sk_priority;
 
 	bt_cb(skb)->l2cap.chan = chan;
Signature · 1 line
2.37.3
#1 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=717902

---Test result---

Test Summary:

CheckPatch                    PASS      1.17 seconds
GitLint                       PASS      0.36 seconds
SubjectPrefix                 PASS      0.13 seconds
BuildKernel                   PASS      37.93 seconds
CheckAllWarning               PASS      41.71 seconds
CheckSparse                   PASS      46.75 seconds
CheckSmatch                   PASS      126.54 seconds
BuildKernel32                 PASS      36.61 seconds
TestRunnerSetup               PASS      525.34 seconds
TestRunner_l2cap-tester       PASS      18.69 seconds
TestRunner_iso-tester         PASS      20.41 seconds
TestRunner_bnep-tester        PASS      6.76 seconds
TestRunner_mgmt-tester        PASS      129.22 seconds
TestRunner_rfcomm-tester      PASS      10.65 seconds
TestRunner_sco-tester         PASS      9.84 seconds
TestRunner_ioctl-tester       PASS      11.64 seconds
TestRunner_mesh-tester        PASS      8.60 seconds
TestRunner_smp-tester         PASS      9.80 seconds
TestRunner_userchan-tester    PASS      7.09 seconds
IncrementalBuild              PASS      34.40 seconds

---
Regards,
Linux Bluetooth

author maintainer
#2 lore raw

Hi Alex,

Quoted 24 lines · On Wed, Feb 1, 2023 at 2:36 PM <[email protected]> wrote:

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

---Test result---

Test Summary:

CheckPatch                    PASS      1.17 seconds
GitLint                       PASS      0.36 seconds
SubjectPrefix                 PASS      0.13 seconds
BuildKernel                   PASS      37.93 seconds
CheckAllWarning               PASS      41.71 seconds
CheckSparse                   PASS      46.75 seconds
CheckSmatch                   PASS      126.54 seconds
BuildKernel32                 PASS      36.61 seconds
TestRunnerSetup               PASS      525.34 seconds
TestRunner_l2cap-tester       PASS      18.69 seconds
TestRunner_iso-tester         PASS      20.41 seconds
TestRunner_bnep-tester        PASS      6.76 seconds
Quoted 8 lines
TestRunner_mgmt-tester        PASS      129.22 seconds
TestRunner_rfcomm-tester      PASS      10.65 seconds
TestRunner_sco-tester         PASS      9.84 seconds
TestRunner_ioctl-tester       PASS      11.64 seconds
TestRunner_mesh-tester        PASS      8.60 seconds
TestRunner_smp-tester         PASS      9.80 seconds
TestRunner_userchan-tester    PASS      7.09 seconds
IncrementalBuild              PASS      34.40 seconds

Looks like it is good with our tests in CI, that said I do wonder if you guys could enable it to run tests that may trigger these problems, since they normally are not found by normal tests.

Quoted 6 lines

---
Regards,
Linux Bluetooth

Signature · 1 line
Luiz Augusto von Dentz
#3 lore raw

Hi Luiz,

Sorry about the very slow response, but I just wanted to make sure that it worked well on our robots for a while (the crashes were sporadic before) before I confirmed that this patch seemed good. We have seen no issues with your proposed patch, and I would be happy to sign off on it if that matters (I don't do much kernel contribution so I don't really know if my sign off means anything).

Regards,
Alexander Coffin

Quoted 51 lines · Luiz Augusto von Dentz

Hi Alex,

Quoted 24 lines · On Wed, Feb 1, 2023 at 2:36 PM <[email protected]> wrote:

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

---Test result---

Test Summary:

CheckPatch                    PASS      1.17 seconds
GitLint                       PASS      0.36 seconds
SubjectPrefix                 PASS      0.13 seconds
BuildKernel                   PASS      37.93 seconds
CheckAllWarning               PASS      41.71 seconds
CheckSparse                   PASS      46.75 seconds
CheckSmatch                   PASS      126.54 seconds
BuildKernel32                 PASS      36.61 seconds
TestRunnerSetup               PASS      525.34 seconds
TestRunner_l2cap-tester       PASS      18.69 seconds
TestRunner_iso-tester         PASS      20.41 seconds
TestRunner_bnep-tester        PASS      6.76 seconds
Quoted 8 lines
TestRunner_mgmt-tester        PASS      129.22 seconds
TestRunner_rfcomm-tester      PASS      10.65 seconds
TestRunner_sco-tester         PASS      9.84 seconds
TestRunner_ioctl-tester       PASS      11.64 seconds
TestRunner_mesh-tester        PASS      8.60 seconds
TestRunner_smp-tester         PASS      9.80 seconds
TestRunner_userchan-tester    PASS      7.09 seconds
IncrementalBuild              PASS      34.40 seconds

Looks like it is good with our tests in CI, that said I do wonder if you guys could enable it to run tests that may trigger these problems, since they normally are not found by normal tests.

Quoted 6 lines

---
Regards,
Linux Bluetooth

--
Luiz Augusto von Dentz

author maintainer
#4 lore raw

Hi Alex,

Quoted 9 lines · Alexander Coffin

Hi Luiz,

Sorry about the very slow response, but I just wanted to make sure that it worked well on our robots for a while (the crashes were sporadic before) before I confirmed that this patch seemed good. We have seen no issues with your proposed patch, and I would be happy to sign off on it if that matters (I don't do much kernel contribution so I don't really know if my sign off means anything).

It has been applied already, but thanks to confirming it is good to go.

Quoted 57 lines

Regards,
Alexander Coffin

Quoted 51 lines · Luiz Augusto von Dentz

Hi Alex,

Quoted 24 lines · On Wed, Feb 1, 2023 at 2:36 PM <[email protected]> wrote:

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

---Test result---

Test Summary:

CheckPatch                    PASS      1.17 seconds
GitLint                       PASS      0.36 seconds
SubjectPrefix                 PASS      0.13 seconds
BuildKernel                   PASS      37.93 seconds
CheckAllWarning               PASS      41.71 seconds
CheckSparse                   PASS      46.75 seconds
CheckSmatch                   PASS      126.54 seconds
BuildKernel32                 PASS      36.61 seconds
TestRunnerSetup               PASS      525.34 seconds
TestRunner_l2cap-tester       PASS      18.69 seconds
TestRunner_iso-tester         PASS      20.41 seconds
TestRunner_bnep-tester        PASS      6.76 seconds
Quoted 8 lines
TestRunner_mgmt-tester        PASS      129.22 seconds
TestRunner_rfcomm-tester      PASS      10.65 seconds
TestRunner_sco-tester         PASS      9.84 seconds
TestRunner_ioctl-tester       PASS      11.64 seconds
TestRunner_mesh-tester        PASS      8.60 seconds
TestRunner_smp-tester         PASS      9.80 seconds
TestRunner_userchan-tester    PASS      7.09 seconds
IncrementalBuild              PASS      34.40 seconds

Looks like it is good with our tests in CI, that said I do wonder if you guys could enable it to run tests that may trigger these problems, since they normally are not found by normal tests.

Quoted 6 lines

---
Regards,
Linux Bluetooth

--
Luiz Augusto von Dentz

Signature · 1 line
Luiz Augusto von Dentz
#5 lore raw
In reply to Luiz Augusto von Dentz
This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

This patch was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 11 lines · On Wed, 1 Feb 2023 14:07:03 -0800 you wrote:

From: Luiz Augusto von Dentz <[email protected]>

This fixes all instances of which requires to allocate a buffer calling alloc_skb which may release the chan lock and reacquire later which makes it possible that the chan is disconnected in the meantime.

Fixes a6a5568c03c4 ("Bluetooth: Lock the L2CAP channel when sending")

[...]

Here is the summary with links:
- Bluetooth: L2CAP: Fix potential user-after-free

    https://git.kernel.org/bluetooth/bluetooth-next/c/c076b7d4e311

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html