From patchwork Fri Mar 22 17:52:41 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: 10866383 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 EA8C714DE for ; Fri, 22 Mar 2019 18:04:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D1D492A5AE for ; Fri, 22 Mar 2019 18:04:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C56162A63F; Fri, 22 Mar 2019 18:04:21 +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 49E572A5AE for ; Fri, 22 Mar 2019 18:04:21 +0000 (UTC) Received: from localhost ([127.0.0.1]:32790 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h7OWi-0003t0-69 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 22 Mar 2019 14:04:20 -0400 Received: from eggs.gnu.org ([209.51.188.92]:33916) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h7OVD-0002bl-R6 for qemu-devel@nongnu.org; Fri, 22 Mar 2019 14:02:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h7OM0-0000ZN-DK for qemu-devel@nongnu.org; Fri, 22 Mar 2019 13:53:18 -0400 Received: from mail-ed1-x543.google.com ([2a00:1450:4864:20::543]:39396) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h7OLw-0000Wz-Sa; Fri, 22 Mar 2019 13:53:14 -0400 Received: by mail-ed1-x543.google.com with SMTP id p20so1916246eds.6; Fri, 22 Mar 2019 10:53:12 -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=9hdvoLW4ZTdzR9lWQHstAXQ0KRxp/0Xdkn2qHioSK8I=; b=rcqmeD+697AJVtyq8TjM9tEYBzmfimQbikGrywOW0aqBerJUw3Pc/3GEO2la2oRgEN Lh/Sx7TsaK4ftGnEHnQCYZnssz97M2eAh2/+BSVm4cVH3fezns10WzOAxtRtd5/2NdiX JdGfV+eTPmJSgKDuhv3ZkACkWDd5QwARglKK7F6WP8Fm8n1cHoKqOHaetVi0TLctdk82 v+2jX/gDNC6HNttGsTUjgqGEXqUPSZWWI1SMIe4eCnduj9whZof2oVJAQVOtHrPFmSEF IlMWWMtxIAFaBMwoIklteavV8FWONv125gpCZTdHhmG2/NQVyD0cX9EEHIsZOyk8qMM6 7+QQ== 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=9hdvoLW4ZTdzR9lWQHstAXQ0KRxp/0Xdkn2qHioSK8I=; b=Sh3QvR8j6r2mhQmek3A5wCVS57zeRbG+/FiwXhBbXrYipMK2UcoNyO4loU6cpTpioR uDhjke0oYa2yiwA9icvicNsarn1GDonZO8IX+yY0tRzurqWfU/uJ0VHZXywQvsxVy3Gs p/y6L1ia+Jvo4D/jPXW0VsYjYiBKm3AFiAjj/qR4qRvq2us4I8WyoTAx10rIKhIPFYSL WjPC1KoUmfLGync/gdW2/dIf7963BxM/ps8ddsjFmv/ywIjneqd+ypw/dDKING7CRV3G HnJ+/t35jqqgLPPHt1/qJ+rcCTSjqk1iBHs/P0HH49FzcbiWzggYdvA2HUQFH43U/EU2 O0VQ== X-Gm-Message-State: APjAAAXplrckAEq8mmK7Y+a9Xh2ZIMq0XSX8uoodQgdFFR9V19TTahQh rSWsBixCDZ6Scg9SAATXkELJWpKK X-Google-Smtp-Source: APXvYqwxFpbk9uFyKqp0n5cjC8AksjBnFbhVOu3nXuCZ9Bj4ON7WQp1IRRh9YpFPaqVmP2OHuZhz8A== X-Received: by 2002:a17:906:49d9:: with SMTP id w25mr6200066ejv.52.1553277191639; Fri, 22 Mar 2019 10:53:11 -0700 (PDT) Received: from rekt.ibmmodules.com ([2804:431:f700:2d8:6f86:51db:ba10:5f2f]) by smtp.gmail.com with ESMTPSA id l26sm1790053eja.29.2019.03.22.10.53.08 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 22 Mar 2019 10:53:11 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Fri, 22 Mar 2019 14:52:41 -0300 Message-Id: <20190322175241.5954-4-danielhb413@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190322175241.5954-1-danielhb413@gmail.com> References: <20190322175241.5954-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: 2a00:1450:4864:20::543 Subject: [Qemu-devel] [PATCH v2 3/3] qemu-img.c: clean up created file on img_create failure 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 img_create to check if @filename is an existing file before bdrv_img_create 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: Daniel P. Berrangé Signed-off-by: Daniel Henrique Barboza --- qemu-img.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/qemu-img.c b/qemu-img.c index 5fac840742..03b139b4ac 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -422,11 +422,12 @@ static int img_create(int argc, char **argv) uint64_t img_size = -1; const char *fmt = "raw"; const char *base_fmt = NULL; - const char *filename; + const char *filename, *path; const char *base_filename = NULL; char *options = NULL; Error *local_err = NULL; bool quiet = false; + bool file_already_existed = false; int flags = 0; for(;;) { @@ -529,6 +530,15 @@ static int img_create(int argc, char **argv) error_exit("Unexpected argument: %s", argv[optind]); } + /* + * Check if 'filename' represents a local file that already + * exists in the file system prior to bdrv_img_create. 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); + bdrv_img_create(filename, fmt, base_filename, base_fmt, options, img_size, flags, quiet, &local_err); if (local_err) { @@ -541,6 +551,23 @@ static int img_create(int argc, char **argv) fail: g_free(options); + /* + * If an error occurred and we ended up creating a bogus + * 'filename' file, delete it + */ + if (!file_already_existed && bdrv_path_is_regular_file(path)) { + + int ret = bdrv_delete_file(path, fmt, &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 ((ret < 0) && (ret != -ENOTSUP) && (ret != -ENOENT)) { + error_reportf_err(local_err, "%s: ", filename); + } + } return 1; }