Skip to contentSkip to conversation

ccid-card-passthru: check buffer size parameter

PATCH In review P J P 3 participants 7 messages Oct 11, 2018

lore
Share & tools
lore

P J P

author
#0 lore raw

PATCH 1/1 ccid-card-passthru: check buffer size parameter

From: Prasad J Pandit <[email protected]>

While reading virtual smart card data, if buffer 'size' is negative it would lead to memory corruption errors. Add check to avoid it.

---
 hw/usb/ccid-card-passthru.c | 1 +
 1 file changed, 1 insertion(+)
hw/usb/ccid-card-passthru.c +1 -0
index 0a6c657228..63ed78f4c6 100644
@@ -275,6 +275,7 @@ static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size)
     PassthruState *card = opaque;
     VSCMsgHeader *hdr;
 
+    assert(0 <= size && size < VSCARD_IN_SIZE);
     if (card->vscard_in_pos + size > VSCARD_IN_SIZE) {
         error_report("no room for data: pos %u +  size %d > %" PRId64 "."
                      " dropping connection.",
Signature · 1 line
2.17.1
#1 lore raw

Cc'ing Paolo & Marc-André.

Quoted 4 lines · P J P

From: Prasad J Pandit <[email protected]>

While reading virtual smart card data, if buffer 'size' is negative it would lead to memory corruption errors. Add check to avoid it.

The IOReadHandler does not have documentation.

typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);

Why is the 'size' argument signed? Does it makes sens to call it with a negative value?

Thanks,

Phil.

Quoted 20 lines
---
 hw/usb/ccid-card-passthru.c | 1 +
 1 file changed, 1 insertion(+)
hw/usb/ccid-card-passthru.c +1 -0
index 0a6c657228..63ed78f4c6 100644
@@ -275,6 +275,7 @@ static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size)
     PassthruState *card = opaque;
     VSCMsgHeader *hdr;
 
+    assert(0 <= size && size < VSCARD_IN_SIZE);
     if (card->vscard_in_pos + size > VSCARD_IN_SIZE) {
         error_report("no room for data: pos %u +  size %d > %" PRId64 "."
                      " dropping connection.",
#2 lore raw
Quoted 8 lines · Philippe Mathieu-Daudé

Cc'ing Paolo & Marc-André.

Quoted 4 lines · P J P

From: Prasad J Pandit <[email protected]>

While reading virtual smart card data, if buffer 'size' is negative it would lead to memory corruption errors. Add check to avoid it.

Quoted 6 lines

The IOReadHandler does not have documentation.

typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);

Why is the 'size' argument signed? Does it makes sens to call it with a negative value?

Yeah, it probably should be changed to size_t (same for the return value of can_read callbacks); the size cannot be negative, in fact it can also never be zero.

Also, the "if" should never trigger because the size is bound to what can_read returns, which is VSCARD_IN_SIZE - card->vscard_in_pos.

So it is probably better to:

1) move the

    assert(card->vscard_in_pos < VSCARD_IN_SIZE);

to can_read, which becomes

    assert(card->vscard_in_pos <= VSCARD_IN_SIZE);
    return VSCARD_IN_SIZE - card->vscard_in_pos;

2) here, replace the if with an

    assert(size <= VSCARD_IN_SIZE - card->vscard_in_pos);

Note that the right side of the comparison is the return value of can_read. Also, this assert will always fail if card->vscard_in_pos >= VSCARD_IN_SIZE), since size > 0.

3) also here, replace the

    assert(card->vscard_in_hdr < VSCARD_IN_SIZE);

with the stricter and more correct

    assert(card->vscard_in_hdr < card->vscard_in_pos);

Related, I don't understand why the read function ends with

    if (card->vscard_in_hdr == card->vscard_in_pos) {
        card->vscard_in_pos = card->vscard_in_hdr = 0;
    }

I would have expected something more generic, like:

    /* Drop any messages that were fully processed.  */
    if (card->vscard_in_hdr > 0) {
        memmove(card->vscard_in_data,
                card->vscard_in_data + card->vscard_in_hdr,
                card->vscard_in_pos - card->vscard_in_hdr);
        card->vscard_in_pos -= card->vscard_in_hdr;
        card->vscard_in_hdr = 0;
    }

Marc-André, do you know something about libcacard that justifies the latter? If the error_report is changed to an assert it's probably a good idea anyway to make the code more liberal in what it accepts. Prasad, can you prepare a v2 that does these changes?

Thanks,

Paolo

Quoted 26 lines

Thanks,

Phil.

Quoted 20 lines
---
 hw/usb/ccid-card-passthru.c | 1 +
 1 file changed, 1 insertion(+)
hw/usb/ccid-card-passthru.c +1 -0
index 0a6c657228..63ed78f4c6 100644
@@ -275,6 +275,7 @@ static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size)
     PassthruState *card = opaque;
     VSCMsgHeader *hdr;
 
+    assert(0 <= size && size < VSCARD_IN_SIZE);
     if (card->vscard_in_pos + size > VSCARD_IN_SIZE) {
         error_report("no room for data: pos %u +  size %d > %" PRId64 "."
                      " dropping connection.",
author
#3 lore raw
In reply to Philippe Mathieu-Daudé
+-- On Thu, 11 Oct 2018, Philippe Mathieu-Daudé wrote --+
| The IOReadHandler does not have documentation.
| 
|  typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
| 
| Why is the 'size' argument signed? Does it makes sens to call it with a
| negative value?

No, it doesn't IMO. I had first changed argument type 'int' to uint32_t'.
as

  typedef void IOReadHandler(void *opaque, const uint8_t *buf, uint32_t size);

But 'IOReadHandler' is registered and called from multiple char devices, which lead to compile time errors. As the function prototype changed.

I'll update them all, if the above change is okay.

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

#4 lore raw
Quoted 11 lines · P J P
+-- On Thu, 11 Oct 2018, Philippe Mathieu-Daudé wrote --+
| The IOReadHandler does not have documentation.
| 
|  typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
| 
| Why is the 'size' argument signed? Does it makes sens to call it with a
| negative value?

No, it doesn't IMO. I had first changed argument type 'int' to uint32_t'.
as

Quoted 6 lines
  typedef void IOReadHandler(void *opaque, const uint8_t *buf, uint32_t size);

But 'IOReadHandler' is registered and called from multiple char devices, which lead to compile time errors. As the function prototype changed.

I'll update them all, if the above change is okay.

Yes, please do so.

Paolo

Quoted 5 lines

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

#5 lore raw
In reply to P J P
Quoted 11 lines · P J P
+-- On Thu, 11 Oct 2018, Philippe Mathieu-Daudé wrote --+
| The IOReadHandler does not have documentation.
| 
|  typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
| 
| Why is the 'size' argument signed? Does it makes sens to call it with a
| negative value?

No, it doesn't IMO. I had first changed argument type 'int' to uint32_t'.
as

Quoted 6 lines
  typedef void IOReadHandler(void *opaque, const uint8_t *buf, uint32_t size);

But 'IOReadHandler' is registered and called from multiple char devices, which lead to compile time errors. As the function prototype changed.

I'll update them all, if the above change is okay.

I started this change and already converted 40 files.

author
#6 lore raw
+-- On Thu, 11 Oct 2018, Philippe Mathieu-Daudé wrote --+
| I started this change and already converted 40 files.

Wow, that's super swift! :) Will wait for the patch V2 from you then.

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F