2 * veritysetup - setup cryptographic volumes for dm-verity
4 * Copyright (C) 2012-2021 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2012-2021 Milan Broz
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "cryptsetup.h"
24 #define PACKAGE_VERITY "veritysetup"
26 static char *opt_fec_device = NULL;
27 static char *opt_hash_algorithm = NULL;
28 static char *opt_salt = NULL;
29 static char *opt_uuid = NULL;
30 static char *opt_root_hash_signature = NULL;
32 static int opt_use_superblock = 1;
33 static int opt_fec_roots = DEFAULT_VERITY_FEC_ROOTS;
34 static int opt_hash_type = 1;
35 static int opt_data_block_size = DEFAULT_VERITY_DATA_BLOCK;
36 static int opt_hash_block_size = DEFAULT_VERITY_HASH_BLOCK;
37 static uint64_t data_blocks = 0;
38 static uint64_t hash_offset = 0;
39 static uint64_t fec_offset = 0;
40 static int opt_restart_on_corruption = 0;
41 static int opt_panic_on_corruption = 0;
42 static int opt_ignore_corruption = 0;
43 static int opt_ignore_zero_blocks = 0;
44 static int opt_check_at_most_once = 0;
46 static const char **action_argv;
47 static int action_argc;
49 void tools_cleanup(void)
51 FREE_AND_NULL(opt_fec_device);
52 FREE_AND_NULL(opt_hash_algorithm);
53 FREE_AND_NULL(opt_salt);
54 FREE_AND_NULL(opt_uuid);
55 FREE_AND_NULL(opt_root_hash_signature);
58 static int _prepare_format(struct crypt_params_verity *params,
59 const char *data_device,
65 params->hash_name = opt_hash_algorithm ?: DEFAULT_VERITY_HASH;
66 params->data_device = data_device;
67 params->fec_device = opt_fec_device;
68 params->fec_roots = opt_fec_roots;
70 if (opt_salt && !strcmp(opt_salt, "-")) {
71 params->salt_size = 0;
73 } else if (opt_salt) {
74 len = crypt_hex_to_bytes(opt_salt, &salt, 0);
76 log_err(_("Invalid salt string specified."));
79 params->salt_size = len;
82 params->salt_size = DEFAULT_VERITY_SALT_SIZE;
86 params->data_block_size = opt_data_block_size;
87 params->hash_block_size = opt_hash_block_size;
88 params->data_size = data_blocks;
89 params->hash_area_offset = hash_offset;
90 params->fec_area_offset = fec_offset;
91 params->hash_type = opt_hash_type;
92 params->flags = flags;
97 static int action_format(int arg)
99 struct crypt_device *cd = NULL;
100 struct crypt_params_verity params = {};
101 uint32_t flags = CRYPT_VERITY_CREATE_HASH;
104 /* Try to create hash image if doesn't exist */
105 r = open(action_argv[1], O_WRONLY | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
106 if (r < 0 && errno != EEXIST) {
107 log_err(_("Cannot create hash image %s for writing."), action_argv[1]);
110 log_dbg("Created hash image %s.", action_argv[1]);
113 /* Try to create FEC image if doesn't exist */
114 if (opt_fec_device) {
115 r = open(opt_fec_device, O_WRONLY | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
116 if (r < 0 && errno != EEXIST) {
117 log_err(_("Cannot create FEC image %s for writing."), opt_fec_device);
120 log_dbg("Created FEC image %s.", opt_fec_device);
125 if ((r = crypt_init(&cd, action_argv[1])))
128 if (!opt_use_superblock)
129 flags |= CRYPT_VERITY_NO_HEADER;
131 r = _prepare_format(¶ms, action_argv[0], flags);
135 r = crypt_format(cd, CRYPT_VERITY, NULL, NULL, opt_uuid, NULL, 0, ¶ms);
140 free(CONST_CAST(char*)params.salt);
144 static int _activate(const char *dm_device,
145 const char *data_device,
146 const char *hash_device,
147 const char *root_hash,
150 struct crypt_device *cd = NULL;
151 struct crypt_params_verity params = {};
152 uint32_t activate_flags = CRYPT_ACTIVATE_READONLY;
153 char *root_hash_bytes = NULL;
156 char *signature = NULL;
157 int signature_size = 0, r;
159 if ((r = crypt_init_data_device(&cd, hash_device, data_device)))
162 if (opt_ignore_corruption)
163 activate_flags |= CRYPT_ACTIVATE_IGNORE_CORRUPTION;
164 if (opt_restart_on_corruption)
165 activate_flags |= CRYPT_ACTIVATE_RESTART_ON_CORRUPTION;
166 if (opt_panic_on_corruption)
167 activate_flags |= CRYPT_ACTIVATE_PANIC_ON_CORRUPTION;
168 if (opt_ignore_zero_blocks)
169 activate_flags |= CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS;
170 if (opt_check_at_most_once)
171 activate_flags |= CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE;
173 if (opt_use_superblock) {
174 params.flags = flags;
175 params.hash_area_offset = hash_offset;
176 params.fec_area_offset = fec_offset;
177 params.fec_device = opt_fec_device;
178 params.fec_roots = opt_fec_roots;
179 r = crypt_load(cd, CRYPT_VERITY, ¶ms);
181 r = _prepare_format(¶ms, data_device, flags | CRYPT_VERITY_NO_HEADER);
184 r = crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms);
189 hash_size = crypt_get_volume_key_size(cd);
190 if (crypt_hex_to_bytes(root_hash, &root_hash_bytes, 0) != hash_size) {
191 log_err(_("Invalid root hash string specified."));
196 if (opt_root_hash_signature) {
197 // FIXME: check max file size
198 if (stat(opt_root_hash_signature, &st) || !S_ISREG(st.st_mode) || !st.st_size) {
199 log_err(_("Invalid signature file %s."), opt_root_hash_signature);
203 signature_size = st.st_size;
204 r = tools_read_mk(opt_root_hash_signature, &signature, signature_size);
206 log_err(_("Cannot read signature file %s."), opt_root_hash_signature);
210 r = crypt_activate_by_signed_key(cd, dm_device,
213 signature, signature_size,
216 crypt_safe_free(signature);
218 free(root_hash_bytes);
219 free(CONST_CAST(char*)params.salt);
223 static int action_open(int arg)
225 return _activate(action_argv[1],
229 opt_root_hash_signature ? CRYPT_VERITY_ROOT_HASH_SIGNATURE : 0);
232 static int action_verify(int arg)
234 return _activate(NULL,
238 CRYPT_VERITY_CHECK_HASH);
241 static int action_close(int arg)
243 struct crypt_device *cd = NULL;
246 r = crypt_init_by_name(&cd, action_argv[0]);
248 r = crypt_deactivate(cd, action_argv[0]);
254 static int action_status(int arg)
256 crypt_status_info ci;
257 struct crypt_active_device cad;
258 struct crypt_params_verity vp = {};
259 struct crypt_device *cd = NULL;
261 char *backing_file, *root_hash;
262 size_t root_hash_size;
263 unsigned i, path = 0;
266 /* perhaps a path, not a dm device name */
267 if (strchr(action_argv[0], '/') && !stat(action_argv[0], &st))
270 ci = crypt_status(NULL, action_argv[0]);
277 log_std("%s is inactive.\n", action_argv[0]);
279 log_std("%s/%s is inactive.\n", crypt_get_dir(), action_argv[0]);
285 log_std("%s is active%s.\n", action_argv[0],
286 ci == CRYPT_BUSY ? " and is in use" : "");
288 log_std("%s/%s is active%s.\n", crypt_get_dir(), action_argv[0],
289 ci == CRYPT_BUSY ? " and is in use" : "");
291 r = crypt_init_by_name_and_header(&cd, action_argv[0], NULL);
295 log_std(" type: %s\n", crypt_get_type(cd) ?: "n/a");
297 r = crypt_get_active_device(cd, action_argv[0], &cad);
301 /* Print only VERITY type devices */
302 r = crypt_get_verity_info(cd, &vp);
306 log_std(" status: %s%s\n",
307 cad.flags & CRYPT_ACTIVATE_CORRUPTED ? "corrupted" : "verified",
308 vp.flags & CRYPT_VERITY_ROOT_HASH_SIGNATURE ? " (with signature)" : "");
310 log_std(" hash type: %u\n", vp.hash_type);
311 log_std(" data block: %u\n", vp.data_block_size);
312 log_std(" hash block: %u\n", vp.hash_block_size);
313 log_std(" hash name: %s\n", vp.hash_name);
316 for(i = 0; i < vp.salt_size; i++)
317 log_std("%02hhx", (const char)vp.salt[i]);
322 log_std(" data device: %s\n", vp.data_device);
323 if ((backing_file = crypt_loop_backing_file(vp.data_device))) {
324 log_std(" data loop: %s\n", backing_file);
327 log_std(" size: %" PRIu64 " sectors\n", cad.size);
328 log_std(" mode: %s\n", cad.flags & CRYPT_ACTIVATE_READONLY ?
329 "readonly" : "read/write");
331 log_std(" hash device: %s\n", vp.hash_device);
332 if ((backing_file = crypt_loop_backing_file(vp.hash_device))) {
333 log_std(" hash loop: %s\n", backing_file);
336 log_std(" hash offset: %" PRIu64 " sectors\n",
337 vp.hash_area_offset * vp.hash_block_size / 512);
340 log_std(" FEC device: %s\n", vp.fec_device);
341 if ((backing_file = crypt_loop_backing_file(opt_fec_device))) {
342 log_std(" FEC loop: %s\n", backing_file);
345 log_std(" FEC offset: %" PRIu64 " sectors\n",
346 vp.fec_area_offset * vp.hash_block_size / 512);
347 log_std(" FEC roots: %u\n", vp.fec_roots);
350 root_hash_size = crypt_get_volume_key_size(cd);
351 if (root_hash_size > 0 && (root_hash = malloc(root_hash_size))) {
352 r = crypt_volume_key_get(cd, CRYPT_ANY_SLOT, root_hash, &root_hash_size, NULL, 0);
354 log_std(" root hash: ");
355 for (i = 0; i < root_hash_size; i++)
356 log_std("%02hhx", (const char)root_hash[i]);
362 if (cad.flags & (CRYPT_ACTIVATE_IGNORE_CORRUPTION|
363 CRYPT_ACTIVATE_RESTART_ON_CORRUPTION|
364 CRYPT_ACTIVATE_PANIC_ON_CORRUPTION|
365 CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS|
366 CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE))
367 log_std(" flags: %s%s%s%s%s\n",
368 (cad.flags & CRYPT_ACTIVATE_IGNORE_CORRUPTION) ? "ignore_corruption " : "",
369 (cad.flags & CRYPT_ACTIVATE_RESTART_ON_CORRUPTION) ? "restart_on_corruption " : "",
370 (cad.flags & CRYPT_ACTIVATE_PANIC_ON_CORRUPTION) ? "panic_on_corruption " : "",
371 (cad.flags & CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS) ? "ignore_zero_blocks " : "",
372 (cad.flags & CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE) ? "check_at_most_once" : "");
381 static int action_dump(int arg)
383 struct crypt_device *cd = NULL;
384 struct crypt_params_verity params = {};
387 if ((r = crypt_init(&cd, action_argv[0])))
390 params.hash_area_offset = hash_offset;
391 params.fec_area_offset = fec_offset;
392 r = crypt_load(cd, CRYPT_VERITY, ¶ms);
399 static struct action_type {
402 int required_action_argc;
403 const char *arg_desc;
406 { "format", action_format, 2, N_("<data_device> <hash_device>"),N_("format device") },
407 { "verify", action_verify, 3, N_("<data_device> <hash_device> <root_hash>"),N_("verify device") },
408 { "open", action_open, 4, N_("<data_device> <name> <hash_device> <root_hash>"),N_("open device as <name>") },
409 { "close", action_close, 1, N_("<name>"),N_("close device (remove mapping)") },
410 { "status", action_status, 1, N_("<name>"),N_("show active device status") },
411 { "dump", action_dump, 1, N_("<hash_device>"),N_("show on-disk information") },
412 { NULL, NULL, 0, NULL, NULL }
415 static void help(poptContext popt_context,
416 enum poptCallbackReason reason __attribute__((unused)),
417 struct poptOption *key,
418 const char *arg __attribute__((unused)),
419 void *data __attribute__((unused)))
421 struct action_type *action;
423 if (key->shortName == '?') {
424 log_std("%s %s\n", PACKAGE_VERITY, PACKAGE_VERSION);
425 poptPrintHelp(popt_context, stdout, 0);
427 "<action> is one of:\n"));
428 for(action = action_types; action->type; action++)
429 log_std("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
431 "<name> is the device to create under %s\n"
432 "<data_device> is the data device\n"
433 "<hash_device> is the device containing verification data\n"
434 "<root_hash> hash of the root node on <hash_device>\n"),
437 log_std(_("\nDefault compiled-in dm-verity parameters:\n"
438 "\tHash: %s, Data block (bytes): %u, "
439 "Hash block (bytes): %u, Salt size: %u, Hash format: %u\n"),
440 DEFAULT_VERITY_HASH, DEFAULT_VERITY_DATA_BLOCK,
441 DEFAULT_VERITY_HASH_BLOCK, DEFAULT_VERITY_SALT_SIZE,
444 poptFreeContext(popt_context);
446 } else if (key->shortName == 'V') {
447 log_std("%s %s\n", PACKAGE_VERITY, PACKAGE_VERSION);
449 poptFreeContext(popt_context);
452 usage(popt_context, EXIT_SUCCESS, NULL, NULL);
455 static int run_action(struct action_type *action)
459 log_dbg("Running command %s.", action->type);
461 r = action->handler(0);
464 return translate_errno(r);
467 int main(int argc, const char **argv)
469 static const char *null_action_argv[] = {NULL};
470 static struct poptOption popt_help_options[] = {
471 { NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL },
472 { "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help message"), NULL },
473 { "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief usage"), NULL },
474 { "version",'V', POPT_ARG_NONE, NULL, 0, N_("Print package version"), NULL },
477 static struct poptOption popt_options[] = {
478 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
479 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 0, N_("Shows more detailed error messages"), NULL },
480 { "debug", '\0', POPT_ARG_NONE, &opt_debug, 0, N_("Show debug messages"), NULL },
481 { "no-superblock", 0, POPT_ARG_VAL, &opt_use_superblock, 0, N_("Do not use verity superblock"), NULL },
482 { "format", 0, POPT_ARG_INT, &opt_hash_type, 0, N_("Format type (1 - normal, 0 - original Chrome OS)"), N_("number") },
483 { "data-block-size", 0, POPT_ARG_INT, &opt_data_block_size, 0, N_("Block size on the data device"), N_("bytes") },
484 { "hash-block-size", 0, POPT_ARG_INT, &opt_hash_block_size, 0, N_("Block size on the hash device"), N_("bytes") },
485 { "fec-roots", 0, POPT_ARG_INT, &opt_fec_roots, 0, N_("FEC parity bytes"), N_("bytes") },
486 { "data-blocks", 0, POPT_ARG_STRING, NULL, 1, N_("The number of blocks in the data file"), N_("blocks") },
487 { "fec-device", 0, POPT_ARG_STRING, &opt_fec_device, 0, N_("Path to device with error correction data"), N_("path") },
488 { "hash-offset", 0, POPT_ARG_STRING, NULL, 2, N_("Starting offset on the hash device"), N_("bytes") },
489 { "fec-offset", 0, POPT_ARG_STRING, NULL, 3, N_("Starting offset on the FEC device"), N_("bytes") },
490 { "hash", 'h', POPT_ARG_STRING, &opt_hash_algorithm, 0, N_("Hash algorithm"), N_("string") },
491 { "salt", 's', POPT_ARG_STRING, &opt_salt, 0, N_("Salt"), N_("hex string") },
492 { "uuid", '\0', POPT_ARG_STRING, &opt_uuid, 0, N_("UUID for device to use"), NULL },
493 { "root-hash-signature",'\0', POPT_ARG_STRING, &opt_root_hash_signature, 0, N_("Path to root hash signature file"), NULL },
494 { "restart-on-corruption", 0,POPT_ARG_NONE,&opt_restart_on_corruption, 0, N_("Restart kernel if corruption is detected"), NULL },
495 { "panic-on-corruption", 0,POPT_ARG_NONE, &opt_panic_on_corruption, 0, N_("Panic kernel if corruption is detected"), NULL },
496 { "ignore-corruption", 0, POPT_ARG_NONE, &opt_ignore_corruption, 0, N_("Ignore corruption, log it only"), NULL },
497 { "ignore-zero-blocks", 0, POPT_ARG_NONE, &opt_ignore_zero_blocks, 0, N_("Do not verify zeroed blocks"), NULL },
498 { "check-at-most-once", 0, POPT_ARG_NONE, &opt_check_at_most_once, 0, N_("Verify data block only the first time it is read"), NULL },
502 poptContext popt_context;
503 struct action_type *action;
507 crypt_set_log_callback(NULL, tool_log, NULL);
509 setlocale(LC_ALL, "");
510 bindtextdomain(PACKAGE, LOCALEDIR);
513 popt_context = poptGetContext("verity", argc, argv, popt_options, 0);
514 poptSetOtherOptionHelp(popt_context,
515 _("[OPTION...] <action> <action-specific>"));
517 while((r = poptGetNextOpt(popt_context)) > 0) {
518 unsigned long long ull_value;
519 char *endp, *str = poptGetOptArg(popt_context);
522 ull_value = strtoull(str, &endp, 10);
523 if (*endp || !*str || !isdigit(*str) ||
524 (errno == ERANGE && ull_value == ULLONG_MAX) ||
525 (errno != 0 && ull_value == 0))
526 r = POPT_ERROR_BADNUMBER;
532 data_blocks = ull_value;
535 hash_offset = ull_value;
538 fec_offset = ull_value;
547 usage(popt_context, EXIT_FAILURE, poptStrerror(r),
548 poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
550 if (!(aname = poptGetArg(popt_context)))
551 usage(popt_context, EXIT_FAILURE, _("Argument <action> missing."),
552 poptGetInvocationName(popt_context));
555 action_argv = poptGetArgs(popt_context);
556 /* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
558 action_argv = null_action_argv;
560 /* Count args, somewhat unnice, change? */
561 while(action_argv[action_argc] != NULL)
565 if (!strcmp(aname, "create") && action_argc > 1) {
566 /* create command had historically switched arguments */
567 if (action_argv[0] && action_argv[1]) {
568 const char *tmp = action_argv[0];
569 action_argv[0] = action_argv[1];
570 action_argv[1] = tmp;
573 } else if (!strcmp(aname, "remove")) {
577 for (action = action_types; action->type; action++)
578 if (strcmp(action->type, aname) == 0)
582 usage(popt_context, EXIT_FAILURE, _("Unknown action."),
583 poptGetInvocationName(popt_context));
585 if (action_argc < action->required_action_argc) {
587 snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
588 usage(popt_context, EXIT_FAILURE, buf,
589 poptGetInvocationName(popt_context));
592 if (opt_data_block_size < 0 || opt_hash_block_size < 0 || opt_hash_type < 0) {
593 usage(popt_context, EXIT_FAILURE,
594 _("Negative number for option not permitted."),
595 poptGetInvocationName(popt_context));
598 if ((opt_ignore_corruption || opt_restart_on_corruption || opt_ignore_zero_blocks) && strcmp(aname, "open"))
599 usage(popt_context, EXIT_FAILURE,
600 _("Option --ignore-corruption, --restart-on-corruption or --ignore-zero-blocks is allowed only for open operation."),
601 poptGetInvocationName(popt_context));
603 if (opt_root_hash_signature && strcmp(aname, "open"))
604 usage(popt_context, EXIT_FAILURE,
605 _("Option --root-hash-signature can be used only for open operation."),
606 poptGetInvocationName(popt_context));
608 if (opt_ignore_corruption && opt_restart_on_corruption)
609 usage(popt_context, EXIT_FAILURE,
610 _("Option --ignore-corruption and --restart-on-corruption cannot be used together."),
611 poptGetInvocationName(popt_context));
613 if (opt_panic_on_corruption && opt_restart_on_corruption)
614 usage(popt_context, EXIT_FAILURE,
615 _("Option --panic-on-corruption and --restart-on-corruption cannot be used together."),
616 poptGetInvocationName(popt_context));
620 crypt_set_debug_level(-1);
621 dbg_version_and_cmd(argc, argv);
624 r = run_action(action);
626 poptFreeContext(popt_context);