From nobody Mon Feb 9 08:10:15 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.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 216.205.24.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=1630667308; cv=none; d=zohomail.com; s=zohoarc; b=XYn523uUCzwn0PEFGhNUPx/FSz1Oypu/1VweOiOKP0tDbD9fqzcGxTtFS8nrbYdPA63rpQdsejJRlTrRpMCFTBXLTwgYSY3HJxC4t6iO1SCicHWbj0+iFFT1vtVql5YcZBJAqSUmvlhJcMNIoEcp4v23RxrtHCDMVKDBLzj+gsw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630667308; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=eD1qqZlm+MV+YIIGwjAnZcGqFrpvQZPW91qRyOYFT9E=; b=bOiL1BWBSS4E/1PLgCRX450TaQwpbgJLKXsrfU+ndTnIyz1d7yWmJZn2sT9yDJmUPcnstS/xiie6+139JBdnvOgYiKUiefhIC3wWK8lXnh2cLnX4WqB0yQFh1yMAlgmiH1DN5kdngtOnI2AYJUH+S89vz1i6V4xuX6RYJy153a8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.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 [216.205.24.124]) by mx.zohomail.com with SMTPS id 1630667308625505.0901912369094; Fri, 3 Sep 2021 04:08:28 -0700 (PDT) Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-559-n2mLeOOhNnWVWtFWfS8uzQ-1; Fri, 03 Sep 2021 07:08:26 -0400 Received: by mail-wr1-f69.google.com with SMTP id t15-20020a5d42cf000000b001565f9c9ee8so1459081wrr.2 for ; Fri, 03 Sep 2021 04:08:26 -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 t64sm3914121wma.48.2021.09.03.04.08.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 04:08:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630667307; 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=eD1qqZlm+MV+YIIGwjAnZcGqFrpvQZPW91qRyOYFT9E=; b=IvgR3Hu2pVG9GY6o066s3a5jOEFoc4AsyPxZzCe7mQ2Na4NshrlIvef8A/ktZw08KxyLP9 oDVeNyi2ms111OHkLAANFW8qvvHT46aXvLXapPnDDMZXDd3RqP2hYfmRaAXkgX1eSxUMaj ebaN3Ti9VnKET1IXPIgwlgJHd2BJt2k= X-MC-Unique: n2mLeOOhNnWVWtFWfS8uzQ-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=eD1qqZlm+MV+YIIGwjAnZcGqFrpvQZPW91qRyOYFT9E=; b=Z02QS2CuikVT9Ju7uzjTH8GUXgXGVn9FG8nFEBtSCUsg6v76tY4fmjEnjMj37ly3vS zULShP6MaVZtVlb2AE6SGVUwLzjtixDKX5ZYm/1wiOoiE7itMAd+qxBQSgYV9xhZECNs YT8+EMnd1Ud6phb6T2polTU9VH7m1LpS9WUhYEJPqW+fEGhaYQDPEJWn0IbbdjcolazT Cgq+LRdXHw77ivwV9v2LOZu0WH65ZccfYvvrdcd5SSQlu2ONU18/+HWOU8GRtlGyTJqZ cCylNeO8Vpw+2ndcMcw1+3J06MZ9bfO6GRt/Uta70kMxYL4vcwltJtyiyV6rTdPNvBnz fEhg== X-Gm-Message-State: AOAM533f22lfm964a00mgZ0SC6tz3SmXBFHJtD9+3W8Ocid/k/W8iIHL NrX1zQ9c9B/lfes9lQN5IaDZVkpWlNGSDfU62WPKwMdikAWcGptWQps0/d8hVzahgpS4oBFoYV1 cKUFWOKJwyaaufw== X-Received: by 2002:a1c:c903:: with SMTP id f3mr7737085wmb.101.1630667305419; Fri, 03 Sep 2021 04:08:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJySTjPFgphZi0Ha8ChgIVmQjp9uZBff9gP4iaRxu5Bv69t9K0UO/ElQXH2XkzX2xYryeljCRA== X-Received: by 2002:a1c:c903:: with SMTP id f3mr7737046wmb.101.1630667305212; Fri, 03 Sep 2021 04:08:25 -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 14/28] hw/nvram/fw_cfg: Replace g_memdup() by g_memdup2_qemu() Date: Fri, 3 Sep 2021 13:06:48 +0200 Message-Id: <20210903110702.588291-15-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: 1630667309414100001 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 --- hw/nvram/fw_cfg.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/hw/nvram/fw_cfg.c b/hw/nvram/fw_cfg.c index 9b8dcca4ead..fefcdeb8241 100644 --- a/hw/nvram/fw_cfg.c +++ b/hw/nvram/fw_cfg.c @@ -205,7 +205,8 @@ static void fw_cfg_bootsplash(FWCfgState *s) /* use little endian format */ bst_le16 =3D cpu_to_le16(bst_val); fw_cfg_add_file(s, "etc/boot-menu-wait", - g_memdup(&bst_le16, sizeof bst_le16), sizeof bst_l= e16); + g_memdup2_qemu(&bst_le16, sizeof bst_le16), + sizeof bst_le16); } =20 /* insert splash file if user configurated */ @@ -260,7 +261,7 @@ static void fw_cfg_reboot(FWCfgState *s) } =20 rt_le32 =3D 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); } =20 static void fw_cfg_write(FWCfgState *s, uint8_t value) @@ -755,7 +756,7 @@ void fw_cfg_add_string(FWCfgState *s, uint16_t key, con= st char *value) size_t sz =3D strlen(value) + 1; =20 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); } =20 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 =3D strlen(value) + 1; char *old; =20 - old =3D fw_cfg_modify_bytes_read(s, key, g_memdup(value, sz), sz); + old =3D fw_cfg_modify_bytes_read(s, key, g_memdup2_qemu(value, sz), sz= ); g_free(old); } =20 --=20 2.31.1