Merge branch 'upstream' into tizen
[platform/upstream/cryptsetup.git] / lib / verity / verity.c
index 5e0860e..0d7a8f5 100644 (file)
@@ -1,19 +1,20 @@
 /*
  * dm-verity volume handling
  *
- * Copyright (C) 2012, Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved.
  *
- * 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.
+ * This file is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
+ * This file is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <stdlib.h>
 #include <string.h>
 #include <stdint.h>
+#include <ctype.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <fcntl.h>
-#include <netinet/in.h>
+#include <uuid/uuid.h>
 
 #include "libcryptsetup.h"
 #include "verity.h"
 #include "internal.h"
 
-#define NEW_SB 1
+#define VERITY_SIGNATURE "verity\0\0"
 
-#ifndef NEW_SB
-struct verity_sb {
-       uint8_t signature[8];
-       uint8_t version;
-       uint8_t data_block_bits;
-       uint8_t hash_block_bits;
-       uint8_t pad1[1];
-       uint16_t salt_size;
-       uint8_t pad2[2];
-       uint32_t data_blocks_hi;
-       uint32_t data_blocks_lo;
-       uint8_t algorithm[16];
-       uint8_t salt[384];
-       uint8_t pad3[88];
-};
-
-#else
+/* https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity#verity-superblock-format */
 struct verity_sb {
        uint8_t  signature[8];  /* "verity\0\0" */
        uint32_t version;       /* superblock version */
        uint32_t hash_type;     /* 0 - Chrome OS, 1 - normal */
        uint8_t  uuid[16];      /* UUID of hash device */
        uint8_t  algorithm[32];/* hash algorithm name */
-       uint64_t data_block_size; /* data block in bytes */
-       uint64_t hash_block_size; /* hash block in bytes */
+       uint32_t data_block_size; /* data block in bytes */
+       uint32_t hash_block_size; /* hash block in bytes */
        uint64_t data_blocks;   /* number of data blocks */
-       uint64_t salt_size;     /* salt size */
+       uint16_t salt_size;     /* salt size */
+       uint8_t  _pad1[6];
        uint8_t  salt[256];     /* salt */
-       uint8_t  _pad[160];
+       uint8_t  _pad2[168];
 } __attribute__((packed));
-#endif
 
 /* Read verity superblock from disk */
 int VERITY_read_sb(struct crypt_device *cd,
-                  const char *device,
                   uint64_t sb_offset,
+                  char **uuid_string,
                   struct crypt_params_verity *params)
 {
+       struct device *device = crypt_metadata_device(cd);
        struct verity_sb sb = {};
        ssize_t hdr_size = sizeof(struct verity_sb);
-       int devfd = 0, sb_version;
-       uint64_t sb_data_blocks;
+       int devfd, sb_version;
 
-       log_dbg("Reading VERITY header of size %u on device %s, offset %" PRIu64 ".",
-               sizeof(struct verity_sb), device, sb_offset);
+       log_dbg(cd, "Reading VERITY header of size %zu on device %s, offset %" PRIu64 ".",
+               sizeof(struct verity_sb), device_path(device), sb_offset);
 
        if (params->flags & CRYPT_VERITY_NO_HEADER) {
-               log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
+               log_err(cd, _("Verity device %s does not use on-disk header."),
+                       device_path(device));
                return -EINVAL;
        }
 
-       devfd = open(device ,O_RDONLY | O_DIRECT);
-       if(devfd == -1) {
-               log_err(cd, _("Cannot open device %s.\n"), device);
+       if (MISALIGNED_512(sb_offset)) {
+               log_err(cd, _("Unsupported VERITY hash offset."));
                return -EINVAL;
        }
 
-       if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
-          read_blockwise(devfd, &sb, hdr_size) < hdr_size) {
-               close(devfd);
-               return -EIO;
-       }
-       close(devfd);
-
-       if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
-               log_err(cd, _("Device %s is not a valid VERITY device.\n"), device);
-               return -EINVAL;
-       }
-#ifndef NEW_SB
-       if (sb.version > 1) {
-               log_err(cd, _("Unsupported VERITY version %d.\n"), sb.version);
+       devfd = device_open(cd, device, O_RDONLY);
+       if (devfd < 0) {
+               log_err(cd, _("Cannot open device %s."), device_path(device));
                return -EINVAL;
        }
 
-       if (sb.data_block_bits < 9 || sb.data_block_bits >= 31 ||
-           sb.hash_block_bits < 9 || sb.hash_block_bits >= 31 ||
-           !memchr(sb.algorithm, 0, sizeof(sb.algorithm)) ||
-           ntohs(sb.salt_size) > 256) {
-               log_err(cd, _("VERITY header corrupted.\n"));
+       if (read_lseek_blockwise(devfd, device_block_size(cd, device),
+                                device_alignment(device), &sb, hdr_size,
+                                sb_offset) < hdr_size)
+               return -EIO;
+
+       if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
+               log_dbg(cd, "No VERITY signature detected.");
                return -EINVAL;
        }
 
-       sb_data_blocks = ((uint64_t)ntohl(sb.data_blocks_hi) << 31 << 1) |
-                               ntohl(sb.data_blocks_lo);
-
-       params->hash_name = strdup((const char*)sb.algorithm);
-       if (!params->hash_name)
-               return -ENOMEM;
-       params->data_block_size = 1 << sb.data_block_bits;
-       params->hash_block_size = 1 << sb.hash_block_bits;
-       params->data_size = sb_data_blocks;
-       params->salt_size = ntohs(sb.salt_size);
-       params->salt = malloc(params->salt_size);
-       if (!params->salt)
-               return -ENOMEM;
-       memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
-       params->hash_type = sb.version;
-#else
        sb_version = le32_to_cpu(sb.version);
        if (sb_version != 1) {
-               log_err(cd, _("Unsupported VERITY version %d.\n"), sb_version);
+               log_err(cd, _("Unsupported VERITY version %d."), sb_version);
                return -EINVAL;
        }
        params->hash_type = le32_to_cpu(sb.hash_type);
-       if (params->hash_type > 1) {
-               log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
+       if (params->hash_type > VERITY_MAX_HASH_TYPE) {
+               log_err(cd, _("Unsupported VERITY hash type %d."), params->hash_type);
                return -EINVAL;
        }
 
-       params->data_block_size = le64_to_cpu(sb.data_block_size);
-       params->hash_block_size = le64_to_cpu(sb.hash_block_size);
-       if (params->data_block_size % 512 || params->hash_block_size % 512) {
-               log_err(cd, _("Unsupported VERITY block size.\n"));
+       params->data_block_size = le32_to_cpu(sb.data_block_size);
+       params->hash_block_size = le32_to_cpu(sb.hash_block_size);
+       if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
+           VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
+               log_err(cd, _("Unsupported VERITY block size."));
                return -EINVAL;
        }
        params->data_size = le64_to_cpu(sb.data_blocks);
 
+       /* Update block size to be used for loop devices */
+       device_set_block_size(crypt_metadata_device(cd), params->hash_block_size);
+       device_set_block_size(crypt_data_device(cd), params->data_block_size);
+
        params->hash_name = strndup((const char*)sb.algorithm, sizeof(sb.algorithm));
        if (!params->hash_name)
                return -ENOMEM;
+       if (crypt_hash_size(params->hash_name) <= 0) {
+               log_err(cd, _("Hash algorithm %s not supported."),
+                       params->hash_name);
+               free(CONST_CAST(char*)params->hash_name);
+               params->hash_name = NULL;
+               return -EINVAL;
+       }
 
-       params->salt_size = le64_to_cpu(sb.salt_size);
+       params->salt_size = le16_to_cpu(sb.salt_size);
        if (params->salt_size > sizeof(sb.salt)) {
-               log_err(cd, _("VERITY header corrupted.\n"));
+               log_err(cd, _("VERITY header corrupted."));
                free(CONST_CAST(char*)params->hash_name);
+               params->hash_name = NULL;
                return -EINVAL;
        }
        params->salt = malloc(params->salt_size);
        if (!params->salt) {
                free(CONST_CAST(char*)params->hash_name);
+               params->hash_name = NULL;
                return -ENOMEM;
        }
        memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
 
-#endif
+       if ((*uuid_string = malloc(40)))
+               uuid_unparse(sb.uuid, *uuid_string);
+
        params->hash_area_offset = sb_offset;
        return 0;
 }
 
+static void _to_lower(char *str)
+{
+       for(; *str; str++)
+               if (isupper(*str))
+                       *str = tolower(*str);
+}
+
 /* Write verity superblock to disk */
 int VERITY_write_sb(struct crypt_device *cd,
-                  const char *device,
                   uint64_t sb_offset,
+                  const char *uuid_string,
                   struct crypt_params_verity *params)
 {
+       struct device *device = crypt_metadata_device(cd);
        struct verity_sb sb = {};
        ssize_t hdr_size = sizeof(struct verity_sb);
-       int r, devfd = 0;
+       size_t block_size;
+       char *algorithm;
+       uuid_t uuid;
+       int r, devfd;
 
-       log_dbg("Updating VERITY header of size %u on device %s, offset %" PRIu64 ".",
-               sizeof(struct verity_sb), device, sb_offset);
+       log_dbg(cd, "Updating VERITY header of size %zu on device %s, offset %" PRIu64 ".",
+               sizeof(struct verity_sb), device_path(device), sb_offset);
+
+       if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
+               log_err(cd, _("Wrong VERITY UUID format provided on device %s."),
+                       device_path(device));
+               return -EINVAL;
+       }
 
        if (params->flags & CRYPT_VERITY_NO_HEADER) {
-               log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
+               log_err(cd, _("Verity device %s does not use on-disk header."),
+                       device_path(device));
                return -EINVAL;
        }
 
-       devfd = open(device, O_RDWR | O_DIRECT);
-       if(devfd == -1) {
-               log_err(cd, _("Cannot open device %s.\n"), device);
+       /* Avoid possible increasing of image size - FEC could fail later because of it */
+       block_size = device_block_size(cd, device);
+       if (block_size > params->hash_block_size) {
+               device_disable_direct_io(device);
+               block_size = params->hash_block_size;
+       }
+
+       devfd = device_open(cd, device, O_RDWR);
+       if (devfd < 0) {
+               log_err(cd, _("Cannot open device %s."), device_path(device));
                return -EINVAL;
        }
 
        memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
-#ifndef NEW_SB
-       sb.version = params->hash_type;
-       sb.data_block_bits = ffs(params->data_block_size) - 1;
-       sb.hash_block_bits = ffs(params->hash_block_size) - 1;
-       sb.salt_size = htons(params->salt_size);
-       sb.data_blocks_hi = htonl(params->data_size >> 31 >> 1);
-       sb.data_blocks_lo = htonl(params->data_size & 0xFFFFFFFF);
-       strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
-       memcpy(sb.salt, params->salt, params->salt_size);
-#else
        sb.version         = cpu_to_le32(1);
        sb.hash_type       = cpu_to_le32(params->hash_type);
-       sb.data_block_size = cpu_to_le64(params->data_block_size);
-       sb.hash_block_size = cpu_to_le64(params->hash_block_size);
-       sb.salt_size       = cpu_to_le64(params->salt_size);
+       sb.data_block_size = cpu_to_le32(params->data_block_size);
+       sb.hash_block_size = cpu_to_le32(params->hash_block_size);
+       sb.salt_size       = cpu_to_le16(params->salt_size);
        sb.data_blocks     = cpu_to_le64(params->data_size);
-       strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
+
+       /* Kernel always use lower-case */
+       algorithm = (char *)sb.algorithm;
+       strncpy(algorithm, params->hash_name, sizeof(sb.algorithm)-1);
+       algorithm[sizeof(sb.algorithm)-1] = '\0';
+       _to_lower(algorithm);
+
        memcpy(sb.salt, params->salt, params->salt_size);
-#endif
-       r = write_lseek_blockwise(devfd, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
+       memcpy(sb.uuid, uuid, sizeof(sb.uuid));
+
+       r = write_lseek_blockwise(devfd, block_size, device_alignment(device),
+                                 (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
        if (r)
-               log_err(cd, _("Error during update of verity header on device %s.\n"), device);
-       close(devfd);
+               log_err(cd, _("Error during update of verity header on device %s."),
+                       device_path(device));
+
+       device_sync(cd, device);
 
        return r;
 }
@@ -238,26 +239,64 @@ uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
        return hash_offset / params->hash_block_size;
 }
 
+int VERITY_UUID_generate(char **uuid_string)
+{
+       uuid_t uuid;
+
+       *uuid_string = malloc(40);
+       if (!*uuid_string)
+               return -ENOMEM;
+       uuid_generate(uuid);
+       uuid_unparse(uuid, *uuid_string);
+       return 0;
+}
+
 /* Activate verity device in kernel device-mapper */
 int VERITY_activate(struct crypt_device *cd,
                     const char *name,
-                    const char *hash_device,
                     const char *root_hash,
                     size_t root_hash_size,
+                    const char *signature_description,
+                    struct device *fec_device,
                     struct crypt_params_verity *verity_hdr,
                     uint32_t activation_flags)
 {
-       struct crypt_dm_active_device dmd;
-       uint64_t offset = 0;
-       int r;
-
-       log_dbg("Trying to activate VERITY device %s using hash %s.",
+       uint32_t dmv_flags;
+       unsigned int fec_errors = 0;
+       int r, v;
+       struct crypt_dm_active_device dmd = {
+               .size = verity_hdr->data_size * verity_hdr->data_block_size / 512,
+               .flags = activation_flags,
+               .uuid = crypt_get_uuid(cd),
+       };
+
+       log_dbg(cd, "Trying to activate VERITY device %s using hash %s.",
                name ?: "[none]", verity_hdr->hash_name);
 
        if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
-               r = VERITY_verify(cd, verity_hdr,
-                                 crypt_get_device_name(cd), hash_device,
-                                 root_hash, root_hash_size);
+               if (signature_description) {
+                       log_err(cd, _("Root hash signature verification is not supported."));
+                       return -EINVAL;
+               }
+
+               log_dbg(cd, "Verification of data in userspace required.");
+               r = VERITY_verify(cd, verity_hdr, root_hash, root_hash_size);
+
+               if ((r == -EPERM || r == -EFAULT) && fec_device) {
+                       v = r;
+                       log_dbg(cd, "Verification failed, trying to repair with FEC device.");
+                       r = VERITY_FEC_process(cd, verity_hdr, fec_device, 1, &fec_errors);
+                       if (r < 0)
+                               log_err(cd, _("Errors cannot be repaired with FEC device."));
+                       else if (fec_errors) {
+                               log_err(cd, _("Found %u repairable errors with FEC device."),
+                                       fec_errors);
+                               /* If root hash failed, we cannot be sure it was properly repaired */
+                       }
+                       if (v == -EFAULT)
+                               r = -EPERM;
+               }
+
                if (r < 0)
                        return r;
        }
@@ -265,35 +304,113 @@ int VERITY_activate(struct crypt_device *cd,
        if (!name)
                return 0;
 
-       dmd.target = DM_VERITY;
-       dmd.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 = activation_flags;
-       dmd.size = verity_hdr->data_size * verity_hdr->data_block_size / 512;
-       dmd.uuid = NULL;
-       dmd.u.verity.vp = verity_hdr;
-
-       r = device_check_and_adjust(cd, dmd.data_device, DEV_EXCL,
-                                   &dmd.size, &offset, &dmd.flags);
+       r = device_block_adjust(cd, crypt_metadata_device(cd), DEV_OK,
+                               0, NULL, NULL);
        if (r)
                return r;
 
-       r = dm_create_device(name, CRYPT_VERITY, &dmd, 0);
-       if (!r && !(dm_flags() & DM_VERITY_SUPPORTED)) {
-               log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
-               return -ENOTSUP;
+       r = device_block_adjust(cd, crypt_data_device(cd), DEV_EXCL,
+                               0, &dmd.size, &dmd.flags);
+       if (r)
+               return r;
+
+       if (fec_device) {
+               r = device_block_adjust(cd, fec_device, DEV_OK,
+                                       0, NULL, NULL);
+               if (r)
+                       return r;
        }
-       if (r < 0)
+
+       r = dm_verity_target_set(&dmd.segment, 0, dmd.size, crypt_data_device(cd),
+                       crypt_metadata_device(cd), fec_device, root_hash,
+                       root_hash_size, signature_description,
+                       VERITY_hash_offset_block(verity_hdr),
+                       VERITY_FEC_blocks(cd, fec_device, verity_hdr), verity_hdr);
+
+       if (r)
                return r;
 
-       r = dm_status_verity_ok(name);
+       r = dm_create_device(cd, name, CRYPT_VERITY, &dmd);
+       if (r < 0 && (dm_flags(cd, DM_VERITY, &dmv_flags) || !(dmv_flags & DM_VERITY_SUPPORTED))) {
+               log_err(cd, _("Kernel does not support dm-verity mapping."));
+               r = -ENOTSUP;
+       }
+       if (r < 0 && signature_description && !(dmv_flags & DM_VERITY_SIGNATURE_SUPPORTED)) {
+               log_err(cd, _("Kernel does not support dm-verity signature option."));
+               r = -ENOTSUP;
+       }
+       if (r < 0)
+               goto out;
+
+       r = dm_status_verity_ok(cd, name);
        if (r < 0)
-               return r;
+               goto out;
 
        if (!r)
-               log_err(cd, _("Verity device detected corruption after activation.\n"));
+               log_err(cd, _("Verity device detected corruption after activation."));
+
+       r = 0;
+out:
+       dm_targets_free(cd, &dmd);
+       return r;
+}
+
+int VERITY_dump(struct crypt_device *cd,
+               struct crypt_params_verity *verity_hdr,
+               const char *root_hash,
+               unsigned int root_hash_size,
+               struct device *fec_device)
+{
+       uint64_t hash_blocks, verity_blocks, fec_blocks = 0, rs_blocks = 0;
+       bool fec_on_hash_device = false;
+
+       hash_blocks  = VERITY_hash_blocks(cd, verity_hdr);
+       verity_blocks = VERITY_hash_offset_block(verity_hdr) + hash_blocks;
+
+       if (fec_device && verity_hdr->fec_roots) {
+               fec_blocks = VERITY_FEC_blocks(cd, fec_device, verity_hdr);
+               rs_blocks  = VERITY_FEC_RS_blocks(fec_blocks, verity_hdr->fec_roots);
+               fec_on_hash_device = device_is_identical(crypt_metadata_device(cd), fec_device) > 0;
+               /*
+               * No way to access fec_area_offset directly.
+               * Assume FEC area starts directly after hash blocks.
+               */
+               if (fec_on_hash_device)
+                       verity_blocks += rs_blocks;
+       }
+
+       log_std(cd, "VERITY header information for %s\n", device_path(crypt_metadata_device(cd)));
+       log_std(cd, "UUID:            \t%s\n", crypt_get_uuid(cd) ?: "");
+       log_std(cd, "Hash type:       \t%u\n", verity_hdr->hash_type);
+       log_std(cd, "Data blocks:     \t%" PRIu64 "\n", verity_hdr->data_size);
+       log_std(cd, "Data block size: \t%u\n", verity_hdr->data_block_size);
+       log_std(cd, "Hash blocks:     \t%" PRIu64 "\n", hash_blocks);
+       log_std(cd, "Hash block size: \t%u\n", verity_hdr->hash_block_size);
+       log_std(cd, "Hash algorithm:  \t%s\n", verity_hdr->hash_name);
+       if (fec_device && fec_blocks) {
+               log_std(cd, "FEC RS roots:   \t%" PRIu32 "\n", verity_hdr->fec_roots);
+               log_std(cd, "FEC blocks:     \t%" PRIu64 "\n", rs_blocks);
+       }
+
+       log_std(cd, "Salt:            \t");
+       if (verity_hdr->salt_size)
+               crypt_log_hex(cd, verity_hdr->salt, verity_hdr->salt_size, "", 0, NULL);
+       else
+               log_std(cd, "-");
+       log_std(cd, "\n");
+
+       if (root_hash) {
+               log_std(cd, "Root hash:      \t");
+               crypt_log_hex(cd, root_hash, root_hash_size, "", 0, NULL);
+               log_std(cd, "\n");
+       }
+
+       /* As dump can take only hash device, we have no idea about offsets here. */
+       if (verity_hdr->hash_area_offset == 0)
+               log_std(cd, "Hash device size: \t%" PRIu64 " [bytes]\n", verity_blocks * verity_hdr->hash_block_size);
+
+       if (fec_device && verity_hdr->fec_area_offset == 0 && fec_blocks && !fec_on_hash_device)
+               log_std(cd, "FEC device size: \t%" PRIu64 " [bytes]\n", rs_blocks * verity_hdr->data_block_size);
+
        return 0;
 }