|
Packit |
94f725 |
/*
|
|
Packit |
94f725 |
* veritysetup - setup cryptographic volumes for dm-verity
|
|
Packit |
94f725 |
*
|
|
Packit |
94f725 |
* Copyright (C) 2012-2020 Red Hat, Inc. All rights reserved.
|
|
Packit |
94f725 |
* Copyright (C) 2012-2020 Milan Broz
|
|
Packit |
94f725 |
*
|
|
Packit |
94f725 |
* This program is free software; you can redistribute it and/or
|
|
Packit |
94f725 |
* modify it under the terms of the GNU General Public License
|
|
Packit |
94f725 |
* as published by the Free Software Foundation; either version 2
|
|
Packit |
94f725 |
* of the License, or (at your option) any later version.
|
|
Packit |
94f725 |
*
|
|
Packit |
94f725 |
* This program is distributed in the hope that it will be useful,
|
|
Packit |
94f725 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
94f725 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
94f725 |
* GNU General Public License for more details.
|
|
Packit |
94f725 |
*
|
|
Packit |
94f725 |
* You should have received a copy of the GNU General Public License
|
|
Packit |
94f725 |
* along with this program; if not, write to the Free Software
|
|
Packit |
94f725 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
Packit |
94f725 |
*/
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
#include "cryptsetup.h"
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
#define PACKAGE_VERITY "veritysetup"
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
static int use_superblock = 1;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
static const char *fec_device = NULL;
|
|
Packit |
94f725 |
static int fec_roots = DEFAULT_VERITY_FEC_ROOTS;
|
|
Packit |
94f725 |
static const char *hash_algorithm = NULL;
|
|
Packit |
94f725 |
static int hash_type = 1;
|
|
Packit |
94f725 |
static int data_block_size = DEFAULT_VERITY_DATA_BLOCK;
|
|
Packit |
94f725 |
static int hash_block_size = DEFAULT_VERITY_HASH_BLOCK;
|
|
Packit |
94f725 |
static uint64_t data_blocks = 0;
|
|
Packit |
94f725 |
static const char *salt_string = NULL;
|
|
Packit |
94f725 |
static uint64_t hash_offset = 0;
|
|
Packit |
94f725 |
static uint64_t fec_offset = 0;
|
|
Packit |
94f725 |
static const char *opt_uuid = NULL;
|
|
Packit |
94f725 |
static int opt_restart_on_corruption = 0;
|
|
Packit |
94f725 |
static int opt_ignore_corruption = 0;
|
|
Packit |
94f725 |
static int opt_ignore_zero_blocks = 0;
|
|
Packit |
94f725 |
static int opt_check_at_most_once = 0;
|
|
Packit |
94f725 |
static const char *opt_root_hash_signature = NULL;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
static const char **action_argv;
|
|
Packit |
94f725 |
static int action_argc;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
static int _prepare_format(struct crypt_params_verity *params,
|
|
Packit |
94f725 |
const char *data_device,
|
|
Packit |
94f725 |
uint32_t flags)
|
|
Packit |
94f725 |
{
|
|
Packit |
94f725 |
char *salt = NULL;
|
|
Packit |
94f725 |
int len;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
params->hash_name = hash_algorithm ?: DEFAULT_VERITY_HASH;
|
|
Packit |
94f725 |
params->data_device = data_device;
|
|
Packit |
94f725 |
params->fec_device = fec_device;
|
|
Packit |
94f725 |
params->fec_roots = fec_roots;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (salt_string && !strcmp(salt_string, "-")) {
|
|
Packit |
94f725 |
params->salt_size = 0;
|
|
Packit |
94f725 |
params->salt = NULL;
|
|
Packit |
94f725 |
} else if (salt_string) {
|
|
Packit |
94f725 |
len = crypt_hex_to_bytes(salt_string, &salt, 0);
|
|
Packit |
94f725 |
if (len < 0) {
|
|
Packit |
94f725 |
log_err(_("Invalid salt string specified."));
|
|
Packit |
94f725 |
return -EINVAL;
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
params->salt_size = len;
|
|
Packit |
94f725 |
params->salt = salt;
|
|
Packit |
94f725 |
} else {
|
|
Packit |
94f725 |
params->salt_size = DEFAULT_VERITY_SALT_SIZE;
|
|
Packit |
94f725 |
params->salt = NULL;
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
params->data_block_size = data_block_size;
|
|
Packit |
94f725 |
params->hash_block_size = hash_block_size;
|
|
Packit |
94f725 |
params->data_size = data_blocks;
|
|
Packit |
94f725 |
params->hash_area_offset = hash_offset;
|
|
Packit |
94f725 |
params->fec_area_offset = fec_offset;
|
|
Packit |
94f725 |
params->hash_type = hash_type;
|
|
Packit |
94f725 |
params->flags = flags;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
return 0;
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
static int action_format(int arg)
|
|
Packit |
94f725 |
{
|
|
Packit |
94f725 |
struct crypt_device *cd = NULL;
|
|
Packit |
94f725 |
struct crypt_params_verity params = {};
|
|
Packit |
94f725 |
uint32_t flags = CRYPT_VERITY_CREATE_HASH;
|
|
Packit |
94f725 |
int r;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
/* Try to create hash image if doesn't exist */
|
|
Packit |
94f725 |
r = open(action_argv[1], O_WRONLY | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
|
|
Packit |
94f725 |
if (r < 0 && errno != EEXIST) {
|
|
Packit |
94f725 |
log_err(_("Cannot create hash image %s for writing."), action_argv[1]);
|
|
Packit |
94f725 |
return -EINVAL;
|
|
Packit |
94f725 |
} else if (r >= 0) {
|
|
Packit |
94f725 |
log_dbg("Created hash image %s.", action_argv[1]);
|
|
Packit |
94f725 |
close(r);
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
/* Try to create FEC image if doesn't exist */
|
|
Packit |
94f725 |
if (fec_device) {
|
|
Packit |
94f725 |
r = open(fec_device, O_WRONLY | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
|
|
Packit |
94f725 |
if (r < 0 && errno != EEXIST) {
|
|
Packit |
94f725 |
log_err(_("Cannot create FEC image %s for writing."), fec_device);
|
|
Packit |
94f725 |
return -EINVAL;
|
|
Packit |
94f725 |
} else if (r >= 0) {
|
|
Packit |
94f725 |
log_dbg("Created FEC image %s.", fec_device);
|
|
Packit |
94f725 |
close(r);
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if ((r = crypt_init(&cd, action_argv[1])))
|
|
Packit |
94f725 |
goto out;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (!use_superblock)
|
|
Packit |
94f725 |
flags |= CRYPT_VERITY_NO_HEADER;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
r = _prepare_format(¶ms, action_argv[0], flags);
|
|
Packit |
94f725 |
if (r < 0)
|
|
Packit |
94f725 |
goto out;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
r = crypt_format(cd, CRYPT_VERITY, NULL, NULL, opt_uuid, NULL, 0, ¶ms);
|
|
Packit |
94f725 |
if (!r)
|
|
Packit |
94f725 |
crypt_dump(cd);
|
|
Packit |
94f725 |
out:
|
|
Packit |
94f725 |
crypt_free(cd);
|
|
Packit |
94f725 |
free(CONST_CAST(char*)params.salt);
|
|
Packit |
94f725 |
return r;
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
static int _activate(const char *dm_device,
|
|
Packit |
94f725 |
const char *data_device,
|
|
Packit |
94f725 |
const char *hash_device,
|
|
Packit |
94f725 |
const char *root_hash,
|
|
Packit |
94f725 |
uint32_t flags)
|
|
Packit |
94f725 |
{
|
|
Packit |
94f725 |
struct crypt_device *cd = NULL;
|
|
Packit |
94f725 |
struct crypt_params_verity params = {};
|
|
Packit |
94f725 |
uint32_t activate_flags = CRYPT_ACTIVATE_READONLY;
|
|
Packit |
94f725 |
char *root_hash_bytes = NULL;
|
|
Packit |
94f725 |
ssize_t hash_size;
|
|
Packit |
94f725 |
struct stat st;
|
|
Packit |
94f725 |
char *signature = NULL;
|
|
Packit |
94f725 |
int signature_size = 0, r;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if ((r = crypt_init_data_device(&cd, hash_device, data_device)))
|
|
Packit |
94f725 |
goto out;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (opt_ignore_corruption)
|
|
Packit |
94f725 |
activate_flags |= CRYPT_ACTIVATE_IGNORE_CORRUPTION;
|
|
Packit |
94f725 |
if (opt_restart_on_corruption)
|
|
Packit |
94f725 |
activate_flags |= CRYPT_ACTIVATE_RESTART_ON_CORRUPTION;
|
|
Packit |
94f725 |
if (opt_ignore_zero_blocks)
|
|
Packit |
94f725 |
activate_flags |= CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS;
|
|
Packit |
94f725 |
if (opt_check_at_most_once)
|
|
Packit |
94f725 |
activate_flags |= CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (use_superblock) {
|
|
Packit |
94f725 |
params.flags = flags;
|
|
Packit |
94f725 |
params.hash_area_offset = hash_offset;
|
|
Packit |
94f725 |
params.fec_area_offset = fec_offset;
|
|
Packit |
94f725 |
params.fec_device = fec_device;
|
|
Packit |
94f725 |
params.fec_roots = fec_roots;
|
|
Packit |
94f725 |
r = crypt_load(cd, CRYPT_VERITY, ¶ms);
|
|
Packit |
94f725 |
} else {
|
|
Packit |
94f725 |
r = _prepare_format(¶ms, data_device, flags | CRYPT_VERITY_NO_HEADER);
|
|
Packit |
94f725 |
if (r < 0)
|
|
Packit |
94f725 |
goto out;
|
|
Packit |
94f725 |
r = crypt_format(cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0, ¶ms);
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
if (r < 0)
|
|
Packit |
94f725 |
goto out;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
hash_size = crypt_get_volume_key_size(cd);
|
|
Packit |
94f725 |
if (crypt_hex_to_bytes(root_hash, &root_hash_bytes, 0) != hash_size) {
|
|
Packit |
94f725 |
log_err(_("Invalid root hash string specified."));
|
|
Packit |
94f725 |
r = -EINVAL;
|
|
Packit |
94f725 |
goto out;
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (opt_root_hash_signature) {
|
|
Packit |
94f725 |
// FIXME: check max file size
|
|
Packit |
94f725 |
if (stat(opt_root_hash_signature, &st) || !S_ISREG(st.st_mode) || !st.st_size) {
|
|
Packit |
94f725 |
log_err(_("Invalid signature file %s."), opt_root_hash_signature);
|
|
Packit |
94f725 |
r = -EINVAL;
|
|
Packit |
94f725 |
goto out;
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
signature_size = st.st_size;
|
|
Packit |
94f725 |
r = tools_read_mk(opt_root_hash_signature, &signature, signature_size);
|
|
Packit |
94f725 |
if (r < 0) {
|
|
Packit |
94f725 |
log_err(_("Cannot read signature file %s."), opt_root_hash_signature);
|
|
Packit |
94f725 |
goto out;
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
r = crypt_activate_by_signed_key(cd, dm_device,
|
|
Packit |
94f725 |
root_hash_bytes,
|
|
Packit |
94f725 |
hash_size,
|
|
Packit |
94f725 |
signature, signature_size,
|
|
Packit |
94f725 |
activate_flags);
|
|
Packit |
94f725 |
out:
|
|
Packit |
94f725 |
crypt_safe_free(signature);
|
|
Packit |
94f725 |
crypt_free(cd);
|
|
Packit |
94f725 |
free(root_hash_bytes);
|
|
Packit |
94f725 |
free(CONST_CAST(char*)params.salt);
|
|
Packit |
94f725 |
return r;
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
static int action_open(int arg)
|
|
Packit |
94f725 |
{
|
|
Packit |
94f725 |
return _activate(action_argv[1],
|
|
Packit |
94f725 |
action_argv[0],
|
|
Packit |
94f725 |
action_argv[2],
|
|
Packit |
94f725 |
action_argv[3],
|
|
Packit |
94f725 |
opt_root_hash_signature ? CRYPT_VERITY_ROOT_HASH_SIGNATURE : 0);
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
static int action_verify(int arg)
|
|
Packit |
94f725 |
{
|
|
Packit |
94f725 |
return _activate(NULL,
|
|
Packit |
94f725 |
action_argv[0],
|
|
Packit |
94f725 |
action_argv[1],
|
|
Packit |
94f725 |
action_argv[2],
|
|
Packit |
94f725 |
CRYPT_VERITY_CHECK_HASH);
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
static int action_close(int arg)
|
|
Packit |
94f725 |
{
|
|
Packit |
94f725 |
struct crypt_device *cd = NULL;
|
|
Packit |
94f725 |
int r;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
r = crypt_init_by_name(&cd, action_argv[0]);
|
|
Packit |
94f725 |
if (r == 0)
|
|
Packit |
94f725 |
r = crypt_deactivate(cd, action_argv[0]);
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
crypt_free(cd);
|
|
Packit |
94f725 |
return r;
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
static int action_status(int arg)
|
|
Packit |
94f725 |
{
|
|
Packit |
94f725 |
crypt_status_info ci;
|
|
Packit |
94f725 |
struct crypt_active_device cad;
|
|
Packit |
94f725 |
struct crypt_params_verity vp = {};
|
|
Packit |
94f725 |
struct crypt_device *cd = NULL;
|
|
Packit |
94f725 |
struct stat st;
|
|
Packit |
94f725 |
char *backing_file, *root_hash;
|
|
Packit |
94f725 |
size_t root_hash_size;
|
|
Packit |
94f725 |
unsigned i, path = 0;
|
|
Packit |
94f725 |
int r = 0;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
/* perhaps a path, not a dm device name */
|
|
Packit |
94f725 |
if (strchr(action_argv[0], '/') && !stat(action_argv[0], &st))
|
|
Packit |
94f725 |
path = 1;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
ci = crypt_status(NULL, action_argv[0]);
|
|
Packit |
94f725 |
switch (ci) {
|
|
Packit |
94f725 |
case CRYPT_INVALID:
|
|
Packit |
94f725 |
r = -EINVAL;
|
|
Packit |
94f725 |
break;
|
|
Packit |
94f725 |
case CRYPT_INACTIVE:
|
|
Packit |
94f725 |
if (path)
|
|
Packit |
94f725 |
log_std("%s is inactive.\n", action_argv[0]);
|
|
Packit |
94f725 |
else
|
|
Packit |
94f725 |
log_std("%s/%s is inactive.\n", crypt_get_dir(), action_argv[0]);
|
|
Packit |
94f725 |
r = -ENODEV;
|
|
Packit |
94f725 |
break;
|
|
Packit |
94f725 |
case CRYPT_ACTIVE:
|
|
Packit |
94f725 |
case CRYPT_BUSY:
|
|
Packit |
94f725 |
if (path)
|
|
Packit |
94f725 |
log_std("%s is active%s.\n", action_argv[0],
|
|
Packit |
94f725 |
ci == CRYPT_BUSY ? " and is in use" : "");
|
|
Packit |
94f725 |
else
|
|
Packit |
94f725 |
log_std("%s/%s is active%s.\n", crypt_get_dir(), action_argv[0],
|
|
Packit |
94f725 |
ci == CRYPT_BUSY ? " and is in use" : "");
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
r = crypt_init_by_name_and_header(&cd, action_argv[0], NULL);
|
|
Packit |
94f725 |
if (r < 0)
|
|
Packit |
94f725 |
goto out;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
log_std(" type: %s\n", crypt_get_type(cd) ?: "n/a");
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
r = crypt_get_active_device(cd, action_argv[0], &cad;;
|
|
Packit |
94f725 |
if (r < 0)
|
|
Packit |
94f725 |
goto out;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
/* Print only VERITY type devices */
|
|
Packit |
94f725 |
r = crypt_get_verity_info(cd, &vp);
|
|
Packit |
94f725 |
if (r < 0)
|
|
Packit |
94f725 |
goto out;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
log_std(" status: %s%s\n",
|
|
Packit |
94f725 |
cad.flags & CRYPT_ACTIVATE_CORRUPTED ? "corrupted" : "verified",
|
|
Packit |
94f725 |
vp.flags & CRYPT_VERITY_ROOT_HASH_SIGNATURE ? " (with signature)" : "");
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
log_std(" hash type: %u\n", vp.hash_type);
|
|
Packit |
94f725 |
log_std(" data block: %u\n", vp.data_block_size);
|
|
Packit |
94f725 |
log_std(" hash block: %u\n", vp.hash_block_size);
|
|
Packit |
94f725 |
log_std(" hash name: %s\n", vp.hash_name);
|
|
Packit |
94f725 |
log_std(" salt: ");
|
|
Packit |
94f725 |
if (vp.salt_size)
|
|
Packit |
94f725 |
for(i = 0; i < vp.salt_size; i++)
|
|
Packit |
94f725 |
log_std("%02hhx", (const char)vp.salt[i]);
|
|
Packit |
94f725 |
else
|
|
Packit |
94f725 |
log_std("-");
|
|
Packit |
94f725 |
log_std("\n");
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
log_std(" data device: %s\n", vp.data_device);
|
|
Packit |
94f725 |
if ((backing_file = crypt_loop_backing_file(vp.data_device))) {
|
|
Packit |
94f725 |
log_std(" data loop: %s\n", backing_file);
|
|
Packit |
94f725 |
free(backing_file);
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
log_std(" size: %" PRIu64 " sectors\n", cad.size);
|
|
Packit |
94f725 |
log_std(" mode: %s\n", cad.flags & CRYPT_ACTIVATE_READONLY ?
|
|
Packit |
94f725 |
"readonly" : "read/write");
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
log_std(" hash device: %s\n", vp.hash_device);
|
|
Packit |
94f725 |
if ((backing_file = crypt_loop_backing_file(vp.hash_device))) {
|
|
Packit |
94f725 |
log_std(" hash loop: %s\n", backing_file);
|
|
Packit |
94f725 |
free(backing_file);
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
log_std(" hash offset: %" PRIu64 " sectors\n",
|
|
Packit |
94f725 |
vp.hash_area_offset * vp.hash_block_size / 512);
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (vp.fec_device) {
|
|
Packit |
94f725 |
log_std(" FEC device: %s\n", vp.fec_device);
|
|
Packit |
94f725 |
if ((backing_file = crypt_loop_backing_file(vp.fec_device))) {
|
|
Packit |
94f725 |
log_std(" FEC loop: %s\n", backing_file);
|
|
Packit |
94f725 |
free(backing_file);
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
log_std(" FEC offset: %" PRIu64 " sectors\n",
|
|
Packit |
94f725 |
vp.fec_area_offset * vp.hash_block_size / 512);
|
|
Packit |
94f725 |
log_std(" FEC roots: %u\n", vp.fec_roots);
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
root_hash_size = crypt_get_volume_key_size(cd);
|
|
Packit |
94f725 |
if (root_hash_size > 0 && (root_hash = malloc(root_hash_size))) {
|
|
Packit |
94f725 |
r = crypt_volume_key_get(cd, CRYPT_ANY_SLOT, root_hash, &root_hash_size, NULL, 0);
|
|
Packit |
94f725 |
if (!r) {
|
|
Packit |
94f725 |
log_std(" root hash: ");
|
|
Packit |
94f725 |
for (i = 0; i < root_hash_size; i++)
|
|
Packit |
94f725 |
log_std("%02hhx", (const char)root_hash[i]);
|
|
Packit |
94f725 |
log_std("\n");
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
free(root_hash);
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (cad.flags & (CRYPT_ACTIVATE_IGNORE_CORRUPTION|
|
|
Packit |
94f725 |
CRYPT_ACTIVATE_RESTART_ON_CORRUPTION|
|
|
Packit |
94f725 |
CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS|
|
|
Packit |
94f725 |
CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE))
|
|
Packit |
94f725 |
log_std(" flags: %s%s%s%s\n",
|
|
Packit |
94f725 |
(cad.flags & CRYPT_ACTIVATE_IGNORE_CORRUPTION) ? "ignore_corruption " : "",
|
|
Packit |
94f725 |
(cad.flags & CRYPT_ACTIVATE_RESTART_ON_CORRUPTION) ? "restart_on_corruption " : "",
|
|
Packit |
94f725 |
(cad.flags & CRYPT_ACTIVATE_IGNORE_ZERO_BLOCKS) ? "ignore_zero_blocks " : "",
|
|
Packit |
94f725 |
(cad.flags & CRYPT_ACTIVATE_CHECK_AT_MOST_ONCE) ? "check_at_most_once" : "");
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
out:
|
|
Packit |
94f725 |
crypt_free(cd);
|
|
Packit |
94f725 |
if (r == -ENOTSUP)
|
|
Packit |
94f725 |
r = 0;
|
|
Packit |
94f725 |
return r;
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
static int action_dump(int arg)
|
|
Packit |
94f725 |
{
|
|
Packit |
94f725 |
struct crypt_device *cd = NULL;
|
|
Packit |
94f725 |
struct crypt_params_verity params = {};
|
|
Packit |
94f725 |
int r;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if ((r = crypt_init(&cd, action_argv[0])))
|
|
Packit |
94f725 |
return r;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
params.hash_area_offset = hash_offset;
|
|
Packit |
94f725 |
params.fec_area_offset = fec_offset;
|
|
Packit |
94f725 |
r = crypt_load(cd, CRYPT_VERITY, ¶ms);
|
|
Packit |
94f725 |
if (!r)
|
|
Packit |
94f725 |
crypt_dump(cd);
|
|
Packit |
94f725 |
crypt_free(cd);
|
|
Packit |
94f725 |
return r;
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
static struct action_type {
|
|
Packit |
94f725 |
const char *type;
|
|
Packit |
94f725 |
int (*handler)(int);
|
|
Packit |
94f725 |
int required_action_argc;
|
|
Packit |
94f725 |
const char *arg_desc;
|
|
Packit |
94f725 |
const char *desc;
|
|
Packit |
94f725 |
} action_types[] = {
|
|
Packit |
94f725 |
{ "format", action_format, 2, N_("<data_device> <hash_device>"),N_("format device") },
|
|
Packit |
94f725 |
{ "verify", action_verify, 3, N_("<data_device> <hash_device> <root_hash>"),N_("verify device") },
|
|
Packit |
94f725 |
{ "open", action_open, 4, N_("<data_device> <name> <hash_device> <root_hash>"),N_("open device as <name>") },
|
|
Packit |
94f725 |
{ "close", action_close, 1, N_("<name>"),N_("close device (remove mapping)") },
|
|
Packit |
94f725 |
{ "status", action_status, 1, N_("<name>"),N_("show active device status") },
|
|
Packit |
94f725 |
{ "dump", action_dump, 1, N_("<hash_device>"),N_("show on-disk information") },
|
|
Packit |
94f725 |
{ NULL, NULL, 0, NULL, NULL }
|
|
Packit |
94f725 |
};
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
static void help(poptContext popt_context,
|
|
Packit |
94f725 |
enum poptCallbackReason reason __attribute__((unused)),
|
|
Packit |
94f725 |
struct poptOption *key,
|
|
Packit |
94f725 |
const char *arg __attribute__((unused)),
|
|
Packit |
94f725 |
void *data __attribute__((unused)))
|
|
Packit |
94f725 |
{
|
|
Packit |
94f725 |
struct action_type *action;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (key->shortName == '?') {
|
|
Packit |
94f725 |
log_std("%s %s\n", PACKAGE_VERITY, PACKAGE_VERSION);
|
|
Packit |
94f725 |
poptPrintHelp(popt_context, stdout, 0);
|
|
Packit |
94f725 |
log_std(_("\n"
|
|
Packit |
94f725 |
"<action> is one of:\n"));
|
|
Packit |
94f725 |
for(action = action_types; action->type; action++)
|
|
Packit |
94f725 |
log_std("\t%s %s - %s\n", action->type, _(action->arg_desc), _(action->desc));
|
|
Packit |
94f725 |
log_std(_("\n"
|
|
Packit |
94f725 |
"<name> is the device to create under %s\n"
|
|
Packit |
94f725 |
"<data_device> is the data device\n"
|
|
Packit |
94f725 |
"<hash_device> is the device containing verification data\n"
|
|
Packit |
94f725 |
"<root_hash> hash of the root node on <hash_device>\n"),
|
|
Packit |
94f725 |
crypt_get_dir());
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
log_std(_("\nDefault compiled-in dm-verity parameters:\n"
|
|
Packit |
94f725 |
"\tHash: %s, Data block (bytes): %u, "
|
|
Packit |
94f725 |
"Hash block (bytes): %u, Salt size: %u, Hash format: %u\n"),
|
|
Packit |
94f725 |
DEFAULT_VERITY_HASH, DEFAULT_VERITY_DATA_BLOCK,
|
|
Packit |
94f725 |
DEFAULT_VERITY_HASH_BLOCK, DEFAULT_VERITY_SALT_SIZE,
|
|
Packit |
94f725 |
1);
|
|
Packit |
94f725 |
poptFreeContext(popt_context);
|
|
Packit |
94f725 |
exit(EXIT_SUCCESS);
|
|
Packit |
94f725 |
} else if (key->shortName == 'V') {
|
|
Packit |
94f725 |
log_std("%s %s\n", PACKAGE_VERITY, PACKAGE_VERSION);
|
|
Packit |
94f725 |
poptFreeContext(popt_context);
|
|
Packit |
94f725 |
exit(EXIT_SUCCESS);
|
|
Packit |
94f725 |
} else
|
|
Packit |
94f725 |
usage(popt_context, EXIT_SUCCESS, NULL, NULL);
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
static int run_action(struct action_type *action)
|
|
Packit |
94f725 |
{
|
|
Packit |
94f725 |
int r;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
log_dbg("Running command %s.", action->type);
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
r = action->handler(0);
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
show_status(r);
|
|
Packit |
94f725 |
return translate_errno(r);
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
int main(int argc, const char **argv)
|
|
Packit |
94f725 |
{
|
|
Packit |
94f725 |
static char *popt_tmp;
|
|
Packit |
94f725 |
static const char *null_action_argv[] = {NULL};
|
|
Packit |
94f725 |
static struct poptOption popt_help_options[] = {
|
|
Packit |
94f725 |
{ NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL },
|
|
Packit |
94f725 |
{ "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help message"), NULL },
|
|
Packit |
94f725 |
{ "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief usage"), NULL },
|
|
Packit |
94f725 |
{ "version",'V', POPT_ARG_NONE, NULL, 0, N_("Print package version"), NULL },
|
|
Packit |
94f725 |
POPT_TABLEEND
|
|
Packit |
94f725 |
};
|
|
Packit |
94f725 |
static struct poptOption popt_options[] = {
|
|
Packit |
94f725 |
{ NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
|
|
Packit |
94f725 |
{ "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 0, N_("Shows more detailed error messages"), NULL },
|
|
Packit |
94f725 |
{ "debug", '\0', POPT_ARG_NONE, &opt_debug, 0, N_("Show debug messages"), NULL },
|
|
Packit |
94f725 |
{ "no-superblock", 0, POPT_ARG_VAL, &use_superblock, 0, N_("Do not use verity superblock"), NULL },
|
|
Packit |
94f725 |
{ "format", 0, POPT_ARG_INT, &hash_type, 0, N_("Format type (1 - normal, 0 - original Chrome OS)"), N_("number") },
|
|
Packit |
94f725 |
{ "data-block-size", 0, POPT_ARG_INT, &data_block_size, 0, N_("Block size on the data device"), N_("bytes") },
|
|
Packit |
94f725 |
{ "hash-block-size", 0, POPT_ARG_INT, &hash_block_size, 0, N_("Block size on the hash device"), N_("bytes") },
|
|
Packit |
94f725 |
{ "fec-roots", 0, POPT_ARG_INT, &fec_roots, 0, N_("FEC parity bytes"), N_("bytes") },
|
|
Packit |
94f725 |
{ "data-blocks", 0, POPT_ARG_STRING, &popt_tmp, 1, N_("The number of blocks in the data file"), N_("blocks") },
|
|
Packit |
94f725 |
{ "fec-device", 0, POPT_ARG_STRING, &fec_device, 0, N_("Path to device with error correction data"), N_("path") },
|
|
Packit |
94f725 |
{ "hash-offset", 0, POPT_ARG_STRING, &popt_tmp, 2, N_("Starting offset on the hash device"), N_("bytes") },
|
|
Packit |
94f725 |
{ "fec-offset", 0, POPT_ARG_STRING, &popt_tmp, 3, N_("Starting offset on the FEC device"), N_("bytes") },
|
|
Packit |
94f725 |
{ "hash", 'h', POPT_ARG_STRING, &hash_algorithm, 0, N_("Hash algorithm"), N_("string") },
|
|
Packit |
94f725 |
{ "salt", 's', POPT_ARG_STRING, &salt_string, 0, N_("Salt"), N_("hex string") },
|
|
Packit |
94f725 |
{ "uuid", '\0', POPT_ARG_STRING, &opt_uuid, 0, N_("UUID for device to use"), NULL },
|
|
Packit |
94f725 |
{ "root-hash-signature",'\0', POPT_ARG_STRING, &opt_root_hash_signature, 0, N_("Path to root hash signature file"), NULL },
|
|
Packit |
94f725 |
{ "restart-on-corruption", 0,POPT_ARG_NONE,&opt_restart_on_corruption, 0, N_("Restart kernel if corruption is detected"), NULL },
|
|
Packit |
94f725 |
{ "ignore-corruption", 0, POPT_ARG_NONE, &opt_ignore_corruption, 0, N_("Ignore corruption, log it only"), NULL },
|
|
Packit |
94f725 |
{ "ignore-zero-blocks", 0, POPT_ARG_NONE, &opt_ignore_zero_blocks, 0, N_("Do not verify zeroed blocks"), NULL },
|
|
Packit |
94f725 |
{ "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 },
|
|
Packit |
94f725 |
POPT_TABLEEND
|
|
Packit |
94f725 |
};
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
poptContext popt_context;
|
|
Packit |
94f725 |
struct action_type *action;
|
|
Packit |
94f725 |
const char *aname;
|
|
Packit |
94f725 |
int r;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
crypt_set_log_callback(NULL, tool_log, NULL);
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
setlocale(LC_ALL, "");
|
|
Packit |
94f725 |
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
Packit |
94f725 |
textdomain(PACKAGE);
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
popt_context = poptGetContext("verity", argc, argv, popt_options, 0);
|
|
Packit |
94f725 |
poptSetOtherOptionHelp(popt_context,
|
|
Packit |
94f725 |
_("[OPTION...] <action> <action-specific>"));
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
while((r = poptGetNextOpt(popt_context)) > 0) {
|
|
Packit |
94f725 |
unsigned long long ull_value;
|
|
Packit |
94f725 |
char *endp;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
errno = 0;
|
|
Packit |
94f725 |
ull_value = strtoull(popt_tmp, &endp, 10);
|
|
Packit |
94f725 |
if (*endp || !*popt_tmp || !isdigit(*popt_tmp) ||
|
|
Packit |
94f725 |
(errno == ERANGE && ull_value == ULLONG_MAX) ||
|
|
Packit |
94f725 |
(errno != 0 && ull_value == 0))
|
|
Packit |
94f725 |
r = POPT_ERROR_BADNUMBER;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
switch(r) {
|
|
Packit |
94f725 |
case 1:
|
|
Packit |
94f725 |
data_blocks = ull_value;
|
|
Packit |
94f725 |
break;
|
|
Packit |
94f725 |
case 2:
|
|
Packit |
94f725 |
hash_offset = ull_value;
|
|
Packit |
94f725 |
break;
|
|
Packit |
94f725 |
case 3:
|
|
Packit |
94f725 |
fec_offset = ull_value;
|
|
Packit |
94f725 |
break;
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (r < 0)
|
|
Packit |
94f725 |
break;
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (r < -1)
|
|
Packit |
94f725 |
usage(popt_context, EXIT_FAILURE, poptStrerror(r),
|
|
Packit |
94f725 |
poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (!(aname = poptGetArg(popt_context)))
|
|
Packit |
94f725 |
usage(popt_context, EXIT_FAILURE, _("Argument <action> missing."),
|
|
Packit |
94f725 |
poptGetInvocationName(popt_context));
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
action_argc = 0;
|
|
Packit |
94f725 |
action_argv = poptGetArgs(popt_context);
|
|
Packit |
94f725 |
/* Make return values of poptGetArgs more consistent in case of remaining argc = 0 */
|
|
Packit |
94f725 |
if(!action_argv)
|
|
Packit |
94f725 |
action_argv = null_action_argv;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
/* Count args, somewhat unnice, change? */
|
|
Packit |
94f725 |
while(action_argv[action_argc] != NULL)
|
|
Packit |
94f725 |
action_argc++;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
/* Handle aliases */
|
|
Packit |
94f725 |
if (!strcmp(aname, "create") && action_argc > 1) {
|
|
Packit |
94f725 |
/* create command had historically switched arguments */
|
|
Packit |
94f725 |
if (action_argv[0] && action_argv[1]) {
|
|
Packit |
94f725 |
const char *tmp = action_argv[0];
|
|
Packit |
94f725 |
action_argv[0] = action_argv[1];
|
|
Packit |
94f725 |
action_argv[1] = tmp;
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
aname = "open";
|
|
Packit |
94f725 |
} else if (!strcmp(aname, "remove")) {
|
|
Packit |
94f725 |
aname = "close";
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
for (action = action_types; action->type; action++)
|
|
Packit |
94f725 |
if (strcmp(action->type, aname) == 0)
|
|
Packit |
94f725 |
break;
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (!action->type)
|
|
Packit |
94f725 |
usage(popt_context, EXIT_FAILURE, _("Unknown action."),
|
|
Packit |
94f725 |
poptGetInvocationName(popt_context));
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (action_argc < action->required_action_argc) {
|
|
Packit |
94f725 |
char buf[128];
|
|
Packit |
94f725 |
snprintf(buf, 128,_("%s: requires %s as arguments"), action->type, action->arg_desc);
|
|
Packit |
94f725 |
usage(popt_context, EXIT_FAILURE, buf,
|
|
Packit |
94f725 |
poptGetInvocationName(popt_context));
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (data_block_size < 0 || hash_block_size < 0 || hash_type < 0) {
|
|
Packit |
94f725 |
usage(popt_context, EXIT_FAILURE,
|
|
Packit |
94f725 |
_("Negative number for option not permitted."),
|
|
Packit |
94f725 |
poptGetInvocationName(popt_context));
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if ((opt_ignore_corruption || opt_restart_on_corruption || opt_ignore_zero_blocks) && strcmp(aname, "open"))
|
|
Packit |
94f725 |
usage(popt_context, EXIT_FAILURE,
|
|
Packit |
94f725 |
_("Option --ignore-corruption, --restart-on-corruption or --ignore-zero-blocks is allowed only for open operation."),
|
|
Packit |
94f725 |
poptGetInvocationName(popt_context));
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (opt_root_hash_signature && strcmp(aname, "open"))
|
|
Packit |
94f725 |
usage(popt_context, EXIT_FAILURE,
|
|
Packit |
94f725 |
_("Option --root-hash-signature can be used only for open operation."),
|
|
Packit |
94f725 |
poptGetInvocationName(popt_context));
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (opt_ignore_corruption && opt_restart_on_corruption)
|
|
Packit |
94f725 |
usage(popt_context, EXIT_FAILURE,
|
|
Packit |
94f725 |
_("Option --ignore-corruption and --restart-on-corruption cannot be used together."),
|
|
Packit |
94f725 |
poptGetInvocationName(popt_context));
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
if (opt_debug) {
|
|
Packit |
94f725 |
opt_verbose = 1;
|
|
Packit |
94f725 |
crypt_set_debug_level(-1);
|
|
Packit |
94f725 |
dbg_version_and_cmd(argc, argv);
|
|
Packit |
94f725 |
}
|
|
Packit |
94f725 |
|
|
Packit |
94f725 |
r = run_action(action);
|
|
Packit |
94f725 |
poptFreeContext(popt_context);
|
|
Packit |
94f725 |
return r;
|
|
Packit |
94f725 |
}
|