]>
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; | |
bbfdae91 | 40 | bool updating_keys; |
78368575 DB |
41 | }; |
42 | ||
43 | ||
44 | static int block_crypto_probe_generic(QCryptoBlockFormat format, | |
45 | const uint8_t *buf, | |
46 | int buf_size, | |
47 | const char *filename) | |
48 | { | |
49 | if (qcrypto_block_has_format(format, buf, buf_size)) { | |
50 | return 100; | |
51 | } else { | |
52 | return 0; | |
53 | } | |
54 | } | |
55 | ||
56 | ||
57 | static ssize_t block_crypto_read_func(QCryptoBlock *block, | |
58 | size_t offset, | |
59 | uint8_t *buf, | |
60 | size_t buflen, | |
e4a3507e | 61 | void *opaque, |
37509233 | 62 | Error **errp) |
78368575 DB |
63 | { |
64 | BlockDriverState *bs = opaque; | |
65 | ssize_t ret; | |
66 | ||
cf2ab8fc | 67 | ret = bdrv_pread(bs->file, offset, buf, buflen); |
78368575 DB |
68 | if (ret < 0) { |
69 | error_setg_errno(errp, -ret, "Could not read encryption header"); | |
70 | return ret; | |
71 | } | |
72 | return ret; | |
73 | } | |
74 | ||
bbfdae91 ML |
75 | static ssize_t block_crypto_write_func(QCryptoBlock *block, |
76 | size_t offset, | |
77 | const uint8_t *buf, | |
78 | size_t buflen, | |
79 | void *opaque, | |
80 | Error **errp) | |
81 | { | |
82 | BlockDriverState *bs = opaque; | |
83 | ssize_t ret; | |
84 | ||
85 | ret = bdrv_pwrite(bs->file, offset, buf, buflen); | |
86 | if (ret < 0) { | |
87 | error_setg_errno(errp, -ret, "Could not write encryption header"); | |
88 | return ret; | |
89 | } | |
90 | return ret; | |
91 | } | |
92 | ||
78368575 DB |
93 | |
94 | struct BlockCryptoCreateData { | |
78368575 DB |
95 | BlockBackend *blk; |
96 | uint64_t size; | |
672de729 | 97 | PreallocMode prealloc; |
78368575 DB |
98 | }; |
99 | ||
100 | ||
e0d0ddc5 ML |
101 | static ssize_t block_crypto_create_write_func(QCryptoBlock *block, |
102 | size_t offset, | |
103 | const uint8_t *buf, | |
104 | size_t buflen, | |
105 | void *opaque, | |
106 | Error **errp) | |
78368575 DB |
107 | { |
108 | struct BlockCryptoCreateData *data = opaque; | |
109 | ssize_t ret; | |
110 | ||
8341f00d | 111 | ret = blk_pwrite(data->blk, offset, buf, buflen, 0); |
78368575 DB |
112 | if (ret < 0) { |
113 | error_setg_errno(errp, -ret, "Could not write encryption header"); | |
114 | return ret; | |
115 | } | |
116 | return ret; | |
117 | } | |
118 | ||
e0d0ddc5 ML |
119 | static ssize_t block_crypto_create_init_func(QCryptoBlock *block, |
120 | size_t headerlen, | |
121 | void *opaque, | |
122 | Error **errp) | |
78368575 DB |
123 | { |
124 | struct BlockCryptoCreateData *data = opaque; | |
3d1900a4 ML |
125 | Error *local_error = NULL; |
126 | int ret; | |
78368575 | 127 | |
3d7ed9c4 | 128 | if (data->size > INT64_MAX || headerlen > INT64_MAX - data->size) { |
3d1900a4 ML |
129 | ret = -EFBIG; |
130 | goto error; | |
3d7ed9c4 KW |
131 | } |
132 | ||
78368575 DB |
133 | /* User provided size should reflect amount of space made |
134 | * available to the guest, so we must take account of that | |
135 | * which will be used by the crypto header | |
136 | */ | |
3d1900a4 ML |
137 | ret = blk_truncate(data->blk, data->size + headerlen, false, |
138 | data->prealloc, 0, &local_error); | |
139 | ||
140 | if (ret >= 0) { | |
141 | return ret; | |
142 | } | |
143 | ||
144 | error: | |
145 | if (ret == -EFBIG) { | |
146 | /* Replace the error message with a better one */ | |
147 | error_free(local_error); | |
148 | error_setg(errp, "The requested file size is too large"); | |
149 | } else { | |
150 | error_propagate(errp, local_error); | |
151 | } | |
152 | ||
153 | return ret; | |
78368575 DB |
154 | } |
155 | ||
156 | ||
157 | static QemuOptsList block_crypto_runtime_opts_luks = { | |
158 | .name = "crypto", | |
159 | .head = QTAILQ_HEAD_INITIALIZER(block_crypto_runtime_opts_luks.head), | |
160 | .desc = { | |
4a47f854 | 161 | BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""), |
78368575 DB |
162 | { /* end of list */ } |
163 | }, | |
164 | }; | |
165 | ||
166 | ||
167 | static QemuOptsList block_crypto_create_opts_luks = { | |
168 | .name = "crypto", | |
169 | .head = QTAILQ_HEAD_INITIALIZER(block_crypto_create_opts_luks.head), | |
170 | .desc = { | |
171 | { | |
172 | .name = BLOCK_OPT_SIZE, | |
173 | .type = QEMU_OPT_SIZE, | |
174 | .help = "Virtual disk size" | |
175 | }, | |
4a47f854 DB |
176 | BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""), |
177 | BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG(""), | |
178 | BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE(""), | |
179 | BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG(""), | |
180 | BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG(""), | |
181 | BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG(""), | |
182 | BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME(""), | |
78368575 DB |
183 | { /* end of list */ } |
184 | }, | |
185 | }; | |
186 | ||
187 | ||
bbfdae91 ML |
188 | static QemuOptsList block_crypto_amend_opts_luks = { |
189 | .name = "crypto", | |
190 | .head = QTAILQ_HEAD_INITIALIZER(block_crypto_create_opts_luks.head), | |
191 | .desc = { | |
192 | BLOCK_CRYPTO_OPT_DEF_LUKS_STATE(""), | |
193 | BLOCK_CRYPTO_OPT_DEF_LUKS_KEYSLOT(""), | |
194 | BLOCK_CRYPTO_OPT_DEF_LUKS_OLD_SECRET(""), | |
195 | BLOCK_CRYPTO_OPT_DEF_LUKS_NEW_SECRET(""), | |
196 | BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME(""), | |
197 | { /* end of list */ } | |
198 | }, | |
199 | }; | |
200 | ||
306a06e5 | 201 | QCryptoBlockOpenOptions * |
796d3239 | 202 | block_crypto_open_opts_init(QDict *opts, Error **errp) |
78368575 | 203 | { |
09204eac | 204 | Visitor *v; |
796d3239 | 205 | QCryptoBlockOpenOptions *ret; |
78368575 | 206 | |
796d3239 | 207 | v = qobject_input_visitor_new_flat_confused(opts, errp); |
e6af90f3 | 208 | if (!v) { |
796d3239 | 209 | return NULL; |
f853465a | 210 | } |
78368575 | 211 | |
796d3239 | 212 | visit_type_QCryptoBlockOpenOptions(v, NULL, &ret, errp); |
78368575 | 213 | |
09204eac | 214 | visit_free(v); |
78368575 DB |
215 | return ret; |
216 | } | |
217 | ||
218 | ||
306a06e5 | 219 | QCryptoBlockCreateOptions * |
796d3239 | 220 | block_crypto_create_opts_init(QDict *opts, Error **errp) |
78368575 | 221 | { |
09204eac | 222 | Visitor *v; |
796d3239 | 223 | QCryptoBlockCreateOptions *ret; |
78368575 | 224 | |
796d3239 | 225 | v = qobject_input_visitor_new_flat_confused(opts, errp); |
e6af90f3 | 226 | if (!v) { |
796d3239 | 227 | return NULL; |
f853465a | 228 | } |
78368575 | 229 | |
796d3239 | 230 | visit_type_QCryptoBlockCreateOptions(v, NULL, &ret, errp); |
78368575 | 231 | |
09204eac | 232 | visit_free(v); |
78368575 DB |
233 | return ret; |
234 | } | |
235 | ||
43cbd06d ML |
236 | QCryptoBlockAmendOptions * |
237 | block_crypto_amend_opts_init(QDict *opts, Error **errp) | |
238 | { | |
239 | Visitor *v; | |
240 | QCryptoBlockAmendOptions *ret; | |
241 | ||
242 | v = qobject_input_visitor_new_flat_confused(opts, errp); | |
243 | if (!v) { | |
244 | return NULL; | |
245 | } | |
246 | ||
247 | visit_type_QCryptoBlockAmendOptions(v, NULL, &ret, errp); | |
248 | ||
249 | visit_free(v); | |
250 | return ret; | |
251 | } | |
252 | ||
78368575 DB |
253 | |
254 | static int block_crypto_open_generic(QCryptoBlockFormat format, | |
255 | QemuOptsList *opts_spec, | |
256 | BlockDriverState *bs, | |
257 | QDict *options, | |
258 | int flags, | |
259 | Error **errp) | |
260 | { | |
261 | BlockCrypto *crypto = bs->opaque; | |
262 | QemuOpts *opts = NULL; | |
263 | Error *local_err = NULL; | |
264 | int ret = -EINVAL; | |
265 | QCryptoBlockOpenOptions *open_opts = NULL; | |
266 | unsigned int cflags = 0; | |
306a06e5 | 267 | QDict *cryptoopts = NULL; |
78368575 | 268 | |
8b1869da HR |
269 | bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds, |
270 | BDRV_CHILD_IMAGE, false, errp); | |
4e4bf5c4 KW |
271 | if (!bs->file) { |
272 | return -EINVAL; | |
273 | } | |
274 | ||
d67a6b09 DB |
275 | bs->supported_write_flags = BDRV_REQ_FUA & |
276 | bs->file->bs->supported_write_flags; | |
277 | ||
78368575 | 278 | opts = qemu_opts_create(opts_spec, NULL, 0, &error_abort); |
235e59cf | 279 | if (!qemu_opts_absorb_qdict(opts, options, &local_err)) { |
78368575 DB |
280 | error_propagate(errp, local_err); |
281 | goto cleanup; | |
282 | } | |
283 | ||
306a06e5 | 284 | cryptoopts = qemu_opts_to_qdict(opts, NULL); |
796d3239 | 285 | qdict_put_str(cryptoopts, "format", QCryptoBlockFormat_str(format)); |
306a06e5 | 286 | |
796d3239 | 287 | open_opts = block_crypto_open_opts_init(cryptoopts, errp); |
78368575 DB |
288 | if (!open_opts) { |
289 | goto cleanup; | |
290 | } | |
291 | ||
292 | if (flags & BDRV_O_NO_IO) { | |
293 | cflags |= QCRYPTO_BLOCK_OPEN_NO_IO; | |
294 | } | |
1cd9a787 | 295 | crypto->block = qcrypto_block_open(open_opts, NULL, |
78368575 DB |
296 | block_crypto_read_func, |
297 | bs, | |
298 | cflags, | |
c972fa12 | 299 | 1, |
78368575 DB |
300 | errp); |
301 | ||
302 | if (!crypto->block) { | |
303 | ret = -EIO; | |
304 | goto cleanup; | |
305 | } | |
306 | ||
54115412 | 307 | bs->encrypted = true; |
78368575 DB |
308 | |
309 | ret = 0; | |
310 | cleanup: | |
cb3e7f08 | 311 | qobject_unref(cryptoopts); |
78368575 DB |
312 | qapi_free_QCryptoBlockOpenOptions(open_opts); |
313 | return ret; | |
314 | } | |
315 | ||
316 | ||
1ec4f416 KW |
317 | static int block_crypto_co_create_generic(BlockDriverState *bs, |
318 | int64_t size, | |
319 | QCryptoBlockCreateOptions *opts, | |
672de729 | 320 | PreallocMode prealloc, |
1ec4f416 | 321 | Error **errp) |
78368575 | 322 | { |
1ec4f416 KW |
323 | int ret; |
324 | BlockBackend *blk; | |
78368575 | 325 | QCryptoBlock *crypto = NULL; |
1ec4f416 | 326 | struct BlockCryptoCreateData data; |
306a06e5 | 327 | |
a3aeeab5 EB |
328 | blk = blk_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL, |
329 | errp); | |
330 | if (!blk) { | |
331 | ret = -EPERM; | |
1ec4f416 | 332 | goto cleanup; |
3b5a1f6a KW |
333 | } |
334 | ||
672de729 ML |
335 | if (prealloc == PREALLOC_MODE_METADATA) { |
336 | prealloc = PREALLOC_MODE_OFF; | |
337 | } | |
338 | ||
1ec4f416 KW |
339 | data = (struct BlockCryptoCreateData) { |
340 | .blk = blk, | |
341 | .size = size, | |
672de729 | 342 | .prealloc = prealloc, |
1ec4f416 | 343 | }; |
3b5a1f6a | 344 | |
1ec4f416 | 345 | crypto = qcrypto_block_create(opts, NULL, |
e0d0ddc5 ML |
346 | block_crypto_create_init_func, |
347 | block_crypto_create_write_func, | |
78368575 DB |
348 | &data, |
349 | errp); | |
350 | ||
351 | if (!crypto) { | |
352 | ret = -EIO; | |
353 | goto cleanup; | |
354 | } | |
355 | ||
356 | ret = 0; | |
357 | cleanup: | |
358 | qcrypto_block_free(crypto); | |
1ec4f416 | 359 | blk_unref(blk); |
78368575 DB |
360 | return ret; |
361 | } | |
362 | ||
061ca8a3 | 363 | static int coroutine_fn |
c80d8b06 | 364 | block_crypto_co_truncate(BlockDriverState *bs, int64_t offset, bool exact, |
92b92799 KW |
365 | PreallocMode prealloc, BdrvRequestFlags flags, |
366 | Error **errp) | |
78368575 DB |
367 | { |
368 | BlockCrypto *crypto = bs->opaque; | |
31376555 | 369 | uint64_t payload_offset = |
78368575 | 370 | qcrypto_block_get_payload_offset(crypto->block); |
120bc742 KW |
371 | |
372 | if (payload_offset > INT64_MAX - offset) { | |
373 | error_setg(errp, "The requested file size is too large"); | |
374 | return -EFBIG; | |
375 | } | |
78368575 DB |
376 | |
377 | offset += payload_offset; | |
378 | ||
7b8e4857 | 379 | return bdrv_co_truncate(bs->file, offset, exact, prealloc, 0, errp); |
78368575 DB |
380 | } |
381 | ||
382 | static void block_crypto_close(BlockDriverState *bs) | |
383 | { | |
384 | BlockCrypto *crypto = bs->opaque; | |
385 | qcrypto_block_free(crypto->block); | |
386 | } | |
387 | ||
f87e08f9 DB |
388 | static int block_crypto_reopen_prepare(BDRVReopenState *state, |
389 | BlockReopenQueue *queue, Error **errp) | |
390 | { | |
391 | /* nothing needs checking */ | |
392 | return 0; | |
393 | } | |
78368575 | 394 | |
161253e2 DB |
395 | /* |
396 | * 1 MB bounce buffer gives good performance / memory tradeoff | |
397 | * when using cache=none|directsync. | |
398 | */ | |
399 | #define BLOCK_CRYPTO_MAX_IO_SIZE (1024 * 1024) | |
78368575 DB |
400 | |
401 | static coroutine_fn int | |
a73466fb DB |
402 | block_crypto_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes, |
403 | QEMUIOVector *qiov, int flags) | |
78368575 DB |
404 | { |
405 | BlockCrypto *crypto = bs->opaque; | |
a73466fb | 406 | uint64_t cur_bytes; /* number of bytes in current iteration */ |
78368575 DB |
407 | uint64_t bytes_done = 0; |
408 | uint8_t *cipher_data = NULL; | |
409 | QEMUIOVector hd_qiov; | |
410 | int ret = 0; | |
a73466fb DB |
411 | uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block); |
412 | uint64_t payload_offset = qcrypto_block_get_payload_offset(crypto->block); | |
a73466fb DB |
413 | |
414 | assert(!flags); | |
415 | assert(payload_offset < INT64_MAX); | |
416 | assert(QEMU_IS_ALIGNED(offset, sector_size)); | |
417 | assert(QEMU_IS_ALIGNED(bytes, sector_size)); | |
78368575 DB |
418 | |
419 | qemu_iovec_init(&hd_qiov, qiov->niov); | |
420 | ||
161253e2 DB |
421 | /* Bounce buffer because we don't wish to expose cipher text |
422 | * in qiov which points to guest memory. | |
78368575 DB |
423 | */ |
424 | cipher_data = | |
161253e2 | 425 | qemu_try_blockalign(bs->file->bs, MIN(BLOCK_CRYPTO_MAX_IO_SIZE, |
78368575 DB |
426 | qiov->size)); |
427 | if (cipher_data == NULL) { | |
428 | ret = -ENOMEM; | |
429 | goto cleanup; | |
430 | } | |
431 | ||
a73466fb DB |
432 | while (bytes) { |
433 | cur_bytes = MIN(bytes, BLOCK_CRYPTO_MAX_IO_SIZE); | |
78368575 DB |
434 | |
435 | qemu_iovec_reset(&hd_qiov); | |
a73466fb | 436 | qemu_iovec_add(&hd_qiov, cipher_data, cur_bytes); |
78368575 | 437 | |
a73466fb DB |
438 | ret = bdrv_co_preadv(bs->file, payload_offset + offset + bytes_done, |
439 | cur_bytes, &hd_qiov, 0); | |
78368575 DB |
440 | if (ret < 0) { |
441 | goto cleanup; | |
442 | } | |
443 | ||
4609742a DB |
444 | if (qcrypto_block_decrypt(crypto->block, offset + bytes_done, |
445 | cipher_data, cur_bytes, NULL) < 0) { | |
78368575 DB |
446 | ret = -EIO; |
447 | goto cleanup; | |
448 | } | |
449 | ||
a73466fb | 450 | qemu_iovec_from_buf(qiov, bytes_done, cipher_data, cur_bytes); |
78368575 | 451 | |
a73466fb DB |
452 | bytes -= cur_bytes; |
453 | bytes_done += cur_bytes; | |
78368575 DB |
454 | } |
455 | ||
456 | cleanup: | |
457 | qemu_iovec_destroy(&hd_qiov); | |
458 | qemu_vfree(cipher_data); | |
459 | ||
460 | return ret; | |
461 | } | |
462 | ||
463 | ||
464 | static coroutine_fn int | |
a73466fb DB |
465 | block_crypto_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes, |
466 | QEMUIOVector *qiov, int flags) | |
78368575 DB |
467 | { |
468 | BlockCrypto *crypto = bs->opaque; | |
a73466fb | 469 | uint64_t cur_bytes; /* number of bytes in current iteration */ |
78368575 DB |
470 | uint64_t bytes_done = 0; |
471 | uint8_t *cipher_data = NULL; | |
472 | QEMUIOVector hd_qiov; | |
473 | int ret = 0; | |
a73466fb DB |
474 | uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block); |
475 | uint64_t payload_offset = qcrypto_block_get_payload_offset(crypto->block); | |
a73466fb | 476 | |
d67a6b09 | 477 | assert(!(flags & ~BDRV_REQ_FUA)); |
a73466fb DB |
478 | assert(payload_offset < INT64_MAX); |
479 | assert(QEMU_IS_ALIGNED(offset, sector_size)); | |
480 | assert(QEMU_IS_ALIGNED(bytes, sector_size)); | |
78368575 DB |
481 | |
482 | qemu_iovec_init(&hd_qiov, qiov->niov); | |
483 | ||
161253e2 DB |
484 | /* Bounce buffer because we're not permitted to touch |
485 | * contents of qiov - it points to guest memory. | |
78368575 DB |
486 | */ |
487 | cipher_data = | |
161253e2 | 488 | qemu_try_blockalign(bs->file->bs, MIN(BLOCK_CRYPTO_MAX_IO_SIZE, |
78368575 DB |
489 | qiov->size)); |
490 | if (cipher_data == NULL) { | |
491 | ret = -ENOMEM; | |
492 | goto cleanup; | |
493 | } | |
494 | ||
a73466fb DB |
495 | while (bytes) { |
496 | cur_bytes = MIN(bytes, BLOCK_CRYPTO_MAX_IO_SIZE); | |
78368575 | 497 | |
a73466fb | 498 | qemu_iovec_to_buf(qiov, bytes_done, cipher_data, cur_bytes); |
78368575 | 499 | |
4609742a DB |
500 | if (qcrypto_block_encrypt(crypto->block, offset + bytes_done, |
501 | cipher_data, cur_bytes, NULL) < 0) { | |
78368575 DB |
502 | ret = -EIO; |
503 | goto cleanup; | |
504 | } | |
505 | ||
506 | qemu_iovec_reset(&hd_qiov); | |
a73466fb | 507 | qemu_iovec_add(&hd_qiov, cipher_data, cur_bytes); |
78368575 | 508 | |
a73466fb | 509 | ret = bdrv_co_pwritev(bs->file, payload_offset + offset + bytes_done, |
d67a6b09 | 510 | cur_bytes, &hd_qiov, flags); |
78368575 DB |
511 | if (ret < 0) { |
512 | goto cleanup; | |
513 | } | |
514 | ||
a73466fb DB |
515 | bytes -= cur_bytes; |
516 | bytes_done += cur_bytes; | |
78368575 DB |
517 | } |
518 | ||
519 | cleanup: | |
520 | qemu_iovec_destroy(&hd_qiov); | |
521 | qemu_vfree(cipher_data); | |
522 | ||
523 | return ret; | |
524 | } | |
525 | ||
a73466fb DB |
526 | static void block_crypto_refresh_limits(BlockDriverState *bs, Error **errp) |
527 | { | |
528 | BlockCrypto *crypto = bs->opaque; | |
529 | uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block); | |
530 | bs->bl.request_alignment = sector_size; /* No sub-sector I/O */ | |
531 | } | |
532 | ||
78368575 DB |
533 | |
534 | static int64_t block_crypto_getlength(BlockDriverState *bs) | |
535 | { | |
536 | BlockCrypto *crypto = bs->opaque; | |
537 | int64_t len = bdrv_getlength(bs->file->bs); | |
538 | ||
31376555 DB |
539 | uint64_t offset = qcrypto_block_get_payload_offset(crypto->block); |
540 | assert(offset < INT64_MAX); | |
e39e959e KW |
541 | |
542 | if (offset > len) { | |
543 | return -EIO; | |
544 | } | |
78368575 DB |
545 | |
546 | len -= offset; | |
547 | ||
548 | return len; | |
549 | } | |
550 | ||
551 | ||
a9da6e49 SH |
552 | static BlockMeasureInfo *block_crypto_measure(QemuOpts *opts, |
553 | BlockDriverState *in_bs, | |
554 | Error **errp) | |
555 | { | |
556 | g_autoptr(QCryptoBlockCreateOptions) create_opts = NULL; | |
557 | Error *local_err = NULL; | |
558 | BlockMeasureInfo *info; | |
559 | uint64_t size; | |
560 | size_t luks_payload_size; | |
561 | QDict *cryptoopts; | |
562 | ||
563 | /* | |
564 | * Preallocation mode doesn't affect size requirements but we must consume | |
565 | * the option. | |
566 | */ | |
567 | g_free(qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC)); | |
568 | ||
569 | size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); | |
570 | ||
571 | if (in_bs) { | |
572 | int64_t ssize = bdrv_getlength(in_bs); | |
573 | ||
574 | if (ssize < 0) { | |
575 | error_setg_errno(&local_err, -ssize, | |
576 | "Unable to get image virtual_size"); | |
577 | goto err; | |
578 | } | |
579 | ||
580 | size = ssize; | |
581 | } | |
582 | ||
583 | cryptoopts = qemu_opts_to_qdict_filtered(opts, NULL, | |
584 | &block_crypto_create_opts_luks, true); | |
585 | qdict_put_str(cryptoopts, "format", "luks"); | |
586 | create_opts = block_crypto_create_opts_init(cryptoopts, &local_err); | |
587 | qobject_unref(cryptoopts); | |
588 | if (!create_opts) { | |
589 | goto err; | |
590 | } | |
591 | ||
592 | if (!qcrypto_block_calculate_payload_offset(create_opts, NULL, | |
593 | &luks_payload_size, | |
594 | &local_err)) { | |
595 | goto err; | |
596 | } | |
597 | ||
598 | /* | |
599 | * Unallocated blocks are still encrypted so allocation status makes no | |
600 | * difference to the file size. | |
601 | */ | |
5d72c68b | 602 | info = g_new0(BlockMeasureInfo, 1); |
a9da6e49 SH |
603 | info->fully_allocated = luks_payload_size + size; |
604 | info->required = luks_payload_size + size; | |
605 | return info; | |
606 | ||
607 | err: | |
608 | error_propagate(errp, local_err); | |
609 | return NULL; | |
610 | } | |
611 | ||
612 | ||
78368575 DB |
613 | static int block_crypto_probe_luks(const uint8_t *buf, |
614 | int buf_size, | |
615 | const char *filename) { | |
616 | return block_crypto_probe_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS, | |
617 | buf, buf_size, filename); | |
618 | } | |
619 | ||
620 | static int block_crypto_open_luks(BlockDriverState *bs, | |
621 | QDict *options, | |
622 | int flags, | |
623 | Error **errp) | |
624 | { | |
625 | return block_crypto_open_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS, | |
626 | &block_crypto_runtime_opts_luks, | |
627 | bs, options, flags, errp); | |
628 | } | |
629 | ||
1bedcaf1 KW |
630 | static int coroutine_fn |
631 | block_crypto_co_create_luks(BlockdevCreateOptions *create_options, Error **errp) | |
632 | { | |
633 | BlockdevCreateOptionsLUKS *luks_opts; | |
634 | BlockDriverState *bs = NULL; | |
635 | QCryptoBlockCreateOptions create_opts; | |
672de729 | 636 | PreallocMode preallocation = PREALLOC_MODE_OFF; |
1bedcaf1 KW |
637 | int ret; |
638 | ||
639 | assert(create_options->driver == BLOCKDEV_DRIVER_LUKS); | |
640 | luks_opts = &create_options->u.luks; | |
641 | ||
642 | bs = bdrv_open_blockdev_ref(luks_opts->file, errp); | |
643 | if (bs == NULL) { | |
644 | return -EIO; | |
645 | } | |
646 | ||
647 | create_opts = (QCryptoBlockCreateOptions) { | |
648 | .format = Q_CRYPTO_BLOCK_FORMAT_LUKS, | |
649 | .u.luks = *qapi_BlockdevCreateOptionsLUKS_base(luks_opts), | |
650 | }; | |
651 | ||
672de729 ML |
652 | if (luks_opts->has_preallocation) { |
653 | preallocation = luks_opts->preallocation; | |
654 | } | |
655 | ||
1bedcaf1 | 656 | ret = block_crypto_co_create_generic(bs, luks_opts->size, &create_opts, |
672de729 | 657 | preallocation, errp); |
1bedcaf1 KW |
658 | if (ret < 0) { |
659 | goto fail; | |
660 | } | |
661 | ||
662 | ret = 0; | |
663 | fail: | |
664 | bdrv_unref(bs); | |
665 | return ret; | |
666 | } | |
667 | ||
b92902df ML |
668 | static int coroutine_fn block_crypto_co_create_opts_luks(BlockDriver *drv, |
669 | const char *filename, | |
efc75e2a SH |
670 | QemuOpts *opts, |
671 | Error **errp) | |
78368575 | 672 | { |
1ec4f416 KW |
673 | QCryptoBlockCreateOptions *create_opts = NULL; |
674 | BlockDriverState *bs = NULL; | |
675 | QDict *cryptoopts; | |
672de729 ML |
676 | PreallocMode prealloc; |
677 | char *buf = NULL; | |
1ec4f416 KW |
678 | int64_t size; |
679 | int ret; | |
672de729 | 680 | Error *local_err = NULL; |
1ec4f416 KW |
681 | |
682 | /* Parse options */ | |
683 | size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); | |
684 | ||
672de729 ML |
685 | buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); |
686 | prealloc = qapi_enum_parse(&PreallocMode_lookup, buf, | |
687 | PREALLOC_MODE_OFF, &local_err); | |
688 | g_free(buf); | |
689 | if (local_err) { | |
690 | error_propagate(errp, local_err); | |
691 | return -EINVAL; | |
692 | } | |
693 | ||
1ec4f416 KW |
694 | cryptoopts = qemu_opts_to_qdict_filtered(opts, NULL, |
695 | &block_crypto_create_opts_luks, | |
696 | true); | |
697 | ||
796d3239 MA |
698 | qdict_put_str(cryptoopts, "format", "luks"); |
699 | create_opts = block_crypto_create_opts_init(cryptoopts, errp); | |
1ec4f416 KW |
700 | if (!create_opts) { |
701 | ret = -EINVAL; | |
702 | goto fail; | |
703 | } | |
704 | ||
705 | /* Create protocol layer */ | |
706 | ret = bdrv_create_file(filename, opts, errp); | |
707 | if (ret < 0) { | |
0b68589d | 708 | goto fail; |
1ec4f416 KW |
709 | } |
710 | ||
711 | bs = bdrv_open(filename, NULL, NULL, | |
712 | BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp); | |
713 | if (!bs) { | |
714 | ret = -EINVAL; | |
715 | goto fail; | |
716 | } | |
717 | ||
718 | /* Create format layer */ | |
672de729 | 719 | ret = block_crypto_co_create_generic(bs, size, create_opts, prealloc, errp); |
1ec4f416 KW |
720 | if (ret < 0) { |
721 | goto fail; | |
722 | } | |
723 | ||
724 | ret = 0; | |
725 | fail: | |
1bba30da DHB |
726 | /* |
727 | * If an error occurred, delete 'filename'. Even if the file existed | |
728 | * beforehand, it has been truncated and corrupted in the process. | |
729 | */ | |
730 | if (ret && bs) { | |
731 | Error *local_delete_err = NULL; | |
732 | int r_del = bdrv_co_delete_file(bs, &local_delete_err); | |
733 | /* | |
734 | * ENOTSUP will happen if the block driver doesn't support | |
735 | * the 'bdrv_co_delete_file' interface. This is a predictable | |
736 | * scenario and shouldn't be reported back to the user. | |
737 | */ | |
738 | if ((r_del < 0) && (r_del != -ENOTSUP)) { | |
739 | error_report_err(local_delete_err); | |
740 | } | |
741 | } | |
742 | ||
1ec4f416 KW |
743 | bdrv_unref(bs); |
744 | qapi_free_QCryptoBlockCreateOptions(create_opts); | |
cb3e7f08 | 745 | qobject_unref(cryptoopts); |
1ec4f416 | 746 | return ret; |
78368575 DB |
747 | } |
748 | ||
c7c4cf49 DB |
749 | static int block_crypto_get_info_luks(BlockDriverState *bs, |
750 | BlockDriverInfo *bdi) | |
751 | { | |
752 | BlockDriverInfo subbdi; | |
753 | int ret; | |
754 | ||
755 | ret = bdrv_get_info(bs->file->bs, &subbdi); | |
756 | if (ret != 0) { | |
757 | return ret; | |
758 | } | |
759 | ||
c7c4cf49 DB |
760 | bdi->cluster_size = subbdi.cluster_size; |
761 | ||
762 | return 0; | |
763 | } | |
764 | ||
765 | static ImageInfoSpecific * | |
1bf6e9ca | 766 | block_crypto_get_specific_info_luks(BlockDriverState *bs, Error **errp) |
c7c4cf49 DB |
767 | { |
768 | BlockCrypto *crypto = bs->opaque; | |
769 | ImageInfoSpecific *spec_info; | |
770 | QCryptoBlockInfo *info; | |
771 | ||
1bf6e9ca | 772 | info = qcrypto_block_get_info(crypto->block, errp); |
c7c4cf49 DB |
773 | if (!info) { |
774 | return NULL; | |
775 | } | |
1bf6e9ca | 776 | assert(info->format == Q_CRYPTO_BLOCK_FORMAT_LUKS); |
c7c4cf49 DB |
777 | |
778 | spec_info = g_new(ImageInfoSpecific, 1); | |
779 | spec_info->type = IMAGE_INFO_SPECIFIC_KIND_LUKS; | |
780 | spec_info->u.luks.data = g_new(QCryptoBlockInfoLUKS, 1); | |
781 | *spec_info->u.luks.data = info->u.luks; | |
782 | ||
783 | /* Blank out pointers we've just stolen to avoid double free */ | |
784 | memset(&info->u.luks, 0, sizeof(info->u.luks)); | |
785 | ||
786 | qapi_free_QCryptoBlockInfo(info); | |
787 | ||
788 | return spec_info; | |
789 | } | |
790 | ||
bbfdae91 | 791 | static int |
30da9dd8 ML |
792 | block_crypto_amend_options_generic_luks(BlockDriverState *bs, |
793 | QCryptoBlockAmendOptions *amend_options, | |
794 | bool force, | |
795 | Error **errp) | |
bbfdae91 ML |
796 | { |
797 | BlockCrypto *crypto = bs->opaque; | |
bbfdae91 ML |
798 | int ret; |
799 | ||
800 | assert(crypto); | |
801 | assert(crypto->block); | |
bbfdae91 | 802 | |
30da9dd8 ML |
803 | /* apply for exclusive read/write permissions to the underlying file*/ |
804 | crypto->updating_keys = true; | |
bbfdae91 | 805 | ret = bdrv_child_refresh_perms(bs, bs->file, errp); |
30da9dd8 | 806 | if (ret) { |
bbfdae91 ML |
807 | goto cleanup; |
808 | } | |
809 | ||
810 | ret = qcrypto_block_amend_options(crypto->block, | |
811 | block_crypto_read_func, | |
812 | block_crypto_write_func, | |
813 | bs, | |
814 | amend_options, | |
815 | force, | |
816 | errp); | |
817 | cleanup: | |
30da9dd8 | 818 | /* release exclusive read/write permissions to the underlying file*/ |
bbfdae91 ML |
819 | crypto->updating_keys = false; |
820 | bdrv_child_refresh_perms(bs, bs->file, errp); | |
30da9dd8 ML |
821 | return ret; |
822 | } | |
823 | ||
824 | static int | |
825 | block_crypto_amend_options_luks(BlockDriverState *bs, | |
826 | QemuOpts *opts, | |
827 | BlockDriverAmendStatusCB *status_cb, | |
828 | void *cb_opaque, | |
829 | bool force, | |
830 | Error **errp) | |
831 | { | |
832 | BlockCrypto *crypto = bs->opaque; | |
833 | QDict *cryptoopts = NULL; | |
834 | QCryptoBlockAmendOptions *amend_options = NULL; | |
835 | int ret = -EINVAL; | |
836 | ||
837 | assert(crypto); | |
838 | assert(crypto->block); | |
839 | ||
840 | cryptoopts = qemu_opts_to_qdict(opts, NULL); | |
841 | qdict_put_str(cryptoopts, "format", "luks"); | |
842 | amend_options = block_crypto_amend_opts_init(cryptoopts, errp); | |
bbfdae91 | 843 | qobject_unref(cryptoopts); |
30da9dd8 ML |
844 | if (!amend_options) { |
845 | goto cleanup; | |
846 | } | |
847 | ret = block_crypto_amend_options_generic_luks(bs, amend_options, | |
848 | force, errp); | |
849 | cleanup: | |
850 | qapi_free_QCryptoBlockAmendOptions(amend_options); | |
bbfdae91 ML |
851 | return ret; |
852 | } | |
853 | ||
30da9dd8 ML |
854 | static int |
855 | coroutine_fn block_crypto_co_amend_luks(BlockDriverState *bs, | |
856 | BlockdevAmendOptions *opts, | |
857 | bool force, | |
858 | Error **errp) | |
859 | { | |
860 | QCryptoBlockAmendOptions amend_opts; | |
861 | ||
862 | amend_opts = (QCryptoBlockAmendOptions) { | |
863 | .format = Q_CRYPTO_BLOCK_FORMAT_LUKS, | |
864 | .u.luks = *qapi_BlockdevAmendOptionsLUKS_base(&opts->u.luks), | |
865 | }; | |
866 | return block_crypto_amend_options_generic_luks(bs, &amend_opts, | |
867 | force, errp); | |
868 | } | |
bbfdae91 ML |
869 | |
870 | static void | |
871 | block_crypto_child_perms(BlockDriverState *bs, BdrvChild *c, | |
872 | const BdrvChildRole role, | |
873 | BlockReopenQueue *reopen_queue, | |
874 | uint64_t perm, uint64_t shared, | |
875 | uint64_t *nperm, uint64_t *nshared) | |
876 | { | |
877 | ||
878 | BlockCrypto *crypto = bs->opaque; | |
879 | ||
880 | bdrv_default_perms(bs, c, role, reopen_queue, perm, shared, nperm, nshared); | |
881 | ||
882 | /* | |
883 | * For backward compatibility, manually share the write | |
884 | * and resize permission | |
885 | */ | |
886 | *nshared |= (BLK_PERM_WRITE | BLK_PERM_RESIZE); | |
887 | /* | |
888 | * Since we are not fully a format driver, don't always request | |
889 | * the read/resize permission but only when explicitly | |
890 | * requested | |
891 | */ | |
892 | *nperm &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE); | |
893 | *nperm |= perm & (BLK_PERM_WRITE | BLK_PERM_RESIZE); | |
894 | ||
895 | /* | |
896 | * This driver doesn't modify LUKS metadata except | |
897 | * when updating the encryption slots. | |
898 | * Thus unlike a proper format driver we don't ask for | |
899 | * shared write/read permission. However we need it | |
900 | * when we are updating the keys, to ensure that only we | |
901 | * have access to the device. | |
902 | * | |
903 | * Encryption update will set the crypto->updating_keys | |
904 | * during that period and refresh permissions | |
905 | * | |
906 | */ | |
907 | if (crypto->updating_keys) { | |
908 | /* need exclusive write access for header update */ | |
909 | *nperm |= BLK_PERM_WRITE; | |
910 | /* unshare read and write permission */ | |
911 | *nshared &= ~(BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE); | |
912 | } | |
913 | } | |
914 | ||
915 | ||
2654267c HR |
916 | static const char *const block_crypto_strong_runtime_opts[] = { |
917 | BLOCK_CRYPTO_OPT_LUKS_KEY_SECRET, | |
918 | ||
919 | NULL | |
920 | }; | |
921 | ||
782b9d06 | 922 | static BlockDriver bdrv_crypto_luks = { |
78368575 DB |
923 | .format_name = "luks", |
924 | .instance_size = sizeof(BlockCrypto), | |
925 | .bdrv_probe = block_crypto_probe_luks, | |
926 | .bdrv_open = block_crypto_open_luks, | |
927 | .bdrv_close = block_crypto_close, | |
bbfdae91 | 928 | .bdrv_child_perm = block_crypto_child_perms, |
1bedcaf1 | 929 | .bdrv_co_create = block_crypto_co_create_luks, |
efc75e2a | 930 | .bdrv_co_create_opts = block_crypto_co_create_opts_luks, |
061ca8a3 | 931 | .bdrv_co_truncate = block_crypto_co_truncate, |
78368575 | 932 | .create_opts = &block_crypto_create_opts_luks, |
bbfdae91 | 933 | .amend_opts = &block_crypto_amend_opts_luks, |
78368575 | 934 | |
f87e08f9 | 935 | .bdrv_reopen_prepare = block_crypto_reopen_prepare, |
a73466fb DB |
936 | .bdrv_refresh_limits = block_crypto_refresh_limits, |
937 | .bdrv_co_preadv = block_crypto_co_preadv, | |
938 | .bdrv_co_pwritev = block_crypto_co_pwritev, | |
78368575 | 939 | .bdrv_getlength = block_crypto_getlength, |
a9da6e49 | 940 | .bdrv_measure = block_crypto_measure, |
c7c4cf49 DB |
941 | .bdrv_get_info = block_crypto_get_info_luks, |
942 | .bdrv_get_specific_info = block_crypto_get_specific_info_luks, | |
bbfdae91 | 943 | .bdrv_amend_options = block_crypto_amend_options_luks, |
30da9dd8 | 944 | .bdrv_co_amend = block_crypto_co_amend_luks, |
2654267c | 945 | |
d67066d8 HR |
946 | .is_format = true, |
947 | ||
2654267c | 948 | .strong_runtime_opts = block_crypto_strong_runtime_opts, |
78368575 DB |
949 | }; |
950 | ||
951 | static void block_crypto_init(void) | |
952 | { | |
953 | bdrv_register(&bdrv_crypto_luks); | |
954 | } | |
955 | ||
956 | block_init(block_crypto_init); |