From patchwork Tue Mar 26 21:17:42 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: 10872189 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4707E13B5 for ; Tue, 26 Mar 2019 21:22:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2EC5A28CC2 for ; Tue, 26 Mar 2019 21:22:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1BA0428CC9; Tue, 26 Mar 2019 21:22:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.7 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 8FC5B28CC2 for ; Tue, 26 Mar 2019 21:22:11 +0000 (UTC) Received: from localhost ([127.0.0.1]:38111 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8tWL-0002Je-PV for patchwork-qemu-devel@patchwork.kernel.org; Tue, 26 Mar 2019 17:22:09 -0400 Received: from eggs.gnu.org ([209.51.188.92]:58509) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8tV5-0001OW-Ne for qemu-devel@nongnu.org; Tue, 26 Mar 2019 17:20:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h8tSL-000180-Il for qemu-devel@nongnu.org; Tue, 26 Mar 2019 17:18:02 -0400 Received: from mail-qk1-x742.google.com ([2607:f8b0:4864:20::742]:39032) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h8tSL-00017q-Ck; Tue, 26 Mar 2019 17:18:01 -0400 Received: by mail-qk1-x742.google.com with SMTP id c189so8602885qke.6; Tue, 26 Mar 2019 14:18:01 -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=9MtXhX5dJ+tUvF2J8ktiidfJ2IOKL8NxvGnYVHhQzDg=; b=tFP8v0OFtoSQmXkcBxK2eScRgthaVhV2J5HgU8XQK584JqjO61AZIEoIVrEscvaeUB VTBPsl7zuPEB0R5p4NT58+j6etMFFXybOjVoZ2sXyuyQkHbEtjxjogSEtgYTeLJBjMMT 5/CabdUQLi7jR6Jyk11NgoW3Pn+jekoVFYG3S28ETZqg+aI9JXZq9/ilgFK43B2u4AJ8 Qi9TtiHIDODnApOOV/WNwUGDZLIZ1HunYAu2UW3jfHA4Rw1j+wyo7R69BcojHOruMHZp uwdhdWQrRnjXu7OxgV+vWHdakL8u970yok+xoPmubGWE9cq9FWHCJ1txktedHlA0bAH6 pPow== 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=9MtXhX5dJ+tUvF2J8ktiidfJ2IOKL8NxvGnYVHhQzDg=; b=Ii4rleJRbXdLpBrR61C1FXON14Njh3rhbsT258JyP25jXBhUA5+K4uBVhiC3rR0Idh mOE5PpEq6SGA2gqpAIIoJKI4CyvS/nF38SrmE0PpZqrJlAUkvI4c/3UsdFIR8h+9UOt9 SUH8KMdw7p4VIMGgDBE8jxoh78uOvBhy0YO4emu4AFvI/1sJRi0TA/hIrnAdfeoaod1n ymDssHK8vAMsgosCHqJ2Tj2Bz0R3dsitpNIPnUCOMydgDFCKI2HnMi8LH6AyQkGwSXit d4R0tAShIPHVyndRqabmT6fBPwg5q8kv+w+jJoyurhUMFKdrS8BGLSeNmHd3XhUFK8Sr Dwuw== X-Gm-Message-State: APjAAAXKCK6Y/6wk8h/YGFZidfB34D0vccnbYl+nuNk2BeKdW4wBvVOA RDiCOvelxuE7Q+/CTA7HVmYAFi1DlCY= X-Google-Smtp-Source: APXvYqw6HrO3K115wck7uwp3AuTaasMm2/nx2AwfmJGxyYMS4zLbIDlgQ/M/sICwrema1YGcsvbuYQ== X-Received: by 2002:a37:5d06:: with SMTP id r6mr25784797qkb.148.1553635080734; Tue, 26 Mar 2019 14:18:00 -0700 (PDT) Received: from rekt.ibmmodules.com ([2804:431:f700:6d41:b488:ffd7:8d35:4aac]) by smtp.gmail.com with ESMTPSA id d21sm11735060qtc.91.2019.03.26.14.17.58 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 26 Mar 2019 14:18:00 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Tue, 26 Mar 2019 18:17:42 -0300 Message-Id: <20190326211742.26140-4-danielhb413@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190326211742.26140-1-danielhb413@gmail.com> References: <20190326211742.26140-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::742 Subject: [Qemu-devel] [PATCH v3 3/3] crypto.c: cleanup created file when block_crypto_co_create_opts_luks fails X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Srikanth Aithal , qemu-block@nongnu.org, Daniel Henrique Barboza , mreitz@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 check if @filename is an existing file before bdrv_create_file is called. In case of failure, if @filename didn't exist before, check again for its existence and, if affirmative, erase it by calling bdrv_delete_file. * 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 | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/block/crypto.c b/block/crypto.c index 3af46b805f..7c1b80616c 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -29,6 +29,7 @@ #include "qapi/qobject-input-visitor.h" #include "qapi/error.h" #include "qemu/option.h" +#include "qemu/cutils.h" #include "crypto.h" typedef struct BlockCrypto BlockCrypto; @@ -533,6 +534,8 @@ static int coroutine_fn block_crypto_co_create_opts_luks(const char *filename, BlockDriverState *bs = NULL; QDict *cryptoopts; int64_t size; + const char *path; + bool file_already_existed = false; int ret; /* Parse options */ @@ -549,6 +552,15 @@ static int coroutine_fn block_crypto_co_create_opts_luks(const char *filename, goto fail; } + /* + * Check if 'filename' represents a local file that already + * exists in the file system prior to bdrv_create_file. Strip + * the leading 'file:' from the filename if it exists. + */ + path = filename; + strstart(path, "file:", &path); + file_already_existed = bdrv_path_is_regular_file(path); + /* Create protocol layer */ ret = bdrv_create_file(filename, opts, errp); if (ret < 0) { @@ -573,6 +585,25 @@ fail: bdrv_unref(bs); qapi_free_QCryptoBlockCreateOptions(create_opts); qobject_unref(cryptoopts); + + /* + * If an error occurred and we ended up creating a bogus + * 'filename' file, delete it + */ + if (ret && !file_already_existed && bdrv_path_is_regular_file(path)) { + Error *local_err; + int r_del = bdrv_delete_file(path, &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 != -ENOTSUP) && (r_del != -ENOENT)) { + error_reportf_err(local_err, "%s: ", path); + } + } + return ret; }