Debian Codegraph @

src/cryptenroll/cryptenroll-wipe.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "cryptenroll.h"
5 #include "cryptenroll-wipe.h"
6 #include "cryptsetup-util.h"
7 #include "json-util.h"
8 #include "log.h"
9 #include "parse-util.h"
10 #include "set.h"
11 #include "sort-util.h"
12
13 static int find_all_slots(struct crypt_device *cd, Set *wipe_slots, Set *keep_slots) {
14 int slot_max;
15
16 assert(cd);
17 assert(wipe_slots);
18 assert_se((slot_max = sym_crypt_keyslot_max(CRYPT_LUKS2)) > 0);
19
20 /* Finds all currently assigned slots, and adds them to 'wipe_slots', except if listed already in 'keep_slots' */
21
22 for (int slot = 0; slot < slot_max; slot++) {
23 crypt_keyslot_info status;
24
25 /* No need to check this slot if we already know we want to wipe it or definitely keep it. */
26 if (set_contains(keep_slots, INT_TO_PTR(slot)) ||
27 set_contains(wipe_slots, INT_TO_PTR(slot)))
28 continue;
29
30 status = sym_crypt_keyslot_status(cd, slot);
31 if (!IN_SET(status, CRYPT_SLOT_ACTIVE, CRYPT_SLOT_ACTIVE_LAST))
32 continue;
33
34 if (set_put(wipe_slots, INT_TO_PTR(slot)) < 0)
35 return log_oom();
36 }
37
38 return 0;
39 }
40
41 static int find_empty_passphrase_slots(struct crypt_device *cd, Set *wipe_slots, Set *keep_slots) {
42 size_t vks;
43 int r, slot_max;
44
45 assert(cd);
46 assert(wipe_slots);
47 assert_se((slot_max = sym_crypt_keyslot_max(CRYPT_LUKS2)) > 0);
48
49 /* Finds all slots with an empty passphrase assigned (i.e. "") and adds them to 'wipe_slots', except
50 * if listed already in 'keep_slots' */
51
52 r = sym_crypt_get_volume_key_size(cd);
53 if (r <= 0)
54 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine LUKS volume key size");
55 vks = (size_t) r;
56
57 for (int slot = 0; slot < slot_max; slot++) {
58 _cleanup_(erase_and_freep) char *vk = NULL;
59 crypt_keyslot_info status;
60
61 /* No need to check this slot if we already know we want to wipe it or definitely keep it. */
62 if (set_contains(keep_slots, INT_TO_PTR(slot)) ||
63 set_contains(wipe_slots, INT_TO_PTR(slot)))
64 continue;
65
66 status = sym_crypt_keyslot_status(cd, slot);
67 if (!IN_SET(status, CRYPT_SLOT_ACTIVE, CRYPT_SLOT_ACTIVE_LAST))
68 continue;
69
70 vk = malloc(vks);
71 if (!vk)
72 return log_oom();
73
74 r = sym_crypt_volume_key_get(cd, slot, vk, &vks, "", 0);
75 if (r < 0) {
76 log_debug_errno(r, "Failed to acquire volume key from slot %i with empty password, ignoring: %m", slot);
77 continue;
78 }
79
80 if (set_put(wipe_slots, INT_TO_PTR(r)) < 0)
81 return log_oom();
82 }
83
84 return 0;
85 }
86
87 static int find_slots_by_mask(
88 struct crypt_device *cd,
89 Set *wipe_slots,
90 Set *keep_slots,
91 unsigned by_mask) {
92
93 _cleanup_set_free_ Set *listed_slots = NULL;
94 int r;
95
96 assert(cd);
97 assert(wipe_slots);
98
99 if (by_mask == 0)
100 return 0;
101
102 /* Find all slots that are associated with a token of a type in the specified token type mask */
103
104 for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
105 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
106 sd_json_variant *w, *z;
107 EnrollType t;
108
109 r = cryptsetup_get_token_as_json(cd, token, NULL, &v);
110 if (IN_SET(r, -ENOENT, -EINVAL))
111 continue;
112 if (r < 0) {
113 log_warning_errno(r, "Failed to read JSON token data off disk, ignoring: %m");
114 continue;
115 }
116
117 w = sd_json_variant_by_key(v, "type");
118 if (!w || !sd_json_variant_is_string(w)) {
119 log_warning("Token JSON data lacks type field, ignoring.");
120 continue;
121 }
122
123 t = luks2_token_type_from_string(sd_json_variant_string(w));
124
125 w = sd_json_variant_by_key(v, "keyslots");
126 if (!w || !sd_json_variant_is_array(w)) {
127 log_warning("Token JSON data lacks keyslots field, ignoring.");
128 continue;
129 }
130
131 JSON_VARIANT_ARRAY_FOREACH(z, w) {
132 int slot;
133
134 if (!sd_json_variant_is_string(z)) {
135 log_warning("Token JSON data's keyslot field is not an array of strings, ignoring.");
136 continue;
137 }
138
139 r = safe_atoi(sd_json_variant_string(z), &slot);
140 if (r < 0) {
141 log_warning_errno(r, "Token JSON data's keyslot filed is not an integer formatted as string, ignoring.");
142 continue;
143 }
144
145 if (t >= 0 && (by_mask & (1U << t)) != 0) {
146 /* Selected by token type */
147 if (set_put(wipe_slots, INT_TO_PTR(slot)) < 0)
148 return log_oom();
149 } else if ((by_mask & (1U << ENROLL_PASSWORD)) != 0) {
150 /* If we shall remove all plain password slots, let's maintain a list of
151 * slots that are listed in any tokens, since those are *NOT* plain
152 * passwords */
153 if (set_ensure_allocated(&listed_slots, NULL) < 0)
154 return log_oom();
155
156 if (set_put(listed_slots, INT_TO_PTR(slot)) < 0)
157 return log_oom();
158 }
159 }
160 }
161
162 /* "password" slots are those which have no token assigned. If we shall remove those, iterate through
163 * all slots and mark those for wiping that weren't listed in any token */
164 if ((by_mask & (1U << ENROLL_PASSWORD)) != 0) {
165 int slot_max;
166
167 assert_se((slot_max = sym_crypt_keyslot_max(CRYPT_LUKS2)) > 0);
168
169 for (int slot = 0; slot < slot_max; slot++) {
170 crypt_keyslot_info status;
171
172 /* No need to check this slot if we already know we want to wipe it or definitely keep it. */
173 if (set_contains(keep_slots, INT_TO_PTR(slot)) ||
174 set_contains(wipe_slots, INT_TO_PTR(slot)))
175 continue;
176
177 if (set_contains(listed_slots, INT_TO_PTR(slot))) /* This has a token, hence is not a password. */
178 continue;
179
180 status = sym_crypt_keyslot_status(cd, slot);
181 if (!IN_SET(status, CRYPT_SLOT_ACTIVE, CRYPT_SLOT_ACTIVE_LAST)) /* Not actually assigned? */
182 continue;
183
184 /* Finally, we found a password, add it to the list of slots to wipe */
185 if (set_put(wipe_slots, INT_TO_PTR(slot)) < 0)
186 return log_oom();
187 }
188 }
189
190 return 0;
191 }
192
193 static int find_slot_tokens(struct crypt_device *cd, Set *wipe_slots, Set *keep_slots, Set *wipe_tokens) {
194 int r;
195
196 assert(cd);
197 assert(wipe_slots);
198 assert(keep_slots);
199 assert(wipe_tokens);
200
201 /* Find all tokens matching the slots we want to wipe, so that we can wipe them too. Also, for update
202 * the slots sets according to the token data: add any other slots listed in the tokens we act on. */
203
204 for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
205 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
206 bool shall_wipe = false;
207 sd_json_variant *w, *z;
208
209 r = cryptsetup_get_token_as_json(cd, token, NULL, &v);
210 if (IN_SET(r, -ENOENT, -EINVAL))
211 continue;
212 if (r < 0) {
213 log_warning_errno(r, "Failed to read JSON token data off disk, ignoring: %m");
214 continue;
215 }
216
217 w = sd_json_variant_by_key(v, "keyslots");
218 if (!w || !sd_json_variant_is_array(w)) {
219 log_warning("Token JSON data lacks keyslots field, ignoring.");
220 continue;
221 }
222
223 /* Go through the slots associated with this token: if we shall keep any slot of them, the token shall stay too. */
224 JSON_VARIANT_ARRAY_FOREACH(z, w) {
225 int slot;
226
227 if (!sd_json_variant_is_string(z)) {
228 log_warning("Token JSON data's keyslot field is not an array of strings, ignoring.");
229 continue;
230 }
231
232 r = safe_atoi(sd_json_variant_string(z), &slot);
233 if (r < 0) {
234 log_warning_errno(r, "Token JSON data's keyslot filed is not an integer formatted as string, ignoring.");
235 continue;
236 }
237
238 if (set_contains(keep_slots, INT_TO_PTR(slot))) {
239 shall_wipe = false;
240 break; /* If we shall keep this slot, then this is definite: we will keep its token too */
241 }
242
243 /* If there's a slot associated with this token that we shall wipe, then remove the
244 * token too. But we are careful here: let's continue iterating, maybe there's a slot
245 * that we need to keep, in which case we can reverse the decision again. */
246 if (set_contains(wipe_slots, INT_TO_PTR(slot)))
247 shall_wipe = true;
248 }
249
250 /* Go through the slots again, and this time add them to the list of slots to keep/remove */
251 JSON_VARIANT_ARRAY_FOREACH(z, w) {
252 int slot;
253
254 if (!sd_json_variant_is_string(z))
255 continue;
256 if (safe_atoi(sd_json_variant_string(z), &slot) < 0)
257 continue;
258
259 if (set_put(shall_wipe ? wipe_slots : keep_slots, INT_TO_PTR(slot)) < 0)
260 return log_oom();
261 }
262
263 /* And of course, also remember the tokens to remove. */
264 if (shall_wipe)
265 if (set_put(wipe_tokens, INT_TO_PTR(token)) < 0)
266 return log_oom();
267 }
268
269 return 0;
270 }
271
272 static bool slots_remain(struct crypt_device *cd, Set *wipe_slots, Set *keep_slots) {
273 int slot_max;
274
275 assert(cd);
276 assert_se((slot_max = sym_crypt_keyslot_max(CRYPT_LUKS2)) > 0);
277
278 /* Checks if any slots remaining in the LUKS2 header if we remove all slots listed in 'wipe_slots'
279 * (keeping those listed in 'keep_slots') */
280
281 for (int slot = 0; slot < slot_max; slot++) {
282 crypt_keyslot_info status;
283
284 status = sym_crypt_keyslot_status(cd, slot);
285 if (!IN_SET(status, CRYPT_SLOT_ACTIVE, CRYPT_SLOT_ACTIVE_LAST))
286 continue;
287
288 /* The "keep" set wins if a slot is listed in both sets. This is important so that we can
289 * safely add a new slot and remove all others of the same type, which in a naive
290 * implementation might mean we remove what we just added — which we of course don't want. */
291 if (set_contains(keep_slots, INT_TO_PTR(slot)) ||
292 !set_contains(wipe_slots, INT_TO_PTR(slot)))
293 return true;
294 }
295
296 return false;
297 }
298
299 int wipe_slots(struct crypt_device *cd,
300 const int explicit_slots[],
301 size_t n_explicit_slots,
302 WipeScope by_scope,
303 unsigned by_mask,
304 int except_slot) {
305
306 _cleanup_set_free_ Set *wipe_slots = NULL, *wipe_tokens = NULL, *keep_slots = NULL;
307 _cleanup_free_ int *ordered_slots = NULL, *ordered_tokens = NULL;
308 size_t n_ordered_slots = 0, n_ordered_tokens = 0;
309 int r, slot_max, ret;
310 void *e;
311
312 assert_se(cd);
313
314 /* Shortcut if nothing to wipe. */
315 if (n_explicit_slots == 0 && by_mask == 0 && by_scope == WIPE_EXPLICIT)
316 return 0;
317
318 /* So this is a bit more complicated than I'd wish, but we want support three different axis for wiping slots:
319 *
320 * 1. Wiping by slot indexes
321 * 2. Wiping slots of specified token types
322 * 3. Wiping "all" entries, or entries with an empty password (i.e. "")
323 *
324 * (or any combination of the above)
325 *
326 * Plus: We always want to remove tokens matching the slots.
327 * Plus: We always want to exclude the slots/tokens we just added.
328 */
329
330 wipe_slots = set_new(NULL);
331 keep_slots = set_new(NULL);
332 wipe_tokens = set_new(NULL);
333 if (!wipe_slots || !keep_slots || !wipe_tokens)
334 return log_oom();
335
336 /* Let's maintain one set of slots for the slots we definitely want to keep */
337 if (except_slot >= 0)
338 if (set_put(keep_slots, INT_TO_PTR(except_slot)) < 0)
339 return log_oom();
340
341 assert_se((slot_max = sym_crypt_keyslot_max(CRYPT_LUKS2)) > 0);
342
343 /* Maintain another set of the slots we intend to wipe */
344 for (size_t i = 0; i < n_explicit_slots; i++) {
345 if (explicit_slots[i] >= slot_max)
346 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Slot index %i out of range.", explicit_slots[i]);
347
348 if (set_put(wipe_slots, INT_TO_PTR(explicit_slots[i])) < 0)
349 return log_oom();
350 }
351
352 /* Now, handle the "all" and "empty passphrase" cases. */
353 switch (by_scope) {
354
355 case WIPE_EXPLICIT:
356 break; /* Nothing to do here */
357
358 case WIPE_ALL:
359 r = find_all_slots(cd, wipe_slots, keep_slots);
360 if (r < 0)
361 return r;
362
363 break;
364
365 case WIPE_EMPTY_PASSPHRASE:
366 r = find_empty_passphrase_slots(cd, wipe_slots, keep_slots);
367 if (r < 0)
368 return r;
369
370 break;
371 default:
372 assert_not_reached();
373 }
374
375 /* Then add all slots that match a token type */
376 r = find_slots_by_mask(cd, wipe_slots, keep_slots, by_mask);
377 if (r < 0)
378 return r;
379
380 /* And determine tokens that we shall remove */
381 r = find_slot_tokens(cd, wipe_slots, keep_slots, wipe_tokens);
382 if (r < 0)
383 return r;
384
385 /* Safety check: let's make sure that after we are done there's at least one slot remaining */
386 if (!slots_remain(cd, wipe_slots, keep_slots))
387 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
388 "Wipe operation would leave no valid slots around, can't allow that, sorry.");
389
390 /* Generated ordered lists of the slots and the tokens to remove */
391 ordered_slots = new(int, set_size(wipe_slots));
392 if (!ordered_slots)
393 return log_oom();
394 SET_FOREACH(e, wipe_slots) {
395 int slot = PTR_TO_INT(e);
396
397 if (set_contains(keep_slots, INT_TO_PTR(slot)))
398 continue;
399
400 ordered_slots[n_ordered_slots++] = slot;
401 }
402 typesafe_qsort(ordered_slots, n_ordered_slots, cmp_int);
403
404 ordered_tokens = new(int, set_size(wipe_tokens));
405 if (!ordered_tokens)
406 return log_oom();
407 SET_FOREACH(e, wipe_tokens)
408 ordered_tokens[n_ordered_tokens++] = PTR_TO_INT(e);
409 typesafe_qsort(ordered_tokens, n_ordered_tokens, cmp_int);
410
411 if (n_ordered_slots == 0 && n_ordered_tokens == 0) {
412 log_full(except_slot < 0 ? LOG_NOTICE : LOG_DEBUG,
413 "No slots to remove selected.");
414 return 0;
415 }
416
417 if (DEBUG_LOGGING) {
418 for (size_t i = 0; i < n_ordered_slots; i++)
419 log_debug("Going to wipe slot %i.", ordered_slots[i]);
420 for (size_t i = 0; i < n_ordered_tokens; i++)
421 log_debug("Going to wipe token %i.", ordered_tokens[i]);
422 }
423
424 /* Now, let's actually start wiping things. (We go from back to front, to make space at the end
425 * first.) */
426 ret = 0;
427 for (size_t i = n_ordered_slots; i > 0; i--) {
428 r = sym_crypt_keyslot_destroy(cd, ordered_slots[i - 1]);
429 if (r < 0) {
430 if (r == -ENOENT)
431 log_warning_errno(r, "Failed to wipe non-existent slot %i, continuing.", ordered_slots[i - 1]);
432 else
433 log_warning_errno(r, "Failed to wipe slot %i, continuing: %m", ordered_slots[i - 1]);
434 if (ret == 0)
435 ret = r;
436 } else
437 log_info("Wiped slot %i.", ordered_slots[i - 1]);
438 }
439
440 for (size_t i = n_ordered_tokens; i > 0; i--) {
441 r = sym_crypt_token_json_set(cd, ordered_tokens[i - 1], NULL);
442 if (r < 0) {
443 log_warning_errno(r, "Failed to wipe token %i, continuing: %m", ordered_tokens[i - 1]);
444 if (ret == 0)
445 ret = r;
446 }
447 }
448
449 return ret;
450 }