From nobody Sun Feb 8 17:21:27 2026 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=1561751604; cv=none; d=zoho.com; s=zohoarc; b=ha+HwtPlL67PPg14kWjyZE8/Qst4FOltFtRChfUegS9KacK3tRj3/ed1Y0dA+bAxL4tQvUBpH/Ck2kTZX49MMHIZXfixF/ETPhfAXl8/eKI0H/QaFNy9LJqqRGqgJj8/QK2abxwvGoCGhqDZ7GM7hTbeeHDLtPVw2iYh4xXAuOI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1561751604; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=T3WLgtmuAu7cjJ7PRM9PUfqyVZbL0EGGzc5/L/cNNS4=; b=EgisRU7fWOJotSzVaCkIpA9tMjf+hYgpezTiE0+gD5iksdguKsilaAkyPe5jImqhJR4qtOWYsu9KGWSdjNAcMWnMUm/zfb9ADy34Dt+eLhoWt+eqLxjWGzwnAimXLJekapKf/sb7hUMimRU/z835WI1O9iVKH5fbX8U6i/wuAzU= 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 (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1561751604054309.31340295331825; Fri, 28 Jun 2019 12:53:24 -0700 (PDT) Received: from localhost ([::1]:35993 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hgwvq-0005VN-Vk for importer@patchew.org; Fri, 28 Jun 2019 15:53:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34396) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hgwoP-0000cJ-Go for qemu-devel@nongnu.org; Fri, 28 Jun 2019 15:45:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hgwoM-0000wS-Rr for qemu-devel@nongnu.org; Fri, 28 Jun 2019 15:45:33 -0400 Received: from mail-qk1-x742.google.com ([2607:f8b0:4864:20::742]:44081) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hgwoM-0000vW-M9 for qemu-devel@nongnu.org; Fri, 28 Jun 2019 15:45:30 -0400 Received: by mail-qk1-x742.google.com with SMTP id p144so5866508qke.11 for ; Fri, 28 Jun 2019 12:45:30 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c6:927e:501b:7135:21ca:8a87]) by smtp.gmail.com with ESMTPSA id b69sm1602288qkg.105.2019.06.28.12.45.27 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 28 Jun 2019 12:45:29 -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=T3WLgtmuAu7cjJ7PRM9PUfqyVZbL0EGGzc5/L/cNNS4=; b=FiQgE6kkvyQ1iisedNDHbfVyjkNH8GUJUVv4fLKJ6yF+46ENvyowm5talKxYTHRrNS ywOBK0EFdbnLZqauu3Eu4IJFNo59yQzUVd+dI1K649/uCuUVlN2y/MzrcQp3O3ipWDDb Kk1O+GQrNFfbwG0D4hw2KuJlh6c3MpwfujjC9132woagOqJHnF4gFrtKjZW/xdpXSpum lf7yk60BbcTr2HYITZXfErTgjRN1eNFimKT6V73cWrrarAa7IqzysDtTfrrhXO1cfEtP JEc5sZUpKXpL1kElGBGSpsMNHR65sGknpmgGKCdagk7K5hqBxz1kNJo3o2kpWoGjaow2 Tc4Q== 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=T3WLgtmuAu7cjJ7PRM9PUfqyVZbL0EGGzc5/L/cNNS4=; b=j1LEE/fwoh+Ves6Ufy1Tz1QJrFw4UwosCOJ4JA1XW9z5CcI3pF0QD5W20pa21R6gRo kSNqhXSqUpPyVB6eakD7rwwNPpmTD/1kP3TO2IqNMmpmEVL0eULovzJFdvy7W+Nu0HzN nzuxosv/KgfGsDPUq24kfDW000ZpSFF2oFS3j51daTTOzw/9i1i5WLWJx6t5em/Rj9GC RJtV01Rcq2F58mfQYWgt4VNTnbG9WzzSjBUrF0L8dFo7zHPp/x0zpZMwr+WGgO2en/Uq gxbsXEM3vfsz/999DqCWS+1JTZe0VpWXCion8YO//z4yogP/r7sSs7cDO63N80vAzzxd 8xNQ== X-Gm-Message-State: APjAAAWCBsrb+vUd4x/bhl9yZkmIs9P1bIdtowNVfuKsY+IK+I2gGgxq +sVwNYfUcgMCbdWHsMd7BtB7zU21 X-Google-Smtp-Source: APXvYqzcoPu9p59nvdDiMoYb9tIYVNEuAAwZ6U4GFCbbOsX6vUg3Wktx4BClMr7CQ6kNWJjIivYEcA== X-Received: by 2002:a37:a692:: with SMTP id p140mr9965384qke.432.1561751129802; Fri, 28 Jun 2019 12:45:29 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Fri, 28 Jun 2019 16:45:11 -0300 Message-Id: <20190628194512.21311-4-danielhb413@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190628194512.21311-1-danielhb413@gmail.com> References: <20190628194512.21311-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::742 Subject: [Qemu-devel] [PATCH v4 3/4] crypto.c: cleanup created file when block_crypto_co_create_opts_luks fails X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, berrange@redhat.com, Daniel Henrique Barboza , mreitz@redhat.com, Srikanth Aithal , jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" When using a non-UTF8 secret to create a volume using qemu-img, the following error happens: $ qemu-img create -f luks --object secret,id=3Dvol_1_encrypt0,file=3Dvol_re= size_pool.vol_1.secret.qzVQrI -o key-secret=3Dvol_1_encrypt0 /var/tmp/pool_= target/vol_1 10240K Formatting '/var/tmp/pool_target/vol_1', fmt=3Dluks size=3D10485760 key-sec= ret=3Dvol_1_encrypt0 qemu-img: /var/tmp/pool_target/vol_1: Data from secret vol_1_encrypt0 is no= t valid UTF-8 However, the created file /var/tmp/pool_target/vol_1 is left behind in the file system after the failure. This behavior can be observed when creating the volume using Libvirt, via 'virsh vol-create', and then getting "volume target path already exist" errors when trying to re-create the volume. The volume file is created inside block_crypto_co_create_opts_luks, in block/crypto.c. If the bdrv_create_file() call is successful but any succeeding step fails*, the existing 'fail' label does not take into account the created file, leaving it behind. This patch changes block_crypto_co_create_opts_luks to 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 8237424ae6..146f3eb721 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; @@ -535,6 +536,8 @@ static int coroutine_fn block_crypto_co_create_opts_luk= s(const char *filename, BlockDriverState *bs =3D NULL; QDict *cryptoopts; int64_t size; + const char *path; + bool file_already_existed =3D false; int ret; =20 /* Parse options */ @@ -551,6 +554,15 @@ static int coroutine_fn block_crypto_co_create_opts_lu= ks(const char *filename, goto fail; } =20 + /* + * 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 =3D filename; + strstart(path, "file:", &path); + file_already_existed =3D bdrv_path_is_regular_file(path); + /* Create protocol layer */ ret =3D bdrv_create_file(filename, opts, errp); if (ret < 0) { @@ -575,6 +587,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 =3D 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 !=3D -ENOTSUP) && (r_del !=3D -ENOENT)) { + error_reportf_err(local_err, "%s: ", path); + } + } + return ret; } =20 --=20 2.20.1