All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] scsi-disk: fix race between WRITE SAME and MODE SELECT
@ 2026-08-26 18:17 Paolo Bonzini
  2026-08-26 18:17 ` [PATCH 1/2] scsi-disk: fix out-of-bound read in WRITE SAME Paolo Bonzini
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Paolo Bonzini @ 2026-08-26 18:17 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-stable, Mark Cave-Ayland

A guest with an attached scsi-hd can force QEMU's SCSI disk emulation
to read roughly 60 KiB past the end of a heap buffer, copying that out
of bounds host memory into the guest's own disk image.

This is due to a race between WRITE SAME and an obscure MODE SELECT
feature which can be restricted to the Q800 machine.

Paolo

Paolo Bonzini (2):
  scsi-disk: fix out-of-bound read in WRITE SAME
  scsi: hide MODE SELECT block size change behind a quirk

 include/hw/scsi/scsi.h |  1 +
 hw/m68k/q800.c         |  2 ++
 hw/scsi/scsi-disk.c    | 17 +++++++++++++----
 3 files changed, 16 insertions(+), 4 deletions(-)

-- 
2.55.0



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/2] scsi-disk: fix out-of-bound read in WRITE SAME
  2026-08-26 18:17 [PATCH 0/2] scsi-disk: fix race between WRITE SAME and MODE SELECT Paolo Bonzini
@ 2026-08-26 18:17 ` Paolo Bonzini
  2026-08-26 18:59   ` Philippe Mathieu-Daudé
  2026-08-26 18:17 ` [PATCH 2/2] scsi: hide MODE SELECT block size change behind a quirk Paolo Bonzini
  2026-08-26 18:52 ` [PATCH 0/2] scsi-disk: fix race between WRITE SAME and MODE SELECT Stefan Hajnoczi
  2 siblings, 1 reply; 6+ messages in thread
From: Paolo Bonzini @ 2026-08-26 18:17 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-stable, Mark Cave-Ayland

A guest with an attached scsi-hd can force QEMU's SCSI disk emulation
to read roughly 60 KiB past the end of a heap buffer, copying that out
of bounds host memory into the guest's own disk image.

WRITE SAME computes the request transfer length at dev->blocksize
when the request is prepared and sets cmd->xfer from dev->blocksize.
scsi_disk_emulate_command() then uses cmd->xfer as the size of the
request buffer.

However, MODE SELECT can race with the WRITE SAME command and guest raise
the logical block size to any value whose low bits fit 0xfe00, up to 65024.
In the presence of this race, scsi_disk_emulate_write_same() will read
from memory as many bytes as indicated by the *new* dev->blocksize,
and write it to disk.

The read length in WRITE SAME must be bounded by the buffer that was
actually allocated, not by the mutable s->qdev.blocksize, so clamp the
length used against inbuf to r->buflen.  Re-validating req->cmd.xfer
against the current block size would not work because the race is
intrinsic in the SCSI protocol.

I am not sure if this is exploitable with virtio-scsi and other SG-capable
HBAs, because it should process the WRITE SAME input immediately,
without letting the MODE SELECT command race with it; but probably
it is because s->qdev.blocksize is accessed without a lock.

Fixes: 356c4c441ec ("scsi-disk: allow MODE SELECT block descriptor to set the block size", 2022-07-13)
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4365
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 hw/scsi/scsi-disk.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c
index 1b0cce128c5..5bb7a974d69 100644
--- a/hw/scsi/scsi-disk.c
+++ b/hw/scsi/scsi-disk.c
@@ -1911,6 +1911,7 @@ static void scsi_disk_emulate_write_same(SCSIDiskReq *r, uint8_t *inbuf)
     SCSIRequest *req = &r->req;
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
     uint32_t nb_sectors = scsi_data_cdb_xfer(r->req.cmd.buf);
+    uint32_t buflen = MIN(s->qdev.blocksize, r->buflen);
     WriteSameCBData *data;
     uint8_t *buf;
     int i, l;
@@ -1930,7 +1931,7 @@ static void scsi_disk_emulate_write_same(SCSIDiskReq *r, uint8_t *inbuf)
         return;
     }
 
-    if ((req->cmd.buf[1] & 0x1) || buffer_is_zero(inbuf, s->qdev.blocksize)) {
+    if ((req->cmd.buf[1] & 0x1) || buffer_is_zero(inbuf, buflen)) {
         int flags = (req->cmd.buf[1] & 0x8) ? BDRV_REQ_MAY_UNMAP : 0;
 
         /* The request is used as the AIO opaque value, so add a ref.  */
@@ -1956,7 +1957,7 @@ static void scsi_disk_emulate_write_same(SCSIDiskReq *r, uint8_t *inbuf)
     qemu_iovec_init_external(&data->qiov, &data->iov, 1);
 
     for (i = 0; i < data->iov.iov_len; i += l) {
-        l = MIN(s->qdev.blocksize, data->iov.iov_len - i);
+        l = MIN(buflen, data->iov.iov_len - i);
         memcpy(&buf[i], inbuf, l);
     }
 
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/2] scsi: hide MODE SELECT block size change behind a quirk
  2026-08-26 18:17 [PATCH 0/2] scsi-disk: fix race between WRITE SAME and MODE SELECT Paolo Bonzini
  2026-08-26 18:17 ` [PATCH 1/2] scsi-disk: fix out-of-bound read in WRITE SAME Paolo Bonzini
@ 2026-08-26 18:17 ` Paolo Bonzini
  2026-08-27 20:12   ` Mark Cave-Ayland
  2026-08-26 18:52 ` [PATCH 0/2] scsi-disk: fix race between WRITE SAME and MODE SELECT Stefan Hajnoczi
  2 siblings, 1 reply; 6+ messages in thread
From: Paolo Bonzini @ 2026-08-26 18:17 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-stable, Mark Cave-Ayland

This is a dangerous operation in that the block size is not
protected by a lock, but it can be written concurrently if
you have a multi-queue virtio-scsi HBA.  Put it behind a quirk
that is only enabled by the Q800 machine, since the MODE
SELECT feature was added for A/UX.

Fixes: 356c4c441ec ("scsi-disk: allow MODE SELECT block descriptor to set the block size", 2022-07-13)
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 include/hw/scsi/scsi.h |  1 +
 hw/m68k/q800.c         |  2 ++
 hw/scsi/scsi-disk.c    | 12 ++++++++++--
 3 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/include/hw/scsi/scsi.h b/include/hw/scsi/scsi.h
index c60c6e8810e..3eab339b0d2 100644
--- a/include/hw/scsi/scsi.h
+++ b/include/hw/scsi/scsi.h
@@ -262,5 +262,6 @@ bool scsi_generic_pr_state_preempt(SCSIDevice *s, Error **errp);
 #define SCSI_DISK_QUIRK_MODE_SENSE_ROM_USE_DBD             1
 #define SCSI_DISK_QUIRK_MODE_PAGE_VENDOR_SPECIFIC_APPLE    2
 #define SCSI_DISK_QUIRK_MODE_PAGE_TRUNCATED                3
+#define SCSI_DISK_QUIRK_MODE_PAGE_SET_BLOCK_SIZE           4
 
 #endif
diff --git a/hw/m68k/q800.c b/hw/m68k/q800.c
index ab64250c47c..1ad3f93b294 100644
--- a/hw/m68k/q800.c
+++ b/hw/m68k/q800.c
@@ -710,12 +710,14 @@ static void q800_init(Object *obj)
 
 static GlobalProperty hw_compat_q800[] = {
     { "scsi-hd", "quirk_mode_page_vendor_specific_apple", "on" },
+    { "scsi-hd", "quirk_mode_page_set_block_size", "on" },
     { "scsi-hd", "vendor", " SEAGATE" },
     { "scsi-hd", "product", "          ST225N" },
     { "scsi-hd", "ver", "1.0 " },
     { "scsi-cd", "quirk_mode_page_apple_vendor", "on" },
     { "scsi-cd", "quirk_mode_sense_rom_use_dbd", "on" },
     { "scsi-cd", "quirk_mode_page_vendor_specific_apple", "on" },
+    { "scsi-cd", "quirk_mode_page_set_block_size", "on" },
     { "scsi-cd", "quirk_mode_page_truncated", "on" },
     { "scsi-cd", "vendor", "MATSHITA" },
     { "scsi-cd", "product", "CD-ROM CR-8005" },
diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c
index 5bb7a974d69..a42f7d8e77d 100644
--- a/hw/scsi/scsi-disk.c
+++ b/hw/scsi/scsi-disk.c
@@ -1673,8 +1673,12 @@ static void scsi_disk_emulate_mode_select(SCSIDiskReq *r, uint8_t *inbuf)
         goto invalid_param;
     }
 
-    /* Allow changing the block size */
-    if (bd_len) {
+    /*
+     * Allow changing the block size only if the quirk is enabled for it.
+     * Writing s->qdev.blocksize is not thread safe!
+     */
+    if (bd_len && (s->quirks &
+                   (1 << SCSI_DISK_QUIRK_MODE_PAGE_SET_BLOCK_SIZE))) {
         bs = p[5] << 16 | p[6] << 8 | p[7];
 
         /*
@@ -3247,6 +3251,8 @@ static const Property scsi_hd_properties[] = {
     DEFINE_PROP_BIT("quirk_mode_page_vendor_specific_apple", SCSIDiskState,
                     quirks, SCSI_DISK_QUIRK_MODE_PAGE_VENDOR_SPECIFIC_APPLE,
                     0),
+    DEFINE_PROP_BIT("quirk_mode_page_set_block_size", SCSIDiskState,
+                    quirks, SCSI_DISK_QUIRK_MODE_PAGE_SET_BLOCK_SIZE, 0),
     DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState, qdev.conf),
 };
 
@@ -3352,6 +3358,8 @@ static const Property scsi_cd_properties[] = {
                     0),
     DEFINE_PROP_BIT("quirk_mode_page_truncated", SCSIDiskState, quirks,
                     SCSI_DISK_QUIRK_MODE_PAGE_TRUNCATED, 0),
+    DEFINE_PROP_BIT("quirk_mode_page_set_block_size", SCSIDiskState,
+                    quirks, SCSI_DISK_QUIRK_MODE_PAGE_SET_BLOCK_SIZE, 0),
 };
 
 static void scsi_cd_class_initfn(ObjectClass *klass, const void *data)
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/2] scsi-disk: fix race between WRITE SAME and MODE SELECT
  2026-08-26 18:17 [PATCH 0/2] scsi-disk: fix race between WRITE SAME and MODE SELECT Paolo Bonzini
  2026-08-26 18:17 ` [PATCH 1/2] scsi-disk: fix out-of-bound read in WRITE SAME Paolo Bonzini
  2026-08-26 18:17 ` [PATCH 2/2] scsi: hide MODE SELECT block size change behind a quirk Paolo Bonzini
@ 2026-08-26 18:52 ` Stefan Hajnoczi
  2 siblings, 0 replies; 6+ messages in thread
From: Stefan Hajnoczi @ 2026-08-26 18:52 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: qemu-devel, qemu-stable, Mark Cave-Ayland

[-- Attachment #1: Type: text/plain, Size: 812 bytes --]

On Wed, Aug 26, 2026 at 08:17:23PM +0200, Paolo Bonzini wrote:
> A guest with an attached scsi-hd can force QEMU's SCSI disk emulation
> to read roughly 60 KiB past the end of a heap buffer, copying that out
> of bounds host memory into the guest's own disk image.
> 
> This is due to a race between WRITE SAME and an obscure MODE SELECT
> feature which can be restricted to the Q800 machine.
> 
> Paolo
> 
> Paolo Bonzini (2):
>   scsi-disk: fix out-of-bound read in WRITE SAME
>   scsi: hide MODE SELECT block size change behind a quirk
> 
>  include/hw/scsi/scsi.h |  1 +
>  hw/m68k/q800.c         |  2 ++
>  hw/scsi/scsi-disk.c    | 17 +++++++++++++----
>  3 files changed, 16 insertions(+), 4 deletions(-)
> 
> -- 
> 2.55.0
> 
> 

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] scsi-disk: fix out-of-bound read in WRITE SAME
  2026-08-26 18:17 ` [PATCH 1/2] scsi-disk: fix out-of-bound read in WRITE SAME Paolo Bonzini
@ 2026-08-26 18:59   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 6+ messages in thread
From: Philippe Mathieu-Daudé @ 2026-08-26 18:59 UTC (permalink / raw)
  To: Paolo Bonzini, qemu-devel; +Cc: qemu-stable, Mark Cave-Ayland

On 26/8/26 20:17, Paolo Bonzini wrote:
> A guest with an attached scsi-hd can force QEMU's SCSI disk emulation
> to read roughly 60 KiB past the end of a heap buffer, copying that out
> of bounds host memory into the guest's own disk image.
> 
> WRITE SAME computes the request transfer length at dev->blocksize
> when the request is prepared and sets cmd->xfer from dev->blocksize.
> scsi_disk_emulate_command() then uses cmd->xfer as the size of the
> request buffer.
> 
> However, MODE SELECT can race with the WRITE SAME command and guest raise
> the logical block size to any value whose low bits fit 0xfe00, up to 65024.
> In the presence of this race, scsi_disk_emulate_write_same() will read
> from memory as many bytes as indicated by the *new* dev->blocksize,
> and write it to disk.
> 
> The read length in WRITE SAME must be bounded by the buffer that was
> actually allocated, not by the mutable s->qdev.blocksize, so clamp the
> length used against inbuf to r->buflen.  Re-validating req->cmd.xfer
> against the current block size would not work because the race is
> intrinsic in the SCSI protocol.
> 
> I am not sure if this is exploitable with virtio-scsi and other SG-capable
> HBAs, because it should process the WRITE SAME input immediately,
> without letting the MODE SELECT command race with it; but probably
> it is because s->qdev.blocksize is accessed without a lock.
> 
> Fixes: 356c4c441ec ("scsi-disk: allow MODE SELECT block descriptor to set the block size", 2022-07-13)
> Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4365
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>   hw/scsi/scsi-disk.c | 5 +++--
>   1 file changed, 3 insertions(+), 2 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] scsi: hide MODE SELECT block size change behind a quirk
  2026-08-26 18:17 ` [PATCH 2/2] scsi: hide MODE SELECT block size change behind a quirk Paolo Bonzini
@ 2026-08-27 20:12   ` Mark Cave-Ayland
  0 siblings, 0 replies; 6+ messages in thread
From: Mark Cave-Ayland @ 2026-08-27 20:12 UTC (permalink / raw)
  To: Paolo Bonzini, qemu-devel; +Cc: qemu-stable, Mark Cave-Ayland

On 26/08/2026 19:17, Paolo Bonzini wrote:

> This is a dangerous operation in that the block size is not
> protected by a lock, but it can be written concurrently if
> you have a multi-queue virtio-scsi HBA.  Put it behind a quirk
> that is only enabled by the Q800 machine, since the MODE
> SELECT feature was added for A/UX.
> 
> Fixes: 356c4c441ec ("scsi-disk: allow MODE SELECT block descriptor to set the block size", 2022-07-13)
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>   include/hw/scsi/scsi.h |  1 +
>   hw/m68k/q800.c         |  2 ++
>   hw/scsi/scsi-disk.c    | 12 ++++++++++--
>   3 files changed, 13 insertions(+), 2 deletions(-)
> 
> diff --git a/include/hw/scsi/scsi.h b/include/hw/scsi/scsi.h
> index c60c6e8810e..3eab339b0d2 100644
> --- a/include/hw/scsi/scsi.h
> +++ b/include/hw/scsi/scsi.h
> @@ -262,5 +262,6 @@ bool scsi_generic_pr_state_preempt(SCSIDevice *s, Error **errp);
>   #define SCSI_DISK_QUIRK_MODE_SENSE_ROM_USE_DBD             1
>   #define SCSI_DISK_QUIRK_MODE_PAGE_VENDOR_SPECIFIC_APPLE    2
>   #define SCSI_DISK_QUIRK_MODE_PAGE_TRUNCATED                3
> +#define SCSI_DISK_QUIRK_MODE_PAGE_SET_BLOCK_SIZE           4
>   
>   #endif
> diff --git a/hw/m68k/q800.c b/hw/m68k/q800.c
> index ab64250c47c..1ad3f93b294 100644
> --- a/hw/m68k/q800.c
> +++ b/hw/m68k/q800.c
> @@ -710,12 +710,14 @@ static void q800_init(Object *obj)
>   
>   static GlobalProperty hw_compat_q800[] = {
>       { "scsi-hd", "quirk_mode_page_vendor_specific_apple", "on" },
> +    { "scsi-hd", "quirk_mode_page_set_block_size", "on" },
>       { "scsi-hd", "vendor", " SEAGATE" },
>       { "scsi-hd", "product", "          ST225N" },
>       { "scsi-hd", "ver", "1.0 " },
>       { "scsi-cd", "quirk_mode_page_apple_vendor", "on" },
>       { "scsi-cd", "quirk_mode_sense_rom_use_dbd", "on" },
>       { "scsi-cd", "quirk_mode_page_vendor_specific_apple", "on" },
> +    { "scsi-cd", "quirk_mode_page_set_block_size", "on" },
>       { "scsi-cd", "quirk_mode_page_truncated", "on" },
>       { "scsi-cd", "vendor", "MATSHITA" },
>       { "scsi-cd", "product", "CD-ROM CR-8005" },
> diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c
> index 5bb7a974d69..a42f7d8e77d 100644
> --- a/hw/scsi/scsi-disk.c
> +++ b/hw/scsi/scsi-disk.c
> @@ -1673,8 +1673,12 @@ static void scsi_disk_emulate_mode_select(SCSIDiskReq *r, uint8_t *inbuf)
>           goto invalid_param;
>       }
>   
> -    /* Allow changing the block size */
> -    if (bd_len) {
> +    /*
> +     * Allow changing the block size only if the quirk is enabled for it.
> +     * Writing s->qdev.blocksize is not thread safe!
> +     */
> +    if (bd_len && (s->quirks &
> +                   (1 << SCSI_DISK_QUIRK_MODE_PAGE_SET_BLOCK_SIZE))) {
>           bs = p[5] << 16 | p[6] << 8 | p[7];
>   
>           /*
> @@ -3247,6 +3251,8 @@ static const Property scsi_hd_properties[] = {
>       DEFINE_PROP_BIT("quirk_mode_page_vendor_specific_apple", SCSIDiskState,
>                       quirks, SCSI_DISK_QUIRK_MODE_PAGE_VENDOR_SPECIFIC_APPLE,
>                       0),
> +    DEFINE_PROP_BIT("quirk_mode_page_set_block_size", SCSIDiskState,
> +                    quirks, SCSI_DISK_QUIRK_MODE_PAGE_SET_BLOCK_SIZE, 0),
>       DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState, qdev.conf),
>   };
>   
> @@ -3352,6 +3358,8 @@ static const Property scsi_cd_properties[] = {
>                       0),
>       DEFINE_PROP_BIT("quirk_mode_page_truncated", SCSIDiskState, quirks,
>                       SCSI_DISK_QUIRK_MODE_PAGE_TRUNCATED, 0),
> +    DEFINE_PROP_BIT("quirk_mode_page_set_block_size", SCSIDiskState,
> +                    quirks, SCSI_DISK_QUIRK_MODE_PAGE_SET_BLOCK_SIZE, 0),
>   };
>   
>   static void scsi_cd_class_initfn(ObjectClass *klass, const void *data)

Apologies for this, I really didn't expect this to cause a problem with multi-queue 
virtio-scsi. The fix looks good to me.

FWIW early SPARC Solaris also requires this if using a ROM from a real machine and 
booting from CDROM: OpenBIOS has a few hacks in place that allow this to work (for now).


ATB,

Mark.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-08-27 20:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-26 18:17 [PATCH 0/2] scsi-disk: fix race between WRITE SAME and MODE SELECT Paolo Bonzini
2026-08-26 18:17 ` [PATCH 1/2] scsi-disk: fix out-of-bound read in WRITE SAME Paolo Bonzini
2026-08-26 18:59   ` Philippe Mathieu-Daudé
2026-08-26 18:17 ` [PATCH 2/2] scsi: hide MODE SELECT block size change behind a quirk Paolo Bonzini
2026-08-27 20:12   ` Mark Cave-Ayland
2026-08-26 18:52 ` [PATCH 0/2] scsi-disk: fix race between WRITE SAME and MODE SELECT Stefan Hajnoczi

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.