Skip to contentSkip to conversation

hw/sd/sdcard: Do not attempt to erase out of range addresses

RFC PATCH Superseded Philippe Mathieu-Daudé 6 participants 17 messages Sep 18, 2020 – Oct 15, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover hw/sd/sdcard: Do not attempt to erase out of range addresses

Yet another bug in the sdcard model found by libfuzzer:
https://bugs.launchpad.net/bugs/1895310

The bug is fixed, but there is a migration issue to be resolved... so posting as RFC.

Philippe Mathieu-Daudé (6):
hw/sd/sdcard: Add trace event for ERASE command (CMD38) hw/sd/sdcard: Introduce the INVALID_ADDRESS definition hw/sd/sdcard: Do not use legal address '0' for INVALID_ADDRESS hw/sd/sdcard: Reset both start/end addresses on error hw/sd/sdcard: Do not attempt to erase out of range addresses hw/sd/sdcard: Assert if accessing an illegal group

 hw/sd/sd.c         | 30 ++++++++++++++++++++++--------
 hw/sd/trace-events |  2 +-
 2 files changed, 23 insertions(+), 9 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

RFC PATCH 1/6 hw/sd/sdcard: Add trace event for ERASE command (CMD38)

Trace addresses provided to the ERASE command.

---
 hw/sd/sd.c         | 2 +-
 hw/sd/trace-events | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
hw/sd/sd.c +1 -1
index 00128822224..2606b969e34 100644
@@ -749,7 +749,7 @@ static void sd_erase(SDState *sd)
     uint64_t erase_start = sd->erase_start;
     uint64_t erase_end = sd->erase_end;
 
-    trace_sdcard_erase();
+    trace_sdcard_erase(sd->erase_start, sd->erase_end);
     if (!sd->erase_start || !sd->erase_end) {
         sd->card_status |= ERASE_SEQ_ERROR;
         return;
hw/sd/trace-events +1 -1
index a87d7355fb8..96c7ea5e52f 100644
@@ -46,7 +46,7 @@ sdcard_reset(void) ""
 sdcard_set_blocklen(uint16_t length) "0x%03x"
 sdcard_inserted(bool readonly) "read_only: %u"
 sdcard_ejected(void) ""
-sdcard_erase(void) ""
+sdcard_erase(uint32_t first, uint32_t last) "addr first 0x%" PRIx32" last 0x%" PRIx32
 sdcard_lock(void) ""
 sdcard_unlock(void) ""
 sdcard_read_block(uint64_t addr, uint32_t len) "addr 0x%" PRIx64 " size 0x%x"
Signature · 1 line
2.26.2
#2 lore raw

RFC PATCH 2/6 hw/sd/sdcard: Introduce the INVALID_ADDRESS definition

In reply to Philippe Mathieu-Daudé

'0' is used as a value to indicate an invalid (or unset) address. Use a definition instead of a magic value.

---
 hw/sd/sd.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
hw/sd/sd.c +8 -5
index 2606b969e34..30ae435d669 100644
@@ -53,6 +53,8 @@
 
 #define SDSC_MAX_CAPACITY   (2 * GiB)
 
+#define INVALID_ADDRESS     0
+
 typedef enum {
     sd_r0 = 0,    /* no response */
     sd_r1,        /* normal response command */
@@ -575,8 +577,8 @@ static void sd_reset(DeviceState *dev)
     sd->wpgrps_size = sect;
     sd->wp_groups = bitmap_new(sd->wpgrps_size);
     memset(sd->function_group, 0, sizeof(sd->function_group));
-    sd->erase_start = 0;
-    sd->erase_end = 0;
+    sd->erase_start = INVALID_ADDRESS;
+    sd->erase_end = INVALID_ADDRESS;
     sd->size = size;
     sd->blk_len = 0x200;
     sd->pwd_len = 0;
@@ -750,7 +752,8 @@ static void sd_erase(SDState *sd)
     uint64_t erase_end = sd->erase_end;
 
     trace_sdcard_erase(sd->erase_start, sd->erase_end);
-    if (!sd->erase_start || !sd->erase_end) {
+    if (sd->erase_start == INVALID_ADDRESS
+            || sd->erase_end == INVALID_ADDRESS) {
         sd->card_status |= ERASE_SEQ_ERROR;
         return;
     }
@@ -763,8 +766,8 @@ static void sd_erase(SDState *sd)
 
     erase_start = sd_addr_to_wpnum(erase_start);
     erase_end = sd_addr_to_wpnum(erase_end);
-    sd->erase_start = 0;
-    sd->erase_end = 0;
+    sd->erase_start = INVALID_ADDRESS;
+    sd->erase_end = INVALID_ADDRESS;
     sd->csd[14] |= 0x40;
 
     for (i = erase_start; i <= erase_end; i++) {
Signature · 1 line
2.26.2
#3 lore raw

RFC PATCH 3/6 hw/sd/sdcard: Do not use legal address '0' for INVALID_ADDRESS

In reply to Philippe Mathieu-Daudé

As it is legal to WRITE/ERASE the address/block 0, change the value of this definition to an illegal
address: UINT32_MAX.

---
Cc: Dr. David Alan Gilbert <[email protected]>
Cc: Markus Armbruster <[email protected]>

Same problem I had with the pflash device last year... This break migration :(
What is the best way to do this?

---
 hw/sd/sd.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/sd/sd.c +3 -3
index 30ae435d669..4c05152f189 100644
@@ -53,7 +53,7 @@
 
 #define SDSC_MAX_CAPACITY   (2 * GiB)
 
-#define INVALID_ADDRESS     0
+#define INVALID_ADDRESS     UINT32_MAX
 
 typedef enum {
     sd_r0 = 0,    /* no response */
@@ -666,8 +666,8 @@ static int sd_vmstate_pre_load(void *opaque)
 
 static const VMStateDescription sd_vmstate = {
     .name = "sd-card",
-    .version_id = 1,
-    .minimum_version_id = 1,
+    .version_id = 2,
+    .minimum_version_id = 2,
     .pre_load = sd_vmstate_pre_load,
     .fields = (VMStateField[]) {
         VMSTATE_UINT32(mode, SDState),
Signature · 1 line
2.26.2
#4 lore raw

Re: [RFC PATCH 3/6] hw/sd/sdcard: Do not use legal address '0' for INVALID_ADDRESS

Quoted 4 lines · Philippe Mathieu-Daudé

As it is legal to WRITE/ERASE the address/block 0, change the value of this definition to an illegal
address: UINT32_MAX.

Quoted 8 lines

---
Cc: Dr. David Alan Gilbert <[email protected]>
Cc: Markus Armbruster <[email protected]>

Same problem I had with the pflash device last year... This break migration :(
What is the best way to do this?

Remind me: did we solve the problem with pflash, and if yes, how?

Quoted 28 lines
---
 hw/sd/sd.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/sd/sd.c +3 -3
index 30ae435d669..4c05152f189 100644
@@ -53,7 +53,7 @@
 
 #define SDSC_MAX_CAPACITY   (2 * GiB)
 
-#define INVALID_ADDRESS     0
+#define INVALID_ADDRESS     UINT32_MAX
 
 typedef enum {
     sd_r0 = 0,    /* no response */
@@ -666,8 +666,8 @@ static int sd_vmstate_pre_load(void *opaque)
 
 static const VMStateDescription sd_vmstate = {
     .name = "sd-card",
-    .version_id = 1,
-    .minimum_version_id = 1,
+    .version_id = 2,
+    .minimum_version_id = 2,
     .pre_load = sd_vmstate_pre_load,
     .fields = (VMStateField[]) {
         VMSTATE_UINT32(mode, SDState),
#5 lore raw

Re: [RFC PATCH 3/6] hw/sd/sdcard: Do not use legal address '0' for INVALID_ADDRESS

+Paolo & Kevin.

Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Philippe Mathieu-Daudé

As it is legal to WRITE/ERASE the address/block 0, change the value of this definition to an illegal
address: UINT32_MAX.

---
Cc: Dr. David Alan Gilbert <[email protected]>
Cc: Markus Armbruster <[email protected]>

Quoted 5 lines
Quoted 3 lines

Same problem I had with the pflash device last year... This break migration :(
What is the best way to do this?

Remind me: did we solve the problem with pflash, and if yes, how?

No we can't. The best I could do is add a comment and as this is not fixable. See commit aba53a12bd5: ("hw/block/pflash_cfi01:
Document use of non-CFI compliant command '0x00'").

I now consider the device in maintenance-only
mode and won't add any new features.

I started working on a new implementation, hoping it can be a drop in replacement. Laszlo still has hope that QEMU pflash device will support sector locking so firmware developers could test upgrading fw in VMs.

Back to the SDcard, it might be less critical, so a migration breaking change might be acceptable. I'm only aware of Paolo and Kevin using this device for testing. Not sure of its importance in production.

Quoted 31 lines
Quoted 29 lines
---
 hw/sd/sd.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/sd/sd.c +3 -3
index 30ae435d669..4c05152f189 100644
@@ -53,7 +53,7 @@
 
 #define SDSC_MAX_CAPACITY   (2 * GiB)
 
-#define INVALID_ADDRESS     0
+#define INVALID_ADDRESS     UINT32_MAX
 
 typedef enum {
     sd_r0 = 0,    /* no response */
@@ -666,8 +666,8 @@ static int sd_vmstate_pre_load(void *opaque)
 
 static const VMStateDescription sd_vmstate = {
     .name = "sd-card",
-    .version_id = 1,
-    .minimum_version_id = 1,
+    .version_id = 2,
+    .minimum_version_id = 2,
     .pre_load = sd_vmstate_pre_load,
     .fields = (VMStateField[]) {
         VMSTATE_UINT32(mode, SDState),
#6 lore raw

Re: [RFC PATCH 3/6] hw/sd/sdcard: Do not use legal address '0' for INVALID_ADDRESS

Quoted 32 lines · Philippe Mathieu-Daudé

+Paolo & Kevin.

Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Philippe Mathieu-Daudé

As it is legal to WRITE/ERASE the address/block 0, change the value of this definition to an illegal
address: UINT32_MAX.

---
Cc: Dr. David Alan Gilbert <[email protected]>
Cc: Markus Armbruster <[email protected]>

Quoted 5 lines
Quoted 3 lines

Same problem I had with the pflash device last year... This break migration :(
What is the best way to do this?

Remind me: did we solve the problem with pflash, and if yes, how?

No we can't. The best I could do is add a comment and as this is not fixable. See commit aba53a12bd5: ("hw/block/pflash_cfi01:
Document use of non-CFI compliant command '0x00'").

I now consider the device in maintenance-only
mode and won't add any new features.

I started working on a new implementation, hoping it can be a drop in replacement. Laszlo still has hope that QEMU pflash device will support sector locking so firmware developers could test upgrading fw in VMs.

Quoted 4 lines

Back to the SDcard, it might be less critical, so a migration breaking change might be acceptable. I'm only aware of Paolo and Kevin using this device for testing. Not sure of its importance in production.

Neither am I.

Which machine types include this device by default?

How can a non-default device be added, and to which machine types?

I gather the fix changes device state incompatibly. Always, or only in certain states? I'm asking because if device state remains compatible most of the time, we might be able use subsection trickery to keep migration working most of the time. Has been done before, I think.

#7 lore raw

Re: [RFC PATCH 3/6] hw/sd/sdcard: Do not use legal address '0' for INVALID_ADDRESS

Quoted 34 lines · * Markus Armbruster ([email protected]) wrote:
Quoted 32 lines · Philippe Mathieu-Daudé

+Paolo & Kevin.

Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Philippe Mathieu-Daudé

As it is legal to WRITE/ERASE the address/block 0, change the value of this definition to an illegal
address: UINT32_MAX.

---
Cc: Dr. David Alan Gilbert <[email protected]>
Cc: Markus Armbruster <[email protected]>

Quoted 5 lines
Quoted 3 lines

Same problem I had with the pflash device last year... This break migration :(
What is the best way to do this?

Remind me: did we solve the problem with pflash, and if yes, how?

No we can't. The best I could do is add a comment and as this is not fixable. See commit aba53a12bd5: ("hw/block/pflash_cfi01:
Document use of non-CFI compliant command '0x00'").

I now consider the device in maintenance-only
mode and won't add any new features.

I started working on a new implementation, hoping it can be a drop in replacement. Laszlo still has hope that QEMU pflash device will support sector locking so firmware developers could test upgrading fw in VMs.

Quoted 8 lines
Quoted 4 lines

Back to the SDcard, it might be less critical, so a migration breaking change might be acceptable. I'm only aware of Paolo and Kevin using this device for testing. Not sure of its importance in production.

Neither am I.

Which machine types include this device by default?

To me it looks like it's some of the ARM boards.

Dave

Quoted 6 lines

How can a non-default device be added, and to which machine types?

I gather the fix changes device state incompatibly. Always, or only in certain states? I'm asking because if device state remains compatible most of the time, we might be able use subsection trickery to keep migration working most of the time. Has been done before, I think.

Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
#8 lore raw

Re: [RFC PATCH 3/6] hw/sd/sdcard: Do not use legal address '0' for INVALID_ADDRESS

Quoted 40 lines · Dr. David Alan Gilbert
Quoted 39 lines · * Markus Armbruster ([email protected]) wrote:
Quoted 36 lines · Philippe Mathieu-Daudé

+Paolo & Kevin.

Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Philippe Mathieu-Daudé

As it is legal to WRITE/ERASE the address/block 0, change the value of this definition to an illegal
address: UINT32_MAX.

---
Cc: Dr. David Alan Gilbert <[email protected]>
Cc: Markus Armbruster <[email protected]>

Quoted 5 lines
Quoted 3 lines

Same problem I had with the pflash device last year... This break migration :(
What is the best way to do this?

Remind me: did we solve the problem with pflash, and if yes, how?

No we can't. The best I could do is add a comment and as this is not fixable. See commit aba53a12bd5: ("hw/block/pflash_cfi01:
Document use of non-CFI compliant command '0x00'").

I now consider the device in maintenance-only
mode and won't add any new features.

I started working on a new implementation, hoping it can be a drop in replacement. Laszlo still has hope that QEMU pflash device will support sector locking so firmware developers could test upgrading fw in VMs.

Back to the SDcard, it might be less critical, so a migration breaking change might be acceptable. I'm only aware of Paolo and Kevin using this device for testing. Not sure of its importance in production.

Quoted 5 lines
Quoted 3 lines

Neither am I.

Which machine types include this device by default?

To me it looks like it's some of the ARM boards.

My worry is TYPE_PCI_SDHCI ("sdhci-pci"):

    k->vendor_id = PCI_VENDOR_ID_REDHAT;
    k->device_id = PCI_DEVICE_ID_REDHAT_SDHCI;
    k->class_id = PCI_CLASS_SYSTEM_SDHCI;

config SDHCI_PCI
bool
default y if PCI_DEVICES

Quoted 9 lines

Dave

Quoted 6 lines

How can a non-default device be added, and to which machine types?

I gather the fix changes device state incompatibly. Always, or only in certain states? I'm asking because if device state remains compatible most of the time, we might be able use subsection trickery to keep migration working most of the time. Has been done before, I think.

#9 lore raw

Re: [RFC PATCH 3/6] hw/sd/sdcard: Do not use legal address '0' for INVALID_ADDRESS

Quoted 49 lines · Philippe Mathieu-Daudé
Quoted 40 lines · Dr. David Alan Gilbert
Quoted 39 lines · * Markus Armbruster ([email protected]) wrote:
Quoted 36 lines · Philippe Mathieu-Daudé

+Paolo & Kevin.

Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Philippe Mathieu-Daudé

As it is legal to WRITE/ERASE the address/block 0, change the value of this definition to an illegal
address: UINT32_MAX.

---
Cc: Dr. David Alan Gilbert <[email protected]>
Cc: Markus Armbruster <[email protected]>

Quoted 5 lines
Quoted 3 lines

Same problem I had with the pflash device last year... This break migration :(
What is the best way to do this?

Remind me: did we solve the problem with pflash, and if yes, how?

No we can't. The best I could do is add a comment and as this is not fixable. See commit aba53a12bd5: ("hw/block/pflash_cfi01:
Document use of non-CFI compliant command '0x00'").

I now consider the device in maintenance-only
mode and won't add any new features.

I started working on a new implementation, hoping it can be a drop in replacement. Laszlo still has hope that QEMU pflash device will support sector locking so firmware developers could test upgrading fw in VMs.

Back to the SDcard, it might be less critical, so a migration breaking change might be acceptable. I'm only aware of Paolo and Kevin using this device for testing. Not sure of its importance in production.

Quoted 5 lines
Quoted 3 lines

Neither am I.

Which machine types include this device by default?

To me it looks like it's some of the ARM boards.

My worry is TYPE_PCI_SDHCI ("sdhci-pci"):

Quoted 7 lines
    k->vendor_id = PCI_VENDOR_ID_REDHAT;
    k->device_id = PCI_DEVICE_ID_REDHAT_SDHCI;
    k->class_id = PCI_CLASS_SYSTEM_SDHCI;

config SDHCI_PCI
bool
default y if PCI_DEVICES

Ah, now I remember. Not the first time I wished it wouldn't exist...

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

How can a non-default device be added, and to which machine types?

I gather the fix changes device state incompatibly. Always, or only in certain states?

I think we need to answer this question.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I'm asking because if device state remains compatible
most of the time, we might be able use subsection trickery to keep migration working most of the time. Has been done before, I think.

#10 lore raw

Re: [RFC PATCH 3/6] hw/sd/sdcard: Do not use legal address '0' for INVALID_ADDRESS

I think we can just bite the bullet and bump the version number. Just like not all boards are created equal in terms of migration compatibility, neither are all devices.

Unfortunately pflash is among those that need some care, but we have much more leeway with sdhci-pci.

Paolo

Il lun 21 set 2020, 17:08 Markus Armbruster <[email protected]> ha scritto:

Quoted 73 lines
Quoted 49 lines · Philippe Mathieu-Daudé
Quoted 40 lines · Dr. David Alan Gilbert
Quoted 39 lines · * Markus Armbruster ([email protected]) wrote:
Quoted 36 lines · Philippe Mathieu-Daudé

+Paolo & Kevin.

Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Philippe Mathieu-Daudé

As it is legal to WRITE/ERASE the address/block 0, change the value of this definition to an illegal
address: UINT32_MAX.

---
Cc: Dr. David Alan Gilbert <[email protected]>
Cc: Markus Armbruster <[email protected]>

Quoted 5 lines
Quoted 3 lines

Same problem I had with the pflash device last year... This break migration :(
What is the best way to do this?

Remind me: did we solve the problem with pflash, and if yes, how?

No we can't. The best I could do is add a comment and as this is not fixable. See commit aba53a12bd5: ("hw/block/pflash_cfi01:
Document use of non-CFI compliant command '0x00'").

I now consider the device in maintenance-only
mode and won't add any new features.

I started working on a new implementation, hoping it can be a drop in replacement. Laszlo still has hope that QEMU pflash device will support sector locking so firmware developers could test upgrading fw in VMs.

Back to the SDcard, it might be less critical, so a migration breaking change might be acceptable. I'm only aware of Paolo and Kevin using this device for testing. Not sure of its importance in production.

Quoted 5 lines
Quoted 3 lines

Neither am I.

Which machine types include this device by default?

To me it looks like it's some of the ARM boards.

My worry is TYPE_PCI_SDHCI ("sdhci-pci"):

Quoted 7 lines
    k->vendor_id = PCI_VENDOR_ID_REDHAT;
    k->device_id = PCI_DEVICE_ID_REDHAT_SDHCI;
    k->class_id = PCI_CLASS_SYSTEM_SDHCI;

config SDHCI_PCI
bool
default y if PCI_DEVICES

Ah, now I remember. Not the first time I wished it wouldn't exist...

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

How can a non-default device be added, and to which machine types?

I gather the fix changes device state incompatibly. Always, or only in certain states?

I think we need to answer this question.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I'm asking because if device state remains compatible
most of the time, we might be able use subsection trickery to keep migration working most of the time. Has been done before, I think.

#11 lore raw

Re: [RFC PATCH 3/6] hw/sd/sdcard: Do not use legal address '0' for INVALID_ADDRESS

Quoted 6 lines · Paolo Bonzini

I think we can just bite the bullet and bump the version number. Just like not all boards are created equal in terms of migration compatibility, neither are all devices.

Unfortunately pflash is among those that need some care, but we have much more leeway with sdhci-pci.

No objection.

#12 lore raw

Re: [RFC PATCH 3/6] hw/sd/sdcard: Do not use legal address '0' for INVALID_ADDRESS

Quoted 5 lines · Markus Armbruster
Quoted 3 lines · Paolo Bonzini

I think we can just bite the bullet and bump the version number. Just like not all boards are created equal in terms of migration compatibility, neither are all devices.

Great. I'll add that to the commit description.

Quoted 6 lines
Quoted 3 lines

Unfortunately pflash is among those that need some care, but we have much more leeway with sdhci-pci.

No objection.

#13 lore raw

Re: [RFC PATCH 3/6] hw/sd/sdcard: Do not use legal address '0' for INVALID_ADDRESS

In reply to Philippe Mathieu-Daudé
Quoted 4 lines · Philippe Mathieu-Daudé

Back to the SDcard, it might be less critical, so a migration breaking change might be acceptable. I'm only aware of Paolo and Kevin using this device for testing. Not sure of its importance in production.

FWIW, I only use the sdcard for testing (and only use sdhci-pci). I don't know if others use it in production, however.

Cheers,
-Kevin

#14 lore raw

RFC PATCH 4/6 hw/sd/sdcard: Reset both start/end addresses on error

In reply to Philippe Mathieu-Daudé

From the Spec "4.3.5 Erase":

The host should adhere to the following command
sequence: ERASE_WR_BLK_START, ERASE_WR_BLK_END and ERASE (CMD38).

If an erase (CMD38) or address setting (CMD32, 33) command is received out of sequence, the card shall set the ERASE_SEQ_ERROR bit in the status register and reset the whole sequence.

Reset both addresses if the ERASE command occured
out of sequence (one of the start/end address is
not set).

---
 hw/sd/sd.c | 2 ++
 1 file changed, 2 insertions(+)
hw/sd/sd.c +2 -0
index 4c05152f189..ee7b64023aa 100644
@@ -755,6 +755,8 @@ static void sd_erase(SDState *sd)
     if (sd->erase_start == INVALID_ADDRESS
             || sd->erase_end == INVALID_ADDRESS) {
         sd->card_status |= ERASE_SEQ_ERROR;
+        sd->erase_start = INVALID_ADDRESS;
+        sd->erase_end = INVALID_ADDRESS;
         return;
     }
 
Signature · 1 line
2.26.2
#15 lore raw

RFC PATCH 5/6 hw/sd/sdcard: Do not attempt to erase out of range addresses

In reply to Philippe Mathieu-Daudé

While the Spec v3 is not very clear, v6 states:

If the host provides an out of range address as an argument to CMD32 or CMD33, the card shall indicate OUT_OF_RANGE error in R1 (ERX) for CMD38.

If an address is out of range, do not attempt to erase it:
return R1 with the error bit set.

---
 hw/sd/sd.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/sd/sd.c +7 -0
index ee7b64023aa..4454d168e2f 100644
@@ -766,6 +766,13 @@ static void sd_erase(SDState *sd)
         erase_end *= 512;
     }
 
+    if (sd->erase_start > sd->size || sd->erase_end > sd->size) {
+        sd->card_status |= OUT_OF_RANGE;
+        sd->erase_start = INVALID_ADDRESS;
+        sd->erase_end = INVALID_ADDRESS;
+        return;
+    }
+
     erase_start = sd_addr_to_wpnum(erase_start);
     erase_end = sd_addr_to_wpnum(erase_end);
     sd->erase_start = INVALID_ADDRESS;
Signature · 1 line
2.26.2
#16 lore raw

RFC PATCH 6/6 hw/sd/sdcard: Assert if accessing an illegal group

In reply to Philippe Mathieu-Daudé

We can not have more group than 'wpgrps_size'. Assert if we are accessing a group above this limit.

---
 hw/sd/sd.c | 2 ++
 1 file changed, 2 insertions(+)
hw/sd/sd.c +2 -0
index 4454d168e2f..c3febed2434 100644
@@ -780,6 +780,7 @@ static void sd_erase(SDState *sd)
     sd->csd[14] |= 0x40;
 
     for (i = erase_start; i <= erase_end; i++) {
+        assert(i < sd->wpgrps_size);
         if (test_bit(i, sd->wp_groups)) {
             sd->card_status |= WP_ERASE_SKIP;
         }
@@ -794,6 +795,7 @@ static uint32_t sd_wpbits(SDState *sd, uint64_t addr)
     wpnum = sd_addr_to_wpnum(addr);
 
     for (i = 0; i < 32; i++, wpnum++, addr += WPGROUP_SIZE) {
+        assert(wpnum < sd->wpgrps_size);
         if (addr < sd->size && test_bit(wpnum, sd->wp_groups)) {
             ret |= (1 << i);
         }
Signature · 1 line
2.26.2