/src/cryptsetup/lib/luks2/luks2_reencrypt.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | /* |
3 | | * LUKS - Linux Unified Key Setup v2, reencryption helpers |
4 | | * |
5 | | * Copyright (C) 2015-2026 Red Hat, Inc. All rights reserved. |
6 | | * Copyright (C) 2015-2026 Ondrej Kozina |
7 | | */ |
8 | | |
9 | | #include "luks2_internal.h" |
10 | | #include "utils_device_locking.h" |
11 | | #include "keyslot_context.h" |
12 | | #include "utils_storage_wrappers.h" |
13 | | |
14 | | struct luks2_reencrypt { |
15 | | /* reencryption window attributes */ |
16 | | uint64_t offset; |
17 | | uint64_t progress; |
18 | | uint64_t length; |
19 | | uint64_t device_size; |
20 | | bool online; |
21 | | bool fixed_length; |
22 | | crypt_reencrypt_direction_info direction; |
23 | | crypt_reencrypt_mode_info mode; |
24 | | |
25 | | char *device_name; |
26 | | char *hotzone_name; |
27 | | char *overlay_name; |
28 | | uint32_t flags; |
29 | | |
30 | | /* reencryption window persistence attributes */ |
31 | | struct reenc_protection rp; |
32 | | struct reenc_protection rp_moved_segment; |
33 | | |
34 | | int reenc_keyslot; |
35 | | |
36 | | /* already running reencryption */ |
37 | | json_object *jobj_segs_hot; |
38 | | json_object *jobj_segs_post; |
39 | | |
40 | | /* backup segments */ |
41 | | json_object *jobj_segment_new; |
42 | | int digest_new; |
43 | | json_object *jobj_segment_old; |
44 | | int digest_old; |
45 | | json_object *jobj_segment_moved; |
46 | | |
47 | | struct volume_key *vks; |
48 | | |
49 | | void *reenc_buffer; |
50 | | ssize_t read; |
51 | | |
52 | | struct crypt_storage_wrapper *cw1; |
53 | | struct crypt_storage_wrapper *cw2; |
54 | | |
55 | | uint32_t wflags1; |
56 | | uint32_t wflags2; |
57 | | |
58 | | struct device *hotzone_device; |
59 | | |
60 | | struct crypt_lock_handle *reenc_lock; |
61 | | }; |
62 | | #if USE_LUKS2_REENCRYPTION |
63 | | static uint64_t data_shift_value(struct reenc_protection *rp) |
64 | 0 | { |
65 | 0 | return rp->type == REENC_PROTECTION_DATASHIFT ? rp->p.ds.data_shift : 0; |
66 | 0 | } |
67 | | |
68 | | static json_object *reencrypt_segment(struct luks2_hdr *hdr, unsigned new) |
69 | 0 | { |
70 | 0 | return LUKS2_get_segment_by_flag(hdr, new ? "backup-final" : "backup-previous"); |
71 | 0 | } |
72 | | |
73 | | static json_object *reencrypt_segment_new(struct luks2_hdr *hdr) |
74 | 0 | { |
75 | 0 | return reencrypt_segment(hdr, 1); |
76 | 0 | } |
77 | | |
78 | | static json_object *reencrypt_segment_old(struct luks2_hdr *hdr) |
79 | 0 | { |
80 | 0 | return reencrypt_segment(hdr, 0); |
81 | 0 | } |
82 | | |
83 | | static json_object *reencrypt_segments_old(struct luks2_hdr *hdr) |
84 | 0 | { |
85 | 0 | json_object *jobj_segments, *jobj = NULL; |
86 | |
|
87 | 0 | if (json_object_copy(reencrypt_segment_old(hdr), &jobj)) |
88 | 0 | return NULL; |
89 | | |
90 | 0 | json_segment_remove_flag(jobj, "backup-previous"); |
91 | |
|
92 | 0 | jobj_segments = json_object_new_object(); |
93 | 0 | if (!jobj_segments) { |
94 | 0 | json_object_put(jobj); |
95 | 0 | return NULL; |
96 | 0 | } |
97 | | |
98 | 0 | if (json_object_object_add_by_uint(jobj_segments, 0, jobj)) { |
99 | 0 | json_object_put(jobj); |
100 | 0 | json_object_put(jobj_segments); |
101 | 0 | return NULL; |
102 | 0 | } |
103 | | |
104 | 0 | return jobj_segments; |
105 | 0 | } |
106 | | |
107 | | static const char *reencrypt_segment_cipher_new(struct luks2_hdr *hdr) |
108 | 0 | { |
109 | 0 | return json_segment_get_cipher(reencrypt_segment(hdr, 1)); |
110 | 0 | } |
111 | | |
112 | | static const char *reencrypt_segment_cipher_old(struct luks2_hdr *hdr) |
113 | 0 | { |
114 | 0 | return json_segment_get_cipher(reencrypt_segment(hdr, 0)); |
115 | 0 | } |
116 | | |
117 | | static uint32_t reencrypt_get_sector_size_new(struct luks2_hdr *hdr) |
118 | 0 | { |
119 | 0 | return json_segment_get_sector_size(reencrypt_segment(hdr, 1)); |
120 | 0 | } |
121 | | |
122 | | static uint32_t reencrypt_get_sector_size_old(struct luks2_hdr *hdr) |
123 | 0 | { |
124 | 0 | return json_segment_get_sector_size(reencrypt_segment(hdr, 0)); |
125 | 0 | } |
126 | | |
127 | | static uint64_t reencrypt_data_offset(struct luks2_hdr *hdr, unsigned new) |
128 | 0 | { |
129 | 0 | json_object *jobj = reencrypt_segment(hdr, new); |
130 | 0 | if (jobj) |
131 | 0 | return json_segment_get_offset(jobj, 0); |
132 | | |
133 | 0 | return LUKS2_get_data_offset(hdr) << SECTOR_SHIFT; |
134 | 0 | } |
135 | | |
136 | | static uint64_t LUKS2_reencrypt_get_data_offset_moved(struct luks2_hdr *hdr) |
137 | 0 | { |
138 | 0 | json_object *jobj_segment = LUKS2_get_segment_by_flag(hdr, "backup-moved-segment"); |
139 | |
|
140 | 0 | if (!jobj_segment) |
141 | 0 | return 0; |
142 | | |
143 | 0 | return json_segment_get_offset(jobj_segment, 0); |
144 | 0 | } |
145 | | |
146 | | static uint64_t reencrypt_get_data_offset_new(struct luks2_hdr *hdr) |
147 | 0 | { |
148 | 0 | return reencrypt_data_offset(hdr, 1); |
149 | 0 | } |
150 | | |
151 | | static uint64_t reencrypt_get_data_offset_old(struct luks2_hdr *hdr) |
152 | 0 | { |
153 | 0 | return reencrypt_data_offset(hdr, 0); |
154 | 0 | } |
155 | | #endif |
156 | | |
157 | | static int reencrypt_digest(struct luks2_hdr *hdr, unsigned new) |
158 | 0 | { |
159 | 0 | int segment = LUKS2_get_segment_id_by_flag(hdr, new ? "backup-final" : "backup-previous"); |
160 | |
|
161 | 0 | if (segment < 0) |
162 | 0 | return segment; |
163 | | |
164 | 0 | return LUKS2_digest_by_segment(hdr, segment); |
165 | 0 | } |
166 | | |
167 | | int LUKS2_reencrypt_digest_new(struct luks2_hdr *hdr) |
168 | 0 | { |
169 | 0 | return reencrypt_digest(hdr, 1); |
170 | 0 | } |
171 | | |
172 | | int LUKS2_reencrypt_digest_old(struct luks2_hdr *hdr) |
173 | 0 | { |
174 | 0 | return reencrypt_digest(hdr, 0); |
175 | 0 | } |
176 | | |
177 | | int LUKS2_reencrypt_segment_new(struct luks2_hdr *hdr) |
178 | 0 | { |
179 | 0 | return LUKS2_get_segment_id_by_flag(hdr, "backup-final"); |
180 | 0 | } |
181 | | |
182 | | int LUKS2_reencrypt_segment_old(struct luks2_hdr *hdr) |
183 | 0 | { |
184 | 0 | return LUKS2_get_segment_id_by_flag(hdr, "backup-previous"); |
185 | 0 | } |
186 | | |
187 | | unsigned LUKS2_reencrypt_vks_count(struct luks2_hdr *hdr) |
188 | 0 | { |
189 | 0 | int digest_old, digest_new; |
190 | 0 | unsigned vks_count = 0; |
191 | |
|
192 | 0 | if ((digest_new = LUKS2_reencrypt_digest_new(hdr)) >= 0) |
193 | 0 | vks_count++; |
194 | 0 | if ((digest_old = LUKS2_reencrypt_digest_old(hdr)) >= 0) { |
195 | 0 | if (digest_old != digest_new) |
196 | 0 | vks_count++; |
197 | 0 | } |
198 | |
|
199 | 0 | return vks_count; |
200 | 0 | } |
201 | | |
202 | | /* none, checksums, journal or shift */ |
203 | | static const char *reencrypt_resilience_type(struct luks2_hdr *hdr) |
204 | 0 | { |
205 | 0 | json_object *jobj_keyslot, *jobj_area, *jobj_type; |
206 | 0 | int ks = LUKS2_find_keyslot(hdr, "reencrypt"); |
207 | |
|
208 | 0 | if (ks < 0) |
209 | 0 | return NULL; |
210 | | |
211 | 0 | jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, ks); |
212 | |
|
213 | 0 | json_object_object_get_ex(jobj_keyslot, "area", &jobj_area); |
214 | 0 | if (!json_object_object_get_ex(jobj_area, "type", &jobj_type)) |
215 | 0 | return NULL; |
216 | | |
217 | 0 | return json_object_get_string(jobj_type); |
218 | 0 | } |
219 | | |
220 | | static const char *reencrypt_resilience_hash(struct luks2_hdr *hdr) |
221 | 0 | { |
222 | 0 | json_object *jobj_keyslot, *jobj_area, *jobj_type, *jobj_hash; |
223 | 0 | int ks = LUKS2_find_keyslot(hdr, "reencrypt"); |
224 | |
|
225 | 0 | if (ks < 0) |
226 | 0 | return NULL; |
227 | | |
228 | 0 | jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, ks); |
229 | |
|
230 | 0 | json_object_object_get_ex(jobj_keyslot, "area", &jobj_area); |
231 | 0 | if (!json_object_object_get_ex(jobj_area, "type", &jobj_type)) |
232 | 0 | return NULL; |
233 | 0 | if (strcmp(json_object_get_string(jobj_type), "checksum")) |
234 | 0 | return NULL; |
235 | 0 | if (!json_object_object_get_ex(jobj_area, "hash", &jobj_hash)) |
236 | 0 | return NULL; |
237 | | |
238 | 0 | return json_object_get_string(jobj_hash); |
239 | 0 | } |
240 | | #if USE_LUKS2_REENCRYPTION |
241 | | static json_object *_enc_create_segments_shift_after(struct luks2_reencrypt *rh, uint64_t data_offset) |
242 | 0 | { |
243 | 0 | int reenc_seg, i = 0; |
244 | 0 | json_object *jobj, *jobj_copy = NULL, *jobj_seg_new = NULL, *jobj_segs_post = json_object_new_object(); |
245 | 0 | uint64_t tmp; |
246 | |
|
247 | 0 | if (!rh->jobj_segs_hot || !jobj_segs_post) |
248 | 0 | goto err; |
249 | | |
250 | 0 | if (json_segments_count(rh->jobj_segs_hot) == 0) |
251 | 0 | return jobj_segs_post; |
252 | | |
253 | 0 | reenc_seg = json_segments_segment_in_reencrypt(rh->jobj_segs_hot); |
254 | 0 | if (reenc_seg < 0) |
255 | 0 | goto err; |
256 | | |
257 | 0 | while (i < reenc_seg) { |
258 | 0 | jobj_copy = json_segments_get_segment(rh->jobj_segs_hot, i); |
259 | 0 | if (!jobj_copy || json_object_object_add_by_uint(jobj_segs_post, i++, json_object_get(jobj_copy))) |
260 | 0 | goto err; |
261 | 0 | } |
262 | 0 | jobj_copy = NULL; |
263 | |
|
264 | 0 | jobj = json_segments_get_segment(rh->jobj_segs_hot, reenc_seg + 1); |
265 | 0 | if (!jobj) { |
266 | 0 | jobj = json_segments_get_segment(rh->jobj_segs_hot, reenc_seg); |
267 | 0 | if (!jobj || json_object_copy(jobj, &jobj_seg_new)) |
268 | 0 | goto err; |
269 | 0 | json_segment_remove_flag(jobj_seg_new, "in-reencryption"); |
270 | 0 | tmp = rh->length; |
271 | 0 | } else { |
272 | 0 | if (json_object_copy(jobj, &jobj_seg_new)) |
273 | 0 | goto err; |
274 | 0 | json_object_object_add(jobj_seg_new, "offset", crypt_jobj_new_uint64(rh->offset + data_offset)); |
275 | 0 | json_object_object_add(jobj_seg_new, "iv_tweak", crypt_jobj_new_uint64(rh->offset >> SECTOR_SHIFT)); |
276 | 0 | tmp = json_segment_get_size(jobj_seg_new, 0) + rh->length; |
277 | 0 | } |
278 | | |
279 | | /* alter size of new segment, reenc_seg == 0 we're finished */ |
280 | 0 | json_object_object_add(jobj_seg_new, "size", reenc_seg > 0 ? crypt_jobj_new_uint64(tmp) : json_object_new_string("dynamic")); |
281 | 0 | if (!json_object_object_add_by_uint(jobj_segs_post, reenc_seg, jobj_seg_new)) |
282 | 0 | return jobj_segs_post; |
283 | | |
284 | 0 | err: |
285 | 0 | json_object_put(jobj_seg_new); |
286 | 0 | json_object_put(jobj_copy); |
287 | 0 | json_object_put(jobj_segs_post); |
288 | 0 | return NULL; |
289 | 0 | } |
290 | | |
291 | | static json_object *reencrypt_make_hot_segments_encrypt_shift(struct luks2_hdr *hdr, |
292 | | struct luks2_reencrypt *rh, |
293 | | uint64_t data_offset) |
294 | 0 | { |
295 | 0 | int sg, crypt_seg, i = 0; |
296 | 0 | uint64_t segment_size; |
297 | 0 | json_object *jobj_seg_shrunk = NULL, *jobj_seg_new = NULL, *jobj_copy = NULL, *jobj_enc_seg = NULL, |
298 | 0 | *jobj_segs_hot = json_object_new_object(); |
299 | |
|
300 | 0 | if (!jobj_segs_hot) |
301 | 0 | return NULL; |
302 | | |
303 | 0 | crypt_seg = LUKS2_segment_by_type(hdr, "crypt"); |
304 | | |
305 | | /* FIXME: This is hack. Find proper way to fix it. */ |
306 | 0 | sg = LUKS2_last_segment_by_type(hdr, "linear"); |
307 | 0 | if (rh->offset && sg < 0) |
308 | 0 | goto err; |
309 | 0 | if (sg < 0) |
310 | 0 | return jobj_segs_hot; |
311 | | |
312 | 0 | jobj_enc_seg = json_segment_create_crypt(data_offset + rh->offset, |
313 | 0 | rh->offset >> SECTOR_SHIFT, |
314 | 0 | &rh->length, |
315 | 0 | reencrypt_segment_cipher_new(hdr), |
316 | 0 | NULL, 0, /* integrity */ |
317 | 0 | reencrypt_get_sector_size_new(hdr), |
318 | 0 | 1); |
319 | |
|
320 | 0 | while (i < sg) { |
321 | 0 | jobj_copy = LUKS2_get_segment_jobj(hdr, i); |
322 | 0 | if (!jobj_copy || json_object_object_add_by_uint(jobj_segs_hot, i++, json_object_get(jobj_copy))) |
323 | 0 | goto err; |
324 | 0 | } |
325 | 0 | jobj_copy = NULL; |
326 | |
|
327 | 0 | segment_size = LUKS2_segment_size(hdr, sg, 0); |
328 | 0 | if (segment_size > rh->length) { |
329 | 0 | if (json_object_copy(LUKS2_get_segment_jobj(hdr, sg), &jobj_seg_shrunk)) |
330 | 0 | goto err; |
331 | 0 | json_object_object_add(jobj_seg_shrunk, "size", crypt_jobj_new_uint64(segment_size - rh->length)); |
332 | 0 | if (json_object_object_add_by_uint_by_ref(jobj_segs_hot, sg++, &jobj_seg_shrunk)) |
333 | 0 | goto err; |
334 | 0 | } |
335 | | |
336 | 0 | if (json_object_object_add_by_uint_by_ref(jobj_segs_hot, sg++, &jobj_enc_seg)) |
337 | 0 | goto err; |
338 | | |
339 | | /* first crypt segment after encryption ? */ |
340 | 0 | if (crypt_seg >= 0) { |
341 | 0 | jobj_seg_new = LUKS2_get_segment_jobj(hdr, crypt_seg); |
342 | 0 | if (!jobj_seg_new || json_object_object_add_by_uint(jobj_segs_hot, sg, json_object_get(jobj_seg_new))) |
343 | 0 | goto err; |
344 | 0 | } |
345 | | |
346 | 0 | return jobj_segs_hot; |
347 | 0 | err: |
348 | 0 | json_object_put(jobj_copy); |
349 | 0 | json_object_put(jobj_seg_new); |
350 | 0 | json_object_put(jobj_seg_shrunk); |
351 | 0 | json_object_put(jobj_enc_seg); |
352 | 0 | json_object_put(jobj_segs_hot); |
353 | |
|
354 | 0 | return NULL; |
355 | 0 | } |
356 | | |
357 | | static json_object *reencrypt_make_segment_new(struct crypt_device *cd, |
358 | | struct luks2_hdr *hdr, |
359 | | const struct luks2_reencrypt *rh, |
360 | | uint64_t data_offset, |
361 | | uint64_t segment_offset, |
362 | | uint64_t iv_offset, |
363 | | const uint64_t *segment_length) |
364 | 0 | { |
365 | 0 | switch (rh->mode) { |
366 | 0 | case CRYPT_REENCRYPT_REENCRYPT: |
367 | 0 | case CRYPT_REENCRYPT_ENCRYPT: |
368 | 0 | return json_segment_create_crypt(data_offset + segment_offset, |
369 | 0 | crypt_get_iv_offset(cd) + (iv_offset >> SECTOR_SHIFT), |
370 | 0 | segment_length, |
371 | 0 | reencrypt_segment_cipher_new(hdr), |
372 | 0 | NULL, 0, /* integrity */ |
373 | 0 | reencrypt_get_sector_size_new(hdr), 0); |
374 | 0 | case CRYPT_REENCRYPT_DECRYPT: |
375 | 0 | return json_segment_create_linear(data_offset + segment_offset, segment_length, 0); |
376 | 0 | } |
377 | | |
378 | 0 | return NULL; |
379 | 0 | } |
380 | | |
381 | | static json_object *reencrypt_make_post_segments_forward(struct crypt_device *cd, |
382 | | struct luks2_hdr *hdr, |
383 | | struct luks2_reencrypt *rh, |
384 | | uint64_t data_offset) |
385 | 0 | { |
386 | 0 | int reenc_seg; |
387 | 0 | json_object *jobj_old_seg, *jobj_new_seg_after = NULL, *jobj_old_seg_copy = NULL, |
388 | 0 | *jobj_segs_post = json_object_new_object(); |
389 | 0 | uint64_t fixed_length = rh->offset + rh->length; |
390 | |
|
391 | 0 | if (!rh->jobj_segs_hot || !jobj_segs_post) |
392 | 0 | goto err; |
393 | | |
394 | 0 | reenc_seg = json_segments_segment_in_reencrypt(rh->jobj_segs_hot); |
395 | 0 | if (reenc_seg < 0) |
396 | 0 | goto err; |
397 | | |
398 | 0 | jobj_old_seg = json_segments_get_segment(rh->jobj_segs_hot, reenc_seg + 1); |
399 | | |
400 | | /* |
401 | | * if there's no old segment after reencryption, we're done. |
402 | | * Set size to 'dynamic' again. |
403 | | */ |
404 | 0 | jobj_new_seg_after = reencrypt_make_segment_new(cd, hdr, rh, data_offset, 0, 0, jobj_old_seg ? &fixed_length : NULL); |
405 | 0 | if (!jobj_new_seg_after || json_object_object_add_by_uint_by_ref(jobj_segs_post, 0, &jobj_new_seg_after)) |
406 | 0 | goto err; |
407 | | |
408 | 0 | if (jobj_old_seg) { |
409 | 0 | if (rh->fixed_length) { |
410 | 0 | if (json_object_copy(jobj_old_seg, &jobj_old_seg_copy)) |
411 | 0 | goto err; |
412 | 0 | fixed_length = rh->device_size - fixed_length; |
413 | 0 | json_object_object_add(jobj_old_seg_copy, "size", crypt_jobj_new_uint64(fixed_length)); |
414 | 0 | } else |
415 | 0 | jobj_old_seg_copy = json_object_get(jobj_old_seg); |
416 | | |
417 | 0 | if (json_object_object_add_by_uint_by_ref(jobj_segs_post, 1, &jobj_old_seg_copy)) |
418 | 0 | goto err; |
419 | 0 | } |
420 | | |
421 | 0 | return jobj_segs_post; |
422 | 0 | err: |
423 | 0 | json_object_put(jobj_new_seg_after); |
424 | 0 | json_object_put(jobj_old_seg_copy); |
425 | 0 | json_object_put(jobj_segs_post); |
426 | 0 | return NULL; |
427 | 0 | } |
428 | | |
429 | | static json_object *reencrypt_make_post_segments_backward(struct crypt_device *cd, |
430 | | struct luks2_hdr *hdr, |
431 | | struct luks2_reencrypt *rh, |
432 | | uint64_t data_offset) |
433 | 0 | { |
434 | 0 | int reenc_seg; |
435 | 0 | uint64_t fixed_length; |
436 | |
|
437 | 0 | json_object *jobj_new_seg_after = NULL, *jobj_old_seg = NULL, |
438 | 0 | *jobj_segs_post = json_object_new_object(); |
439 | |
|
440 | 0 | if (!rh->jobj_segs_hot || !jobj_segs_post) |
441 | 0 | goto err; |
442 | | |
443 | 0 | reenc_seg = json_segments_segment_in_reencrypt(rh->jobj_segs_hot); |
444 | 0 | if (reenc_seg < 0) |
445 | 0 | goto err; |
446 | | |
447 | 0 | jobj_old_seg = json_segments_get_segment(rh->jobj_segs_hot, reenc_seg - 1); |
448 | 0 | if (jobj_old_seg) { |
449 | 0 | json_object_get(jobj_old_seg); |
450 | 0 | if (json_object_object_add_by_uint_by_ref(jobj_segs_post, reenc_seg - 1, &jobj_old_seg)) |
451 | 0 | goto err; |
452 | 0 | } |
453 | | |
454 | 0 | if (rh->fixed_length && rh->offset) { |
455 | 0 | fixed_length = rh->device_size - rh->offset; |
456 | 0 | jobj_new_seg_after = reencrypt_make_segment_new(cd, hdr, rh, data_offset, rh->offset, rh->offset, &fixed_length); |
457 | 0 | } else |
458 | 0 | jobj_new_seg_after = reencrypt_make_segment_new(cd, hdr, rh, data_offset, rh->offset, rh->offset, NULL); |
459 | |
|
460 | 0 | if (jobj_new_seg_after && !json_object_object_add_by_uint(jobj_segs_post, reenc_seg, jobj_new_seg_after)) |
461 | 0 | return jobj_segs_post; |
462 | 0 | err: |
463 | 0 | json_object_put(jobj_new_seg_after); |
464 | 0 | json_object_put(jobj_old_seg); |
465 | 0 | json_object_put(jobj_segs_post); |
466 | 0 | return NULL; |
467 | 0 | } |
468 | | |
469 | | static json_object *reencrypt_make_segment_reencrypt(struct crypt_device *cd, |
470 | | struct luks2_hdr *hdr, |
471 | | const struct luks2_reencrypt *rh, |
472 | | uint64_t data_offset, |
473 | | uint64_t segment_offset, |
474 | | uint64_t iv_offset, |
475 | | const uint64_t *segment_length) |
476 | 0 | { |
477 | 0 | switch (rh->mode) { |
478 | 0 | case CRYPT_REENCRYPT_REENCRYPT: |
479 | 0 | case CRYPT_REENCRYPT_ENCRYPT: |
480 | 0 | return json_segment_create_crypt(data_offset + segment_offset, |
481 | 0 | crypt_get_iv_offset(cd) + (iv_offset >> SECTOR_SHIFT), |
482 | 0 | segment_length, |
483 | 0 | reencrypt_segment_cipher_new(hdr), |
484 | 0 | NULL, 0, /* integrity */ |
485 | 0 | reencrypt_get_sector_size_new(hdr), 1); |
486 | 0 | case CRYPT_REENCRYPT_DECRYPT: |
487 | 0 | return json_segment_create_linear(data_offset + segment_offset, segment_length, 1); |
488 | 0 | } |
489 | | |
490 | 0 | return NULL; |
491 | 0 | } |
492 | | |
493 | | static json_object *reencrypt_make_segment_old(struct crypt_device *cd, |
494 | | struct luks2_hdr *hdr, |
495 | | const struct luks2_reencrypt *rh, |
496 | | uint64_t data_offset, |
497 | | uint64_t segment_offset, |
498 | | const uint64_t *segment_length) |
499 | 0 | { |
500 | 0 | json_object *jobj_old_seg = NULL; |
501 | |
|
502 | 0 | switch (rh->mode) { |
503 | 0 | case CRYPT_REENCRYPT_REENCRYPT: |
504 | 0 | case CRYPT_REENCRYPT_DECRYPT: |
505 | 0 | jobj_old_seg = json_segment_create_crypt(data_offset + segment_offset, |
506 | 0 | crypt_get_iv_offset(cd) + (segment_offset >> SECTOR_SHIFT), |
507 | 0 | segment_length, |
508 | 0 | reencrypt_segment_cipher_old(hdr), |
509 | 0 | NULL, 0, /* integrity */ |
510 | 0 | reencrypt_get_sector_size_old(hdr), |
511 | 0 | 0); |
512 | 0 | break; |
513 | 0 | case CRYPT_REENCRYPT_ENCRYPT: |
514 | 0 | jobj_old_seg = json_segment_create_linear(data_offset + segment_offset, segment_length, 0); |
515 | 0 | } |
516 | | |
517 | 0 | return jobj_old_seg; |
518 | 0 | } |
519 | | |
520 | | static json_object *reencrypt_make_hot_segments_forward(struct crypt_device *cd, |
521 | | struct luks2_hdr *hdr, |
522 | | struct luks2_reencrypt *rh, |
523 | | uint64_t device_size, |
524 | | uint64_t data_offset) |
525 | 0 | { |
526 | 0 | uint64_t fixed_length, tmp = rh->offset + rh->length; |
527 | 0 | json_object *jobj_segs_hot = json_object_new_object(), *jobj_reenc_seg = NULL, |
528 | 0 | *jobj_old_seg = NULL, *jobj_new_seg = NULL; |
529 | 0 | unsigned int sg = 0; |
530 | |
|
531 | 0 | if (!jobj_segs_hot) |
532 | 0 | return NULL; |
533 | | |
534 | 0 | if (rh->offset) { |
535 | 0 | jobj_new_seg = reencrypt_make_segment_new(cd, hdr, rh, data_offset, 0, 0, &rh->offset); |
536 | 0 | if (!jobj_new_seg || json_object_object_add_by_uint_by_ref(jobj_segs_hot, sg++, &jobj_new_seg)) |
537 | 0 | goto err; |
538 | 0 | } |
539 | | |
540 | 0 | jobj_reenc_seg = reencrypt_make_segment_reencrypt(cd, hdr, rh, data_offset, rh->offset, rh->offset, &rh->length); |
541 | 0 | if (!jobj_reenc_seg) |
542 | 0 | goto err; |
543 | | |
544 | 0 | if (json_object_object_add_by_uint_by_ref(jobj_segs_hot, sg++, &jobj_reenc_seg)) |
545 | 0 | goto err; |
546 | | |
547 | 0 | if (tmp < device_size) { |
548 | 0 | fixed_length = device_size - tmp; |
549 | 0 | jobj_old_seg = reencrypt_make_segment_old(cd, hdr, rh, data_offset + data_shift_value(&rh->rp), |
550 | 0 | rh->offset + rh->length, rh->fixed_length ? &fixed_length : NULL); |
551 | 0 | if (!jobj_old_seg || json_object_object_add_by_uint_by_ref(jobj_segs_hot, sg, &jobj_old_seg)) |
552 | 0 | goto err; |
553 | 0 | } |
554 | | |
555 | 0 | return jobj_segs_hot; |
556 | 0 | err: |
557 | 0 | json_object_put(jobj_reenc_seg); |
558 | 0 | json_object_put(jobj_old_seg); |
559 | 0 | json_object_put(jobj_new_seg); |
560 | 0 | json_object_put(jobj_segs_hot); |
561 | 0 | return NULL; |
562 | 0 | } |
563 | | |
564 | | static json_object *reencrypt_make_hot_segments_decrypt_shift(struct crypt_device *cd, |
565 | | struct luks2_hdr *hdr, struct luks2_reencrypt *rh, |
566 | | uint64_t device_size, uint64_t data_offset) |
567 | 0 | { |
568 | 0 | uint64_t fixed_length, tmp = rh->offset + rh->length, linear_length = rh->progress; |
569 | 0 | json_object *jobj, *jobj_segs_hot = json_object_new_object(), *jobj_reenc_seg = NULL, |
570 | 0 | *jobj_old_seg = NULL, *jobj_new_seg = NULL; |
571 | 0 | unsigned int sg = 0; |
572 | |
|
573 | 0 | if (!jobj_segs_hot) |
574 | 0 | return NULL; |
575 | | |
576 | 0 | if (rh->offset) { |
577 | 0 | jobj = LUKS2_get_segment_jobj(hdr, 0); |
578 | 0 | if (!jobj) |
579 | 0 | goto err; |
580 | | |
581 | 0 | jobj_new_seg = json_object_get(jobj); |
582 | 0 | if (json_object_object_add_by_uint_by_ref(jobj_segs_hot, sg++, &jobj_new_seg)) |
583 | 0 | goto err; |
584 | | |
585 | 0 | if (linear_length) { |
586 | 0 | jobj_new_seg = reencrypt_make_segment_new(cd, hdr, rh, |
587 | 0 | data_offset, |
588 | 0 | json_segment_get_size(jobj, 0), |
589 | 0 | 0, |
590 | 0 | &linear_length); |
591 | 0 | if (!jobj_new_seg || json_object_object_add_by_uint_by_ref(jobj_segs_hot, sg++, &jobj_new_seg)) |
592 | 0 | goto err; |
593 | 0 | } |
594 | 0 | } |
595 | | |
596 | 0 | jobj_reenc_seg = reencrypt_make_segment_reencrypt(cd, hdr, rh, data_offset, |
597 | 0 | rh->offset, |
598 | 0 | rh->offset, |
599 | 0 | &rh->length); |
600 | 0 | if (!jobj_reenc_seg || json_object_object_add_by_uint_by_ref(jobj_segs_hot, sg++, &jobj_reenc_seg)) |
601 | 0 | goto err; |
602 | | |
603 | 0 | if (!rh->offset && (jobj = LUKS2_get_segment_jobj(hdr, 1)) && |
604 | 0 | !json_segment_is_backup(jobj)) { |
605 | 0 | jobj_new_seg = json_object_get(jobj); |
606 | 0 | if (json_object_object_add_by_uint_by_ref(jobj_segs_hot, sg++, &jobj_new_seg)) |
607 | 0 | goto err; |
608 | 0 | } else if (tmp < device_size) { |
609 | 0 | fixed_length = device_size - tmp; |
610 | 0 | jobj_old_seg = reencrypt_make_segment_old(cd, hdr, rh, |
611 | 0 | data_offset + data_shift_value(&rh->rp), |
612 | 0 | rh->offset + rh->length, |
613 | 0 | rh->fixed_length ? &fixed_length : NULL); |
614 | 0 | if (!jobj_old_seg || json_object_object_add_by_uint_by_ref(jobj_segs_hot, sg, &jobj_old_seg)) |
615 | 0 | goto err; |
616 | 0 | } |
617 | | |
618 | 0 | return jobj_segs_hot; |
619 | 0 | err: |
620 | 0 | json_object_put(jobj_reenc_seg); |
621 | 0 | json_object_put(jobj_old_seg); |
622 | 0 | json_object_put(jobj_new_seg); |
623 | 0 | json_object_put(jobj_segs_hot); |
624 | 0 | return NULL; |
625 | 0 | } |
626 | | |
627 | | static json_object *_dec_create_segments_shift_after(struct crypt_device *cd, |
628 | | struct luks2_hdr *hdr, |
629 | | struct luks2_reencrypt *rh, |
630 | | uint64_t data_offset) |
631 | 0 | { |
632 | 0 | int reenc_seg, i = 0; |
633 | 0 | json_object *jobj_seg_old, *jobj_copy = NULL, *jobj_seg_old_copy = NULL, *jobj_seg_new = NULL, |
634 | 0 | *jobj_segs_post = json_object_new_object(); |
635 | 0 | unsigned segs; |
636 | 0 | uint64_t tmp; |
637 | |
|
638 | 0 | if (!rh->jobj_segs_hot || !jobj_segs_post) |
639 | 0 | goto err; |
640 | | |
641 | 0 | segs = json_segments_count(rh->jobj_segs_hot); |
642 | 0 | if (segs == 0) |
643 | 0 | return jobj_segs_post; |
644 | | |
645 | 0 | reenc_seg = json_segments_segment_in_reencrypt(rh->jobj_segs_hot); |
646 | 0 | if (reenc_seg < 0) |
647 | 0 | goto err; |
648 | | |
649 | 0 | if (reenc_seg == 0) { |
650 | 0 | jobj_seg_new = reencrypt_make_segment_new(cd, hdr, rh, data_offset, 0, 0, NULL); |
651 | 0 | if (!jobj_seg_new || json_object_object_add_by_uint(jobj_segs_post, 0, jobj_seg_new)) |
652 | 0 | goto err; |
653 | | |
654 | 0 | return jobj_segs_post; |
655 | 0 | } |
656 | | |
657 | 0 | jobj_copy = json_segments_get_segment(rh->jobj_segs_hot, 0); |
658 | 0 | if (!jobj_copy) |
659 | 0 | goto err; |
660 | 0 | json_object_get(jobj_copy); |
661 | 0 | if (json_object_object_add_by_uint_by_ref(jobj_segs_post, i++, &jobj_copy)) |
662 | 0 | goto err; |
663 | | |
664 | 0 | if ((jobj_seg_old = json_segments_get_segment(rh->jobj_segs_hot, reenc_seg + 1))) |
665 | 0 | jobj_seg_old_copy = json_object_get(jobj_seg_old); |
666 | |
|
667 | 0 | tmp = rh->length + rh->progress; |
668 | 0 | jobj_seg_new = reencrypt_make_segment_new(cd, hdr, rh, data_offset, |
669 | 0 | json_segment_get_size(rh->jobj_segment_moved, 0), |
670 | 0 | data_shift_value(&rh->rp), |
671 | 0 | jobj_seg_old ? &tmp : NULL); |
672 | 0 | if (!jobj_seg_new || json_object_object_add_by_uint_by_ref(jobj_segs_post, i++, &jobj_seg_new)) |
673 | 0 | goto err; |
674 | | |
675 | 0 | if (jobj_seg_old_copy && json_object_object_add_by_uint(jobj_segs_post, i, jobj_seg_old_copy)) |
676 | 0 | goto err; |
677 | | |
678 | 0 | return jobj_segs_post; |
679 | 0 | err: |
680 | 0 | json_object_put(jobj_copy); |
681 | 0 | json_object_put(jobj_seg_old_copy); |
682 | 0 | json_object_put(jobj_seg_new); |
683 | 0 | json_object_put(jobj_segs_post); |
684 | 0 | return NULL; |
685 | 0 | } |
686 | | |
687 | | static json_object *reencrypt_make_hot_segments_backward(struct crypt_device *cd, |
688 | | struct luks2_hdr *hdr, |
689 | | struct luks2_reencrypt *rh, |
690 | | uint64_t device_size, |
691 | | uint64_t data_offset) |
692 | 0 | { |
693 | 0 | uint64_t fixed_length, tmp = rh->offset + rh->length; |
694 | 0 | json_object *jobj_reenc_seg = NULL, *jobj_new_seg = NULL, *jobj_old_seg = NULL, |
695 | 0 | *jobj_segs_hot = json_object_new_object(); |
696 | 0 | int sg = 0; |
697 | |
|
698 | 0 | if (!jobj_segs_hot) |
699 | 0 | return NULL; |
700 | | |
701 | 0 | if (rh->offset) { |
702 | 0 | if (json_object_copy(LUKS2_get_segment_jobj(hdr, 0), &jobj_old_seg)) |
703 | 0 | goto err; |
704 | 0 | json_object_object_add(jobj_old_seg, "size", crypt_jobj_new_uint64(rh->offset)); |
705 | |
|
706 | 0 | if (json_object_object_add_by_uint_by_ref(jobj_segs_hot, sg++, &jobj_old_seg)) |
707 | 0 | goto err; |
708 | 0 | } |
709 | | |
710 | 0 | jobj_reenc_seg = reencrypt_make_segment_reencrypt(cd, hdr, rh, data_offset, rh->offset, rh->offset, &rh->length); |
711 | 0 | if (!jobj_reenc_seg || json_object_object_add_by_uint_by_ref(jobj_segs_hot, sg++, &jobj_reenc_seg)) |
712 | 0 | goto err; |
713 | | |
714 | 0 | if (tmp < device_size) { |
715 | 0 | fixed_length = device_size - tmp; |
716 | 0 | jobj_new_seg = reencrypt_make_segment_new(cd, hdr, rh, data_offset, rh->offset + rh->length, |
717 | 0 | rh->offset + rh->length, rh->fixed_length ? &fixed_length : NULL); |
718 | 0 | if (!jobj_new_seg || json_object_object_add_by_uint_by_ref(jobj_segs_hot, sg, &jobj_new_seg)) |
719 | 0 | goto err; |
720 | 0 | } |
721 | | |
722 | 0 | return jobj_segs_hot; |
723 | 0 | err: |
724 | 0 | json_object_put(jobj_reenc_seg); |
725 | 0 | json_object_put(jobj_new_seg); |
726 | 0 | json_object_put(jobj_old_seg); |
727 | 0 | json_object_put(jobj_segs_hot); |
728 | 0 | return NULL; |
729 | 0 | } |
730 | | |
731 | | static int reencrypt_make_hot_segments(struct crypt_device *cd, |
732 | | struct luks2_hdr *hdr, |
733 | | struct luks2_reencrypt *rh, |
734 | | uint64_t device_size, |
735 | | uint64_t data_offset) |
736 | 0 | { |
737 | 0 | rh->jobj_segs_hot = NULL; |
738 | |
|
739 | 0 | if (rh->mode == CRYPT_REENCRYPT_ENCRYPT && rh->direction == CRYPT_REENCRYPT_BACKWARD && |
740 | 0 | rh->rp.type == REENC_PROTECTION_DATASHIFT && rh->jobj_segment_moved) { |
741 | 0 | log_dbg(cd, "Calculating hot segments for encryption with data move."); |
742 | 0 | rh->jobj_segs_hot = reencrypt_make_hot_segments_encrypt_shift(hdr, rh, data_offset); |
743 | 0 | } else if (rh->mode == CRYPT_REENCRYPT_DECRYPT && rh->direction == CRYPT_REENCRYPT_FORWARD && |
744 | 0 | rh->rp.type == REENC_PROTECTION_DATASHIFT && rh->jobj_segment_moved) { |
745 | 0 | log_dbg(cd, "Calculating hot segments for decryption with data move."); |
746 | 0 | rh->jobj_segs_hot = reencrypt_make_hot_segments_decrypt_shift(cd, hdr, rh, device_size, data_offset); |
747 | 0 | } else if (rh->direction == CRYPT_REENCRYPT_FORWARD) { |
748 | 0 | log_dbg(cd, "Calculating hot segments (forward direction)."); |
749 | 0 | rh->jobj_segs_hot = reencrypt_make_hot_segments_forward(cd, hdr, rh, device_size, data_offset); |
750 | 0 | } else if (rh->direction == CRYPT_REENCRYPT_BACKWARD) { |
751 | 0 | log_dbg(cd, "Calculating hot segments (backward direction)."); |
752 | 0 | rh->jobj_segs_hot = reencrypt_make_hot_segments_backward(cd, hdr, rh, device_size, data_offset); |
753 | 0 | } |
754 | |
|
755 | 0 | return rh->jobj_segs_hot ? 0 : -EINVAL; |
756 | 0 | } |
757 | | |
758 | | static int reencrypt_make_post_segments(struct crypt_device *cd, |
759 | | struct luks2_hdr *hdr, |
760 | | struct luks2_reencrypt *rh, |
761 | | uint64_t data_offset) |
762 | 0 | { |
763 | 0 | rh->jobj_segs_post = NULL; |
764 | |
|
765 | 0 | if (rh->mode == CRYPT_REENCRYPT_ENCRYPT && rh->direction == CRYPT_REENCRYPT_BACKWARD && |
766 | 0 | rh->rp.type == REENC_PROTECTION_DATASHIFT && rh->jobj_segment_moved) { |
767 | 0 | log_dbg(cd, "Calculating post segments for encryption with data move."); |
768 | 0 | rh->jobj_segs_post = _enc_create_segments_shift_after(rh, data_offset); |
769 | 0 | } else if (rh->mode == CRYPT_REENCRYPT_DECRYPT && rh->direction == CRYPT_REENCRYPT_FORWARD && |
770 | 0 | rh->rp.type == REENC_PROTECTION_DATASHIFT && rh->jobj_segment_moved) { |
771 | 0 | log_dbg(cd, "Calculating post segments for decryption with data move."); |
772 | 0 | rh->jobj_segs_post = _dec_create_segments_shift_after(cd, hdr, rh, data_offset); |
773 | 0 | } else if (rh->direction == CRYPT_REENCRYPT_FORWARD) { |
774 | 0 | log_dbg(cd, "Calculating post segments (forward direction)."); |
775 | 0 | rh->jobj_segs_post = reencrypt_make_post_segments_forward(cd, hdr, rh, data_offset); |
776 | 0 | } else if (rh->direction == CRYPT_REENCRYPT_BACKWARD) { |
777 | 0 | log_dbg(cd, "Calculating segments (backward direction)."); |
778 | 0 | rh->jobj_segs_post = reencrypt_make_post_segments_backward(cd, hdr, rh, data_offset); |
779 | 0 | } |
780 | |
|
781 | 0 | return rh->jobj_segs_post ? 0 : -EINVAL; |
782 | 0 | } |
783 | | #endif |
784 | | |
785 | | static uint64_t reencrypt_data_shift(struct luks2_hdr *hdr) |
786 | 0 | { |
787 | 0 | json_object *jobj_keyslot, *jobj_area, *jobj_data_shift; |
788 | 0 | int ks = LUKS2_find_keyslot(hdr, "reencrypt"); |
789 | |
|
790 | 0 | if (ks < 0) |
791 | 0 | return 0; |
792 | | |
793 | 0 | jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, ks); |
794 | |
|
795 | 0 | json_object_object_get_ex(jobj_keyslot, "area", &jobj_area); |
796 | 0 | if (!json_object_object_get_ex(jobj_area, "shift_size", &jobj_data_shift)) |
797 | 0 | return 0; |
798 | | |
799 | 0 | return crypt_jobj_get_uint64(jobj_data_shift); |
800 | 0 | } |
801 | | |
802 | | static crypt_reencrypt_mode_info reencrypt_mode(struct luks2_hdr *hdr) |
803 | 0 | { |
804 | 0 | const char *mode; |
805 | 0 | crypt_reencrypt_mode_info mi = CRYPT_REENCRYPT_REENCRYPT; |
806 | 0 | json_object *jobj_keyslot, *jobj_mode; |
807 | |
|
808 | 0 | jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, LUKS2_find_keyslot(hdr, "reencrypt")); |
809 | 0 | if (!jobj_keyslot) |
810 | 0 | return mi; |
811 | | |
812 | 0 | json_object_object_get_ex(jobj_keyslot, "mode", &jobj_mode); |
813 | 0 | mode = json_object_get_string(jobj_mode); |
814 | | |
815 | | /* validation enforces allowed values */ |
816 | 0 | if (!strcmp(mode, "encrypt")) |
817 | 0 | mi = CRYPT_REENCRYPT_ENCRYPT; |
818 | 0 | else if (!strcmp(mode, "decrypt")) |
819 | 0 | mi = CRYPT_REENCRYPT_DECRYPT; |
820 | |
|
821 | 0 | return mi; |
822 | 0 | } |
823 | | |
824 | | static crypt_reencrypt_direction_info reencrypt_direction(struct luks2_hdr *hdr) |
825 | 0 | { |
826 | 0 | const char *value; |
827 | 0 | json_object *jobj_keyslot, *jobj_mode; |
828 | 0 | crypt_reencrypt_direction_info di = CRYPT_REENCRYPT_FORWARD; |
829 | |
|
830 | 0 | jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, LUKS2_find_keyslot(hdr, "reencrypt")); |
831 | 0 | if (!jobj_keyslot) |
832 | 0 | return di; |
833 | | |
834 | 0 | json_object_object_get_ex(jobj_keyslot, "direction", &jobj_mode); |
835 | 0 | value = json_object_get_string(jobj_mode); |
836 | | |
837 | | /* validation enforces allowed values */ |
838 | 0 | if (strcmp(value, "forward")) |
839 | 0 | di = CRYPT_REENCRYPT_BACKWARD; |
840 | |
|
841 | 0 | return di; |
842 | 0 | } |
843 | | |
844 | | typedef enum { REENC_OK = 0, |
845 | | /* |
846 | | * The state not requiring LUKS2 reencryption recovery. We can rollback |
847 | | * to last known safe state (hold in memory since last metadata write) |
848 | | * and teardown reencryption device stack (if used). |
849 | | * The reencryption fails but does not require recovery |
850 | | */ |
851 | | REENC_ERR_ROLLBACK_MEMORY, |
852 | | /* |
853 | | * Error while writing hotzone (short write or sync fail) or failed metadata |
854 | | * update post hotzone write. |
855 | | */ |
856 | | REENC_ERR_FATAL |
857 | | } reenc_status_t; |
858 | | |
859 | | void LUKS2_reencrypt_protection_erase(struct reenc_protection *rp) |
860 | 0 | { |
861 | 0 | if (!rp || rp->type != REENC_PROTECTION_CHECKSUM) |
862 | 0 | return; |
863 | | |
864 | 0 | if (rp->p.csum.ch) { |
865 | 0 | crypt_hash_destroy(rp->p.csum.ch); |
866 | 0 | rp->p.csum.ch = NULL; |
867 | 0 | } |
868 | |
|
869 | 0 | if (rp->p.csum.checksums) { |
870 | 0 | crypt_safe_memzero(rp->p.csum.checksums, rp->p.csum.checksums_len); |
871 | 0 | free(rp->p.csum.checksums); |
872 | 0 | rp->p.csum.checksums = NULL; |
873 | 0 | } |
874 | 0 | } |
875 | | |
876 | | void LUKS2_reencrypt_free(struct crypt_device *cd, struct luks2_reencrypt *rh) |
877 | 1.77k | { |
878 | 1.77k | if (!rh) |
879 | 1.77k | return; |
880 | | |
881 | 0 | LUKS2_reencrypt_protection_erase(&rh->rp); |
882 | 0 | LUKS2_reencrypt_protection_erase(&rh->rp_moved_segment); |
883 | |
|
884 | 0 | json_object_put(rh->jobj_segs_hot); |
885 | 0 | rh->jobj_segs_hot = NULL; |
886 | 0 | json_object_put(rh->jobj_segs_post); |
887 | 0 | rh->jobj_segs_post = NULL; |
888 | 0 | json_object_put(rh->jobj_segment_old); |
889 | 0 | rh->jobj_segment_old = NULL; |
890 | 0 | json_object_put(rh->jobj_segment_new); |
891 | 0 | rh->jobj_segment_new = NULL; |
892 | 0 | json_object_put(rh->jobj_segment_moved); |
893 | 0 | rh->jobj_segment_moved = NULL; |
894 | |
|
895 | 0 | free(rh->reenc_buffer); |
896 | 0 | rh->reenc_buffer = NULL; |
897 | 0 | crypt_storage_wrapper_destroy(rh->cw1); |
898 | 0 | rh->cw1 = NULL; |
899 | 0 | crypt_storage_wrapper_destroy(rh->cw2); |
900 | 0 | rh->cw2 = NULL; |
901 | 0 | device_free(cd, rh->hotzone_device); |
902 | 0 | rh->hotzone_device = NULL; |
903 | |
|
904 | 0 | free(rh->device_name); |
905 | 0 | free(rh->overlay_name); |
906 | 0 | free(rh->hotzone_name); |
907 | 0 | crypt_drop_uploaded_keyring_key(cd, rh->vks); |
908 | 0 | crypt_free_volume_key(rh->vks); |
909 | 0 | device_release_excl(cd, crypt_data_device(cd)); |
910 | 0 | crypt_unlock_internal(cd, rh->reenc_lock); |
911 | 0 | free(rh); |
912 | 0 | } |
913 | | |
914 | | #if USE_LUKS2_REENCRYPTION |
915 | | int LUKS2_reencrypt_max_hotzone_size(struct crypt_device *cd __attribute__((unused)), |
916 | | struct luks2_hdr *hdr, |
917 | | const struct reenc_protection *rp, |
918 | | int reencrypt_keyslot, |
919 | | uint64_t *r_length) |
920 | 0 | { |
921 | 0 | int r; |
922 | 0 | uint64_t dummy, area_length; |
923 | |
|
924 | 0 | assert(hdr); |
925 | 0 | assert(rp); |
926 | 0 | assert(r_length); |
927 | |
|
928 | 0 | if (rp->type <= REENC_PROTECTION_NONE) { |
929 | 0 | *r_length = LUKS2_REENCRYPT_MAX_HOTZONE_LENGTH; |
930 | 0 | return 0; |
931 | 0 | } |
932 | | |
933 | 0 | if (rp->type == REENC_PROTECTION_DATASHIFT) { |
934 | 0 | *r_length = rp->p.ds.data_shift; |
935 | 0 | return 0; |
936 | 0 | } |
937 | | |
938 | 0 | r = LUKS2_keyslot_area(hdr, reencrypt_keyslot, &dummy, &area_length); |
939 | 0 | if (r < 0) |
940 | 0 | return -EINVAL; |
941 | | |
942 | 0 | if (rp->type == REENC_PROTECTION_JOURNAL) { |
943 | 0 | *r_length = area_length; |
944 | 0 | return 0; |
945 | 0 | } |
946 | | |
947 | 0 | if (rp->type == REENC_PROTECTION_CHECKSUM) { |
948 | 0 | *r_length = (area_length / rp->p.csum.hash_size) * rp->p.csum.block_size; |
949 | 0 | return 0; |
950 | 0 | } |
951 | | |
952 | 0 | return -EINVAL; |
953 | 0 | } |
954 | | |
955 | | static size_t reencrypt_get_alignment(struct crypt_device *cd, |
956 | | struct luks2_hdr *hdr) |
957 | 0 | { |
958 | 0 | size_t ss, alignment = device_block_size(cd, crypt_data_device(cd)); |
959 | |
|
960 | 0 | ss = reencrypt_get_sector_size_old(hdr); |
961 | 0 | if (ss > alignment) |
962 | 0 | alignment = ss; |
963 | 0 | ss = reencrypt_get_sector_size_new(hdr); |
964 | 0 | if (ss > alignment) |
965 | 0 | alignment = ss; |
966 | |
|
967 | 0 | return alignment; |
968 | 0 | } |
969 | | |
970 | | /* returns void because it must not fail on valid LUKS2 header */ |
971 | | static void _load_backup_segments(struct luks2_hdr *hdr, |
972 | | struct luks2_reencrypt *rh) |
973 | 0 | { |
974 | 0 | int segment = LUKS2_get_segment_id_by_flag(hdr, "backup-final"); |
975 | |
|
976 | 0 | if (segment >= 0) { |
977 | 0 | rh->jobj_segment_new = json_object_get(LUKS2_get_segment_jobj(hdr, segment)); |
978 | 0 | rh->digest_new = LUKS2_digest_by_segment(hdr, segment); |
979 | 0 | } else { |
980 | 0 | rh->jobj_segment_new = NULL; |
981 | 0 | rh->digest_new = -ENOENT; |
982 | 0 | } |
983 | |
|
984 | 0 | segment = LUKS2_get_segment_id_by_flag(hdr, "backup-previous"); |
985 | 0 | if (segment >= 0) { |
986 | 0 | rh->jobj_segment_old = json_object_get(LUKS2_get_segment_jobj(hdr, segment)); |
987 | 0 | rh->digest_old = LUKS2_digest_by_segment(hdr, segment); |
988 | 0 | } else { |
989 | 0 | rh->jobj_segment_old = NULL; |
990 | 0 | rh->digest_old = -ENOENT; |
991 | 0 | } |
992 | |
|
993 | 0 | segment = LUKS2_get_segment_id_by_flag(hdr, "backup-moved-segment"); |
994 | 0 | if (segment >= 0) |
995 | 0 | rh->jobj_segment_moved = json_object_get(LUKS2_get_segment_jobj(hdr, segment)); |
996 | 0 | else |
997 | 0 | rh->jobj_segment_moved = NULL; |
998 | 0 | } |
999 | | |
1000 | | static int reencrypt_offset_backward_moved(struct luks2_hdr *hdr, json_object *jobj_segments, |
1001 | | uint64_t *reencrypt_length, uint64_t data_shift, uint64_t *offset) |
1002 | 0 | { |
1003 | 0 | uint64_t tmp, linear_length = 0; |
1004 | 0 | int sg, segs = json_segments_count(jobj_segments); |
1005 | | |
1006 | | /* find reencrypt offset with data shift */ |
1007 | 0 | for (sg = 0; sg < segs; sg++) |
1008 | 0 | if (LUKS2_segment_is_type(hdr, sg, "linear")) |
1009 | 0 | linear_length += LUKS2_segment_size(hdr, sg, 0); |
1010 | | |
1011 | | /* all active linear segments length */ |
1012 | 0 | if (linear_length && segs > 1) { |
1013 | 0 | if (linear_length < data_shift) |
1014 | 0 | return -EINVAL; |
1015 | 0 | tmp = linear_length - data_shift; |
1016 | 0 | if (tmp && tmp < data_shift) { |
1017 | 0 | *offset = data_shift; |
1018 | 0 | *reencrypt_length = tmp; |
1019 | 0 | } else |
1020 | 0 | *offset = tmp; |
1021 | 0 | return 0; |
1022 | 0 | } |
1023 | | |
1024 | 0 | if (segs == 1) { |
1025 | 0 | *offset = 0; |
1026 | 0 | return 0; |
1027 | 0 | } |
1028 | | |
1029 | | /* should be unreachable */ |
1030 | | |
1031 | 0 | return -EINVAL; |
1032 | 0 | } |
1033 | | |
1034 | | static int reencrypt_offset_forward_moved(struct luks2_hdr *hdr, |
1035 | | uint64_t data_shift, |
1036 | | uint64_t *offset) |
1037 | 0 | { |
1038 | 0 | int last_crypt = LUKS2_last_segment_by_type(hdr, "crypt"); |
1039 | | |
1040 | | /* if last crypt segment exists and it's first one, just return offset = 0 */ |
1041 | 0 | if (last_crypt <= 0) { |
1042 | 0 | *offset = 0; |
1043 | 0 | return 0; |
1044 | 0 | } |
1045 | | |
1046 | 0 | *offset = LUKS2_segment_offset(hdr, last_crypt, 0) - data_shift; |
1047 | 0 | return 0; |
1048 | 0 | } |
1049 | | |
1050 | | static int _offset_forward(json_object *jobj_segments, uint64_t *offset) |
1051 | 0 | { |
1052 | 0 | int segs = json_segments_count(jobj_segments); |
1053 | |
|
1054 | 0 | if (segs == 1) |
1055 | 0 | *offset = 0; |
1056 | 0 | else if (segs == 2) { |
1057 | 0 | *offset = json_segment_get_size(json_segments_get_segment(jobj_segments, 0), 0); |
1058 | 0 | if (!*offset) |
1059 | 0 | return -EINVAL; |
1060 | 0 | } else |
1061 | 0 | return -EINVAL; |
1062 | | |
1063 | 0 | return 0; |
1064 | 0 | } |
1065 | | |
1066 | | static int _offset_backward(json_object *jobj_segments, uint64_t device_size, uint64_t *length, uint64_t *offset) |
1067 | 0 | { |
1068 | 0 | int segs = json_segments_count(jobj_segments); |
1069 | 0 | uint64_t tmp; |
1070 | |
|
1071 | 0 | if (segs == 1) { |
1072 | 0 | if (device_size < *length) |
1073 | 0 | *length = device_size; |
1074 | 0 | *offset = device_size - *length; |
1075 | 0 | } else if (segs == 2) { |
1076 | 0 | tmp = json_segment_get_size(json_segments_get_segment(jobj_segments, 0), 0); |
1077 | 0 | if (tmp < *length) |
1078 | 0 | *length = tmp; |
1079 | 0 | *offset = tmp - *length; |
1080 | 0 | } else |
1081 | 0 | return -EINVAL; |
1082 | | |
1083 | 0 | return 0; |
1084 | 0 | } |
1085 | | |
1086 | | /* must be always relative to data offset */ |
1087 | | /* the LUKS2 header MUST be valid */ |
1088 | | static int reencrypt_offset(struct luks2_hdr *hdr, |
1089 | | crypt_reencrypt_direction_info di, |
1090 | | uint64_t device_size, |
1091 | | uint64_t *reencrypt_length, |
1092 | | uint64_t *offset) |
1093 | 0 | { |
1094 | 0 | int r, sg; |
1095 | 0 | json_object *jobj_segments; |
1096 | 0 | uint64_t data_shift = reencrypt_data_shift(hdr); |
1097 | |
|
1098 | 0 | if (!offset) |
1099 | 0 | return -EINVAL; |
1100 | | |
1101 | | /* if there's segment in reencryption return directly offset of it */ |
1102 | 0 | json_object_object_get_ex(hdr->jobj, "segments", &jobj_segments); |
1103 | 0 | sg = json_segments_segment_in_reencrypt(jobj_segments); |
1104 | 0 | if (sg >= 0) { |
1105 | 0 | *offset = LUKS2_segment_offset(hdr, sg, 0) - (reencrypt_get_data_offset_new(hdr)); |
1106 | 0 | return 0; |
1107 | 0 | } |
1108 | | |
1109 | 0 | if (di == CRYPT_REENCRYPT_FORWARD) { |
1110 | 0 | if (reencrypt_mode(hdr) == CRYPT_REENCRYPT_DECRYPT && |
1111 | 0 | LUKS2_get_segment_id_by_flag(hdr, "backup-moved-segment") >= 0) { |
1112 | 0 | r = reencrypt_offset_forward_moved(hdr, data_shift, offset); |
1113 | 0 | if (!r && *offset > device_size) |
1114 | 0 | *offset = device_size; |
1115 | 0 | return r; |
1116 | 0 | } |
1117 | 0 | return _offset_forward(jobj_segments, offset); |
1118 | 0 | } else if (di == CRYPT_REENCRYPT_BACKWARD) { |
1119 | 0 | if (reencrypt_mode(hdr) == CRYPT_REENCRYPT_ENCRYPT && |
1120 | 0 | LUKS2_get_segment_id_by_flag(hdr, "backup-moved-segment") >= 0) |
1121 | 0 | return reencrypt_offset_backward_moved(hdr, jobj_segments, reencrypt_length, data_shift, offset); |
1122 | 0 | return _offset_backward(jobj_segments, device_size, reencrypt_length, offset); |
1123 | 0 | } |
1124 | | |
1125 | 0 | return -EINVAL; |
1126 | 0 | } |
1127 | | |
1128 | | static uint64_t reencrypt_length(struct crypt_device *cd, |
1129 | | struct reenc_protection *rp, |
1130 | | uint64_t keyslot_area_length, |
1131 | | uint64_t length_max, |
1132 | | size_t alignment) |
1133 | 0 | { |
1134 | 0 | unsigned long dummy, optimal_alignment; |
1135 | 0 | uint64_t length, soft_mem_limit; |
1136 | |
|
1137 | 0 | if (rp->type == REENC_PROTECTION_NONE) |
1138 | 0 | length = length_max ?: LUKS2_DEFAULT_NONE_REENCRYPTION_LENGTH; |
1139 | 0 | else if (rp->type == REENC_PROTECTION_CHECKSUM) |
1140 | 0 | length = (keyslot_area_length / rp->p.csum.hash_size) * rp->p.csum.block_size; |
1141 | 0 | else if (rp->type == REENC_PROTECTION_DATASHIFT) |
1142 | 0 | return rp->p.ds.data_shift; |
1143 | 0 | else |
1144 | 0 | length = keyslot_area_length; |
1145 | | |
1146 | | /* hard limit */ |
1147 | 0 | if (length > LUKS2_REENCRYPT_MAX_HOTZONE_LENGTH) |
1148 | 0 | length = LUKS2_REENCRYPT_MAX_HOTZONE_LENGTH; |
1149 | | |
1150 | | /* soft limit is 1/4 of system memory */ |
1151 | 0 | soft_mem_limit = crypt_getphysmemory_kb() << 8; /* multiply by (1024/4) */ |
1152 | |
|
1153 | 0 | if (soft_mem_limit && length > soft_mem_limit) |
1154 | 0 | length = soft_mem_limit; |
1155 | |
|
1156 | 0 | if (length_max && length > length_max) |
1157 | 0 | length = length_max; |
1158 | |
|
1159 | 0 | length -= (length % alignment); |
1160 | | |
1161 | | /* Emits error later */ |
1162 | 0 | if (!length) |
1163 | 0 | return length; |
1164 | | |
1165 | 0 | device_topology_alignment(cd, crypt_data_device(cd), &optimal_alignment, &dummy, length); |
1166 | | |
1167 | | /* we have to stick with encryption sector size alignment */ |
1168 | 0 | if (optimal_alignment % alignment) |
1169 | 0 | return length; |
1170 | | |
1171 | | /* align to opt-io size only if remaining size allows it */ |
1172 | 0 | if (length > optimal_alignment) |
1173 | 0 | length -= (length % optimal_alignment); |
1174 | |
|
1175 | 0 | return length; |
1176 | 0 | } |
1177 | | |
1178 | | static int reencrypt_context_init(struct crypt_device *cd, |
1179 | | struct luks2_hdr *hdr, |
1180 | | struct luks2_reencrypt *rh, |
1181 | | uint64_t device_size, |
1182 | | uint64_t max_hotzone_size, |
1183 | | uint64_t fixed_device_size) |
1184 | 0 | { |
1185 | 0 | int r; |
1186 | 0 | size_t alignment; |
1187 | 0 | uint64_t dummy, area_length; |
1188 | |
|
1189 | 0 | rh->reenc_keyslot = LUKS2_find_keyslot(hdr, "reencrypt"); |
1190 | 0 | if (rh->reenc_keyslot < 0) |
1191 | 0 | return -EINVAL; |
1192 | 0 | if (LUKS2_keyslot_area(hdr, rh->reenc_keyslot, &dummy, &area_length) < 0) |
1193 | 0 | return -EINVAL; |
1194 | | |
1195 | 0 | rh->mode = reencrypt_mode(hdr); |
1196 | |
|
1197 | 0 | rh->direction = reencrypt_direction(hdr); |
1198 | |
|
1199 | 0 | r = LUKS2_keyslot_reencrypt_load(cd, hdr, rh->reenc_keyslot, &rh->rp, true); |
1200 | 0 | if (r < 0) |
1201 | 0 | return r; |
1202 | | |
1203 | 0 | if (rh->rp.type == REENC_PROTECTION_CHECKSUM) |
1204 | 0 | alignment = rh->rp.p.csum.block_size; |
1205 | 0 | else |
1206 | 0 | alignment = reencrypt_get_alignment(cd, hdr); |
1207 | |
|
1208 | 0 | if (!alignment) |
1209 | 0 | return -EINVAL; |
1210 | | |
1211 | 0 | if ((max_hotzone_size << SECTOR_SHIFT) % alignment) { |
1212 | 0 | log_err(cd, _("Hotzone size must be multiple of calculated zone alignment (%zu bytes)."), alignment); |
1213 | 0 | return -EINVAL; |
1214 | 0 | } |
1215 | | |
1216 | 0 | if ((fixed_device_size << SECTOR_SHIFT) % alignment) { |
1217 | 0 | log_err(cd, _("Device size must be multiple of calculated zone alignment (%zu bytes)."), alignment); |
1218 | 0 | return -EINVAL; |
1219 | 0 | } |
1220 | | |
1221 | 0 | if (fixed_device_size) { |
1222 | 0 | log_dbg(cd, "Switching reencryption to fixed size mode."); |
1223 | 0 | device_size = fixed_device_size << SECTOR_SHIFT; |
1224 | 0 | rh->fixed_length = true; |
1225 | 0 | } else |
1226 | 0 | rh->fixed_length = false; |
1227 | |
|
1228 | 0 | rh->length = reencrypt_length(cd, &rh->rp, area_length, max_hotzone_size << SECTOR_SHIFT, alignment); |
1229 | 0 | if (!rh->length) { |
1230 | 0 | log_dbg(cd, "Invalid reencryption length."); |
1231 | 0 | return -EINVAL; |
1232 | 0 | } |
1233 | | |
1234 | 0 | if (reencrypt_offset(hdr, rh->direction, device_size, &rh->length, &rh->offset)) { |
1235 | 0 | log_dbg(cd, "Failed to get reencryption offset."); |
1236 | 0 | return -EINVAL; |
1237 | 0 | } |
1238 | | |
1239 | 0 | if (rh->offset > device_size) |
1240 | 0 | return -EINVAL; |
1241 | 0 | if (rh->length > device_size - rh->offset) |
1242 | 0 | rh->length = device_size - rh->offset; |
1243 | |
|
1244 | 0 | _load_backup_segments(hdr, rh); |
1245 | |
|
1246 | 0 | r = LUKS2_keyslot_reencrypt_load(cd, hdr, rh->reenc_keyslot, &rh->rp_moved_segment, false); |
1247 | 0 | if (r < 0) |
1248 | 0 | return r; |
1249 | | |
1250 | 0 | if (rh->rp_moved_segment.type == REENC_PROTECTION_NOT_SET) |
1251 | 0 | log_dbg(cd, "No moved segment resilience configured."); |
1252 | |
|
1253 | 0 | if (rh->direction == CRYPT_REENCRYPT_BACKWARD) |
1254 | 0 | rh->progress = device_size - rh->offset - rh->length; |
1255 | 0 | else if (rh->jobj_segment_moved && rh->direction == CRYPT_REENCRYPT_FORWARD) { |
1256 | 0 | if (rh->offset == json_segment_get_offset(LUKS2_get_segment_by_flag(hdr, "backup-moved-segment"), false)) |
1257 | 0 | rh->progress = device_size - json_segment_get_size(LUKS2_get_segment_by_flag(hdr, "backup-moved-segment"), false); |
1258 | 0 | else |
1259 | 0 | rh->progress = rh->offset - json_segment_get_size(rh->jobj_segment_moved, 0); |
1260 | 0 | } else |
1261 | 0 | rh->progress = rh->offset; |
1262 | |
|
1263 | 0 | log_dbg(cd, "reencrypt-direction: %s", rh->direction == CRYPT_REENCRYPT_FORWARD ? "forward" : "backward"); |
1264 | 0 | log_dbg(cd, "backup-previous digest id: %d", rh->digest_old); |
1265 | 0 | log_dbg(cd, "backup-final digest id: %d", rh->digest_new); |
1266 | 0 | log_dbg(cd, "reencrypt length: %" PRIu64, rh->length); |
1267 | 0 | log_dbg(cd, "reencrypt offset: %" PRIu64, rh->offset); |
1268 | 0 | log_dbg(cd, "reencrypt shift: %s%" PRIu64, |
1269 | 0 | (rh->rp.type == REENC_PROTECTION_DATASHIFT && rh->direction == CRYPT_REENCRYPT_BACKWARD ? "-" : ""), |
1270 | 0 | data_shift_value(&rh->rp)); |
1271 | 0 | log_dbg(cd, "reencrypt alignment: %zu", alignment); |
1272 | 0 | log_dbg(cd, "reencrypt progress: %" PRIu64, rh->progress); |
1273 | |
|
1274 | 0 | rh->device_size = device_size; |
1275 | |
|
1276 | 0 | return rh->length < 512 ? -EINVAL : 0; |
1277 | 0 | } |
1278 | | |
1279 | | static size_t reencrypt_buffer_length(struct luks2_reencrypt *rh) |
1280 | 0 | { |
1281 | 0 | if (rh->rp.type == REENC_PROTECTION_DATASHIFT) |
1282 | 0 | return data_shift_value(&rh->rp); |
1283 | 0 | return rh->length; |
1284 | 0 | } |
1285 | | |
1286 | | static int reencrypt_load_clean(struct crypt_device *cd, |
1287 | | struct luks2_hdr *hdr, |
1288 | | uint64_t device_size, |
1289 | | uint64_t max_hotzone_size, |
1290 | | uint64_t fixed_device_size, |
1291 | | struct luks2_reencrypt **rh) |
1292 | 0 | { |
1293 | 0 | int r; |
1294 | 0 | struct luks2_reencrypt *tmp = crypt_zalloc(sizeof (*tmp)); |
1295 | |
|
1296 | 0 | if (!tmp) |
1297 | 0 | return -ENOMEM; |
1298 | | |
1299 | 0 | log_dbg(cd, "Loading stored reencryption context."); |
1300 | |
|
1301 | 0 | r = reencrypt_context_init(cd, hdr, tmp, device_size, max_hotzone_size, fixed_device_size); |
1302 | 0 | if (r) |
1303 | 0 | goto err; |
1304 | | |
1305 | 0 | if (posix_memalign(&tmp->reenc_buffer, device_alignment(crypt_data_device(cd)), |
1306 | 0 | reencrypt_buffer_length(tmp))) { |
1307 | 0 | r = -ENOMEM; |
1308 | 0 | goto err; |
1309 | 0 | } |
1310 | | |
1311 | 0 | *rh = tmp; |
1312 | |
|
1313 | 0 | return 0; |
1314 | 0 | err: |
1315 | 0 | LUKS2_reencrypt_free(cd, tmp); |
1316 | |
|
1317 | 0 | return r; |
1318 | 0 | } |
1319 | | |
1320 | | static int reencrypt_make_segments(struct crypt_device *cd, |
1321 | | struct luks2_hdr *hdr, |
1322 | | struct luks2_reencrypt *rh, |
1323 | | uint64_t device_size) |
1324 | 0 | { |
1325 | 0 | int r; |
1326 | 0 | uint64_t data_offset = reencrypt_get_data_offset_new(hdr); |
1327 | |
|
1328 | 0 | log_dbg(cd, "Calculating segments."); |
1329 | |
|
1330 | 0 | r = reencrypt_make_hot_segments(cd, hdr, rh, device_size, data_offset); |
1331 | 0 | if (!r) { |
1332 | 0 | r = reencrypt_make_post_segments(cd, hdr, rh, data_offset); |
1333 | 0 | if (r) |
1334 | 0 | json_object_put(rh->jobj_segs_hot); |
1335 | 0 | } |
1336 | |
|
1337 | 0 | if (r) |
1338 | 0 | log_dbg(cd, "Failed to make reencryption segments."); |
1339 | |
|
1340 | 0 | return r; |
1341 | 0 | } |
1342 | | |
1343 | | static int reencrypt_make_segments_crashed(struct crypt_device *cd, |
1344 | | struct luks2_hdr *hdr, |
1345 | | struct luks2_reencrypt *rh) |
1346 | 0 | { |
1347 | 0 | int r; |
1348 | 0 | uint64_t data_offset = crypt_get_data_offset(cd) << SECTOR_SHIFT; |
1349 | |
|
1350 | 0 | if (!rh) |
1351 | 0 | return -EINVAL; |
1352 | | |
1353 | 0 | rh->jobj_segs_hot = json_object_new_object(); |
1354 | 0 | if (!rh->jobj_segs_hot) |
1355 | 0 | return -ENOMEM; |
1356 | | |
1357 | 0 | json_object_object_foreach(LUKS2_get_segments_jobj(hdr), key, val) { |
1358 | 0 | if (json_segment_is_backup(val)) |
1359 | 0 | continue; |
1360 | 0 | json_object_object_add(rh->jobj_segs_hot, key, json_object_get(val)); |
1361 | 0 | } |
1362 | |
|
1363 | 0 | r = reencrypt_make_post_segments(cd, hdr, rh, data_offset); |
1364 | 0 | if (r) { |
1365 | 0 | json_object_put(rh->jobj_segs_hot); |
1366 | 0 | rh->jobj_segs_hot = NULL; |
1367 | 0 | } |
1368 | |
|
1369 | 0 | return r; |
1370 | 0 | } |
1371 | | |
1372 | | static int reencrypt_load_crashed(struct crypt_device *cd, |
1373 | | struct luks2_hdr *hdr, uint64_t device_size, struct luks2_reencrypt **rh) |
1374 | 0 | { |
1375 | 0 | bool dynamic; |
1376 | 0 | uint64_t required_device_size; |
1377 | 0 | int r, reenc_seg; |
1378 | |
|
1379 | 0 | if (LUKS2_get_data_size(hdr, &required_device_size, &dynamic)) |
1380 | 0 | return -EINVAL; |
1381 | | |
1382 | 0 | if (dynamic) |
1383 | 0 | required_device_size = 0; |
1384 | 0 | else |
1385 | 0 | required_device_size >>= SECTOR_SHIFT; |
1386 | |
|
1387 | 0 | r = reencrypt_load_clean(cd, hdr, device_size, 0, required_device_size, rh); |
1388 | |
|
1389 | 0 | if (!r) { |
1390 | 0 | reenc_seg = json_segments_segment_in_reencrypt(LUKS2_get_segments_jobj(hdr)); |
1391 | 0 | if (reenc_seg < 0) |
1392 | 0 | r = -EINVAL; |
1393 | 0 | else |
1394 | 0 | (*rh)->length = LUKS2_segment_size(hdr, reenc_seg, 0); |
1395 | 0 | } |
1396 | |
|
1397 | 0 | if (!r) |
1398 | 0 | r = reencrypt_make_segments_crashed(cd, hdr, *rh); |
1399 | |
|
1400 | 0 | if (r) { |
1401 | 0 | LUKS2_reencrypt_free(cd, *rh); |
1402 | 0 | *rh = NULL; |
1403 | 0 | } |
1404 | 0 | return r; |
1405 | 0 | } |
1406 | | |
1407 | | static int reencrypt_init_storage_wrappers(struct crypt_device *cd, |
1408 | | struct luks2_hdr *hdr, |
1409 | | struct luks2_reencrypt *rh, |
1410 | | struct volume_key *vks) |
1411 | 0 | { |
1412 | 0 | int r; |
1413 | 0 | struct volume_key *vk; |
1414 | 0 | uint32_t wrapper_flags = (getuid() || geteuid()) ? 0 : CSW_DISABLE_KCAPI; |
1415 | |
|
1416 | 0 | vk = crypt_volume_key_by_id(vks, rh->digest_old); |
1417 | 0 | r = crypt_storage_wrapper_init(cd, &rh->cw1, crypt_data_device(cd), |
1418 | 0 | reencrypt_get_data_offset_old(hdr), |
1419 | 0 | crypt_get_iv_offset(cd), |
1420 | 0 | reencrypt_get_sector_size_old(hdr), |
1421 | 0 | reencrypt_segment_cipher_old(hdr), |
1422 | 0 | vk, wrapper_flags | CSW_OPEN_READONLY); |
1423 | 0 | if (r) { |
1424 | 0 | log_err(cd, _("Failed to initialize old segment storage wrapper.")); |
1425 | 0 | return r; |
1426 | 0 | } |
1427 | 0 | rh->wflags1 = wrapper_flags | CSW_OPEN_READONLY; |
1428 | 0 | log_dbg(cd, "Old cipher storage wrapper type: %d.", crypt_storage_wrapper_get_type(rh->cw1)); |
1429 | |
|
1430 | 0 | vk = crypt_volume_key_by_id(vks, rh->digest_new); |
1431 | 0 | r = crypt_storage_wrapper_init(cd, &rh->cw2, crypt_data_device(cd), |
1432 | 0 | reencrypt_get_data_offset_new(hdr), |
1433 | 0 | crypt_get_iv_offset(cd), |
1434 | 0 | reencrypt_get_sector_size_new(hdr), |
1435 | 0 | reencrypt_segment_cipher_new(hdr), |
1436 | 0 | vk, wrapper_flags); |
1437 | 0 | if (r) { |
1438 | 0 | log_err(cd, _("Failed to initialize new segment storage wrapper.")); |
1439 | 0 | return r; |
1440 | 0 | } |
1441 | 0 | rh->wflags2 = wrapper_flags; |
1442 | 0 | log_dbg(cd, "New cipher storage wrapper type: %d", crypt_storage_wrapper_get_type(rh->cw2)); |
1443 | |
|
1444 | 0 | return 0; |
1445 | 0 | } |
1446 | | |
1447 | | static int reencrypt_context_set_names(struct luks2_reencrypt *rh, const char *name) |
1448 | 0 | { |
1449 | 0 | if (!rh || !name) |
1450 | 0 | return -EINVAL; |
1451 | | |
1452 | 0 | if (*name == '/') { |
1453 | 0 | if (!(rh->device_name = dm_device_name(name))) |
1454 | 0 | return -EINVAL; |
1455 | 0 | } else if (!(rh->device_name = strdup(name))) |
1456 | 0 | return -ENOMEM; |
1457 | | |
1458 | 0 | if (asprintf(&rh->hotzone_name, "%s-hotzone-%s", rh->device_name, |
1459 | 0 | rh->direction == CRYPT_REENCRYPT_FORWARD ? "forward" : "backward") < 0) { |
1460 | 0 | rh->hotzone_name = NULL; |
1461 | 0 | return -ENOMEM; |
1462 | 0 | } |
1463 | 0 | if (asprintf(&rh->overlay_name, "%s-overlay", rh->device_name) < 0) { |
1464 | 0 | rh->overlay_name = NULL; |
1465 | 0 | return -ENOMEM; |
1466 | 0 | } |
1467 | | |
1468 | 0 | rh->online = true; |
1469 | 0 | return 0; |
1470 | 0 | } |
1471 | | |
1472 | | static int modify_offset(uint64_t *offset, uint64_t data_shift, crypt_reencrypt_direction_info di) |
1473 | 0 | { |
1474 | 0 | int r = -EINVAL; |
1475 | |
|
1476 | 0 | if (!offset) |
1477 | 0 | return r; |
1478 | | |
1479 | 0 | if (di == CRYPT_REENCRYPT_FORWARD) { |
1480 | 0 | if (*offset >= data_shift) { |
1481 | 0 | *offset -= data_shift; |
1482 | 0 | r = 0; |
1483 | 0 | } |
1484 | 0 | } else if (di == CRYPT_REENCRYPT_BACKWARD) { |
1485 | 0 | *offset += data_shift; |
1486 | 0 | r = 0; |
1487 | 0 | } |
1488 | |
|
1489 | 0 | return r; |
1490 | 0 | } |
1491 | | |
1492 | | static int reencrypt_update_flag(struct crypt_device *cd, uint8_t version, |
1493 | | bool enable, bool commit) |
1494 | 0 | { |
1495 | 0 | uint32_t reqs; |
1496 | 0 | struct luks2_hdr *hdr = crypt_get_hdr(cd, CRYPT_LUKS2); |
1497 | |
|
1498 | 0 | if (enable) { |
1499 | 0 | log_dbg(cd, "Going to store reencryption requirement flag (version: %u).", version); |
1500 | 0 | return LUKS2_config_set_requirement_version(cd, hdr, CRYPT_REQUIREMENT_ONLINE_REENCRYPT, version, commit); |
1501 | 0 | } |
1502 | | |
1503 | 0 | LUKS2_config_get_requirements(cd, hdr, &reqs); |
1504 | |
|
1505 | 0 | reqs &= ~CRYPT_REQUIREMENT_ONLINE_REENCRYPT; |
1506 | |
|
1507 | 0 | log_dbg(cd, "Going to wipe reencryption requirement flag."); |
1508 | |
|
1509 | 0 | return LUKS2_config_set_requirements(cd, hdr, reqs, commit); |
1510 | 0 | } |
1511 | | |
1512 | | static int reencrypt_hotzone_protect_ready(struct crypt_device *cd, |
1513 | | struct reenc_protection *rp) |
1514 | 0 | { |
1515 | 0 | assert(rp); |
1516 | |
|
1517 | 0 | if (rp->type == REENC_PROTECTION_NOT_SET) |
1518 | 0 | return -EINVAL; |
1519 | | |
1520 | 0 | if (rp->type != REENC_PROTECTION_CHECKSUM) |
1521 | 0 | return 0; |
1522 | | |
1523 | 0 | if (!rp->p.csum.checksums) { |
1524 | 0 | log_dbg(cd, "Allocating buffer for storing resilience checksums."); |
1525 | 0 | if (posix_memalign(&rp->p.csum.checksums, device_alignment(crypt_metadata_device(cd)), |
1526 | 0 | rp->p.csum.checksums_len)) |
1527 | 0 | return -ENOMEM; |
1528 | 0 | } |
1529 | | |
1530 | 0 | return 0; |
1531 | 0 | } |
1532 | | |
1533 | | static int reencrypt_recover_segment(struct crypt_device *cd, |
1534 | | struct luks2_hdr *hdr, |
1535 | | struct luks2_reencrypt *rh, |
1536 | | struct volume_key *vks) |
1537 | 0 | { |
1538 | 0 | struct volume_key *vk_old, *vk_new; |
1539 | 0 | size_t count, s; |
1540 | 0 | ssize_t read, w; |
1541 | 0 | struct reenc_protection *rp; |
1542 | 0 | int devfd, r, new_sector_size, old_sector_size, rseg; |
1543 | 0 | uint64_t area_offset, area_length, area_length_read, crash_iv_offset, |
1544 | 0 | data_offset = crypt_get_data_offset(cd) << SECTOR_SHIFT; |
1545 | 0 | char *checksum_tmp = NULL, *data_buffer = NULL; |
1546 | 0 | struct crypt_storage_wrapper *cw1 = NULL, *cw2 = NULL; |
1547 | |
|
1548 | 0 | assert(hdr); |
1549 | 0 | assert(rh); |
1550 | 0 | assert(vks); |
1551 | |
|
1552 | 0 | rseg = json_segments_segment_in_reencrypt(rh->jobj_segs_hot); |
1553 | 0 | if (rh->offset == 0 && rh->rp_moved_segment.type > REENC_PROTECTION_NOT_SET) { |
1554 | 0 | log_dbg(cd, "Recovery using moved segment protection."); |
1555 | 0 | rp = &rh->rp_moved_segment; |
1556 | 0 | } else |
1557 | 0 | rp = &rh->rp; |
1558 | |
|
1559 | 0 | if (rseg < 0 || rh->length < 512) |
1560 | 0 | return -EINVAL; |
1561 | | |
1562 | 0 | r = reencrypt_hotzone_protect_ready(cd, rp); |
1563 | 0 | if (r) { |
1564 | 0 | log_err(cd, _("Failed to initialize hotzone protection.")); |
1565 | 0 | return -EINVAL; |
1566 | 0 | } |
1567 | | |
1568 | 0 | vk_new = crypt_volume_key_by_id(vks, rh->digest_new); |
1569 | 0 | if (!vk_new && rh->mode != CRYPT_REENCRYPT_DECRYPT) |
1570 | 0 | return -EINVAL; |
1571 | 0 | vk_old = crypt_volume_key_by_id(vks, rh->digest_old); |
1572 | 0 | if (!vk_old && rh->mode != CRYPT_REENCRYPT_ENCRYPT) |
1573 | 0 | return -EINVAL; |
1574 | 0 | old_sector_size = json_segment_get_sector_size(reencrypt_segment_old(hdr)); |
1575 | 0 | new_sector_size = json_segment_get_sector_size(reencrypt_segment_new(hdr)); |
1576 | 0 | if (rh->mode == CRYPT_REENCRYPT_DECRYPT) |
1577 | 0 | crash_iv_offset = rh->offset >> SECTOR_SHIFT; /* TODO: + old iv_tweak */ |
1578 | 0 | else |
1579 | 0 | crash_iv_offset = json_segment_get_iv_offset(json_segments_get_segment(rh->jobj_segs_hot, rseg)); |
1580 | |
|
1581 | 0 | log_dbg(cd, "crash_offset: %" PRIu64 ", crash_length: %" PRIu64 ", crash_iv_offset: %" PRIu64, |
1582 | 0 | data_offset + rh->offset, rh->length, crash_iv_offset); |
1583 | |
|
1584 | 0 | r = crypt_storage_wrapper_init(cd, &cw2, crypt_data_device(cd), |
1585 | 0 | data_offset + rh->offset, crash_iv_offset, new_sector_size, |
1586 | 0 | reencrypt_segment_cipher_new(hdr), vk_new, 0); |
1587 | 0 | if (r) { |
1588 | 0 | log_err(cd, _("Failed to initialize new segment storage wrapper.")); |
1589 | 0 | return r; |
1590 | 0 | } |
1591 | | |
1592 | 0 | if (LUKS2_keyslot_area(hdr, rh->reenc_keyslot, &area_offset, &area_length)) { |
1593 | 0 | r = -EINVAL; |
1594 | 0 | goto out; |
1595 | 0 | } |
1596 | | |
1597 | 0 | if (posix_memalign((void**)&data_buffer, device_alignment(crypt_data_device(cd)), rh->length)) { |
1598 | 0 | r = -ENOMEM; |
1599 | 0 | goto out; |
1600 | 0 | } |
1601 | | |
1602 | 0 | switch (rp->type) { |
1603 | 0 | case REENC_PROTECTION_CHECKSUM: |
1604 | 0 | log_dbg(cd, "Checksums based recovery."); |
1605 | |
|
1606 | 0 | r = crypt_storage_wrapper_init(cd, &cw1, crypt_data_device(cd), |
1607 | 0 | data_offset + rh->offset, crash_iv_offset, old_sector_size, |
1608 | 0 | reencrypt_segment_cipher_old(hdr), vk_old, 0); |
1609 | 0 | if (r) { |
1610 | 0 | log_err(cd, _("Failed to initialize old segment storage wrapper.")); |
1611 | 0 | goto out; |
1612 | 0 | } |
1613 | | |
1614 | 0 | count = rh->length / rp->p.csum.block_size; |
1615 | 0 | area_length_read = count * rp->p.csum.hash_size; |
1616 | 0 | if (area_length_read > area_length) { |
1617 | 0 | log_dbg(cd, "Internal error in calculated area_length."); |
1618 | 0 | r = -EINVAL; |
1619 | 0 | goto out; |
1620 | 0 | } |
1621 | | |
1622 | 0 | checksum_tmp = malloc(rp->p.csum.hash_size); |
1623 | 0 | if (!checksum_tmp) { |
1624 | 0 | r = -ENOMEM; |
1625 | 0 | goto out; |
1626 | 0 | } |
1627 | | |
1628 | | /* TODO: lock for read */ |
1629 | 0 | devfd = device_open(cd, crypt_metadata_device(cd), O_RDONLY); |
1630 | 0 | if (devfd < 0) |
1631 | 0 | goto out; |
1632 | | |
1633 | | /* read old data checksums */ |
1634 | 0 | read = read_lseek_blockwise(devfd, device_block_size(cd, crypt_metadata_device(cd)), |
1635 | 0 | device_alignment(crypt_metadata_device(cd)), rp->p.csum.checksums, area_length_read, area_offset); |
1636 | 0 | if (read < 0 || (size_t)read != area_length_read) { |
1637 | 0 | log_err(cd, _("Failed to read checksums for current hotzone.")); |
1638 | 0 | r = -EINVAL; |
1639 | 0 | goto out; |
1640 | 0 | } |
1641 | | |
1642 | 0 | read = crypt_storage_wrapper_read(cw2, 0, data_buffer, rh->length); |
1643 | 0 | if (read < 0 || (size_t)read != rh->length) { |
1644 | 0 | log_err(cd, _("Failed to read hotzone area starting at %" PRIu64 "."), rh->offset + data_offset); |
1645 | 0 | r = -EINVAL; |
1646 | 0 | goto out; |
1647 | 0 | } |
1648 | | |
1649 | 0 | for (s = 0; s < count; s++) { |
1650 | 0 | if (crypt_hash_write(rp->p.csum.ch, data_buffer + (s * rp->p.csum.block_size), rp->p.csum.block_size)) { |
1651 | 0 | log_dbg(cd, "Failed to write hash."); |
1652 | 0 | r = -EINVAL; |
1653 | 0 | goto out; |
1654 | 0 | } |
1655 | 0 | if (crypt_hash_final(rp->p.csum.ch, checksum_tmp, rp->p.csum.hash_size)) { |
1656 | 0 | log_dbg(cd, "Failed to finalize hash."); |
1657 | 0 | r = -EINVAL; |
1658 | 0 | goto out; |
1659 | 0 | } |
1660 | 0 | if (!memcmp(checksum_tmp, (char *)rp->p.csum.checksums + (s * rp->p.csum.hash_size), rp->p.csum.hash_size)) { |
1661 | 0 | log_dbg(cd, "Sector %zu (size %zu, offset %zu) needs recovery", s, rp->p.csum.block_size, s * rp->p.csum.block_size); |
1662 | 0 | if (crypt_storage_wrapper_decrypt(cw1, s * rp->p.csum.block_size, data_buffer + (s * rp->p.csum.block_size), rp->p.csum.block_size)) { |
1663 | 0 | log_err(cd, _("Failed to decrypt sector %zu."), s); |
1664 | 0 | r = -EINVAL; |
1665 | 0 | goto out; |
1666 | 0 | } |
1667 | 0 | w = crypt_storage_wrapper_encrypt_write(cw2, s * rp->p.csum.block_size, data_buffer + (s * rp->p.csum.block_size), rp->p.csum.block_size); |
1668 | 0 | if (w < 0 || (size_t)w != rp->p.csum.block_size) { |
1669 | 0 | log_err(cd, _("Failed to recover sector %zu."), s); |
1670 | 0 | r = -EINVAL; |
1671 | 0 | goto out; |
1672 | 0 | } |
1673 | 0 | } |
1674 | 0 | } |
1675 | | |
1676 | 0 | r = 0; |
1677 | 0 | break; |
1678 | 0 | case REENC_PROTECTION_JOURNAL: |
1679 | 0 | log_dbg(cd, "Journal based recovery."); |
1680 | | |
1681 | | /* FIXME: validation candidate */ |
1682 | 0 | if (rh->length > area_length) { |
1683 | 0 | r = -EINVAL; |
1684 | 0 | log_dbg(cd, "Invalid journal size."); |
1685 | 0 | goto out; |
1686 | 0 | } |
1687 | | |
1688 | | /* TODO locking */ |
1689 | 0 | r = crypt_storage_wrapper_init(cd, &cw1, crypt_metadata_device(cd), |
1690 | 0 | area_offset, crash_iv_offset, old_sector_size, |
1691 | 0 | reencrypt_segment_cipher_old(hdr), vk_old, 0); |
1692 | 0 | if (r) { |
1693 | 0 | log_err(cd, _("Failed to initialize old segment storage wrapper.")); |
1694 | 0 | goto out; |
1695 | 0 | } |
1696 | 0 | read = crypt_storage_wrapper_read_decrypt(cw1, 0, data_buffer, rh->length); |
1697 | 0 | if (read < 0 || (size_t)read != rh->length) { |
1698 | 0 | log_dbg(cd, "Failed to read journaled data."); |
1699 | 0 | r = -EIO; |
1700 | | /* may content plaintext */ |
1701 | 0 | crypt_safe_memzero(data_buffer, rh->length); |
1702 | 0 | goto out; |
1703 | 0 | } |
1704 | 0 | read = crypt_storage_wrapper_encrypt_write(cw2, 0, data_buffer, rh->length); |
1705 | | /* may content plaintext */ |
1706 | 0 | crypt_safe_memzero(data_buffer, rh->length); |
1707 | 0 | if (read < 0 || (size_t)read != rh->length) { |
1708 | 0 | log_dbg(cd, "recovery write failed."); |
1709 | 0 | r = -EINVAL; |
1710 | 0 | goto out; |
1711 | 0 | } |
1712 | | |
1713 | 0 | r = 0; |
1714 | 0 | break; |
1715 | 0 | case REENC_PROTECTION_DATASHIFT: |
1716 | 0 | log_dbg(cd, "Data shift based recovery."); |
1717 | |
|
1718 | 0 | if (rseg == 0) { |
1719 | 0 | r = crypt_storage_wrapper_init(cd, &cw1, crypt_data_device(cd), |
1720 | 0 | json_segment_get_offset(rh->jobj_segment_moved, 0), 0, |
1721 | 0 | reencrypt_get_sector_size_old(hdr), |
1722 | 0 | reencrypt_segment_cipher_old(hdr), vk_old, 0); |
1723 | 0 | } else { |
1724 | 0 | if (rh->direction == CRYPT_REENCRYPT_FORWARD) |
1725 | 0 | data_offset = data_offset + rh->offset + data_shift_value(rp); |
1726 | 0 | else |
1727 | 0 | data_offset = data_offset + rh->offset - data_shift_value(rp); |
1728 | 0 | r = crypt_storage_wrapper_init(cd, &cw1, crypt_data_device(cd), |
1729 | 0 | data_offset, |
1730 | 0 | crash_iv_offset, |
1731 | 0 | reencrypt_get_sector_size_old(hdr), |
1732 | 0 | reencrypt_segment_cipher_old(hdr), vk_old, 0); |
1733 | 0 | } |
1734 | 0 | if (r) { |
1735 | 0 | log_err(cd, _("Failed to initialize old segment storage wrapper.")); |
1736 | 0 | goto out; |
1737 | 0 | } |
1738 | | |
1739 | 0 | read = crypt_storage_wrapper_read_decrypt(cw1, 0, data_buffer, rh->length); |
1740 | 0 | if (read < 0 || (size_t)read != rh->length) { |
1741 | 0 | log_dbg(cd, "Failed to read data."); |
1742 | 0 | r = -EIO; |
1743 | | /* may content plaintext */ |
1744 | 0 | crypt_safe_memzero(data_buffer, rh->length); |
1745 | 0 | goto out; |
1746 | 0 | } |
1747 | | |
1748 | 0 | read = crypt_storage_wrapper_encrypt_write(cw2, 0, data_buffer, rh->length); |
1749 | | /* may content plaintext */ |
1750 | 0 | crypt_safe_memzero(data_buffer, rh->length); |
1751 | 0 | if (read < 0 || (size_t)read != rh->length) { |
1752 | 0 | log_dbg(cd, "recovery write failed."); |
1753 | 0 | r = -EINVAL; |
1754 | 0 | goto out; |
1755 | 0 | } |
1756 | 0 | r = 0; |
1757 | 0 | break; |
1758 | 0 | default: |
1759 | 0 | r = -EINVAL; |
1760 | 0 | } |
1761 | | |
1762 | 0 | if (!r) |
1763 | 0 | rh->read = rh->length; |
1764 | 0 | out: |
1765 | 0 | free(data_buffer); |
1766 | 0 | free(checksum_tmp); |
1767 | 0 | crypt_storage_wrapper_destroy(cw1); |
1768 | 0 | crypt_storage_wrapper_destroy(cw2); |
1769 | |
|
1770 | 0 | return r; |
1771 | 0 | } |
1772 | | |
1773 | | static int reencrypt_add_moved_segment(struct crypt_device *cd, struct luks2_hdr *hdr, struct luks2_reencrypt *rh) |
1774 | 0 | { |
1775 | 0 | int digest = rh->digest_old, s = LUKS2_segment_first_unused_id(hdr); |
1776 | |
|
1777 | 0 | if (!rh->jobj_segment_moved) |
1778 | 0 | return 0; |
1779 | | |
1780 | 0 | if (s < 0) |
1781 | 0 | return s; |
1782 | | |
1783 | 0 | if (json_object_object_add_by_uint(LUKS2_get_segments_jobj(hdr), s, json_object_get(rh->jobj_segment_moved))) { |
1784 | 0 | json_object_put(rh->jobj_segment_moved); |
1785 | 0 | return -EINVAL; |
1786 | 0 | } |
1787 | | |
1788 | 0 | if (!strcmp(json_segment_type(rh->jobj_segment_moved), "crypt")) |
1789 | 0 | return LUKS2_digest_segment_assign(cd, hdr, s, digest, 1, 0); |
1790 | | |
1791 | 0 | return 0; |
1792 | 0 | } |
1793 | | |
1794 | | static int reencrypt_add_backup_segment(struct crypt_device *cd, |
1795 | | struct luks2_hdr *hdr, |
1796 | | struct luks2_reencrypt *rh, |
1797 | | unsigned final) |
1798 | 0 | { |
1799 | 0 | int digest, s = LUKS2_segment_first_unused_id(hdr); |
1800 | 0 | json_object *jobj; |
1801 | |
|
1802 | 0 | if (s < 0) |
1803 | 0 | return s; |
1804 | | |
1805 | 0 | digest = final ? rh->digest_new : rh->digest_old; |
1806 | 0 | jobj = final ? rh->jobj_segment_new : rh->jobj_segment_old; |
1807 | |
|
1808 | 0 | if (json_object_object_add_by_uint(LUKS2_get_segments_jobj(hdr), s, json_object_get(jobj))) { |
1809 | 0 | json_object_put(jobj); |
1810 | 0 | return -EINVAL; |
1811 | 0 | } |
1812 | | |
1813 | 0 | if (strcmp(json_segment_type(jobj), "crypt")) |
1814 | 0 | return 0; |
1815 | | |
1816 | 0 | return LUKS2_digest_segment_assign(cd, hdr, s, digest, 1, 0); |
1817 | 0 | } |
1818 | | |
1819 | | static int reencrypt_assign_segments_simple(struct crypt_device *cd, |
1820 | | struct luks2_hdr *hdr, |
1821 | | struct luks2_reencrypt *rh, |
1822 | | unsigned hot, |
1823 | | unsigned commit) |
1824 | 0 | { |
1825 | 0 | int r, sg; |
1826 | |
|
1827 | 0 | if (hot && json_segments_count(rh->jobj_segs_hot) > 0) { |
1828 | 0 | log_dbg(cd, "Setting 'hot' segments."); |
1829 | |
|
1830 | 0 | r = LUKS2_segments_set(cd, hdr, rh->jobj_segs_hot, 0); |
1831 | 0 | if (!r) |
1832 | 0 | rh->jobj_segs_hot = NULL; |
1833 | 0 | } else if (!hot && json_segments_count(rh->jobj_segs_post) > 0) { |
1834 | 0 | log_dbg(cd, "Setting 'post' segments."); |
1835 | 0 | r = LUKS2_segments_set(cd, hdr, rh->jobj_segs_post, 0); |
1836 | 0 | if (!r) |
1837 | 0 | rh->jobj_segs_post = NULL; |
1838 | 0 | } else { |
1839 | 0 | log_dbg(cd, "No segments to set."); |
1840 | 0 | return -EINVAL; |
1841 | 0 | } |
1842 | | |
1843 | 0 | if (r) { |
1844 | 0 | log_dbg(cd, "Failed to assign new enc segments."); |
1845 | 0 | return r; |
1846 | 0 | } |
1847 | | |
1848 | 0 | r = reencrypt_add_backup_segment(cd, hdr, rh, 0); |
1849 | 0 | if (r) { |
1850 | 0 | log_dbg(cd, "Failed to assign reencryption previous backup segment."); |
1851 | 0 | return r; |
1852 | 0 | } |
1853 | | |
1854 | 0 | r = reencrypt_add_backup_segment(cd, hdr, rh, 1); |
1855 | 0 | if (r) { |
1856 | 0 | log_dbg(cd, "Failed to assign reencryption final backup segment."); |
1857 | 0 | return r; |
1858 | 0 | } |
1859 | | |
1860 | 0 | r = reencrypt_add_moved_segment(cd, hdr, rh); |
1861 | 0 | if (r) { |
1862 | 0 | log_dbg(cd, "Failed to assign reencryption moved backup segment."); |
1863 | 0 | return r; |
1864 | 0 | } |
1865 | | |
1866 | 0 | for (sg = 0; sg < LUKS2_segments_count(hdr); sg++) { |
1867 | 0 | if (LUKS2_segment_is_type(hdr, sg, "crypt") && |
1868 | 0 | LUKS2_digest_segment_assign(cd, hdr, sg, rh->mode == CRYPT_REENCRYPT_ENCRYPT ? rh->digest_new : rh->digest_old, 1, 0)) { |
1869 | 0 | log_dbg(cd, "Failed to assign digest %u to segment %u.", rh->digest_new, sg); |
1870 | 0 | return -EINVAL; |
1871 | 0 | } |
1872 | 0 | } |
1873 | | |
1874 | 0 | return commit ? LUKS2_hdr_write(cd, hdr) : 0; |
1875 | 0 | } |
1876 | | |
1877 | | static int reencrypt_assign_segments(struct crypt_device *cd, |
1878 | | struct luks2_hdr *hdr, |
1879 | | struct luks2_reencrypt *rh, |
1880 | | unsigned hot, |
1881 | | unsigned commit) |
1882 | 0 | { |
1883 | 0 | bool forward; |
1884 | 0 | int rseg, scount, r = -EINVAL; |
1885 | | |
1886 | | /* FIXME: validate in reencrypt context load */ |
1887 | 0 | if (rh->digest_new < 0 && rh->mode != CRYPT_REENCRYPT_DECRYPT) |
1888 | 0 | return -EINVAL; |
1889 | | |
1890 | 0 | if (LUKS2_digest_segment_assign(cd, hdr, CRYPT_ANY_SEGMENT, CRYPT_ANY_DIGEST, 0, 0)) |
1891 | 0 | return -EINVAL; |
1892 | | |
1893 | 0 | if (rh->mode == CRYPT_REENCRYPT_ENCRYPT || rh->mode == CRYPT_REENCRYPT_DECRYPT) |
1894 | 0 | return reencrypt_assign_segments_simple(cd, hdr, rh, hot, commit); |
1895 | | |
1896 | 0 | if (hot && rh->jobj_segs_hot) { |
1897 | 0 | log_dbg(cd, "Setting 'hot' segments."); |
1898 | |
|
1899 | 0 | r = LUKS2_segments_set(cd, hdr, rh->jobj_segs_hot, 0); |
1900 | 0 | if (!r) |
1901 | 0 | rh->jobj_segs_hot = NULL; |
1902 | 0 | } else if (!hot && rh->jobj_segs_post) { |
1903 | 0 | log_dbg(cd, "Setting 'post' segments."); |
1904 | 0 | r = LUKS2_segments_set(cd, hdr, rh->jobj_segs_post, 0); |
1905 | 0 | if (!r) |
1906 | 0 | rh->jobj_segs_post = NULL; |
1907 | 0 | } |
1908 | |
|
1909 | 0 | if (r) |
1910 | 0 | return r; |
1911 | | |
1912 | 0 | scount = LUKS2_segments_count(hdr); |
1913 | | |
1914 | | /* segment in reencryption has to hold reference on both digests */ |
1915 | 0 | rseg = json_segments_segment_in_reencrypt(LUKS2_get_segments_jobj(hdr)); |
1916 | 0 | if (rseg < 0 && hot) |
1917 | 0 | return -EINVAL; |
1918 | | |
1919 | 0 | if (rseg >= 0) { |
1920 | 0 | LUKS2_digest_segment_assign(cd, hdr, rseg, rh->digest_new, 1, 0); |
1921 | 0 | LUKS2_digest_segment_assign(cd, hdr, rseg, rh->digest_old, 1, 0); |
1922 | 0 | } |
1923 | |
|
1924 | 0 | forward = (rh->direction == CRYPT_REENCRYPT_FORWARD); |
1925 | 0 | if (hot) { |
1926 | 0 | if (rseg > 0) |
1927 | 0 | LUKS2_digest_segment_assign(cd, hdr, 0, forward ? rh->digest_new : rh->digest_old, 1, 0); |
1928 | 0 | if (scount > rseg + 1) |
1929 | 0 | LUKS2_digest_segment_assign(cd, hdr, rseg + 1, forward ? rh->digest_old : rh->digest_new, 1, 0); |
1930 | 0 | } else { |
1931 | 0 | LUKS2_digest_segment_assign(cd, hdr, 0, forward || scount == 1 ? rh->digest_new : rh->digest_old, 1, 0); |
1932 | 0 | if (scount > 1) |
1933 | 0 | LUKS2_digest_segment_assign(cd, hdr, 1, forward ? rh->digest_old : rh->digest_new, 1, 0); |
1934 | 0 | } |
1935 | |
|
1936 | 0 | r = reencrypt_add_backup_segment(cd, hdr, rh, 0); |
1937 | 0 | if (r) { |
1938 | 0 | log_dbg(cd, "Failed to assign hot reencryption backup segment."); |
1939 | 0 | return r; |
1940 | 0 | } |
1941 | 0 | r = reencrypt_add_backup_segment(cd, hdr, rh, 1); |
1942 | 0 | if (r) { |
1943 | 0 | log_dbg(cd, "Failed to assign post reencryption backup segment."); |
1944 | 0 | return r; |
1945 | 0 | } |
1946 | | |
1947 | 0 | return commit ? LUKS2_hdr_write(cd, hdr) : 0; |
1948 | 0 | } |
1949 | | |
1950 | | static int reencrypt_set_encrypt_segments(struct crypt_device *cd, struct luks2_hdr *hdr, |
1951 | | uint64_t dev_size, uint64_t data_size, uint64_t data_shift, bool move_first_segment, |
1952 | | crypt_reencrypt_direction_info di) |
1953 | 0 | { |
1954 | 0 | int r; |
1955 | 0 | uint64_t first_segment_offset, first_segment_length, |
1956 | 0 | second_segment_offset, second_segment_length, |
1957 | 0 | data_offset = LUKS2_get_data_offset(hdr) << SECTOR_SHIFT; |
1958 | 0 | json_object *jobj_segment_first = NULL, *jobj_segment_second = NULL, *jobj_segments; |
1959 | |
|
1960 | 0 | if (dev_size < data_shift) |
1961 | 0 | return -EINVAL; |
1962 | | |
1963 | 0 | if (data_shift && (di == CRYPT_REENCRYPT_FORWARD)) |
1964 | 0 | return -ENOTSUP; |
1965 | | |
1966 | 0 | if (move_first_segment) { |
1967 | | /* |
1968 | | * future data_device layout: |
1969 | | * [future LUKS2 header (data shift size)][second data segment][gap (data shift size)][first data segment (data shift size)] |
1970 | | */ |
1971 | 0 | first_segment_offset = dev_size; |
1972 | 0 | if (data_size < data_shift) { |
1973 | 0 | first_segment_length = data_size; |
1974 | 0 | second_segment_length = second_segment_offset = 0; |
1975 | 0 | } else { |
1976 | 0 | first_segment_length = data_shift; |
1977 | 0 | second_segment_offset = data_shift; |
1978 | 0 | second_segment_length = data_size - data_shift; |
1979 | 0 | } |
1980 | 0 | } else if (data_shift) { |
1981 | 0 | first_segment_offset = data_offset; |
1982 | 0 | first_segment_length = dev_size; |
1983 | 0 | } else { |
1984 | | /* future data_device layout with detached header: [first data segment] */ |
1985 | 0 | first_segment_offset = data_offset; |
1986 | 0 | first_segment_length = 0; /* dynamic */ |
1987 | 0 | } |
1988 | |
|
1989 | 0 | jobj_segments = json_object_new_object(); |
1990 | 0 | if (!jobj_segments) |
1991 | 0 | return -ENOMEM; |
1992 | | |
1993 | 0 | r = -EINVAL; |
1994 | 0 | if (move_first_segment) { |
1995 | 0 | jobj_segment_first = json_segment_create_linear(first_segment_offset, &first_segment_length, 0); |
1996 | 0 | if (second_segment_length && |
1997 | 0 | !(jobj_segment_second = json_segment_create_linear(second_segment_offset, &second_segment_length, 0))) { |
1998 | 0 | log_dbg(cd, "Failed generate 2nd segment."); |
1999 | 0 | return r; |
2000 | 0 | } |
2001 | 0 | } else |
2002 | 0 | jobj_segment_first = json_segment_create_linear(first_segment_offset, first_segment_length ? &first_segment_length : NULL, 0); |
2003 | | |
2004 | 0 | if (!jobj_segment_first) { |
2005 | 0 | log_dbg(cd, "Failed generate 1st segment."); |
2006 | 0 | return r; |
2007 | 0 | } |
2008 | | |
2009 | 0 | json_object_object_add(jobj_segments, "0", jobj_segment_first); |
2010 | 0 | if (jobj_segment_second) |
2011 | 0 | json_object_object_add(jobj_segments, "1", jobj_segment_second); |
2012 | |
|
2013 | 0 | r = LUKS2_digest_segment_assign(cd, hdr, CRYPT_ANY_SEGMENT, CRYPT_ANY_DIGEST, 0, 0); |
2014 | |
|
2015 | 0 | return r ?: LUKS2_segments_set(cd, hdr, jobj_segments, 0); |
2016 | 0 | } |
2017 | | |
2018 | | static int reencrypt_set_decrypt_shift_segments(struct crypt_device *cd, |
2019 | | struct luks2_hdr *hdr, |
2020 | | uint64_t dev_size, |
2021 | | uint64_t moved_segment_length, |
2022 | | crypt_reencrypt_direction_info di) |
2023 | 0 | { |
2024 | 0 | int digest, r; |
2025 | 0 | uint64_t data_offset = LUKS2_get_data_offset(hdr) << SECTOR_SHIFT; |
2026 | 0 | json_object *jobj_segment_first = NULL, *jobj_segment_second = NULL, *jobj_segments; |
2027 | |
|
2028 | 0 | if (di == CRYPT_REENCRYPT_BACKWARD) |
2029 | 0 | return -ENOTSUP; |
2030 | | |
2031 | 0 | digest = LUKS2_digest_by_segment(hdr, CRYPT_DEFAULT_SEGMENT); |
2032 | 0 | if (digest < 0) |
2033 | 0 | return -EINVAL; |
2034 | | |
2035 | | /* |
2036 | | * future data_device layout: |
2037 | | * [encrypted first segment (max data shift size)][gap (data shift size)][second encrypted data segment] |
2038 | | */ |
2039 | 0 | jobj_segments = json_object_new_object(); |
2040 | 0 | if (!jobj_segments) |
2041 | 0 | return -ENOMEM; |
2042 | | |
2043 | 0 | r = -EINVAL; |
2044 | 0 | jobj_segment_first = json_segment_create_crypt(0, crypt_get_iv_offset(cd), |
2045 | 0 | &moved_segment_length, crypt_get_cipher_spec(cd), |
2046 | 0 | NULL, 0, crypt_get_sector_size(cd), 0); |
2047 | |
|
2048 | 0 | if (!jobj_segment_first) { |
2049 | 0 | log_dbg(cd, "Failed generate 1st segment."); |
2050 | 0 | goto err; |
2051 | 0 | } |
2052 | | |
2053 | 0 | r = json_object_object_add_by_uint_by_ref(jobj_segments, 0, &jobj_segment_first); |
2054 | 0 | if (r) |
2055 | 0 | goto err; |
2056 | | |
2057 | 0 | if (dev_size > moved_segment_length) { |
2058 | 0 | jobj_segment_second = json_segment_create_crypt(data_offset + moved_segment_length, |
2059 | 0 | crypt_get_iv_offset(cd) + (moved_segment_length >> SECTOR_SHIFT), |
2060 | 0 | NULL, |
2061 | 0 | crypt_get_cipher_spec(cd), |
2062 | 0 | NULL, 0, /* integrity */ |
2063 | 0 | crypt_get_sector_size(cd), 0); |
2064 | 0 | if (!jobj_segment_second) { |
2065 | 0 | r = -EINVAL; |
2066 | 0 | log_dbg(cd, "Failed generate 2nd segment."); |
2067 | 0 | goto err; |
2068 | 0 | } |
2069 | | |
2070 | 0 | r = json_object_object_add_by_uint_by_ref(jobj_segments, 1, &jobj_segment_second); |
2071 | 0 | if (r) |
2072 | 0 | goto err; |
2073 | 0 | } |
2074 | | |
2075 | 0 | if (!(r = LUKS2_segments_set(cd, hdr, jobj_segments, 0))) |
2076 | 0 | return LUKS2_digest_segment_assign(cd, hdr, CRYPT_ANY_SEGMENT, digest, 1, 0); |
2077 | 0 | err: |
2078 | 0 | json_object_put(jobj_segment_first); |
2079 | 0 | json_object_put(jobj_segment_second); |
2080 | 0 | json_object_put(jobj_segments); |
2081 | 0 | return r; |
2082 | 0 | } |
2083 | | |
2084 | | static int reencrypt_make_targets(struct crypt_device *cd, |
2085 | | struct luks2_hdr *hdr, |
2086 | | struct device *hz_device, |
2087 | | struct volume_key *vks, |
2088 | | struct dm_target *result, |
2089 | | uint64_t size) |
2090 | 0 | { |
2091 | 0 | bool reenc_seg; |
2092 | 0 | struct volume_key *vk; |
2093 | 0 | uint64_t segment_size, segment_offset, segment_start = 0; |
2094 | 0 | int r; |
2095 | 0 | int s = 0; |
2096 | 0 | json_object *jobj, *jobj_segments = LUKS2_get_segments_jobj(hdr); |
2097 | |
|
2098 | 0 | while (result) { |
2099 | 0 | jobj = json_segments_get_segment(jobj_segments, s); |
2100 | 0 | if (!jobj) { |
2101 | 0 | log_dbg(cd, "Internal error. Segment %u is null.", s); |
2102 | 0 | return -EINVAL; |
2103 | 0 | } |
2104 | | |
2105 | 0 | reenc_seg = (s == json_segments_segment_in_reencrypt(jobj_segments)); |
2106 | |
|
2107 | 0 | segment_offset = json_segment_get_offset(jobj, 1); |
2108 | 0 | segment_size = json_segment_get_size(jobj, 1); |
2109 | | /* 'dynamic' length allowed in last segment only */ |
2110 | 0 | if (!segment_size && !result->next) |
2111 | 0 | segment_size = (size >> SECTOR_SHIFT) - segment_start; |
2112 | 0 | if (!segment_size) { |
2113 | 0 | log_dbg(cd, "Internal error. Wrong segment size %u", s); |
2114 | 0 | return -EINVAL; |
2115 | 0 | } |
2116 | | |
2117 | 0 | if (reenc_seg) |
2118 | 0 | segment_offset -= crypt_get_data_offset(cd); |
2119 | |
|
2120 | 0 | if (!strcmp(json_segment_type(jobj), "crypt")) { |
2121 | 0 | vk = crypt_volume_key_by_id(vks, reenc_seg ? LUKS2_reencrypt_digest_new(hdr) : LUKS2_digest_by_segment(hdr, s)); |
2122 | 0 | if (!vk) { |
2123 | 0 | log_err(cd, _("Missing key for dm-crypt segment %u"), s); |
2124 | 0 | return -EINVAL; |
2125 | 0 | } |
2126 | | |
2127 | 0 | r = dm_crypt_target_set(result, segment_start, segment_size, |
2128 | 0 | reenc_seg ? hz_device : crypt_data_device(cd), |
2129 | 0 | vk, |
2130 | 0 | json_segment_get_cipher(jobj), |
2131 | 0 | json_segment_get_iv_offset(jobj), |
2132 | 0 | segment_offset, |
2133 | 0 | "none", 0, 0, |
2134 | 0 | json_segment_get_sector_size(jobj)); |
2135 | 0 | if (r) { |
2136 | 0 | log_err(cd, _("Failed to set dm-crypt segment.")); |
2137 | 0 | return r; |
2138 | 0 | } |
2139 | 0 | } else if (!strcmp(json_segment_type(jobj), "linear")) { |
2140 | 0 | r = dm_linear_target_set(result, segment_start, segment_size, reenc_seg ? hz_device : crypt_data_device(cd), segment_offset); |
2141 | 0 | if (r) { |
2142 | 0 | log_err(cd, _("Failed to set dm-linear segment.")); |
2143 | 0 | return r; |
2144 | 0 | } |
2145 | 0 | } else |
2146 | 0 | return EINVAL; |
2147 | | |
2148 | 0 | segment_start += segment_size; |
2149 | 0 | s++; |
2150 | 0 | result = result->next; |
2151 | 0 | } |
2152 | | |
2153 | 0 | return s; |
2154 | 0 | } |
2155 | | |
2156 | | /* GLOBAL FIXME: audit function names and parameters names */ |
2157 | | |
2158 | | /* FIXME: |
2159 | | * 1) audit log routines |
2160 | | * 2) can't we derive hotzone device name from crypt context? (unlocked name, device uuid, etc?) |
2161 | | */ |
2162 | | static int reencrypt_load_overlay_device(struct crypt_device *cd, struct luks2_hdr *hdr, |
2163 | | const char *overlay, struct device *hotzone_device, struct volume_key *vks, uint64_t size, |
2164 | | uint32_t flags) |
2165 | 0 | { |
2166 | 0 | int r; |
2167 | |
|
2168 | 0 | struct crypt_dm_active_device dmd = { |
2169 | 0 | .flags = flags, |
2170 | 0 | }; |
2171 | |
|
2172 | 0 | log_dbg(cd, "Loading new table for overlay device %s.", overlay); |
2173 | |
|
2174 | 0 | r = dm_targets_allocate(&dmd.segment, LUKS2_segments_count(hdr)); |
2175 | 0 | if (r) |
2176 | 0 | goto out; |
2177 | | |
2178 | 0 | r = reencrypt_make_targets(cd, hdr, hotzone_device, vks, &dmd.segment, size); |
2179 | 0 | if (r < 0) |
2180 | 0 | goto out; |
2181 | | |
2182 | 0 | r = dm_reload_device(cd, overlay, &dmd, 0, 0); |
2183 | | |
2184 | | /* what else on error here ? */ |
2185 | 0 | out: |
2186 | 0 | dm_targets_free(cd, &dmd); |
2187 | |
|
2188 | 0 | return r; |
2189 | 0 | } |
2190 | | |
2191 | | static int reencrypt_replace_device(struct crypt_device *cd, const char *target, const char *source, uint32_t flags) |
2192 | 0 | { |
2193 | 0 | int r, exists = 1; |
2194 | 0 | struct crypt_dm_active_device dmd_source, dmd_target = {}; |
2195 | 0 | uint64_t dmflags = DM_SUSPEND_SKIP_LOCKFS | DM_SUSPEND_NOFLUSH; |
2196 | |
|
2197 | 0 | log_dbg(cd, "Replacing table in device %s with table from device %s.", target, source); |
2198 | | |
2199 | | /* check only whether target device exists */ |
2200 | 0 | r = dm_status_device(cd, target); |
2201 | 0 | if (r < 0) { |
2202 | 0 | if (r == -ENODEV) |
2203 | 0 | exists = 0; |
2204 | 0 | else |
2205 | 0 | return r; |
2206 | 0 | } |
2207 | | |
2208 | 0 | r = dm_query_device(cd, source, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER | |
2209 | 0 | DM_ACTIVE_CRYPT_KEYSIZE | DM_ACTIVE_CRYPT_KEY, &dmd_source); |
2210 | |
|
2211 | 0 | if (r < 0) |
2212 | 0 | return r; |
2213 | | |
2214 | 0 | if (exists && ((r = dm_query_device(cd, target, 0, &dmd_target)) < 0)) |
2215 | 0 | goto out; |
2216 | | |
2217 | 0 | dmd_source.flags |= flags; |
2218 | 0 | dmd_source.uuid = crypt_get_uuid(cd); |
2219 | |
|
2220 | 0 | if (exists) { |
2221 | 0 | if (dmd_target.size != dmd_source.size) { |
2222 | 0 | log_err(cd, _("Source and target device sizes don't match. Source %" PRIu64 ", target: %" PRIu64 "."), |
2223 | 0 | dmd_source.size, dmd_target.size); |
2224 | 0 | r = -EINVAL; |
2225 | 0 | goto out; |
2226 | 0 | } |
2227 | 0 | r = dm_reload_device(cd, target, &dmd_source, 0, 0); |
2228 | 0 | if (!r) { |
2229 | 0 | log_dbg(cd, "Resuming device %s", target); |
2230 | 0 | r = dm_resume_device(cd, target, dmflags | act2dmflags(dmd_source.flags)); |
2231 | 0 | } |
2232 | 0 | } else |
2233 | 0 | r = dm_create_device(cd, target, CRYPT_SUBDEV, &dmd_source); |
2234 | 0 | out: |
2235 | 0 | dm_targets_free(cd, &dmd_source); |
2236 | 0 | dm_targets_free(cd, &dmd_target); |
2237 | |
|
2238 | 0 | return r; |
2239 | 0 | } |
2240 | | |
2241 | | static int reencrypt_swap_backing_device(struct crypt_device *cd, const char *name, |
2242 | | const char *new_backend_name) |
2243 | 0 | { |
2244 | 0 | int r; |
2245 | 0 | struct device *overlay_dev = NULL; |
2246 | 0 | char overlay_path[PATH_MAX] = { 0 }; |
2247 | 0 | struct crypt_dm_active_device dmd = {}; |
2248 | |
|
2249 | 0 | log_dbg(cd, "Redirecting %s mapping to new backing device: %s.", name, new_backend_name); |
2250 | |
|
2251 | 0 | r = snprintf(overlay_path, PATH_MAX, "%s/%s", dm_get_dir(), new_backend_name); |
2252 | 0 | if (r < 0 || r >= PATH_MAX) { |
2253 | 0 | r = -EINVAL; |
2254 | 0 | goto out; |
2255 | 0 | } |
2256 | | |
2257 | 0 | r = device_alloc(cd, &overlay_dev, overlay_path); |
2258 | 0 | if (r) |
2259 | 0 | goto out; |
2260 | | |
2261 | 0 | r = device_block_adjust(cd, overlay_dev, DEV_OK, |
2262 | 0 | 0, &dmd.size, &dmd.flags); |
2263 | 0 | if (r) |
2264 | 0 | goto out; |
2265 | | |
2266 | 0 | r = dm_linear_target_set(&dmd.segment, 0, dmd.size, overlay_dev, 0); |
2267 | 0 | if (r) |
2268 | 0 | goto out; |
2269 | | |
2270 | 0 | r = dm_reload_device(cd, name, &dmd, 0, 0); |
2271 | 0 | if (!r) { |
2272 | 0 | log_dbg(cd, "Resuming device %s", name); |
2273 | 0 | r = dm_resume_device(cd, name, DM_SUSPEND_SKIP_LOCKFS | DM_SUSPEND_NOFLUSH); |
2274 | 0 | } |
2275 | |
|
2276 | 0 | out: |
2277 | 0 | dm_targets_free(cd, &dmd); |
2278 | 0 | device_free(cd, overlay_dev); |
2279 | |
|
2280 | 0 | return r; |
2281 | 0 | } |
2282 | | |
2283 | | static int reencrypt_activate_hotzone_device(struct crypt_device *cd, const char *name, uint64_t device_size, uint32_t flags) |
2284 | 0 | { |
2285 | 0 | int r; |
2286 | 0 | uint64_t new_offset = reencrypt_get_data_offset_new(crypt_get_hdr(cd, CRYPT_LUKS2)) >> SECTOR_SHIFT; |
2287 | |
|
2288 | 0 | struct crypt_dm_active_device dmd = { |
2289 | 0 | .flags = flags, |
2290 | 0 | .uuid = crypt_get_uuid(cd), |
2291 | 0 | .size = device_size >> SECTOR_SHIFT |
2292 | 0 | }; |
2293 | |
|
2294 | 0 | log_dbg(cd, "Activating hotzone device %s.", name); |
2295 | |
|
2296 | 0 | r = device_block_adjust(cd, crypt_data_device(cd), DEV_OK, |
2297 | 0 | new_offset, &dmd.size, &dmd.flags); |
2298 | 0 | if (r) |
2299 | 0 | goto out; |
2300 | | |
2301 | 0 | r = dm_linear_target_set(&dmd.segment, 0, dmd.size, crypt_data_device(cd), new_offset); |
2302 | 0 | if (r) |
2303 | 0 | goto out; |
2304 | | |
2305 | 0 | r = dm_create_device(cd, name, CRYPT_SUBDEV, &dmd); |
2306 | 0 | out: |
2307 | 0 | dm_targets_free(cd, &dmd); |
2308 | |
|
2309 | 0 | return r; |
2310 | 0 | } |
2311 | | |
2312 | | static int reencrypt_init_device_stack(struct crypt_device *cd, |
2313 | | struct luks2_reencrypt *rh) |
2314 | 0 | { |
2315 | 0 | int r; |
2316 | 0 | char hz_path[PATH_MAX]; |
2317 | |
|
2318 | 0 | assert(rh); |
2319 | 0 | assert(!rh->hotzone_device); |
2320 | | |
2321 | | /* Activate hotzone device 1:1 linear mapping to data_device */ |
2322 | 0 | r = reencrypt_activate_hotzone_device(cd, rh->hotzone_name, rh->device_size, CRYPT_ACTIVATE_PRIVATE); |
2323 | 0 | if (r) { |
2324 | 0 | log_err(cd, _("Failed to activate hotzone device %s."), rh->hotzone_name); |
2325 | 0 | return r; |
2326 | 0 | } |
2327 | | |
2328 | 0 | r = snprintf(hz_path, PATH_MAX, "%s/%s", dm_get_dir(), rh->hotzone_name); |
2329 | 0 | if (r < 0 || r >= PATH_MAX) { |
2330 | 0 | r = -EINVAL; |
2331 | 0 | goto err; |
2332 | 0 | } |
2333 | | |
2334 | 0 | r = device_alloc(cd, &rh->hotzone_device, hz_path); |
2335 | 0 | if (r) { |
2336 | 0 | log_err(cd, _("Failed to allocate hotzone device %s."), rh->hotzone_name); |
2337 | 0 | goto err; |
2338 | 0 | } |
2339 | | |
2340 | | /* |
2341 | | * Activate overlay device with exactly same table as original 'name' mapping. |
2342 | | * Note that within this step the 'name' device may already include a table |
2343 | | * constructed from more than single dm-crypt segment. Therefore transfer |
2344 | | * mapping as is. |
2345 | | * |
2346 | | * If we're about to resume reencryption orig mapping has to be already validated for |
2347 | | * abrupt shutdown and rchunk_offset has to point on next chunk to reencrypt! |
2348 | | * |
2349 | | * TODO: in crypt_activate_by* |
2350 | | */ |
2351 | 0 | r = reencrypt_replace_device(cd, rh->overlay_name, rh->device_name, CRYPT_ACTIVATE_PRIVATE); |
2352 | 0 | if (r) { |
2353 | 0 | log_err(cd, _("Failed to activate overlay device %s with actual origin table."), rh->overlay_name); |
2354 | 0 | goto err; |
2355 | 0 | } |
2356 | | |
2357 | | /* swap origin mapping to overlay device */ |
2358 | 0 | r = reencrypt_swap_backing_device(cd, rh->device_name, rh->overlay_name); |
2359 | 0 | if (r) { |
2360 | 0 | log_err(cd, _("Failed to load new mapping for device %s."), rh->device_name); |
2361 | 0 | goto err; |
2362 | 0 | } |
2363 | | |
2364 | | /* |
2365 | | * Now the 'name' (unlocked luks) device is mapped via dm-linear to an overlay dev. |
2366 | | * The overlay device has a original live table of 'name' device in-before the swap. |
2367 | | */ |
2368 | | |
2369 | 0 | return 0; |
2370 | 0 | err: |
2371 | | /* TODO: force error helper devices on error path */ |
2372 | 0 | dm_remove_device(cd, rh->overlay_name, 0); |
2373 | 0 | dm_remove_device(cd, rh->hotzone_name, 0); |
2374 | |
|
2375 | 0 | return r; |
2376 | 0 | } |
2377 | | |
2378 | | static reenc_status_t reenc_refresh_helper_devices(struct crypt_device *cd, const char *overlay, |
2379 | | const char *hotzone) |
2380 | 0 | { |
2381 | 0 | int r; |
2382 | | |
2383 | | /* |
2384 | | * we have to explicitly suspend the overlay device before suspending |
2385 | | * the hotzone one. Resuming overlay device (aka switching tables) only |
2386 | | * after suspending the hotzone may lead to deadlock. |
2387 | | * |
2388 | | * In other words: always suspend the stack from top to bottom! |
2389 | | */ |
2390 | 0 | r = dm_suspend_device(cd, overlay, DM_SUSPEND_SKIP_LOCKFS | DM_SUSPEND_NOFLUSH); |
2391 | 0 | if (r) { |
2392 | 0 | log_err(cd, _("Failed to suspend device %s."), overlay); |
2393 | 0 | return REENC_ERR_ROLLBACK_MEMORY; |
2394 | 0 | } |
2395 | | |
2396 | | /* suspend HZ device */ |
2397 | 0 | r = dm_suspend_device(cd, hotzone, DM_SUSPEND_SKIP_LOCKFS | DM_SUSPEND_NOFLUSH); |
2398 | 0 | if (r) { |
2399 | 0 | log_err(cd, _("Failed to suspend device %s."), hotzone); |
2400 | 0 | return REENC_ERR_ROLLBACK_MEMORY; |
2401 | 0 | } |
2402 | | |
2403 | | /* resume overlay device: inactive table (with hotozne) -> live */ |
2404 | 0 | r = dm_resume_device(cd, overlay, DM_RESUME_PRIVATE); |
2405 | 0 | if (r) { |
2406 | 0 | log_err(cd, _("Failed to resume device %s."), overlay); |
2407 | 0 | return REENC_ERR_ROLLBACK_MEMORY; |
2408 | 0 | } |
2409 | | |
2410 | 0 | return REENC_OK; |
2411 | 0 | } |
2412 | | |
2413 | | static reenc_status_t reencrypt_refresh_overlay_devices(struct crypt_device *cd, |
2414 | | struct luks2_hdr *hdr, |
2415 | | const char *overlay, |
2416 | | const char *hotzone, |
2417 | | struct device *hotzone_device, |
2418 | | struct volume_key *vks, |
2419 | | uint64_t device_size, |
2420 | | uint32_t flags) |
2421 | 0 | { |
2422 | 0 | int r = reencrypt_load_overlay_device(cd, hdr, overlay, hotzone_device, vks, device_size, flags); |
2423 | 0 | if (r) { |
2424 | 0 | log_err(cd, _("Failed to reload device %s."), overlay); |
2425 | 0 | return REENC_ERR_ROLLBACK_MEMORY; |
2426 | 0 | } |
2427 | | |
2428 | 0 | r = reenc_refresh_helper_devices(cd, overlay, hotzone); |
2429 | 0 | if (r != REENC_OK) |
2430 | 0 | log_err(cd, _("Failed to refresh reencryption devices stack.")); |
2431 | |
|
2432 | 0 | return r; |
2433 | 0 | } |
2434 | | |
2435 | | static int reencrypt_move_data(struct crypt_device *cd, |
2436 | | int devfd, |
2437 | | uint64_t data_shift, |
2438 | | crypt_reencrypt_mode_info mode) |
2439 | 0 | { |
2440 | 0 | void *buffer; |
2441 | 0 | int r; |
2442 | 0 | ssize_t ret; |
2443 | 0 | uint64_t buffer_len, offset, |
2444 | 0 | read_offset = (mode == CRYPT_REENCRYPT_ENCRYPT ? 0 : data_shift); |
2445 | 0 | struct luks2_hdr *hdr = crypt_get_hdr(cd, CRYPT_LUKS2); |
2446 | |
|
2447 | 0 | offset = json_segment_get_offset(LUKS2_get_segment_jobj(hdr, 0), 0); |
2448 | 0 | buffer_len = json_segment_get_size(LUKS2_get_segment_jobj(hdr, 0), 0); |
2449 | 0 | if (!buffer_len || buffer_len > data_shift) |
2450 | 0 | return -EINVAL; |
2451 | | |
2452 | 0 | if (posix_memalign(&buffer, device_alignment(crypt_data_device(cd)), buffer_len)) |
2453 | 0 | return -ENOMEM; |
2454 | | |
2455 | 0 | ret = read_lseek_blockwise(devfd, |
2456 | 0 | device_block_size(cd, crypt_data_device(cd)), |
2457 | 0 | device_alignment(crypt_data_device(cd)), |
2458 | 0 | buffer, buffer_len, read_offset); |
2459 | 0 | if (ret < 0 || (uint64_t)ret != buffer_len) { |
2460 | 0 | log_dbg(cd, "Failed to read data at offset %" PRIu64 " (size: %zu)", |
2461 | 0 | read_offset, buffer_len); |
2462 | 0 | r = -EIO; |
2463 | 0 | goto out; |
2464 | 0 | } |
2465 | | |
2466 | 0 | log_dbg(cd, "Going to write %" PRIu64 " bytes read at offset %" PRIu64 " to new offset %" PRIu64, |
2467 | 0 | buffer_len, read_offset, offset); |
2468 | 0 | ret = write_lseek_blockwise(devfd, |
2469 | 0 | device_block_size(cd, crypt_data_device(cd)), |
2470 | 0 | device_alignment(crypt_data_device(cd)), |
2471 | 0 | buffer, buffer_len, offset); |
2472 | 0 | if (ret < 0 || (uint64_t)ret != buffer_len) { |
2473 | 0 | log_dbg(cd, "Failed to write data at offset %" PRIu64 " (size: %zu)", |
2474 | 0 | offset, buffer_len); |
2475 | 0 | r = -EIO; |
2476 | 0 | goto out; |
2477 | 0 | } |
2478 | | |
2479 | 0 | r = 0; |
2480 | 0 | out: |
2481 | 0 | crypt_safe_memzero(buffer, buffer_len); |
2482 | 0 | free(buffer); |
2483 | 0 | return r; |
2484 | 0 | } |
2485 | | |
2486 | | static int reencrypt_make_backup_segments(struct crypt_device *cd, |
2487 | | struct luks2_hdr *hdr, |
2488 | | int digest_new, |
2489 | | const char *cipher, |
2490 | | uint64_t data_offset, |
2491 | | const struct crypt_params_reencrypt *params) |
2492 | 0 | { |
2493 | 0 | const char *type; |
2494 | 0 | int r, segment, moved_segment = -1, digest_old = -1; |
2495 | 0 | json_object *jobj_tmp, *jobj_segment_new = NULL, *jobj_segment_old = NULL, *jobj_segment_bcp = NULL; |
2496 | 0 | uint32_t sector_size = params->luks2 ? params->luks2->sector_size : SECTOR_SIZE; |
2497 | 0 | uint64_t segment_offset, tmp, data_shift = params->data_shift << SECTOR_SHIFT, |
2498 | 0 | device_size = params->device_size << SECTOR_SHIFT; |
2499 | |
|
2500 | 0 | if (params->mode != CRYPT_REENCRYPT_DECRYPT && digest_new < 0) |
2501 | 0 | return -EINVAL; |
2502 | | |
2503 | 0 | if (params->mode != CRYPT_REENCRYPT_ENCRYPT) { |
2504 | 0 | digest_old = LUKS2_digest_by_segment(hdr, CRYPT_DEFAULT_SEGMENT); |
2505 | 0 | if (digest_old < 0) |
2506 | 0 | return -EINVAL; |
2507 | 0 | } |
2508 | | |
2509 | 0 | segment = LUKS2_segment_first_unused_id(hdr); |
2510 | 0 | if (segment < 0) |
2511 | 0 | return -EINVAL; |
2512 | | |
2513 | 0 | if (params->flags & CRYPT_REENCRYPT_MOVE_FIRST_SEGMENT) { |
2514 | 0 | if (json_object_copy(LUKS2_get_segment_jobj(hdr, 0), &jobj_segment_bcp)) { |
2515 | 0 | r = -EINVAL; |
2516 | 0 | goto err; |
2517 | 0 | } |
2518 | 0 | r = LUKS2_segment_set_flag(jobj_segment_bcp, "backup-moved-segment"); |
2519 | 0 | if (r) |
2520 | 0 | goto err; |
2521 | 0 | moved_segment = segment++; |
2522 | 0 | r = json_object_object_add_by_uint_by_ref(LUKS2_get_segments_jobj(hdr), moved_segment, &jobj_segment_bcp); |
2523 | 0 | if (r) |
2524 | 0 | goto err; |
2525 | | |
2526 | 0 | if (!(type = json_segment_type(LUKS2_get_segment_jobj(hdr, moved_segment)))) { |
2527 | 0 | r = -EINVAL; |
2528 | 0 | goto err; |
2529 | 0 | } |
2530 | | |
2531 | 0 | if (!strcmp(type, "crypt") && ((r = LUKS2_digest_segment_assign(cd, hdr, moved_segment, digest_old, 1, 0)))) |
2532 | 0 | goto err; |
2533 | 0 | } |
2534 | | |
2535 | | /* FIXME: Add detection for case (digest old == digest new && old segment == new segment) */ |
2536 | 0 | if (digest_old >= 0) { |
2537 | 0 | if (params->flags & CRYPT_REENCRYPT_MOVE_FIRST_SEGMENT) { |
2538 | 0 | jobj_tmp = LUKS2_get_segment_jobj(hdr, 0); |
2539 | 0 | if (!jobj_tmp) { |
2540 | 0 | r = -EINVAL; |
2541 | 0 | goto err; |
2542 | 0 | } |
2543 | | |
2544 | 0 | jobj_segment_old = json_segment_create_crypt(data_offset, |
2545 | 0 | json_segment_get_iv_offset(jobj_tmp), |
2546 | 0 | device_size ? &device_size : NULL, |
2547 | 0 | json_segment_get_cipher(jobj_tmp), |
2548 | 0 | NULL, 0, /* integrity */ |
2549 | 0 | json_segment_get_sector_size(jobj_tmp), |
2550 | 0 | 0); |
2551 | 0 | } else { |
2552 | 0 | if (json_object_copy(LUKS2_get_segment_jobj(hdr, CRYPT_DEFAULT_SEGMENT), &jobj_segment_old)) { |
2553 | 0 | r = -EINVAL; |
2554 | 0 | goto err; |
2555 | 0 | } |
2556 | 0 | } |
2557 | 0 | } else if (params->mode == CRYPT_REENCRYPT_ENCRYPT) { |
2558 | 0 | r = LUKS2_get_data_size(hdr, &tmp, NULL); |
2559 | 0 | if (r) |
2560 | 0 | goto err; |
2561 | | |
2562 | 0 | if (params->flags & CRYPT_REENCRYPT_MOVE_FIRST_SEGMENT) |
2563 | 0 | jobj_segment_old = json_segment_create_linear(0, tmp ? &tmp : NULL, 0); |
2564 | 0 | else |
2565 | 0 | jobj_segment_old = json_segment_create_linear(data_offset, tmp ? &tmp : NULL, 0); |
2566 | 0 | } |
2567 | | |
2568 | 0 | if (!jobj_segment_old) { |
2569 | 0 | r = -EINVAL; |
2570 | 0 | goto err; |
2571 | 0 | } |
2572 | | |
2573 | 0 | r = LUKS2_segment_set_flag(jobj_segment_old, "backup-previous"); |
2574 | 0 | if (r) |
2575 | 0 | goto err; |
2576 | | |
2577 | 0 | r = json_object_object_add_by_uint_by_ref(LUKS2_get_segments_jobj(hdr), segment, &jobj_segment_old); |
2578 | 0 | if (r) |
2579 | 0 | goto err; |
2580 | | |
2581 | 0 | if (digest_old >= 0 && (r = LUKS2_digest_segment_assign(cd, hdr, segment, digest_old, 1, 0))) |
2582 | 0 | goto err; |
2583 | | |
2584 | 0 | segment++; |
2585 | |
|
2586 | 0 | if (digest_new >= 0) { |
2587 | 0 | segment_offset = data_offset; |
2588 | 0 | if (params->mode != CRYPT_REENCRYPT_ENCRYPT && |
2589 | 0 | modify_offset(&segment_offset, data_shift, params->direction)) { |
2590 | 0 | r = -EINVAL; |
2591 | 0 | goto err; |
2592 | 0 | } |
2593 | 0 | jobj_segment_new = json_segment_create_crypt(segment_offset, |
2594 | 0 | crypt_get_iv_offset(cd), |
2595 | 0 | NULL, cipher, NULL, 0, sector_size, 0); |
2596 | 0 | } else if (params->mode == CRYPT_REENCRYPT_DECRYPT) { |
2597 | 0 | segment_offset = data_offset; |
2598 | 0 | if (modify_offset(&segment_offset, data_shift, params->direction)) { |
2599 | 0 | r = -EINVAL; |
2600 | 0 | goto err; |
2601 | 0 | } |
2602 | 0 | jobj_segment_new = json_segment_create_linear(segment_offset, NULL, 0); |
2603 | 0 | } |
2604 | | |
2605 | 0 | if (!jobj_segment_new) { |
2606 | 0 | r = -EINVAL; |
2607 | 0 | goto err; |
2608 | 0 | } |
2609 | | |
2610 | 0 | r = LUKS2_segment_set_flag(jobj_segment_new, "backup-final"); |
2611 | 0 | if (r) |
2612 | 0 | goto err; |
2613 | | |
2614 | 0 | r = json_object_object_add_by_uint_by_ref(LUKS2_get_segments_jobj(hdr), segment, &jobj_segment_new); |
2615 | 0 | if (r) |
2616 | 0 | goto err; |
2617 | | |
2618 | 0 | if (digest_new >= 0 && (r = LUKS2_digest_segment_assign(cd, hdr, segment, digest_new, 1, 0))) |
2619 | 0 | goto err; |
2620 | | |
2621 | | /* FIXME: also check occupied space by keyslot in shrunk area */ |
2622 | 0 | if (params->direction == CRYPT_REENCRYPT_FORWARD && data_shift && |
2623 | 0 | crypt_metadata_device(cd) == crypt_data_device(cd) && |
2624 | 0 | LUKS2_set_keyslots_size(hdr, json_segment_get_offset(reencrypt_segment_new(hdr), 0))) { |
2625 | 0 | log_err(cd, _("Failed to set new keyslots area size.")); |
2626 | 0 | r = -EINVAL; |
2627 | 0 | goto err; |
2628 | 0 | } |
2629 | | |
2630 | 0 | return 0; |
2631 | 0 | err: |
2632 | 0 | json_object_put(jobj_segment_new); |
2633 | 0 | json_object_put(jobj_segment_old); |
2634 | 0 | json_object_put(jobj_segment_bcp); |
2635 | 0 | return r; |
2636 | 0 | } |
2637 | | |
2638 | | static int reencrypt_verify_single_key(struct crypt_device *cd, int digest, struct volume_key *vks) |
2639 | 0 | { |
2640 | 0 | struct volume_key *vk; |
2641 | |
|
2642 | 0 | vk = crypt_volume_key_by_id(vks, digest); |
2643 | 0 | if (!vk) |
2644 | 0 | return -ENOENT; |
2645 | | |
2646 | 0 | if (LUKS2_digest_verify_by_digest(cd, digest, vk) != digest) |
2647 | 0 | return -EINVAL; |
2648 | | |
2649 | 0 | return 0; |
2650 | 0 | } |
2651 | | |
2652 | | static int reencrypt_verify_keys(struct crypt_device *cd, |
2653 | | int digest_old, |
2654 | | int digest_new, |
2655 | | struct volume_key *vks) |
2656 | 0 | { |
2657 | 0 | int r; |
2658 | |
|
2659 | 0 | if (digest_new >= 0 && (r = reencrypt_verify_single_key(cd, digest_new, vks))) |
2660 | 0 | return r; |
2661 | | |
2662 | 0 | if (digest_old >= 0 && (r = reencrypt_verify_single_key(cd, digest_old, vks))) |
2663 | 0 | return r; |
2664 | | |
2665 | 0 | return 0; |
2666 | 0 | } |
2667 | | |
2668 | | static int reencrypt_upload_single_key(struct crypt_device *cd, |
2669 | | int digest, |
2670 | | struct volume_key *vks) |
2671 | 0 | { |
2672 | 0 | struct volume_key *vk; |
2673 | |
|
2674 | 0 | vk = crypt_volume_key_by_id(vks, digest); |
2675 | 0 | if (!vk) |
2676 | 0 | return -EINVAL; |
2677 | | |
2678 | 0 | return LUKS2_volume_key_load_in_keyring_by_digest(cd, vk, digest); |
2679 | 0 | } |
2680 | | |
2681 | | static int reencrypt_upload_keys(struct crypt_device *cd, |
2682 | | struct luks2_hdr *hdr, |
2683 | | int digest_old, |
2684 | | int digest_new, |
2685 | | struct volume_key *vks) |
2686 | 0 | { |
2687 | 0 | int r; |
2688 | |
|
2689 | 0 | if (!crypt_use_keyring_for_vk(cd)) |
2690 | 0 | return 0; |
2691 | | |
2692 | 0 | if (digest_new >= 0 && !crypt_is_cipher_null(reencrypt_segment_cipher_new(hdr)) && |
2693 | 0 | (r = reencrypt_upload_single_key(cd, digest_new, vks))) |
2694 | 0 | return r; |
2695 | | |
2696 | 0 | if (digest_old >= 0 && !crypt_is_cipher_null(reencrypt_segment_cipher_old(hdr)) && |
2697 | 0 | (r = reencrypt_upload_single_key(cd, digest_old, vks))) { |
2698 | 0 | crypt_drop_uploaded_keyring_key(cd, vks); |
2699 | 0 | return r; |
2700 | 0 | } |
2701 | | |
2702 | 0 | return 0; |
2703 | 0 | } |
2704 | | |
2705 | | static int reencrypt_verify_and_upload_keys(struct crypt_device *cd, |
2706 | | struct luks2_hdr *hdr, |
2707 | | int digest_old, |
2708 | | int digest_new, |
2709 | | struct volume_key *vks) |
2710 | 0 | { |
2711 | 0 | int r; |
2712 | |
|
2713 | 0 | r = reencrypt_verify_keys(cd, digest_old, digest_new, vks); |
2714 | 0 | if (r) |
2715 | 0 | return r; |
2716 | | |
2717 | 0 | r = reencrypt_upload_keys(cd, hdr, digest_old, digest_new, vks); |
2718 | 0 | if (r) |
2719 | 0 | return r; |
2720 | | |
2721 | 0 | return 0; |
2722 | 0 | } |
2723 | | |
2724 | | static int reencrypt_verify_checksum_params(struct crypt_device *cd, |
2725 | | const struct crypt_params_reencrypt *params) |
2726 | 0 | { |
2727 | 0 | size_t len; |
2728 | 0 | struct crypt_hash *ch; |
2729 | |
|
2730 | 0 | assert(params); |
2731 | |
|
2732 | 0 | if (!params->hash) |
2733 | 0 | return -EINVAL; |
2734 | | |
2735 | 0 | len = strlen(params->hash); |
2736 | 0 | if (!len || len > (LUKS2_CHECKSUM_ALG_L - 1)) |
2737 | 0 | return -EINVAL; |
2738 | | |
2739 | 0 | if (crypt_hash_size(params->hash) <= 0) |
2740 | 0 | return -EINVAL; |
2741 | | |
2742 | 0 | if (crypt_hash_init(&ch, params->hash)) { |
2743 | 0 | log_err(cd, _("Hash algorithm %s is not available."), params->hash); |
2744 | 0 | return -EINVAL; |
2745 | 0 | } |
2746 | | /* We just check for alg availability */ |
2747 | 0 | crypt_hash_destroy(ch); |
2748 | |
|
2749 | 0 | return 0; |
2750 | 0 | } |
2751 | | |
2752 | | static int reencrypt_verify_datashift_params(struct crypt_device *cd, |
2753 | | const struct crypt_params_reencrypt *params, |
2754 | | uint32_t sector_size) |
2755 | 0 | { |
2756 | 0 | assert(params); |
2757 | |
|
2758 | 0 | if (!params->data_shift) |
2759 | 0 | return -EINVAL; |
2760 | 0 | if (MISALIGNED(params->data_shift, sector_size >> SECTOR_SHIFT)) { |
2761 | 0 | log_err(cd, _("Data shift value is not aligned to encryption sector size (%" PRIu32 " bytes)."), |
2762 | 0 | sector_size); |
2763 | 0 | return -EINVAL; |
2764 | 0 | } |
2765 | | |
2766 | 0 | return 0; |
2767 | 0 | } |
2768 | | |
2769 | | static int reencrypt_verify_resilience_params(struct crypt_device *cd, |
2770 | | const struct crypt_params_reencrypt *params, |
2771 | | uint32_t sector_size, bool move_first_segment) |
2772 | 0 | { |
2773 | | /* no change requested */ |
2774 | 0 | if (!params || !params->resilience) |
2775 | 0 | return 0; |
2776 | | |
2777 | 0 | if (!strcmp(params->resilience, "journal")) |
2778 | 0 | return (params->data_shift || move_first_segment) ? -EINVAL : 0; |
2779 | 0 | else if (!strcmp(params->resilience, "none")) |
2780 | 0 | return (params->data_shift || move_first_segment) ? -EINVAL : 0; |
2781 | 0 | else if (!strcmp(params->resilience, "datashift")) |
2782 | 0 | return reencrypt_verify_datashift_params(cd, params, sector_size); |
2783 | 0 | else if (!strcmp(params->resilience, "checksum")) { |
2784 | 0 | if (params->data_shift || move_first_segment) |
2785 | 0 | return -EINVAL; |
2786 | 0 | return reencrypt_verify_checksum_params(cd, params); |
2787 | 0 | } else if (!strcmp(params->resilience, "datashift-checksum")) { |
2788 | 0 | if (!move_first_segment || |
2789 | 0 | reencrypt_verify_datashift_params(cd, params, sector_size)) |
2790 | 0 | return -EINVAL; |
2791 | 0 | return reencrypt_verify_checksum_params(cd, params); |
2792 | 0 | } else if (!strcmp(params->resilience, "datashift-journal")) { |
2793 | 0 | if (!move_first_segment) |
2794 | 0 | return -EINVAL; |
2795 | 0 | return reencrypt_verify_datashift_params(cd, params, sector_size); |
2796 | 0 | } |
2797 | | |
2798 | 0 | log_err(cd, _("Unsupported resilience mode %s"), params->resilience); |
2799 | 0 | return -EINVAL; |
2800 | 0 | } |
2801 | | |
2802 | | static int reencrypt_decrypt_with_datashift_init(struct crypt_device *cd, |
2803 | | const char *name, |
2804 | | struct luks2_hdr *hdr, |
2805 | | int reencrypt_keyslot, |
2806 | | uint32_t sector_size, |
2807 | | uint64_t data_size, |
2808 | | uint64_t data_offset, |
2809 | | struct crypt_keyslot_context *kc_old, |
2810 | | int keyslot_old, |
2811 | | const struct crypt_params_reencrypt *params, |
2812 | | struct volume_key **vks) |
2813 | 0 | { |
2814 | 0 | bool clear_table = false; |
2815 | 0 | int r, devfd = -1; |
2816 | 0 | uint64_t data_shift, max_moved_segment_length, moved_segment_length; |
2817 | 0 | struct reenc_protection check_rp = {}; |
2818 | 0 | struct crypt_dm_active_device dmd_target, dmd_source = { |
2819 | 0 | .uuid = crypt_get_uuid(cd), |
2820 | 0 | .flags = CRYPT_ACTIVATE_SHARED /* turn off exclusive open checks */ |
2821 | 0 | }; |
2822 | 0 | json_object *jobj_segments_old; |
2823 | |
|
2824 | 0 | assert(hdr); |
2825 | 0 | assert(params); |
2826 | 0 | assert(params->resilience); |
2827 | 0 | assert(params->data_shift); |
2828 | 0 | assert(vks); |
2829 | |
|
2830 | 0 | if (!data_offset) |
2831 | 0 | return -EINVAL; |
2832 | | |
2833 | 0 | if (params->max_hotzone_size > params->data_shift) { |
2834 | 0 | log_err(cd, _("Moved segment size can not be greater than data shift value.")); |
2835 | 0 | return -EINVAL; |
2836 | 0 | } |
2837 | | |
2838 | 0 | log_dbg(cd, "Initializing decryption with datashift."); |
2839 | |
|
2840 | 0 | data_shift = params->data_shift << SECTOR_SHIFT; |
2841 | | |
2842 | | /* |
2843 | | * In offline mode we must perform data move with exclusively opened data |
2844 | | * device in order to exclude LUKS2 decryption process and filesystem mount. |
2845 | | */ |
2846 | 0 | if (name) |
2847 | 0 | devfd = device_open(cd, crypt_data_device(cd), O_RDWR); |
2848 | 0 | else |
2849 | 0 | devfd = device_open_excl(cd, crypt_data_device(cd), O_RDWR); |
2850 | 0 | if (devfd < 0) |
2851 | 0 | return -EINVAL; |
2852 | | |
2853 | | /* in-memory only */ |
2854 | 0 | moved_segment_length = params->max_hotzone_size << SECTOR_SHIFT; |
2855 | 0 | if (!moved_segment_length) |
2856 | 0 | moved_segment_length = data_shift < LUKS2_DEFAULT_NONE_REENCRYPTION_LENGTH ? |
2857 | 0 | data_shift : LUKS2_DEFAULT_NONE_REENCRYPTION_LENGTH; |
2858 | |
|
2859 | 0 | if (moved_segment_length > data_size) |
2860 | 0 | moved_segment_length = data_size; |
2861 | |
|
2862 | 0 | r = reencrypt_set_decrypt_shift_segments(cd, hdr, data_size, |
2863 | 0 | moved_segment_length, |
2864 | 0 | params->direction); |
2865 | 0 | if (r) |
2866 | 0 | goto out; |
2867 | | |
2868 | 0 | r = reencrypt_make_backup_segments(cd, hdr, CRYPT_ANY_DIGEST, NULL, data_offset, params); |
2869 | 0 | if (r) { |
2870 | 0 | log_dbg(cd, "Failed to create reencryption backup device segments."); |
2871 | 0 | goto out; |
2872 | 0 | } |
2873 | | |
2874 | 0 | r = reencrypt_verify_resilience_params(cd, params, sector_size, true); |
2875 | 0 | if (r < 0) { |
2876 | 0 | log_err(cd, _("Invalid reencryption resilience parameters.")); |
2877 | 0 | goto out; |
2878 | 0 | } |
2879 | | |
2880 | 0 | r = LUKS2_keyslot_reencrypt_allocate(cd, hdr, reencrypt_keyslot, |
2881 | 0 | params, reencrypt_get_alignment(cd, hdr)); |
2882 | 0 | if (r < 0) |
2883 | 0 | goto out; |
2884 | | |
2885 | 0 | r = LUKS2_keyslot_reencrypt_load(cd, hdr, reencrypt_keyslot, &check_rp, false); |
2886 | 0 | if (r < 0) |
2887 | 0 | goto out; |
2888 | | |
2889 | 0 | r = LUKS2_reencrypt_max_hotzone_size(cd, hdr, &check_rp, |
2890 | 0 | reencrypt_keyslot, |
2891 | 0 | &max_moved_segment_length); |
2892 | 0 | if (r < 0) |
2893 | 0 | goto out; |
2894 | | |
2895 | 0 | LUKS2_reencrypt_protection_erase(&check_rp); |
2896 | |
|
2897 | 0 | if (moved_segment_length > max_moved_segment_length) { |
2898 | 0 | log_err(cd, _("Moved segment too large. Requested size %" PRIu64 ", available space for: %" PRIu64 "."), |
2899 | 0 | moved_segment_length, max_moved_segment_length); |
2900 | 0 | r = -EINVAL; |
2901 | 0 | goto out; |
2902 | 0 | } |
2903 | | |
2904 | 0 | r = LUKS2_keyslot_context_open_all_segments(cd, keyslot_old, CRYPT_ANY_SLOT, |
2905 | 0 | kc_old, NULL, vks); |
2906 | 0 | if (r < 0) |
2907 | 0 | goto out; |
2908 | | |
2909 | 0 | r = LUKS2_keyslot_reencrypt_digest_create(cd, hdr, LUKS2_DECRYPT_DATASHIFT_REQ_VERSION, *vks); |
2910 | 0 | if (r < 0) |
2911 | 0 | goto out; |
2912 | | |
2913 | 0 | if (name) { |
2914 | 0 | r = reencrypt_verify_and_upload_keys(cd, hdr, |
2915 | 0 | LUKS2_reencrypt_digest_old(hdr), |
2916 | 0 | LUKS2_reencrypt_digest_new(hdr), |
2917 | 0 | *vks); |
2918 | 0 | if (r) |
2919 | 0 | goto out; |
2920 | | |
2921 | 0 | r = dm_query_device(cd, name, DM_ACTIVE_UUID | DM_ACTIVE_DEVICE | |
2922 | 0 | DM_ACTIVE_CRYPT_KEYSIZE | DM_ACTIVE_CRYPT_KEY | |
2923 | 0 | DM_ACTIVE_CRYPT_CIPHER, &dmd_target); |
2924 | 0 | if (r < 0) |
2925 | 0 | goto out; |
2926 | | |
2927 | 0 | jobj_segments_old = reencrypt_segments_old(hdr); |
2928 | 0 | if (!jobj_segments_old) { |
2929 | 0 | dm_targets_free(cd, &dmd_target); |
2930 | 0 | free(CONST_CAST(void*)dmd_target.uuid); |
2931 | 0 | r = -EINVAL; |
2932 | 0 | goto out; |
2933 | 0 | } |
2934 | 0 | r = LUKS2_assembly_multisegment_dmd(cd, hdr, *vks, jobj_segments_old, &dmd_source); |
2935 | 0 | if (!r) { |
2936 | 0 | r = crypt_compare_dm_devices(cd, &dmd_source, &dmd_target); |
2937 | 0 | if (r) |
2938 | 0 | log_err(cd, _("Mismatching parameters on device %s."), name); |
2939 | 0 | } |
2940 | 0 | json_object_put(jobj_segments_old); |
2941 | |
|
2942 | 0 | dm_targets_free(cd, &dmd_source); |
2943 | 0 | dm_targets_free(cd, &dmd_target); |
2944 | 0 | free(CONST_CAST(void*)dmd_target.uuid); |
2945 | |
|
2946 | 0 | if (r) |
2947 | 0 | goto out; |
2948 | | |
2949 | 0 | dmd_source.size = dmd_target.size; |
2950 | 0 | r = LUKS2_assembly_multisegment_dmd(cd, hdr, *vks, LUKS2_get_segments_jobj(hdr), &dmd_source); |
2951 | 0 | if (!r) { |
2952 | 0 | r = dm_reload_device(cd, name, &dmd_source, dmd_target.flags, 0); |
2953 | 0 | if (r) |
2954 | 0 | log_err(cd, _("Failed to reload device %s."), name); |
2955 | 0 | else |
2956 | 0 | clear_table = true; |
2957 | 0 | } |
2958 | |
|
2959 | 0 | dm_targets_free(cd, &dmd_source); |
2960 | |
|
2961 | 0 | if (r) |
2962 | 0 | goto out; |
2963 | 0 | } |
2964 | | |
2965 | 0 | if (name) { |
2966 | 0 | r = dm_suspend_device(cd, name, DM_SUSPEND_SKIP_LOCKFS); |
2967 | 0 | if (r) { |
2968 | 0 | log_err(cd, _("Failed to suspend device %s."), name); |
2969 | 0 | goto out; |
2970 | 0 | } |
2971 | 0 | } |
2972 | | |
2973 | 0 | if (reencrypt_move_data(cd, devfd, data_shift, params->mode)) { |
2974 | 0 | r = -EIO; |
2975 | 0 | goto out; |
2976 | 0 | } |
2977 | | |
2978 | | /* This must be first and only write in LUKS2 metadata during _reencrypt_init */ |
2979 | 0 | r = reencrypt_update_flag(cd, LUKS2_DECRYPT_DATASHIFT_REQ_VERSION, true, true); |
2980 | 0 | if (r) { |
2981 | 0 | log_dbg(cd, "Failed to set online-reencryption requirement."); |
2982 | 0 | r = -EINVAL; |
2983 | 0 | } else |
2984 | 0 | r = reencrypt_keyslot; |
2985 | 0 | out: |
2986 | 0 | if (r < 0 && clear_table && dm_clear_device(cd, name)) |
2987 | 0 | log_err(cd, _("Failed to clear table.")); |
2988 | 0 | else if (clear_table && dm_resume_device(cd, name, DM_SUSPEND_SKIP_LOCKFS)) |
2989 | 0 | log_err(cd, _("Failed to resume device %s."), name); |
2990 | |
|
2991 | 0 | device_release_excl(cd, crypt_data_device(cd)); |
2992 | 0 | if (r < 0 && LUKS2_hdr_rollback(cd, hdr) < 0) |
2993 | 0 | log_dbg(cd, "Failed to rollback LUKS2 metadata after failure."); |
2994 | |
|
2995 | 0 | return r; |
2996 | 0 | } |
2997 | | |
2998 | | /* This function must be called with metadata lock held */ |
2999 | | static int reencrypt_init(struct crypt_device *cd, |
3000 | | const char *name, |
3001 | | struct luks2_hdr *hdr, |
3002 | | struct crypt_keyslot_context *kc_old, |
3003 | | struct crypt_keyslot_context *kc_new, |
3004 | | int keyslot_old, |
3005 | | int keyslot_new, |
3006 | | const char *cipher, |
3007 | | const char *cipher_mode, |
3008 | | const struct crypt_params_reencrypt *params, |
3009 | | struct volume_key **vks) |
3010 | 0 | { |
3011 | 0 | bool move_first_segment; |
3012 | 0 | char _cipher[128]; |
3013 | 0 | uint32_t check_sector_size, new_sector_size, old_sector_size; |
3014 | 0 | int digest_new, r, reencrypt_keyslot, devfd = -1; |
3015 | 0 | uint64_t data_offset_bytes, data_size_bytes, data_shift_bytes, device_size_bytes; |
3016 | 0 | struct volume_key *vk; |
3017 | 0 | struct crypt_dm_active_device dmd_target, dmd_source = { |
3018 | 0 | .uuid = crypt_get_uuid(cd), |
3019 | 0 | .flags = CRYPT_ACTIVATE_SHARED /* turn off exclusive open checks */ |
3020 | 0 | }; |
3021 | |
|
3022 | 0 | assert(cd); |
3023 | 0 | assert(hdr); |
3024 | |
|
3025 | 0 | if (!params || !params->resilience || params->mode > CRYPT_REENCRYPT_DECRYPT) |
3026 | 0 | return -EINVAL; |
3027 | | |
3028 | 0 | if (params->mode != CRYPT_REENCRYPT_DECRYPT && |
3029 | 0 | (!params->luks2 || !(cipher && cipher_mode) || |
3030 | 0 | (keyslot_new < 0 && !(params->flags & CRYPT_REENCRYPT_CREATE_NEW_DIGEST)))) |
3031 | 0 | return -EINVAL; |
3032 | | |
3033 | 0 | log_dbg(cd, "Initializing reencryption (mode: %s) in LUKS2 metadata.", |
3034 | 0 | crypt_reencrypt_mode_to_str(params->mode)); |
3035 | |
|
3036 | 0 | move_first_segment = (params->flags & CRYPT_REENCRYPT_MOVE_FIRST_SEGMENT); |
3037 | |
|
3038 | 0 | old_sector_size = LUKS2_get_sector_size(hdr); |
3039 | | |
3040 | | /* implicit sector size 512 for decryption */ |
3041 | 0 | new_sector_size = params->luks2 ? params->luks2->sector_size : SECTOR_SIZE; |
3042 | 0 | if (new_sector_size < SECTOR_SIZE || new_sector_size > MAX_SECTOR_SIZE || |
3043 | 0 | NOTPOW2(new_sector_size)) { |
3044 | 0 | log_err(cd, _("Unsupported encryption sector size.")); |
3045 | 0 | return -EINVAL; |
3046 | 0 | } |
3047 | | /* check the larger encryption sector size only */ |
3048 | 0 | check_sector_size = new_sector_size > old_sector_size ? new_sector_size : old_sector_size; |
3049 | |
|
3050 | 0 | if (!cipher_mode || *cipher_mode == '\0') |
3051 | 0 | r = snprintf(_cipher, sizeof(_cipher), "%s", cipher); |
3052 | 0 | else |
3053 | 0 | r = snprintf(_cipher, sizeof(_cipher), "%s-%s", cipher, cipher_mode); |
3054 | 0 | if (r < 0 || (size_t)r >= sizeof(_cipher)) |
3055 | 0 | return -EINVAL; |
3056 | | |
3057 | 0 | data_offset_bytes = LUKS2_get_data_offset(hdr) << SECTOR_SHIFT; |
3058 | |
|
3059 | 0 | r = device_check_access(cd, crypt_data_device(cd), DEV_OK); |
3060 | 0 | if (r) |
3061 | 0 | return r; |
3062 | | |
3063 | 0 | r = device_check_size(cd, crypt_data_device(cd), data_offset_bytes, 1); |
3064 | 0 | if (r) |
3065 | 0 | return r; |
3066 | | |
3067 | 0 | r = device_size(crypt_data_device(cd), &device_size_bytes); |
3068 | 0 | if (r) |
3069 | 0 | return r; |
3070 | | |
3071 | 0 | if (move_first_segment && params->mode == CRYPT_REENCRYPT_ENCRYPT && |
3072 | 0 | params->data_shift < LUKS2_get_data_offset(hdr)) { |
3073 | 0 | log_err(cd, _("Data shift (%" PRIu64 " sectors) is less than future data offset (%" PRIu64 " sectors)."), |
3074 | 0 | params->data_shift, LUKS2_get_data_offset(hdr)); |
3075 | 0 | return -EINVAL; |
3076 | 0 | } |
3077 | | |
3078 | 0 | device_size_bytes -= data_offset_bytes; |
3079 | 0 | data_shift_bytes = params->data_shift << SECTOR_SHIFT; |
3080 | 0 | data_size_bytes = params->device_size << SECTOR_SHIFT; |
3081 | |
|
3082 | 0 | if (device_size_bytes < data_shift_bytes && params->direction == CRYPT_REENCRYPT_BACKWARD) { |
3083 | 0 | log_err(cd, _("Device %s is too small."), device_path(crypt_data_device(cd))); |
3084 | 0 | return -EINVAL; |
3085 | 0 | } |
3086 | | |
3087 | 0 | if (data_size_bytes > device_size_bytes) { |
3088 | 0 | log_err(cd, _("Reduced data size is larger than real device size.")); |
3089 | 0 | return -EINVAL; |
3090 | 0 | } |
3091 | | |
3092 | 0 | if (data_size_bytes && params->mode == CRYPT_REENCRYPT_ENCRYPT && |
3093 | 0 | move_first_segment && data_shift_bytes) { |
3094 | 0 | if (data_size_bytes > device_size_bytes - data_shift_bytes) { |
3095 | 0 | log_err(cd, _("Reduced data size is larger than real device size.")); |
3096 | 0 | return -EINVAL; |
3097 | 0 | } |
3098 | 0 | } else if (!data_size_bytes && params->mode == CRYPT_REENCRYPT_ENCRYPT && |
3099 | 0 | move_first_segment && data_shift_bytes) |
3100 | 0 | data_size_bytes = device_size_bytes - data_shift_bytes; |
3101 | 0 | else if (!data_size_bytes) |
3102 | 0 | data_size_bytes = device_size_bytes; |
3103 | | |
3104 | 0 | if (MISALIGNED(data_size_bytes, check_sector_size)) { |
3105 | 0 | log_err(cd, _("Data device is not aligned to encryption sector size (%" PRIu32 " bytes)."), check_sector_size); |
3106 | 0 | return -EINVAL; |
3107 | 0 | } |
3108 | | |
3109 | 0 | reencrypt_keyslot = LUKS2_keyslot_find_empty(cd, hdr, 0); |
3110 | 0 | if (reencrypt_keyslot < 0) { |
3111 | 0 | log_err(cd, _("All key slots full.")); |
3112 | 0 | return -EINVAL; |
3113 | 0 | } |
3114 | | |
3115 | 0 | if (params->mode == CRYPT_REENCRYPT_DECRYPT && data_shift_bytes && move_first_segment) |
3116 | 0 | return reencrypt_decrypt_with_datashift_init(cd, name, hdr, |
3117 | 0 | reencrypt_keyslot, |
3118 | 0 | check_sector_size, |
3119 | 0 | data_size_bytes, |
3120 | 0 | data_offset_bytes, |
3121 | 0 | kc_old, |
3122 | 0 | keyslot_old, |
3123 | 0 | params, |
3124 | 0 | vks); |
3125 | | |
3126 | | /* |
3127 | | * We must perform data move with exclusive open data device |
3128 | | * to exclude another cryptsetup process to colide with |
3129 | | * encryption initialization (or mount) |
3130 | | */ |
3131 | 0 | if (move_first_segment) { |
3132 | 0 | devfd = device_open_excl(cd, crypt_data_device(cd), O_RDWR); |
3133 | 0 | if (devfd < 0) { |
3134 | 0 | if (devfd == -EBUSY) |
3135 | 0 | log_err(cd,_("Failed to open %s in exclusive mode (already mapped or mounted)."), |
3136 | 0 | device_path(crypt_data_device(cd))); |
3137 | 0 | return -EINVAL; |
3138 | 0 | } |
3139 | 0 | } |
3140 | | |
3141 | 0 | if (params->mode == CRYPT_REENCRYPT_ENCRYPT) { |
3142 | | /* in-memory only */ |
3143 | 0 | r = reencrypt_set_encrypt_segments(cd, hdr, device_size_bytes, data_size_bytes, |
3144 | 0 | data_shift_bytes, |
3145 | 0 | move_first_segment, |
3146 | 0 | params->direction); |
3147 | 0 | if (r) |
3148 | 0 | goto out; |
3149 | 0 | } |
3150 | | |
3151 | 0 | if (params->flags & CRYPT_REENCRYPT_CREATE_NEW_DIGEST) { |
3152 | 0 | assert(kc_new->get_luks2_key); |
3153 | 0 | r = kc_new->get_luks2_key(cd, kc_new, CRYPT_ANY_SLOT, CRYPT_ANY_SEGMENT, &vk); |
3154 | 0 | if (r < 0) |
3155 | 0 | goto out; |
3156 | | |
3157 | | /* do not create new digest in case it matches the current one */ |
3158 | 0 | r = LUKS2_digest_verify_by_segment(cd, hdr, CRYPT_DEFAULT_SEGMENT, vk); |
3159 | 0 | if (r == -EPERM || r == -ENOENT) |
3160 | 0 | r = LUKS2_digest_create(cd, "pbkdf2", hdr, vk); |
3161 | |
|
3162 | 0 | crypt_free_volume_key(vk); |
3163 | 0 | if (r < 0) |
3164 | 0 | goto out; |
3165 | 0 | digest_new = r; |
3166 | 0 | } else |
3167 | 0 | digest_new = LUKS2_digest_by_keyslot(hdr, keyslot_new); |
3168 | | |
3169 | 0 | r = reencrypt_make_backup_segments(cd, hdr, digest_new, _cipher, data_offset_bytes, params); |
3170 | 0 | if (r) { |
3171 | 0 | log_dbg(cd, "Failed to create reencryption backup device segments."); |
3172 | 0 | goto out; |
3173 | 0 | } |
3174 | | |
3175 | 0 | r = reencrypt_verify_resilience_params(cd, params, check_sector_size, move_first_segment); |
3176 | 0 | if (r < 0) |
3177 | 0 | goto out; |
3178 | | |
3179 | 0 | r = LUKS2_keyslot_reencrypt_allocate(cd, hdr, reencrypt_keyslot, params, |
3180 | 0 | reencrypt_get_alignment(cd, hdr)); |
3181 | 0 | if (r < 0) |
3182 | 0 | goto out; |
3183 | | |
3184 | 0 | r = LUKS2_keyslot_context_open_all_segments(cd, keyslot_old, keyslot_new, kc_old, kc_new, vks); |
3185 | 0 | if (r < 0) |
3186 | 0 | goto out; |
3187 | | |
3188 | 0 | r = LUKS2_keyslot_reencrypt_digest_create(cd, hdr, LUKS2_REENCRYPT_REQ_VERSION, *vks); |
3189 | 0 | if (r < 0) |
3190 | 0 | goto out; |
3191 | | |
3192 | 0 | if (name && params->mode != CRYPT_REENCRYPT_ENCRYPT) { |
3193 | 0 | r = reencrypt_verify_and_upload_keys(cd, hdr, LUKS2_reencrypt_digest_old(hdr), LUKS2_reencrypt_digest_new(hdr), *vks); |
3194 | 0 | if (r) |
3195 | 0 | goto out; |
3196 | | |
3197 | 0 | r = dm_query_device(cd, name, DM_ACTIVE_UUID | DM_ACTIVE_DEVICE | |
3198 | 0 | DM_ACTIVE_CRYPT_KEYSIZE | DM_ACTIVE_CRYPT_KEY | |
3199 | 0 | DM_ACTIVE_CRYPT_CIPHER, &dmd_target); |
3200 | 0 | if (r < 0) |
3201 | 0 | goto out; |
3202 | | |
3203 | 0 | r = LUKS2_assembly_multisegment_dmd(cd, hdr, *vks, LUKS2_get_segments_jobj(hdr), &dmd_source); |
3204 | 0 | if (!r) { |
3205 | 0 | r = crypt_compare_dm_devices(cd, &dmd_source, &dmd_target); |
3206 | 0 | if (r) |
3207 | 0 | log_err(cd, _("Mismatching parameters on device %s."), name); |
3208 | 0 | } |
3209 | |
|
3210 | 0 | dm_targets_free(cd, &dmd_source); |
3211 | 0 | dm_targets_free(cd, &dmd_target); |
3212 | 0 | free(CONST_CAST(void*)dmd_target.uuid); |
3213 | |
|
3214 | 0 | if (r) |
3215 | 0 | goto out; |
3216 | 0 | } |
3217 | | |
3218 | 0 | if (move_first_segment && reencrypt_move_data(cd, devfd, data_shift_bytes, params->mode)) { |
3219 | 0 | r = -EIO; |
3220 | 0 | goto out; |
3221 | 0 | } |
3222 | | |
3223 | | /* This must be first and only write in LUKS2 metadata during reencrypt_init */ |
3224 | 0 | r = reencrypt_update_flag(cd, LUKS2_REENCRYPT_REQ_VERSION, true, true); |
3225 | 0 | if (r) { |
3226 | 0 | log_dbg(cd, "Failed to set online-reencryption requirement."); |
3227 | 0 | r = -EINVAL; |
3228 | 0 | } else |
3229 | 0 | r = reencrypt_keyslot; |
3230 | 0 | out: |
3231 | 0 | device_release_excl(cd, crypt_data_device(cd)); |
3232 | 0 | if (r < 0 && LUKS2_hdr_rollback(cd, hdr) < 0) |
3233 | 0 | log_dbg(cd, "Failed to rollback LUKS2 metadata after failure."); |
3234 | |
|
3235 | 0 | return r; |
3236 | 0 | } |
3237 | | |
3238 | | static int reencrypt_hotzone_protect_final(struct crypt_device *cd, |
3239 | | struct luks2_hdr *hdr, int reencrypt_keyslot, |
3240 | | const struct reenc_protection *rp, |
3241 | | const void *buffer, size_t buffer_len) |
3242 | 0 | { |
3243 | 0 | const void *pbuffer; |
3244 | 0 | size_t data_offset, len; |
3245 | 0 | int r; |
3246 | |
|
3247 | 0 | assert(hdr); |
3248 | 0 | assert(rp); |
3249 | |
|
3250 | 0 | if (rp->type == REENC_PROTECTION_NONE) |
3251 | 0 | return 0; |
3252 | | |
3253 | 0 | if (rp->type == REENC_PROTECTION_CHECKSUM) { |
3254 | 0 | log_dbg(cd, "Checksums hotzone resilience."); |
3255 | |
|
3256 | 0 | for (data_offset = 0, len = 0; data_offset < buffer_len; data_offset += rp->p.csum.block_size, len += rp->p.csum.hash_size) { |
3257 | 0 | if (crypt_hash_write(rp->p.csum.ch, (const char *)buffer + data_offset, rp->p.csum.block_size)) { |
3258 | 0 | log_dbg(cd, "Failed to hash sector at offset %zu.", data_offset); |
3259 | 0 | return -EINVAL; |
3260 | 0 | } |
3261 | 0 | if (crypt_hash_final(rp->p.csum.ch, (char *)rp->p.csum.checksums + len, rp->p.csum.hash_size)) { |
3262 | 0 | log_dbg(cd, "Failed to finalize hash."); |
3263 | 0 | return -EINVAL; |
3264 | 0 | } |
3265 | 0 | } |
3266 | 0 | pbuffer = rp->p.csum.checksums; |
3267 | 0 | } else if (rp->type == REENC_PROTECTION_JOURNAL) { |
3268 | 0 | log_dbg(cd, "Journal hotzone resilience."); |
3269 | 0 | len = buffer_len; |
3270 | 0 | pbuffer = buffer; |
3271 | 0 | } else if (rp->type == REENC_PROTECTION_DATASHIFT) { |
3272 | 0 | log_dbg(cd, "Data shift hotzone resilience."); |
3273 | 0 | return LUKS2_hdr_write(cd, hdr); |
3274 | 0 | } else |
3275 | 0 | return -EINVAL; |
3276 | | |
3277 | 0 | log_dbg(cd, "Going to store %zu bytes in reencrypt keyslot.", len); |
3278 | |
|
3279 | 0 | r = LUKS2_keyslot_reencrypt_store(cd, hdr, reencrypt_keyslot, pbuffer, len); |
3280 | |
|
3281 | 0 | return r > 0 ? 0 : r; |
3282 | 0 | } |
3283 | | |
3284 | | static int reencrypt_context_update(struct crypt_device *cd, |
3285 | | struct luks2_reencrypt *rh) |
3286 | 0 | { |
3287 | 0 | if (rh->read < 0) |
3288 | 0 | return -EINVAL; |
3289 | | |
3290 | 0 | if (rh->direction == CRYPT_REENCRYPT_BACKWARD) { |
3291 | 0 | if (rh->rp.type == REENC_PROTECTION_DATASHIFT && rh->mode == CRYPT_REENCRYPT_ENCRYPT) { |
3292 | 0 | if (rh->offset) |
3293 | 0 | rh->offset -= data_shift_value(&rh->rp); |
3294 | 0 | if (rh->offset && (rh->offset < data_shift_value(&rh->rp))) { |
3295 | 0 | rh->length = rh->offset; |
3296 | 0 | rh->offset = data_shift_value(&rh->rp); |
3297 | 0 | } |
3298 | 0 | if (!rh->offset) |
3299 | 0 | rh->length = data_shift_value(&rh->rp); |
3300 | 0 | } else { |
3301 | 0 | if (rh->offset < rh->length) |
3302 | 0 | rh->length = rh->offset; |
3303 | 0 | rh->offset -= rh->length; |
3304 | 0 | } |
3305 | 0 | } else if (rh->direction == CRYPT_REENCRYPT_FORWARD) { |
3306 | 0 | rh->offset += (uint64_t)rh->read; |
3307 | 0 | if (rh->device_size == rh->offset && |
3308 | 0 | rh->jobj_segment_moved && |
3309 | 0 | rh->mode == CRYPT_REENCRYPT_DECRYPT && |
3310 | 0 | rh->rp.type == REENC_PROTECTION_DATASHIFT) { |
3311 | 0 | rh->offset = 0; |
3312 | 0 | rh->length = json_segment_get_size(rh->jobj_segment_moved, 0); |
3313 | 0 | } |
3314 | | /* it fails in-case of device_size < rh->offset later */ |
3315 | 0 | else if (rh->device_size - rh->offset < rh->length) |
3316 | 0 | rh->length = rh->device_size - rh->offset; |
3317 | 0 | } else |
3318 | 0 | return -EINVAL; |
3319 | | |
3320 | 0 | if (rh->device_size < rh->offset) { |
3321 | 0 | log_dbg(cd, "Calculated reencryption offset %" PRIu64 " is beyond device size %" PRIu64 ".", rh->offset, rh->device_size); |
3322 | 0 | return -EINVAL; |
3323 | 0 | } |
3324 | | |
3325 | 0 | rh->progress += (uint64_t)rh->read; |
3326 | |
|
3327 | 0 | return 0; |
3328 | 0 | } |
3329 | | |
3330 | | static int reencrypt_load(struct crypt_device *cd, struct luks2_hdr *hdr, |
3331 | | uint64_t device_size, |
3332 | | uint64_t max_hotzone_size, |
3333 | | uint64_t required_device_size, |
3334 | | struct volume_key *vks, |
3335 | | struct luks2_reencrypt **rh) |
3336 | 0 | { |
3337 | 0 | int r; |
3338 | 0 | struct luks2_reencrypt *tmp = NULL; |
3339 | 0 | crypt_reencrypt_info ri = LUKS2_reencrypt_status(hdr); |
3340 | |
|
3341 | 0 | if (ri == CRYPT_REENCRYPT_NONE) { |
3342 | 0 | log_err(cd, _("Device not marked for LUKS2 reencryption.")); |
3343 | 0 | return -EINVAL; |
3344 | 0 | } else if (ri == CRYPT_REENCRYPT_INVALID) |
3345 | 0 | return -EINVAL; |
3346 | | |
3347 | 0 | r = LUKS2_reencrypt_digest_verify(cd, hdr, vks); |
3348 | 0 | if (r < 0) |
3349 | 0 | return r; |
3350 | | |
3351 | 0 | if (ri == CRYPT_REENCRYPT_CLEAN) |
3352 | 0 | r = reencrypt_load_clean(cd, hdr, device_size, max_hotzone_size, required_device_size, &tmp); |
3353 | 0 | else if (ri == CRYPT_REENCRYPT_CRASH) |
3354 | 0 | r = reencrypt_load_crashed(cd, hdr, device_size, &tmp); |
3355 | 0 | else |
3356 | 0 | r = -EINVAL; |
3357 | |
|
3358 | 0 | if (r < 0 || !tmp) { |
3359 | 0 | log_err(cd, _("Failed to load LUKS2 reencryption context.")); |
3360 | 0 | return r < 0 ? r : -EINVAL; |
3361 | 0 | } |
3362 | | |
3363 | 0 | *rh = tmp; |
3364 | |
|
3365 | 0 | return 0; |
3366 | 0 | } |
3367 | | #else |
3368 | | int LUKS2_reencrypt_max_hotzone_size(struct crypt_device *cd __attribute__((unused)), |
3369 | | struct luks2_hdr *hdr __attribute__((unused)), |
3370 | | const struct reenc_protection *rp __attribute__((unused)), |
3371 | | int reencrypt_keyslot __attribute__((unused)), |
3372 | | uint64_t *r_length __attribute__((unused))) |
3373 | | { |
3374 | | return -ENOTSUP; |
3375 | | } |
3376 | | #endif |
3377 | | |
3378 | | static int reencrypt_lock_internal(struct crypt_device *cd, const char *uuid, struct crypt_lock_handle **reencrypt_lock) |
3379 | 0 | { |
3380 | 0 | int r; |
3381 | 0 | char *lock_resource; |
3382 | |
|
3383 | 0 | assert(uuid); |
3384 | |
|
3385 | 0 | if (!crypt_metadata_locking_enabled()) { |
3386 | 0 | *reencrypt_lock = NULL; |
3387 | 0 | return 0; |
3388 | 0 | } |
3389 | | |
3390 | 0 | r = asprintf(&lock_resource, "LUKS2-reencryption-%s", uuid); |
3391 | 0 | if (r < 0) |
3392 | 0 | return -ENOMEM; |
3393 | 0 | if (r < 20) { |
3394 | 0 | free(lock_resource); |
3395 | 0 | return -EINVAL; |
3396 | 0 | } |
3397 | | |
3398 | 0 | r = crypt_write_lock(cd, lock_resource, false, reencrypt_lock); |
3399 | |
|
3400 | 0 | free(lock_resource); |
3401 | |
|
3402 | 0 | return r; |
3403 | 0 | } |
3404 | | |
3405 | | /* internal only */ |
3406 | | int LUKS2_reencrypt_lock_by_dm_uuid(struct crypt_device *cd, const char *dm_uuid, |
3407 | | struct crypt_lock_handle **reencrypt_lock) |
3408 | 0 | { |
3409 | 0 | int r; |
3410 | 0 | char hdr_uuid[37]; |
3411 | 0 | const char *uuid = crypt_get_uuid(cd); |
3412 | |
|
3413 | 0 | if (!dm_uuid) |
3414 | 0 | return -EINVAL; |
3415 | | |
3416 | 0 | if (!uuid) { |
3417 | 0 | r = snprintf(hdr_uuid, sizeof(hdr_uuid), "%.8s-%.4s-%.4s-%.4s-%.12s", |
3418 | 0 | dm_uuid + 6, dm_uuid + 14, dm_uuid + 18, dm_uuid + 22, dm_uuid + 26); |
3419 | 0 | if (r < 0 || (size_t)r != (sizeof(hdr_uuid) - 1)) |
3420 | 0 | return -EINVAL; |
3421 | 0 | uuid = hdr_uuid; |
3422 | 0 | } else if (dm_uuid_cmp(dm_uuid, uuid)) |
3423 | 0 | return -EINVAL; |
3424 | | |
3425 | 0 | return reencrypt_lock_internal(cd, uuid, reencrypt_lock); |
3426 | 0 | } |
3427 | | |
3428 | | /* internal only */ |
3429 | | int LUKS2_reencrypt_lock(struct crypt_device *cd, struct crypt_lock_handle **reencrypt_lock) |
3430 | 0 | { |
3431 | 0 | if (!cd || !crypt_get_type(cd) || strcmp(crypt_get_type(cd), CRYPT_LUKS2)) |
3432 | 0 | return -EINVAL; |
3433 | | |
3434 | 0 | return reencrypt_lock_internal(cd, crypt_get_uuid(cd), reencrypt_lock); |
3435 | 0 | } |
3436 | | |
3437 | | /* internal only */ |
3438 | | void LUKS2_reencrypt_unlock(struct crypt_device *cd, struct crypt_lock_handle *reencrypt_lock) |
3439 | 0 | { |
3440 | 0 | crypt_unlock_internal(cd, reencrypt_lock); |
3441 | 0 | } |
3442 | | #if USE_LUKS2_REENCRYPTION |
3443 | | static int reencrypt_lock_and_verify(struct crypt_device *cd, struct luks2_hdr *hdr, |
3444 | | struct crypt_lock_handle **reencrypt_lock) |
3445 | 0 | { |
3446 | 0 | int r; |
3447 | 0 | crypt_reencrypt_info ri; |
3448 | 0 | struct crypt_lock_handle *h; |
3449 | |
|
3450 | 0 | ri = LUKS2_reencrypt_status(hdr); |
3451 | 0 | if (ri == CRYPT_REENCRYPT_INVALID) |
3452 | 0 | return -EINVAL; |
3453 | 0 | if (ri < CRYPT_REENCRYPT_CLEAN) { |
3454 | 0 | log_err(cd, _("Device is not in reencryption.")); |
3455 | 0 | return -EINVAL; |
3456 | 0 | } |
3457 | | |
3458 | 0 | r = LUKS2_reencrypt_lock(cd, &h); |
3459 | 0 | if (r < 0) { |
3460 | 0 | if (r == -EBUSY) |
3461 | 0 | log_err(cd, _("Reencryption process is already running.")); |
3462 | 0 | else |
3463 | 0 | log_err(cd, _("Failed to acquire reencryption lock.")); |
3464 | 0 | return r; |
3465 | 0 | } |
3466 | | |
3467 | | /* With reencryption lock held, reload device context and verify metadata state */ |
3468 | 0 | r = crypt_load(cd, CRYPT_LUKS2, NULL); |
3469 | 0 | if (r) { |
3470 | 0 | LUKS2_reencrypt_unlock(cd, h); |
3471 | 0 | return r; |
3472 | 0 | } |
3473 | | |
3474 | 0 | ri = LUKS2_reencrypt_status(hdr); |
3475 | 0 | if (ri == CRYPT_REENCRYPT_CLEAN) { |
3476 | 0 | *reencrypt_lock = h; |
3477 | 0 | return 0; |
3478 | 0 | } |
3479 | | |
3480 | 0 | LUKS2_reencrypt_unlock(cd, h); |
3481 | 0 | log_err(cd, _("Cannot proceed with reencryption. Run reencryption recovery first.")); |
3482 | 0 | return -EINVAL; |
3483 | 0 | } |
3484 | | |
3485 | | static int reencrypt_load_by_keyslot_context(struct crypt_device *cd, |
3486 | | const char *name, |
3487 | | struct crypt_keyslot_context *kc_old, |
3488 | | struct crypt_keyslot_context *kc_new, |
3489 | | int keyslot_old, |
3490 | | int keyslot_new, |
3491 | | struct volume_key **vks, |
3492 | | const struct crypt_params_reencrypt *params) |
3493 | 0 | { |
3494 | 0 | int r, reencrypt_slot; |
3495 | 0 | struct luks2_hdr *hdr; |
3496 | 0 | struct crypt_lock_handle *reencrypt_lock; |
3497 | 0 | struct luks2_reencrypt *rh; |
3498 | 0 | const struct volume_key *vk; |
3499 | 0 | size_t alignment; |
3500 | 0 | uint32_t old_sector_size, new_sector_size, sector_size; |
3501 | 0 | struct crypt_dm_active_device dmd_target, dmd_source = { |
3502 | 0 | .uuid = crypt_get_uuid(cd), |
3503 | 0 | .flags = CRYPT_ACTIVATE_SHARED /* turn off exclusive open checks */ |
3504 | 0 | }; |
3505 | 0 | uint64_t minimal_size, device_size, mapping_size = 0, required_size = 0, |
3506 | 0 | max_hotzone_size = 0; |
3507 | 0 | bool dynamic; |
3508 | 0 | uint32_t flags = 0; |
3509 | |
|
3510 | 0 | assert(cd); |
3511 | |
|
3512 | 0 | hdr = crypt_get_hdr(cd, CRYPT_LUKS2); |
3513 | 0 | if (!hdr) |
3514 | 0 | return -EINVAL; |
3515 | | |
3516 | 0 | log_dbg(cd, "Loading LUKS2 reencryption context."); |
3517 | |
|
3518 | 0 | old_sector_size = reencrypt_get_sector_size_old(hdr); |
3519 | 0 | new_sector_size = reencrypt_get_sector_size_new(hdr); |
3520 | 0 | sector_size = new_sector_size > old_sector_size ? new_sector_size : old_sector_size; |
3521 | |
|
3522 | 0 | r = reencrypt_verify_resilience_params(cd, params, sector_size, |
3523 | 0 | LUKS2_get_segment_id_by_flag(hdr, "backup-moved-segment") >= 0); |
3524 | 0 | if (r < 0) |
3525 | 0 | return r; |
3526 | | |
3527 | 0 | if (params) { |
3528 | 0 | required_size = params->device_size; |
3529 | 0 | max_hotzone_size = params->max_hotzone_size; |
3530 | 0 | } |
3531 | |
|
3532 | 0 | rh = crypt_get_luks2_reencrypt(cd); |
3533 | 0 | if (rh) { |
3534 | 0 | LUKS2_reencrypt_free(cd, rh); |
3535 | 0 | crypt_set_luks2_reencrypt(cd, NULL); |
3536 | 0 | rh = NULL; |
3537 | 0 | } |
3538 | |
|
3539 | 0 | r = reencrypt_lock_and_verify(cd, hdr, &reencrypt_lock); |
3540 | 0 | if (r) |
3541 | 0 | return r; |
3542 | | |
3543 | 0 | reencrypt_slot = LUKS2_find_keyslot(hdr, "reencrypt"); |
3544 | 0 | if (reencrypt_slot < 0) { |
3545 | 0 | r = -EINVAL; |
3546 | 0 | goto err; |
3547 | 0 | } |
3548 | | |
3549 | | /* From now on we hold reencryption lock */ |
3550 | | |
3551 | 0 | if (LUKS2_get_data_size(hdr, &minimal_size, &dynamic)) { |
3552 | 0 | r = -EINVAL; |
3553 | 0 | goto err; |
3554 | 0 | } |
3555 | | |
3556 | | /* some configurations provides fixed device size */ |
3557 | 0 | r = LUKS2_reencrypt_check_device_size(cd, hdr, minimal_size, &device_size, false, dynamic); |
3558 | 0 | if (r) { |
3559 | 0 | r = -EINVAL; |
3560 | 0 | goto err; |
3561 | 0 | } |
3562 | | |
3563 | 0 | minimal_size >>= SECTOR_SHIFT; |
3564 | |
|
3565 | 0 | r = reencrypt_verify_keys(cd, LUKS2_reencrypt_digest_old(hdr), LUKS2_reencrypt_digest_new(hdr), *vks); |
3566 | 0 | if (r == -ENOENT) { |
3567 | 0 | log_dbg(cd, "Keys are not ready. Unlocking all volume keys."); |
3568 | 0 | r = LUKS2_keyslot_context_open_all_segments(cd, keyslot_old, keyslot_new, |
3569 | 0 | kc_old, kc_new, vks); |
3570 | 0 | } |
3571 | |
|
3572 | 0 | if (r < 0) |
3573 | 0 | goto err; |
3574 | | |
3575 | 0 | if (name) { |
3576 | 0 | r = reencrypt_upload_keys(cd, hdr, LUKS2_reencrypt_digest_old(hdr), LUKS2_reencrypt_digest_new(hdr), *vks); |
3577 | 0 | if (r < 0) |
3578 | 0 | goto err; |
3579 | | |
3580 | 0 | r = dm_query_device(cd, name, DM_ACTIVE_UUID | DM_ACTIVE_DEVICE | |
3581 | 0 | DM_ACTIVE_CRYPT_KEYSIZE | DM_ACTIVE_CRYPT_KEY | |
3582 | 0 | DM_ACTIVE_CRYPT_CIPHER, &dmd_target); |
3583 | 0 | if (r < 0) |
3584 | 0 | goto err; |
3585 | 0 | flags = dmd_target.flags; |
3586 | | |
3587 | | /* |
3588 | | * By default reencryption code aims to retain flags from existing dm device. |
3589 | | * The keyring activation flag can not be inherited if original cipher is null. |
3590 | | * |
3591 | | * In this case override the flag based on decision made in reencrypt_upload_keys |
3592 | | * above. The code checks if new VK is eligible for keyring. |
3593 | | */ |
3594 | 0 | vk = crypt_volume_key_by_id(*vks, LUKS2_reencrypt_digest_new(hdr)); |
3595 | 0 | if (vk && crypt_volume_key_description(vk) && crypt_is_cipher_null(reencrypt_segment_cipher_old(hdr))) { |
3596 | 0 | flags |= CRYPT_ACTIVATE_KEYRING_KEY; |
3597 | 0 | dmd_source.flags |= CRYPT_ACTIVATE_KEYRING_KEY; |
3598 | 0 | } |
3599 | |
|
3600 | 0 | r = LUKS2_assembly_multisegment_dmd(cd, hdr, *vks, LUKS2_get_segments_jobj(hdr), &dmd_source); |
3601 | 0 | if (!r) { |
3602 | 0 | r = crypt_compare_dm_devices(cd, &dmd_source, &dmd_target); |
3603 | 0 | if (r) |
3604 | 0 | log_err(cd, _("Mismatching parameters on device %s."), name); |
3605 | 0 | } |
3606 | |
|
3607 | 0 | dm_targets_free(cd, &dmd_source); |
3608 | 0 | dm_targets_free(cd, &dmd_target); |
3609 | 0 | free(CONST_CAST(void*)dmd_target.uuid); |
3610 | 0 | if (r) |
3611 | 0 | goto err; |
3612 | 0 | mapping_size = dmd_target.size; |
3613 | 0 | } |
3614 | | |
3615 | 0 | r = -EINVAL; |
3616 | 0 | if (required_size && mapping_size && (required_size != mapping_size)) { |
3617 | 0 | log_err(cd, _("Active device size and requested reencryption size don't match.")); |
3618 | 0 | goto err; |
3619 | 0 | } |
3620 | | |
3621 | 0 | if (mapping_size) |
3622 | 0 | required_size = mapping_size; |
3623 | |
|
3624 | 0 | if (required_size) { |
3625 | | /* TODO: Add support for changing fixed minimal size in reencryption mda where possible */ |
3626 | 0 | if ((minimal_size && (required_size < minimal_size)) || |
3627 | 0 | (required_size > (device_size >> SECTOR_SHIFT)) || |
3628 | 0 | (!dynamic && (required_size != minimal_size)) || |
3629 | 0 | (old_sector_size > 0 && MISALIGNED(required_size, old_sector_size >> SECTOR_SHIFT)) || |
3630 | 0 | (new_sector_size > 0 && MISALIGNED(required_size, new_sector_size >> SECTOR_SHIFT))) { |
3631 | 0 | log_err(cd, _("Illegal device size requested in reencryption parameters.")); |
3632 | 0 | goto err; |
3633 | 0 | } |
3634 | 0 | } |
3635 | | |
3636 | 0 | alignment = reencrypt_get_alignment(cd, hdr); |
3637 | |
|
3638 | 0 | r = LUKS2_keyslot_reencrypt_update_needed(cd, hdr, reencrypt_slot, params, alignment); |
3639 | 0 | if (r > 0) /* metadata update needed */ |
3640 | 0 | r = LUKS2_keyslot_reencrypt_update(cd, hdr, reencrypt_slot, params, alignment, *vks); |
3641 | 0 | if (r < 0) |
3642 | 0 | goto err; |
3643 | | |
3644 | 0 | r = reencrypt_load(cd, hdr, device_size, max_hotzone_size, required_size, *vks, &rh); |
3645 | 0 | if (r < 0 || !rh) |
3646 | 0 | goto err; |
3647 | | |
3648 | 0 | if (name && (r = reencrypt_context_set_names(rh, name))) |
3649 | 0 | goto err; |
3650 | | |
3651 | | /* Reassure device is not mounted and there's no dm mapping active */ |
3652 | 0 | if (!name && (device_open_excl(cd, crypt_data_device(cd), O_RDONLY) < 0)) { |
3653 | 0 | log_err(cd,_("Failed to open %s in exclusive mode (already mapped or mounted)."), device_path(crypt_data_device(cd))); |
3654 | 0 | r = -EBUSY; |
3655 | 0 | goto err; |
3656 | 0 | } |
3657 | 0 | device_release_excl(cd, crypt_data_device(cd)); |
3658 | | |
3659 | | /* There's a race for dm device activation not managed by cryptsetup. |
3660 | | * |
3661 | | * 1) excl close |
3662 | | * 2) rogue dm device activation |
3663 | | * 3) one or more dm-crypt based wrapper activation |
3664 | | * 4) next excl open gets skipped due to 3) device from 2) remains undetected. |
3665 | | */ |
3666 | 0 | r = reencrypt_init_storage_wrappers(cd, hdr, rh, *vks); |
3667 | 0 | if (r) |
3668 | 0 | goto err; |
3669 | | |
3670 | | /* If one of wrappers is based on dmcrypt fallback it already blocked mount */ |
3671 | 0 | if (!name && crypt_storage_wrapper_get_type(rh->cw1) != DMCRYPT && |
3672 | 0 | crypt_storage_wrapper_get_type(rh->cw2) != DMCRYPT) { |
3673 | 0 | if (device_open_excl(cd, crypt_data_device(cd), O_RDONLY) < 0) { |
3674 | 0 | log_err(cd,_("Failed to open %s in exclusive mode (already mapped or mounted)."), device_path(crypt_data_device(cd))); |
3675 | 0 | r = -EBUSY; |
3676 | 0 | goto err; |
3677 | 0 | } |
3678 | 0 | } |
3679 | | |
3680 | 0 | rh->flags = flags; |
3681 | |
|
3682 | 0 | MOVE_REF(rh->vks, *vks); |
3683 | 0 | MOVE_REF(rh->reenc_lock, reencrypt_lock); |
3684 | |
|
3685 | 0 | crypt_set_luks2_reencrypt(cd, rh); |
3686 | |
|
3687 | 0 | return 0; |
3688 | 0 | err: |
3689 | 0 | LUKS2_reencrypt_unlock(cd, reencrypt_lock); |
3690 | 0 | LUKS2_reencrypt_free(cd, rh); |
3691 | 0 | return r; |
3692 | 0 | } |
3693 | | |
3694 | | static int reencrypt_locked_recovery(struct crypt_device *cd, |
3695 | | int keyslot_old, |
3696 | | int keyslot_new, |
3697 | | struct crypt_keyslot_context *kc_old, |
3698 | | struct crypt_keyslot_context *kc_new, |
3699 | | struct volume_key **r_vks) |
3700 | 0 | { |
3701 | 0 | int keyslot, r = -EINVAL; |
3702 | 0 | struct volume_key *_vks = NULL; |
3703 | |
|
3704 | 0 | r = LUKS2_keyslot_context_open_all_segments(cd, keyslot_old, keyslot_new, |
3705 | 0 | kc_old, kc_new, &_vks); |
3706 | 0 | if (r < 0) |
3707 | 0 | return r; |
3708 | 0 | keyslot = r; |
3709 | |
|
3710 | 0 | r = LUKS2_reencrypt_locked_recovery_by_vks(cd, _vks); |
3711 | 0 | if (!r && r_vks) |
3712 | 0 | MOVE_REF(*r_vks, _vks); |
3713 | |
|
3714 | 0 | crypt_free_volume_key(_vks); |
3715 | |
|
3716 | 0 | return r < 0 ? r : keyslot; |
3717 | 0 | } |
3718 | | |
3719 | | static int reencrypt_recovery_by_keyslot_context(struct crypt_device *cd, |
3720 | | struct luks2_hdr *hdr, |
3721 | | int keyslot_old, |
3722 | | int keyslot_new, |
3723 | | struct crypt_keyslot_context *kc_old, |
3724 | | struct crypt_keyslot_context *kc_new) |
3725 | 0 | { |
3726 | 0 | int r; |
3727 | 0 | crypt_reencrypt_info ri; |
3728 | 0 | struct crypt_lock_handle *reencrypt_lock; |
3729 | |
|
3730 | 0 | r = LUKS2_reencrypt_lock(cd, &reencrypt_lock); |
3731 | 0 | if (r) { |
3732 | 0 | if (r == -EBUSY) |
3733 | 0 | log_err(cd, _("Reencryption in-progress. Cannot perform recovery.")); |
3734 | 0 | else |
3735 | 0 | log_err(cd, _("Failed to get reencryption lock.")); |
3736 | 0 | return r; |
3737 | 0 | } |
3738 | | |
3739 | 0 | if ((r = crypt_load(cd, CRYPT_LUKS2, NULL))) { |
3740 | 0 | LUKS2_reencrypt_unlock(cd, reencrypt_lock); |
3741 | 0 | return r; |
3742 | 0 | } |
3743 | | |
3744 | 0 | ri = LUKS2_reencrypt_status(hdr); |
3745 | 0 | if (ri == CRYPT_REENCRYPT_INVALID) { |
3746 | 0 | LUKS2_reencrypt_unlock(cd, reencrypt_lock); |
3747 | 0 | return -EINVAL; |
3748 | 0 | } |
3749 | | |
3750 | 0 | if (ri == CRYPT_REENCRYPT_CRASH) { |
3751 | 0 | r = reencrypt_locked_recovery(cd, keyslot_old, keyslot_new, |
3752 | 0 | kc_old, kc_new, NULL); |
3753 | 0 | if (r < 0) |
3754 | 0 | log_err(cd, _("LUKS2 reencryption recovery failed.")); |
3755 | 0 | } else { |
3756 | 0 | log_dbg(cd, "No LUKS2 reencryption recovery needed."); |
3757 | 0 | r = 0; |
3758 | 0 | } |
3759 | |
|
3760 | 0 | LUKS2_reencrypt_unlock(cd, reencrypt_lock); |
3761 | 0 | return r; |
3762 | 0 | } |
3763 | | |
3764 | | static int reencrypt_repair( |
3765 | | struct crypt_device *cd, |
3766 | | struct luks2_hdr *hdr, |
3767 | | int keyslot_old, |
3768 | | int keyslot_new, |
3769 | | struct crypt_keyslot_context *kc_old, |
3770 | | struct crypt_keyslot_context *kc_new) |
3771 | 0 | { |
3772 | 0 | int r; |
3773 | 0 | struct crypt_lock_handle *reencrypt_lock; |
3774 | 0 | struct luks2_reencrypt *rh; |
3775 | 0 | crypt_reencrypt_info ri; |
3776 | 0 | uint8_t requirement_version; |
3777 | 0 | const char *resilience; |
3778 | 0 | struct volume_key *vks = NULL; |
3779 | |
|
3780 | 0 | log_dbg(cd, "Loading LUKS2 reencryption context for metadata repair."); |
3781 | |
|
3782 | 0 | rh = crypt_get_luks2_reencrypt(cd); |
3783 | 0 | if (rh) { |
3784 | 0 | LUKS2_reencrypt_free(cd, rh); |
3785 | 0 | crypt_set_luks2_reencrypt(cd, NULL); |
3786 | 0 | rh = NULL; |
3787 | 0 | } |
3788 | |
|
3789 | 0 | ri = LUKS2_reencrypt_status(hdr); |
3790 | 0 | if (ri == CRYPT_REENCRYPT_INVALID) |
3791 | 0 | return -EINVAL; |
3792 | | |
3793 | 0 | if (ri < CRYPT_REENCRYPT_CLEAN) { |
3794 | 0 | log_err(cd, _("Device is not in reencryption.")); |
3795 | 0 | return -EINVAL; |
3796 | 0 | } |
3797 | | |
3798 | 0 | r = LUKS2_reencrypt_lock(cd, &reencrypt_lock); |
3799 | 0 | if (r < 0) { |
3800 | 0 | if (r == -EBUSY) |
3801 | 0 | log_err(cd, _("Reencryption process is already running.")); |
3802 | 0 | else |
3803 | 0 | log_err(cd, _("Failed to acquire reencryption lock.")); |
3804 | 0 | return r; |
3805 | 0 | } |
3806 | | |
3807 | | /* With reencryption lock held, reload device context and verify metadata state */ |
3808 | 0 | r = crypt_load(cd, CRYPT_LUKS2, NULL); |
3809 | 0 | if (r) |
3810 | 0 | goto out; |
3811 | | |
3812 | 0 | ri = LUKS2_reencrypt_status(hdr); |
3813 | 0 | if (ri == CRYPT_REENCRYPT_INVALID) { |
3814 | 0 | r = -EINVAL; |
3815 | 0 | goto out; |
3816 | 0 | } |
3817 | 0 | if (ri == CRYPT_REENCRYPT_NONE) { |
3818 | 0 | r = 0; |
3819 | 0 | goto out; |
3820 | 0 | } |
3821 | | |
3822 | 0 | resilience = reencrypt_resilience_type(hdr); |
3823 | 0 | if (!resilience) { |
3824 | 0 | r = -EINVAL; |
3825 | 0 | goto out; |
3826 | 0 | } |
3827 | | |
3828 | 0 | if (reencrypt_mode(hdr) == CRYPT_REENCRYPT_DECRYPT && |
3829 | 0 | !strncmp(resilience, "datashift-", 10) && |
3830 | 0 | LUKS2_get_segment_id_by_flag(hdr, "backup-moved-segment") >= 0) |
3831 | 0 | requirement_version = LUKS2_DECRYPT_DATASHIFT_REQ_VERSION; |
3832 | 0 | else |
3833 | 0 | requirement_version = LUKS2_REENCRYPT_REQ_VERSION; |
3834 | |
|
3835 | 0 | r = LUKS2_keyslot_context_open_all_segments(cd, keyslot_old, keyslot_new, kc_old, kc_new, &vks); |
3836 | 0 | if (r < 0) |
3837 | 0 | goto out; |
3838 | | |
3839 | 0 | r = LUKS2_keyslot_reencrypt_digest_create(cd, hdr, requirement_version, vks); |
3840 | 0 | crypt_free_volume_key(vks); |
3841 | 0 | vks = NULL; |
3842 | 0 | if (r < 0) |
3843 | 0 | goto out; |
3844 | | |
3845 | | /* replaces old online-reencrypt flag with updated version and commits metadata */ |
3846 | 0 | r = reencrypt_update_flag(cd, requirement_version, true, true); |
3847 | 0 | out: |
3848 | 0 | LUKS2_reencrypt_unlock(cd, reencrypt_lock); |
3849 | 0 | crypt_free_volume_key(vks); |
3850 | 0 | return r; |
3851 | |
|
3852 | 0 | } |
3853 | | |
3854 | | static int reencrypt_init_by_keyslot_context(struct crypt_device *cd, |
3855 | | const char *name, |
3856 | | struct crypt_keyslot_context *kc_old, |
3857 | | struct crypt_keyslot_context *kc_new, |
3858 | | int keyslot_old, |
3859 | | int keyslot_new, |
3860 | | const char *cipher, |
3861 | | const char *cipher_mode, |
3862 | | const struct crypt_params_reencrypt *params) |
3863 | 0 | { |
3864 | 0 | int r; |
3865 | 0 | crypt_reencrypt_info ri; |
3866 | 0 | size_t key_length; |
3867 | 0 | struct volume_key *vks = NULL; |
3868 | 0 | uint32_t flags = params ? params->flags : 0; |
3869 | 0 | struct luks2_hdr *hdr = crypt_get_hdr(cd, CRYPT_LUKS2); |
3870 | |
|
3871 | 0 | if (params && (params->flags & CRYPT_REENCRYPT_CREATE_NEW_DIGEST) && |
3872 | 0 | (!kc_new || !kc_new->get_luks2_key || !kc_new->get_key_size || |
3873 | 0 | (params->flags & CRYPT_REENCRYPT_RESUME_ONLY))) |
3874 | 0 | return -EINVAL; |
3875 | | |
3876 | | /* short-circuit in reencryption metadata update and finish immediately. */ |
3877 | 0 | if (flags & CRYPT_REENCRYPT_REPAIR_NEEDED) |
3878 | 0 | return reencrypt_repair(cd, hdr, keyslot_old, keyslot_new, kc_old, kc_new); |
3879 | | |
3880 | | /* short-circuit in recovery and finish immediately. */ |
3881 | 0 | if (flags & CRYPT_REENCRYPT_RECOVERY) |
3882 | 0 | return reencrypt_recovery_by_keyslot_context(cd, hdr, keyslot_old, keyslot_new, kc_old, kc_new); |
3883 | | |
3884 | 0 | if (name && !device_direct_io(crypt_data_device(cd))) { |
3885 | 0 | log_dbg(cd, "Device %s does not support direct I/O.", device_path(crypt_data_device(cd))); |
3886 | | /* FIXME: Add more specific error message for translation later. */ |
3887 | 0 | log_err(cd, _("Failed to initialize reencryption device stack.")); |
3888 | 0 | return -EINVAL; |
3889 | 0 | } |
3890 | | |
3891 | 0 | if (cipher && !crypt_cipher_wrapped_key(cipher, cipher_mode)) { |
3892 | 0 | if (keyslot_new == CRYPT_ANY_SLOT && kc_new && kc_new->get_key_size) |
3893 | 0 | r = kc_new->get_key_size(cd, kc_new, &key_length); |
3894 | 0 | else { |
3895 | 0 | r = crypt_keyslot_get_key_size(cd, keyslot_new); |
3896 | 0 | if (r >= 0) |
3897 | 0 | key_length = r; |
3898 | 0 | } |
3899 | 0 | if (r < 0) |
3900 | 0 | return r; |
3901 | 0 | r = crypt_check_cipher(cd, key_length, cipher, cipher_mode); |
3902 | 0 | if (r < 0) { |
3903 | 0 | log_err(cd, _("Unable to use cipher specification %s-%s for LUKS2."), cipher, cipher_mode); |
3904 | 0 | return r; |
3905 | 0 | } |
3906 | 0 | } |
3907 | | |
3908 | 0 | r = LUKS2_device_write_lock(cd, hdr, crypt_metadata_device(cd)); |
3909 | 0 | if (r) |
3910 | 0 | return r; |
3911 | | |
3912 | 0 | ri = LUKS2_reencrypt_status(hdr); |
3913 | 0 | if (ri == CRYPT_REENCRYPT_INVALID) { |
3914 | 0 | device_write_unlock(cd, crypt_metadata_device(cd)); |
3915 | 0 | return -EINVAL; |
3916 | 0 | } |
3917 | | |
3918 | 0 | if ((ri > CRYPT_REENCRYPT_NONE) && (flags & CRYPT_REENCRYPT_INITIALIZE_ONLY)) { |
3919 | 0 | device_write_unlock(cd, crypt_metadata_device(cd)); |
3920 | 0 | log_err(cd, _("LUKS2 reencryption already initialized in metadata.")); |
3921 | 0 | return -EBUSY; |
3922 | 0 | } |
3923 | | |
3924 | 0 | if (ri == CRYPT_REENCRYPT_NONE && !(flags & CRYPT_REENCRYPT_RESUME_ONLY)) { |
3925 | 0 | r = reencrypt_init(cd, name, hdr, kc_old, kc_new, keyslot_old, |
3926 | 0 | keyslot_new, cipher, cipher_mode, params, &vks); |
3927 | 0 | if (r < 0) |
3928 | 0 | log_err(cd, _("Failed to initialize LUKS2 reencryption in metadata.")); |
3929 | 0 | } else if (ri > CRYPT_REENCRYPT_NONE) { |
3930 | 0 | log_dbg(cd, "LUKS2 reencryption already initialized."); |
3931 | 0 | r = 0; |
3932 | 0 | } |
3933 | |
|
3934 | 0 | device_write_unlock(cd, crypt_metadata_device(cd)); |
3935 | |
|
3936 | 0 | if (r < 0 || (flags & CRYPT_REENCRYPT_INITIALIZE_ONLY)) |
3937 | 0 | goto out; |
3938 | | |
3939 | 0 | r = reencrypt_load_by_keyslot_context(cd, name, kc_old, kc_new, keyslot_old, |
3940 | 0 | keyslot_new, &vks, params); |
3941 | 0 | out: |
3942 | 0 | if (r < 0) |
3943 | 0 | crypt_drop_uploaded_keyring_key(cd, vks); |
3944 | 0 | crypt_free_volume_key(vks); |
3945 | 0 | return r < 0 ? r : LUKS2_find_keyslot(hdr, "reencrypt"); |
3946 | 0 | } |
3947 | | #else |
3948 | | static int reencrypt_init_by_keyslot_context(struct crypt_device *cd, |
3949 | | const char *name __attribute__((unused)), |
3950 | | struct crypt_keyslot_context *kc_old __attribute__((unused)), |
3951 | | struct crypt_keyslot_context *kc_new __attribute__((unused)), |
3952 | | int keyslot_old __attribute__((unused)), |
3953 | | int keyslot_new __attribute__((unused)), |
3954 | | const char *cipher __attribute__((unused)), |
3955 | | const char *cipher_mode __attribute__((unused)), |
3956 | | const struct crypt_params_reencrypt *params __attribute__((unused))) |
3957 | | { |
3958 | | log_err(cd, _("This operation is not supported for this device type.")); |
3959 | | return -ENOTSUP; |
3960 | | } |
3961 | | #endif |
3962 | | |
3963 | | int crypt_reencrypt_init_by_keyring(struct crypt_device *cd, |
3964 | | const char *name, |
3965 | | const char *passphrase_description, |
3966 | | int keyslot_old, |
3967 | | int keyslot_new, |
3968 | | const char *cipher, |
3969 | | const char *cipher_mode, |
3970 | | const struct crypt_params_reencrypt *params) |
3971 | 0 | { |
3972 | 0 | int r; |
3973 | 0 | struct crypt_keyslot_context kc = {0}; |
3974 | |
|
3975 | 0 | if (onlyLUKS2reencrypt(cd) || !passphrase_description) |
3976 | 0 | return -EINVAL; |
3977 | 0 | if (params && (params->flags & CRYPT_REENCRYPT_INITIALIZE_ONLY) && (params->flags & CRYPT_REENCRYPT_RESUME_ONLY)) |
3978 | 0 | return -EINVAL; |
3979 | | |
3980 | 0 | if (device_is_dax(crypt_data_device(cd)) > 0) { |
3981 | 0 | log_err(cd, _("Reencryption is not supported for DAX (persistent memory) devices.")); |
3982 | 0 | return -EINVAL; |
3983 | 0 | } |
3984 | | |
3985 | 0 | crypt_keyslot_context_init_by_keyring_internal(&kc, passphrase_description); |
3986 | 0 | r = reencrypt_init_by_keyslot_context(cd, name, &kc, &kc, keyslot_old, |
3987 | 0 | keyslot_new, cipher, cipher_mode, params); |
3988 | |
|
3989 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
3990 | |
|
3991 | 0 | return r; |
3992 | 0 | } |
3993 | | |
3994 | | int crypt_reencrypt_init_by_passphrase(struct crypt_device *cd, |
3995 | | const char *name, |
3996 | | const char *passphrase, |
3997 | | size_t passphrase_size, |
3998 | | int keyslot_old, |
3999 | | int keyslot_new, |
4000 | | const char *cipher, |
4001 | | const char *cipher_mode, |
4002 | | const struct crypt_params_reencrypt *params) |
4003 | 0 | { |
4004 | 0 | int r; |
4005 | 0 | struct crypt_keyslot_context kc = {0}; |
4006 | |
|
4007 | 0 | if (onlyLUKS2reencrypt(cd) || !passphrase) |
4008 | 0 | return -EINVAL; |
4009 | 0 | if (params && (params->flags & CRYPT_REENCRYPT_INITIALIZE_ONLY) && (params->flags & CRYPT_REENCRYPT_RESUME_ONLY)) |
4010 | 0 | return -EINVAL; |
4011 | | |
4012 | 0 | if (device_is_dax(crypt_data_device(cd)) > 0) { |
4013 | 0 | log_err(cd, _("Reencryption is not supported for DAX (persistent memory) devices.")); |
4014 | 0 | return -EINVAL; |
4015 | 0 | } |
4016 | | |
4017 | 0 | crypt_keyslot_context_init_by_passphrase_internal(&kc, passphrase, passphrase_size); |
4018 | |
|
4019 | 0 | r = reencrypt_init_by_keyslot_context(cd, name, &kc, &kc, keyslot_old, |
4020 | 0 | keyslot_new, cipher, cipher_mode, params); |
4021 | |
|
4022 | 0 | crypt_keyslot_context_destroy_internal(&kc); |
4023 | |
|
4024 | 0 | return r; |
4025 | 0 | } |
4026 | | |
4027 | | int crypt_reencrypt_init_by_keyslot_context(struct crypt_device *cd, |
4028 | | const char *name, |
4029 | | struct crypt_keyslot_context *kc_old, |
4030 | | struct crypt_keyslot_context *kc_new, |
4031 | | int keyslot_old, |
4032 | | int keyslot_new, |
4033 | | const char *cipher, |
4034 | | const char *cipher_mode, |
4035 | | const struct crypt_params_reencrypt *params) |
4036 | 0 | { |
4037 | 0 | if (onlyLUKS2reencrypt(cd) || (!kc_old && !kc_new)) |
4038 | 0 | return -EINVAL; |
4039 | 0 | if (params && (params->flags & CRYPT_REENCRYPT_INITIALIZE_ONLY) && (params->flags & CRYPT_REENCRYPT_RESUME_ONLY)) |
4040 | 0 | return -EINVAL; |
4041 | | |
4042 | 0 | if (device_is_dax(crypt_data_device(cd)) > 0) { |
4043 | 0 | log_err(cd, _("Reencryption is not supported for DAX (persistent memory) devices.")); |
4044 | 0 | return -EINVAL; |
4045 | 0 | } |
4046 | | |
4047 | 0 | return reencrypt_init_by_keyslot_context(cd, name, kc_old, kc_new, keyslot_old, keyslot_new, cipher, cipher_mode, params); |
4048 | 0 | } |
4049 | | |
4050 | | #if USE_LUKS2_REENCRYPTION |
4051 | | static reenc_status_t reencrypt_step(struct crypt_device *cd, |
4052 | | struct luks2_hdr *hdr, |
4053 | | struct luks2_reencrypt *rh, |
4054 | | uint64_t device_size, |
4055 | | bool online) |
4056 | 0 | { |
4057 | 0 | int r; |
4058 | 0 | struct reenc_protection *rp; |
4059 | |
|
4060 | 0 | assert(hdr); |
4061 | 0 | assert(rh); |
4062 | |
|
4063 | 0 | rp = &rh->rp; |
4064 | | |
4065 | | /* in memory only */ |
4066 | 0 | r = reencrypt_make_segments(cd, hdr, rh, device_size); |
4067 | 0 | if (r) |
4068 | 0 | return REENC_ERR_ROLLBACK_MEMORY; |
4069 | | |
4070 | 0 | r = reencrypt_assign_segments(cd, hdr, rh, 1, 0); |
4071 | 0 | if (r) { |
4072 | 0 | log_err(cd, _("Failed to set device segments for next reencryption hotzone.")); |
4073 | 0 | return REENC_ERR_ROLLBACK_MEMORY; |
4074 | 0 | } |
4075 | | |
4076 | 0 | log_dbg(cd, "Reencrypting chunk starting at offset: %" PRIu64 ", size :%" PRIu64 ".", rh->offset, rh->length); |
4077 | 0 | log_dbg(cd, "data_offset: %" PRIu64, crypt_get_data_offset(cd) << SECTOR_SHIFT); |
4078 | |
|
4079 | 0 | if (!rh->offset && rp->type == REENC_PROTECTION_DATASHIFT && rh->jobj_segment_moved) { |
4080 | 0 | crypt_storage_wrapper_destroy(rh->cw1); |
4081 | 0 | log_dbg(cd, "Reinitializing old segment storage wrapper for moved segment."); |
4082 | 0 | r = crypt_storage_wrapper_init(cd, &rh->cw1, crypt_data_device(cd), |
4083 | 0 | LUKS2_reencrypt_get_data_offset_moved(hdr), |
4084 | 0 | crypt_get_iv_offset(cd), |
4085 | 0 | reencrypt_get_sector_size_old(hdr), |
4086 | 0 | reencrypt_segment_cipher_old(hdr), |
4087 | 0 | crypt_volume_key_by_id(rh->vks, rh->digest_old), |
4088 | 0 | rh->wflags1); |
4089 | 0 | if (r) { |
4090 | 0 | log_err(cd, _("Failed to initialize old segment storage wrapper.")); |
4091 | 0 | return REENC_ERR_ROLLBACK_MEMORY; |
4092 | 0 | } |
4093 | | |
4094 | 0 | if (rh->rp_moved_segment.type != REENC_PROTECTION_NOT_SET) { |
4095 | 0 | log_dbg(cd, "Switching to moved segment resilience type."); |
4096 | 0 | rp = &rh->rp_moved_segment; |
4097 | 0 | } |
4098 | 0 | } |
4099 | | |
4100 | 0 | r = reencrypt_hotzone_protect_ready(cd, rp); |
4101 | 0 | if (r) { |
4102 | 0 | log_err(cd, _("Failed to initialize hotzone protection.")); |
4103 | 0 | return REENC_ERR_ROLLBACK_MEMORY; |
4104 | 0 | } |
4105 | | |
4106 | 0 | if (online) { |
4107 | 0 | r = reencrypt_refresh_overlay_devices(cd, hdr, rh->overlay_name, rh->hotzone_name, |
4108 | 0 | rh->hotzone_device, rh->vks, rh->device_size, rh->flags); |
4109 | | /* Teardown overlay devices with dm-error. None bio shall pass! */ |
4110 | 0 | if (r != REENC_OK) |
4111 | 0 | return r; |
4112 | 0 | } |
4113 | | |
4114 | 0 | rh->read = crypt_storage_wrapper_read(rh->cw1, rh->offset, rh->reenc_buffer, rh->length); |
4115 | 0 | if (rh->read < 0) { |
4116 | | /* severity normal */ |
4117 | 0 | log_err(cd, _("Failed to read hotzone area starting at %" PRIu64 "."), rh->offset); |
4118 | 0 | return REENC_ERR_ROLLBACK_MEMORY; |
4119 | 0 | } |
4120 | | |
4121 | | /* metadata commit point */ |
4122 | 0 | r = reencrypt_hotzone_protect_final(cd, hdr, rh->reenc_keyslot, rp, rh->reenc_buffer, rh->read); |
4123 | 0 | if (r < 0) { |
4124 | | /* |
4125 | | * Nothing was written in hotzone area yet. Even if metadata write failed the previous |
4126 | | * state is still valid. If the metadata write passed and there was another |
4127 | | * error it's harmless to do recovery. Recovery may be run several times with no |
4128 | | * negative side effect. |
4129 | | */ |
4130 | 0 | log_err(cd, _("Failed to write reencryption resilience metadata.")); |
4131 | 0 | return REENC_ERR_ROLLBACK_MEMORY; |
4132 | 0 | } |
4133 | | |
4134 | 0 | r = crypt_storage_wrapper_decrypt(rh->cw1, rh->offset, rh->reenc_buffer, rh->read); |
4135 | 0 | if (r) { |
4136 | | /* |
4137 | | * Ideally, this would be specific error (REENC_ERR_ROLLBACK_METADATA) case where |
4138 | | * it would rollback on-disk metadata to the last valid state (still no write in |
4139 | | * hotzone area). But it's not worth the effort. This will trigger full LUKS2 |
4140 | | * reencryption recovery despite not being necessary. |
4141 | | */ |
4142 | 0 | log_err(cd, _("Decryption failed.")); |
4143 | 0 | return REENC_ERR_ROLLBACK_MEMORY; |
4144 | 0 | } |
4145 | 0 | if (rh->read != crypt_storage_wrapper_encrypt_write(rh->cw2, rh->offset, rh->reenc_buffer, rh->read)) { |
4146 | | /* severity fatal */ |
4147 | 0 | log_err(cd, _("Failed to write hotzone area starting at %" PRIu64 "."), rh->offset); |
4148 | 0 | return REENC_ERR_FATAL; |
4149 | 0 | } |
4150 | | |
4151 | 0 | if (rp->type != REENC_PROTECTION_NONE && crypt_storage_wrapper_datasync(rh->cw2)) { |
4152 | 0 | log_err(cd, _("Failed to sync data.")); |
4153 | 0 | return REENC_ERR_FATAL; |
4154 | 0 | } |
4155 | | |
4156 | | /* metadata commit safe point */ |
4157 | 0 | r = reencrypt_assign_segments(cd, hdr, rh, 0, rp->type != REENC_PROTECTION_NONE); |
4158 | 0 | if (r) { |
4159 | | /* severity fatal */ |
4160 | 0 | log_err(cd, _("Failed to update metadata after current reencryption hotzone completed.")); |
4161 | 0 | return REENC_ERR_FATAL; |
4162 | 0 | } |
4163 | | |
4164 | 0 | if (online) { |
4165 | 0 | log_dbg(cd, "Resuming device %s", rh->hotzone_name); |
4166 | 0 | r = dm_resume_device(cd, rh->hotzone_name, DM_RESUME_PRIVATE); |
4167 | 0 | if (r) { |
4168 | 0 | log_err(cd, _("Failed to resume device %s."), rh->hotzone_name); |
4169 | 0 | return REENC_ERR_ROLLBACK_MEMORY; |
4170 | 0 | } |
4171 | 0 | } |
4172 | | |
4173 | 0 | return REENC_OK; |
4174 | 0 | } |
4175 | | |
4176 | | static int reencrypt_erase_backup_segments(struct crypt_device *cd, |
4177 | | struct luks2_hdr *hdr) |
4178 | 0 | { |
4179 | 0 | int segment = LUKS2_get_segment_id_by_flag(hdr, "backup-previous"); |
4180 | 0 | if (segment >= 0) { |
4181 | 0 | if (LUKS2_digest_segment_assign(cd, hdr, segment, CRYPT_ANY_DIGEST, 0, 0)) |
4182 | 0 | return -EINVAL; |
4183 | 0 | json_object_object_del_by_uint(LUKS2_get_segments_jobj(hdr), segment); |
4184 | 0 | } |
4185 | 0 | segment = LUKS2_get_segment_id_by_flag(hdr, "backup-final"); |
4186 | 0 | if (segment >= 0) { |
4187 | 0 | if (LUKS2_digest_segment_assign(cd, hdr, segment, CRYPT_ANY_DIGEST, 0, 0)) |
4188 | 0 | return -EINVAL; |
4189 | 0 | json_object_object_del_by_uint(LUKS2_get_segments_jobj(hdr), segment); |
4190 | 0 | } |
4191 | 0 | segment = LUKS2_get_segment_id_by_flag(hdr, "backup-moved-segment"); |
4192 | 0 | if (segment >= 0) { |
4193 | 0 | if (LUKS2_digest_segment_assign(cd, hdr, segment, CRYPT_ANY_DIGEST, 0, 0)) |
4194 | 0 | return -EINVAL; |
4195 | 0 | json_object_object_del_by_uint(LUKS2_get_segments_jobj(hdr), segment); |
4196 | 0 | } |
4197 | | |
4198 | 0 | return 0; |
4199 | 0 | } |
4200 | | |
4201 | | static int reencrypt_wipe_unused_device_area(struct crypt_device *cd, struct luks2_reencrypt *rh) |
4202 | 0 | { |
4203 | 0 | uint64_t offset, length, dev_size; |
4204 | 0 | int r = 0; |
4205 | |
|
4206 | 0 | assert(cd); |
4207 | 0 | assert(rh); |
4208 | |
|
4209 | 0 | if (rh->jobj_segment_moved && rh->mode == CRYPT_REENCRYPT_ENCRYPT) { |
4210 | 0 | offset = json_segment_get_offset(rh->jobj_segment_moved, 0); |
4211 | 0 | length = json_segment_get_size(rh->jobj_segment_moved, 0); |
4212 | 0 | log_dbg(cd, "Wiping %" PRIu64 " bytes of backup segment data at offset %" PRIu64, |
4213 | 0 | length, offset); |
4214 | 0 | r = crypt_wipe_device(cd, crypt_data_device(cd), CRYPT_WIPE_RANDOM, |
4215 | 0 | offset, length, 1024 * 1024, NULL, NULL); |
4216 | 0 | } |
4217 | |
|
4218 | 0 | if (r < 0) |
4219 | 0 | return r; |
4220 | | |
4221 | 0 | if (rh->rp.type == REENC_PROTECTION_DATASHIFT && rh->direction == CRYPT_REENCRYPT_FORWARD) { |
4222 | 0 | r = device_size(crypt_data_device(cd), &dev_size); |
4223 | 0 | if (r < 0) |
4224 | 0 | return r; |
4225 | | |
4226 | 0 | if (dev_size < data_shift_value(&rh->rp)) |
4227 | 0 | return -EINVAL; |
4228 | | |
4229 | 0 | offset = dev_size - data_shift_value(&rh->rp); |
4230 | 0 | length = data_shift_value(&rh->rp); |
4231 | 0 | log_dbg(cd, "Wiping %" PRIu64 " bytes of data at offset %" PRIu64, |
4232 | 0 | length, offset); |
4233 | 0 | r = crypt_wipe_device(cd, crypt_data_device(cd), CRYPT_WIPE_RANDOM, |
4234 | 0 | offset, length, 1024 * 1024, NULL, NULL); |
4235 | 0 | } |
4236 | | |
4237 | 0 | return r; |
4238 | 0 | } |
4239 | | |
4240 | | static int replace_hotzone_device_with_error(struct crypt_device *cd, struct luks2_reencrypt *rh) |
4241 | 0 | { |
4242 | 0 | log_dbg(cd, "Replacing device %s with dm-error.", rh->hotzone_name); |
4243 | 0 | if (dm_error_device(cd, rh->hotzone_name)) { |
4244 | 0 | log_err(cd, _("Failed to replace suspended device %s with dm-error target."), rh->hotzone_name); |
4245 | 0 | log_err(cd, _("Do not resume the device unless replaced with error target manually.")); |
4246 | 0 | return -EIO; |
4247 | 0 | } |
4248 | | |
4249 | 0 | return 0; |
4250 | 0 | } |
4251 | | |
4252 | | static int teardown_overlay_devices(struct crypt_device *cd, struct luks2_reencrypt *rh) |
4253 | 0 | { |
4254 | 0 | bool overlay_suspended, hotzone_suspended; |
4255 | 0 | int r; |
4256 | | |
4257 | | /* Reload device with current LUKS2 segments */ |
4258 | 0 | r = LUKS2_reload(cd, rh->device_name, rh->vks, rh->device_size, rh->flags); |
4259 | 0 | if (r) { |
4260 | 0 | log_err(cd, _("Failed to reload device %s."), rh->device_name); |
4261 | 0 | return r; |
4262 | 0 | } |
4263 | | |
4264 | 0 | overlay_suspended = dm_status_suspended(cd, rh->overlay_name) > 0; |
4265 | 0 | hotzone_suspended = dm_status_suspended(cd, rh->hotzone_name) > 0; |
4266 | | |
4267 | | /* |
4268 | | * The overlay (if suspended) may hold already queued I/Os. |
4269 | | * Reload the overlay device with the table identical to the one |
4270 | | * loaded to the top level device. The overlay device will dropped |
4271 | | * shortly after successful top level device resume. |
4272 | | */ |
4273 | 0 | if (overlay_suspended) { |
4274 | 0 | log_dbg(cd, "Reverting suspended device %s to previous metadata segments", rh->overlay_name); |
4275 | 0 | r = LUKS2_reload(cd, rh->overlay_name, rh->vks, rh->device_size, rh->flags); |
4276 | 0 | if (r) { |
4277 | 0 | log_err(cd, _("Failed to reload device %s."), rh->overlay_name); |
4278 | 0 | return r; |
4279 | 0 | } |
4280 | 0 | } |
4281 | | |
4282 | | /* |
4283 | | * if the hotzone is suspended we must error all pending I/O waiting in the device. The |
4284 | | * reencryption step was not completed and the pending I/O would corrupt the data on data |
4285 | | * device. |
4286 | | * |
4287 | | * If the hotzone table replacement fails we must abort! |
4288 | | */ |
4289 | 0 | if (hotzone_suspended && (r = replace_hotzone_device_with_error(cd, rh))) |
4290 | 0 | return r; |
4291 | | |
4292 | 0 | if (overlay_suspended) { |
4293 | | /* Resume will pass since the hotzone (if previously suspended) is now |
4294 | | * replaced with live dm-error table */ |
4295 | 0 | r = dm_resume_device(cd, rh->overlay_name, DM_RESUME_PRIVATE); |
4296 | 0 | if (r) { |
4297 | 0 | log_err(cd, _("Failed to resume device %s."), rh->overlay_name); |
4298 | 0 | return r; |
4299 | 0 | } |
4300 | 0 | } |
4301 | | |
4302 | | /* Now we can switch original top level device away from overlay device */ |
4303 | 0 | r = dm_resume_device(cd, rh->device_name, DM_SUSPEND_SKIP_LOCKFS | DM_SUSPEND_NOFLUSH); |
4304 | 0 | if (r) { |
4305 | 0 | log_err(cd, _("Failed to resume device %s."), rh->device_name); |
4306 | 0 | return r; |
4307 | 0 | } |
4308 | | |
4309 | | /* |
4310 | | * This should not affect teardown return value. There may be other processes |
4311 | | * touching those devices despite being private. |
4312 | | */ |
4313 | 0 | if (dm_remove_device(cd, rh->overlay_name, 0)) |
4314 | 0 | log_dbg(cd, "Failed to remove unused device %s", rh->overlay_name); |
4315 | 0 | if (dm_remove_device(cd, rh->hotzone_name, 0)) |
4316 | 0 | log_dbg(cd, "Failed to remove unused device %s", rh->hotzone_name); |
4317 | |
|
4318 | 0 | return 0; |
4319 | 0 | } |
4320 | | |
4321 | | static int reencrypt_teardown_ok(struct crypt_device *cd, struct luks2_hdr *hdr, struct luks2_reencrypt *rh) |
4322 | 0 | { |
4323 | 0 | int i, r; |
4324 | 0 | uint64_t dmt_flags; |
4325 | 0 | bool finished = !(rh->device_size > rh->progress); |
4326 | |
|
4327 | 0 | if (rh->rp.type == REENC_PROTECTION_NONE && |
4328 | 0 | LUKS2_hdr_write(cd, hdr)) { |
4329 | 0 | log_err(cd, _("Failed to write LUKS2 metadata.")); |
4330 | 0 | return -EINVAL; |
4331 | 0 | } |
4332 | | |
4333 | 0 | if (rh->online) { |
4334 | 0 | r = teardown_overlay_devices(cd, rh); |
4335 | 0 | if (r) |
4336 | 0 | return r; |
4337 | | |
4338 | 0 | if (finished && rh->mode == CRYPT_REENCRYPT_DECRYPT && |
4339 | 0 | !dm_flags(cd, DM_LINEAR, &dmt_flags) && (dmt_flags & DM_DEFERRED_SUPPORTED)) |
4340 | 0 | dm_remove_device(cd, rh->device_name, CRYPT_DEACTIVATE_DEFERRED); |
4341 | 0 | } |
4342 | | |
4343 | 0 | if (finished) { |
4344 | 0 | if (reencrypt_wipe_unused_device_area(cd, rh)) |
4345 | 0 | log_err(cd, _("Failed to wipe unused data device area.")); |
4346 | 0 | if (reencrypt_get_data_offset_new(hdr) && LUKS2_set_keyslots_size(hdr, reencrypt_get_data_offset_new(hdr))) |
4347 | 0 | log_dbg(cd, "Failed to set new keyslots area size."); |
4348 | 0 | if (rh->digest_old >= 0 && rh->digest_new != rh->digest_old) |
4349 | 0 | for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) |
4350 | 0 | if (LUKS2_digest_by_keyslot(hdr, i) == rh->digest_old && crypt_keyslot_destroy(cd, i)) |
4351 | 0 | log_err(cd, _("Failed to remove unused (unbound) keyslot %d."), i); |
4352 | |
|
4353 | 0 | if (reencrypt_erase_backup_segments(cd, hdr)) |
4354 | 0 | log_dbg(cd, "Failed to erase backup segments"); |
4355 | |
|
4356 | 0 | if (reencrypt_update_flag(cd, 0, false, false)) |
4357 | 0 | log_dbg(cd, "Failed to disable reencryption requirement flag."); |
4358 | | |
4359 | | /* metadata commit point also removing reencryption flag on-disk */ |
4360 | 0 | if (crypt_keyslot_destroy(cd, rh->reenc_keyslot)) { |
4361 | 0 | log_err(cd, _("Failed to remove reencryption keyslot.")); |
4362 | 0 | return -EINVAL; |
4363 | 0 | } |
4364 | 0 | } |
4365 | | |
4366 | 0 | return 0; |
4367 | 0 | } |
4368 | | |
4369 | | static void reencrypt_teardown_rollback(struct crypt_device *cd, struct luks2_hdr *hdr, |
4370 | | struct luks2_reencrypt *rh) |
4371 | 0 | { |
4372 | | /* |
4373 | | * We cannot rollback for REENC_PROTECTION_NONE. It does not commit metadata as |
4374 | | * it progresses. In this case, the device stack is intentionally left as-is. |
4375 | | */ |
4376 | 0 | if (rh->rp.type <= REENC_PROTECTION_NONE) |
4377 | 0 | return; |
4378 | | |
4379 | | /* |
4380 | | * If metadata rollback fails, we cannot proceed with device teardown |
4381 | | * as we do not have proper metadata snapshot for LUKS2_reload(). |
4382 | | */ |
4383 | 0 | if (LUKS2_hdr_rollback(cd, hdr)) |
4384 | 0 | return; |
4385 | | |
4386 | 0 | if (!rh->online) |
4387 | 0 | return; |
4388 | | |
4389 | 0 | teardown_overlay_devices(cd, rh); |
4390 | 0 | } |
4391 | | |
4392 | | static void reencrypt_teardown_fatal(struct crypt_device *cd, struct luks2_reencrypt *rh) |
4393 | 0 | { |
4394 | 0 | log_err(cd, _("Fatal error while reencrypting chunk starting at %" PRIu64 ", %" PRIu64 " sectors long."), |
4395 | 0 | (rh->offset >> SECTOR_SHIFT) + crypt_get_data_offset(cd), rh->length >> SECTOR_SHIFT); |
4396 | |
|
4397 | 0 | if (rh->online) { |
4398 | 0 | log_err(cd, _("Online reencryption failed.")); |
4399 | 0 | if (dm_status_suspended(cd, rh->hotzone_name) > 0) |
4400 | 0 | replace_hotzone_device_with_error(cd, rh); |
4401 | 0 | } |
4402 | 0 | } |
4403 | | |
4404 | | static int reencrypt_teardown(struct crypt_device *cd, struct luks2_hdr *hdr, |
4405 | | struct luks2_reencrypt *rh, reenc_status_t rs, bool interrupted, |
4406 | | int (*progress)(uint64_t size, uint64_t offset, void *usrptr), |
4407 | | void *usrptr) |
4408 | 0 | { |
4409 | 0 | int r; |
4410 | |
|
4411 | 0 | switch (rs) { |
4412 | 0 | case REENC_OK: |
4413 | 0 | if (progress && !interrupted) |
4414 | 0 | progress(rh->device_size, rh->progress, usrptr); |
4415 | 0 | r = reencrypt_teardown_ok(cd, hdr, rh); |
4416 | 0 | break; |
4417 | 0 | case REENC_ERR_ROLLBACK_MEMORY: |
4418 | 0 | reencrypt_teardown_rollback(cd, hdr, rh); |
4419 | 0 | r = -EINVAL; |
4420 | 0 | break; |
4421 | 0 | case REENC_ERR_FATAL: |
4422 | 0 | reencrypt_teardown_fatal(cd, rh); |
4423 | | /* fall-through */ |
4424 | 0 | default: |
4425 | 0 | r = -EIO; |
4426 | 0 | } |
4427 | | |
4428 | | /* this frees reencryption lock */ |
4429 | 0 | LUKS2_reencrypt_free(cd, rh); |
4430 | 0 | crypt_set_luks2_reencrypt(cd, NULL); |
4431 | |
|
4432 | 0 | return r; |
4433 | 0 | } |
4434 | | |
4435 | | int crypt_reencrypt_run( |
4436 | | struct crypt_device *cd, |
4437 | | int (*progress)(uint64_t size, uint64_t offset, void *usrptr), |
4438 | | void *usrptr) |
4439 | 0 | { |
4440 | 0 | int r; |
4441 | 0 | crypt_reencrypt_info ri; |
4442 | 0 | struct luks2_hdr *hdr; |
4443 | 0 | struct luks2_reencrypt *rh; |
4444 | 0 | reenc_status_t rs; |
4445 | 0 | bool quit = false; |
4446 | |
|
4447 | 0 | if (onlyLUKS2reencrypt(cd)) |
4448 | 0 | return -EINVAL; |
4449 | | |
4450 | 0 | hdr = crypt_get_hdr(cd, CRYPT_LUKS2); |
4451 | |
|
4452 | 0 | ri = LUKS2_reencrypt_status(hdr); |
4453 | 0 | if (ri > CRYPT_REENCRYPT_CLEAN) { |
4454 | 0 | log_err(cd, _("Cannot proceed with reencryption. Unexpected reencryption status.")); |
4455 | 0 | return -EINVAL; |
4456 | 0 | } |
4457 | | |
4458 | 0 | rh = crypt_get_luks2_reencrypt(cd); |
4459 | 0 | if (!rh || (!rh->reenc_lock && crypt_metadata_locking_enabled())) { |
4460 | 0 | log_err(cd, _("Missing or invalid reencrypt context.")); |
4461 | 0 | return -EINVAL; |
4462 | 0 | } |
4463 | | |
4464 | 0 | log_dbg(cd, "Resuming LUKS2 reencryption."); |
4465 | |
|
4466 | 0 | if (rh->online) { |
4467 | | /* This is last resort to avoid data corruption. Abort is justified here. */ |
4468 | 0 | assert(device_direct_io(crypt_data_device(cd))); |
4469 | |
|
4470 | 0 | if (reencrypt_init_device_stack(cd, rh)) { |
4471 | 0 | log_err(cd, _("Failed to initialize reencryption device stack.")); |
4472 | 0 | return -EINVAL; |
4473 | 0 | } |
4474 | 0 | } |
4475 | | |
4476 | 0 | log_dbg(cd, "Progress %" PRIu64 ", device_size %" PRIu64, rh->progress, rh->device_size); |
4477 | |
|
4478 | 0 | rs = REENC_OK; |
4479 | |
|
4480 | 0 | if (progress && progress(rh->device_size, rh->progress, usrptr)) |
4481 | 0 | quit = true; |
4482 | |
|
4483 | 0 | while (!quit && (rh->device_size > rh->progress)) { |
4484 | 0 | rs = reencrypt_step(cd, hdr, rh, rh->device_size, rh->online); |
4485 | 0 | if (rs != REENC_OK) |
4486 | 0 | break; |
4487 | | |
4488 | 0 | log_dbg(cd, "Progress %" PRIu64 ", device_size %" PRIu64, rh->progress, rh->device_size); |
4489 | 0 | if (progress && progress(rh->device_size, rh->progress, usrptr)) |
4490 | 0 | quit = true; |
4491 | |
|
4492 | 0 | r = reencrypt_context_update(cd, rh); |
4493 | 0 | if (r) { |
4494 | 0 | log_err(cd, _("Failed to update reencryption context.")); |
4495 | 0 | rs = REENC_ERR_ROLLBACK_MEMORY; |
4496 | 0 | break; |
4497 | 0 | } |
4498 | | |
4499 | 0 | log_dbg(cd, "Next reencryption offset will be %" PRIu64 " sectors.", rh->offset); |
4500 | 0 | log_dbg(cd, "Next reencryption chunk size will be %" PRIu64 " sectors).", rh->length); |
4501 | 0 | } |
4502 | |
|
4503 | 0 | r = reencrypt_teardown(cd, hdr, rh, rs, quit, progress, usrptr); |
4504 | 0 | return r; |
4505 | 0 | } |
4506 | | |
4507 | | |
4508 | | static int reencrypt_recovery(struct crypt_device *cd, |
4509 | | struct luks2_hdr *hdr, |
4510 | | uint64_t device_size, |
4511 | | struct volume_key *vks) |
4512 | 0 | { |
4513 | 0 | int r; |
4514 | 0 | struct luks2_reencrypt *rh = NULL; |
4515 | |
|
4516 | 0 | r = reencrypt_load(cd, hdr, device_size, 0, 0, vks, &rh); |
4517 | 0 | if (r < 0) { |
4518 | 0 | log_err(cd, _("Failed to load LUKS2 reencryption context.")); |
4519 | 0 | return r; |
4520 | 0 | } |
4521 | | |
4522 | 0 | r = reencrypt_recover_segment(cd, hdr, rh, vks); |
4523 | 0 | if (r < 0) |
4524 | 0 | goto out; |
4525 | | |
4526 | 0 | if ((r = reencrypt_assign_segments(cd, hdr, rh, 0, 0))) |
4527 | 0 | goto out; |
4528 | | |
4529 | 0 | r = reencrypt_context_update(cd, rh); |
4530 | 0 | if (r) { |
4531 | 0 | log_err(cd, _("Failed to update reencryption context.")); |
4532 | 0 | goto out; |
4533 | 0 | } |
4534 | | |
4535 | 0 | r = reencrypt_teardown_ok(cd, hdr, rh); |
4536 | 0 | if (!r) |
4537 | 0 | r = LUKS2_hdr_write(cd, hdr); |
4538 | 0 | out: |
4539 | 0 | LUKS2_reencrypt_free(cd, rh); |
4540 | |
|
4541 | 0 | return r; |
4542 | 0 | } |
4543 | | #else /* USE_LUKS2_REENCRYPTION */ |
4544 | | int crypt_reencrypt_run( |
4545 | | struct crypt_device *cd, |
4546 | | int (*progress)(uint64_t size, uint64_t offset, void *usrptr), |
4547 | | void *usrptr) |
4548 | | { |
4549 | | UNUSED(progress); |
4550 | | UNUSED(usrptr); |
4551 | | |
4552 | | log_err(cd, _("This operation is not supported for this device type.")); |
4553 | | return -ENOTSUP; |
4554 | | } |
4555 | | #endif |
4556 | | |
4557 | | int crypt_reencrypt( |
4558 | | struct crypt_device *cd, |
4559 | | int (*progress)(uint64_t size, uint64_t offset, void *usrptr)) |
4560 | 0 | { |
4561 | 0 | return crypt_reencrypt_run(cd, progress, NULL); |
4562 | 0 | } |
4563 | | |
4564 | | /* |
4565 | | * use only for calculation of minimal data device size. |
4566 | | * The real data offset is taken directly from segments! |
4567 | | */ |
4568 | | uint64_t LUKS2_reencrypt_data_offset(struct luks2_hdr *hdr, bool blockwise) |
4569 | 0 | { |
4570 | 0 | crypt_reencrypt_info ri = LUKS2_reencrypt_status(hdr); |
4571 | 0 | uint64_t data_offset = LUKS2_get_data_offset(hdr); |
4572 | |
|
4573 | 0 | if (ri == CRYPT_REENCRYPT_CLEAN && reencrypt_direction(hdr) == CRYPT_REENCRYPT_FORWARD) |
4574 | 0 | data_offset += reencrypt_data_shift(hdr) >> SECTOR_SHIFT; |
4575 | |
|
4576 | 0 | return blockwise ? data_offset : data_offset << SECTOR_SHIFT; |
4577 | 0 | } |
4578 | | |
4579 | | /* internal only */ |
4580 | | int LUKS2_reencrypt_check_device_size(struct crypt_device *cd, struct luks2_hdr *hdr, |
4581 | | uint64_t check_size, uint64_t *dev_size, bool device_exclusive_check, bool dynamic) |
4582 | 0 | { |
4583 | 0 | int r; |
4584 | 0 | uint64_t data_offset, real_size = 0; |
4585 | |
|
4586 | 0 | if (reencrypt_direction(hdr) == CRYPT_REENCRYPT_BACKWARD && |
4587 | 0 | (LUKS2_get_segment_by_flag(hdr, "backup-moved-segment") || dynamic)) |
4588 | 0 | check_size += reencrypt_data_shift(hdr); |
4589 | |
|
4590 | 0 | r = device_check_access(cd, crypt_data_device(cd), |
4591 | 0 | device_exclusive_check ? DEV_EXCL : DEV_OK); |
4592 | 0 | if (r) |
4593 | 0 | return r; |
4594 | | |
4595 | 0 | data_offset = LUKS2_reencrypt_data_offset(hdr, false); |
4596 | |
|
4597 | 0 | r = device_check_size(cd, crypt_data_device(cd), data_offset, 1); |
4598 | 0 | if (r) |
4599 | 0 | return r; |
4600 | | |
4601 | 0 | r = device_size(crypt_data_device(cd), &real_size); |
4602 | 0 | if (r) |
4603 | 0 | return r; |
4604 | | |
4605 | 0 | log_dbg(cd, "Required minimal device size: %" PRIu64 " (%" PRIu64 " sectors)" |
4606 | 0 | ", real device size: %" PRIu64 " (%" PRIu64 " sectors) " |
4607 | 0 | "calculated device size: %" PRIu64 " (%" PRIu64 " sectors)", |
4608 | 0 | check_size, check_size >> SECTOR_SHIFT, real_size, real_size >> SECTOR_SHIFT, |
4609 | 0 | real_size - data_offset, (real_size - data_offset) >> SECTOR_SHIFT); |
4610 | |
|
4611 | 0 | if (real_size < data_offset || (check_size && real_size < check_size)) { |
4612 | 0 | log_err(cd, _("Device %s is too small."), device_path(crypt_data_device(cd))); |
4613 | 0 | return -EINVAL; |
4614 | 0 | } |
4615 | | |
4616 | 0 | *dev_size = real_size - data_offset; |
4617 | |
|
4618 | 0 | return 0; |
4619 | 0 | } |
4620 | | #if USE_LUKS2_REENCRYPTION |
4621 | | /* returns keyslot number on success (>= 0) or negative errnor otherwise */ |
4622 | | int LUKS2_reencrypt_locked_recovery_by_vks(struct crypt_device *cd, |
4623 | | struct volume_key *vks) |
4624 | 0 | { |
4625 | 0 | uint64_t minimal_size, device_size; |
4626 | 0 | int r = -EINVAL; |
4627 | 0 | struct luks2_hdr *hdr = crypt_get_hdr(cd, CRYPT_LUKS2); |
4628 | |
|
4629 | 0 | log_dbg(cd, "Entering reencryption crash recovery."); |
4630 | |
|
4631 | 0 | if (LUKS2_get_data_size(hdr, &minimal_size, NULL)) |
4632 | 0 | return r; |
4633 | 0 | if (LUKS2_reencrypt_check_device_size(cd, hdr, minimal_size, &device_size, true, false)) |
4634 | 0 | goto out; |
4635 | | |
4636 | 0 | r = reencrypt_recovery(cd, hdr, device_size, vks); |
4637 | |
|
4638 | 0 | out: |
4639 | 0 | if (r < 0) |
4640 | 0 | crypt_drop_uploaded_keyring_key(cd, vks); |
4641 | 0 | return r; |
4642 | 0 | } |
4643 | | #endif |
4644 | | crypt_reencrypt_info LUKS2_reencrypt_get_params(struct luks2_hdr *hdr, |
4645 | | struct crypt_params_reencrypt *params) |
4646 | 0 | { |
4647 | 0 | crypt_reencrypt_info ri; |
4648 | 0 | int digest; |
4649 | 0 | uint8_t version; |
4650 | |
|
4651 | 0 | if (params) |
4652 | 0 | memset(params, 0, sizeof(*params)); |
4653 | |
|
4654 | 0 | ri = LUKS2_reencrypt_status(hdr); |
4655 | 0 | if (ri == CRYPT_REENCRYPT_NONE || ri == CRYPT_REENCRYPT_INVALID || !params) |
4656 | 0 | return ri; |
4657 | | |
4658 | 0 | digest = LUKS2_digest_by_keyslot(hdr, LUKS2_find_keyslot(hdr, "reencrypt")); |
4659 | 0 | if (digest < 0 && digest != -ENOENT) |
4660 | 0 | return CRYPT_REENCRYPT_INVALID; |
4661 | | |
4662 | | /* |
4663 | | * In case there's an old "online-reencrypt" requirement or reencryption |
4664 | | * keyslot digest is missing inform caller reencryption metadata requires repair. |
4665 | | */ |
4666 | 0 | if (!LUKS2_config_get_reencrypt_version(hdr, &version) && |
4667 | 0 | (version < 2 || digest == -ENOENT)) { |
4668 | 0 | params->flags |= CRYPT_REENCRYPT_REPAIR_NEEDED; |
4669 | 0 | return ri; |
4670 | 0 | } |
4671 | | |
4672 | 0 | params->mode = reencrypt_mode(hdr); |
4673 | 0 | params->direction = reencrypt_direction(hdr); |
4674 | 0 | params->resilience = reencrypt_resilience_type(hdr); |
4675 | 0 | params->hash = reencrypt_resilience_hash(hdr); |
4676 | 0 | params->data_shift = reencrypt_data_shift(hdr) >> SECTOR_SHIFT; |
4677 | 0 | params->max_hotzone_size = 0; |
4678 | 0 | if (LUKS2_get_segment_id_by_flag(hdr, "backup-moved-segment") >= 0) |
4679 | 0 | params->flags |= CRYPT_REENCRYPT_MOVE_FIRST_SEGMENT; |
4680 | |
|
4681 | 0 | return ri; |
4682 | 0 | } |