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 "sysemu/block-backend.h"
25 #include "crypto/block.h"
26 #include "qapi/opts-visitor.h"
27 #include "qapi/qmp/qdict.h"
28 #include "qapi/qobject-input-visitor.h"
29 #include "qapi-visit.h"
30 #include "qapi/error.h"
31 #include "qemu/option.h"
32 #include "block/crypto.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
{
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
;
108 /* User provided size should reflect amount of space made
109 * available to the guest, so we must take account of that
110 * which will be used by the crypto header
112 data
->size
+= headerlen
;
114 qemu_opt_set_number(data
->opts
, BLOCK_OPT_SIZE
, data
->size
, &error_abort
);
115 ret
= bdrv_create_file(data
->filename
, data
->opts
, errp
);
120 data
->blk
= blk_new_open(data
->filename
, NULL
, NULL
,
121 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, errp
);
130 static QemuOptsList block_crypto_runtime_opts_luks
= {
132 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_runtime_opts_luks
.head
),
134 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
135 { /* end of list */ }
140 static QemuOptsList block_crypto_create_opts_luks
= {
142 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_create_opts_luks
.head
),
145 .name
= BLOCK_OPT_SIZE
,
146 .type
= QEMU_OPT_SIZE
,
147 .help
= "Virtual disk size"
149 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
150 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG(""),
151 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE(""),
152 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG(""),
153 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG(""),
154 BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG(""),
155 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME(""),
156 { /* end of list */ }
161 QCryptoBlockOpenOptions
*
162 block_crypto_open_opts_init(QCryptoBlockFormat format
,
167 QCryptoBlockOpenOptions
*ret
= NULL
;
168 Error
*local_err
= NULL
;
170 ret
= g_new0(QCryptoBlockOpenOptions
, 1);
171 ret
->format
= format
;
173 v
= qobject_input_visitor_new_keyval(QOBJECT(opts
));
175 visit_start_struct(v
, NULL
, NULL
, 0, &local_err
);
181 case Q_CRYPTO_BLOCK_FORMAT_LUKS
:
182 visit_type_QCryptoBlockOptionsLUKS_members(
183 v
, &ret
->u
.luks
, &local_err
);
186 case Q_CRYPTO_BLOCK_FORMAT_QCOW
:
187 visit_type_QCryptoBlockOptionsQCow_members(
188 v
, &ret
->u
.qcow
, &local_err
);
192 error_setg(&local_err
, "Unsupported block format %d", format
);
196 visit_check_struct(v
, &local_err
);
199 visit_end_struct(v
, NULL
);
203 error_propagate(errp
, local_err
);
204 qapi_free_QCryptoBlockOpenOptions(ret
);
212 QCryptoBlockCreateOptions
*
213 block_crypto_create_opts_init(QCryptoBlockFormat format
,
218 QCryptoBlockCreateOptions
*ret
= NULL
;
219 Error
*local_err
= NULL
;
221 ret
= g_new0(QCryptoBlockCreateOptions
, 1);
222 ret
->format
= format
;
224 v
= qobject_input_visitor_new_keyval(QOBJECT(opts
));
226 visit_start_struct(v
, NULL
, NULL
, 0, &local_err
);
232 case Q_CRYPTO_BLOCK_FORMAT_LUKS
:
233 visit_type_QCryptoBlockCreateOptionsLUKS_members(
234 v
, &ret
->u
.luks
, &local_err
);
237 case Q_CRYPTO_BLOCK_FORMAT_QCOW
:
238 visit_type_QCryptoBlockOptionsQCow_members(
239 v
, &ret
->u
.qcow
, &local_err
);
243 error_setg(&local_err
, "Unsupported block format %d", format
);
247 visit_check_struct(v
, &local_err
);
250 visit_end_struct(v
, NULL
);
254 error_propagate(errp
, local_err
);
255 qapi_free_QCryptoBlockCreateOptions(ret
);
263 static int block_crypto_open_generic(QCryptoBlockFormat format
,
264 QemuOptsList
*opts_spec
,
265 BlockDriverState
*bs
,
270 BlockCrypto
*crypto
= bs
->opaque
;
271 QemuOpts
*opts
= NULL
;
272 Error
*local_err
= NULL
;
274 QCryptoBlockOpenOptions
*open_opts
= NULL
;
275 unsigned int cflags
= 0;
276 QDict
*cryptoopts
= NULL
;
278 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
284 bs
->supported_write_flags
= BDRV_REQ_FUA
&
285 bs
->file
->bs
->supported_write_flags
;
287 opts
= qemu_opts_create(opts_spec
, NULL
, 0, &error_abort
);
288 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
290 error_propagate(errp
, local_err
);
294 cryptoopts
= qemu_opts_to_qdict(opts
, NULL
);
296 open_opts
= block_crypto_open_opts_init(format
, cryptoopts
, errp
);
301 if (flags
& BDRV_O_NO_IO
) {
302 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
304 crypto
->block
= qcrypto_block_open(open_opts
, NULL
,
305 block_crypto_read_func
,
310 if (!crypto
->block
) {
315 bs
->encrypted
= true;
320 qapi_free_QCryptoBlockOpenOptions(open_opts
);
325 static int block_crypto_create_generic(QCryptoBlockFormat format
,
326 const char *filename
,
331 QCryptoBlockCreateOptions
*create_opts
= NULL
;
332 QCryptoBlock
*crypto
= NULL
;
333 struct BlockCryptoCreateData data
= {
334 .size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
337 .filename
= filename
,
341 cryptoopts
= qemu_opts_to_qdict(opts
, NULL
);
343 create_opts
= block_crypto_create_opts_init(format
, cryptoopts
, errp
);
348 crypto
= qcrypto_block_create(create_opts
, NULL
,
349 block_crypto_init_func
,
350 block_crypto_write_func
,
362 qcrypto_block_free(crypto
);
364 qapi_free_QCryptoBlockCreateOptions(create_opts
);
368 static int block_crypto_truncate(BlockDriverState
*bs
, int64_t offset
,
369 PreallocMode prealloc
, Error
**errp
)
371 BlockCrypto
*crypto
= bs
->opaque
;
372 uint64_t payload_offset
=
373 qcrypto_block_get_payload_offset(crypto
->block
);
374 assert(payload_offset
< (INT64_MAX
- offset
));
376 offset
+= payload_offset
;
378 return bdrv_truncate(bs
->file
, offset
, prealloc
, errp
);
381 static void block_crypto_close(BlockDriverState
*bs
)
383 BlockCrypto
*crypto
= bs
->opaque
;
384 qcrypto_block_free(crypto
->block
);
389 * 1 MB bounce buffer gives good performance / memory tradeoff
390 * when using cache=none|directsync.
392 #define BLOCK_CRYPTO_MAX_IO_SIZE (1024 * 1024)
394 static coroutine_fn
int
395 block_crypto_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
396 QEMUIOVector
*qiov
, int flags
)
398 BlockCrypto
*crypto
= bs
->opaque
;
399 uint64_t cur_bytes
; /* number of bytes in current iteration */
400 uint64_t bytes_done
= 0;
401 uint8_t *cipher_data
= NULL
;
402 QEMUIOVector hd_qiov
;
404 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
405 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
408 assert(payload_offset
< INT64_MAX
);
409 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
410 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
412 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
414 /* Bounce buffer because we don't wish to expose cipher text
415 * in qiov which points to guest memory.
418 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
420 if (cipher_data
== NULL
) {
426 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
428 qemu_iovec_reset(&hd_qiov
);
429 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
431 ret
= bdrv_co_preadv(bs
->file
, payload_offset
+ offset
+ bytes_done
,
432 cur_bytes
, &hd_qiov
, 0);
437 if (qcrypto_block_decrypt(crypto
->block
, offset
+ bytes_done
,
438 cipher_data
, cur_bytes
, NULL
) < 0) {
443 qemu_iovec_from_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
446 bytes_done
+= cur_bytes
;
450 qemu_iovec_destroy(&hd_qiov
);
451 qemu_vfree(cipher_data
);
457 static coroutine_fn
int
458 block_crypto_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
459 QEMUIOVector
*qiov
, int flags
)
461 BlockCrypto
*crypto
= bs
->opaque
;
462 uint64_t cur_bytes
; /* number of bytes in current iteration */
463 uint64_t bytes_done
= 0;
464 uint8_t *cipher_data
= NULL
;
465 QEMUIOVector hd_qiov
;
467 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
468 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
470 assert(!(flags
& ~BDRV_REQ_FUA
));
471 assert(payload_offset
< INT64_MAX
);
472 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
473 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
475 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
477 /* Bounce buffer because we're not permitted to touch
478 * contents of qiov - it points to guest memory.
481 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
483 if (cipher_data
== NULL
) {
489 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
491 qemu_iovec_to_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
493 if (qcrypto_block_encrypt(crypto
->block
, offset
+ bytes_done
,
494 cipher_data
, cur_bytes
, NULL
) < 0) {
499 qemu_iovec_reset(&hd_qiov
);
500 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
502 ret
= bdrv_co_pwritev(bs
->file
, payload_offset
+ offset
+ bytes_done
,
503 cur_bytes
, &hd_qiov
, flags
);
509 bytes_done
+= cur_bytes
;
513 qemu_iovec_destroy(&hd_qiov
);
514 qemu_vfree(cipher_data
);
519 static void block_crypto_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
521 BlockCrypto
*crypto
= bs
->opaque
;
522 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
523 bs
->bl
.request_alignment
= sector_size
; /* No sub-sector I/O */
527 static int64_t block_crypto_getlength(BlockDriverState
*bs
)
529 BlockCrypto
*crypto
= bs
->opaque
;
530 int64_t len
= bdrv_getlength(bs
->file
->bs
);
532 uint64_t offset
= qcrypto_block_get_payload_offset(crypto
->block
);
533 assert(offset
< INT64_MAX
);
534 assert(offset
< len
);
542 static int block_crypto_probe_luks(const uint8_t *buf
,
544 const char *filename
) {
545 return block_crypto_probe_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
546 buf
, buf_size
, filename
);
549 static int block_crypto_open_luks(BlockDriverState
*bs
,
554 return block_crypto_open_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
555 &block_crypto_runtime_opts_luks
,
556 bs
, options
, flags
, errp
);
559 static int block_crypto_create_luks(const char *filename
,
563 return block_crypto_create_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
564 filename
, opts
, errp
);
567 static int block_crypto_get_info_luks(BlockDriverState
*bs
,
568 BlockDriverInfo
*bdi
)
570 BlockDriverInfo subbdi
;
573 ret
= bdrv_get_info(bs
->file
->bs
, &subbdi
);
578 bdi
->unallocated_blocks_are_zero
= false;
579 bdi
->cluster_size
= subbdi
.cluster_size
;
584 static ImageInfoSpecific
*
585 block_crypto_get_specific_info_luks(BlockDriverState
*bs
)
587 BlockCrypto
*crypto
= bs
->opaque
;
588 ImageInfoSpecific
*spec_info
;
589 QCryptoBlockInfo
*info
;
591 info
= qcrypto_block_get_info(crypto
->block
, NULL
);
595 if (info
->format
!= Q_CRYPTO_BLOCK_FORMAT_LUKS
) {
596 qapi_free_QCryptoBlockInfo(info
);
600 spec_info
= g_new(ImageInfoSpecific
, 1);
601 spec_info
->type
= IMAGE_INFO_SPECIFIC_KIND_LUKS
;
602 spec_info
->u
.luks
.data
= g_new(QCryptoBlockInfoLUKS
, 1);
603 *spec_info
->u
.luks
.data
= info
->u
.luks
;
605 /* Blank out pointers we've just stolen to avoid double free */
606 memset(&info
->u
.luks
, 0, sizeof(info
->u
.luks
));
608 qapi_free_QCryptoBlockInfo(info
);
613 BlockDriver bdrv_crypto_luks
= {
614 .format_name
= "luks",
615 .instance_size
= sizeof(BlockCrypto
),
616 .bdrv_probe
= block_crypto_probe_luks
,
617 .bdrv_open
= block_crypto_open_luks
,
618 .bdrv_close
= block_crypto_close
,
619 .bdrv_child_perm
= bdrv_format_default_perms
,
620 .bdrv_create
= block_crypto_create_luks
,
621 .bdrv_truncate
= block_crypto_truncate
,
622 .create_opts
= &block_crypto_create_opts_luks
,
624 .bdrv_refresh_limits
= block_crypto_refresh_limits
,
625 .bdrv_co_preadv
= block_crypto_co_preadv
,
626 .bdrv_co_pwritev
= block_crypto_co_pwritev
,
627 .bdrv_getlength
= block_crypto_getlength
,
628 .bdrv_get_info
= block_crypto_get_info_luks
,
629 .bdrv_get_specific_info
= block_crypto_get_specific_info_luks
,
632 static void block_crypto_init(void)
634 bdrv_register(&bdrv_crypto_luks
);
637 block_init(block_crypto_init
);