From nobody Wed Nov 12 02:02:24 2025 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=1567458181; cv=none; d=zoho.com; s=zohoarc; b=hTu4VDzMyBJARH104+DpqJz/gn5vKGshFYoljY7KvJmcEY8ZlqRpqz4+RRL8y0Ee/24WmoKZE5zaxVevOJPVpxM2TiRR9S6vDXeyQvlMXgHV2rsfqsHUeMWwPuuGukgRkz7d/4Gd9cw9bNc+mdmNQwKKYwCuFItW5Mk+FDHP6RQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1567458181; 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=cqXvzeH9JGbK13wh2PF5LuHVAWopqM5fCsamJ7eRinw=; b=G+EoknQSNzb4lwqKyLQnOAIJR7EMqZnQXZCcPxFT6l4/sEZQUyMflVk6bJFx47r0LiMmjJr0hDSvYEjuFFHF5dGgWBH9yzpjzDZBc06Cp6Ekby9l7kINfb5M5NaOGtcWTKvzBqJ5ucoQSfW5JsNlE6LiEQFBIavbuGpUPf1fZTM= 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 1567458181320658.7371281943771; Mon, 2 Sep 2019 14:03:01 -0700 (PDT) Received: from localhost ([::1]:39990 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i4tTU-0000Cf-QZ for importer@patchew.org; Mon, 02 Sep 2019 17:02:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33429) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i4tPf-0005rz-Bo for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:59:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i4tPd-0004Mu-QL for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:58:59 -0400 Received: from mail-qk1-x742.google.com ([2607:f8b0:4864:20::742]:44465) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i4tPd-0004MG-Mb for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:58:57 -0400 Received: by mail-qk1-x742.google.com with SMTP id i78so12180622qke.11 for ; Mon, 02 Sep 2019 13:58:57 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c6:9589:1351:c7eb:87b:c9d5]) by smtp.gmail.com with ESMTPSA id w80sm4604038qka.62.2019.09.02.13.58.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2019 13:58:56 -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=cqXvzeH9JGbK13wh2PF5LuHVAWopqM5fCsamJ7eRinw=; b=oPaHINxnFgmuXrpWzFMhoyWEZXP+9V2pMbLQt9juEQ88tbDdeStkCgOqZIcYdW+orD CIbrakIBSQjjsBvmbwlC9KEao2dwXRt9mxLuBW2cJkfTwRstrxA9ernAs1MKo/Jx7r7d 6PH6rsYw9p4DFgbgfKyXv4qDtuPsCRgY8+Mb9kLIPjk7wu9rSakfbzjQfKnigw8lmGtL X24rt/3b9zB2K60gr/qUcyrVefExVBS0FPPmofGjoj8/+OiqizSo7fp0O4+mdwmSoY/W /qoyk/kcFwYZMosNeEtE4m29fZkQ583HrkpeC1SH2Q85vwJ+9dgpXY59crw3kI8MtdqD N85A== 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=cqXvzeH9JGbK13wh2PF5LuHVAWopqM5fCsamJ7eRinw=; b=jzqoXrg4/PexmC1ZJjWFjUgYMatMhn90w/5uMSiVRBhKdAJRVDNkr+Ik8zsmryLGIh oYZ5tXag2e2WwPxLQKDrOJB3TmmGWnB8+ZjNSbLWZvZTX74lgE5pFXlUIYbzGmvgrmb1 RS5qbDbbZfxdmgPpzqH8vljJKffjG6duHiibfsXuxY0/VVknzX7BLpFY899Z8wW/ggMy 31qh8YeHRCT2xyuZe1NZgaGVXXhKcycbgssDKzthcSqfTS1BGvTudLLx7QXgjwmQa81T wyvzO7tbiDigFdlQ5OrWizYupJjKpSPJ4n6u4Tgf48d7xsngOhdszyzlpOv/b/iHI09V 1rvA== X-Gm-Message-State: APjAAAWFXysf6wYj7zGdUTd+un6JhJF8yXYlV5XaRHs63tEDP6jLB2yR zx78NSSiQwaAbqzO7VFV+MFtrQm2 X-Google-Smtp-Source: APXvYqyUMmVcYv1AW3X0C4NXs+rGJKFZRlm1deAqpvJmqASPdVJt/nrfzUtC7PLBHdyNMrAAR3YBpg== X-Received: by 2002:a05:620a:1e4:: with SMTP id x4mr16266606qkn.150.1567457937206; Mon, 02 Sep 2019 13:58:57 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Mon, 2 Sep 2019 17:58:41 -0300 Message-Id: <20190902205842.1884-4-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190902205842.1884-1-danielhb413@gmail.com> References: <20190902205842.1884-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 v6 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, 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 | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/block/crypto.c b/block/crypto.c index 7eb698774e..fe2f0c42b0 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; @@ -599,6 +600,27 @@ fail: bdrv_unref(bs); qapi_free_QCryptoBlockCreateOptions(create_opts); qobject_unref(cryptoopts); + + /* + * If an error occurred, delete the file. Even if the file existed + * beforehand, it has been truncated and corrupted in the process. + */ + if (ret) { + Error *local_err; + int r_del =3D bdrv_delete_file(filename, &local_err); + /* + * ENOTSUP will happen if the block driver doesn't support + * 'bdrv_co_delete_file'. ENOENT will be fired by + * 'raw_co_delete_file' if the file doesn't exist. Both are + * predictable (we're not verifying if the driver supports + * file deletion or if the file was created), thus we + * shouldn't report this back to the user. + */ + if ((r_del < 0) && (r_del !=3D -ENOTSUP) && (r_del !=3D -ENOENT)) { + error_reportf_err(local_err, "%s: ", bs->filename); + } + } + return ret; } =20 --=20 2.21.0