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
);
206 static int block_crypto_open_generic(QCryptoBlockFormat format
,
207 QemuOptsList
*opts_spec
,
208 BlockDriverState
*bs
,
213 BlockCrypto
*crypto
= bs
->opaque
;
214 QemuOpts
*opts
= NULL
;
215 Error
*local_err
= NULL
;
217 QCryptoBlockOpenOptions
*open_opts
= NULL
;
218 unsigned int cflags
= 0;
219 QDict
*cryptoopts
= NULL
;
221 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
227 bs
->supported_write_flags
= BDRV_REQ_FUA
&
228 bs
->file
->bs
->supported_write_flags
;
230 opts
= qemu_opts_create(opts_spec
, NULL
, 0, &error_abort
);
231 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
233 error_propagate(errp
, local_err
);
237 cryptoopts
= qemu_opts_to_qdict(opts
, NULL
);
238 qdict_put_str(cryptoopts
, "format", QCryptoBlockFormat_str(format
));
240 open_opts
= block_crypto_open_opts_init(cryptoopts
, errp
);
245 if (flags
& BDRV_O_NO_IO
) {
246 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
248 crypto
->block
= qcrypto_block_open(open_opts
, NULL
,
249 block_crypto_read_func
,
255 if (!crypto
->block
) {
260 bs
->encrypted
= true;
264 qobject_unref(cryptoopts
);
265 qapi_free_QCryptoBlockOpenOptions(open_opts
);
270 static int block_crypto_co_create_generic(BlockDriverState
*bs
,
272 QCryptoBlockCreateOptions
*opts
,
273 PreallocMode prealloc
,
278 QCryptoBlock
*crypto
= NULL
;
279 struct BlockCryptoCreateData data
;
281 blk
= blk_new_with_bs(bs
, BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
,
288 if (prealloc
== PREALLOC_MODE_METADATA
) {
289 prealloc
= PREALLOC_MODE_OFF
;
292 data
= (struct BlockCryptoCreateData
) {
295 .prealloc
= prealloc
,
298 crypto
= qcrypto_block_create(opts
, NULL
,
299 block_crypto_init_func
,
300 block_crypto_write_func
,
311 qcrypto_block_free(crypto
);
316 static int coroutine_fn
317 block_crypto_co_truncate(BlockDriverState
*bs
, int64_t offset
, bool exact
,
318 PreallocMode prealloc
, BdrvRequestFlags flags
,
321 BlockCrypto
*crypto
= bs
->opaque
;
322 uint64_t payload_offset
=
323 qcrypto_block_get_payload_offset(crypto
->block
);
325 if (payload_offset
> INT64_MAX
- offset
) {
326 error_setg(errp
, "The requested file size is too large");
330 offset
+= payload_offset
;
332 return bdrv_co_truncate(bs
->file
, offset
, exact
, prealloc
, 0, errp
);
335 static void block_crypto_close(BlockDriverState
*bs
)
337 BlockCrypto
*crypto
= bs
->opaque
;
338 qcrypto_block_free(crypto
->block
);
341 static int block_crypto_reopen_prepare(BDRVReopenState
*state
,
342 BlockReopenQueue
*queue
, Error
**errp
)
344 /* nothing needs checking */
349 * 1 MB bounce buffer gives good performance / memory tradeoff
350 * when using cache=none|directsync.
352 #define BLOCK_CRYPTO_MAX_IO_SIZE (1024 * 1024)
354 static coroutine_fn
int
355 block_crypto_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
356 QEMUIOVector
*qiov
, int flags
)
358 BlockCrypto
*crypto
= bs
->opaque
;
359 uint64_t cur_bytes
; /* number of bytes in current iteration */
360 uint64_t bytes_done
= 0;
361 uint8_t *cipher_data
= NULL
;
362 QEMUIOVector hd_qiov
;
364 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
365 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
368 assert(payload_offset
< INT64_MAX
);
369 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
370 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
372 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
374 /* Bounce buffer because we don't wish to expose cipher text
375 * in qiov which points to guest memory.
378 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
380 if (cipher_data
== NULL
) {
386 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
388 qemu_iovec_reset(&hd_qiov
);
389 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
391 ret
= bdrv_co_preadv(bs
->file
, payload_offset
+ offset
+ bytes_done
,
392 cur_bytes
, &hd_qiov
, 0);
397 if (qcrypto_block_decrypt(crypto
->block
, offset
+ bytes_done
,
398 cipher_data
, cur_bytes
, NULL
) < 0) {
403 qemu_iovec_from_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
406 bytes_done
+= cur_bytes
;
410 qemu_iovec_destroy(&hd_qiov
);
411 qemu_vfree(cipher_data
);
417 static coroutine_fn
int
418 block_crypto_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
419 QEMUIOVector
*qiov
, int flags
)
421 BlockCrypto
*crypto
= bs
->opaque
;
422 uint64_t cur_bytes
; /* number of bytes in current iteration */
423 uint64_t bytes_done
= 0;
424 uint8_t *cipher_data
= NULL
;
425 QEMUIOVector hd_qiov
;
427 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
428 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
430 assert(!(flags
& ~BDRV_REQ_FUA
));
431 assert(payload_offset
< INT64_MAX
);
432 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
433 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
435 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
437 /* Bounce buffer because we're not permitted to touch
438 * contents of qiov - it points to guest memory.
441 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
443 if (cipher_data
== NULL
) {
449 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
451 qemu_iovec_to_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
453 if (qcrypto_block_encrypt(crypto
->block
, offset
+ bytes_done
,
454 cipher_data
, cur_bytes
, NULL
) < 0) {
459 qemu_iovec_reset(&hd_qiov
);
460 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
462 ret
= bdrv_co_pwritev(bs
->file
, payload_offset
+ offset
+ bytes_done
,
463 cur_bytes
, &hd_qiov
, flags
);
469 bytes_done
+= cur_bytes
;
473 qemu_iovec_destroy(&hd_qiov
);
474 qemu_vfree(cipher_data
);
479 static void block_crypto_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
481 BlockCrypto
*crypto
= bs
->opaque
;
482 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
483 bs
->bl
.request_alignment
= sector_size
; /* No sub-sector I/O */
487 static int64_t block_crypto_getlength(BlockDriverState
*bs
)
489 BlockCrypto
*crypto
= bs
->opaque
;
490 int64_t len
= bdrv_getlength(bs
->file
->bs
);
492 uint64_t offset
= qcrypto_block_get_payload_offset(crypto
->block
);
493 assert(offset
< INT64_MAX
);
505 static BlockMeasureInfo
*block_crypto_measure(QemuOpts
*opts
,
506 BlockDriverState
*in_bs
,
509 g_autoptr(QCryptoBlockCreateOptions
) create_opts
= NULL
;
510 Error
*local_err
= NULL
;
511 BlockMeasureInfo
*info
;
513 size_t luks_payload_size
;
517 * Preallocation mode doesn't affect size requirements but we must consume
520 g_free(qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
));
522 size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
525 int64_t ssize
= bdrv_getlength(in_bs
);
528 error_setg_errno(&local_err
, -ssize
,
529 "Unable to get image virtual_size");
536 cryptoopts
= qemu_opts_to_qdict_filtered(opts
, NULL
,
537 &block_crypto_create_opts_luks
, true);
538 qdict_put_str(cryptoopts
, "format", "luks");
539 create_opts
= block_crypto_create_opts_init(cryptoopts
, &local_err
);
540 qobject_unref(cryptoopts
);
545 if (!qcrypto_block_calculate_payload_offset(create_opts
, NULL
,
552 * Unallocated blocks are still encrypted so allocation status makes no
553 * difference to the file size.
555 info
= g_new(BlockMeasureInfo
, 1);
556 info
->fully_allocated
= luks_payload_size
+ size
;
557 info
->required
= luks_payload_size
+ size
;
561 error_propagate(errp
, local_err
);
566 static int block_crypto_probe_luks(const uint8_t *buf
,
568 const char *filename
) {
569 return block_crypto_probe_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
570 buf
, buf_size
, filename
);
573 static int block_crypto_open_luks(BlockDriverState
*bs
,
578 return block_crypto_open_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
579 &block_crypto_runtime_opts_luks
,
580 bs
, options
, flags
, errp
);
583 static int coroutine_fn
584 block_crypto_co_create_luks(BlockdevCreateOptions
*create_options
, Error
**errp
)
586 BlockdevCreateOptionsLUKS
*luks_opts
;
587 BlockDriverState
*bs
= NULL
;
588 QCryptoBlockCreateOptions create_opts
;
589 PreallocMode preallocation
= PREALLOC_MODE_OFF
;
592 assert(create_options
->driver
== BLOCKDEV_DRIVER_LUKS
);
593 luks_opts
= &create_options
->u
.luks
;
595 bs
= bdrv_open_blockdev_ref(luks_opts
->file
, errp
);
600 create_opts
= (QCryptoBlockCreateOptions
) {
601 .format
= Q_CRYPTO_BLOCK_FORMAT_LUKS
,
602 .u
.luks
= *qapi_BlockdevCreateOptionsLUKS_base(luks_opts
),
605 if (luks_opts
->has_preallocation
) {
606 preallocation
= luks_opts
->preallocation
;
609 ret
= block_crypto_co_create_generic(bs
, luks_opts
->size
, &create_opts
,
610 preallocation
, errp
);
621 static int coroutine_fn
block_crypto_co_create_opts_luks(BlockDriver
*drv
,
622 const char *filename
,
626 QCryptoBlockCreateOptions
*create_opts
= NULL
;
627 BlockDriverState
*bs
= NULL
;
629 PreallocMode prealloc
;
633 Error
*local_err
= NULL
;
636 size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
638 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
639 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, buf
,
640 PREALLOC_MODE_OFF
, &local_err
);
643 error_propagate(errp
, local_err
);
647 cryptoopts
= qemu_opts_to_qdict_filtered(opts
, NULL
,
648 &block_crypto_create_opts_luks
,
651 qdict_put_str(cryptoopts
, "format", "luks");
652 create_opts
= block_crypto_create_opts_init(cryptoopts
, errp
);
658 /* Create protocol layer */
659 ret
= bdrv_create_file(filename
, opts
, errp
);
664 bs
= bdrv_open(filename
, NULL
, NULL
,
665 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
671 /* Create format layer */
672 ret
= block_crypto_co_create_generic(bs
, size
, create_opts
, prealloc
, errp
);
680 * If an error occurred, delete 'filename'. Even if the file existed
681 * beforehand, it has been truncated and corrupted in the process.
684 Error
*local_delete_err
= NULL
;
685 int r_del
= bdrv_co_delete_file(bs
, &local_delete_err
);
687 * ENOTSUP will happen if the block driver doesn't support
688 * the 'bdrv_co_delete_file' interface. This is a predictable
689 * scenario and shouldn't be reported back to the user.
691 if ((r_del
< 0) && (r_del
!= -ENOTSUP
)) {
692 error_report_err(local_delete_err
);
697 qapi_free_QCryptoBlockCreateOptions(create_opts
);
698 qobject_unref(cryptoopts
);
702 static int block_crypto_get_info_luks(BlockDriverState
*bs
,
703 BlockDriverInfo
*bdi
)
705 BlockDriverInfo subbdi
;
708 ret
= bdrv_get_info(bs
->file
->bs
, &subbdi
);
713 bdi
->unallocated_blocks_are_zero
= false;
714 bdi
->cluster_size
= subbdi
.cluster_size
;
719 static ImageInfoSpecific
*
720 block_crypto_get_specific_info_luks(BlockDriverState
*bs
, Error
**errp
)
722 BlockCrypto
*crypto
= bs
->opaque
;
723 ImageInfoSpecific
*spec_info
;
724 QCryptoBlockInfo
*info
;
726 info
= qcrypto_block_get_info(crypto
->block
, errp
);
730 assert(info
->format
== Q_CRYPTO_BLOCK_FORMAT_LUKS
);
732 spec_info
= g_new(ImageInfoSpecific
, 1);
733 spec_info
->type
= IMAGE_INFO_SPECIFIC_KIND_LUKS
;
734 spec_info
->u
.luks
.data
= g_new(QCryptoBlockInfoLUKS
, 1);
735 *spec_info
->u
.luks
.data
= info
->u
.luks
;
737 /* Blank out pointers we've just stolen to avoid double free */
738 memset(&info
->u
.luks
, 0, sizeof(info
->u
.luks
));
740 qapi_free_QCryptoBlockInfo(info
);
745 static const char *const block_crypto_strong_runtime_opts
[] = {
746 BLOCK_CRYPTO_OPT_LUKS_KEY_SECRET
,
751 static BlockDriver bdrv_crypto_luks
= {
752 .format_name
= "luks",
753 .instance_size
= sizeof(BlockCrypto
),
754 .bdrv_probe
= block_crypto_probe_luks
,
755 .bdrv_open
= block_crypto_open_luks
,
756 .bdrv_close
= block_crypto_close
,
757 /* This driver doesn't modify LUKS metadata except when creating image.
758 * Allow share-rw=on as a special case. */
759 .bdrv_child_perm
= bdrv_filter_default_perms
,
760 .bdrv_co_create
= block_crypto_co_create_luks
,
761 .bdrv_co_create_opts
= block_crypto_co_create_opts_luks
,
762 .bdrv_co_truncate
= block_crypto_co_truncate
,
763 .create_opts
= &block_crypto_create_opts_luks
,
765 .bdrv_reopen_prepare
= block_crypto_reopen_prepare
,
766 .bdrv_refresh_limits
= block_crypto_refresh_limits
,
767 .bdrv_co_preadv
= block_crypto_co_preadv
,
768 .bdrv_co_pwritev
= block_crypto_co_pwritev
,
769 .bdrv_getlength
= block_crypto_getlength
,
770 .bdrv_measure
= block_crypto_measure
,
771 .bdrv_get_info
= block_crypto_get_info_luks
,
772 .bdrv_get_specific_info
= block_crypto_get_specific_info_luks
,
774 .strong_runtime_opts
= block_crypto_strong_runtime_opts
,
777 static void block_crypto_init(void)
779 bdrv_register(&bdrv_crypto_luks
);
782 block_init(block_crypto_init
);