From nobody Sun Jul 13 21:48:26 2025 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1652382881; cv=none; d=zohomail.com; s=zohoarc; b=MkDfWE4TD4U9GnvSc4Am/JVMuqxmdqqIj6TZZxC/Or9tQWStMOyfeCynqlTV1t5oz7YMo15N81p1aq+SftURz0bJ8SHlPBefFmEJAbeQEISvnFWeTPpxE634lLVAio2IPBlqvGB6NWJfjuBnscvWA87kfANYzGjqzbpkYm7fWOQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1652382881; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=4PkJ9AftPi32TpD+kjV0SOKYsI2NXHREerwEsgX8ncQ=; b=PvYAWp337Q8Qa8XvTF8OjiM6V+DOmp42aiEesbO3ly8tGlssfccEu1bdvGyDSvCGkCxqoM/Cj6JizgxDcjxh2uR0oYXgdRPACXd9+GADx2lJ8y7TOyyA4V5FVc6j5JxjGch4W2Nvcyr1K0Xbp3tfqWAPVUgv+c6q7g03aOueu4Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1652382881050231.1814037149835; Thu, 12 May 2022 12:14:41 -0700 (PDT) Received: from localhost ([::1]:40120 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1npEGl-00031N-Gu for importer2@patchew.org; Thu, 12 May 2022 15:14:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42730) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1npD4l-0000lT-Rw for qemu-devel@nongnu.org; Thu, 12 May 2022 13:58:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:40186) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1npD4i-0001kr-OD for qemu-devel@nongnu.org; Thu, 12 May 2022 13:58:11 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-325-lj35mmpdNyuKqmB19TRU7g-1; Thu, 12 May 2022 13:58:02 -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 46C4E811E80; Thu, 12 May 2022 17:58:02 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.193.131]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6AE1443E390; Thu, 12 May 2022 17:58:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1652378287; 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=4PkJ9AftPi32TpD+kjV0SOKYsI2NXHREerwEsgX8ncQ=; b=OFO2I68IBoRc+IsCLVMC7Tq4R7uysTPXoA41pB41KmrCkp0slkmJ8tkdklWd+Kb9tEnUbE yA/VibsQU5cpisMY9XwOt4Gt8lMcWliO0eBWc/Cxa7dwgxLS7VHime5QVa7tAzeoGMS1tv wtQBsfo3cI7Vg0EinQBSIdfAcJaVgrM= X-MC-Unique: lj35mmpdNyuKqmB19TRU7g-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Cc: Gautam Dawar , Laurent Vivier , Harpreet Singh Anand , qemu-stable@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , "Michael S. Tsirkin" , Jason Wang , "Gonglei (Arei)" , Cindy Lu Subject: [PATCH 5/6] hw/virtio: Replace g_memdup() by g_memdup2() Date: Thu, 12 May 2022 19:57:46 +0200 Message-Id: <20220512175747.142058-6-eperezma@redhat.com> In-Reply-To: <20220512175747.142058-1-eperezma@redhat.com> References: <20220512175747.142058-1-eperezma@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 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+importer2=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, 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: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1652382882528100001 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. Acked-by: Jason Wang Acked-by: Eugenio P=C3=A9rez 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