Skip to contentSkip to conversation

m25p80: prevent buffer overflow during erasing

PATCH In review Aleksey Kuleshov 2 participants 2 messages Jan 15, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 m25p80: prevent buffer overflow during erasing

memset is not checked, so it's possible to go beyond the storage. Add checks and truncate requested length.

---
 hw/block/m25p80.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/block/m25p80.c +10 -0
index ea142160b3..18ec501912 100644
@@ -539,6 +539,8 @@ static void flash_erase(Flash *s, int offset, FlashCMD cmd)
     uint32_t len;
     uint8_t capa_to_assert = 0;
 
+    assert(0 <= offset && offset < s->size);
+
     switch (cmd) {
     case ERASE_4K:
     case ERASE4_4K:
@@ -581,6 +583,14 @@ static void flash_erase(Flash *s, int offset, FlashCMD cmd)
         qemu_log_mask(LOG_GUEST_ERROR, "M25P80: erase with write protect!\n");
         return;
     }
+
+    if (offset + len > s->size) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                            "M25P80: trying to erase beyond the flash size! "
+                            "Truncating the length...\n");
+        len = s->size - offset;
+    }
+
     memset(s->storage + offset, 0xff, len);
     flash_sync_area(s, offset, len);
 }
Signature · 1 line
2.11.0
Acked-by on v1 1/1
#1 lore raw

W dniu 15.01.2018 o 16:13, Aleksey Kuleshov pisze:

Quoted 4 lines

memset is not checked, so it's possible to go beyond the storage. Add checks and truncate requested length.

Quoted 32 lines
---
  hw/block/m25p80.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
hw/block/m25p80.c +10 -0
index ea142160b3..18ec501912 100644
@@ -539,6 +539,8 @@ static void flash_erase(Flash *s, int offset, FlashCMD cmd)
      uint32_t len;
      uint8_t capa_to_assert = 0;
 
+    assert(0 <= offset && offset < s->size);
+
      switch (cmd) {
      case ERASE_4K:
      case ERASE4_4K:
@@ -581,6 +583,14 @@ static void flash_erase(Flash *s, int offset, FlashCMD cmd)
          qemu_log_mask(LOG_GUEST_ERROR, "M25P80: erase with write protect!\n");
          return;
      }
+
+    if (offset + len > s->size) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                            "M25P80: trying to erase beyond the flash size! "
+                            "Truncating the length...\n");
+        len = s->size - offset;
+    }
+
      memset(s->storage + offset, 0xff, len);
      flash_sync_area(s, offset, len);
  }