From nobody Mon Aug 24 21:50:34 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=1785934952; cv=none; d=zohomail.com; s=zohoarc; b=nBbVe3/te3HZyIu7M3qGeAVb38aduDcLKLSBL2m3kFkocuBXwCmCoFS2FhS8NqRnyuSp+8Ec97GqlxXKfT3ovZgjhaYja8DTAMsHBtcK+6/mxTPgFa3CbE/8MixrVMm6UY8eXinQUa0Qy2onmR01GsRlN+zN7cm+n8IDK3uBRzk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1785934952; 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=woYGaBGfgHz4AceQ3q5fOBMOdc8sEH2yW/Sj656EI/0=; b=a4l9zX+nGxcVdLxOnfdx4TXhRLWIwPyS6Iip9DeV9dsZjizuIsa8q8KaSx2469Tni9Z3QmLGukmdEfulw0Bqvw06bizM29T00avcHZ9YSwOl2lmb4y3arBB4nuC7VzNm8R/EGg1JEGKOR7RJ8VRTBZY0WZaMBeMtT0VZQDXIcBI= 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 1785934952479735.8758173210546; Wed, 5 Aug 2026 06:02:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wrbFw-00044d-Ov; Wed, 05 Aug 2026 09:02:00 -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 1wrbFu-00044R-Nh for qemu-devel@nongnu.org; Wed, 05 Aug 2026 09:01:58 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wrbFs-000321-73 for qemu-devel@nongnu.org; Wed, 05 Aug 2026 09:01:58 -0400 Received: from mx-prod-mc-01.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-56-6BcGwEFzNreJ3yjkbeZGFg-1; Wed, 05 Aug 2026 09:01:49 -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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8A44319560AF; Wed, 5 Aug 2026 13:01:47 +0000 (UTC) Received: from localhost (unknown [10.44.22.2]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 4016A1800346; Wed, 5 Aug 2026 13:01:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1785934913; 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=woYGaBGfgHz4AceQ3q5fOBMOdc8sEH2yW/Sj656EI/0=; b=iwUAvLvgKxh9BuDaQlKiXfNSseccA/w1EaJLFs7+vmYGI4//VZ1+TPVVIC6Za5tTPfTaeZ X/nX9HkN2+WsY7GaTZOWtQYfpKeJzDaHgV3Q113BpJdfDD76ucZ0+MUaXuzx53fnT4tV2B ckzr0dsLDUIdkTe84cZlCGjp14P8puA= X-MC-Unique: 6BcGwEFzNreJ3yjkbeZGFg-1 X-Mimecast-MFC-AGG-ID: 6BcGwEFzNreJ3yjkbeZGFg_1785934908 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 v5] virtio-gpu: use g_try_malloc to avoid guest-triggered abort Date: Wed, 5 Aug 2026 17:01:41 +0400 Message-ID: <20260805130141.211398-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.133.124; envelope-from=marcandre.lureau@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: 4 X-Spam_score: 0.4 X-Spam_bar: / X-Spam_report: (0.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.852, 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_H3=0.001, RCVD_IN_MSPIKE_WL=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: 1785934954326158500 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 Reviewed-by: Akihiko Odaki --- v5: - also fail gracefully in virtio_gpu_create_udmabuf() --- 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-udmabuf.c | 7 +++-- hw/display/virtio-gpu-virgl.c | 6 +++- hw/display/virtio-gpu.c | 42 ++++++++++++++++++------- 7 files changed, 79 insertions(+), 26 deletions(-) diff --git a/contrib/vhost-user-gpu/vhost-user-gpu.c b/contrib/vhost-user-g= pu/vhost-user-gpu.c index 786488150932..933bdbb671c0 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 a054f8117f14..041216a10d04 100644 --- a/hw/display/virtio-gpu-rutabaga.c +++ b/hw/display/virtio-gpu-rutabaga.c @@ -366,10 +366,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-udmabuf.c b/hw/display/virtio-gpu-udmabu= f.c index 5f08c855dde1..816f52a51457 100644 --- a/hw/display/virtio-gpu-udmabuf.c +++ b/hw/display/virtio-gpu-udmabuf.c @@ -39,8 +39,11 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu_= simple_resource *res) return; } =20 - list =3D g_malloc0(sizeof(struct udmabuf_create_list) + - sizeof(struct udmabuf_create_item) * res->iov_cnt); + list =3D g_try_malloc0(sizeof(struct udmabuf_create_list) + + sizeof(struct udmabuf_create_item) * res->iov_cnt= ); + if (!list) { + return; + } =20 for (i =3D 0; i < res->iov_cnt; i++) { rcu_read_lock(); diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c index 6e298f997d66..9bda572426b2 100644 --- a/hw/display/virtio-gpu-virgl.c +++ b/hw/display/virtio-gpu-virgl.c @@ -620,7 +620,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 fbb6fec7a0ad..9eb010082d0d 100644 --- a/hw/display/virtio-gpu.c +++ b/hw/display/virtio-gpu.c @@ -892,7 +892,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) { @@ -913,6 +916,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, @@ -921,20 +925,27 @@ 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) { + dma_memory_unmap(VIRTIO_DEVICE(g)->dma_as, map, len, + DMA_DIRECTION_TO_DEVICE, len); + 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) { + dma_memory_unmap(VIRTIO_DEVICE(g)->dma_as, map, le= n, + DMA_DIRECTION_TO_DEVICE, len); + goto err; + } + *addr =3D new_addr; } } (*iov)[v].iov_base =3D map; @@ -952,6 +963,15 @@ 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; + if (addr) { + g_clear_pointer(addr, g_free); + } + g_free(ents); + return -1; } =20 void virtio_gpu_cleanup_mapping_iov(VirtIOGPU *g, --=20 2.55.0