hw/core/loader: Fix range checks in rom_copy()
PATCH 1/1 hw/core/loader: Fix range checks in rom_copy()
When a ROM blob runs past the end of the destination, rom_copy() clamps the length to "dest - d" instead of "dest + size - d". The result is negative, so memcpy() gets a huge size_t and QEMU crashes. The same mistake is in the memset() path.
This is easy to hit with a crafted -kernel image. For example, a multiboot ELF whose last segment ends right where the BIOS starts (0xfffc0000 with the default SeaBIOS) crashes qemu-system-i386 at startup.
While at it, treat the range as half-open at both ends: a blob that starts at addr + size now ends the loop, so rom_copy() no longer reports a full copy when nothing was copied, and a blob that ends exactly at addr no longer stops the loop before the blobs inside the range.
Commit e423455c4f23 fixed a similar problem at the start of the range.
--- hw/core/loader.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
hw/core/loader.c +4 -4
@@ -1529,10 +1529,10 @@ int rom_copy(uint8_t *dest, hwaddr addr, size_t size)if (rom->mr) {continue ; } -if (rom->addr + rom->romsize < addr) { +if (rom->addr + rom->romsize <= addr) {continue ; } -if (rom->addr > end || rom->addr < addr) { +if (rom->addr >= end || rom->addr < addr) {break ; }
@@ -1541,7 +1541,7 @@ int rom_copy(uint8_t *dest, hwaddr addr, size_t size) l = rom->datasize;if ((d + l) > (dest + size)) { - l = dest - d; + l = dest + size - d; }if (l >0 ) {
@@ -1560,7 +1560,7 @@ int rom_copy(uint8_t *dest, hwaddr addr, size_t size)/* Rom size doesn't fit in the destination area. Adjust to avoid * overflow. */ - l = dest - d; + l = dest + size - d; }if (l >0 ) {
Signature · 1 line
2.43.0