From nobody Tue Nov 26 18:25:50 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=1706593248; cv=none; d=zohomail.com; s=zohoarc; b=K33QO2rX7kj3Xiu5n4+7N+q5/b3MZqut8ZNr0zTbJypJka1tKoVzSU2fr0NgwbC1scmdnyrPO668FP4v/jWaTUXKlJeNsQsR1R0J2i/mHg6alYZa1pLrMXqiml7dg9jVzMbiimvq8FklB8oeh4bMtj/0NRbrKaUqUcV83G8UsF4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1706593248; 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=2Zmg4sbSwcsBk7QBG9lZhae1I6QzpidLZEsTyVPwHJQ=; b=fmWRexqFwBiCuLadSFSBqC6CfZEjVyUkD24jRoRk316V29OIH0WtWdgM1R83a2wag6gjBiTXPQA4aoDa1f1VYdt6O86gDCjEIJpGTbNTUVaexj7WVBG2LVSRN+hmi5O1TKDrMMZiq46IhUEdTAXlx+w2OLFMWNqooN9Ye+PEguU= 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 1706593248148667.0560297412004; Mon, 29 Jan 2024 21:40:48 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rUgqa-0002st-3t; Tue, 30 Jan 2024 00:39: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 1rUgqW-0002rw-In for qemu-devel@nongnu.org; Tue, 30 Jan 2024 00:39:44 -0500 Received: from mail-oo1-xc36.google.com ([2607:f8b0:4864:20::c36]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rUgqU-0001NJ-OI for qemu-devel@nongnu.org; Tue, 30 Jan 2024 00:39:44 -0500 Received: by mail-oo1-xc36.google.com with SMTP id 006d021491bc7-59502aa878aso1803053eaf.1 for ; Mon, 29 Jan 2024 21:37:42 -0800 (PST) Received: from anolis-dev.zelin.local ([221.122.98.162]) by smtp.gmail.com with ESMTPSA id bv123-20020a632e81000000b005c1ce3c960bsm7343532pgb.50.2024.01.29.21.37.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 21:37:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20230601.gappssmtp.com; s=20230601; t=1706593061; x=1707197861; 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=2Zmg4sbSwcsBk7QBG9lZhae1I6QzpidLZEsTyVPwHJQ=; b=WV1D0NjD2ctIOMKVMJ314ln59FkZ0mEObSWnzHGGFniU6UcasUDBV2x76Yjma4um5/ /gmUa5T8efYDqKoaB8BTanWi0xlv/5Gli1ReLaqvFGhQwpYXuXHS0b/44uTxy2ZzqQfp jnYZ7+NSyAqhFDdCmYTaIj2xaxq11BvKnpV0/75hkJHXe7E6ReNwT9jTuXEas6LbACLg 1Ycqg8njBpyYT5qe1Y686hPVTHT5JcVgv2K/ZKxs88N1mtHSOInwNUAPag7dT3Zj1vax rwB3lgT4kRNPk8XtILX9xKG9t97Dtaacv+cEmCjHEkBUbOyVTGpsDgpO2gNfI0A8S3OW YjJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706593061; x=1707197861; 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=2Zmg4sbSwcsBk7QBG9lZhae1I6QzpidLZEsTyVPwHJQ=; b=lxUqiFPVIwvfNMtAed/PmULCnY1hB7RMU75uRpHnqdAvrGLpdwBwxQdrGe3ssGrWE/ e7vyK9J9p+bIAXFiWX3/Wh3BmpNjV6ulQtf7GTRXMkI5jwh+ugZxDScgCNcu626hm/nH KXzK1fBijqQW4KO4q6crdaHqo69ZaQ1ABQ2wr7ff0HjKKocRHS/2M7Jkg9T/1L0ZrjQ+ Kh2mVvFn4jI+YA+sWFASk013QByxpFV1dUdxDvzuu5qVYv6qvkh6H+Rv5L3lkVaMv3RM GvR/W72rrN5G+HDVYf6yEUNUxDRiXpUYAaO8J7/f0WR5kzhHNQISVmctKTDOBfjtkdnN 8xRw== X-Gm-Message-State: AOJu0YxzcMIkKdoCwFVHTCFvyUUpmjE8yV8ss3A2IOTKLiU9NgQHr0ZB G7RCiusk5k26YvNWTv8QZ+FBveeHv+RXBHIyf7i1VI0G6LgeeorU5V05dtXRWFCiOBHIem4QAwy qzI0= X-Google-Smtp-Source: AGHT+IH/yItth8XF+5mVj37Yyx7z6K1F1FW6aeEqwAYcvco07wuR+knKc6CpofOhHp2en7k6V7u0JQ== X-Received: by 2002:a05:6358:72a4:b0:178:7194:b543 with SMTP id w36-20020a05635872a400b001787194b543mr3729757rwf.41.1706593060480; Mon, 29 Jan 2024 21:37:40 -0800 (PST) From: yong.huang@smartx.com To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Eric Blake , Markus Armbruster , Hanna Reitz , Kevin Wolf , yong.huang@smartx.com Subject: [PATCH v4 4/7] block: Support detached LUKS header creation using blockdev-create Date: Tue, 30 Jan 2024 13:37:22 +0800 Message-Id: <9e35e621f12478822072e5c740ed65beb77f1238.1706586786.git.yong.huang@smartx.com> X-Mailer: git-send-email 2.31.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::c36; envelope-from=yong.huang@smartx.com; helo=mail-oo1-xc36.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: 1706593250343100011 Content-Type: text/plain; charset="utf-8" From: Hyman Huang Firstly, enable the ability to choose the block device containing a detachable LUKS header by adding the 'header' parameter to BlockdevCreateOptionsLUKS. Secondly, when formatting the LUKS volume with a detachable header, truncate the payload volume to length without a header size. Using the qmp blockdev command, create the LUKS volume with a detachable header 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 Reviewed-by: Daniel P. Berrang=C3=A9 --- block/crypto.c | 101 +++++++++++++++++++++++++++++++++++++++---- qapi/block-core.json | 3 ++ 2 files changed, 96 insertions(+), 8 deletions(-) diff --git a/block/crypto.c b/block/crypto.c index 1b3f87922a..8e7ee5e9ac 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -162,6 +162,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", @@ -341,7 +383,9 @@ static int block_crypto_open_generic(QCryptoBlockFormat= format, static int coroutine_fn GRAPH_UNLOCKED block_crypto_co_create_generic(BlockDriverState *bs, int64_t size, QCryptoBlockCreateOptions *opts, - PreallocMode prealloc, Error **errp) + PreallocMode prealloc, + unsigned int flags, + Error **errp) { int ret; BlockBackend *blk; @@ -369,7 +413,7 @@ block_crypto_co_create_generic(BlockDriverState *bs, in= t64_t size, block_crypto_create_init_func, block_crypto_create_write_func, &data, - 0, + flags, errp); =20 if (!crypto) { @@ -656,16 +700,26 @@ 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; + unsigned int cflags =3D 0; int ret; =20 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' must " + "be specified"); + return -EINVAL; + } + + if ((luks_opts->preallocation !=3D PREALLOC_MODE_OFF) && + (luks_opts->file =3D=3D NULL)) { + error_setg(errp, "Parameter 'preallocation' requires 'file' to be " + "specified for formatting LUKS disk"); return -EINVAL; } =20 @@ -678,14 +732,38 @@ block_crypto_co_create_luks(BlockdevCreateOptions *cr= eate_options, Error **errp) preallocation =3D luks_opts->preallocation; } =20 - if (luks_opts->file) { + if (luks_opts->header) { + /* LUKS volume with detached header */ + hdr_bs =3D bdrv_co_open_blockdev_ref(luks_opts->header, errp); + if (hdr_bs =3D=3D NULL) { + return -EIO; + } + + cflags |=3D QCRYPTO_BLOCK_CREATE_DETACHED; + + /* Format the LUKS header node */ + ret =3D block_crypto_co_create_generic(hdr_bs, 0, &create_opts, + PREALLOC_MODE_OFF, cflags, er= rp); + if (ret < 0) { + goto fail; + } + + /* Format the LUKS payload node */ + if (luks_opts->file) { + ret =3D block_crypto_co_format_luks_payload(luks_opts, errp); + if (ret < 0) { + goto fail; + } + } + } else if (luks_opts->file) { + /* LUKS volume with none-detached header */ bs =3D bdrv_co_open_blockdev_ref(luks_opts->file, errp); if (bs =3D=3D NULL) { return -EIO; } =20 ret =3D block_crypto_co_create_generic(bs, luks_opts->size, &creat= e_opts, - preallocation, errp); + preallocation, cflags, errp); if (ret < 0) { goto fail; } @@ -693,7 +771,13 @@ block_crypto_co_create_luks(BlockdevCreateOptions *cre= ate_options, Error **errp) =20 ret =3D 0; fail: - bdrv_co_unref(bs); + if (hdr_bs !=3D NULL) { + bdrv_co_unref(hdr_bs); + } + + if (bs !=3D NULL) { + bdrv_co_unref(bs); + } return ret; } =20 @@ -747,7 +831,8 @@ block_crypto_co_create_opts_luks(BlockDriver *drv, cons= t char *filename, } =20 /* Create format layer */ - ret =3D block_crypto_co_create_generic(bs, size, create_opts, prealloc= , errp); + ret =3D block_crypto_co_create_generic(bs, size, create_opts, + prealloc, 0, errp); if (ret < 0) { goto fail; } diff --git a/qapi/block-core.json b/qapi/block-core.json index 69a88d613d..eab15d7dd9 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -4960,6 +4960,8 @@ # @file: Node to create the image format on, mandatory except when # 'preallocation' is not requested # +# @header: Block device holding a detached LUKS header. (since 9.0) +# # @size: Size of the virtual disk in bytes # # @preallocation: Preallocation mode for the new image (since: 4.2) @@ -4970,6 +4972,7 @@ { 'struct': 'BlockdevCreateOptionsLUKS', 'base': 'QCryptoBlockCreateOptionsLUKS', 'data': { '*file': 'BlockdevRef', + '*header': 'BlockdevRef', 'size': 'size', '*preallocation': 'PreallocMode' } } =20 --=20 2.31.1