vfio/pci: Fix buffer overrun when writing the VF token
cover vfio/pci: Fix buffer overrun when writing the VF token
Hello,
This series fixes a buffer overrun in VFIO. The buffer used in vfio_realize() by qemu_uuid_unparse() is too small, UUID_FMT_LEN lacks one byte for the trailing NUL.
Instead of adding + 1, as done elsewhere, the changes introduce a UUID_STR_LEN define for the correct size and use it where required.
Thanks,
C.
Cédric Le Goater (2):
util/uuid: Add UUID_STR_LEN definition vfio/pci: Fix buffer overrun when writing the VF token
include/qemu/uuid.h | 1 + block/parallels-ext.c | 2 +- block/vdi.c | 2 +- hw/core/qdev-properties-system.c | 2 +- hw/hyperv/vmbus.c | 4 ++-- hw/vfio/pci.c | 2 +- migration/savevm.c | 4 ++-- tests/unit/test-uuid.c | 2 +- util/uuid.c | 2 +- 9 files changed, 11 insertions(+), 10 deletions(-)
Signature · 1 line
2.41.0
PATCH 1/2 util/uuid: Add UUID_STR_LEN definition
qemu_uuid_unparse() includes a trailing NUL when writing the uuid string and the buffer size should be UUID_FMT_LEN + 1 bytes. Add a define for this size and use it where required.
--- include/qemu/uuid.h | 1 + block/parallels-ext.c | 2 +- block/vdi.c | 2 +- hw/core/qdev-properties-system.c | 2 +- hw/hyperv/vmbus.c | 4 ++-- migration/savevm.c | 4 ++-- tests/unit/test-uuid.c | 2 +- util/uuid.c | 2 +- 8 files changed, 10 insertions(+), 9 deletions(-)
include/qemu/uuid.h +1 -0
@@ -79,6 +79,7 @@ typedef struct { "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"#define UUID_FMT_LEN 36 +#define UUID_STR_LEN (UUID_FMT_LEN + 1)#define UUID_NONE "00000000-0000-0000-0000-000000000000"
block/parallels-ext.c +1 -1
@@ -130,7 +130,7 @@ static BdrvDirtyBitmap *parallels_load_bitmap(BlockDriverState *bs, g_autofree uint64_t *l1_table = NULL;BdrvDirtyBitmap *bitmap;QemuUUID uuid; -char uuidstr[UUID_FMT_LEN +1 ]; +char uuidstr[UUID_STR_LEN ];int i;if (data_size <sizeof (bf)) {
block/vdi.c +1 -1
@@ -239,7 +239,7 @@ static void vdi_header_to_le(VdiHeader *header)static void vdi_header_print (VdiHeader *header) { -char uuidstr[37 ]; +char uuidstr[UUID_STR_LEN ];QemuUUID uuid;logout ("text %s" , header->text);logout ("signature 0x%08x\n" , header->signature);
hw/core/qdev-properties-system.c +1 -1
@@ -1100,7 +1100,7 @@ static void get_uuid(Object *obj, Visitor *v, const char *name, void *opaque, {Property *prop = opaque;QemuUUID *uuid =object_field_prop_ptr (obj, prop); -char buffer[UUID_FMT_LEN +1 ]; +char buffer[UUID_STR_LEN ];char *p = buffer;qemu_uuid_unparse (uuid, buffer);
hw/hyperv/vmbus.c +2 -2
@@ -2271,7 +2271,7 @@ static void vmbus_dev_realize(DeviceState *dev, Error **errp)VMBus *vmbus =VMBUS (qdev_get_parent_bus (dev));BusChild *child;Error *err =NULL ; -char idstr[UUID_FMT_LEN +1 ]; +char idstr[UUID_STR_LEN ];assert (!qemu_uuid_is_null (&vdev->instanceid));
@@ -2467,7 +2467,7 @@ static char *vmbus_get_dev_path(DeviceState *dev)static char *vmbus_get_fw_dev_path (DeviceState *dev) {VMBusDevice *vdev =VMBUS_DEVICE (dev); -char uuid[UUID_FMT_LEN +1 ]; +char uuid[UUID_STR_LEN ];qemu_uuid_unparse (&vdev->instanceid, uuid);return g_strdup_printf ("%s@%s" ,qdev_fw_name (dev), uuid);
migration/savevm.c +2 -2
@@ -469,8 +469,8 @@ static bool vmstate_uuid_needed(void *opaque)static int vmstate_uuid_post_load (void *opaque,int version_id) {SaveState *state = opaque; -char uuid_src[UUID_FMT_LEN +1 ]; -char uuid_dst[UUID_FMT_LEN +1 ]; +char uuid_src[UUID_STR_LEN ]; +char uuid_dst[UUID_STR_LEN ];if (!qemu_uuid_set) {/*
tests/unit/test-uuid.c +1 -1
@@ -145,7 +145,7 @@ static void test_uuid_unparse(void)int i;for (i =0 ; i <ARRAY_SIZE (uuid_test_data); i++) { -char out[37 ]; +char out[UUID_STR_LEN ];if (!uuid_test_data[i].check_unparse) {continue ;
util/uuid.c +1 -1
@@ -51,7 +51,7 @@ int qemu_uuid_is_equal(const QemuUUID *lhv, const QemuUUID *rhv)void qemu_uuid_unparse (const QemuUUID *uuid,char *out) {const unsigned char *uu = &uuid->data[0 ]; -snprintf (out,UUID_FMT_LEN +1 ,UUID_FMT , +snprintf (out,UUID_STR_LEN ,UUID_FMT , uu[0 ], uu[1 ], uu[2 ], uu[3 ], uu[4 ], uu[5 ], uu[6 ], uu[7 ], uu[8 ], uu[9 ], uu[10 ], uu[11 ], uu[12 ], uu[13 ], uu[14 ], uu[15 ]); }
Signature · 1 line
2.41.0
Re: [PATCH 1/2] util/uuid: Add UUID_STR_LEN definition
include/qemu/uuid.h
Quoted 18 lines · Cédric Le Goater
qemu_uuid_unparse()includes a trailing NUL when writing the uuid string and the buffer size should beUUID_FMT_LEN+ 1 bytes. Add a define for this size and use it where required.Cc Fam Zheng <[email protected]>Signed-off-by Cédric Le Goater <[email protected]>--- include/qemu/uuid.h | 1 + block/parallels-ext.c | 2 +- block/vdi.c | 2 +- hw/core/qdev-properties-system.c | 2 +- hw/hyperv/vmbus.c | 4 ++-- migration/savevm.c | 4 ++-- tests/unit/test-uuid.c | 2 +- util/uuid.c | 2 +- 8 files changed, 10 insertions(+), 9 deletions(-)include/qemu/uuid.h +0
-0
Re: [PATCH 1/2] util/uuid: Add UUID_STR_LEN definition
In reply to Cédric Le GoaterQuoted 6 lines · Cédric Le Goater
qemu_uuid_unparse()includes a trailing NUL when writing the uuid string and the buffer size should beUUID_FMT_LEN+ 1 bytes. Add a define for this size and use it where required.Cc Fam Zheng <[email protected]>Signed-off-by Cédric Le Goater <[email protected]>
Quoted 10 lines
--- include/qemu/uuid.h | 1 + block/parallels-ext.c | 2 +- block/vdi.c | 2 +- hw/core/qdev-properties-system.c | 2 +- hw/hyperv/vmbus.c | 4 ++-- migration/savevm.c | 4 ++-- tests/unit/test-uuid.c | 2 +- util/uuid.c | 2 +- 8 files changed, 10 insertions(+), 9 deletions(-)
include/qemu/uuid.h typedef struct { · line 84
Quoted 11 lines
include/qemu/uuid.h +1
-0@@ -79,6 +79,7 @@ typedef struct { "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"#define UUID_FMT_LEN 36 +#define UUID_STR_LEN (UUID_FMT_LEN + 1)#define UUID_NONE "00000000-0000-0000-0000-000000000000"
After this patch, when do we need UUID_FMT_LEN?
If it is dangerous, better drop it and keep:
#define UUID_STR_LEN (36 + 1)
or
#define UUID_STR_LEN (36 + sizeof('\0'))
PATCH 2/2 vfio/pci: Fix buffer overrun when writing the VF token
In reply to Cédric Le Goaterqemu_uuid_unparse() includes a trailing NUL when writing the uuid string and the buffer size should be UUID_FMT_LEN + 1 bytes. Use the recently added UUID_STR_LEN which defines the correct size.
--- hw/vfio/pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/vfio/pci.c +1 -1
@@ -3274,7 +3274,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)Error *err =NULL ;int i, ret;bool is_mdev; -char uuid[UUID_FMT_LEN ]; +char uuid[UUID_STR_LEN ];char *name;if (vbasedev->fd <0 && !vbasedev->sysfsdev) {
Signature · 1 line
2.41.0
Quoted 8 lines · Cédric Le Goater
qemu_uuid_unparse()includes a trailing NUL when writing the uuid string and the buffer size should beUUID_FMT_LEN+ 1 bytes. Use the recently addedUUID_STR_LENwhich defines the correct size.Fixes CID 1522913Fixes 2dca1b37a760 ("vfio/pci: add support for VF token")Cc Alex Williamson <[email protected]>Signed-off-by Cédric Le Goater <[email protected]>
For what is worth O:-)
Quoted 17 lines · On Wed, 25 Oct 2023 12:12:45 +0200 Cédric Le Goater <[email protected]> wrote:
qemu_uuid_unparse()includes a trailing NUL when writing the uuid string and the buffer size should beUUID_FMT_LEN+ 1 bytes. Use the recently addedUUID_STR_LENwhich defines the correct size.Fixes CID 1522913Fixes 2dca1b37a760 ("vfio/pci: add support for VF token")Cc Alex Williamson <[email protected]>Signed-off-by Cédric Le Goater <[email protected]>--- hw/vfio/pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/vfio/pci.c +0
-0
hw/vfio/pci.c static void vfio_realize(PCIDevice *pdev, Error **errp) · line 3280
Quoted 8 lines
hw/vfio/pci.c +1
-1@@ -3274,7 +3274,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)Error *err =NULL ;int i, ret;bool is_mdev; -char uuid[UUID_FMT_LEN ]; +char uuid[UUID_STR_LEN ];char *name;if (vbasedev->fd <0 && !vbasedev->sysfsdev) {