From patchwork Fri Jun 28 19:45:09 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: 1124497 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="vTkidy8p"; 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 45b6jQ2nPfz9s4V for ; Sat, 29 Jun 2019 05:47:04 +1000 (AEST) Received: from localhost ([::1]:35930 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hgwpo-0000hd-LU for incoming@patchwork.ozlabs.org; Fri, 28 Jun 2019 15:47:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34394) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hgwoP-0000cI-Ge 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-0000w6-Ny for qemu-devel@nongnu.org; Fri, 28 Jun 2019 15:45:33 -0400 Received: from mail-qt1-x844.google.com ([2607:f8b0:4864:20::844]:35538) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hgwoK-0000tS-GH for qemu-devel@nongnu.org; Fri, 28 Jun 2019 15:45:30 -0400 Received: by mail-qt1-x844.google.com with SMTP id d23so7690651qto.2 for ; Fri, 28 Jun 2019 12:45:27 -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=fo3Hzp32nsqM8Vt21whJF5z18eDh+DMJbuyQcmDsrqE=; b=vTkidy8pFwISB+yI/LnHbWMmSTsqr067wxfE/72uxcR3FTQc5nbpg9ONNWIf08zi2L BF+Nmv7NxYgn86pSh+l+joyISty4zGUs0vaMlI+Z38RX43f+BxgOADoBsJr0l7a89biN DBIopTKpD+sxhpnTqct6+7JyUo8E7lPXYKXuOnqOh2yhO6C4hvvL9u5ewnyb8vcmoS/W Q4H6L1YGk1JcX8gjMD07YrhGr2w4Q8d0tDKobE5dSu8k1Ly1xd4uG6H//1kUcGF67ttB IWJmyf5IIjNlHOLMAxQbNzRdX+lj4qaDwfpdnNkXA7P0ybTZvV5EOiIPnxx7xDFEBZ1r aE1g== 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=fo3Hzp32nsqM8Vt21whJF5z18eDh+DMJbuyQcmDsrqE=; b=MbD1HMSJhh8VzyGcyvnKamA8LrAP11s8iGTlG41nENzOLIYdtV94C9Oew7CzERd0l4 hCDijTM3n6pIJQZ/gXL4ue7Z2U0b9SvppOczMBr/U+pbVRWiVa7T957tS9xjWl5zj7OI J0WwtapaG/r8GOf4lq2IWK8nt7NWt0IICJWlbd7PCBZJTn1G/ZWr56JC1CfQhtG5S0VJ NGMFeH98VmU/zDge4gwIvlj49ItPP6bDN5bUVwSR1BFr4o6ZBo0na2G3Kd+sym5A4HMY hhUmIVlaFD437e5O6xDez7qLGd6xZrZSY0+JzLplhhg+Zo96Oeix9a69eKTvsLn6uNMZ OSNg== X-Gm-Message-State: APjAAAUerdB2SLaRCjiZeHpZe12QlFSrBpTdw4m/q3+B9j3UWeQ6x03u jjVNSLbLCTTmANq0x537qPcR3fuA X-Google-Smtp-Source: APXvYqyaHEsEhQfpeTsQNjWd3SADfHr6m6c/1k/QQjBa31/NXH2+xjZ/BlmIQnUmqAnN52pKKImvWQ== X-Received: by 2002:ac8:27d4:: with SMTP id x20mr9465269qtx.138.1561751125916; Fri, 28 Jun 2019 12:45:25 -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.23 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 28 Jun 2019 12:45:25 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Fri, 28 Jun 2019 16:45:09 -0300 Message-Id: <20190628194512.21311-2-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 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::844 Subject: [Qemu-devel] [PATCH v4 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, jsnow@redhat.com, Daniel Henrique Barboza , berrange@redhat.com, mreitz@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'. The helper 'bdrv_path_is_regular_file' is being used only in raw_co_delete_file at this moment, but it will be used inside LUKS in a later patch. Foreseeing this future use, let's put it in block.c and make it public. Suggested-by: Daniel P. Berrangé Signed-off-by: Daniel Henrique Barboza --- block.c | 11 +++++++++++ block/file-posix.c | 28 ++++++++++++++++++++++++++++ include/block/block.h | 1 + include/block/block_int.h | 6 ++++++ 4 files changed, 46 insertions(+) diff --git a/block.c b/block.c index c139540f2b..6e2b0f528d 100644 --- a/block.c +++ b/block.c @@ -621,6 +621,17 @@ int get_tmp_filename(char *filename, int size) #endif } +/** + * Helper that checks if a given string represents a regular + * local file. + */ +bool bdrv_path_is_regular_file(const char *path) +{ + struct stat st; + + return (stat(path, &st) == 0) && S_ISREG(st.st_mode); +} + /* * Detect host devices. By convention, /dev/cdrom[N] is always * recognized as a host CDROM. diff --git a/block/file-posix.c b/block/file-posix.c index ab05b51a66..c8a0b109c2 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -2374,6 +2374,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(const char *filename, + Error **errp) +{ + int ret; + + /* Skip file: protocol prefix */ + strstart(filename, "file:", &filename); + + if (!bdrv_path_is_regular_file(filename)) { + ret = -ENOENT; + error_setg_errno(errp, -ret, "%s is not a regular file", filename); + goto done; + } + + ret = unlink(filename); + if (ret < 0) { + ret = -errno; + error_setg_errno(errp, -ret, "Error when deleting file %s", filename); + } + +done: + return ret; +} + /* * Find allocation range in @bs around offset @start. * May change underlying file descriptor's file offset. @@ -2925,6 +2952,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.h b/include/block/block.h index f9415ed740..d287eaa9a6 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -370,6 +370,7 @@ int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base, Error **errp); void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *base); +bool bdrv_path_is_regular_file(const char *path); typedef struct BdrvCheckResult { int corruptions; diff --git a/include/block/block_int.h b/include/block/block_int.h index d6415b53c1..6d4135ff54 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -309,6 +309,12 @@ struct BlockDriver { */ int coroutine_fn (*bdrv_co_flush)(BlockDriverState *bs); + /* + * Delete a local created file. + */ + int coroutine_fn (*bdrv_co_delete_file)(const char *filename, + 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 Fri Jun 28 19:45:10 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: 1124498 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="GZZbvRGq"; 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 45b6jQ3gkCz9s5c for ; Sat, 29 Jun 2019 05:47:03 +1000 (AEST) Received: from localhost ([::1]:35932 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hgwpo-0000ln-JY for incoming@patchwork.ozlabs.org; Fri, 28 Jun 2019 15:47:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34393) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hgwoP-0000cH-Gr 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-0000wJ-Oa for qemu-devel@nongnu.org; Fri, 28 Jun 2019 15:45:33 -0400 Received: from mail-qt1-x842.google.com ([2607:f8b0:4864:20::842]:45857) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hgwoL-0000uB-7T for qemu-devel@nongnu.org; Fri, 28 Jun 2019 15:45:30 -0400 Received: by mail-qt1-x842.google.com with SMTP id j19so7638145qtr.12 for ; Fri, 28 Jun 2019 12:45:28 -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=3dRB10KTGUHWpNRks8/1E0ikbIsIwvHCZ00Qw4pSDZU=; b=GZZbvRGqduvOz3B9qUnr4TnV05HDz4oUZMXERzuYI+jMa0QsF+3rMdcwGUHw34NHXd lITomlurNAGYbloUnSdKrHusUO+Cg4BexV4Lbj/cYUhqZPLc8ENS9QinY0RVwlrNqHRq hLH+VjaLa8Zov6MgbP6gA9I3fGturZrDpkFLzgBwn1o7t0byCgdueS9KuL1pH7FFV7PP wzUFhID0KnPPgWehA0Mnsi5ICBtEshGqJ71k2MA06I4h7HZoevjqPU/7+4fGO/Lhx6Wi kieA+kYP2K4wl7xJkK34/V8TmxtVgNxWdOK4UaucVWVxoS0GWWAGLBR9vjpH4OfGdE61 4TYg== 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=3dRB10KTGUHWpNRks8/1E0ikbIsIwvHCZ00Qw4pSDZU=; b=r9LD6YCpwiEXYnVOQf6FBYm7vWTZfJ2YQJaPcQYpxZY9BUCH6iqx1iImo2wH1QBhPv 171VL2oaMIzsU/B/CWT0D5AImyHoLYEIGl0yz7S8X7t1hgh/rnAvnWOkRuz0vQMm+Q70 0+ZpFhtr85fsuWPFojO94T6IngOfKSW7HXv4qNrIEJ6H0Hj8wcRzcN55B40lnozxd0bf qwoEUvVuYWbvylnqEo4uhO/xgvBxUFoEg1WcBZWFxVxF8jDgWcAPFbOk61ownNj6Y8Zb 7NubmFduPauHsG3D6x6z+2n8uREgxoWF9dRqKgYmzsd9Q3W3QKiYwbBKzNUwjDCXs8jB lD0g== X-Gm-Message-State: APjAAAWdrQPLGF0lV3btersjCXRKUmwuF3WSMoGDZws6Y+qLF09sKguO poW+7X7U7Po865A9Xmql/EdazU3C X-Google-Smtp-Source: APXvYqwQ+hvlU5il0QFnGfKApgU/hDkHqjUSCX1vzQvPS4L8x3YU65SOQJLOsP/td+9LPaUr/xYp/Q== X-Received: by 2002:ac8:2dcf:: with SMTP id q15mr9785228qta.170.1561751127617; Fri, 28 Jun 2019 12:45:27 -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.26 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 28 Jun 2019 12:45:27 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Fri, 28 Jun 2019 16:45:10 -0300 Message-Id: <20190628194512.21311-3-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 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::842 Subject: [Qemu-devel] [PATCH v4 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, jsnow@redhat.com, Daniel Henrique Barboza , berrange@redhat.com, mreitz@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 | 71 +++++++++++++++++++++++++++++++++++++++++++ include/block/block.h | 2 ++ 2 files changed, 73 insertions(+) diff --git a/block.c b/block.c index 6e2b0f528d..11675cfcee 100644 --- a/block.c +++ b/block.c @@ -547,6 +547,77 @@ int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp) return ret; } +static void coroutine_fn bdrv_delete_co_entry(void *opaque) +{ + Error *local_err = NULL; + int ret; + + CreateCo *cco = opaque; + assert(cco->drv); + + ret = cco->drv->bdrv_co_delete_file(cco->filename, &local_err); + error_propagate(&cco->err, local_err); + cco->ret = ret; +} + +int bdrv_delete_file(const char *filename, Error **errp) +{ + + BlockDriver *drv = bdrv_find_protocol(filename, true, errp); + CreateCo cco = { + .drv = drv, + .filename = g_strdup(filename), + .ret = NOT_DONE, + .err = NULL, + }; + Coroutine *co; + int ret; + + if (!drv) { + error_setg(errp, "File '%s' has unknown format", filename); + ret = -ENOENT; + goto out; + } + + if (!drv->bdrv_co_delete_file) { + error_setg(errp, "Driver '%s' does not support image delete", + drv->format_name); + ret = -ENOTSUP; + goto out; + } + + if (!drv->bdrv_co_delete_file) { + error_setg(errp, "Driver '%s' does not support image delete", + drv->format_name); + ret = -ENOTSUP; + goto out; + } + + if (qemu_in_coroutine()) { + /* Fast-path if already in coroutine context */ + bdrv_delete_co_entry(&cco); + } else { + co = qemu_coroutine_create(bdrv_delete_co_entry, &cco); + qemu_coroutine_enter(co); + while (cco.ret == NOT_DONE) { + aio_poll(qemu_get_aio_context(), true); + } + } + + ret = cco.ret; + if (ret < 0) { + if (cco.err) { + error_propagate(errp, cco.err); + } else { + error_setg_errno(errp, -ret, "Could not delete image"); + } + } + +out: + g_free(cco.filename); + 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 d287eaa9a6..5747f2a060 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -371,6 +371,8 @@ int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base, void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *base); bool bdrv_path_is_regular_file(const char *path); +int bdrv_delete_file(const char *filename, Error **errp); + typedef struct BdrvCheckResult { int corruptions; From patchwork Fri Jun 28 19:45:11 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: 1124501 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="FiQgE6kk"; 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 45b6qV326Zz9s4V for ; Sat, 29 Jun 2019 05:52:18 +1000 (AEST) Received: from localhost ([::1]:35986 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hgwut-00040A-Ic for incoming@patchwork.ozlabs.org; Fri, 28 Jun 2019 15:52:15 -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) 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) 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) 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 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+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 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" typedef struct BlockCrypto BlockCrypto; @@ -535,6 +536,8 @@ static int coroutine_fn block_crypto_co_create_opts_luks(const char *filename, BlockDriverState *bs = NULL; QDict *cryptoopts; int64_t size; + const char *path; + bool file_already_existed = false; int ret; /* Parse options */ @@ -551,6 +554,15 @@ static int coroutine_fn block_crypto_co_create_opts_luks(const char *filename, goto fail; } + /* + * 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 = filename; + strstart(path, "file:", &path); + file_already_existed = bdrv_path_is_regular_file(path); + /* Create protocol layer */ ret = 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 = 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 != -ENOTSUP) && (r_del != -ENOENT)) { + error_reportf_err(local_err, "%s: ", path); + } + } + return ret; } From patchwork Fri Jun 28 19:45:12 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: 1124500 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="oMkOEByA"; 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 45b6qV4nvRz9s8m for ; Sat, 29 Jun 2019 05:52:18 +1000 (AEST) Received: from localhost ([::1]:35988 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hgwuv-00046H-2m for incoming@patchwork.ozlabs.org; Fri, 28 Jun 2019 15:52:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34427) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hgwoS-0000gl-VL for qemu-devel@nongnu.org; Fri, 28 Jun 2019 15:45:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hgwoP-0000yg-M9 for qemu-devel@nongnu.org; Fri, 28 Jun 2019 15:45:36 -0400 Received: from mail-qk1-x72e.google.com ([2607:f8b0:4864:20::72e]:45202) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hgwoP-0000xZ-Fl for qemu-devel@nongnu.org; Fri, 28 Jun 2019 15:45:33 -0400 Received: by mail-qk1-x72e.google.com with SMTP id s22so5876095qkj.12 for ; Fri, 28 Jun 2019 12:45:32 -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=ERGUElZlTYh7HYWiUP5pCt+6UgNRD2a1NhzEKHWJLUY=; b=oMkOEByAS+tWexLBQIxOJLYY3x9467f3WEbuea5kx3ZdIGvCrDXKWa/n/f0+Eul3IW yWOzfG6Y6IqbPEdnLwjZyKfs5zbWafYf7MyigfEtrJ2TSUkDlf4QhBEE4cx11kClMUnM qHSIOzVxrB7Qdf/cO1XnlDxK1cSVd/OADEpPsgaeKxWzjXnDlSzvufITi//ffnQUpeef GYfiMLkHyhOqHNb4zdRvrhJuIBZZZMSlo8kUJ1rn/9/Ti8BLWpqFFCC9naefnq5K6scS eI3nBiI1IWIZptrUd1CS1A5sdWJH3El7MoF9ho778PReOeHme899L5YljwHkGMiVK+we tdkQ== 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=ERGUElZlTYh7HYWiUP5pCt+6UgNRD2a1NhzEKHWJLUY=; b=NkskaaloMb1zjRUYIBZmuzfZEeXUMM7Sz11tIqKSrY1/pQ6y7l2yiR6hXtgntyYPQA fW8T+WK9uuFmIc/H/+03m4bUz3Cw8aAfgQolZ9JD6j9oPerZP/OkP5jkwUcZgqwTUxpV mGea+ddXl19sXXHAKmbmy4BPyAXoHxFXHMfKS5jzuGtcQuMhCj/0chInVyJxMv6moKvF zW6wB5oAapbg0lsyCnro0SUksfdu9+THFmgLT5WNIQbt4m075O0ffEiie3QKszBBYmtQ lCkyU70Nh9X+bcSg2ofwb+8jvfi9tW8Xrecxo2KC3LCVwyKrMwkbhBnl3fHq0WflqmbD n9Lg== X-Gm-Message-State: APjAAAVMvsAXMaTs08Bsev8OenyYD4KewH7muZOsktH90YvCeU4vOfjY 7vmf7cOk+BRJLckbWSCpgehR8tz4 X-Google-Smtp-Source: APXvYqwBc0O8YBBgu0oC6qdTr5qqvdkPsSy3Li0BjP5MwnAB5OLjVFb/YNjD2uYIzB82ZnW7zWIleg== X-Received: by 2002:a37:5cc3:: with SMTP id q186mr10123699qkb.74.1561751132091; Fri, 28 Jun 2019 12:45:32 -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.29 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 28 Jun 2019 12:45:31 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Fri, 28 Jun 2019 16:45:12 -0300 Message-Id: <20190628194512.21311-5-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 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::72e Subject: [Qemu-devel] [PATCH v4 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, jsnow@redhat.com, Daniel Henrique Barboza , berrange@redhat.com, mreitz@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. If using an existing image file, do not erase it. If the file was created by the failed qemu-img call, clean it up. Signed-off-by: Daniel Henrique Barboza --- tests/qemu-iotests/257 | 67 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/257.out | 11 +++++++ tests/qemu-iotests/group | 1 + 3 files changed, 79 insertions(+) create mode 100755 tests/qemu-iotests/257 create mode 100644 tests/qemu-iotests/257.out diff --git a/tests/qemu-iotests/257 b/tests/qemu-iotests/257 new file mode 100755 index 0000000000..2402217619 --- /dev/null +++ b/tests/qemu-iotests/257 @@ -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 can not 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/257.out b/tests/qemu-iotests/257.out new file mode 100644 index 0000000000..5efeb1ed29 --- /dev/null +++ b/tests/qemu-iotests/257.out @@ -0,0 +1,11 @@ +QA output created by 257 +== 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 can not be deleted after the error == + *** done diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group index b34c8e3c0c..00fbfefc8e 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -269,3 +269,4 @@ 254 rw auto backing quick 255 rw auto quick 256 rw auto quick +257 rw auto quick