From nobody Fri May 10 15:36:13 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580420628062770.0532455060459; Thu, 30 Jan 2020 13:43:48 -0800 (PST) Received: from localhost ([::1]:39552 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixHbH-0002A5-01 for importer@patchew.org; Thu, 30 Jan 2020 16:43:47 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59218) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixHWx-0003DH-LW for qemu-devel@nongnu.org; Thu, 30 Jan 2020 16:39:20 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ixHWw-0001N4-II for qemu-devel@nongnu.org; Thu, 30 Jan 2020 16:39:19 -0500 Received: from mail-qv1-xf42.google.com ([2607:f8b0:4864:20::f42]:34451) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ixHWw-0001LQ-E4; Thu, 30 Jan 2020 16:39:18 -0500 Received: by mail-qv1-xf42.google.com with SMTP id o18so2282227qvf.1; Thu, 30 Jan 2020 13:39:18 -0800 (PST) Received: from rekt.ibmuc.com ([2804:431:c7c7:c177:887c:ce1b:6a44:15ec]) by smtp.gmail.com with ESMTPSA id f26sm3650597qtv.77.2020.01.30.13.39.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jan 2020 13:39:17 -0800 (PST) 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=j3SBfARHb0Q5itXyfHtP6FdgEQPvIk7U2AMegh5GrHs=; b=srXJ75Nk0svSHAU/Lc0Ul5pfySABkeHQPg5gZPNdkvyb90NayJNDLpNIsPlaugjfNf OTPiqJztov018XNSHgfdYhhqr6ioOWyNIN3eaWOzq+geWlFdSq1Np80oEfuxfqxd0fha rn3Ae6eKj6wZ6ji8dSVekEw84D8GH1lqJIld0f4XEpZXj+aotwZinGHHokSMx0oCHNe6 Dh3qhIMoUCkOrc4Pb/LsZzaWeD8dCgbRP0XNc2ubq8sZ0zn6qEwtuvoY6vkbSzFCeW1v iwWauj5JtQRWSoRlz06Hx1UGnIW/IWCyfYDC5Q5lXESYkDTZYyvVbgzNPGKxDhkjl2sW j1HA== 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=j3SBfARHb0Q5itXyfHtP6FdgEQPvIk7U2AMegh5GrHs=; b=aSzYk+Kt32B8yeEpckm+RPzC9FxE5mgGoQdDuZ/T9ZRGoWhxhm1qeCJIiWXh+hmv8J wOV0FVuuyqVy51bv11ulIPqWhR16RTqtyV3TjZjiTi9U42zLKlTHFzXyUcQYxnn+gYrf AYZqU+gZYc9kSeovrn6PjdFqdc3ub48cGjcae5hbBrZNW7XPJ3ygPANpk42wpeGDZvth XkpRPAtiWnp/PKAiCg/BgzA3xYwAi6J1hOaEbsigYganvPhwyAicSyMTsauLqGt5KYLI a3NTmoCqI23rS3gSh+dZcoRZx/5UpWHBOJ+f9fJYgjsqJZRSjKE94Vc0asX3I9vhoD1H 7kBg== X-Gm-Message-State: APjAAAWAeBfxNf8d9MMWFEb1ItffcXMMgTRiwz+Ohp2KyEydeZFTLHjQ l/3RGhhdcVqekNh9GchHg5e/CIIC X-Google-Smtp-Source: APXvYqx6h0Z5ta9VcTE8ux329Xhz1rLueajd+IDTv8KMHQJWhflQNYUOmKn5phWaswkgupv+yThMBg== X-Received: by 2002:a0c:ea92:: with SMTP id d18mr7006622qvp.201.1580420357635; Thu, 30 Jan 2020 13:39:17 -0800 (PST) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Subject: [PATCH v9 1/4] block: introducing 'bdrv_co_delete_file' interface Date: Thu, 30 Jan 2020 18:39:04 -0300 Message-Id: <20200130213907.2830642-2-danielhb413@gmail.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200130213907.2830642-1-danielhb413@gmail.com> References: <20200130213907.2830642-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::f42 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, Daniel Henrique Barboza , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , qemu-block@nongnu.org, 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 | 23 +++++++++++++++++++++++ include/block/block_int.h | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/block/file-posix.c b/block/file-posix.c index 1b805bd938..ed28234bb8 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -2386,6 +2386,28 @@ static int coroutine_fn raw_co_create_opts(const cha= r *filename, QemuOpts *opts, return raw_co_create(&options, errp); } =20 +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)) { + error_setg_errno(errp, ENOENT, "%s is not a regular file", + bs->filename); + return -ENOENT; + } + + ret =3D unlink(bs->filename); + if (ret < 0) { + ret =3D -errno; + error_setg_errno(errp, -ret, "Error when deleting file %s", + bs->filename); + } + + return ret; +} + /* * Find allocation range in @bs around offset @start. * May change underlying file descriptor's file offset. @@ -3017,6 +3039,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 dd033d0b37..d938d3e8d2 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -314,6 +314,10 @@ struct BlockDriver { */ int coroutine_fn (*bdrv_co_flush)(BlockDriverState *bs); =20 + /* Delete a 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.24.1 From nobody Fri May 10 15:36:13 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580420810936962.114513523736; Thu, 30 Jan 2020 13:46:50 -0800 (PST) Received: from localhost ([::1]:39698 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixHeD-00071W-Rc for importer@patchew.org; Thu, 30 Jan 2020 16:46:49 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59240) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixHWz-0003J0-K4 for qemu-devel@nongnu.org; Thu, 30 Jan 2020 16:39:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ixHWy-0001RJ-IS for qemu-devel@nongnu.org; Thu, 30 Jan 2020 16:39:21 -0500 Received: from mail-qv1-xf44.google.com ([2607:f8b0:4864:20::f44]:35310) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ixHWy-0001R9-E0; Thu, 30 Jan 2020 16:39:20 -0500 Received: by mail-qv1-xf44.google.com with SMTP id u10so2278332qvi.2; Thu, 30 Jan 2020 13:39:20 -0800 (PST) Received: from rekt.ibmuc.com ([2804:431:c7c7:c177:887c:ce1b:6a44:15ec]) by smtp.gmail.com with ESMTPSA id f26sm3650597qtv.77.2020.01.30.13.39.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jan 2020 13:39:19 -0800 (PST) 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=Z3xKI0QCTIV3oPS9zRDoReVS6YYhkQI77L8IETXIfdg=; b=B7GB2iBuFzISiVhb4QxFC8Af11DE7CtFramT15hHTTFdQnneEBieH7zCLXK9lm9DED 3Hlzp14HSAAtJ6wSLYIkM4fSD+GzbxbZIRGXCC4jcVsLRlki6r8BUPS7yTKnw7iVTsqO KTiD+9WIhNJMT487Bz7pRAlCT/F4A9UzUiz21bXYqRbb7YVNewSng9siPpt0HILOsI47 9ujhL5LgwwQkzFHoI++Dm02norSWjxHBXvbNuHFmj8bDrnbaG3nbH1UVma2EbY2ClK3l GSfnTx+li2G8KkgzQXPG8ZXl1YrvV/1RDmFhj3FBR4Ja/UIJ+RtGxtQcAE0mYo9FNnfz r3HA== 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=Z3xKI0QCTIV3oPS9zRDoReVS6YYhkQI77L8IETXIfdg=; b=Vagv2trYrBuk9i5FjkhMGjM/Qa9szy7jazOQgDJV0y2rVyWphLa9zxx6OZBcKHLteH AatIr9Zqs43wFzpJS9Ds07pzYVdDXzoJMgCUpcOJdqQz0IzbTYSdy+THXw+Ree0E0vCG 3vvZaY5TbBCxz51J0JP/xRQOt9jAR6qJaHFAxiARXYOxpi40WRgtPuc90JP3MPLYr3A1 KKqrQMGO40jTolTcAV76Xd38DWkAmVW6YeZLui+/TNOUgWgw1+pRXvO7Te0tAXigSJTL vp8UsBdpDMDiBD6Mdenomw4qMUMd96kwnUTgZqouhjQwyysX6UrOYyfu0uC80EqHTwve gWKw== X-Gm-Message-State: APjAAAV61Xte9a15/wOsi9K13YDd3Wcg3h5zRrmUcHXjLOyq/Jq55W5t 8o8nT2rXJ6VOZPOmpGjIz/LLDgmx X-Google-Smtp-Source: APXvYqzc7yQ4eQ+CCQsq183Qbbmj05wEwZbF+FaxbKxm9/ZCHeKJY1rND6IqPwqspjCsHV2wpN1aSA== X-Received: by 2002:ad4:442c:: with SMTP id e12mr7071424qvt.19.1580420359799; Thu, 30 Jan 2020 13:39:19 -0800 (PST) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Subject: [PATCH v9 2/4] block.c: adding bdrv_co_delete_file Date: Thu, 30 Jan 2020 18:39:05 -0300 Message-Id: <20200130213907.2830642-3-danielhb413@gmail.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200130213907.2830642-1-danielhb413@gmail.com> References: <20200130213907.2830642-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::f44 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, Daniel Henrique Barboza , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , qemu-block@nongnu.org, 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, a pure co_routine function 'bdrv_co_delete_file' inside block.c can can be used in a way similar of the existing bdrv_create_file to to clean up a created file. We're creating a pure co_routine because the only caller of 'bdrv_co_delete_file' will be already in co_routine context, thus there is no need to add all the machinery to check for qemu_in_coroutine() and create a separated co_routine to do the job. Suggested-by: Daniel P. Berrang=C3=A9 Signed-off-by: Daniel Henrique Barboza --- block.c | 26 ++++++++++++++++++++++++++ include/block/block.h | 1 + 2 files changed, 27 insertions(+) diff --git a/block.c b/block.c index ecd09dbbfd..c26d8271a1 100644 --- a/block.c +++ b/block.c @@ -548,6 +548,32 @@ int bdrv_create_file(const char *filename, QemuOpts *o= pts, Error **errp) return ret; } =20 +int coroutine_fn bdrv_co_delete_file(BlockDriverState *bs, Error **errp) +{ + Error *local_err =3D NULL; + int ret; + + assert(bs !=3D NULL); + + if (!bs->drv) { + error_setg(errp, "Block node '%s' is not opened", bs->filename); + return -ENOMEDIUM; + } + + if (!bs->drv->bdrv_co_delete_file) { + error_setg(errp, "Driver '%s' does not support image deletion", + bs->drv->format_name); + return -ENOTSUP; + } + + ret =3D bs->drv->bdrv_co_delete_file(bs, &local_err); + if (ret < 0) { + error_propagate(errp, local_err); + } + + 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 e9dcfef7fa..f7db094859 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -373,6 +373,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 coroutine_fn bdrv_co_delete_file(BlockDriverState *bs, Error **errp); =20 =20 typedef struct BdrvCheckResult { --=20 2.24.1 From nobody Fri May 10 15:36:13 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580420775205629.5495396516468; Thu, 30 Jan 2020 13:46:15 -0800 (PST) Received: from localhost ([::1]:39688 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixHde-0005wr-4e for importer@patchew.org; Thu, 30 Jan 2020 16:46:14 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59284) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixHX2-0003S1-Jl for qemu-devel@nongnu.org; Thu, 30 Jan 2020 16:39:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ixHX1-0001WI-FF for qemu-devel@nongnu.org; Thu, 30 Jan 2020 16:39:24 -0500 Received: from mail-qk1-x744.google.com ([2607:f8b0:4864:20::744]:43062) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ixHX1-0001Sn-B2; Thu, 30 Jan 2020 16:39:23 -0500 Received: by mail-qk1-x744.google.com with SMTP id j20so4499590qka.10; Thu, 30 Jan 2020 13:39:22 -0800 (PST) Received: from rekt.ibmuc.com ([2804:431:c7c7:c177:887c:ce1b:6a44:15ec]) by smtp.gmail.com with ESMTPSA id f26sm3650597qtv.77.2020.01.30.13.39.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jan 2020 13:39:21 -0800 (PST) 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=aYZBLLNRu3YFcgNICbDH2tSO88Vv+b9q9C7iEzpwyPc=; b=dhCyK3jwn9dwQsm71wGRlprzheln3ZoEJ+9qPRaBln8b3WSe5BcJps/28qjyjbLXL1 +FCDLF5SzAZ8KKDhqIIcclUATgLVL5cs4XS5S9YkPhZ01GzH1/41Sfm6rzYtgVCLSYrZ /Uwmgoew1zZuraWdxiDtskg9C0QfZpSD+LhQas8bFk+s10eHqeU/o/EpD/QWuKtG5wdG f/ohCdBsU7gG5k64IpM3wM6nnEl3sIQwnCBDryPvRiAt7eGoUMNmVr+Txipx7Riqcwa7 hP52AmXlb1pv5CJ3vh87KdrgNbAhzHlmh+3YRcc0rHNzdRfikB9iKWN4s82QkEjvv1y/ N3fA== 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=aYZBLLNRu3YFcgNICbDH2tSO88Vv+b9q9C7iEzpwyPc=; b=iOcyx8Eluns00a1zTC0wp0xabSMqVTW87vQ5l0fGSyPtFH4ASivTFR0d4wnRNiWN5b dITAvc8/wJW5JWoFaLgfx/pUBYzgg4GAla9d2V0IiX2/cjKOca2qjpriDfL3ad3kXXwj KvA1xdd28xIf/QOEmhwpsFngd/lKMJ4to6jebcwkzLIhOHjUgAI2BAxKarbvazWpoZTd iPwXgvD68d0m2K7AV+bMH91yVAJWwPR3WiCe3+zeFEQR/ZHQsY1q1Z+Wp5NcsgWVcald Xc8DIFRiMhZI2YBwV7qq5vsmmN7PczdUrrTDtksaJEUCODVLgwer2bgMUbkbPI8GpECj rkTQ== X-Gm-Message-State: APjAAAXmX5f6n0LyHW0R56itneJV3ljf43mk1OW9YvOYBmRnCq2QLPsN RxcuHFshUUr3+NI2gN2cWgTTzcnN X-Google-Smtp-Source: APXvYqyOvaVUzhwczteurBygaAyKmE2jxlOFbgzeJEwPFg7jPyeaoi2jDI9nkoysstRh8+LVfncnHw== X-Received: by 2002:a05:620a:2010:: with SMTP id c16mr7338388qka.386.1580420361631; Thu, 30 Jan 2020 13:39:21 -0800 (PST) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Subject: [PATCH v9 3/4] crypto.c: cleanup created file when block_crypto_co_create_opts_luks fails Date: Thu, 30 Jan 2020 18:39:06 -0300 Message-Id: <20200130213907.2830642-4-danielhb413@gmail.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200130213907.2830642-1-danielhb413@gmail.com> References: <20200130213907.2830642-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::744 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, Daniel Henrique Barboza , Srikanth Aithal , qemu-block@nongnu.org, 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" 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 | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/block/crypto.c b/block/crypto.c index 24823835c1..00e8ec537d 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; @@ -596,6 +597,23 @@ static int coroutine_fn block_crypto_co_create_opts_lu= ks(const char *filename, =20 ret =3D 0; fail: + /* + * If an error occurred, delete 'filename'. Even if the file existed + * beforehand, it has been truncated and corrupted in the process. + */ + if (ret && bs) { + Error *local_delete_err =3D NULL; + int r_del =3D bdrv_co_delete_file(bs, &local_delete_err); + /* + * ENOTSUP will happen if the block driver doesn't support + * the 'bdrv_co_delete_file' interface. This is a predictable + * scenario and shouldn't be reported back to the user. + */ + if ((r_del < 0) && (r_del !=3D -ENOTSUP)) { + error_report_err(local_delete_err); + } + } + bdrv_unref(bs); qapi_free_QCryptoBlockCreateOptions(create_opts); qobject_unref(cryptoopts); --=20 2.24.1 From nobody Fri May 10 15:36:13 2024 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 158042093244532.5148758775515; Thu, 30 Jan 2020 13:48:52 -0800 (PST) Received: from localhost ([::1]:39738 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixHgB-00023q-9H for importer@patchew.org; Thu, 30 Jan 2020 16:48:51 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59298) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixHX4-0003Xy-NA for qemu-devel@nongnu.org; Thu, 30 Jan 2020 16:39:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ixHX3-0001ZO-AE for qemu-devel@nongnu.org; Thu, 30 Jan 2020 16:39:26 -0500 Received: from mail-qk1-x72f.google.com ([2607:f8b0:4864:20::72f]:41260) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ixHX3-0001XJ-66; Thu, 30 Jan 2020 16:39:25 -0500 Received: by mail-qk1-x72f.google.com with SMTP id s187so4510666qke.8; Thu, 30 Jan 2020 13:39:24 -0800 (PST) Received: from rekt.ibmuc.com ([2804:431:c7c7:c177:887c:ce1b:6a44:15ec]) by smtp.gmail.com with ESMTPSA id f26sm3650597qtv.77.2020.01.30.13.39.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jan 2020 13:39:23 -0800 (PST) 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=r4T6tb7o7Rg9yHUzyw3ObbNCSy+JF9iuvJwNjdgekxA=; b=tFU/Jko4NOE4ZNsx1pnEfWMG1+875neVX3l5wsuVfc0IvWPdGQgdJSAaw04WmregAl 7Dv/aFjNqlhD3aLmCKsGZFc3QOjfCLv79RFrMIJTsvtnv9cvq+BOWn4rsSE2VUhtbQD/ wJkp4b4uzWr1atS18KuFPzqFMvyRJbaHyF6v0bqPsLQ7ZKIb02okmZlMroqpub2AXBxT h34LviDaD6aT6e2cm4n/z36UzMUEB+i2Ot2VGhCGsCvAt2K9oSkLLcrigKZsvA7dAbfX 6CwS0vmO435bUV2fmTfmZqFITY2AhFkYN/YnANtJjQ4UZ9JUvJfzzqUlf2ZcZ+wdmvwK ffBA== 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=r4T6tb7o7Rg9yHUzyw3ObbNCSy+JF9iuvJwNjdgekxA=; b=nvX544RR47CaM0mH1SsAYHe3MIZWS6FiUxtTE5Txw0oIl8UnxC/3trsAkqHV1sx4uW 6hL8VfFkELfKKWjswKO0rPrBkS4uR4pJ/F5WM1e2T7HFhH0ArL5kiWtODBp1NCviYBm6 /QhA86lM+91JDc7/VsvS1JbZvF5H1zp9QoD26ue6qAzQO3bMPXCF4nZJ6bkESZtkyrdh D9fgad7dH5pexBTytLLTpZ9U8Teei9Sy36T2dJFjY2s9FXBiCmMGBlTiJDQaofVxdLXc ZjQ1jbiHb3OLpzTpeUj6iKFy9UE2ouNdtGBYsFPn/Z9XnhFEetIa85/dltxRQXH6g1c5 OOrw== X-Gm-Message-State: APjAAAXCKk5u46OuaPXxgm7E3+d0lEEn5zcTGbXWPrxjv7IDhVebHCEg 6K6bQMlYA9UiWFM8c8AOfB1cR0uU X-Google-Smtp-Source: APXvYqyHDTMcNGSizsiAepjDnye+5V39bwaVkN+tw4nMHOOyfKUfj4EeSOWGiHJBiq5FEsSJlHm7AA== X-Received: by 2002:a05:620a:ce5:: with SMTP id c5mr7335365qkj.49.1580420363506; Thu, 30 Jan 2020 13:39:23 -0800 (PST) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Subject: [PATCH v9 4/4] qemu-iotests: adding LUKS cleanup for non-UTF8 secret error Date: Thu, 30 Jan 2020 18:39:07 -0300 Message-Id: <20200130213907.2830642-5-danielhb413@gmail.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200130213907.2830642-1-danielhb413@gmail.com> References: <20200130213907.2830642-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::72f 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, Daniel Henrique Barboza , qemu-block@nongnu.org, 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 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/282 | 67 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/282.out | 11 +++++++ tests/qemu-iotests/group | 1 + 3 files changed, 79 insertions(+) create mode 100755 tests/qemu-iotests/282 create mode 100644 tests/qemu-iotests/282.out diff --git a/tests/qemu-iotests/282 b/tests/qemu-iotests/282 new file mode 100755 index 0000000000..081eb12080 --- /dev/null +++ b/tests/qemu-iotests/282 @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +# +# Test qemu-img file cleanup for LUKS when using a non-UTF8 secret +# +# Copyright (C) 2020, 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/282.out b/tests/qemu-iotests/282.out new file mode 100644 index 0000000000..5d079dabce --- /dev/null +++ b/tests/qemu-iotests/282.out @@ -0,0 +1,11 @@ +QA output created by 282 +=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 e041cc1ee3..9d8bf8f783 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -289,3 +289,4 @@ 279 rw backing quick 280 rw migration quick 281 rw quick +282 rw img quick --=20 2.24.1