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
;
108 if (data
->size
> INT64_MAX
|| headerlen
> INT64_MAX
- data
->size
) {
109 error_setg(errp
, "The requested file size is too large");
113 /* User provided size should reflect amount of space made
114 * available to the guest, so we must take account of that
115 * which will be used by the crypto header
117 return blk_truncate(data
->blk
, data
->size
+ headerlen
, false,
118 data
->prealloc
, 0, errp
);
122 static QemuOptsList block_crypto_runtime_opts_luks
= {
124 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_runtime_opts_luks
.head
),
126 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
127 { /* end of list */ }
132 static QemuOptsList block_crypto_create_opts_luks
= {
134 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_create_opts_luks
.head
),
137 .name
= BLOCK_OPT_SIZE
,
138 .type
= QEMU_OPT_SIZE
,
139 .help
= "Virtual disk size"
141 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
142 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG(""),
143 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE(""),
144 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG(""),
145 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG(""),
146 BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG(""),
147 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME(""),
148 { /* end of list */ }
153 QCryptoBlockOpenOptions
*
154 block_crypto_open_opts_init(QDict
*opts
, Error
**errp
)
157 QCryptoBlockOpenOptions
*ret
;
159 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
164 visit_type_QCryptoBlockOpenOptions(v
, NULL
, &ret
, errp
);
171 QCryptoBlockCreateOptions
*
172 block_crypto_create_opts_init(QDict
*opts
, Error
**errp
)
175 QCryptoBlockCreateOptions
*ret
;
177 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
182 visit_type_QCryptoBlockCreateOptions(v
, NULL
, &ret
, errp
);
189 static int block_crypto_open_generic(QCryptoBlockFormat format
,
190 QemuOptsList
*opts_spec
,
191 BlockDriverState
*bs
,
196 BlockCrypto
*crypto
= bs
->opaque
;
197 QemuOpts
*opts
= NULL
;
198 Error
*local_err
= NULL
;
200 QCryptoBlockOpenOptions
*open_opts
= NULL
;
201 unsigned int cflags
= 0;
202 QDict
*cryptoopts
= NULL
;
204 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
210 bs
->supported_write_flags
= BDRV_REQ_FUA
&
211 bs
->file
->bs
->supported_write_flags
;
213 opts
= qemu_opts_create(opts_spec
, NULL
, 0, &error_abort
);
214 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
216 error_propagate(errp
, local_err
);
220 cryptoopts
= qemu_opts_to_qdict(opts
, NULL
);
221 qdict_put_str(cryptoopts
, "format", QCryptoBlockFormat_str(format
));
223 open_opts
= block_crypto_open_opts_init(cryptoopts
, errp
);
228 if (flags
& BDRV_O_NO_IO
) {
229 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
231 crypto
->block
= qcrypto_block_open(open_opts
, NULL
,
232 block_crypto_read_func
,
238 if (!crypto
->block
) {
243 bs
->encrypted
= true;
247 qobject_unref(cryptoopts
);
248 qapi_free_QCryptoBlockOpenOptions(open_opts
);
253 static int block_crypto_co_create_generic(BlockDriverState
*bs
,
255 QCryptoBlockCreateOptions
*opts
,
256 PreallocMode prealloc
,
261 QCryptoBlock
*crypto
= NULL
;
262 struct BlockCryptoCreateData data
;
264 blk
= blk_new(bdrv_get_aio_context(bs
),
265 BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
);
267 ret
= blk_insert_bs(blk
, bs
, errp
);
272 if (prealloc
== PREALLOC_MODE_METADATA
) {
273 prealloc
= PREALLOC_MODE_OFF
;
276 data
= (struct BlockCryptoCreateData
) {
279 .prealloc
= prealloc
,
282 crypto
= qcrypto_block_create(opts
, NULL
,
283 block_crypto_init_func
,
284 block_crypto_write_func
,
295 qcrypto_block_free(crypto
);
300 static int coroutine_fn
301 block_crypto_co_truncate(BlockDriverState
*bs
, int64_t offset
, bool exact
,
302 PreallocMode prealloc
, BdrvRequestFlags flags
,
305 BlockCrypto
*crypto
= bs
->opaque
;
306 uint64_t payload_offset
=
307 qcrypto_block_get_payload_offset(crypto
->block
);
309 if (payload_offset
> INT64_MAX
- offset
) {
310 error_setg(errp
, "The requested file size is too large");
314 offset
+= payload_offset
;
316 return bdrv_co_truncate(bs
->file
, offset
, exact
, prealloc
, 0, errp
);
319 static void block_crypto_close(BlockDriverState
*bs
)
321 BlockCrypto
*crypto
= bs
->opaque
;
322 qcrypto_block_free(crypto
->block
);
325 static int block_crypto_reopen_prepare(BDRVReopenState
*state
,
326 BlockReopenQueue
*queue
, Error
**errp
)
328 /* nothing needs checking */
333 * 1 MB bounce buffer gives good performance / memory tradeoff
334 * when using cache=none|directsync.
336 #define BLOCK_CRYPTO_MAX_IO_SIZE (1024 * 1024)
338 static coroutine_fn
int
339 block_crypto_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
340 QEMUIOVector
*qiov
, int flags
)
342 BlockCrypto
*crypto
= bs
->opaque
;
343 uint64_t cur_bytes
; /* number of bytes in current iteration */
344 uint64_t bytes_done
= 0;
345 uint8_t *cipher_data
= NULL
;
346 QEMUIOVector hd_qiov
;
348 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
349 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
352 assert(payload_offset
< INT64_MAX
);
353 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
354 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
356 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
358 /* Bounce buffer because we don't wish to expose cipher text
359 * in qiov which points to guest memory.
362 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
364 if (cipher_data
== NULL
) {
370 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
372 qemu_iovec_reset(&hd_qiov
);
373 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
375 ret
= bdrv_co_preadv(bs
->file
, payload_offset
+ offset
+ bytes_done
,
376 cur_bytes
, &hd_qiov
, 0);
381 if (qcrypto_block_decrypt(crypto
->block
, offset
+ bytes_done
,
382 cipher_data
, cur_bytes
, NULL
) < 0) {
387 qemu_iovec_from_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
390 bytes_done
+= cur_bytes
;
394 qemu_iovec_destroy(&hd_qiov
);
395 qemu_vfree(cipher_data
);
401 static coroutine_fn
int
402 block_crypto_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
403 QEMUIOVector
*qiov
, int flags
)
405 BlockCrypto
*crypto
= bs
->opaque
;
406 uint64_t cur_bytes
; /* number of bytes in current iteration */
407 uint64_t bytes_done
= 0;
408 uint8_t *cipher_data
= NULL
;
409 QEMUIOVector hd_qiov
;
411 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
412 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
414 assert(!(flags
& ~BDRV_REQ_FUA
));
415 assert(payload_offset
< INT64_MAX
);
416 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
417 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
419 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
421 /* Bounce buffer because we're not permitted to touch
422 * contents of qiov - it points to guest memory.
425 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
427 if (cipher_data
== NULL
) {
433 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
435 qemu_iovec_to_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
437 if (qcrypto_block_encrypt(crypto
->block
, offset
+ bytes_done
,
438 cipher_data
, cur_bytes
, NULL
) < 0) {
443 qemu_iovec_reset(&hd_qiov
);
444 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
446 ret
= bdrv_co_pwritev(bs
->file
, payload_offset
+ offset
+ bytes_done
,
447 cur_bytes
, &hd_qiov
, flags
);
453 bytes_done
+= cur_bytes
;
457 qemu_iovec_destroy(&hd_qiov
);
458 qemu_vfree(cipher_data
);
463 static void block_crypto_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
465 BlockCrypto
*crypto
= bs
->opaque
;
466 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
467 bs
->bl
.request_alignment
= sector_size
; /* No sub-sector I/O */
471 static int64_t block_crypto_getlength(BlockDriverState
*bs
)
473 BlockCrypto
*crypto
= bs
->opaque
;
474 int64_t len
= bdrv_getlength(bs
->file
->bs
);
476 uint64_t offset
= qcrypto_block_get_payload_offset(crypto
->block
);
477 assert(offset
< INT64_MAX
);
489 static BlockMeasureInfo
*block_crypto_measure(QemuOpts
*opts
,
490 BlockDriverState
*in_bs
,
493 g_autoptr(QCryptoBlockCreateOptions
) create_opts
= NULL
;
494 Error
*local_err
= NULL
;
495 BlockMeasureInfo
*info
;
497 size_t luks_payload_size
;
501 * Preallocation mode doesn't affect size requirements but we must consume
504 g_free(qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
));
506 size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
509 int64_t ssize
= bdrv_getlength(in_bs
);
512 error_setg_errno(&local_err
, -ssize
,
513 "Unable to get image virtual_size");
520 cryptoopts
= qemu_opts_to_qdict_filtered(opts
, NULL
,
521 &block_crypto_create_opts_luks
, true);
522 qdict_put_str(cryptoopts
, "format", "luks");
523 create_opts
= block_crypto_create_opts_init(cryptoopts
, &local_err
);
524 qobject_unref(cryptoopts
);
529 if (!qcrypto_block_calculate_payload_offset(create_opts
, NULL
,
536 * Unallocated blocks are still encrypted so allocation status makes no
537 * difference to the file size.
539 info
= g_new(BlockMeasureInfo
, 1);
540 info
->fully_allocated
= luks_payload_size
+ size
;
541 info
->required
= luks_payload_size
+ size
;
545 error_propagate(errp
, local_err
);
550 static int block_crypto_probe_luks(const uint8_t *buf
,
552 const char *filename
) {
553 return block_crypto_probe_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
554 buf
, buf_size
, filename
);
557 static int block_crypto_open_luks(BlockDriverState
*bs
,
562 return block_crypto_open_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
563 &block_crypto_runtime_opts_luks
,
564 bs
, options
, flags
, errp
);
567 static int coroutine_fn
568 block_crypto_co_create_luks(BlockdevCreateOptions
*create_options
, Error
**errp
)
570 BlockdevCreateOptionsLUKS
*luks_opts
;
571 BlockDriverState
*bs
= NULL
;
572 QCryptoBlockCreateOptions create_opts
;
573 PreallocMode preallocation
= PREALLOC_MODE_OFF
;
576 assert(create_options
->driver
== BLOCKDEV_DRIVER_LUKS
);
577 luks_opts
= &create_options
->u
.luks
;
579 bs
= bdrv_open_blockdev_ref(luks_opts
->file
, errp
);
584 create_opts
= (QCryptoBlockCreateOptions
) {
585 .format
= Q_CRYPTO_BLOCK_FORMAT_LUKS
,
586 .u
.luks
= *qapi_BlockdevCreateOptionsLUKS_base(luks_opts
),
589 if (luks_opts
->has_preallocation
) {
590 preallocation
= luks_opts
->preallocation
;
593 ret
= block_crypto_co_create_generic(bs
, luks_opts
->size
, &create_opts
,
594 preallocation
, errp
);
605 static int coroutine_fn
block_crypto_co_create_opts_luks(BlockDriver
*drv
,
606 const char *filename
,
610 QCryptoBlockCreateOptions
*create_opts
= NULL
;
611 BlockDriverState
*bs
= NULL
;
613 PreallocMode prealloc
;
617 Error
*local_err
= NULL
;
620 size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
622 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
623 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, buf
,
624 PREALLOC_MODE_OFF
, &local_err
);
627 error_propagate(errp
, local_err
);
631 cryptoopts
= qemu_opts_to_qdict_filtered(opts
, NULL
,
632 &block_crypto_create_opts_luks
,
635 qdict_put_str(cryptoopts
, "format", "luks");
636 create_opts
= block_crypto_create_opts_init(cryptoopts
, errp
);
642 /* Create protocol layer */
643 ret
= bdrv_create_file(filename
, opts
, errp
);
648 bs
= bdrv_open(filename
, NULL
, NULL
,
649 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
655 /* Create format layer */
656 ret
= block_crypto_co_create_generic(bs
, size
, create_opts
, prealloc
, errp
);
664 * If an error occurred, delete 'filename'. Even if the file existed
665 * beforehand, it has been truncated and corrupted in the process.
668 Error
*local_delete_err
= NULL
;
669 int r_del
= bdrv_co_delete_file(bs
, &local_delete_err
);
671 * ENOTSUP will happen if the block driver doesn't support
672 * the 'bdrv_co_delete_file' interface. This is a predictable
673 * scenario and shouldn't be reported back to the user.
675 if ((r_del
< 0) && (r_del
!= -ENOTSUP
)) {
676 error_report_err(local_delete_err
);
681 qapi_free_QCryptoBlockCreateOptions(create_opts
);
682 qobject_unref(cryptoopts
);
686 static int block_crypto_get_info_luks(BlockDriverState
*bs
,
687 BlockDriverInfo
*bdi
)
689 BlockDriverInfo subbdi
;
692 ret
= bdrv_get_info(bs
->file
->bs
, &subbdi
);
697 bdi
->unallocated_blocks_are_zero
= false;
698 bdi
->cluster_size
= subbdi
.cluster_size
;
703 static ImageInfoSpecific
*
704 block_crypto_get_specific_info_luks(BlockDriverState
*bs
, Error
**errp
)
706 BlockCrypto
*crypto
= bs
->opaque
;
707 ImageInfoSpecific
*spec_info
;
708 QCryptoBlockInfo
*info
;
710 info
= qcrypto_block_get_info(crypto
->block
, errp
);
714 assert(info
->format
== Q_CRYPTO_BLOCK_FORMAT_LUKS
);
716 spec_info
= g_new(ImageInfoSpecific
, 1);
717 spec_info
->type
= IMAGE_INFO_SPECIFIC_KIND_LUKS
;
718 spec_info
->u
.luks
.data
= g_new(QCryptoBlockInfoLUKS
, 1);
719 *spec_info
->u
.luks
.data
= info
->u
.luks
;
721 /* Blank out pointers we've just stolen to avoid double free */
722 memset(&info
->u
.luks
, 0, sizeof(info
->u
.luks
));
724 qapi_free_QCryptoBlockInfo(info
);
729 static const char *const block_crypto_strong_runtime_opts
[] = {
730 BLOCK_CRYPTO_OPT_LUKS_KEY_SECRET
,
735 static BlockDriver bdrv_crypto_luks
= {
736 .format_name
= "luks",
737 .instance_size
= sizeof(BlockCrypto
),
738 .bdrv_probe
= block_crypto_probe_luks
,
739 .bdrv_open
= block_crypto_open_luks
,
740 .bdrv_close
= block_crypto_close
,
741 /* This driver doesn't modify LUKS metadata except when creating image.
742 * Allow share-rw=on as a special case. */
743 .bdrv_child_perm
= bdrv_filter_default_perms
,
744 .bdrv_co_create
= block_crypto_co_create_luks
,
745 .bdrv_co_create_opts
= block_crypto_co_create_opts_luks
,
746 .bdrv_co_truncate
= block_crypto_co_truncate
,
747 .create_opts
= &block_crypto_create_opts_luks
,
749 .bdrv_reopen_prepare
= block_crypto_reopen_prepare
,
750 .bdrv_refresh_limits
= block_crypto_refresh_limits
,
751 .bdrv_co_preadv
= block_crypto_co_preadv
,
752 .bdrv_co_pwritev
= block_crypto_co_pwritev
,
753 .bdrv_getlength
= block_crypto_getlength
,
754 .bdrv_measure
= block_crypto_measure
,
755 .bdrv_get_info
= block_crypto_get_info_luks
,
756 .bdrv_get_specific_info
= block_crypto_get_specific_info_luks
,
758 .strong_runtime_opts
= block_crypto_strong_runtime_opts
,
761 static void block_crypto_init(void)
763 bdrv_register(&bdrv_crypto_luks
);
766 block_init(block_crypto_init
);