Skip to contentSkip to conversation

migration: validate page_size in mapped-ram header before use

PATCH Superseded Trieu Huynh 2 participants 3 messages Mar 28 – Apr 1

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 migration: validate page_size in mapped-ram header before use

mapped_ram_read_header() reads page_size from the migration stream and stores it in MappedRamHeader, but does not validate that the value is non-zero before it is later used in parse_ramblock_mapped_ram():

num_pages = length / header.page_size;

If a corrupted or malformed migration stream provides page_size = 0, QEMU unexpectedly crashes.

Add validation in mapped_ram_read_header() to reject zero page_size values early and return an error instead of continuing with an invalid header.

Steps to reproduce:

Create a migration snapshot with mapped-ram enabled:
(qemu) migrate_set_capability mapped-ram on
(qemu) migrate file:/tmp/qemu-snapshots/snapshot.bin Modify the snapshot so that MappedRamHeader.page_size becomes 0. Restore the snapshot:
(qemu) migrate_set_capability mapped-ram on
(qemu) migrate_incoming file:/tmp/qemu-bug-test/snapshot.bin

As-is: qemu crashes immediately
(qemu) Floating point exception (core dumped)
To-be: qemu continue running
(qemu) qemu-system-x86_64: Migration mapped-ram header has invalid page_size of 0

---
 migration/ram.c | 5 +++++
 1 file changed, 5 insertions(+)
migration/ram.c +5 -0
index 979751f61b..2a7e958e87 100644
@@ -3088,6 +3088,11 @@ static bool mapped_ram_read_header(QEMUFile *file, MappedRamHeader *header,
     }
 
     header->page_size = be64_to_cpu(header->page_size);
+    if (header->page_size == 0) {
+        error_setg(errp, "Migration mapped-ram header has invalid "
+                   "page_size of 0");
+        return false;
+    }
     header->bitmap_offset = be64_to_cpu(header->bitmap_offset);
     header->pages_offset = be64_to_cpu(header->pages_offset);
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 39 lines · Trieu Huynh

mapped_ram_read_header() reads page_size from the migration stream and stores it in MappedRamHeader, but does not validate that the value is non-zero before it is later used in parse_ramblock_mapped_ram():

num_pages = length / header.page_size;

If a corrupted or malformed migration stream provides page_size = 0, QEMU unexpectedly crashes.

Add validation in mapped_ram_read_header() to reject zero page_size values early and return an error instead of continuing with an invalid header.

Steps to reproduce:

Create a migration snapshot with mapped-ram enabled:
(qemu) migrate_set_capability mapped-ram on
(qemu) migrate file:/tmp/qemu-snapshots/snapshot.bin Modify the snapshot so that MappedRamHeader.page_size becomes 0. Restore the snapshot:
(qemu) migrate_set_capability mapped-ram on
(qemu) migrate_incoming file:/tmp/qemu-bug-test/snapshot.bin

As-is: qemu crashes immediately
(qemu) Floating point exception (core dumped)
To-be: qemu continue running
(qemu) qemu-system-x86_64: Migration mapped-ram header has invalid page_size of 0

---
 migration/ram.c | 5 +++++
 1 file changed, 5 insertions(+)
migration/ram.c +0 -0
index 979751f61b..2a7e958e87 100644

migration/ram.c static bool mapped_ram_read_header(QEMUFile *file, MappedRamHeader *header, · line 3095

Quoted 8 lines
migration/ram.c +5 -0
@@ -3088,6 +3088,11 @@ static bool mapped_ram_read_header(QEMUFile *file, MappedRamHeader *header,
     }
 
     header->page_size = be64_to_cpu(header->page_size);
+    if (header->page_size == 0) {
+        error_setg(errp, "Migration mapped-ram header has invalid "
+                   "page_size of 0");
+        return false;
+    }

Thanks for the patch.

When at this, one more thing we could do is verify the value is exactly target psize; afaict that's the assumption across the whole mapped-ram feature.

Quoted 6 lines
migration/ram.c +0 -0
     header->bitmap_offset = be64_to_cpu(header->bitmap_offset);
     header->pages_offset = be64_to_cpu(header->pages_offset);
 
Signature · 1 line
2.43.0
Signature · 1 line
Peter Xu
#2 lore raw
Quoted 51 lines · Peter Xu
Quoted 39 lines · Trieu Huynh

mapped_ram_read_header() reads page_size from the migration stream and stores it in MappedRamHeader, but does not validate that the value is non-zero before it is later used in parse_ramblock_mapped_ram():

num_pages = length / header.page_size;

If a corrupted or malformed migration stream provides page_size = 0, QEMU unexpectedly crashes.

Add validation in mapped_ram_read_header() to reject zero page_size values early and return an error instead of continuing with an invalid header.

Steps to reproduce:

Create a migration snapshot with mapped-ram enabled:
(qemu) migrate_set_capability mapped-ram on
(qemu) migrate file:/tmp/qemu-snapshots/snapshot.bin Modify the snapshot so that MappedRamHeader.page_size becomes 0. Restore the snapshot:
(qemu) migrate_set_capability mapped-ram on
(qemu) migrate_incoming file:/tmp/qemu-bug-test/snapshot.bin

As-is: qemu crashes immediately
(qemu) Floating point exception (core dumped)
To-be: qemu continue running
(qemu) qemu-system-x86_64: Migration mapped-ram header has invalid page_size of 0

---
 migration/ram.c | 5 +++++
 1 file changed, 5 insertions(+)
migration/ram.c +0 -0
index 979751f61b..2a7e958e87 100644

migration/ram.c static bool mapped_ram_read_header(QEMUFile *file, MappedRamHeader *header, · line 3095

Quoted 8 lines
migration/ram.c +5 -0
@@ -3088,6 +3088,11 @@ static bool mapped_ram_read_header(QEMUFile *file, MappedRamHeader *header,
     }
 
     header->page_size = be64_to_cpu(header->page_size);
+    if (header->page_size == 0) {
+        error_setg(errp, "Migration mapped-ram header has invalid "
+                   "page_size of 0");
+        return false;
+    }

Thanks for the patch.

Quoted 4 lines

When at this, one more thing we could do is verify the value is exactly target psize; afaict that's the assumption across the whole mapped-ram feature.

ack, will add it in v2. Thank you.

Quoted 10 lines
Quoted 6 lines
migration/ram.c +0 -0
     header->bitmap_offset = be64_to_cpu(header->bitmap_offset);
     header->pages_offset = be64_to_cpu(header->pages_offset);
 
Signature · 1 line
2.43.0
Signature · 1 line
Peter Xu