On 12/11/26 14:34, Denis V. Lunev wrote:
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;
The range is checked using unsigned 32-bit arithmetic, so we need to
do the addition in 64 bits to avoid wrapping at 2^32. Simply casting:

-- >8 --
diff --git a/hw/ide/atapi.c b/hw/ide/atapi.c
index 2c928de8db8f..bc18654522d0 100644
--- a/hw/ide/atapi.c
+++ b/hw/ide/atapi.c
@@ -983,7 +983,8 @@ static void cmd_read(IDEState *s, uint8_t* buf)
     }

     lba = ldl_be_p(buf + 2);
-    if (lba >= total_sectors || lba + nb_sectors - 1 >= total_sectors) {
+    if (lba >= total_sectors ||
+        (uint64_t)lba + nb_sectors - 1 >= total_sectors) {
         ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
         return;
     }
@@ -1005,7 +1006,8 @@ static void cmd_read_cd(IDEState *s, uint8_t* buf)
     }

     lba = ldl_be_p(buf + 2);
-    if (lba >= total_sectors || lba + nb_sectors - 1 >= total_sectors) {
+    if (lba >= total_sectors ||
+        (uint64_t)lba + nb_sectors - 1 >= total_sectors) {
         ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
         return;
     }
---

Or nicer comparing against the remaining sectors instead:

-- >8 --
diff --git a/hw/ide/atapi.c b/hw/ide/atapi.c
index 2c928de8db8f..aec20f968da0 100644
--- a/hw/ide/atapi.c
+++ b/hw/ide/atapi.c
@@ -983,7 +983,7 @@ static void cmd_read(IDEState *s, uint8_t* buf)
     }

     lba = ldl_be_p(buf + 2);
-    if (lba >= total_sectors || lba + nb_sectors - 1 >= total_sectors) {
+    if (lba >= total_sectors || nb_sectors > total_sectors - lba) {
         ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
         return;
     }
@@ -1005,7 +1005,7 @@ static void cmd_read_cd(IDEState *s, uint8_t* buf)
     }

     lba = ldl_be_p(buf + 2);
-    if (lba >= total_sectors || lba + nb_sectors - 1 >= total_sectors) {
+    if (lba >= total_sectors || nb_sectors > total_sectors - lba) {
         ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
         return;
     }
---

Reviewed-by: Philippe Mathieu-Daudé <[email protected]>


Reply via email to