Skip to content

Commit 498444e

Browse files
committed
Merge tag 'for-net-2023-12-15' of git://git.kernel.org/pub/scm/linux/kernel/git/bluetooth/bluetooth
Luiz Augusto von Dentz says: ==================== bluetooth pull request for net: - Add encryption key size check when acting as peripheral - Shut up false-positive build warning - Send reject if L2CAP command request is corrupted - Fix Use-After-Free in bt_sock_recvmsg - Fix not notifying when connection encryption changes - Fix not checking if HCI_OP_INQUIRY has been sent - Fix address type send over to the MGMT interface - Fix deadlock in vhci_send_frame ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 8353c2a + 2e07e83 commit 498444e

File tree

7 files changed

+80
-29
lines changed

7 files changed

+80
-29
lines changed

drivers/bluetooth/hci_vhci.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/module.h>
1212
#include <asm/unaligned.h>
1313

14+
#include <linux/atomic.h>
1415
#include <linux/kernel.h>
1516
#include <linux/init.h>
1617
#include <linux/slab.h>
@@ -44,6 +45,7 @@ struct vhci_data {
4445
bool wakeup;
4546
__u16 msft_opcode;
4647
bool aosp_capable;
48+
atomic_t initialized;
4749
};
4850

4951
static int vhci_open_dev(struct hci_dev *hdev)
@@ -75,11 +77,10 @@ static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
7577

7678
memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
7779

78-
mutex_lock(&data->open_mutex);
7980
skb_queue_tail(&data->readq, skb);
80-
mutex_unlock(&data->open_mutex);
8181

82-
wake_up_interruptible(&data->read_wait);
82+
if (atomic_read(&data->initialized))
83+
wake_up_interruptible(&data->read_wait);
8384
return 0;
8485
}
8586

@@ -464,7 +465,8 @@ static int __vhci_create_device(struct vhci_data *data, __u8 opcode)
464465
skb_put_u8(skb, 0xff);
465466
skb_put_u8(skb, opcode);
466467
put_unaligned_le16(hdev->id, skb_put(skb, 2));
467-
skb_queue_tail(&data->readq, skb);
468+
skb_queue_head(&data->readq, skb);
469+
atomic_inc(&data->initialized);
468470

469471
wake_up_interruptible(&data->read_wait);
470472
return 0;

include/net/bluetooth/hci_core.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ struct blocked_key {
189189
struct smp_csrk {
190190
bdaddr_t bdaddr;
191191
u8 bdaddr_type;
192+
u8 link_type;
192193
u8 type;
193194
u8 val[16];
194195
};
@@ -198,6 +199,7 @@ struct smp_ltk {
198199
struct rcu_head rcu;
199200
bdaddr_t bdaddr;
200201
u8 bdaddr_type;
202+
u8 link_type;
201203
u8 authenticated;
202204
u8 type;
203205
u8 enc_size;
@@ -212,13 +214,16 @@ struct smp_irk {
212214
bdaddr_t rpa;
213215
bdaddr_t bdaddr;
214216
u8 addr_type;
217+
u8 link_type;
215218
u8 val[16];
216219
};
217220

218221
struct link_key {
219222
struct list_head list;
220223
struct rcu_head rcu;
221224
bdaddr_t bdaddr;
225+
u8 bdaddr_type;
226+
u8 link_type;
222227
u8 type;
223228
u8 val[HCI_LINK_KEY_SIZE];
224229
u8 pin_len;
@@ -1227,11 +1232,11 @@ static inline struct hci_conn *hci_conn_hash_lookup_cis(struct hci_dev *hdev,
12271232
continue;
12281233

12291234
/* Match CIG ID if set */
1230-
if (cig != BT_ISO_QOS_CIG_UNSET && cig != c->iso_qos.ucast.cig)
1235+
if (cig != c->iso_qos.ucast.cig)
12311236
continue;
12321237

12331238
/* Match CIS ID if set */
1234-
if (id != BT_ISO_QOS_CIS_UNSET && id != c->iso_qos.ucast.cis)
1239+
if (id != c->iso_qos.ucast.cis)
12351240
continue;
12361241

12371242
/* Match destination address if set */

net/bluetooth/af_bluetooth.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,14 @@ int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
309309
if (flags & MSG_OOB)
310310
return -EOPNOTSUPP;
311311

312+
lock_sock(sk);
313+
312314
skb = skb_recv_datagram(sk, flags, &err);
313315
if (!skb) {
314316
if (sk->sk_shutdown & RCV_SHUTDOWN)
315-
return 0;
317+
err = 0;
316318

319+
release_sock(sk);
317320
return err;
318321
}
319322

@@ -343,6 +346,8 @@ int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
343346

344347
skb_free_datagram(sk, skb);
345348

349+
release_sock(sk);
350+
346351
if (flags & MSG_TRUNC)
347352
copied = skblen;
348353

net/bluetooth/hci_event.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,9 @@ static u8 hci_cc_read_class_of_dev(struct hci_dev *hdev, void *data,
516516
{
517517
struct hci_rp_read_class_of_dev *rp = data;
518518

519+
if (WARN_ON(!hdev))
520+
return HCI_ERROR_UNSPECIFIED;
521+
519522
bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
520523

521524
if (rp->status)
@@ -747,9 +750,23 @@ static u8 hci_cc_read_enc_key_size(struct hci_dev *hdev, void *data,
747750
} else {
748751
conn->enc_key_size = rp->key_size;
749752
status = 0;
753+
754+
if (conn->enc_key_size < hdev->min_enc_key_size) {
755+
/* As slave role, the conn->state has been set to
756+
* BT_CONNECTED and l2cap conn req might not be received
757+
* yet, at this moment the l2cap layer almost does
758+
* nothing with the non-zero status.
759+
* So we also clear encrypt related bits, and then the
760+
* handler of l2cap conn req will get the right secure
761+
* state at a later time.
762+
*/
763+
status = HCI_ERROR_AUTH_FAILURE;
764+
clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
765+
clear_bit(HCI_CONN_AES_CCM, &conn->flags);
766+
}
750767
}
751768

752-
hci_encrypt_cfm(conn, 0);
769+
hci_encrypt_cfm(conn, status);
753770

754771
done:
755772
hci_dev_unlock(hdev);
@@ -820,8 +837,6 @@ static u8 hci_cc_write_auth_payload_timeout(struct hci_dev *hdev, void *data,
820837
if (!rp->status)
821838
conn->auth_payload_timeout = get_unaligned_le16(sent + 2);
822839

823-
hci_encrypt_cfm(conn, 0);
824-
825840
unlock:
826841
hci_dev_unlock(hdev);
827842

@@ -2304,7 +2319,8 @@ static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
23042319
return;
23052320
}
23062321

2307-
set_bit(HCI_INQUIRY, &hdev->flags);
2322+
if (hci_sent_cmd_data(hdev, HCI_OP_INQUIRY))
2323+
set_bit(HCI_INQUIRY, &hdev->flags);
23082324
}
23092325

23102326
static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
@@ -3683,12 +3699,8 @@ static void hci_encrypt_change_evt(struct hci_dev *hdev, void *data,
36833699
cp.handle = cpu_to_le16(conn->handle);
36843700
cp.timeout = cpu_to_le16(hdev->auth_payload_timeout);
36853701
if (hci_send_cmd(conn->hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO,
3686-
sizeof(cp), &cp)) {
3702+
sizeof(cp), &cp))
36873703
bt_dev_err(hdev, "write auth payload timeout failed");
3688-
goto notify;
3689-
}
3690-
3691-
goto unlock;
36923704
}
36933705

36943706
notify:

net/bluetooth/l2cap_core.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6492,6 +6492,14 @@ static inline void l2cap_le_sig_channel(struct l2cap_conn *conn,
64926492
kfree_skb(skb);
64936493
}
64946494

6495+
static inline void l2cap_sig_send_rej(struct l2cap_conn *conn, u16 ident)
6496+
{
6497+
struct l2cap_cmd_rej_unk rej;
6498+
6499+
rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
6500+
l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
6501+
}
6502+
64956503
static inline void l2cap_sig_channel(struct l2cap_conn *conn,
64966504
struct sk_buff *skb)
64976505
{
@@ -6517,23 +6525,24 @@ static inline void l2cap_sig_channel(struct l2cap_conn *conn,
65176525

65186526
if (len > skb->len || !cmd->ident) {
65196527
BT_DBG("corrupted command");
6528+
l2cap_sig_send_rej(conn, cmd->ident);
65206529
break;
65216530
}
65226531

65236532
err = l2cap_bredr_sig_cmd(conn, cmd, len, skb->data);
65246533
if (err) {
6525-
struct l2cap_cmd_rej_unk rej;
6526-
65276534
BT_ERR("Wrong link type (%d)", err);
6528-
6529-
rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
6530-
l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
6531-
sizeof(rej), &rej);
6535+
l2cap_sig_send_rej(conn, cmd->ident);
65326536
}
65336537

65346538
skb_pull(skb, len);
65356539
}
65366540

6541+
if (skb->len > 0) {
6542+
BT_DBG("corrupted command");
6543+
l2cap_sig_send_rej(conn, 0);
6544+
}
6545+
65376546
drop:
65386547
kfree_skb(skb);
65396548
}

net/bluetooth/mgmt.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2897,7 +2897,8 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
28972897
for (i = 0; i < key_count; i++) {
28982898
struct mgmt_link_key_info *key = &cp->keys[i];
28992899

2900-
if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
2900+
/* Considering SMP over BREDR/LE, there is no need to check addr_type */
2901+
if (key->type > 0x08)
29012902
return mgmt_cmd_status(sk, hdev->id,
29022903
MGMT_OP_LOAD_LINK_KEYS,
29032904
MGMT_STATUS_INVALID_PARAMS);
@@ -7130,6 +7131,7 @@ static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
71307131

71317132
for (i = 0; i < irk_count; i++) {
71327133
struct mgmt_irk_info *irk = &cp->irks[i];
7134+
u8 addr_type = le_addr_type(irk->addr.type);
71337135

71347136
if (hci_is_blocked_key(hdev,
71357137
HCI_BLOCKED_KEY_TYPE_IRK,
@@ -7139,8 +7141,12 @@ static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
71397141
continue;
71407142
}
71417143

7144+
/* When using SMP over BR/EDR, the addr type should be set to BREDR */
7145+
if (irk->addr.type == BDADDR_BREDR)
7146+
addr_type = BDADDR_BREDR;
7147+
71427148
hci_add_irk(hdev, &irk->addr.bdaddr,
7143-
le_addr_type(irk->addr.type), irk->val,
7149+
addr_type, irk->val,
71447150
BDADDR_ANY);
71457151
}
71467152

@@ -7221,6 +7227,7 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
72217227
for (i = 0; i < key_count; i++) {
72227228
struct mgmt_ltk_info *key = &cp->keys[i];
72237229
u8 type, authenticated;
7230+
u8 addr_type = le_addr_type(key->addr.type);
72247231

72257232
if (hci_is_blocked_key(hdev,
72267233
HCI_BLOCKED_KEY_TYPE_LTK,
@@ -7255,8 +7262,12 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
72557262
continue;
72567263
}
72577264

7265+
/* When using SMP over BR/EDR, the addr type should be set to BREDR */
7266+
if (key->addr.type == BDADDR_BREDR)
7267+
addr_type = BDADDR_BREDR;
7268+
72587269
hci_add_ltk(hdev, &key->addr.bdaddr,
7259-
le_addr_type(key->addr.type), type, authenticated,
7270+
addr_type, type, authenticated,
72607271
key->val, key->enc_size, key->ediv, key->rand);
72617272
}
72627273

@@ -9523,7 +9534,7 @@ void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
95239534

95249535
ev.store_hint = persistent;
95259536
bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
9526-
ev.key.addr.type = BDADDR_BREDR;
9537+
ev.key.addr.type = link_to_bdaddr(key->link_type, key->bdaddr_type);
95279538
ev.key.type = key->type;
95289539
memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
95299540
ev.key.pin_len = key->pin_len;
@@ -9574,7 +9585,7 @@ void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
95749585
ev.store_hint = persistent;
95759586

95769587
bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
9577-
ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
9588+
ev.key.addr.type = link_to_bdaddr(key->link_type, key->bdaddr_type);
95789589
ev.key.type = mgmt_ltk_type(key);
95799590
ev.key.enc_size = key->enc_size;
95809591
ev.key.ediv = key->ediv;
@@ -9603,7 +9614,7 @@ void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent)
96039614

96049615
bacpy(&ev.rpa, &irk->rpa);
96059616
bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
9606-
ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
9617+
ev.irk.addr.type = link_to_bdaddr(irk->link_type, irk->addr_type);
96079618
memcpy(ev.irk.val, irk->val, sizeof(irk->val));
96089619

96099620
mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
@@ -9632,7 +9643,7 @@ void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
96329643
ev.store_hint = persistent;
96339644

96349645
bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
9635-
ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
9646+
ev.key.addr.type = link_to_bdaddr(csrk->link_type, csrk->bdaddr_type);
96369647
ev.key.type = csrk->type;
96379648
memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
96389649

net/bluetooth/smp.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,7 @@ static void smp_notify_keys(struct l2cap_conn *conn)
10591059
}
10601060

10611061
if (smp->remote_irk) {
1062+
smp->remote_irk->link_type = hcon->type;
10621063
mgmt_new_irk(hdev, smp->remote_irk, persistent);
10631064

10641065
/* Now that user space can be considered to know the
@@ -1078,24 +1079,28 @@ static void smp_notify_keys(struct l2cap_conn *conn)
10781079
}
10791080

10801081
if (smp->csrk) {
1082+
smp->csrk->link_type = hcon->type;
10811083
smp->csrk->bdaddr_type = hcon->dst_type;
10821084
bacpy(&smp->csrk->bdaddr, &hcon->dst);
10831085
mgmt_new_csrk(hdev, smp->csrk, persistent);
10841086
}
10851087

10861088
if (smp->responder_csrk) {
1089+
smp->responder_csrk->link_type = hcon->type;
10871090
smp->responder_csrk->bdaddr_type = hcon->dst_type;
10881091
bacpy(&smp->responder_csrk->bdaddr, &hcon->dst);
10891092
mgmt_new_csrk(hdev, smp->responder_csrk, persistent);
10901093
}
10911094

10921095
if (smp->ltk) {
1096+
smp->ltk->link_type = hcon->type;
10931097
smp->ltk->bdaddr_type = hcon->dst_type;
10941098
bacpy(&smp->ltk->bdaddr, &hcon->dst);
10951099
mgmt_new_ltk(hdev, smp->ltk, persistent);
10961100
}
10971101

10981102
if (smp->responder_ltk) {
1103+
smp->responder_ltk->link_type = hcon->type;
10991104
smp->responder_ltk->bdaddr_type = hcon->dst_type;
11001105
bacpy(&smp->responder_ltk->bdaddr, &hcon->dst);
11011106
mgmt_new_ltk(hdev, smp->responder_ltk, persistent);
@@ -1115,6 +1120,8 @@ static void smp_notify_keys(struct l2cap_conn *conn)
11151120
key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
11161121
smp->link_key, type, 0, &persistent);
11171122
if (key) {
1123+
key->link_type = hcon->type;
1124+
key->bdaddr_type = hcon->dst_type;
11181125
mgmt_new_link_key(hdev, key, persistent);
11191126

11201127
/* Don't keep debug keys around if the relevant

0 commit comments

Comments
 (0)