Skip to contentSkip to conversation

pc: fix possible NULL pointer dereference in pc_machine_get_device_memory_region_size()

PATCH Applied Igor Mammedov 3 participants 5 messages Jun 10, 2019 – Jun 24, 2019

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pc: fix possible NULL pointer dereference in pc_machine_get_device_memory_region_size()

QEMU will crash when device-memory-region-size property is read if ms->device_memory wasn't initialized yet.

Crash can be reproduced with:
$QEMU -preconfig -qmp unix:qmp_socket,server,nowait & ./scripts/qmp/qom-get -s qmp_socket /machine.device-memory-region-size

Instead of crashing return 0 if ms->device_memory hasn't been initialized.

---
v2:
add reproducer to commit message
(Markus Armbruster <[email protected]>)

 hw/i386/pc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
hw/i386/pc.c +5 -1
index edc240b..1b7ead9 100644
@@ -2459,7 +2459,11 @@ pc_machine_get_device_memory_region_size(Object *obj, Visitor *v,
                                          Error **errp)
 {
     MachineState *ms = MACHINE(obj);
-    int64_t value = memory_region_size(&ms->device_memory->mr);
+    int64_t value = 0;
+
+    if (ms->device_memory) {
+        memory_region_size(&ms->device_memory->mr);
+    }
 
     visit_type_int(v, name, &value, errp);
 }
Signature · 1 line
2.7.4
#1 lore raw
Quoted 26 lines · Igor Mammedov

QEMU will crash when device-memory-region-size property is read if ms->device_memory wasn't initialized yet.

Crash can be reproduced with:
$QEMU -preconfig -qmp unix:qmp_socket,server,nowait & ./scripts/qmp/qom-get -s qmp_socket /machine.device-memory-region-size

Instead of crashing return 0 if ms->device_memory hasn't been initialized.

---
v2:
add reproducer to commit message
(Markus Armbruster <[email protected]>)

 hw/i386/pc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
hw/i386/pc.c +0 -0
index edc240b..1b7ead9 100644
@@ -2459,7 +2459,11 @@ pc_machine_get_device_memory_region_size(Object *obj, Visitor *v,
                                          Error **errp)
 {

hw/i386/pc.c pc_machine_get_device_memory_region_size(Object *obj, Visitor *v, · line 2469

Quoted 10 lines
hw/i386/pc.c +5 -1
     MachineState *ms = MACHINE(obj);
-    int64_t value = memory_region_size(&ms->device_memory->mr);
+    int64_t value = 0;
+
+    if (ms->device_memory) {
+        memory_region_size(&ms->device_memory->mr);
+    }
 
     visit_type_int(v, name, &value, errp);
 }

Queued, thanks.

Paolo

#2 lore raw
In reply to Igor Mammedov
Quoted 8 lines · Igor Mammedov

QEMU will crash when device-memory-region-size property is read if ms->device_memory wasn't initialized yet.

Crash can be reproduced with:
$QEMU -preconfig -qmp unix:qmp_socket,server,nowait & ./scripts/qmp/qom-get -s qmp_socket /machine.device-memory-region-size

Instead of crashing return 0 if ms->device_memory hasn't been initialized.

This patch breaks bios-tables-test /x86_64/acpi/piix64/cpuhp:

acpi-test: Warning! SRAT binary file mismatch. Actual [aml:/tmp/aml-RIFK3Z], Expected [aml:tests/data/acpi/pc/SRAT.memhp].
acpi-test: Warning! SRAT mismatch. Actual [asl:/tmp/asl-TLFK3Z.dsl, aml:/tmp/aml-RIFK3Z], Expected [asl:/tmp/asl-JL5J3Z.dsl, aml:tests/data/acpi/pc/SRAT.memhp]. **
ERROR:/home/pbonzini/work/upstream/qemu/tests/bios-tables-test.c:434:test_acpi_asl: assertion failed: (all_tables_match)
ERROR - Bail out! ERROR:/home/pbonzini/work/upstream/qemu/tests/bios-tables-test.c:434:test_acpi_asl: assertion failed: (all_tables_match)

So I'm removing it from the pull request.

Paolo

Quoted 27 lines

---
v2:
add reproducer to commit message
(Markus Armbruster <[email protected]>)

 hw/i386/pc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
hw/i386/pc.c +5 -1
index edc240b..1b7ead9 100644
@@ -2459,7 +2459,11 @@ pc_machine_get_device_memory_region_size(Object *obj, Visitor *v,
                                          Error **errp)
 {
     MachineState *ms = MACHINE(obj);
-    int64_t value = memory_region_size(&ms->device_memory->mr);
+    int64_t value = 0;
+
+    if (ms->device_memory) {
+        memory_region_size(&ms->device_memory->mr);
+    }
 
     visit_type_int(v, name, &value, errp);
 }
#3 lore raw
Quoted 12 lines · Paolo Bonzini
Quoted 8 lines · Igor Mammedov

QEMU will crash when device-memory-region-size property is read if ms->device_memory wasn't initialized yet.

Crash can be reproduced with:
$QEMU -preconfig -qmp unix:qmp_socket,server,nowait & ./scripts/qmp/qom-get -s qmp_socket /machine.device-memory-region-size

Instead of crashing return 0 if ms->device_memory hasn't been initialized.

This patch breaks bios-tables-test /x86_64/acpi/piix64/cpuhp:

Quoted 7 lines

acpi-test: Warning! SRAT binary file mismatch. Actual [aml:/tmp/aml-RIFK3Z], Expected [aml:tests/data/acpi/pc/SRAT.memhp].
acpi-test: Warning! SRAT mismatch. Actual [asl:/tmp/asl-TLFK3Z.dsl, aml:/tmp/aml-RIFK3Z], Expected [asl:/tmp/asl-JL5J3Z.dsl, aml:tests/data/acpi/pc/SRAT.memhp]. **
ERROR:/home/pbonzini/work/upstream/qemu/tests/bios-tables-test.c:434:test_acpi_asl: assertion failed: (all_tables_match)
ERROR - Bail out! ERROR:/home/pbonzini/work/upstream/qemu/tests/bios-tables-test.c:434:test_acpi_asl: assertion failed: (all_tables_match)

So I'm removing it from the pull request.

The patch makes all memory regions return 0 as its size.

Quoted 9 lines
Quoted 9 lines

---
v2:
add reproducer to commit message
(Markus Armbruster <[email protected]>)

 hw/i386/pc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 13 lines

hw/i386/pc.c pc_machine_get_device_memory_region_size(Object *obj, Visitor *v, · line 2465

Quoted 13 lines
hw/i386/pc.c +4 -1
index edc240b..1b7ead9 100644
@@ -2459,7 +2459,11 @@ pc_machine_get_device_memory_region_size(Object *obj, Visitor *v,
                                          Error **errp)
 {
     MachineState *ms = MACHINE(obj);
-    int64_t value = memory_region_size(&ms->device_memory->mr);
+    int64_t value = 0;
+
+    if (ms->device_memory) {
+        memory_region_size(&ms->device_memory->mr);

This was supposed to be:

        value = memory_region_size(&ms->device_memory->mr);
Quoted 6 lines
Quoted 5 lines
hw/i386/pc.c +1 -0
+    }
 
     visit_type_int(v, name, &value, errp);
 }
Signature · 1 line
Eduardo
#4 lore raw
Quoted 19 lines · On Thu, 20 Jun 2019 22:46:15 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 18 lines · Paolo Bonzini
Quoted 8 lines · Igor Mammedov

QEMU will crash when device-memory-region-size property is read if ms->device_memory wasn't initialized yet.

Crash can be reproduced with:
$QEMU -preconfig -qmp unix:qmp_socket,server,nowait & ./scripts/qmp/qom-get -s qmp_socket /machine.device-memory-region-size

Instead of crashing return 0 if ms->device_memory hasn't been initialized.

This patch breaks bios-tables-test /x86_64/acpi/piix64/cpuhp:

acpi-test: Warning! SRAT binary file mismatch. Actual [aml:/tmp/aml-RIFK3Z], Expected [aml:tests/data/acpi/pc/SRAT.memhp].
acpi-test: Warning! SRAT mismatch. Actual [asl:/tmp/asl-TLFK3Z.dsl, aml:/tmp/aml-RIFK3Z], Expected [asl:/tmp/asl-JL5J3Z.dsl, aml:tests/data/acpi/pc/SRAT.memhp]. **
ERROR:/home/pbonzini/work/upstream/qemu/tests/bios-tables-test.c:434:test_acpi_asl: assertion failed: (all_tables_match)
ERROR - Bail out! ERROR:/home/pbonzini/work/upstream/qemu/tests/bios-tables-test.c:434:test_acpi_asl: assertion failed: (all_tables_match)

Quoted 3 lines
Quoted 1 line

So I'm removing it from the pull request.

The patch makes all memory regions return 0 as its size.

Indeed, sorry for not testing before posting patch. I'll send properly tested v2.

Quoted 36 lines
Quoted 11 lines
Quoted 11 lines

---
v2:
add reproducer to commit message
(Markus Armbruster <[email protected]>)

 hw/i386/pc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 13 lines

hw/i386/pc.c pc_machine_get_device_memory_region_size(Object *obj, Visitor *v, · line 2465

Quoted 13 lines
hw/i386/pc.c +4 -1
index edc240b..1b7ead9 100644
@@ -2459,7 +2459,11 @@ pc_machine_get_device_memory_region_size(Object *obj, Visitor *v,
                                          Error **errp)
 {
     MachineState *ms = MACHINE(obj);
-    int64_t value = memory_region_size(&ms->device_memory->mr);
+    int64_t value = 0;
+
+    if (ms->device_memory) {
+        memory_region_size(&ms->device_memory->mr);  

This was supposed to be:

        value = memory_region_size(&ms->device_memory->mr);
Quoted 6 lines
Quoted 5 lines
hw/i386/pc.c +1 -0
+    }
 
     visit_type_int(v, name, &value, errp);
 }