2 * dm-verity volume handling
4 * Copyright (C) 2012-2020 Red Hat, Inc. All rights reserved.
6 * This file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This file is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <uuid/uuid.h>
31 #include "libcryptsetup.h"
35 #define VERITY_SIGNATURE "verity\0\0"
37 /* https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity#verity-superblock-format */
39 uint8_t signature[8]; /* "verity\0\0" */
40 uint32_t version; /* superblock version */
41 uint32_t hash_type; /* 0 - Chrome OS, 1 - normal */
42 uint8_t uuid[16]; /* UUID of hash device */
43 uint8_t algorithm[32];/* hash algorithm name */
44 uint32_t data_block_size; /* data block in bytes */
45 uint32_t hash_block_size; /* hash block in bytes */
46 uint64_t data_blocks; /* number of data blocks */
47 uint16_t salt_size; /* salt size */
49 uint8_t salt[256]; /* salt */
51 } __attribute__((packed));
53 /* Read verity superblock from disk */
54 int VERITY_read_sb(struct crypt_device *cd,
57 struct crypt_params_verity *params)
59 struct device *device = crypt_metadata_device(cd);
60 struct verity_sb sb = {};
61 ssize_t hdr_size = sizeof(struct verity_sb);
62 int devfd, sb_version;
64 log_dbg(cd, "Reading VERITY header of size %zu on device %s, offset %" PRIu64 ".",
65 sizeof(struct verity_sb), device_path(device), sb_offset);
67 if (params->flags & CRYPT_VERITY_NO_HEADER) {
68 log_err(cd, _("Verity device %s does not use on-disk header."),
73 if (MISALIGNED_512(sb_offset)) {
74 log_err(cd, _("Unsupported VERITY hash offset."));
78 devfd = device_open(cd, device, O_RDONLY);
80 log_err(cd, _("Cannot open device %s."), device_path(device));
84 if (read_lseek_blockwise(devfd, device_block_size(cd, device),
85 device_alignment(device), &sb, hdr_size,
86 sb_offset) < hdr_size)
89 if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
90 log_err(cd, _("Device %s is not a valid VERITY device."),
95 sb_version = le32_to_cpu(sb.version);
96 if (sb_version != 1) {
97 log_err(cd, _("Unsupported VERITY version %d."), sb_version);
100 params->hash_type = le32_to_cpu(sb.hash_type);
101 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
102 log_err(cd, _("Unsupported VERITY hash type %d."), params->hash_type);
106 params->data_block_size = le32_to_cpu(sb.data_block_size);
107 params->hash_block_size = le32_to_cpu(sb.hash_block_size);
108 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
109 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
110 log_err(cd, _("Unsupported VERITY block size."));
113 params->data_size = le64_to_cpu(sb.data_blocks);
115 params->hash_name = strndup((const char*)sb.algorithm, sizeof(sb.algorithm));
116 if (!params->hash_name)
118 if (crypt_hash_size(params->hash_name) <= 0) {
119 log_err(cd, _("Hash algorithm %s not supported."),
121 free(CONST_CAST(char*)params->hash_name);
122 params->hash_name = NULL;
126 params->salt_size = le16_to_cpu(sb.salt_size);
127 if (params->salt_size > sizeof(sb.salt)) {
128 log_err(cd, _("VERITY header corrupted."));
129 free(CONST_CAST(char*)params->hash_name);
130 params->hash_name = NULL;
133 params->salt = malloc(params->salt_size);
135 free(CONST_CAST(char*)params->hash_name);
136 params->hash_name = NULL;
139 memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
141 if ((*uuid_string = malloc(40)))
142 uuid_unparse(sb.uuid, *uuid_string);
144 params->hash_area_offset = sb_offset;
148 /* Write verity superblock to disk */
149 int VERITY_write_sb(struct crypt_device *cd,
151 const char *uuid_string,
152 struct crypt_params_verity *params)
154 struct device *device = crypt_metadata_device(cd);
155 struct verity_sb sb = {};
156 ssize_t hdr_size = sizeof(struct verity_sb);
161 log_dbg(cd, "Updating VERITY header of size %zu on device %s, offset %" PRIu64 ".",
162 sizeof(struct verity_sb), device_path(device), sb_offset);
164 if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
165 log_err(cd, _("Wrong VERITY UUID format provided on device %s."),
166 device_path(device));
170 if (params->flags & CRYPT_VERITY_NO_HEADER) {
171 log_err(cd, _("Verity device %s does not use on-disk header."),
172 device_path(device));
176 devfd = device_open(cd, device, O_RDWR);
178 log_err(cd, _("Cannot open device %s."), device_path(device));
182 memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
183 sb.version = cpu_to_le32(1);
184 sb.hash_type = cpu_to_le32(params->hash_type);
185 sb.data_block_size = cpu_to_le32(params->data_block_size);
186 sb.hash_block_size = cpu_to_le32(params->hash_block_size);
187 sb.salt_size = cpu_to_le16(params->salt_size);
188 sb.data_blocks = cpu_to_le64(params->data_size);
189 algorithm = (char *)sb.algorithm;
190 algorithm[sizeof(sb.algorithm)-1] = '\0';
191 strncpy(algorithm, params->hash_name, sizeof(sb.algorithm)-1);
192 memcpy(sb.salt, params->salt, params->salt_size);
193 memcpy(sb.uuid, uuid, sizeof(sb.uuid));
195 r = write_lseek_blockwise(devfd, device_block_size(cd, device), device_alignment(device),
196 (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
198 log_err(cd, _("Error during update of verity header on device %s."),
199 device_path(device));
201 device_sync(cd, device);
206 /* Calculate hash offset in hash blocks */
207 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
209 uint64_t hash_offset = params->hash_area_offset;
211 if (params->flags & CRYPT_VERITY_NO_HEADER)
212 return hash_offset / params->hash_block_size;
214 hash_offset += sizeof(struct verity_sb);
215 hash_offset += params->hash_block_size - 1;
217 return hash_offset / params->hash_block_size;
220 int VERITY_UUID_generate(struct crypt_device *cd, char **uuid_string)
224 *uuid_string = malloc(40);
228 uuid_unparse(uuid, *uuid_string);
232 /* Activate verity device in kernel device-mapper */
233 int VERITY_activate(struct crypt_device *cd,
235 const char *root_hash,
236 size_t root_hash_size,
237 const char *signature_description,
238 struct device *fec_device,
239 struct crypt_params_verity *verity_hdr,
240 uint32_t activation_flags)
243 unsigned int fec_errors = 0;
245 struct crypt_dm_active_device dmd = {
246 .size = verity_hdr->data_size * verity_hdr->data_block_size / 512,
247 .flags = activation_flags,
248 .uuid = crypt_get_uuid(cd),
251 log_dbg(cd, "Trying to activate VERITY device %s using hash %s.",
252 name ?: "[none]", verity_hdr->hash_name);
254 if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
255 if (signature_description) {
256 log_err(cd, _("Root hash signature verification is not supported."));
260 log_dbg(cd, "Verification of data in userspace required.");
261 r = VERITY_verify(cd, verity_hdr, root_hash, root_hash_size);
263 if (r == -EPERM && fec_device) {
264 log_dbg(cd, "Verification failed, trying to repair with FEC device.");
265 r = VERITY_FEC_process(cd, verity_hdr, fec_device, 1, &fec_errors);
267 log_err(cd, _("Errors cannot be repaired with FEC device."));
269 log_err(cd, _("Found %u repairable errors with FEC device."),
280 r = device_block_adjust(cd, crypt_metadata_device(cd), DEV_OK,
285 r = device_block_adjust(cd, crypt_data_device(cd), DEV_EXCL,
286 0, &dmd.size, &dmd.flags);
291 r = device_block_adjust(cd, fec_device, DEV_OK,
297 r = dm_verity_target_set(&dmd.segment, 0, dmd.size, crypt_data_device(cd),
298 crypt_metadata_device(cd), fec_device, root_hash,
299 root_hash_size, signature_description,
300 VERITY_hash_offset_block(verity_hdr),
301 VERITY_hash_blocks(cd, verity_hdr), verity_hdr);
306 r = dm_create_device(cd, name, CRYPT_VERITY, &dmd);
307 if (r < 0 && (dm_flags(cd, DM_VERITY, &dmv_flags) || !(dmv_flags & DM_VERITY_SUPPORTED))) {
308 log_err(cd, _("Kernel does not support dm-verity mapping."));
311 if (r < 0 && signature_description && !(dmv_flags & DM_VERITY_SIGNATURE_SUPPORTED)) {
312 log_err(cd, _("Kernel does not support dm-verity signature option."));
318 r = dm_status_verity_ok(cd, name);
323 log_err(cd, _("Verity device detected corruption after activation."));
327 dm_targets_free(cd, &dmd);