From nobody Mon Feb 9 09:16:38 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1649688844296128.7705257210115; Mon, 11 Apr 2022 07:54:04 -0700 (PDT) Received: from localhost ([::1]:44158 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ndvQX-0000Us-Qk for importer@patchew.org; Mon, 11 Apr 2022 10:54:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40504) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ndvNX-0005nv-M5 for qemu-devel@nongnu.org; Mon, 11 Apr 2022 10:50:55 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:60842) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ndvNS-0007kO-3F for qemu-devel@nongnu.org; Mon, 11 Apr 2022 10:50:55 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-37-_W1x-OBSMPCDBrXBUelyTw-1; Mon, 11 Apr 2022 10:50:46 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 30B791C0B044; Mon, 11 Apr 2022 14:50:46 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.192.138]) by smtp.corp.redhat.com (Postfix) with ESMTP id 200F854AC99; Mon, 11 Apr 2022 14:50:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649688649; 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=n9vkaECCZXkAflzlL3P30ZPEXvMKzVcAl99HEU/aA4A=; b=af2F1VPS4kP2FYsBCapFy2e5AJil3ppuoAMVKrU2mXIygvt+jNFkWyiP5mzo9HuYCvQO+A dpYCqX9jL5+SFpPuubQwxdN03LlOer4tmMfXlgEtZrfNmnGo8F57urqVKKt8QZG1R4VFIY ZH1obZYBwcXwCoLOx3IDJAtaq4mBCak= X-MC-Unique: _W1x-OBSMPCDBrXBUelyTw-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [RFC PATCH v6 04/23] hw/virtio: Replace g_memdup() by g_memdup2() Date: Mon, 11 Apr 2022 16:50:07 +0200 Message-Id: <20220411145026.1618035-5-eperezma@redhat.com> In-Reply-To: <20220411145026.1618035-1-eperezma@redhat.com> References: <20220411145026.1618035-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laurent Vivier , Parav Pandit , Cindy Lu , "Michael S. Tsirkin" , Jason Wang , Gautam Dawar , Harpreet Singh Anand , Eli Cohen , Paolo Bonzini , Liuxiangdong , Zhu Lingshan Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1649688846097100001 From: Philippe Mathieu-Daud=C3=A9 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() wrapper. Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- hw/net/virtio-net.c | 3 ++- hw/virtio/virtio-crypto.c | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 1067e72b39..e4748a7e6c 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -1443,7 +1443,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev= , VirtQueue *vq) } =20 iov_cnt =3D elem->out_num; - iov2 =3D iov =3D g_memdup(elem->out_sg, sizeof(struct iovec) * ele= m->out_num); + iov2 =3D iov =3D g_memdup2(elem->out_sg, + sizeof(struct iovec) * elem->out_num); s =3D iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl)); iov_discard_front(&iov, &iov_cnt, sizeof(ctrl)); if (s !=3D sizeof(ctrl)) { diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c index dcd80b904d..0e31e3cc04 100644 --- a/hw/virtio/virtio-crypto.c +++ b/hw/virtio/virtio-crypto.c @@ -242,7 +242,7 @@ static void virtio_crypto_handle_ctrl(VirtIODevice *vde= v, VirtQueue *vq) } =20 out_num =3D elem->out_num; - out_iov_copy =3D g_memdup(elem->out_sg, sizeof(out_iov[0]) * out_n= um); + out_iov_copy =3D g_memdup2(elem->out_sg, sizeof(out_iov[0]) * out_= num); out_iov =3D out_iov_copy; =20 in_num =3D elem->in_num; @@ -605,11 +605,11 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request) } =20 out_num =3D elem->out_num; - out_iov_copy =3D g_memdup(elem->out_sg, sizeof(out_iov[0]) * out_num); + out_iov_copy =3D g_memdup2(elem->out_sg, sizeof(out_iov[0]) * out_num); out_iov =3D out_iov_copy; =20 in_num =3D elem->in_num; - in_iov_copy =3D g_memdup(elem->in_sg, sizeof(in_iov[0]) * in_num); + in_iov_copy =3D g_memdup2(elem->in_sg, sizeof(in_iov[0]) * in_num); in_iov =3D in_iov_copy; =20 if (unlikely(iov_to_buf(out_iov, out_num, 0, &req, sizeof(req)) --=20 2.27.0