From nobody Tue Nov 11 17:26:17 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1565187737; cv=none; d=zoho.com; s=zohoarc; b=hz5tKy/dTO3WSTr8UdWqulkBXj3wXVvPeCxb+NJKYT5gi7Re5SphVi1rlzVfp9mVBlJeISsTy1sLKwIQ57I1ujk/ePFXthnGcefuCJjA/QVb9vadGeayV19GgNGh8Myc+U3pmVUKwPW4Scy1iLe2tAEmtCeGj4bgtmearK/40VQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1565187737; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=kkvGcqaG6Hw21CZ6CMR4F3QU5tZjnOVQB5zjN4lycZY=; b=V4/zTXPMAxHh2xOpAtflSF1zqEJ5pdVWC/z9X6aks1/2SrTetpLmuMDpuRNIXfE0HBSG4npoqs+xUg2r902PSm9qyhh62X0ikxwMXdenCIbDpdVZt4yaxvgRjwj2JXRjxAAe+x4tJ5s/JqQZ2EKQ5xTBRWtBz7N+x+Po36TGcf8= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1565187737941192.67158052417267; Wed, 7 Aug 2019 07:22:17 -0700 (PDT) Received: from localhost ([::1]:41764 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMpV-0007NX-1s for importer@patchew.org; Wed, 07 Aug 2019 10:22:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39003) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMom-0005xO-2V for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hvMol-0008Vo-19 for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:32 -0400 Received: from mail-qk1-x743.google.com ([2607:f8b0:4864:20::743]:40251) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hvMok-0008VS-Tc for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:30 -0400 Received: by mail-qk1-x743.google.com with SMTP id s145so65919906qke.7 for ; Wed, 07 Aug 2019 07:21:30 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c7:5ef4:7266:b7a7:9def:f8fc]) by smtp.gmail.com with ESMTPSA id e18sm31472034qkm.49.2019.08.07.07.21.28 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 07 Aug 2019 07:21:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kkvGcqaG6Hw21CZ6CMR4F3QU5tZjnOVQB5zjN4lycZY=; b=ND0QVaF0LBULJ/dWcaXXlRMEYtojW4/DZ1Q3fBgV86/PuowwVee2vxF88gl8kVp9yl s7ya5tw64Txcys4KX+0H80xtnEyJAMP+KYJ665DEJ88x2LjtNF3LGMIIvZLAD/2maxAM lC1uJ0zJrh3gr80EvIlMtj0fpiiCJJYGqduV1Fq1POL8xLn3Pmd3gCVBWuM1NDTRtnV+ MQhI8i8FkybEpBj/g4lRmtRwjtMBWKAStPa2byQIIudspdv2cdNSoqipMnT+Sz3koQaf Ti7pTyOuZSbHQwuL66euQeZDuippdleAdTuHDaop7XiV3hg8pWzboNqf1Bkh7fPGtFXp VegQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kkvGcqaG6Hw21CZ6CMR4F3QU5tZjnOVQB5zjN4lycZY=; b=bf0zXhrbSes/LawDyVlGKhtU+wslPm9AnayuhUiowvXW7+p10QmVaJxcsNnPt8Jmvt UzcojSo4mHLNB2BscXC/oz4o80Mii2zgX/UOMqmZFIJzS4ZzySh+dwGQTeQS+bYfbOCb Q+gAqNU+V3DmAtxDZAxr57zMYsGvCnoWEbc5l4s4dtZkxXUwwHrW6IF1f9ol7x3zBmQY A8iJVyjW/yxoxPQUKPxoZ4O2KrKn6PZmJw0lug7BMabptrCnZ5cZvHL2YC+k2JDvlRw3 +Wc/h+/M4I/l/JGWGVpYVBqzpD6kNyR9p8vCVnFMnziwwIi+Skexn/sxvCu1T4M3DXkc 7F0w== X-Gm-Message-State: APjAAAUR1nK/rjKPdy9n48iUPPE70SAn7jjf4+j0wVilKmImNIcjzPJB r5mWghy6D9NkryWkYUmoQj0fx8MF X-Google-Smtp-Source: APXvYqxfcw8jp43y7sZ9T3yjPDk86xADVFye5tOvbVvlO1RgZqocvxm5yuzPLzG1Tl+L9yc1Wqv5SQ== X-Received: by 2002:a37:bc03:: with SMTP id m3mr8307018qkf.199.1565187690319; Wed, 07 Aug 2019 07:21:30 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Wed, 7 Aug 2019 11:21:11 -0300 Message-Id: <20190807142114.17569-2-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190807142114.17569-1-danielhb413@gmail.com> References: <20190807142114.17569-1-danielhb413@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::743 Subject: [Qemu-devel] [PATCH v5 1/4] block: introducing 'bdrv_co_delete_file' interface X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jsnow@redhat.com, Daniel Henrique Barboza , berrange@redhat.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Adding to Block Drivers the capability of being able to clean up its created files can be useful in certain situations. For the LUKS driver, for instance, a failure in one of its authentication steps can leave files in the host that weren't there before. This patch adds the 'bdrv_co_delete_file' interface to block drivers and add it to the 'file' driver in file-posix.c.The implementation is given by 'raw_co_delete_file'. Suggested-by: Daniel P. Berrang=C3=A9 Signed-off-by: Daniel Henrique Barboza --- block/file-posix.c | 28 ++++++++++++++++++++++++++++ include/block/block_int.h | 6 ++++++ 2 files changed, 34 insertions(+) diff --git a/block/file-posix.c b/block/file-posix.c index 4479cc7ab4..278952d5a2 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -2376,6 +2376,33 @@ static int coroutine_fn raw_co_create_opts(const cha= r *filename, QemuOpts *opts, return raw_co_create(&options, errp); } =20 +/** + * Co-routine function that erases a regular file. + */ +static int coroutine_fn raw_co_delete_file(BlockDriverState *bs, + Error **errp) +{ + struct stat st; + int ret; + + if (!(stat(bs->filename, &st) =3D=3D 0) || !S_ISREG(st.st_mode)) { + ret =3D -ENOENT; + error_setg_errno(errp, -ret, "%s is not a regular file", + bs->filename); + goto done; + } + + ret =3D unlink(bs->filename); + if (ret < 0) { + ret =3D -errno; + error_setg_errno(errp, -ret, "Error when deleting file %s", + bs->filename); + } + +done: + return ret; +} + /* * Find allocation range in @bs around offset @start. * May change underlying file descriptor's file offset. @@ -2927,6 +2954,7 @@ BlockDriver bdrv_file =3D { .bdrv_co_block_status =3D raw_co_block_status, .bdrv_co_invalidate_cache =3D raw_co_invalidate_cache, .bdrv_co_pwrite_zeroes =3D raw_co_pwrite_zeroes, + .bdrv_co_delete_file =3D raw_co_delete_file, =20 .bdrv_co_preadv =3D raw_co_preadv, .bdrv_co_pwritev =3D raw_co_pwritev, diff --git a/include/block/block_int.h b/include/block/block_int.h index 3aa1e832a8..4cb3232dc4 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -309,6 +309,12 @@ struct BlockDriver { */ int coroutine_fn (*bdrv_co_flush)(BlockDriverState *bs); =20 + /* + * Delete a local created file. + */ + int coroutine_fn (*bdrv_co_delete_file)(BlockDriverState *bs, + Error **errp); + /* * Flushes all data that was already written to the OS all the way dow= n to * the disk (for example file-posix.c calls fsync()). --=20 2.21.0 From nobody Tue Nov 11 17:26:17 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1565187795; cv=none; d=zoho.com; s=zohoarc; b=V+CjE/GgrWV+YaW8qeB7a1kw9UAHEHLOJKMzSbtfj3/6M7WQiPdAe/Xz0KC2gMbb2TCZ6jUndOw30QYnKpYzAczeOgD5RpgvqFjFkArhYsuGGxFTUe2bSVkAwuCNuWOH5uiQLoqLDmUZK9avIImYe+31Ya3dm/uJN6fzzBGuWwo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1565187795; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=9epP7UsNCqbjxzCDIUUM8j75jBuiFJRy3qqtGTzHLVk=; b=AITulE3kgQc2/dkFqg+HhKQzn2Yfh3/qqKDQxcp/cMjltKQwCvM4cPRMN3aBybl1Ry8dewykGpPxRywgteNXkL/yVqFrrqd7VbYBr0qk3IYlhJq0KH1Nw2JG4tIls/hBi1bwbvIUDyqEMRq3C4B4YubUxu/7kZVBuXRuj98yVVc= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1565187795096693.9372194238446; Wed, 7 Aug 2019 07:23:15 -0700 (PDT) Received: from localhost ([::1]:41790 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMqQ-0001Hl-4M for importer@patchew.org; Wed, 07 Aug 2019 10:23:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39018) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMoo-00061r-O0 for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hvMon-00005D-6N for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:34 -0400 Received: from mail-qk1-x741.google.com ([2607:f8b0:4864:20::741]:33998) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hvMon-000054-2T for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:33 -0400 Received: by mail-qk1-x741.google.com with SMTP id t8so65900057qkt.1 for ; Wed, 07 Aug 2019 07:21:33 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c7:5ef4:7266:b7a7:9def:f8fc]) by smtp.gmail.com with ESMTPSA id e18sm31472034qkm.49.2019.08.07.07.21.30 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 07 Aug 2019 07:21:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9epP7UsNCqbjxzCDIUUM8j75jBuiFJRy3qqtGTzHLVk=; b=HX4HWE6/5/Ks2Ubcp/8NLOuMnwuvPC5rrmOv37ezmUJy4SF2Ezz3HVUhhkJJ3OLOsc fFvXjZgp4qh3sKRaiqbRyHAqMn2sd5rM4LamrRjWuIUvGyEfGUkjbpa+Q8FWqvDl8pe2 fG4L/SAQoVRn7/gZ2mbogNjMd9ZqzB0Nbaa4SrYZ7TkzlTKu4F8yK5md3e+kXBRE2nLo nxhOKVWjUQrqf+trTDvMyKNip2VeA1je1GbNOXDqMZuFHue3Gjs9d6cW+w+9v14EGDww HmkE2MaYe7OsNu+NUuxJbCIUs7zYcQOxa/AAiuRAswozirzvoB5FvOr5hYypAQKPTVMZ kl0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9epP7UsNCqbjxzCDIUUM8j75jBuiFJRy3qqtGTzHLVk=; b=VkzgLWMuk8/Q1SZRtczS2VLu6r4ajRYx5yWG63kTGSqtmyLmNk+c0n9Lb4mGLbxIzG ia8Srn3Y8jTiC0yd+YpDePAFQMyEwgsmHHYrDQ9GYWWVsovtDknTY3yUR/0PWgNq2sX1 z535MS+HFrCbWDko2sVuHtwSHnFvhcIRsxy5uUIq8x8MuQ/n/tPNcJQZd224+TtgNQ58 m+jXw4My/YVFZDC/qEguZZyKtJzUzukvkxEiF5LwTyiyIOmWO3RlhPQyGlM+CVRzl9aV k5Qs76AzZ5T5hFC0Mgkf6OPvJ/LPv/F/pMj9vrkGKd2WkAMQ6Gcihbt00YullTcAD/vy seTA== X-Gm-Message-State: APjAAAXpDrRndUWzkGKilJkWdaw1l9RDKyUOpTgUyVf7/XJJrK7iYGwM vvJ9QeLpTGMIX9VsKDD36Z6S+An5 X-Google-Smtp-Source: APXvYqznVeecbHpwuNVYJxe6IIPtNIigC44qNTn0/sb1tpUMlJrsuO7MV+ctZiPp+1ffC0KdCehwJw== X-Received: by 2002:a05:620a:10bc:: with SMTP id h28mr8350840qkk.289.1565187692452; Wed, 07 Aug 2019 07:21:32 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Wed, 7 Aug 2019 11:21:12 -0300 Message-Id: <20190807142114.17569-3-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190807142114.17569-1-danielhb413@gmail.com> References: <20190807142114.17569-1-danielhb413@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::741 Subject: [Qemu-devel] [PATCH v5 2/4] block.c: adding bdrv_delete_file X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jsnow@redhat.com, Daniel Henrique Barboza , berrange@redhat.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Using the new 'bdrv_co_delete_file' interface, bdrv_delete_file can be used in a way similar of the existing bdrv_create_file to to clean up a created file. The logic is also similar to what is already done in bdrv_create_file: a qemu_coroutine is created if needed, a specialized function bdrv_delete_co_entry is used to call the bdrv_co_delete_file co-routine of the driver, if the driver implements it. Suggested-by: Daniel P. Berrang=C3=A9 Signed-off-by: Daniel Henrique Barboza --- block.c | 77 +++++++++++++++++++++++++++++++++++++++++++ include/block/block.h | 1 + 2 files changed, 78 insertions(+) diff --git a/block.c b/block.c index cbd8da5f3b..1e20250627 100644 --- a/block.c +++ b/block.c @@ -547,6 +547,83 @@ int bdrv_create_file(const char *filename, QemuOpts *o= pts, Error **errp) return ret; } =20 +typedef struct DeleteCo { + BlockDriver *drv; + BlockDriverState *bs; + int ret; + Error *err; +} DeleteCo; + +static void coroutine_fn bdrv_delete_co_entry(void *opaque) +{ + Error *local_err =3D NULL; + DeleteCo *dco =3D opaque; + + assert(dco->bs); + + dco->ret =3D dco->drv->bdrv_co_delete_file(dco->bs, &local_err); + error_propagate(&dco->err, local_err); +} + +int bdrv_delete_file(const char *filename, Error **errp) +{ + BlockDriver *drv =3D bdrv_find_protocol(filename, true, NULL); + BlockDriverState *bs =3D bdrv_open(filename, NULL, NULL, + BDRV_O_RDWR | BDRV_O_PROTOCOL, NULL); + DeleteCo dco =3D { + .drv =3D drv, + .bs =3D bs, + .ret =3D NOT_DONE, + .err =3D NULL, + }; + Coroutine *co; + int ret; + + if (!drv) { + error_setg(errp, "File '%s' has unknown format", filename); + ret =3D -ENOENT; + goto out; + } + + if (!drv->bdrv_co_delete_file) { + error_setg(errp, "Driver '%s' does not support image delete", + drv->format_name); + ret =3D -ENOTSUP; + goto out; + } + + if (!bs) { + error_setg(errp, "Could not open image '%s' for erasing", + filename); + ret =3D 1; + goto out; + } + + if (qemu_in_coroutine()) { + /* Fast-path if already in coroutine context */ + bdrv_delete_co_entry(&dco); + } else { + co =3D qemu_coroutine_create(bdrv_delete_co_entry, &dco); + qemu_coroutine_enter(co); + while (dco.ret =3D=3D NOT_DONE) { + aio_poll(qemu_get_aio_context(), true); + } + } + + ret =3D dco.ret; + if (ret < 0) { + if (dco.err) { + error_propagate(errp, dco.err); + } else { + error_setg_errno(errp, -ret, "Could not delete image"); + } + } + +out: + bdrv_unref(bs); + return ret; +} + /** * Try to get @bs's logical and physical block size. * On success, store them in @bsz struct and return 0. diff --git a/include/block/block.h b/include/block/block.h index 50a07c1c33..5e83532364 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -369,6 +369,7 @@ bool bdrv_is_backing_chain_frozen(BlockDriverState *bs,= BlockDriverState *base, int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base, Error **errp); void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *b= ase); +int bdrv_delete_file(const char *filename, Error **errp); =20 =20 typedef struct BdrvCheckResult { --=20 2.21.0 From nobody Tue Nov 11 17:26:17 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1565187793; cv=none; d=zoho.com; s=zohoarc; b=avXF4rMjwRWY6mENWZS4Pig4gnAbDrpARefJSFf8t6KTmJDe7xlRCppcFJetPDvc/Jx1QgflVTFN2vmhjtxznWA+mx3Cb3vq67/8wgCSECY5oUSPySHVGFH31hncbPPIDWktU6e6djI+DNWcqadilUMI6vkNA6G0ioECJg7XBlM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1565187793; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=dGfD+OKaAJtklYlmiZJfVWpoqCv42S59cVjzwBeRJNs=; b=NHYDRhgRiCoL8e95Zf40UCyN4/LAewP+pnhKsZSdFxGz0giVhxP3NPF2OVfSX5MzXV9d3YMd3U5l/eV9dfievujR/71B9/k4/ef+yMi1D3rSwOTozX+y22o46qtqZuXXSgFrHJhck2ADvttH9pA/mzAoYMchqtjHjsPnNpmb9A8= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 156518779388157.22075992488715; Wed, 7 Aug 2019 07:23:13 -0700 (PDT) Received: from localhost ([::1]:41788 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMqO-0001E1-VT for importer@patchew.org; Wed, 07 Aug 2019 10:23:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39042) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMoq-00067D-FY for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hvMop-000076-AA for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:36 -0400 Received: from mail-qt1-x842.google.com ([2607:f8b0:4864:20::842]:34685) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hvMop-00006x-5q for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:35 -0400 Received: by mail-qt1-x842.google.com with SMTP id k10so19333533qtq.1 for ; Wed, 07 Aug 2019 07:21:35 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c7:5ef4:7266:b7a7:9def:f8fc]) by smtp.gmail.com with ESMTPSA id e18sm31472034qkm.49.2019.08.07.07.21.32 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 07 Aug 2019 07:21:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dGfD+OKaAJtklYlmiZJfVWpoqCv42S59cVjzwBeRJNs=; b=fLaUpvx+YTXTwSrSe/iAG0Z7uSXo7Wcgu4E24TBm/UxIfq8POpcJqdfiuTRvX9qj0T D6klQjWfclW1ugpsxa90cjxRH0UPoXToibXlZ97+bCAQx7cmIGjNmyqFFPvS8lnPtbjy b4+DGYcMBePyMOjTjskvRJHvyVldEICdbuAFToUgeejyhsnHf7V94VFD+V1ry73cODxz TAyoze6y3dn2h2aaCTjoYox6oOlUSP42XTbF09Nk+k8cxhUn+6/m69zosYdYgQaGVUsL dQsR3wilDq4pkcqENBDtFBw50dmExmY/ylQ9auzzSIRuU0DUgC0unwCOw1LCRelYWxPb JEAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=dGfD+OKaAJtklYlmiZJfVWpoqCv42S59cVjzwBeRJNs=; b=Or33CUo2rDvdoQRiylAv7dCICyKOTQ6BgTri14+9sLQaABzLu8NxIW7RPr5ajdDK+E m2NksVpkwQq3rT7hOTgrC60zmeB09c3PVlwzlFOxhqzkA6NOwO9CoUVkSaX9B21v4PIQ ObyGeh4N8DscsaNTP3IWsZ0NiHIQkaoe1WVjnHoYGoUUWerzdFl9gHoM0QzBd82i8uBm 0HGeG6rP+Y1tCMR+UnxvqvLRBRiS4fZzUMY1myJ0KNqfWrk5ghQiPXU/tHiYAwf4VWJ0 MOk3uYn7MRd7UqwQWQEckX5hjS+Lbgclc3rDm5RtbrbkPOBuWS/Qy/OZwoBzj2Ek/tzb oWPw== X-Gm-Message-State: APjAAAVOWPuK6otuE2EJ4+RJYwdoRKmgoXE9Q+Xh2XFGd8bH8opdbGME 7cIKLPkAp7YSijw0tYmBsFPpULFi X-Google-Smtp-Source: APXvYqwDxuCSHvRbxW42whOvF1eLZbuBwmtBfH2LmZ7I+ICRCKOilf7wV2xsxC1VIRmeqhibqa10IA== X-Received: by 2002:ac8:c0e:: with SMTP id k14mr8248597qti.72.1565187694567; Wed, 07 Aug 2019 07:21:34 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Wed, 7 Aug 2019 11:21:13 -0300 Message-Id: <20190807142114.17569-4-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190807142114.17569-1-danielhb413@gmail.com> References: <20190807142114.17569-1-danielhb413@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::842 Subject: [Qemu-devel] [PATCH v5 3/4] crypto.c: cleanup created file when block_crypto_co_create_opts_luks fails X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, berrange@redhat.com, Daniel Henrique Barboza , mreitz@redhat.com, Srikanth Aithal , jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" When using a non-UTF8 secret to create a volume using qemu-img, the following error happens: $ qemu-img create -f luks --object secret,id=3Dvol_1_encrypt0,file=3Dvol_re= size_pool.vol_1.secret.qzVQrI -o key-secret=3Dvol_1_encrypt0 /var/tmp/pool_= target/vol_1 10240K Formatting '/var/tmp/pool_target/vol_1', fmt=3Dluks size=3D10485760 key-sec= ret=3Dvol_1_encrypt0 qemu-img: /var/tmp/pool_target/vol_1: Data from secret vol_1_encrypt0 is no= t valid UTF-8 However, the created file '/var/tmp/pool_target/vol_1' is left behind in the file system after the failure. This behavior can be observed when creating the volume using Libvirt, via 'virsh vol-create', and then getting "volume target path already exist" errors when trying to re-create the volume. The volume file is created inside block_crypto_co_create_opts_luks(), in block/crypto.c. If the bdrv_create_file() call is successful but any succeeding step fails*, the existing 'fail' label does not take into account the created file, leaving it behind. This patch changes block_crypto_co_create_opts_luks() to delete 'filename' in case of failure. A failure in this point means that the volume is now truncated/corrupted, so even if 'filename' was an existing volume before calling qemu-img, it is now unusable. Deleting the file it is not much worse than leaving it in the filesystem in this scenario, and we don't have to deal with checking the file pre-existence in the code. * in our case, block_crypto_co_create_generic calls qcrypto_block_create, which calls qcrypto_block_luks_create, and this function fails when calling qcrypto_secret_lookup_as_utf8. Reported-by: Srikanth Aithal Suggested-by: Kevin Wolf Signed-off-by: Daniel Henrique Barboza --- block/crypto.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/block/crypto.c b/block/crypto.c index 8237424ae6..8ffca81df6 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -30,6 +30,7 @@ #include "qapi/error.h" #include "qemu/module.h" #include "qemu/option.h" +#include "qemu/cutils.h" #include "crypto.h" =20 typedef struct BlockCrypto BlockCrypto; @@ -575,6 +576,25 @@ fail: bdrv_unref(bs); qapi_free_QCryptoBlockCreateOptions(create_opts); qobject_unref(cryptoopts); + + /* + * If an error occurred, delete the file. Even if the file existed + * beforehand, it has been truncated and corrupted in the process. + */ + if (ret) { + Error *local_err; + int r_del =3D bdrv_delete_file(filename, &local_err); + /* + * ENOTSUP will happen if the block driver doesn't support + * 'bdrv_co_delete_file'. ENOENT will happen if the file + * doesn't exist. Both are predictable and shouldn't be + * reported back to the user. + */ + if ((r_del < 0) && (r_del !=3D -ENOTSUP) && (r_del !=3D -ENOENT)) { + error_reportf_err(local_err, "%s: ", bs->filename); + } + } + return ret; } =20 --=20 2.21.0 From nobody Tue Nov 11 17:26:17 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1565187744; cv=none; d=zoho.com; s=zohoarc; b=EKErUV5W7MUe7pT0NSRrwZkZwttc0XgX+36hQc7yR/kL4wqSvr9NB2wLdgKVP3ri6lGwTB3R+Og+URtoAV7z14TcAQF9zXm741YBOp+IHPvXHqh0dFkR1Ts2+KVCqyDe5Ut2fMcyzrqf7wgnKViKsfS6A3747WhlDM16fNSOcaI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1565187744; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=NwJ4uWjVtM+26b3Wt3huTtV/qJaSrXNc+otrwh9eGR4=; b=GonsmkKAY2by05Zm8iwGBV2rPddPTqBNe3fCxeef4rmd8wi+c3txSiDXFlGOASNpQQ91Pa/l/wJ+unolMYijIfSnAZQkPd32QCt+nKhBxoLokzSo4vZzGyzkj9cl8fq/rVZM2pvvrfVu9nFvdvgv6aTgtilj73I6BLr3Ig3eZTE= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1565187744095514.3088878529575; Wed, 7 Aug 2019 07:22:24 -0700 (PDT) Received: from localhost ([::1]:41768 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMpb-0007jv-5t for importer@patchew.org; Wed, 07 Aug 2019 10:22:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39062) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMot-0006Ev-3S for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hvMor-00007y-Mi for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:39 -0400 Received: from mail-qt1-x82e.google.com ([2607:f8b0:4864:20::82e]:34417) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hvMor-00007o-Ip for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:37 -0400 Received: by mail-qt1-x82e.google.com with SMTP id k10so19333669qtq.1 for ; Wed, 07 Aug 2019 07:21:37 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c7:5ef4:7266:b7a7:9def:f8fc]) by smtp.gmail.com with ESMTPSA id e18sm31472034qkm.49.2019.08.07.07.21.34 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 07 Aug 2019 07:21:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NwJ4uWjVtM+26b3Wt3huTtV/qJaSrXNc+otrwh9eGR4=; b=eJoPLpFPnGtP6/gNG6bsKghxb7SVMFTEX8Gyfc7NOYGV56e5i4g3euyrzq863WzpEt YfYbtNyphMvv9Vr08aIl4mCkNi2cSrUCdinVMX59HgrlgwljxVxrwZ0/Y9sFqpP/fUR2 n4sCV/1HmoLJ0JK+4sFNFw6Ye/MYlmMscFGb3kcpF9BP8OKOgnPjTvtuzArsCEl9fU0R u7jCbbAn0om+CmN5WU7HWENdv7XINgGx9nisRda964IaM3FMoSjC6aimzX4iSjoPIcwM nmUdX8kUtSDokegc7rsafuxX4lFVnOXszK0rHiMOMEmjcZDOzgBFcxP2JnucasAihRl8 w34w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NwJ4uWjVtM+26b3Wt3huTtV/qJaSrXNc+otrwh9eGR4=; b=IrBUp9dB8dDd0kD6h4/6Lg4UZ2tFeCeFVYui6vdM0XCKj/FVgtuoZEi86S5k09veAo Ice2J6ycgsWEL6cY7WAzLgnNbOuc+hrvNEvmznXKvm5POf5wi5K36Iy+RSWBQaM8aJCV yaB9fs3fAIu5aVVVoVgylHxkMAxZA0IJEgbtuiGgct/D9tynIOubo9qpA+iIX5ldIEXL ScV8aVgnE78Rl33Ub9X9FLMiPHdR6qpm1LWOh6/J2D6FdKEdnPDzf9Zxg77P6J+bPzN+ 4fzbwophSZrEhfX8Eg24CbdBll2XxOQKG3GiKbJKruU1T1U54MA4QZ1L00IlsCt9Qpey nUdQ== X-Gm-Message-State: APjAAAUjF+tbu7PAr7cCueWXgFbXzl+QAsla1UMGTbHMLuHohU5y9Eoi uFboof0S5yW9TY+mxL2j4UMUCMgW X-Google-Smtp-Source: APXvYqw8vkfOvWKMox19ZE3JTlAn8g1D117j1IKGGwdItmwLHR+QNT9yb7R/S98STFLaqjUtWOYckg== X-Received: by 2002:ac8:2f43:: with SMTP id k3mr8416162qta.179.1565187696773; Wed, 07 Aug 2019 07:21:36 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Wed, 7 Aug 2019 11:21:14 -0300 Message-Id: <20190807142114.17569-5-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190807142114.17569-1-danielhb413@gmail.com> References: <20190807142114.17569-1-danielhb413@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::82e Subject: [Qemu-devel] [PATCH v5 4/4] qemu-iotests: adding LUKS cleanup for non-UTF8 secret error X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, jsnow@redhat.com, Daniel Henrique Barboza , berrange@redhat.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patch adds a new test file, 257, to exercise the case where qemu-img fails to complete for the LUKS format when a non-UTF8 secret is used. Signed-off-by: Daniel Henrique Barboza --- tests/qemu-iotests/257 | 67 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/257.out | 11 +++++++ tests/qemu-iotests/group | 1 + 3 files changed, 79 insertions(+) create mode 100755 tests/qemu-iotests/257 create mode 100644 tests/qemu-iotests/257.out diff --git a/tests/qemu-iotests/257 b/tests/qemu-iotests/257 new file mode 100755 index 0000000000..cb362598b4 --- /dev/null +++ b/tests/qemu-iotests/257 @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +# +# Test qemu-img file cleanup for LUKS when using a non-UTF8 secret +# +# Copyright (C) 2019, IBM Corporation. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +seq=3D`basename $0` +echo "QA output created by $seq" + +status=3D1 # failure is the default! +TEST_IMAGE_FILE=3D'vol.img' + +_cleanup() +{ + _cleanup_test_img + rm non_utf8_secret + rm -f $TEST_IMAGE_FILE +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +. ./common.rc +. ./common.filter + +_supported_fmt luks +_supported_proto generic +_unsupported_proto vxhs + +echo "=3D=3D Create non-UTF8 secret =3D=3D" +echo -n -e '\x3a\x3c\x3b\xff' > non_utf8_secret +SECRET=3D"secret,id=3Dsec0,file=3Dnon_utf8_secret" + +echo "=3D=3D Throws an error because of invalid UTF-8 secret =3D=3D" +$QEMU_IMG create -f $IMGFMT --object $SECRET -o "key-secret=3Dsec0" $TEST_= IMAGE_FILE 4M + +echo "=3D=3D Image file should not exist after the error =3D=3D" +if test -f "$TEST_IMAGE_FILE"; then + exit 1 +fi + +echo "=3D=3D Create a stub image file and run qemu-img again =3D=3D" +touch $TEST_IMAGE_FILE +$QEMU_IMG create -f $IMGFMT --object $SECRET -o "key-secret=3Dsec0" $TEST_= IMAGE_FILE 4M + +echo "=3D=3D Pre-existing image file should also be deleted after the erro= r =3D=3D" +if test -f "$TEST_IMAGE_FILE"; then + exit 1 +fi + +# success, all done +echo "*** done" +rm -f $seq.full +status=3D0 diff --git a/tests/qemu-iotests/257.out b/tests/qemu-iotests/257.out new file mode 100644 index 0000000000..3803933571 --- /dev/null +++ b/tests/qemu-iotests/257.out @@ -0,0 +1,11 @@ +QA output created by 257 +=3D=3D Create non-UTF8 secret =3D=3D +=3D=3D Throws an error because of invalid UTF-8 secret =3D=3D +qemu-img: vol.img: Data from secret sec0 is not valid UTF-8 +Formatting 'vol.img', fmt=3Dluks size=3D4194304 key-secret=3Dsec0 +=3D=3D Image file should not exist after the error =3D=3D +=3D=3D Create a stub image file and run qemu-img again =3D=3D +qemu-img: vol.img: Data from secret sec0 is not valid UTF-8 +Formatting 'vol.img', fmt=3Dluks size=3D4194304 key-secret=3Dsec0 +=3D=3D Pre-existing image file should also be deleted after the error =3D= =3D + *** done diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group index f13e5f2e23..ae7bb89642 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -271,3 +271,4 @@ 254 rw backing quick 255 rw quick 256 rw quick +257 rw auto quick --=20 2.21.0