Bluetooth: L2CAP: Fix accepting channels with insufficient security
Subsystems (2)
Luiz Augusto von Dentz
maintainerPATCH v3 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.
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, and by only accepting a channel in BT_CONNECT2 if the link meets its 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.
---
v3: Don't use l2cap_chan_check_security(), which may start
authentication or encryption, instead compare the security level of the link with the one of the channel. Treat encryption completing below the security level of the channel as a downgrade and disconnect the channel instead of lowering its security level.
v2: Drop BT_SK_ACCEPTED and the changes to l2cap_sock_defer(), only check
l2cap_chan_check_security() in the BT_CONNECT2 branch of l2cap_security_cfm(). The early accept is handled by patch 2/2.
--- net/bluetooth/l2cap_core.c | 42 ++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-)
net/bluetooth/l2cap_core.c +36 -6
@@ -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,32 @@ 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 )) { +/* Fixed channels are removed from the list */ +l2cap_chan_hold (chan); +__l2cap_chan_close (chan,ECONNREFUSED ); +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 +8161,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 && !downgrade && +l2cap_check_enc_key_size (hcon, chan))l2cap_start_connection (chan);else __set_chan_timer (chan,L2CAP_DISC_TIMEOUT );
@@ -8143,7 +8172,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
Luiz Augusto von Dentz
maintainerPATCH v3 2/2 Bluetooth: L2CAP: Don't offer deferred channels before security is met
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.
---
v3: Only let accept() return children in BT_CONNECT2, as
bt_accept_poll() does, instead of only skipping the ones in BT_OPEN.
v2: New patch.
--- 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
@@ -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
@@ -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
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/1217495/
---Test result---
Test Summary:
CheckPatch FAIL 1.73 seconds VerifyFixes PASS 0.12 seconds VerifySignedoff PASS 0.12 seconds GitLint PASS 0.64 seconds SubjectPrefix PASS 0.22 seconds BuildKernel PASS 30.39 seconds CheckAllWarning PASS 34.08 seconds CheckSparse PASS 36.41 seconds BuildKernel32 PASS 29.58 seconds CheckKernelLLVM PASS 30.77 seconds TestRunnerSetup PASS 696.22 seconds TestRunner_l2cap-tester FAIL 14.53 seconds TestRunner_iso-tester PASS 12.76 seconds TestRunner_bnep-tester PASS 3.18 seconds TestRunner_mgmt-tester PASS 87.85 seconds TestRunner_rfcomm-tester PASS 4.51 seconds TestRunner_sco-tester PASS 5.61 seconds TestRunner_ioctl-tester PASS 4.61 seconds TestRunner_mesh-tester PASS 4.50 seconds TestRunner_smp-tester PASS 4.37 seconds TestRunner_userchan-tester PASS 3.25 seconds TestRunner_6lowpan-tester PASS 4.41 seconds IncrementalBuild PASS 29.87 seconds
Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[v3,2/2] Bluetooth: L2CAP: Don't offer deferred channels before security is met
WARNING: Non-standard signature: Assisted-by:
#147:
ERROR: Unrecognized email address: 'OpenCode:claude-fable-5.2' #147:
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/15006063.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.124 seconds
https://github.com/bluez/bluetooth-next/pull/1170
---
Regards,
Linux Bluetooth