glib: Replace g_memdup() by g_memdup2_qemu()
Subsystems (3)
cover glib: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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
Please review,
Phil.
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_qemu() accel/tcg: Replace g_memdup() by g_memdup2_qemu() block/qcow2-bitmap: Replace g_memdup() by g_memdup2_qemu()
softmmu: Replace g_memdup() by g_memdup2_qemu() hw/9pfs: Replace g_memdup() by g_memdup2_qemu() hw/acpi: Avoid truncating acpi_data_len() to 32-bit hw/acpi: Replace g_memdup() by g_memdup2_qemu() hw/core/machine: Replace g_memdup() by g_memdup2_qemu() hw/hppa/machine: Replace g_memdup() by g_memdup2_qemu() hw/i386/multiboot: Replace g_memdup() by g_memdup2_qemu() hw/net/eepro100: Replace g_memdup() by g_memdup2_qemu() hw/nvram/fw_cfg: Replace g_memdup() by g_memdup2_qemu() hw/scsi/mptsas: Replace g_memdup() by g_memdup2_qemu() hw/ppc/spapr_pci: Replace g_memdup() by g_memdup2_qemu() hw/rdma: Replace g_memdup() by g_memdup2_qemu() hw/vfio/pci: Replace g_memdup() by g_memdup2_qemu() RFC hw/virtio: Replace g_memdup() by g_memdup2_qemu() net/colo: Replace g_memdup() by g_memdup2_qemu() RFC ui/clipboard: Replace g_memdup() by g_memdup2_qemu() RFC linux-user: Replace g_memdup() by g_memdup2_qemu() tests/unit: Replace g_memdup() by g_memdup2_qemu() tests/qtest: Replace g_memdup() by g_memdup2_qemu() target/arm: Replace g_memdup() by g_memdup2_qemu() target/ppc: Replace g_memdup() by g_memdup2_qemu()
contrib: Replace g_memdup() by g_memdup2_qemu()
checkpatch: Do not allow deprecated g_memdup()
include/glib-compat.h | 36 ++++++++++++++++++++++ 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 | 8 ++--- hw/rdma/rdma_utils.c | 2 +- hw/scsi/mptsas.c | 5 ++-- hw/vfio/pci.c | 2 +- hw/virtio/virtio-crypto.c | 7 +++-- 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 | 7 +++-- target/ppc/mmu-hash64.c | 3 +- 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, 141 insertions(+), 153 deletions(-)
Signature · 1 line
2.31.1
PATCH 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 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 | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
include/glib-compat.h +36 -0
@@ -68,6 +68,42 @@* 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 +} +#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 02/28] glib-compat: Introduce g_memdup2() wrapper
Quoted 58 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 | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)include/glib-compat.h +25
-0@@ -68,6 +68,42 @@* 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; +
include/glib-compat.h line 105
Quoted 10 lines
include/glib-compat.h +10
-0+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 +}
Close, but you missed the final piece of the puzzle
#define g_memdup2(a) g_memdup2_qemu(a)
Such that in all following patches you can use the normal "g_memdup2" API. This means when we later update min glib, we just delete the compat code here, and the callers don't need updates.
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 02/28] glib-compat: Introduce g_memdup2() wrapper
Quoted 72 lines · Daniel P. Berrangé
Quoted 58 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 | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)include/glib-compat.h +25
-0@@ -68,6 +68,42 @@* 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; +include/glib-compat.h line 105
Quoted 10 lines
include/glib-compat.h +10
-0+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 +}Close, but you missed the final piece of the puzzle
Quoted 7 lines
#define g_memdup2(a) g_memdup2_qemu(a)Such that in all following patches you can use the normal "
g_memdup2" API. This means when we later update min glib, we just delete the compat code here, and the callers don't need updates.
That was most probably already discussed, so sorry my question:
Why we can't just do
#if ! GLIB_CHECK_VERSION(2, 68, 0)
static inline gpointer g_memdup2(gconstpointer mem, gsize byte_size)
{
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
?
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 02/28] glib-compat: Introduce g_memdup2() wrapper
Quoted 18 lines · Vladimir Sementsov-Ogievskiy
That was most probably already discussed, so sorry my question:
Why we can't just do
#if !
GLIB_CHECK_VERSION(2, 68, 0)
static inline gpointerg_memdup2(gconstpointer mem, gsize byte_size){ 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; }
Quoted 3 lines
#endif
?
This doesn't play with GLIB_VERSION_MAX_ALLOWED - any use of g_memdup2 will trigger compile warnings since we're using an API that only exists in a glib version newer than our declared baseline.
The inline wrapper + macro is a trick that lets us backport new features, while avoiding the compile warnings.
This is documented in the include/glib-compat.h file that Philippe is modifying.
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 02/28] glib-compat: Introduce g_memdup2() wrapper
Quoted 23 lines · Daniel P. Berrangé
Quoted 18 lines · Vladimir Sementsov-Ogievskiy
That was most probably already discussed, so sorry my question:
Why we can't just do
#if !
GLIB_CHECK_VERSION(2, 68, 0)
static inline gpointerg_memdup2(gconstpointer mem, gsize byte_size){ 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; }Quoted 3 lines
#endif
?
Quoted 7 lines
This doesn't play with
GLIB_VERSION_MAX_ALLOWED- any use ofg_memdup2will trigger compile warnings since we're using an API that only exists in a glib version newer than our declared baseline.The inline wrapper + macro is a trick that lets us backport new features, while avoiding the compile warnings.
Ah right, with macro, compiler doesn't see g_memdup2() invocations in code. Thanks!
Quoted 7 lines
This is documented in the
include/glib-compat.hfile that Philippe is modifying.Regards,
Daniel
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 02/28] glib-compat: Introduce g_memdup2() wrapper
In reply to Daniel P. BerrangéQuoted 72 lines · Daniel P. Berrangé
Quoted 58 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 | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)include/glib-compat.h +25
-0@@ -68,6 +68,42 @@* 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; +include/glib-compat.h line 105
Quoted 10 lines
include/glib-compat.h +10
-0+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 +}Close, but you missed the final piece of the puzzle
Quoted 1 line
#define g_memdup2(a) g_memdup2_qemu(a)
Doh :/
Quoted 3 lines
Such that in all following patches you can use the normal "
g_memdup2" API. This means when we later update min glib, we just delete the compat code here, and the callers don't need updates.
Painful rebase in perspective...
PATCH 3/28 qapi: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (*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_qemu (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 03/28] qapi: Replace g_memdup() by g_memdup2_qemu()
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.Replace
g_memdup()by the saferg_memdup2_qemu()wrapper.
This and all following patches should directly use "g_memdup2" rather than the wrapper which is supposed to remain "secret" in the glib-compat.h header.
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 03/28] qapi: Replace g_memdup() by g_memdup2_qemu()
Quoted 11 lines · Daniel P. Berrangé
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 5 lines
Quoted 1 line
Replace
g_memdup()by the saferg_memdup2_qemu()wrapper.This and all following patches should directly use "
g_memdup2" rather than the wrapper which is supposed to remain "secret" in theglib-compat.hheader.
Yep, got it, thanks for the quick review!
PATCH 4/28 accel/tcg: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (&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_qemu (&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_qemu (&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_qemu (&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 5/28 block/qcow2-bitmap: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (&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
PATCH 6/28 softmmu: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (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_qemu (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 7/28 hw/9pfs: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (&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_qemu (src->data, src->size); } int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
Signature · 1 line
2.31.1
PATCH 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
Re: [PATCH 08/28] hw/acpi: Avoid truncating acpi_data_len() to 32-bit
Quoted 6 lines · On Fri, 3 Sep 2021 13:06:42 +0200 Philippe Mathieu-Daudé <[email protected]>…
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.Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 40 lines
--- 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,
PATCH 9/28 hw/acpi: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (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_qemu (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
Re: [PATCH 09/28] hw/acpi: Replace g_memdup() by g_memdup2_qemu()
Quoted 10 lines · On Fri, 3 Sep 2021 13:06:43 +0200 Philippe Mathieu-Daudé <[email protected]>…
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_qemu()wrapper.Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 32 lines
--- 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_qemu (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_qemu (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);
PATCH 10/28 hw/core/machine: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (&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 11/28 hw/hppa/machine: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (&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_qemu (&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_qemu (&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_qemu (&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 12/28 hw/i386/multiboot: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (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 13/28 hw/net/eepro100: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (&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 14/28 hw/nvram/fw_cfg: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (&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_qemu (&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_qemu (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_qemu (value, sz), sz);g_free (old); }
Signature · 1 line
2.31.1
PATCH 15/28 hw/scsi/mptsas: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (&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_qemu (&reply,sizeof (MPIMsgSCSITaskMgmtReply)); reply_async->IOCLogInfo =INT_MAX ; count =0 ;
Signature · 1 line
2.31.1
PATCH 16/28 hw/ppc/spapr_pci: Replace g_memdup() by g_memdup2_qemu()
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_qemu() wrapper.
--- hw/ppc/spapr_pci.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
hw/ppc/spapr_pci.c +4 -4
@@ -2201,10 +2201,10 @@ 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_qemu (&sphb->msi_devs[i].key, +sizeof (sphb->msi_devs[i].key)); + value =g_memdup2_qemu (&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 16/28] hw/ppc/spapr_pci: Replace g_memdup() by g_memdup2_qemu()
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_qemu()wrapper.Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 23 lines
--- hw/ppc/spapr_pci.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)hw/ppc/spapr_pci.c +4
-4@@ -2201,10 +2201,10 @@ 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_qemu (&sphb->msi_devs[i].key, +sizeof (sphb->msi_devs[i].key)); + value =g_memdup2_qemu (&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 17/28 hw/rdma: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (&value,sizeof (value)));qemu_mutex_unlock (&list->lock); }
Signature · 1 line
2.31.1
PATCH 18/28 hw/vfio/pci: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (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 19/28 hw/virtio: Replace g_memdup() by g_memdup2_qemu()
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_qemu() wrapper.
---
Should we check in_num/out_num in range?
--- hw/net/virtio-net.c | 3 ++- hw/virtio/virtio-crypto.c | 7 ++++--- 2 files changed, 6 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_qemu (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 +4 -3
@@ -242,7 +242,8 @@ 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_qemu (elem->out_sg, +sizeof (out_iov[0 ]) * out_num); out_iov = out_iov_copy; in_num = elem->in_num;
@@ -605,11 +606,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_qemu (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_qemu (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
PATCH 20/28 net/colo: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (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_qemu (key,sizeof (*key)); conn =connection_new (key);
Signature · 1 line
2.31.1
RFC PATCH 21/28 ui/clipboard: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (data, size); info->types[type].size = size; info->types[type].available = true;
Signature · 1 line
2.31.1
RFC PATCH 22/28 linux-user: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (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_qemu (host_addr, len); }else { host_addr =g_malloc0 (len); }
Signature · 1 line
2.31.1
PATCH 23/28 tests/unit: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (&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_qemu (&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_qemu (&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_qemu (&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_qemu (&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_qemu (&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_qemu (&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_qemu (&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_qemu (&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_qemu (&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_qemu (&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_qemu (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_qemu (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_qemu (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_qemu (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_qemu (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_qemu (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_qemu (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_qemu (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_qemu (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_qemu (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_qemu (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_qemu (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_qemu (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 24/28 tests/qtest: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu ((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_qemu (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_qemu (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
PATCH 25/28 target/arm: Replace g_memdup() by g_memdup2_qemu()
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_qemu() wrapper.
--- target/arm/helper.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
target/arm/helper.c +4 -3
@@ -6242,8 +6242,9 @@ 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_qemu (src_reg, +sizeof (ARMCPRegInfo)); +uint32_t *new_key =g_memdup2_qemu (&a->new_key,sizeof (uint32_t ));bool ok; new_reg->name = a->new_name;
@@ -8818,7 +8819,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_qemu (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 26/28 target/ppc: Replace g_memdup() by g_memdup2_qemu()
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_qemu() wrapper.
--- target/ppc/mmu-hash64.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
target/ppc/mmu-hash64.c +2 -1
@@ -1122,7 +1122,8 @@ void ppc_hash64_init(PowerPCCPU *cpu)return ; } - cpu->hash64_opts =g_memdup (pcc->hash64_opts,sizeof (*cpu->hash64_opts)); + cpu->hash64_opts =g_memdup2_qemu (pcc->hash64_opts, +sizeof (*cpu->hash64_opts)); }void ppc_hash64_finalize (PowerPCCPU *cpu)
Signature · 1 line
2.31.1
Re: [PATCH 26/28] target/ppc: Replace g_memdup() by g_memdup2_qemu()
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_qemu()wrapper.Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 18 lines
--- target/ppc/mmu-hash64.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)target/ppc/mmu-hash64.c +2
-1@@ -1122,7 +1122,8 @@ void ppc_hash64_init(PowerPCCPU *cpu)return ; } - cpu->hash64_opts =g_memdup (pcc->hash64_opts,sizeof (*cpu->hash64_opts)); + cpu->hash64_opts =g_memdup2_qemu (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 27/28 contrib: Replace g_memdup() by g_memdup2_qemu()
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_qemu() 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_qemu (&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_qemu (&gid_ifid, +sizeof (gid_ifid)),g_memdup2_qemu (&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_qemu (&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_qemu (&comm_id,sizeof (comm_id)), +g_memdup2_qemu (&fde,sizeof (fde)));pthread_rwlock_unlock (&server.lock); }
Signature · 1 line
2.31.1
PATCH 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_qemu() wrapper.
Do not allow more g_memdup() calls in the repository, provide a hint to use g_memdup2_qemu().
--- 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_qemu() over g_memdup() + if ($line =~ /\bg_memdup\s*\(/) { + ERROR("use g_memdup2_qemu() 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