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
{
80 static ssize_t
block_crypto_write_func(QCryptoBlock
*block
,
87 struct BlockCryptoCreateData
*data
= opaque
;
90 ret
= blk_pwrite(data
->blk
, offset
, buf
, buflen
, 0);
92 error_setg_errno(errp
, -ret
, "Could not write encryption header");
99 static ssize_t
block_crypto_init_func(QCryptoBlock
*block
,
104 struct BlockCryptoCreateData
*data
= opaque
;
106 if (data
->size
> INT64_MAX
|| headerlen
> INT64_MAX
- data
->size
) {
107 error_setg(errp
, "The requested file size is too large");
111 /* User provided size should reflect amount of space made
112 * available to the guest, so we must take account of that
113 * which will be used by the crypto header
115 return blk_truncate(data
->blk
, data
->size
+ headerlen
, PREALLOC_MODE_OFF
,
120 static QemuOptsList block_crypto_runtime_opts_luks
= {
122 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_runtime_opts_luks
.head
),
124 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
125 { /* end of list */ }
130 static QemuOptsList block_crypto_create_opts_luks
= {
132 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_create_opts_luks
.head
),
135 .name
= BLOCK_OPT_SIZE
,
136 .type
= QEMU_OPT_SIZE
,
137 .help
= "Virtual disk size"
139 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
140 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG(""),
141 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE(""),
142 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG(""),
143 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG(""),
144 BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG(""),
145 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME(""),
146 { /* end of list */ }
151 QCryptoBlockOpenOptions
*
152 block_crypto_open_opts_init(QDict
*opts
, Error
**errp
)
155 QCryptoBlockOpenOptions
*ret
;
157 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
162 visit_type_QCryptoBlockOpenOptions(v
, NULL
, &ret
, errp
);
169 QCryptoBlockCreateOptions
*
170 block_crypto_create_opts_init(QDict
*opts
, Error
**errp
)
173 QCryptoBlockCreateOptions
*ret
;
175 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
180 visit_type_QCryptoBlockCreateOptions(v
, NULL
, &ret
, errp
);
187 static int block_crypto_open_generic(QCryptoBlockFormat format
,
188 QemuOptsList
*opts_spec
,
189 BlockDriverState
*bs
,
194 BlockCrypto
*crypto
= bs
->opaque
;
195 QemuOpts
*opts
= NULL
;
196 Error
*local_err
= NULL
;
198 QCryptoBlockOpenOptions
*open_opts
= NULL
;
199 unsigned int cflags
= 0;
200 QDict
*cryptoopts
= NULL
;
202 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
208 bs
->supported_write_flags
= BDRV_REQ_FUA
&
209 bs
->file
->bs
->supported_write_flags
;
211 opts
= qemu_opts_create(opts_spec
, NULL
, 0, &error_abort
);
212 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
214 error_propagate(errp
, local_err
);
218 cryptoopts
= qemu_opts_to_qdict(opts
, NULL
);
219 qdict_put_str(cryptoopts
, "format", QCryptoBlockFormat_str(format
));
221 open_opts
= block_crypto_open_opts_init(cryptoopts
, errp
);
226 if (flags
& BDRV_O_NO_IO
) {
227 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
229 crypto
->block
= qcrypto_block_open(open_opts
, NULL
,
230 block_crypto_read_func
,
236 if (!crypto
->block
) {
241 bs
->encrypted
= true;
245 qobject_unref(cryptoopts
);
246 qapi_free_QCryptoBlockOpenOptions(open_opts
);
251 static int block_crypto_co_create_generic(BlockDriverState
*bs
,
253 QCryptoBlockCreateOptions
*opts
,
258 QCryptoBlock
*crypto
= NULL
;
259 struct BlockCryptoCreateData data
;
261 blk
= blk_new(bdrv_get_aio_context(bs
),
262 BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
);
264 ret
= blk_insert_bs(blk
, bs
, errp
);
269 data
= (struct BlockCryptoCreateData
) {
274 crypto
= qcrypto_block_create(opts
, NULL
,
275 block_crypto_init_func
,
276 block_crypto_write_func
,
287 qcrypto_block_free(crypto
);
292 static int coroutine_fn
293 block_crypto_co_truncate(BlockDriverState
*bs
, int64_t offset
,
294 PreallocMode prealloc
, Error
**errp
)
296 BlockCrypto
*crypto
= bs
->opaque
;
297 uint64_t payload_offset
=
298 qcrypto_block_get_payload_offset(crypto
->block
);
300 if (payload_offset
> INT64_MAX
- offset
) {
301 error_setg(errp
, "The requested file size is too large");
305 offset
+= payload_offset
;
307 return bdrv_co_truncate(bs
->file
, offset
, prealloc
, errp
);
310 static void block_crypto_close(BlockDriverState
*bs
)
312 BlockCrypto
*crypto
= bs
->opaque
;
313 qcrypto_block_free(crypto
->block
);
316 static int block_crypto_reopen_prepare(BDRVReopenState
*state
,
317 BlockReopenQueue
*queue
, Error
**errp
)
319 /* nothing needs checking */
324 * 1 MB bounce buffer gives good performance / memory tradeoff
325 * when using cache=none|directsync.
327 #define BLOCK_CRYPTO_MAX_IO_SIZE (1024 * 1024)
329 static coroutine_fn
int
330 block_crypto_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
331 QEMUIOVector
*qiov
, int flags
)
333 BlockCrypto
*crypto
= bs
->opaque
;
334 uint64_t cur_bytes
; /* number of bytes in current iteration */
335 uint64_t bytes_done
= 0;
336 uint8_t *cipher_data
= NULL
;
337 QEMUIOVector hd_qiov
;
339 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
340 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
343 assert(payload_offset
< INT64_MAX
);
344 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
345 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
347 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
349 /* Bounce buffer because we don't wish to expose cipher text
350 * in qiov which points to guest memory.
353 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
355 if (cipher_data
== NULL
) {
361 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
363 qemu_iovec_reset(&hd_qiov
);
364 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
366 ret
= bdrv_co_preadv(bs
->file
, payload_offset
+ offset
+ bytes_done
,
367 cur_bytes
, &hd_qiov
, 0);
372 if (qcrypto_block_decrypt(crypto
->block
, offset
+ bytes_done
,
373 cipher_data
, cur_bytes
, NULL
) < 0) {
378 qemu_iovec_from_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
381 bytes_done
+= cur_bytes
;
385 qemu_iovec_destroy(&hd_qiov
);
386 qemu_vfree(cipher_data
);
392 static coroutine_fn
int
393 block_crypto_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
394 QEMUIOVector
*qiov
, int flags
)
396 BlockCrypto
*crypto
= bs
->opaque
;
397 uint64_t cur_bytes
; /* number of bytes in current iteration */
398 uint64_t bytes_done
= 0;
399 uint8_t *cipher_data
= NULL
;
400 QEMUIOVector hd_qiov
;
402 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
403 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
405 assert(!(flags
& ~BDRV_REQ_FUA
));
406 assert(payload_offset
< INT64_MAX
);
407 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
408 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
410 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
412 /* Bounce buffer because we're not permitted to touch
413 * contents of qiov - it points to guest memory.
416 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
418 if (cipher_data
== NULL
) {
424 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
426 qemu_iovec_to_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
428 if (qcrypto_block_encrypt(crypto
->block
, offset
+ bytes_done
,
429 cipher_data
, cur_bytes
, NULL
) < 0) {
434 qemu_iovec_reset(&hd_qiov
);
435 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
437 ret
= bdrv_co_pwritev(bs
->file
, payload_offset
+ offset
+ bytes_done
,
438 cur_bytes
, &hd_qiov
, flags
);
444 bytes_done
+= cur_bytes
;
448 qemu_iovec_destroy(&hd_qiov
);
449 qemu_vfree(cipher_data
);
454 static void block_crypto_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
456 BlockCrypto
*crypto
= bs
->opaque
;
457 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
458 bs
->bl
.request_alignment
= sector_size
; /* No sub-sector I/O */
462 static int64_t block_crypto_getlength(BlockDriverState
*bs
)
464 BlockCrypto
*crypto
= bs
->opaque
;
465 int64_t len
= bdrv_getlength(bs
->file
->bs
);
467 uint64_t offset
= qcrypto_block_get_payload_offset(crypto
->block
);
468 assert(offset
< INT64_MAX
);
480 static int block_crypto_probe_luks(const uint8_t *buf
,
482 const char *filename
) {
483 return block_crypto_probe_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
484 buf
, buf_size
, filename
);
487 static int block_crypto_open_luks(BlockDriverState
*bs
,
492 return block_crypto_open_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
493 &block_crypto_runtime_opts_luks
,
494 bs
, options
, flags
, errp
);
497 static int coroutine_fn
498 block_crypto_co_create_luks(BlockdevCreateOptions
*create_options
, Error
**errp
)
500 BlockdevCreateOptionsLUKS
*luks_opts
;
501 BlockDriverState
*bs
= NULL
;
502 QCryptoBlockCreateOptions create_opts
;
505 assert(create_options
->driver
== BLOCKDEV_DRIVER_LUKS
);
506 luks_opts
= &create_options
->u
.luks
;
508 bs
= bdrv_open_blockdev_ref(luks_opts
->file
, errp
);
513 create_opts
= (QCryptoBlockCreateOptions
) {
514 .format
= Q_CRYPTO_BLOCK_FORMAT_LUKS
,
515 .u
.luks
= *qapi_BlockdevCreateOptionsLUKS_base(luks_opts
),
518 ret
= block_crypto_co_create_generic(bs
, luks_opts
->size
, &create_opts
,
530 static int coroutine_fn
block_crypto_co_create_opts_luks(const char *filename
,
534 QCryptoBlockCreateOptions
*create_opts
= NULL
;
535 BlockDriverState
*bs
= NULL
;
541 size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
543 cryptoopts
= qemu_opts_to_qdict_filtered(opts
, NULL
,
544 &block_crypto_create_opts_luks
,
547 qdict_put_str(cryptoopts
, "format", "luks");
548 create_opts
= block_crypto_create_opts_init(cryptoopts
, errp
);
554 /* Create protocol layer */
555 ret
= bdrv_create_file(filename
, opts
, errp
);
560 bs
= bdrv_open(filename
, NULL
, NULL
,
561 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
567 /* Create format layer */
568 ret
= block_crypto_co_create_generic(bs
, size
, create_opts
, errp
);
576 qapi_free_QCryptoBlockCreateOptions(create_opts
);
577 qobject_unref(cryptoopts
);
581 static int block_crypto_get_info_luks(BlockDriverState
*bs
,
582 BlockDriverInfo
*bdi
)
584 BlockDriverInfo subbdi
;
587 ret
= bdrv_get_info(bs
->file
->bs
, &subbdi
);
592 bdi
->unallocated_blocks_are_zero
= false;
593 bdi
->cluster_size
= subbdi
.cluster_size
;
598 static ImageInfoSpecific
*
599 block_crypto_get_specific_info_luks(BlockDriverState
*bs
, Error
**errp
)
601 BlockCrypto
*crypto
= bs
->opaque
;
602 ImageInfoSpecific
*spec_info
;
603 QCryptoBlockInfo
*info
;
605 info
= qcrypto_block_get_info(crypto
->block
, errp
);
609 assert(info
->format
== Q_CRYPTO_BLOCK_FORMAT_LUKS
);
611 spec_info
= g_new(ImageInfoSpecific
, 1);
612 spec_info
->type
= IMAGE_INFO_SPECIFIC_KIND_LUKS
;
613 spec_info
->u
.luks
.data
= g_new(QCryptoBlockInfoLUKS
, 1);
614 *spec_info
->u
.luks
.data
= info
->u
.luks
;
616 /* Blank out pointers we've just stolen to avoid double free */
617 memset(&info
->u
.luks
, 0, sizeof(info
->u
.luks
));
619 qapi_free_QCryptoBlockInfo(info
);
624 static const char *const block_crypto_strong_runtime_opts
[] = {
625 BLOCK_CRYPTO_OPT_LUKS_KEY_SECRET
,
630 static BlockDriver bdrv_crypto_luks
= {
631 .format_name
= "luks",
632 .instance_size
= sizeof(BlockCrypto
),
633 .bdrv_probe
= block_crypto_probe_luks
,
634 .bdrv_open
= block_crypto_open_luks
,
635 .bdrv_close
= block_crypto_close
,
636 /* This driver doesn't modify LUKS metadata except when creating image.
637 * Allow share-rw=on as a special case. */
638 .bdrv_child_perm
= bdrv_filter_default_perms
,
639 .bdrv_co_create
= block_crypto_co_create_luks
,
640 .bdrv_co_create_opts
= block_crypto_co_create_opts_luks
,
641 .bdrv_co_truncate
= block_crypto_co_truncate
,
642 .create_opts
= &block_crypto_create_opts_luks
,
644 .bdrv_reopen_prepare
= block_crypto_reopen_prepare
,
645 .bdrv_refresh_limits
= block_crypto_refresh_limits
,
646 .bdrv_co_preadv
= block_crypto_co_preadv
,
647 .bdrv_co_pwritev
= block_crypto_co_pwritev
,
648 .bdrv_getlength
= block_crypto_getlength
,
649 .bdrv_get_info
= block_crypto_get_info_luks
,
650 .bdrv_get_specific_info
= block_crypto_get_specific_info_luks
,
652 .strong_runtime_opts
= block_crypto_strong_runtime_opts
,
655 static void block_crypto_init(void)
657 bdrv_register(&bdrv_crypto_luks
);
660 block_init(block_crypto_init
);