hw/sd: sdhci: Fixes to CVE-2020-17380, CVE-2020-25085, CVE-2021-3409
v3 cover hw/sd: sdhci: Fixes to CVE-2020-17380, CVE-2020-25085, CVE-2021-3409
This series includes several fixes to CVE-2020-17380, CVE-2020-25085 and CVE-2021-3409 that are heap-based buffer overflow issues existing in the sdhci model.
These CVEs are pretty much similar, and were filed using different reproducers. With this series, current known reproducers I have cannot be reproduced any more.
The implementation of this model still has some issues, e.g.: some codes do not seem to strictly match the spec, but since this series only aimes to address CVEs, such issue should be fixed in a separate series in the future, if I have time :)
Changes in v3:
- Embed the reproducer in the commit message
Changes in v2:
- new patch: sdhci: Limit block size only when SDHC_BLKSIZE register is writable
- new patch: sdhci: Reset the data pointer of s->fifo_buffer[] when a different block size is programmed
Bin Meng (5):
hw/sd: sdhci: Don't transfer any data when command time out hw/sd: sdhci: Don't write to SDHC_SYSAD register when transfer is in
progress
hw/sd: sdhci: Correctly set the controller status for ADMA hw/sd: sdhci: Limit block size only when SDHC_BLKSIZE register is
writable
hw/sd: sdhci: Reset the data pointer of s->fifo_buffer[] when a
different block size is programmed
hw/sd/sdhci.c | 53 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 17 deletions(-)
Signature · 1 line
2.7.4
PATCH v3 1/5 hw/sd: sdhci: Don't transfer any data when command time out
At the end of sdhci_send_command(), it starts a data transfer if the command register indicates data is associated. But the data transfer should only be initiated when the command execution has succeeded.
With this fix, the following reproducer:
outl 0xcf8 0x80001810
outl 0xcfc 0xe1068000
outl 0xcf8 0x80001804
outw 0xcfc 0x7
write 0xe106802c 0x1 0x0f
write 0xe1068004 0xc 0x2801d10101fffffbff28a384
write 0xe106800c 0x1f 0x9dacbbcad9e8f7061524334251606f7e8d9cabbac9d8e7f60514233241505f write 0xe1068003 0x28 0x80d000251480d000252280d000253080d000253e80d000254c80d000255a80d000256880d0002576 write 0xe1068003 0x1 0xfe
cannot be reproduced with the following QEMU command line:
$ qemu-system-x86_64 -nographic -M pc-q35-5.0 \
-device sdhci-pci,sd-spec-version=3 \
-drive if=sd,index=0,file=null-co://,format=raw,id=mydrive \
-device sd-card,drive=mydrive \
-monitor none -serial none -qtest stdio---
(no changes since v1)
hw/sd/sdhci.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
hw/sd/sdhci.c +3 -1
@@ -326,6 +326,7 @@ static void sdhci_send_command(SDHCIState *s)SDRequest request;uint8_t response[16 ];int rlen; +bool timeout = false; s->errintsts =0 ; s->acmd12errsts =0 ;
@@ -349,6 +350,7 @@ static void sdhci_send_command(SDHCIState *s)trace_sdhci_response16 (s->rspreg[3 ], s->rspreg[2 ], s->rspreg[1 ], s->rspreg[0 ]); }else { + timeout = true;trace_sdhci_error ("timeout waiting for command response" );if (s->errintstsen &SDHC_EISEN_CMDTIMEOUT ) { s->errintsts |=SDHC_EIS_CMDTIMEOUT ;
@@ -369,7 +371,7 @@ static void sdhci_send_command(SDHCIState *s)sdhci_update_irq (s); -if (s->blksize && (s->cmdreg &SDHC_CMD_DATA_PRESENT )) { +if (!timeout && s->blksize && (s->cmdreg &SDHC_CMD_DATA_PRESENT )) { s->data_count =0 ;sdhci_data_transfer (s); }
Signature · 1 line
2.7.4
PATCH v3 2/5 hw/sd: sdhci: Don't write to SDHC_SYSAD register when transfer is in progress
In reply to Bin MengPer "SD Host Controller Standard Specification Version 7.00" chapter 2.2.1 SDMA System Address Register:
This register can be accessed only if no transaction is executing (i.e., after a transaction has stopped).
With this fix, the following reproducer:
outl 0xcf8 0x80001010
outl 0xcfc 0xfbefff00
outl 0xcf8 0x80001001
outl 0xcfc 0x06000000
write 0xfbefff2c 0x1 0x05
write 0xfbefff0f 0x1 0x37
write 0xfbefff0a 0x1 0x01
write 0xfbefff0f 0x1 0x29
write 0xfbefff0f 0x1 0x02
write 0xfbefff0f 0x1 0x03
write 0xfbefff04 0x1 0x01
write 0xfbefff05 0x1 0x01
write 0xfbefff07 0x1 0x02
write 0xfbefff0c 0x1 0x33
write 0xfbefff0e 0x1 0x20
write 0xfbefff0f 0x1 0x00
write 0xfbefff2a 0x1 0x01
write 0xfbefff0c 0x1 0x00
write 0xfbefff03 0x1 0x00
write 0xfbefff05 0x1 0x00
write 0xfbefff2a 0x1 0x02
write 0xfbefff0c 0x1 0x32
write 0xfbefff01 0x1 0x01
write 0xfbefff02 0x1 0x01
write 0xfbefff03 0x1 0x01
cannot be reproduced with the following QEMU command line:
$ qemu-system-x86_64 -nographic -machine accel=qtest -m 512M \
-nodefaults -device sdhci-pci,sd-spec-version=3 \
-drive if=sd,index=0,file=null-co://,format=raw,id=mydrive \
-device sd-card,drive=mydrive -qtest stdio---
Changes in v3:
- Embed the reproducer in the commit message
hw/sd/sdhci.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
hw/sd/sdhci.c +11 -9
@@ -1121,15 +1121,17 @@ sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)switch (offset & ~0x3 ) {case SDHC_SYSAD : - s->sdmasysad = (s->sdmasysad & mask) | value; -MASKED_WRITE (s->sdmasysad, mask, value); -/* Writing to last byte of sdmasysad might trigger transfer */ -if (!(mask &0xFF000000 ) &&TRANSFERRING_DATA (s->prnsts) && s->blkcnt && - s->blksize &&SDHC_DMA_TYPE (s->hostctl1) ==SDHC_CTRL_SDMA ) { -if (s->trnmod &SDHC_TRNS_MULTI ) { -sdhci_sdma_transfer_multi_blocks (s); - }else { -sdhci_sdma_transfer_single_block (s); +if (!TRANSFERRING_DATA (s->prnsts)) { + s->sdmasysad = (s->sdmasysad & mask) | value; +MASKED_WRITE (s->sdmasysad, mask, value); +/* Writing to last byte of sdmasysad might trigger transfer */ +if (!(mask &0xFF000000 ) && s->blkcnt && s->blksize && +SDHC_DMA_TYPE (s->hostctl1) ==SDHC_CTRL_SDMA ) { +if (s->trnmod &SDHC_TRNS_MULTI ) { +sdhci_sdma_transfer_multi_blocks (s); + }else { +sdhci_sdma_transfer_single_block (s); + } } }break ;
Signature · 1 line
2.7.4
PATCH v3 3/5 hw/sd: sdhci: Correctly set the controller status for ADMA
In reply to Bin MengWhen an ADMA transfer is started, the codes forget to set the controller status to indicate a transfer is in progress.
With this fix, the following 2 reproducers:
https://paste.debian.net/plain/1185136
https://paste.debian.net/plain/1185141
cannot be reproduced with the following QEMU command line:
$ qemu-system-x86_64 -nographic -machine accel=qtest -m 512M \
-nodefaults -device sdhci-pci,sd-spec-version=3 \
-drive if=sd,index=0,file=null-co://,format=raw,id=mydrive \
-device sd-card,drive=mydrive -qtest stdio---
(no changes since v1)
hw/sd/sdhci.c | 3 +++ 1 file changed, 3 insertions(+)
hw/sd/sdhci.c +3 -0
@@ -768,7 +768,9 @@ static void sdhci_do_adma(SDHCIState *s)switch (dscr.attr &SDHC_ADMA_ATTR_ACT_MASK ) {case SDHC_ADMA_ATTR_ACT_TRAN :/* data transfer */ + s->prnsts |=SDHC_DATA_INHIBIT |SDHC_DAT_LINE_ACTIVE ;if (s->trnmod &SDHC_TRNS_READ ) { + s->prnsts |=SDHC_DOING_READ ;while (length) {if (s->data_count ==0 ) {sdbus_read_data (&s->sdbus, s->fifo_buffer, block_size);
@@ -796,6 +798,7 @@ static void sdhci_do_adma(SDHCIState *s) } } }else { + s->prnsts |=SDHC_DOING_WRITE ;while (length) { begin = s->data_count;if ((length + begin) < block_size) {
Signature · 1 line
2.7.4
PATCH v3 4/5 hw/sd: sdhci: Limit block size only when SDHC_BLKSIZE register is writable
In reply to Bin MengThe codes to limit the maximum block size is only necessary when SDHC_BLKSIZE register is writable.
---
(no changes since v2)
Changes in v2:
- new patch: sdhci: Limit block size only when SDHC_BLKSIZE register is writable
hw/sd/sdhci.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
hw/sd/sdhci.c +7 -7
@@ -1142,15 +1142,15 @@ sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)if (!TRANSFERRING_DATA (s->prnsts)) {MASKED_WRITE (s->blksize, mask,extract32 (value,0 ,12 ));MASKED_WRITE (s->blkcnt, mask >>16 , value >>16 ); - } -/* Limit block size to the maximum buffer size */ -if (extract32 (s->blksize,0 ,12 ) > s->buf_maxsz) { -qemu_log_mask (LOG_GUEST_ERROR ,"%s: Size 0x%x is larger than " -"the maximum buffer 0x%x\n" , __func__, s->blksize, - s->buf_maxsz); +/* Limit block size to the maximum buffer size */ +if (extract32 (s->blksize,0 ,12 ) > s->buf_maxsz) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Size 0x%x is larger than " +"the maximum buffer 0x%x\n" , __func__, s->blksize, + s->buf_maxsz); - s->blksize =deposit32 (s->blksize,0 ,12 , s->buf_maxsz); + s->blksize =deposit32 (s->blksize,0 ,12 , s->buf_maxsz); + } }break ;
Signature · 1 line
2.7.4
PATCH v3 5/5 hw/sd: sdhci: Reset the data pointer of s->fifo_buffer[] when a different block size is programmed
In reply to Bin MengIf the block size is programmed to a different value from the previous one, reset the data pointer of s->fifo_buffer[] so that s->fifo_buffer[] can be filled in using the new block size in the next transfer.
With this fix, the following reproducer:
outl 0xcf8 0x80001010
outl 0xcfc 0xe0000000
outl 0xcf8 0x80001001
outl 0xcfc 0x06000000
write 0xe000002c 0x1 0x05
write 0xe0000005 0x1 0x02
write 0xe0000007 0x1 0x01
write 0xe0000028 0x1 0x10
write 0x0 0x1 0x23
write 0x2 0x1 0x08
write 0xe000000c 0x1 0x01
write 0xe000000e 0x1 0x20
write 0xe000000f 0x1 0x00
write 0xe000000c 0x1 0x32
write 0xe0000004 0x2 0x0200
write 0xe0000028 0x1 0x00
write 0xe0000003 0x1 0x40
cannot be reproduced with the following QEMU command line:
$ qemu-system-x86_64 -nographic -machine accel=qtest -m 512M \
-nodefaults -device sdhci-pci,sd-spec-version=3 \
-drive if=sd,index=0,file=null-co://,format=raw,id=mydrive \
-device sd-card,drive=mydrive -qtest stdio---
(no changes since v2)
Changes in v2:
- new patch: sdhci: Reset the data pointer of s->fifo_buffer[] when a different block size is programmed
hw/sd/sdhci.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
hw/sd/sdhci.c +12 -0
@@ -1140,6 +1140,8 @@ sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)break ;case SDHC_BLKSIZE :if (!TRANSFERRING_DATA (s->prnsts)) { +uint16_t blksize = s->blksize; +MASKED_WRITE (s->blksize, mask,extract32 (value,0 ,12 ));MASKED_WRITE (s->blkcnt, mask >>16 , value >>16 );
@@ -1151,6 +1153,16 @@ sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size) s->blksize =deposit32 (s->blksize,0 ,12 , s->buf_maxsz); } + +/* +* If the block size is programmed to a different value from +* the previous one, reset the data pointer of s->fifo_buffer[] +* so that s->fifo_buffer[] can be filled in using the new block +* size in the next transfer. +*/ +if (blksize != s->blksize) { + s->data_count =0 ; + } }break ;
Signature · 1 line
2.7.4