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/qapi-visit-crypto.h"
28 #include "qapi/qmp/qdict.h"
29 #include "qapi/qobject-input-visitor.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
{
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_keyval(QOBJECT(opts
));
164 visit_start_struct(v
, NULL
, NULL
, 0, &local_err
);
170 case Q_CRYPTO_BLOCK_FORMAT_LUKS
:
171 visit_type_QCryptoBlockOptionsLUKS_members(
172 v
, &ret
->u
.luks
, &local_err
);
175 case Q_CRYPTO_BLOCK_FORMAT_QCOW
:
176 visit_type_QCryptoBlockOptionsQCow_members(
177 v
, &ret
->u
.qcow
, &local_err
);
181 error_setg(&local_err
, "Unsupported block format %d", format
);
185 visit_check_struct(v
, &local_err
);
188 visit_end_struct(v
, NULL
);
192 error_propagate(errp
, local_err
);
193 qapi_free_QCryptoBlockOpenOptions(ret
);
201 QCryptoBlockCreateOptions
*
202 block_crypto_create_opts_init(QCryptoBlockFormat format
,
207 QCryptoBlockCreateOptions
*ret
= NULL
;
208 Error
*local_err
= NULL
;
210 ret
= g_new0(QCryptoBlockCreateOptions
, 1);
211 ret
->format
= format
;
213 v
= qobject_input_visitor_new_keyval(QOBJECT(opts
));
215 visit_start_struct(v
, NULL
, NULL
, 0, &local_err
);
221 case Q_CRYPTO_BLOCK_FORMAT_LUKS
:
222 visit_type_QCryptoBlockCreateOptionsLUKS_members(
223 v
, &ret
->u
.luks
, &local_err
);
226 case Q_CRYPTO_BLOCK_FORMAT_QCOW
:
227 visit_type_QCryptoBlockOptionsQCow_members(
228 v
, &ret
->u
.qcow
, &local_err
);
232 error_setg(&local_err
, "Unsupported block format %d", format
);
236 visit_check_struct(v
, &local_err
);
239 visit_end_struct(v
, NULL
);
243 error_propagate(errp
, local_err
);
244 qapi_free_QCryptoBlockCreateOptions(ret
);
252 static int block_crypto_open_generic(QCryptoBlockFormat format
,
253 QemuOptsList
*opts_spec
,
254 BlockDriverState
*bs
,
259 BlockCrypto
*crypto
= bs
->opaque
;
260 QemuOpts
*opts
= NULL
;
261 Error
*local_err
= NULL
;
263 QCryptoBlockOpenOptions
*open_opts
= NULL
;
264 unsigned int cflags
= 0;
265 QDict
*cryptoopts
= NULL
;
267 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
273 bs
->supported_write_flags
= BDRV_REQ_FUA
&
274 bs
->file
->bs
->supported_write_flags
;
276 opts
= qemu_opts_create(opts_spec
, NULL
, 0, &error_abort
);
277 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
279 error_propagate(errp
, local_err
);
283 cryptoopts
= qemu_opts_to_qdict(opts
, NULL
);
285 open_opts
= block_crypto_open_opts_init(format
, cryptoopts
, errp
);
290 if (flags
& BDRV_O_NO_IO
) {
291 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
293 crypto
->block
= qcrypto_block_open(open_opts
, NULL
,
294 block_crypto_read_func
,
299 if (!crypto
->block
) {
304 bs
->encrypted
= true;
309 qapi_free_QCryptoBlockOpenOptions(open_opts
);
314 static int block_crypto_co_create_generic(BlockDriverState
*bs
,
316 QCryptoBlockCreateOptions
*opts
,
321 QCryptoBlock
*crypto
= NULL
;
322 struct BlockCryptoCreateData data
;
324 blk
= blk_new(BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
);
326 ret
= blk_insert_bs(blk
, bs
, errp
);
331 data
= (struct BlockCryptoCreateData
) {
336 crypto
= qcrypto_block_create(opts
, NULL
,
337 block_crypto_init_func
,
338 block_crypto_write_func
,
349 qcrypto_block_free(crypto
);
354 static int block_crypto_truncate(BlockDriverState
*bs
, int64_t offset
,
355 PreallocMode prealloc
, Error
**errp
)
357 BlockCrypto
*crypto
= bs
->opaque
;
358 uint64_t payload_offset
=
359 qcrypto_block_get_payload_offset(crypto
->block
);
360 assert(payload_offset
< (INT64_MAX
- offset
));
362 offset
+= payload_offset
;
364 return bdrv_truncate(bs
->file
, offset
, prealloc
, errp
);
367 static void block_crypto_close(BlockDriverState
*bs
)
369 BlockCrypto
*crypto
= bs
->opaque
;
370 qcrypto_block_free(crypto
->block
);
373 static int block_crypto_reopen_prepare(BDRVReopenState
*state
,
374 BlockReopenQueue
*queue
, Error
**errp
)
376 /* nothing needs checking */
381 * 1 MB bounce buffer gives good performance / memory tradeoff
382 * when using cache=none|directsync.
384 #define BLOCK_CRYPTO_MAX_IO_SIZE (1024 * 1024)
386 static coroutine_fn
int
387 block_crypto_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
388 QEMUIOVector
*qiov
, int flags
)
390 BlockCrypto
*crypto
= bs
->opaque
;
391 uint64_t cur_bytes
; /* number of bytes in current iteration */
392 uint64_t bytes_done
= 0;
393 uint8_t *cipher_data
= NULL
;
394 QEMUIOVector hd_qiov
;
396 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
397 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
400 assert(payload_offset
< INT64_MAX
);
401 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
402 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
404 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
406 /* Bounce buffer because we don't wish to expose cipher text
407 * in qiov which points to guest memory.
410 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
412 if (cipher_data
== NULL
) {
418 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
420 qemu_iovec_reset(&hd_qiov
);
421 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
423 ret
= bdrv_co_preadv(bs
->file
, payload_offset
+ offset
+ bytes_done
,
424 cur_bytes
, &hd_qiov
, 0);
429 if (qcrypto_block_decrypt(crypto
->block
, offset
+ bytes_done
,
430 cipher_data
, cur_bytes
, NULL
) < 0) {
435 qemu_iovec_from_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
438 bytes_done
+= cur_bytes
;
442 qemu_iovec_destroy(&hd_qiov
);
443 qemu_vfree(cipher_data
);
449 static coroutine_fn
int
450 block_crypto_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
451 QEMUIOVector
*qiov
, int flags
)
453 BlockCrypto
*crypto
= bs
->opaque
;
454 uint64_t cur_bytes
; /* number of bytes in current iteration */
455 uint64_t bytes_done
= 0;
456 uint8_t *cipher_data
= NULL
;
457 QEMUIOVector hd_qiov
;
459 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
460 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
462 assert(!(flags
& ~BDRV_REQ_FUA
));
463 assert(payload_offset
< INT64_MAX
);
464 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
465 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
467 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
469 /* Bounce buffer because we're not permitted to touch
470 * contents of qiov - it points to guest memory.
473 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
475 if (cipher_data
== NULL
) {
481 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
483 qemu_iovec_to_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
485 if (qcrypto_block_encrypt(crypto
->block
, offset
+ bytes_done
,
486 cipher_data
, cur_bytes
, NULL
) < 0) {
491 qemu_iovec_reset(&hd_qiov
);
492 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
494 ret
= bdrv_co_pwritev(bs
->file
, payload_offset
+ offset
+ bytes_done
,
495 cur_bytes
, &hd_qiov
, flags
);
501 bytes_done
+= cur_bytes
;
505 qemu_iovec_destroy(&hd_qiov
);
506 qemu_vfree(cipher_data
);
511 static void block_crypto_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
513 BlockCrypto
*crypto
= bs
->opaque
;
514 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
515 bs
->bl
.request_alignment
= sector_size
; /* No sub-sector I/O */
519 static int64_t block_crypto_getlength(BlockDriverState
*bs
)
521 BlockCrypto
*crypto
= bs
->opaque
;
522 int64_t len
= bdrv_getlength(bs
->file
->bs
);
524 uint64_t offset
= qcrypto_block_get_payload_offset(crypto
->block
);
525 assert(offset
< INT64_MAX
);
537 static int block_crypto_probe_luks(const uint8_t *buf
,
539 const char *filename
) {
540 return block_crypto_probe_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
541 buf
, buf_size
, filename
);
544 static int block_crypto_open_luks(BlockDriverState
*bs
,
549 return block_crypto_open_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
550 &block_crypto_runtime_opts_luks
,
551 bs
, options
, flags
, errp
);
554 static int coroutine_fn
555 block_crypto_co_create_luks(BlockdevCreateOptions
*create_options
, Error
**errp
)
557 BlockdevCreateOptionsLUKS
*luks_opts
;
558 BlockDriverState
*bs
= NULL
;
559 QCryptoBlockCreateOptions create_opts
;
562 assert(create_options
->driver
== BLOCKDEV_DRIVER_LUKS
);
563 luks_opts
= &create_options
->u
.luks
;
565 bs
= bdrv_open_blockdev_ref(luks_opts
->file
, errp
);
570 create_opts
= (QCryptoBlockCreateOptions
) {
571 .format
= Q_CRYPTO_BLOCK_FORMAT_LUKS
,
572 .u
.luks
= *qapi_BlockdevCreateOptionsLUKS_base(luks_opts
),
575 ret
= block_crypto_co_create_generic(bs
, luks_opts
->size
, &create_opts
,
587 static int coroutine_fn
block_crypto_co_create_opts_luks(const char *filename
,
591 QCryptoBlockCreateOptions
*create_opts
= NULL
;
592 BlockDriverState
*bs
= NULL
;
598 size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
600 cryptoopts
= qemu_opts_to_qdict_filtered(opts
, NULL
,
601 &block_crypto_create_opts_luks
,
604 create_opts
= block_crypto_create_opts_init(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
611 /* Create protocol layer */
612 ret
= bdrv_create_file(filename
, opts
, errp
);
617 bs
= bdrv_open(filename
, NULL
, NULL
,
618 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
624 /* Create format layer */
625 ret
= block_crypto_co_create_generic(bs
, size
, create_opts
, errp
);
633 qapi_free_QCryptoBlockCreateOptions(create_opts
);
638 static int block_crypto_get_info_luks(BlockDriverState
*bs
,
639 BlockDriverInfo
*bdi
)
641 BlockDriverInfo subbdi
;
644 ret
= bdrv_get_info(bs
->file
->bs
, &subbdi
);
649 bdi
->unallocated_blocks_are_zero
= false;
650 bdi
->cluster_size
= subbdi
.cluster_size
;
655 static ImageInfoSpecific
*
656 block_crypto_get_specific_info_luks(BlockDriverState
*bs
)
658 BlockCrypto
*crypto
= bs
->opaque
;
659 ImageInfoSpecific
*spec_info
;
660 QCryptoBlockInfo
*info
;
662 info
= qcrypto_block_get_info(crypto
->block
, NULL
);
666 if (info
->format
!= Q_CRYPTO_BLOCK_FORMAT_LUKS
) {
667 qapi_free_QCryptoBlockInfo(info
);
671 spec_info
= g_new(ImageInfoSpecific
, 1);
672 spec_info
->type
= IMAGE_INFO_SPECIFIC_KIND_LUKS
;
673 spec_info
->u
.luks
.data
= g_new(QCryptoBlockInfoLUKS
, 1);
674 *spec_info
->u
.luks
.data
= info
->u
.luks
;
676 /* Blank out pointers we've just stolen to avoid double free */
677 memset(&info
->u
.luks
, 0, sizeof(info
->u
.luks
));
679 qapi_free_QCryptoBlockInfo(info
);
684 BlockDriver bdrv_crypto_luks
= {
685 .format_name
= "luks",
686 .instance_size
= sizeof(BlockCrypto
),
687 .bdrv_probe
= block_crypto_probe_luks
,
688 .bdrv_open
= block_crypto_open_luks
,
689 .bdrv_close
= block_crypto_close
,
690 .bdrv_child_perm
= bdrv_format_default_perms
,
691 .bdrv_co_create
= block_crypto_co_create_luks
,
692 .bdrv_co_create_opts
= block_crypto_co_create_opts_luks
,
693 .bdrv_truncate
= block_crypto_truncate
,
694 .create_opts
= &block_crypto_create_opts_luks
,
696 .bdrv_reopen_prepare
= block_crypto_reopen_prepare
,
697 .bdrv_refresh_limits
= block_crypto_refresh_limits
,
698 .bdrv_co_preadv
= block_crypto_co_preadv
,
699 .bdrv_co_pwritev
= block_crypto_co_pwritev
,
700 .bdrv_getlength
= block_crypto_getlength
,
701 .bdrv_get_info
= block_crypto_get_info_luks
,
702 .bdrv_get_specific_info
= block_crypto_get_specific_info_luks
,
705 static void block_crypto_init(void)
707 bdrv_register(&bdrv_crypto_luks
);
710 block_init(block_crypto_init
);