Skip to contentSkip to conversation

Bluetooth: Fix SMP support for encryption key refresh

PATCH Superseded Johan Hedberg 3 participants 10 messages Jun 7, 2012 – Jun 9, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Bluetooth: Fix SMP support for encryption key refresh

Hi,

These two patches are necessary for elevating the security level from medium (unauthenticated) to high (authenticated), aka. encryption key refresh.

Johan

#1 lore raw

PATCH 1/2 Bluetooth: Fix SMP security elevation from medium to high

From: Johan Hedberg <[email protected]>

If we have an unauthenticated key it is not sufficient to acheive high security. Therefore, when deciding whether to encrypt the link or request pairing, it is essential to in addition to checking the existence of a key to also check whether it is authenticated or not.

---
 net/bluetooth/smp.c |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
net/bluetooth/smp.c +6 -3
index 1885697..16ef0dc 100644
@@ -704,7 +704,7 @@ static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
 	return 0;
 }
 
-static u8 smp_ltk_encrypt(struct l2cap_conn *conn)
+static u8 smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
 {
 	struct smp_ltk *key;
 	struct hci_conn *hcon = conn->hcon;
@@ -713,6 +713,9 @@ static u8 smp_ltk_encrypt(struct l2cap_conn *conn)
 	if (!key)
 		return 0;
 
+	if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated)
+		return 0;
+
 	if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
 		return 1;
 
@@ -733,7 +736,7 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
 
 	hcon->pending_sec_level = authreq_to_seclevel(rp->auth_req);
 
-	if (smp_ltk_encrypt(conn))
+	if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
 		return 0;
 
 	if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
@@ -772,7 +775,7 @@ int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level)
 		return 1;
 
 	if (hcon->link_mode & HCI_LM_MASTER)
-		if (smp_ltk_encrypt(conn))
+		if (smp_ltk_encrypt(conn, sec_level))
 			goto done;
 
 	if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
Signature · 1 line
1.7.10
#2 lore raw

Re: [PATCH 1/2] Bluetooth: Fix SMP security elevation from medium to high

Hi Johan,

Quoted 11 lines · Johan Hedberg

From: Johan Hedberg <[email protected]>

If we have an unauthenticated key it is not sufficient to acheive high security. Therefore, when deciding whether to encrypt the link or request pairing, it is essential to in addition to checking the existence of a key to also check whether it is authenticated or not.

---
 net/bluetooth/smp.c |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

Patch has been applied to bluetooth.git. Thanks.

Gustavo

#3 lore raw

PATCH 2/2 Bluetooth: Add support for encryption key refresh

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

With LE/SMP the completion of a security level elavation from medium to high is indicated by a HCI Encryption Key Refresh complete event. The necessary behavior upon receiving this event is a mix of what's done for auth_complete and encryption_change, which is also where most of the event handling code has been copied from.

---
 include/net/bluetooth/hci.h |    6 ++++++
 net/bluetooth/hci_event.c   |   48 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)
include/net/bluetooth/hci.h +6 -0
index 3f5d682..2a6b0b8 100644
@@ -1195,6 +1195,12 @@ struct extended_inquiry_info {
 	__u8     data[240];
 } __packed;
 
+#define HCI_EV_KEY_REFRESH_COMPLETE	0x30
+struct hci_ev_key_refresh_complete {
+	__u8	status;
+	__le16	handle;
+} __packed;
+
 #define HCI_EV_IO_CAPA_REQUEST		0x31
 struct hci_ev_io_capa_request {
 	bdaddr_t bdaddr;
net/bluetooth/hci_event.c +48 -0

60 lines not on this page · raw message

Signature · 1 line
1.7.10
#4 lore raw

Re: [PATCH 2/2] Bluetooth: Add support for encryption key refresh

Hi Johan,

Quoted 9 lines · Johan Hedberg

From: Johan Hedberg <[email protected]>

With LE/SMP the completion of a security level elavation from medium to high is indicated by a HCI Encryption Key Refresh complete event. The necessary behavior upon receiving this event is a mix of what's done for auth_complete and encryption_change, which is also where most of the event handling code has been copied from.

...

Quoted 26 lines
net/bluetooth/hci_event.c +26 -0
+static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
+					 struct sk_buff *skb)
+{
+	struct hci_ev_key_refresh_complete *ev = (void *) skb->data;
+	struct hci_conn *conn;
+
+	BT_DBG("%s status %u handle %u", hdev->name, ev->status,
+	       __le16_to_cpu(ev->handle));
+
+	hci_dev_lock(hdev);
+
+	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
+	if (!conn)
+		goto unlock;
+
+	if (!ev->status)
+		conn->sec_level = conn->pending_sec_level;
+
+	clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
+
+	if (ev->status && conn->state == BT_CONNECTED) {
+		hci_acl_disconn(conn, HCI_ERROR_AUTH_FAILURE);
+		hci_conn_put(conn);
+		goto unlock;
+	}
+

net/bluetooth/hci_event.c static void hci_extended_inquiry_result_evt(struct hci_dev *hdev, · line 3078

Quoted 10 lines
net/bluetooth/hci_event.c +10 -0
+	if (conn->state == BT_CONFIG) {
+		if (!ev->status)
+			conn->state = BT_CONNECTED;
+
+		hci_proto_connect_cfm(conn, ev->status);
+		hci_conn_put(conn);
+	} else {
+		hci_auth_cfm(conn, ev->status);
+
+		hci_conn_hold(conn);

If you want to keep extra hold(conn) you may just not put. Is this typo?

net/bluetooth/hci_event.c static void hci_extended_inquiry_result_evt(struct hci_dev *hdev, · line 3081

Quoted 3 lines
net/bluetooth/hci_event.c +3 -0
+		conn->disc_timeout = HCI_DISCONN_TIMEOUT;
+		hci_conn_put(conn);
+	}

Best regards
Andrei Emeltchenko

#5 lore raw

Re: [PATCH 2/2] Bluetooth: Add support for encryption key refresh

Hi Andrei,

Quoted 13 lines · Andrei Emeltchenko

net/bluetooth/hci_event.c static void hci_extended_inquiry_result_evt(struct hci_dev *hdev, · line 3078

Quoted 10 lines
net/bluetooth/hci_event.c +10 -0
+	if (conn->state == BT_CONFIG) {
+		if (!ev->status)
+			conn->state = BT_CONNECTED;
+
+		hci_proto_connect_cfm(conn, ev->status);
+		hci_conn_put(conn);
+	} else {
+		hci_auth_cfm(conn, ev->status);
+
+		hci_conn_hold(conn);

If you want to keep extra hold(conn) you may just not put. Is this typo?

Quoted 3 lines

net/bluetooth/hci_event.c static void hci_extended_inquiry_result_evt(struct hci_dev *hdev, · line 3081

Quoted 3 lines
net/bluetooth/hci_event.c +3 -0
+		conn->disc_timeout = HCI_DISCONN_TIMEOUT;
+		hci_conn_put(conn);
+	}

This is just copy-paste from hci_auth_complete_evt(). I think the intention of the hold+put is to restart or remove the disconnect timer with the new disc_timeout value or something similar. If the code above needs fixing then so does the code in hci_auth_complete_evt().

Johan

#6 lore raw

Re: [PATCH 2/2] Bluetooth: Add support for encryption key refresh

Hi Johan,

Quoted 14 lines · Johan Hedberg
Quoted 13 lines · Andrei Emeltchenko

net/bluetooth/hci_event.c static void hci_extended_inquiry_result_evt(struct hci_dev *hdev, · line 3078

Quoted 10 lines
net/bluetooth/hci_event.c +10 -0
+	if (conn->state == BT_CONFIG) {
+		if (!ev->status)
+			conn->state = BT_CONNECTED;
+
+		hci_proto_connect_cfm(conn, ev->status);
+		hci_conn_put(conn);
+	} else {
+		hci_auth_cfm(conn, ev->status);
+
+		hci_conn_hold(conn);

If you want to keep extra hold(conn) you may just not put. Is this typo?

Quoted 8 lines
Quoted 3 lines

net/bluetooth/hci_event.c static void hci_extended_inquiry_result_evt(struct hci_dev *hdev, · line 3081

Quoted 3 lines
net/bluetooth/hci_event.c +3 -0
+		conn->disc_timeout = HCI_DISCONN_TIMEOUT;
+		hci_conn_put(conn);
+	}

This is just copy-paste from hci_auth_complete_evt(). I think the intention of the hold+put is to restart or remove the disconnect timer with the new disc_timeout value or something similar. If the code above needs fixing then so does the code in hci_auth_complete_evt().

OK. Got it. Seems that cancel_delayed_work(&conn->disc_work); is enough here.

Best regards
Andrei Emeltchenko

#7 lore raw

Re: [PATCH 2/2] Bluetooth: Add support for encryption key refresh

In reply to Johan Hedberg

Hi Johan,

Quoted 8 lines · Johan Hedberg

From: Johan Hedberg <[email protected]>

With LE/SMP the completion of a security level elavation from medium to high is indicated by a HCI Encryption Key Refresh complete event. The necessary behavior upon receiving this event is a mix of what's done for auth_complete and encryption_change, which is also where most of the event handling code has been copied from.

Quoted 5 lines
---
 include/net/bluetooth/hci.h |    6 ++++++
 net/bluetooth/hci_event.c   |   48 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)

This patch look ok to me, but if we want to get it into bluetooth.git we need a rebased version here.

Gustavo

#8 lore raw

PATCH v2 1/1 Bluetooth: Add support for encryption key refresh

From: Johan Hedberg <[email protected]>

With LE/SMP the completion of a security level elavation from medium to high is indicated by a HCI Encryption Key Refresh Complete event. The necessary behavior upon receiving this event is a mix of what's done for auth_complete and encryption_change, which is also where most of the event handling code has been copied from.

---
v2: Rebased on top of latest bluetooth.git

 include/net/bluetooth/hci.h |    6 ++++++
 net/bluetooth/hci_event.c   |   48 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)
include/net/bluetooth/hci.h +6 -0
index 66a7b57..3def64b 100644
@@ -1144,6 +1144,12 @@ struct extended_inquiry_info {
 	__u8     data[240];
 } __packed;
 
+#define HCI_EV_KEY_REFRESH_COMPLETE	0x30
+struct hci_ev_key_refresh_complete {
+	__u8	status;
+	__le16	handle;
+} __packed;
+
 #define HCI_EV_IO_CAPA_REQUEST		0x31
 struct hci_ev_io_capa_request {
 	bdaddr_t bdaddr;
net/bluetooth/hci_event.c +48 -0

60 lines not on this page · raw message

Signature · 1 line
1.7.10
#9 lore raw

Re: [PATCH v2] Bluetooth: Add support for encryption key refresh

Hi Johan,

Quoted 8 lines · Johan Hedberg

From: Johan Hedberg <[email protected]>

With LE/SMP the completion of a security level elavation from medium to high is indicated by a HCI Encryption Key Refresh Complete event. The necessary behavior upon receiving this event is a mix of what's done for auth_complete and encryption_change, which is also where most of the event handling code has been copied from.

Quoted 7 lines

---
v2: Rebased on top of latest bluetooth.git

 include/net/bluetooth/hci.h |    6 ++++++
 net/bluetooth/hci_event.c   |   48 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)

Patch has been applied to bluetooth.git. Thanks.

Gustavo