From nobody Mon Feb 9 14:03:03 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.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.129.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=1630667338; cv=none; d=zohomail.com; s=zohoarc; b=Wvg5qGyk3YRUnmxRoVpKsIhAgIWnoEmf5IPqE4Cz6ogt2i7ouA2uTyjiRNWU4neX6c3ZJ4kdimlui1/prPPxLoBC/hHiZOAv3YAEjT4FHpmkxiznSBEWGkYLgxu4+Bcscjs4ck8CAkOlRKhCkEm0+yBIeuqIlDYgWp+DVdgMTns= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1630667338; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=e+/T5ni6234ihtWfiY6ChgUlClrUNMBebZZu1LShGeE=; b=fk6UwYBrAxIcZKW6htpdzyU91nCQxTIewcSjhaN6cqzxBsTB9RY7dctMDfwXqSZfUxdvsC0+gVWcmtnrLhATNrHh2n+XaHKL/aWbR+nSv2RzvqEbZ2U0UtvBSzO5j9AQNwHb/TDVB1DN8/QW59g72vR01Tqv5Ts+BY9FpJk+GVU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.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.129.124]) by mx.zohomail.com with SMTPS id 1630667338706100.97539098178811; Fri, 3 Sep 2021 04:08:58 -0700 (PDT) Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-33-iLCS7IMmNxKCjwfGV7ZtMw-1; Fri, 03 Sep 2021 07:08:54 -0400 Received: by mail-wr1-f70.google.com with SMTP id n1-20020a5d4c41000000b00159305d19baso1455376wrt.11 for ; Fri, 03 Sep 2021 04:08:54 -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 h16sm4386547wre.52.2021.09.03.04.08.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 04:08:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630667337; 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=e+/T5ni6234ihtWfiY6ChgUlClrUNMBebZZu1LShGeE=; b=I9czOIExIrB9X0nSaDFSM4J2Z1Pbu2cK1FcV8/tBjpiyiLjwwlnHXPmQ4ToEljB7Mq+djt KF/ABxajLCyupQ7i51Az3SHTL0WEtk3101pyBHYjIJ2NsRtd2hIjeiOurI9r1jfqHgrGKk 3Kzt2r5o4IAXFUKKLPqj5l2WuCcaUD4= X-MC-Unique: iLCS7IMmNxKCjwfGV7ZtMw-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=e+/T5ni6234ihtWfiY6ChgUlClrUNMBebZZu1LShGeE=; b=DmXxV5kfCnVVTEjWDoDk95ef49AWomKk8orWZ2vZfd6CI3+/09+8jC7xZmLyDmdiTo kIGX24gc6aaV7Kuz4Gby2RDynQ2o1f3Z/2WwzYOUtaACBW9HdIU4yQX4aqalLiDigNP1 3eD+jxzrUJwm8HLFo6QZxtBF9Se/WDw6GGomDOtlK+OgG7Y286+eAS9ogybHBo44LeJX s9Kqfcnu/f/EbLeLOm/u/66Ecf8MjH57VQ5iUfOZp5j8hf3j42ZeTy+ux75ffo8Tuu2b BH/42lBGST5td37wyaxEjxGYUOxC0rFeGnMPMT9RMbILE6SE9+XOM9Zd5X8HxtG498e/ +mZA== X-Gm-Message-State: AOAM533tlp+7A03eaDcC8Wb/KCBVRf63zEqtsotfvMEPmRTLP+RhTfre xUswaWQXpbU1yN4sG5H+9mqD7Nbbf+Z4LWU7cB5cGdDG1JGYzEyr0Qx0L/SgghvpcgoUiKV+qll n5xPkRfbBVwAjuA== X-Received: by 2002:adf:816f:: with SMTP id 102mr3412386wrm.368.1630667333532; Fri, 03 Sep 2021 04:08:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwKVIhqhQclBigb+uSRWXSSmTSWnWM8sjZh3FL9BO89jaiUDjU5xH1NQtxLg8HHW+REZ68Nfw== X-Received: by 2002:adf:816f:: with SMTP id 102mr3412332wrm.368.1630667333320; Fri, 03 Sep 2021 04:08:53 -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: [RFC PATCH 19/28] hw/virtio: Replace g_memdup() by g_memdup2_qemu() Date: Fri, 3 Sep 2021 13:06:53 +0200 Message-Id: <20210903110702.588291-20-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: 1630667340409100001 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 --- Should we check in_num/out_num in range? --- hw/net/virtio-net.c | 3 ++- hw/virtio/virtio-crypto.c | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 16d20cdee52..8fa23d5f941 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -1449,7 +1449,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_qemu(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 54f9bbb789c..43c1a39e469 100644 --- a/hw/virtio/virtio-crypto.c +++ b/hw/virtio/virtio-crypto.c @@ -242,7 +242,8 @@ 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_qemu(elem->out_sg, + sizeof(out_iov[0]) * out_num); out_iov =3D out_iov_copy; =20 in_num =3D elem->in_num; @@ -605,11 +606,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_qemu(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_qemu(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.31.1