From nobody Mon Sep 28 02:02:25 2026 Delivered-To: importer@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+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1785309645; cv=none; d=zohomail.com; s=zohoarc; b=BjxTrUwfFBBmT8sacF9tfMMiGa/WnXU63YjnLBwJKXqbdOVgh0ooMeV3NeJqMUp697KsHYVKshCdQdI7uZg2S99Ywx8vjNxR+Ggg9GhcCoqJ9ruQFvNuxHCD6U6xPbq/5FoqtF04l07dPV51czCJKaqi9CDlRdUjd471ZHaCkR0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1785309645; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=0cBcPG8hhBA8exhkhF/jl6VTvQ+AFlhuHkVLgIDI6Ro=; b=GD7LWniByEgNYpG7pLFdHUcAEiYOrVgL2ava26qUOhWEFr24wzigE5vkcbmAndwEGjTYsoatSNwtVcNYdBi3TMtWJZpa7zY+tv1cMP2tLzLr2CQwCP07+Xb80Lx+ZErC8sOMjQTBbD40WKDg6gbtfQevpMe+QpcFB8Drq+7OYMA= 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+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1785309645867409.01411071517964; Wed, 29 Jul 2026 00:20:45 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1woyaE-0003Uh-Nm; Wed, 29 Jul 2026 03:20:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1woyaC-0003Tw-RX for qemu-devel@nongnu.org; Wed, 29 Jul 2026 03:20:04 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1woya9-0004aA-2S for qemu-devel@nongnu.org; Wed, 29 Jul 2026 03:20:03 -0400 Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-155-qlVajLmGPjemI7-c6N9XwQ-1; Wed, 29 Jul 2026 03:19:57 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BB00C19560AA; Wed, 29 Jul 2026 07:19:55 +0000 (UTC) Received: from localhost (unknown [10.44.24.15]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id DCB8A180044F; Wed, 29 Jul 2026 07:19:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1785309598; 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=0cBcPG8hhBA8exhkhF/jl6VTvQ+AFlhuHkVLgIDI6Ro=; b=ehGFuA4H8ZU5ynMb6AbsyZu6D0+g1L33xc1U6cV8EbZMzzLZz4xUkgn1MKxsOgydYZBNiq 2r030Flzly2QPTUzUy9di6z5Eri0Sq4/H2XwMGLRUpukV0d2ou8mFwt93+FEq88hJuXpyj RKungd8syPO0fpshheYV//23BDghNVo= X-MC-Unique: qlVajLmGPjemI7-c6N9XwQ-1 X-Mimecast-MFC-AGG-ID: qlVajLmGPjemI7-c6N9XwQ_1785309596 From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Cc: akihiko.odaki@gmail.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S. Tsirkin" , Stefano Garzarella , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Akihiko Odaki , Dmitry Osipenko Subject: [PATCH v3] virtio-gpu: use g_try_malloc to avoid guest-triggered abort Date: Wed, 29 Jul 2026 11:19:49 +0400 Message-ID: <20260729071950.276303-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=marcandre.lureau@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -2 X-Spam_score: -0.3 X-Spam_bar: / X-Spam_report: (-0.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.58, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1785309648646158500 From: Marc-Andr=C3=A9 Lureau Use g_try_malloc/g_try_new0 for guest-controlled allocation, so failure returns an error to the guest rather than crashing the host (glib behaviour). Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3898 Signed-off-by: Marc-Andr=C3=A9 Lureau --- Based-on: <20260710134720.2317856-1-marcandre.lureau@redhat.com> ("[PATCH] = vhost-user-gpu: fix integer overflow in buffer allocation") v3: - also try allocations for SG DMA mappings --- contrib/vhost-user-gpu/vhost-user-gpu.c | 25 +++++++++++------ contrib/vhost-user-gpu/virgl.c | 6 ++++- contrib/vhost-user-gpu/vugbm.c | 5 +++- hw/display/virtio-gpu-rutabaga.c | 14 ++++++++-- hw/display/virtio-gpu-virgl.c | 6 ++++- hw/display/virtio-gpu.c | 36 +++++++++++++++++-------- 6 files changed, 68 insertions(+), 24 deletions(-) diff --git a/contrib/vhost-user-gpu/vhost-user-gpu.c b/contrib/vhost-user-g= pu/vhost-user-gpu.c index ee9858c397ce..0fd115300425 100644 --- a/contrib/vhost-user-gpu/vhost-user-gpu.c +++ b/contrib/vhost-user-gpu/vhost-user-gpu.c @@ -487,7 +487,7 @@ vg_create_mapping_iov(VuGpu *g, struct virtio_gpu_ctrl_command *cmd, struct iovec **iov) { - struct virtio_gpu_mem_entry *ents; + g_autofree struct virtio_gpu_mem_entry *ents =3D NULL; size_t esize, s; int i; =20 @@ -498,17 +498,22 @@ vg_create_mapping_iov(VuGpu *g, } =20 esize =3D sizeof(*ents) * ab->nr_entries; - ents =3D g_malloc(esize); + ents =3D g_try_malloc(esize); + if (!ents && esize) { + return -1; + } s =3D iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num, sizeof(*ab), ents, esize); if (s !=3D esize) { g_critical("%s: command data size incorrect %zu vs %zu", __func__, s, esize); - g_free(ents); return -1; } =20 - *iov =3D g_new0(struct iovec, ab->nr_entries); + *iov =3D g_try_new0(struct iovec, ab->nr_entries); + if (!*iov && ab->nr_entries) { + return -1; + } for (i =3D 0; i < ab->nr_entries; i++) { uint64_t len =3D ents[i].length; (*iov)[i].iov_len =3D ents[i].length; @@ -517,12 +522,10 @@ vg_create_mapping_iov(VuGpu *g, g_critical("%s: resource %d element %d", __func__, ab->resource_id, i); g_free(*iov); - g_free(ents); *iov =3D NULL; return -1; } } - g_free(ents); return 0; } =20 @@ -828,8 +831,14 @@ vg_resource_flush(VuGpu *g, PIXMAN_FORMAT_BPP(pixman_image_get_format(res->image)) / 8; size_t size =3D width * height * bpp; =20 - void *p =3D g_malloc(VHOST_USER_GPU_HDR_SIZE + - sizeof(VhostUserGpuUpdate) + size); + void *p =3D g_try_malloc(VHOST_USER_GPU_HDR_SIZE + + sizeof(VhostUserGpuUpdate) + size); + if (!p) { + pixman_region_fini(®ion); + pixman_region_fini(&finalregion); + cmd->error =3D VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY; + break; + } VhostUserGpuMsg *msg =3D p; msg->request =3D VHOST_USER_GPU_UPDATE; msg->size =3D sizeof(VhostUserGpuUpdate) + size; diff --git a/contrib/vhost-user-gpu/virgl.c b/contrib/vhost-user-gpu/virgl.c index 550fd03bf5c4..20bae57d0fe4 100644 --- a/contrib/vhost-user-gpu/virgl.c +++ b/contrib/vhost-user-gpu/virgl.c @@ -209,7 +209,11 @@ virgl_cmd_submit_3d(VuGpu *g, return; } =20 - buf =3D g_malloc(cs.size); + buf =3D g_try_malloc(cs.size); + if (!buf && cs.size) { + cmd->error =3D VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY; + return; + } s =3D iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num, sizeof(cs), buf, cs.size); if (s !=3D cs.size) { diff --git a/contrib/vhost-user-gpu/vugbm.c b/contrib/vhost-user-gpu/vugbm.c index 710d54529779..e2d8385fd857 100644 --- a/contrib/vhost-user-gpu/vugbm.c +++ b/contrib/vhost-user-gpu/vugbm.c @@ -13,7 +13,10 @@ static bool mem_alloc_bo(struct vugbm_buffer *buf) { - buf->mmap =3D g_malloc((uint64_t)buf->width * buf->height * 4); + buf->mmap =3D g_try_malloc((uint64_t)buf->width * buf->height * 4); + if (!buf->mmap && buf->width && buf->height) { + return false; + } buf->stride =3D buf->width * 4; return true; } diff --git a/hw/display/virtio-gpu-rutabaga.c b/hw/display/virtio-gpu-rutab= aga.c index e28aad94eead..60f64d9a8fa7 100644 --- a/hw/display/virtio-gpu-rutabaga.c +++ b/hw/display/virtio-gpu-rutabaga.c @@ -360,10 +360,20 @@ rutabaga_cmd_submit_3d(VirtIOGPU *g, return; } =20 - buf =3D g_new0(uint8_t, cs.size); + buf =3D g_try_new0(uint8_t, cs.size); + if (!buf && cs.size) { + cmd->error =3D VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY; + return; + } s =3D iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num, sizeof(cs), buf, cs.size); - CHECK(s =3D=3D cs.size, cmd); + if (s !=3D cs.size) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: size mismatch (%zu/%u)\n", + __func__, s, cs.size); + cmd->error =3D VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER; + return; + } =20 rutabaga_cmd.ctx_id =3D cs.hdr.ctx_id; rutabaga_cmd.cmd =3D buf; diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c index d9e5b0110497..028ab776df6a 100644 --- a/hw/display/virtio-gpu-virgl.c +++ b/hw/display/virtio-gpu-virgl.c @@ -615,7 +615,11 @@ static void virgl_cmd_submit_3d(VirtIOGPU *g, return; } =20 - buf =3D g_malloc(cs.size); + buf =3D g_try_malloc(cs.size); + if (!buf && cs.size) { + cmd->error =3D VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY; + return; + } s =3D iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num, sizeof(cs), buf, cs.size); if (s !=3D cs.size) { diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c index c707f1f516ea..6894d500fb36 100644 --- a/hw/display/virtio-gpu.c +++ b/hw/display/virtio-gpu.c @@ -882,7 +882,10 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g, } =20 esize =3D sizeof(*ents) * nr_entries; - ents =3D g_malloc(esize); + ents =3D g_try_malloc(esize); + if (!ents && esize) { + return -1; + } s =3D iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num, offset, ents, esize); if (s !=3D esize) { @@ -903,6 +906,7 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g, hwaddr len; void *map; =20 + /* TODO: a common DMA map SG helper */ do { len =3D l; map =3D dma_memory_map(VIRTIO_DEVICE(g)->dma_as, a, &len, @@ -911,20 +915,23 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g, if (!map) { qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to map MMIO mem= ory for" " element %d\n", __func__, e); - virtio_gpu_cleanup_mapping_iov(g, *iov, v); - g_free(ents); - *iov =3D NULL; - if (addr) { - g_free(*addr); - *addr =3D NULL; - } - return -1; + goto err; } =20 if (!(v % 16)) { - *iov =3D g_renew(struct iovec, *iov, v + 16); + struct iovec *new_iov; + new_iov =3D g_try_renew(struct iovec, *iov, v + 16); + if (!new_iov) { + goto err; + } + *iov =3D new_iov; if (addr) { - *addr =3D g_renew(uint64_t, *addr, v + 16); + uint64_t *new_addr; + new_addr =3D g_try_renew(uint64_t, *addr, v + 16); + if (!new_addr) { + goto err; + } + *addr =3D new_addr; } } (*iov)[v].iov_base =3D map; @@ -942,6 +949,13 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g, =20 g_free(ents); return 0; + +err: + virtio_gpu_cleanup_mapping_iov(g, *iov, v); + *iov =3D NULL; + g_clear_pointer(addr, g_free); + g_free(ents); + return -1; } =20 void virtio_gpu_cleanup_mapping_iov(VirtIOGPU *g, --=20 2.55.0