From nobody Mon Feb 9 09:09:00 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1630667246; cv=none; d=zohomail.com; s=zohoarc; b=DZPgdq7lC9A3EmnwrzFA6qGWfS+tHqlXShcNUW4BHUvSpqB8AtcH7+pZyK/sO48CQvmRaFXGEIwdfTsQDph6Dhop0Whne3x5AUkzgMWpYCUbdTALpL2pFW3AwbmEKplfi+qm8hZKlYtJ60adgymgUdVdlcsK0i4tDO5hUH5+4Fs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630667246; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=IJcPcmBTlXv680lRxXxIAa1x+QW8hhus++pQyqguqCw=; b=cZhrbUuV2O7kRx6Xj1WnV5gFmxiKKDIZusb7oHK6bU6uvwmoWC+Zavr2e5Nr4vvQ66z3w3P2ehNj8EvNP/rp1R4S7dnTv9A6utGz/QKibmiSjwFygqth28S7EHWMdF2H9UJb2dAvCp4NT/QzySSdp6ZxNwMw4NV8dRaCx8y2ToI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1630667246666102.05455946565485; Fri, 3 Sep 2021 04:07:26 -0700 (PDT) Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-198-2fvijZMaOCeQtIJRKeEdsQ-1; Fri, 03 Sep 2021 07:07:24 -0400 Received: by mail-wm1-f70.google.com with SMTP id j33-20020a05600c48a100b002e879427915so1767697wmp.5 for ; Fri, 03 Sep 2021 04:07:24 -0700 (PDT) Return-Path: Return-Path: Received: from x1w.. (163.red-83-52-55.dynamicip.rima-tde.net. [83.52.55.163]) by smtp.gmail.com with ESMTPSA id k25sm4747138wrd.42.2021.09.03.04.07.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 04:07:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630667245; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IJcPcmBTlXv680lRxXxIAa1x+QW8hhus++pQyqguqCw=; b=CfnwAl5Uy73PnPFbWIOSuCTs+/fY8KX/IeK2VPvD0jxp1XDiZAyfmgonbEBiECCmevPFM7 HzZI8zY6inLxkWdzKwzvGkkayfXCCulh46J1G4rUStLQzxoS/kyCE90XaNdhs2y0oQdgaV 14YHUCDuDo+eAFm7DB7c8hywb3BqGkE= X-MC-Unique: 2fvijZMaOCeQtIJRKeEdsQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IJcPcmBTlXv680lRxXxIAa1x+QW8hhus++pQyqguqCw=; b=XOeqTCWKFBGLs0mtkpXAxyVKE7/c0HgUN8sWaWyjpViZjAPm4NYDsPg6uMN8/IbL2V lLeqp9gSvnenFiTe9lCd2fknJ1SiyV8gOlrWZVWd24cZMEexNCNWoMm4cAg7i55OiZ7K Uqn2he/tfXqLyKLXi6GanqnZzSKdgrtiwzSqdN0MvOSvNw7aBMz27mg7ipqSP6bFSwza xKdQIbhMmpQ1WMqdqkfx0j6aomsAjtNHsWSOzkQp7j6OxpTI8jsFxX3MHwUOyEzU65UM IBCbMT3lfQmT+7PZ0CdvTgejn5CXYKkXqPpnc25L7u4Zn5VC9hjM59Zt1mth8KTIz8bD HxBA== X-Gm-Message-State: AOAM532pPKW7uKCdkIeXh7Ss8Qt7cFSkAQT60bpp5Y9n0gLlVQUnC+I4 nee9VyHPQdhza4ZjwvVygk4Nqq9+C7KX6+Xuxe/z6PMRDVaPrh9yIzAvgkFs52H39xAs+EcvsQk quxm+ovXe/FL5yA== X-Received: by 2002:a7b:c014:: with SMTP id c20mr2767827wmb.81.1630667243364; Fri, 03 Sep 2021 04:07:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx907zJ6KqO6waEP4roxOCLkQUMR0ZCH7m0SU6uYQccu8GgebksOgpFBvIeXxJzbM2ctmPo7w== X-Received: by 2002:a7b:c014:: with SMTP id c20mr2767781wmb.81.1630667243141; Fri, 03 Sep 2021 04:07:23 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Hanna Reitz , Igor Mammedov , Laurent Vivier , Alexandre Iooss , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Michael Roth , Zhang Chen , Shannon Zhao , Richard Henderson , Alex Williamson , Eduardo Habkost , Markus Armbruster , Eric Blake , Stefan Weil , John Snow , Mahmoud Mandour , Li Zhijian , Marcel Apfelbaum , qemu-block@nongnu.org, Helge Deller , "Michael S. Tsirkin" , David Gibson , Peter Xu , "Gonglei (Arei)" , Gerd Hoffmann , Fam Zheng , Jason Wang , Vladimir Sementsov-Ogievskiy , Christian Schoenebeck , Kevin Wolf , Yuval Shaia , Paolo Bonzini , Peter Maydell , qemu-arm@nongnu.org, Thomas Huth , Laurent Vivier , Greg Kurz , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , qemu-ppc@nongnu.org, David Hildenbrand Subject: [PATCH 03/28] qapi: Replace g_memdup() by g_memdup2_qemu() Date: Fri, 3 Sep 2021 13:06:37 +0200 Message-Id: <20210903110702.588291-4-philmd@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210903110702.588291-1-philmd@redhat.com> References: <20210903110702.588291-1-philmd@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1630667248656100001 Per https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdu= p2-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. Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- qapi/qapi-clone-visitor.c | 16 ++++++++-------- qapi/qapi-visit-core.c | 6 ++++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/qapi/qapi-clone-visitor.c b/qapi/qapi-clone-visitor.c index c45c5caa3b8..fb38505d982 100644 --- a/qapi/qapi-clone-visitor.c +++ b/qapi/qapi-clone-visitor.c @@ -37,7 +37,7 @@ static bool qapi_clone_start_struct(Visitor *v, const cha= r *name, void **obj, return true; } =20 - *obj =3D g_memdup(*obj, size); + *obj =3D g_memdup2_qemu(*obj, size); qcv->depth++; return true; } @@ -65,8 +65,8 @@ static GenericList *qapi_clone_next_list(Visitor *v, Gene= ricList *tail, QapiCloneVisitor *qcv =3D to_qcv(v); =20 assert(qcv->depth); - /* Unshare the tail of the list cloned by g_memdup() */ - tail->next =3D g_memdup(tail->next, size); + /* Unshare the tail of the list cloned by g_memdup2() */ + tail->next =3D g_memdup2_qemu(tail->next, size); return tail->next; } =20 @@ -83,7 +83,7 @@ static bool qapi_clone_type_int64(Visitor *v, const char = *name, int64_t *obj, QapiCloneVisitor *qcv =3D to_qcv(v); =20 assert(qcv->depth); - /* Value was already cloned by g_memdup() */ + /* Value was already cloned by g_memdup2() */ return true; } =20 @@ -93,7 +93,7 @@ static bool qapi_clone_type_uint64(Visitor *v, const char= *name, QapiCloneVisitor *qcv =3D to_qcv(v); =20 assert(qcv->depth); - /* Value was already cloned by g_memdup() */ + /* Value was already cloned by g_memdup2() */ return true; } =20 @@ -103,7 +103,7 @@ static bool qapi_clone_type_bool(Visitor *v, const char= *name, bool *obj, QapiCloneVisitor *qcv =3D to_qcv(v); =20 assert(qcv->depth); - /* Value was already cloned by g_memdup() */ + /* Value was already cloned by g_memdup2() */ return true; } =20 @@ -114,7 +114,7 @@ static bool qapi_clone_type_str(Visitor *v, const char = *name, char **obj, =20 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 ch= ar *name, double *obj, QapiCloneVisitor *qcv =3D to_qcv(v); =20 assert(qcv->depth); - /* Value was already cloned by g_memdup() */ + /* Value was already cloned by g_memdup2() */ return true; } =20 diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c index a641adec51e..ebabe63b6ea 100644 --- a/qapi/qapi-visit-core.c +++ b/qapi/qapi-visit-core.c @@ -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 */ --=20 2.31.1