Skip to contentSkip to conversation

Bluetooth: Simplify logic when checking SMP_FLAG_TK_VALID

PATCH In review [email protected] 2 participants 6 messages Mar 19, 2014

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

PATCH v2 1/3 Bluetooth: Simplify logic when checking SMP_FLAG_TK_VALID

From: Johan Hedberg <[email protected]>

This is a trivial coding style simplification by instead of having an extra early return to instead revert the if condition and do the single needed queue_work() call there.

---
 net/bluetooth/smp.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
net/bluetooth/smp.c +2 -4
index a0150033e797..ed1c9547ba6e 100644
@@ -761,10 +761,8 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
 	set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags);
 
 	/* Can't compose response until we have been confirmed */
-	if (!test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags))
-		return 0;
-
-	queue_work(hdev->workqueue, &smp->confirm);
+	if (test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags))
+		queue_work(hdev->workqueue, &smp->confirm);
 
 	return 0;
 }
Signature · 1 line
1.8.5.3
#1 lore raw

PATCH v2 2/3 Bluetooth: Increase SMP re-encryption delay to 500ms

From: Johan Hedberg <[email protected]>

In some cases the current 250ms delay is not enough for the remote to receive the keys, as can be witnessed by the following log:

Quoted 1 line

ACL Data RX: Handle 64 flags 0x02 dlen 21 [hci1] 231.414217

      SMP: Signing Information (0x0a) len 16
        Signature key: 555bb66b7ab3abc9d5c287c97fe6eb29
< ACL Data TX: Handle 64 flags 0x00 dlen 21               [hci1] 231.414414
      SMP: Encryption Information (0x06) len 16
        Long term key: 2a7cdc233c9a4b1f3ed31dd9843fea29
< ACL Data TX: Handle 64 flags 0x00 dlen 15               [hci1] 231.414466
      SMP: Master Identification (0x07) len 10
        EDIV: 0xeccc
        Rand: 0x322e0ef50bd9308a
< ACL Data TX: Handle 64 flags 0x00 dlen 21               [hci1] 231.414505
      SMP: Signing Information (0x0a) len 16
        Signature key: bbda1b2076e2325aa66fbcdd5388f745
Quoted 1 line

HCI Event: Number of Completed Packets (0x13) plen 5 [hci1] 231.483130

Num handles: 1
Handle: 64
Count: 2
< HCI Command: LE Start Encryption (0x08|0x0019) plen 28 [hci1] 231.664211

        Handle: 64
        Random number: 0x5052ad2b75fed54b
        Encrypted diversifier: 0xb7c2
        Long term key: a336ede66711b49a84bde9b41426692e
Quoted 1 line

HCI Event: Command Status (0x0f) plen 4 [hci1] 231.666937

      LE Start Encryption (0x08|0x0019) ncmd 1
        Status: Success (0x00)
Quoted 1 line

HCI Event: Number of Completed Packets (0x13) plen 5 [hci1] 231.712646

Num handles: 1
Handle: 64
Count: 1

Quoted 1 line

HCI Event: Disconnect Complete (0x05) plen 4 [hci1] 232.562587

        Status: Success (0x00)
        Handle: 64
        Reason: Remote User Terminated Connection (0x13)

As can be seen, the last key (Signing Information) is sent at 231.414505 but the completed packets event for it comes only at 231.712646, i.e. roughly 298ms later.

To have a better margin of error this patch increases the delay to 500ms.

---
v2: Update the right timeout

 net/bluetooth/smp.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/bluetooth/smp.h +1 -1
index f223b9d38b61..b6913471815a 100644
@@ -121,7 +121,7 @@ struct smp_cmd_security_req {
 #define SMP_FLAG_LTK_ENCRYPT	4
 #define SMP_FLAG_COMPLETE	5
 
-#define SMP_REENCRYPT_TIMEOUT	msecs_to_jiffies(250)
+#define SMP_REENCRYPT_TIMEOUT	msecs_to_jiffies(500)
 
 struct smp_chan {
 	struct l2cap_conn *conn;
Signature · 1 line
1.8.5.3
maintainer
#2 lore raw

Re: [PATCH v2 2/3] Bluetooth: Increase SMP re-encryption delay to 500ms

Hi Johan,

Quoted 44 lines

In some cases the current 250ms delay is not enough for the remote to receive the keys, as can be witnessed by the following log:

Quoted 1 line

ACL Data RX: Handle 64 flags 0x02 dlen 21 [hci1] 231.414217

     SMP: Signing Information (0x0a) len 16
       Signature key: 555bb66b7ab3abc9d5c287c97fe6eb29
< ACL Data TX: Handle 64 flags 0x00 dlen 21               [hci1] 231.414414
     SMP: Encryption Information (0x06) len 16
       Long term key: 2a7cdc233c9a4b1f3ed31dd9843fea29
< ACL Data TX: Handle 64 flags 0x00 dlen 15               [hci1] 231.414466
     SMP: Master Identification (0x07) len 10
       EDIV: 0xeccc
       Rand: 0x322e0ef50bd9308a
< ACL Data TX: Handle 64 flags 0x00 dlen 21               [hci1] 231.414505
     SMP: Signing Information (0x0a) len 16
       Signature key: bbda1b2076e2325aa66fbcdd5388f745
Quoted 1 line

HCI Event: Number of Completed Packets (0x13) plen 5 [hci1] 231.483130

Num handles: 1
Handle: 64
Count: 2
< HCI Command: LE Start Encryption (0x08|0x0019) plen 28 [hci1] 231.664211

       Handle: 64
       Random number: 0x5052ad2b75fed54b
       Encrypted diversifier: 0xb7c2
       Long term key: a336ede66711b49a84bde9b41426692e
Quoted 1 line

HCI Event: Command Status (0x0f) plen 4 [hci1] 231.666937

     LE Start Encryption (0x08|0x0019) ncmd 1
       Status: Success (0x00)
Quoted 1 line

HCI Event: Number of Completed Packets (0x13) plen 5 [hci1] 231.712646

Num handles: 1
Handle: 64
Count: 1

Quoted 1 line

HCI Event: Disconnect Complete (0x05) plen 4 [hci1] 232.562587

       Status: Success (0x00)
       Handle: 64
       Reason: Remote User Terminated Connection (0x13)

As can be seen, the last key (Signing Information) is sent at 231.414505 but the completed packets event for it comes only at 231.712646, i.e. roughly 298ms later.

To have a better margin of error this patch increases the delay to 500ms.

Quoted 6 lines

---
v2: Update the right timeout

net/bluetooth/smp.h | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)

patch has been applied to bluetooth-next tree.

Regards

Marcel

#3 lore raw

PATCH v2 3/3 Bluetooth: Fix SMP user passkey notification mgmt event

In reply to [email protected]

From: Johan Hedberg <[email protected]>

When performing SMP pairing with MITM protection one side needs to enter the passkey while the other side displays to the user what needs to be entered. Nowhere in the SMP specification does it say that the displaying side needs to any kind of confirmation of the passkey, even though a code comment in smp.c implies this.

This patch removes the misleading comment and converts the code to use the passkey notification mgmt event instead of the passkey confirmation mgmt event.

---
 net/bluetooth/smp.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
net/bluetooth/smp.c +3 -2
index ed1c9547ba6e..2a7ee7f6cd8b 100644
@@ -407,13 +407,14 @@ static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
 			method = REQ_PASSKEY;
 	}
 
-	/* Generate random passkey. Not valid until confirmed. */
+	/* Generate random passkey. */
 	if (method == CFM_PASSKEY) {
 		memset(smp->tk, 0, sizeof(smp->tk));
 		get_random_bytes(&passkey, sizeof(passkey));
 		passkey %= 1000000;
 		put_unaligned_le32(passkey, smp->tk);
 		BT_DBG("PassKey: %d", passkey);
+		set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags);
 	}
 
 	hci_dev_lock(hcon->hdev);
@@ -422,7 +423,7 @@ static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
 		ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
 						hcon->type, hcon->dst_type);
 	else
-		ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
+		ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
 						hcon->type, hcon->dst_type,
 						cpu_to_le32(passkey), 0);
 
Signature · 1 line
1.8.5.3
maintainer
#4 lore raw

Re: [PATCH v2 3/3] Bluetooth: Fix SMP user passkey notification mgmt event

Hi Johan,

Quoted 6 lines

When performing SMP pairing with MITM protection one side needs to enter the passkey while the other side displays to the user what needs to be entered. Nowhere in the SMP specification does it say that the displaying side needs to any kind of confirmation of the passkey, even though a code comment in smp.c implies this.

Quoted 8 lines

This patch removes the misleading comment and converts the code to use the passkey notification mgmt event instead of the passkey confirmation mgmt event.

---
net/bluetooth/smp.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)

patch has been applied to bluetooth-next tree.

Regards

Marcel

maintainer
#5 lore raw
In reply to [email protected]

Hi Johan,

Quoted 8 lines

This is a trivial coding style simplification by instead of having an extra early return to instead revert the if condition and do the single needed queue_work() call there.

---
net/bluetooth/smp.c | 6 ++----

1 file changed, 2 insertions(+), 4 deletions(-)

patch has been applied to bluetooth-next tree.

Regards

Marcel