2 * veritysetup - setup cryptographic volumes for dm-verity
4 * Copyright (C) 2012-2020 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2012-2020 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 int use_superblock = 1;
28 static const char *fec_device = NULL;
29 static int fec_roots = DEFAULT_VERITY_FEC_ROOTS;
30 static const char *hash_algorithm = NULL;
31 static int hash_type = 1;
32 static int data_block_size = DEFAULT_VERITY_DATA_BLOCK;
33 static int hash_block_size = DEFAULT_VERITY_HASH_BLOCK;
34 static uint64_t data_blocks = 0;
35 static const char *salt_string = NULL;
36 static uint64_t hash_offset = 0;
37 static uint64_t fec_offset = 0;
38 static const char *opt_uuid = NULL;
39 static int opt_restart_on_corruption = 0;
40 static int opt_ignore_corruption = 0;
41 static int opt_ignore_zero_blocks = 0;
42 static int opt_check_at_most_once = 0;
43 static const char *opt_root_hash_signature = NULL;
45 static const char **action_argv;
46 static int action_argc;
48 static int _prepare_format(struct crypt_params_verity *params,
49 const char *data_device,
55 params->hash_name = hash_algorithm ?: DEFAULT_VERITY_HASH;
56 params->data_device = data_device;
57 params->fec_device = fec_device;
58 params->fec_roots = fec_roots;
60 if (salt_string && !strcmp(salt_string, "-")) {
61 params->salt_size = 0;
63 } else if (salt_string) {
64 len = crypt_hex_to_bytes(salt_string, &salt, 0);
66 log_err(_("Invalid salt string specified."));
69 params->salt_size = len;
72 params->salt_size = DEFAULT_VERITY_SALT_SIZE;
76 params->data_block_size = data_block_size;
77 params->hash_block_size = hash_block_size;
78 params->data_size = data_blocks;
79 params->hash_area_offset = hash_offset;
80 params->fec_area_offset = fec_offset;
81 params->hash_type = hash_type;
82 params->flags = flags;
87 static int action_format(int arg)
89 struct crypt_device *cd = NULL;
90 struct crypt_params_verity params = {};
91 uint32_t flags = CRYPT_VERITY_CREATE_HASH;
94 /* Try to create hash image if doesn't exist */
95 r = open(action_argv[1], O_WRONLY | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
96 if (r < 0 && errno != EEXIST) {
97 log_err(_("Cannot create hash image %s for writing."), action_argv[1]);
100 log_dbg("Created hash image %s.", action_argv[1]);
103 /* Try to create FEC image if doesn't exist */
105 r = open(fec_device, O_WRONLY | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
106 if (r < 0 && errno != EEXIST) {
107 log_err(_("Cannot create FEC image %s for writing."), fec_device);
110 log_dbg("Created FEC image %s.", fec_device);
115 if ((r = crypt_init(&cd, action_argv[1])))
119 flags |= CRYPT_VERITY_NO_HEADER;
121 r = _prepare_format(¶ms, action_argv[0], flags);
125 r = crypt_format(cd, CRYPT_VERITY, NULL, NULL, opt_uuid, NULL, 0, ¶ms);
130 free(CONST_CAST(char*)params.salt);
134 static int _activate(const char *dm_device,
135 const char *data_device,
136 const char *hash_device,
137 const char *root_hash,
140 struct crypt_device *cd = NULL;
141 struct crypt_params_verity params = {};
142 uint32_t activate_flags = CRYPT_ACTIVATE_READONLY;
143 char *root_hash_bytes = NULL;
146 char *signature = NULL;
147 int signature_size = 0, r;
149 if ((r = crypt_init_data_device(&cd, hash_device, data_device)))
152 if (opt_ignore_corruption)
153 activate_flags |= CRYPT_ACTIVATE_IGNORE_CORRUPTION;
154 if (opt_restart_on_corruption)
155 activate_flags |= CRYPT_ACTIVATE_RESTART_ON_CORRUPTION;
156 if (opt_ignore_zero_blocks)
157 activate_flags |= CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS;
158 if (opt_check_at_most_once)
159 activate_flags |= CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE;
161 if (use_superblock) {
162 params.flags = flags;
163 params.hash_area_offset = hash_offset;
164 params.fec_area_offset = fec_offset;
165 params.fec_device = fec_device;
166 params.fec_roots = fec_roots;
167 r = crypt_load(cd, CRYPT_VERITY, ¶ms);
169 r = _prepare_format(¶ms, data_device, flags | CRYPT_VERITY_NO_HEADER);
172 r = crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms);
177 hash_size = crypt_get_volume_key_size(cd);
178 if (crypt_hex_to_bytes(root_hash, &root_hash_bytes, 0) != hash_size) {
179 log_err(_("Invalid root hash string specified."));
184 if (opt_root_hash_signature) {
185 // FIXME: check max file size
186 if (stat(opt_root_hash_signature, &st) || !S_ISREG(st.st_mode) || !st.st_size) {
187 log_err(_("Invalid signature file %s."), opt_root_hash_signature);
191 signature_size = st.st_size;
192 r = tools_read_mk(opt_root_hash_signature, &signature, signature_size);
194 log_err(_("Cannot read signature file %s."), opt_root_hash_signature);
198 r = crypt_activate_by_signed_key(cd, dm_device,
201 signature, signature_size,
204 crypt_safe_free(signature);
206 free(root_hash_bytes);
207 free(CONST_CAST(char*)params.salt);
211 static int action_open(int arg)
213 return _activate(action_argv[1],
217 opt_root_hash_signature ? CRYPT_VERITY_ROOT_HASH_SIGNATURE : 0);
220 static int action_verify(int arg)
222 return _activate(NULL,
226 CRYPT_VERITY_CHECK_HASH);
229 static int action_close(int arg)
231 struct crypt_device *cd = NULL;
234 r = crypt_init_by_name(&cd, action_argv[0]);
236 r = crypt_deactivate(cd, action_argv[0]);
242 static int action_status(int arg)
244 crypt_status_info ci;
245 struct crypt_active_device cad;
246 struct crypt_params_verity vp = {};
247 struct crypt_device *cd = NULL;
249 char *backing_file, *root_hash;
250 size_t root_hash_size;
251 unsigned i, path = 0;
254 /* perhaps a path, not a dm device name */
255 if (strchr(action_argv[0], '/') && !stat(action_argv[0], &st))
258 ci = crypt_status(NULL, action_argv[0]);
265 log_std("%s is inactive.\n", action_argv[0]);
267 log_std("%s/%s is inactive.\n", crypt_get_dir(), action_argv[0]);
273 log_std("%s is active%s.\n", action_argv[0],
274 ci == CRYPT_BUSY ? " and is in use" : "");
276 log_std("%s/%s is active%s.\n", crypt_get_dir(), action_argv[0],
277 ci == CRYPT_BUSY ? " and is in use" : "");
279 r = crypt_init_by_name_and_header(&cd, action_argv[0], NULL);
283 log_std(" type: %s\n", crypt_get_type(cd) ?: "n/a");
285 r = crypt_get_active_device(cd, action_argv[0], &cad);
289 /* Print only VERITY type devices */
290 r = crypt_get_verity_info(cd, &vp);
294 log_std(" status: %s%s\n",
295 cad.flags & CRYPT_ACTIVATE_CORRUPTED ? "corrupted" : "verified",
296 vp.flags & CRYPT_VERITY_ROOT_HASH_SIGNATURE ? " (with signature)" : "");
298 log_std(" hash type: %u\n", vp.hash_type);
299 log_std(" data block: %u\n", vp.data_block_size);
300 log_std(" hash block: %u\n", vp.hash_block_size);
301 log_std(" hash name: %s\n", vp.hash_name);
304 for(i = 0; i < vp.salt_size; i++)
305 log_std("%02hhx", (const char)vp.salt[i]);
310 log_std(" data device: %s\n", vp.data_device);
311 if ((backing_file = crypt_loop_backing_file(vp.data_device))) {
312 log_std(" data loop: %s\n", backing_file);
315 log_std(" size: %" PRIu64 " sectors\n", cad.size);
316 log_std(" mode: %s\n", cad.flags & CRYPT_ACTIVATE_READONLY ?
317 "readonly" : "read/write");
319 log_std(" hash device: %s\n", vp.hash_device);
320 if ((backing_file = crypt_loop_backing_file(vp.hash_device))) {
321 log_std(" hash loop: %s\n", backing_file);
324 log_std(" hash offset: %" PRIu64 " sectors\n",
325 vp.hash_area_offset * vp.hash_block_size / 512);
328 log_std(" FEC device: %s\n", vp.fec_device);
329 if ((backing_file = crypt_loop_backing_file(vp.fec_device))) {
330 log_std(" FEC loop: %s\n", backing_file);
333 log_std(" FEC offset: %" PRIu64 " sectors\n",
334 vp.fec_area_offset * vp.hash_block_size / 512);
335 log_std(" FEC roots: %u\n", vp.fec_roots);
338 root_hash_size = crypt_get_volume_key_size(cd);
339 if (root_hash_size > 0 && (root_hash = malloc(root_hash_size))) {
340 r = crypt_volume_key_get(cd, CRYPT_ANY_SLOT, root_hash, &root_hash_size, NULL, 0);
342 log_std(" root hash: ");
343 for (i = 0; i < root_hash_size; i++)
344 log_std("%02hhx", (const char)root_hash[i]);
350 if (cad.flags & (CRYPT_ACTIVATE_IGNORE_CORRUPTION|
351 CRYPT_ACTIVATE_RESTART_ON_CORRUPTION|
352 CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS|
353 CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE))
354 log_std(" flags: %s%s%s%s\n",
355 (cad.flags & CRYPT_ACTIVATE_IGNORE_CORRUPTION) ? "ignore_corruption " : "",
356 (cad.flags & CRYPT_ACTIVATE_RESTART_ON_CORRUPTION) ? "restart_on_corruption " : "",
357 (cad.flags & CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS) ? "ignore_zero_blocks " : "",
358 (cad.flags & CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE) ? "check_at_most_once" : "");
367 static int action_dump(int arg)
369 struct crypt_device *cd = NULL;
370 struct crypt_params_verity params = {};
373 if ((r = crypt_init(&cd, action_argv[0])))
376 params.hash_area_offset = hash_offset;
377 params.fec_area_offset = fec_offset;
378 r = crypt_load(cd, CRYPT_VERITY, ¶ms);
385 static struct action_type {
388 int required_action_argc;
389 const char *arg_desc;
392 { "format", action_format, 2, N_("<data_device> <hash_device>"),N_("format device") },
393 { "verify", action_verify, 3, N_("<data_device> <hash_device> <root_hash>"),N_("verify device") },
394 { "open", action_open, 4, N_("<data_device> <name> <hash_device> <root_hash>"),N_("open device as <name>") },
395 { "close", action_close, 1, N_("<name>"),N_("close device (remove mapping)") },
396 { "status", action_status, 1, N_("<name>"),N_("show active device status") },
397 { "dump", action_dump, 1, N_("<hash_device>"),N_("show on-disk information") },
398 { NULL, NULL, 0, NULL, NULL }
401 static void help(poptContext popt_context,
402 enum poptCallbackReason reason __attribute__((unused)),
403 struct poptOption *key,
404 const char *arg __attribute__((unused)),
405 void *data __attribute__((unused)))
407 struct action_type *action;
409 if (key->shortName == '?') {
410 log_std("%s %s\n", PACKAGE_VERITY, PACKAGE_VERSION);
411 poptPrintHelp(popt_context, stdout, 0);
413 "<action> is one of:\n"));
414 for(action = action_types; action->type; action++)
415 log_std("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
417 "<name> is the device to create under %s\n"
418 "<data_device> is the data device\n"
419 "<hash_device> is the device containing verification data\n"
420 "<root_hash> hash of the root node on <hash_device>\n"),
423 log_std(_("\nDefault compiled-in dm-verity parameters:\n"
424 "\tHash: %s, Data block (bytes): %u, "
425 "Hash block (bytes): %u, Salt size: %u, Hash format: %u\n"),
426 DEFAULT_VERITY_HASH, DEFAULT_VERITY_DATA_BLOCK,
427 DEFAULT_VERITY_HASH_BLOCK, DEFAULT_VERITY_SALT_SIZE,
429 poptFreeContext(popt_context);
431 } else if (key->shortName == 'V') {
432 log_std("%s %s\n", PACKAGE_VERITY, PACKAGE_VERSION);
433 poptFreeContext(popt_context);
436 usage(popt_context, EXIT_SUCCESS, NULL, NULL);
439 static int run_action(struct action_type *action)
443 log_dbg("Running command %s.", action->type);
445 r = action->handler(0);
448 return translate_errno(r);
451 int main(int argc, const char **argv)
453 static char *popt_tmp;
454 static const char *null_action_argv[] = {NULL};
455 static struct poptOption popt_help_options[] = {
456 { NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL },
457 { "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help message"), NULL },
458 { "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief usage"), NULL },
459 { "version",'V', POPT_ARG_NONE, NULL, 0, N_("Print package version"), NULL },
462 static struct poptOption popt_options[] = {
463 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
464 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 0, N_("Shows more detailed error messages"), NULL },
465 { "debug", '\0', POPT_ARG_NONE, &opt_debug, 0, N_("Show debug messages"), NULL },
466 { "no-superblock", 0, POPT_ARG_VAL, &use_superblock, 0, N_("Do not use verity superblock"), NULL },
467 { "format", 0, POPT_ARG_INT, &hash_type, 0, N_("Format type (1 - normal, 0 - original Chrome OS)"), N_("number") },
468 { "data-block-size", 0, POPT_ARG_INT, &data_block_size, 0, N_("Block size on the data device"), N_("bytes") },
469 { "hash-block-size", 0, POPT_ARG_INT, &hash_block_size, 0, N_("Block size on the hash device"), N_("bytes") },
470 { "fec-roots", 0, POPT_ARG_INT, &fec_roots, 0, N_("FEC parity bytes"), N_("bytes") },
471 { "data-blocks", 0, POPT_ARG_STRING, &popt_tmp, 1, N_("The number of blocks in the data file"), N_("blocks") },
472 { "fec-device", 0, POPT_ARG_STRING, &fec_device, 0, N_("Path to device with error correction data"), N_("path") },
473 { "hash-offset", 0, POPT_ARG_STRING, &popt_tmp, 2, N_("Starting offset on the hash device"), N_("bytes") },
474 { "fec-offset", 0, POPT_ARG_STRING, &popt_tmp, 3, N_("Starting offset on the FEC device"), N_("bytes") },
475 { "hash", 'h', POPT_ARG_STRING, &hash_algorithm, 0, N_("Hash algorithm"), N_("string") },
476 { "salt", 's', POPT_ARG_STRING, &salt_string, 0, N_("Salt"), N_("hex string") },
477 { "uuid", '\0', POPT_ARG_STRING, &opt_uuid, 0, N_("UUID for device to use"), NULL },
478 { "root-hash-signature",'\0', POPT_ARG_STRING, &opt_root_hash_signature, 0, N_("Path to root hash signature file"), NULL },
479 { "restart-on-corruption", 0,POPT_ARG_NONE,&opt_restart_on_corruption, 0, N_("Restart kernel if corruption is detected"), NULL },
480 { "ignore-corruption", 0, POPT_ARG_NONE, &opt_ignore_corruption, 0, N_("Ignore corruption, log it only"), NULL },
481 { "ignore-zero-blocks", 0, POPT_ARG_NONE, &opt_ignore_zero_blocks, 0, N_("Do not verify zeroed blocks"), NULL },
482 { "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 },
486 poptContext popt_context;
487 struct action_type *action;
491 crypt_set_log_callback(NULL, tool_log, NULL);
493 setlocale(LC_ALL, "");
494 bindtextdomain(PACKAGE, LOCALEDIR);
497 popt_context = poptGetContext("verity", argc, argv, popt_options, 0);
498 poptSetOtherOptionHelp(popt_context,
499 _("[OPTION...] <action> <action-specific>"));
501 while((r = poptGetNextOpt(popt_context)) > 0) {
502 unsigned long long ull_value;
506 ull_value = strtoull(popt_tmp, &endp, 10);
507 if (*endp || !*popt_tmp || !isdigit(*popt_tmp) ||
508 (errno == ERANGE && ull_value == ULLONG_MAX) ||
509 (errno != 0 && ull_value == 0))
510 r = POPT_ERROR_BADNUMBER;
514 data_blocks = ull_value;
517 hash_offset = ull_value;
520 fec_offset = ull_value;
529 usage(popt_context, EXIT_FAILURE, poptStrerror(r),
530 poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
532 if (!(aname = poptGetArg(popt_context)))
533 usage(popt_context, EXIT_FAILURE, _("Argument <action> missing."),
534 poptGetInvocationName(popt_context));
537 action_argv = poptGetArgs(popt_context);
538 /* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
540 action_argv = null_action_argv;
542 /* Count args, somewhat unnice, change? */
543 while(action_argv[action_argc] != NULL)
547 if (!strcmp(aname, "create") && action_argc > 1) {
548 /* create command had historically switched arguments */
549 if (action_argv[0] && action_argv[1]) {
550 const char *tmp = action_argv[0];
551 action_argv[0] = action_argv[1];
552 action_argv[1] = tmp;
555 } else if (!strcmp(aname, "remove")) {
559 for (action = action_types; action->type; action++)
560 if (strcmp(action->type, aname) == 0)
564 usage(popt_context, EXIT_FAILURE, _("Unknown action."),
565 poptGetInvocationName(popt_context));
567 if (action_argc < action->required_action_argc) {
569 snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
570 usage(popt_context, EXIT_FAILURE, buf,
571 poptGetInvocationName(popt_context));
574 if (data_block_size < 0 || hash_block_size < 0 || hash_type < 0) {
575 usage(popt_context, EXIT_FAILURE,
576 _("Negative number for option not permitted."),
577 poptGetInvocationName(popt_context));
580 if ((opt_ignore_corruption || opt_restart_on_corruption || opt_ignore_zero_blocks) && strcmp(aname, "open"))
581 usage(popt_context, EXIT_FAILURE,
582 _("Option --ignore-corruption, --restart-on-corruption or --ignore-zero-blocks is allowed only for open operation."),
583 poptGetInvocationName(popt_context));
585 if (opt_root_hash_signature && strcmp(aname, "open"))
586 usage(popt_context, EXIT_FAILURE,
587 _("Option --root-hash-signature can be used only for open operation."),
588 poptGetInvocationName(popt_context));
590 if (opt_ignore_corruption && opt_restart_on_corruption)
591 usage(popt_context, EXIT_FAILURE,
592 _("Option --ignore-corruption and --restart-on-corruption cannot be used together."),
593 poptGetInvocationName(popt_context));
597 crypt_set_debug_level(-1);
598 dbg_version_and_cmd(argc, argv);
601 r = run_action(action);
602 poptFreeContext(popt_context);