Add verity uuid specification.
[platform/upstream/cryptsetup.git] / lib / setup.c
index 115d558..bcd22df 100644 (file)
@@ -67,9 +67,9 @@ struct crypt_device {
 
        /* used in CRYPT_VERITY */
        struct crypt_params_verity verity_hdr;
-       uint32_t verity_flags;
        char *verity_root_hash;
        uint64_t verity_root_hash_size;
+       char *verity_uuid;
 
        /* callbacks definitions */
        void (*log)(int level, const char *msg, void *usrptr);
@@ -305,8 +305,8 @@ int PLAIN_activate(struct crypt_device *cd,
                .uuid   = crypt_get_uuid(cd),
                .size   = size,
                .flags  = flags,
+               .data_device = crypt_get_device_name(cd),
                .u.crypt  = {
-                       .device = crypt_get_device_name(cd),
                        .cipher = NULL,
                        .vk     = vk,
                        .offset = crypt_get_data_offset(cd),
@@ -319,7 +319,7 @@ int PLAIN_activate(struct crypt_device *cd,
        else
                device_check = DEV_EXCL;
 
-       r = device_check_and_adjust(cd, dmd.u.crypt.device, device_check,
+       r = device_check_and_adjust(cd, dmd.data_device, device_check,
                                    &dmd.size, &dmd.u.crypt.offset, &dmd.flags);
        if (r)
                return r;
@@ -655,15 +655,19 @@ static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verit
        if (r < 0)
                return r;
 
+       if (params->flags & CRYPT_VERITY_NO_HEADER)
+               return -EINVAL;
+
        if (params)
                sb_offset = params->hash_area_offset;
 
-       r = VERITY_read_sb(cd, mdata_device(cd), sb_offset, &cd->verity_hdr);
+       r = VERITY_read_sb(cd, mdata_device(cd), sb_offset,
+                          &cd->verity_uuid, &cd->verity_hdr);
        if (r < 0)
                return r;
 
        if (params)
-               cd->verity_flags = params->flags;
+               cd->verity_hdr.flags = params->flags;
 
        if (params && params->data_device &&
            (r = crypt_set_data_device(cd, params->data_device)) < 0)
@@ -677,15 +681,124 @@ static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verit
        return r;
 }
 
+static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
+{
+       struct crypt_dm_active_device dmd = {};
+       char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
+       int key_nums, r;
+
+       r = dm_query_device(name, DM_ACTIVE_DEVICE |
+                                  DM_ACTIVE_UUID |
+                                  DM_ACTIVE_CRYPT_CIPHER |
+                                  DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
+       if (r < 0)
+               goto out;
+
+       if (isPLAIN(cd->type)) {
+               cd->plain_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
+               cd->plain_hdr.hash = NULL; /* no way to get this */
+               cd->plain_hdr.offset = dmd.u.crypt.offset;
+               cd->plain_hdr.skip = dmd.u.crypt.iv_offset;
+               cd->plain_key_size = dmd.u.crypt.vk->keylength;
+
+               r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
+               if (!r) {
+                       cd->plain_cipher = strdup(cipher);
+                       cd->plain_cipher_mode = strdup(cipher_mode);
+               }
+       } else if (isLOOPAES(cd->type)) {
+               cd->loopaes_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
+               cd->loopaes_hdr.offset = dmd.u.crypt.offset;
+
+               r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
+                                             &key_nums, cipher_mode);
+               if (!r) {
+                       cd->loopaes_cipher = strdup(cipher);
+                       cd->loopaes_cipher_mode = strdup(cipher_mode);
+                       /* version 3 uses last key for IV */
+                       if (dmd.u.crypt.vk->keylength % key_nums)
+                               key_nums++;
+                       cd->loopaes_key_size = dmd.u.crypt.vk->keylength / key_nums;
+               }
+       } else if (isLUKS(cd->type)) {
+               if (mdata_device(cd)) {
+                       r = _crypt_load_luks1(cd, 0, 0);
+                       if (r < 0) {
+                               log_dbg("LUKS device header does not match active device.");
+                               free(cd->type);
+                               cd->type = NULL;
+                               r = 0;
+                               goto out;
+                       }
+                       /* check whether UUIDs match each other */
+                       r = crypt_uuid_cmp(dmd.uuid, cd->hdr.uuid);
+                       if (r < 0) {
+                               log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
+                                       cd->hdr.uuid, dmd.uuid);
+                               free(cd->type);
+                               cd->type = NULL;
+                               r = 0;
+                               goto out;
+                       }
+               }
+       }
+out:
+       crypt_free_volume_key(dmd.u.crypt.vk);
+       free(CONST_CAST(void*)dmd.u.crypt.cipher);
+       free(CONST_CAST(void*)dmd.data_device);
+       free(CONST_CAST(void*)dmd.uuid);
+       return r;
+}
+
+static int _init_by_name_verity(struct crypt_device *cd, const char *name)
+{
+       struct crypt_params_verity params = {};
+       struct crypt_dm_active_device dmd = {
+               .target = DM_VERITY,
+               .u.verity.vp = &params,
+       };
+       int r;
+
+       r = dm_query_device(name, DM_ACTIVE_DEVICE |
+                                  DM_ACTIVE_UUID |
+                                  DM_ACTIVE_VERITY_HASH_DEVICE |
+                                  DM_ACTIVE_VERITY_PARAMS, &dmd);
+       if (r < 0)
+               goto out;
+
+       if (isVERITY(cd->type)) {
+               cd->verity_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
+               cd->verity_hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
+               cd->verity_hdr.data_size = params.data_size;
+               cd->verity_root_hash_size = dmd.u.verity.root_hash_size;
+               cd->verity_root_hash = NULL;
+               cd->verity_hdr.hash_name = params.hash_name;
+               cd->verity_hdr.data_device = NULL;
+               cd->verity_hdr.hash_device = NULL;
+               cd->verity_hdr.data_block_size = params.data_block_size;
+               cd->verity_hdr.hash_block_size = params.hash_block_size;
+               cd->verity_hdr.hash_area_offset = dmd.u.verity.hash_offset;
+               cd->verity_hdr.hash_type = params.hash_type;
+               cd->verity_hdr.flags = params.flags;
+               cd->verity_hdr.salt_size = params.salt_size;
+               cd->verity_hdr.salt = params.salt;
+               if (!(cd->metadata_device = strdup(dmd.u.verity.hash_device)))
+                       r = -ENOMEM;
+       }
+out:
+       free(CONST_CAST(void*)dmd.u.verity.hash_device);
+       free(CONST_CAST(void*)dmd.data_device);
+       free(CONST_CAST(void*)dmd.uuid);
+       return r;
+}
+
 int crypt_init_by_name_and_header(struct crypt_device **cd,
                                  const char *name,
                                  const char *header_device)
 {
        crypt_status_info ci;
        struct crypt_dm_active_device dmd;
-       char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
-       int key_nums, r;
-
+       int r;
 
        log_dbg("Allocating crypt device context by device %s.", name);
 
@@ -698,8 +811,7 @@ int crypt_init_by_name_and_header(struct crypt_device **cd,
                return -ENODEV;
        }
 
-       r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
-                                 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE, &dmd);
+       r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
        if (r < 0)
                goto out;
 
@@ -708,17 +820,17 @@ int crypt_init_by_name_and_header(struct crypt_device **cd,
        if (header_device) {
                r = crypt_init(cd, header_device);
        } else {
-               r = crypt_init(cd, dmd.u.crypt.device);
+               r = crypt_init(cd, dmd.data_device);
 
                /* Underlying device disappeared but mapping still active */
-               if (!dmd.u.crypt.device || r == -ENOTBLK)
+               if (!dmd.data_device || r == -ENOTBLK)
                        log_verbose(NULL, _("Underlying device for crypt device %s disappeared.\n"),
                                    name);
 
                /* Underlying device is not readable but crypt mapping exists */
                if (r == -ENOTBLK) {
-                       free(CONST_CAST(void*)dmd.u.crypt.device);
-                       dmd.u.crypt.device = NULL;
+                       free(CONST_CAST(void*)dmd.data_device);
+                       dmd.data_device = NULL;
                        r = crypt_init(cd, NULL);
                }
        }
@@ -733,83 +845,38 @@ int crypt_init_by_name_and_header(struct crypt_device **cd,
                        (*cd)->type = strdup(CRYPT_LOOPAES);
                else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
                        (*cd)->type = strdup(CRYPT_LUKS1);
+               else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
+                       (*cd)->type = strdup(CRYPT_VERITY);
                else
                        log_dbg("Unknown UUID set, some parameters are not set.");
        } else
                log_dbg("Active device has no UUID set, some parameters are not set.");
 
        if (header_device) {
-               r = crypt_set_data_device(*cd, dmd.u.crypt.device);
+               r = crypt_set_data_device(*cd, dmd.data_device);
                if (r < 0)
                        goto out;
        }
 
        /* Try to initialise basic parameters from active device */
 
-       if (!(*cd)->backing_file && dmd.u.crypt.device &&
-           crypt_loop_device(dmd.u.crypt.device) &&
-           !((*cd)->backing_file = crypt_loop_backing_file(dmd.u.crypt.device))) {
+       if (!(*cd)->backing_file && dmd.data_device &&
+           crypt_loop_device(dmd.data_device) &&
+           !((*cd)->backing_file = crypt_loop_backing_file(dmd.data_device))) {
                r = -ENOMEM;
                goto out;
        }
 
-       if (isPLAIN((*cd)->type)) {
-               (*cd)->plain_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
-               (*cd)->plain_hdr.hash = NULL; /* no way to get this */
-               (*cd)->plain_hdr.offset = dmd.u.crypt.offset;
-               (*cd)->plain_hdr.skip = dmd.u.crypt.iv_offset;
-               (*cd)->plain_key_size = dmd.u.crypt.vk->keylength;
-
-               r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher, NULL, cipher_mode);
-               if (!r) {
-                       (*cd)->plain_cipher = strdup(cipher);
-                       (*cd)->plain_cipher_mode = strdup(cipher_mode);
-               }
-       } else if (isLOOPAES((*cd)->type)) {
-               (*cd)->loopaes_uuid = dmd.uuid ? strdup(dmd.uuid) : NULL;
-               (*cd)->loopaes_hdr.offset = dmd.u.crypt.offset;
-
-               r = crypt_parse_name_and_mode(dmd.u.crypt.cipher, cipher,
-                                             &key_nums, cipher_mode);
-               if (!r) {
-                       (*cd)->loopaes_cipher = strdup(cipher);
-                       (*cd)->loopaes_cipher_mode = strdup(cipher_mode);
-                       /* version 3 uses last key for IV */
-                       if (dmd.u.crypt.vk->keylength % key_nums)
-                               key_nums++;
-                       (*cd)->loopaes_key_size = dmd.u.crypt.vk->keylength / key_nums;
-               }
-       } else if (isLUKS((*cd)->type)) {
-               if (mdata_device(*cd)) {
-                       r = _crypt_load_luks1(*cd, 0, 0);
-                       if (r < 0) {
-                               log_dbg("LUKS device header does not match active device.");
-                               free((*cd)->type);
-                               (*cd)->type = NULL;
-                               r = 0;
-                               goto out;
-                       }
-                       /* checks whether UUIDs match each other */
-                       r = crypt_uuid_cmp(dmd.uuid, (*cd)->hdr.uuid);
-                       if (r < 0) {
-                               log_dbg("LUKS device header uuid: %s mismatches DM returned uuid %s",
-                                       (*cd)->hdr.uuid, dmd.uuid);
-                               free((*cd)->type);
-                               (*cd)->type = NULL;
-                               r = 0;
-                               goto out;
-                       }
-               }
-       }
-
+       if (dmd.target == DM_CRYPT)
+               r = _init_by_name_crypt(*cd, name);
+       else if (dmd.target == DM_VERITY)
+               r = _init_by_name_verity(*cd, name);
 out:
        if (r < 0) {
                crypt_free(*cd);
                *cd = NULL;
        }
-       crypt_free_volume_key(dmd.u.crypt.vk);
-       free(CONST_CAST(void*)dmd.u.crypt.device);
-       free(CONST_CAST(void*)dmd.u.crypt.cipher);
+       free(CONST_CAST(void*)dmd.data_device);
        free(CONST_CAST(void*)dmd.uuid);
        return r;
 }
@@ -958,6 +1025,7 @@ static int _crypt_format_loopaes(struct crypt_device *cd,
 }
 
 static int _crypt_format_verity(struct crypt_device *cd,
+                                const char *uuid,
                                 struct crypt_params_verity *params)
 {
        int r = 0;
@@ -971,10 +1039,7 @@ static int _crypt_format_verity(struct crypt_device *cd,
        if (!params || !params->data_device)
                return -EINVAL;
 
-       if (params->version > 1)
-               return -EINVAL;
-
-       /* set dat device */
+       /* set data device */
        cd->type = CRYPT_VERITY;
        r = crypt_set_data_device(cd, params->data_device);
        cd->type = NULL;
@@ -989,22 +1054,21 @@ static int _crypt_format_verity(struct crypt_device *cd,
        } else
                cd->verity_hdr.data_size = params->data_size;
 
-
        cd->verity_root_hash_size = crypt_hash_size(params->hash_name);
        if (!cd->verity_root_hash_size)
                return -EINVAL;
 
-       cd->verity_flags = params->flags;
        cd->verity_root_hash = malloc(cd->verity_root_hash_size);
        if (!cd->verity_root_hash)
                return -ENOMEM;
 
+       cd->verity_hdr.flags = params->flags;
        cd->verity_hdr.hash_name = strdup(params->hash_name);
        cd->verity_hdr.data_device = NULL;
        cd->verity_hdr.data_block_size = params->data_block_size;
        cd->verity_hdr.hash_block_size = params->hash_block_size;
        cd->verity_hdr.hash_area_offset = params->hash_area_offset;
-       cd->verity_hdr.version = params->version;
+       cd->verity_hdr.hash_type = params->hash_type;
        cd->verity_hdr.flags = params->flags;
        cd->verity_hdr.salt_size = params->salt_size;
        cd->verity_hdr.salt = malloc(params->salt_size);
@@ -1015,24 +1079,29 @@ static int _crypt_format_verity(struct crypt_device *cd,
                r = crypt_random_get(cd, CONST_CAST(char*)cd->verity_hdr.salt,
                                     params->salt_size, CRYPT_RND_SALT);
        if (r)
-               goto out;
-
-       log_dbg("Creating verity hash on device %s.", mdata_device(cd));
-       r = VERITY_create(cd, &cd->verity_hdr, cd->device, mdata_device(cd),
-                         cd->verity_root_hash, cd->verity_root_hash_size);
-       if (r)
-               goto out;
+               return r;
 
-       r = VERITY_write_sb(cd, mdata_device(cd),
-                           cd->verity_hdr.hash_area_offset,
-                           &cd->verity_hdr);
-out:
-       if (r) {
-               free(cd->verity_root_hash);
-               free(CONST_CAST(char*)cd->verity_hdr.hash_name);
-               free(CONST_CAST(char*)cd->verity_hdr.salt);
+       if (params->flags & CRYPT_VERITY_CREATE_HASH) {
+               r = VERITY_create(cd, &cd->verity_hdr, cd->device, mdata_device(cd),
+                                 cd->verity_root_hash, cd->verity_root_hash_size);
+               if (r)
+                       return r;
        }
 
+       if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
+               if (uuid)
+                       cd->verity_uuid = strdup(uuid);
+               else {
+                       r = VERITY_UUID_generate(cd, &cd->verity_uuid);
+                       if (r)
+                               return r;
+               }
+
+               r = VERITY_write_sb(cd, mdata_device(cd),
+                                   cd->verity_hdr.hash_area_offset,
+                                   cd->verity_uuid,
+                                   &cd->verity_hdr);
+       }
        return r;
 }
 
@@ -1070,7 +1139,7 @@ int crypt_format(struct crypt_device *cd,
        else if (isLOOPAES(type))
                r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
        else if (isVERITY(type))
-               r = _crypt_format_verity(cd, params);
+               r = _crypt_format_verity(cd, uuid, params);
        else {
                /* FIXME: allow plugins here? */
                log_err(cd, _("Unknown crypt device type %s requested.\n"), type);
@@ -1171,20 +1240,20 @@ int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
 
        log_dbg("Resizing device %s to %" PRIu64 " sectors.", name, new_size);
 
-       r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CIPHER |
-                                 DM_ACTIVE_UUID | DM_ACTIVE_KEYSIZE |
-                                 DM_ACTIVE_KEY, &dmd);
+       r = dm_query_device(name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
+                                 DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
+                                 DM_ACTIVE_CRYPT_KEY, &dmd);
        if (r < 0) {
                log_err(NULL, _("Device %s is not active.\n"), name);
-               goto out;
+               return -EINVAL;
        }
 
-       if (!dmd.uuid) {
+       if (!dmd.uuid || dmd.target != DM_CRYPT) {
                r = -EINVAL;
                goto out;
        }
 
-       r = device_check_and_adjust(cd, dmd.u.crypt.device, DEV_OK, &new_size,
+       r = device_check_and_adjust(cd, dmd.data_device, DEV_OK, &new_size,
                                    &dmd.u.crypt.offset, &dmd.flags);
        if (r)
                goto out;
@@ -1198,9 +1267,11 @@ int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
                r = dm_create_device(name, cd->type, &dmd, 1);
        }
 out:
-       crypt_free_volume_key(dmd.u.crypt.vk);
-       free(CONST_CAST(void*)dmd.u.crypt.cipher);
-       free(CONST_CAST(void*)dmd.u.crypt.device);
+       if (dmd.target == DM_CRYPT) {
+               crypt_free_volume_key(dmd.u.crypt.vk);
+               free(CONST_CAST(void*)dmd.u.crypt.cipher);
+       }
+       free(CONST_CAST(void*)dmd.data_device);
        free(CONST_CAST(void*)dmd.uuid);
 
        return r;
@@ -1258,7 +1329,6 @@ int crypt_header_restore(struct crypt_device *cd,
        if (requested_type && !isLUKS(requested_type))
                return -EINVAL;
 
-       /* Some hash functions need initialized gcrypt library */
        r = init_crypto(cd);
        if (r < 0)
                return r;
@@ -1300,6 +1370,7 @@ void crypt_free(struct crypt_device *cd)
                free(CONST_CAST(void*)cd->verity_hdr.hash_name);
                free(CONST_CAST(void*)cd->verity_hdr.salt);
                free(cd->verity_root_hash);
+               free(cd->verity_uuid);
 
                free(cd);
        }
@@ -1877,7 +1948,7 @@ int crypt_activate_by_volume_key(struct crypt_device *cd,
        struct volume_key *vk = NULL;
        int r = -EINVAL;
 
-       log_dbg("Activating volume %s by volume key.", name);
+       log_dbg("Activating volume %s by volume key.", name ?: "[none]");
 
        if (name) {
                ci = crypt_status(NULL, name);
@@ -1934,7 +2005,7 @@ int crypt_activate_by_volume_key(struct crypt_device *cd,
 
                r = VERITY_activate(cd, name, mdata_device(cd),
                                    volume_key, volume_key_size,
-                                   &cd->verity_hdr, cd->verity_flags);
+                                   &cd->verity_hdr, CRYPT_ACTIVATE_READONLY);
 
                if (r == -EPERM) {
                        free(cd->verity_root_hash);
@@ -2186,7 +2257,8 @@ static int _luks_dump(struct crypt_device *cd)
 static int _verity_dump(struct crypt_device *cd)
 {
        log_std(cd, "VERITY header information for %s\n", mdata_device(cd));
-       log_std(cd, "Version:         \t%u\n", cd->verity_hdr.version);
+       log_std(cd, "UUID:            \t%s\n", cd->verity_uuid ?: "");
+       log_std(cd, "Hash type:       \t%u\n", cd->verity_hdr.hash_type);
        log_std(cd, "Data blocks:     \t%" PRIu64 "\n", cd->verity_hdr.data_size);
        log_std(cd, "Data block size: \t%u\n", cd->verity_hdr.data_block_size);
        log_std(cd, "Hash block size: \t%u\n", cd->verity_hdr.hash_block_size);
@@ -2255,6 +2327,9 @@ const char *crypt_get_uuid(struct crypt_device *cd)
        if (isLOOPAES(cd->type))
                return cd->loopaes_uuid;
 
+       if (isVERITY(cd->type))
+               return cd->verity_uuid;
+
        return NULL;
 }
 
@@ -2331,6 +2406,26 @@ const char *crypt_get_type(struct crypt_device *cd)
        return cd->type;
 }
 
+int crypt_get_verity_info(struct crypt_device *cd,
+       struct crypt_params_verity *vp)
+{
+       if (!isVERITY(cd->type) || !vp)
+               return -EINVAL;
+
+       vp->data_device = cd->device;
+       vp->hash_device = mdata_device(cd);
+       vp->hash_name = cd->verity_hdr.hash_name;
+       vp->salt = cd->verity_hdr.salt;
+       vp->salt_size = cd->verity_hdr.salt_size;
+       vp->data_block_size = cd->verity_hdr.data_block_size;
+       vp->hash_block_size = cd->verity_hdr.hash_block_size;
+       vp->data_size = cd->verity_hdr.data_size;
+       vp->hash_area_offset = cd->verity_hdr.hash_area_offset;
+       vp->hash_type = cd->verity_hdr.hash_type;
+       vp->flags = cd->verity_hdr.flags & CRYPT_VERITY_NO_HEADER;
+       return 0;
+}
+
 int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
                            const char *name,
                            struct crypt_active_device *cad)
@@ -2342,6 +2437,9 @@ int crypt_get_active_device(struct crypt_device *cd __attribute__((unused)),
        if (r < 0)
                return r;
 
+       if (dmd.target != DM_CRYPT && dmd.target != DM_VERITY)
+               return -ENOTSUP;
+
        cad->offset     = dmd.u.crypt.offset;
        cad->iv_offset  = dmd.u.crypt.iv_offset;
        cad->size       = dmd.size;