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(QDict
*opts
, Error
**errp
)
154 QCryptoBlockOpenOptions
*ret
;
156 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
161 visit_type_QCryptoBlockOpenOptions(v
, NULL
, &ret
, errp
);
168 QCryptoBlockCreateOptions
*
169 block_crypto_create_opts_init(QDict
*opts
, Error
**errp
)
172 QCryptoBlockCreateOptions
*ret
;
174 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
179 visit_type_QCryptoBlockCreateOptions(v
, NULL
, &ret
, errp
);
186 static int block_crypto_open_generic(QCryptoBlockFormat format
,
187 QemuOptsList
*opts_spec
,
188 BlockDriverState
*bs
,
193 BlockCrypto
*crypto
= bs
->opaque
;
194 QemuOpts
*opts
= NULL
;
195 Error
*local_err
= NULL
;
197 QCryptoBlockOpenOptions
*open_opts
= NULL
;
198 unsigned int cflags
= 0;
199 QDict
*cryptoopts
= NULL
;
201 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
207 bs
->supported_write_flags
= BDRV_REQ_FUA
&
208 bs
->file
->bs
->supported_write_flags
;
210 opts
= qemu_opts_create(opts_spec
, NULL
, 0, &error_abort
);
211 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
213 error_propagate(errp
, local_err
);
217 cryptoopts
= qemu_opts_to_qdict(opts
, NULL
);
218 qdict_put_str(cryptoopts
, "format", QCryptoBlockFormat_str(format
));
220 open_opts
= block_crypto_open_opts_init(cryptoopts
, errp
);
225 if (flags
& BDRV_O_NO_IO
) {
226 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
228 crypto
->block
= qcrypto_block_open(open_opts
, NULL
,
229 block_crypto_read_func
,
235 if (!crypto
->block
) {
240 bs
->encrypted
= true;
244 qobject_unref(cryptoopts
);
245 qapi_free_QCryptoBlockOpenOptions(open_opts
);
250 static int block_crypto_co_create_generic(BlockDriverState
*bs
,
252 QCryptoBlockCreateOptions
*opts
,
257 QCryptoBlock
*crypto
= NULL
;
258 struct BlockCryptoCreateData data
;
260 blk
= blk_new(BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
);
262 ret
= blk_insert_bs(blk
, bs
, errp
);
267 data
= (struct BlockCryptoCreateData
) {
272 crypto
= qcrypto_block_create(opts
, NULL
,
273 block_crypto_init_func
,
274 block_crypto_write_func
,
285 qcrypto_block_free(crypto
);
290 static int coroutine_fn
291 block_crypto_co_truncate(BlockDriverState
*bs
, int64_t offset
,
292 PreallocMode prealloc
, Error
**errp
)
294 BlockCrypto
*crypto
= bs
->opaque
;
295 uint64_t payload_offset
=
296 qcrypto_block_get_payload_offset(crypto
->block
);
298 if (payload_offset
> INT64_MAX
- offset
) {
299 error_setg(errp
, "The requested file size is too large");
303 offset
+= payload_offset
;
305 return bdrv_co_truncate(bs
->file
, offset
, prealloc
, errp
);
308 static void block_crypto_close(BlockDriverState
*bs
)
310 BlockCrypto
*crypto
= bs
->opaque
;
311 qcrypto_block_free(crypto
->block
);
314 static int block_crypto_reopen_prepare(BDRVReopenState
*state
,
315 BlockReopenQueue
*queue
, Error
**errp
)
317 /* nothing needs checking */
322 * 1 MB bounce buffer gives good performance / memory tradeoff
323 * when using cache=none|directsync.
325 #define BLOCK_CRYPTO_MAX_IO_SIZE (1024 * 1024)
327 static coroutine_fn
int
328 block_crypto_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
329 QEMUIOVector
*qiov
, int flags
)
331 BlockCrypto
*crypto
= bs
->opaque
;
332 uint64_t cur_bytes
; /* number of bytes in current iteration */
333 uint64_t bytes_done
= 0;
334 uint8_t *cipher_data
= NULL
;
335 QEMUIOVector hd_qiov
;
337 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
338 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
341 assert(payload_offset
< INT64_MAX
);
342 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
343 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
345 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
347 /* Bounce buffer because we don't wish to expose cipher text
348 * in qiov which points to guest memory.
351 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
353 if (cipher_data
== NULL
) {
359 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
361 qemu_iovec_reset(&hd_qiov
);
362 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
364 ret
= bdrv_co_preadv(bs
->file
, payload_offset
+ offset
+ bytes_done
,
365 cur_bytes
, &hd_qiov
, 0);
370 if (qcrypto_block_decrypt(crypto
->block
, offset
+ bytes_done
,
371 cipher_data
, cur_bytes
, NULL
) < 0) {
376 qemu_iovec_from_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
379 bytes_done
+= cur_bytes
;
383 qemu_iovec_destroy(&hd_qiov
);
384 qemu_vfree(cipher_data
);
390 static coroutine_fn
int
391 block_crypto_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
392 QEMUIOVector
*qiov
, int flags
)
394 BlockCrypto
*crypto
= bs
->opaque
;
395 uint64_t cur_bytes
; /* number of bytes in current iteration */
396 uint64_t bytes_done
= 0;
397 uint8_t *cipher_data
= NULL
;
398 QEMUIOVector hd_qiov
;
400 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
401 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
403 assert(!(flags
& ~BDRV_REQ_FUA
));
404 assert(payload_offset
< INT64_MAX
);
405 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
406 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
408 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
410 /* Bounce buffer because we're not permitted to touch
411 * contents of qiov - it points to guest memory.
414 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
416 if (cipher_data
== NULL
) {
422 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
424 qemu_iovec_to_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
426 if (qcrypto_block_encrypt(crypto
->block
, offset
+ bytes_done
,
427 cipher_data
, cur_bytes
, NULL
) < 0) {
432 qemu_iovec_reset(&hd_qiov
);
433 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
435 ret
= bdrv_co_pwritev(bs
->file
, payload_offset
+ offset
+ bytes_done
,
436 cur_bytes
, &hd_qiov
, flags
);
442 bytes_done
+= cur_bytes
;
446 qemu_iovec_destroy(&hd_qiov
);
447 qemu_vfree(cipher_data
);
452 static void block_crypto_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
454 BlockCrypto
*crypto
= bs
->opaque
;
455 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
456 bs
->bl
.request_alignment
= sector_size
; /* No sub-sector I/O */
460 static int64_t block_crypto_getlength(BlockDriverState
*bs
)
462 BlockCrypto
*crypto
= bs
->opaque
;
463 int64_t len
= bdrv_getlength(bs
->file
->bs
);
465 uint64_t offset
= qcrypto_block_get_payload_offset(crypto
->block
);
466 assert(offset
< INT64_MAX
);
478 static int block_crypto_probe_luks(const uint8_t *buf
,
480 const char *filename
) {
481 return block_crypto_probe_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
482 buf
, buf_size
, filename
);
485 static int block_crypto_open_luks(BlockDriverState
*bs
,
490 return block_crypto_open_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
491 &block_crypto_runtime_opts_luks
,
492 bs
, options
, flags
, errp
);
495 static int coroutine_fn
496 block_crypto_co_create_luks(BlockdevCreateOptions
*create_options
, Error
**errp
)
498 BlockdevCreateOptionsLUKS
*luks_opts
;
499 BlockDriverState
*bs
= NULL
;
500 QCryptoBlockCreateOptions create_opts
;
503 assert(create_options
->driver
== BLOCKDEV_DRIVER_LUKS
);
504 luks_opts
= &create_options
->u
.luks
;
506 bs
= bdrv_open_blockdev_ref(luks_opts
->file
, errp
);
511 create_opts
= (QCryptoBlockCreateOptions
) {
512 .format
= Q_CRYPTO_BLOCK_FORMAT_LUKS
,
513 .u
.luks
= *qapi_BlockdevCreateOptionsLUKS_base(luks_opts
),
516 ret
= block_crypto_co_create_generic(bs
, luks_opts
->size
, &create_opts
,
528 static int coroutine_fn
block_crypto_co_create_opts_luks(const char *filename
,
532 QCryptoBlockCreateOptions
*create_opts
= NULL
;
533 BlockDriverState
*bs
= NULL
;
539 size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
541 cryptoopts
= qemu_opts_to_qdict_filtered(opts
, NULL
,
542 &block_crypto_create_opts_luks
,
545 qdict_put_str(cryptoopts
, "format", "luks");
546 create_opts
= block_crypto_create_opts_init(cryptoopts
, errp
);
552 /* Create protocol layer */
553 ret
= bdrv_create_file(filename
, opts
, errp
);
558 bs
= bdrv_open(filename
, NULL
, NULL
,
559 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
565 /* Create format layer */
566 ret
= block_crypto_co_create_generic(bs
, size
, create_opts
, errp
);
574 qapi_free_QCryptoBlockCreateOptions(create_opts
);
575 qobject_unref(cryptoopts
);
579 static int block_crypto_get_info_luks(BlockDriverState
*bs
,
580 BlockDriverInfo
*bdi
)
582 BlockDriverInfo subbdi
;
585 ret
= bdrv_get_info(bs
->file
->bs
, &subbdi
);
590 bdi
->unallocated_blocks_are_zero
= false;
591 bdi
->cluster_size
= subbdi
.cluster_size
;
596 static ImageInfoSpecific
*
597 block_crypto_get_specific_info_luks(BlockDriverState
*bs
)
599 BlockCrypto
*crypto
= bs
->opaque
;
600 ImageInfoSpecific
*spec_info
;
601 QCryptoBlockInfo
*info
;
603 info
= qcrypto_block_get_info(crypto
->block
, NULL
);
607 if (info
->format
!= Q_CRYPTO_BLOCK_FORMAT_LUKS
) {
608 qapi_free_QCryptoBlockInfo(info
);
612 spec_info
= g_new(ImageInfoSpecific
, 1);
613 spec_info
->type
= IMAGE_INFO_SPECIFIC_KIND_LUKS
;
614 spec_info
->u
.luks
.data
= g_new(QCryptoBlockInfoLUKS
, 1);
615 *spec_info
->u
.luks
.data
= info
->u
.luks
;
617 /* Blank out pointers we've just stolen to avoid double free */
618 memset(&info
->u
.luks
, 0, sizeof(info
->u
.luks
));
620 qapi_free_QCryptoBlockInfo(info
);
625 BlockDriver bdrv_crypto_luks
= {
626 .format_name
= "luks",
627 .instance_size
= sizeof(BlockCrypto
),
628 .bdrv_probe
= block_crypto_probe_luks
,
629 .bdrv_open
= block_crypto_open_luks
,
630 .bdrv_close
= block_crypto_close
,
631 /* This driver doesn't modify LUKS metadata except when creating image.
632 * Allow share-rw=on as a special case. */
633 .bdrv_child_perm
= bdrv_filter_default_perms
,
634 .bdrv_co_create
= block_crypto_co_create_luks
,
635 .bdrv_co_create_opts
= block_crypto_co_create_opts_luks
,
636 .bdrv_co_truncate
= block_crypto_co_truncate
,
637 .create_opts
= &block_crypto_create_opts_luks
,
639 .bdrv_reopen_prepare
= block_crypto_reopen_prepare
,
640 .bdrv_refresh_limits
= block_crypto_refresh_limits
,
641 .bdrv_co_preadv
= block_crypto_co_preadv
,
642 .bdrv_co_pwritev
= block_crypto_co_pwritev
,
643 .bdrv_getlength
= block_crypto_getlength
,
644 .bdrv_get_info
= block_crypto_get_info_luks
,
645 .bdrv_get_specific_info
= block_crypto_get_specific_info_luks
,
648 static void block_crypto_init(void)
650 bdrv_register(&bdrv_crypto_luks
);
653 block_init(block_crypto_init
);