diff mbox

[v3,00/28] glib: Replace g_memdup() by g_memdup2()

Message ID 20210903174510.751630-1-philmd@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Philippe Mathieu-Daudé Sept. 3, 2021, 5:44 p.m. UTC
Per https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538

  The old API took the size of the memory to duplicate as a guint,
  whereas most memory functions take memory sizes as a gsize. This
  made it easy to accidentally pass a gsize to g_memdup(). For large
  values, that would lead to a silent truncation of the size from 64
  to 32 bits, and result in a heap area being returned which is
  significantly smaller than what the caller expects. This can likely
  be exploited in various modules to cause a heap buffer overflow.

g_memdup() as been deprecated in GLib 2.68. Since QEMU defines
GLIB_VERSION_MAX_ALLOWED to GLIB_VERSION_2_56, the deprecation
is not displayed (on GLib >= 2.68 such available on Fedora 34).
However the function is still unsafe, so it is better to avoid
its use.

This series provides the safely equivalent g_memdup2() wrapper,
and replace all g_memdup() calls by it.

The previous link recommend to audit the call sites. Most of the
calls use byte_size=sizeof(STRUCT), and no STRUCT appears to be
> 4GiB.  Few calls use unsigned/size_t/uint16_t. Where code is
doing multiplication, patches are sent as RFC. In particular:
    hw/net/virtio-net.c
    hw/virtio/virtio-crypto.c

Since v1:
- Added missing g_memdup2 -> g_memdup2_qemu compat definition (danpb)
- Do not call g_memdup2_qemu() but directly g_memdup2() (danpb)

Tested with the following snippet:
-- >8 --
---

Philippe Mathieu-Daudé (28):
  hw/hyperv/vmbus: Remove unused vmbus_load/save_req()
  glib-compat: Introduce g_memdup2() wrapper
  qapi: Replace g_memdup() by g_memdup2()
  accel/tcg: Replace g_memdup() by g_memdup2()
  block/qcow2-bitmap: Replace g_memdup() by g_memdup2()
  softmmu: Replace g_memdup() by g_memdup2()
  hw/9pfs: Replace g_memdup() by g_memdup2()
  hw/acpi: Avoid truncating acpi_data_len() to 32-bit
  hw/acpi: Replace g_memdup() by g_memdup2()
  hw/core/machine: Replace g_memdup() by g_memdup2()
  hw/hppa/machine: Replace g_memdup() by g_memdup2()
  hw/i386/multiboot: Replace g_memdup() by g_memdup2()
  hw/net/eepro100: Replace g_memdup() by g_memdup2()
  hw/nvram/fw_cfg: Replace g_memdup() by g_memdup2()
  hw/scsi/mptsas: Replace g_memdup() by g_memdup2()
  hw/ppc/spapr_pci: Replace g_memdup() by g_memdup2()
  hw/rdma: Replace g_memdup() by g_memdup2()
  hw/vfio/pci: Replace g_memdup() by g_memdup2()
  hw/virtio: Replace g_memdup() by g_memdup2()
  net/colo: Replace g_memdup() by g_memdup2()
  ui/clipboard: Replace g_memdup() by g_memdup2()
  linux-user: Replace g_memdup() by g_memdup2()
  tests/unit: Replace g_memdup() by g_memdup2()
  tests/qtest: Replace g_memdup() by g_memdup2()
  target/arm: Replace g_memdup() by g_memdup2()
  target/ppc: Replace g_memdup() by g_memdup2()
  contrib: Replace g_memdup() by g_memdup2()
  checkpatch: Do not allow deprecated g_memdup()

 include/glib-compat.h       | 37 +++++++++++++++++++++++
 include/hw/hyperv/vmbus.h   |  3 --
 accel/tcg/cputlb.c          |  8 ++---
 block/qcow2-bitmap.c        |  2 +-
 contrib/plugins/lockstep.c  |  2 +-
 contrib/rdmacm-mux/main.c   | 10 +++----
 hw/9pfs/9p-synth.c          |  2 +-
 hw/9pfs/9p.c                |  2 +-
 hw/acpi/core.c              |  3 +-
 hw/arm/virt-acpi-build.c    |  2 +-
 hw/core/machine.c           |  4 +--
 hw/hppa/machine.c           |  8 ++---
 hw/hyperv/vmbus.c           | 59 -------------------------------------
 hw/i386/acpi-build.c        |  6 ++--
 hw/i386/multiboot.c         |  2 +-
 hw/net/eepro100.c           |  2 +-
 hw/net/virtio-net.c         |  3 +-
 hw/nvram/fw_cfg.c           |  9 +++---
 hw/ppc/spapr_pci.c          |  7 ++---
 hw/rdma/rdma_utils.c        |  2 +-
 hw/scsi/mptsas.c            |  5 ++--
 hw/vfio/pci.c               |  2 +-
 hw/virtio/virtio-crypto.c   |  6 ++--
 linux-user/syscall.c        |  2 +-
 linux-user/uaccess.c        |  2 +-
 net/colo.c                  |  4 +--
 qapi/qapi-clone-visitor.c   | 16 +++++-----
 qapi/qapi-visit-core.c      |  6 ++--
 softmmu/memory.c            |  2 +-
 softmmu/vl.c                |  2 +-
 target/arm/helper.c         |  6 ++--
 target/ppc/mmu-hash64.c     |  2 +-
 tests/qtest/libqos/ahci.c   |  6 ++--
 tests/qtest/libqos/qgraph.c |  2 +-
 tests/unit/ptimer-test.c    | 22 +++++++-------
 tests/unit/test-iov.c       | 26 ++++++++--------
 ui/clipboard.c              |  2 +-
 scripts/checkpatch.pl       |  5 ++++
 38 files changed, 138 insertions(+), 153 deletions(-)

Comments

Philippe Mathieu-Daudé Dec. 15, 2021, 4:54 p.m. UTC | #1
Hi Laurent,

On 9/3/21 19:44, Philippe Mathieu-Daudé wrote:

> This series provides the safely equivalent g_memdup2() wrapper,
> and replace all g_memdup() calls by it.

> Philippe Mathieu-Daudé (28):
>   hw/hyperv/vmbus: Remove unused vmbus_load/save_req()
>   glib-compat: Introduce g_memdup2() wrapper
>   qapi: Replace g_memdup() by g_memdup2()
>   accel/tcg: Replace g_memdup() by g_memdup2()
>   block/qcow2-bitmap: Replace g_memdup() by g_memdup2()
>   softmmu: Replace g_memdup() by g_memdup2()
>   hw/9pfs: Replace g_memdup() by g_memdup2()
>   hw/acpi: Avoid truncating acpi_data_len() to 32-bit
>   hw/acpi: Replace g_memdup() by g_memdup2()
>   hw/core/machine: Replace g_memdup() by g_memdup2()
>   hw/hppa/machine: Replace g_memdup() by g_memdup2()
>   hw/i386/multiboot: Replace g_memdup() by g_memdup2()
>   hw/net/eepro100: Replace g_memdup() by g_memdup2()
>   hw/nvram/fw_cfg: Replace g_memdup() by g_memdup2()
>   hw/scsi/mptsas: Replace g_memdup() by g_memdup2()
>   hw/ppc/spapr_pci: Replace g_memdup() by g_memdup2()
>   hw/rdma: Replace g_memdup() by g_memdup2()
>   hw/vfio/pci: Replace g_memdup() by g_memdup2()
>   hw/virtio: Replace g_memdup() by g_memdup2()
>   net/colo: Replace g_memdup() by g_memdup2()
>   ui/clipboard: Replace g_memdup() by g_memdup2()
>   linux-user: Replace g_memdup() by g_memdup2()
>   tests/unit: Replace g_memdup() by g_memdup2()
>   tests/qtest: Replace g_memdup() by g_memdup2()
>   target/arm: Replace g_memdup() by g_memdup2()
>   target/ppc: Replace g_memdup() by g_memdup2()
>   contrib: Replace g_memdup() by g_memdup2()
>   checkpatch: Do not allow deprecated g_memdup(
Is it possible to take the reviewed patches 2, 24 and 28
via qemu-trivial, so the rest can be merged slowly by
each submaintainer?
Laurent Vivier Dec. 17, 2021, 10:59 a.m. UTC | #2
Le 15/12/2021 à 17:54, Philippe Mathieu-Daudé a écrit :
> Hi Laurent,
> 
> On 9/3/21 19:44, Philippe Mathieu-Daudé wrote:
> 
>> This series provides the safely equivalent g_memdup2() wrapper,
>> and replace all g_memdup() calls by it.
> 
>> Philippe Mathieu-Daudé (28):
>>    hw/hyperv/vmbus: Remove unused vmbus_load/save_req()
>>    glib-compat: Introduce g_memdup2() wrapper
>>    qapi: Replace g_memdup() by g_memdup2()
>>    accel/tcg: Replace g_memdup() by g_memdup2()
>>    block/qcow2-bitmap: Replace g_memdup() by g_memdup2()
>>    softmmu: Replace g_memdup() by g_memdup2()
>>    hw/9pfs: Replace g_memdup() by g_memdup2()
>>    hw/acpi: Avoid truncating acpi_data_len() to 32-bit
>>    hw/acpi: Replace g_memdup() by g_memdup2()
>>    hw/core/machine: Replace g_memdup() by g_memdup2()
>>    hw/hppa/machine: Replace g_memdup() by g_memdup2()
>>    hw/i386/multiboot: Replace g_memdup() by g_memdup2()
>>    hw/net/eepro100: Replace g_memdup() by g_memdup2()
>>    hw/nvram/fw_cfg: Replace g_memdup() by g_memdup2()
>>    hw/scsi/mptsas: Replace g_memdup() by g_memdup2()
>>    hw/ppc/spapr_pci: Replace g_memdup() by g_memdup2()
>>    hw/rdma: Replace g_memdup() by g_memdup2()
>>    hw/vfio/pci: Replace g_memdup() by g_memdup2()
>>    hw/virtio: Replace g_memdup() by g_memdup2()
>>    net/colo: Replace g_memdup() by g_memdup2()
>>    ui/clipboard: Replace g_memdup() by g_memdup2()
>>    linux-user: Replace g_memdup() by g_memdup2()
>>    tests/unit: Replace g_memdup() by g_memdup2()
>>    tests/qtest: Replace g_memdup() by g_memdup2()
>>    target/arm: Replace g_memdup() by g_memdup2()
>>    target/ppc: Replace g_memdup() by g_memdup2()
>>    contrib: Replace g_memdup() by g_memdup2()
>>    checkpatch: Do not allow deprecated g_memdup(
> Is it possible to take the reviewed patches 2, 24 and 28
> via qemu-trivial, so the rest can be merged slowly by
> each submaintainer?
> 
> 

Done for these 3 patches.

Thanks,
Laurent
Philippe Mathieu-Daudé May 8, 2024, 9:22 p.m. UTC | #3
On 3/9/21 19:44, Philippe Mathieu-Daudé wrote:
> Per https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538
> 
>    The old API took the size of the memory to duplicate as a guint,
>    whereas most memory functions take memory sizes as a gsize. This
>    made it easy to accidentally pass a gsize to g_memdup(). For large
>    values, that would lead to a silent truncation of the size from 64
>    to 32 bits, and result in a heap area being returned which is
>    significantly smaller than what the caller expects. This can likely
>    be exploited in various modules to cause a heap buffer overflow.
> 
> g_memdup() as been deprecated in GLib 2.68. Since QEMU defines
> GLIB_VERSION_MAX_ALLOWED to GLIB_VERSION_2_56, the deprecation
> is not displayed (on GLib >= 2.68 such available on Fedora 34).
> However the function is still unsafe, so it is better to avoid
> its use.
> 
> This series provides the safely equivalent g_memdup2() wrapper,
> and replace all g_memdup() calls by it.


> Philippe Mathieu-Daudé (28):
>    block/qcow2-bitmap: Replace g_memdup() by g_memdup2()
>    hw/hppa/machine: Replace g_memdup() by g_memdup2()
>    hw/ppc/spapr_pci: Replace g_memdup() by g_memdup2()
>    target/ppc: Replace g_memdup() by g_memdup2()

Thanks, patches 5, 11, 15 & 26 queued to hw-misc tree
adding the comment suggested by Eric on patch 5.
diff mbox

Patch

diff --git a/include/glib-compat.h b/include/glib-compat.h
index 8d01a8c01fb..2b33dea71a8 100644
--- a/include/glib-compat.h
+++ b/include/glib-compat.h
@@ -21,3 +21,3 @@ 
  */
-#define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_56
+#define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_68

@@ -26,3 +26,3 @@ 
  */
-#define GLIB_VERSION_MAX_ALLOWED GLIB_VERSION_2_56
+#define GLIB_VERSION_MAX_ALLOWED GLIB_VERSION_2_68