Skip to contentSkip to conversation

NFC: potential overflows in microread_target_discovered()

PATCH Applied Dan Carpenter 3 participants 5 messages Sep 1, 2014 – Sep 4, 2014

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

PATCH 1/1 NFC: potential overflows in microread_target_discovered()

Smatch says that skb->data is untrusted so we need to check to make sure that the memcpy() doesn't overflow.

Fixes cfad1ba87150 ('NFC: Initial support for Inside Secure microread')

---
I sent this in January but never received any response. I don't know the subsystem very well but it looks like it could have security implications.

Compile tested only.

drivers/nfc/microread/microread.c +6 -2
index f868333271aa..963a4a5dc88e 100644
@@ -501,9 +501,13 @@ static void microread_target_discovered(struct nfc_hci_dev *hdev, u8 gate,
 		targets->sens_res  			 be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A_ATQA]);
 		targets->sel_res = skb->data[MICROREAD_EMCF_A_SAK];
-		memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A_UID],
-		       skb->data[MICROREAD_EMCF_A_LEN]);
 		targets->nfcid1_len = skb->data[MICROREAD_EMCF_A_LEN];
+		if (targets->nfcid1_len > sizeof(targets->nfcid1)) {
+			r = -EINVAL;
+			goto exit_free;
+		}
+		memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A_UID],
+		       targets->nfcid1_len);
 		break;
 	case MICROREAD_GATE_ID_MREAD_ISO_A_3:
 		targets->supported_protocols @@ -511,9 +515,13 @@ static void microread_target_discovered(struct nfc_hci_dev *hdev, u8 gate,
 		targets->sens_res  			 be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A3_ATQA]);

targets->sel_res = skb->data[MICROREAD_EMCF_A3_SAK];
- memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A3_UID],
- skb->data[MICROREAD_EMCF_A3_LEN]);
targets->nfcid1_len = skb->data[MICROREAD_EMCF_A3_LEN];
+ if (targets->nfcid1_len > sizeof(targets->nfcid1)) {
+ r = -EINVAL;
+ goto exit_free;
+ }
+ memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A3_UID],
+ targets->nfcid1_len);
break;
case MICROREAD_GATE_ID_MREAD_ISO_B:
targets->supported_protocols = NFC_PROTO_ISO14443_B_MASK;

#1 lore raw

Hi,

Quoted 17 lines · Dan Carpenter

Smatch says that skb->data is untrusted so we need to check to make sure that the memcpy() doesn't overflow.

Fixes cfad1ba87150 ('NFC: Initial support for Inside Secure microread')

---
I sent this in January but never received any response. I don't know the subsystem very well but it looks like it could have security implications.

Compile tested only.

drivers/nfc/microread/microread.c +0 -0
index f868333271aa..963a4a5dc88e 100644

drivers/nfc/microread/microread.c static void microread_target_discovered(struct nfc_hci_dev *hdev, u8 gate, · line 504

Quoted 6 lines
drivers/nfc/microread/microread.c +1 -2
@@ -501,9 +501,13 @@ static void microread_target_discovered(struct nfc_hci_dev *hdev, u8 gate,
                targets->sens_res >                          be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A_ATQA]);
                targets->sel_res = skb->data[MICROREAD_EMCF_A_SAK];
-               memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A_UID],
-                      skb->data[MICROREAD_EMCF_A_LEN]);
                targets->nfcid1_len = skb->data[MICROREAD_EMCF_A_LEN];
+               if (targets->nfcid1_len > sizeof(targets->nfcid1)) {

You should probably compare against sizeof(*targets->nfcid1).

Quoted 10 lines
drivers/nfc/microread/microread.c +5 -0
+                       r = -EINVAL;
+                       goto exit_free;
+               }
+               memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A_UID],
+                      targets->nfcid1_len);
                break;
        case MICROREAD_GATE_ID_MREAD_ISO_A_3:
                targets->supported_protocols > @@ -511,9 +515,13 @@ static void microread_target_discovered(struct nfc_hci_dev *hdev, u8 gate,
                targets->sens_res >                          be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A3_ATQA]);
                targets->sel_res = skb->data[MICROREAD_EMCF_A3_SAK];

drivers/nfc/microread/microread.c

Quoted 4 lines
drivers/nfc/microread/microread.c +1 -2
-               memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A3_UID],
-                      skb->data[MICROREAD_EMCF_A3_LEN]);
                targets->nfcid1_len = skb->data[MICROREAD_EMCF_A3_LEN];
+               if (targets->nfcid1_len > sizeof(targets->nfcid1)) {

Same here.

Quoted 12 lines
drivers/nfc/microread/microread.c +5 -1
+                       r = -EINVAL;
+                       goto exit_free;
+               }
+               memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A3_UID],
+                      targets->nfcid1_len);
                break;
        case MICROREAD_GATE_ID_MREAD_ISO_B:
                targets->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
--

To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 5 lines · Frans Klaver
Quoted 5 lines
drivers/nfc/microread/microread.c +0 -0
index f868333271aa..963a4a5dc88e 100644
@@ -501,9 +501,13 @@ static void microread_target_discovered(struct nfc_hci_dev *hdev, u8 gate,
Quoted 9 lines

drivers/nfc/microread/microread.c static void microread_target_discovered(struct nfc_hci_dev *hdev, u8 gate, · line 504

Quoted 6 lines
drivers/nfc/microread/microread.c +1 -2
                targets->sens_res > >                          be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A_ATQA]);
                targets->sel_res = skb->data[MICROREAD_EMCF_A_SAK];
-               memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A_UID],
-                      skb->data[MICROREAD_EMCF_A_LEN]);
                targets->nfcid1_len = skb->data[MICROREAD_EMCF_A_LEN];
+               if (targets->nfcid1_len > sizeof(targets->nfcid1)) {

You should probably compare against sizeof(*targets->nfcid1).

No. It's an array not a pointer.

You should make a small test program to test your ideas.

int main(void)

{
        char buf[10];

        printf("%d %d\n", sizeof(buf), sizeof(*buf));
        return 0;
}

regards,
dan carpenter

#3 lore raw
Quoted 16 lines · Dan Carpenter
Quoted 5 lines · Frans Klaver
Quoted 5 lines
drivers/nfc/microread/microread.c +0 -0
index f868333271aa..963a4a5dc88e 100644
@@ -501,9 +501,13 @@ static void microread_target_discovered(struct nfc_hci_dev *hdev, u8 gate,
Quoted 9 lines

drivers/nfc/microread/microread.c static void microread_target_discovered(struct nfc_hci_dev *hdev, u8 gate, · line 504

Quoted 6 lines
drivers/nfc/microread/microread.c +1 -2
                targets->sens_res >> >                          be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A_ATQA]);
                targets->sel_res = skb->data[MICROREAD_EMCF_A_SAK];
-               memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A_UID],
-                      skb->data[MICROREAD_EMCF_A_LEN]);
                targets->nfcid1_len = skb->data[MICROREAD_EMCF_A_LEN];
+               if (targets->nfcid1_len > sizeof(targets->nfcid1)) {

You should probably compare against sizeof(*targets->nfcid1).

Quoted 1 line

No. It's an array not a pointer.

Ai, I overlooked that one. My bad.

Applied
#4 lore raw
In reply to Dan Carpenter

Hi Dan,

Quoted 2 lines · Dan Carpenter

Smatch says that skb->data is untrusted so we need to check to make sure that the memcpy() doesn't overflow.

Applied to nfc-fixes, thanks.

Cheers,
Samuel.

Signature · 2 lines
Intel Open Source Technology Centre
http://oss.intel.com/