0727d3
From 4e8fb957a349558648d5cddb80a89460bc97439e Mon Sep 17 00:00:00 2001
0727d3
From: Jon Maloy <jmaloy@redhat.com>
0727d3
Date: Wed, 30 Mar 2022 14:52:34 -0400
0727d3
Subject: [PATCH 09/18] acpi: fix QEMU crash when started with SLIC table
0727d3
MIME-Version: 1.0
0727d3
Content-Type: text/plain; charset=UTF-8
0727d3
Content-Transfer-Encoding: 8bit
0727d3
0727d3
RH-Author: Jon Maloy <jmaloy@redhat.com>
0727d3
RH-MergeRequest: 141: acpi: fix QEMU crash when started with SLIC table
0727d3
RH-Commit: [1/10] 0c34e80346c33da4f220d9c486b120c35005144e (jmaloy/qemu-kvm)
0727d3
RH-Bugzilla: 2062611
0727d3
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
0727d3
0727d3
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2062611
0727d3
Upstream: Merged
0727d3
0727d3
commit 8cdb99af45365727ac17f45239a9b8c1d5155c6d)
0727d3
Author: Igor Mammedov <imammedo@redhat.com>
0727d3
Date:   Mon Dec 27 14:31:17 2021 -0500
0727d3
0727d3
    acpi: fix QEMU crash when started with SLIC table
0727d3
0727d3
    if QEMU is started with used provided SLIC table blob,
0727d3
0727d3
      -acpitable sig=SLIC,oem_id='CRASH ',oem_table_id="ME",oem_rev=00002210,asl_compiler_id="",asl_compiler_rev=00000000,data=/dev/null
0727d3
    it will assert with:
0727d3
0727d3
      hw/acpi/aml-build.c:61:build_append_padded_str: assertion failed: (len <= maxlen)
0727d3
0727d3
    and following backtrace:
0727d3
0727d3
      ...
0727d3
      build_append_padded_str (array=0x555556afe320, str=0x555556afdb2e "CRASH ME", maxlen=0x6, pad=0x20) at hw/acpi/aml-build.c:61
0727d3
      acpi_table_begin (desc=0x7fffffffd1b0, array=0x555556afe320) at hw/acpi/aml-build.c:1727
0727d3
      build_fadt (tbl=0x555556afe320, linker=0x555557ca3830, f=0x7fffffffd318, oem_id=0x555556afdb2e "CRASH ME", oem_table_id=0x555556afdb34 "ME") at hw/acpi/aml-build.c:2064
0727d3
      ...
0727d3
0727d3
    which happens due to acpi_table_begin() expecting NULL terminated
0727d3
    oem_id and oem_table_id strings, which is normally the case, but
0727d3
    in case of user provided SLIC table, oem_id points to table's blob
0727d3
    directly and as result oem_id became longer than expected.
0727d3
0727d3
    Fix issue by handling oem_id consistently and make acpi_get_slic_oem()
0727d3
    return NULL terminated strings.
0727d3
0727d3
    PS:
0727d3
    After [1] refactoring, oem_id semantics became inconsistent, where
0727d3
    NULL terminated string was coming from machine and old way pointer
0727d3
    into byte array coming from -acpitable option. That used to work
0727d3
    since build_header() wasn't expecting NULL terminated string and
0727d3
    blindly copied the 1st 6 bytes only.
0727d3
0727d3
    However commit [2] broke that by replacing build_header() with
0727d3
    acpi_table_begin(), which was expecting NULL terminated string
0727d3
    and was checking oem_id size.
0727d3
0727d3
    1) 602b45820 ("acpi: Permit OEM ID and OEM table ID fields to be changed")
0727d3
    2)
0727d3
    Fixes: 4b56e1e4eb08 ("acpi: build_fadt: use acpi_table_begin()/acpi_table_end() instead of build_header()")
0727d3
    Resolves: https://gitlab.com/qemu-project/qemu/-/issues/786
0727d3
    Signed-off-by: Igor Mammedov <imammedo@redhat.com>
0727d3
    Message-Id: <20211227193120.1084176-2-imammedo@redhat.com>
0727d3
    Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
0727d3
    Tested-by: Denis Lisov <dennis.lissov@gmail.com>
0727d3
    Tested-by: Alexander Tsoy <alexander@tsoy.me>
0727d3
    Cc: qemu-stable@nongnu.org
0727d3
    Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
0727d3
    Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
0727d3
0727d3
(cherry picked from commit 8cdb99af45365727ac17f45239a9b8c1d5155c6d)
0727d3
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
0727d3
---
0727d3
 hw/acpi/core.c       | 4 ++--
0727d3
 hw/i386/acpi-build.c | 2 ++
0727d3
 2 files changed, 4 insertions(+), 2 deletions(-)
0727d3
0727d3
diff --git a/hw/acpi/core.c b/hw/acpi/core.c
0727d3
index 1e004d0078..3e811bf03c 100644
0727d3
--- a/hw/acpi/core.c
0727d3
+++ b/hw/acpi/core.c
0727d3
@@ -345,8 +345,8 @@ int acpi_get_slic_oem(AcpiSlicOem *oem)
0727d3
         struct acpi_table_header *hdr = (void *)(u - sizeof(hdr->_length));
0727d3
 
0727d3
         if (memcmp(hdr->sig, "SLIC", 4) == 0) {
0727d3
-            oem->id = hdr->oem_id;
0727d3
-            oem->table_id = hdr->oem_table_id;
0727d3
+            oem->id = g_strndup(hdr->oem_id, 6);
0727d3
+            oem->table_id = g_strndup(hdr->oem_table_id, 8);
0727d3
             return 0;
0727d3
         }
0727d3
     }
0727d3
diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
0727d3
index a4478e77b7..acc4869db0 100644
0727d3
--- a/hw/i386/acpi-build.c
0727d3
+++ b/hw/i386/acpi-build.c
0727d3
@@ -2726,6 +2726,8 @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine)
0727d3
 
0727d3
     /* Cleanup memory that's no longer used. */
0727d3
     g_array_free(table_offsets, true);
0727d3
+    g_free(slic_oem.id);
0727d3
+    g_free(slic_oem.table_id);
0727d3
 }
0727d3
 
0727d3
 static void acpi_ram_update(MemoryRegion *mr, GArray *data)
0727d3
-- 
0727d3
2.27.0
0727d3