From: Fabiano Rosas <farosas@suse.de>
To: qemu-devel@nongnu.org
Cc: Peter Xu <peterx@redhat.com>,
Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>,
Alexandr Moshkov <dtalexundeer@yandex-team.ru>,
"Michael S . Tsirkin" <mst@redhat.com>
Subject: [PATCH v2 7/8] migration: Harden vmstate_handle_alloc
Date: Tue, 18 Aug 2026 15:24:40 -0300 [thread overview]
Message-ID: <20260818182441.404790-8-farosas@suse.de> (raw)
In-Reply-To: <20260818182441.404790-1-farosas@suse.de>
Harden the vmstate_handle_alloc function against overflow of the 64bit
integers it consumes and failure to allocate due to an exceedingly
large request.
Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
migration/vmstate.c | 30 ++++++++++++++++++++++--------
1 file changed, 22 insertions(+), 8 deletions(-)
diff --git a/migration/vmstate.c b/migration/vmstate.c
index 51d02b87e7e..1d028bfe009 100644
--- a/migration/vmstate.c
+++ b/migration/vmstate.c
@@ -135,16 +135,28 @@ static uint64_t vmstate_size(void *opaque, const VMStateField *field)
return size;
}
-static void vmstate_handle_alloc(void *ptr, const VMStateField *field,
- void *opaque)
+static bool vmstate_handle_alloc(void *ptr, const VMStateField *field,
+ uint64_t n, uint64_t size, Error **errp)
{
+ void *p;
+
if (field->flags & VMS_POINTER && field->flags & VMS_ALLOC) {
- uint64_t size = vmstate_size(opaque, field);
- size *= vmstate_n_elems(opaque, field);
- if (size) {
- *(void **)ptr = g_malloc(size);
- }
+ if (size && n) {
+ if (umul64_overflow(size, n, &size)) {
+ error_setg(errp, "%s: field '%s' multiply overflow",
+ __func__, field->name);
+ return false;
+ }
+ p = g_try_malloc(size);
+ if (!p) {
+ error_setg(errp, "%s: Could not allocate memory for field '%s'",
+ __func__, field->name);
+ return false;
+ }
+ *(void **)ptr = p;
+ }
}
+ return true;
}
static bool vmstate_ptr_marker_load(QEMUFile *f, bool *load_field,
@@ -354,7 +366,9 @@ bool vmstate_load_vmsd(QEMUFile *f, const VMStateDescription *vmsd,
uint64_t n_elems = vmstate_n_elems(opaque, field);
uint64_t size = vmstate_size(opaque, field);
- vmstate_handle_alloc(first_elem, field, opaque);
+ if (!vmstate_handle_alloc(first_elem, field, n_elems, size, errp)) {
+ return false;
+ }
if (field->flags & VMS_POINTER) {
first_elem = *(void **)first_elem;
assert(first_elem || !n_elems || !size);
--
2.53.0
next prev parent reply other threads:[~2026-08-18 18:25 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-18 18:24 [PATCH v2 0/8] migration: Remove extra type-checking from vmstate macros Fabiano Rosas
2026-08-18 18:24 ` [PATCH v2 1/8] migration: Remove unused " Fabiano Rosas
2026-08-18 18:24 ` [PATCH v2 2/8] migration: Introduce VMStateStructMember Fabiano Rosas
2026-08-18 19:12 ` Peter Xu
2026-08-18 18:24 ` [PATCH v2 3/8] migration: Remove redundant flags Fabiano Rosas
2026-08-18 18:24 ` [PATCH v2 4/8] migration: Remove duplicate vmstate macros Fabiano Rosas
2026-08-18 18:24 ` [PATCH v2 5/8] migration: Add VMS_NO_STATE flag Fabiano Rosas
2026-08-18 18:24 ` [PATCH v2 6/8] migration: Check more vmstate flags Fabiano Rosas
2026-08-18 18:24 ` Fabiano Rosas [this message]
2026-08-18 18:24 ` [PATCH v2 8/8] migration: Harden vmstate_size Fabiano Rosas
2026-08-18 19:12 ` [PATCH v2 0/8] migration: Remove extra type-checking from vmstate macros Peter Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260818182441.404790-8-farosas@suse.de \
--to=farosas@suse.de \
--cc=dtalexundeer@yandex-team.ru \
--cc=mst@redhat.com \
--cc=peterx@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=vsementsov@yandex-team.ru \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.