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/option.h"
34 typedef struct BlockCrypto BlockCrypto
;
41 static int block_crypto_probe_generic(QCryptoBlockFormat format
,
46 if (qcrypto_block_has_format(format
, buf
, buf_size
)) {
54 static ssize_t
block_crypto_read_func(QCryptoBlock
*block
,
61 BlockDriverState
*bs
= opaque
;
64 ret
= bdrv_pread(bs
->file
, offset
, buf
, buflen
);
66 error_setg_errno(errp
, -ret
, "Could not read encryption header");
73 struct BlockCryptoCreateData
{
79 static ssize_t
block_crypto_write_func(QCryptoBlock
*block
,
86 struct BlockCryptoCreateData
*data
= opaque
;
89 ret
= blk_pwrite(data
->blk
, offset
, buf
, buflen
, 0);
91 error_setg_errno(errp
, -ret
, "Could not write encryption header");
98 static ssize_t
block_crypto_init_func(QCryptoBlock
*block
,
103 struct BlockCryptoCreateData
*data
= opaque
;
105 if (data
->size
> INT64_MAX
|| headerlen
> INT64_MAX
- data
->size
) {
106 error_setg(errp
, "The requested file size is too large");
110 /* User provided size should reflect amount of space made
111 * available to the guest, so we must take account of that
112 * which will be used by the crypto header
114 return blk_truncate(data
->blk
, data
->size
+ headerlen
, PREALLOC_MODE_OFF
,
119 static QemuOptsList block_crypto_runtime_opts_luks
= {
121 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_runtime_opts_luks
.head
),
123 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
124 { /* end of list */ }
129 static QemuOptsList block_crypto_create_opts_luks
= {
131 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_create_opts_luks
.head
),
134 .name
= BLOCK_OPT_SIZE
,
135 .type
= QEMU_OPT_SIZE
,
136 .help
= "Virtual disk size"
138 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
139 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG(""),
140 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE(""),
141 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG(""),
142 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG(""),
143 BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG(""),
144 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME(""),
145 { /* end of list */ }
150 QCryptoBlockOpenOptions
*
151 block_crypto_open_opts_init(QCryptoBlockFormat format
,
156 QCryptoBlockOpenOptions
*ret
= NULL
;
157 Error
*local_err
= NULL
;
159 ret
= g_new0(QCryptoBlockOpenOptions
, 1);
160 ret
->format
= format
;
162 v
= qobject_input_visitor_new_flat_confused(opts
, &local_err
);
167 visit_start_struct(v
, NULL
, NULL
, 0, &local_err
);
173 case Q_CRYPTO_BLOCK_FORMAT_LUKS
:
174 visit_type_QCryptoBlockOptionsLUKS_members(
175 v
, &ret
->u
.luks
, &local_err
);
178 case Q_CRYPTO_BLOCK_FORMAT_QCOW
:
179 visit_type_QCryptoBlockOptionsQCow_members(
180 v
, &ret
->u
.qcow
, &local_err
);
184 error_setg(&local_err
, "Unsupported block format %d", format
);
188 visit_check_struct(v
, &local_err
);
191 visit_end_struct(v
, NULL
);
195 error_propagate(errp
, local_err
);
196 qapi_free_QCryptoBlockOpenOptions(ret
);
204 QCryptoBlockCreateOptions
*
205 block_crypto_create_opts_init(QCryptoBlockFormat format
,
210 QCryptoBlockCreateOptions
*ret
= NULL
;
211 Error
*local_err
= NULL
;
213 ret
= g_new0(QCryptoBlockCreateOptions
, 1);
214 ret
->format
= format
;
216 v
= qobject_input_visitor_new_flat_confused(opts
, &local_err
);
221 visit_start_struct(v
, NULL
, NULL
, 0, &local_err
);
227 case Q_CRYPTO_BLOCK_FORMAT_LUKS
:
228 visit_type_QCryptoBlockCreateOptionsLUKS_members(
229 v
, &ret
->u
.luks
, &local_err
);
232 case Q_CRYPTO_BLOCK_FORMAT_QCOW
:
233 visit_type_QCryptoBlockOptionsQCow_members(
234 v
, &ret
->u
.qcow
, &local_err
);
238 error_setg(&local_err
, "Unsupported block format %d", format
);
242 visit_check_struct(v
, &local_err
);
245 visit_end_struct(v
, NULL
);
249 error_propagate(errp
, local_err
);
250 qapi_free_QCryptoBlockCreateOptions(ret
);
258 static int block_crypto_open_generic(QCryptoBlockFormat format
,
259 QemuOptsList
*opts_spec
,
260 BlockDriverState
*bs
,
265 BlockCrypto
*crypto
= bs
->opaque
;
266 QemuOpts
*opts
= NULL
;
267 Error
*local_err
= NULL
;
269 QCryptoBlockOpenOptions
*open_opts
= NULL
;
270 unsigned int cflags
= 0;
271 QDict
*cryptoopts
= NULL
;
273 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
279 bs
->supported_write_flags
= BDRV_REQ_FUA
&
280 bs
->file
->bs
->supported_write_flags
;
282 opts
= qemu_opts_create(opts_spec
, NULL
, 0, &error_abort
);
283 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
285 error_propagate(errp
, local_err
);
289 cryptoopts
= qemu_opts_to_qdict(opts
, NULL
);
291 open_opts
= block_crypto_open_opts_init(format
, cryptoopts
, errp
);
296 if (flags
& BDRV_O_NO_IO
) {
297 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
299 crypto
->block
= qcrypto_block_open(open_opts
, NULL
,
300 block_crypto_read_func
,
305 if (!crypto
->block
) {
310 bs
->encrypted
= true;
314 qobject_unref(cryptoopts
);
315 qapi_free_QCryptoBlockOpenOptions(open_opts
);
320 static int block_crypto_co_create_generic(BlockDriverState
*bs
,
322 QCryptoBlockCreateOptions
*opts
,
327 QCryptoBlock
*crypto
= NULL
;
328 struct BlockCryptoCreateData data
;
330 blk
= blk_new(BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
);
332 ret
= blk_insert_bs(blk
, bs
, errp
);
337 data
= (struct BlockCryptoCreateData
) {
342 crypto
= qcrypto_block_create(opts
, NULL
,
343 block_crypto_init_func
,
344 block_crypto_write_func
,
355 qcrypto_block_free(crypto
);
360 static int block_crypto_truncate(BlockDriverState
*bs
, int64_t offset
,
361 PreallocMode prealloc
, Error
**errp
)
363 BlockCrypto
*crypto
= bs
->opaque
;
364 uint64_t payload_offset
=
365 qcrypto_block_get_payload_offset(crypto
->block
);
367 if (payload_offset
> INT64_MAX
- offset
) {
368 error_setg(errp
, "The requested file size is too large");
372 offset
+= payload_offset
;
374 return bdrv_truncate(bs
->file
, offset
, prealloc
, errp
);
377 static void block_crypto_close(BlockDriverState
*bs
)
379 BlockCrypto
*crypto
= bs
->opaque
;
380 qcrypto_block_free(crypto
->block
);
383 static int block_crypto_reopen_prepare(BDRVReopenState
*state
,
384 BlockReopenQueue
*queue
, Error
**errp
)
386 /* nothing needs checking */
391 * 1 MB bounce buffer gives good performance / memory tradeoff
392 * when using cache=none|directsync.
394 #define BLOCK_CRYPTO_MAX_IO_SIZE (1024 * 1024)
396 static coroutine_fn
int
397 block_crypto_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
398 QEMUIOVector
*qiov
, int flags
)
400 BlockCrypto
*crypto
= bs
->opaque
;
401 uint64_t cur_bytes
; /* number of bytes in current iteration */
402 uint64_t bytes_done
= 0;
403 uint8_t *cipher_data
= NULL
;
404 QEMUIOVector hd_qiov
;
406 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
407 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
410 assert(payload_offset
< INT64_MAX
);
411 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
412 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
414 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
416 /* Bounce buffer because we don't wish to expose cipher text
417 * in qiov which points to guest memory.
420 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
422 if (cipher_data
== NULL
) {
428 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
430 qemu_iovec_reset(&hd_qiov
);
431 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
433 ret
= bdrv_co_preadv(bs
->file
, payload_offset
+ offset
+ bytes_done
,
434 cur_bytes
, &hd_qiov
, 0);
439 if (qcrypto_block_decrypt(crypto
->block
, offset
+ bytes_done
,
440 cipher_data
, cur_bytes
, NULL
) < 0) {
445 qemu_iovec_from_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
448 bytes_done
+= cur_bytes
;
452 qemu_iovec_destroy(&hd_qiov
);
453 qemu_vfree(cipher_data
);
459 static coroutine_fn
int
460 block_crypto_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
461 QEMUIOVector
*qiov
, int flags
)
463 BlockCrypto
*crypto
= bs
->opaque
;
464 uint64_t cur_bytes
; /* number of bytes in current iteration */
465 uint64_t bytes_done
= 0;
466 uint8_t *cipher_data
= NULL
;
467 QEMUIOVector hd_qiov
;
469 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
470 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
472 assert(!(flags
& ~BDRV_REQ_FUA
));
473 assert(payload_offset
< INT64_MAX
);
474 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
475 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
477 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
479 /* Bounce buffer because we're not permitted to touch
480 * contents of qiov - it points to guest memory.
483 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
485 if (cipher_data
== NULL
) {
491 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
493 qemu_iovec_to_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
495 if (qcrypto_block_encrypt(crypto
->block
, offset
+ bytes_done
,
496 cipher_data
, cur_bytes
, NULL
) < 0) {
501 qemu_iovec_reset(&hd_qiov
);
502 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
504 ret
= bdrv_co_pwritev(bs
->file
, payload_offset
+ offset
+ bytes_done
,
505 cur_bytes
, &hd_qiov
, flags
);
511 bytes_done
+= cur_bytes
;
515 qemu_iovec_destroy(&hd_qiov
);
516 qemu_vfree(cipher_data
);
521 static void block_crypto_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
523 BlockCrypto
*crypto
= bs
->opaque
;
524 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
525 bs
->bl
.request_alignment
= sector_size
; /* No sub-sector I/O */
529 static int64_t block_crypto_getlength(BlockDriverState
*bs
)
531 BlockCrypto
*crypto
= bs
->opaque
;
532 int64_t len
= bdrv_getlength(bs
->file
->bs
);
534 uint64_t offset
= qcrypto_block_get_payload_offset(crypto
->block
);
535 assert(offset
< INT64_MAX
);
547 static int block_crypto_probe_luks(const uint8_t *buf
,
549 const char *filename
) {
550 return block_crypto_probe_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
551 buf
, buf_size
, filename
);
554 static int block_crypto_open_luks(BlockDriverState
*bs
,
559 return block_crypto_open_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
560 &block_crypto_runtime_opts_luks
,
561 bs
, options
, flags
, errp
);
564 static int coroutine_fn
565 block_crypto_co_create_luks(BlockdevCreateOptions
*create_options
, Error
**errp
)
567 BlockdevCreateOptionsLUKS
*luks_opts
;
568 BlockDriverState
*bs
= NULL
;
569 QCryptoBlockCreateOptions create_opts
;
572 assert(create_options
->driver
== BLOCKDEV_DRIVER_LUKS
);
573 luks_opts
= &create_options
->u
.luks
;
575 bs
= bdrv_open_blockdev_ref(luks_opts
->file
, errp
);
580 create_opts
= (QCryptoBlockCreateOptions
) {
581 .format
= Q_CRYPTO_BLOCK_FORMAT_LUKS
,
582 .u
.luks
= *qapi_BlockdevCreateOptionsLUKS_base(luks_opts
),
585 ret
= block_crypto_co_create_generic(bs
, luks_opts
->size
, &create_opts
,
597 static int coroutine_fn
block_crypto_co_create_opts_luks(const char *filename
,
601 QCryptoBlockCreateOptions
*create_opts
= NULL
;
602 BlockDriverState
*bs
= NULL
;
608 size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
610 cryptoopts
= qemu_opts_to_qdict_filtered(opts
, NULL
,
611 &block_crypto_create_opts_luks
,
614 create_opts
= block_crypto_create_opts_init(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
621 /* Create protocol layer */
622 ret
= bdrv_create_file(filename
, opts
, errp
);
627 bs
= bdrv_open(filename
, NULL
, NULL
,
628 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
634 /* Create format layer */
635 ret
= block_crypto_co_create_generic(bs
, size
, create_opts
, errp
);
643 qapi_free_QCryptoBlockCreateOptions(create_opts
);
644 qobject_unref(cryptoopts
);
648 static int block_crypto_get_info_luks(BlockDriverState
*bs
,
649 BlockDriverInfo
*bdi
)
651 BlockDriverInfo subbdi
;
654 ret
= bdrv_get_info(bs
->file
->bs
, &subbdi
);
659 bdi
->unallocated_blocks_are_zero
= false;
660 bdi
->cluster_size
= subbdi
.cluster_size
;
665 static ImageInfoSpecific
*
666 block_crypto_get_specific_info_luks(BlockDriverState
*bs
)
668 BlockCrypto
*crypto
= bs
->opaque
;
669 ImageInfoSpecific
*spec_info
;
670 QCryptoBlockInfo
*info
;
672 info
= qcrypto_block_get_info(crypto
->block
, NULL
);
676 if (info
->format
!= Q_CRYPTO_BLOCK_FORMAT_LUKS
) {
677 qapi_free_QCryptoBlockInfo(info
);
681 spec_info
= g_new(ImageInfoSpecific
, 1);
682 spec_info
->type
= IMAGE_INFO_SPECIFIC_KIND_LUKS
;
683 spec_info
->u
.luks
.data
= g_new(QCryptoBlockInfoLUKS
, 1);
684 *spec_info
->u
.luks
.data
= info
->u
.luks
;
686 /* Blank out pointers we've just stolen to avoid double free */
687 memset(&info
->u
.luks
, 0, sizeof(info
->u
.luks
));
689 qapi_free_QCryptoBlockInfo(info
);
694 BlockDriver bdrv_crypto_luks
= {
695 .format_name
= "luks",
696 .instance_size
= sizeof(BlockCrypto
),
697 .bdrv_probe
= block_crypto_probe_luks
,
698 .bdrv_open
= block_crypto_open_luks
,
699 .bdrv_close
= block_crypto_close
,
700 .bdrv_child_perm
= bdrv_format_default_perms
,
701 .bdrv_co_create
= block_crypto_co_create_luks
,
702 .bdrv_co_create_opts
= block_crypto_co_create_opts_luks
,
703 .bdrv_truncate
= block_crypto_truncate
,
704 .create_opts
= &block_crypto_create_opts_luks
,
706 .bdrv_reopen_prepare
= block_crypto_reopen_prepare
,
707 .bdrv_refresh_limits
= block_crypto_refresh_limits
,
708 .bdrv_co_preadv
= block_crypto_co_preadv
,
709 .bdrv_co_pwritev
= block_crypto_co_pwritev
,
710 .bdrv_getlength
= block_crypto_getlength
,
711 .bdrv_get_info
= block_crypto_get_info_luks
,
712 .bdrv_get_specific_info
= block_crypto_get_specific_info_luks
,
715 static void block_crypto_init(void)
717 bdrv_register(&bdrv_crypto_luks
);
720 block_init(block_crypto_init
);