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