Skip to contentSkip to conversation

Replace variable length arrays in ppc KVM code

PATCH In review Thomas Huth 4 participants 12 messages Feb 21, 2024 – Feb 22, 2024 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover Replace variable length arrays in ppc KVM code

It would be good to enable -Wvla as an additional security feature
when compiling QEMU (see also Peter's explanation here:
https://lore.kernel.org/qemu-devel/[email protected]/ ). There are currently only two spots with variable lengths arrays left,
so if we fix those, we can finally enable the -Wvla compiler switch.

Peter Maydell (1):
meson: Enable -Wvla

Thomas Huth (2):
target/ppc/kvm: Replace variable length array in kvmppc_save_htab() target/ppc/kvm: Replace variable length array in kvmppc_read_hptes()

 meson.build      | 1 +
 target/ppc/kvm.c | 6 +++---
 2 files changed, 4 insertions(+), 3 deletions(-)
Signature · 1 line
2.43.2
#1 lore raw

PATCH 1/3 target/ppc/kvm: Replace variable length array in kvmppc_save_htab()

To be able to compile QEMU with -Wvla (to prevent potential security issues), we need to get rid of the variable length array in the kvmppc_save_htab() function. Replace it with a heap allocation instead.

---
 target/ppc/kvm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/ppc/kvm.c +1 -1
index 26fa9d0575..e7e39c3091 100644
@@ -2688,7 +2688,7 @@ int kvmppc_get_htab_fd(bool write, uint64_t index, Error **errp)
 int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
 {
     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
-    uint8_t buf[bufsize];
+    g_autofree uint8_t *buf = g_malloc(bufsize);
     ssize_t rc;
 
     do {
Signature · 1 line
2.43.2
#6 lore raw

PATCH 3/3 meson: Enable -Wvla

In reply to Thomas Huth

From: Peter Maydell <[email protected]>

QEMU has historically used variable length arrays only very rarely. Variable length arrays are a potential security issue where an on-stack dynamic allocation isn't correctly size-checked, especially when the size comes from the guest. (An example problem of this kind from the past is CVE-2021-3527). Forbidding them entirely is a defensive measure against further bugs of this kind.

Enable -Wvla to prevent any new uses from sneaking into the codebase.

Message-ID: <[email protected]>

[thuth: rebased to current master branch]
---
 meson.build | 1 +
 1 file changed, 1 insertion(+)
meson.build +1 -0
index c1dc83e4c0..0ef1654e86 100644
@@ -592,6 +592,7 @@ warn_flags = [
   '-Wstrict-prototypes',
   '-Wtype-limits',
   '-Wundef',
+  '-Wvla',
   '-Wwrite-strings',
 
   # Then disable some undesirable warnings
Signature · 1 line
2.43.2
#4 lore raw

PATCH 2/3 target/ppc/kvm: Replace variable length array in kvmppc_read_hptes()

In reply to Thomas Huth

HPTES_PER_GROUP is 8 and HASH_PTE_SIZE_64 is 16, so we don't waste too many bytes by always allocating the maximum amount of bytes on the stack here to get rid of the variable length array.

---
 target/ppc/kvm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/ppc/kvm.c +2 -2
index e7e39c3091..bcf30a5400 100644
@@ -2770,9 +2770,9 @@ void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n)
     while (i < n) {
         struct kvm_get_htab_header *hdr;
         int m = n < HPTES_PER_GROUP ? n : HPTES_PER_GROUP;
-        char buf[sizeof(*hdr) + m * HASH_PTE_SIZE_64];
+        char buf[sizeof(*hdr) + HPTES_PER_GROUP * HASH_PTE_SIZE_64];
 
-        rc = read(fd, buf, sizeof(buf));
+        rc = read(fd, buf, sizeof(*hdr) + m * HASH_PTE_SIZE_64);
         if (rc < 0) {
             hw_error("kvmppc_read_hptes: Unable to read HPTEs");
         }
Signature · 1 line
2.43.2
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] target/ppc/kvm: Replace variable length array in kvmppc_save_htab()

In reply to Thomas Huth
Quoted 15 lines · Thomas Huth

To be able to compile QEMU with -Wvla (to prevent potential security issues), we need to get rid of the variable length array in the kvmppc_save_htab() function. Replace it with a heap allocation instead.

---
 target/ppc/kvm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/ppc/kvm.c +0 -0
index 26fa9d0575..e7e39c3091 100644

target/ppc/kvm.c int kvmppc_get_htab_fd(bool write, uint64_t index, Error **errp) · line 2693

Quoted 7 lines
target/ppc/kvm.c +1 -1
@@ -2688,7 +2688,7 @@ int kvmppc_get_htab_fd(bool write, uint64_t index, Error **errp)
 int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
 {
     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
-    uint8_t buf[bufsize];
+    g_autofree uint8_t *buf = g_malloc(bufsize);
     ssize_t rc;
 

This works, so

but you could also drop the bufsize argument, because there are only two callers and they both pass MAX_KVM_BUF_SIZE, and then declare the array as fixed size with "uint8_t buf[MAX_KVM_BUF_SIZE]".

thanks
-- PMM

Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] target/ppc/kvm: Replace variable length array in kvmppc_read_hptes()

In reply to Thomas Huth
Quoted 9 lines · Thomas Huth

HPTES_PER_GROUP is 8 and HASH_PTE_SIZE_64 is 16, so we don't waste too many bytes by always allocating the maximum amount of bytes on the stack here to get rid of the variable length array.

---
 target/ppc/kvm.c | 4 ++--

thanks
-- PMM

#3 lore raw

Re: [PATCH 1/3] target/ppc/kvm: Replace variable length array in kvmppc_save_htab()

In reply to Peter Maydell
Quoted 25 lines · Peter Maydell
Quoted 15 lines · Thomas Huth

To be able to compile QEMU with -Wvla (to prevent potential security issues), we need to get rid of the variable length array in the kvmppc_save_htab() function. Replace it with a heap allocation instead.

---
  target/ppc/kvm.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
target/ppc/kvm.c +0 -0
index 26fa9d0575..e7e39c3091 100644

target/ppc/kvm.c int kvmppc_get_htab_fd(bool write, uint64_t index, Error **errp) · line 2693

Quoted 7 lines
target/ppc/kvm.c +1 -1
@@ -2688,7 +2688,7 @@ int kvmppc_get_htab_fd(bool write, uint64_t index, Error **errp)
  int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
  {
      int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
-    uint8_t buf[bufsize];
+    g_autofree uint8_t *buf = g_malloc(bufsize);
      ssize_t rc;
 

This works, so

Quoted 5 lines

but you could also drop the bufsize argument, because there are only two callers and they both pass MAX_KVM_BUF_SIZE, and then declare the array as fixed size with "uint8_t buf[MAX_KVM_BUF_SIZE]".

Yes, that's an alternative ... my thinking was that MAX_KVM_BUF_SIZE = 2048 is already a rather big buffer which should maybe rather be allocated on the heap than the stack? But I don't mind too much, so if ppc folks prefer the stack allocation, I can change the patch, too.

Thomas

#7 lore raw

Re: [PATCH 3/3] meson: Enable -Wvla

In reply to Thomas Huth
Quoted 24 lines · Thomas Huth

From: Peter Maydell <[email protected]>

QEMU has historically used variable length arrays only very rarely. Variable length arrays are a potential security issue where an on-stack dynamic allocation isn't correctly size-checked, especially when the size comes from the guest. (An example problem of this kind from the past is CVE-2021-3527). Forbidding them entirely is a defensive measure against further bugs of this kind.

Enable -Wvla to prevent any new uses from sneaking into the codebase.

Message-ID: <[email protected]>

[thuth: rebased to current master branch]
---
  meson.build | 1 +
  1 file changed, 1 insertion(+)
meson.build +0 -0
index c1dc83e4c0..0ef1654e86 100644

meson.build warn_flags = [ · line 598

Quoted 7 lines
meson.build +1 -0
@@ -592,6 +592,7 @@ warn_flags = [
    '-Wstrict-prototypes',
    '-Wtype-limits',
    '-Wundef',
+  '-Wvla',
    '-Wwrite-strings',
 
    # Then disable some undesirable warnings

Sigh, there's a new warning in the latest master branch:

https://gitlab.com/thuth/qemu/-/jobs/6225992174

Caused by commit d65aba828 ("hw/sparc/leon3: implement multiprocessor")...
Clément, Philippe, could this maybe be written in a different way that does not trigger a -Wvla warning?

Thomas

Reviewed-by Tested-by
#8 lore raw

Re: [PATCH 3/3] meson: Enable -Wvla

Quoted 12 lines · Thomas Huth
Quoted 11 lines · Thomas Huth

From: Peter Maydell <[email protected]>

QEMU has historically used variable length arrays only very rarely. Variable length arrays are a potential security issue where an on-stack dynamic allocation isn't correctly size-checked, especially when the size comes from the guest.  (An example problem of this kind from the past is CVE-2021-3527).  Forbidding them entirely is a defensive measure against further bugs of this kind.

Enable -Wvla to prevent any new uses from sneaking into the codebase.

Quoted 7 lines
Quoted 7 lines

Message-ID: <[email protected]>

[thuth: rebased to current master branch]

---
  meson.build | 1 +

  1 file changed, 1 insertion(+)
Quoted 14 lines

meson.build warn_flags = [ · line 597

Quoted 11 lines
meson.build +1 -0
index c1dc83e4c0..0ef1654e86 100644
@@ -592,6 +592,7 @@ warn_flags = [
     '-Wstrict-prototypes',
     '-Wtype-limits',
     '-Wundef',
+  '-Wvla',
     '-Wwrite-strings',
     # Then disable some undesirable warnings

Sigh, there's a new warning in the latest master branch:

Quoted 4 lines

 https://gitlab.com/thuth/qemu/-/jobs/6225992174

Caused by commit d65aba828 ("hw/sparc/leon3: implement multiprocessor")...
Clément, Philippe, could this maybe be written in a different way that does not trigger a -Wvla warning?

Clément, ResetData::entry isn't used, so we can simplify removing the whole ResetData structure, but I'm not sure this is intended:

-- >8 --

hw/sparc/leon3.c +12 -30
index 4873b59b6c..1ff6b5d63d 100644
@@ -68,14 +68,6 @@
 #define LEON3_APB_PNP_OFFSET (0x800FF000)
 #define LEON3_AHB_PNP_OFFSET (0xFFFFF000)
 
-typedef struct ResetData {
-    struct CPUResetData {
-        int id;
-        SPARCCPU *cpu;
-    } info[MAX_CPUS];
-    uint32_t entry;             /* save kernel entry in case of reset */
-} ResetData;
-
 static uint32_t *gen_store_u32(uint32_t *code, hwaddr addr, uint32_t val)
 {
     stl_p(code++, 0x82100000); /* mov %g0, %g1                */
@@ -148,17 +140,14 @@ static void write_bootloader(void *ptr, hwaddr kernel_addr)
 
 static void leon3_cpu_reset(void *opaque)
 {
-    struct CPUResetData *info = (struct CPUResetData *) opaque;
-    int id = info->id;
-    ResetData *s = (ResetData *)DO_UPCAST(ResetData, info[id], info);
-    CPUState *cpu = CPU(s->info[id].cpu);
+    CPUState *cpu = opaque;
     CPUSPARCState *env = cpu_env(cpu);
 
     cpu_reset(cpu);
 
     cpu->halted = cpu->cpu_index != 0;
-    env->pc = s->entry;
-    env->npc = s->entry + 4;
+    env->pc = LEON3_PROM_OFFSET;
+    env->npc = LEON3_PROM_OFFSET + 4;
 }
 
 static void leon3_cache_control_int(CPUSPARCState *env)
@@ -259,7 +248,7 @@ static void leon3_generic_hw_init(MachineState *machine)
     ram_addr_t ram_size = machine->ram_size;
     const char *bios_name = machine->firmware ?: LEON3_PROM_FILENAME;
     const char *kernel_filename = machine->kernel_filename;
-    SPARCCPU *cpu;
+    SPARCCPU *cpu[MAX_CPUS];
     CPUSPARCState   *env;
     MemoryRegion *address_space_mem = get_system_memory();
     MemoryRegion *prom = g_new(MemoryRegion, 1);
@@ -267,28 +256,22 @@ static void leon3_generic_hw_init(MachineState *machine)
     char       *filename;
     int         bios_size;
     int         prom_size;
-    ResetData  *reset_info;
     DeviceState *dev, *irqmpdev;
     int i;
     AHBPnp *ahb_pnp;
     APBPnp *apb_pnp;
 
-    reset_info = g_malloc0(sizeof(ResetData));
-
     for (i = 0; i < machine->smp.cpus; i++) {
         /* Init CPU */
-        cpu = SPARC_CPU(object_new(machine->cpu_type));
-        qdev_init_gpio_in_named(DEVICE(cpu), leon3_start_cpu, "start_cpu", 1);
-        qdev_init_gpio_in_named(DEVICE(cpu), leon3_set_pil_in, "pil", 1);
-        qdev_realize(DEVICE(cpu), NULL, &error_fatal);
-        env = &cpu->env;
+        cpu[i] = SPARC_CPU(object_new(machine->cpu_type));
+        qdev_init_gpio_in_named(DEVICE(cpu[i]), leon3_start_cpu, "start_cpu", 1);
+        qdev_init_gpio_in_named(DEVICE(cpu[i]), leon3_set_pil_in, "pil", 1);
+        qdev_realize(DEVICE(cpu[i]), NULL, &error_fatal);
+        env = &cpu[i]->env;
 
         cpu_sparc_set_id(env, i);
 
-        /* Reset data */
-        reset_info->info[i].id = i;
-        reset_info->info[i].cpu = cpu;
-        qemu_register_reset(leon3_cpu_reset, &reset_info->info[i]);
+        qemu_register_reset(leon3_cpu_reset, cpu[i]);
     }
 
     ahb_pnp = GRLIB_AHB_PNP(qdev_new(TYPE_GRLIB_AHB_PNP));
@@ -312,13 +295,12 @@ static void leon3_generic_hw_init(MachineState *machine)
     sysbus_realize_and_unref(SYS_BUS_DEVICE(irqmpdev), &error_fatal);
 
     for (i = 0; i < machine->smp.cpus; i++) {
-        cpu = reset_info->info[i].cpu;
-        env = &cpu->env;
+        env = &cpu[i]->env;
         qdev_connect_gpio_out_named(irqmpdev, "grlib-start-cpu", i,
-                                    qdev_get_gpio_in_named(DEVICE(cpu),
+                                    qdev_get_gpio_in_named(DEVICE(cpu[i]),
 

"start_cpu", 0));
qdev_connect_gpio_out_named(irqmpdev, "grlib-irq", i,
- qdev_get_gpio_in_named(DEVICE(cpu),
+ qdev_get_gpio_in_named(DEVICE(cpu[i]),

                                                            "pil", 0));
         env->irq_manager = irqmpdev;
         env->qemu_irq_ack = leon3_irq_manager;
@@ -396,11 +378,6 @@ static void leon3_generic_hw_init(MachineState *machine)
              * bootloader.
              */
             write_bootloader(memory_region_get_ram_ptr(prom), entry);

- reset_info->entry = LEON3_PROM_OFFSET;
- for (i = 0; i < machine->smp.cpus; i++) {
- reset_info->info[i].cpu->env.pc = LEON3_PROM_OFFSET;
- reset_info->info[i].cpu->env.npc = LEON3_PROM_OFFSET + 4;
- }

         }
     }

---

Regards,

Phil.

#9 lore raw

Re: [PATCH 3/3] meson: Enable -Wvla

Quoted 81 lines · Philippe Mathieu-Daudé

Clément, ResetData::entry isn't used, so we can simplify removing the whole ResetData structure, but I'm not sure this is intended:

-- >8 --

hw/sparc/leon3.c +3 -14
index 4873b59b6c..1ff6b5d63d 100644
@@ -68,14 +68,6 @@
  #define LEON3_APB_PNP_OFFSET (0x800FF000)
  #define LEON3_AHB_PNP_OFFSET (0xFFFFF000)
 
-typedef struct ResetData {
-    struct CPUResetData {
-        int id;
-        SPARCCPU *cpu;
-    } info[MAX_CPUS];
-    uint32_t entry;             /* save kernel entry in case of reset */
-} ResetData;
-
  static uint32_t *gen_store_u32(uint32_t *code, hwaddr addr, uint32_t val)
  {
      stl_p(code++, 0x82100000); /* mov %g0, %g1                */
@@ -148,17 +140,14 @@ static void write_bootloader(void *ptr, hwaddr 
 kernel_addr)
 
  static void leon3_cpu_reset(void *opaque)
  {
-    struct CPUResetData *info = (struct CPUResetData *) opaque;
-    int id = info->id;
-    ResetData *s = (ResetData *)DO_UPCAST(ResetData, info[id], info);
-    CPUState *cpu = CPU(s->info[id].cpu);
+    CPUState *cpu = opaque;
      CPUSPARCState *env = cpu_env(cpu);
 
      cpu_reset(cpu);
 
      cpu->halted = cpu->cpu_index != 0;
-    env->pc = s->entry;
-    env->npc = s->entry + 4;
+    env->pc = LEON3_PROM_OFFSET;
+    env->npc = LEON3_PROM_OFFSET + 4;
  }
 

 static void leon3_cache_control_int(CPUSPARCState *env)

hw/sparc/leon3.c +1 -1
@@ -259,7 +248,7 @@ static void leon3_generic_hw_init(MachineState 
 *machine)
      ram_addr_t ram_size = machine->ram_size;
      const char *bios_name = machine->firmware ?: LEON3_PROM_FILENAME;
      const char *kernel_filename = machine->kernel_filename;
-    SPARCCPU *cpu;
+    SPARCCPU *cpu[MAX_CPUS];
      CPUSPARCState   *env;
      MemoryRegion *address_space_mem = get_system_memory();

     MemoryRegion *prom = g_new(MemoryRegion, 1);

hw/sparc/leon3.c +3 -8
@@ -267,28 +256,22 @@ static void leon3_generic_hw_init(MachineState 
 *machine)
      char       *filename;
      int         bios_size;
      int         prom_size;
-    ResetData  *reset_info;
      DeviceState *dev, *irqmpdev;
      int i;
      AHBPnp *ahb_pnp;
      APBPnp *apb_pnp;
 
-    reset_info = g_malloc0(sizeof(ResetData));
-
      for (i = 0; i < machine->smp.cpus; i++) {
          /* Init CPU */
-        cpu = SPARC_CPU(object_new(machine->cpu_type));
-        qdev_init_gpio_in_named(DEVICE(cpu), leon3_start_cpu, 
 "start_cpu", 1);
-        qdev_init_gpio_in_named(DEVICE(cpu), leon3_set_pil_in, "pil", 1);
-        qdev_realize(DEVICE(cpu), NULL, &error_fatal);
-        env = &cpu->env;
+        cpu[i] = SPARC_CPU(object_new(machine->cpu_type));
+        qdev_init_gpio_in_named(DEVICE(cpu[i]), leon3_start_cpu, 
 "start_cpu", 1);
+        qdev_init_gpio_in_named(DEVICE(cpu[i]), leon3_set_pil_in, 
 "pil", 1);

hw/sparc/leon3.c static void leon3_generic_hw_init(MachineState · line 274

Quoted 10 lines
hw/sparc/leon3.c +3 -4
+        qdev_realize(DEVICE(cpu[i]), NULL, &error_fatal);
+        env = &cpu[i]->env;
 
          cpu_sparc_set_id(env, i);
 
-        /* Reset data */
-        reset_info->info[i].id = i;
-        reset_info->info[i].cpu = cpu;
-        qemu_register_reset(leon3_cpu_reset, &reset_info->info[i]);
+        qemu_register_reset(leon3_cpu_reset, cpu[i]);
          qemu_register_reset(leon3_cpu_reset, CPU(cpu[i]));
Quoted 39 lines

     }

     ahb_pnp = GRLIB_AHB_PNP(qdev_new(TYPE_GRLIB_AHB_PNP));

hw/sparc/leon3.c +3 -4
@@ -312,13 +295,12 @@ static void leon3_generic_hw_init(MachineState 
 *machine)
      sysbus_realize_and_unref(SYS_BUS_DEVICE(irqmpdev), &error_fatal);
 
      for (i = 0; i < machine->smp.cpus; i++) {
-        cpu = reset_info->info[i].cpu;
-        env = &cpu->env;
+        env = &cpu[i]->env;
          qdev_connect_gpio_out_named(irqmpdev, "grlib-start-cpu", i,
-                                    qdev_get_gpio_in_named(DEVICE(cpu),
+                                    qdev_get_gpio_in_named(DEVICE(cpu[i]),
 
 "start_cpu", 0));
          qdev_connect_gpio_out_named(irqmpdev, "grlib-irq", i,
-                                    qdev_get_gpio_in_named(DEVICE(cpu),
+                                    qdev_get_gpio_in_named(DEVICE(cpu[i]),
                                                             "pil", 0));

         env->irq_manager = irqmpdev;
         env->qemu_irq_ack = leon3_irq_manager;

hw/sparc/leon3.c +0 -6
@@ -396,11 +378,6 @@ static void leon3_generic_hw_init(MachineState 
 *machine)
               * bootloader.
               */
              write_bootloader(memory_region_get_ram_ptr(prom), entry);
-            reset_info->entry = LEON3_PROM_OFFSET;
-            for (i = 0; i < machine->smp.cpus; i++) {
-                reset_info->info[i].cpu->env.pc = LEON3_PROM_OFFSET;
-                reset_info->info[i].cpu->env.npc = LEON3_PROM_OFFSET + 4;
-            }
          }
      }
---

Regards,

Phil.

#11 lore raw

Re: [PATCH 3/3] meson: Enable -Wvla

In reply to Thomas Huth
Quoted 34 lines · Thomas Huth
Quoted 24 lines · Thomas Huth

From: Peter Maydell <[email protected]>

QEMU has historically used variable length arrays only very rarely. Variable length arrays are a potential security issue where an on-stack dynamic allocation isn't correctly size-checked, especially when the size comes from the guest.  (An example problem of this kind from the past is CVE-2021-3527).  Forbidding them entirely is a defensive measure against further bugs of this kind.

Enable -Wvla to prevent any new uses from sneaking into the codebase.

Message-ID: <[email protected]>

[thuth: rebased to current master branch]

---
  meson.build | 1 +

  1 file changed, 1 insertion(+)
meson.build +0 -0
index c1dc83e4c0..0ef1654e86 100644

meson.build warn_flags = [ · line 597

Quoted 6 lines
meson.build +1 -0
@@ -592,6 +592,7 @@ warn_flags = [
     '-Wstrict-prototypes',
     '-Wtype-limits',
     '-Wundef',
+  '-Wvla',
     '-Wwrite-strings',
     # Then disable some undesirable warnings

Sigh, there's a new warning in the latest master branch:

Quoted 4 lines

 https://gitlab.com/thuth/qemu/-/jobs/6225992174

Caused by commit d65aba828 ("hw/sparc/leon3: implement multiprocessor")...
Clément, Philippe, could this maybe be written in a different way that does not trigger a -Wvla warning?

I think the DO_UPCAST is wrong here - if I got that right, DO_UPCAST is supposed to check that the second parameter is at the same location as the first type later points to. This is not the case here. I think we rather want container_of() here, so this patch is likely a simple fix:

hw/sparc/leon3.c +1 -1
@@ -150,7 +150,7 @@ static void leon3_cpu_reset(void *opaque)
 {
     struct CPUResetData *info = (struct CPUResetData *) opaque;
     int id = info->id;
-    ResetData *s = (ResetData *)DO_UPCAST(ResetData, info[id], info);
+    ResetData *s = container_of(info, ResetData, info[id]);
     CPUState *cpu = CPU(s->info[id].cpu);
     CPUSPARCState *env = cpu_env(cpu);
 

I can send it as a proper patch, too.

Thomas

#10 lore raw

Re: [PATCH 3/3] meson: Enable -Wvla

In reply to Philippe Mathieu-Daudé
Quoted 39 lines · Philippe Mathieu-Daudé
Quoted 12 lines · Thomas Huth
Quoted 11 lines · Thomas Huth

From: Peter Maydell <[email protected]>

QEMU has historically used variable length arrays only very rarely. Variable length arrays are a potential security issue where an on-stack dynamic allocation isn't correctly size-checked, especially when the size comes from the guest. (An example problem of this kind from the past is CVE-2021-3527). Forbidding them entirely is a defensive measure against further bugs of this kind.

Enable -Wvla to prevent any new uses from sneaking into the codebase.

Quoted 7 lines
Quoted 7 lines

Message-ID: <[email protected]>

[thuth: rebased to current master branch]
---
  meson.build | 1 +
  1 file changed, 1 insertion(+)
Quoted 14 lines

meson.build warn_flags = [ · line 597

Quoted 11 lines
meson.build +1 -0
index c1dc83e4c0..0ef1654e86 100644
@@ -592,6 +592,7 @@ warn_flags = [
    '-Wstrict-prototypes',
    '-Wtype-limits',
    '-Wundef',
+  '-Wvla',
    '-Wwrite-strings',
    # Then disable some undesirable warnings

Sigh, there's a new warning in the latest master branch:

Quoted 8 lines
Quoted 5 lines

https://gitlab.com/thuth/qemu/-/jobs/6225992174

Caused by commit d65aba828 ("hw/sparc/leon3: implement multiprocessor")... Clément, Philippe, could this maybe be written in a different way that does not trigger a -Wvla warning?

Clément, ResetData::entry isn't used, so we can simplify removing the whole ResetData structure, but I'm not sure this is intended:

AFAICT, it used to take the kernel entry point before the bootloader part was implemented. But I'm wondering if it could not be one of the issues being the avocado failure. I do want to resurrect it but I missed the time at the moment...
I'll do some testing with your patch and see how it goes anyway.

Quoted 126 lines

-- >8 --

hw/sparc/leon3.c +3 -14
index 4873b59b6c..1ff6b5d63d 100644
@@ -68,14 +68,6 @@
  #define LEON3_APB_PNP_OFFSET (0x800FF000)
  #define LEON3_AHB_PNP_OFFSET (0xFFFFF000)
 
-typedef struct ResetData {
-    struct CPUResetData {
-        int id;
-        SPARCCPU *cpu;
-    } info[MAX_CPUS];
-    uint32_t entry;             /* save kernel entry in case of reset */
-} ResetData;
-
  static uint32_t *gen_store_u32(uint32_t *code, hwaddr addr, uint32_t val)
  {
      stl_p(code++, 0x82100000); /* mov %g0, %g1                */
@@ -148,17 +140,14 @@ static void write_bootloader(void *ptr, hwaddr
 kernel_addr)
 
  static void leon3_cpu_reset(void *opaque)
  {
-    struct CPUResetData *info = (struct CPUResetData *) opaque;
-    int id = info->id;
-    ResetData *s = (ResetData *)DO_UPCAST(ResetData, info[id], info);
-    CPUState *cpu = CPU(s->info[id].cpu);
+    CPUState *cpu = opaque;
      CPUSPARCState *env = cpu_env(cpu);
 
      cpu_reset(cpu);
 
      cpu->halted = cpu->cpu_index != 0;
-    env->pc = s->entry;
-    env->npc = s->entry + 4;
+    env->pc = LEON3_PROM_OFFSET;
+    env->npc = LEON3_PROM_OFFSET + 4;
  }
 

static void leon3_cache_control_int(CPUSPARCState *env)

hw/sparc/leon3.c +10 -17
@@ -259,7 +248,7 @@ static void leon3_generic_hw_init(MachineState *machine)
      ram_addr_t ram_size = machine->ram_size;
      const char *bios_name = machine->firmware ?: LEON3_PROM_FILENAME;
      const char *kernel_filename = machine->kernel_filename;
-    SPARCCPU *cpu;
+    SPARCCPU *cpu[MAX_CPUS];
      CPUSPARCState   *env;
      MemoryRegion *address_space_mem = get_system_memory();
      MemoryRegion *prom = g_new(MemoryRegion, 1);
@@ -267,28 +256,22 @@ static void leon3_generic_hw_init(MachineState
 *machine)
      char       *filename;
      int         bios_size;
      int         prom_size;
-    ResetData  *reset_info;
      DeviceState *dev, *irqmpdev;
      int i;
      AHBPnp *ahb_pnp;
      APBPnp *apb_pnp;
 
-    reset_info = g_malloc0(sizeof(ResetData));
-
      for (i = 0; i < machine->smp.cpus; i++) {
          /* Init CPU */
-        cpu = SPARC_CPU(object_new(machine->cpu_type));
-        qdev_init_gpio_in_named(DEVICE(cpu), leon3_start_cpu,
 "start_cpu", 1);
-        qdev_init_gpio_in_named(DEVICE(cpu), leon3_set_pil_in, "pil", 1);
-        qdev_realize(DEVICE(cpu), NULL, &error_fatal);
-        env = &cpu->env;
+        cpu[i] = SPARC_CPU(object_new(machine->cpu_type));
+        qdev_init_gpio_in_named(DEVICE(cpu[i]), leon3_start_cpu,
 "start_cpu", 1);
+        qdev_init_gpio_in_named(DEVICE(cpu[i]), leon3_set_pil_in,
 "pil", 1);
+        qdev_realize(DEVICE(cpu[i]), NULL, &error_fatal);
+        env = &cpu[i]->env;
 
          cpu_sparc_set_id(env, i);
 
-        /* Reset data */
-        reset_info->info[i].id = i;
-        reset_info->info[i].cpu = cpu;
-        qemu_register_reset(leon3_cpu_reset, &reset_info->info[i]);
+        qemu_register_reset(leon3_cpu_reset, cpu[i]);
      }
 
      ahb_pnp = GRLIB_AHB_PNP(qdev_new(TYPE_GRLIB_AHB_PNP));
@@ -312,13 +295,12 @@ static void leon3_generic_hw_init(MachineState
 *machine)
      sysbus_realize_and_unref(SYS_BUS_DEVICE(irqmpdev), &error_fatal);
 
      for (i = 0; i < machine->smp.cpus; i++) {
-        cpu = reset_info->info[i].cpu;
-        env = &cpu->env;
+        env = &cpu[i]->env;
          qdev_connect_gpio_out_named(irqmpdev, "grlib-start-cpu", i,
-                                    qdev_get_gpio_in_named(DEVICE(cpu),
+                                    qdev_get_gpio_in_named(DEVICE(cpu[i]),
 
 "start_cpu", 0));
          qdev_connect_gpio_out_named(irqmpdev, "grlib-irq", i,
-                                    qdev_get_gpio_in_named(DEVICE(cpu),
+                                    qdev_get_gpio_in_named(DEVICE(cpu[i]),
                                                             "pil", 0));
          env->irq_manager = irqmpdev;
          env->qemu_irq_ack = leon3_irq_manager;
hw/sparc/leon3.c +0 -6
@@ -396,11 +378,6 @@ static void leon3_generic_hw_init(MachineState
 *machine)
               * bootloader.
               */
              write_bootloader(memory_region_get_ram_ptr(prom), entry);
-            reset_info->entry = LEON3_PROM_OFFSET;
-            for (i = 0; i < machine->smp.cpus; i++) {
-                reset_info->info[i].cpu->env.pc = LEON3_PROM_OFFSET;
-                reset_info->info[i].cpu->env.npc = LEON3_PROM_OFFSET + 4;
-            }
          }
      }
---

Regards,

Phil.