From nobody Wed Nov 27 00:21:40 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1703482259; cv=none; d=zohomail.com; s=zohoarc; b=b03QWRorYNtztk0ZRdwV6aM5Nh2jE4Ska82wieNjvtm27l9ZC88o5KyYjuYL+Qkz6X70SDNm8dnOACLMyQIswQCz6hqKySfkCc0/EL0NPMkcLEbjaqeG3PchRQ7fQig4I1mbFLNaSfWhBxaz2NXUvZkXa+Qn5bofCxJR9q/ZpUo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1703482259; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=L3ApTkAuvEEyKhHkfQqZ7GJ+q36mBNZFj8dE6xUr2+E=; b=iERSjho8YKXH2f7Q8f94dLrg+lx/PxyABos6VPNpfk09Bs4mO01puWEYEJfAkBmBPCMD/3vmCtvsmDY7IObw34IiIJO0bIFc2qdKN74AeWgPfKWaDWa8J4/vI8QHA5sEcE1MyQaqImPICKvNuCdM0prEt+n/zCRn+8Ua+5pGw90= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1703482259158666.9271631548467; Sun, 24 Dec 2023 21:30:59 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rHdXA-00055j-42; Mon, 25 Dec 2023 00:29:48 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rHdX8-00055L-JD for qemu-devel@nongnu.org; Mon, 25 Dec 2023 00:29:46 -0500 Received: from mail-oi1-x231.google.com ([2607:f8b0:4864:20::231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rHdX6-0007Jt-QY for qemu-devel@nongnu.org; Mon, 25 Dec 2023 00:29:46 -0500 Received: by mail-oi1-x231.google.com with SMTP id 5614622812f47-3ba52d0f9feso2842132b6e.0 for ; Sun, 24 Dec 2023 21:27:44 -0800 (PST) Received: from localhost.localdomain ([118.114.58.28]) by smtp.gmail.com with ESMTPSA id f7-20020aa79d87000000b006d991505b4csm4555800pfq.76.2023.12.24.21.27.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Dec 2023 21:27:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20230601.gappssmtp.com; s=20230601; t=1703482063; x=1704086863; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=L3ApTkAuvEEyKhHkfQqZ7GJ+q36mBNZFj8dE6xUr2+E=; b=FgOgfScYgrhbPsMfDBNd8TGhAu/srDlBbeQbwVZM8pkX5g6B5S4rBzkB9qmL/+waSu 2xQCcrRLE2wLTgF7/1mTDxOHeI+TLYVeqd4mdW52QtxA/AEm3h7WfxjhD1bQVRc018Dz DkgqT3yB262yVJlM/H/qInUuMF78/03xb/Ml8u8HXQnj+tk6BB+KEopNU6tQnM7AuRjI LlXCIoYPnlaXbOMSsuOanG+DwZ3KMGFCcgtPL3+wvS4y6x0IA+NZXOBGb8BHVp02XsBV AiAtTrklNERGn2qkt4QtM8yyBfXLef/Bog8h4r2F6Z88w8Pz3l2hxw5N8c7U0wXtrQzs SmKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703482063; x=1704086863; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=L3ApTkAuvEEyKhHkfQqZ7GJ+q36mBNZFj8dE6xUr2+E=; b=qNaJzUn3luA96V9ESIZBSpRMDjOhOXjXMP91I9m+A1TqRBr6WSTNboppRLAZ8xx+2k Gv1fId0PWISnzQzmXONm92g7vEic+FghqQme1cFkiE1d2AYpyguRsAoXSHDsxdRZZiZe 7KwUH6oA/LIZtUeNvnOOZUXt9PkzyV6jO9n9PHWILKcCf/HZi74RsdaVlP15b7ni1oWz wXBOAhZqz4u/nIox7EuK250pd9Qm3oaD+aKTgkGrjzJ7SrpIO0lXoQ4GMAOQGqF2f5c1 GgZ0u+KgR8UD4QEjqqNp1f3uWw1IMZn1swo2X/+3UEzMpZ96MAf+t90OoHE/9H21gwb2 t9CQ== X-Gm-Message-State: AOJu0YzHlC6vkLHNL2vjytBINX/s1U757j/BikUKAsFF43pJ0++tJ7f2 9JzpiK4XVp2vfUsVWGEdM3M9upR8BJcJlNrUAe9WT7U44/6GCg== X-Google-Smtp-Source: AGHT+IHj/uoN+AIt8v1z9vnEwv81FaGGtD69jiBWh3d8X2nqgCc6Xhvs7lZvrFQlD0ECk1rMG7S4RA== X-Received: by 2002:a05:6808:2f1a:b0:3bb:afe2:e758 with SMTP id gu26-20020a0568082f1a00b003bbafe2e758mr422308oib.105.1703482063017; Sun, 24 Dec 2023 21:27:43 -0800 (PST) From: Hyman Huang To: qemu-devel Cc: Kevin Wolf , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Hanna Reitz , Eric Blake , Markus Armbruster , Hyman Huang Subject: [v3 06/10] block: Support detached LUKS header creation using blockdev-create Date: Mon, 25 Dec 2023 13:26:56 +0800 Message-Id: <20ab47b728492cedb7ea671239f0397a141c3f5a.1703481380.git.yong.huang@smartx.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.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; Received-SPF: none client-ip=2607:f8b0:4864:20::231; envelope-from=yong.huang@smartx.com; helo=mail-oi1-x231.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @smartx-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1703482260837100014 Content-Type: text/plain; charset="utf-8" The LUKS disk with detached header consists of a separate LUKS header and payload. This LUKS disk type should be formatted as follows: 1. add the secret to lock/unlock the cipher stored in the detached LUKS header $ virsh qemu-monitor-command vm '{"execute":"object-add", > "arguments":{"qom-type": "secret", "id": "sec0", "data": "foo"}}' 2. create a header img with 0 size $ virsh qemu-monitor-command vm '{"execute":"blockdev-create", > "arguments":{"job-id":"job0", "options":{"driver":"file", > "filename":"/path/to/detached_luks_header.img", "size":0 }}}' 3. add protocol blockdev node for header $ virsh qemu-monitor-command vm '{"execute":"blockdev-add", > "arguments": {"driver":"file", "filename": > "/path/to/detached_luks_header.img", "node-name": > "detached-luks-header-storage"}}' 4. create a payload img with 0 size $ virsh qemu-monitor-command vm '{"execute":"blockdev-create", > "arguments":{"job-id":"job1", "options":{"driver":"file", > "filename":"/path/to/detached_luks_payload_raw.img", "size":0}}}' 5. add protocol blockdev node for payload $ virsh qemu-monitor-command vm '{"execute":"blockdev-add", > "arguments": {"driver":"file", "filename": > "/path/to/detached_luks_payload_raw.img", "node-name": > "luks-payload-raw-storage"}}' 6. do the formatting with 128M size $ virsh qemu-monitor-command c81_node1 '{"execute":"blockdev-create", > "arguments":{"job-id":"job2", "options":{"driver":"luks", "header": > "detached-luks-header-storage", "file":"luks-payload-raw-storage", > "size":134217728, "preallocation":"full", "key-secret":"sec0" }}}' Signed-off-by: Hyman Huang --- block/crypto.c | 109 ++++++++++++++++++++++++++++++++++++++++---- crypto/block-luks.c | 6 ++- crypto/block.c | 1 + 3 files changed, 106 insertions(+), 10 deletions(-) diff --git a/block/crypto.c b/block/crypto.c index 78fbe79c95..76cc8bda49 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -160,6 +160,48 @@ error: return ret; } =20 +static int coroutine_fn GRAPH_UNLOCKED +block_crypto_co_format_luks_payload(BlockdevCreateOptionsLUKS *luks_opts, + Error **errp) +{ + BlockDriverState *bs =3D NULL; + BlockBackend *blk =3D NULL; + Error *local_error =3D NULL; + int ret; + + if (luks_opts->size > INT64_MAX) { + return -EFBIG; + } + + bs =3D bdrv_co_open_blockdev_ref(luks_opts->file, errp); + if (bs =3D=3D NULL) { + return -EIO; + } + + blk =3D blk_co_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, + BLK_PERM_ALL, errp); + if (!blk) { + ret =3D -EPERM; + goto fail; + } + + ret =3D blk_truncate(blk, luks_opts->size, true, + luks_opts->preallocation, 0, &local_error); + if (ret < 0) { + if (ret =3D=3D -EFBIG) { + /* Replace the error message with a better one */ + error_free(local_error); + error_setg(errp, "The requested file size is too large"); + } + goto fail; + } + + ret =3D 0; + +fail: + bdrv_co_unref(bs); + return ret; +} =20 static QemuOptsList block_crypto_runtime_opts_luks =3D { .name =3D "crypto", @@ -651,6 +693,7 @@ static int coroutine_fn GRAPH_UNLOCKED block_crypto_co_create_luks(BlockdevCreateOptions *create_options, Error *= *errp) { BlockdevCreateOptionsLUKS *luks_opts; + BlockDriverState *hdr_bs =3D NULL; BlockDriverState *bs =3D NULL; QCryptoBlockCreateOptions create_opts; PreallocMode preallocation =3D PREALLOC_MODE_OFF; @@ -659,8 +702,22 @@ block_crypto_co_create_luks(BlockdevCreateOptions *cre= ate_options, Error **errp) assert(create_options->driver =3D=3D BLOCKDEV_DRIVER_LUKS); luks_opts =3D &create_options->u.luks; =20 - if (luks_opts->file =3D=3D NULL) { - error_setg(errp, "Formatting LUKS disk requires parameter 'file'"); + if (luks_opts->header =3D=3D NULL && luks_opts->file =3D=3D NULL) { + error_setg(errp, "Either the parameter 'header' or 'file' should " + "be specified"); + return -EINVAL; + } + + if (luks_opts->detached_header && luks_opts->header =3D=3D NULL) { + error_setg(errp, "Formatting a detached LUKS disk requries " + "'header' to be specified"); + return -EINVAL; + } + + if ((luks_opts->preallocation !=3D PREALLOC_MODE_OFF) && + (luks_opts->file =3D=3D NULL)) { + error_setg(errp, "Parameter 'preallocation' requries 'file' to be " + "specified for formatting LUKS disk"); return -EINVAL; } =20 @@ -673,7 +730,40 @@ block_crypto_co_create_luks(BlockdevCreateOptions *cre= ate_options, Error **errp) preallocation =3D luks_opts->preallocation; } =20 - if (luks_opts->file) { + if (luks_opts->header) { + hdr_bs =3D bdrv_co_open_blockdev_ref(luks_opts->header, errp); + if (hdr_bs =3D=3D NULL) { + return -EIO; + } + + /* + * If blockdev reference of header is specified, + * detached_header default to true + */ + create_opts.u.luks.detached_header =3D true; + + /* Format the LUKS header node */ + ret =3D block_crypto_co_create_generic(hdr_bs, 0, &create_opts, + PREALLOC_MODE_OFF, errp); + if (ret < 0) { + goto hdr_bs_failed; + } + + /* Format the LUKS payload node */ + if (luks_opts->file) { + ret =3D block_crypto_co_format_luks_payload(luks_opts, errp); + if (ret < 0) { + goto hdr_bs_failed; + } + } + + ret =3D 0; + +hdr_bs_failed: + bdrv_co_unref(hdr_bs); + return ret; + } else if (luks_opts->file) { + /* None detached LUKS header path */ bs =3D bdrv_co_open_blockdev_ref(luks_opts->file, errp); if (bs =3D=3D NULL) { return -EIO; @@ -682,14 +772,15 @@ block_crypto_co_create_luks(BlockdevCreateOptions *cr= eate_options, Error **errp) ret =3D block_crypto_co_create_generic(bs, luks_opts->size, &creat= e_opts, preallocation, errp); if (ret < 0) { - goto fail; + goto bs_failed; } - } =20 - ret =3D 0; -fail: - bdrv_co_unref(bs); - return ret; + ret =3D 0; + +bs_failed: + bdrv_co_unref(bs); + return ret; + } } =20 static int coroutine_fn GRAPH_UNLOCKED diff --git a/crypto/block-luks.c b/crypto/block-luks.c index 48443ffcae..474c7aee2e 100644 --- a/crypto/block-luks.c +++ b/crypto/block-luks.c @@ -1561,8 +1561,12 @@ qcrypto_block_luks_create(QCryptoBlock *block, block->payload_offset =3D qcrypto_block_luks_payload_offset(luks->header.payload_offset_sect= or); =20 + block->detached_header_size =3D + (header_sectors + QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS * + split_key_sectors) * block->sector_size; + /* Reserve header space to match payload offset */ - initfunc(block, block->payload_offset, opaque, &local_err); + initfunc(block, block->detached_header_size, opaque, &local_err); if (local_err) { error_propagate(errp, local_err); goto error; diff --git a/crypto/block.c b/crypto/block.c index 7bb4b74a37..ea493f056e 100644 --- a/crypto/block.c +++ b/crypto/block.c @@ -102,6 +102,7 @@ QCryptoBlock *qcrypto_block_create(QCryptoBlockCreateOp= tions *options, } =20 block->driver =3D qcrypto_block_drivers[options->format]; + block->detached_header =3D options->u.luks.detached_header; =20 if (block->driver->create(block, options, optprefix, initfunc, writefunc, opaque, errp) < 0) { --=20 2.39.1