Bluetooth: Fix properly ignoring unexpected SMP PDUs
[cascardo/linux.git] / net / bluetooth / smp.c
index 4500736..5867c1c 100644 (file)
@@ -78,6 +78,52 @@ static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
        return err;
 }
 
+static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
+{
+       u8 _res[16], k[16];
+       int err;
+
+       /* r' = padding || r */
+       memset(_res, 0, 13);
+       _res[13] = r[2];
+       _res[14] = r[1];
+       _res[15] = r[0];
+
+       swap128(irk, k);
+       err = smp_e(tfm, k, _res);
+       if (err) {
+               BT_ERR("Encrypt error");
+               return err;
+       }
+
+       /* The output of the random address function ah is:
+        *      ah(h, r) = e(k, r') mod 2^24
+        * The output of the security function e is then truncated to 24 bits
+        * by taking the least significant 24 bits of the output of e as the
+        * result of ah.
+        */
+       res[0] = _res[15];
+       res[1] = _res[14];
+       res[2] = _res[13];
+
+       return 0;
+}
+
+bool smp_irk_matches(struct crypto_blkcipher *tfm, u8 irk[16],
+                    bdaddr_t *bdaddr)
+{
+       u8 hash[3];
+       int err;
+
+       BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
+
+       err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
+       if (err)
+               return false;
+
+       return !memcmp(bdaddr->b, hash, 3);
+}
+
 static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16],
                  u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia,
                  u8 _rat, bdaddr_t *ra, u8 res[16])
@@ -203,31 +249,42 @@ static void build_pairing_cmd(struct l2cap_conn *conn,
                              struct smp_cmd_pairing *req,
                              struct smp_cmd_pairing *rsp, __u8 authreq)
 {
-       u8 dist_keys = 0;
+       struct smp_chan *smp = conn->smp_chan;
+       struct hci_conn *hcon = conn->hcon;
+       struct hci_dev *hdev = hcon->hdev;
+       u8 local_dist = 0, remote_dist = 0;
 
        if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->dev_flags)) {
-               dist_keys = SMP_DIST_ENC_KEY;
+               local_dist = SMP_DIST_ENC_KEY;
+               remote_dist = SMP_DIST_ENC_KEY;
                authreq |= SMP_AUTH_BONDING;
        } else {
                authreq &= ~SMP_AUTH_BONDING;
        }
 
+       if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
+               remote_dist |= SMP_DIST_ID_KEY;
+
        if (rsp == NULL) {
                req->io_capability = conn->hcon->io_capability;
                req->oob_flag = SMP_OOB_NOT_PRESENT;
                req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
-               req->init_key_dist = 0;
-               req->resp_key_dist = dist_keys;
+               req->init_key_dist = local_dist;
+               req->resp_key_dist = remote_dist;
                req->auth_req = (authreq & AUTH_REQ_MASK);
+
+               smp->remote_key_dist = remote_dist;
                return;
        }
 
        rsp->io_capability = conn->hcon->io_capability;
        rsp->oob_flag = SMP_OOB_NOT_PRESENT;
        rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
-       rsp->init_key_dist = 0;
-       rsp->resp_key_dist = req->resp_key_dist & dist_keys;
+       rsp->init_key_dist = req->init_key_dist & remote_dist;
+       rsp->resp_key_dist = req->resp_key_dist & local_dist;
        rsp->auth_req = (authreq & AUTH_REQ_MASK);
+
+       smp->remote_key_dist = rsp->init_key_dist;
 }
 
 static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
@@ -565,6 +622,9 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
 
        BT_DBG("conn %p", conn);
 
+       if (skb->len < sizeof(*req))
+               return SMP_UNSPECIFIED;
+
        if (conn->hcon->link_mode & HCI_LM_MASTER)
                return SMP_CMD_NOTSUPP;
 
@@ -617,6 +677,9 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
 
        BT_DBG("conn %p", conn);
 
+       if (skb->len < sizeof(*rsp))
+               return SMP_UNSPECIFIED;
+
        if (!(conn->hcon->link_mode & HCI_LM_MASTER))
                return SMP_CMD_NOTSUPP;
 
@@ -661,6 +724,9 @@ static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
 
        BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
 
+       if (skb->len < sizeof(smp->pcnf))
+               return SMP_UNSPECIFIED;
+
        memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
        skb_pull(skb, sizeof(smp->pcnf));
 
@@ -686,6 +752,9 @@ static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
 
        BT_DBG("conn %p", conn);
 
+       if (skb->len < sizeof(smp->rrnd))
+               return SMP_UNSPECIFIED;
+
        swap128(skb->data, smp->rrnd);
        skb_pull(skb, sizeof(smp->rrnd));
 
@@ -699,7 +768,8 @@ static u8 smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
        struct smp_ltk *key;
        struct hci_conn *hcon = conn->hcon;
 
-       key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type);
+       key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type,
+                                  hcon->out);
        if (!key)
                return 0;
 
@@ -724,6 +794,9 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
 
        BT_DBG("conn %p", conn);
 
+       if (skb->len < sizeof(*rp))
+               return SMP_UNSPECIFIED;
+
        if (!(conn->hcon->link_mode & HCI_LM_MASTER))
                return SMP_CMD_NOTSUPP;
 
@@ -813,6 +886,15 @@ static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
        struct smp_cmd_encrypt_info *rp = (void *) skb->data;
        struct smp_chan *smp = conn->smp_chan;
 
+       BT_DBG("conn %p", conn);
+
+       if (skb->len < sizeof(*rp))
+               return SMP_UNSPECIFIED;
+
+       /* Ignore this PDU if it wasn't requested */
+       if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY))
+               return 0;
+
        skb_pull(skb, sizeof(*rp));
 
        memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
@@ -828,6 +910,15 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
        struct hci_conn *hcon = conn->hcon;
        u8 authenticated;
 
+       BT_DBG("conn %p", conn);
+
+       if (skb->len < sizeof(*rp))
+               return SMP_UNSPECIFIED;
+
+       /* Ignore this PDU if it wasn't requested */
+       if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY))
+               return 0;
+
        skb_pull(skb, sizeof(*rp));
 
        hci_dev_lock(hdev);
@@ -835,12 +926,69 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
        hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, HCI_SMP_LTK, 1,
                    authenticated, smp->tk, smp->enc_key_size,
                    rp->ediv, rp->rand);
-       smp_distribute_keys(conn, 1);
+       if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
+               smp_distribute_keys(conn, 1);
        hci_dev_unlock(hdev);
 
        return 0;
 }
 
+static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
+{
+       struct smp_cmd_ident_info *info = (void *) skb->data;
+       struct smp_chan *smp = conn->smp_chan;
+
+       BT_DBG("");
+
+       if (skb->len < sizeof(*info))
+               return SMP_UNSPECIFIED;
+
+       /* Ignore this PDU if it wasn't requested */
+       if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
+               return 0;
+
+       skb_pull(skb, sizeof(*info));
+
+       memcpy(smp->irk, info->irk, 16);
+
+       return 0;
+}
+
+static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
+                                  struct sk_buff *skb)
+{
+       struct smp_cmd_ident_addr_info *info = (void *) skb->data;
+       struct smp_chan *smp = conn->smp_chan;
+       struct hci_conn *hcon = conn->hcon;
+       bdaddr_t rpa;
+
+       BT_DBG("");
+
+       if (skb->len < sizeof(*info))
+               return SMP_UNSPECIFIED;
+
+       /* Ignore this PDU if it wasn't requested */
+       if (!(smp->remote_key_dist & SMP_DIST_ID_KEY))
+               return 0;
+
+       skb_pull(skb, sizeof(*info));
+
+       bacpy(&smp->id_addr, &info->bdaddr);
+       smp->id_addr_type = info->addr_type;
+
+       if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
+               bacpy(&rpa, &hcon->dst);
+       else
+               bacpy(&rpa, BDADDR_ANY);
+
+       hci_add_irk(conn->hcon->hdev, &smp->id_addr, smp->id_addr_type,
+                   smp->irk, &rpa);
+
+       smp_distribute_keys(conn, 1);
+
+       return 0;
+}
+
 int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
 {
        struct hci_conn *hcon = conn->hcon;
@@ -915,7 +1063,13 @@ int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
                break;
 
        case SMP_CMD_IDENT_INFO:
+               reason = smp_cmd_ident_info(conn, skb);
+               break;
+
        case SMP_CMD_IDENT_ADDR_INFO:
+               reason = smp_cmd_ident_addr_info(conn, skb);
+               break;
+
        case SMP_CMD_SIGN_INFO:
                /* Just ignored */
                reason = 0;
@@ -964,7 +1118,6 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
                *keydist &= req->resp_key_dist;
        }
 
-
        BT_DBG("keydist 0x%x", *keydist);
 
        if (*keydist & SMP_DIST_ENC_KEY) {