From nobody Sun May 19 14:14:19 2024 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=none dis=none) header.from=suse.de ARC-Seal: i=1; a=rsa-sha256; t=1676036652; cv=none; d=zohomail.com; s=zohoarc; b=Epbs80K9YQ6OXtX2YOaPftCVXB5CF3pEtOvmof9ytC5KdGDyCXdP3Ft/GVU/JiLO7HUmoz1hJJ1dyF2MTPGJjkWr9mTwI31z3SGjUmvA+ug+sfiYfbkPZmzCkvmiqEihTyrzx2v9znYw5SUj7DcquNyHYaZERkeHtWtliVDnhcQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1676036652; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=rPO7Tl5ZAji92Jbza+uQUlcMvktjS3fjWKiTWL5j7g4=; b=drKwSBNsoBAeo1oAMJSa7aaAkcFMt5Qr0OUdb6J6RHSNSMAsKc2Vtx3w9E/gGv1TI0quemGmpINzjwXFYRpWvpxUdoqOG5edasilsmO/c+CwQoQ/jubkrsUBz6gU/QdJLfAtKFNxKiJLrt+FWFyNC54gStv6vj4Ddh/e7bqdzMM= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1676036652200458.42922360228215; Fri, 10 Feb 2023 05:44:12 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pQTgk-0007rb-D3; Fri, 10 Feb 2023 08:43:42 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pQRYA-0003cP-Ul for qemu-devel@nongnu.org; Fri, 10 Feb 2023 06:26:42 -0500 Received: from smtp-out2.suse.de ([2001:67c:2178:6::1d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pQRY6-00033g-7E for qemu-devel@nongnu.org; Fri, 10 Feb 2023 06:26:41 -0500 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 98F316752D; Fri, 10 Feb 2023 11:26:32 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 4BCAE1325E; Fri, 10 Feb 2023 11:26:32 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id TRrwDugp5mM3aQAAMHmgww (envelope-from ); Fri, 10 Feb 2023 11:26:32 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1676028392; h=from:from:reply-to: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=rPO7Tl5ZAji92Jbza+uQUlcMvktjS3fjWKiTWL5j7g4=; b=N/KCXa/W8Tbj7axofwqyfOqjM1Ues5oEo2SlSiRGFsbQbCZMra4g0AJNBu9bECivfzCZXn MWpdajzVA1KQGLeDPnAMllap8wIosiRXaKhVV5d8EidcweEBrCgJ1MbuHNV0bYsV7aOisX 5XawODh/lYnHD1HpbHRAoDUyVVMwui4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1676028392; h=from:from:reply-to: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=rPO7Tl5ZAji92Jbza+uQUlcMvktjS3fjWKiTWL5j7g4=; b=rbAAouO3MbP1b4OPm2WktKL4q4OsvSWnACKKPDDGloA50V0Fo27no0/NziaiIPRT4o3lLd b/ZODcAqk0vgbUDg== From: =?UTF-8?q?Carlos=20L=C3=B3pez?= To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Carlos=20L=C3=B3pez?= , "Michael S. Tsirkin" Subject: [PATCH] libvhost-user: check for NULL when allocating a virtqueue element Date: Fri, 10 Feb 2023 12:25:15 +0100 Message-Id: <20230210112514.16858-1-clopez@suse.de> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=lists.gnu.org; Received-SPF: pass client-ip=2001:67c:2178:6::1d; envelope-from=clopez@suse.de; helo=smtp-out2.suse.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Fri, 10 Feb 2023 08:43:40 -0500 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @suse.de) X-ZM-MESSAGEID: 1676036652950100002 Check the return value for malloc(), avoiding a NULL pointer dereference, and propagate error in function callers. Found with GCC 13 and -fanalyzer: ../subprojects/libvhost-user/libvhost-user.c: In function =E2=80=98virtqueu= e_alloc_element=E2=80=99: ../subprojects/libvhost-user/libvhost-user.c:2556:19: error: dereference of= possibly-NULL =E2=80=98elem=E2=80=99 [CWE-690] [-Werror=3Danalyzer-possibl= e-null-dereference] 2556 | elem->out_num =3D out_num; | ~~~~~~~~~~~~~~^~~~~~~~~ =E2=80=98virtqueue_alloc_element=E2=80=99: event 1 | | 2554 | assert(sz >=3D sizeof(VuVirtqElement)); | | ^~~~~~ | | | | | (1) following =E2=80=98true=E2=80=99 branch (when =E2=80= =98sz > 31=E2=80=99)... | =E2=80=98virtqueue_alloc_element=E2=80=99: events 2-4 | | 2555 | elem =3D malloc(out_sg_end); | | ^~~~ ~~~~~~~~~~~~~~~~~~ | | | | | | | (3) this call could return NULL | | (2) ...to here | 2556 | elem->out_num =3D out_num; | | ~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (4) =E2=80=98elem=E2=80=99 could be NULL: un= checked value from (3) | Signed-off-by: Carlos L=C3=B3pez --- subprojects/libvhost-user/libvhost-user.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index fc69783d2b..225bbbe3c8 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -2553,6 +2553,10 @@ virtqueue_alloc_element(size_t sz, =20 assert(sz >=3D sizeof(VuVirtqElement)); elem =3D malloc(out_sg_end); + if (!elem) { + DPRINT("%s: failed to malloc virtqueue element\n", __func__); + return NULL; + } elem->out_num =3D out_num; elem->in_num =3D in_num; elem->in_sg =3D (void *)elem + in_sg_ofs; @@ -2639,6 +2643,9 @@ vu_queue_map_desc(VuDev *dev, VuVirtq *vq, unsigned i= nt idx, size_t sz) =20 /* Now copy what we have collected and mapped */ elem =3D virtqueue_alloc_element(sz, out_num, in_num); + if (!elem) { + return NULL; + } elem->index =3D idx; for (i =3D 0; i < out_num; i++) { elem->out_sg[i] =3D iov[i]; --=20 2.35.3