From patchwork Fri Jul 14 11:00:48 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= X-Patchwork-Id: 9840565 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 10D9160212 for ; Fri, 14 Jul 2017 11:01:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0384528752 for ; Fri, 14 Jul 2017 11:01:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EC6002876E; Fri, 14 Jul 2017 11:01:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5689A28752 for ; Fri, 14 Jul 2017 11:01:47 +0000 (UTC) Received: from localhost ([::1]:36993 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVyLy-0005ov-Mh for patchwork-qemu-devel@patchwork.kernel.org; Fri, 14 Jul 2017 07:01:46 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55414) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVyLM-0005nv-Uw for qemu-devel@nongnu.org; Fri, 14 Jul 2017 07:01:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dVyLG-0002Yz-Uy for qemu-devel@nongnu.org; Fri, 14 Jul 2017 07:01:09 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40814) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dVyLC-0002X6-Az; Fri, 14 Jul 2017 07:00:58 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D93343B70C; Fri, 14 Jul 2017 11:00:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com D93343B70C Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=berrange@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com D93343B70C Received: from redhat.com (ovpn-117-13.ams2.redhat.com [10.36.117.13]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 31B316C404; Fri, 14 Jul 2017 11:00:53 +0000 (UTC) Date: Fri, 14 Jul 2017 12:00:48 +0100 From: "Daniel P. Berrange" To: qemu-devel@nongnu.org, qemu-block@nongnu.org Message-ID: <20170714110048.GC28095@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.8.3 (2017-05-23) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 14 Jul 2017 11:00:57 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] qcow2 not cleaning up during image create failure 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: , Reply-To: "Daniel P. Berrange" Cc: Kevin Wolf Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP I've just been looking at the qcow2 image creation code, and found that if any method in qcow2_create2() returns an error, then we'll report that, but leave the newly created image file on disk in some partially initialized state. A user may unwittingly use this file later with undefined behaviour. This is particularly bad if we fail to setup encryption, because the user is left with a file with no encryption enabled. So I'm wondering how is the best way to clean up after failure ? Naively I would like to just unlink(filename), but IIUC, filename is not guaranteed to refer to a local file, and AFAIK, there is not bdrv_delete() method todo this portably. If we can't delete a file (because its a block device or network volume), then we must at least blank out the just-written qcow2 header with zeros. Ideas / suggestions. You can demo this easily, by adding ret = -EINVAL; immediately after the first blk_pwrite() call in qcow2_create2() $ qemu-img create -f qcow2 eek.img 1g Formatting 'eek.img', fmt=qcow2 size=1073741824 cluster_size=65536 lazy_refcounts=off refcount_bits=16 qemu-img: eek.img: Could not write qcow2 header: Invalid argument $ qemu-img info eek.img image: eek.img file format: qcow2 virtual size: 0 (0 bytes) disk size: 64K cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false I imagine this may well affect other disk format drivers too. Regards, Daniel diff --git a/block/qcow2.c b/block/qcow2.c index 75d3e3c731..205c924f6d 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2778,6 +2778,7 @@ static int qcow2_create2(const char *filename, int64_t total_size, ret = blk_pwrite(blk, 0, header, cluster_size, 0); g_free(header); + ret = -EINVAL; if (ret < 0) { error_setg_errno(errp, -ret, "Could not write qcow2 header"); goto out;