From nobody Mon Feb 9 21:19:33 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=1630667266; cv=none; d=zohomail.com; s=zohoarc; b=YEnd1dxM3x4pdHL2BGbtmyFSrsJGkAFivubHvniuGaOAU6Xn7Rkp3ymVHW3ga4siyIpZofFp5aCHWN3SemGiSEqCutWRw2aeZDJN2q1PyK+szdUK+nGKva+QqDPnpEnLCMzcbT0Yj4xoyrbrV8rdib178YUX/BKVtrAw5J3w2GY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630667266; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=vyN09QcPG+VDBDsPFQVE2hfeE/VYAwQywY+WfYVVI7k=; b=T+3rSVmM6W66FSRPMHiPrHh+R5xri7dfGSjQa5Bme8y7vVxGHl491Roqn8jnXxDqVcut705Zijc71yiYjg85JicpPrj3sjnncYn4gW6Ub/DY0gi/fuHjOg1CkVrbcxInGAaBhn0CyjAnkuPz2VtHxBqOqDHRQ0ARJf1l5UUxo48= 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 1630667265997162.01666967704875; Fri, 3 Sep 2021 04:07:45 -0700 (PDT) Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-380-DhCSzM-WPEyJjPJQFGfMKQ-1; Fri, 03 Sep 2021 07:07:41 -0400 Received: by mail-wr1-f72.google.com with SMTP id h14-20020a056000000e00b001575b00eb08so1445944wrx.13 for ; Fri, 03 Sep 2021 04:07:40 -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 f3sm3966844wmj.28.2021.09.03.04.07.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 04:07:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630667264; 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=vyN09QcPG+VDBDsPFQVE2hfeE/VYAwQywY+WfYVVI7k=; b=exSoiuabXggiJrGD0SvjZLNAKHsFBKtu5mR22GrZ5aP5E6vTjNzn7pKiUnAmDMMP1PpXO1 YyQ6S7Vian98Qq8HUCT0w5s3pW1ruiu1LKIMMMScnnzGkhHf5OOYYEZ24QeCfZngZzhP1y JqWGUjTNFVNSrpCRvMdnrhxLZmPqMAE= X-MC-Unique: DhCSzM-WPEyJjPJQFGfMKQ-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=vyN09QcPG+VDBDsPFQVE2hfeE/VYAwQywY+WfYVVI7k=; b=Dff0k+5d8nCe1QCJmtUxgD1r7ZX3VZ4rq0izkjD/FABuxtN4wyqP0Ul9Pj5NWRVmGD 6RhP/fzonYV7YCw23qym3QqnuPiqoMupyKty0wLNT5WWHiG8Zw4wukYnOfKH94OS32c3 SmEjYs/PamlBzOYiW91pWAIXbTCIpVi/2//1yiRN5Y5P51Q2DqgtDNwIHy3uSPUX/P/M iuPO3BIWxAwRV+9XzW/8iD6Gl6PBNS29aLNDFzCUxAtIeoBvC+Tw4kvdRed9GKsFc3cF DUEv7KZCN/FjD+0RRW7KuUZmpLE84jVawZEaa9OWj1MjnZQUrdzAB6Btd5aZhBLOhX2B 6eEQ== X-Gm-Message-State: AOAM532L5y1erayIJ6M0TqCAWWXap/wMLXjQvIrNJwYdI+ad9dGFxlH2 /8KVgCCW11WbfYFPlzUwNhB0kGNaxM/LEY8v7s9ty/Nu+OjrnMmn7vs1jtF0NOzDejDRQEZAmmF XBRoJhXZSj8wrRw== X-Received: by 2002:adf:e6c5:: with SMTP id y5mr3584614wrm.198.1630667259941; Fri, 03 Sep 2021 04:07:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwTXAcCGMILiN6BBKbtvJurMFFvfI5fIWlIBSfIhCVNo+ciUFdo+9M1Slhc1REyU+6QphSpBA== X-Received: by 2002:adf:e6c5:: with SMTP id y5mr3584577wrm.198.1630667259732; Fri, 03 Sep 2021 04:07:39 -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 06/28] softmmu: Replace g_memdup() by g_memdup2_qemu() Date: Fri, 3 Sep 2021 13:06:40 +0200 Message-Id: <20210903110702.588291-7-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: 1630667266711100001 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 --- softmmu/memory.c | 2 +- softmmu/vl.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/softmmu/memory.c b/softmmu/memory.c index bfedaf9c4df..838a274b627 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -1140,7 +1140,7 @@ static char *memory_region_escape_name(const char *na= me) bytes +=3D memory_region_need_escape(*p) ? 4 : 1; } if (bytes =3D=3D p - name) { - return g_memdup(name, bytes + 1); + return g_memdup2_qemu(name, bytes + 1); } =20 escaped =3D g_malloc(bytes + 1); diff --git a/softmmu/vl.c b/softmmu/vl.c index ea05bb39c50..a136ef0bfb6 100644 --- a/softmmu/vl.c +++ b/softmmu/vl.c @@ -1154,7 +1154,7 @@ static int parse_fw_cfg(void *opaque, QemuOpts *opts,= Error **errp) } if (nonempty_str(str)) { size =3D strlen(str); /* NUL terminator NOT included in fw_cfg blo= b */ - buf =3D g_memdup(str, size); + buf =3D 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; --=20 2.31.1