From: Ren Yanyi <[email protected]>

virt_acpi_build() gates HEST table construction on:

    acpi_ged_state = ACPI_GED(vms->acpi_dev);
    ags = &acpi_ged_state->ghes_state;
    if (ags) {

ags is the address of the ghes_state member of AcpiGedState and can
never be NULL, so the check always passes.  Whether the GED device
exists is a separate condition (has_ged also depends on firmware
being loaded and on ACPI being enabled), so a machine started with
ras=on but without a GED device reaches this code with a NULL
acpi_ged_state; ags then holds a bogus non-NULL pointer and
acpi_build_hest() dereferences it.

Test vms->acpi_dev in the enclosing condition instead, so the HEST
table is only built when the GED device exists and the ACPI_GED()
cast is always valid.

Fixes: 2e9c5c5bc85a ("acpi/ghes: prepare to change the way HEST offsets are 
calculated")
Signed-off-by: Ren Yanyi <[email protected]>
Reviewed-by: Peter Maydell <[email protected]>
Signed-off-by: Peter Maydell <[email protected]>
---
 hw/arm/virt-acpi-build.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)

diff --git a/hw/arm/virt-acpi-build.c b/hw/arm/virt-acpi-build.c
index d368913e766..5670d1eca71 100644
--- a/hw/arm/virt-acpi-build.c
+++ b/hw/arm/virt-acpi-build.c
@@ -1406,7 +1406,7 @@ void virt_acpi_build(VirtMachineState *vms, 
AcpiBuildTables *tables)
     acpi_add_table(table_offsets, tables_blob);
     build_dbg2(tables_blob, tables->linker, vms);
 
-    if (vms->ras) {
+    if (vms->ras && vms->acpi_dev) {
         AcpiGedState *acpi_ged_state;
         static const AcpiNotificationSourceId *notify;
         unsigned int notify_sz;
@@ -1414,21 +1414,20 @@ void virt_acpi_build(VirtMachineState *vms, 
AcpiBuildTables *tables)
 
         acpi_ged_state = ACPI_GED(vms->acpi_dev);
         ags = &acpi_ged_state->ghes_state;
-        if (ags) {
-            acpi_add_table(table_offsets, tables_blob);
 
-            if (!ags->use_hest_addr) {
-                notify = hest_ghes_notify_10_0;
-                notify_sz = ARRAY_SIZE(hest_ghes_notify_10_0);
-            } else {
-                notify = hest_ghes_notify;
-                notify_sz = ARRAY_SIZE(hest_ghes_notify);
-            }
+        acpi_add_table(table_offsets, tables_blob);
 
-            acpi_build_hest(ags, tables_blob, tables->hardware_errors,
-                            tables->linker, notify, notify_sz,
-                            vms->oem_id, vms->oem_table_id);
+        if (!ags->use_hest_addr) {
+            notify = hest_ghes_notify_10_0;
+            notify_sz = ARRAY_SIZE(hest_ghes_notify_10_0);
+        } else {
+            notify = hest_ghes_notify;
+            notify_sz = ARRAY_SIZE(hest_ghes_notify);
         }
+
+        acpi_build_hest(ags, tables_blob, tables->hardware_errors,
+                        tables->linker, notify, notify_sz,
+                        vms->oem_id, vms->oem_table_id);
     }
 
     if (ms->numa_state->num_nodes > 0) {
-- 
2.43.0


Reply via email to