pci: add romsize property
v3 cover pci: add romsize property
This property can be useful for distros to set up known-good ROM sizes for migration purposes. The VM will fail to start if the ROM is too large, and migration compatibility will not be broken if the ROM is too small.
v1->v2: fix overflow issues in nearby code [Laszlo]
v2->v3: consistently use %u in error messages [David]
Paolo
PATCH v3 1/2 pci: reject too large ROMs
get_image_size() returns an int64_t, which pci_add_option_rom() assigns to an "int" without any range checking. A 32-bit BAR could be up to 2 GiB in size, so reject anything above it. In order to accomodate a rounded-up size of 2 GiB, change pci_patch_ids's size argument to unsigned.
--- hw/pci/pci.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
hw/pci/pci.c +8 -2
@@ -25,6 +25,7 @@#include "qemu/osdep.h" #include "qemu-common.h" #include "qemu/datadir.h" +#include "qemu/units.h" #include "hw/irq.h" #include "hw/pci/pci.h" #include "hw/pci/pci_bridge.h"
@@ -2234,7 +2235,7 @@ static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset)/* Patch the PCI vendor and device ids in a PCI rom image if necessary. This is needed for an option rom which is used for more than one device. */ -static void pci_patch_ids (PCIDevice *pdev,uint8_t *ptr,int size) +static void pci_patch_ids (PCIDevice *pdev,uint8_t *ptr,uint32_t size) {uint16_t vendor_id;uint16_t device_id;
@@ -2292,7 +2293,7 @@ static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, int size)static void pci_add_option_rom (PCIDevice *pdev,bool is_default_rom,Error **errp) { -int size; +int64_t size;char *path;void *ptr;char name[32 ];
@@ -2342,6 +2343,11 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,error_setg (errp,"romfile \"%s\" is empty" , pdev->romfile);g_free (path);return ; + }else if (size >2 * GiB) { +error_setg (errp,"romfile \"%s\" too large (size cannot exceed 2 GiB)" , + pdev->romfile); +g_free (path); +return ; } size =pow2ceil (size);
Signature · 1 line
2.29.2
Re: [PATCH v3 1/2] pci: reject too large ROMs
Quoted 10 lines · Paolo Bonzini
get_image_size()returns anint64_t, whichpci_add_option_rom()assigns to an "int" without any range checking. A 32-bit BAR could be up to 2 GiB in size, so reject anything above it. In order to accomodate a rounded-up size of 2 GiB, change pci_patch_ids's size argument to unsigned.Reviewed-by Peter Xu <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>Reviewed-by Laszlo Ersek <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>
Quoted 48 lines
--- hw/pci/pci.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)hw/pci/pci.c +8
-2@@ -25,6 +25,7 @@#include "qemu/osdep.h" #include "qemu-common.h" #include "qemu/datadir.h" +#include "qemu/units.h" #include "hw/irq.h" #include "hw/pci/pci.h" #include "hw/pci/pci_bridge.h" @@ -2234,7 +2235,7 @@ static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset)/* Patch the PCI vendor and device ids in a PCI rom image if necessary. This is needed for an option rom which is used for more than one device. */ -static void pci_patch_ids (PCIDevice *pdev,uint8_t *ptr,int size) +static void pci_patch_ids (PCIDevice *pdev,uint8_t *ptr,uint32_t size) {uint16_t vendor_id;uint16_t device_id;@@ -2292,7 +2293,7 @@ static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, int size)static void pci_add_option_rom (PCIDevice *pdev,bool is_default_rom,Error **errp) { -int size; +int64_t size;char *path;void *ptr;char name[32 ];@@ -2342,6 +2343,11 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,error_setg (errp,"romfile \"%s\" is empty" , pdev->romfile);g_free (path);return ; + }else if (size >2 * GiB) { +error_setg (errp,"romfile \"%s\" too large (size cannot exceed 2 GiB)" , + pdev->romfile); +g_free (path); +return ; } size =pow2ceil (size);Signature · 1 line
2.29.2
dme.
Signature · 1 line
I used to worry, thought I was goin' mad in a hurry.
PATCH v3 2/2 pci: add romsize property
In reply to Paolo BonziniThis property can be useful for distros to set up known-good ROM sizes for migration purposes. The VM will fail to start if the ROM is too large, and migration compatibility will not be broken if the ROM is too small.
Note that even though romsize is a uint32_t, it has to be between 1 (because empty ROM files are not accepted, and romsize must be greater than the file) and 2^31 (because values above are not powers of two and are rejected).
Message-Id: <[email protected]>
--- hw/pci/pci.c | 19 +++++++++++++++++-- hw/xen/xen_pt_load_rom.c | 14 ++++++++++++-- include/hw/pci/pci.h | 1 + 3 files changed, 30 insertions(+), 4 deletions(-)
hw/pci/pci.c +17 -2
@@ -69,6 +69,7 @@ static void pcibus_reset(BusState *qbus);static Property pci_props[] = {DEFINE_PROP_PCI_DEVFN ("addr" , PCIDevice, devfn,-1 ),DEFINE_PROP_STRING ("romfile" , PCIDevice, romfile), +DEFINE_PROP_UINT32 ("romsize" , PCIDevice, romsize,-1 ),DEFINE_PROP_UINT32 ("rombar" , PCIDevice, rom_bar,1 ),DEFINE_PROP_BIT ("multifunction" , PCIDevice, cap_present,QEMU_PCI_CAP_MULTIFUNCTION_BITNR , false),
@@ -2084,6 +2085,11 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)bool is_default_rom;uint16_t class_id; +if (pci_dev->romsize !=-1 && !is_power_of_2 (pci_dev->romsize)) { +error_setg (errp,"ROM size %u is not a power of two" , pci_dev->romsize); +return ; + } +/* initialize cap_present for pci_is_express() and pci_config_size(), * Note that hybrid PCIs are not set automatically and need to manage * QEMU_PCI_CAP_EXPRESS manually */
@@ -2349,7 +2355,16 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,g_free (path);return ; } - size =pow2ceil (size); +if (pdev->romsize !=-1 ) { +if (size > pdev->romsize) { +error_setg (errp,"romfile \"%s\" (%u bytes) is too large for ROM size %u" , + pdev->romfile, (uint32_t )size, pdev->romsize); +g_free (path); +return ; + } + }else { + pdev->romsize =pow2ceil (size); + } vmsd =qdev_get_vmsd (DEVICE (pdev));
@@ -2359,7 +2374,7 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,snprintf (name,sizeof (name),"%s.rom" ,object_get_typename (OBJECT (pdev))); } pdev->has_rom = true; -memory_region_init_rom (&pdev->rom,OBJECT (pdev), name, size, &error_fatal); +memory_region_init_rom (&pdev->rom,OBJECT (pdev), name, pdev->romsize, &error_fatal); ptr =memory_region_get_ram_ptr (&pdev->rom);if (load_image_size (path, ptr, size) <0 ) {error_setg (errp,"failed to load romfile \"%s\"" , pdev->romfile);
hw/xen/xen_pt_load_rom.c +12 -2
@@ -53,10 +53,20 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev, }fseek (fp,0 ,SEEK_SET ); +if (dev->romsize !=-1 ) { +if (st.st_size > dev->romsize) { +error_report ("ROM BAR \"%s\" (%ld bytes) is too large for ROM size %u" , + rom_file, (long ) st.st_size, dev->romsize); + goto close_rom; + } + }else { + dev->romsize = st.st_size; + } +snprintf (name,sizeof (name),"%s.rom" ,object_get_typename (owner)); -memory_region_init_ram (&dev->rom, owner, name, st.st_size, &error_abort); +memory_region_init_ram (&dev->rom, owner, name, dev->romsize, &error_abort); ptr =memory_region_get_ram_ptr (&dev->rom); -memset (ptr,0xff , st.st_size); +memset (ptr,0xff , dev->romsize);if (!fread (ptr,1 , st.st_size, fp)) {error_report ("pci-assign: Cannot read from host %s" , rom_file);
include/hw/pci/pci.h +1 -0
@@ -344,6 +344,7 @@ struct PCIDevice {/* Location of option rom */ char *romfile; +uint32_t romsize;bool has_rom;MemoryRegion rom;uint32_t rom_bar;
Signature · 1 line
2.29.2
Quoted 9 lines · Paolo Bonzini
This property can be useful for distros to set up known-good ROM sizes for migration purposes. The VM will fail to start if the ROM is too large, and migration compatibility will not be broken if the ROM is too small.
Note that even though romsize is a
uint32_t, it has to be between 1 (because empty ROM files are not accepted, and romsize must be greater than the file) and 2^31 (because values above are not powers of two and are rejected).
Quoted 5 lines
Signed-off-by Paolo Bonzini <[email protected]>Reviewed-by Dr. David Alan Gilbert <[email protected]>Reviewed-by Peter Xu <[email protected]>Message-Id: <[email protected]>
Signed-off-by Paolo Bonzini <[email protected]>
Quoted 98 lines
--- hw/pci/pci.c | 19 +++++++++++++++++-- hw/xen/xen_pt_load_rom.c | 14 ++++++++++++-- include/hw/pci/pci.h | 1 + 3 files changed, 30 insertions(+), 4 deletions(-)hw/pci/pci.c +17
-2@@ -69,6 +69,7 @@ static void pcibus_reset(BusState *qbus);static Property pci_props[] = {DEFINE_PROP_PCI_DEVFN ("addr" , PCIDevice, devfn,-1 ),DEFINE_PROP_STRING ("romfile" , PCIDevice, romfile), +DEFINE_PROP_UINT32 ("romsize" , PCIDevice, romsize,-1 ),DEFINE_PROP_UINT32 ("rombar" , PCIDevice, rom_bar,1 ),DEFINE_PROP_BIT ("multifunction" , PCIDevice, cap_present,QEMU_PCI_CAP_MULTIFUNCTION_BITNR , false),@@ -2084,6 +2085,11 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)bool is_default_rom;uint16_t class_id; +if (pci_dev->romsize !=-1 && !is_power_of_2 (pci_dev->romsize)) { +error_setg (errp,"ROM size %u is not a power of two" , pci_dev->romsize); +return ; + } +/* initialize cap_present for pci_is_express() and pci_config_size(), * Note that hybrid PCIs are not set automatically and need to manage * QEMU_PCI_CAP_EXPRESS manually */ @@ -2349,7 +2355,16 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,g_free (path);return ; } - size =pow2ceil (size); +if (pdev->romsize !=-1 ) { +if (size > pdev->romsize) { +error_setg (errp,"romfile \"%s\" (%u bytes) is too large for ROM size %u" , + pdev->romfile, (uint32_t )size, pdev->romsize); +g_free (path); +return ; + } + }else { + pdev->romsize =pow2ceil (size); + } vmsd =qdev_get_vmsd (DEVICE (pdev));@@ -2359,7 +2374,7 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,snprintf (name,sizeof (name),"%s.rom" ,object_get_typename (OBJECT (pdev))); } pdev->has_rom = true; -memory_region_init_rom (&pdev->rom,OBJECT (pdev), name, size, &error_fatal); +memory_region_init_rom (&pdev->rom,OBJECT (pdev), name, pdev->romsize, &error_fatal); ptr =memory_region_get_ram_ptr (&pdev->rom);if (load_image_size (path, ptr, size) <0 ) {error_setg (errp,"failed to load romfile \"%s\"" , pdev->romfile);hw/xen/xen_pt_load_rom.c +12
-2@@ -53,10 +53,20 @@ void *pci_assign_dev_load_option_rom(PCIDevice *dev, }fseek (fp,0 ,SEEK_SET ); +if (dev->romsize !=-1 ) { +if (st.st_size > dev->romsize) { +error_report ("ROM BAR \"%s\" (%ld bytes) is too large for ROM size %u" , + rom_file, (long ) st.st_size, dev->romsize); + goto close_rom; + } + }else { + dev->romsize = st.st_size; + } +snprintf (name,sizeof (name),"%s.rom" ,object_get_typename (owner)); -memory_region_init_ram (&dev->rom, owner, name, st.st_size, &error_abort); +memory_region_init_ram (&dev->rom, owner, name, dev->romsize, &error_abort); ptr =memory_region_get_ram_ptr (&dev->rom); -memset (ptr,0xff , st.st_size); +memset (ptr,0xff , dev->romsize);if (!fread (ptr,1 , st.st_size, fp)) {error_report ("pci-assign: Cannot read from host %s" , rom_file);include/hw/pci/pci.h +1
-0@@ -344,6 +344,7 @@ struct PCIDevice {/* Location of option rom */ char *romfile; +uint32_t romsize;bool has_rom;MemoryRegion rom;uint32_t rom_bar;Signature · 1 line
2.29.2
dme.
Signature · 1 line
I'm not the reason you're looking for redemption.
Quoted 12 lines · Paolo Bonzini
This property can be useful for distros to set up known-good ROM sizes for migration purposes. The VM will fail to start if the ROM is too large, and migration compatibility will not be broken if the ROM is too small.
Note that even though romsize is a
uint32_t, it has to be between 1 (because empty ROM files are not accepted, and romsize must be greater than the file) and 2^31 (because values above are not powers of two and are rejected).Signed-off-by Paolo Bonzini <[email protected]>Reviewed-by Dr. David Alan Gilbert <[email protected]>Reviewed-by Peter Xu <[email protected]>
Quoted 7 lines
Message-Id: <[email protected]>
Signed-off-by Paolo Bonzini <[email protected]>--- hw/pci/pci.c | 19 +++++++++++++++++-- hw/xen/xen_pt_load_rom.c | 14 ++++++++++++-- include/hw/pci/pci.h | 1 + 3 files changed, 30 insertions(+), 4 deletions(-)