]>
Commit | Line | Data |
---|---|---|
78368575 DB |
1 | /* |
2 | * QEMU block full disk encryption | |
3 | * | |
4 | * Copyright (c) 2015-2016 Red Hat, Inc. | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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/>. | |
18 | * | |
19 | */ | |
20 | ||
21 | #include "qemu/osdep.h" | |
22 | ||
23 | #include "block/block_int.h" | |
f853465a | 24 | #include "block/qdict.h" |
78368575 DB |
25 | #include "sysemu/block-backend.h" |
26 | #include "crypto/block.h" | |
27 | #include "qapi/opts-visitor.h" | |
9af23989 | 28 | #include "qapi/qapi-visit-crypto.h" |
306a06e5 | 29 | #include "qapi/qobject-input-visitor.h" |
78368575 | 30 | #include "qapi/error.h" |
0b8fa32f | 31 | #include "qemu/module.h" |
922a01a0 | 32 | #include "qemu/option.h" |
1bba30da | 33 | #include "qemu/cutils.h" |
0d8c41da | 34 | #include "crypto.h" |
78368575 DB |
35 | |
36 | typedef struct BlockCrypto BlockCrypto; | |
37 | ||
38 | struct BlockCrypto { | |
39 | QCryptoBlock *block; | |
40 | }; | |
41 | ||
42 | ||
43 | static int block_crypto_probe_generic(QCryptoBlockFormat format, | |
44 | const uint8_t *buf, | |
45 | int buf_size, | |
46 | const char *filename) | |
47 | { | |
48 | if (qcrypto_block_has_format(format, buf, buf_size)) { | |
49 | return 100; | |
50 | } else { | |
51 | return 0; | |
52 | } | |
53 | } | |
54 | ||
55 | ||
56 | static ssize_t block_crypto_read_func(QCryptoBlock *block, | |
57 | size_t offset, | |
58 | uint8_t *buf, | |
59 | size_t buflen, | |
e4a3507e | 60 | void *opaque, |
37509233 | 61 | Error **errp) |
78368575 DB |
62 | { |
63 | BlockDriverState *bs = opaque; | |
64 | ssize_t ret; | |
65 | ||
cf2ab8fc | 66 | ret = bdrv_pread(bs->file, offset, buf, buflen); |
78368575 DB |
67 | if (ret < 0) { |
68 | error_setg_errno(errp, -ret, "Could not read encryption header"); | |
69 | return ret; | |
70 | } | |
71 | return ret; | |
72 | } | |
73 | ||
74 | ||
75 | struct BlockCryptoCreateData { | |
78368575 DB |
76 | BlockBackend *blk; |
77 | uint64_t size; | |
672de729 | 78 | PreallocMode prealloc; |
78368575 DB |
79 | }; |
80 | ||
81 | ||
82 | static ssize_t block_crypto_write_func(QCryptoBlock *block, | |
83 | size_t offset, | |
84 | const uint8_t *buf, | |
85 | size_t buflen, | |
e4a3507e | 86 | void *opaque, |
37509233 | 87 | Error **errp) |
78368575 DB |
88 | { |
89 | struct BlockCryptoCreateData *data = opaque; | |
90 | ssize_t ret; | |
91 | ||
8341f00d | 92 | ret = blk_pwrite(data->blk, offset, buf, buflen, 0); |
78368575 DB |
93 | if (ret < 0) { |
94 | error_setg_errno(errp, -ret, "Could not write encryption header"); | |
95 | return ret; | |
96 | } | |
97 | return ret; | |
98 | } | |
99 | ||
100 | ||
101 | static ssize_t block_crypto_init_func(QCryptoBlock *block, | |
102 | size_t headerlen, | |
e4a3507e | 103 | void *opaque, |
37509233 | 104 | Error **errp) |
78368575 DB |
105 | { |
106 | struct BlockCryptoCreateData *data = opaque; | |
3d1900a4 ML |
107 | Error *local_error = NULL; |
108 | int ret; | |
78368575 | 109 | |
3d7ed9c4 | 110 | if (data->size > INT64_MAX || headerlen > INT64_MAX - data->size) { |
3d1900a4 ML |
111 | ret = -EFBIG; |
112 | goto error; | |
3d7ed9c4 KW |
113 | } |
114 | ||
78368575 DB |
115 | /* User provided size should reflect amount of space made |
116 | * available to the guest, so we must take account of that | |
117 | * which will be used by the crypto header | |
118 | */ | |
3d1900a4 ML |
119 | ret = blk_truncate(data->blk, data->size + headerlen, false, |
120 | data->prealloc, 0, &local_error); | |
121 | ||
122 | if (ret >= 0) { | |
123 | return ret; | |
124 | } | |
125 | ||
126 | error: | |
127 | if (ret == -EFBIG) { | |
128 | /* Replace the error message with a better one */ | |
129 | error_free(local_error); | |
130 | error_setg(errp, "The requested file size is too large"); | |
131 | } else { | |
132 | error_propagate(errp, local_error); | |
133 | } | |
134 | ||
135 | return ret; | |
78368575 DB |
136 | } |
137 | ||
138 | ||
139 | static QemuOptsList block_crypto_runtime_opts_luks = { | |
140 | .name = "crypto", | |
141 | .head = QTAILQ_HEAD_INITIALIZER(block_crypto_runtime_opts_luks.head), | |
142 | .desc = { | |
4a47f854 | 143 | BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""), |
78368575 DB |
144 | { /* end of list */ } |
145 | }, | |
146 | }; | |
147 | ||
148 | ||
149 | static QemuOptsList block_crypto_create_opts_luks = { | |
150 | .name = "crypto", | |
151 | .head = QTAILQ_HEAD_INITIALIZER(block_crypto_create_opts_luks.head), | |
152 | .desc = { | |
153 | { | |
154 | .name = BLOCK_OPT_SIZE, | |
155 | .type = QEMU_OPT_SIZE, | |
156 | .help = "Virtual disk size" | |
157 | }, | |
4a47f854 DB |
158 | BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""), |
159 | BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG(""), | |
160 | BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE(""), | |
161 | BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG(""), | |
162 | BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG(""), | |
163 | BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG(""), | |
164 | BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME(""), | |
78368575 DB |
165 | { /* end of list */ } |
166 | }, | |
167 | }; | |
168 | ||
169 | ||
306a06e5 | 170 | QCryptoBlockOpenOptions * |
796d3239 | 171 | block_crypto_open_opts_init(QDict *opts, Error **errp) |
78368575 | 172 | { |
09204eac | 173 | Visitor *v; |
796d3239 | 174 | QCryptoBlockOpenOptions *ret; |
78368575 | 175 | |
796d3239 | 176 | v = qobject_input_visitor_new_flat_confused(opts, errp); |
e6af90f3 | 177 | if (!v) { |
796d3239 | 178 | return NULL; |
f853465a | 179 | } |
78368575 | 180 | |
796d3239 | 181 | visit_type_QCryptoBlockOpenOptions(v, NULL, &ret, errp); |
78368575 | 182 | |
09204eac | 183 | visit_free(v); |
78368575 DB |
184 | return ret; |
185 | } | |
186 | ||
187 | ||
306a06e5 | 188 | QCryptoBlockCreateOptions * |
796d3239 | 189 | block_crypto_create_opts_init(QDict *opts, Error **errp) |
78368575 | 190 | { |
09204eac | 191 | Visitor *v; |
796d3239 | 192 | QCryptoBlockCreateOptions *ret; |
78368575 | 193 | |
796d3239 | 194 | v = qobject_input_visitor_new_flat_confused(opts, errp); |
e6af90f3 | 195 | if (!v) { |
796d3239 | 196 | return NULL; |
f853465a | 197 | } |
78368575 | 198 | |
796d3239 | 199 | visit_type_QCryptoBlockCreateOptions(v, NULL, &ret, errp); |
78368575 | 200 | |
09204eac | 201 | visit_free(v); |
78368575 DB |
202 | return ret; |
203 | } | |
204 | ||
205 | ||
206 | static int block_crypto_open_generic(QCryptoBlockFormat format, | |
207 | QemuOptsList *opts_spec, | |
208 | BlockDriverState *bs, | |
209 | QDict *options, | |
210 | int flags, | |
211 | Error **errp) | |
212 | { | |
213 | BlockCrypto *crypto = bs->opaque; | |
214 | QemuOpts *opts = NULL; | |
215 | Error *local_err = NULL; | |
216 | int ret = -EINVAL; | |
217 | QCryptoBlockOpenOptions *open_opts = NULL; | |
218 | unsigned int cflags = 0; | |
306a06e5 | 219 | QDict *cryptoopts = NULL; |
78368575 | 220 | |
8b1869da HR |
221 | bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds, |
222 | BDRV_CHILD_IMAGE, false, errp); | |
4e4bf5c4 KW |
223 | if (!bs->file) { |
224 | return -EINVAL; | |
225 | } | |
226 | ||
d67a6b09 DB |
227 | bs->supported_write_flags = BDRV_REQ_FUA & |
228 | bs->file->bs->supported_write_flags; | |
229 | ||
78368575 DB |
230 | opts = qemu_opts_create(opts_spec, NULL, 0, &error_abort); |
231 | qemu_opts_absorb_qdict(opts, options, &local_err); | |
232 | if (local_err) { | |
233 | error_propagate(errp, local_err); | |
234 | goto cleanup; | |
235 | } | |
236 | ||
306a06e5 | 237 | cryptoopts = qemu_opts_to_qdict(opts, NULL); |
796d3239 | 238 | qdict_put_str(cryptoopts, "format", QCryptoBlockFormat_str(format)); |
306a06e5 | 239 | |
796d3239 | 240 | open_opts = block_crypto_open_opts_init(cryptoopts, errp); |
78368575 DB |
241 | if (!open_opts) { |
242 | goto cleanup; | |
243 | } | |
244 | ||
245 | if (flags & BDRV_O_NO_IO) { | |
246 | cflags |= QCRYPTO_BLOCK_OPEN_NO_IO; | |
247 | } | |
1cd9a787 | 248 | crypto->block = qcrypto_block_open(open_opts, NULL, |
78368575 DB |
249 | block_crypto_read_func, |
250 | bs, | |
251 | cflags, | |
c972fa12 | 252 | 1, |
78368575 DB |
253 | errp); |
254 | ||
255 | if (!crypto->block) { | |
256 | ret = -EIO; | |
257 | goto cleanup; | |
258 | } | |
259 | ||
54115412 | 260 | bs->encrypted = true; |
78368575 DB |
261 | |
262 | ret = 0; | |
263 | cleanup: | |
cb3e7f08 | 264 | qobject_unref(cryptoopts); |
78368575 DB |
265 | qapi_free_QCryptoBlockOpenOptions(open_opts); |
266 | return ret; | |
267 | } | |
268 | ||
269 | ||
1ec4f416 KW |
270 | static int block_crypto_co_create_generic(BlockDriverState *bs, |
271 | int64_t size, | |
272 | QCryptoBlockCreateOptions *opts, | |
672de729 | 273 | PreallocMode prealloc, |
1ec4f416 | 274 | Error **errp) |
78368575 | 275 | { |
1ec4f416 KW |
276 | int ret; |
277 | BlockBackend *blk; | |
78368575 | 278 | QCryptoBlock *crypto = NULL; |
1ec4f416 | 279 | struct BlockCryptoCreateData data; |
306a06e5 | 280 | |
a3aeeab5 EB |
281 | blk = blk_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL, |
282 | errp); | |
283 | if (!blk) { | |
284 | ret = -EPERM; | |
1ec4f416 | 285 | goto cleanup; |
3b5a1f6a KW |
286 | } |
287 | ||
672de729 ML |
288 | if (prealloc == PREALLOC_MODE_METADATA) { |
289 | prealloc = PREALLOC_MODE_OFF; | |
290 | } | |
291 | ||
1ec4f416 KW |
292 | data = (struct BlockCryptoCreateData) { |
293 | .blk = blk, | |
294 | .size = size, | |
672de729 | 295 | .prealloc = prealloc, |
1ec4f416 | 296 | }; |
3b5a1f6a | 297 | |
1ec4f416 | 298 | crypto = qcrypto_block_create(opts, NULL, |
78368575 DB |
299 | block_crypto_init_func, |
300 | block_crypto_write_func, | |
301 | &data, | |
302 | errp); | |
303 | ||
304 | if (!crypto) { | |
305 | ret = -EIO; | |
306 | goto cleanup; | |
307 | } | |
308 | ||
309 | ret = 0; | |
310 | cleanup: | |
311 | qcrypto_block_free(crypto); | |
1ec4f416 | 312 | blk_unref(blk); |
78368575 DB |
313 | return ret; |
314 | } | |
315 | ||
061ca8a3 | 316 | static int coroutine_fn |
c80d8b06 | 317 | block_crypto_co_truncate(BlockDriverState *bs, int64_t offset, bool exact, |
92b92799 KW |
318 | PreallocMode prealloc, BdrvRequestFlags flags, |
319 | Error **errp) | |
78368575 DB |
320 | { |
321 | BlockCrypto *crypto = bs->opaque; | |
31376555 | 322 | uint64_t payload_offset = |
78368575 | 323 | qcrypto_block_get_payload_offset(crypto->block); |
120bc742 KW |
324 | |
325 | if (payload_offset > INT64_MAX - offset) { | |
326 | error_setg(errp, "The requested file size is too large"); | |
327 | return -EFBIG; | |
328 | } | |
78368575 DB |
329 | |
330 | offset += payload_offset; | |
331 | ||
7b8e4857 | 332 | return bdrv_co_truncate(bs->file, offset, exact, prealloc, 0, errp); |
78368575 DB |
333 | } |
334 | ||
335 | static void block_crypto_close(BlockDriverState *bs) | |
336 | { | |
337 | BlockCrypto *crypto = bs->opaque; | |
338 | qcrypto_block_free(crypto->block); | |
339 | } | |
340 | ||
f87e08f9 DB |
341 | static int block_crypto_reopen_prepare(BDRVReopenState *state, |
342 | BlockReopenQueue *queue, Error **errp) | |
343 | { | |
344 | /* nothing needs checking */ | |
345 | return 0; | |
346 | } | |
78368575 | 347 | |
161253e2 DB |
348 | /* |
349 | * 1 MB bounce buffer gives good performance / memory tradeoff | |
350 | * when using cache=none|directsync. | |
351 | */ | |
352 | #define BLOCK_CRYPTO_MAX_IO_SIZE (1024 * 1024) | |
78368575 DB |
353 | |
354 | static coroutine_fn int | |
a73466fb DB |
355 | block_crypto_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes, |
356 | QEMUIOVector *qiov, int flags) | |
78368575 DB |
357 | { |
358 | BlockCrypto *crypto = bs->opaque; | |
a73466fb | 359 | uint64_t cur_bytes; /* number of bytes in current iteration */ |
78368575 DB |
360 | uint64_t bytes_done = 0; |
361 | uint8_t *cipher_data = NULL; | |
362 | QEMUIOVector hd_qiov; | |
363 | int ret = 0; | |
a73466fb DB |
364 | uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block); |
365 | uint64_t payload_offset = qcrypto_block_get_payload_offset(crypto->block); | |
a73466fb DB |
366 | |
367 | assert(!flags); | |
368 | assert(payload_offset < INT64_MAX); | |
369 | assert(QEMU_IS_ALIGNED(offset, sector_size)); | |
370 | assert(QEMU_IS_ALIGNED(bytes, sector_size)); | |
78368575 DB |
371 | |
372 | qemu_iovec_init(&hd_qiov, qiov->niov); | |
373 | ||
161253e2 DB |
374 | /* Bounce buffer because we don't wish to expose cipher text |
375 | * in qiov which points to guest memory. | |
78368575 DB |
376 | */ |
377 | cipher_data = | |
161253e2 | 378 | qemu_try_blockalign(bs->file->bs, MIN(BLOCK_CRYPTO_MAX_IO_SIZE, |
78368575 DB |
379 | qiov->size)); |
380 | if (cipher_data == NULL) { | |
381 | ret = -ENOMEM; | |
382 | goto cleanup; | |
383 | } | |
384 | ||
a73466fb DB |
385 | while (bytes) { |
386 | cur_bytes = MIN(bytes, BLOCK_CRYPTO_MAX_IO_SIZE); | |
78368575 DB |
387 | |
388 | qemu_iovec_reset(&hd_qiov); | |
a73466fb | 389 | qemu_iovec_add(&hd_qiov, cipher_data, cur_bytes); |
78368575 | 390 | |
a73466fb DB |
391 | ret = bdrv_co_preadv(bs->file, payload_offset + offset + bytes_done, |
392 | cur_bytes, &hd_qiov, 0); | |
78368575 DB |
393 | if (ret < 0) { |
394 | goto cleanup; | |
395 | } | |
396 | ||
4609742a DB |
397 | if (qcrypto_block_decrypt(crypto->block, offset + bytes_done, |
398 | cipher_data, cur_bytes, NULL) < 0) { | |
78368575 DB |
399 | ret = -EIO; |
400 | goto cleanup; | |
401 | } | |
402 | ||
a73466fb | 403 | qemu_iovec_from_buf(qiov, bytes_done, cipher_data, cur_bytes); |
78368575 | 404 | |
a73466fb DB |
405 | bytes -= cur_bytes; |
406 | bytes_done += cur_bytes; | |
78368575 DB |
407 | } |
408 | ||
409 | cleanup: | |
410 | qemu_iovec_destroy(&hd_qiov); | |
411 | qemu_vfree(cipher_data); | |
412 | ||
413 | return ret; | |
414 | } | |
415 | ||
416 | ||
417 | static coroutine_fn int | |
a73466fb DB |
418 | block_crypto_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes, |
419 | QEMUIOVector *qiov, int flags) | |
78368575 DB |
420 | { |
421 | BlockCrypto *crypto = bs->opaque; | |
a73466fb | 422 | uint64_t cur_bytes; /* number of bytes in current iteration */ |
78368575 DB |
423 | uint64_t bytes_done = 0; |
424 | uint8_t *cipher_data = NULL; | |
425 | QEMUIOVector hd_qiov; | |
426 | int ret = 0; | |
a73466fb DB |
427 | uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block); |
428 | uint64_t payload_offset = qcrypto_block_get_payload_offset(crypto->block); | |
a73466fb | 429 | |
d67a6b09 | 430 | assert(!(flags & ~BDRV_REQ_FUA)); |
a73466fb DB |
431 | assert(payload_offset < INT64_MAX); |
432 | assert(QEMU_IS_ALIGNED(offset, sector_size)); | |
433 | assert(QEMU_IS_ALIGNED(bytes, sector_size)); | |
78368575 DB |
434 | |
435 | qemu_iovec_init(&hd_qiov, qiov->niov); | |
436 | ||
161253e2 DB |
437 | /* Bounce buffer because we're not permitted to touch |
438 | * contents of qiov - it points to guest memory. | |
78368575 DB |
439 | */ |
440 | cipher_data = | |
161253e2 | 441 | qemu_try_blockalign(bs->file->bs, MIN(BLOCK_CRYPTO_MAX_IO_SIZE, |
78368575 DB |
442 | qiov->size)); |
443 | if (cipher_data == NULL) { | |
444 | ret = -ENOMEM; | |
445 | goto cleanup; | |
446 | } | |
447 | ||
a73466fb DB |
448 | while (bytes) { |
449 | cur_bytes = MIN(bytes, BLOCK_CRYPTO_MAX_IO_SIZE); | |
78368575 | 450 | |
a73466fb | 451 | qemu_iovec_to_buf(qiov, bytes_done, cipher_data, cur_bytes); |
78368575 | 452 | |
4609742a DB |
453 | if (qcrypto_block_encrypt(crypto->block, offset + bytes_done, |
454 | cipher_data, cur_bytes, NULL) < 0) { | |
78368575 DB |
455 | ret = -EIO; |
456 | goto cleanup; | |
457 | } | |
458 | ||
459 | qemu_iovec_reset(&hd_qiov); | |
a73466fb | 460 | qemu_iovec_add(&hd_qiov, cipher_data, cur_bytes); |
78368575 | 461 | |
a73466fb | 462 | ret = bdrv_co_pwritev(bs->file, payload_offset + offset + bytes_done, |
d67a6b09 | 463 | cur_bytes, &hd_qiov, flags); |
78368575 DB |
464 | if (ret < 0) { |
465 | goto cleanup; | |
466 | } | |
467 | ||
a73466fb DB |
468 | bytes -= cur_bytes; |
469 | bytes_done += cur_bytes; | |
78368575 DB |
470 | } |
471 | ||
472 | cleanup: | |
473 | qemu_iovec_destroy(&hd_qiov); | |
474 | qemu_vfree(cipher_data); | |
475 | ||
476 | return ret; | |
477 | } | |
478 | ||
a73466fb DB |
479 | static void block_crypto_refresh_limits(BlockDriverState *bs, Error **errp) |
480 | { | |
481 | BlockCrypto *crypto = bs->opaque; | |
482 | uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block); | |
483 | bs->bl.request_alignment = sector_size; /* No sub-sector I/O */ | |
484 | } | |
485 | ||
78368575 DB |
486 | |
487 | static int64_t block_crypto_getlength(BlockDriverState *bs) | |
488 | { | |
489 | BlockCrypto *crypto = bs->opaque; | |
490 | int64_t len = bdrv_getlength(bs->file->bs); | |
491 | ||
31376555 DB |
492 | uint64_t offset = qcrypto_block_get_payload_offset(crypto->block); |
493 | assert(offset < INT64_MAX); | |
e39e959e KW |
494 | |
495 | if (offset > len) { | |
496 | return -EIO; | |
497 | } | |
78368575 DB |
498 | |
499 | len -= offset; | |
500 | ||
501 | return len; | |
502 | } | |
503 | ||
504 | ||
a9da6e49 SH |
505 | static BlockMeasureInfo *block_crypto_measure(QemuOpts *opts, |
506 | BlockDriverState *in_bs, | |
507 | Error **errp) | |
508 | { | |
509 | g_autoptr(QCryptoBlockCreateOptions) create_opts = NULL; | |
510 | Error *local_err = NULL; | |
511 | BlockMeasureInfo *info; | |
512 | uint64_t size; | |
513 | size_t luks_payload_size; | |
514 | QDict *cryptoopts; | |
515 | ||
516 | /* | |
517 | * Preallocation mode doesn't affect size requirements but we must consume | |
518 | * the option. | |
519 | */ | |
520 | g_free(qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC)); | |
521 | ||
522 | size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); | |
523 | ||
524 | if (in_bs) { | |
525 | int64_t ssize = bdrv_getlength(in_bs); | |
526 | ||
527 | if (ssize < 0) { | |
528 | error_setg_errno(&local_err, -ssize, | |
529 | "Unable to get image virtual_size"); | |
530 | goto err; | |
531 | } | |
532 | ||
533 | size = ssize; | |
534 | } | |
535 | ||
536 | cryptoopts = qemu_opts_to_qdict_filtered(opts, NULL, | |
537 | &block_crypto_create_opts_luks, true); | |
538 | qdict_put_str(cryptoopts, "format", "luks"); | |
539 | create_opts = block_crypto_create_opts_init(cryptoopts, &local_err); | |
540 | qobject_unref(cryptoopts); | |
541 | if (!create_opts) { | |
542 | goto err; | |
543 | } | |
544 | ||
545 | if (!qcrypto_block_calculate_payload_offset(create_opts, NULL, | |
546 | &luks_payload_size, | |
547 | &local_err)) { | |
548 | goto err; | |
549 | } | |
550 | ||
551 | /* | |
552 | * Unallocated blocks are still encrypted so allocation status makes no | |
553 | * difference to the file size. | |
554 | */ | |
5d72c68b | 555 | info = g_new0(BlockMeasureInfo, 1); |
a9da6e49 SH |
556 | info->fully_allocated = luks_payload_size + size; |
557 | info->required = luks_payload_size + size; | |
558 | return info; | |
559 | ||
560 | err: | |
561 | error_propagate(errp, local_err); | |
562 | return NULL; | |
563 | } | |
564 | ||
565 | ||
78368575 DB |
566 | static int block_crypto_probe_luks(const uint8_t *buf, |
567 | int buf_size, | |
568 | const char *filename) { | |
569 | return block_crypto_probe_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS, | |
570 | buf, buf_size, filename); | |
571 | } | |
572 | ||
573 | static int block_crypto_open_luks(BlockDriverState *bs, | |
574 | QDict *options, | |
575 | int flags, | |
576 | Error **errp) | |
577 | { | |
578 | return block_crypto_open_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS, | |
579 | &block_crypto_runtime_opts_luks, | |
580 | bs, options, flags, errp); | |
581 | } | |
582 | ||
1bedcaf1 KW |
583 | static int coroutine_fn |
584 | block_crypto_co_create_luks(BlockdevCreateOptions *create_options, Error **errp) | |
585 | { | |
586 | BlockdevCreateOptionsLUKS *luks_opts; | |
587 | BlockDriverState *bs = NULL; | |
588 | QCryptoBlockCreateOptions create_opts; | |
672de729 | 589 | PreallocMode preallocation = PREALLOC_MODE_OFF; |
1bedcaf1 KW |
590 | int ret; |
591 | ||
592 | assert(create_options->driver == BLOCKDEV_DRIVER_LUKS); | |
593 | luks_opts = &create_options->u.luks; | |
594 | ||
595 | bs = bdrv_open_blockdev_ref(luks_opts->file, errp); | |
596 | if (bs == NULL) { | |
597 | return -EIO; | |
598 | } | |
599 | ||
600 | create_opts = (QCryptoBlockCreateOptions) { | |
601 | .format = Q_CRYPTO_BLOCK_FORMAT_LUKS, | |
602 | .u.luks = *qapi_BlockdevCreateOptionsLUKS_base(luks_opts), | |
603 | }; | |
604 | ||
672de729 ML |
605 | if (luks_opts->has_preallocation) { |
606 | preallocation = luks_opts->preallocation; | |
607 | } | |
608 | ||
1bedcaf1 | 609 | ret = block_crypto_co_create_generic(bs, luks_opts->size, &create_opts, |
672de729 | 610 | preallocation, errp); |
1bedcaf1 KW |
611 | if (ret < 0) { |
612 | goto fail; | |
613 | } | |
614 | ||
615 | ret = 0; | |
616 | fail: | |
617 | bdrv_unref(bs); | |
618 | return ret; | |
619 | } | |
620 | ||
b92902df ML |
621 | static int coroutine_fn block_crypto_co_create_opts_luks(BlockDriver *drv, |
622 | const char *filename, | |
efc75e2a SH |
623 | QemuOpts *opts, |
624 | Error **errp) | |
78368575 | 625 | { |
1ec4f416 KW |
626 | QCryptoBlockCreateOptions *create_opts = NULL; |
627 | BlockDriverState *bs = NULL; | |
628 | QDict *cryptoopts; | |
672de729 ML |
629 | PreallocMode prealloc; |
630 | char *buf = NULL; | |
1ec4f416 KW |
631 | int64_t size; |
632 | int ret; | |
672de729 | 633 | Error *local_err = NULL; |
1ec4f416 KW |
634 | |
635 | /* Parse options */ | |
636 | size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); | |
637 | ||
672de729 ML |
638 | buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); |
639 | prealloc = qapi_enum_parse(&PreallocMode_lookup, buf, | |
640 | PREALLOC_MODE_OFF, &local_err); | |
641 | g_free(buf); | |
642 | if (local_err) { | |
643 | error_propagate(errp, local_err); | |
644 | return -EINVAL; | |
645 | } | |
646 | ||
1ec4f416 KW |
647 | cryptoopts = qemu_opts_to_qdict_filtered(opts, NULL, |
648 | &block_crypto_create_opts_luks, | |
649 | true); | |
650 | ||
796d3239 MA |
651 | qdict_put_str(cryptoopts, "format", "luks"); |
652 | create_opts = block_crypto_create_opts_init(cryptoopts, errp); | |
1ec4f416 KW |
653 | if (!create_opts) { |
654 | ret = -EINVAL; | |
655 | goto fail; | |
656 | } | |
657 | ||
658 | /* Create protocol layer */ | |
659 | ret = bdrv_create_file(filename, opts, errp); | |
660 | if (ret < 0) { | |
0b68589d | 661 | goto fail; |
1ec4f416 KW |
662 | } |
663 | ||
664 | bs = bdrv_open(filename, NULL, NULL, | |
665 | BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp); | |
666 | if (!bs) { | |
667 | ret = -EINVAL; | |
668 | goto fail; | |
669 | } | |
670 | ||
671 | /* Create format layer */ | |
672de729 | 672 | ret = block_crypto_co_create_generic(bs, size, create_opts, prealloc, errp); |
1ec4f416 KW |
673 | if (ret < 0) { |
674 | goto fail; | |
675 | } | |
676 | ||
677 | ret = 0; | |
678 | fail: | |
1bba30da DHB |
679 | /* |
680 | * If an error occurred, delete 'filename'. Even if the file existed | |
681 | * beforehand, it has been truncated and corrupted in the process. | |
682 | */ | |
683 | if (ret && bs) { | |
684 | Error *local_delete_err = NULL; | |
685 | int r_del = bdrv_co_delete_file(bs, &local_delete_err); | |
686 | /* | |
687 | * ENOTSUP will happen if the block driver doesn't support | |
688 | * the 'bdrv_co_delete_file' interface. This is a predictable | |
689 | * scenario and shouldn't be reported back to the user. | |
690 | */ | |
691 | if ((r_del < 0) && (r_del != -ENOTSUP)) { | |
692 | error_report_err(local_delete_err); | |
693 | } | |
694 | } | |
695 | ||
1ec4f416 KW |
696 | bdrv_unref(bs); |
697 | qapi_free_QCryptoBlockCreateOptions(create_opts); | |
cb3e7f08 | 698 | qobject_unref(cryptoopts); |
1ec4f416 | 699 | return ret; |
78368575 DB |
700 | } |
701 | ||
c7c4cf49 DB |
702 | static int block_crypto_get_info_luks(BlockDriverState *bs, |
703 | BlockDriverInfo *bdi) | |
704 | { | |
705 | BlockDriverInfo subbdi; | |
706 | int ret; | |
707 | ||
708 | ret = bdrv_get_info(bs->file->bs, &subbdi); | |
709 | if (ret != 0) { | |
710 | return ret; | |
711 | } | |
712 | ||
713 | bdi->unallocated_blocks_are_zero = false; | |
c7c4cf49 DB |
714 | bdi->cluster_size = subbdi.cluster_size; |
715 | ||
716 | return 0; | |
717 | } | |
718 | ||
719 | static ImageInfoSpecific * | |
1bf6e9ca | 720 | block_crypto_get_specific_info_luks(BlockDriverState *bs, Error **errp) |
c7c4cf49 DB |
721 | { |
722 | BlockCrypto *crypto = bs->opaque; | |
723 | ImageInfoSpecific *spec_info; | |
724 | QCryptoBlockInfo *info; | |
725 | ||
1bf6e9ca | 726 | info = qcrypto_block_get_info(crypto->block, errp); |
c7c4cf49 DB |
727 | if (!info) { |
728 | return NULL; | |
729 | } | |
1bf6e9ca | 730 | assert(info->format == Q_CRYPTO_BLOCK_FORMAT_LUKS); |
c7c4cf49 DB |
731 | |
732 | spec_info = g_new(ImageInfoSpecific, 1); | |
733 | spec_info->type = IMAGE_INFO_SPECIFIC_KIND_LUKS; | |
734 | spec_info->u.luks.data = g_new(QCryptoBlockInfoLUKS, 1); | |
735 | *spec_info->u.luks.data = info->u.luks; | |
736 | ||
737 | /* Blank out pointers we've just stolen to avoid double free */ | |
738 | memset(&info->u.luks, 0, sizeof(info->u.luks)); | |
739 | ||
740 | qapi_free_QCryptoBlockInfo(info); | |
741 | ||
742 | return spec_info; | |
743 | } | |
744 | ||
2654267c HR |
745 | static const char *const block_crypto_strong_runtime_opts[] = { |
746 | BLOCK_CRYPTO_OPT_LUKS_KEY_SECRET, | |
747 | ||
748 | NULL | |
749 | }; | |
750 | ||
782b9d06 | 751 | static BlockDriver bdrv_crypto_luks = { |
78368575 DB |
752 | .format_name = "luks", |
753 | .instance_size = sizeof(BlockCrypto), | |
754 | .bdrv_probe = block_crypto_probe_luks, | |
755 | .bdrv_open = block_crypto_open_luks, | |
756 | .bdrv_close = block_crypto_close, | |
497da823 FZ |
757 | /* This driver doesn't modify LUKS metadata except when creating image. |
758 | * Allow share-rw=on as a special case. */ | |
69dca43d | 759 | .bdrv_child_perm = bdrv_default_perms, |
1bedcaf1 | 760 | .bdrv_co_create = block_crypto_co_create_luks, |
efc75e2a | 761 | .bdrv_co_create_opts = block_crypto_co_create_opts_luks, |
061ca8a3 | 762 | .bdrv_co_truncate = block_crypto_co_truncate, |
78368575 DB |
763 | .create_opts = &block_crypto_create_opts_luks, |
764 | ||
f87e08f9 | 765 | .bdrv_reopen_prepare = block_crypto_reopen_prepare, |
a73466fb DB |
766 | .bdrv_refresh_limits = block_crypto_refresh_limits, |
767 | .bdrv_co_preadv = block_crypto_co_preadv, | |
768 | .bdrv_co_pwritev = block_crypto_co_pwritev, | |
78368575 | 769 | .bdrv_getlength = block_crypto_getlength, |
a9da6e49 | 770 | .bdrv_measure = block_crypto_measure, |
c7c4cf49 DB |
771 | .bdrv_get_info = block_crypto_get_info_luks, |
772 | .bdrv_get_specific_info = block_crypto_get_specific_info_luks, | |
2654267c | 773 | |
d67066d8 HR |
774 | .is_format = true, |
775 | ||
2654267c | 776 | .strong_runtime_opts = block_crypto_strong_runtime_opts, |
78368575 DB |
777 | }; |
778 | ||
779 | static void block_crypto_init(void) | |
780 | { | |
781 | bdrv_register(&bdrv_crypto_luks); | |
782 | } | |
783 | ||
784 | block_init(block_crypto_init); |