glib: Replace g_memdup() by g_memdup2()
Subsystems (3)
v3 cover glib: Replace g_memdup() by g_memdup2()
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
Quoted 1 line
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 --
include/glib-compat.h +2 -2
@@ -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
---
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(-)
Signature · 1 line
2.31.1
PATCH v3 1/28 hw/hyperv/vmbus: Remove unused vmbus_load/save_req()
vmbus_save_req() and vmbus_load_req() are not used. Remove them to avoid maintaining dead code.
--- include/hw/hyperv/vmbus.h | 3 -- hw/hyperv/vmbus.c | 59 --------------------------------------- 2 files changed, 62 deletions(-)
include/hw/hyperv/vmbus.h +0 -3
@@ -223,7 +223,4 @@ int vmbus_map_sgl(VMBusChanReq *req, DMADirection dir, struct iovec *iov,void vmbus_unmap_sgl (VMBusChanReq *req,DMADirection dir,struct iovec *iov,unsigned iov_cnt,size_t accessed); -void vmbus_save_req (QEMUFile *f,VMBusChanReq *req); -void *vmbus_load_req (QEMUFile *f,VMBusDevice *dev,uint32_t size); -#endif
hw/hyperv/vmbus.c +0 -59
65 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v3 2/28 glib-compat: Introduce g_memdup2() wrapper
In reply to Philippe Mathieu-DaudéWhen experimenting raising GLIB_VERSION_MIN_REQUIRED to 2.68 (Fedora 34 provides GLib 2.68.1) we get:
hw/virtio/virtio-crypto.c:245:24: error: 'g_memdup' is deprecated: Use 'g_memdup2' instead [-Werror,-Wdeprecated-declarations]
...
g_memdup() has been updated by g_memdup2() to fix eventual security issues (size argument is 32-bit and could be truncated / wrapping). GLib recommends to copy their static inline version of g_memdup2():
https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538
Our glib-compat.h provides a comment explaining how to deal with these deprecated declarations (see commit e71e8cc0355 "glib: enforce the minimum required version and warn about old APIs").
Following this comment suggestion, implement the g_memdup2_qemu() wrapper to g_memdup2(), and use the safer equivalent inlined when we are using pre-2.68 GLib.
--- include/glib-compat.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
include/glib-compat.h +37 -0
@@ -68,6 +68,43 @@* without generating warnings. */ +/* +* g_memdup2_qemu: +* @mem: (nullable): the memory to copy. +* @byte_size: the number of bytes to copy. +* +* Allocates @byte_size bytes of memory, and copies @byte_size bytes into it +* from @mem. If @mem is %NULL it returns %NULL. +* +* This replaces g_memdup(), which was prone to integer overflows when +* converting the argument from a #gsize to a #guint. +* +* This static inline version is a backport of the new public API from +* GLib 2.68, kept internal to GLib for backport to older stable releases. +* See https://gitlab.gnome.org/GNOME/glib/-/issues/2319. +* +* Returns: (nullable): a pointer to the newly-allocated copy of the memory, +* or %NULL if @mem is %NULL. +*/ +static inline gpointer g_memdup2_qemu (gconstpointer mem,gsize byte_size) +{ +#if GLIB_CHECK_VERSION (2 ,68 ,0 ) +return g_memdup2 (mem, byte_size); +#else +gpointer new_mem; + +if (mem && byte_size !=0 ) { + new_mem =g_malloc (byte_size); +memcpy (new_mem, mem, byte_size); + }else { + new_mem =NULL ; + } + +return new_mem; +#endif +} +#define g_memdup2 (m, s) g_memdup2_qemu(m, s) +#if defined(G_OS_UNIX )/* * Note: The fallback implementation is not MT-safe, and it returns a copy of
Signature · 1 line
2.31.1
Re: [PATCH v3 02/28] glib-compat: Introduce g_memdup2() wrapper
Quoted 19 lines · Philippe Mathieu-Daudé
When experimenting raising
GLIB_VERSION_MIN_REQUIREDto 2.68 (Fedora 34 provides GLib 2.68.1) we get:
hw/virtio/virtio-crypto.c:245:24: error: 'g_memdup' is deprecated: Use 'g_memdup2' instead [-Werror,-Wdeprecated-declarations]
...
g_memdup()has been updated byg_memdup2()to fix eventual security issues (size argument is 32-bit and could be truncated / wrapping). GLib recommends to copy their static inline version ofg_memdup2():
https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538Our
glib-compat.hprovides a comment explaining how to deal with these deprecated declarations (see commite71e8cc0355"glib: enforce the minimum required version and warn about old APIs").Following this comment suggestion, implement the
g_memdup2_qemu()wrapper tog_memdup2(), and use the safer equivalent inlined when we are using pre-2.68 GLib.
Quoted 5 lines
Reported-by Eric Blake <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/glib-compat.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH v3 02/28] glib-compat: Introduce g_memdup2() wrapper
In reply to Philippe Mathieu-DaudéQuoted 60 lines · Philippe Mathieu-Daudé
When experimenting raising
GLIB_VERSION_MIN_REQUIREDto 2.68 (Fedora 34 provides GLib 2.68.1) we get:
hw/virtio/virtio-crypto.c:245:24: error: 'g_memdup' is deprecated: Use 'g_memdup2' instead [-Werror,-Wdeprecated-declarations]
...
g_memdup()has been updated byg_memdup2()to fix eventual security issues (size argument is 32-bit and could be truncated / wrapping). GLib recommends to copy their static inline version ofg_memdup2():
https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538Our
glib-compat.hprovides a comment explaining how to deal with these deprecated declarations (see commite71e8cc0355"glib: enforce the minimum required version and warn about old APIs").Following this comment suggestion, implement the
g_memdup2_qemu()wrapper tog_memdup2(), and use the safer equivalent inlined when we are using pre-2.68 GLib.Reported-by Eric Blake <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/glib-compat.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)include/glib-compat.h +27
-0@@ -68,6 +68,43 @@* without generating warnings. */ +/* +* g_memdup2_qemu: +* @mem: (nullable): the memory to copy. +* @byte_size: the number of bytes to copy. +* +* Allocates @byte_size bytes of memory, and copies @byte_size bytes into it +* from @mem. If @mem is %NULL it returns %NULL. +* +* This replaces g_memdup(), which was prone to integer overflows when +* converting the argument from a #gsize to a #guint. +* +* This static inline version is a backport of the new public API from +* GLib 2.68, kept internal to GLib for backport to older stable releases. +* See https://gitlab.gnome.org/GNOME/glib/-/issues/2319. +* +* Returns: (nullable): a pointer to the newly-allocated copy of the memory, +* or %NULL if @mem is %NULL. +*/ +static inline gpointer g_memdup2_qemu (gconstpointer mem,gsize byte_size) +{ +#if GLIB_CHECK_VERSION (2 ,68 ,0 ) +return g_memdup2 (mem, byte_size); +#else +gpointer new_mem; + +if (mem && byte_size !=0 ) { + new_mem =g_malloc (byte_size);
include/glib-compat.h line 107
Quoted 10 lines
include/glib-compat.h +10
-0+ memcpy(new_mem, mem, byte_size); + }else { + new_mem =NULL ; + } + +return new_mem; +#endif +} +#define g_memdup2 (m, s) g_memdup2_qemu(m, s) +
As per our style wouldn't it make sense to just call it qemu_memdup(m, s)?
Signature · 1 line
Alex Bennée
Re: [PATCH v3 02/28] glib-compat: Introduce g_memdup2() wrapper
Quoted 67 lines · Alex Bennée
Quoted 65 lines · Philippe Mathieu-Daudé
When experimenting raising
GLIB_VERSION_MIN_REQUIREDto 2.68 (Fedora 34 provides GLib 2.68.1) we get:
hw/virtio/virtio-crypto.c:245:24: error: 'g_memdup' is deprecated: Use 'g_memdup2' instead [-Werror,-Wdeprecated-declarations]
...
g_memdup()has been updated byg_memdup2()to fix eventual security issues (size argument is 32-bit and could be truncated / wrapping). GLib recommends to copy their static inline version ofg_memdup2():
https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538Our
glib-compat.hprovides a comment explaining how to deal with these deprecated declarations (see commite71e8cc0355"glib: enforce the minimum required version and warn about old APIs").Following this comment suggestion, implement the
g_memdup2_qemu()wrapper tog_memdup2(), and use the safer equivalent inlined when we are using pre-2.68 GLib.Reported-by Eric Blake <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/glib-compat.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)include/glib-compat.h +32
-0@@ -68,6 +68,43 @@* without generating warnings. */ +/* +* g_memdup2_qemu: +* @mem: (nullable): the memory to copy. +* @byte_size: the number of bytes to copy. +* +* Allocates @byte_size bytes of memory, and copies @byte_size bytes into it +* from @mem. If @mem is %NULL it returns %NULL. +* +* This replaces g_memdup(), which was prone to integer overflows when +* converting the argument from a #gsize to a #guint. +* +* This static inline version is a backport of the new public API from +* GLib 2.68, kept internal to GLib for backport to older stable releases. +* See https://gitlab.gnome.org/GNOME/glib/-/issues/2319. +* +* Returns: (nullable): a pointer to the newly-allocated copy of the memory, +* or %NULL if @mem is %NULL. +*/ +static inline gpointer g_memdup2_qemu (gconstpointer mem,gsize byte_size) +{ +#if GLIB_CHECK_VERSION (2 ,68 ,0 ) +return g_memdup2 (mem, byte_size); +#else +gpointer new_mem; + +if (mem && byte_size !=0 ) { + new_mem =g_malloc (byte_size); +memcpy (new_mem, mem, byte_size); + }else { + new_mem =NULL ; + } +
Quoted 8 lines
include/glib-compat.h line 107
Quoted 5 lines
include/glib-compat.h +5
-0+return new_mem; +#endif +} +#define g_memdup2 (m, s) g_memdup2_qemu(m, s) +As per our style wouldn't it make sense to just call it
qemu_memdup(m, s)?
I followed the documentation in include/glib-compat.h:
/*
* Note that because of the GLIB_VERSION_MAX_ALLOWED constant above,
allowing
* use of functions from newer GLib via this compat header needs a little
* trickery to prevent warnings being emitted.
*
* Consider a function from newer glib-X.Y that we want to use
*
* int g_foo(const char *wibble)
*
* We must define a static inline function with the same signature that does
* what we need, but with a "_qemu" suffix e.g.
*
* static inline void g_foo_qemu(const char *wibble)
* {
* #if GLIB_CHECK_VERSION(X, Y, 0)
* g_foo(wibble)
* #else
* g_something_equivalent_in_older_glib(wibble);
* #endif
* }
*
* The #pragma at the top of this file turns off -Wdeprecated-declarations,
* ensuring this wrapper function impl doesn't trigger the compiler warning
* about using too new glib APIs. Finally we can do
*
* #define g_foo(a) g_foo_qemu(a)
*
* So now the code elsewhere in QEMU, which *does* have the
* -Wdeprecated-declarations warning active, can call g_foo(...) as normal,
* without generating warnings.
*/
which is how g_unix_get_passwd_entry_qemu() is implemented.
Should we reword the documentation first?
Re: [PATCH v3 02/28] glib-compat: Introduce g_memdup2() wrapper
Quoted 18 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Alex Bennée
Quoted 15 lines · Philippe Mathieu-Daudé
When experimenting raising
GLIB_VERSION_MIN_REQUIREDto 2.68 (Fedora 34 provides GLib 2.68.1) we get:
hw/virtio/virtio-crypto.c:245:24: error: 'g_memdup' is deprecated: Use 'g_memdup2' instead [-Werror,-Wdeprecated-declarations]
...
g_memdup()has been updated byg_memdup2()to fix eventual security issues (size argument is 32-bit and could be truncated / wrapping). GLib recommends to copy their static inline version ofg_memdup2():
https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538Our
glib-compat.hprovides a comment explaining how to deal with these deprecated declarations (see commite71e8cc0355"glib: enforce the minimum required version and warn about old APIs").
<snip>
Quoted 41 lines
Quoted 5 lines
include/glib-compat.h line 107
Quoted 2 lines
include/glib-compat.h +2
-0+#define g_memdup2 (m, s) g_memdup2_qemu(m, s) +As per our style wouldn't it make sense to just call it
qemu_memdup(m, s)?I followed the documentation in
include/glib-compat.h:/*
* Note that because of theGLIB_VERSION_MAX_ALLOWEDconstant above,
allowing
* use of functions from newer GLib via this compat header needs a little
* trickery to prevent warnings being emitted.
*
* Consider a function from newer glib-X.Y that we want to use
*
* intg_foo(const char *wibble)
*
* We must define a static inline function with the same signature that does
* what we need, but with a "_qemu" suffix e.g.
*
* static inline voidg_foo_qemu(const char *wibble)
* {
* #ifGLIB_CHECK_VERSION(X, Y, 0)
*g_foo(wibble)
* #else
*g_something_equivalent_in_older_glib(wibble);
* #endif
* }
*
* The #pragma at the top of this file turns off -Wdeprecated-declarations,
* ensuring this wrapper function impl doesn't trigger the compiler warning
* about using too new glib APIs. Finally we can do
*
* #defineg_foo(a)g_foo_qemu(a)
*
* So now the code elsewhere in QEMU, which *does* have the
* -Wdeprecated-declarations warning active, can callg_foo(...)as normal,
* without generating warnings.
*/
Quoted 1 line
which is how
g_unix_get_passwd_entry_qemu()is implemented.
Yet later we have qemu_g_test_slow following the style guide. Also I'm confused by the usage of g_unix_get_passwd_entry_qemu because the only place I see it in qga/commands-posix-ssh.c right before it does:
#define g_unix_get_passwd_entry_qemu(username, err) \
test_get_passwd_entry(username, err)
although I think that only hold when the file is built with QGA_BUILD_UNIT_TEST.
Quoted 1 line
Should we reword the documentation first?
The original wording in glib-compat.h was added by Daniel in 2018 but the commit that added the password function comments:
Since the fallback version is still unsafe, I would rather keep the _qemu postfix, to make sure it's not being misused by mistake. When/if necessary, we can implement a safer fallback and drop the _qemu suffix.
So if we are going to make a distinction between a qemu prefix and suffix we should agree that and add it to the style document.
Signature · 1 line
Alex Bennée
Re: [PATCH v3 02/28] glib-compat: Introduce g_memdup2() wrapper
Alex,
I've added this patch to my trivial patches branch, do you want I drop it?
Thanks,
Laurent
Quoted 82 lines · Alex Bennée
Quoted 18 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Alex Bennée
Quoted 15 lines · Philippe Mathieu-Daudé
When experimenting raising
GLIB_VERSION_MIN_REQUIREDto 2.68 (Fedora 34 provides GLib 2.68.1) we get:
hw/virtio/virtio-crypto.c:245:24: error: 'g_memdup' is deprecated: Use 'g_memdup2' instead [-Werror,-Wdeprecated-declarations]
...
g_memdup()has been updated byg_memdup2()to fix eventual security issues (size argument is 32-bit and could be truncated / wrapping). GLib recommends to copy their static inline version ofg_memdup2():
https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538Our
glib-compat.hprovides a comment explaining how to deal with these deprecated declarations (see commite71e8cc0355"glib: enforce the minimum required version and warn about old APIs").<snip>
Quoted 39 lines
Quoted 4 lines
include/glib-compat.h line 107
Quoted 2 lines
include/glib-compat.h +2
-0+#define g_memdup2 (m, s) g_memdup2_qemu(m, s) +As per our style wouldn't it make sense to just call it
qemu_memdup(m, s)?I followed the documentation in
include/glib-compat.h:/*
* Note that because of theGLIB_VERSION_MAX_ALLOWEDconstant above,
allowing
* use of functions from newer GLib via this compat header needs a little
* trickery to prevent warnings being emitted.
*
* Consider a function from newer glib-X.Y that we want to use
*
* intg_foo(const char *wibble)
*
* We must define a static inline function with the same signature that does
* what we need, but with a "_qemu" suffix e.g.
*
* static inline voidg_foo_qemu(const char *wibble)
* {
* #ifGLIB_CHECK_VERSION(X, Y, 0)
*g_foo(wibble)
* #else
*g_something_equivalent_in_older_glib(wibble);
* #endif
* }
*
* The #pragma at the top of this file turns off -Wdeprecated-declarations,
* ensuring this wrapper function impl doesn't trigger the compiler warning
* about using too new glib APIs. Finally we can do
*
* #defineg_foo(a)g_foo_qemu(a)
*
* So now the code elsewhere in QEMU, which *does* have the
* -Wdeprecated-declarations warning active, can callg_foo(...)as normal,
* without generating warnings.
*/Quoted 1 line
which is how
g_unix_get_passwd_entry_qemu()is implemented.Yet later we have
qemu_g_test_slowfollowing the style guide. Also I'm confused by the usage ofg_unix_get_passwd_entry_qemubecause the only place I see it inqga/commands-posix-ssh.cright before it does:#define
g_unix_get_passwd_entry_qemu(username, err)\
test_get_passwd_entry(username, err)although I think that only hold when the file is built with
QGA_BUILD_UNIT_TEST.Quoted 1 line
Should we reword the documentation first?
The original wording in
glib-compat.hwas added by Daniel in 2018 but the commit that added the password function comments:Since the fallback version is still unsafe, I would rather keep the
_qemupostfix, to make sure it's not being misused by mistake. When/if necessary, we can implement a safer fallback and drop the_qemusuffix.So if we are going to make a distinction between a qemu prefix and suffix we should agree that and add it to the style document.
Re: [PATCH v3 02/28] glib-compat: Introduce g_memdup2() wrapper
Quoted 4 lines · Laurent Vivier
Alex,
I've added this patch to my trivial patches branch, do you want I drop it?
No - the patch itself is fine. I would like to fix up the consistency later if we can agree on what it should be.
Signature · 1 line
Alex Bennée
Re: [PATCH v3 02/28] glib-compat: Introduce g_memdup2() wrapper
In reply to Alex BennéeQuoted 63 lines · Alex Bennée
Quoted 18 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Alex Bennée
Quoted 15 lines · Philippe Mathieu-Daudé
When experimenting raising
GLIB_VERSION_MIN_REQUIREDto 2.68 (Fedora 34 provides GLib 2.68.1) we get:
hw/virtio/virtio-crypto.c:245:24: error: 'g_memdup' is deprecated: Use 'g_memdup2' instead [-Werror,-Wdeprecated-declarations]
...
g_memdup()has been updated byg_memdup2()to fix eventual security issues (size argument is 32-bit and could be truncated / wrapping). GLib recommends to copy their static inline version ofg_memdup2():
https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538Our
glib-compat.hprovides a comment explaining how to deal with these deprecated declarations (see commite71e8cc0355"glib: enforce the minimum required version and warn about old APIs").<snip>
Quoted 41 lines
Quoted 5 lines
include/glib-compat.h line 107
Quoted 2 lines
include/glib-compat.h +2
-0+#define g_memdup2 (m, s) g_memdup2_qemu(m, s) +As per our style wouldn't it make sense to just call it
qemu_memdup(m, s)?I followed the documentation in
include/glib-compat.h:/*
* Note that because of theGLIB_VERSION_MAX_ALLOWEDconstant above,
allowing
* use of functions from newer GLib via this compat header needs a little
* trickery to prevent warnings being emitted.
*
* Consider a function from newer glib-X.Y that we want to use
*
* intg_foo(const char *wibble)
*
* We must define a static inline function with the same signature that does
* what we need, but with a "_qemu" suffix e.g.
*
* static inline voidg_foo_qemu(const char *wibble)
* {
* #ifGLIB_CHECK_VERSION(X, Y, 0)
*g_foo(wibble)
* #else
*g_something_equivalent_in_older_glib(wibble);
* #endif
* }
*
* The #pragma at the top of this file turns off -Wdeprecated-declarations,
* ensuring this wrapper function impl doesn't trigger the compiler warning
* about using too new glib APIs. Finally we can do
*
* #defineg_foo(a)g_foo_qemu(a)
*
* So now the code elsewhere in QEMU, which *does* have the
* -Wdeprecated-declarations warning active, can callg_foo(...)as normal,
* without generating warnings.
*/
Quoted 8 lines
Quoted 1 line
which is how
g_unix_get_passwd_entry_qemu()is implemented.Yet later we have
qemu_g_test_slowfollowing the style guide. Also I'm confused by the usage ofg_unix_get_passwd_entry_qemubecause the only place I see it inqga/commands-posix-ssh.cright before it does:#define
g_unix_get_passwd_entry_qemu(username, err)\
test_get_passwd_entry(username, err)
The g_unix_get_passwd_entry method is a bad example as it is not following the guide for transparent replacement.
Quoted 9 lines
although I think that only hold when the file is built with
QGA_BUILD_UNIT_TEST.Quoted 1 line
Should we reword the documentation first?
The original wording in
glib-compat.hwas added by Daniel in 2018 but the commit that added the password function comments:
Quoted 6 lines
Since the fallback version is still unsafe, I would rather keep the
_qemupostfix, to make sure it's not being misused by mistake. When/if necessary, we can implement a safer fallback and drop the_qemusuffix.So if we are going to make a distinction between a qemu prefix and suffix we should agree that and add it to the style document.
The glibcompat.h header should only be used for cases where we are doing transparent replacement such that callers continue using the normal glib API name, and the suffix is a hidden impl detail only seen in the header.
I think in that particular case we should have just had 'qemu_unix_get_passwd_entry' defined in a completely separate place like osdep.c.
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 02/28] glib-compat: Introduce g_memdup2() wrapper
In reply to Alex BennéeQuoted 68 lines · Alex Bennée
Quoted 65 lines · Philippe Mathieu-Daudé
When experimenting raising
GLIB_VERSION_MIN_REQUIREDto 2.68 (Fedora 34 provides GLib 2.68.1) we get:
hw/virtio/virtio-crypto.c:245:24: error: 'g_memdup' is deprecated: Use 'g_memdup2' instead [-Werror,-Wdeprecated-declarations]
...
g_memdup()has been updated byg_memdup2()to fix eventual security issues (size argument is 32-bit and could be truncated / wrapping). GLib recommends to copy their static inline version ofg_memdup2():
https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538Our
glib-compat.hprovides a comment explaining how to deal with these deprecated declarations (see commite71e8cc0355"glib: enforce the minimum required version and warn about old APIs").Following this comment suggestion, implement the
g_memdup2_qemu()wrapper tog_memdup2(), and use the safer equivalent inlined when we are using pre-2.68 GLib.Reported-by Eric Blake <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/glib-compat.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)include/glib-compat.h +32
-0@@ -68,6 +68,43 @@* without generating warnings. */ +/* +* g_memdup2_qemu: +* @mem: (nullable): the memory to copy. +* @byte_size: the number of bytes to copy. +* +* Allocates @byte_size bytes of memory, and copies @byte_size bytes into it +* from @mem. If @mem is %NULL it returns %NULL. +* +* This replaces g_memdup(), which was prone to integer overflows when +* converting the argument from a #gsize to a #guint. +* +* This static inline version is a backport of the new public API from +* GLib 2.68, kept internal to GLib for backport to older stable releases. +* See https://gitlab.gnome.org/GNOME/glib/-/issues/2319. +* +* Returns: (nullable): a pointer to the newly-allocated copy of the memory, +* or %NULL if @mem is %NULL. +*/ +static inline gpointer g_memdup2_qemu (gconstpointer mem,gsize byte_size) +{ +#if GLIB_CHECK_VERSION (2 ,68 ,0 ) +return g_memdup2 (mem, byte_size); +#else +gpointer new_mem; + +if (mem && byte_size !=0 ) { + new_mem =g_malloc (byte_size); +memcpy (new_mem, mem, byte_size); + }else { + new_mem =NULL ; + } +
Quoted 8 lines
include/glib-compat.h line 107
Quoted 5 lines
include/glib-compat.h +5
-0+return new_mem; +#endif +} +#define g_memdup2 (m, s) g_memdup2_qemu(m, s) +As per our style wouldn't it make sense to just call it
qemu_memdup(m, s)?
Not in this case. We use suffix as we don't want people calling this directly with the suffix.
In the glibcompat.h header we're attempting to transparently/secretly replace/wrap standard glib APIs. All the callers should remain using the plain glib API name, never call the method with the suffix at all. This lets us delete the wrapper later and not have to update any callers. The suffix is basically just a hack of the impl we use for transparent replacement.
A method with a 'qemu_' prefix by constrast is something that callers are explicitly expected to call directly.
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v3 02/28] glib-compat: Introduce g_memdup2() wrapper
Quoted 81 lines · Daniel P. Berrangé
Quoted 68 lines · Alex Bennée
Quoted 65 lines · Philippe Mathieu-Daudé
When experimenting raising
GLIB_VERSION_MIN_REQUIREDto 2.68 (Fedora 34 provides GLib 2.68.1) we get:
hw/virtio/virtio-crypto.c:245:24: error: 'g_memdup' is deprecated: Use 'g_memdup2' instead [-Werror,-Wdeprecated-declarations]
...
g_memdup()has been updated byg_memdup2()to fix eventual security issues (size argument is 32-bit and could be truncated / wrapping). GLib recommends to copy their static inline version ofg_memdup2():
https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538Our
glib-compat.hprovides a comment explaining how to deal with these deprecated declarations (see commite71e8cc0355"glib: enforce the minimum required version and warn about old APIs").Following this comment suggestion, implement the
g_memdup2_qemu()wrapper tog_memdup2(), and use the safer equivalent inlined when we are using pre-2.68 GLib.Reported-by Eric Blake <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/glib-compat.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)include/glib-compat.h +32
-0@@ -68,6 +68,43 @@* without generating warnings. */ +/* +* g_memdup2_qemu: +* @mem: (nullable): the memory to copy. +* @byte_size: the number of bytes to copy. +* +* Allocates @byte_size bytes of memory, and copies @byte_size bytes into it +* from @mem. If @mem is %NULL it returns %NULL. +* +* This replaces g_memdup(), which was prone to integer overflows when +* converting the argument from a #gsize to a #guint. +* +* This static inline version is a backport of the new public API from +* GLib 2.68, kept internal to GLib for backport to older stable releases. +* See https://gitlab.gnome.org/GNOME/glib/-/issues/2319. +* +* Returns: (nullable): a pointer to the newly-allocated copy of the memory, +* or %NULL if @mem is %NULL. +*/ +static inline gpointer g_memdup2_qemu (gconstpointer mem,gsize byte_size) +{ +#if GLIB_CHECK_VERSION (2 ,68 ,0 ) +return g_memdup2 (mem, byte_size); +#else +gpointer new_mem; + +if (mem && byte_size !=0 ) { + new_mem =g_malloc (byte_size); +memcpy (new_mem, mem, byte_size); + }else { + new_mem =NULL ; + } +Quoted 8 lines
include/glib-compat.h line 107
Quoted 5 lines
include/glib-compat.h +5
-0+return new_mem; +#endif +} +#define g_memdup2 (m, s) g_memdup2_qemu(m, s) +As per our style wouldn't it make sense to just call it
qemu_memdup(m, s)?Not in this case. We use suffix as we don't want people calling this directly with the suffix.
Quoted 6 lines
In the
glibcompat.hheader we're attempting to transparently/secretly replace/wrap standard glib APIs. All the callers should remain using the plain glib API name, never call the method with the suffix at all. This lets us delete the wrapper later and not have to update any callers. The suffix is basically just a hack of the impl we use for transparent replacement.
Right - at the risk of bike shedding names maybe we should choose a suffix the better reflects the purpose like _alt or _internal rather than overloading qemu?
We already document _locked for example.
Quoted 6 lines
A method with a '
qemu_' prefix by constrast is something that callers are explicitly expected to call directly.Regards,
Daniel
Signature · 1 line
Alex Bennée
Re: [PATCH v3 02/28] glib-compat: Introduce g_memdup2() wrapper
Quoted 91 lines · Alex Bennée
Quoted 81 lines · Daniel P. Berrangé
Quoted 68 lines · Alex Bennée
Quoted 65 lines · Philippe Mathieu-Daudé
When experimenting raising
GLIB_VERSION_MIN_REQUIREDto 2.68 (Fedora 34 provides GLib 2.68.1) we get:
hw/virtio/virtio-crypto.c:245:24: error: 'g_memdup' is deprecated: Use 'g_memdup2' instead [-Werror,-Wdeprecated-declarations]
...
g_memdup()has been updated byg_memdup2()to fix eventual security issues (size argument is 32-bit and could be truncated / wrapping). GLib recommends to copy their static inline version ofg_memdup2():
https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538Our
glib-compat.hprovides a comment explaining how to deal with these deprecated declarations (see commite71e8cc0355"glib: enforce the minimum required version and warn about old APIs").Following this comment suggestion, implement the
g_memdup2_qemu()wrapper tog_memdup2(), and use the safer equivalent inlined when we are using pre-2.68 GLib.Reported-by Eric Blake <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/glib-compat.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)include/glib-compat.h +32
-0@@ -68,6 +68,43 @@* without generating warnings. */ +/* +* g_memdup2_qemu: +* @mem: (nullable): the memory to copy. +* @byte_size: the number of bytes to copy. +* +* Allocates @byte_size bytes of memory, and copies @byte_size bytes into it +* from @mem. If @mem is %NULL it returns %NULL. +* +* This replaces g_memdup(), which was prone to integer overflows when +* converting the argument from a #gsize to a #guint. +* +* This static inline version is a backport of the new public API from +* GLib 2.68, kept internal to GLib for backport to older stable releases. +* See https://gitlab.gnome.org/GNOME/glib/-/issues/2319. +* +* Returns: (nullable): a pointer to the newly-allocated copy of the memory, +* or %NULL if @mem is %NULL. +*/ +static inline gpointer g_memdup2_qemu (gconstpointer mem,gsize byte_size) +{ +#if GLIB_CHECK_VERSION (2 ,68 ,0 ) +return g_memdup2 (mem, byte_size); +#else +gpointer new_mem; + +if (mem && byte_size !=0 ) { + new_mem =g_malloc (byte_size); +memcpy (new_mem, mem, byte_size); + }else { + new_mem =NULL ; + } +Quoted 8 lines
include/glib-compat.h line 107
Quoted 5 lines
include/glib-compat.h +5
-0+return new_mem; +#endif +} +#define g_memdup2 (m, s) g_memdup2_qemu(m, s) +As per our style wouldn't it make sense to just call it
qemu_memdup(m, s)?Not in this case. We use suffix as we don't want people calling this directly with the suffix.
Quoted 6 lines
In the
glibcompat.hheader we're attempting to transparently/secretly replace/wrap standard glib APIs. All the callers should remain using the plain glib API name, never call the method with the suffix at all. This lets us delete the wrapper later and not have to update any callers. The suffix is basically just a hack of the impl we use for transparent replacement.
Quoted 3 lines
Right - at the risk of bike shedding names maybe we should choose a suffix the better reflects the purpose like
_altor_internalrather than overloading qemu?
Sure, I'm fine with that.
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Laurent Vivier
AppliedRe: [PATCH v3 02/28] glib-compat: Introduce g_memdup2() wrapper
In reply to Philippe Mathieu-DaudéLe 03/09/2021 à 19:44, Philippe Mathieu-Daudé a écrit :
Quoted 64 lines
When experimenting raising
GLIB_VERSION_MIN_REQUIREDto 2.68 (Fedora 34 provides GLib 2.68.1) we get:
hw/virtio/virtio-crypto.c:245:24: error: 'g_memdup' is deprecated: Use 'g_memdup2' instead [-Werror,-Wdeprecated-declarations]
...
g_memdup()has been updated byg_memdup2()to fix eventual security issues (size argument is 32-bit and could be truncated / wrapping). GLib recommends to copy their static inline version ofg_memdup2():
https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538Our
glib-compat.hprovides a comment explaining how to deal with these deprecated declarations (see commite71e8cc0355"glib: enforce the minimum required version and warn about old APIs").Following this comment suggestion, implement the
g_memdup2_qemu()wrapper tog_memdup2(), and use the safer equivalent inlined when we are using pre-2.68 GLib.Reported-by Eric Blake <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/glib-compat.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)include/glib-compat.h +30
-0@@ -68,6 +68,43 @@* without generating warnings. */ +/* +* g_memdup2_qemu: +* @mem: (nullable): the memory to copy. +* @byte_size: the number of bytes to copy. +* +* Allocates @byte_size bytes of memory, and copies @byte_size bytes into it +* from @mem. If @mem is %NULL it returns %NULL. +* +* This replaces g_memdup(), which was prone to integer overflows when +* converting the argument from a #gsize to a #guint. +* +* This static inline version is a backport of the new public API from +* GLib 2.68, kept internal to GLib for backport to older stable releases. +* See https://gitlab.gnome.org/GNOME/glib/-/issues/2319. +* +* Returns: (nullable): a pointer to the newly-allocated copy of the memory, +* or %NULL if @mem is %NULL. +*/ +static inline gpointer g_memdup2_qemu (gconstpointer mem,gsize byte_size) +{ +#if GLIB_CHECK_VERSION (2 ,68 ,0 ) +return g_memdup2 (mem, byte_size); +#else +gpointer new_mem; + +if (mem && byte_size !=0 ) { + new_mem =g_malloc (byte_size); +memcpy (new_mem, mem, byte_size); + }else { + new_mem =NULL ;
include/glib-compat.h line 110
Quoted 10 lines
include/glib-compat.h +7
-0+ } + +return new_mem; +#endif +} +#define g_memdup2 (m, s) g_memdup2_qemu(m, s) +#if defined(G_OS_UNIX )/* * Note: The fallback implementation is not MT-safe, and it returns a copy of
Applied to my trivial-patches branch.
Thanks,
Laurent
PATCH v3 3/28 qapi: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- qapi/qapi-clone-visitor.c | 16 ++++++++-------- qapi/qapi-visit-core.c | 6 ++++-- 2 files changed, 12 insertions(+), 10 deletions(-)
qapi/qapi-clone-visitor.c +8 -8
@@ -37,7 +37,7 @@ static bool qapi_clone_start_struct(Visitor *v, const char *name, void **obj,return true; } - *obj =g_memdup (*obj, size); + *obj =g_memdup2 (*obj, size); qcv->depth++;return true; }
@@ -65,8 +65,8 @@ static GenericList *qapi_clone_next_list(Visitor *v, GenericList *tail,QapiCloneVisitor *qcv =to_qcv (v);assert (qcv->depth); -/* Unshare the tail of the list cloned by g_memdup() */ - tail->next =g_memdup (tail->next, size); +/* Unshare the tail of the list cloned by g_memdup2() */ + tail->next =g_memdup2 (tail->next, size);return tail->next; }
@@ -83,7 +83,7 @@ static bool qapi_clone_type_int64(Visitor *v, const char *name, int64_t *obj,QapiCloneVisitor *qcv =to_qcv (v);assert (qcv->depth); -/* Value was already cloned by g_memdup() */ +/* Value was already cloned by g_memdup2() */ return true; }
@@ -93,7 +93,7 @@ static bool qapi_clone_type_uint64(Visitor *v, const char *name,QapiCloneVisitor *qcv =to_qcv (v);assert (qcv->depth); -/* Value was already cloned by g_memdup() */ +/* Value was already cloned by g_memdup2() */ return true; }
@@ -103,7 +103,7 @@ static bool qapi_clone_type_bool(Visitor *v, const char *name, bool *obj,QapiCloneVisitor *qcv =to_qcv (v);assert (qcv->depth); -/* Value was already cloned by g_memdup() */ +/* Value was already cloned by g_memdup2() */ return true; }
@@ -114,7 +114,7 @@ static bool qapi_clone_type_str(Visitor *v, const char *name, char **obj,assert (qcv->depth);/* -* Pointer was already cloned by g_memdup; create fresh copy. +* Pointer was already cloned by g_memdup2; create fresh copy. * Note that as long as qobject-output-visitor accepts NULL instead of * "", then we must do likewise. However, we want to obey the * input visitor semantics of never producing NULL when the empty
@@ -130,7 +130,7 @@ static bool qapi_clone_type_number(Visitor *v, const char *name, double *obj,QapiCloneVisitor *qcv =to_qcv (v);assert (qcv->depth); -/* Value was already cloned by g_memdup() */ +/* Value was already cloned by g_memdup2() */ return true; }
qapi/qapi-visit-core.c +4 -2
@@ -413,8 +413,10 @@ bool visit_type_enum(Visitor *v, const char *name, int *obj,case VISITOR_OUTPUT :return output_type_enum (v, name, obj, lookup, errp);case VISITOR_CLONE : -/* nothing further to do, scalar value was already copied by -* g_memdup() during visit_start_*() */ +/* +* nothing further to do, scalar value was already copied by +* g_memdup2() during visit_start_*() +*/ return true;case VISITOR_DEALLOC :/* nothing to deallocate for a scalar */
Signature · 1 line
2.31.1
Re: [PATCH v3 03/28] qapi: Replace g_memdup() by g_memdup2()
Quoted 10 lines · Philippe Mathieu-Daudé
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.
Quoted 7 lines
Replace
g_memdup()by the saferg_memdup2()wrapper.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- qapi/qapi-clone-visitor.c | 16 ++++++++-------- qapi/qapi-visit-core.c | 6 ++++-- 2 files changed, 12 insertions(+), 10 deletions(-)
qapi/qapi-clone-visitor.c static bool qapi_clone_start_struct(Visitor *v, const char *name, void **obj, · line 40
Quoted 11 lines
qapi/qapi-clone-visitor.c +1
-1@@ -37,7 +37,7 @@ static bool qapi_clone_start_struct(Visitor *v, const char *name, void **obj,return true; } - *obj =g_memdup (*obj, size); + *obj =g_memdup2 (*obj, size);
I did not audit whether any callers were previously vulnerable, although I suspect most (if not all) callers were from the generated QAPI code passing in the results of sizeof, and none of our QAPI types are 4G large to cause overflow.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH v3 4/28 accel/tcg: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- accel/tcg/cputlb.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
accel/tcg/cputlb.c +4 -4
@@ -826,7 +826,7 @@ void tlb_flush_range_by_mmuidx(CPUState *cpu, target_ulong addr,tlb_flush_range_by_mmuidx_async_0 (cpu, d); }else {/* Otherwise allocate a structure, freed by the worker. */ -TLBFlushRangeData *p =g_memdup (&d,sizeof (d)); +TLBFlushRangeData *p =g_memdup2 (&d,sizeof (d));async_run_on_cpu (cpu, tlb_flush_range_by_mmuidx_async_1,RUN_ON_CPU_HOST_PTR (p)); }
@@ -868,7 +868,7 @@ void tlb_flush_range_by_mmuidx_all_cpus(CPUState *src_cpu,/* Allocate a separate data block for each destination cpu. */ CPU_FOREACH (dst_cpu) {if (dst_cpu != src_cpu) { -TLBFlushRangeData *p =g_memdup (&d,sizeof (d)); +TLBFlushRangeData *p =g_memdup2 (&d,sizeof (d));async_run_on_cpu (dst_cpu, tlb_flush_range_by_mmuidx_async_1,RUN_ON_CPU_HOST_PTR (p));
@@ -918,13 +918,13 @@ void tlb_flush_range_by_mmuidx_all_cpus_synced(CPUState *src_cpu,/* Allocate a separate data block for each destination cpu. */ CPU_FOREACH (dst_cpu) {if (dst_cpu != src_cpu) { - p =g_memdup (&d,sizeof (d)); + p =g_memdup2 (&d,sizeof (d));async_run_on_cpu (dst_cpu, tlb_flush_range_by_mmuidx_async_1,RUN_ON_CPU_HOST_PTR (p)); } } - p =g_memdup (&d,sizeof (d)); + p =g_memdup2 (&d,sizeof (d));async_safe_run_on_cpu (src_cpu, tlb_flush_range_by_mmuidx_async_1,RUN_ON_CPU_HOST_PTR (p)); }
Signature · 1 line
2.31.1
PATCH v3 5/28 block/qcow2-bitmap: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- block/qcow2-bitmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/qcow2-bitmap.c +1 -1
@@ -1599,7 +1599,7 @@ bool qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs, name); goto fail; } - tb =g_memdup (&bm->table,sizeof (bm->table)); + tb =g_memdup2 (&bm->table,sizeof (bm->table)); bm->table.offset =0 ; bm->table.size =0 ;QSIMPLEQ_INSERT_TAIL (&drop_tables, tb, entry);
Signature · 1 line
2.31.1
Re: [PATCH v3 05/28] block/qcow2-bitmap: Replace g_memdup() by g_memdup2()
Quoted 22 lines · Philippe Mathieu-Daudé
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.Replace
g_memdup()by the saferg_memdup2()wrapper.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- block/qcow2-bitmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)block/qcow2-bitmap.c +0
-0
block/qcow2-bitmap.c bool qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs, · line 1602
Quoted 5 lines
block/qcow2-bitmap.c +1
-1@@ -1599,7 +1599,7 @@ bool qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs, name); goto fail; } - tb =g_memdup (&bm->table,sizeof (bm->table)); + tb =g_memdup2 (&bm->table,sizeof (bm->table));
Trivially safe. It might be worth a comment in the various commit messages for which patches are trivially safe (because the argument was directly from sizeof), and which would require a larger audit of callers to see if we had any (unlikely) bug (such as patch 3/28).
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH v3 05/28] block/qcow2-bitmap: Replace g_memdup() by g_memdup2()
Quoted 18 lines · Eric Blake
Quoted 17 lines · Philippe Mathieu-Daudé
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.Replace
g_memdup()by the saferg_memdup2()wrapper.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- block/qcow2-bitmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines
block/qcow2-bitmap.c bool qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs, · line 1602
Quoted 10 lines
block/qcow2-bitmap.c +1
-1@@ -1599,7 +1599,7 @@ bool qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs, name); goto fail; } - tb =g_memdup (&bm->table,sizeof (bm->table)); + tb =g_memdup2 (&bm->table,sizeof (bm->table));Trivially safe. It might be worth a comment in the various commit messages for which patches are trivially safe (because the argument was directly from sizeof), and which would require a larger audit of callers to see if we had any (unlikely) bug (such as patch 3/28).
Yes, will do.
Quoted 1 line
Reviewed-by Eric Blake <[email protected]>
Thanks!
PATCH v3 6/28 softmmu: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- softmmu/memory.c | 2 +- softmmu/vl.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
softmmu/memory.c +1 -1
@@ -1140,7 +1140,7 @@ static char *memory_region_escape_name(const char *name) bytes +=memory_region_need_escape (*p) ?4 :1 ; }if (bytes == p - name) { -return g_memdup (name, bytes +1 ); +return g_memdup2 (name, bytes +1 ); } escaped =g_malloc (bytes +1 );
softmmu/vl.c +1 -1
@@ -1154,7 +1154,7 @@ static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp) }if (nonempty_str (str)) { size =strlen (str);/* NUL terminator NOT included in fw_cfg blob */ - buf =g_memdup (str, size); + buf =g_memdup2 (str, size); }else if (nonempty_str (gen_id)) {if (!fw_cfg_add_from_generator (fw_cfg, name, gen_id, errp)) {return -1 ;
Signature · 1 line
2.31.1
PATCH v3 7/28 hw/9pfs: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- hw/9pfs/9p-synth.c | 2 +- hw/9pfs/9p.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
hw/9pfs/9p-synth.c +1 -1
@@ -497,7 +497,7 @@ static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path, out:/* Copy the node pointer to fid */ g_free (target->data); - target->data =g_memdup (&node,sizeof (void *)); + target->data =g_memdup2 (&node,sizeof (void *)); target->size =sizeof (void *);return 0 ; }
hw/9pfs/9p.c +1 -1
@@ -202,7 +202,7 @@ void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src) {v9fs_path_free (dst); dst->size = src->size; - dst->data =g_memdup (src->data, src->size); + dst->data =g_memdup2 (src->data, src->size); } int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
Signature · 1 line
2.31.1
Re: [PATCH v3 07/28] hw/9pfs: Replace g_memdup() by g_memdup2()
Quoted 25 lines · On Freitag, 3. September 2021 19:44:49 CEST Philippe Mathieu-Daudé wrote:
Per
https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-n
ow/5538The 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.Replace
g_memdup()by the saferg_memdup2()wrapper.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/9pfs/9p-synth.c | 2 +- hw/9pfs/9p.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)hw/9pfs/9p-synth.c +0
-0
hw/9pfs/9p-synth.c static int synth_name_to_path(FsContext *ctx, V9fsPath · line 503
Quoted 8 lines
hw/9pfs/9p-synth.c +1
-1@@ -497,7 +497,7 @@ static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path, out:/* Copy the node pointer to fid */ g_free (target->data); - target->data =g_memdup (&node,sizeof (void *)); + target->data =g_memdup2 (&node,sizeof (void *)); target->size =sizeof (void *);return 0 ; }
That's Ok, trivial change.
hw/9pfs/9p.c void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src) · line 208
Quoted 13 lines
hw/9pfs/9p.c +1
-1@@ -202,7 +202,7 @@ void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src) {v9fs_path_free (dst); dst->size = src->size; - dst->data =g_memdup (src->data, src->size); + dst->data =g_memdup2 (src->data, src->size); } int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
src->size is actually just 16 bit (fsdev/file-op-9p.h):
struct V9fsPath {
uint16_t size;
char *data;
};Should (still) be Ok as well as V9fsPath is about file system pathes which are currently limited to 4k (PATH_MAX).
Best regards,
Christian Schoenebeck
PATCH v3 8/28 hw/acpi: Avoid truncating acpi_data_len() to 32-bit
In reply to Philippe Mathieu-Daudéacpi_data_len() returns an unsigned type, which might be bigger than 32-bit (although it is unlikely such value is returned). Hold the returned value in an 'unsigned' type to avoid unlikely size truncation.
--- hw/arm/virt-acpi-build.c | 2 +- hw/i386/acpi-build.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
hw/arm/virt-acpi-build.c +1 -1
@@ -885,7 +885,7 @@ void virt_acpi_build(VirtMachineState *vms, AcpiBuildTables *tables)static void acpi_ram_update (MemoryRegion *mr,GArray *data) { -uint32_t size =acpi_data_len (data); +unsigned size =acpi_data_len (data);/* Make sure RAM size is correct - in case it got changed * e.g. by migration */
hw/i386/acpi-build.c +2 -2
@@ -2660,7 +2660,7 @@ void acpi_build(AcpiBuildTables *tables, MachineState *machine)static void acpi_ram_update (MemoryRegion *mr,GArray *data) { -uint32_t size =acpi_data_len (data); +unsigned size =acpi_data_len (data);/* Make sure RAM size is correct - in case it got changed e.g. by migration */ memory_region_ram_resize (mr, size, &error_abort);
@@ -2783,7 +2783,7 @@ void acpi_setup(void)* Though RSDP is small, its contents isn't immutable, so * we'll update it along with the rest of tables on guest access. */ -uint32_t rsdp_size =acpi_data_len (tables.rsdp); +unsigned rsdp_size =acpi_data_len (tables.rsdp); build_state->rsdp =g_memdup (tables.rsdp->data, rsdp_size); fw_cfg_add_file_callback(x86ms->fw_cfg, ACPI_BUILD_RSDP_FILE,
Signature · 1 line
2.31.1
PATCH v3 9/28 hw/acpi: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- hw/acpi/core.c | 3 ++- hw/i386/acpi-build.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-)
hw/acpi/core.c +2 -1
@@ -637,7 +637,8 @@ void acpi_pm1_cnt_init(ACPIREGS *ar, MemoryRegion *parent, suspend[3 ] =1 | ((!disable_s3) <<7 ); suspend[4 ] = s4_val | ((!disable_s4) <<7 ); -fw_cfg_add_file (fw_cfg,"etc/system-states" ,g_memdup (suspend,6 ),6 ); +fw_cfg_add_file (fw_cfg,"etc/system-states" , +g_memdup2 (suspend,6 ),6 ); } }
hw/i386/acpi-build.c +1 -1
@@ -2785,7 +2785,7 @@ void acpi_setup(void)*/ unsigned rsdp_size =acpi_data_len (tables.rsdp); - build_state->rsdp =g_memdup (tables.rsdp->data, rsdp_size); + build_state->rsdp =g_memdup2 (tables.rsdp->data, rsdp_size);fw_cfg_add_file_callback (x86ms->fw_cfg,ACPI_BUILD_RSDP_FILE , acpi_build_update,NULL , build_state, build_state->rsdp, rsdp_size, true);
Signature · 1 line
2.31.1
PATCH v3 10/28 hw/core/machine: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- hw/core/machine.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/core/machine.c +2 -2
@@ -615,8 +615,8 @@ HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine) cpu_item->type =g_strdup (machine->possible_cpus->cpus[i].type); cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count; - cpu_item->props =g_memdup (&machine->possible_cpus->cpus[i].props, -sizeof (*cpu_item->props)); + cpu_item->props =g_memdup2 (&machine->possible_cpus->cpus[i].props, +sizeof (*cpu_item->props)); cpu = machine->possible_cpus->cpus[i].cpu;if (cpu) {
Signature · 1 line
2.31.1
PATCH v3 11/28 hw/hppa/machine: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- hw/hppa/machine.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
hw/hppa/machine.c +4 -4
@@ -101,19 +101,19 @@ static FWCfgState *create_fw_cfg(MachineState *ms) val =cpu_to_le64 (MIN_SEABIOS_HPPA_VERSION );fw_cfg_add_file (fw_cfg,"/etc/firmware-min-version" , -g_memdup (&val,sizeof (val)),sizeof (val)); +g_memdup2 (&val,sizeof (val)),sizeof (val)); val =cpu_to_le64 (HPPA_TLB_ENTRIES );fw_cfg_add_file (fw_cfg,"/etc/cpu/tlb_entries" , -g_memdup (&val,sizeof (val)),sizeof (val)); +g_memdup2 (&val,sizeof (val)),sizeof (val)); val =cpu_to_le64 (HPPA_BTLB_ENTRIES );fw_cfg_add_file (fw_cfg,"/etc/cpu/btlb_entries" , -g_memdup (&val,sizeof (val)),sizeof (val)); +g_memdup2 (&val,sizeof (val)),sizeof (val)); val =cpu_to_le64 (HPA_POWER_BUTTON );fw_cfg_add_file (fw_cfg,"/etc/power-button-addr" , -g_memdup (&val,sizeof (val)),sizeof (val)); +g_memdup2 (&val,sizeof (val)),sizeof (val));fw_cfg_add_i16 (fw_cfg,FW_CFG_BOOT_DEVICE , ms->boot_order[0 ]);qemu_register_boot_set (fw_cfg_boot_set, fw_cfg);
Signature · 1 line
2.31.1
PATCH v3 12/28 hw/i386/multiboot: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- hw/i386/multiboot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i386/multiboot.c +1 -1
@@ -387,7 +387,7 @@ int load_multiboot(FWCfgState *fw_cfg,mb_debug (" mb_mods_count = %d" , mbs.mb_mods_count);/* save bootinfo off the stack */ - mb_bootinfo_data =g_memdup (bootinfo,sizeof (bootinfo)); + mb_bootinfo_data =g_memdup2 (bootinfo,sizeof (bootinfo));/* Pass variables to option rom */ fw_cfg_add_i32 (fw_cfg,FW_CFG_KERNEL_ENTRY , mh_entry_addr);
Signature · 1 line
2.31.1
PATCH v3 13/28 hw/net/eepro100: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- hw/net/eepro100.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/eepro100.c +1 -1
@@ -1872,7 +1872,7 @@ static void e100_nic_realize(PCIDevice *pci_dev, Error **errp)qemu_register_reset (nic_reset, s); - s->vmstate =g_memdup (&vmstate_eepro100,sizeof (vmstate_eepro100)); + s->vmstate =g_memdup2 (&vmstate_eepro100,sizeof (vmstate_eepro100)); s->vmstate->name =qemu_get_queue (s->nic)->model;vmstate_register (VMSTATE_IF (&pci_dev->qdev),VMSTATE_INSTANCE_ID_ANY , s->vmstate, s);
Signature · 1 line
2.31.1
PATCH v3 14/28 hw/nvram/fw_cfg: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- hw/nvram/fw_cfg.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
hw/nvram/fw_cfg.c +5 -4
@@ -205,7 +205,8 @@ static void fw_cfg_bootsplash(FWCfgState *s)/* use little endian format */ bst_le16 =cpu_to_le16 (bst_val);fw_cfg_add_file (s,"etc/boot-menu-wait" , -g_memdup (&bst_le16,sizeof bst_le16),sizeof bst_le16); +g_memdup2 (&bst_le16,sizeof bst_le16), +sizeof bst_le16); }/* insert splash file if user configurated */
@@ -260,7 +261,7 @@ static void fw_cfg_reboot(FWCfgState *s) } rt_le32 =cpu_to_le32 (rt_val); -fw_cfg_add_file (s,"etc/boot-fail-wait" ,g_memdup (&rt_le32,4 ),4 ); +fw_cfg_add_file (s,"etc/boot-fail-wait" ,g_memdup2 (&rt_le32,4 ),4 ); }static void fw_cfg_write (FWCfgState *s,uint8_t value)
@@ -755,7 +756,7 @@ void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)size_t sz =strlen (value) +1 ;trace_fw_cfg_add_string (key,trace_key_name (key), value); -fw_cfg_add_bytes (s, key,g_memdup (value, sz), sz); +fw_cfg_add_bytes (s, key,g_memdup2 (value, sz), sz); }void fw_cfg_modify_string (FWCfgState *s,uint16_t key,const char *value)
@@ -763,7 +764,7 @@ void fw_cfg_modify_string(FWCfgState *s, uint16_t key, const char *value)size_t sz =strlen (value) +1 ;char *old; - old =fw_cfg_modify_bytes_read (s, key,g_memdup (value, sz), sz); + old =fw_cfg_modify_bytes_read (s, key,g_memdup2 (value, sz), sz);g_free (old); }
Signature · 1 line
2.31.1
PATCH v3 15/28 hw/scsi/mptsas: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- hw/scsi/mptsas.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
hw/scsi/mptsas.c +3 -2
@@ -449,7 +449,8 @@ static void mptsas_process_scsi_task_mgmt(MPTSASState *s, MPIMsgSCSITaskMgmt *re }else {MPTSASCancelNotifier *notifier; - reply_async =g_memdup (&reply,sizeof (MPIMsgSCSITaskMgmtReply)); + reply_async =g_memdup2 (&reply, +sizeof (MPIMsgSCSITaskMgmtReply)); reply_async->IOCLogInfo =INT_MAX ; count =1 ;
@@ -476,7 +477,7 @@ static void mptsas_process_scsi_task_mgmt(MPTSASState *s, MPIMsgSCSITaskMgmt *re goto out; } - reply_async =g_memdup (&reply,sizeof (MPIMsgSCSITaskMgmtReply)); + reply_async =g_memdup2 (&reply,sizeof (MPIMsgSCSITaskMgmtReply)); reply_async->IOCLogInfo =INT_MAX ; count =0 ;
Signature · 1 line
2.31.1
PATCH v3 16/28 hw/ppc/spapr_pci: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- hw/ppc/spapr_pci.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
hw/ppc/spapr_pci.c +3 -4
@@ -2201,10 +2201,9 @@ static int spapr_pci_post_load(void *opaque, int version_id)int i;for (i =0 ; i < sphb->msi_devs_num; ++i) { - key =g_memdup (&sphb->msi_devs[i].key, -sizeof (sphb->msi_devs[i].key)); - value =g_memdup (&sphb->msi_devs[i].value, -sizeof (sphb->msi_devs[i].value)); + key =g_memdup2 (&sphb->msi_devs[i].key,sizeof (sphb->msi_devs[i].key)); + value =g_memdup2 (&sphb->msi_devs[i].value, +sizeof (sphb->msi_devs[i].value));g_hash_table_insert (sphb->msi, key, value); }g_free (sphb->msi_devs);
Signature · 1 line
2.31.1
Re: [PATCH v3 16/28] hw/ppc/spapr_pci: Replace g_memdup() by g_memdup2()
Quoted 10 lines · Philippe Mathieu-Daudé
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.
Quoted 3 lines
Replace
g_memdup()by the saferg_memdup2()wrapper.Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 22 lines
--- hw/ppc/spapr_pci.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)hw/ppc/spapr_pci.c +3
-4@@ -2201,10 +2201,9 @@ static int spapr_pci_post_load(void *opaque, int version_id)int i;for (i =0 ; i < sphb->msi_devs_num; ++i) { - key =g_memdup (&sphb->msi_devs[i].key, -sizeof (sphb->msi_devs[i].key)); - value =g_memdup (&sphb->msi_devs[i].value, -sizeof (sphb->msi_devs[i].value)); + key =g_memdup2 (&sphb->msi_devs[i].key,sizeof (sphb->msi_devs[i].key)); + value =g_memdup2 (&sphb->msi_devs[i].value, +sizeof (sphb->msi_devs[i].value));g_hash_table_insert (sphb->msi, key, value); }g_free (sphb->msi_devs);
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
PATCH v3 17/28 hw/rdma: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- hw/rdma/rdma_utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/rdma/rdma_utils.c +1 -1
@@ -71,7 +71,7 @@ void rdma_protected_gqueue_append_int64(RdmaProtectedGQueue *list, int64_t value) {qemu_mutex_lock (&list->lock); -g_queue_push_tail (list->list,g_memdup (&value,sizeof (value))); +g_queue_push_tail (list->list,g_memdup2 (&value,sizeof (value)));qemu_mutex_unlock (&list->lock); }
Signature · 1 line
2.31.1
PATCH v3 18/28 hw/vfio/pci: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- hw/vfio/pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/vfio/pci.c +1 -1
@@ -2040,7 +2040,7 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)* physical device, we cache the config space to avoid overwriting * the original config space when we parse the extended capabilities. */ - config =g_memdup (pdev->config, vdev->config_size); + config =g_memdup2 (pdev->config, vdev->config_size);/* * Extended capabilities are chained with each pointing to the next, so we
Signature · 1 line
2.31.1
RFC PATCH v3 19/28 hw/virtio: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
---
Should we check in_num/out_num in range?
--- hw/net/virtio-net.c | 3 ++- hw/virtio/virtio-crypto.c | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-)
hw/net/virtio-net.c +2 -1
@@ -1449,7 +1449,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) } iov_cnt = elem->out_num; - iov2 = iov =g_memdup (elem->out_sg,sizeof (struct iovec ) * elem->out_num); + iov2 = iov =g_memdup2 (elem->out_sg, +sizeof (struct iovec ) * elem->out_num); s =iov_to_buf (iov, iov_cnt,0 , &ctrl,sizeof (ctrl));iov_discard_front (&iov, &iov_cnt,sizeof (ctrl));if (s !=sizeof (ctrl)) {
hw/virtio/virtio-crypto.c +3 -3
@@ -242,7 +242,7 @@ static void virtio_crypto_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) } out_num = elem->out_num; - out_iov_copy =g_memdup (elem->out_sg,sizeof (out_iov[0 ]) * out_num); + out_iov_copy =g_memdup2 (elem->out_sg,sizeof (out_iov[0 ]) * out_num); out_iov = out_iov_copy; in_num = elem->in_num;
@@ -605,11 +605,11 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request) } out_num = elem->out_num; - out_iov_copy =g_memdup (elem->out_sg,sizeof (out_iov[0 ]) * out_num); + out_iov_copy =g_memdup2 (elem->out_sg,sizeof (out_iov[0 ]) * out_num); out_iov = out_iov_copy; in_num = elem->in_num; - in_iov_copy =g_memdup (elem->in_sg,sizeof (in_iov[0 ]) * in_num); + in_iov_copy =g_memdup2 (elem->in_sg,sizeof (in_iov[0 ]) * in_num); in_iov = in_iov_copy; if (unlikely(iov_to_buf(out_iov, out_num, 0, &req, sizeof(req))
Signature · 1 line
2.31.1
Re: [RFC PATCH v3 19/28] hw/virtio: Replace g_memdup() by g_memdup2()
Quoted 11 lines · Philippe Mathieu-Daudé
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.
Quoted 5 lines
Replace
g_memdup()by the saferg_memdup2()wrapper.Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
Should we check in_num/out_num in range?
I'd say it is not needed to check: virtqueue_pop fills them by iterating through the descriptor chain so the range is restricted to [0, 1024].
Quoted 50 lines
--- hw/net/virtio-net.c | 3 ++- hw/virtio/virtio-crypto.c | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-)hw/net/virtio-net.c +2
-1@@ -1449,7 +1449,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) } iov_cnt = elem->out_num; - iov2 = iov =g_memdup (elem->out_sg,sizeof (struct iovec ) * elem->out_num); + iov2 = iov =g_memdup2 (elem->out_sg, +sizeof (struct iovec ) * elem->out_num); s =iov_to_buf (iov, iov_cnt,0 , &ctrl,sizeof (ctrl));iov_discard_front (&iov, &iov_cnt,sizeof (ctrl));if (s !=sizeof (ctrl)) {hw/virtio/virtio-crypto.c +3
-4@@ -242,7 +242,7 @@ static void virtio_crypto_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) } out_num = elem->out_num; - out_iov_copy =g_memdup (elem->out_sg,sizeof (out_iov[0 ]) * out_num); + out_iov_copy =g_memdup2 (elem->out_sg,sizeof (out_iov[0 ]) * out_num); out_iov = out_iov_copy; in_num = elem->in_num;@@ -605,11 +605,11 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request) } out_num = elem->out_num; - out_iov_copy =g_memdup (elem->out_sg,sizeof (out_iov[0 ]) * out_num); + out_iov_copy =g_memdup2 (elem->out_sg,sizeof (out_iov[0 ]) * out_num); out_iov = out_iov_copy; in_num = elem->in_num; - in_iov_copy =g_memdup (elem->in_sg,sizeof (in_iov[0 ]) * in_num); + in_iov_copy =g_memdup2 (elem->in_sg,sizeof (in_iov[0 ]) * in_num); in_iov = in_iov_copy; if (unlikely(iov_to_buf(out_iov, out_num, 0, &req, sizeof(req)) --2.31.1
PATCH v3 20/28 net/colo: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
packet_new() is called from packet_enqueue() with size being 32-bit (of type SocketReadState::packet_len).
Replace g_memdup() by the safer g_memdup2() wrapper.
--- net/colo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/colo.c +2 -2
@@ -159,7 +159,7 @@ Packet *packet_new(const void *data, int size, int vnet_hdr_len) {Packet *pkt =g_slice_new0 (Packet); - pkt->data =g_memdup (data, size); + pkt->data =g_memdup2 (data, size); pkt->size = size; pkt->creation_ms =qemu_clock_get_ms (QEMU_CLOCK_HOST ); pkt->vnet_hdr_len = vnet_hdr_len;
@@ -214,7 +214,7 @@ Connection *connection_get(GHashTable *connection_track_table,Connection *conn =g_hash_table_lookup (connection_track_table, key);if (conn ==NULL ) { -ConnectionKey *new_key =g_memdup (key,sizeof (*key)); +ConnectionKey *new_key =g_memdup2 (key,sizeof (*key)); conn =connection_new (key);
Signature · 1 line
2.31.1
RFC PATCH v3 21/28 ui/clipboard: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
---
TODO: audit qemu_clipboard_set_data() calls
--- ui/clipboard.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
ui/clipboard.c +1 -1
@@ -123,7 +123,7 @@ void qemu_clipboard_set_data(QemuClipboardPeer *peer, }g_free (info->types[type].data); - info->types[type].data =g_memdup (data, size); + info->types[type].data =g_memdup2 (data, size); info->types[type].size = size; info->types[type].available = true;
Signature · 1 line
2.31.1
RFC PATCH v3 22/28 linux-user: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
---do_open_by_handle_at() doesn't check:
size + sizeof(struct file_handle) < 4GiB
--- linux-user/syscall.c | 2 +- linux-user/uaccess.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
linux-user/syscall.c +1 -1
@@ -7665,7 +7665,7 @@ static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,return -TARGET_EFAULT ; } - fh =g_memdup (target_fh, total_size); + fh =g_memdup2 (target_fh, total_size); fh->handle_bytes = size; fh->handle_type =tswap32 (target_fh->handle_type);
linux-user/uaccess.c +1 -1
@@ -15,7 +15,7 @@ void *lock_user(int type, abi_ulong guest_addr, ssize_t len, bool copy) host_addr =g2h_untagged (guest_addr);#ifdef DEBUG_REMAP if (copy) { - host_addr =g_memdup (host_addr, len); + host_addr =g_memdup2 (host_addr, len); }else { host_addr =g_malloc0 (len); }
Signature · 1 line
2.31.1
PATCH v3 23/28 tests/unit: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- tests/unit/ptimer-test.c | 22 +++++++++++----------- tests/unit/test-iov.c | 26 +++++++++++++------------- 2 files changed, 24 insertions(+), 24 deletions(-)
tests/unit/ptimer-test.c +11 -11
@@ -798,64 +798,64 @@ static void add_ptimer_tests(uint8_t policy)g_test_add_data_func_full ( tmp =g_strdup_printf ("/ptimer/set_count policy=%s" , policy_name), -g_memdup (&policy,1 ), check_set_count, g_free); +g_memdup2 (&policy,1 ), check_set_count, g_free);g_free (tmp);g_test_add_data_func_full ( tmp =g_strdup_printf ("/ptimer/set_limit policy=%s" , policy_name), -g_memdup (&policy,1 ), check_set_limit, g_free); +g_memdup2 (&policy,1 ), check_set_limit, g_free);g_free (tmp);g_test_add_data_func_full ( tmp =g_strdup_printf ("/ptimer/oneshot policy=%s" , policy_name), -g_memdup (&policy,1 ), check_oneshot, g_free); +g_memdup2 (&policy,1 ), check_oneshot, g_free);g_free (tmp);g_test_add_data_func_full ( tmp =g_strdup_printf ("/ptimer/periodic policy=%s" , policy_name), -g_memdup (&policy,1 ), check_periodic, g_free); +g_memdup2 (&policy,1 ), check_periodic, g_free);g_free (tmp);g_test_add_data_func_full ( tmp =g_strdup_printf ("/ptimer/on_the_fly_mode_change policy=%s" , policy_name), -g_memdup (&policy,1 ), check_on_the_fly_mode_change, g_free); +g_memdup2 (&policy,1 ), check_on_the_fly_mode_change, g_free);g_free (tmp);g_test_add_data_func_full ( tmp =g_strdup_printf ("/ptimer/on_the_fly_period_change policy=%s" , policy_name), -g_memdup (&policy,1 ), check_on_the_fly_period_change, g_free); +g_memdup2 (&policy,1 ), check_on_the_fly_period_change, g_free);g_free (tmp);g_test_add_data_func_full ( tmp =g_strdup_printf ("/ptimer/on_the_fly_freq_change policy=%s" , policy_name), -g_memdup (&policy,1 ), check_on_the_fly_freq_change, g_free); +g_memdup2 (&policy,1 ), check_on_the_fly_freq_change, g_free);g_free (tmp);g_test_add_data_func_full ( tmp =g_strdup_printf ("/ptimer/run_with_period_0 policy=%s" , policy_name), -g_memdup (&policy,1 ), check_run_with_period_0, g_free); +g_memdup2 (&policy,1 ), check_run_with_period_0, g_free);g_free (tmp);g_test_add_data_func_full ( tmp =g_strdup_printf ("/ptimer/run_with_delta_0 policy=%s" , policy_name), -g_memdup (&policy,1 ), check_run_with_delta_0, g_free); +g_memdup2 (&policy,1 ), check_run_with_delta_0, g_free);g_free (tmp);g_test_add_data_func_full ( tmp =g_strdup_printf ("/ptimer/periodic_with_load_0 policy=%s" , policy_name), -g_memdup (&policy,1 ), check_periodic_with_load_0, g_free); +g_memdup2 (&policy,1 ), check_periodic_with_load_0, g_free);g_free (tmp);g_test_add_data_func_full ( tmp =g_strdup_printf ("/ptimer/oneshot_with_load_0 policy=%s" , policy_name), -g_memdup (&policy,1 ), check_oneshot_with_load_0, g_free); +g_memdup2 (&policy,1 ), check_oneshot_with_load_0, g_free);g_free (tmp); }
tests/unit/test-iov.c +13 -13
@@ -173,7 +173,7 @@ static void test_io(void) }iov_from_buf (iov, niov,0 , buf, sz); - siov =g_memdup (iov,sizeof (*iov) * niov); + siov =g_memdup2 (iov,sizeof (*iov) * niov);if (socketpair (PF_UNIX ,SOCK_STREAM ,0 , sv) <0 ) {perror ("socketpair" );
@@ -350,7 +350,7 @@ static void test_discard_front_undo(void)/* Discard zero bytes */ iov_random (&iov, &iov_cnt); - iov_orig =g_memdup (iov,sizeof (iov[0 ]) * iov_cnt); + iov_orig =g_memdup2 (iov,sizeof (iov[0 ]) * iov_cnt); iov_tmp = iov; iov_cnt_tmp = iov_cnt;iov_discard_front_undoable (&iov_tmp, &iov_cnt_tmp,0 , &undo);
@@ -361,7 +361,7 @@ static void test_discard_front_undo(void)/* Discard more bytes than vector size */ iov_random (&iov, &iov_cnt); - iov_orig =g_memdup (iov,sizeof (iov[0 ]) * iov_cnt); + iov_orig =g_memdup2 (iov,sizeof (iov[0 ]) * iov_cnt); iov_tmp = iov; iov_cnt_tmp = iov_cnt; size =iov_size (iov, iov_cnt);
@@ -373,7 +373,7 @@ static void test_discard_front_undo(void)/* Discard entire vector */ iov_random (&iov, &iov_cnt); - iov_orig =g_memdup (iov,sizeof (iov[0 ]) * iov_cnt); + iov_orig =g_memdup2 (iov,sizeof (iov[0 ]) * iov_cnt); iov_tmp = iov; iov_cnt_tmp = iov_cnt; size =iov_size (iov, iov_cnt);
@@ -385,7 +385,7 @@ static void test_discard_front_undo(void)/* Discard within first element */ iov_random (&iov, &iov_cnt); - iov_orig =g_memdup (iov,sizeof (iov[0 ]) * iov_cnt); + iov_orig =g_memdup2 (iov,sizeof (iov[0 ]) * iov_cnt); iov_tmp = iov; iov_cnt_tmp = iov_cnt; size =g_test_rand_int_range (1 , iov->iov_len);
@@ -397,7 +397,7 @@ static void test_discard_front_undo(void)/* Discard entire first element */ iov_random (&iov, &iov_cnt); - iov_orig =g_memdup (iov,sizeof (iov[0 ]) * iov_cnt); + iov_orig =g_memdup2 (iov,sizeof (iov[0 ]) * iov_cnt); iov_tmp = iov; iov_cnt_tmp = iov_cnt;iov_discard_front_undoable (&iov_tmp, &iov_cnt_tmp, iov->iov_len, &undo);
@@ -408,7 +408,7 @@ static void test_discard_front_undo(void)/* Discard within second element */ iov_random (&iov, &iov_cnt); - iov_orig =g_memdup (iov,sizeof (iov[0 ]) * iov_cnt); + iov_orig =g_memdup2 (iov,sizeof (iov[0 ]) * iov_cnt); iov_tmp = iov; iov_cnt_tmp = iov_cnt; size = iov->iov_len +g_test_rand_int_range (1 , iov[1 ].iov_len);
@@ -499,7 +499,7 @@ static void test_discard_back_undo(void)/* Discard zero bytes */ iov_random (&iov, &iov_cnt); - iov_orig =g_memdup (iov,sizeof (iov[0 ]) * iov_cnt); + iov_orig =g_memdup2 (iov,sizeof (iov[0 ]) * iov_cnt); iov_cnt_tmp = iov_cnt;iov_discard_back_undoable (iov, &iov_cnt_tmp,0 , &undo);iov_discard_undo (&undo);
@@ -509,7 +509,7 @@ static void test_discard_back_undo(void)/* Discard more bytes than vector size */ iov_random (&iov, &iov_cnt); - iov_orig =g_memdup (iov,sizeof (iov[0 ]) * iov_cnt); + iov_orig =g_memdup2 (iov,sizeof (iov[0 ]) * iov_cnt); iov_cnt_tmp = iov_cnt; size =iov_size (iov, iov_cnt);iov_discard_back_undoable (iov, &iov_cnt_tmp, size +1 , &undo);
@@ -520,7 +520,7 @@ static void test_discard_back_undo(void)/* Discard entire vector */ iov_random (&iov, &iov_cnt); - iov_orig =g_memdup (iov,sizeof (iov[0 ]) * iov_cnt); + iov_orig =g_memdup2 (iov,sizeof (iov[0 ]) * iov_cnt); iov_cnt_tmp = iov_cnt; size =iov_size (iov, iov_cnt);iov_discard_back_undoable (iov, &iov_cnt_tmp, size, &undo);
@@ -531,7 +531,7 @@ static void test_discard_back_undo(void)/* Discard within last element */ iov_random (&iov, &iov_cnt); - iov_orig =g_memdup (iov,sizeof (iov[0 ]) * iov_cnt); + iov_orig =g_memdup2 (iov,sizeof (iov[0 ]) * iov_cnt); iov_cnt_tmp = iov_cnt; size =g_test_rand_int_range (1 , iov[iov_cnt -1 ].iov_len);iov_discard_back_undoable (iov, &iov_cnt_tmp, size, &undo);
@@ -542,7 +542,7 @@ static void test_discard_back_undo(void)/* Discard entire last element */ iov_random (&iov, &iov_cnt); - iov_orig =g_memdup (iov,sizeof (iov[0 ]) * iov_cnt); + iov_orig =g_memdup2 (iov,sizeof (iov[0 ]) * iov_cnt); iov_cnt_tmp = iov_cnt; size = iov[iov_cnt -1 ].iov_len;iov_discard_back_undoable (iov, &iov_cnt_tmp, size, &undo);
@@ -553,7 +553,7 @@ static void test_discard_back_undo(void)/* Discard within second-to-last element */ iov_random (&iov, &iov_cnt); - iov_orig =g_memdup (iov,sizeof (iov[0 ]) * iov_cnt); + iov_orig =g_memdup2 (iov,sizeof (iov[0 ]) * iov_cnt); iov_cnt_tmp = iov_cnt; size = iov[iov_cnt -1 ].iov_len +g_test_rand_int_range (1 , iov[iov_cnt -2 ].iov_len);
Signature · 1 line
2.31.1
PATCH v3 24/28 tests/qtest: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- tests/qtest/libqos/ahci.c | 6 +++--- tests/qtest/libqos/qgraph.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-)
tests/qtest/libqos/ahci.c +3 -3
@@ -639,8 +639,8 @@ void ahci_exec(AHCIQState *ahci, uint8_t port,AHCIOpts *opts;uint64_t buffer_in; - opts =g_memdup ((opts_in ==NULL ? &default_opts : opts_in), -sizeof (AHCIOpts)); + opts =g_memdup2 ((opts_in ==NULL ? &default_opts : opts_in), +sizeof (AHCIOpts)); buffer_in = opts->buffer;
@@ -860,7 +860,7 @@ AHCICommand *ahci_command_create(uint8_t command_name)g_assert (!props->ncq || props->lba48);/* Defaults and book-keeping */ - cmd->props =g_memdup (props,sizeof (AHCICommandProp)); + cmd->props =g_memdup2 (props,sizeof (AHCICommandProp)); cmd->name = command_name; cmd->xbytes = props->size; cmd->prd_size =4096 ;
tests/qtest/libqos/qgraph.c +1 -1
@@ -93,7 +93,7 @@ static void add_edge(const char *source, const char *dest, edge->type = type; edge->dest =g_strdup (dest); edge->edge_name =g_strdup (opts->edge_name ?: dest); - edge->arg =g_memdup (opts->arg, opts->size_arg); + edge->arg =g_memdup2 (opts->arg, opts->size_arg); edge->before_cmd_line = opts->before_cmd_line ?g_strconcat (" " , opts->before_cmd_line,NULL ) :NULL ;
Signature · 1 line
2.31.1
Re: [PATCH v3 24/28] tests/qtest: Replace g_memdup() by g_memdup2()
Quoted 48 lines · Philippe Mathieu-Daudé
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.Replace
g_memdup()by the saferg_memdup2()wrapper.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- tests/qtest/libqos/ahci.c | 6 +++--- tests/qtest/libqos/qgraph.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-)tests/qtest/libqos/ahci.c +3
-3@@ -639,8 +639,8 @@ void ahci_exec(AHCIQState *ahci, uint8_t port,AHCIOpts *opts;uint64_t buffer_in; - opts =g_memdup ((opts_in ==NULL ? &default_opts : opts_in), -sizeof (AHCIOpts)); + opts =g_memdup2 ((opts_in ==NULL ? &default_opts : opts_in), +sizeof (AHCIOpts)); buffer_in = opts->buffer;@@ -860,7 +860,7 @@ AHCICommand *ahci_command_create(uint8_t command_name)g_assert (!props->ncq || props->lba48);/* Defaults and book-keeping */ - cmd->props =g_memdup (props,sizeof (AHCICommandProp)); + cmd->props =g_memdup2 (props,sizeof (AHCICommandProp)); cmd->name = command_name; cmd->xbytes = props->size; cmd->prd_size =4096 ;tests/qtest/libqos/qgraph.c +0
-0
tests/qtest/libqos/qgraph.c static void add_edge(const char *source, const char *dest, · line 99
Quoted 8 lines
tests/qtest/libqos/qgraph.c +1
-1@@ -93,7 +93,7 @@ static void add_edge(const char *source, const char *dest, edge->type = type; edge->dest =g_strdup (dest); edge->edge_name =g_strdup (opts->edge_name ?: dest); - edge->arg =g_memdup (opts->arg, opts->size_arg); + edge->arg =g_memdup2 (opts->arg, opts->size_arg); edge->before_cmd_line = opts->before_cmd_line ?g_strconcat (" " , opts->before_cmd_line,NULL ) :NULL ;
Laurent Vivier
AppliedRe: [PATCH v3 24/28] tests/qtest: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-DaudéLe 03/09/2021 à 19:45, Philippe Mathieu-Daudé a écrit :
Quoted 48 lines
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.Replace
g_memdup()by the saferg_memdup2()wrapper.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- tests/qtest/libqos/ahci.c | 6 +++--- tests/qtest/libqos/qgraph.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-)tests/qtest/libqos/ahci.c +3
-3@@ -639,8 +639,8 @@ void ahci_exec(AHCIQState *ahci, uint8_t port,AHCIOpts *opts;uint64_t buffer_in; - opts =g_memdup ((opts_in ==NULL ? &default_opts : opts_in), -sizeof (AHCIOpts)); + opts =g_memdup2 ((opts_in ==NULL ? &default_opts : opts_in), +sizeof (AHCIOpts)); buffer_in = opts->buffer;@@ -860,7 +860,7 @@ AHCICommand *ahci_command_create(uint8_t command_name)g_assert (!props->ncq || props->lba48);/* Defaults and book-keeping */ - cmd->props =g_memdup (props,sizeof (AHCICommandProp)); + cmd->props =g_memdup2 (props,sizeof (AHCICommandProp)); cmd->name = command_name; cmd->xbytes = props->size; cmd->prd_size =4096 ;tests/qtest/libqos/qgraph.c +0
-0
tests/qtest/libqos/qgraph.c static void add_edge(const char *source, const char *dest, · line 99
Quoted 8 lines
tests/qtest/libqos/qgraph.c +1
-1@@ -93,7 +93,7 @@ static void add_edge(const char *source, const char *dest, edge->type = type; edge->dest =g_strdup (dest); edge->edge_name =g_strdup (opts->edge_name ?: dest); - edge->arg =g_memdup (opts->arg, opts->size_arg); + edge->arg =g_memdup2 (opts->arg, opts->size_arg); edge->before_cmd_line = opts->before_cmd_line ?g_strconcat (" " , opts->before_cmd_line,NULL ) :NULL ;
Applied to my trivial-patches branch.
Thanks,
Laurent
PATCH v3 25/28 target/arm: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- target/arm/helper.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
target/arm/helper.c +3 -3
@@ -6242,8 +6242,8 @@ static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)/* Create alias before redirection so we dup the right data. */ if (a->new_key) { -ARMCPRegInfo *new_reg =g_memdup (src_reg,sizeof (ARMCPRegInfo)); -uint32_t *new_key =g_memdup (&a->new_key,sizeof (uint32_t )); +ARMCPRegInfo *new_reg =g_memdup2 (src_reg,sizeof (ARMCPRegInfo)); +uint32_t *new_key =g_memdup2 (&a->new_key,sizeof (uint32_t ));bool ok; new_reg->name = a->new_name;
@@ -8818,7 +8818,7 @@ static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,* add a single reginfo struct to the hash table. */ uint32_t *key =g_new (uint32_t,1 ); -ARMCPRegInfo *r2 =g_memdup (r,sizeof (ARMCPRegInfo)); +ARMCPRegInfo *r2 =g_memdup2 (r,sizeof (ARMCPRegInfo));int is64 = (r->type &ARM_CP_64BIT ) ?1 :0 ;int ns = (secstate &ARM_CP_SECSTATE_NS ) ?1 :0 ;
Signature · 1 line
2.31.1
PATCH v3 26/28 target/ppc: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- target/ppc/mmu-hash64.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/ppc/mmu-hash64.c +1 -1
@@ -1122,7 +1122,7 @@ void ppc_hash64_init(PowerPCCPU *cpu)return ; } - cpu->hash64_opts =g_memdup (pcc->hash64_opts,sizeof (*cpu->hash64_opts)); + cpu->hash64_opts =g_memdup2 (pcc->hash64_opts,sizeof (*cpu->hash64_opts)); }void ppc_hash64_finalize (PowerPCCPU *cpu)
Signature · 1 line
2.31.1
Re: [PATCH v3 26/28] target/ppc: Replace g_memdup() by g_memdup2()
Quoted 10 lines · Philippe Mathieu-Daudé
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.
Quoted 3 lines
Replace
g_memdup()by the saferg_memdup2()wrapper.Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 17 lines
--- target/ppc/mmu-hash64.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/ppc/mmu-hash64.c +1
-1@@ -1122,7 +1122,7 @@ void ppc_hash64_init(PowerPCCPU *cpu)return ; } - cpu->hash64_opts =g_memdup (pcc->hash64_opts,sizeof (*cpu->hash64_opts)); + cpu->hash64_opts =g_memdup2 (pcc->hash64_opts,sizeof (*cpu->hash64_opts)); }void ppc_hash64_finalize (PowerPCCPU *cpu)
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
PATCH v3 27/28 contrib: Replace g_memdup() by g_memdup2()
In reply to Philippe Mathieu-Daudé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.
Replace g_memdup() by the safer g_memdup2() wrapper.
--- contrib/plugins/lockstep.c | 2 +- contrib/rdmacm-mux/main.c | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-)
contrib/plugins/lockstep.c +1 -1
@@ -130,7 +130,7 @@ static void report_divergance(ExecState *us, ExecState *them) } } divergence_log =g_slist_prepend (divergence_log, -g_memdup (&divrec,sizeof (divrec))); +g_memdup2 (&divrec,sizeof (divrec)));/* Output short log entry of going out of sync... */ if (verbose || divrec.distance ==1 || diverged) {
contrib/rdmacm-mux/main.c +5 -5
@@ -227,8 +227,8 @@ static RdmaCmMuxErrCode add_fd_ifid_pair(int fd, __be64 gid_ifid)RDMACM_MUX_ERR_CODE_EACCES ; } -g_hash_table_insert (server.umad_agent.gid2fd,g_memdup (&gid_ifid, -sizeof (gid_ifid)),g_memdup (&fd,sizeof (fd))); +g_hash_table_insert (server.umad_agent.gid2fd,g_memdup2 (&gid_ifid, +sizeof (gid_ifid)),g_memdup2 (&fd,sizeof (fd)));pthread_rwlock_unlock (&server.lock);
@@ -250,7 +250,7 @@ static RdmaCmMuxErrCode delete_fd_ifid_pair(int fd, __be64 gid_ifid)return RDMACM_MUX_ERR_CODE_ENOTFOUND ; } -g_hash_table_remove (server.umad_agent.gid2fd,g_memdup (&gid_ifid, +g_hash_table_remove (server.umad_agent.gid2fd,g_memdup2 (&gid_ifid,sizeof (gid_ifid)));pthread_rwlock_unlock (&server.lock);
@@ -267,8 +267,8 @@ static void hash_tbl_save_fd_comm_id_pair(int fd, uint32_t comm_id,pthread_rwlock_wrlock (&server.lock);g_hash_table_insert (server.umad_agent.commid2fd, -g_memdup (&comm_id,sizeof (comm_id)), -g_memdup (&fde,sizeof (fde))); +g_memdup2 (&comm_id,sizeof (comm_id)), +g_memdup2 (&fde,sizeof (fde)));pthread_rwlock_unlock (&server.lock); }
Signature · 1 line
2.31.1
PATCH v3 28/28 checkpatch: Do not allow deprecated g_memdup()
In reply to Philippe Mathieu-Daudég_memdup() is insecure and as been deprecated in GLib 2.68. QEMU provides the safely equivalent g_memdup2() wrapper.
Do not allow more g_memdup() calls in the repository, provide a hint to use g_memdup2().
--- scripts/checkpatch.pl | 5 +++++ 1 file changed, 5 insertions(+)
scripts/checkpatch.pl +5 -0
@@ -2850,6 +2850,11 @@ sub process { WARN("consider using g_path_get_$1() in preference to g_strdup($1())\n" . $herecurr); } +# enforce g_memdup2() over g_memdup() + if ($line =~ /\bg_memdup\s*\(/) { + ERROR("use g_memdup2() instead of unsafe g_memdup()\n" . $herecurr); + } + # recommend qemu_strto* over strto* for numeric conversions if ($line =~ /\b(strto[^kd].*?)\s*\(/) { ERROR("consider using qemu_$1 in preference to $1\n" . $herecurr);
Signature · 1 line
2.31.1
Re: [PATCH v3 28/28] checkpatch: Do not allow deprecated g_memdup()
Quoted 10 lines · Philippe Mathieu-Daudé
g_memdup()is insecure and as been deprecated in GLib 2.68. QEMU provides the safely equivalentg_memdup2()wrapper.Do not allow more
g_memdup()calls in the repository, provide a hint to useg_memdup2().Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- scripts/checkpatch.pl | 5 +++++ 1 file changed, 5 insertions(+)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Laurent Vivier
AppliedRe: [PATCH v3 28/28] checkpatch: Do not allow deprecated g_memdup()
In reply to Philippe Mathieu-DaudéLe 03/09/2021 à 19:45, Philippe Mathieu-Daudé a écrit :
Quoted 18 lines
g_memdup()is insecure and as been deprecated in GLib 2.68. QEMU provides the safely equivalentg_memdup2()wrapper.Do not allow more
g_memdup()calls in the repository, provide a hint to useg_memdup2().Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- scripts/checkpatch.pl | 5 +++++ 1 file changed, 5 insertions(+)scripts/checkpatch.pl +0
-0@@ -2850,6 +2850,11 @@ sub process { WARN("consider using g_path_get_$1() in preference to g_strdup($1())\n" . $herecurr);
scripts/checkpatch.pl sub process { · line 2860
Quoted 10 lines
scripts/checkpatch.pl +5
-0} +# enforce g_memdup2() over g_memdup() + if ($line =~ /\bg_memdup\s*\(/) { + ERROR("use g_memdup2() instead of unsafe g_memdup()\n" . $herecurr); + } + # recommend qemu_strto* over strto* for numeric conversions if ($line =~ /\b(strto[^kd].*?)\s*\(/) { ERROR("consider using qemu_$1 in preference to $1\n" . $herecurr);
Applied to my trivial-patches branch.
Thanks,
Laurent
Hi Laurent,
Quoted 24 lines · Philippe Mathieu-Daudé
This series provides the safely equivalent
g_memdup2()wrapper, and replace allg_memdup()calls by it.Philippe Mathieu-Daudé (28):
hw/hyperv/vmbus: Remove unused vmbus_load/save_req()
glib-compat: Introduceg_memdup2()wrapper
qapi: Replaceg_memdup()byg_memdup2()accel/tcg: Replaceg_memdup()byg_memdup2()block/qcow2-bitmap: Replaceg_memdup()byg_memdup2()
softmmu: Replaceg_memdup()byg_memdup2()hw/9pfs: Replaceg_memdup()byg_memdup2()hw/acpi: Avoid truncatingacpi_data_len()to 32-bit hw/acpi: Replaceg_memdup()byg_memdup2()hw/core/machine: Replaceg_memdup()byg_memdup2()hw/hppa/machine: Replaceg_memdup()byg_memdup2()hw/i386/multiboot: Replaceg_memdup()byg_memdup2()hw/net/eepro100: Replaceg_memdup()byg_memdup2()hw/nvram/fw_cfg: Replaceg_memdup()byg_memdup2()hw/scsi/mptsas: Replaceg_memdup()byg_memdup2()hw/ppc/spapr_pci: Replaceg_memdup()byg_memdup2()hw/rdma: Replaceg_memdup()byg_memdup2()hw/vfio/pci: Replaceg_memdup()byg_memdup2()hw/virtio: Replaceg_memdup()byg_memdup2()net/colo: Replaceg_memdup()byg_memdup2()
Quoted 8 lines
ui/clipboard: Replace
g_memdup()byg_memdup2()
linux-user: Replaceg_memdup()byg_memdup2()
tests/unit: Replaceg_memdup()byg_memdup2()
tests/qtest: Replaceg_memdup()byg_memdup2()
target/arm: Replaceg_memdup()byg_memdup2()
target/ppc: Replaceg_memdup()byg_memdup2()
contrib: Replaceg_memdup()byg_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?
Le 15/12/2021 à 17:54, Philippe Mathieu-Daudé a écrit :
Quoted 36 lines
Hi Laurent,
Quoted 24 lines · Philippe Mathieu-Daudé
This series provides the safely equivalent
g_memdup2()wrapper, and replace allg_memdup()calls by it.Philippe Mathieu-Daudé (28):
hw/hyperv/vmbus: Remove unused vmbus_load/save_req()
glib-compat: Introduceg_memdup2()wrapper
qapi: Replaceg_memdup()byg_memdup2()accel/tcg: Replaceg_memdup()byg_memdup2()block/qcow2-bitmap: Replaceg_memdup()byg_memdup2()
softmmu: Replaceg_memdup()byg_memdup2()hw/9pfs: Replaceg_memdup()byg_memdup2()hw/acpi: Avoid truncatingacpi_data_len()to 32-bit hw/acpi: Replaceg_memdup()byg_memdup2()hw/core/machine: Replaceg_memdup()byg_memdup2()hw/hppa/machine: Replaceg_memdup()byg_memdup2()hw/i386/multiboot: Replaceg_memdup()byg_memdup2()hw/net/eepro100: Replaceg_memdup()byg_memdup2()hw/nvram/fw_cfg: Replaceg_memdup()byg_memdup2()hw/scsi/mptsas: Replaceg_memdup()byg_memdup2()hw/ppc/spapr_pci: Replaceg_memdup()byg_memdup2()hw/rdma: Replaceg_memdup()byg_memdup2()hw/vfio/pci: Replaceg_memdup()byg_memdup2()hw/virtio: Replaceg_memdup()byg_memdup2()net/colo: Replaceg_memdup()byg_memdup2()Quoted 8 lines
ui/clipboard: Replace
g_memdup()byg_memdup2()
linux-user: Replaceg_memdup()byg_memdup2()tests/unit: Replaceg_memdup()byg_memdup2()tests/qtest: Replaceg_memdup()byg_memdup2()target/arm: Replaceg_memdup()byg_memdup2()target/ppc: Replaceg_memdup()byg_memdup2()
contrib: Replaceg_memdup()byg_memdup2()
checkpatch: Do not allow deprecated g_memdup(
Quoted 5 lines
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
Quoted 20 lines · Philippe Mathieu-Daudé
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 definesGLIB_VERSION_MAX_ALLOWEDtoGLIB_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 allg_memdup()calls by it.
Quoted 5 lines
Philippe Mathieu-Daudé (28):
block/qcow2-bitmap: Replaceg_memdup()byg_memdup2()hw/hppa/machine: Replaceg_memdup()byg_memdup2()hw/ppc/spapr_pci: Replaceg_memdup()byg_memdup2()target/ppc: Replaceg_memdup()byg_memdup2()
Thanks, patches 5, 11, 15 & 26 queued to hw-misc tree adding the comment suggested by Eric on patch 5.