All Virtuozzo development lists (kernel + QEMU)
 help / color / mirror / Atom feed
From: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
To: "Denis V. Lunev" <den@openvz.org>, svt-core@virtuozzo.com
Subject: Re: [PATCH hci-dev 1/1] hw/ide/atapi: fix abort on CD-ROM reads past LBA 2^31 #VSTOR-154795
Date: Thu, 12 Nov 2026 19:12:46 +0200	[thread overview]
Message-ID: <4b94b1ff-59d1-4b08-8a25-428790a367b8@virtuozzo.com> (raw)
In-Reply-To: <20261112134758.646697-2-den@openvz.org>

Ack
Applied to hci-dev

On 11/12/26 3:47 PM, Denis V. Lunev wrote:
> From: Denis V. Lunev <den@openvz.org>
> 
> 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 <den@openvz.org>
> Cc: John Snow <jsnow@redhat.com>
> Cc: Fabiano Rosas <farosas@suse.de>
> Cc: Laurent Vivier <lvivier@redhat.com>
> ---
>  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 a31b945a66a..1fa73041938 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;
> @@ -318,10 +318,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;
> @@ -423,10 +423,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;
> @@ -441,7 +441,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 c23bf32d2b1..b539a31f1ce 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 ca696ebd6d7..c0aaeb15a03 100644
> --- a/hw/ide/trace-events
> +++ b/hw/ide/trace-events
> @@ -47,17 +47,17 @@ bmdma_read_via(uint64_t addr, uint32_t val) "bmdma: readb 0x%"PRIx64" : 0x%02x"
>  bmdma_write_via(uint64_t addr, uint64_t val) "bmdma: writeb 0x%"PRIx64" : 0x%02"PRIx64
>  
>  # atapi.c
> -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 92e88687800..d6f646528aa 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 26082eed3a1..30564982722 100644
> --- a/tests/qtest/ahci-test.c
> +++ b/tests/qtest/ahci-test.c
> @@ -1691,6 +1691,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
> @@ -2234,6 +2268,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);


      parent reply	other threads:[~2026-11-12 17:12 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 13:47 [PATCH hci-dev 0/1] " Denis V. Lunev
2026-11-12 13:47 ` [PATCH hci-dev 1/1] " Denis V. Lunev
2026-11-12 13:51   ` no-reply
2026-11-12 17:12   ` Andrey Drobyshev [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4b94b1ff-59d1-4b08-8a25-428790a367b8@virtuozzo.com \
    --to=andrey.drobyshev@virtuozzo.com \
    --cc=den@openvz.org \
    --cc=svt-core@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.