Skip to contentSkip to conversation

Fixes for 11.1

PATCH Superseded Marc-André Lureau 5 participants 19 messages Jul 25 – Jul 27 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fixes for 11.1

Hi

Here are some assorted patches worth considering for the 11.1 freeze bug-fix period.

---
Marc-André Lureau (7):
hw/display/virtio-gpu-rutabaga: zero-init capset info response block/blkio: fix error return value on getlength()
Fix many -Werror=maybe-uninitialized
hw/hexagon: fix machine->fdt leak in qom-test hw/core/machine: free machine->fdt in machine_finalize() migration/multifd: fix Error leak in multifd_recv_terminate_threads()
qdev-monitor: drain RCU callbacks in qdev_device_add_from_qdict

 block/blkio.c                    |  8 ++++----
 block/qcow2.c                    |  2 +-
 hw/core/machine.c                |  1 +
 hw/cxl/cxl-host.c                |  2 +-
 hw/display/virtio-gpu-rutabaga.c |  2 ++
 hw/hexagon/virt.c                |  9 +--------
 hw/scsi/scsi-disk.c              |  2 +-
 hw/scsi/scsi-generic.c           |  4 ++--
 hw/scsi/virtio-scsi.c            |  2 +-
 io/net-listener.c                | 10 +++++-----
 migration/multifd.c              |  1 +
 system/qdev-monitor.c            | 33 ++++++++++-----------------------
 target/i386/cpu.c                |  3 ++-
 target/i386/emulate/x86_mmu.c    |  4 ++--
 14 files changed, 34 insertions(+), 49 deletions(-)

---
base-commit: 300438ffbb8d9430cac2fcc15cba6f482b2c0587
change-id: 20260725-fix2-6e51d231794b

Best regards,

Signature · 1 line
Marc-André Lureau <[email protected]>
#1 lore raw

PATCH 1/7 hw/display/virtio-gpu-rutabaga: zero-init capset info response

rutabaga_cmd_get_capset_info() only fills in capset_id, capset_max_version and capset_max_size before sending the response to the guest. The remaining fields of struct virtio_gpu_resp_capset_info, including hdr.fence_id, hdr.ctx_id and hdr.ring_idx, are left with stack garbage and leaked to the guest, including host pointers useful for an ASLR bypass.

Zero the response first, matching virgl_cmd_get_capset_info().

Not a real risk thanks to -ftrivial-auto-var-init=zero.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3609

Fixes 1dcc6adbc168 ("gfxstream + rutabaga: add initial support for gfxstream")
---
 hw/display/virtio-gpu-rutabaga.c | 2 ++
 1 file changed, 2 insertions(+)
hw/display/virtio-gpu-rutabaga.c +2 -0
index 4d7d7b245929..95a19eed7eda 100644
@@ -555,6 +555,8 @@ rutabaga_cmd_get_capset_info(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
 
     VIRTIO_GPU_FILL_CMD(info);
 
+    memset(&resp, 0, sizeof(resp));
+
     result = rutabaga_get_capset_info(vr->rutabaga, info.capset_index,
                                       &resp.capset_id, &resp.capset_max_version,
                                       &resp.capset_max_size);
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH 1/7] hw/display/virtio-gpu-rutabaga: zero-init capset info response

Quoted 10 lines · Marc-André Lureau

rutabaga_cmd_get_capset_info() only fills in capset_id, capset_max_version and capset_max_size before sending the response to the guest. The remaining fields of struct virtio_gpu_resp_capset_info, including hdr.fence_id, hdr.ctx_id and hdr.ring_idx, are left with stack garbage and leaked to the guest, including host pointers useful for an ASLR bypass.

Zero the response first, matching virgl_cmd_get_capset_info().

Not a real risk thanks to -ftrivial-auto-var-init=zero.

GCC only added -ftrivial-auto-var-init=zero in GCC 12, while QEMU still supports GCC 10.4/11.

Regards,
Akihiko Odaki

Quoted 23 lines

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3609

Fixes 1dcc6adbc168 ("gfxstream + rutabaga: add initial support for gfxstream")
---
  hw/display/virtio-gpu-rutabaga.c | 2 ++
  1 file changed, 2 insertions(+)
hw/display/virtio-gpu-rutabaga.c +2 -0
index 4d7d7b245929..95a19eed7eda 100644
@@ -555,6 +555,8 @@ rutabaga_cmd_get_capset_info(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
 
      VIRTIO_GPU_FILL_CMD(info);
 
+    memset(&resp, 0, sizeof(resp));
+
      result = rutabaga_get_capset_info(vr->rutabaga, info.capset_index,
                                        &resp.capset_id, &resp.capset_max_version,
                                        &resp.capset_max_size);
#3 lore raw

Re: [PATCH 1/7] hw/display/virtio-gpu-rutabaga: zero-init capset info response

Hi

Quoted 9 lines · Akihiko Odaki
Quoted 7 lines · Marc-André Lureau

rutabaga_cmd_get_capset_info() only fills in capset_id, capset_max_version and capset_max_size before sending the response to the guest. The remaining fields of struct virtio_gpu_resp_capset_info, including hdr.fence_id, hdr.ctx_id and hdr.ring_idx, are left with stack garbage and leaked to the guest, including host pointers useful for an ASLR bypass.

Quoted 7 lines
Quoted 3 lines

Zero the response first, matching virgl_cmd_get_capset_info().

Not a real risk thanks to -ftrivial-auto-var-init=zero.

GCC only added -ftrivial-auto-var-init=zero in GCC 12, while QEMU still supports GCC 10.4/11.

Good point, may be we should update:
https://patchew.org/QEMU/[email protected]/

ack otherwise?

Quoted 27 lines

Regards,
Akihiko Odaki

Quoted 23 lines

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3609

Fixes 1dcc6adbc168 ("gfxstream + rutabaga: add initial support for gfxstream")
---
  hw/display/virtio-gpu-rutabaga.c | 2 ++
  1 file changed, 2 insertions(+)
hw/display/virtio-gpu-rutabaga.c +2 -0
index 4d7d7b245929..95a19eed7eda 100644
@@ -555,6 +555,8 @@ rutabaga_cmd_get_capset_info(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
 
      VIRTIO_GPU_FILL_CMD(info);
 
+    memset(&resp, 0, sizeof(resp));
+
      result = rutabaga_get_capset_info(vr->rutabaga, info.capset_index,
                                        &resp.capset_id, &resp.capset_max_version,
                                        &resp.capset_max_size);
Reviewed-by
#4 lore raw

Re: [PATCH 1/7] hw/display/virtio-gpu-rutabaga: zero-init capset info response

Quoted 17 lines · Marc-André Lureau

Hi

Quoted 13 lines · Akihiko Odaki
Quoted 10 lines · Marc-André Lureau

rutabaga_cmd_get_capset_info() only fills in capset_id, capset_max_version and capset_max_size before sending the response to the guest. The remaining fields of struct virtio_gpu_resp_capset_info, including hdr.fence_id, hdr.ctx_id and hdr.ring_idx, are left with stack garbage and leaked to the guest, including host pointers useful for an ASLR bypass.

Zero the response first, matching virgl_cmd_get_capset_info().

Not a real risk thanks to -ftrivial-auto-var-init=zero.

Quoted 8 lines
Quoted 3 lines

GCC only added -ftrivial-auto-var-init=zero in GCC 12, while QEMU still supports GCC 10.4/11.

Good point, may be we should update:
https://patchew.org/QEMU/[email protected]/

ack otherwise?

Yes:

Regards.
Akihiko Odaki

Quoted 29 lines
Quoted 28 lines

Regards,
Akihiko Odaki

Quoted 23 lines

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3609

Fixes 1dcc6adbc168 ("gfxstream + rutabaga: add initial support for gfxstream")
---
   hw/display/virtio-gpu-rutabaga.c | 2 ++
   1 file changed, 2 insertions(+)
hw/display/virtio-gpu-rutabaga.c +2 -0
index 4d7d7b245929..95a19eed7eda 100644
@@ -555,6 +555,8 @@ rutabaga_cmd_get_capset_info(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd)
 
       VIRTIO_GPU_FILL_CMD(info);
 
+    memset(&resp, 0, sizeof(resp));
+
       result = rutabaga_get_capset_info(vr->rutabaga, info.capset_index,
                                         &resp.capset_id, &resp.capset_max_version,
                                         &resp.capset_max_size);
#5 lore raw

Re: [PATCH 1/7] hw/display/virtio-gpu-rutabaga: zero-init capset info response

In reply to Marc-André Lureau
Quoted 19 lines · Marc-André Lureau

Hi

Quoted 15 lines · Akihiko Odaki
Quoted 10 lines · Marc-André Lureau

rutabaga_cmd_get_capset_info() only fills in capset_id, capset_max_version and capset_max_size before sending the response to the guest. The remaining fields of struct virtio_gpu_resp_capset_info, including hdr.fence_id, hdr.ctx_id and hdr.ring_idx, are left with stack garbage and leaked to the guest, including host pointers useful for an ASLR bypass.

Zero the response first, matching virgl_cmd_get_capset_info().

Not a real risk thanks to -ftrivial-auto-var-init=zero.

GCC only added -ftrivial-auto-var-init=zero in GCC 12, while QEMU still supports GCC 10.4/11.

Opps, I thought we had new enough versions already.

Quoted 3 lines

Good point, may be we should update:
https://patchew.org/QEMU/[email protected]/

I'm inclined to bump our min GCC/Clang versions so we can assume zero-init.

We picked 10.5 due to NetBSD, and CentOS 9 still has 11.x

In both those cases, however, Clang is available in new enough version to support -ftrivial-auto-var-init=zero. In NetBSD there are also many new versions of GCC available, only the base system GCC is 10.5.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#6 lore raw

Re: [PATCH 1/7] hw/display/virtio-gpu-rutabaga: zero-init capset info response

Hi

Quoted 31 lines · Daniel P. Berrangé
Quoted 19 lines · Marc-André Lureau

Hi

Quoted 15 lines · Akihiko Odaki
Quoted 10 lines · Marc-André Lureau

rutabaga_cmd_get_capset_info() only fills in capset_id, capset_max_version and capset_max_size before sending the response to the guest. The remaining fields of struct virtio_gpu_resp_capset_info, including hdr.fence_id, hdr.ctx_id and hdr.ring_idx, are left with stack garbage and leaked to the guest, including host pointers useful for an ASLR bypass.

Zero the response first, matching virgl_cmd_get_capset_info().

Not a real risk thanks to -ftrivial-auto-var-init=zero.

GCC only added -ftrivial-auto-var-init=zero in GCC 12, while QEMU still supports GCC 10.4/11.

Opps, I thought we had new enough versions already.

Quoted 3 lines

Good point, may be we should update:
https://patchew.org/QEMU/[email protected]/

I'm inclined to bump our min GCC/Clang versions so we can assume zero-init.

Quoted 6 lines

We picked 10.5 due to NetBSD, and CentOS 9 still has 11.x

In both those cases, however, Clang is available in new enough version to support -ftrivial-auto-var-init=zero. In NetBSD there are also many new versions of GCC available, only the base system GCC is 10.5.

Apparently it needs clang 16 from Mar 2023
(https://releases.llvm.org/18.1.6/tools/clang/docs/ReleaseNotes.html#removed-compiler-flags)

Quoted 10 lines

With regards,
Daniel
--

|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#7 lore raw

PATCH 2/7 block/blkio: fix error return value on getlength()

In reply to Marc-André Lureau

Negative values should be return for errors.

It also helps with GCC false-positives:
../block/blkio.c: In function ‘blkio_co_getlength’:
../block/blkio.c:943:8: error: ‘ret’ may be used uninitialized [-Werror=maybe-uninitialized]
943 | if (ret < 0) {
| ^

Fixes fd66dbd424 ("blkio: add libblkio block driver")
---
 block/blkio.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/blkio.c +2 -2
index fb8bec27d71a..d2ba2a4d58dc 100644
@@ -935,13 +935,13 @@ static int64_t coroutine_fn blkio_co_getlength(BlockDriverState *bs)
 {
     BDRVBlkioState *s = bs->opaque;
     uint64_t capacity;
-    int ret;
+    int ret = -1;
 
     WITH_QEMU_LOCK_GUARD(&s->blkio_lock) {
         ret = blkio_get_uint64(s->blkio, "capacity", &capacity);
     }
     if (ret < 0) {
-        return -ret;
+        return ret;
     }
 
     return capacity;
Signature · 1 line
2.55.0
#8 lore raw

Re: [PATCH 2/7] block/blkio: fix error return value on getlength()

Quoted 1 line · Marc-André Lureau

Negative values should be return for errors.

Nit: s/return/returned/

Quoted 18 lines

It also helps with GCC false-positives:
../block/blkio.c: In function ‘blkio_co_getlength’:
../block/blkio.c:943:8: error: ‘ret’ may be used uninitialized [-Werror=maybe-uninitialized]
943 | if (ret < 0) {
| ^

Fixes fd66dbd424 ("blkio: add libblkio block driver")
---
  block/blkio.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
block/blkio.c +0 -0
index fb8bec27d71a..d2ba2a4d58dc 100644

block/blkio.c static int64_t coroutine_fn blkio_co_getlength(BlockDriverState *bs) · line 938

Quoted 5 lines
block/blkio.c +1 -1
@@ -935,13 +935,13 @@ static int64_t coroutine_fn blkio_co_getlength(BlockDriverState *bs)
  {
      BDRVBlkioState *s = bs->opaque;
      uint64_t capacity;
-    int ret;
+    int ret = -1;

Initializing a variable with a dummy value can suppress a useful warning. Replacing WITH_QEMU_LOCK_GUARD() with QEMU_LOCK_GUARD() will fix it without introducing a dummy value and also result in a bit simpler code.

Regards,
Akihiko Odaki

Quoted 11 lines
block/blkio.c +1 -1
      WITH_QEMU_LOCK_GUARD(&s->blkio_lock) {
          ret = blkio_get_uint64(s->blkio, "capacity", &capacity);
      }
      if (ret < 0) {
-        return -ret;
+        return ret;
      }
 
      return capacity;
 
#9 lore raw

PATCH 3/7 Fix many -Werror=maybe-uninitialized

In reply to Marc-André Lureau

When compiled with -Og, gcc produces many false-positives gcc (GCC) 16.1.1 20260515 (Red Hat 16.1.1-2).

We already use auto-var-init=zero, but better be explicit.

---
 block/blkio.c                 |  4 ++--
 block/qcow2.c                 |  2 +-
 hw/cxl/cxl-host.c             |  2 +-
 hw/scsi/scsi-disk.c           |  2 +-
 hw/scsi/scsi-generic.c        |  4 ++--
 hw/scsi/virtio-scsi.c         |  2 +-
 io/net-listener.c             | 10 +++++-----
 target/i386/cpu.c             |  3 ++-
 target/i386/emulate/x86_mmu.c |  4 ++--
 9 files changed, 17 insertions(+), 16 deletions(-)
block/blkio.c +2 -2
index d2ba2a4d58dc..f7cb6e0b57d4 100644
@@ -278,7 +278,7 @@ static bool blkio_completion_fd_poll(void *opaque)
 {
     BlockDriverState *bs = opaque;
     BDRVBlkioState *s = bs->opaque;
-    int ret;
+    int ret = -1;
 
     /* Just in case we already fetched a completion */
     if (s->poll_completion.user_data != NULL) {
@@ -559,7 +559,7 @@ static bool blkio_register_buf(BlockDriverState *bs, void *host, size_t size,
     BDRVBlkioState *s = bs->opaque;
     struct blkio_mem_region region;
     BlkioMemRegionResult region_result;
-    int ret;
+    int ret = -1;
 
     /*
      * Mapping memory regions conflicts with RAM discard (virtio-mem) when
block/qcow2.c +1 -1
index 19271b10a49f..639593d737c8 100644
@@ -838,7 +838,7 @@ static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
 static void coroutine_fn cache_clean_timer(void *opaque)
 {
     BDRVQcow2State *s = opaque;
-    uint64_t wait_ns;
+    uint64_t wait_ns = 0;
 
     WITH_QEMU_LOCK_GUARD(&s->lock) {
         wait_ns = s->cache_clean_interval * NANOSECONDS_PER_SECOND;
hw/cxl/cxl-host.c +1 -1
index 7e744312f1d8..eba13c9e7cba 100644
@@ -279,7 +279,7 @@ static void cxl_fmws_direct_passthrough_setup(CXLDirectPTState *state,
     MemoryRegion *mr = NULL;
     uint64_t vmr_size = 0, pmr_size = 0, offset = 0;
     MemoryRegion *direct_mr;
-    g_autofree char *direct_mr_name;
+    g_autofree char *direct_mr_name = NULL;
     unsigned int idx = state->hdm_decoder_idx;
 
     if (ct3d->hostvmem) {
hw/scsi/scsi-disk.c +1 -1
index 1b0cce128c5e..82dc75cb7d10 100644
@@ -3263,7 +3263,7 @@ static bool scsi_disk_pr_state_needed(void *opaque)
 {
     SCSIDiskState *s = opaque;
     SCSIPRState *pr_state = &s->qdev.pr_state;
-    bool ret;
+    bool ret = false;
 
     if (!s->qdev.migrate_pr) {
         return false;
hw/scsi/scsi-generic.c +2 -2
index 8999f3b72006..b044561ae2dd 100644
@@ -553,8 +553,8 @@ bool scsi_generic_pr_state_preempt(SCSIDevice *s, Error **errp)
     SCSIPRState *pr_state = &s->pr_state;
     Error *local_err = NULL;
     bool check_stale_key = true;
-    uint64_t key;
-    uint8_t resv_type;
+    uint64_t key = 0;
+    uint8_t resv_type = 0;
 
     /* Get the migrated PR state */
     WITH_QEMU_LOCK_GUARD(&pr_state->mutex) {
hw/scsi/virtio-scsi.c +1 -1
index 6c7376801190..833f773d7f6c 100644
@@ -1093,7 +1093,7 @@ static void virtio_scsi_push_event(VirtIOSCSI *s,
 
 static void virtio_scsi_handle_event_vq(VirtIOSCSI *s, VirtQueue *vq)
 {
-    bool events_dropped;
+    bool events_dropped = false;
 
     WITH_QEMU_LOCK_GUARD(&s->event_lock) {
         events_dropped = s->events_dropped;
io/net-listener.c +5 -5
index 1fd0f6cb5ab8..8df19c35bd8f 100644
@@ -54,11 +54,11 @@ static gboolean qio_net_listener_channel_func(QIOChannel *ioc,
                                               gpointer opaque)
 {
     QIONetListener *listener = QIO_NET_LISTENER(opaque);
-    QIOChannelSocket *sioc;
-    QIONetListenerClientFunc io_func;
-    gpointer io_data;
-    GMainContext *context;
-    AioContext *aio_context;
+    QIOChannelSocket *sioc = NULL;
+    QIONetListenerClientFunc io_func = NULL;
+    gpointer io_data = NULL;
+    GMainContext *context = NULL;
+    AioContext *aio_context = NULL;
 
     sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
                                      NULL);
target/i386/cpu.c +2 -1
index 5805d33ab92d..e9759ab25077 100644
@@ -7734,7 +7734,7 @@ static void x86_cpuid_get_avx10_version(Object *obj, Visitor *v,
 static bool x86_cpu_apply_avx10_features(X86CPU *cpu, uint8_t version,
                                          Error **errp)
 {
-    const AVX10VersionDefinition *def;
+    const AVX10VersionDefinition *def = NULL;
     CPUX86State *env = &cpu->env;
 
     if (!version) {
@@ -7758,6 +7758,7 @@ static bool x86_cpu_apply_avx10_features(X86CPU *cpu, uint8_t version,
         }
     }
 
+    assert(def != NULL);
     if (def->version < version) {
         error_setg(errp, "avx10-version can be at most %d", def->version);
         return false;
target/i386/emulate/x86_mmu.c +2 -2
index 8d4371467fd7..c1cb385b3228 100644
@@ -185,8 +185,8 @@ static MMUTranslateResult walk_gpt(CPUState *cpu, target_ulong addr, MMUTranslat
     int largeness = 0;
     target_ulong cr3 = x86_read_cr(cpu, 3);
     uint64_t page_mask = pae ? PAE_PTE_PAGE_MASK : LEGACY_PTE_PAGE_MASK;
-    MMUTranslateResult res;
-    
+    MMUTranslateResult res = 0;
+
     memset(pt, 0, sizeof(*pt));
     top_level = gpt_top_level(cpu, pae);
 
Signature · 1 line
2.55.0
#10 lore raw

Re: [PATCH 3/7] Fix many -Werror=maybe-uninitialized

Quoted 4 lines · Marc-André Lureau

When compiled with -Og, gcc produces many false-positives gcc (GCC) 16.1.1 20260515 (Red Hat 16.1.1-2).

We already use auto-var-init=zero, but better be explicit.

I'm kind of reluctant to describe it is more explicit and it is better. The variables causing the false positives do not contain a useful value until later assignments. Initializing them obscures the fact and suppresses useful warnings. This patch may serve as a workaround, but its scope should be minimized.

I think WITH_QEMU_LOCK_GUARD() should be fixed to explicitly tell that the block is unconditionally executed. Linux does so, for example:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fcc22ac5baf06dd17193de44b60dbceea6461983

Quoted 54 lines
---
  block/blkio.c                 |  4 ++--
  block/qcow2.c                 |  2 +-
  hw/cxl/cxl-host.c             |  2 +-
  hw/scsi/scsi-disk.c           |  2 +-
  hw/scsi/scsi-generic.c        |  4 ++--
  hw/scsi/virtio-scsi.c         |  2 +-
  io/net-listener.c             | 10 +++++-----
  target/i386/cpu.c             |  3 ++-
  target/i386/emulate/x86_mmu.c |  4 ++--
  9 files changed, 17 insertions(+), 16 deletions(-)
block/blkio.c +2 -2
index d2ba2a4d58dc..f7cb6e0b57d4 100644
@@ -278,7 +278,7 @@ static bool blkio_completion_fd_poll(void *opaque)
  {
      BlockDriverState *bs = opaque;
      BDRVBlkioState *s = bs->opaque;
-    int ret;
+    int ret = -1;
 
      /* Just in case we already fetched a completion */
      if (s->poll_completion.user_data != NULL) {
@@ -559,7 +559,7 @@ static bool blkio_register_buf(BlockDriverState *bs, void *host, size_t size,
      BDRVBlkioState *s = bs->opaque;
      struct blkio_mem_region region;
      BlkioMemRegionResult region_result;
-    int ret;
+    int ret = -1;
 
      /*
       * Mapping memory regions conflicts with RAM discard (virtio-mem) when
block/qcow2.c +1 -1
index 19271b10a49f..639593d737c8 100644
@@ -838,7 +838,7 @@ static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
  static void coroutine_fn cache_clean_timer(void *opaque)
  {
      BDRVQcow2State *s = opaque;
-    uint64_t wait_ns;
+    uint64_t wait_ns = 0;
 
      WITH_QEMU_LOCK_GUARD(&s->lock) {
          wait_ns = s->cache_clean_interval * NANOSECONDS_PER_SECOND;
hw/cxl/cxl-host.c +0 -0
index 7e744312f1d8..eba13c9e7cba 100644

hw/cxl/cxl-host.c static void cxl_fmws_direct_passthrough_setup(CXLDirectPTState *state, · line 282

Quoted 5 lines
hw/cxl/cxl-host.c +1 -1
@@ -279,7 +279,7 @@ static void cxl_fmws_direct_passthrough_setup(CXLDirectPTState *state,
      MemoryRegion *mr = NULL;
      uint64_t vmr_size = 0, pmr_size = 0, offset = 0;
      MemoryRegion *direct_mr;
-    g_autofree char *direct_mr_name;
+    g_autofree char *direct_mr_name = NULL;

This fixes a real issue, not a false positive. An early return can trigger a cleanup on an uninitialized variable.

Regards,
Akihiko Odaki

Quoted 102 lines
hw/cxl/cxl-host.c +0 -0
      unsigned int idx = state->hdm_decoder_idx;
 
      if (ct3d->hostvmem) {
hw/scsi/scsi-disk.c +1 -1
index 1b0cce128c5e..82dc75cb7d10 100644
@@ -3263,7 +3263,7 @@ static bool scsi_disk_pr_state_needed(void *opaque)
  {
      SCSIDiskState *s = opaque;
      SCSIPRState *pr_state = &s->qdev.pr_state;
-    bool ret;
+    bool ret = false;
 
      if (!s->qdev.migrate_pr) {
          return false;
hw/scsi/scsi-generic.c +2 -2
index 8999f3b72006..b044561ae2dd 100644
@@ -553,8 +553,8 @@ bool scsi_generic_pr_state_preempt(SCSIDevice *s, Error **errp)
      SCSIPRState *pr_state = &s->pr_state;
      Error *local_err = NULL;
      bool check_stale_key = true;
-    uint64_t key;
-    uint8_t resv_type;
+    uint64_t key = 0;
+    uint8_t resv_type = 0;
 
      /* Get the migrated PR state */
      WITH_QEMU_LOCK_GUARD(&pr_state->mutex) {
hw/scsi/virtio-scsi.c +1 -1
index 6c7376801190..833f773d7f6c 100644
@@ -1093,7 +1093,7 @@ static void virtio_scsi_push_event(VirtIOSCSI *s,
 
  static void virtio_scsi_handle_event_vq(VirtIOSCSI *s, VirtQueue *vq)
  {
-    bool events_dropped;
+    bool events_dropped = false;
 
      WITH_QEMU_LOCK_GUARD(&s->event_lock) {
          events_dropped = s->events_dropped;
io/net-listener.c +5 -5
index 1fd0f6cb5ab8..8df19c35bd8f 100644
@@ -54,11 +54,11 @@ static gboolean qio_net_listener_channel_func(QIOChannel *ioc,
                                                gpointer opaque)
  {
      QIONetListener *listener = QIO_NET_LISTENER(opaque);
-    QIOChannelSocket *sioc;
-    QIONetListenerClientFunc io_func;
-    gpointer io_data;
-    GMainContext *context;
-    AioContext *aio_context;
+    QIOChannelSocket *sioc = NULL;
+    QIONetListenerClientFunc io_func = NULL;
+    gpointer io_data = NULL;
+    GMainContext *context = NULL;
+    AioContext *aio_context = NULL;
 
      sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
                                       NULL);
target/i386/cpu.c +2 -1
index 5805d33ab92d..e9759ab25077 100644
@@ -7734,7 +7734,7 @@ static void x86_cpuid_get_avx10_version(Object *obj, Visitor *v,
  static bool x86_cpu_apply_avx10_features(X86CPU *cpu, uint8_t version,
                                           Error **errp)
  {
-    const AVX10VersionDefinition *def;
+    const AVX10VersionDefinition *def = NULL;
      CPUX86State *env = &cpu->env;
 
      if (!version) {
@@ -7758,6 +7758,7 @@ static bool x86_cpu_apply_avx10_features(X86CPU *cpu, uint8_t version,
          }
      }
 
+    assert(def != NULL);
      if (def->version < version) {
          error_setg(errp, "avx10-version can be at most %d", def->version);
          return false;
target/i386/emulate/x86_mmu.c +2 -2
index 8d4371467fd7..c1cb385b3228 100644
@@ -185,8 +185,8 @@ static MMUTranslateResult walk_gpt(CPUState *cpu, target_ulong addr, MMUTranslat
      int largeness = 0;
      target_ulong cr3 = x86_read_cr(cpu, 3);
      uint64_t page_mask = pae ? PAE_PTE_PAGE_MASK : LEGACY_PTE_PAGE_MASK;
-    MMUTranslateResult res;
-
+    MMUTranslateResult res = 0;
+
      memset(pt, 0, sizeof(*pt));
      top_level = gpt_top_level(cpu, pae);
 
#11 lore raw

PATCH 4/7 hw/hexagon: fix machine->fdt leak in qom-test

In reply to Marc-André Lureau

virt_instance_init() built the FDT unconditionally at QOM instance-init time, so simply instantiating the object (e.g. via qom-test's introspection, without ever realizing the machine) leaked the 1MB FDT blob: machine_finalize() does not free machine->fdt.

Other boards (arm/virt, riscv/virt, ...) build the FDT lazily from their MachineClass::init callback, which only runs when the machine is actually selected to boot. Do the same here by moving create_fdt() into virt_init().

Fixes 88a8bc7f43ff ("hw/hexagon: Define hexagon "virt" machine")
---
 hw/hexagon/virt.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
hw/hexagon/virt.c +1 -8
index a3638998b873..b75047250260 100644
@@ -175,13 +175,6 @@ static void fdt_add_cpu_nodes(const HexagonVirtMachineState *vms)
 
 
 
-static void virt_instance_init(Object *obj)
-{
-    HexagonVirtMachineState *vms = HEXAGON_VIRT_MACHINE(obj);
-
-    create_fdt(vms);
-}
-
 void hexagon_load_fdt(const HexagonVirtMachineState *vms)
 {
     MachineState *ms = MACHINE(vms);
@@ -242,6 +235,7 @@ static void virt_init(MachineState *ms)
     DeviceState *cpu0;
     int32_t clk_phandle;
 
+    create_fdt(vms);
     qemu_fdt_setprop_string(ms->fdt, "/chosen", "bootargs", ms->kernel_cmdline);
 
     vms->sys = get_system_memory();
@@ -341,7 +335,6 @@ static const TypeInfo virt_machine_types[] = { {
     .parent = TYPE_HEXAGON_COMMON_MACHINE,
     .instance_size = sizeof(HexagonVirtMachineState),
     .class_init = virt_class_init,
-    .instance_init = virt_instance_init,
 } };
 
 DEFINE_TYPES(virt_machine_types)
Signature · 1 line
2.55.0
Reviewed-by on v1 4/7
#12 lore raw

Re: [PATCH 4/7] hw/hexagon: fix machine->fdt leak in qom-test

Quoted 10 lines · Marc-André Lureau

virt_instance_init() built the FDT unconditionally at QOM instance-init time, so simply instantiating the object (e.g. via qom-test's introspection, without ever realizing the machine) leaked the 1MB FDT blob: machine_finalize() does not free machine->fdt.

Other boards (arm/virt, riscv/virt, ...) build the FDT lazily from their MachineClass::init callback, which only runs when the machine is actually selected to boot. Do the same here by moving create_fdt() into virt_init().

Quoted 5 lines
Fixes 88a8bc7f43ff ("hw/hexagon: Define hexagon "virt" machine")
---
  hw/hexagon/virt.c | 9 +--------
  1 file changed, 1 insertion(+), 8 deletions(-)
Reviewed-by on v1 4/7
#13 lore raw

Re: [PATCH 4/7] hw/hexagon: fix machine->fdt leak in qom-test

In reply to Marc-André Lureau
Quoted 5 lines · Marc-André Lureau

virt_instance_init() built the FDT unconditionally at QOM instance-init time, so simply instantiating the object (e.g. via qom-test's introspection, without ever realizing the machine) leaked the 1MB FDT blob: machine_finalize() does not free machine->fdt.

Quoted 8 lines

Other boards (arm/virt, riscv/virt, ...) build the FDT lazily from their MachineClass::init callback, which only runs when the machine is actually selected to boot. Do the same here by moving create_fdt() into virt_init().

Fixes 88a8bc7f43ff ("hw/hexagon: Define hexagon "virt" machine")

---

Quoted 38 lines
  hw/hexagon/virt.c | 9 +--------
  1 file changed, 1 insertion(+), 8 deletions(-)
hw/hexagon/virt.c +1 -8
index a3638998b873..b75047250260 100644
@@ -175,13 +175,6 @@ static void fdt_add_cpu_nodes(const HexagonVirtMachineState *vms)
 
 
 
-static void virt_instance_init(Object *obj)
-{
-    HexagonVirtMachineState *vms = HEXAGON_VIRT_MACHINE(obj);
-
-    create_fdt(vms);
-}
-
  void hexagon_load_fdt(const HexagonVirtMachineState *vms)
  {
      MachineState *ms = MACHINE(vms);
@@ -242,6 +235,7 @@ static void virt_init(MachineState *ms)
      DeviceState *cpu0;
      int32_t clk_phandle;
 
+    create_fdt(vms);
      qemu_fdt_setprop_string(ms->fdt, "/chosen", "bootargs", ms->kernel_cmdline);
 
      vms->sys = get_system_memory();
@@ -341,7 +335,6 @@ static const TypeInfo virt_machine_types[] = { {
      .parent = TYPE_HEXAGON_COMMON_MACHINE,
      .instance_size = sizeof(HexagonVirtMachineState),
      .class_init = virt_class_init,
-    .instance_init = virt_instance_init,
  } };
 
  DEFINE_TYPES(virt_machine_types)
#14 lore raw

PATCH 5/7 hw/core/machine: free machine->fdt in machine_finalize()

In reply to Marc-André Lureau

machine->fdt is a plain g_malloc0() buffer (see create_device_tree()) that boards populate during MachineClass::init, but machine_finalize() never freed it, unlike the other boot-time buffers (dtb, dumpdtb, kernel_filename, ...). Free it for consistency.

---
 hw/core/machine.c | 1 +
 1 file changed, 1 insertion(+)
hw/core/machine.c +1 -0
index 805148678d57..2b24d711d76f 100644
@@ -1301,6 +1301,7 @@ static void machine_finalize(Object *obj)
     g_free(ms->kernel_cmdline);
     g_free(ms->dtb);
     g_free(ms->dumpdtb);
+    g_free(ms->fdt);
     g_free(ms->dt_compatible);
     g_free(ms->firmware);
     g_free(ms->device_memory);
Signature · 1 line
2.55.0
#15 lore raw

Re: [PATCH 5/7] hw/core/machine: free machine->fdt in machine_finalize()

Quoted 4 lines · Marc-André Lureau

machine->fdt is a plain g_malloc0() buffer (see create_device_tree()) that boards populate during MachineClass::init, but machine_finalize() never freed it, unlike the other boot-time buffers (dtb, dumpdtb, kernel_filename, ...). Free it for consistency.

This unfortunately converts an existing bug into double-free.

spapr_machine_reset() aliases machine->fdt to spapr->fdt_blob. spapr_dtb_pre_load() and h_update_dt() later free it via spapr->fdt_blob, but does not update machine->fdt, making it dangling.

Leaving machine->fdt dangling is already a bug. do_client_architecture_support() explains that as follows:

/*
* Set the machine->fdt pointer again since we just freed
* it above (by freeing spapr->fdt_blob). We set this
* pointer to enable support for the 'dumpdtb' QMP/HMP
* command.
*/
MACHINE(spapr)->fdt = fdt;

This patch introduces another path to trigger the bug.

Regards,
Akihiko Odaki

Quoted 19 lines
---
  hw/core/machine.c | 1 +
  1 file changed, 1 insertion(+)
hw/core/machine.c +1 -0
index 805148678d57..2b24d711d76f 100644
@@ -1301,6 +1301,7 @@ static void machine_finalize(Object *obj)
      g_free(ms->kernel_cmdline);
      g_free(ms->dtb);
      g_free(ms->dumpdtb);
+    g_free(ms->fdt);
      g_free(ms->dt_compatible);
      g_free(ms->firmware);
      g_free(ms->device_memory);
#16 lore raw

PATCH 6/7 migration/multifd: fix Error leak in multifd_recv_terminate_threads()

In reply to Marc-André Lureau

If err != NULL, free it.

Fixes 11dd7be57524 ("migration/multifd: Remove p->quit from recv side")
---
 migration/multifd.c | 1 +
 1 file changed, 1 insertion(+)
migration/multifd.c +1 -0
index dbad525d2a28..503014f76ba5 100644
@@ -1056,6 +1056,7 @@ static void multifd_recv_terminate_threads(Error *err)
     trace_multifd_recv_terminate_threads(err != NULL);
 
     if (qatomic_xchg(&multifd_recv_state->exiting, 1)) {
+        error_free(err);
         return;
     }
 
Signature · 1 line
2.55.0
#17 lore raw

PATCH 7/7 qdev-monitor: drain RCU callbacks in qdev_device_add_from_qdict

In reply to Marc-André Lureau

qmp_device_add() and hmp_device_add() drain pending RCU callbacks after a failed device_add, since some bus teardown (e.g. bus_remove_child()) is deferred to call_rcu(). -device on the command line reaches the same qdev_device_add_from_qdict() with errp pointing at error_fatal, whose ERRP_GUARD() exits before control returns to the caller, so the caller's drain_call_rcu() never runs.

Move the drain into qdev_device_add_from_qdict()'s own err_del_dev path so it always runs, and drop the now-redundant calls in qmp_device_add() and hmp_device_add().

---
 system/qdev-monitor.c | 33 ++++++++++-----------------------
 1 file changed, 10 insertions(+), 23 deletions(-)
system/qdev-monitor.c +10 -23
index 00fed791cce1..884d407d60d4 100644
@@ -746,6 +746,16 @@ err_del_dev:
     object_unparent(OBJECT(dev));
     object_unref(OBJECT(dev));
 
+    /*
+     * Drain all pending RCU callbacks. This is done because some bus
+     * related operations can delay a device removal (in this case this
+     * can happen if device is added and then removed due to a
+     * configuration error) to a RCU callback, but a caller might expect
+     * the failed device to be fully torn down once this function
+     * returns.
+     */
+    drain_call_rcu();
+
     return NULL;
 }
 
@@ -870,18 +880,6 @@ void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
     DeviceState *dev;
 
     dev = qdev_device_add_from_qdict(qdict, true, errp);
-    if (!dev) {
-        /*
-         * Drain all pending RCU callbacks. This is done because
-         * some bus related operations can delay a device removal
-         * (in this case this can happen if device is added and then
-         * removed due to a configuration error)
-         * to a RCU callback, but user might expect that this interface
-         * will finish its job completely once qmp command returns result
-         * to the user
-         */
-        drain_call_rcu();
-    }
     object_unref(OBJECT(dev));
 }
 
@@ -1017,17 +1015,6 @@ void hmp_device_add(Monitor *mon, const QDict *qdict)
     }
     dev = qdev_device_add(opts, &err);
     if (!dev) {
-        /*
-         * Drain all pending RCU callbacks. This is done because
-         * some bus related operations can delay a device removal
-         * (in this case this can happen if device is added and then
-         * removed due to a configuration error)
-         * to a RCU callback, but user might expect that this interface
-         * will finish its job completely once qmp command returns result
-         * to the user
-         */
-        drain_call_rcu();
-
         qemu_opts_del(opts);
     }
     object_unref(dev);
Signature · 1 line
2.55.0
#18 lore raw

Re: [PATCH 7/7] qdev-monitor: drain RCU callbacks in qdev_device_add_from_qdict

Quoted 10 lines · Marc-André Lureau

qmp_device_add() and hmp_device_add() drain pending RCU callbacks after a failed device_add, since some bus teardown (e.g. bus_remove_child()) is deferred to call_rcu(). -device on the command line reaches the same qdev_device_add_from_qdict() with errp pointing at error_fatal, whose ERRP_GUARD() exits before control returns to the caller, so the caller's drain_call_rcu() never runs.

Move the drain into qdev_device_add_from_qdict()'s own err_del_dev path so it always runs, and drop the now-redundant calls in qmp_device_add() and hmp_device_add().

It is reachable from virtio-net failover during guest feature negotiation. MMIO dispatch holds an RCU read lock at address_space_write(). If deferred primary creation fails, drain_call_rcu() waits for a grace period while its own thread remains a reader, deadlocking the VM.

Regards,
Akihiko Odaki

Quoted 65 lines
---
  system/qdev-monitor.c | 33 ++++++++++-----------------------
  1 file changed, 10 insertions(+), 23 deletions(-)
system/qdev-monitor.c +10 -23
index 00fed791cce1..884d407d60d4 100644
@@ -746,6 +746,16 @@ err_del_dev:
      object_unparent(OBJECT(dev));
      object_unref(OBJECT(dev));
 
+    /*
+     * Drain all pending RCU callbacks. This is done because some bus
+     * related operations can delay a device removal (in this case this
+     * can happen if device is added and then removed due to a
+     * configuration error) to a RCU callback, but a caller might expect
+     * the failed device to be fully torn down once this function
+     * returns.
+     */
+    drain_call_rcu();
+
      return NULL;
  }
 
@@ -870,18 +880,6 @@ void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
      DeviceState *dev;
 
      dev = qdev_device_add_from_qdict(qdict, true, errp);
-    if (!dev) {
-        /*
-         * Drain all pending RCU callbacks. This is done because
-         * some bus related operations can delay a device removal
-         * (in this case this can happen if device is added and then
-         * removed due to a configuration error)
-         * to a RCU callback, but user might expect that this interface
-         * will finish its job completely once qmp command returns result
-         * to the user
-         */
-        drain_call_rcu();
-    }
      object_unref(OBJECT(dev));
  }
 
@@ -1017,17 +1015,6 @@ void hmp_device_add(Monitor *mon, const QDict *qdict)
      }
      dev = qdev_device_add(opts, &err);
      if (!dev) {
-        /*
-         * Drain all pending RCU callbacks. This is done because
-         * some bus related operations can delay a device removal
-         * (in this case this can happen if device is added and then
-         * removed due to a configuration error)
-         * to a RCU callback, but user might expect that this interface
-         * will finish its job completely once qmp command returns result
-         * to the user
-         */
-        drain_call_rcu();
-
          qemu_opts_del(opts);
      }
      object_unref(dev);