scsi-disk: fix race between WRITE SAME and MODE SELECT
cover scsi-disk: fix race between WRITE SAME and MODE SELECT
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(-)
Signature · 1 line
2.55.0
PATCH 1/2 scsi-disk: fix out-of-bound read in WRITE SAME
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.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4365
--- hw/scsi/scsi-disk.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
hw/scsi/scsi-disk.c +3 -2
@@ -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); }
Signature · 1 line
2.55.0
Re: [PATCH 1/2] scsi-disk: fix out-of-bound read in WRITE SAME
Quoted 26 lines · Paolo Bonzini
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->blocksizewhen the request is prepared and setscmd->xferfromdev->blocksize.scsi_disk_emulate_command()then usescmd->xferas 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 tor->buflen. Re-validatingreq->cmd.xferagainst 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.blocksizeis accessed without a lock.
Quoted 6 lines
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 <[email protected]>--- hw/scsi/scsi-disk.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
PATCH 2/2 scsi: hide MODE SELECT block size change behind a quirk
In reply to Paolo BonziniThis 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.
--- include/hw/scsi/scsi.h | 1 + hw/m68k/q800.c | 2 ++ hw/scsi/scsi-disk.c | 12 ++++++++++-- 3 files changed, 13 insertions(+), 2 deletions(-)
include/hw/scsi/scsi.h +1 -0
@@ -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
hw/m68k/q800.c +2 -0
@@ -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" },
hw/scsi/scsi-disk.c +10 -2
@@ -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)
Signature · 1 line
2.55.0
Re: [PATCH 2/2] scsi: hide MODE SELECT block size change behind a quirk
Quoted 73 lines · Paolo Bonzini
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 <[email protected]>--- include/hw/scsi/scsi.h | 1 + hw/m68k/q800.c | 2 ++ hw/scsi/scsi-disk.c | 12 ++++++++++-- 3 files changed, 13 insertions(+), 2 deletions(-)include/hw/scsi/scsi.h +1
-0@@ -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 hw/m68k/q800.c +2
-0@@ -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" },hw/scsi/scsi-disk.c +8
-2@@ -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), };
hw/scsi/scsi-disk.c static const Property scsi_cd_properties[] = { · line 3365
Quoted 8 lines
hw/scsi/scsi-disk.c +2
-0@@ -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.
Quoted 9 lines · Paolo Bonzini
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
Quoted 13 lines
Paolo Bonzini (2):
scsi-disk: fix out-of-bound read in WRITE SAME
scsi: hide MODE SELECT block size change behind a quirkinclude/hw/scsi/scsi.h | 1 + hw/m68k/q800.c | 2 ++ hw/scsi/scsi-disk.c | 17 +++++++++++++---- 3 files changed, 16 insertions(+), 4 deletions(-)Signature · 1 line
2.55.0