Skip to contentSkip to conversation

L2CAP: Fix channels being established with insufficient security

PATCH Superseded Luiz Augusto von Dentz 2 participants 4 messages Nov 24 – Nov 25

Subsystems (2)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

v4 cover L2CAP: Fix channels being established with insufficient security

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

l2cap_security_cfm() lowers the security level of a channel to the one of the link when encryption completes, and accepts channels waiting for the security to be met as long as the status is success, so a channel can end up established, or be offered to a listener using BT_DEFER_SETUP, over a link that doesn't meet the security level it requires, e.g. if the peer disables encryption while the channel waits for userspace to authorize it.

The first patch makes l2cap_security_cfm() compare the security level of the link with the one required by the channel: encryption completing below it is a downgrade, so the channel is no longer lowered to match it but disconnected, or refused if it was still pending, and the link is disconnected if the channel is a fixed one since it can't be disconnected at L2CAP. Channels waiting to be accepted or started only proceed if the link meets their security level.

The second patch makes a BR/EDR channel visible to the listener only once it is offered to it, after the security is met, instead of as soon as the connection request is responded as pending, so a listener that accepts without waiting to be notified can't authorize a connection while authentication is still pending.

Tests for both are sent to the BlueZ mailing list as "[PATCH BlueZ v3 0/5] Add tests for security of deferred setup".

A channel requiring a security level that pairing can't provide, e.g. BT_SECURITY_HIGH when the IO capability is NoInputNoOutput, is now refused instead of being connected with the security level the link ended up with.

v4:
- Disconnect the link instead of the channel if a fixed channel is downgraded, since it can't be disconnected at L2CAP.
- Call chan->ops->close() when a channel was deleted by the downgrade, so its socket is not leaked, and also refuse deferred LE Credit Based channels in BT_CONNECT2.
- Only start channels in BT_CONNECT if the link meets their security level.
v3:
- Don't use l2cap_chan_check_security(), which may start authentication or encryption, compare the security levels instead.
- Treat encryption completing below the security level of a channel as a downgrade and disconnect it instead of lowering its security level.
- Only let accept() return children in BT_CONNECT2, as bt_accept_poll() does.
v2:
- Drop BT_SK_ACCEPTED and the changes to l2cap_sock_defer().
- Add the second patch for listeners accepting while the security is still pending.

Luiz Augusto von Dentz (1):
Bluetooth: L2CAP: Don't offer deferred channels before security is met

Mohammed Alquraini (1):
Bluetooth: L2CAP: Fix accepting channels with insufficient security

 net/bluetooth/af_bluetooth.c |  6 +++-
 net/bluetooth/l2cap_core.c   | 58 ++++++++++++++++++++++++++++++++----
 net/bluetooth/l2cap_sock.c   | 10 ++++++-
 3 files changed, 66 insertions(+), 8 deletions(-)
Signature · 1 line
2.56.0
author maintainer
#1 lore raw

PATCH v4 1/2 Bluetooth: L2CAP: Fix accepting channels with insufficient security

From: Mohammed Alquraini <[email protected]>

When the link security changes, l2cap_security_cfm() doesn't check that the result meets the security level required by the channels:

- If encryption completes it sets chan->sec_level to hcon->sec_level, so a channel requiring a higher security level than the one the link ended up with has its requirement lowered instead of being refused.

- A channel in BT_CONNECT2 is accepted, or offered to the listener if it uses BT_DEFER_SETUP, as long as the status is success and the encryption key size is valid, e.g. if the peer disables encryption while the channel waits for userspace to authorize it, the channel is offered again and, once authorized, it is established over an unencrypted link.

- A channel in BT_CONNECT is started as long as the status is success, no matter if the link meets its security level.

Fix this by treating encryption completing with a security level below the one required by a channel as a downgrade: the security level of the channel is no longer lowered and the channel is disconnected, or refused with L2CAP_CR_SEC_BLOCK if it was pending. There is no way to disconnect a fixed channel at L2CAP, so for those the link is disconnected instead. Channels in BT_CONNECT2 and BT_CONNECT are only accepted, or started, if the link meets their security level.

This means a channel requiring a security level that pairing can't provide, e.g. BT_SECURITY_HIGH when the IO capability is NoInputNoOutput, is now refused instead of being connected with the security level the link ended up with.

AI assistance was used in finding and preparing this patch.

Fixes f66dc81f44d9 ("Bluetooth: Add support for deferring L2CAP connection setup")
---
 net/bluetooth/l2cap_core.c | 58 ++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 6 deletions(-)
net/bluetooth/l2cap_core.c +52 -6
index 943effc86740..eea74188db97 100644
@@ -8095,11 +8095,19 @@ static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
 	}
 }
 
+/* Check if the link meets the security level required by the channel */
+static bool l2cap_chan_sec_level_met(struct hci_conn *hcon,
+				     struct l2cap_chan *chan)
+{
+	return chan->sec_level <= BT_SECURITY_LOW ||
+	       hcon->sec_level >= chan->sec_level;
+}
+
 static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
 	__must_hold(&hcon->hdev->lock)
 {
 	struct l2cap_conn *conn = hcon->l2cap_data;
-	struct l2cap_chan *chan;
+	struct l2cap_chan *chan, *tmp;
 
 	if (!conn)
 		return;
@@ -8108,7 +8116,9 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
 
 	mutex_lock(&conn->lock);
 
-	list_for_each_entry(chan, &conn->chan_l, list) {
+	list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
+		bool downgrade = false;
+
 		l2cap_chan_lock(chan);
 
 		lockdep_assert_held(&chan->conn->lock);
@@ -8116,14 +8126,48 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
 		BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid,
 		       state_to_string(chan->state));
 
-		if (!status && encrypt)
-			chan->sec_level = hcon->sec_level;
+		if (!status && encrypt) {
+			/* Encryption completing with a security level below
+			 * the one the channel requires is a downgrade, don't
+			 * lower the requirement of the channel to match it.
+			 */
+			if (!l2cap_chan_sec_level_met(hcon, chan))
+				downgrade = true;
+			else
+				chan->sec_level = hcon->sec_level;
+		}
 
 		if (!__l2cap_no_conn_pending(chan)) {
 			l2cap_chan_unlock(chan);
 			continue;
 		}
 
+		if (downgrade && (chan->state == BT_CONNECTED ||
+				  chan->state == BT_CONFIG ||
+				  (chan->state == BT_CONNECT2 &&
+				   chan->mode == L2CAP_MODE_LE_FLOWCTL))) {
+			/* There is no way to disconnect a fixed channel at
+			 * L2CAP, so drop the link, which takes down the rest
+			 * of the channels as well.
+			 */
+			if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
+				hci_disconnect(hcon, HCI_ERROR_AUTH_FAILURE);
+				l2cap_chan_unlock(chan);
+				break;
+			}
+
+			/* The channel may be removed from the list */
+			l2cap_chan_hold(chan);
+			__l2cap_chan_close(chan, ECONNREFUSED);
+
+			if (test_bit(FLAG_DEL, &chan->flags))
+				chan->ops->close(chan);
+
+			l2cap_chan_unlock(chan);
+			l2cap_chan_put(chan);
+			continue;
+		}
+
 		if (!status && (chan->state == BT_CONNECTED ||
 				chan->state == BT_CONFIG)) {
 			chan->ops->resume(chan);
@@ -8133,7 +8177,8 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
 		}
 
 		if (chan->state == BT_CONNECT) {
-			if (!status && l2cap_check_enc_key_size(hcon, chan))
+			if (!status && l2cap_chan_sec_level_met(hcon, chan) &&
+			    l2cap_check_enc_key_size(hcon, chan))
 				l2cap_start_connection(chan);
 			else
 				__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
@@ -8143,7 +8188,8 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
 			struct l2cap_conn_rsp rsp;
 			__u16 res, stat;
 
-			if (!status && l2cap_check_enc_key_size(hcon, chan)) {
+			if (!status && l2cap_check_enc_key_size(hcon, chan) &&
+			    l2cap_chan_sec_level_met(hcon, chan)) {
 				if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
 					res = L2CAP_CR_PEND;
 					stat = L2CAP_CS_AUTHOR_PEND;
Signature · 1 line
2.56.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/series/1221284/

---Test result---

Test Summary:

CheckPatch                    FAIL      1.78 seconds
VerifyFixes                   PASS      0.12 seconds
VerifySignedoff               PASS      0.12 seconds
GitLint                       PASS      0.66 seconds
SubjectPrefix                 PASS      0.23 seconds
BuildKernel                   PASS      31.47 seconds
CheckAllWarning               PASS      35.50 seconds
CheckSparse                   PASS      38.50 seconds
BuildKernel32                 PASS      31.62 seconds
CheckKernelLLVM               PASS      32.82 seconds
TestRunnerSetup               PASS      757.30 seconds
TestRunner_l2cap-tester       FAIL      15.11 seconds
TestRunner_iso-tester         PASS      13.31 seconds
TestRunner_bnep-tester        PASS      3.17 seconds
TestRunner_mgmt-tester        PASS      89.01 seconds
TestRunner_rfcomm-tester      PASS      4.52 seconds
TestRunner_sco-tester         PASS      5.68 seconds
TestRunner_ioctl-tester       PASS      4.68 seconds
TestRunner_mesh-tester        PASS      4.53 seconds
TestRunner_smp-tester         PASS      4.39 seconds
TestRunner_userchan-tester    PASS      3.26 seconds
TestRunner_6lowpan-tester     PASS      4.47 seconds
IncrementalBuild              PASS      31.84 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[v4,2/2] Bluetooth: L2CAP: Don't offer deferred channels before security is met
WARNING: Non-standard signature: Assisted-by:
#147:

Assisted-by OpenCode:claude-fable-5.2

ERROR: Unrecognized email address: 'OpenCode:claude-fable-5.2' #147:

Assisted-by OpenCode:claude-fable-5.2

total: 1 errors, 1 warnings, 0 checks, 35 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

/home/runner/work/bluetooth-next/bluetooth-next/src/patch/15018725.patch has style problems, please review.

NOTE: Ignored message types: UNKNOWN_COMMIT_ID

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.

##############################
Test: TestRunner_l2cap-tester - FAIL
Desc: Run l2cap-tester with test-runner Output:
Total: 144, Passed: 143 (99.3%), Failed: 1, Not Run: 0

Failed Test Cases
L2CAP BR/EDR Client SSP - Success 2 Failed 0.125 seconds

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

---
Regards,
Linux Bluetooth

author maintainer
#3 lore raw

PATCH v4 2/2 Bluetooth: L2CAP: Don't offer deferred channels before security is met

In reply to Luiz Augusto von Dentz

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

A BR/EDR channel is moved to BT_CONNECT2 as soon as the connection request is answered as pending, no matter if it is waiting for the information exchange, for the link security or for userspace to authorize it, and the socket state follows the channel state.

bt_accept_poll() assumes that a child in BT_CONNECT2 of a listener using BT_DEFER_SETUP is waiting to be authorized, and bt_accept_dequeue() returns any child of such a listener, so although the listener is only notified once the security is met, a listener that doesn't wait for it can accept() the child and then authorize it with recvmsg() while authentication is still pending. l2cap_sock_defer() then moves the channel to BT_CONFIG and responds with L2CAP_CR_SUCCESS, and if authentication fails afterwards the channel is left open since l2cap_security_cfm() doesn't close channels in BT_CONFIG.

Fix this by only moving the socket to BT_CONNECT2 when the channel is offered to the listener with l2cap_sock_defer_cb(), which is only called once the security is met, and by only letting accept() return a child of a listener using BT_DEFER_SETUP once it is in BT_CONNECT2, as bt_accept_poll() does. The child stays in the accept queue so the backlog limit and the cleanup done when the listener is closed are not affected.

Fixes f66dc81f44d9 ("Bluetooth: Add support for deferring L2CAP connection setup")
Assisted-by OpenCode:claude-fable-5.2
---
 net/bluetooth/af_bluetooth.c |  6 +++++-
 net/bluetooth/l2cap_sock.c   | 10 +++++++++-
 2 files changed, 14 insertions(+), 2 deletions(-)
net/bluetooth/af_bluetooth.c +5 -1
index 7758e9ea3848..103d61d13fbf 100644
@@ -330,8 +330,12 @@ struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock)
 			continue;
 		}
 
+		/* With deferred setup only a child in BT_CONNECT2 has been
+		 * offered to the listener, see bt_accept_poll().
+		 */
 		if (sk->sk_state == BT_CONNECTED || !newsock ||
-		    test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags)) {
+		    (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags) &&
+		     sk->sk_state == BT_CONNECT2)) {
 			bt_accept_unlink(sk);
 			if (newsock)
 				sock_graft(sk, newsock);
net/bluetooth/l2cap_sock.c +9 -1
index d5663a4642a9..57ada00f76eb 100644
@@ -1812,7 +1812,13 @@ static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state,
 
 	lock_sock(sk);
 
-	sk->sk_state = state;
+	/* BT_CONNECT2 also covers a channel still waiting for the information
+	 * exchange or link security. Only l2cap_sock_defer_cb() moves the
+	 * socket to BT_CONNECT2, once the channel can be offered to the
+	 * listener.
+	 */
+	if (state != BT_CONNECT2)
+		sk->sk_state = state;
 
 	if (err)
 		sk->sk_err = err;
@@ -1880,6 +1886,8 @@ static void l2cap_sock_defer_cb(struct l2cap_chan *chan)
 
 	lock_sock(sk);
 
+	sk->sk_state = BT_CONNECT2;
+
 	parent = bt_sk(sk)->parent;
 	if (parent)
 		parent->sk_data_ready(parent);
Signature · 1 line
2.56.0