From patchwork Mon Nov 11 17:01:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 11237355 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B2299159A for ; Mon, 11 Nov 2019 17:04:35 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8788E214DB for ; Mon, 11 Nov 2019 17:04:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AVQTYALo" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8788E214DB Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:55558 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iUD79-0000H8-2d for patchwork-qemu-devel@patchwork.kernel.org; Mon, 11 Nov 2019 12:04:34 -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) 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) 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) 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 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_P_=2E_Berrang=C3=A9?= , Daniel Henrique Barboza , jsnow@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" 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é 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 char *filename, QemuOpts *opts, return raw_co_create(&options, errp); } +static int coroutine_fn raw_co_delete_file(BlockDriverState *bs, + Error **errp) +{ + struct stat st; + int ret; + + if (!(stat(bs->filename, &st) == 0) || !S_ISREG(st.st_mode)) { + error_setg_errno(errp, ENOENT, "%s is not a regular file", + bs->filename); + return -ENOENT; + } + + ret = unlink(bs->filename); + if (ret < 0) { + ret = -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 = { .bdrv_co_block_status = raw_co_block_status, .bdrv_co_invalidate_cache = raw_co_invalidate_cache, .bdrv_co_pwrite_zeroes = raw_co_pwrite_zeroes, + .bdrv_co_delete_file = raw_co_delete_file, .bdrv_co_preadv = raw_co_preadv, .bdrv_co_pwritev = 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); + /* 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 down to * the disk (for example file-posix.c calls fsync()). From patchwork Mon Nov 11 17:01:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 11237363 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C2DFE15AB for ; Mon, 11 Nov 2019 17:10:14 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 97F21214E0 for ; Mon, 11 Nov 2019 17:10:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mvAC5Rqe" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 97F21214E0 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:55618 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iUDCf-0005XM-Me for patchwork-qemu-devel@patchwork.kernel.org; Mon, 11 Nov 2019 12:10:13 -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) 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) 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) 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 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_P_=2E_Berrang=C3=A9?= , Daniel Henrique Barboza , jsnow@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" 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é 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 *opts, Error **errp) return ret; } +int coroutine_fn bdrv_co_delete_file(BlockDriverState *bs, Error **errp) +{ + Error *local_err = NULL; + int ret; + + assert(bs != 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 = 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 *base); +int coroutine_fn bdrv_co_delete_file(BlockDriverState *bs, Error **errp); typedef struct BdrvCheckResult { From patchwork Mon Nov 11 17:01:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 11237359 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B55221709 for ; Mon, 11 Nov 2019 17:08:24 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 894E420656 for ; Mon, 11 Nov 2019 17:08:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YA4rv04v" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 894E420656 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:55604 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iUDAt-0003lb-DM for patchwork-qemu-devel@patchwork.kernel.org; Mon, 11 Nov 2019 12:08:23 -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) 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) 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) 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 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" 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=vol_1_encrypt0,file=vol_resize_pool.vol_1.secret.qzVQrI -o key-secret=vol_1_encrypt0 /var/tmp/pool_target/vol_1 10240K Formatting '/var/tmp/pool_target/vol_1', fmt=luks size=10485760 key-secret=vol_1_encrypt0 qemu-img: /var/tmp/pool_target/vol_1: Data from secret vol_1_encrypt0 is not 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" typedef struct BlockCrypto BlockCrypto; @@ -596,6 +597,23 @@ static int coroutine_fn block_crypto_co_create_opts_luks(const char *filename, ret = 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 = NULL; + int r_del = 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 != -ENOTSUP)) { + error_report_err(local_delete_err); + } + } + bdrv_unref(bs); qapi_free_QCryptoBlockCreateOptions(create_opts); qobject_unref(cryptoopts); From patchwork Mon Nov 11 17:01:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 11237365 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A0FC91709 for ; Mon, 11 Nov 2019 17:10:42 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 76383214E0 for ; Mon, 11 Nov 2019 17:10:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="N3uun9No" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 76383214E0 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:55620 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iUDD7-00062f-Ic for patchwork-qemu-devel@patchwork.kernel.org; Mon, 11 Nov 2019 12:10:41 -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) 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) 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) 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 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" 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=`basename $0` +echo "QA output created by $seq" + +status=1 # failure is the default! +TEST_IMAGE_FILE='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 "== Create non-UTF8 secret ==" +echo -n -e '\x3a\x3c\x3b\xff' > non_utf8_secret +SECRET="secret,id=sec0,file=non_utf8_secret" + +echo "== Throws an error because of invalid UTF-8 secret ==" +$QEMU_IMG create -f $IMGFMT --object $SECRET -o "key-secret=sec0" $TEST_IMAGE_FILE 4M + +echo "== Image file should not exist after the error ==" +if test -f "$TEST_IMAGE_FILE"; then + exit 1 +fi + +echo "== Create a stub image file and run qemu-img again ==" +touch $TEST_IMAGE_FILE +$QEMU_IMG create -f $IMGFMT --object $SECRET -o "key-secret=sec0" $TEST_IMAGE_FILE 4M + +echo "== Pre-existing image file should also be deleted after the error ==" +if test -f "$TEST_IMAGE_FILE"; then + exit 1 +fi + +# success, all done +echo "*** done" +rm -f $seq.full +status=0 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 +== Create non-UTF8 secret == +== Throws an error because of invalid UTF-8 secret == +qemu-img: vol.img: Data from secret sec0 is not valid UTF-8 +Formatting 'vol.img', fmt=luks size=4194304 key-secret=sec0 +== Image file should not exist after the error == +== Create a stub image file and run qemu-img again == +qemu-img: vol.img: Data from secret sec0 is not valid UTF-8 +Formatting 'vol.img', fmt=luks size=4194304 key-secret=sec0 +== Pre-existing image file should also be deleted after the error == + *** 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