Skip to contentSkip to conversation

hw/acpi/erst.c: Fix memset argument order

PATCH In review Christian A. Ehrhardt 6 participants 18 messages Oct 19, 2022 – Oct 25, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/acpi/erst.c: Fix memset argument order

Fix memset argument order: The second argument is
the value, the length goes last.

Cc Eric DeVolder <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")
---
 hw/acpi/erst.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/acpi/erst.c +1 -1
index df856b2669..26391f93ca 100644
@@ -716,7 +716,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
     if (nvram) {
         /* Write the record into the slot */
         memcpy(nvram, exchange, record_length);
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);
         /* If a new record, increment the record_count */
         if (!record_found) {
             uint32_t record_count;
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 16 lines · Christian A. Ehrhardt

Fix memset argument order: The second argument is
the value, the length goes last.

Cc Eric DeVolder <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")
---
  hw/acpi/erst.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/acpi/erst.c +0 -0
index df856b2669..26391f93ca 100644

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s) · line 719

Quoted 5 lines
hw/acpi/erst.c +1 -1
@@ -716,7 +716,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
      if (nvram) {
          /* Write the record into the slot */
          memcpy(nvram, exchange, record_length);
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);

Ouch

Reviewed-by
#2 lore raw
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Christian A. Ehrhardt

Fix memset argument order: The second argument is
the value, the length goes last.

Cc Eric DeVolder <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")

---
  hw/acpi/erst.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 11 lines

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s) · line 719

Quoted 10 lines
hw/acpi/erst.c +1 -1
index df856b2669..26391f93ca 100644
@@ -716,7 +716,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
       if (nvram) {
           /* Write the record into the slot */
           memcpy(nvram, exchange, record_length);
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);

Ouch

Sheesh, I'd hate to be that guy...

Quoted 3 lines
#3 lore raw
In reply to Christian A. Ehrhardt
Quoted 2 lines · Christian A. Ehrhardt

Fix memset argument order: The second argument is
the value, the length goes last.

Impact of the bug?

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s)

Quoted 13 lines
Cc Eric DeVolder <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")
---
 hw/acpi/erst.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/acpi/erst.c +0 -0
index df856b2669..26391f93ca 100644
@@ -716,7 +716,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
       exchange_length = memory_region_size(&s->exchange_mr);

This is the size of the exchange buffer.

Aside: it's unsigned int, but memory_region_size() returns uint64_t. Unclean if it fits, bug if it doesn't.

       /* Validate record_offset */
       if (s->record_offset > (exchange_length - UEFI_CPER_RECORD_MIN_SIZE)) {
           return STATUS_FAILED;
       }

       /* Obtain pointer to record in the exchange buffer */
       exchange = memory_region_get_ram_ptr(&s->exchange_mr);
       exchange += s->record_offset;

       /* Validate CPER record_length */
       memcpy((uint8_t *)&record_length, &exchange[UEFI_CPER_RECORD_LENGTH_OFFSET],
           sizeof(uint32_t));

Aside: record_length = *(uint32_t *)exchange[UEFI_CPER_RECORD_LENGTH_OFFSET] would do, since UEFI_CPER_RECORD_LENGTH_OFFSET is a multiple of 4.

       record_length = le32_to_cpu(record_length);
       if (record_length < UEFI_CPER_RECORD_MIN_SIZE) {
           return STATUS_FAILED;
       }
       if ((s->record_offset + record_length) > exchange_length) {
           return STATUS_FAILED;
       }

This ensures there are at least @record_length bytes of space left in the exchange buffer. Good.

       [...]

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s) · line 718

Quoted 3 lines
hw/acpi/erst.c +0 -0
     if (nvram) {
         /* Write the record into the slot */
         memcpy(nvram, exchange, record_length);

This first copies @record_length bytes into the exchange buffer.

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s) · line 719

Quoted 2 lines
hw/acpi/erst.c +1 -1
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);

The new code pads it to the full exchange buffer size.

The old code writes 0xFF bytes.

If 0xFF < exchange_length - record_length, the padding doesn't extend to the end of the buffer. Impact?

If 0xFF > exchange_length - record_length, we write beyond the end of the buffer. Impact?

Quoted 3 lines
hw/acpi/erst.c +0 -0
         /* If a new record, increment the record_count */
         if (!record_found) {
             uint32_t record_count;
#4 lore raw

Hi Markus,

Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Christian A. Ehrhardt

Fix memset argument order: The second argument is
the value, the length goes last.

Impact of the bug?

Well, this is a memory error, i.e. the potential impact is anything from silent data corruption to arbitrary code execution. Phillipe described this accurately as "Ouch".

Quoted 5 lines
Quoted 5 lines
Cc Eric DeVolder <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")
         /* Write the record into the slot */

[ ... ]

Quoted 11 lines

This first copies @record_length bytes into the exchange buffer.

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s) · line 719

Quoted 2 lines
hw/acpi/erst.c +1 -1
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);

The new code pads it to the full exchange buffer size.

The old code writes 0xFF bytes.

If 0xFF < exchange_length - record_length, the padding doesn't extend to the end of the buffer. Impact?

Incorrect and insufficient data is written.

Quoted 2 lines

If 0xFF > exchange_length - record_length, we write beyond the end of the buffer. Impact?

Buffer overrun with well known potentially catastrophic consequences. Additionally, incorrect data is used for the padding.

regards Christian

#5 lore raw
Quoted 34 lines · Christian A. Ehrhardt

Hi Markus,

Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Christian A. Ehrhardt

Fix memset argument order: The second argument is
the value, the length goes last.

Impact of the bug?

Well, this is a memory error, i.e. the potential impact is anything from silent data corruption to arbitrary code execution. Phillipe described this accurately as "Ouch".

Quoted 5 lines
Quoted 5 lines
Cc Eric DeVolder <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")
         /* Write the record into the slot */

[ ... ]

Quoted 11 lines

This first copies @record_length bytes into the exchange buffer.

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s) · line 719

Quoted 2 lines
hw/acpi/erst.c +1 -1
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);

The new code pads it to the full exchange buffer size.

The old code writes 0xFF bytes.

If 0xFF < exchange_length - record_length, the padding doesn't extend to the end of the buffer. Impact?

Quoted 7 lines

Incorrect and insufficient data is written.

Quoted 2 lines

If 0xFF > exchange_length - record_length, we write beyond the end of the buffer. Impact?

Buffer overrun with well known potentially catastrophic consequences. Additionally, incorrect data is used for the padding.

Is record_length controlled by the guest?

#6 lore raw
Quoted 38 lines · Markus Armbruster
Quoted 36 lines · Christian A. Ehrhardt

Hi Markus,

Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Christian A. Ehrhardt

Fix memset argument order: The second argument is
the value, the length goes last.

Impact of the bug?

Well, this is a memory error, i.e. the potential impact is anything from silent data corruption to arbitrary code execution. Phillipe described this accurately as "Ouch".

Quoted 5 lines
Quoted 5 lines
Cc Eric DeVolder <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")
         /* Write the record into the slot */

[ ... ]

Quoted 11 lines

This first copies @record_length bytes into the exchange buffer.

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s) · line 719

Quoted 2 lines
hw/acpi/erst.c +1 -1
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);

The new code pads it to the full exchange buffer size.

The old code writes 0xFF bytes.

If 0xFF < exchange_length - record_length, the padding doesn't extend to the end of the buffer. Impact?

Incorrect and insufficient data is written.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

If 0xFF > exchange_length - record_length, we write beyond the end of the buffer. Impact?

Buffer overrun with well known potentially catastrophic consequences. Additionally, incorrect data is used for the padding.

Is record_length controlled by the guest?

Yes, it is taken from the CPER header in the exchange store.

regards Christian

#7 lore raw
In reply to Markus Armbruster
Quoted 40 lines · Markus Armbruster
Quoted 2 lines · Christian A. Ehrhardt

Fix memset argument order: The second argument is
the value, the length goes last.

Impact of the bug?

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s)

Quoted 13 lines
Cc Eric DeVolder <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")
---
  hw/acpi/erst.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/acpi/erst.c +0 -0
index df856b2669..26391f93ca 100644
@@ -716,7 +716,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
        exchange_length = memory_region_size(&s->exchange_mr);

This is the size of the exchange buffer.

Aside: it's unsigned int, but memory_region_size() returns uint64_t. Unclean if it fits, bug if it doesn't.

        /* Validate record_offset */
        if (s->record_offset > (exchange_length - UEFI_CPER_RECORD_MIN_SIZE)) {
            return STATUS_FAILED;
        }

        /* Obtain pointer to record in the exchange buffer */
        exchange = memory_region_get_ram_ptr(&s->exchange_mr);
        exchange += s->record_offset;

        /* Validate CPER record_length */
        memcpy((uint8_t *)&record_length, &exchange[UEFI_CPER_RECORD_LENGTH_OFFSET],
            sizeof(uint32_t));
Quoted 2 lines

Aside: record_length = *(uint32_t *)exchange[UEFI_CPER_RECORD_LENGTH_OFFSET] would do, since UEFI_CPER_RECORD_LENGTH_OFFSET is a multiple of 4.

Igor requested I use memcpy() so that this would work on EB and EL hosts.

Quoted 22 lines
        record_length = le32_to_cpu(record_length);
        if (record_length < UEFI_CPER_RECORD_MIN_SIZE) {
            return STATUS_FAILED;
        }
        if ((s->record_offset + record_length) > exchange_length) {
            return STATUS_FAILED;
        }

This ensures there are at least @record_length bytes of space left in the exchange buffer. Good.

        [...]

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s) · line 718

Quoted 3 lines
hw/acpi/erst.c +0 -0
      if (nvram) {
          /* Write the record into the slot */
          memcpy(nvram, exchange, record_length);

This first copies @record_length bytes into the exchange buffer.

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s) · line 719

Quoted 2 lines
hw/acpi/erst.c +1 -1
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);
Quoted 6 lines

The new code pads it to the full exchange buffer size.

The old code writes 0xFF bytes.

If 0xFF < exchange_length - record_length, the padding doesn't extend to the end of the buffer. Impact?

The purpose of the memset() is to ensure the slot does not contain any remnants of a previous record. There is no functional requirement for this; other than it was intended to prevent the possibility of leaking data.

If there were a previously deleted/overwritten record in that slot, then the tail of that record would remain. However, it still isn't visible upon the record read; it would only be visible by directly accessing the backing file/memory.

Quoted 3 lines

If 0xFF > exchange_length - record_length, we write beyond the end of the buffer. Impact?

There are two cases here, if the record is stored in any slot but the last, then it has the opportunity to corrupt the next adjacent slot/record. Given that the CPER format places the magic 'CPER' as the first 4 bytes, then I believe that upon next read of this corrupted record, it will be rejected as it does not have a valid CPER header.

If the record is the last in the backing storage, then it would attempt to write beyond the end. The backing store is memory mapped into the guest, so I believe that an attempt to write beyond the end will result in a segfault.

Previously stated: "Well, this is a memory error, i.e. the potential impact is anything from silent data corruption to arbitrary code execution. Phillipe described this accurately as "Ouch".

Yes, ouch. I had it correct until patch series v7 (of v15); not that that is helpful.

However, I do not see a path to arbitrary code execution. The erroneous memset() will write out a constant value (exchange_length - record_length) for 0xFF bytes.

In terms of current Linux real world impact, ERST is used as pstore backend and writes to pstore typically only happen on kernel panic, if configured. Furthermore, the systemd-pstore service attempts to keep the pstore empty, so that reduces the chances of pstore/ERST filling up and reaching that last slot that could cause a segfault.

ERST can be written by MCE, I think; not sure how relevant that is to guests.

eric

Quoted 5 lines
Quoted 4 lines
hw/acpi/erst.c +0 -0
          /* If a new record, increment the record_count */
          if (!record_found) {
              uint32_t record_count;
Reviewed-by on v1 1/1
#8 lore raw
In reply to Christian A. Ehrhardt
Quoted 16 lines · Christian A. Ehrhardt

Fix memset argument order: The second argument is
the value, the length goes last.

Cc Eric DeVolder <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")
---
 hw/acpi/erst.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/acpi/erst.c +0 -0
index df856b2669..26391f93ca 100644

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s) · line 719

Quoted 5 lines
hw/acpi/erst.c +1 -1
@@ -716,7 +716,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
     if (nvram) {
         /* Write the record into the slot */
         memcpy(nvram, exchange, record_length);
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);

Hi,
I'm running the fuzzer over this code. So far, it hasn't complained about this particular memset call, but it has crashed on the memcpy, directly preceding it. I think the record_length checks might be insufficient. I made an issue/reproducer:
https://gitlab.com/qemu-project/qemu/-/issues/1268

In that particular case, record_length is ffffff00 and passes the checks. I'll keep an eye on the fuzzer to see if it will be able to crash the memset.

For this patch:

Quoted 7 lines
hw/acpi/erst.c +0 -0
         /* If a new record, increment the record_count */
         if (!record_found) {
             uint32_t record_count;
Signature · 1 line
2.34.1
#9 lore raw
Quoted 30 lines · Alexander Bulekov
Quoted 16 lines · Christian A. Ehrhardt

Fix memset argument order: The second argument is
the value, the length goes last.

Cc Eric DeVolder <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")
---
 hw/acpi/erst.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/acpi/erst.c +0 -0
index df856b2669..26391f93ca 100644

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s) · line 719

Quoted 5 lines
hw/acpi/erst.c +1 -1
@@ -716,7 +716,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
     if (nvram) {
         /* Write the record into the slot */
         memcpy(nvram, exchange, record_length);
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);

Hi,
I'm running the fuzzer over this code. So far, it hasn't complained about this particular memset call, but it has crashed on the memcpy, directly preceding it. I think the record_length checks might be insufficient. I made an issue/reproducer:
https://gitlab.com/qemu-project/qemu/-/issues/1268

Quoted 3 lines

In that particular case, record_length is ffffff00 and passes the checks. I'll keep an eye on the fuzzer to see if it will be able to crash the memset.

Here's a testcase for the memset issue:

cat << EOF | ./qemu-system-i386 -display none -machine accel=qtest, -m \
512M -object memory-backend-ram,id=erstnvram,size=0x10000 -device \
acpi-erst,memdev=erstnvram -nodefaults -qtest stdio
outl 0xcf8 0x80001010
outl 0xcfc 0xe0000000
outl 0xcf8 0x80001014
outl 0xcfc 0xe0002000
outl 0xcf8 0x80001004
outw 0xcfc 0x02
write 0xe0000008 0x1 0x9c
write 0xe0002015 0x1 0x01
write 0xe0002066 0x1 0x02
write 0xe0000000 0x1 0x05
write 0xe0002066 0x1 0x04
write 0xe0000000 0x1 0x05
write 0xe0002066 0x1 0x01
write 0xe0000000 0x1 0x05
write 0xe0002065 0x1 0x01
write 0xe0000000 0x1 0x05
write 0xe0002066 0x1 0x02
write 0xe0000000 0x1 0x05
write 0xe0002066 0x1 0x03
write 0xe0000000 0x1 0x05
write 0xe0002015 0x1 0x20
write 0xe0002066 0x1 0x00
write 0xe0000000 0x1 0x05
EOF

Quoted 11 lines

For this patch:

Quoted 7 lines
hw/acpi/erst.c +0 -0
         /* If a new record, increment the record_count */
         if (!record_found) {
             uint32_t record_count;
Signature · 1 line
2.34.1
#10 lore raw

Hi,

Quoted 31 lines · Alexander Bulekov
Quoted 30 lines · Alexander Bulekov
Quoted 16 lines · Christian A. Ehrhardt

Fix memset argument order: The second argument is
the value, the length goes last.

Cc Eric DeVolder <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")
---
 hw/acpi/erst.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/acpi/erst.c +0 -0
index df856b2669..26391f93ca 100644

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s) · line 719

Quoted 5 lines
hw/acpi/erst.c +1 -1
@@ -716,7 +716,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
     if (nvram) {
         /* Write the record into the slot */
         memcpy(nvram, exchange, record_length);
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);

Hi,
I'm running the fuzzer over this code. So far, it hasn't complained about this particular memset call, but it has crashed on the memcpy, directly preceding it. I think the record_length checks might be insufficient. I made an issue/reproducer:
https://gitlab.com/qemu-project/qemu/-/issues/1268

Quoted 5 lines
Quoted 3 lines

In that particular case, record_length is ffffff00 and passes the checks. I'll keep an eye on the fuzzer to see if it will be able to crash the memset.

Here's a testcase for the memset issue:

Looks like this check in {read,write}_erst_record():

|   if ((s->record_offset + record_length) > exchange_length) {
|       return STATUS_FAILED;
|   }

has an integer overrun and should be re-written as:

|   if (record_length > exchange_length - s->record_offset) {
|       return STATUS_FAILED;
|   }

regards Christian

#11 lore raw
Quoted 46 lines · Christian A. Ehrhardt

Hi,

Quoted 31 lines · Alexander Bulekov
Quoted 30 lines · Alexander Bulekov
Quoted 16 lines · Christian A. Ehrhardt

Fix memset argument order: The second argument is
the value, the length goes last.

Cc Eric DeVolder <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")
---
 hw/acpi/erst.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/acpi/erst.c +0 -0
index df856b2669..26391f93ca 100644

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s) · line 719

Quoted 5 lines
hw/acpi/erst.c +1 -1
@@ -716,7 +716,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
     if (nvram) {
         /* Write the record into the slot */
         memcpy(nvram, exchange, record_length);
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);

Hi,
I'm running the fuzzer over this code. So far, it hasn't complained about this particular memset call, but it has crashed on the memcpy, directly preceding it. I think the record_length checks might be insufficient. I made an issue/reproducer:
https://gitlab.com/qemu-project/qemu/-/issues/1268

Quoted 5 lines
Quoted 3 lines

In that particular case, record_length is ffffff00 and passes the checks. I'll keep an eye on the fuzzer to see if it will be able to crash the memset.

Here's a testcase for the memset issue:

Looks like this check in {read,write}_erst_record():

|   if ((s->record_offset + record_length) > exchange_length) {
|       return STATUS_FAILED;
|   }
Quoted 6 lines

has an integer overrun and should be re-written as:

|   if (record_length > exchange_length - s->record_offset) {
|       return STATUS_FAILED;
|   }

regards Christian

Hi Christian,
With this change applied (along with the memset fix), the fuzzer doesn't find any crashes.
Thanks
-Alex

#12 lore raw
Quoted 52 lines · Alexander Bulekov
Quoted 51 lines · Christian A. Ehrhardt

Hi,

Quoted 31 lines · Alexander Bulekov
Quoted 30 lines · Alexander Bulekov
Quoted 16 lines · Christian A. Ehrhardt

Fix memset argument order: The second argument is
the value, the length goes last.

Cc Eric DeVolder <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")
---
 hw/acpi/erst.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/acpi/erst.c +0 -0
index df856b2669..26391f93ca 100644

hw/acpi/erst.c static unsigned write_erst_record(ERSTDeviceState *s) · line 719

Quoted 5 lines
hw/acpi/erst.c +1 -1
@@ -716,7 +716,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
     if (nvram) {
         /* Write the record into the slot */
         memcpy(nvram, exchange, record_length);
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);

Hi,
I'm running the fuzzer over this code. So far, it hasn't complained about this particular memset call, but it has crashed on the memcpy, directly preceding it. I think the record_length checks might be insufficient. I made an issue/reproducer:
https://gitlab.com/qemu-project/qemu/-/issues/1268

Quoted 5 lines
Quoted 3 lines

In that particular case, record_length is ffffff00 and passes the checks. I'll keep an eye on the fuzzer to see if it will be able to crash the memset.

Here's a testcase for the memset issue:

Looks like this check in {read,write}_erst_record():

|   if ((s->record_offset + record_length) > exchange_length) {
|       return STATUS_FAILED;
|   }

has an integer overrun and should be re-written as:

|   if (record_length > exchange_length - s->record_offset) {
|       return STATUS_FAILED;
|   }
Quoted 7 lines
Quoted 1 line

regards Christian

Hi Christian,
With this change applied (along with the memset fix), the fuzzer doesn't find any crashes.
Thanks
-Alex

Thanks!
Christian, are you doing to be sending a combined patch for the whole issue? If you do pls add Tested-by tags as appropriate. Thanks!

Signature · 1 line
MST
#13 lore raw

PATCH v2 1/1 hw/acpi/erst.c: Fix memory handling issues

- Fix memset argument order: The second argument is the value, the length goes last.
- Fix an integer overflow reported by Alexander Bulekov.

Both issues allow the guest to overrun the host buffer allocated for the ERST memory device.

Cc Eric DeVolder <[email protected]
Cc Alexander Bulekov <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")
---
 hw/acpi/erst.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/acpi/erst.c +3 -3
index df856b2669..aefcc03ad6 100644
@@ -635,7 +635,7 @@ static unsigned read_erst_record(ERSTDeviceState *s)
         if (record_length < UEFI_CPER_RECORD_MIN_SIZE) {
             rc = STATUS_FAILED;
         }
-        if ((s->record_offset + record_length) > exchange_length) {
+        if (record_length > exchange_length - s->record_offset) {
             rc = STATUS_FAILED;
         }
         /* If all is ok, copy the record to the exchange buffer */
@@ -684,7 +684,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
     if (record_length < UEFI_CPER_RECORD_MIN_SIZE) {
         return STATUS_FAILED;
     }
-    if ((s->record_offset + record_length) > exchange_length) {
+    if (record_length > exchange_length - s->record_offset) {
         return STATUS_FAILED;
     }
 
@@ -716,7 +716,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
     if (nvram) {
         /* Write the record into the slot */
         memcpy(nvram, exchange, record_length);
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);
         /* If a new record, increment the record_count */
         if (!record_found) {
             uint32_t record_count;
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#14 lore raw

Re: [PATCH v2] hw/acpi/erst.c: Fix memory handling issues

Quoted 4 lines · Christian A. Ehrhardt

- Fix memset argument order: The second argument is the value, the length goes last.
- Fix an integer overflow reported by Alexander Bulekov.

Quoted 8 lines

Both issues allow the guest to overrun the host buffer allocated for the ERST memory device.

Cc Eric DeVolder <[email protected]
Cc Alexander Bulekov <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")
Quoted 40 lines
---
 hw/acpi/erst.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/acpi/erst.c +3 -3
index df856b2669..aefcc03ad6 100644
@@ -635,7 +635,7 @@ static unsigned read_erst_record(ERSTDeviceState *s)
         if (record_length < UEFI_CPER_RECORD_MIN_SIZE) {
             rc = STATUS_FAILED;
         }
-        if ((s->record_offset + record_length) > exchange_length) {
+        if (record_length > exchange_length - s->record_offset) {
             rc = STATUS_FAILED;
         }
         /* If all is ok, copy the record to the exchange buffer */
@@ -684,7 +684,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
     if (record_length < UEFI_CPER_RECORD_MIN_SIZE) {
         return STATUS_FAILED;
     }
-    if ((s->record_offset + record_length) > exchange_length) {
+    if (record_length > exchange_length - s->record_offset) {
         return STATUS_FAILED;
     }
 
@@ -716,7 +716,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
     if (nvram) {
         /* Write the record into the slot */
         memcpy(nvram, exchange, record_length);
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);
         /* If a new record, increment the record_count */
         if (!record_found) {
             uint32_t record_count;
Signature · 1 line
2.34.1
#15 lore raw

Re: [PATCH v2] hw/acpi/erst.c: Fix memory handling issues

Quoted 5 lines · Alexander Bulekov
Quoted 4 lines · Christian A. Ehrhardt

- Fix memset argument order: The second argument is the value, the length goes last.
- Fix an integer overflow reported by Alexander Bulekov.

Quoted 9 lines
Quoted 8 lines

Both issues allow the guest to overrun the host buffer allocated for the ERST memory device.

Cc Eric DeVolder <[email protected]
Cc Alexander Bulekov <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")

Also:
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1268

Quoted 42 lines
Quoted 40 lines
---
 hw/acpi/erst.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/acpi/erst.c +3 -3
index df856b2669..aefcc03ad6 100644
@@ -635,7 +635,7 @@ static unsigned read_erst_record(ERSTDeviceState *s)
         if (record_length < UEFI_CPER_RECORD_MIN_SIZE) {
             rc = STATUS_FAILED;
         }
-        if ((s->record_offset + record_length) > exchange_length) {
+        if (record_length > exchange_length - s->record_offset) {
             rc = STATUS_FAILED;
         }
         /* If all is ok, copy the record to the exchange buffer */
@@ -684,7 +684,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
     if (record_length < UEFI_CPER_RECORD_MIN_SIZE) {
         return STATUS_FAILED;
     }
-    if ((s->record_offset + record_length) > exchange_length) {
+    if (record_length > exchange_length - s->record_offset) {
         return STATUS_FAILED;
     }
 
@@ -716,7 +716,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
     if (nvram) {
         /* Write the record into the slot */
         memcpy(nvram, exchange, record_length);
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);
         /* If a new record, increment the record_count */
         if (!record_found) {
             uint32_t record_count;
Signature · 1 line
2.34.1
#16 lore raw

Re: [PATCH v2] hw/acpi/erst.c: Fix memory handling issues

In reply to Christian A. Ehrhardt
Quoted 7 lines · Christian A. Ehrhardt

- Fix memset argument order: The second argument is the value, the length goes last.
- Fix an integer overflow reported by Alexander Bulekov.

Both issues allow the guest to overrun the host buffer allocated for the ERST memory device.

Quoted 6 lines
Cc Eric DeVolder <[email protected]
Cc Alexander Bulekov <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")

queued, thanks!

Quoted 37 lines
---
 hw/acpi/erst.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/acpi/erst.c +3 -3
index df856b2669..aefcc03ad6 100644
@@ -635,7 +635,7 @@ static unsigned read_erst_record(ERSTDeviceState *s)
         if (record_length < UEFI_CPER_RECORD_MIN_SIZE) {
             rc = STATUS_FAILED;
         }
-        if ((s->record_offset + record_length) > exchange_length) {
+        if (record_length > exchange_length - s->record_offset) {
             rc = STATUS_FAILED;
         }
         /* If all is ok, copy the record to the exchange buffer */
@@ -684,7 +684,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
     if (record_length < UEFI_CPER_RECORD_MIN_SIZE) {
         return STATUS_FAILED;
     }
-    if ((s->record_offset + record_length) > exchange_length) {
+    if (record_length > exchange_length - s->record_offset) {
         return STATUS_FAILED;
     }
 
@@ -716,7 +716,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
     if (nvram) {
         /* Write the record into the slot */
         memcpy(nvram, exchange, record_length);
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);
         /* If a new record, increment the record_count */
         if (!record_found) {
             uint32_t record_count;
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#17 lore raw

Re: [PATCH v2] hw/acpi/erst.c: Fix memory handling issues

In reply to Christian A. Ehrhardt
Quoted 7 lines · Christian A. Ehrhardt

- Fix memset argument order: The second argument is the value, the length goes last.
- Fix an integer overflow reported by Alexander Bulekov.

Both issues allow the guest to overrun the host buffer allocated for the ERST memory device.

Quoted 6 lines
Cc Eric DeVolder <[email protected]
Cc Alexander Bulekov <[email protected]>
Fixes f7e26ffa590 ("ACPI ERST: support for ACPI ERST feature")

Thanks Christian!
eric

Quoted 36 lines
---
  hw/acpi/erst.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
hw/acpi/erst.c +3 -3
index df856b2669..aefcc03ad6 100644
@@ -635,7 +635,7 @@ static unsigned read_erst_record(ERSTDeviceState *s)
          if (record_length < UEFI_CPER_RECORD_MIN_SIZE) {
              rc = STATUS_FAILED;
          }
-        if ((s->record_offset + record_length) > exchange_length) {
+        if (record_length > exchange_length - s->record_offset) {
              rc = STATUS_FAILED;
          }
          /* If all is ok, copy the record to the exchange buffer */
@@ -684,7 +684,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
      if (record_length < UEFI_CPER_RECORD_MIN_SIZE) {
          return STATUS_FAILED;
      }
-    if ((s->record_offset + record_length) > exchange_length) {
+    if (record_length > exchange_length - s->record_offset) {
          return STATUS_FAILED;
      }
 
@@ -716,7 +716,7 @@ static unsigned write_erst_record(ERSTDeviceState *s)
      if (nvram) {
          /* Write the record into the slot */
          memcpy(nvram, exchange, record_length);
-        memset(nvram + record_length, exchange_length - record_length, 0xFF);
+        memset(nvram + record_length, 0xFF, exchange_length - record_length);
          /* If a new record, increment the record_count */
          if (!record_found) {
              uint32_t record_count;