From: Denis V. Lunev <[email protected]>

cmd_read() and cmd_read_cd() check the 32-bit LBA from the CDB against
the medium size, then pass it to ide_atapi_cmd_read() as an int. On a
medium larger than 4 TiB, a read at LBA 2^31 or above passes the range
check, turns negative and trips the assertion:

  hw/ide/atapi.c:315: ide_atapi_cmd_read_pio: Assertion
  `0 <= lba && lba < (s->nb_sectors >> 2)' failed.

Reading the last sector is enough, e.g. a UDF anchor probe, so the
guest aborts QEMU just by looking at the disc.

Pass the LBA as uint32_t, which is what the CDB carries, and widen
IDEState.lba to int64_t so it holds every valid LBA and keeps -1 as
the "not a sector read" marker. The field is not migrated.

Fixes: b8d7f1bc5927 ("ide: atapi: check logical block address and read size 
(CVE-2020-29443)")
Signed-off-by: Denis V. Lunev <[email protected]>
Cc: John Snow <[email protected]>
Cc: Fabiano Rosas <[email protected]>
Cc: Laurent Vivier <[email protected]>
---
 hw/ide/atapi.c           | 18 +++++++++---------
 hw/ide/macio.c           |  2 +-
 hw/ide/trace-events      |  8 ++++----
 include/hw/ide/ide-dev.h |  2 +-
 tests/qtest/ahci-test.c  | 36 ++++++++++++++++++++++++++++++++++++
 5 files changed, 51 insertions(+), 15 deletions(-)

diff --git a/hw/ide/atapi.c b/hw/ide/atapi.c
index 0ea149ad8c6..2c928de8db8 100644
--- a/hw/ide/atapi.c
+++ b/hw/ide/atapi.c
@@ -47,7 +47,7 @@ static void padstr8(uint8_t *buf, int buf_size, const char 
*src)
     }
 }
 
-static void lba_to_msf(uint8_t *buf, int lba)
+static void lba_to_msf(uint8_t *buf, int64_t lba)
 {
     lba += 150;
     buf[0] = (lba / 75) / 60;
@@ -71,7 +71,7 @@ static inline int media_is_cd(IDEState *s)
     return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
 }
 
-static void cd_data_to_raw(uint8_t *buf, int lba)
+static void cd_data_to_raw(uint8_t *buf, int64_t lba)
 {
     /* sync bytes */
     buf[0] = 0x00;
@@ -309,10 +309,10 @@ static void ide_atapi_cmd_reply(IDEState *s, int size, 
int max_size)
 }
 
 /* start a CD-ROM read command */
-static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
-                                   int sector_size)
+static void ide_atapi_cmd_read_pio(IDEState *s, uint32_t lba,
+                                   int nb_sectors, int sector_size)
 {
-    assert(0 <= lba && lba < (s->nb_sectors >> 2));
+    assert(lba < (s->nb_sectors >> 2));
 
     s->lba = lba;
     s->packet_transfer_size = nb_sectors * sector_size;
@@ -409,10 +409,10 @@ eot:
 
 /* start a CD-ROM read command with DMA */
 /* XXX: test if DMA is available */
-static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
-                                   int sector_size)
+static void ide_atapi_cmd_read_dma(IDEState *s, uint32_t lba,
+                                   int nb_sectors, int sector_size)
 {
-    assert(0 <= lba && lba < (s->nb_sectors >> 2));
+    assert(lba < (s->nb_sectors >> 2));
 
     s->lba = lba;
     s->packet_transfer_size = nb_sectors * sector_size;
@@ -427,7 +427,7 @@ static void ide_atapi_cmd_read_dma(IDEState *s, int lba, 
int nb_sectors,
     ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
 }
 
-static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
+static void ide_atapi_cmd_read(IDEState *s, uint32_t lba, int nb_sectors,
                                int sector_size)
 {
     trace_ide_atapi_cmd_read(s, s->atapi_dma ? "dma" : "pio",
diff --git a/hw/ide/macio.c b/hw/ide/macio.c
index a95e68083f4..6f62d018849 100644
--- a/hw/ide/macio.c
+++ b/hw/ide/macio.c
@@ -396,7 +396,7 @@ static void ide_dbdma_start(const IDEDMA *dma, IDEState *s,
     MACIO_DPRINTF("\n\n------------ IDE transfer\n");
     MACIO_DPRINTF("buffer_size: %x   buffer_index: %x\n",
                   s->io_buffer_size, s->io_buffer_index);
-    MACIO_DPRINTF("lba: %x    size: %x\n", s->lba, s->io_buffer_size);
+    MACIO_DPRINTF("lba: %" PRIx64 "    size: %x\n", s->lba, s->io_buffer_size);
     MACIO_DPRINTF("-------------------------\n");
 
     m->dma_active = true;
diff --git a/hw/ide/trace-events b/hw/ide/trace-events
index c44eec6c585..dc34b09c613 100644
--- a/hw/ide/trace-events
+++ b/hw/ide/trace-events
@@ -52,17 +52,17 @@ bmdma_write_via(uint64_t addr, uint64_t val) "bmdma: writeb 
0x%"PRIx64" : 0x%02"
 
 # atapi.c
 cd_read_sector_sync(int lba) "lba=%d"
-cd_read_sector_cb(int lba, int ret) "lba=%d ret=%d"
-cd_read_sector(int lba) "lba=%d"
+cd_read_sector_cb(int64_t lba, int ret) "lba=%"PRId64" ret=%d"
+cd_read_sector(int64_t lba) "lba=%"PRId64
 ide_atapi_cmd_error(void *s, int sense_key, int asc) "IDEState: %p; sense=0x%x 
asc=0x%x"
 ide_atapi_cmd_reply_end(void *s, int tx_size, int elem_tx_size, int32_t index) 
"IDEState %p; reply: tx_size=%d elem_tx_size=%d index=%"PRId32
 ide_atapi_cmd_reply_end_eot(void *s, int status) "IDEState: %p; end of 
transfer, status=0x%x"
 ide_atapi_cmd_reply_end_bcl(void *s, int bcl) "IDEState: %p; 
byte_count_limit=%d"
 ide_atapi_cmd_reply_end_new(void *s, int status) "IDEState: %p; new transfer 
started, status=0x%x"
 ide_atapi_cmd_check_status(void *s) "IDEState: %p"
-ide_atapi_cmd_read(void *s, const char *method, int lba, int nb_sectors) 
"IDEState: %p; read %s: LBA=%d nb_sectors=%d"
+ide_atapi_cmd_read(void *s, const char *method, uint32_t lba, int nb_sectors) 
"IDEState: %p; read %s: LBA=%"PRIu32" nb_sectors=%d"
 ide_atapi_cmd(void *s, uint8_t cmd) "IDEState: %p; cmd: 0x%02x"
-ide_atapi_cmd_read_dma_cb_aio(void *s, int lba, int n) "IDEState: %p; aio 
read: lba=%d n=%d"
+ide_atapi_cmd_read_dma_cb_aio(void *s, int64_t lba, int n) "IDEState: %p; aio 
read: lba=%"PRId64" n=%d"
 # Warning: Verbose
 ide_atapi_cmd_packet(void *s, uint16_t limit, const char *packet) "IDEState: 
%p; limit=0x%x packet: %s"
 
diff --git a/include/hw/ide/ide-dev.h b/include/hw/ide/ide-dev.h
index f5eaaf68e65..c6fd8d7eb1f 100644
--- a/include/hw/ide/ide-dev.h
+++ b/include/hw/ide/ide-dev.h
@@ -101,7 +101,7 @@ struct IDEState {
     int packet_transfer_size;
     int elementary_transfer_size;
     int32_t io_buffer_index;
-    int lba;
+    int64_t lba;
     int cd_sector_size;
     int atapi_dma; /* true if dma is requested for the packet cmd */
     BlockAcctCookie acct;
diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index 291f29e762c..37f6dee0a7f 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -1737,6 +1737,40 @@ static void test_cdrom_pio_raw(void)
     ahci_test_cdrom_raw(3, false);
 }
 
+/* Regression test: read the last sector of a medium over 4 TiB. */
+static void ahci_test_cdrom_high_lba(bool dma)
+{
+    AHCIQState *ahci;
+    g_autofree unsigned char *tx = g_malloc0(ATAPI_SECTOR_SIZE);
+    uint64_t lba = (uint64_t)INT32_MAX + 1;
+    AHCIOpts opts = {
+        .size = ATAPI_SECTOR_SIZE,
+        .lba = lba,
+        .atapi = true,
+        .atapi_dma = dma,
+        .post_cb = ahci_cb_cmp_buff,
+        .opaque = tx,
+    };
+
+    ahci = ahci_boot_and_enable("-blockdev driver=null-co,node-name=cd0,"
+                                "read-only=on,read-zeroes=on,size=%" PRIu64 " "
+                                "-M q35 "
+                                "-device ide-cd,drive=cd0 ",
+                                (lba + 1) * ATAPI_SECTOR_SIZE);
+    ahci_exec(ahci, ahci_port_select(ahci), CMD_ATAPI_READ_10, &opts);
+    ahci_shutdown(ahci);
+}
+
+static void test_cdrom_dma_high_lba(void)
+{
+    ahci_test_cdrom_high_lba(true);
+}
+
+static void test_cdrom_pio_high_lba(void)
+{
+    ahci_test_cdrom_high_lba(false);
+}
+
 /*
  * Regression test: a buffered ATAPI read completing after a command
  * engine restart must not dereference the cleared cur_cmd. Cover both
@@ -2427,6 +2461,8 @@ int main(int argc, char **argv)
     qtest_add_func("/ahci/cdrom/pio/single", test_cdrom_pio);
     qtest_add_func("/ahci/cdrom/pio/multi", test_cdrom_pio_multi);
     qtest_add_func("/ahci/cdrom/pio/raw", test_cdrom_pio_raw);
+    qtest_add_func("/ahci/cdrom/dma/high_lba", test_cdrom_dma_high_lba);
+    qtest_add_func("/ahci/cdrom/pio/high_lba", test_cdrom_pio_high_lba);
 
     qtest_add_func("/ahci/cdrom/pio/bcl", test_atapi_bcl);
     qtest_add_func("/ahci/cdrom/eject", test_atapi_tray);
-- 
2.53.0


Reply via email to