From nobody Wed Nov 5 06:58:14 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499620165725639.9844661788776; Sun, 9 Jul 2017 10:09:25 -0700 (PDT) Received: from localhost ([::1]:36871 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dUFi0-0000YR-3p for importer@patchew.org; Sun, 09 Jul 2017 13:09:24 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41678) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dUFh6-0008RB-Eu for qemu-devel@nongnu.org; Sun, 09 Jul 2017 13:08:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dUFh2-0007VK-E4 for qemu-devel@nongnu.org; Sun, 09 Jul 2017 13:08:28 -0400 Received: from orth.archaic.org.uk ([81.2.115.148]:48405) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dUFh2-0007Uf-6G; Sun, 09 Jul 2017 13:08:24 -0400 Received: from mnementh.archaic.org.uk ([81.2.115.146]) by orth.archaic.org.uk with esmtp (Exim 4.84_2) (envelope-from ) id 1dUFew-0000n9-AR; Sun, 09 Jul 2017 18:06:14 +0100 Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1dUFew-0006VP-9i; Sun, 09 Jul 2017 18:06:14 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Date: Sun, 9 Jul 2017 18:06:14 +0100 Message-Id: <20170709170614.24967-1-peter.maydell@linaro.org> X-Mailer: git-send-email 2.11.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 81.2.115.148 Subject: [Qemu-devel] [PATCH] block/vmdk: Report failures in vmdk_read_cid() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Max Reitz , Fam Zheng , qemu-block@nongnu.org, patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The function vmdk_read_cid() can fail if the read on the underlying block device fails, or if there's a format error in the VMDK file. However its API doesn't provide a mechanism to report these errors, and in some cases we were returning a CID of 0 and in some cases a CID of 0xffffffff, either of which might potentially be valid values. Change the function to return 0 on success or a negative errno, and return the CID via a uint32_t* argument. Update the callsites to handle and propagate the error appropriately. This fixes in passing a Coverity-spotted issue (CID 1350038) where we weren't checking the return value from sscanf(). Signed-off-by: Peter Maydell Reviewed-by: Fam Zheng --- block/vmdk.c | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/block/vmdk.c b/block/vmdk.c index 55581b03fe..0c9949fc0c 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -242,10 +242,11 @@ static void vmdk_free_last_extent(BlockDriverState *b= s) s->extents =3D g_renew(VmdkExtent, s->extents, s->num_extents); } =20 -static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent) +/* Return -ve errno, or 0 on success and write CID into *pcid. */ +static int vmdk_read_cid(BlockDriverState *bs, int parent, uint32_t *pcid) { char *desc; - uint32_t cid =3D 0xffffffff; + uint32_t cid; const char *p_name, *cid_str; size_t cid_str_size; BDRVVmdkState *s =3D bs->opaque; @@ -254,8 +255,7 @@ static uint32_t vmdk_read_cid(BlockDriverState *bs, int= parent) desc =3D g_malloc0(DESC_SIZE); ret =3D bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE); if (ret < 0) { - g_free(desc); - return 0; + goto out; } =20 if (parent) { @@ -268,13 +268,21 @@ static uint32_t vmdk_read_cid(BlockDriverState *bs, i= nt parent) =20 desc[DESC_SIZE - 1] =3D '\0'; p_name =3D strstr(desc, cid_str); - if (p_name !=3D NULL) { - p_name +=3D cid_str_size; - sscanf(p_name, "%" SCNx32, &cid); + if (p_name =3D=3D NULL) { + ret =3D -EINVAL; + goto out; } + p_name +=3D cid_str_size; + if (sscanf(p_name, "%" SCNx32, &cid) !=3D 1) { + ret =3D -EINVAL; + goto out; + } + *pcid =3D cid; + ret =3D 0; =20 +out: g_free(desc); - return cid; + return ret; } =20 static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid) @@ -322,7 +330,10 @@ static int vmdk_is_cid_valid(BlockDriverState *bs) if (!s->cid_checked && bs->backing) { BlockDriverState *p_bs =3D bs->backing->bs; =20 - cur_pcid =3D vmdk_read_cid(p_bs, 0); + if (vmdk_read_cid(p_bs, 0, &cur_pcid) !=3D 0) { + /* read failure: report as not valid */ + return 0; + } if (s->parent_cid !=3D cur_pcid) { /* CID not valid */ return 0; @@ -975,8 +986,14 @@ static int vmdk_open(BlockDriverState *bs, QDict *opti= ons, int flags, if (ret) { goto fail; } - s->cid =3D vmdk_read_cid(bs, 0); - s->parent_cid =3D vmdk_read_cid(bs, 1); + ret =3D vmdk_read_cid(bs, 0, &s->cid); + if (ret) { + goto fail; + } + ret =3D vmdk_read_cid(bs, 1, &s->parent_cid); + if (ret) { + goto fail; + } qemu_co_mutex_init(&s->lock); =20 /* Disable migration when VMDK images are used */ @@ -2007,8 +2024,11 @@ static int vmdk_create(const char *filename, QemuOpt= s *opts, Error **errp) ret =3D -EINVAL; goto exit; } - parent_cid =3D vmdk_read_cid(blk_bs(blk), 0); + ret =3D vmdk_read_cid(blk_bs(blk), 0, &parent_cid); blk_unref(blk); + if (ret) { + goto exit; + } snprintf(parent_desc_line, BUF_SIZE, "parentFileNameHint=3D\"%s\"", backing_file); } --=20 2.11.0