X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fveritysetup.c;h=cc2095dec3d545a185c519985eefcceea42732d5;hb=e689eb4a0aee2fb5aaccbbeb15364d7f7354e074;hp=f8241e1d4322b04701f8cbfc8e8218f0072c2159;hpb=4b8f91d0d9576d89e0fc00e0be36b6250cac1027;p=platform%2Fupstream%2Fcryptsetup.git diff --git a/src/veritysetup.c b/src/veritysetup.c index f8241e1..cc2095d 100644 --- a/src/veritysetup.c +++ b/src/veritysetup.c @@ -5,7 +5,8 @@ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License - * version 2 as published by the Free Software Foundation. + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -17,16 +18,6 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include -#include -#include -#include -#include -#include -#include -#include -#include - #include "cryptsetup.h" #define PACKAGE_VERITY "veritysetup" @@ -39,85 +30,20 @@ static int data_block_size = DEFAULT_VERITY_DATA_BLOCK; static int hash_block_size = DEFAULT_VERITY_HASH_BLOCK; static uint64_t data_blocks = 0; static const char *salt_string = NULL; -static uint64_t hash_start = 0; +static uint64_t hash_offset = 0; static const char *opt_uuid = NULL; -static int opt_verbose = 0; -static int opt_debug = 0; static int opt_version_mode = 0; static const char **action_argv; static int action_argc; -static size_t hex_to_bytes(const char *hex, char *result) -{ - char buf[3] = "xx\0", *endp; - size_t i, len; - - len = strlen(hex) / 2; - for (i = 0; i < len; i++) { - memcpy(buf, &hex[i * 2], 2); - result[i] = strtoul(buf, &endp, 16); - if (endp != &buf[2]) - return -EINVAL; - } - return i; -} - -__attribute__((format(printf, 5, 6))) -static void clogger(struct crypt_device *cd, int level, const char *file, - int line, const char *format, ...) -{ - va_list argp; - char *target = NULL; - - va_start(argp, format); - - if (vasprintf(&target, format, argp) > 0) { - if (level >= 0) { - crypt_log(cd, level, target); -#ifdef CRYPT_DEBUG - } else if (opt_debug) - printf("# %s:%d %s\n", file ?: "?", line, target); -#else - } else if (opt_debug) - printf("# %s\n", target); -#endif - } - - va_end(argp); - free(target); -} - -static void _log(int level, const char *msg, void *usrptr __attribute__((unused))) -{ - switch(level) { - - case CRYPT_LOG_NORMAL: - fputs(msg, stdout); - break; - case CRYPT_LOG_VERBOSE: - if (opt_verbose) - fputs(msg, stdout); - break; - case CRYPT_LOG_ERROR: - fputs(msg, stderr); - break; - case CRYPT_LOG_DEBUG: - if (opt_debug) - printf("# %s\n", msg); - break; - default: - fprintf(stderr, "Internal error on logging class for msg: %s", msg); - break; - } -} - static int _prepare_format(struct crypt_params_verity *params, const char *data_device, uint32_t flags) { - static char salt_bytes[512]; + char *salt = NULL; + int len; params->hash_name = hash_algorithm ?: DEFAULT_VERITY_HASH; params->data_device = data_device; @@ -126,17 +52,22 @@ static int _prepare_format(struct crypt_params_verity *params, params->salt_size = 0; params->salt = NULL; } else if (salt_string) { - params->salt_size = strlen(salt_string) / 2; - if (hex_to_bytes(salt_string, salt_bytes) != params->salt_size) + len = crypt_hex_to_bytes(salt_string, &salt, 0); + if (len < 0) { + log_err(_("Invalid salt string specified.\n")); return -EINVAL; - params->salt = salt_bytes; - } else + } + params->salt_size = len; + params->salt = salt; + } else { params->salt_size = DEFAULT_VERITY_SALT_SIZE; + params->salt = NULL; + } params->data_block_size = data_block_size; params->hash_block_size = hash_block_size; params->data_size = data_blocks; - params->hash_area_offset = hash_start; + params->hash_area_offset = hash_offset; params->hash_type = hash_type; params->flags = flags; @@ -150,6 +81,16 @@ static int action_format(int arg) uint32_t flags = CRYPT_VERITY_CREATE_HASH; int r; + /* Try to create hash image if doesn't exist */ + r = open(action_argv[1], O_WRONLY | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR); + if (r < 0 && errno != EEXIST) { + log_err(_("Cannot create hash image %s for writing.\n"), action_argv[1]); + return -EINVAL; + } else if (r >= 0) { + log_dbg("Created hash image %s.", action_argv[1]); + close(r); + } + if ((r = crypt_init(&cd, action_argv[1]))) goto out; @@ -165,6 +106,7 @@ static int action_format(int arg) crypt_dump(cd); out: crypt_free(cd); + free(CONST_CAST(char*)params.salt); return r; } @@ -177,8 +119,8 @@ static int _activate(const char *dm_device, struct crypt_device *cd = NULL; struct crypt_params_verity params = {}; uint32_t activate_flags = CRYPT_ACTIVATE_READONLY; - char root_hash_bytes[128]; - size_t hash_size; + char *root_hash_bytes = NULL; + ssize_t hash_size; int r; if ((r = crypt_init(&cd, hash_device))) @@ -186,7 +128,7 @@ static int _activate(const char *dm_device, if (use_superblock) { params.flags = flags; - params.hash_area_offset = hash_start; + params.hash_area_offset = hash_offset; r = crypt_load(cd, CRYPT_VERITY, ¶ms); } else { r = _prepare_format(¶ms, data_device, flags | CRYPT_VERITY_NO_HEADER); @@ -201,7 +143,8 @@ static int _activate(const char *dm_device, goto out; hash_size = crypt_get_volume_key_size(cd); - if (hex_to_bytes(root_hash, root_hash_bytes) != hash_size) { + if (crypt_hex_to_bytes(root_hash, &root_hash_bytes, 0) != hash_size) { + log_err(_("Invalid root hash string specified.\n")); r = -EINVAL; goto out; } @@ -211,6 +154,8 @@ static int _activate(const char *dm_device, activate_flags); out: crypt_free(cd); + free(root_hash_bytes); + free(CONST_CAST(char*)params.salt); return r; } @@ -344,7 +289,7 @@ static int action_dump(int arg) if ((r = crypt_init(&cd, action_argv[0]))) return r; - params.hash_area_offset = hash_start; + params.hash_area_offset = hash_offset; r = crypt_load(cd, CRYPT_VERITY, ¶ms); if (!r) crypt_dump(cd); @@ -352,30 +297,6 @@ static int action_dump(int arg) return r; } -static __attribute__ ((noreturn)) void usage(poptContext popt_context, - int exitcode, const char *error, - const char *more) -{ - poptPrintUsage(popt_context, stderr, 0); - if (error) - log_err("%s: %s\n", more, error); - poptFreeContext(popt_context); - exit(exitcode); -} - -static void _dbg_version_and_cmd(int argc, const char **argv) -{ - int i; - - log_std("# %s %s processing \"", PACKAGE_VERITY, PACKAGE_VERSION); - for (i = 0; i < argc; i++) { - if (i) - log_std(" "); - log_std("%s", argv[i]); - } - log_std("\"\n"); -} - static struct action_type { const char *type; int (*handler)(int); @@ -425,35 +346,6 @@ static void help(poptContext popt_context, usage(popt_context, EXIT_SUCCESS, NULL, NULL); } -static void show_status(int errcode) -{ - char error[256], *error_; - - if(!opt_verbose) - return; - - if(!errcode) { - log_std(_("Command successful.\n")); - return; - } - - crypt_get_error(error, sizeof(error)); - - if (!error[0]) { - error_ = strerror_r(-errcode, error, sizeof(error)); - if (error_ != error) { - strncpy(error, error_, sizeof(error)); - error[sizeof(error) - 1] = '\0'; - } - } - - log_err(_("Command failed with code %i"), -errcode); - if (*error) - log_err(": %s\n", error); - else - log_err(".\n"); -} - static int run_action(struct action_type *action) { int r; @@ -463,27 +355,13 @@ static int run_action(struct action_type *action) r = action->handler(0); show_status(r); - - /* Translate exit code to simple codes */ - switch (r) { - case 0: r = EXIT_SUCCESS; break; - case -EEXIST: - case -EBUSY: r = 5; break; - case -ENOTBLK: - case -ENODEV: r = 4; break; - case -ENOMEM: r = 3; break; - case -EPERM: r = 2; break; - case -EINVAL: - case -ENOENT: - case -ENOSYS: - default: r = EXIT_FAILURE; - } - return r; + return translate_errno(r); } int main(int argc, const char **argv) { static char *popt_tmp; + static const char *null_action_argv[] = {NULL}; static struct poptOption popt_help_options[] = { { NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL }, { "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help message"), NULL }, @@ -500,7 +378,7 @@ int main(int argc, const char **argv) { "data-block-size", 0, POPT_ARG_INT, &data_block_size, 0, N_("Block size on the data device"), N_("bytes") }, { "hash-block-size", 0, POPT_ARG_INT, &hash_block_size, 0, N_("Block size on the hash device"), N_("bytes") }, { "data-blocks", 0, POPT_ARG_STRING, &popt_tmp, 1, N_("The number of blocks in the data file"), N_("blocks") }, - { "hash-start", 0, POPT_ARG_STRING, &popt_tmp, 2, N_("Starting block on the hash device"), N_("512-byte sectors") }, + { "hash-offset", 0, POPT_ARG_STRING, &popt_tmp, 2, N_("Starting offset on the hash device"), N_("bytes") }, { "hash", 'h', POPT_ARG_STRING, &hash_algorithm, 0, N_("Hash algorithm"), N_("string") }, { "salt", 's', POPT_ARG_STRING, &salt_string, 0, N_("Salt"), N_("hex string") }, { "uuid", '\0', POPT_ARG_STRING, &opt_uuid, 0, N_("UUID for device to use."), NULL }, @@ -509,10 +387,10 @@ int main(int argc, const char **argv) poptContext popt_context; struct action_type *action; - const char *aname, *null_action_argv[] = {NULL}; + const char *aname; int r; - crypt_set_log_callback(NULL, _log, NULL); + crypt_set_log_callback(NULL, tool_log, NULL); setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); @@ -520,15 +398,15 @@ int main(int argc, const char **argv) popt_context = poptGetContext("verity", argc, argv, popt_options, 0); poptSetOtherOptionHelp(popt_context, - N_("[OPTION...] ")); + _("[OPTION...] ")); while((r = poptGetNextOpt(popt_context)) > 0) { unsigned long long ull_value; char *endp; errno = 0; - ull_value = strtoull(popt_tmp, &endp, 0); - if (*endp || !*popt_tmp || + ull_value = strtoull(popt_tmp, &endp, 10); + if (*endp || !*popt_tmp || !isdigit(*popt_tmp) || (errno == ERANGE && ull_value == ULLONG_MAX) || (errno != 0 && ull_value == 0)) r = POPT_ERROR_BADNUMBER; @@ -538,7 +416,7 @@ int main(int argc, const char **argv) data_blocks = ull_value; break; case 2: - hash_start = ull_value * 512; + hash_offset = ull_value; break; } @@ -583,10 +461,16 @@ int main(int argc, const char **argv) poptGetInvocationName(popt_context)); } + if (data_block_size < 0 || hash_block_size < 0 || hash_type < 0) { + usage(popt_context, EXIT_FAILURE, + _("Negative number for option not permitted."), + poptGetInvocationName(popt_context)); + } + if (opt_debug) { opt_verbose = 1; crypt_set_debug_level(-1); - _dbg_version_and_cmd(argc, argv); + dbg_version_and_cmd(argc, argv); } r = run_action(action);