Bluetooth: Fix missing NULL check for smp_chan_create() return value
Subsystems (2)
PATCH v2 1/6 Bluetooth: Fix missing NULL check for smp_chan_create() return value
From: Johan Hedberg <[email protected]>
The smp_chan_create function may return NULL, e.g. in the case of memory allocation failure, so we always need to check for this.
--- net/bluetooth/smp.c | 2 ++ 1 file changed, 2 insertions(+)
net/bluetooth/smp.c +2 -0
@@ -856,6 +856,8 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)return 0 ; smp =smp_chan_create (conn); +if (!smp) +return SMP_UNSPECIFIED ;skb_pull (skb,sizeof (*rp));
Signature · 1 line
1.9.3
PATCH v2 2/6 Bluetooth: Remove unnecessary SMP STK define
From: Johan Hedberg <[email protected]>
We never store the "master" type of STKs since we request encryption directly with them so we only need one STK type (the one that's looked-up on the slave side). Simply remove the unnecessary define and rename the _SLAVE one to the shorter form.
--- include/net/bluetooth/hci.h | 1 - net/bluetooth/hci_event.c | 2 +- net/bluetooth/smp.c | 6 +++++- 3 files changed, 6 insertions(+), 3 deletions(-)
include/net/bluetooth/hci.h +0 -1
@@ -349,7 +349,6 @@ enum {#define HCI_LK_AUTH_COMBINATION_P256 0x08/* The spec doesn't define types for SMP keys, the _MASTER suffix is implied */ #define HCI_SMP_STK 0x80 -#define HCI_SMP_STK_SLAVE 0x81#define HCI_SMP_LTK 0x82#define HCI_SMP_LTK_SLAVE 0x83
net/bluetooth/hci_event.c +1 -1
@@ -4241,7 +4241,7 @@ static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)* distribute the keys. Later, security can be re-established * using a distributed LTK. */ -if (ltk->type ==HCI_SMP_STK_SLAVE ) { +if (ltk->type ==HCI_SMP_STK ) {list_del (<k->list);kfree (ltk); }
net/bluetooth/smp.c +5 -1
@@ -561,8 +561,12 @@ static u8 smp_random(struct smp_chan *smp)else auth =0 ; +/* Even though there's no _SLAVE suffix this is the +* slave STK we're adding for later lookup (the master +* STK never needs to be stored). +*/ hci_add_ltk (hcon->hdev, &hcon->dst, hcon->dst_type, -HCI_SMP_STK_SLAVE , auth, stk, smp->enc_key_size, +HCI_SMP_STK , auth, stk, smp->enc_key_size, ediv, rand); }
Signature · 1 line
1.9.3
PATCH v2 3/6 Bluetooth: Remove HCI prefix from SMP LTK defines
In reply to [email protected]From: Johan Hedberg <[email protected]>
The LTK type has really nothing to do with HCI so it makes more sense to have these in smp.h than hci.h. This patch moves the defines to smp.h and removes the HCI_ prefix in the same go.
--- include/net/bluetooth/hci.h | 4 ---- net/bluetooth/hci_core.c | 2 +- net/bluetooth/hci_event.c | 3 ++- net/bluetooth/mgmt.c | 6 +++--- net/bluetooth/smp.c | 7 +++---- net/bluetooth/smp.h | 7 +++++++ 6 files changed, 16 insertions(+), 13 deletions(-)
include/net/bluetooth/hci.h +0 -4
@@ -347,10 +347,6 @@ enum {#define HCI_LK_CHANGED_COMBINATION 0x06#define HCI_LK_UNAUTH_COMBINATION_P256 0x07#define HCI_LK_AUTH_COMBINATION_P256 0x08 -/* The spec doesn't define types for SMP keys, the _MASTER suffix is implied */ -#define HCI_SMP_STK 0x80 -#define HCI_SMP_LTK 0x82 -#define HCI_SMP_LTK_SLAVE 0x83/* Long Term Key types */ #define HCI_LTK_UNAUTH 0x00
net/bluetooth/hci_core.c +1 -1
@@ -2974,7 +2974,7 @@ static bool hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn,static bool ltk_type_master (u8 type) { -if (type ==HCI_SMP_STK || type ==HCI_SMP_LTK ) +if (type ==SMP_STK || type ==SMP_LTK )return true;return false;
net/bluetooth/hci_event.c +2 -1
@@ -32,6 +32,7 @@#include "a2mp.h" #include "amp.h" +#include "smp.h" /* Handle HCI Event packets */
@@ -4241,7 +4242,7 @@ static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)* distribute the keys. Later, security can be re-established * using a distributed LTK. */ -if (ltk->type ==HCI_SMP_STK ) { +if (ltk->type ==SMP_STK ) {list_del (<k->list);kfree (ltk); }
net/bluetooth/mgmt.c +3 -3
@@ -4550,9 +4550,9 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev, addr_type =ADDR_LE_DEV_RANDOM ;if (key->master) - type =HCI_SMP_LTK ; + type =SMP_LTK ;else - type =HCI_SMP_LTK_SLAVE ; + type =SMP_LTK_SLAVE ;switch (key->type) {case MGMT_LTK_UNAUTHENTICATED :
@@ -5279,7 +5279,7 @@ void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent) ev.key.ediv = key->ediv; ev.key.rand = key->rand; -if (key->type ==HCI_SMP_LTK ) +if (key->type ==SMP_LTK ) ev.key.master =1 ;memcpy (ev.key.val, key->val,sizeof (key->val));
net/bluetooth/smp.c +3 -4
@@ -566,8 +566,7 @@ static u8 smp_random(struct smp_chan *smp)* STK never needs to be stored). */ hci_add_ltk (hcon->hdev, &hcon->dst, hcon->dst_type, -HCI_SMP_STK , auth, stk, smp->enc_key_size, - ediv, rand); +SMP_STK , auth, stk, smp->enc_key_size, ediv, rand); }return 0 ;
@@ -995,7 +994,7 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)hci_dev_lock (hdev); authenticated = (hcon->sec_level ==BT_SECURITY_HIGH ); - ltk =hci_add_ltk (hdev, &hcon->dst, hcon->dst_type,HCI_SMP_LTK , + ltk =hci_add_ltk (hdev, &hcon->dst, hcon->dst_type,SMP_LTK , authenticated, smp->tk, smp->enc_key_size, rp->ediv, rp->rand); smp->ltk = ltk;
@@ -1311,7 +1310,7 @@ int smp_distribute_keys(struct l2cap_conn *conn) authenticated = hcon->sec_level ==BT_SECURITY_HIGH ; ltk =hci_add_ltk (hdev, &hcon->dst, hcon->dst_type, -HCI_SMP_LTK_SLAVE , authenticated, enc.ltk, +SMP_LTK_SLAVE , authenticated, enc.ltk, smp->enc_key_size, ediv, rand); smp->slave_ltk = ltk;
net/bluetooth/smp.h +7 -0
@@ -116,6 +116,13 @@ struct smp_cmd_security_req {#define SMP_MIN_ENC_KEY_SIZE 7#define SMP_MAX_ENC_KEY_SIZE 16 +/* LTK types used in internal storage (struct smp_ltk) */ +enum { +SMP_STK , +SMP_LTK , +SMP_LTK_SLAVE , +}; +/* SMP Commands */ bool smp_sufficient_security (struct hci_conn *hcon,u8 sec_level);int smp_conn_security (struct hci_conn *hcon,__u8 sec_level);
Signature · 1 line
1.9.3
PATCH v2 4/6 Bluetooth: Remove unused LTK authentication defines
In reply to [email protected]From: Johan Hedberg <[email protected]>
These defines were probably put in to track authenticated vs unauthenticated LTKs, however since the LTK struct has a separate boolean authenticated member these were never used.
--- include/net/bluetooth/hci.h | 4 ---- 1 file changed, 4 deletions(-)
include/net/bluetooth/hci.h +0 -4
@@ -348,10 +348,6 @@ enum {#define HCI_LK_UNAUTH_COMBINATION_P256 0x07#define HCI_LK_AUTH_COMBINATION_P256 0x08 -/* Long Term Key types */ -#define HCI_LTK_UNAUTH 0x00 -#define HCI_LTK_AUTH 0x01 -/* ---- HCI Error Codes ---- */ #define HCI_ERROR_AUTH_FAILURE 0x05#define HCI_ERROR_MEMORY_EXCEEDED 0x07
Signature · 1 line
1.9.3
PATCH v2 5/6 Bluetooth: Convert SMP flags into an enum
In reply to [email protected]From: Johan Hedberg <[email protected]>
There's no reason to have explicit values for these flags. Convert them to an enum to be consistent with other similar flags.
--- net/bluetooth/smp.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
net/bluetooth/smp.c +7 -5
@@ -35,11 +35,13 @@#define AUTH_REQ_MASK 0x07 -#define SMP_FLAG_TK_VALID 1 -#define SMP_FLAG_CFM_PENDING 2 -#define SMP_FLAG_MITM_AUTH 3 -#define SMP_FLAG_COMPLETE 4 -#define SMP_FLAG_INITIATOR 5 +enum { +SMP_FLAG_TK_VALID , +SMP_FLAG_CFM_PENDING , +SMP_FLAG_MITM_AUTH , +SMP_FLAG_COMPLETE , +SMP_FLAG_INITIATOR , +};struct smp_chan {struct l2cap_conn *conn;
Signature · 1 line
1.9.3
PATCH v2 6/6 Bluetooth: Add flexible buffer byte order swapping function
In reply to [email protected]From: Johan Hedberg <[email protected]>
Since the SMP code needs to swap ordering of variable length buffers add a convenience function that can be used for any length buffer.
--- net/bluetooth/smp.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-)
net/bluetooth/smp.c +7 -13
@@ -64,18 +64,12 @@ struct smp_chan {unsigned long flags; }; -static inline void swap128 (const u8 src[16 ],u8 dst[16 ]) +static inline void swap_buf (const u8 *src,u8 *dst,size_t len) { -int i; -for (i =0 ; i <16 ; i++) - dst[15 - i] = src[i]; -} +size_t i; -static inline void swap56 (const u8 src[7 ],u8 dst[7 ]) -{ -int i; -for (i =0 ; i <7 ; i++) - dst[6 - i] = src[i]; +for (i =0 ; i < len; i++) + dst[len -1 - i] = src[i]; }static int smp_e (struct crypto_blkcipher *tfm,const u8 *k,u8 *r)
@@ -94,7 +88,7 @@ static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) desc.flags =0 ;/* The most significant octet of key corresponds to k[0] */ -swap128 (k, tmp); +swap_buf (k, tmp,16 ); err =crypto_blkcipher_setkey (tfm, tmp,16 );if (err) {
@@ -103,7 +97,7 @@ static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) }/* Most significant octet of plaintextData corresponds to data[0] */ -swap128 (r, data); +swap_buf (r, data,16 );sg_init_one (&sg, data,16 );
@@ -112,7 +106,7 @@ static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)BT_ERR ("Encrypt data error %d" , err);/* Most significant octet of encryptedData corresponds to data[0] */ -swap128 (data, r); +swap_buf (data, r,16 );return err; }
Signature · 1 line
1.9.3