From: Chao Liu <[email protected]>

Erase commands are addressed at the containing erase unit; the flash
ignores the low address bits, so an erase in the last erase unit of the
device must not extend past the end of the backing store.

Fixes: 82a2499011a7 ("m25p80: Initial implementation of SPI flash device")
Signed-off-by: Chao Liu <[email protected]>
Signed-off-by: Yifan Wang <[email protected]>
---
 hw/block/m25p80.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/hw/block/m25p80.c b/hw/block/m25p80.c
index baf798c6b5..2a882c392c 100644
--- a/hw/block/m25p80.c
+++ b/hw/block/m25p80.c
@@ -639,6 +639,19 @@ static void flash_erase(Flash *s, int offset, FlashCMD cmd)
         abort();
     }
 
+    /*
+     * The flash ignores the low address bits of erase commands and erases
+     * the containing erase unit. Reject requests whose erase unit is not
+     * entirely within the device.
+     */
+    offset &= ~(len - 1);
+    if (offset + len > s->size) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "M25P80: erase at 0x%x for %u bytes is outside the "
+                      "device (size 0x%x)\n", offset, len, s->size);
+        return;
+    }
+
     trace_m25p80_flash_erase(s, offset, len);
 
     if ((s->pi->flags & capa_to_assert) != capa_to_assert) {
-- 
2.53.0


Reply via email to