Bluetooth: SMP fixes/improvements
Subsystems (2)
v4 cover Bluetooth: SMP fixes/improvements
Hi,
This v4 adds has the following changes to previous revisions:
* Add KEY_DIST_MASK define to avoid hard-coded 0x07 values
* Update the last patch to use defines instead of helper functions
* Update the last patch to use a "drop:" label to simplify the if-statment used for dropping data.
Johan
----------------------------------------------------------------
Johan Hedberg (8):
Bluetooth: Remove unnecessary checks after canceling SMP security timer
Bluetooth: Don't take any action in smp_resume_cb if not encrypted
Bluetooth: Move identity address update behind a workqueue
Bluetooth: Remove unnecessary deferred work for SMP key distribution
Bluetooth: Fix locking of the SMP context
Bluetooth: Add define for key distribution mask
Bluetooth: Fix calling smp_distribute_keys() when still waiting for keys
Bluetooth: Add strict checks for allowed SMP PDUs
include/net/bluetooth/hci_core.h | 1 - include/net/bluetooth/l2cap.h | 3 +- net/bluetooth/l2cap_core.c | 10 +- net/bluetooth/smp.c | 225 +++++++++++++++++++++-------------- net/bluetooth/smp.h | 2 + 5 files changed, 147 insertions(+), 94 deletions(-)
PATCH v4 1/8 Bluetooth: Remove unnecessary checks after canceling SMP security timer
From: Johan Hedberg <[email protected]>
The SMP security timer used to be able to modify the SMP context state but now days it simply calls hci_disconnect(). It is therefore unnecessary to have extra sanity checks for the SMP context after canceling the timer.
--- net/bluetooth/smp.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
net/bluetooth/smp.c +1 -5
@@ -282,8 +282,7 @@ static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) smp = chan->data;cancel_delayed_work_sync (&smp->security_timer); -if (test_bit (HCI_CONN_LE_SMP_PEND , &conn->hcon->flags)) -schedule_delayed_work (&smp->security_timer,SMP_TIMEOUT ); +schedule_delayed_work (&smp->security_timer,SMP_TIMEOUT ); }static __u8 authreq_to_seclevel (__u8 authreq)
@@ -375,9 +374,6 @@ static void smp_chan_destroy(struct l2cap_conn *conn)BUG_ON (!smp);cancel_delayed_work_sync (&smp->security_timer); -/* In case the timeout freed the SMP context */ -if (!chan->data) -return ;if (work_pending (&smp->distribute_work)) {cancel_work_sync (&smp->distribute_work);
Signature · 1 line
1.9.3
PATCH v4 2/8 Bluetooth: Don't take any action in smp_resume_cb if not encrypted
In reply to [email protected]From: Johan Hedberg <[email protected]>
When smp_resume_cb is called if we're not encrypted (i.e. the callback wasn't called because the connection became encrypted) we shouldn't take any action at all. This patch moves also the security_timer cancellation behind this condition.
--- net/bluetooth/smp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
net/bluetooth/smp.c +4 -2
@@ -1533,10 +1533,12 @@ static void smp_resume_cb(struct l2cap_chan *chan)if (!smp)return ; +if (!test_bit (HCI_CONN_ENCRYPT , &hcon->flags)) +return ; +cancel_delayed_work (&smp->security_timer); -if (test_bit (HCI_CONN_ENCRYPT , &hcon->flags)) -queue_work (hdev->workqueue, &smp->distribute_work); +queue_work (hdev->workqueue, &smp->distribute_work); }static void smp_ready_cb (struct l2cap_chan *chan)
Signature · 1 line
1.9.3
PATCH v4 3/8 Bluetooth: Move identity address update behind a workqueue
In reply to [email protected]From: Johan Hedberg <[email protected]>
The identity address update of all channels for an l2cap_conn needs to take the lock for each channel, i.e. it's safest to do this by a separate workqueue callback.
Previously this was partially solved by moving the entire SMP key distribution behind a workqueue. However, if we want SMP context locking to be correct and safe we should always use the l2cap_chan lock when accessing it, meaning even smp_distribute_keys needs to take that lock which would once again create a dead lock when updating the identity address.
The simplest way to solve this is to have l2cap_conn manage the deferred work which is what this patch does. A subsequent patch will remove the now unnecessary SMP key distribution work struct.
--- include/net/bluetooth/l2cap.h | 3 ++- net/bluetooth/l2cap_core.c | 10 ++++++++-- net/bluetooth/smp.c | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-)
include/net/bluetooth/l2cap.h +2 -1
@@ -633,6 +633,8 @@ struct l2cap_conn {struct sk_buff_head pending_rx;struct work_struct pending_rx_work; +struct work_struct id_addr_update_work; +__u8 disc_reason;struct l2cap_chan *smp;
@@ -937,7 +939,6 @@ int l2cap_ertm_init(struct l2cap_chan *chan);void l2cap_chan_add (struct l2cap_conn *conn,struct l2cap_chan *chan);void __l2cap_chan_add (struct l2cap_conn *conn,struct l2cap_chan *chan);void l2cap_chan_del (struct l2cap_chan *chan,int err); -void l2cap_conn_update_id_addr (struct hci_conn *hcon);void l2cap_send_conn_req (struct l2cap_chan *chan);void l2cap_move_start (struct l2cap_chan *chan); void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan,
net/bluetooth/l2cap_core.c +8 -2
@@ -631,9 +631,11 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) }EXPORT_SYMBOL_GPL (l2cap_chan_del); -void l2cap_conn_update_id_addr (struct hci_conn *hcon) +static void l2cap_conn_update_id_addr (struct work_struct *work) { -struct l2cap_conn *conn = hcon->l2cap_data; + struct l2cap_conn *conn = container_of(work, struct l2cap_conn, + id_addr_update_work); +struct hci_conn *hcon = conn->hcon;struct l2cap_chan *chan;mutex_lock (&conn->chan_lock);
@@ -1635,6 +1637,9 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err)if (work_pending (&conn->pending_rx_work))cancel_work_sync (&conn->pending_rx_work); +if (work_pending (&conn->id_addr_update_work)) +cancel_work_sync (&conn->id_addr_update_work); +l2cap_unregister_all_users (conn);/* Force the connection to be immediately dropped */
@@ -6927,6 +6932,7 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon)skb_queue_head_init (&conn->pending_rx);INIT_WORK (&conn->pending_rx_work, process_pending_rx); +INIT_WORK (&conn->id_addr_update_work, l2cap_conn_update_id_addr); conn->disc_reason =HCI_ERROR_REMOTE_USER_TERM ;
net/bluetooth/smp.c +1 -1
@@ -654,7 +654,7 @@ static void smp_notify_keys(struct l2cap_conn *conn)*/ bacpy (&hcon->dst, &smp->remote_irk->bdaddr); hcon->dst_type = smp->remote_irk->addr_type; -l2cap_conn_update_id_addr (hcon); +queue_work (hdev->workqueue, &conn->id_addr_update_work);/* When receiving an indentity resolving key for * a remote device that does not use a resolvable
Signature · 1 line
1.9.3
PATCH v4 4/8 Bluetooth: Remove unnecessary deferred work for SMP key distribution
In reply to [email protected]From: Johan Hedberg <[email protected]>
Now that the identity address update happens through its own deferred work there's no need to have smp_distribute_keys anymore behind a second deferred work. This patch removes this extra construction and makes the code do direct calls to smp_distribute_keys() again.
--- net/bluetooth/smp.c | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-)
net/bluetooth/smp.c +5 -16
@@ -46,7 +46,6 @@ enum {struct smp_chan {struct l2cap_conn *conn;struct delayed_work security_timer; -struct work_struct distribute_work;u8 preq[7 ];/* SMP Pairing Request */ u8 prsp[7 ];/* SMP Pairing Response */
@@ -375,12 +374,6 @@ static void smp_chan_destroy(struct l2cap_conn *conn)cancel_delayed_work_sync (&smp->security_timer); -if (work_pending (&smp->distribute_work)) { -cancel_work_sync (&smp->distribute_work); -if (!chan->data) -return ; - } - complete =test_bit (SMP_FLAG_COMPLETE , &smp->flags);mgmt_smp_complete (conn->hcon, complete);
@@ -703,10 +696,8 @@ static void smp_notify_keys(struct l2cap_conn *conn) } } -static void smp_distribute_keys (struct work_struct *work) +static void smp_distribute_keys (struct smp_chan *smp) { - struct smp_chan *smp = container_of(work, struct smp_chan, - distribute_work);struct smp_cmd_pairing *req, *rsp;struct l2cap_conn *conn = smp->conn;struct hci_conn *hcon = conn->hcon;
@@ -850,7 +841,6 @@ static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) smp->conn = conn; chan->data = smp; -INIT_WORK (&smp->distribute_work, smp_distribute_keys);INIT_DELAYED_WORK (&smp->security_timer, smp_timeout);hci_conn_hold (conn->hcon);
@@ -1290,7 +1280,7 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) rp->ediv, rp->rand); smp->ltk = ltk;if (!(smp->remote_key_dist &SMP_DIST_ID_KEY )) -queue_work (hdev->workqueue, &smp->distribute_work); +smp_distribute_keys (smp);hci_dev_unlock (hdev);return 0 ;
@@ -1368,7 +1358,7 @@ static int smp_cmd_ident_addr_info(struct l2cap_conn *conn, smp->id_addr_type, smp->irk, &rpa); distribute: -queue_work (hdev->workqueue, &smp->distribute_work); +smp_distribute_keys (smp);hci_dev_unlock (hcon->hdev);
@@ -1404,7 +1394,7 @@ static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)memcpy (csrk->val, rp->csrk,sizeof (csrk->val)); } smp->csrk = csrk; -queue_work (hdev->workqueue, &smp->distribute_work); +smp_distribute_keys (smp);hci_dev_unlock (hdev);return 0 ;
@@ -1526,7 +1516,6 @@ static void smp_resume_cb(struct l2cap_chan *chan)struct smp_chan *smp = chan->data;struct l2cap_conn *conn = chan->conn;struct hci_conn *hcon = conn->hcon; -struct hci_dev *hdev = hcon->hdev;BT_DBG ("chan %p" , chan);
@@ -1538,7 +1527,7 @@ static void smp_resume_cb(struct l2cap_chan *chan)cancel_delayed_work (&smp->security_timer); -queue_work (hdev->workqueue, &smp->distribute_work); +smp_distribute_keys (smp); }static void smp_ready_cb (struct l2cap_chan *chan)
Signature · 1 line
1.9.3
PATCH v4 5/8 Bluetooth: Fix locking of the SMP context
In reply to [email protected]From: Johan Hedberg <[email protected]>
Before the move the l2cap_chan the SMP context (smp_chan) didn't have any kind of proper locking. The best there existed was the HCI_CONN_LE_SMP_PEND flag which was used to enable mutual exclusion for potential multiple creators of the SMP context.
Now that SMP has been converted to use the l2cap_chan infrastructure and since the SMP context is directly mapped to a corresponding l2cap_chan we get the SMP context locking essentially for free through the l2cap_chan lock. For all callbacks that l2cap_core.c makes for each channel implementation (smp.c in the case of SMP) the l2cap_chan lock is held through l2cap_chan_lock(chan).
Since the calls from l2cap_core.c to smp.c are covered the only missing piece to have the locking implemented properly is to ensure that the lock is held for any other call path that may access the SMP context. This means user responses through mgmt.c, requests to elevate the security of a connection through hci_conn.c, as well as any deferred work through workqueues.
This patch adds the necessary locking to all these other code paths that try to access the SMP context. Since mutual exclusion for the l2cap_chan access is now covered from all directions the patch also removes unnecessary HCI_CONN_LE_SMP_PEND flag (once we've acquired the chan lock we can simply check whether chan->smp is set to know if there's an SMP context).
--- include/net/bluetooth/hci_core.h | 1 - net/bluetooth/smp.c | 76 +++++++++++++++++++++++----------------- 2 files changed, 44 insertions(+), 33 deletions(-)
include/net/bluetooth/hci_core.h +0 -1
@@ -539,7 +539,6 @@ enum {HCI_CONN_RSWITCH_PEND ,HCI_CONN_MODE_CHANGE_PEND ,HCI_CONN_SCO_SETUP_PEND , -HCI_CONN_LE_SMP_PEND ,HCI_CONN_MGMT_CONNECTED ,HCI_CONN_SSP_ENABLED , HCI_CONN_SC_ENABLED,
net/bluetooth/smp.c +44 -32
209 lines not on this page · raw message
Signature · 1 line
1.9.3
PATCH v4 6/8 Bluetooth: Add define for key distribution mask
In reply to [email protected]From: Johan Hedberg <[email protected]>
This patch adds a define for the allowed bits of the key distribution mask so we don't have to have magic 0x07 constants throughout the code.
--- net/bluetooth/smp.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
net/bluetooth/smp.c +3 -2
@@ -34,6 +34,7 @@#define SMP_TIMEOUT msecs_to_jiffies(30000)#define AUTH_REQ_MASK 0x07 +#define KEY_DIST_MASK 0x07enum {SMP_FLAG_TK_VALID ,
@@ -703,7 +704,7 @@ static void smp_distribute_keys(struct smp_chan *smp) rsp = (void *) &smp->prsp[1 ];/* The responder sends its keys first */ -if (hcon->out && (smp->remote_key_dist &0x07 )) +if (hcon->out && (smp->remote_key_dist &KEY_DIST_MASK ))return ; req = (void *) &smp->preq[1 ];
@@ -789,7 +790,7 @@ static void smp_distribute_keys(struct smp_chan *smp) }/* If there are still keys to be received wait for them */ -if ((smp->remote_key_dist &0x07 )) +if (smp->remote_key_dist &KEY_DIST_MASK )return ;set_bit (SMP_FLAG_COMPLETE , &smp->flags);
Signature · 1 line
1.9.3
PATCH v4 7/8 Bluetooth: Fix calling smp_distribute_keys() when still waiting for keys
In reply to [email protected]From: Johan Hedberg <[email protected]>
When we're in the process of receiving keys in phase 3 of SMP we keep track of which keys are still expected in the smp->remote_key_dist variable. If we still have some key bits set we need to continue waiting for more PDUs and not needlessly call smp_distribute_keys(). This patch fixes two such cases in the smp_cmd_master_ident() and smp_cmd_ident_addr_info() handler functions.
--- net/bluetooth/smp.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
net/bluetooth/smp.c +3 -2
@@ -1293,7 +1293,7 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) authenticated, smp->tk, smp->enc_key_size, rp->ediv, rp->rand); smp->ltk = ltk; -if (!(smp->remote_key_dist &SMP_DIST_ID_KEY )) +if (!(smp->remote_key_dist &KEY_DIST_MASK ))smp_distribute_keys (smp);hci_dev_unlock (hdev);
@@ -1371,7 +1371,8 @@ static int smp_cmd_ident_addr_info(struct l2cap_conn *conn, smp->id_addr_type, smp->irk, &rpa); distribute: -smp_distribute_keys (smp); +if (!(smp->remote_key_dist &KEY_DIST_MASK )) +smp_distribute_keys (smp);hci_dev_unlock (hcon->hdev);
Signature · 1 line
1.9.3
PATCH v4 8/8 Bluetooth: Add strict checks for allowed SMP PDUs
In reply to [email protected]From: Johan Hedberg <[email protected]>
SMP defines quite clearly when certain PDUs are to be expected/allowed and when not, but doesn't have any explicit request/response definition. So far the code has relied on each PDU handler to behave correctly if receiving PDUs at an unexpected moment, however this requires many different checks and is prone to errors.
This patch introduces a generic way to keep track of allowed PDUs and thereby reduces the responsibility & load on individual command handlers. The tracking is implemented using a simple bit-mask where each opcode maps to its own bit. If the bit is set the corresponding PDU is allow and if the bit is not set the PDU is not allowed.
As a simple example, when we send the Pairing Request we'd set the bit for Pairing Response, and when we receive the Pairing Response we'd clear the bit for Pairing Response.
Since the disallowed PDU rejection is now done in a single central place we need to be a bit careful of which action makes most sense to all cases. Previously some, such as Security Request, have been simply ignored whereas others have caused an explicit disconnect.
The only PDU rejection action that keeps good interoperability and can be used for all the applicable use cases is to drop the data. This may raise some concerns of us now being more lenient for misbehaving (and potentially malicious) devices, but the policy of simply dropping data has been a successful one for many years e.g. in L2CAP (where this is the *only* policy for such cases - we never request disconnection in l2cap_core.c because of bad data). Furthermore, we cannot prevent connected devices from creating the SMP context (through a Security or Pairing Request), and once the context exists looking up the corresponding bit for the received opcode and deciding to reject it is essentially an equally lightweight operation as the kind of rejection that l2cap_core.c already successfully does.
--- net/bluetooth/smp.c | 120 +++++++++++++++++++++++++++++++++++----------------- net/bluetooth/smp.h | 2 + 2 files changed, 84 insertions(+), 38 deletions(-)
net/bluetooth/smp.c +82 -38
@@ -31,6 +31,9 @@#include "smp.h" +#define SMP_ALLOW_CMD (smp, code) set_bit(code, &smp->allow_cmd) +#define SMP_DISALLOW_CMD (smp, code) clear_bit(code, &smp->allow_cmd) +#define SMP_TIMEOUT msecs_to_jiffies(30000)#define AUTH_REQ_MASK 0x07
@@ -47,6 +50,7 @@ enum {struct smp_chan {struct l2cap_conn *conn;struct delayed_work security_timer; +unsigned long allow_cmd;/* Bitmask of allowed commands */ u8 preq[7 ];/* SMP Pairing Request */ u8 prsp[7 ];/* SMP Pairing Response */
@@ -553,6 +557,11 @@ static u8 smp_confirm(struct smp_chan *smp)smp_send_cmd (smp->conn,SMP_CMD_PAIRING_CONFIRM ,sizeof (cp), &cp); +if (conn->hcon->out) +SMP_ALLOW_CMD (smp,SMP_CMD_PAIRING_CONFIRM ); +else +SMP_ALLOW_CMD (smp,SMP_CMD_PAIRING_RANDOM ); +return 0 ; }
@@ -691,6 +700,20 @@ static void smp_notify_keys(struct l2cap_conn *conn) } } +static void smp_allow_key_dist (struct smp_chan *smp) +{ +/* Allow the first expected phase 3 PDU. The rest of the PDUs +* will be allowed in each PDU handler to ensure we receive +* them in the correct order. +*/ +if (smp->remote_key_dist &SMP_DIST_ENC_KEY ) +SMP_ALLOW_CMD (smp,SMP_CMD_ENCRYPT_INFO ); +else if (smp->remote_key_dist &SMP_DIST_ID_KEY ) +SMP_ALLOW_CMD (smp,SMP_CMD_IDENT_INFO ); +else if (smp->remote_key_dist &SMP_DIST_SIGN ) +SMP_ALLOW_CMD (smp,SMP_CMD_SIGN_INFO ); +} +static void smp_distribute_keys (struct smp_chan *smp) {struct smp_cmd_pairing *req, *rsp;
@@ -704,8 +727,10 @@ static void smp_distribute_keys(struct smp_chan *smp) rsp = (void *) &smp->prsp[1 ];/* The responder sends its keys first */ -if (hcon->out && (smp->remote_key_dist &KEY_DIST_MASK )) +if (hcon->out && (smp->remote_key_dist &KEY_DIST_MASK )) { +smp_allow_key_dist (smp);return ; + } req = (void *) &smp->preq[1 ];
@@ -790,8 +815,10 @@ static void smp_distribute_keys(struct smp_chan *smp) }/* If there are still keys to be received wait for them */ -if (smp->remote_key_dist &KEY_DIST_MASK ) +if (smp->remote_key_dist &KEY_DIST_MASK ) { +smp_allow_key_dist (smp);return ; + }set_bit (SMP_FLAG_COMPLETE , &smp->flags);smp_notify_keys (conn);
@@ -829,6 +856,8 @@ static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) smp->conn = conn; chan->data = smp; +SMP_ALLOW_CMD (smp,SMP_CMD_PAIRING_FAIL ); +INIT_DELAYED_WORK (&smp->security_timer, smp_timeout);hci_conn_hold (conn->hcon);
@@ -925,6 +954,8 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) (req->auth_req & SMP_AUTH_BONDING))return SMP_PAIRING_NOTSUPP ; +SMP_DISALLOW_CMD (smp,SMP_CMD_PAIRING_REQ ); + smp->preq[0 ] =SMP_CMD_PAIRING_REQ ;memcpy (&smp->preq[1 ], req,sizeof (*req));skb_pull (skb,sizeof (*req));
@@ -958,6 +989,7 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)memcpy (&smp->prsp[1 ], &rsp,sizeof (rsp));smp_send_cmd (conn,SMP_CMD_PAIRING_RSP ,sizeof (rsp), &rsp); +SMP_ALLOW_CMD (smp,SMP_CMD_PAIRING_CONFIRM );/* Request setup of TK */ ret =tk_request (conn,0 , auth, rsp.io_capability, req->io_capability);
@@ -983,6 +1015,8 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)if (conn->hcon->role !=HCI_ROLE_MASTER )return SMP_CMD_NOTSUPP ; +SMP_DISALLOW_CMD (smp,SMP_CMD_PAIRING_RSP ); +skb_pull (skb,sizeof (*rsp)); req = (void *) &smp->preq[1 ];
@@ -1040,13 +1074,19 @@ static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)if (skb->len <sizeof (smp->pcnf))return SMP_INVALID_PARAMS ; +SMP_DISALLOW_CMD (smp,SMP_CMD_PAIRING_CONFIRM ); +memcpy (smp->pcnf, skb->data,sizeof (smp->pcnf));skb_pull (skb,sizeof (smp->pcnf)); -if (conn->hcon->out) +if (conn->hcon->out) {smp_send_cmd (conn,SMP_CMD_PAIRING_RANDOM ,sizeof (smp->prnd), smp->prnd); -else if (test_bit (SMP_FLAG_TK_VALID , &smp->flags)) +SMP_ALLOW_CMD (smp,SMP_CMD_PAIRING_RANDOM ); +return 0 ; + } + +if (test_bit (SMP_FLAG_TK_VALID , &smp->flags))return smp_confirm (smp);else set_bit (SMP_FLAG_CFM_PENDING , &smp->flags);
@@ -1064,6 +1104,8 @@ static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)if (skb->len <sizeof (smp->rrnd))return SMP_INVALID_PARAMS ; +SMP_DISALLOW_CMD (smp,SMP_CMD_PAIRING_RANDOM ); +memcpy (smp->rrnd, skb->data,sizeof (smp->rrnd));skb_pull (skb,sizeof (smp->rrnd));
@@ -1122,7 +1164,6 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)struct smp_cmd_security_req *rp = (void *) skb->data;struct smp_cmd_pairing cp;struct hci_conn *hcon = conn->hcon; -struct l2cap_chan *chan = conn->smp;struct smp_chan *smp;u8 sec_level;
@@ -1144,10 +1185,6 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)if (smp_ltk_encrypt (conn, hcon->pending_sec_level))return 0 ; -/* If SMP is already in progress ignore this request */ -if (chan->data) -return 0 ; - smp =smp_chan_create (conn);if (!smp)return SMP_UNSPECIFIED ;
@@ -1165,6 +1202,7 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)memcpy (&smp->preq[1 ], &cp,sizeof (cp));smp_send_cmd (conn,SMP_CMD_PAIRING_REQ ,sizeof (cp), &cp); +SMP_ALLOW_CMD (smp,SMP_CMD_PAIRING_RSP );return 0 ; }
@@ -1227,10 +1265,12 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)memcpy (&smp->preq[1 ], &cp,sizeof (cp));smp_send_cmd (conn,SMP_CMD_PAIRING_REQ ,sizeof (cp), &cp); +SMP_ALLOW_CMD (smp,SMP_CMD_PAIRING_RSP ); }else {struct smp_cmd_security_req cp; cp.auth_req = authreq;smp_send_cmd (conn,SMP_CMD_SECURITY_REQ ,sizeof (cp), &cp); +SMP_ALLOW_CMD (smp,SMP_CMD_PAIRING_REQ ); }set_bit (SMP_FLAG_INITIATOR , &smp->flags);
@@ -1252,9 +1292,8 @@ static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)if (skb->len <sizeof (*rp))return SMP_INVALID_PARAMS ; -/* Ignore this PDU if it wasn't requested */ -if (!(smp->remote_key_dist &SMP_DIST_ENC_KEY )) -return 0 ; +SMP_DISALLOW_CMD (smp,SMP_CMD_ENCRYPT_INFO ); +SMP_ALLOW_CMD (smp,SMP_CMD_MASTER_IDENT );skb_pull (skb,sizeof (*rp));
@@ -1278,13 +1317,13 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)if (skb->len <sizeof (*rp))return SMP_INVALID_PARAMS ; -/* Ignore this PDU if it wasn't requested */ -if (!(smp->remote_key_dist &SMP_DIST_ENC_KEY )) -return 0 ; -/* Mark the information as received */ smp->remote_key_dist &= ~SMP_DIST_ENC_KEY ; +SMP_DISALLOW_CMD (smp,SMP_CMD_MASTER_IDENT ); +if (smp->remote_key_dist &SMP_DIST_ID_KEY ) +SMP_ALLOW_CMD (smp,SMP_CMD_IDENT_INFO ); +skb_pull (skb,sizeof (*rp));hci_dev_lock (hdev);
@@ -1311,9 +1350,8 @@ static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)if (skb->len <sizeof (*info))return SMP_INVALID_PARAMS ; -/* Ignore this PDU if it wasn't requested */ -if (!(smp->remote_key_dist &SMP_DIST_ID_KEY )) -return 0 ; +SMP_DISALLOW_CMD (smp,SMP_CMD_IDENT_INFO ); +SMP_ALLOW_CMD (smp,SMP_CMD_IDENT_ADDR_INFO );skb_pull (skb,sizeof (*info));
@@ -1336,13 +1374,13 @@ static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,if (skb->len <sizeof (*info))return SMP_INVALID_PARAMS ; -/* Ignore this PDU if it wasn't requested */ -if (!(smp->remote_key_dist &SMP_DIST_ID_KEY )) -return 0 ; -/* Mark the information as received */ smp->remote_key_dist &= ~SMP_DIST_ID_KEY ; +SMP_DISALLOW_CMD (smp,SMP_CMD_IDENT_ADDR_INFO ); +if (smp->remote_key_dist &SMP_DIST_SIGN ) +SMP_ALLOW_CMD (smp,SMP_CMD_SIGN_INFO ); +skb_pull (skb,sizeof (*info));hci_dev_lock (hcon->hdev);
@@ -1392,13 +1430,11 @@ static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)if (skb->len <sizeof (*rp))return SMP_INVALID_PARAMS ; -/* Ignore this PDU if it wasn't requested */ -if (!(smp->remote_key_dist &SMP_DIST_SIGN )) -return 0 ; -/* Mark the information as received */ smp->remote_key_dist &= ~SMP_DIST_SIGN ; +SMP_DISALLOW_CMD (smp,SMP_CMD_SIGN_INFO ); +skb_pull (skb,sizeof (*rp));hci_dev_lock (hdev);
@@ -1418,6 +1454,7 @@ static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb) {struct l2cap_conn *conn = chan->conn;struct hci_conn *hcon = conn->hcon; +struct smp_chan *smp;__u8 code, reason;int err =0 ;
@@ -1437,18 +1474,19 @@ static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb) code = skb->data[0 ];skb_pull (skb,sizeof (code)); -/* -* The SMP context must be initialized for all other PDUs except -* pairing and security requests. If we get any other PDU when -* not initialized simply disconnect (done if this function -* returns an error). + smp = chan->data; + +if (code >SMP_CMD_MAX ) + goto drop; + +if (smp && !test_bit (code, &smp->allow_cmd)) + goto drop; + +/* If we don't have a context the only allowed commands are +* pairing request and security request. */ -if (code !=SMP_CMD_PAIRING_REQ && code !=SMP_CMD_SECURITY_REQ && - !chan->data) { -BT_ERR ("Unexpected SMP command 0x%02x. Disconnecting." , code); - err = -EOPNOTSUPP ; - goto done; - } +if (!smp && code !=SMP_CMD_PAIRING_REQ && code !=SMP_CMD_SECURITY_REQ ) + goto drop;switch (code) {case SMP_CMD_PAIRING_REQ :
@@ -1510,6 +1548,12 @@ done: }return err; + +drop: +BT_ERR ("%s unexpected SMP command 0x%02x from %pMR" , hcon->hdev->name, + code, &hcon->dst); +kfree_skb (skb); +return 0 ; }static void smp_teardown_cb (struct l2cap_chan *chan,int err)
net/bluetooth/smp.h +2 -0
@@ -102,6 +102,8 @@ struct smp_cmd_security_req {__u8 auth_req; } __packed; +#define SMP_CMD_MAX 0x0b +#define SMP_PASSKEY_ENTRY_FAILED 0x01#define SMP_OOB_NOT_AVAIL 0x02#define SMP_AUTH_REQUIREMENTS 0x03
Signature · 1 line
1.9.3
Marcel Holtmann
maintainerHi Johan,
Quoted 20 lines
This v4 adds has the following changes to previous revisions:
* Add
KEY_DIST_MASKdefine to avoid hard-coded0x07values
* Update the last patch to use defines instead of helper functions
* Update the last patch to use a "drop:" label to simplify the if-statment used for dropping data.Johan
----------------------------------------------------------------
Johan Hedberg (8):
Bluetooth: Remove unnecessary checks after canceling SMP security timer
Bluetooth: Don't take any action insmp_resume_cbif not encrypted
Bluetooth: Move identity address update behind a workqueue
Bluetooth: Remove unnecessary deferred work for SMP key distribution
Bluetooth: Fix locking of the SMP context
Bluetooth: Add define for key distribution mask
Bluetooth: Fix callingsmp_distribute_keys()when still waiting for keys
Bluetooth: Add strict checks for allowed SMP PDUs
Quoted 6 lines
include/net/bluetooth/hci_core.h | 1 - include/net/bluetooth/l2cap.h | 3 +- net/bluetooth/l2cap_core.c | 10 +- net/bluetooth/smp.c | 225 +++++++++++++++++++++-------------- net/bluetooth/smp.h | 2 + 5 files changed, 147 insertions(+), 94 deletions(-)
all 8 patches have been applied to bluetooth-next tree.
Regards
Marcel