From patchwork Sun Jul 9 17:06:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 107238 Delivered-To: patches@linaro.org Received: by 10.140.101.44 with SMTP id t41csp2476796qge; Sun, 9 Jul 2017 10:06:15 -0700 (PDT) X-Received: by 10.28.175.212 with SMTP id y203mr5207841wme.2.1499619975718; Sun, 09 Jul 2017 10:06:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1499619975; cv=none; d=google.com; s=arc-20160816; b=0uSCs9QJNb4V1fkS1a+ybX57k1cBxbq2Ame/W7TdGhc1ZTjWMMVG/U+PnCs4e3ilob va0rdlWBddP1ec/LeHs/zDRsrLVwrsCbyxitBb8x00Eq91IgqSwG88A0lzRY2vfizman kNOtlKRzXuzgAJIkMgNThIgtVbQuEN4UXZ4Fxi5QqiheNQ7II9ZlPG70VNZuz/f98Tyi PXG7sqlZrc/W1HObgGizs4l5orIzq1mxW4BtbwsOCHvJMgCuf52Eox/FTxpkKuxB4yVv 7HCuFL6X+JZRC1difNlNs1OXLl2yHsDx5hXdfMAaeUj+BID4/FBjPmyNIsR1YUmJWC1L SnTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:arc-authentication-results; bh=lAdoCaIBsIh+q7p/N2ZToIz5rpmbcARly29FPExwB8g=; b=n0UHUgUG+tSsWKNs7xJQ8mEIsjtIBMwuuQrHHATCVaVZ9bRzDRSXDsVXhSz83smVvO nM7359UOg/Ndfj8rACPmgwCvy/aA9litiD636p82NZRUlbZmt61WOvv9zH2nDoNebcMq kBOsc2A03Bk8iF1KO2QEpnwhZ/rBoctKiOkbWz+ubYAWcITtpjbZWdYgFClPAIwz3+Ye j4tt92fI+7xuPF05t3U4xH2zL+3Ny65c3Im15ojDuJU08RrhBR7lD4d03E5NocUXEtSf KxKtifeofoya1qlpFM4rJvnsaPPA7KnuipG1W1YYP1NVPvjtwdM5owt7s3WkO4rERxbR FqFQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 2001:8b0:1d0::2 as permitted sender) smtp.mailfrom=pm215@archaic.org.uk; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by mx.google.com with ESMTPS id a3si4684750wma.180.2017.07.09.10.06.15 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 09 Jul 2017 10:06:15 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 2001:8b0:1d0::2 as permitted sender) client-ip=2001:8b0:1d0::2; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 2001:8b0:1d0::2 as permitted sender) smtp.mailfrom=pm215@archaic.org.uk; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org 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 Cc: patches@linaro.org, qemu-block@nongnu.org, Max Reitz , Kevin Wolf , Fam Zheng Subject: [PATCH] block/vmdk: Report failures in vmdk_read_cid() 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 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 --- block/vmdk.c | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) -- 2.11.0 Reviewed-by: Fam Zheng 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 *bs) s->extents = g_renew(VmdkExtent, s->extents, s->num_extents); } -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 = 0xffffffff; + uint32_t cid; const char *p_name, *cid_str; size_t cid_str_size; BDRVVmdkState *s = bs->opaque; @@ -254,8 +255,7 @@ static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent) desc = g_malloc0(DESC_SIZE); ret = bdrv_pread(bs->file, s->desc_offset, desc, DESC_SIZE); if (ret < 0) { - g_free(desc); - return 0; + goto out; } if (parent) { @@ -268,13 +268,21 @@ static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent) desc[DESC_SIZE - 1] = '\0'; p_name = strstr(desc, cid_str); - if (p_name != NULL) { - p_name += cid_str_size; - sscanf(p_name, "%" SCNx32, &cid); + if (p_name == NULL) { + ret = -EINVAL; + goto out; } + p_name += cid_str_size; + if (sscanf(p_name, "%" SCNx32, &cid) != 1) { + ret = -EINVAL; + goto out; + } + *pcid = cid; + ret = 0; +out: g_free(desc); - return cid; + return ret; } 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 = bs->backing->bs; - cur_pcid = vmdk_read_cid(p_bs, 0); + if (vmdk_read_cid(p_bs, 0, &cur_pcid) != 0) { + /* read failure: report as not valid */ + return 0; + } if (s->parent_cid != cur_pcid) { /* CID not valid */ return 0; @@ -975,8 +986,14 @@ static int vmdk_open(BlockDriverState *bs, QDict *options, int flags, if (ret) { goto fail; } - s->cid = vmdk_read_cid(bs, 0); - s->parent_cid = vmdk_read_cid(bs, 1); + ret = vmdk_read_cid(bs, 0, &s->cid); + if (ret) { + goto fail; + } + ret = vmdk_read_cid(bs, 1, &s->parent_cid); + if (ret) { + goto fail; + } qemu_co_mutex_init(&s->lock); /* Disable migration when VMDK images are used */ @@ -2007,8 +2024,11 @@ static int vmdk_create(const char *filename, QemuOpts *opts, Error **errp) ret = -EINVAL; goto exit; } - parent_cid = vmdk_read_cid(blk_bs(blk), 0); + ret = vmdk_read_cid(blk_bs(blk), 0, &parent_cid); blk_unref(blk); + if (ret) { + goto exit; + } snprintf(parent_desc_line, BUF_SIZE, "parentFileNameHint=\"%s\"", backing_file); }