Debian Codegraph @

src/veritysetup/veritysetup.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4 #include <sys/stat.h>
5
6 #include "sd-device.h"
7
8 #include "alloc-util.h"
9 #include "argv-util.h"
10 #include "cryptsetup-util.h"
11 #include "extract-word.h"
12 #include "fileio.h"
13 #include "format-table.h"
14 #include "fstab-util.h"
15 #include "help-util.h"
16 #include "hexdecoct.h"
17 #include "log.h"
18 #include "main-func.h"
19 #include "parse-util.h"
20 #include "path-util.h"
21 #include "pcrextend-util.h"
22 #include "string-util.h"
23 #include "strv.h"
24 #include "tpm2-util.h"
25 #include "verbs.h"
26
27 static char *arg_hash = NULL; /* the hash algorithm */
28 static bool arg_superblock = true;
29 static int arg_format = 1;
30 static uint64_t arg_data_block_size = 4096;
31 static uint64_t arg_hash_block_size = 4096;
32 static uint64_t arg_data_blocks = 0;
33 static uint64_t arg_hash_offset = 0;
34 static void *arg_salt = NULL;
35 static uint64_t arg_salt_size = 32;
36 static char *arg_uuid = NULL;
37 static uint32_t arg_activate_flags = CRYPT_ACTIVATE_READONLY;
38 static char *arg_fec_what = NULL;
39 static uint64_t arg_fec_offset = 0;
40 static uint64_t arg_fec_roots = 2;
41 static void *arg_root_hash_signature = NULL;
42 static size_t arg_root_hash_signature_size = 0;
43 static bool arg_root_hash_signature_auto = false;
44 static char *arg_tpm2_measure_nvpcr = NULL;
45
46 STATIC_DESTRUCTOR_REGISTER(arg_hash, freep);
47 STATIC_DESTRUCTOR_REGISTER(arg_salt, freep);
48 STATIC_DESTRUCTOR_REGISTER(arg_uuid, freep);
49 STATIC_DESTRUCTOR_REGISTER(arg_fec_what, freep);
50 STATIC_DESTRUCTOR_REGISTER(arg_root_hash_signature, freep);
51 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_measure_nvpcr, freep);
52
53 static int help(void) {
54 _cleanup_(table_unrefp) Table *verbs = NULL;
55 int r;
56
57 r = verbs_get_help_table(&verbs);
58 if (r < 0)
59 return r;
60
61 help_cmdline("COMMAND ...");
62 help_abstract("Attach or detach a verity protected block device.");
63
64 help_section("Commands");
65 r = table_print_or_warn(verbs);
66 if (r < 0)
67 return r;
68
69 help_man_page_reference("[email protected]", "8");
70 return 0;
71 }
72
73 static int parse_roothashsig_option(const char *option, bool strict) {
74 _cleanup_free_ void *rhs = NULL;
75 size_t rhss = 0;
76 bool set_auto = false;
77 int r;
78
79 assert(option);
80
81 const char *value = startswith(option, "base64:");
82 if (value) {
83 r = unbase64mem(value, &rhs, &rhss);
84 if (r < 0)
85 return log_error_errno(r, "Failed to parse root hash signature '%s': %m", option);
86
87 } else if (path_is_absolute(option)) {
88 r = read_full_file_full(
89 AT_FDCWD,
90 option,
91 /* offset= */ UINT64_MAX,
92 /* size= */ SIZE_MAX,
93 READ_FULL_FILE_CONNECT_SOCKET,
94 /* bind_name= */ NULL,
95 (char**) &rhs,
96 &rhss);
97 if (r < 0)
98 return log_error_errno(r, "Failed to read root hash signature: %m");
99
100 } else if (streq(option, "auto"))
101 /* auto → Derive signature from udev property ID_DISSECT_PART_ROOTHASH_SIG */
102 set_auto = true;
103 else if (strict)
104 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
105 "root-hash-signature= expects either full path to signature file or "
106 "base64 string encoding signature prefixed by base64:.");
107 else
108 return false;
109
110 free_and_replace(arg_root_hash_signature, rhs);
111 arg_root_hash_signature_size = rhss;
112 arg_root_hash_signature_auto = set_auto;
113
114 return true;
115 }
116
117 static int parse_block_size(const char *t, uint64_t *size) {
118 uint64_t u;
119 int r;
120
121 assert(size);
122
123 r = parse_size(t, 1024, &u);
124 if (r < 0)
125 return r;
126
127 if (u < 512 || u > (512 * 1024))
128 return -ERANGE;
129
130 if ((u % 512) != 0 || !ISPOWEROF2(u))
131 return -EINVAL;
132
133 *size = u;
134
135 return 0;
136 }
137
138 static int parse_options(const char *options) {
139 int r;
140
141 /* backward compatibility with the obsolete ROOTHASHSIG positional argument */
142 r = parse_roothashsig_option(options, /* strict= */ false);
143 if (r < 0)
144 return r;
145 if (r > 0) {
146 log_warning("Usage of ROOTHASHSIG positional argument is deprecated. "
147 "Please use the option root-hash-signature=%s instead.", options);
148 return 0;
149 }
150
151 for (;;) {
152 _cleanup_free_ char *word = NULL;
153 const char *val;
154
155 r = extract_first_word(&options, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS | EXTRACT_UNESCAPE_SEPARATORS);
156 if (r < 0)
157 return log_error_errno(r, "Failed to parse options: %m");
158 if (r == 0)
159 break;
160
161 if (STR_IN_SET(word, "noauto", "auto", "nofail", "fail", "_netdev"))
162 continue;
163
164 if (isempty(word))
165 continue;
166 else if (streq(word, "ignore-corruption"))
167 arg_activate_flags |= CRYPT_ACTIVATE_IGNORE_CORRUPTION;
168 else if (streq(word, "restart-on-corruption"))
169 arg_activate_flags |= CRYPT_ACTIVATE_RESTART_ON_CORRUPTION;
170 else if (streq(word, "ignore-zero-blocks"))
171 arg_activate_flags |= CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS;
172 else if (streq(word, "check-at-most-once"))
173 arg_activate_flags |= CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE;
174 else if (streq(word, "panic-on-corruption"))
175 arg_activate_flags |= CRYPT_ACTIVATE_PANIC_ON_CORRUPTION;
176 else if ((val = startswith(word, "superblock="))) {
177
178 r = parse_boolean(val);
179 if (r < 0)
180 return log_error_errno(r, "Failed to parse boolean '%s': %m", word);
181
182 arg_superblock = r;
183 } else if ((val = startswith(word, "format="))) {
184
185 if (!STR_IN_SET(val, "0", "1"))
186 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "format= expects either 0 (original Chrome OS version) or "
187 "1 (modern version).");
188
189 arg_format = val[0] - '0';
190 } else if ((val = startswith(word, "data-block-size="))) {
191 uint64_t sz;
192
193 r = parse_block_size(val, &sz);
194 if (r < 0)
195 return log_error_errno(r, "Failed to parse size '%s': %m", word);
196
197 arg_data_block_size = sz;
198 } else if ((val = startswith(word, "hash-block-size="))) {
199 uint64_t sz;
200
201 r = parse_block_size(val, &sz);
202 if (r < 0)
203 return log_error_errno(r, "Failed to parse size '%s': %m", word);
204
205 arg_hash_block_size = sz;
206 } else if ((val = startswith(word, "data-blocks="))) {
207 uint64_t u;
208
209 r = safe_atou64(val, &u);
210 if (r < 0)
211 return log_error_errno(r, "Failed to parse number '%s': %m", word);
212
213 arg_data_blocks = u;
214 } else if ((val = startswith(word, "hash-offset="))) {
215 uint64_t off;
216
217 r = parse_size(val, 1024, &off);
218 if (r < 0)
219 return log_error_errno(r, "Failed to parse offset '%s': %m", word);
220 if (off % 512 != 0)
221 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "hash-offset= expects a 512-byte aligned value.");
222
223 arg_hash_offset = off;
224 } else if ((val = startswith(word, "salt="))) {
225
226 if (isempty(val)) {
227 arg_salt = mfree(arg_salt);
228 arg_salt_size = 32;
229 } else if (streq(val, "-")) {
230 arg_salt = mfree(arg_salt);
231 arg_salt_size = 0;
232 } else {
233 size_t l;
234 void *m;
235
236 r = unhexmem(val, &m, &l);
237 if (r < 0)
238 return log_error_errno(r, "Failed to parse salt '%s': %m", word);
239
240 free_and_replace(arg_salt, m);
241 arg_salt_size = l;
242 }
243 } else if ((val = startswith(word, "uuid="))) {
244
245 r = sd_id128_from_string(val, NULL);
246 if (r < 0)
247 return log_error_errno(r, "Failed to parse UUID '%s': %m", word);
248
249 r = free_and_strdup(&arg_uuid, val);
250 if (r < 0)
251 return log_oom();
252 } else if ((val = startswith(word, "hash="))) {
253
254 r = free_and_strdup(&arg_hash, val);
255 if (r < 0)
256 return log_oom();
257 } else if ((val = startswith(word, "fec-device="))) {
258 _cleanup_free_ char *what = NULL;
259
260 what = fstab_node_to_udev_node(val);
261 if (!what)
262 return log_oom();
263
264 if (!path_is_absolute(what))
265 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "fec-device= expects an absolute path.");
266
267 if (!path_is_normalized(what))
268 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "fec-device= expects an normalized path.");
269
270 r = free_and_strdup(&arg_fec_what, what);
271 if (r < 0)
272 return log_oom();
273 } else if ((val = startswith(word, "fec-offset="))) {
274 uint64_t off;
275
276 r = parse_size(val, 1024, &off);
277 if (r < 0)
278 return log_error_errno(r, "Failed to parse offset '%s': %m", word);
279 if (off % 512 != 0)
280 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "fec-offset= expects a 512-byte aligned value.");
281
282 arg_fec_offset = off;
283 } else if ((val = startswith(word, "fec-roots="))) {
284 uint64_t u;
285
286 r = safe_atou64(val, &u);
287 if (r < 0)
288 return log_error_errno(r, "Failed to parse number '%s', ignoring: %m", word);
289 if (u < 2 || u > 24)
290 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "fec-rootfs= expects a value between 2 and 24 (including).");
291
292 arg_fec_roots = u;
293 } else if ((val = startswith(word, "root-hash-signature="))) {
294 r = parse_roothashsig_option(val, /* strict= */ true);
295 if (r < 0)
296 return r;
297
298 } else if ((val = startswith(word, "tpm2-measure-nvpcr="))) {
299 r = isempty(val) ? 0 : parse_boolean(val);
300 if (r == 0) {
301 arg_tpm2_measure_nvpcr = mfree(arg_tpm2_measure_nvpcr);
302 return 0;
303 }
304 if (r > 0)
305 val = "verity";
306 else if (!tpm2_nvpcr_name_is_valid(val)) {
307 log_warning("Invalid NvPCR name, ignoring: %s", word);
308 return 0;
309 }
310
311 if (free_and_strdup(&arg_tpm2_measure_nvpcr, val) < 0)
312 return log_oom();
313 } else
314 log_warning("Encountered unknown option '%s', ignoring.", word);
315 }
316
317 return r;
318 }
319
320 VERB(verb_attach, "attach", "VOLUME DATADEVICE HASHDEVICE ROOTHASH [OPTIONS]", 5, 6, 0,
321 "Attach a verity protected block device");
322 static int verb_attach(int argc, char *argv[], uintptr_t _data, void *userdata) {
323 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
324 _cleanup_free_ void *rh = NULL;
325 struct crypt_params_verity p = {};
326 crypt_status_info status;
327 size_t rh_size = 0;
328 int r;
329
330 assert(argc >= 5);
331
332 const char *volume = argv[1],
333 *data_device = argv[2],
334 *verity_device = argv[3],
335 *root_hash = argv[4],
336 *options = mangle_none(argc > 5 ? argv[5] : NULL);
337
338 if (!filename_is_valid(volume))
339 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
340
341 if (options) {
342 r = parse_options(options);
343 if (r < 0)
344 return log_error_errno(r, "Failed to parse options: %m");
345 }
346
347 if (empty_or_dash(root_hash) || streq_ptr(root_hash, "auto"))
348 root_hash = NULL;
349
350 _cleanup_(sd_device_unrefp) sd_device *datadev = NULL;
351 if (!root_hash || arg_root_hash_signature_auto) {
352 r = sd_device_new_from_path(&datadev, data_device);
353 if (r < 0)
354 return log_error_errno(r, "Failed to acquire udev object for data device '%s': %m", data_device);
355 }
356
357 if (!root_hash) {
358 /* If no literal root hash is specified try to determine it automatically from the
359 * ID_DISSECT_PART_ROOTHASH udev property. */
360 r = sd_device_get_property_value(ASSERT_PTR(datadev), "ID_DISSECT_PART_ROOTHASH", &root_hash);
361 if (r < 0)
362 return log_error_errno(r, "No root hash specified, and device doesn't carry ID_DISSECT_PART_ROOTHASH property, cannot determine root hash.");
363 }
364
365 r = unhexmem(root_hash, &rh, &rh_size);
366 if (r < 0)
367 return log_error_errno(r, "Failed to parse root hash: %m");
368
369 if (arg_root_hash_signature_auto) {
370 assert(!arg_root_hash_signature);
371 assert(arg_root_hash_signature_size == 0);
372
373 const char *t;
374 r = sd_device_get_property_value(ASSERT_PTR(datadev), "ID_DISSECT_PART_ROOTHASH_SIG", &t);
375 if (r < 0)
376 return log_error_errno(r, "Automatic root hash signature pick up requested, and device doesn't carry ID_DISSECT_PART_ROOTHASH_SIG property, cannot determine root hash signature.");
377
378 r = unbase64mem(t, &arg_root_hash_signature, &arg_root_hash_signature_size);
379 if (r < 0)
380 return log_error_errno(r, "Failed to decode root hash signature data from udev data device: %m");
381 }
382
383 r = sym_crypt_init(&cd, verity_device);
384 if (r < 0)
385 return log_error_errno(r, "Failed to open verity device %s: %m", verity_device);
386
387 cryptsetup_enable_logging(cd);
388
389 status = sym_crypt_status(cd, volume);
390 if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) {
391 log_info("Volume %s already active.", volume);
392 return 0;
393 }
394
395 if (arg_superblock) {
396 p = (struct crypt_params_verity) {
397 .fec_device = arg_fec_what,
398 .hash_area_offset = arg_hash_offset,
399 .fec_area_offset = arg_fec_offset,
400 .fec_roots = arg_fec_roots,
401 };
402
403 r = sym_crypt_load(cd, CRYPT_VERITY, &p);
404 if (r < 0)
405 return log_error_errno(r, "Failed to load verity superblock: %m");
406 } else {
407 p = (struct crypt_params_verity) {
408 .hash_name = arg_hash,
409 .data_device = data_device,
410 .fec_device = arg_fec_what,
411 .salt = arg_salt,
412 .salt_size = arg_salt_size,
413 .hash_type = arg_format,
414 .data_block_size = arg_data_block_size,
415 .hash_block_size = arg_hash_block_size,
416 .data_size = arg_data_blocks,
417 .hash_area_offset = arg_hash_offset,
418 .fec_area_offset = arg_fec_offset,
419 .fec_roots = arg_fec_roots,
420 .flags = CRYPT_VERITY_NO_HEADER,
421 };
422
423 r = sym_crypt_format(cd, CRYPT_VERITY, NULL, NULL, arg_uuid, NULL, 0, &p);
424 if (r < 0)
425 return log_error_errno(r, "Failed to format verity superblock: %m");
426 }
427
428 r = sym_crypt_set_data_device(cd, data_device);
429 if (r < 0)
430 return log_error_errno(r, "Failed to configure data device: %m");
431
432 if (arg_root_hash_signature_size > 0) {
433 r = sym_crypt_activate_by_signed_key(cd, volume, rh, rh_size, arg_root_hash_signature, arg_root_hash_signature_size, arg_activate_flags);
434 if (r < 0) {
435 log_info_errno(r, "Unable to activate verity device '%s' with root hash signature (%m), retrying without.", volume);
436
437 r = sym_crypt_activate_by_volume_key(cd, volume, rh, rh_size, arg_activate_flags);
438 if (r < 0)
439 return log_error_errno(r, "Failed to activate verity device '%s' both with and without root hash signature: %m", volume);
440
441 log_info("Activation of verity device '%s' succeeded without root hash signature.", volume);
442 }
443 } else
444 r = sym_crypt_activate_by_volume_key(cd, volume, rh, rh_size, arg_activate_flags);
445 if (r < 0)
446 return log_error_errno(r, "Failed to set up verity device '%s': %m", volume);
447
448 (void) pcrextend_verity_now(
449 volume,
450 &IOVEC_MAKE(rh, rh_size),
451 &IOVEC_MAKE(arg_root_hash_signature, arg_root_hash_signature_size));
452
453 return 0;
454 }
455
456 VERB(verb_detach, "detach", "VOLUME", 2, 2, 0,
457 "Detach a verity protected block device");
458 static int verb_detach(int argc, char *argv[], uintptr_t _data, void *userdata) {
459 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
460 int r;
461
462 assert(argc == 2);
463
464 const char *volume = argv[1];
465
466 if (!filename_is_valid(volume))
467 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume);
468
469 r = sym_crypt_init_by_name(&cd, volume);
470 if (r == -ENODEV) {
471 log_info("Volume %s 'already' inactive.", volume);
472 return 0;
473 }
474 if (r < 0)
475 return log_error_errno(r, "crypt_init_by_name() for volume '%s' failed: %m", volume);
476
477 cryptsetup_enable_logging(cd);
478
479 r = sym_crypt_deactivate(cd, volume);
480 if (r < 0)
481 return log_error_errno(r, "Failed to deactivate volume '%s': %m", volume);
482
483 return 0;
484 }
485
486 static int run(int argc, char *argv[]) {
487 int r;
488
489 if (argv_looks_like_help(argc, argv))
490 return help();
491
492 log_setup();
493
494 r = DLOPEN_CRYPTSETUP(LOG_ERR, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED);
495 if (r < 0)
496 return r;
497
498 umask(0022);
499
500 return dispatch_verb(strv_skip(argv, 1), /* userdata= */ NULL);
501 }
502
503 DEFINE_MAIN_FUNCTION(run);