vfio: Fix CID 1458134 in vfio_register_ram_discard_listener()
PATCH 1/1 vfio: Fix CID 1458134 in vfio_register_ram_discard_listener()
CID 1458134: Integer handling issues (BAD_SHIFT)
In expression "1 << ctz64(container->pgsizes)", left shifting by more
than 31 bits has undefined behavior. The shift amount,
"ctz64(container->pgsizes)", is 64.Commit 5e3b981c330c ("vfio: Support for RamDiscardManager in the !vIOMMU case") added an assertion that our granularity is at least as big as the page size.
Although unlikely, we could have a page size that does not fit into 32 bit. In that case, we'd try shifting by more than 31 bit.
Let's use 1ULL instead and make sure we're not shifting by more than 63 bit by asserting that any bit in container->pgsizes is set.
--- hw/vfio/common.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/vfio/common.c +2 -1
@@ -783,7 +783,8 @@ static void vfio_register_ram_discard_listener(VFIOContainer *container, section->mr);g_assert (vrdl->granularity &&is_power_of_2 (vrdl->granularity)); -g_assert (vrdl->granularity >=1 <<ctz64 (container->pgsizes)); +g_assert (container->pgsizes && + vrdl->granularity >=1ULL <<ctz64 (container->pgsizes)); ram_discard_listener_init(&vrdl->listener, vfio_ram_discard_notify_populate,
Signature · 1 line
2.31.1
Quoted 15 lines · On Mon, 12 Jul 2021 10:31:35 +0200 David Hildenbrand <[email protected]> wrote:
CID 1458134: Integer handling issues (
BAD_SHIFT)In expression "1 << ctz64(container->pgsizes)", left shifting by more than 31 bits has undefined behavior. The shift amount, "ctz64(container->pgsizes)", is 64.Commit
5e3b981c330c("vfio: Support for RamDiscardManager in the !vIOMMU case") added an assertion that our granularity is at least as big as the page size.Although unlikely, we could have a page size that does not fit into 32 bit. In that case, we'd try shifting by more than 31 bit.
Let's use 1ULL instead and make sure we're not shifting by more than 63 bit by asserting that any bit in
container->pgsizesis set.
Quoted 14 lines
Fixes CID 1458134Cc Alex Williamson <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc Dr. David Alan Gilbert <[email protected]>Cc Igor Mammedov <[email protected]>Cc Pankaj Gupta <[email protected]>Cc Peter Xu <[email protected]>Cc Auger Eric <[email protected]>Cc Wei Yang <[email protected]>Cc teawater <[email protected]>Cc Marek Kedzierski <[email protected]>Signed-off-by David Hildenbrand <[email protected]>
Quoted 18 lines
--- hw/vfio/common.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)hw/vfio/common.c +2
-1@@ -783,7 +783,8 @@ static void vfio_register_ram_discard_listener(VFIOContainer *container, section->mr);g_assert (vrdl->granularity &&is_power_of_2 (vrdl->granularity)); -g_assert (vrdl->granularity >=1 <<ctz64 (container->pgsizes)); +g_assert (container->pgsizes && + vrdl->granularity >=1ULL <<ctz64 (container->pgsizes)); ram_discard_listener_init(&vrdl->listener, vfio_ram_discard_notify_populate,
Quoted 62 lines
CID 1458134: Integer handling issues (
BAD_SHIFT)In expression "1 << ctz64(container->pgsizes)", left shifting by more than 31 bits has undefined behavior. The shift amount, "ctz64(container->pgsizes)", is 64.Commit
5e3b981c330c("vfio: Support for RamDiscardManager in the !vIOMMU case") added an assertion that our granularity is at least as big as the page size.Although unlikely, we could have a page size that does not fit into 32 bit. In that case, we'd try shifting by more than 31 bit.
Let's use 1ULL instead and make sure we're not shifting by more than 63 bit by asserting that any bit in
container->pgsizesis set.Fixes CID 1458134Cc Alex Williamson <[email protected]>Cc Eduardo Habkost <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc Dr. David Alan Gilbert <[email protected]>Cc Igor Mammedov <[email protected]>Cc Pankaj Gupta <[email protected]>Cc Peter Xu <[email protected]>Cc Auger Eric <[email protected]>Cc Wei Yang <[email protected]>Cc teawater <[email protected]>Cc Marek Kedzierski <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- hw/vfio/common.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)hw/vfio/common.c +2
-1@@ -783,7 +783,8 @@ static void vfio_register_ram_discard_listener(VFIOContainer *container, section->mr);g_assert (vrdl->granularity &&is_power_of_2 (vrdl->granularity)); -g_assert (vrdl->granularity >=1 <<ctz64 (container->pgsizes)); +g_assert (container->pgsizes && + vrdl->granularity >=1ULL <<ctz64 (container->pgsizes)); ram_discard_listener_init(&vrdl->listener, vfio_ram_discard_notify_populate,Fixes d5015b801340 ("softmmu/memory: Pass ram_flags to
qemu_ram_alloc_from_fd()")Signed-off-by Yang Zhong <[email protected]>Reviewed-by David Hildenbrand <[email protected]>--- hw/remote/memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/remote/memory.c +0
-0
hw/remote/memory.c void remote_sysmem_reconfig(MPQemuMsg *msg, Error **errp) · line 52
Quoted 8 lines
hw/remote/memory.c +1
-1@@ -46,7 +46,7 @@ void remote_sysmem_reconfig(MPQemuMsg *msg, Error **errp) subregion =g_new (MemoryRegion,1 );memory_region_init_ram_from_fd (subregion,NULL , name, sysmem_info->sizes[region], - true, msg->fds[region], +RAM_SHARED , msg->fds[region], sysmem_info->offsets[region], errp);