2 * QEMU block full disk encryption
4 * Copyright (c) 2015-2016 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
23 #include "block/block_int.h"
24 #include "block/qdict.h"
25 #include "sysemu/block-backend.h"
26 #include "crypto/block.h"
27 #include "qapi/opts-visitor.h"
28 #include "qapi/qapi-visit-crypto.h"
29 #include "qapi/qobject-input-visitor.h"
30 #include "qapi/error.h"
31 #include "qemu/module.h"
32 #include "qemu/option.h"
33 #include "qemu/cutils.h"
36 typedef struct BlockCrypto BlockCrypto
;
43 static int block_crypto_probe_generic(QCryptoBlockFormat format
,
48 if (qcrypto_block_has_format(format
, buf
, buf_size
)) {
56 static ssize_t
block_crypto_read_func(QCryptoBlock
*block
,
63 BlockDriverState
*bs
= opaque
;
66 ret
= bdrv_pread(bs
->file
, offset
, buf
, buflen
);
68 error_setg_errno(errp
, -ret
, "Could not read encryption header");
75 struct BlockCryptoCreateData
{
78 PreallocMode prealloc
;
82 static ssize_t
block_crypto_write_func(QCryptoBlock
*block
,
89 struct BlockCryptoCreateData
*data
= opaque
;
92 ret
= blk_pwrite(data
->blk
, offset
, buf
, buflen
, 0);
94 error_setg_errno(errp
, -ret
, "Could not write encryption header");
101 static ssize_t
block_crypto_init_func(QCryptoBlock
*block
,
106 struct BlockCryptoCreateData
*data
= opaque
;
107 Error
*local_error
= NULL
;
110 if (data
->size
> INT64_MAX
|| headerlen
> INT64_MAX
- data
->size
) {
115 /* User provided size should reflect amount of space made
116 * available to the guest, so we must take account of that
117 * which will be used by the crypto header
119 ret
= blk_truncate(data
->blk
, data
->size
+ headerlen
, false,
120 data
->prealloc
, 0, &local_error
);
128 /* Replace the error message with a better one */
129 error_free(local_error
);
130 error_setg(errp
, "The requested file size is too large");
132 error_propagate(errp
, local_error
);
139 static QemuOptsList block_crypto_runtime_opts_luks
= {
141 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_runtime_opts_luks
.head
),
143 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
144 { /* end of list */ }
149 static QemuOptsList block_crypto_create_opts_luks
= {
151 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_create_opts_luks
.head
),
154 .name
= BLOCK_OPT_SIZE
,
155 .type
= QEMU_OPT_SIZE
,
156 .help
= "Virtual disk size"
158 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
159 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG(""),
160 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE(""),
161 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG(""),
162 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG(""),
163 BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG(""),
164 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME(""),
165 { /* end of list */ }
170 QCryptoBlockOpenOptions
*
171 block_crypto_open_opts_init(QDict
*opts
, Error
**errp
)
174 QCryptoBlockOpenOptions
*ret
;
176 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
181 visit_type_QCryptoBlockOpenOptions(v
, NULL
, &ret
, errp
);
188 QCryptoBlockCreateOptions
*
189 block_crypto_create_opts_init(QDict
*opts
, Error
**errp
)
192 QCryptoBlockCreateOptions
*ret
;
194 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
199 visit_type_QCryptoBlockCreateOptions(v
, NULL
, &ret
, errp
);
205 QCryptoBlockAmendOptions
*
206 block_crypto_amend_opts_init(QDict
*opts
, Error
**errp
)
209 QCryptoBlockAmendOptions
*ret
;
211 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
216 visit_type_QCryptoBlockAmendOptions(v
, NULL
, &ret
, errp
);
223 static int block_crypto_open_generic(QCryptoBlockFormat format
,
224 QemuOptsList
*opts_spec
,
225 BlockDriverState
*bs
,
230 BlockCrypto
*crypto
= bs
->opaque
;
231 QemuOpts
*opts
= NULL
;
232 Error
*local_err
= NULL
;
234 QCryptoBlockOpenOptions
*open_opts
= NULL
;
235 unsigned int cflags
= 0;
236 QDict
*cryptoopts
= NULL
;
238 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_of_bds
,
239 BDRV_CHILD_IMAGE
, false, errp
);
244 bs
->supported_write_flags
= BDRV_REQ_FUA
&
245 bs
->file
->bs
->supported_write_flags
;
247 opts
= qemu_opts_create(opts_spec
, NULL
, 0, &error_abort
);
248 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
250 error_propagate(errp
, local_err
);
254 cryptoopts
= qemu_opts_to_qdict(opts
, NULL
);
255 qdict_put_str(cryptoopts
, "format", QCryptoBlockFormat_str(format
));
257 open_opts
= block_crypto_open_opts_init(cryptoopts
, errp
);
262 if (flags
& BDRV_O_NO_IO
) {
263 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
265 crypto
->block
= qcrypto_block_open(open_opts
, NULL
,
266 block_crypto_read_func
,
272 if (!crypto
->block
) {
277 bs
->encrypted
= true;
281 qobject_unref(cryptoopts
);
282 qapi_free_QCryptoBlockOpenOptions(open_opts
);
287 static int block_crypto_co_create_generic(BlockDriverState
*bs
,
289 QCryptoBlockCreateOptions
*opts
,
290 PreallocMode prealloc
,
295 QCryptoBlock
*crypto
= NULL
;
296 struct BlockCryptoCreateData data
;
298 blk
= blk_new_with_bs(bs
, BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
,
305 if (prealloc
== PREALLOC_MODE_METADATA
) {
306 prealloc
= PREALLOC_MODE_OFF
;
309 data
= (struct BlockCryptoCreateData
) {
312 .prealloc
= prealloc
,
315 crypto
= qcrypto_block_create(opts
, NULL
,
316 block_crypto_init_func
,
317 block_crypto_write_func
,
328 qcrypto_block_free(crypto
);
333 static int coroutine_fn
334 block_crypto_co_truncate(BlockDriverState
*bs
, int64_t offset
, bool exact
,
335 PreallocMode prealloc
, BdrvRequestFlags flags
,
338 BlockCrypto
*crypto
= bs
->opaque
;
339 uint64_t payload_offset
=
340 qcrypto_block_get_payload_offset(crypto
->block
);
342 if (payload_offset
> INT64_MAX
- offset
) {
343 error_setg(errp
, "The requested file size is too large");
347 offset
+= payload_offset
;
349 return bdrv_co_truncate(bs
->file
, offset
, exact
, prealloc
, 0, errp
);
352 static void block_crypto_close(BlockDriverState
*bs
)
354 BlockCrypto
*crypto
= bs
->opaque
;
355 qcrypto_block_free(crypto
->block
);
358 static int block_crypto_reopen_prepare(BDRVReopenState
*state
,
359 BlockReopenQueue
*queue
, Error
**errp
)
361 /* nothing needs checking */
366 * 1 MB bounce buffer gives good performance / memory tradeoff
367 * when using cache=none|directsync.
369 #define BLOCK_CRYPTO_MAX_IO_SIZE (1024 * 1024)
371 static coroutine_fn
int
372 block_crypto_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
373 QEMUIOVector
*qiov
, int flags
)
375 BlockCrypto
*crypto
= bs
->opaque
;
376 uint64_t cur_bytes
; /* number of bytes in current iteration */
377 uint64_t bytes_done
= 0;
378 uint8_t *cipher_data
= NULL
;
379 QEMUIOVector hd_qiov
;
381 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
382 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
385 assert(payload_offset
< INT64_MAX
);
386 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
387 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
389 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
391 /* Bounce buffer because we don't wish to expose cipher text
392 * in qiov which points to guest memory.
395 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
397 if (cipher_data
== NULL
) {
403 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
405 qemu_iovec_reset(&hd_qiov
);
406 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
408 ret
= bdrv_co_preadv(bs
->file
, payload_offset
+ offset
+ bytes_done
,
409 cur_bytes
, &hd_qiov
, 0);
414 if (qcrypto_block_decrypt(crypto
->block
, offset
+ bytes_done
,
415 cipher_data
, cur_bytes
, NULL
) < 0) {
420 qemu_iovec_from_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
423 bytes_done
+= cur_bytes
;
427 qemu_iovec_destroy(&hd_qiov
);
428 qemu_vfree(cipher_data
);
434 static coroutine_fn
int
435 block_crypto_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
436 QEMUIOVector
*qiov
, int flags
)
438 BlockCrypto
*crypto
= bs
->opaque
;
439 uint64_t cur_bytes
; /* number of bytes in current iteration */
440 uint64_t bytes_done
= 0;
441 uint8_t *cipher_data
= NULL
;
442 QEMUIOVector hd_qiov
;
444 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
445 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
447 assert(!(flags
& ~BDRV_REQ_FUA
));
448 assert(payload_offset
< INT64_MAX
);
449 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
450 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
452 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
454 /* Bounce buffer because we're not permitted to touch
455 * contents of qiov - it points to guest memory.
458 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
460 if (cipher_data
== NULL
) {
466 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
468 qemu_iovec_to_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
470 if (qcrypto_block_encrypt(crypto
->block
, offset
+ bytes_done
,
471 cipher_data
, cur_bytes
, NULL
) < 0) {
476 qemu_iovec_reset(&hd_qiov
);
477 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
479 ret
= bdrv_co_pwritev(bs
->file
, payload_offset
+ offset
+ bytes_done
,
480 cur_bytes
, &hd_qiov
, flags
);
486 bytes_done
+= cur_bytes
;
490 qemu_iovec_destroy(&hd_qiov
);
491 qemu_vfree(cipher_data
);
496 static void block_crypto_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
498 BlockCrypto
*crypto
= bs
->opaque
;
499 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
500 bs
->bl
.request_alignment
= sector_size
; /* No sub-sector I/O */
504 static int64_t block_crypto_getlength(BlockDriverState
*bs
)
506 BlockCrypto
*crypto
= bs
->opaque
;
507 int64_t len
= bdrv_getlength(bs
->file
->bs
);
509 uint64_t offset
= qcrypto_block_get_payload_offset(crypto
->block
);
510 assert(offset
< INT64_MAX
);
522 static BlockMeasureInfo
*block_crypto_measure(QemuOpts
*opts
,
523 BlockDriverState
*in_bs
,
526 g_autoptr(QCryptoBlockCreateOptions
) create_opts
= NULL
;
527 Error
*local_err
= NULL
;
528 BlockMeasureInfo
*info
;
530 size_t luks_payload_size
;
534 * Preallocation mode doesn't affect size requirements but we must consume
537 g_free(qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
));
539 size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
542 int64_t ssize
= bdrv_getlength(in_bs
);
545 error_setg_errno(&local_err
, -ssize
,
546 "Unable to get image virtual_size");
553 cryptoopts
= qemu_opts_to_qdict_filtered(opts
, NULL
,
554 &block_crypto_create_opts_luks
, true);
555 qdict_put_str(cryptoopts
, "format", "luks");
556 create_opts
= block_crypto_create_opts_init(cryptoopts
, &local_err
);
557 qobject_unref(cryptoopts
);
562 if (!qcrypto_block_calculate_payload_offset(create_opts
, NULL
,
569 * Unallocated blocks are still encrypted so allocation status makes no
570 * difference to the file size.
572 info
= g_new0(BlockMeasureInfo
, 1);
573 info
->fully_allocated
= luks_payload_size
+ size
;
574 info
->required
= luks_payload_size
+ size
;
578 error_propagate(errp
, local_err
);
583 static int block_crypto_probe_luks(const uint8_t *buf
,
585 const char *filename
) {
586 return block_crypto_probe_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
587 buf
, buf_size
, filename
);
590 static int block_crypto_open_luks(BlockDriverState
*bs
,
595 return block_crypto_open_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
596 &block_crypto_runtime_opts_luks
,
597 bs
, options
, flags
, errp
);
600 static int coroutine_fn
601 block_crypto_co_create_luks(BlockdevCreateOptions
*create_options
, Error
**errp
)
603 BlockdevCreateOptionsLUKS
*luks_opts
;
604 BlockDriverState
*bs
= NULL
;
605 QCryptoBlockCreateOptions create_opts
;
606 PreallocMode preallocation
= PREALLOC_MODE_OFF
;
609 assert(create_options
->driver
== BLOCKDEV_DRIVER_LUKS
);
610 luks_opts
= &create_options
->u
.luks
;
612 bs
= bdrv_open_blockdev_ref(luks_opts
->file
, errp
);
617 create_opts
= (QCryptoBlockCreateOptions
) {
618 .format
= Q_CRYPTO_BLOCK_FORMAT_LUKS
,
619 .u
.luks
= *qapi_BlockdevCreateOptionsLUKS_base(luks_opts
),
622 if (luks_opts
->has_preallocation
) {
623 preallocation
= luks_opts
->preallocation
;
626 ret
= block_crypto_co_create_generic(bs
, luks_opts
->size
, &create_opts
,
627 preallocation
, errp
);
638 static int coroutine_fn
block_crypto_co_create_opts_luks(BlockDriver
*drv
,
639 const char *filename
,
643 QCryptoBlockCreateOptions
*create_opts
= NULL
;
644 BlockDriverState
*bs
= NULL
;
646 PreallocMode prealloc
;
650 Error
*local_err
= NULL
;
653 size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
655 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
656 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, buf
,
657 PREALLOC_MODE_OFF
, &local_err
);
660 error_propagate(errp
, local_err
);
664 cryptoopts
= qemu_opts_to_qdict_filtered(opts
, NULL
,
665 &block_crypto_create_opts_luks
,
668 qdict_put_str(cryptoopts
, "format", "luks");
669 create_opts
= block_crypto_create_opts_init(cryptoopts
, errp
);
675 /* Create protocol layer */
676 ret
= bdrv_create_file(filename
, opts
, errp
);
681 bs
= bdrv_open(filename
, NULL
, NULL
,
682 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
688 /* Create format layer */
689 ret
= block_crypto_co_create_generic(bs
, size
, create_opts
, prealloc
, errp
);
697 * If an error occurred, delete 'filename'. Even if the file existed
698 * beforehand, it has been truncated and corrupted in the process.
701 Error
*local_delete_err
= NULL
;
702 int r_del
= bdrv_co_delete_file(bs
, &local_delete_err
);
704 * ENOTSUP will happen if the block driver doesn't support
705 * the 'bdrv_co_delete_file' interface. This is a predictable
706 * scenario and shouldn't be reported back to the user.
708 if ((r_del
< 0) && (r_del
!= -ENOTSUP
)) {
709 error_report_err(local_delete_err
);
714 qapi_free_QCryptoBlockCreateOptions(create_opts
);
715 qobject_unref(cryptoopts
);
719 static int block_crypto_get_info_luks(BlockDriverState
*bs
,
720 BlockDriverInfo
*bdi
)
722 BlockDriverInfo subbdi
;
725 ret
= bdrv_get_info(bs
->file
->bs
, &subbdi
);
730 bdi
->unallocated_blocks_are_zero
= false;
731 bdi
->cluster_size
= subbdi
.cluster_size
;
736 static ImageInfoSpecific
*
737 block_crypto_get_specific_info_luks(BlockDriverState
*bs
, Error
**errp
)
739 BlockCrypto
*crypto
= bs
->opaque
;
740 ImageInfoSpecific
*spec_info
;
741 QCryptoBlockInfo
*info
;
743 info
= qcrypto_block_get_info(crypto
->block
, errp
);
747 assert(info
->format
== Q_CRYPTO_BLOCK_FORMAT_LUKS
);
749 spec_info
= g_new(ImageInfoSpecific
, 1);
750 spec_info
->type
= IMAGE_INFO_SPECIFIC_KIND_LUKS
;
751 spec_info
->u
.luks
.data
= g_new(QCryptoBlockInfoLUKS
, 1);
752 *spec_info
->u
.luks
.data
= info
->u
.luks
;
754 /* Blank out pointers we've just stolen to avoid double free */
755 memset(&info
->u
.luks
, 0, sizeof(info
->u
.luks
));
757 qapi_free_QCryptoBlockInfo(info
);
762 static const char *const block_crypto_strong_runtime_opts
[] = {
763 BLOCK_CRYPTO_OPT_LUKS_KEY_SECRET
,
768 static BlockDriver bdrv_crypto_luks
= {
769 .format_name
= "luks",
770 .instance_size
= sizeof(BlockCrypto
),
771 .bdrv_probe
= block_crypto_probe_luks
,
772 .bdrv_open
= block_crypto_open_luks
,
773 .bdrv_close
= block_crypto_close
,
774 /* This driver doesn't modify LUKS metadata except when creating image.
775 * Allow share-rw=on as a special case. */
776 .bdrv_child_perm
= bdrv_default_perms
,
777 .bdrv_co_create
= block_crypto_co_create_luks
,
778 .bdrv_co_create_opts
= block_crypto_co_create_opts_luks
,
779 .bdrv_co_truncate
= block_crypto_co_truncate
,
780 .create_opts
= &block_crypto_create_opts_luks
,
782 .bdrv_reopen_prepare
= block_crypto_reopen_prepare
,
783 .bdrv_refresh_limits
= block_crypto_refresh_limits
,
784 .bdrv_co_preadv
= block_crypto_co_preadv
,
785 .bdrv_co_pwritev
= block_crypto_co_pwritev
,
786 .bdrv_getlength
= block_crypto_getlength
,
787 .bdrv_measure
= block_crypto_measure
,
788 .bdrv_get_info
= block_crypto_get_info_luks
,
789 .bdrv_get_specific_info
= block_crypto_get_specific_info_luks
,
793 .strong_runtime_opts
= block_crypto_strong_runtime_opts
,
796 static void block_crypto_init(void)
798 bdrv_register(&bdrv_crypto_luks
);
801 block_init(block_crypto_init
);