return params;
}
static char *get_dm_verity_params(struct crypt_params_verity *vp,
- struct crypt_dm_active_verity *dmd)
+ struct crypt_dm_active_device *dmd)
{
int max_size, r;
char *params = NULL, *hexroot = NULL, *hexsalt = NULL;
- hexroot = crypt_safe_alloc(dmd->root_hash_size * 2 + 1);
+ hexroot = crypt_safe_alloc(dmd->u.verity.root_hash_size * 2 + 1);
if (!hexroot)
goto out;
- hex_key(hexroot, dmd->root_hash_size, dmd->root_hash);
+ hex_key(hexroot, dmd->u.verity.root_hash_size, dmd->u.verity.root_hash);
hexsalt = crypt_safe_alloc(vp->salt_size * 2 + 1);
if (!hexsalt)
hex_key(hexsalt, vp->salt_size, vp->salt);
max_size = strlen(hexroot) + strlen(hexsalt) +
- strlen(dmd->data_device) + strlen(dmd->hash_device) +
+ strlen(dmd->u.verity.data_device) +
+ strlen(dmd->u.verity.hash_device) +
strlen(vp->hash_name) + 128;
params = crypt_safe_alloc(max_size);
r = snprintf(params, max_size,
"%u %s %s %u %u %" PRIu64 " %" PRIu64 " %s %s %s",
- vp->version, dmd->data_device, dmd->hash_device,
+ vp->version, dmd->u.verity.data_device,
+ dmd->u.verity.hash_device,
vp->data_block_size, vp->hash_block_size,
- vp->data_size, dmd->hash_offset,
+ vp->data_size, dmd->u.verity.hash_offset,
vp->hash_name, hexroot, hexsalt);
if (r < 0 || r >= max_size) {
crypt_safe_free(params);
int dm_create_device(const char *name,
const char *type,
struct crypt_dm_active_device *dmd,
+ void *params,
int reload)
{
char *table_params = NULL;
- table_params = get_dm_crypt_params(dmd);
+ if (dmd->target == DM_CRYPT)
+ table_params = get_dm_crypt_params(dmd);
+ else if (dmd->target == DM_VERITY)
+ table_params = get_dm_verity_params(params, dmd);
+
if (!table_params)
return -EINVAL;
dmd->uuid, dmd->size, table_params, reload);
}
-int dm_create_verity(const char *name,
- struct crypt_params_verity *params,
- struct crypt_dm_active_verity *dmd)
-{
- char *table_params = NULL;
-
- table_params = get_dm_verity_params(params, dmd);
- if (!table_params)
- return -EINVAL;
-
- return _dm_create_device(name, CRYPT_VERITY, dmd->data_device, dmd->flags,
- NULL, dmd->size, table_params, 0);
-}
-
static int dm_status_dmi(const char *name, struct dm_info *dmi,
const char *target, char **status_line)
{
return r;
}
-int dm_query_device(const char *name, uint32_t get_flags,
- struct crypt_dm_active_device *dmd)
+static int _dm_query_crypt(uint32_t get_flags,
+ struct dm_info *dmi,
+ char *params,
+ struct crypt_dm_active_device *dmd)
{
- struct dm_task *dmt;
- struct dm_info dmi;
- uint64_t start, length, val64;
- char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *arg;
- const char *tmp_uuid;
- void *next = NULL;
+ uint64_t val64;
+ char *rcipher, *key_, *rdevice, *endp, buffer[3], *arg;
unsigned int i;
- int r = -EINVAL;
-
- memset(dmd, 0, sizeof(*dmd));
-
- if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
- goto out;
- if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
- goto out;
- if (!dm_task_set_name(dmt, name))
- goto out;
- r = -ENODEV;
- if (!dm_task_run(dmt))
- goto out;
-
- r = -EINVAL;
- if (!dm_task_get_info(dmt, &dmi))
- goto out;
- if (!dmi.exists) {
- r = -ENODEV;
- goto out;
- }
-
- tmp_uuid = dm_task_get_uuid(dmt);
-
- next = dm_get_next_target(dmt, next, &start, &length,
- &target_type, ¶ms);
- if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
- start != 0 || next)
- goto out;
-
- dmd->size = length;
+ dmd->target = DM_CRYPT;
rcipher = strsep(¶ms, " ");
/* cipher */
/* skip */
key_ = strsep(¶ms, " ");
if (!params)
- goto out;
+ return -EINVAL;
val64 = strtoull(params, ¶ms, 10);
if (*params != ' ')
- goto out;
+ return -EINVAL;
params++;
dmd->u.crypt.iv_offset = val64;
/*offset */
if (!params)
- goto out;
+ return -EINVAL;
val64 = strtoull(params, ¶ms, 10);
dmd->u.crypt.offset = val64;
/* Features section, available since crypt target version 1.11 */
if (*params) {
if (*params != ' ')
- goto out;
+ return -EINVAL;
params++;
/* Number of arguments */
val64 = strtoull(params, ¶ms, 10);
if (*params != ' ')
- goto out;
+ return -EINVAL;
params++;
for (i = 0; i < val64; i++) {
if (!params)
- goto out;
+ return -EINVAL;
arg = strsep(¶ms, " ");
if (!strcasecmp(arg, "allow_discards"))
dmd->flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
else /* unknown option */
- goto out;
+ return -EINVAL;
}
/* All parameters shold be processed */
if (params)
- goto out;
+ return -EINVAL;
}
/* Never allow to return empty key */
- if ((get_flags & DM_ACTIVE_KEY) && dmi.suspended) {
+ if ((get_flags & DM_ACTIVE_KEY) && dmi->suspended) {
log_dbg("Cannot read volume key while suspended.");
- r = -EINVAL;
- goto out;
+ return -EINVAL;
}
if (get_flags & DM_ACTIVE_KEYSIZE) {
dmd->u.crypt.vk = crypt_alloc_volume_key(strlen(key_) / 2, NULL);
- if (!dmd->u.crypt.vk) {
- r = -ENOMEM;
- goto out;
- }
+ if (!dmd->u.crypt.vk)
+ return -ENOMEM;
if (get_flags & DM_ACTIVE_KEY) {
buffer[2] = '\0';
if (endp != &buffer[2]) {
crypt_free_volume_key(dmd->u.crypt.vk);
dmd->u.crypt.vk = NULL;
- r = -EINVAL;
- goto out;
+ return -EINVAL;
}
}
}
}
memset(key_, 0, strlen(key_));
+ return 0;
+}
+
+static int _dm_query_verity(uint32_t get_flags,
+ struct dm_info *dmi,
+ char *params,
+ struct crypt_dm_active_device *dmd)
+{
+ dmd->target = DM_VERITY;
+ return -EINVAL;
+}
+
+int dm_query_device(const char *name, uint32_t get_flags,
+ struct crypt_dm_active_device *dmd)
+{
+ struct dm_task *dmt;
+ struct dm_info dmi;
+ uint64_t start, length;
+ char *target_type, *params;
+ const char *tmp_uuid;
+ void *next = NULL;
+ int r = -EINVAL;
+
+ memset(dmd, 0, sizeof(*dmd));
+
+ if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
+ goto out;
+ if ((dm_flags() & DM_SECURE_SUPPORTED) && !dm_task_secure_data(dmt))
+ goto out;
+ if (!dm_task_set_name(dmt, name))
+ goto out;
+ r = -ENODEV;
+ if (!dm_task_run(dmt))
+ goto out;
+
+ r = -EINVAL;
+ if (!dm_task_get_info(dmt, &dmi))
+ goto out;
+
+ if (!dmi.exists) {
+ r = -ENODEV;
+ goto out;
+ }
+
+ next = dm_get_next_target(dmt, next, &start, &length,
+ &target_type, ¶ms);
+
+ if (!target_type || start != 0 || next)
+ goto out;
+
+ if (!strcmp(target_type, DM_CRYPT_TARGET))
+ r = _dm_query_crypt(get_flags, &dmi, params, dmd);
+ else if (!strcmp(target_type, DM_VERITY_TARGET))
+ r = _dm_query_verity(get_flags, &dmi, params, dmd);
+ else
+ r = -EINVAL;
+
+ if (r < 0)
+ goto out;
+
+ dmd->size = length;
+
if (dmi.read_only)
dmd->flags |= CRYPT_ACTIVATE_READONLY;
+ tmp_uuid = dm_task_get_uuid(dmt);
if (!tmp_uuid)
dmd->flags |= CRYPT_ACTIVATE_NO_UUID;
else if (get_flags & DM_ACTIVE_UUID) {
return r;
}
-int dm_query_verity(const char *name,
- struct crypt_dm_active_verity *dmd)
-{
- int r = -EINVAL;
-
- return r;
-}
-
static int _dm_message(const char *name, const char *msg)
{
int r = 0;
struct crypt_params_verity *verity_hdr,
uint32_t flags)
{
- struct crypt_dm_active_verity dmd;
+ struct crypt_dm_active_device dmd;
uint64_t offset = 0;
int r;
if (!name)
return 0;
- dmd.data_device = crypt_get_device_name(cd);
- dmd.hash_device = hash_device;
- dmd.root_hash = root_hash;
- dmd.root_hash_size = root_hash_size;
- dmd.hash_offset = VERITY_hash_offset_block(verity_hdr),
+ dmd.target = DM_VERITY;
+ dmd.u.verity.data_device = crypt_get_device_name(cd);
+ dmd.u.verity.hash_device = hash_device;
+ dmd.u.verity.root_hash = root_hash;
+ dmd.u.verity.root_hash_size = root_hash_size;
+ dmd.u.verity.hash_offset = VERITY_hash_offset_block(verity_hdr),
dmd.flags = CRYPT_ACTIVATE_READONLY;
dmd.size = verity_hdr->data_size * verity_hdr->data_block_size / 512;
+ dmd.uuid = NULL;
- r = device_check_and_adjust(cd, dmd.data_device, DEV_EXCL,
+ r = device_check_and_adjust(cd, dmd.u.verity.data_device, DEV_EXCL,
&dmd.size, &offset, &dmd.flags);
if (r)
return r;
- r = dm_create_verity(name, verity_hdr, &dmd);
+ r = dm_create_device(name, CRYPT_VERITY, &dmd, verity_hdr, 0);
if (!r && !(dm_flags() & DM_VERITY_SUPPORTED)) {
log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
return -ENOTSUP;