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"
35 typedef struct BlockCrypto BlockCrypto
;
42 static int block_crypto_probe_generic(QCryptoBlockFormat format
,
47 if (qcrypto_block_has_format(format
, buf
, buf_size
)) {
55 static ssize_t
block_crypto_read_func(QCryptoBlock
*block
,
62 BlockDriverState
*bs
= opaque
;
65 ret
= bdrv_pread(bs
->file
, offset
, buf
, buflen
);
67 error_setg_errno(errp
, -ret
, "Could not read encryption header");
74 struct BlockCryptoCreateData
{
77 PreallocMode prealloc
;
81 static ssize_t
block_crypto_write_func(QCryptoBlock
*block
,
88 struct BlockCryptoCreateData
*data
= opaque
;
91 ret
= blk_pwrite(data
->blk
, offset
, buf
, buflen
, 0);
93 error_setg_errno(errp
, -ret
, "Could not write encryption header");
100 static ssize_t
block_crypto_init_func(QCryptoBlock
*block
,
105 struct BlockCryptoCreateData
*data
= opaque
;
107 if (data
->size
> INT64_MAX
|| headerlen
> INT64_MAX
- data
->size
) {
108 error_setg(errp
, "The requested file size is too large");
112 /* User provided size should reflect amount of space made
113 * available to the guest, so we must take account of that
114 * which will be used by the crypto header
116 return blk_truncate(data
->blk
, data
->size
+ headerlen
, false,
117 data
->prealloc
, errp
);
121 static QemuOptsList block_crypto_runtime_opts_luks
= {
123 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_runtime_opts_luks
.head
),
125 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
126 { /* end of list */ }
131 static QemuOptsList block_crypto_create_opts_luks
= {
133 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_create_opts_luks
.head
),
136 .name
= BLOCK_OPT_SIZE
,
137 .type
= QEMU_OPT_SIZE
,
138 .help
= "Virtual disk size"
140 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
141 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG(""),
142 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE(""),
143 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG(""),
144 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG(""),
145 BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG(""),
146 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME(""),
147 { /* end of list */ }
152 QCryptoBlockOpenOptions
*
153 block_crypto_open_opts_init(QDict
*opts
, Error
**errp
)
156 QCryptoBlockOpenOptions
*ret
;
158 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
163 visit_type_QCryptoBlockOpenOptions(v
, NULL
, &ret
, errp
);
170 QCryptoBlockCreateOptions
*
171 block_crypto_create_opts_init(QDict
*opts
, Error
**errp
)
174 QCryptoBlockCreateOptions
*ret
;
176 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
181 visit_type_QCryptoBlockCreateOptions(v
, NULL
, &ret
, errp
);
188 static int block_crypto_open_generic(QCryptoBlockFormat format
,
189 QemuOptsList
*opts_spec
,
190 BlockDriverState
*bs
,
195 BlockCrypto
*crypto
= bs
->opaque
;
196 QemuOpts
*opts
= NULL
;
197 Error
*local_err
= NULL
;
199 QCryptoBlockOpenOptions
*open_opts
= NULL
;
200 unsigned int cflags
= 0;
201 QDict
*cryptoopts
= NULL
;
203 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
209 bs
->supported_write_flags
= BDRV_REQ_FUA
&
210 bs
->file
->bs
->supported_write_flags
;
212 opts
= qemu_opts_create(opts_spec
, NULL
, 0, &error_abort
);
213 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
215 error_propagate(errp
, local_err
);
219 cryptoopts
= qemu_opts_to_qdict(opts
, NULL
);
220 qdict_put_str(cryptoopts
, "format", QCryptoBlockFormat_str(format
));
222 open_opts
= block_crypto_open_opts_init(cryptoopts
, errp
);
227 if (flags
& BDRV_O_NO_IO
) {
228 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
230 crypto
->block
= qcrypto_block_open(open_opts
, NULL
,
231 block_crypto_read_func
,
237 if (!crypto
->block
) {
242 bs
->encrypted
= true;
246 qobject_unref(cryptoopts
);
247 qapi_free_QCryptoBlockOpenOptions(open_opts
);
252 static int block_crypto_co_create_generic(BlockDriverState
*bs
,
254 QCryptoBlockCreateOptions
*opts
,
255 PreallocMode prealloc
,
260 QCryptoBlock
*crypto
= NULL
;
261 struct BlockCryptoCreateData data
;
263 blk
= blk_new(bdrv_get_aio_context(bs
),
264 BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
);
266 ret
= blk_insert_bs(blk
, bs
, errp
);
271 if (prealloc
== PREALLOC_MODE_METADATA
) {
272 prealloc
= PREALLOC_MODE_OFF
;
275 data
= (struct BlockCryptoCreateData
) {
278 .prealloc
= prealloc
,
281 crypto
= qcrypto_block_create(opts
, NULL
,
282 block_crypto_init_func
,
283 block_crypto_write_func
,
294 qcrypto_block_free(crypto
);
299 static int coroutine_fn
300 block_crypto_co_truncate(BlockDriverState
*bs
, int64_t offset
, bool exact
,
301 PreallocMode prealloc
, Error
**errp
)
303 BlockCrypto
*crypto
= bs
->opaque
;
304 uint64_t payload_offset
=
305 qcrypto_block_get_payload_offset(crypto
->block
);
307 if (payload_offset
> INT64_MAX
- offset
) {
308 error_setg(errp
, "The requested file size is too large");
312 offset
+= payload_offset
;
314 return bdrv_co_truncate(bs
->file
, offset
, exact
, prealloc
, errp
);
317 static void block_crypto_close(BlockDriverState
*bs
)
319 BlockCrypto
*crypto
= bs
->opaque
;
320 qcrypto_block_free(crypto
->block
);
323 static int block_crypto_reopen_prepare(BDRVReopenState
*state
,
324 BlockReopenQueue
*queue
, Error
**errp
)
326 /* nothing needs checking */
331 * 1 MB bounce buffer gives good performance / memory tradeoff
332 * when using cache=none|directsync.
334 #define BLOCK_CRYPTO_MAX_IO_SIZE (1024 * 1024)
336 static coroutine_fn
int
337 block_crypto_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
338 QEMUIOVector
*qiov
, int flags
)
340 BlockCrypto
*crypto
= bs
->opaque
;
341 uint64_t cur_bytes
; /* number of bytes in current iteration */
342 uint64_t bytes_done
= 0;
343 uint8_t *cipher_data
= NULL
;
344 QEMUIOVector hd_qiov
;
346 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
347 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
350 assert(payload_offset
< INT64_MAX
);
351 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
352 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
354 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
356 /* Bounce buffer because we don't wish to expose cipher text
357 * in qiov which points to guest memory.
360 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
362 if (cipher_data
== NULL
) {
368 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
370 qemu_iovec_reset(&hd_qiov
);
371 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
373 ret
= bdrv_co_preadv(bs
->file
, payload_offset
+ offset
+ bytes_done
,
374 cur_bytes
, &hd_qiov
, 0);
379 if (qcrypto_block_decrypt(crypto
->block
, offset
+ bytes_done
,
380 cipher_data
, cur_bytes
, NULL
) < 0) {
385 qemu_iovec_from_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
388 bytes_done
+= cur_bytes
;
392 qemu_iovec_destroy(&hd_qiov
);
393 qemu_vfree(cipher_data
);
399 static coroutine_fn
int
400 block_crypto_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
401 QEMUIOVector
*qiov
, int flags
)
403 BlockCrypto
*crypto
= bs
->opaque
;
404 uint64_t cur_bytes
; /* number of bytes in current iteration */
405 uint64_t bytes_done
= 0;
406 uint8_t *cipher_data
= NULL
;
407 QEMUIOVector hd_qiov
;
409 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
410 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
412 assert(!(flags
& ~BDRV_REQ_FUA
));
413 assert(payload_offset
< INT64_MAX
);
414 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
415 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
417 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
419 /* Bounce buffer because we're not permitted to touch
420 * contents of qiov - it points to guest memory.
423 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
425 if (cipher_data
== NULL
) {
431 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
433 qemu_iovec_to_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
435 if (qcrypto_block_encrypt(crypto
->block
, offset
+ bytes_done
,
436 cipher_data
, cur_bytes
, NULL
) < 0) {
441 qemu_iovec_reset(&hd_qiov
);
442 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
444 ret
= bdrv_co_pwritev(bs
->file
, payload_offset
+ offset
+ bytes_done
,
445 cur_bytes
, &hd_qiov
, flags
);
451 bytes_done
+= cur_bytes
;
455 qemu_iovec_destroy(&hd_qiov
);
456 qemu_vfree(cipher_data
);
461 static void block_crypto_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
463 BlockCrypto
*crypto
= bs
->opaque
;
464 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
465 bs
->bl
.request_alignment
= sector_size
; /* No sub-sector I/O */
469 static int64_t block_crypto_getlength(BlockDriverState
*bs
)
471 BlockCrypto
*crypto
= bs
->opaque
;
472 int64_t len
= bdrv_getlength(bs
->file
->bs
);
474 uint64_t offset
= qcrypto_block_get_payload_offset(crypto
->block
);
475 assert(offset
< INT64_MAX
);
487 static int block_crypto_probe_luks(const uint8_t *buf
,
489 const char *filename
) {
490 return block_crypto_probe_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
491 buf
, buf_size
, filename
);
494 static int block_crypto_open_luks(BlockDriverState
*bs
,
499 return block_crypto_open_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
500 &block_crypto_runtime_opts_luks
,
501 bs
, options
, flags
, errp
);
504 static int coroutine_fn
505 block_crypto_co_create_luks(BlockdevCreateOptions
*create_options
, Error
**errp
)
507 BlockdevCreateOptionsLUKS
*luks_opts
;
508 BlockDriverState
*bs
= NULL
;
509 QCryptoBlockCreateOptions create_opts
;
510 PreallocMode preallocation
= PREALLOC_MODE_OFF
;
513 assert(create_options
->driver
== BLOCKDEV_DRIVER_LUKS
);
514 luks_opts
= &create_options
->u
.luks
;
516 bs
= bdrv_open_blockdev_ref(luks_opts
->file
, errp
);
521 create_opts
= (QCryptoBlockCreateOptions
) {
522 .format
= Q_CRYPTO_BLOCK_FORMAT_LUKS
,
523 .u
.luks
= *qapi_BlockdevCreateOptionsLUKS_base(luks_opts
),
526 if (luks_opts
->has_preallocation
) {
527 preallocation
= luks_opts
->preallocation
;
530 ret
= block_crypto_co_create_generic(bs
, luks_opts
->size
, &create_opts
,
531 preallocation
, errp
);
542 static int coroutine_fn
block_crypto_co_create_opts_luks(const char *filename
,
546 QCryptoBlockCreateOptions
*create_opts
= NULL
;
547 BlockDriverState
*bs
= NULL
;
549 PreallocMode prealloc
;
553 Error
*local_err
= NULL
;
556 size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
558 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
559 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, buf
,
560 PREALLOC_MODE_OFF
, &local_err
);
563 error_propagate(errp
, local_err
);
567 cryptoopts
= qemu_opts_to_qdict_filtered(opts
, NULL
,
568 &block_crypto_create_opts_luks
,
571 qdict_put_str(cryptoopts
, "format", "luks");
572 create_opts
= block_crypto_create_opts_init(cryptoopts
, errp
);
578 /* Create protocol layer */
579 ret
= bdrv_create_file(filename
, opts
, errp
);
584 bs
= bdrv_open(filename
, NULL
, NULL
,
585 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
591 /* Create format layer */
592 ret
= block_crypto_co_create_generic(bs
, size
, create_opts
, prealloc
, errp
);
600 qapi_free_QCryptoBlockCreateOptions(create_opts
);
601 qobject_unref(cryptoopts
);
605 static int block_crypto_get_info_luks(BlockDriverState
*bs
,
606 BlockDriverInfo
*bdi
)
608 BlockDriverInfo subbdi
;
611 ret
= bdrv_get_info(bs
->file
->bs
, &subbdi
);
616 bdi
->unallocated_blocks_are_zero
= false;
617 bdi
->cluster_size
= subbdi
.cluster_size
;
622 static ImageInfoSpecific
*
623 block_crypto_get_specific_info_luks(BlockDriverState
*bs
, Error
**errp
)
625 BlockCrypto
*crypto
= bs
->opaque
;
626 ImageInfoSpecific
*spec_info
;
627 QCryptoBlockInfo
*info
;
629 info
= qcrypto_block_get_info(crypto
->block
, errp
);
633 assert(info
->format
== Q_CRYPTO_BLOCK_FORMAT_LUKS
);
635 spec_info
= g_new(ImageInfoSpecific
, 1);
636 spec_info
->type
= IMAGE_INFO_SPECIFIC_KIND_LUKS
;
637 spec_info
->u
.luks
.data
= g_new(QCryptoBlockInfoLUKS
, 1);
638 *spec_info
->u
.luks
.data
= info
->u
.luks
;
640 /* Blank out pointers we've just stolen to avoid double free */
641 memset(&info
->u
.luks
, 0, sizeof(info
->u
.luks
));
643 qapi_free_QCryptoBlockInfo(info
);
648 static const char *const block_crypto_strong_runtime_opts
[] = {
649 BLOCK_CRYPTO_OPT_LUKS_KEY_SECRET
,
654 static BlockDriver bdrv_crypto_luks
= {
655 .format_name
= "luks",
656 .instance_size
= sizeof(BlockCrypto
),
657 .bdrv_probe
= block_crypto_probe_luks
,
658 .bdrv_open
= block_crypto_open_luks
,
659 .bdrv_close
= block_crypto_close
,
660 /* This driver doesn't modify LUKS metadata except when creating image.
661 * Allow share-rw=on as a special case. */
662 .bdrv_child_perm
= bdrv_filter_default_perms
,
663 .bdrv_co_create
= block_crypto_co_create_luks
,
664 .bdrv_co_create_opts
= block_crypto_co_create_opts_luks
,
665 .bdrv_co_truncate
= block_crypto_co_truncate
,
666 .create_opts
= &block_crypto_create_opts_luks
,
668 .bdrv_reopen_prepare
= block_crypto_reopen_prepare
,
669 .bdrv_refresh_limits
= block_crypto_refresh_limits
,
670 .bdrv_co_preadv
= block_crypto_co_preadv
,
671 .bdrv_co_pwritev
= block_crypto_co_pwritev
,
672 .bdrv_getlength
= block_crypto_getlength
,
673 .bdrv_get_info
= block_crypto_get_info_luks
,
674 .bdrv_get_specific_info
= block_crypto_get_specific_info_luks
,
676 .strong_runtime_opts
= block_crypto_strong_runtime_opts
,
679 static void block_crypto_init(void)
681 bdrv_register(&bdrv_crypto_luks
);
684 block_init(block_crypto_init
);