Skip to content

Commit 693cd8c

Browse files
holtmanntorvalds
authored andcommitted
Bluetooth: Fix regression with minimum encryption key size alignment
When trying to align the minimum encryption key size requirement for Bluetooth connections, it turns out doing this in a central location in the HCI connection handling code is not possible. Original Bluetooth version up to 2.0 used a security model where the L2CAP service would enforce authentication and encryption. Starting with Bluetooth 2.1 and Secure Simple Pairing that model has changed into that the connection initiator is responsible for providing an encrypted ACL link before any L2CAP communication can happen. Now connecting Bluetooth 2.1 or later devices with Bluetooth 2.0 and before devices are causing a regression. The encryption key size check needs to be moved out of the HCI connection handling into the L2CAP channel setup. To achieve this, the current check inside hci_conn_security() has been moved into l2cap_check_enc_key_size() helper function and then called from four decisions point inside L2CAP to cover all combinations of Secure Simple Pairing enabled devices and device using legacy pairing and legacy service security model. Fixes: d5bb334 ("Bluetooth: Align minimum encryption key size for LE and BR/EDR connections") Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=203643 Signed-off-by: Marcel Holtmann <[email protected]> Cc: [email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent c356dc4 commit 693cd8c

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

net/bluetooth/hci_conn.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,14 +1276,6 @@ int hci_conn_check_link_mode(struct hci_conn *conn)
12761276
!test_bit(HCI_CONN_ENCRYPT, &conn->flags))
12771277
return 0;
12781278

1279-
/* The minimum encryption key size needs to be enforced by the
1280-
* host stack before establishing any L2CAP connections. The
1281-
* specification in theory allows a minimum of 1, but to align
1282-
* BR/EDR and LE transports, a minimum of 7 is chosen.
1283-
*/
1284-
if (conn->enc_key_size < HCI_MIN_ENC_KEY_SIZE)
1285-
return 0;
1286-
12871279
return 1;
12881280
}
12891281

@@ -1400,8 +1392,16 @@ int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
14001392
return 0;
14011393

14021394
encrypt:
1403-
if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
1395+
if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) {
1396+
/* Ensure that the encryption key size has been read,
1397+
* otherwise stall the upper layer responses.
1398+
*/
1399+
if (!conn->enc_key_size)
1400+
return 0;
1401+
1402+
/* Nothing else needed, all requirements are met */
14041403
return 1;
1404+
}
14051405

14061406
hci_conn_encrypt(conn);
14071407
return 0;

net/bluetooth/l2cap_core.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,21 @@ static void l2cap_request_info(struct l2cap_conn *conn)
13411341
sizeof(req), &req);
13421342
}
13431343

1344+
static bool l2cap_check_enc_key_size(struct hci_conn *hcon)
1345+
{
1346+
/* The minimum encryption key size needs to be enforced by the
1347+
* host stack before establishing any L2CAP connections. The
1348+
* specification in theory allows a minimum of 1, but to align
1349+
* BR/EDR and LE transports, a minimum of 7 is chosen.
1350+
*
1351+
* This check might also be called for unencrypted connections
1352+
* that have no key size requirements. Ensure that the link is
1353+
* actually encrypted before enforcing a key size.
1354+
*/
1355+
return (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags) ||
1356+
hcon->enc_key_size > HCI_MIN_ENC_KEY_SIZE);
1357+
}
1358+
13441359
static void l2cap_do_start(struct l2cap_chan *chan)
13451360
{
13461361
struct l2cap_conn *conn = chan->conn;
@@ -1358,9 +1373,14 @@ static void l2cap_do_start(struct l2cap_chan *chan)
13581373
if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
13591374
return;
13601375

1361-
if (l2cap_chan_check_security(chan, true) &&
1362-
__l2cap_no_conn_pending(chan))
1376+
if (!l2cap_chan_check_security(chan, true) ||
1377+
!__l2cap_no_conn_pending(chan))
1378+
return;
1379+
1380+
if (l2cap_check_enc_key_size(conn->hcon))
13631381
l2cap_start_connection(chan);
1382+
else
1383+
__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
13641384
}
13651385

13661386
static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
@@ -1439,7 +1459,10 @@ static void l2cap_conn_start(struct l2cap_conn *conn)
14391459
continue;
14401460
}
14411461

1442-
l2cap_start_connection(chan);
1462+
if (l2cap_check_enc_key_size(conn->hcon))
1463+
l2cap_start_connection(chan);
1464+
else
1465+
l2cap_chan_close(chan, ECONNREFUSED);
14431466

14441467
} else if (chan->state == BT_CONNECT2) {
14451468
struct l2cap_conn_rsp rsp;
@@ -7490,7 +7513,7 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
74907513
}
74917514

74927515
if (chan->state == BT_CONNECT) {
7493-
if (!status)
7516+
if (!status && l2cap_check_enc_key_size(hcon))
74947517
l2cap_start_connection(chan);
74957518
else
74967519
__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
@@ -7499,7 +7522,7 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
74997522
struct l2cap_conn_rsp rsp;
75007523
__u16 res, stat;
75017524

7502-
if (!status) {
7525+
if (!status && l2cap_check_enc_key_size(hcon)) {
75037526
if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
75047527
res = L2CAP_CR_PEND;
75057528
stat = L2CAP_CS_AUTHOR_PEND;

0 commit comments

Comments
 (0)