Skip to contentSkip to conversation

nfc: pn533: fix OOB read in pn533_acr122_is_rx_frame_valid()

PATCH Superseded Deepanshu Kartikey 2 participants 3 messages Sep 6 – Sep 9 NFC

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nfc: pn533: fix OOB read in pn533_acr122_is_rx_frame_valid()

frame->ccid.datalen is read directly from the USB response frame and used, unchecked, as an index into frame->data[]. A malicious or malfunctioning device can set this field to an arbitrary value, causing the driver to read far outside the received buffer.

Bound ccid.datalen against the maximum possible ACR122 frame size before using it, and reject values less than 2 to avoid the "datalen - 2" underflowing.

Fixes 9815c7cf22da ("NFC: pn533: Separate physical layer from the core implementation")
Reported-by [email protected]
---
 drivers/nfc/pn533/usb.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/nfc/pn533/usb.c +5 -0
index efb07f944fce..d08d3b0df988 100644
@@ -322,6 +322,11 @@ static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev)
 	if (!frame->ccid.datalen)
 		return false;
 
+	if (frame->ccid.datalen < 2 ||
+	    frame->ccid.datalen > PN533_ACR122_FRAME_MAX_PAYLOAD_LEN +
+	    PN533_ACR122_RX_FRAME_TAIL_LEN)
+		return false;
+
 	if (frame->data[frame->ccid.datalen - 2] == 0x63)
 		return false;
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 9 lines · Deepanshu Kartikey

frame->ccid.datalen is read directly from the USB response frame and used, unchecked, as an index into frame->data[]. A malicious or malfunctioning device can set this field to an arbitrary value, causing the driver to read far outside the received buffer.

Bound ccid.datalen against the maximum possible ACR122 frame size before using it, and reject values less than 2 to avoid the "datalen - 2" underflowing.

Quoted 5 lines
Fixes 9815c7cf22da ("NFC: pn533: Separate physical layer from the core implementation")
Reported-by [email protected]

Are you missing an Assisted-by tag here?

Quoted 9 lines
---
 drivers/nfc/pn533/usb.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/nfc/pn533/usb.c +0 -0
index efb07f944fce..d08d3b0df988 100644

drivers/nfc/pn533/usb.c static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev) · line 329

Quoted 8 lines
drivers/nfc/pn533/usb.c +5 -0
@@ -322,6 +322,11 @@ static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev)
 	if (!frame->ccid.datalen)
 		return false;
 
+	if (frame->ccid.datalen < 2 ||
+	    frame->ccid.datalen > PN533_ACR122_FRAME_MAX_PAYLOAD_LEN +
+	    PN533_ACR122_RX_FRAME_TAIL_LEN)
+		return false;
+

Just a drive-by comment, but should this not replace the existing datalen == 0 check?

drivers/nfc/pn533/usb.c static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev) · line 331

Quoted 2 lines
drivers/nfc/pn533/usb.c +0 -0
 	if (frame->data[frame->ccid.datalen - 2] == 0x63)
 		return false;

Johan

#2 lore raw
Quoted 4 lines · Johan Hovold

Are you missing an Assisted-by tag here?

Sure , I will add assisted-by tag .

Quoted 7 lines

Just a drive-by comment, but should this not replace the existing datalen == 0 check?

Quoted 2 lines
      if (frame->data[frame->ccid.datalen - 2] == 0x63)
              return false;

Johan

I will remove the older if (!frame->ccid.datalen) check.

I will send patch v2 shortly

Thanks

Deepanshu