From nobody Wed Nov 12 03:40:44 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=1567519187; cv=none; d=zoho.com; s=zohoarc; b=k8AQFW77zt3kMABntHsgAwIvivo+rV3Avlis9q8381CyGhEOFohYdojYPpRCrFpf2e0DNeoGtBoCd5QnjApscNgXRTz1LQvWV8Cw9gZwdbc0Mxcwg4SBWibYKdRGIbdLzUYUuqfndO5i7UwsSAREpuotqNlo3RaPtzEu9mFbzSk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1567519187; 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=VQHc2Ows/HevyIUsCH9uoWSWREQDTgPtdPClqT9JG70=; b=NE/MkdHOMqcXg12ZRIsOLzDtQP4463KI/U2kiY0WTKZqAkHMhwUsDD4gd874ke+2RGIb0Oqpbt4oAvGznNXxTjj38ZXO0VqC28PSu9LAdAY2SSPeEs8cXVjvezVG5DpQcLTWrd8adP+phvw+d5nEZ7yW2r/3j+DU+Q6FfWfNnbI= 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 1567519187764745.5177039251532; Tue, 3 Sep 2019 06:59:47 -0700 (PDT) Received: from localhost ([::1]:46550 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i59LV-0005pB-T2 for importer@patchew.org; Tue, 03 Sep 2019 09:59:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33796) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i59JK-0003Nh-3Q for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:57:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i59JI-0005lx-Vp for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:57:30 -0400 Received: from mail-qk1-x744.google.com ([2607:f8b0:4864:20::744]:38627) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i59JD-0005it-Fx for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:57:25 -0400 Received: by mail-qk1-x744.google.com with SMTP id x5so1898288qkh.5 for ; Tue, 03 Sep 2019 06:57:23 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c7:5c9f:9471:e230:52b:8c93]) by smtp.gmail.com with ESMTPSA id k49sm9211158qtc.9.2019.09.03.06.57.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2019 06:57:22 -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=VQHc2Ows/HevyIUsCH9uoWSWREQDTgPtdPClqT9JG70=; b=fBdp8HcFBZf8xRfN+wQNp0oWeJ9yZjxpS3nIw5NJodmtDzlfl1Dz652RlXna4aIcry o4oFUQPWbdq6xL2Lkt8ePJZhfXjI+eTEA+WebGAK31nEuf12rT6EEHSHh/wo2jBtEZEb 5i8Rh8weZgwvwMq4wWh09K5R36HHA2bnudHECGcR86GMuaeMoZHO6iPxEe+VT8RUAXiL Tgl1tFWu0cz9iJzkKWZnmhxn4/fJEPretkrhFuCVqMyDFKGy8wIsGuH/rCFOgFJQftQU p0McwitjGtXHVmWBOXn8YIXF/ETDNuDzwhSqjOO5FKIXCFAzQK8CL3vker6ztiPcDVQu s9dw== 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=VQHc2Ows/HevyIUsCH9uoWSWREQDTgPtdPClqT9JG70=; b=VK6/ZJ/7Fe3t1nSl5A1horN5Ky02g4Iq3SHbC/bdn7bE7o7dai0EgK/AhUckxtJmHU 493lKbxUbzTKHWDrJ5MuN2KK+A9uhT8BweoKHUrrDgd05zOvagPq+oSffEBDwiT+zC32 VH0tNvNaA+9EXvCuWwSY72Ia810hXuf+bTroi5L0p+spUvANsLwAkxWraPOLbbmDAp0t KXT0ye7I+U7sWTP/z6m8nPp3PDcduG6IPBsi4La0QHAdbda4Mo+Gsk8s/I1ae4M7jvQc +A9mIikQw7Ym5HddreMJBWIrL1w7vCS6yZgC/nb3qB+ugzipsMbwC5+00rykwLjGzdpz /Qig== X-Gm-Message-State: APjAAAWki1GTD6FQZlQuftXrIa7M9ClyugZXdCSY5wNx/NEyvX8LhjE0 rs5ErzNaJ4sGMHNs1cpkT2k5iJQdLVc= X-Google-Smtp-Source: APXvYqzpt50s4qr8Fz7mD+eHe76et6O8b/5kwtWHW0SS2uM92FRDA5kOXX7ZxTQ0V14VYQYYQyg4Zw== X-Received: by 2002:a37:a44f:: with SMTP id n76mr22731968qke.414.1567519042547; Tue, 03 Sep 2019 06:57:22 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Tue, 3 Sep 2019 10:57:07 -0300 Message-Id: <20190903135708.21624-4-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190903135708.21624-1-danielhb413@gmail.com> References: <20190903135708.21624-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::744 Subject: [Qemu-devel] [PATCH v7 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..29496d247e 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,9 +597,30 @@ 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) { + Error *local_err; + int r_del =3D bdrv_delete_file(bs, &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); + } + } + bdrv_unref(bs); qapi_free_QCryptoBlockCreateOptions(create_opts); qobject_unref(cryptoopts); + return ret; } =20 --=20 2.21.0