ccid-card-passthru: check buffer size parameter
v2 cover ccid-card-passthru: check buffer size parameter
Hi,
This is the v2 of Prasad J Pandit first version [*], with Paolo's review comment addressed.
This is a quick fix for CVE-2018-18438: "Integer overflow in ccid_card_vscard_read() allows memory corruption".
Please review,
Phil.
[*] https://lists.gnu.org/archive/html/qemu-devel/2018-10/msg02200.html
Philippe Mathieu-Daudé (9):
ccid-card-passthru: Move assertion in read() to can_read()
ccid-card-passthru: Replace never trigger if statement by an assertion
ccid-card-passthru: Assert on a stricter expression
ccid-card-passthru: Let the chardev::read() be more generic
ccid-card-passthru: Replace assert() by QEMU_BUILD_BUG_ON()
ccid-card-passthru: Simplify the if() condition
ccid-card-passthru: Use QERR_MISSING_PARAMETER
ccid-card-passthru: Use size_t to hold size argument
ccid-card-passthru: Use size_t for index
hw/usb/ccid-card-passthru.c | 73 +++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 39 deletions(-)
Signature · 1 line
2.20.1
PATCH v2 1/9 ccid-card-passthru: Move assertion in read() to can_read()
chardev::read() depends of what chardev::can_read() returns, move the assertion to can_read().
--- hw/usb/ccid-card-passthru.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
hw/usb/ccid-card-passthru.c +2 -3
@@ -116,8 +116,8 @@ static int ccid_card_vscard_can_read(void *opaque) {PassthruState *card = opaque; -return VSCARD_IN_SIZE >= card->vscard_in_pos ? -VSCARD_IN_SIZE - card->vscard_in_pos :0 ; +assert (card->vscard_in_pos <=VSCARD_IN_SIZE ); +return VSCARD_IN_SIZE - card->vscard_in_pos; } static void ccid_card_vscard_handle_init(
@@ -282,7 +282,6 @@ static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size)ccid_card_vscard_drop_connection (card);return ; } -assert (card->vscard_in_pos <VSCARD_IN_SIZE );assert (card->vscard_in_hdr <VSCARD_IN_SIZE );memcpy (card->vscard_in_data + card->vscard_in_pos, buf, size); card->vscard_in_pos += size;
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH v2 1/9] ccid-card-passthru: Move assertion in read() to can_read()
Quoted 1 line · Philippe Mathieu-Daudé
chardev::read()depends of whatchardev::can_read()returns, move the
s/of/on/
Quoted 8 lines
assertion to
can_read().Suggested-by Paolo Bonzini <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/usb/ccid-card-passthru.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH v2 1/9] ccid-card-passthru: Move assertion in read() to can_read()
In reply to Philippe Mathieu-DaudéQuoted 14 lines · Philippe Mathieu-Daudé
chardev::read()depends of whatchardev::can_read()returns, move the assertion tocan_read().Suggested-by Paolo Bonzini <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
hw/usb/ccid-card-passthru.c| 5 ++---1 file changed, 2 insertions(+), 3 deletions(-)hw/usb/ccid-card-passthru.c +0
-0
hw/usb/ccid-card-passthru.c static int ccid_card_vscard_can_read(void *opaque) · line 119
Quoted 6 lines
hw/usb/ccid-card-passthru.c +1
-2@@ -116,8 +116,8 @@ static int ccid_card_vscard_can_read(void *opaque) {PassthruState *card = opaque; -return VSCARD_IN_SIZE >= card->vscard_in_pos ? -VSCARD_IN_SIZE - card->vscard_in_pos :0 ; +assert (card->vscard_in_pos <=VSCARD_IN_SIZE );
Do you have special reason change "<" to "<="?
Quoted 15 lines
hw/usb/ccid-card-passthru.c +1
-0+return VSCARD_IN_SIZE - card->vscard_in_pos; }static void ccid_card_vscard_handle_init(
hw/usb/ccid-card-passthru.c +0
-1@@ -282,7 +282,6 @@ static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size)ccid_card_vscard_drop_connection (card);return ; } -assert (card->vscard_in_pos <VSCARD_IN_SIZE );assert (card->vscard_in_hdr <VSCARD_IN_SIZE );memcpy (card->vscard_in_data + card->vscard_in_pos, buf, size); card->vscard_in_pos += size;Signature · 1 line
2.20.1
Signature · 2 lines
Wei Yang Help you, Help me
Re: [Qemu-devel] [PATCH v2 1/9] ccid-card-passthru: Move assertion in read() to can_read()
In reply to Philippe Mathieu-DaudéHi
Quoted 6 lines · Philippe Mathieu-Daudé
chardev::read()depends of whatchardev::can_read()returns, move the assertion tocan_read().Suggested-by Paolo Bonzini <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Moving to can_read() makes sense, because nothing (except the chardev BREAK event) should update vscard_in_pos between the can_read() and read() callback.
Changing the condition from < to <= could use some explanation. The can_read() callback should handle the case where the vscard_in buffer is full (adding = is necessary). And the read() callback should not be called with size == 0, when card->vscard_in_pos == VSCARD_IN_SIZE (no data to read).
But it wouldn't harm to leave the existing assert().
Quoted 30 lines
--- hw/usb/ccid-card-passthru.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)hw/usb/ccid-card-passthru.c +2
-4@@ -116,8 +116,8 @@ static int ccid_card_vscard_can_read(void *opaque) {PassthruState *card = opaque; -return VSCARD_IN_SIZE >= card->vscard_in_pos ? -VSCARD_IN_SIZE - card->vscard_in_pos :0 ; +assert (card->vscard_in_pos <=VSCARD_IN_SIZE ); +return VSCARD_IN_SIZE - card->vscard_in_pos; } static void ccid_card_vscard_handle_init(@@ -282,7 +282,6 @@ static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size)ccid_card_vscard_drop_connection (card);return ; } -assert (card->vscard_in_pos <VSCARD_IN_SIZE );assert (card->vscard_in_hdr <VSCARD_IN_SIZE );memcpy (card->vscard_in_data + card->vscard_in_pos, buf, size); card->vscard_in_pos += size; --2.20.1
PATCH v2 2/9 ccid-card-passthru: Replace never trigger if statement by an assertion
In reply to Philippe Mathieu-DaudéThe right side of the comparison is the return value of can_read():VSCARD_IN_SIZE - card->vscard_in_pos.
Since the 'size' argument of chardev::read() is bound to what chardev::can_read() returns, this condition can never happen.
Add an assertion, which will always fail if card->vscard_in_pos >= VSCARD_IN_SIZE), since size > 0.
This is a quick fix for CVE-2018-18438 "Integer overflow in ccid_card_vscard_read() allows memory corruption".
--- hw/usb/ccid-card-passthru.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-)
hw/usb/ccid-card-passthru.c +1 -13
@@ -264,24 +264,12 @@ static void ccid_card_vscard_handle_message(PassthruState *card, } } -static void ccid_card_vscard_drop_connection (PassthruState *card) -{ -qemu_chr_fe_deinit (&card->cs, true); - card->vscard_in_pos = card->vscard_in_hdr =0 ; -} -static void ccid_card_vscard_read (void *opaque,const uint8_t *buf,int size) {PassthruState *card = opaque;VSCMsgHeader *hdr; -if (card->vscard_in_pos + size >VSCARD_IN_SIZE ) { -error_report ("no room for data: pos %u + size %d > %" PRId64"." -" dropping connection." , - card->vscard_in_pos, size,VSCARD_IN_SIZE ); -ccid_card_vscard_drop_connection (card); -return ; - } +assert (size <=VSCARD_IN_SIZE - card->vscard_in_pos);assert (card->vscard_in_hdr <VSCARD_IN_SIZE );memcpy (card->vscard_in_data + card->vscard_in_pos, buf, size); card->vscard_in_pos += size;
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH v2 2/9] ccid-card-passthru: Replace never trigger if statement by an assertion
Hi
Quoted 5 lines · Philippe Mathieu-Daudé
The right side of the comparison is the return value of
can_read():VSCARD_IN_SIZE-card->vscard_in_pos.
Since the 'size' argument ofchardev::read()is bound to whatchardev::can_read()returns, this condition can never happen.
I think so too, because vscard_in_pos is unchanged between the 2 callbacks (or set to 0 in break event).
Quoted 3 lines
Add an assertion, which will always fail if
card->vscard_in_pos>=VSCARD_IN_SIZE), since size > 0.
If "size > VSCARD_IN_SIZE - card->vscard_in_pos" this is a chardev bug. But which backend does that?
Iow, did we ever reach the "no room for data" error?
Quoted 3 lines
This is a quick fix for CVE-2018-18438 "Integer overflow in
ccid_card_vscard_read()allows memory corruption".
I have a hard time to find how that memory corruption can happen. It would be a broken chardev (one calling qemu_chr_be_write() with a size bigger than qemu_chr_be_can_write()). It would need to be fixed. But which one does that?
Quoted 42 lines
Fixes CVE-2018-18438Reported-by Arash Tohidi Chafi <[email protected]>Suggested-by Paolo Bonzini <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/usb/ccid-card-passthru.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-)hw/usb/ccid-card-passthru.c +1
-14@@ -264,24 +264,12 @@ static void ccid_card_vscard_handle_message(PassthruState *card, } } -static void ccid_card_vscard_drop_connection (PassthruState *card) -{ -qemu_chr_fe_deinit (&card->cs, true); - card->vscard_in_pos = card->vscard_in_hdr =0 ; -} -static void ccid_card_vscard_read (void *opaque,const uint8_t *buf,int size) {PassthruState *card = opaque;VSCMsgHeader *hdr; -if (card->vscard_in_pos + size >VSCARD_IN_SIZE ) { -error_report ("no room for data: pos %u + size %d > %" PRId64"." -" dropping connection." , - card->vscard_in_pos, size,VSCARD_IN_SIZE ); -ccid_card_vscard_drop_connection (card); -return ; - } +assert (size <=VSCARD_IN_SIZE - card->vscard_in_pos);assert (card->vscard_in_hdr <VSCARD_IN_SIZE );memcpy (card->vscard_in_data + card->vscard_in_pos, buf, size); card->vscard_in_pos += size; --2.20.1
Re: [Qemu-devel] [PATCH v2 2/9] ccid-card-passthru: Replace never trigger if statement by an assertion
Quoted 23 lines · Marc-André Lureau
Hi
Quoted 5 lines · Philippe Mathieu-Daudé
The right side of the comparison is the return value of
can_read():VSCARD_IN_SIZE-card->vscard_in_pos.
Since the 'size' argument ofchardev::read()is bound to whatchardev::can_read()returns, this condition can never happen.I think so too, because
vscard_in_posis unchanged between the 2 callbacks (or set to 0 in break event).Quoted 3 lines
Add an assertion, which will always fail if
card->vscard_in_pos>=VSCARD_IN_SIZE), since size > 0.If "size >
VSCARD_IN_SIZE-card->vscard_in_pos" this is a chardev bug. But which backend does that?Iow, did we ever reach the "no room for data" error?
Quoted 1 line
Quoted 6 lines
Quoted 2 lines
This is a quick fix for CVE-2018-18438 "Integer overflow in
ccid_card_vscard_read()allows memory corruption".I have a hard time to find how that memory corruption can happen. It would be a broken chardev (one calling
qemu_chr_be_write()with a size bigger thanqemu_chr_be_can_write()). It would need to be fixed. But
It will :)
Quoted 1 line
which one does that?
Arash or Prasad can you help us here? Do you have a reproducer?
Quoted 42 lines
Quoted 42 lines
Fixes CVE-2018-18438Reported-by Arash Tohidi Chafi <[email protected]>Suggested-by Paolo Bonzini <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/usb/ccid-card-passthru.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-)hw/usb/ccid-card-passthru.c +1
-14@@ -264,24 +264,12 @@ static void ccid_card_vscard_handle_message(PassthruState *card, } } -static void ccid_card_vscard_drop_connection (PassthruState *card) -{ -qemu_chr_fe_deinit (&card->cs, true); - card->vscard_in_pos = card->vscard_in_hdr =0 ; -} -static void ccid_card_vscard_read (void *opaque,const uint8_t *buf,int size) {PassthruState *card = opaque;VSCMsgHeader *hdr; -if (card->vscard_in_pos + size >VSCARD_IN_SIZE ) { -error_report ("no room for data: pos %u + size %d > %" PRId64"." -" dropping connection." , - card->vscard_in_pos, size,VSCARD_IN_SIZE ); -ccid_card_vscard_drop_connection (card); -return ; - } +assert (size <=VSCARD_IN_SIZE - card->vscard_in_pos);assert (card->vscard_in_hdr <VSCARD_IN_SIZE );memcpy (card->vscard_in_data + card->vscard_in_pos, buf, size); card->vscard_in_pos += size; --2.20.1
Re: [Qemu-devel] [PATCH v2 2/9] ccid-card-passthru: Replace never trigger if statement by an assertion
+-- On Mon, 18 Feb 2019, Philippe Mathieu-Daudé wrote --+ | On 2/15/19 11:59 AM, Marc-André Lureau wrote: | Arash or Prasad can you help us here? Do you have a reproducer?
No, we don't have a reproducer handy I'm afraid.
Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F
Re: [Qemu-devel] [PATCH v2 2/9] ccid-card-passthru: Replace never trigger if statement by an assertion
Hi
Quoted 6 lines · P J P
+-- On Mon, 18 Feb 2019, Philippe Mathieu-Daudé wrote --+ | On 2/15/19 11:59 AM, Marc-André Lureau wrote: | Arash or Prasad can you help us here? Do you have a reproducer?No, we don't have a reproducer handy I'm afraid.
Without a reproducer or convincing arguments, do we have a CVE?
Signature · 1 line
Marc-André Lureau
PATCH v2 3/9 ccid-card-passthru: Assert on a stricter expression
In reply to Philippe Mathieu-Daudé--- hw/usb/ccid-card-passthru.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/usb/ccid-card-passthru.c +1 -1
@@ -270,7 +270,7 @@ static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size)VSCMsgHeader *hdr;assert (size <=VSCARD_IN_SIZE - card->vscard_in_pos); -assert (card->vscard_in_hdr <VSCARD_IN_SIZE ); +assert (card->vscard_in_hdr < card->vscard_in_pos);memcpy (card->vscard_in_data + card->vscard_in_pos, buf, size); card->vscard_in_pos += size; hdr = (VSCMsgHeader *)(card->vscard_in_data + card->vscard_in_hdr);
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH v2 3/9] ccid-card-passthru: Assert on a stricter expression
Would it be better to have some description?
Quoted 22 lines · Philippe Mathieu-Daudé
Suggested-by Paolo Bonzini <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
hw/usb/ccid-card-passthru.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)hw/usb/ccid-card-passthru.c +1
-1@@ -270,7 +270,7 @@ static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size)VSCMsgHeader *hdr;assert (size <=VSCARD_IN_SIZE - card->vscard_in_pos); -assert (card->vscard_in_hdr <VSCARD_IN_SIZE ); +assert (card->vscard_in_hdr < card->vscard_in_pos);memcpy (card->vscard_in_data + card->vscard_in_pos, buf, size); card->vscard_in_pos += size; hdr = (VSCMsgHeader *)(card->vscard_in_data + card->vscard_in_hdr);Signature · 1 line
2.20.1
Signature · 2 lines
Wei Yang Help you, Help me
Re: [Qemu-devel] [PATCH v2 3/9] ccid-card-passthru: Assert on a stricter expression
In reply to Philippe Mathieu-DaudéHi
Quoted 12 lines · Philippe Mathieu-Daudé
Suggested-by Paolo Bonzini <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/usb/ccid-card-passthru.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/usb/ccid-card-passthru.c +0
-0
hw/usb/ccid-card-passthru.c static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size) · line 273
Quoted 5 lines
hw/usb/ccid-card-passthru.c +1
-1@@ -270,7 +270,7 @@ static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size)VSCMsgHeader *hdr;assert (size <=VSCARD_IN_SIZE - card->vscard_in_pos); -assert (card->vscard_in_hdr <VSCARD_IN_SIZE ); +assert (card->vscard_in_hdr < card->vscard_in_pos);
I think you need "<=".
card->vscard_in_hdr is updated in the loop and may reach card->vscard_in_pos:
while ((card->vscard_in_pos - card->vscard_in_hdr >= sizeof(VSCMsgHeader))
&&(card->vscard_in_pos - card->vscard_in_hdr >=
sizeof(VSCMsgHeader) + ntohl(hdr->length))) {
...
card->vscard_in_hdr += hdr->length + sizeof(VSCMsgHeader);
...
}if "hdr->length + sizeof(VSCMsgHeader)" == "card->vscard_in_pos - card->vscard_in_hdr", card->vscard_in_hdr == card->vscard_in_pos after the loop.
I think the existing assert() could also stay.
Quoted 6 lines
hw/usb/ccid-card-passthru.c +0
-1memcpy (card->vscard_in_data + card->vscard_in_pos, buf, size); card->vscard_in_pos += size; hdr = (VSCMsgHeader *)(card->vscard_in_data + card->vscard_in_hdr); --2.20.1
PATCH v2 4/9 ccid-card-passthru: Let the chardev::read() be more generic
In reply to Philippe Mathieu-Daudé--- hw/usb/ccid-card-passthru.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
hw/usb/ccid-card-passthru.c +8 -2
@@ -285,8 +285,14 @@ static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size) card->vscard_in_hdr += hdr->length +sizeof (VSCMsgHeader); hdr = (VSCMsgHeader *)(card->vscard_in_data + card->vscard_in_hdr); } -if (card->vscard_in_hdr == card->vscard_in_pos) { - card->vscard_in_pos = card->vscard_in_hdr =0 ; + +/* 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 ; } }
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH v2 4/9] ccid-card-passthru: Let the chardev::read() be more generic
Hi
Quoted 12 lines · Philippe Mathieu-Daudé
Suggested-by Paolo Bonzini <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/usb/ccid-card-passthru.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)hw/usb/ccid-card-passthru.c +0
-0
hw/usb/ccid-card-passthru.c static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size) · line 289
Quoted 5 lines
hw/usb/ccid-card-passthru.c +0
-2@@ -285,8 +285,14 @@ static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size) card->vscard_in_hdr += hdr->length +sizeof (VSCMsgHeader); hdr = (VSCMsgHeader *)(card->vscard_in_data + card->vscard_in_hdr); } -if (card->vscard_in_hdr == card->vscard_in_pos) { - card->vscard_in_pos = card->vscard_in_hdr =0 ;
Interesting, it looks like we could end in a blocking condition today.
card->vscard_in_pos - card->vscard_in_hdr could not have enough room to process an incoming message. After filling the buffer, it would stop reading.
hw/usb/ccid-card-passthru.c static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size) · line 297
Quoted 10 lines
hw/usb/ccid-card-passthru.c +8
-0+ +/* 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 ; } }
At least, by moving data around, this would leave always enough space for the header to be fully read.
But I think we should add a condition like card->vscard_in_hdr + hdr->length + sizeof(VSCMsgHeader) <= VSCARD_IN_SIZE, to make sure the incoming message fits in the vscard_in_data buffer, else disconnect?
Quoted 4 lines
hw/usb/ccid-card-passthru.c +0
-1--2.20.1
PATCH v2 5/9 ccid-card-passthru: Replace assert() by QEMU_BUILD_BUG_ON()
In reply to Philippe Mathieu-Daudé--- hw/usb/ccid-card-passthru.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
hw/usb/ccid-card-passthru.c +5 -4
@@ -29,6 +29,9 @@ do { \#define D_MORE_INFO 3#define D_VERBOSE 4 +/* maximum size of ATR - from 7816-3 */ +#define MAX_ATR_SIZE 40 +/* TODO: do we still need this? */ static const uint8_t DEFAULT_ATR [] = {/*
@@ -41,10 +44,9 @@ static const uint8_t DEFAULT_ATR[] = {0x13 ,0x08 }; -#define VSCARD_IN_SIZE (64 * KiB) +QEMU_BUILD_BUG_ON (sizeof (DEFAULT_ATR ) >MAX_ATR_SIZE ); -/* maximum size of ATR - from 7816-3 */ -#define MAX_ATR_SIZE 40 +#define VSCARD_IN_SIZE (64 * KiB)typedef struct PassthruState PassthruState ;
@@ -351,7 +353,6 @@ static void passthru_realize(CCIDCardState *base, Error **errp) } card->debug =parse_debug_env ("QEMU_CCID_PASSTHRU_DEBUG" ,D_VERBOSE , card->debug); -assert (sizeof (DEFAULT_ATR ) <=MAX_ATR_SIZE );memcpy (card->atr,DEFAULT_ATR ,sizeof (DEFAULT_ATR )); card->atr_length =sizeof (DEFAULT_ATR ); }
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH v2 5/9] ccid-card-passthru: Replace assert() by QEMU_BUILD_BUG_ON()
Quoted 2 lines · Philippe Mathieu-Daudé
Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 42 lines
--- hw/usb/ccid-card-passthru.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)hw/usb/ccid-card-passthru.c +5
-5@@ -29,6 +29,9 @@ do { \#define D_MORE_INFO 3#define D_VERBOSE 4 +/* maximum size of ATR - from 7816-3 */ +#define MAX_ATR_SIZE 40 +/* TODO: do we still need this? */ static const uint8_t DEFAULT_ATR [] = {/* @@ -41,10 +44,9 @@ static const uint8_t DEFAULT_ATR[] = {0x13 ,0x08 }; -#define VSCARD_IN_SIZE (64 * KiB) +QEMU_BUILD_BUG_ON (sizeof (DEFAULT_ATR ) >MAX_ATR_SIZE ); -/* maximum size of ATR - from 7816-3 */ -#define MAX_ATR_SIZE 40 +#define VSCARD_IN_SIZE (64 * KiB)typedef struct PassthruState PassthruState ;@@ -351,7 +353,6 @@ static void passthru_realize(CCIDCardState *base, Error **errp) } card->debug =parse_debug_env ("QEMU_CCID_PASSTHRU_DEBUG" ,D_VERBOSE , card->debug); -assert (sizeof (DEFAULT_ATR ) <=MAX_ATR_SIZE );memcpy (card->atr,DEFAULT_ATR ,sizeof (DEFAULT_ATR )); card->atr_length =sizeof (DEFAULT_ATR ); } --2.20.1
PATCH v2 6/9 ccid-card-passthru: Simplify the if() condition
In reply to Philippe Mathieu-DaudéSimplify the if() condition so we can remove an indent layer and the code is easier to review.
--- hw/usb/ccid-card-passthru.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)
hw/usb/ccid-card-passthru.c +8 -10
@@ -338,19 +338,17 @@ static void passthru_realize(CCIDCardState *base, Error **errp) {PassthruState *card =PASSTHRU_CCID_CARD (base); - card->vscard_in_pos =0 ; - card->vscard_in_hdr =0 ; -if (qemu_chr_fe_backend_connected (&card->cs)) { -DPRINTF (card,D_INFO ,"ccid-card-passthru: initing chardev" ); -qemu_chr_fe_set_handlers (&card->cs, - ccid_card_vscard_can_read, - ccid_card_vscard_read, - ccid_card_vscard_event,NULL , card,NULL , true); -ccid_card_vscard_send_init (card); - }else { +if (!qemu_chr_fe_backend_connected (&card->cs)) {error_setg (errp,"missing chardev" );return ; } + card->vscard_in_pos =0 ; + card->vscard_in_hdr =0 ; +DPRINTF (card,D_INFO ,"ccid-card-passthru: initing chardev" ); +qemu_chr_fe_set_handlers (&card->cs, ccid_card_vscard_can_read, + ccid_card_vscard_read, ccid_card_vscard_event, +NULL , card,NULL , true); +ccid_card_vscard_send_init (card); card->debug =parse_debug_env ("QEMU_CCID_PASSTHRU_DEBUG" ,D_VERBOSE , card->debug);memcpy (card->atr,DEFAULT_ATR ,sizeof (DEFAULT_ATR ));
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH v2 6/9] ccid-card-passthru: Simplify the if() condition
Hi
Quoted 5 lines · Philippe Mathieu-Daudé
Simplify the
if()condition so we can remove an indent layer and the code is easier to review.Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 9 lines
--- hw/usb/ccid-card-passthru.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)hw/usb/ccid-card-passthru.c +0
-0
hw/usb/ccid-card-passthru.c static void passthru_realize(CCIDCardState *base, Error **errp) · line 342
Quoted 5 lines
hw/usb/ccid-card-passthru.c +0
-2@@ -338,19 +338,17 @@ static void passthru_realize(CCIDCardState *base, Error **errp) {PassthruState *card =PASSTHRU_CCID_CARD (base); - card->vscard_in_pos =0 ; - card->vscard_in_hdr =0 ;
The 0 assignment are needless too (objects are memset 0), but not a problem either.
Quoted 25 lines
hw/usb/ccid-card-passthru.c +8
-9-if (qemu_chr_fe_backend_connected (&card->cs)) { -DPRINTF (card,D_INFO ,"ccid-card-passthru: initing chardev" ); -qemu_chr_fe_set_handlers (&card->cs, - ccid_card_vscard_can_read, - ccid_card_vscard_read, - ccid_card_vscard_event,NULL , card,NULL , true); -ccid_card_vscard_send_init (card); - }else { +if (!qemu_chr_fe_backend_connected (&card->cs)) {error_setg (errp,"missing chardev" );return ; } + card->vscard_in_pos =0 ; + card->vscard_in_hdr =0 ; +DPRINTF (card,D_INFO ,"ccid-card-passthru: initing chardev" ); +qemu_chr_fe_set_handlers (&card->cs, ccid_card_vscard_can_read, + ccid_card_vscard_read, ccid_card_vscard_event, +NULL , card,NULL , true); +ccid_card_vscard_send_init (card); card->debug =parse_debug_env ("QEMU_CCID_PASSTHRU_DEBUG" ,D_VERBOSE , card->debug);memcpy (card->atr,DEFAULT_ATR ,sizeof (DEFAULT_ATR )); --2.20.1
PATCH v2 7/9 ccid-card-passthru: Use QERR_MISSING_PARAMETER
In reply to Philippe Mathieu-Daudé--- hw/usb/ccid-card-passthru.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/usb/ccid-card-passthru.c +2 -1
@@ -16,6 +16,7 @@#include "qemu/sockets.h" #include "ccid.h" #include "qapi/error.h" +#include "qapi/qmp/qerror.h" #define DPRINTF (card, lvl, fmt, ...) \ do { \
@@ -339,7 +340,7 @@ static void passthru_realize(CCIDCardState *base, Error **errp)PassthruState *card =PASSTHRU_CCID_CARD (base);if (!qemu_chr_fe_backend_connected (&card->cs)) { -error_setg (errp,"missing chardev" ); +error_setg (errp,QERR_MISSING_PARAMETER ,"chardev" );return ; } card->vscard_in_pos =0 ;
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH v2 7/9] ccid-card-passthru: Use QERR_MISSING_PARAMETER
Quoted 18 lines · Philippe Mathieu-Daudé
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/usb/ccid-card-passthru.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)hw/usb/ccid-card-passthru.c +1
-0@@ -16,6 +16,7 @@#include "qemu/sockets.h" #include "ccid.h" #include "qapi/error.h" +#include "qapi/qmp/qerror.h" #define DPRINTF (card, lvl, fmt, ...) \ do { \
hw/usb/ccid-card-passthru.c static void passthru_realize(CCIDCardState *base, Error **errp) · line 343
Quoted 5 lines
hw/usb/ccid-card-passthru.c +1
-1@@ -339,7 +340,7 @@ static void passthru_realize(CCIDCardState *base, Error **errp)PassthruState *card =PASSTHRU_CCID_CARD (base);if (!qemu_chr_fe_backend_connected (&card->cs)) { -error_setg (errp,"missing chardev" ); +error_setg (errp,QERR_MISSING_PARAMETER ,"chardev" );
We're trying to get rid of QERR_ usage, not add more. But this particular one seems to be a wash:
$ git grep '"missing ' | wc -l 24 $ git grep QERR_MISSING_P | wc -l 25
so I'd be interested if Markus has an opinion on this one.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
PATCH v2 8/9 ccid-card-passthru: Use size_t to hold size argument
In reply to Philippe Mathieu-Daudécheck_atr() is called once with a unsigned argument. Since there is no need to use a signed type, use a size_t.
--- hw/usb/ccid-card-passthru.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
hw/usb/ccid-card-passthru.c +6 -5
@@ -149,9 +149,10 @@ static void ccid_card_vscard_handle_init(ccid_card_vscard_send_init (card); } -static int check_atr (PassthruState *card,uint8_t *data,int len) +static int check_atr (PassthruState *card,uint8_t *data,size_t len) { -int historical_length, opt_bytes; +size_t historical_length; +int opt_bytes;int td_count =0 ;int td;
@@ -185,18 +186,18 @@ static int check_atr(PassthruState *card, uint8_t *data, int len) }if (len <2 + historical_length + opt_bytes) {DPRINTF (card,D_WARN , -"atr too short: len %d, but historical_len %d, T1 0x%X\n" , +"atr too short: len %zu, but historical_len %zu, T1 0x%X\n" , len, historical_length, data[1 ]);return 0 ; }if (len >2 + historical_length + opt_bytes) {DPRINTF (card,D_WARN , -"atr too long: len %d, but hist/opt %d/%d, T1 0x%X\n" , +"atr too long: len %zu, but hist/opt %zu/%d, T1 0x%X\n" , len, historical_length, opt_bytes, data[1 ]);/* let it through */ }DPRINTF (card,D_VERBOSE , -"atr passes check: %d total length, %d historical, %d optional\n" , +"atr passes check: %zu total length, %zu historical, %d optional\n" , len, historical_length, opt_bytes);return 1 ;
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH v2 8/9] ccid-card-passthru: Use size_t to hold size argument
Hi
Quoted 5 lines · Philippe Mathieu-Daudé
check_atr()is called once with a unsigned argument. Since there is no need to use a signed type, use asize_t.Signed-off-by Philippe Mathieu-Daudé <[email protected]>
and make data const?
Quoted 47 lines
--- hw/usb/ccid-card-passthru.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)hw/usb/ccid-card-passthru.c +6
-6@@ -149,9 +149,10 @@ static void ccid_card_vscard_handle_init(ccid_card_vscard_send_init (card); } -static int check_atr (PassthruState *card,uint8_t *data,int len) +static int check_atr (PassthruState *card,uint8_t *data,size_t len) { -int historical_length, opt_bytes; +size_t historical_length; +int opt_bytes;int td_count =0 ;int td;@@ -185,18 +186,18 @@ static int check_atr(PassthruState *card, uint8_t *data, int len) }if (len <2 + historical_length + opt_bytes) {DPRINTF (card,D_WARN , -"atr too short: len %d, but historical_len %d, T1 0x%X\n" , +"atr too short: len %zu, but historical_len %zu, T1 0x%X\n" , len, historical_length, data[1 ]);return 0 ; }if (len >2 + historical_length + opt_bytes) {DPRINTF (card,D_WARN , -"atr too long: len %d, but hist/opt %d/%d, T1 0x%X\n" , +"atr too long: len %zu, but hist/opt %zu/%d, T1 0x%X\n" , len, historical_length, opt_bytes, data[1 ]);/* let it through */ }DPRINTF (card,D_VERBOSE , -"atr passes check: %d total length, %d historical, %d optional\n" , +"atr passes check: %zu total length, %zu historical, %d optional\n" , len, historical_length, opt_bytes);return 1 ; --2.20.1
Signature · 1 line
Marc-André Lureau
PATCH v2 9/9 ccid-card-passthru: Use size_t for index
In reply to Philippe Mathieu-DaudéThe variable 'opt_bytes' is an index to the data[] array. Use size_t for indexes.
--- hw/usb/ccid-card-passthru.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
hw/usb/ccid-card-passthru.c +4 -3
@@ -152,7 +152,7 @@ static void ccid_card_vscard_handle_init(static int check_atr (PassthruState *card,uint8_t *data,size_t len) {size_t historical_length; -int opt_bytes; +size_t opt_bytes;int td_count =0 ;int td;
@@ -192,12 +192,13 @@ static int check_atr(PassthruState *card, uint8_t *data, size_t len) }if (len >2 + historical_length + opt_bytes) {DPRINTF (card,D_WARN , -"atr too long: len %zu, but hist/opt %zu/%d, T1 0x%X\n" , +"atr too long: len %zu, but hist/opt %zu/%zu, T1 0x%X\n" , len, historical_length, opt_bytes, data[1 ]);/* let it through */ }DPRINTF (card,D_VERBOSE , -"atr passes check: %zu total length, %zu historical, %d optional\n" , +"atr passes check: %zu total length," +" %zu historical, %zu optional\n" , len, historical_length, opt_bytes);return 1 ;
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH v2 9/9] ccid-card-passthru: Use size_t for index
Quoted 5 lines · Philippe Mathieu-Daudé
The variable '
opt_bytes' is an index to the data[] array. Usesize_tfor indexes.Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 37 lines
--- hw/usb/ccid-card-passthru.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)hw/usb/ccid-card-passthru.c +4
-4@@ -152,7 +152,7 @@ static void ccid_card_vscard_handle_init(static int check_atr (PassthruState *card,uint8_t *data,size_t len) {size_t historical_length; -int opt_bytes; +size_t opt_bytes;int td_count =0 ;int td;@@ -192,12 +192,13 @@ static int check_atr(PassthruState *card, uint8_t *data, size_t len) }if (len >2 + historical_length + opt_bytes) {DPRINTF (card,D_WARN , -"atr too long: len %zu, but hist/opt %zu/%d, T1 0x%X\n" , +"atr too long: len %zu, but hist/opt %zu/%zu, T1 0x%X\n" , len, historical_length, opt_bytes, data[1 ]);/* let it through */ }DPRINTF (card,D_VERBOSE , -"atr passes check: %zu total length, %zu historical, %d optional\n" , +"atr passes check: %zu total length," +" %zu historical, %zu optional\n" , len, historical_length, opt_bytes);return 1 ; --2.20.1
Signature · 1 line
Marc-André Lureau