From patchwork Fri Sep 3 11:06:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 1524218 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=bD95BwEn; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4H1FR8340Rz9sW4 for ; Fri, 3 Sep 2021 21:08:52 +1000 (AEST) Received: from localhost ([::1]:53150 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mM73y-0005Fq-3A for incoming@patchwork.ozlabs.org; Fri, 03 Sep 2021 07:08:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42298) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mM72R-00047v-3v for qemu-devel@nongnu.org; Fri, 03 Sep 2021 07:07:15 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:44309) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mM72N-0008Fl-Ad for qemu-devel@nongnu.org; Fri, 03 Sep 2021 07:07:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630667229; 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; bh=PF/xnGXQTIoJHa57WKQQPyvoyaxxjuKOqyDKhhHUMA0=; b=bD95BwEnd6jLtsE1fqujz3OSSU9tKtXJpuJXQUcFkKjaaUTXEmT8JfpEZqSAB6pNZQUB9o gd+FEGHQE0hzbALeEzJEREi17wQzAQMMMWx9/4U028vZ6iXzsW4PS4AznFDvH7DcT447Zp IrJfjJWU3/GntPzYQTS8ObDbJeYA2g8= 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-17-yiOaW-3mNNeI156gudqSZg-1; Fri, 03 Sep 2021 07:07:08 -0400 X-MC-Unique: yiOaW-3mNNeI156gudqSZg-1 Received: by mail-wr1-f69.google.com with SMTP id v6-20020adfe4c6000000b001574f9d8336so1458248wrm.15 for ; Fri, 03 Sep 2021 04:07:08 -0700 (PDT) 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:mime-version :content-transfer-encoding; bh=hT4rpMU7gMMiB6PN8r96+Od1up6S2/wmxgTRnbJrmUA=; b=KEiHHG4X/6PsUBJKnFqus4kjl/P30k74OU87OMlW9abP0tjOWksO+8Wkvwzyi8olQz imoOdMTYBYKfRtMZ9FnUAbfzOU6CDAp1E0xhkwA9zSbXiN8BeJaSR3TcPSexn/GNbveg Fv1U0F+GWExLPnejOK2xXJU+VufhPja0iSvEM3VqHuhHD0whFCF9jg5Qm+QyAcO+U0dP JWYog2oQXK6h8vAOVYqwkrPpfje0oZfj1clXb2G3P9nwxEXrZqihhrrrEeWZl/hRHu8R siDnyyLjCUhl+kgCBduqXmZWDolReZK4oxmZfLXXA52fB1L1fljMBxoDEPALmUeppeg2 Ix1Q== X-Gm-Message-State: AOAM533GQjgbBzTS4hBg0sfHfzN7Jthlhmw5mui4/tsMZS6mFMvvTRCE nRQFoQAqHSYFfMGrhE/8cmPK6U5zY9Xb+gK2OaUaTI++rmV27YZnLnqLb6po4L1ZT2VVLLgE49o 32JOoLGF1YNIgtI1igqsI32WeWnUmXbd1wKAN9fuUrrWe24NYEQIm6Po0wP5zmAmR X-Received: by 2002:a05:600c:5102:: with SMTP id o2mr2726548wms.104.1630667226855; Fri, 03 Sep 2021 04:07:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzaINVrz2Oo6Ll0VUL1B7or1/Wu6l6LXRuExjk8CK/rA79v/WgMkhb7LDE02QVwLZ8ojY+26Q== X-Received: by 2002:a05:600c:5102:: with SMTP id o2mr2726472wms.104.1630667226343; Fri, 03 Sep 2021 04:07:06 -0700 (PDT) Received: from x1w.. (163.red-83-52-55.dynamicip.rima-tde.net. [83.52.55.163]) by smtp.gmail.com with ESMTPSA id r10sm4631717wrc.85.2021.09.03.04.07.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 04:07:05 -0700 (PDT) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Subject: [PATCH 00/28] glib: Replace g_memdup() by g_memdup2_qemu() Date: Fri, 3 Sep 2021 13:06:34 +0200 Message-Id: <20210903110702.588291-1-philmd@redhat.com> X-Mailer: git-send-email 2.31.1 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 Received-SPF: pass client-ip=170.10.129.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.392, 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 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , Peter Maydell , Li Zhijian , "Michael S. Tsirkin" , Jason Wang , Christian Schoenebeck , Yuval Shaia , Peter Xu , Gerd Hoffmann , Alexandre Iooss , Eric Blake , qemu-block@nongnu.org, Zhang Chen , =?utf-8?q?Alex_Benn=C3=A9e?= , Helge Deller , David Hildenbrand , Markus Armbruster , "Gonglei \(Arei\)" , Stefan Weil , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Laurent Vivier , Thomas Huth , Eduardo Habkost , Michael Roth , Richard Henderson , Greg Kurz , Alex Williamson , qemu-arm@nongnu.org, Paolo Bonzini , John Snow , David Gibson , Kevin Wolf , Vladimir Sementsov-Ogievskiy , Laurent Vivier , Shannon Zhao , Hanna Reitz , qemu-ppc@nongnu.org, Igor Mammedov , Mahmoud Mandour Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Per https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-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. g_memdup() as been deprecated in GLib 2.68. Since QEMU defines GLIB_VERSION_MAX_ALLOWED to GLIB_VERSION_2_56, the deprecation is not displayed (on GLib >= 2.68 such available on Fedora 34). However the function is still unsafe, so it is better to avoid its use. This series provides the safely equivalent g_memdup2_qemu() wrapper, and replace all g_memdup() calls by it. The previous link recommend to audit the call sites. Most of the calls use byte_size=sizeof(STRUCT), and no STRUCT appears to be > 4GiB. Few calls use unsigned/size_t/uint16_t. Where code is doing multiplication, patches are sent as RFC. In particular: hw/net/virtio-net.c hw/virtio/virtio-crypto.c Please review, Phil. Philippe Mathieu-Daudé (28): hw/hyperv/vmbus: Remove unused vmbus_load/save_req() glib-compat: Introduce g_memdup2() wrapper qapi: Replace g_memdup() by g_memdup2_qemu() accel/tcg: Replace g_memdup() by g_memdup2_qemu() block/qcow2-bitmap: Replace g_memdup() by g_memdup2_qemu() softmmu: Replace g_memdup() by g_memdup2_qemu() hw/9pfs: Replace g_memdup() by g_memdup2_qemu() hw/acpi: Avoid truncating acpi_data_len() to 32-bit hw/acpi: Replace g_memdup() by g_memdup2_qemu() hw/core/machine: Replace g_memdup() by g_memdup2_qemu() hw/hppa/machine: Replace g_memdup() by g_memdup2_qemu() hw/i386/multiboot: Replace g_memdup() by g_memdup2_qemu() hw/net/eepro100: Replace g_memdup() by g_memdup2_qemu() hw/nvram/fw_cfg: Replace g_memdup() by g_memdup2_qemu() hw/scsi/mptsas: Replace g_memdup() by g_memdup2_qemu() hw/ppc/spapr_pci: Replace g_memdup() by g_memdup2_qemu() hw/rdma: Replace g_memdup() by g_memdup2_qemu() hw/vfio/pci: Replace g_memdup() by g_memdup2_qemu() RFC hw/virtio: Replace g_memdup() by g_memdup2_qemu() net/colo: Replace g_memdup() by g_memdup2_qemu() RFC ui/clipboard: Replace g_memdup() by g_memdup2_qemu() RFC linux-user: Replace g_memdup() by g_memdup2_qemu() tests/unit: Replace g_memdup() by g_memdup2_qemu() tests/qtest: Replace g_memdup() by g_memdup2_qemu() target/arm: Replace g_memdup() by g_memdup2_qemu() target/ppc: Replace g_memdup() by g_memdup2_qemu() contrib: Replace g_memdup() by g_memdup2_qemu() checkpatch: Do not allow deprecated g_memdup() include/glib-compat.h | 36 ++++++++++++++++++++++ include/hw/hyperv/vmbus.h | 3 -- accel/tcg/cputlb.c | 8 ++--- block/qcow2-bitmap.c | 2 +- contrib/plugins/lockstep.c | 2 +- contrib/rdmacm-mux/main.c | 10 +++---- hw/9pfs/9p-synth.c | 2 +- hw/9pfs/9p.c | 2 +- hw/acpi/core.c | 3 +- hw/arm/virt-acpi-build.c | 2 +- hw/core/machine.c | 4 +-- hw/hppa/machine.c | 8 ++--- hw/hyperv/vmbus.c | 59 ------------------------------------- hw/i386/acpi-build.c | 6 ++-- hw/i386/multiboot.c | 2 +- hw/net/eepro100.c | 2 +- hw/net/virtio-net.c | 3 +- hw/nvram/fw_cfg.c | 9 +++--- hw/ppc/spapr_pci.c | 8 ++--- hw/rdma/rdma_utils.c | 2 +- hw/scsi/mptsas.c | 5 ++-- hw/vfio/pci.c | 2 +- hw/virtio/virtio-crypto.c | 7 +++-- linux-user/syscall.c | 2 +- linux-user/uaccess.c | 2 +- net/colo.c | 4 +-- qapi/qapi-clone-visitor.c | 16 +++++----- qapi/qapi-visit-core.c | 6 ++-- softmmu/memory.c | 2 +- softmmu/vl.c | 2 +- target/arm/helper.c | 7 +++-- target/ppc/mmu-hash64.c | 3 +- tests/qtest/libqos/ahci.c | 6 ++-- tests/qtest/libqos/qgraph.c | 2 +- tests/unit/ptimer-test.c | 22 +++++++------- tests/unit/test-iov.c | 26 ++++++++-------- ui/clipboard.c | 2 +- scripts/checkpatch.pl | 5 ++++ 38 files changed, 141 insertions(+), 153 deletions(-)