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_create_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");
100 static ssize_t
block_crypto_create_init_func(QCryptoBlock
*block
,
105 struct BlockCryptoCreateData
*data
= opaque
;
106 Error
*local_error
= NULL
;
109 if (data
->size
> INT64_MAX
|| headerlen
> INT64_MAX
- data
->size
) {
114 /* User provided size should reflect amount of space made
115 * available to the guest, so we must take account of that
116 * which will be used by the crypto header
118 ret
= blk_truncate(data
->blk
, data
->size
+ headerlen
, false,
119 data
->prealloc
, 0, &local_error
);
127 /* Replace the error message with a better one */
128 error_free(local_error
);
129 error_setg(errp
, "The requested file size is too large");
131 error_propagate(errp
, local_error
);
138 static QemuOptsList block_crypto_runtime_opts_luks
= {
140 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_runtime_opts_luks
.head
),
142 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
143 { /* end of list */ }
148 static QemuOptsList block_crypto_create_opts_luks
= {
150 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_create_opts_luks
.head
),
153 .name
= BLOCK_OPT_SIZE
,
154 .type
= QEMU_OPT_SIZE
,
155 .help
= "Virtual disk size"
157 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
158 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG(""),
159 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE(""),
160 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG(""),
161 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG(""),
162 BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG(""),
163 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME(""),
164 { /* end of list */ }
169 QCryptoBlockOpenOptions
*
170 block_crypto_open_opts_init(QDict
*opts
, Error
**errp
)
173 QCryptoBlockOpenOptions
*ret
;
175 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
180 visit_type_QCryptoBlockOpenOptions(v
, NULL
, &ret
, errp
);
187 QCryptoBlockCreateOptions
*
188 block_crypto_create_opts_init(QDict
*opts
, Error
**errp
)
191 QCryptoBlockCreateOptions
*ret
;
193 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
198 visit_type_QCryptoBlockCreateOptions(v
, NULL
, &ret
, errp
);
204 QCryptoBlockAmendOptions
*
205 block_crypto_amend_opts_init(QDict
*opts
, Error
**errp
)
208 QCryptoBlockAmendOptions
*ret
;
210 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
215 visit_type_QCryptoBlockAmendOptions(v
, NULL
, &ret
, errp
);
222 static int block_crypto_open_generic(QCryptoBlockFormat format
,
223 QemuOptsList
*opts_spec
,
224 BlockDriverState
*bs
,
229 BlockCrypto
*crypto
= bs
->opaque
;
230 QemuOpts
*opts
= NULL
;
231 Error
*local_err
= NULL
;
233 QCryptoBlockOpenOptions
*open_opts
= NULL
;
234 unsigned int cflags
= 0;
235 QDict
*cryptoopts
= NULL
;
237 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_of_bds
,
238 BDRV_CHILD_IMAGE
, false, errp
);
243 bs
->supported_write_flags
= BDRV_REQ_FUA
&
244 bs
->file
->bs
->supported_write_flags
;
246 opts
= qemu_opts_create(opts_spec
, NULL
, 0, &error_abort
);
247 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
249 error_propagate(errp
, local_err
);
253 cryptoopts
= qemu_opts_to_qdict(opts
, NULL
);
254 qdict_put_str(cryptoopts
, "format", QCryptoBlockFormat_str(format
));
256 open_opts
= block_crypto_open_opts_init(cryptoopts
, errp
);
261 if (flags
& BDRV_O_NO_IO
) {
262 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
264 crypto
->block
= qcrypto_block_open(open_opts
, NULL
,
265 block_crypto_read_func
,
271 if (!crypto
->block
) {
276 bs
->encrypted
= true;
280 qobject_unref(cryptoopts
);
281 qapi_free_QCryptoBlockOpenOptions(open_opts
);
286 static int block_crypto_co_create_generic(BlockDriverState
*bs
,
288 QCryptoBlockCreateOptions
*opts
,
289 PreallocMode prealloc
,
294 QCryptoBlock
*crypto
= NULL
;
295 struct BlockCryptoCreateData data
;
297 blk
= blk_new_with_bs(bs
, BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
,
304 if (prealloc
== PREALLOC_MODE_METADATA
) {
305 prealloc
= PREALLOC_MODE_OFF
;
308 data
= (struct BlockCryptoCreateData
) {
311 .prealloc
= prealloc
,
314 crypto
= qcrypto_block_create(opts
, NULL
,
315 block_crypto_create_init_func
,
316 block_crypto_create_write_func
,
327 qcrypto_block_free(crypto
);
332 static int coroutine_fn
333 block_crypto_co_truncate(BlockDriverState
*bs
, int64_t offset
, bool exact
,
334 PreallocMode prealloc
, BdrvRequestFlags flags
,
337 BlockCrypto
*crypto
= bs
->opaque
;
338 uint64_t payload_offset
=
339 qcrypto_block_get_payload_offset(crypto
->block
);
341 if (payload_offset
> INT64_MAX
- offset
) {
342 error_setg(errp
, "The requested file size is too large");
346 offset
+= payload_offset
;
348 return bdrv_co_truncate(bs
->file
, offset
, exact
, prealloc
, 0, errp
);
351 static void block_crypto_close(BlockDriverState
*bs
)
353 BlockCrypto
*crypto
= bs
->opaque
;
354 qcrypto_block_free(crypto
->block
);
357 static int block_crypto_reopen_prepare(BDRVReopenState
*state
,
358 BlockReopenQueue
*queue
, Error
**errp
)
360 /* nothing needs checking */
365 * 1 MB bounce buffer gives good performance / memory tradeoff
366 * when using cache=none|directsync.
368 #define BLOCK_CRYPTO_MAX_IO_SIZE (1024 * 1024)
370 static coroutine_fn
int
371 block_crypto_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
372 QEMUIOVector
*qiov
, int flags
)
374 BlockCrypto
*crypto
= bs
->opaque
;
375 uint64_t cur_bytes
; /* number of bytes in current iteration */
376 uint64_t bytes_done
= 0;
377 uint8_t *cipher_data
= NULL
;
378 QEMUIOVector hd_qiov
;
380 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
381 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
384 assert(payload_offset
< INT64_MAX
);
385 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
386 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
388 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
390 /* Bounce buffer because we don't wish to expose cipher text
391 * in qiov which points to guest memory.
394 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
396 if (cipher_data
== NULL
) {
402 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
404 qemu_iovec_reset(&hd_qiov
);
405 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
407 ret
= bdrv_co_preadv(bs
->file
, payload_offset
+ offset
+ bytes_done
,
408 cur_bytes
, &hd_qiov
, 0);
413 if (qcrypto_block_decrypt(crypto
->block
, offset
+ bytes_done
,
414 cipher_data
, cur_bytes
, NULL
) < 0) {
419 qemu_iovec_from_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
422 bytes_done
+= cur_bytes
;
426 qemu_iovec_destroy(&hd_qiov
);
427 qemu_vfree(cipher_data
);
433 static coroutine_fn
int
434 block_crypto_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
435 QEMUIOVector
*qiov
, int flags
)
437 BlockCrypto
*crypto
= bs
->opaque
;
438 uint64_t cur_bytes
; /* number of bytes in current iteration */
439 uint64_t bytes_done
= 0;
440 uint8_t *cipher_data
= NULL
;
441 QEMUIOVector hd_qiov
;
443 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
444 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
446 assert(!(flags
& ~BDRV_REQ_FUA
));
447 assert(payload_offset
< INT64_MAX
);
448 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
449 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
451 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
453 /* Bounce buffer because we're not permitted to touch
454 * contents of qiov - it points to guest memory.
457 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
459 if (cipher_data
== NULL
) {
465 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
467 qemu_iovec_to_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
469 if (qcrypto_block_encrypt(crypto
->block
, offset
+ bytes_done
,
470 cipher_data
, cur_bytes
, NULL
) < 0) {
475 qemu_iovec_reset(&hd_qiov
);
476 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
478 ret
= bdrv_co_pwritev(bs
->file
, payload_offset
+ offset
+ bytes_done
,
479 cur_bytes
, &hd_qiov
, flags
);
485 bytes_done
+= cur_bytes
;
489 qemu_iovec_destroy(&hd_qiov
);
490 qemu_vfree(cipher_data
);
495 static void block_crypto_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
497 BlockCrypto
*crypto
= bs
->opaque
;
498 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
499 bs
->bl
.request_alignment
= sector_size
; /* No sub-sector I/O */
503 static int64_t block_crypto_getlength(BlockDriverState
*bs
)
505 BlockCrypto
*crypto
= bs
->opaque
;
506 int64_t len
= bdrv_getlength(bs
->file
->bs
);
508 uint64_t offset
= qcrypto_block_get_payload_offset(crypto
->block
);
509 assert(offset
< INT64_MAX
);
521 static BlockMeasureInfo
*block_crypto_measure(QemuOpts
*opts
,
522 BlockDriverState
*in_bs
,
525 g_autoptr(QCryptoBlockCreateOptions
) create_opts
= NULL
;
526 Error
*local_err
= NULL
;
527 BlockMeasureInfo
*info
;
529 size_t luks_payload_size
;
533 * Preallocation mode doesn't affect size requirements but we must consume
536 g_free(qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
));
538 size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
541 int64_t ssize
= bdrv_getlength(in_bs
);
544 error_setg_errno(&local_err
, -ssize
,
545 "Unable to get image virtual_size");
552 cryptoopts
= qemu_opts_to_qdict_filtered(opts
, NULL
,
553 &block_crypto_create_opts_luks
, true);
554 qdict_put_str(cryptoopts
, "format", "luks");
555 create_opts
= block_crypto_create_opts_init(cryptoopts
, &local_err
);
556 qobject_unref(cryptoopts
);
561 if (!qcrypto_block_calculate_payload_offset(create_opts
, NULL
,
568 * Unallocated blocks are still encrypted so allocation status makes no
569 * difference to the file size.
571 info
= g_new0(BlockMeasureInfo
, 1);
572 info
->fully_allocated
= luks_payload_size
+ size
;
573 info
->required
= luks_payload_size
+ size
;
577 error_propagate(errp
, local_err
);
582 static int block_crypto_probe_luks(const uint8_t *buf
,
584 const char *filename
) {
585 return block_crypto_probe_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
586 buf
, buf_size
, filename
);
589 static int block_crypto_open_luks(BlockDriverState
*bs
,
594 return block_crypto_open_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
595 &block_crypto_runtime_opts_luks
,
596 bs
, options
, flags
, errp
);
599 static int coroutine_fn
600 block_crypto_co_create_luks(BlockdevCreateOptions
*create_options
, Error
**errp
)
602 BlockdevCreateOptionsLUKS
*luks_opts
;
603 BlockDriverState
*bs
= NULL
;
604 QCryptoBlockCreateOptions create_opts
;
605 PreallocMode preallocation
= PREALLOC_MODE_OFF
;
608 assert(create_options
->driver
== BLOCKDEV_DRIVER_LUKS
);
609 luks_opts
= &create_options
->u
.luks
;
611 bs
= bdrv_open_blockdev_ref(luks_opts
->file
, errp
);
616 create_opts
= (QCryptoBlockCreateOptions
) {
617 .format
= Q_CRYPTO_BLOCK_FORMAT_LUKS
,
618 .u
.luks
= *qapi_BlockdevCreateOptionsLUKS_base(luks_opts
),
621 if (luks_opts
->has_preallocation
) {
622 preallocation
= luks_opts
->preallocation
;
625 ret
= block_crypto_co_create_generic(bs
, luks_opts
->size
, &create_opts
,
626 preallocation
, errp
);
637 static int coroutine_fn
block_crypto_co_create_opts_luks(BlockDriver
*drv
,
638 const char *filename
,
642 QCryptoBlockCreateOptions
*create_opts
= NULL
;
643 BlockDriverState
*bs
= NULL
;
645 PreallocMode prealloc
;
649 Error
*local_err
= NULL
;
652 size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
654 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
655 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, buf
,
656 PREALLOC_MODE_OFF
, &local_err
);
659 error_propagate(errp
, local_err
);
663 cryptoopts
= qemu_opts_to_qdict_filtered(opts
, NULL
,
664 &block_crypto_create_opts_luks
,
667 qdict_put_str(cryptoopts
, "format", "luks");
668 create_opts
= block_crypto_create_opts_init(cryptoopts
, errp
);
674 /* Create protocol layer */
675 ret
= bdrv_create_file(filename
, opts
, errp
);
680 bs
= bdrv_open(filename
, NULL
, NULL
,
681 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
687 /* Create format layer */
688 ret
= block_crypto_co_create_generic(bs
, size
, create_opts
, prealloc
, errp
);
696 * If an error occurred, delete 'filename'. Even if the file existed
697 * beforehand, it has been truncated and corrupted in the process.
700 Error
*local_delete_err
= NULL
;
701 int r_del
= bdrv_co_delete_file(bs
, &local_delete_err
);
703 * ENOTSUP will happen if the block driver doesn't support
704 * the 'bdrv_co_delete_file' interface. This is a predictable
705 * scenario and shouldn't be reported back to the user.
707 if ((r_del
< 0) && (r_del
!= -ENOTSUP
)) {
708 error_report_err(local_delete_err
);
713 qapi_free_QCryptoBlockCreateOptions(create_opts
);
714 qobject_unref(cryptoopts
);
718 static int block_crypto_get_info_luks(BlockDriverState
*bs
,
719 BlockDriverInfo
*bdi
)
721 BlockDriverInfo subbdi
;
724 ret
= bdrv_get_info(bs
->file
->bs
, &subbdi
);
729 bdi
->unallocated_blocks_are_zero
= false;
730 bdi
->cluster_size
= subbdi
.cluster_size
;
735 static ImageInfoSpecific
*
736 block_crypto_get_specific_info_luks(BlockDriverState
*bs
, Error
**errp
)
738 BlockCrypto
*crypto
= bs
->opaque
;
739 ImageInfoSpecific
*spec_info
;
740 QCryptoBlockInfo
*info
;
742 info
= qcrypto_block_get_info(crypto
->block
, errp
);
746 assert(info
->format
== Q_CRYPTO_BLOCK_FORMAT_LUKS
);
748 spec_info
= g_new(ImageInfoSpecific
, 1);
749 spec_info
->type
= IMAGE_INFO_SPECIFIC_KIND_LUKS
;
750 spec_info
->u
.luks
.data
= g_new(QCryptoBlockInfoLUKS
, 1);
751 *spec_info
->u
.luks
.data
= info
->u
.luks
;
753 /* Blank out pointers we've just stolen to avoid double free */
754 memset(&info
->u
.luks
, 0, sizeof(info
->u
.luks
));
756 qapi_free_QCryptoBlockInfo(info
);
761 static const char *const block_crypto_strong_runtime_opts
[] = {
762 BLOCK_CRYPTO_OPT_LUKS_KEY_SECRET
,
767 static BlockDriver bdrv_crypto_luks
= {
768 .format_name
= "luks",
769 .instance_size
= sizeof(BlockCrypto
),
770 .bdrv_probe
= block_crypto_probe_luks
,
771 .bdrv_open
= block_crypto_open_luks
,
772 .bdrv_close
= block_crypto_close
,
773 /* This driver doesn't modify LUKS metadata except when creating image.
774 * Allow share-rw=on as a special case. */
775 .bdrv_child_perm
= bdrv_default_perms
,
776 .bdrv_co_create
= block_crypto_co_create_luks
,
777 .bdrv_co_create_opts
= block_crypto_co_create_opts_luks
,
778 .bdrv_co_truncate
= block_crypto_co_truncate
,
779 .create_opts
= &block_crypto_create_opts_luks
,
781 .bdrv_reopen_prepare
= block_crypto_reopen_prepare
,
782 .bdrv_refresh_limits
= block_crypto_refresh_limits
,
783 .bdrv_co_preadv
= block_crypto_co_preadv
,
784 .bdrv_co_pwritev
= block_crypto_co_pwritev
,
785 .bdrv_getlength
= block_crypto_getlength
,
786 .bdrv_measure
= block_crypto_measure
,
787 .bdrv_get_info
= block_crypto_get_info_luks
,
788 .bdrv_get_specific_info
= block_crypto_get_specific_info_luks
,
792 .strong_runtime_opts
= block_crypto_strong_runtime_opts
,
795 static void block_crypto_init(void)
797 bdrv_register(&bdrv_crypto_luks
);
800 block_init(block_crypto_init
);