From nobody Thu May 16 16:43:16 2024 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=1573491923; cv=none; d=zoho.com; s=zohoarc; b=W7s13QflM/sKfk18iTAaSITfeyEwasr3mFuygEH/BAceX00mgXMkgarDS19bgqGdSk2BBhmosR4EEKg/UWWodbCE+M4BXgZaXSkevVm9fDmtbQjWIc82/HzJ+8OYdCSDlDWVwKQqNTCjXYbUoUtT9d0Vut/5IqhrOOFrwERu2rk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573491923; 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; bh=uNJX0SlFEqt2tTbkGEN46Hc3OMmBWRvtaEqtJiCobJY=; b=EYWrDrn4kCpsWmBMRs+td/SLLGAiKzLtXAq/2sZOfSiDskK3lDjzceJjJUvGo5w7iO3sf4erLxgxzkRRVgr3vbDiyxSYTfDZPV4hzcSF0lXrNpUgtxhteaGBaVy7tykWng0I7BnPBb3SEOC1jvz0xM9rpQHC/KCdw7ZJ1mvU7UA= 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 1573491923736699.7602806447821; Mon, 11 Nov 2019 09:05:23 -0800 (PST) Received: from localhost ([::1]:55558 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iUD7v-0000H8-Ol for importer@patchew.org; Mon, 11 Nov 2019 12:05:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:48510) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iUD4Y-0006DR-9v for qemu-devel@nongnu.org; Mon, 11 Nov 2019 12:01:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iUD4X-0006A6-7E for qemu-devel@nongnu.org; Mon, 11 Nov 2019 12:01:50 -0500 Received: from mail-qk1-x743.google.com ([2607:f8b0:4864:20::743]:39126) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iUD4X-00069y-3i for qemu-devel@nongnu.org; Mon, 11 Nov 2019 12:01:49 -0500 Received: by mail-qk1-x743.google.com with SMTP id 15so11778724qkh.6 for ; Mon, 11 Nov 2019 09:01:49 -0800 (PST) Received: from rekt.ibmuc.com ([2804:431:c7c6:980:1591:e29e:17a4:d484]) by smtp.gmail.com with ESMTPSA id n20sm8572462qkn.118.2019.11.11.09.01.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2019 09:01:47 -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=uNJX0SlFEqt2tTbkGEN46Hc3OMmBWRvtaEqtJiCobJY=; b=AVQTYALosqoshT933GNwh/Fr2UE9UN0e5K07IZf3GvpTO5j6ZVzz9hXw6J0UT1XbCK EmWIBJDDPEen19UuPaS5e2YcZRU+EupfxhV7dpzziz/SNiiA9GqdymtFct6GnryCdbw9 8Jpoh+LhzD5VHvEvyMEUvl4J0zvNofQ5eqqnpqYP7CYtrbSdoGL7Nsy0y1LOTTh7/M1D ZFALQ3toL9xTE7zsSzvXfYxJwSaI4tBoqtDtEnbt0lXBntscpskkQ8fMVq9GgqBl3tfK tisx0dBpaVidPu6I91+pSjbyjSBou0flkP7RhLhss5SziGpVzpIyNp2xQDdCPk3kw8V9 hztg== 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=uNJX0SlFEqt2tTbkGEN46Hc3OMmBWRvtaEqtJiCobJY=; b=W8zrTpPPgOlgzH6j8KUd/TF2PvwmS40ROmbBh2A860biFOQ2M8RIsuijT7nLcxTYjR SMFGgV4eWbXNtx4lGziJuIUwgoksDS5ataJO2+ajIrKNVSgj7shSVd6EhE5sb/YalyWE BfngQ4+zpZDvdr2ldFzsskCA9+RV3mz0P/x+4cGN9Nx/+0UWW1vhF9gVvGociR7Cxska s7Xve10vNEeGdnYD0oP8DWtGEd8VJJdU2ji5oOsruJMvqtPE5odIChgP+i427r/gIP5/ 36MhXoaLB0DOz3wE3blP+SGIH3GVH5Yql6KIW8YPonzGV8nHwhD/Jh3YyW1GQZ5mUwFP w0ew== X-Gm-Message-State: APjAAAUghjwUrwTGl7MKflca2cp4R8kiv7lWfrLAQbbCnbXch/RYTw1s fVb9roMdfR9lRhLV5VNuF0ta4ATR X-Google-Smtp-Source: APXvYqyzM9PBp+EN/5id1bXFNbkiFDQJAd7qhGVQuHjKH8dkNsRBb9/d8VdE/LyDnhDbWj0jniwXgQ== X-Received: by 2002:a05:620a:16ba:: with SMTP id s26mr11458151qkj.107.1573491708277; Mon, 11 Nov 2019 09:01:48 -0800 (PST) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Subject: [PATCH v8 1/4] block: introducing 'bdrv_co_delete_file' interface Date: Mon, 11 Nov 2019 14:01:28 -0300 Message-Id: <20191111170131.13742-2-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191111170131.13742-1-danielhb413@gmail.com> References: <20191111170131.13742-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 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, =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Daniel Henrique Barboza , 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) 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 1f0f61a02b..692a36a799 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.21.0 From nobody Thu May 16 16:43:16 2024 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=1573492241; cv=none; d=zoho.com; s=zohoarc; b=hvktitnHnsR6IVX+rYNDWAMTj2bWqNRg66mpX+ke31GiE6yKgMQgZ2AyWHztVo1AUlgVwWe+TxO6fTwdv27pG6yy6K71PCR5m2iQlw8Ezovk21CS1xH+VrI/cSE70V0gM8w73ac8Yhn3T/HZjZQtWhYvgQ5QT+3AXjjaQI+ZYRE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573492241; 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; bh=1+2lpgu3zdyL7nO6XorDplIsYsvqY/zmN6+JOXbDLAk=; b=DyGesnqWSRIB6qHIevZxo4cQ6VGDDn+xoQourrltV1Z0Cxmz6ikvQ4u0IwWRMChMjlkUb4g2tZiYLv9rxsDsVuF7d168fZS0tyDXw8MeH2iajhvcfZ7523whM2N+N3iu+81HyiC5atOYHcMPUa/LkzR3i1xS4ztIW5rGkPGCc+s= 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 1573492241515753.097343475819; Mon, 11 Nov 2019 09:10:41 -0800 (PST) Received: from localhost ([::1]:55618 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iUDD5-0005XM-Dx for importer@patchew.org; Mon, 11 Nov 2019 12:10:39 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:48527) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iUD4a-0006Fn-1U for qemu-devel@nongnu.org; Mon, 11 Nov 2019 12:01:53 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iUD4Y-0006Ao-Q5 for qemu-devel@nongnu.org; Mon, 11 Nov 2019 12:01:51 -0500 Received: from mail-qt1-x842.google.com ([2607:f8b0:4864:20::842]:38155) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iUD4Y-0006AZ-MK for qemu-devel@nongnu.org; Mon, 11 Nov 2019 12:01:50 -0500 Received: by mail-qt1-x842.google.com with SMTP id p20so16394193qtq.5 for ; Mon, 11 Nov 2019 09:01:50 -0800 (PST) Received: from rekt.ibmuc.com ([2804:431:c7c6:980:1591:e29e:17a4:d484]) by smtp.gmail.com with ESMTPSA id n20sm8572462qkn.118.2019.11.11.09.01.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2019 09:01:49 -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=1+2lpgu3zdyL7nO6XorDplIsYsvqY/zmN6+JOXbDLAk=; b=mvAC5Rqe5bLIv86SrqVUisl6QYI6xqUQNsrBq1XdaR42YcfpzYzCYWcrWZHF1AoTwj hFJNGrLJpxdW0aA6JwOkDFo2sVhLXnQCkc/LnUg52RdV4sd6KPIkACRaKIlU0/yq9GN9 4zHHdM97xS8v8iEP/4M7bknk2v5rutkPkK/Rj7C9ld1UpgopZAEuHJrJeZb1K80lrCNk 6OyiqILnxlcfENSnDSPuK6SDD8OwkGkh9qh0CAu6XgZPw3+MA/0H+o98Jj0n3UvzqLGO AcreIhnkLKnzXlavpeCoCu9o0lJrneGgWwnjKbxtPIRGGSp1az0VtFWWUFJh4KkICVhH qyQA== 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=1+2lpgu3zdyL7nO6XorDplIsYsvqY/zmN6+JOXbDLAk=; b=Addaj5rx1NbN5L/u2j/em1WXd8P6xEO9lJuGS3yZXY7yzZzhinG9Uh9m6QR3zL+fI8 RlTMY62tbKX7626XFCCUdWDPWaFfr8v+j2Dtn73I17IxtqfDo+7AyPlQIb68mWYnTWCA PPYfVbkNGrovjMUZ3+vCFk0RKQ5xJQujf3UyfRU64yP56/96iof8nWeo3vQUhKAmgnZr H5395U3VEsiIp/FbWrcf+T1GI2j+MzMsh1XwgLp6YK9CSCpdEpQxvs6zAKGFXMnGamYV sIWKnZdR7lAkoEXY/dXxBfvmVzQ5oI33lEq16oc33Wl7fNM1SZd7ApsodgklBEP/JFWM m6tg== X-Gm-Message-State: APjAAAW/oRQP5+bRoBC74iw8b8gaFGKpHSTZo5dRxidYl5TL1UWONgJW vBQRUZTXlMUYEuVziue9ArgKorea X-Google-Smtp-Source: APXvYqyDQhssQzACInutuxrgHlKwhOkFaItxRpF5Ej+aQ0EJcy0aakk9/77JjEBeCLHSQ1g2+l+bbg== X-Received: by 2002:ac8:38e3:: with SMTP id g32mr26567848qtc.103.1573491709975; Mon, 11 Nov 2019 09:01:49 -0800 (PST) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Subject: [PATCH v8 2/4] block.c: adding bdrv_co_delete_file Date: Mon, 11 Nov 2019 14:01:29 -0300 Message-Id: <20191111170131.13742-3-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191111170131.13742-1-danielhb413@gmail.com> References: <20191111170131.13742-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::842 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, =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Daniel Henrique Barboza , 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) 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 4cffc2bc35..c325104b8c 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 1df9848e74..ec0d82f6b0 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -372,6 +372,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.21.0 From nobody Thu May 16 16:43:16 2024 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=1573492126; cv=none; d=zoho.com; s=zohoarc; b=QuvcxVdAu5AebhpUtRi6bEHkl3MXq5Tp6Gz0CQuVkVjtWZ4RgHJx2E1k92yGQlIuIf/3o72VgO1gDQ5xSxZCLDDTVDm4iqiq84Cc6LXOpd2Qe+CkLRJF/O8q6qSw0Di02uQyBeQJXxbyzB1xEvweUiVkVdVjO5O4DYigI3JOnr8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573492126; 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; bh=o+37rkLSugI95LpgNThRWPsnUABBwxEDt0ZiwnaoT3I=; b=H9NpU657/f8j7sJKJepFNwlB/k5j23NK07Nahm7q9aldW9+61i13gd2J+CVCQrf/7/I+PnnsoN40R8jW9K35T2/XqUypHWj3qEuSkknXt/kcILmXnwhi+5s3fxsWR+vyL4IvTU4w+n9jIxmgqy9exMPaftiPd0S8af792UpwvyA= 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 1573492126635497.9107190130658; Mon, 11 Nov 2019 09:08:46 -0800 (PST) Received: from localhost ([::1]:55604 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iUDBE-0003lb-61 for importer@patchew.org; Mon, 11 Nov 2019 12:08:44 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:48544) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iUD4b-0006IL-VM for qemu-devel@nongnu.org; Mon, 11 Nov 2019 12:01:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iUD4a-0006B7-IU for qemu-devel@nongnu.org; Mon, 11 Nov 2019 12:01:53 -0500 Received: from mail-qv1-xf42.google.com ([2607:f8b0:4864:20::f42]:33547) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iUD4a-0006B1-Ec for qemu-devel@nongnu.org; Mon, 11 Nov 2019 12:01:52 -0500 Received: by mail-qv1-xf42.google.com with SMTP id x14so5170784qvu.0 for ; Mon, 11 Nov 2019 09:01:52 -0800 (PST) Received: from rekt.ibmuc.com ([2804:431:c7c6:980:1591:e29e:17a4:d484]) by smtp.gmail.com with ESMTPSA id n20sm8572462qkn.118.2019.11.11.09.01.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2019 09:01:51 -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=o+37rkLSugI95LpgNThRWPsnUABBwxEDt0ZiwnaoT3I=; b=YA4rv04vyx2F4LHr4HP0It2hhAqLDGEGCm5Tg/f6V1m8gmjPZUbdFDCFuvjyx9ya2J J6Ja6xkteZptCqic42+Ee9CA9oLVq1pfy8CTqhutRkb8t10kG1sFsEGSKtiS04rfFPWq jD8UAkZsixDx1A0YfN9NBBZ0kpcggMM4jRUbYy3M/qb3FfI3v2iHlzp2nLPWvM/+n043 5cP4Z3MHH604L3/DJOg5CVFh04JC+QfWKVepapM3bmzgSCDMPHQ2LOtFJKAfsXvuQgFv ++g5YBiF9bRWAGw8SXfAR9BR5fAf48ddgDgLOqFNGrThyvfkQhJUlJFi9UD3jNFsnHqu InnA== 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=o+37rkLSugI95LpgNThRWPsnUABBwxEDt0ZiwnaoT3I=; b=JivH7f4nfrUgJDlTEU8E692qyBr3Ju6KsJnpLVe/JO0Hwl2FNsPUsNEiSQJExdQZOy hM7l1k/0CYPfH1PCnuNZMdjnxZQ+7YKAj2KpRq1IkmTqwOd4oJizyTm5yCgsnjGfZfJQ B2868tFAizbMJQDaaBXxwDv1pgcumQyfMaDeBjc9fs6NrqUDNq3J/vFc2hqvIsso6UVp FRwuYrEmsAbgvQBXEfphEjhOozfToXQQdkq5yvKrrzH0KEZ9r9m5rHUBXld5j/kKbC1T QIyvOzejf6gGDXBV9yHVOGy4z9qzeAlggy+l47lQoO+v/gdYP70C5QIAbWIAxZqWCLFS tlPQ== X-Gm-Message-State: APjAAAWifUGYqGlrHxzPihsq1EZNtKf366L9n8DLkyhmFAy0e5l9yqRH 7J6oTgd9JmIRXkdg3TZTQs3CAhFAqG8= X-Google-Smtp-Source: APXvYqw9bLWEpCHdqjLXwcm0Wu2S691CJ4o55YttaVt7GDSW/GJFUHLKUWYyy0xKxnBb20l98fRw+Q== X-Received: by 2002:ad4:538b:: with SMTP id i11mr20167656qvv.211.1573491711676; Mon, 11 Nov 2019 09:01:51 -0800 (PST) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Subject: [PATCH v8 3/4] crypto.c: cleanup created file when block_crypto_co_create_opts_luks fails Date: Mon, 11 Nov 2019 14:01:30 -0300 Message-Id: <20191111170131.13742-4-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191111170131.13742-1-danielhb413@gmail.com> References: <20191111170131.13742-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::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, Srikanth Aithal , Daniel Henrique Barboza , 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 | 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.21.0 From nobody Thu May 16 16:43:16 2024 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=1573492257; cv=none; d=zoho.com; s=zohoarc; b=JiJGozBPbCRxQctQ0mfTGx6MJv6Isg1Na3me4YXaP2XvL6f6UBqZ4yTbZCPfgGI7efWhHXxenxfBl02/uBErLU3xD7ZD+kg/3TIs+lomOy8eLg+c2Puhj2ngNp04dHicF74ApmhIHMPir2/r8fJt2m/fW8q3GiPA9kCtDaQfuBQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573492257; 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; bh=NQ6sIHX3cogSCquUSvZcCkqs4h3Hgeb5nTsLWfS1/HI=; b=Ti1FPEVEoklYAw1N0cpRePIqHGVyv6Nyjw5asNKsER/oPdfPq59JcsRh1oz6uWJIgnRV81ywzE4qBtf+v2B0QB+L/H2oEvqHueyhJC5Tzv8CBzg2RvYlUUSaMfLd61BDtHDpjvBlljj27+ba19JK4b2LmjN2sZLvf+zLfgAqH9o= 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 1573492257873847.1232400273402; Mon, 11 Nov 2019 09:10:57 -0800 (PST) Received: from localhost ([::1]:55620 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iUDDM-00062f-MY for importer@patchew.org; Mon, 11 Nov 2019 12:10:56 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:48555) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iUD4d-0006Kp-LS for qemu-devel@nongnu.org; Mon, 11 Nov 2019 12:01:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iUD4c-0006BT-5j for qemu-devel@nongnu.org; Mon, 11 Nov 2019 12:01:55 -0500 Received: from mail-qt1-x836.google.com ([2607:f8b0:4864:20::836]:42443) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iUD4c-0006BN-1e for qemu-devel@nongnu.org; Mon, 11 Nov 2019 12:01:54 -0500 Received: by mail-qt1-x836.google.com with SMTP id t20so16371567qtn.9 for ; Mon, 11 Nov 2019 09:01:54 -0800 (PST) Received: from rekt.ibmuc.com ([2804:431:c7c6:980:1591:e29e:17a4:d484]) by smtp.gmail.com with ESMTPSA id n20sm8572462qkn.118.2019.11.11.09.01.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2019 09:01:52 -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=NQ6sIHX3cogSCquUSvZcCkqs4h3Hgeb5nTsLWfS1/HI=; b=N3uun9No6UhzYE46gRpANAyf4dgZSLeaWkQm9SDACID5hg8tWukx9xN+2Jmg4sKOsm KyBMxuNU9newBcMFrzI08Nd3QBCKH5kNfXtfkjlJE7GtjNnrbPqqJk0bLii+Xi2dcOsb x0OPgsgyPa0wzSPSXxPOFbjGZvZuzB5CApDfJyTB0Cxq29D9k2D9PTdjyXIaSbBt7/Vj CwWnDGST9hoVHqp4yHEEaeEh+zNUQjBoXWFqSEUEJQn7R9pNdkFwlDG20AcTss96BQIA 8pk2tUu7LUHEWF/jdy4y/Rs+TCDwGYgAn+PvSG7QxTpypfWbvFnmOIqs1G77LaCOiIp1 kPxA== 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=NQ6sIHX3cogSCquUSvZcCkqs4h3Hgeb5nTsLWfS1/HI=; b=FALgIMSFzwIzgcra0niu+h6iOSxa93Q1URpoMmac1Ajqrk6ga4JFGcBIbKRdhN5/OM AQyBMwrMHROA1fAHTYS/9SKzmizE/yF2suCqOTY+uULukcs0PHm0NxxX0h/bWyZydtjH 0GxMD/2WzW4zBrcK32rXYfbFmnBWc9Vo5juBJLaNrxRBsGQ18Sn2UtUQ0Fq7fRsfUFaw Xa5bIpMExV6NfR5MhbYm61lsIBPJwdkG+dI75TEyu7q7EXCgM05Rj1wptxxyS1fMsD4l z8z6f4XfSEJc4hQf42klNyRyYkzPKTP77ai/KCzvkcUKdVFUmndk2LMYybvKlfa72jUS hq5g== X-Gm-Message-State: APjAAAWvnhHj2ArI3GOH4lS6EvFwPxtmJQADICtiEPEc1BLzQupXioNN bwr5GCYTLhIDcYSa1GCHm9zM3k8ljig= X-Google-Smtp-Source: APXvYqy21TkPLbqlStOtPou7fk1+Ce3FxSHRIwU2fZF/wv9HVV3ZPvGf8Pdt9oV6Yq3JSE+arlQO/A== X-Received: by 2002:ac8:1e13:: with SMTP id n19mr27180588qtl.384.1573491713352; Mon, 11 Nov 2019 09:01:53 -0800 (PST) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Subject: [PATCH v8 4/4] qemu-iotests: adding LUKS cleanup for non-UTF8 secret error Date: Mon, 11 Nov 2019 14:01:31 -0300 Message-Id: <20191111170131.13742-5-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191111170131.13742-1-danielhb413@gmail.com> References: <20191111170131.13742-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::836 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 , 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" 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/273 | 67 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/273.out | 11 +++++++ tests/qemu-iotests/group | 1 + 3 files changed, 79 insertions(+) create mode 100755 tests/qemu-iotests/273 create mode 100644 tests/qemu-iotests/273.out diff --git a/tests/qemu-iotests/273 b/tests/qemu-iotests/273 new file mode 100755 index 0000000000..cb362598b4 --- /dev/null +++ b/tests/qemu-iotests/273 @@ -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/273.out b/tests/qemu-iotests/273.out new file mode 100644 index 0000000000..8c6653cd82 --- /dev/null +++ b/tests/qemu-iotests/273.out @@ -0,0 +1,11 @@ +QA output created by 273 +=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 065040398d..fc5a680739 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -284,3 +284,4 @@ 268 rw auto quick 270 rw backing quick 272 rw +273 rw img quick \ No newline at end of file --=20 2.21.0