From patchwork Tue Sep 3 13:57:05 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: 1157023 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Iz53mL1n"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46N7ys2h2Sz9s7T for ; Wed, 4 Sep 2019 00:05:57 +1000 (AEST) Received: from localhost ([::1]:46704 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i59RT-0004L9-4q for incoming@patchwork.ozlabs.org; Tue, 03 Sep 2019 10:05:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33756) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i59JF-0003LP-9p for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:57:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i59JC-0005iW-4B for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:57:23 -0400 Received: from mail-qt1-x843.google.com ([2607:f8b0:4864:20::843]:42396) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i59JB-0005hd-E4 for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:57:21 -0400 Received: by mail-qt1-x843.google.com with SMTP id t12so19982133qtp.9 for ; Tue, 03 Sep 2019 06:57:19 -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=ISRFJIcsQHvny5ehyZ7g2NR+kYMFconvzKsY4Lpa5sc=; b=Iz53mL1nw0oDH9yJIX2czma0m3exIDaGGevWWE60ww58fd2dSF/ksVCYqMF0fx+SZZ sHJJPLBA/l4VU55Y9bJlIxtXxf9AZOp0ucwl3b64UvlsR58cJFhVik2qm5iahigvBGbk DTPtsPLF+uJHpJu8mtly99ezJ8NwSbAoXueaJRy49Z9XoIsOEfej7JAQXPP0H+rsIgOv rciKmnRjOrFuX7n/pO01349rqkXOBjR6+5vSa4uqlwcQQEBFP05cHd9bV3mSzBvK03+u LRZrNvo6Z3javk0XiHRmnkULoMcMMp1dGlg3IYcRSRUsTAbUpe61ZP3lUeLsmtQlTtTe G1vQ== 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=ISRFJIcsQHvny5ehyZ7g2NR+kYMFconvzKsY4Lpa5sc=; b=S9CfzeLOVkspEvU1bXO++UPBB/4p+R8Ap6Xlg5M1eSqIV8RVJ9IzJDWdjKUYFOsEW/ ZnTjovCTXIDjod91qJT7x0zEx34xYdYV+n+glqsUcecMgTvKf4owkdYzm28lUrsi38DB gONpK0hyz43AggYZ4OHDqtuOClr7p52sttp3d732e7out9n+A4imsVBy3y4TiLRvJHRM fNFFEUj6kZmzBVd7KT/w+XP06RDauJR+CFut38rPJoTawc8sP671l7HFzkmGLd4GT0OX nvSy4X5DjoF9YBmfgp5rI8Q0pc0S8i90kuZCG3SKVso2X1xjIsHYPKTUQSSwxbBEenL4 5RmA== X-Gm-Message-State: APjAAAWpTHqteHiMF8sLlVbh85vlcEkp7eLkuKUsl82UsArdfxhhaCOq ukWbj8BtjUUVYIwzxVqmqnvH8UuKfLg= X-Google-Smtp-Source: APXvYqz5Wqcfsq/racGGRHgWqMXUKS6/s6I9j4X7CICowQrOafsruN1JTs7jec3UbUnODbG7nMt49Q== X-Received: by 2002:ac8:6745:: with SMTP id n5mr30498619qtp.68.1567519038775; Tue, 03 Sep 2019 06:57:18 -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.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2019 06:57:18 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Tue, 3 Sep 2019 10:57:05 -0300 Message-Id: <20190903135708.21624-2-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 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::843 Subject: [Qemu-devel] [PATCH v7 1/4] block: introducing 'bdrv_co_delete_file' interface 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, =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= , Daniel Henrique Barboza , jsnow@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Adding to Block Drivers the capability of being able to clean up its created files can be useful in certain situations. For the LUKS driver, for instance, a failure in one of its authentication steps can leave files in the host that weren't there before. This patch adds the 'bdrv_co_delete_file' interface to block drivers and add it to the 'file' driver in file-posix.c.The implementation is given by 'raw_co_delete_file'. Suggested-by: Daniel P. Berrangé Signed-off-by: Daniel Henrique Barboza --- block/file-posix.c | 28 ++++++++++++++++++++++++++++ include/block/block_int.h | 6 ++++++ 2 files changed, 34 insertions(+) diff --git a/block/file-posix.c b/block/file-posix.c index fbeb0068db..52756de522 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -2390,6 +2390,33 @@ static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts, return raw_co_create(&options, errp); } +/** + * Co-routine function that erases a regular file. + */ +static int coroutine_fn raw_co_delete_file(BlockDriverState *bs, + Error **errp) +{ + struct stat st; + int ret; + + if (!(stat(bs->filename, &st) == 0) || !S_ISREG(st.st_mode)) { + ret = -ENOENT; + error_setg_errno(errp, -ret, "%s is not a regular file", + bs->filename); + goto done; + } + + ret = unlink(bs->filename); + if (ret < 0) { + ret = -errno; + error_setg_errno(errp, -ret, "Error when deleting file %s", + bs->filename); + } + +done: + return ret; +} + /* * Find allocation range in @bs around offset @start. * May change underlying file descriptor's file offset. @@ -2942,6 +2969,7 @@ BlockDriver bdrv_file = { .bdrv_co_block_status = raw_co_block_status, .bdrv_co_invalidate_cache = raw_co_invalidate_cache, .bdrv_co_pwrite_zeroes = raw_co_pwrite_zeroes, + .bdrv_co_delete_file = raw_co_delete_file, .bdrv_co_preadv = raw_co_preadv, .bdrv_co_pwritev = raw_co_pwritev, diff --git a/include/block/block_int.h b/include/block/block_int.h index 0422acdf1c..a959ec2d1e 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -314,6 +314,12 @@ struct BlockDriver { */ int coroutine_fn (*bdrv_co_flush)(BlockDriverState *bs); + /* + * Delete a local created file. + */ + int coroutine_fn (*bdrv_co_delete_file)(BlockDriverState *bs, + Error **errp); + /* * Flushes all data that was already written to the OS all the way down to * the disk (for example file-posix.c calls fsync()). From patchwork Tue Sep 3 13:57:06 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: 1157019 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="KTH5il9s"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46N7rw3kLBz9s00 for ; Wed, 4 Sep 2019 00:00:48 +1000 (AEST) Received: from localhost ([::1]:46570 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i59MT-0006hR-SN for incoming@patchwork.ozlabs.org; Tue, 03 Sep 2019 10:00:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33758) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i59JF-0003LT-AX for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:57:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i59JC-0005ii-7I for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:57:25 -0400 Received: from mail-qk1-x743.google.com ([2607:f8b0:4864:20::743]:43717) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i59JB-0005i9-F5 for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:57:22 -0400 Received: by mail-qk1-x743.google.com with SMTP id m2so15971093qkd.10 for ; Tue, 03 Sep 2019 06:57:21 -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=WpmpFg50u8+Zk9hRBw0p/auazvjnZ6BkfdcA2q6nNXs=; b=KTH5il9sfnCYCI/v6QvRyUGTSPP39h2MqaArraeRgdX8GsLGYaceGzZv4iTdJbglFa Hm6V3Yvl5buwprF76WMcrWHxRXuvmTvgfSVwhOsy20+q/1DsCX3GnwWyrs+Wdi0ozUj+ EUjDtPZPPNbU6cEM8kgj6k3VAc67Wg3IuUQd2BsajASHioOl17hJ6yPrpKB0DE9sBpdR 1JZOhqGYcJ0PKFUeMDOPJIXE6DJY16DuL856Q9DT3VoRJ82O3Cm6RJJA13wX2BXbNgwK HKbyMm1hYit9IeFRJ0ThvzJCvCgLNYXfu5SdkOdV2MXnyrcIE+SyY55ULZ3mbYe5zli4 pgZw== 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=WpmpFg50u8+Zk9hRBw0p/auazvjnZ6BkfdcA2q6nNXs=; b=Phgdw0pJbe8LNyXeoZsLelPFyXV+s18s3T5RqJoIm4WcGhZsPp3lGrEUsH74ETnqln Z6oRVqtOdBsB2RMaciKXooAsgsduLf5jFXmMxqCdS2RAeuWnD6Akt6LC+XCNpPBsZHQj haqRPhhUERWfXWWLrGNrBhvcsH39sO8ofbRVtutKarh9zuGVV35JFUj8uP0VAbuWOy97 9wMihvEj6NsYtN6I8jEgY7S6RvaqP/G8PmmCY5elO3bMmx/WW0m8gH1coinfqvPOxI5p fraVS82r0px4/Rjv/oa980cgu7QZ/GqSXwdD1aWQ3BeCg1V57hEv7yqKSsEkOGvCZQ+v 1dkw== X-Gm-Message-State: APjAAAVCn3JibCeWfroalPRZvAAmpD7GY7wm0ohIgsh2RgFBy+LPaqWV 17q3RkXMY67402lQxyt0Vj+r98uE9kc= X-Google-Smtp-Source: APXvYqwF4o5v7gBhCtkLCSn944+5ze06oG4fhar0GoUi/8wqReYT1dEobO26gDsZ8tg+Z+RBZeTMmA== X-Received: by 2002:a37:9c81:: with SMTP id f123mr17201440qke.66.1567519040813; Tue, 03 Sep 2019 06:57:20 -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.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2019 06:57:20 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Tue, 3 Sep 2019 10:57:06 -0300 Message-Id: <20190903135708.21624-3-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 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::743 Subject: [Qemu-devel] [PATCH v7 2/4] block.c: adding bdrv_delete_file 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, =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= , Daniel Henrique Barboza , jsnow@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Using the new 'bdrv_co_delete_file' interface, bdrv_delete_file can be used in a way similar of the existing bdrv_create_file to to clean up a created file. The logic is also similar to what is already done in bdrv_create_file: a qemu_coroutine is created if needed, a specialized function bdrv_delete_co_entry is used to call the bdrv_co_delete_file co-routine of the driver, if the driver implements it. Suggested-by: Daniel P. Berrangé Signed-off-by: Daniel Henrique Barboza --- block.c | 73 +++++++++++++++++++++++++++++++++++++++++++ include/block/block.h | 1 + 2 files changed, 74 insertions(+) diff --git a/block.c b/block.c index 874a29a983..250c69ca7a 100644 --- a/block.c +++ b/block.c @@ -548,6 +548,79 @@ int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp) return ret; } +typedef struct DeleteCo { + BlockDriver *drv; + BlockDriverState *bs; + int ret; + Error *err; +} DeleteCo; + +static void coroutine_fn bdrv_delete_co_entry(void *opaque) +{ + Error *local_err = NULL; + DeleteCo *dco = opaque; + BlockDriver *drv = dco->bs->drv; + + assert(dco->bs); + + dco->ret = drv->bdrv_co_delete_file(dco->bs, &local_err); + error_propagate(&dco->err, local_err); +} + +int bdrv_delete_file(BlockDriverState *bs, Error **errp) +{ + DeleteCo dco = { + .bs = bs, + .ret = NOT_DONE, + .err = NULL, + }; + Coroutine *co; + int ret; + + if (!bs) { + error_setg(errp, "Could not open image '%s' for erasing", + bs->filename); + ret = -1; + goto out; + } + + if (!bs->drv) { + error_setg(errp, "File '%s' has unknown format", bs->filename); + ret = -ENOMEDIUM; + goto out; + } + + if (!bs->drv->bdrv_co_delete_file) { + error_setg(errp, "Driver '%s' does not support image delete", + bs->drv->format_name); + ret = -ENOTSUP; + goto out; + } + + if (qemu_in_coroutine()) { + /* Fast-path if already in coroutine context */ + bdrv_delete_co_entry(&dco); + } else { + co = qemu_coroutine_create(bdrv_delete_co_entry, &dco); + qemu_coroutine_enter(co); + while (dco.ret == NOT_DONE) { + aio_poll(qemu_get_aio_context(), true); + } + } + + ret = dco.ret; + if (ret < 0) { + if (dco.err) { + error_propagate(errp, dco.err); + } else { + error_setg_errno(errp, -ret, "Could not delete image"); + } + } + +out: + return ret; +} + /** * Try to get @bs's logical and physical block size. * On success, store them in @bsz struct and return 0. diff --git a/include/block/block.h b/include/block/block.h index 124ad40809..00fe8d6534 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -374,6 +374,7 @@ bool bdrv_is_backing_chain_frozen(BlockDriverState *bs, BlockDriverState *base, int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base, Error **errp); void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *base); +int bdrv_delete_file(BlockDriverState *bs, Error **errp); typedef struct BdrvCheckResult { From patchwork Tue Sep 3 13:57:07 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: 1157017 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="fBdp8HcF"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46N7pg5pYKz9s00 for ; Tue, 3 Sep 2019 23:58:50 +1000 (AEST) Received: from localhost ([::1]:46536 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i59KY-00048B-OC for incoming@patchwork.ozlabs.org; Tue, 03 Sep 2019 09:58:46 -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) 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) 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) 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 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 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" typedef struct BlockCrypto BlockCrypto; @@ -596,9 +597,30 @@ static int coroutine_fn block_crypto_co_create_opts_luks(const char *filename, ret = 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 = 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 != -ENOTSUP) && (r_del != -ENOENT)) { + error_reportf_err(local_err, "%s: ", bs->filename); + } + } + bdrv_unref(bs); qapi_free_QCryptoBlockCreateOptions(create_opts); qobject_unref(cryptoopts); + return ret; } From patchwork Tue Sep 3 13:57:08 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: 1157020 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Y6e6daJV"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46N7sn2TmBz9s00 for ; Wed, 4 Sep 2019 00:01:33 +1000 (AEST) Received: from localhost ([::1]:46594 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i59NC-0007ne-TM for incoming@patchwork.ozlabs.org; Tue, 03 Sep 2019 10:01:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33803) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i59JK-0003Nm-GV 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 1i59JJ-0005mA-1C for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:57:30 -0400 Received: from mail-qt1-x836.google.com ([2607:f8b0:4864:20::836]:43315) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i59JF-0005kg-88 for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:57:27 -0400 Received: by mail-qt1-x836.google.com with SMTP id l22so7767263qtp.10 for ; Tue, 03 Sep 2019 06:57:24 -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=3vBr1n1UsZgU8PgLgokhaXnjo/VGwWwRQ5hmljwJgVU=; b=Y6e6daJV9/1SfIAKAh6wC8Ew44SuR4x4sVPSyc1oyjfnPOXEbPpQYh6zhck3LCzwpr p/cTN2RXOe2Yr0mus4bA3xXKH+6eXU9l8uNJpw37V3aiyuh9uQ/979fy30spg1mGTAyK U3Ub3ZHx/2f95RLOP/1l4KA+hD9sRn4hEutd246RYOI7KCp92X0y6qkxDtFcGo6nPr/g 6bskxi5ZAhB5eF188URXfRQAJyhbQMKbY2MVHxtNmPdDQwaG3wmUgX0Kn7OyWV6daYc7 QiQSgpifsR9ofRrIXgZgN3jaZILdJ7Rzy+shim3fafqVTzTGgKifdQLeeQNyqq+4m0x8 iarA== 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=3vBr1n1UsZgU8PgLgokhaXnjo/VGwWwRQ5hmljwJgVU=; b=e8K4Hu0gkA6Y9j3/KMVMvqtDJMEyIRGkGf7cKQJQbuv5h3r7Z37hzgdRlFSgHL5p78 A2JYdfvdfpLpl8/4Eiw2YI7t1oJ2J53cis5z0jveUSIViI8wsWClevyVnzdFTywBBew6 lNBTIar30J9j55EvpCNrIDgBD3CGpvFmXMHJkf+i+x8QExZLEQV4BHIi/qnIJsLBIwRh i2htPpvTGv9+t3XRzAbRTfMZQGXNUjqPGK7G+z6OO3CXOg6tO2baUZGJeUerkq0bvTMT dgrYxWNfVDD6uz2epzi31/esLllkz6azR15rKlO+W4hljrEVD+ymChT7R9DsX/buJgl8 +yrA== X-Gm-Message-State: APjAAAUVjIKXkOLA9V+Ll9f/jsJTm+cYsiOYKBHrbmWEHRLfxTleM49w L7e3uBI5tNeQ5TR8zdApOtSjmXvE69E= X-Google-Smtp-Source: APXvYqwXqOzBobl+pwRFr7qAODjHZA419uA0lOXicNZIejbA58jkHlKMMTwpaNSzT2Ce59gT/8MKbw== X-Received: by 2002:ac8:6ce:: with SMTP id j14mr33704693qth.373.1567519044204; Tue, 03 Sep 2019 06:57:24 -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.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2019 06:57:23 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Tue, 3 Sep 2019 10:57:08 -0300 Message-Id: <20190903135708.21624-5-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 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::836 Subject: [Qemu-devel] [PATCH v7 4/4] qemu-iotests: adding LUKS cleanup for non-UTF8 secret error 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, Daniel Henrique Barboza , jsnow@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" This patch adds a new test file, 257, to exercise the case where qemu-img fails to complete for the LUKS format when a non-UTF8 secret is used. Signed-off-by: Daniel Henrique Barboza --- tests/qemu-iotests/259 | 67 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/259.out | 11 +++++++ tests/qemu-iotests/group | 1 + 3 files changed, 79 insertions(+) create mode 100755 tests/qemu-iotests/259 create mode 100644 tests/qemu-iotests/259.out diff --git a/tests/qemu-iotests/259 b/tests/qemu-iotests/259 new file mode 100755 index 0000000000..cb362598b4 --- /dev/null +++ b/tests/qemu-iotests/259 @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +# +# Test qemu-img file cleanup for LUKS when using a non-UTF8 secret +# +# Copyright (C) 2019, IBM Corporation. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +seq=`basename $0` +echo "QA output created by $seq" + +status=1 # failure is the default! +TEST_IMAGE_FILE='vol.img' + +_cleanup() +{ + _cleanup_test_img + rm non_utf8_secret + rm -f $TEST_IMAGE_FILE +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +. ./common.rc +. ./common.filter + +_supported_fmt luks +_supported_proto generic +_unsupported_proto vxhs + +echo "== Create non-UTF8 secret ==" +echo -n -e '\x3a\x3c\x3b\xff' > non_utf8_secret +SECRET="secret,id=sec0,file=non_utf8_secret" + +echo "== Throws an error because of invalid UTF-8 secret ==" +$QEMU_IMG create -f $IMGFMT --object $SECRET -o "key-secret=sec0" $TEST_IMAGE_FILE 4M + +echo "== Image file should not exist after the error ==" +if test -f "$TEST_IMAGE_FILE"; then + exit 1 +fi + +echo "== Create a stub image file and run qemu-img again ==" +touch $TEST_IMAGE_FILE +$QEMU_IMG create -f $IMGFMT --object $SECRET -o "key-secret=sec0" $TEST_IMAGE_FILE 4M + +echo "== Pre-existing image file should also be deleted after the error ==" +if test -f "$TEST_IMAGE_FILE"; then + exit 1 +fi + +# success, all done +echo "*** done" +rm -f $seq.full +status=0 diff --git a/tests/qemu-iotests/259.out b/tests/qemu-iotests/259.out new file mode 100644 index 0000000000..6b0188111c --- /dev/null +++ b/tests/qemu-iotests/259.out @@ -0,0 +1,11 @@ +QA output created by 259 +== Create non-UTF8 secret == +== Throws an error because of invalid UTF-8 secret == +qemu-img: vol.img: Data from secret sec0 is not valid UTF-8 +Formatting 'vol.img', fmt=luks size=4194304 key-secret=sec0 +== Image file should not exist after the error == +== Create a stub image file and run qemu-img again == +qemu-img: vol.img: Data from secret sec0 is not valid UTF-8 +Formatting 'vol.img', fmt=luks size=4194304 key-secret=sec0 +== Pre-existing image file should also be deleted after the error == + *** done diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group index d95d556414..8f50bd91e5 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -273,4 +273,5 @@ 256 rw quick 257 rw 258 rw quick +259 rw img quick 262 rw quick migration