2 * dm-verity volume handling
4 * Copyright (C) 2012-2023 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.
27 #include <sys/types.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_dbg(cd, "No VERITY signature detected.");
94 sb_version = le32_to_cpu(sb.version);
95 if (sb_version != 1) {
96 log_err(cd, _("Unsupported VERITY version %d."), sb_version);
99 params->hash_type = le32_to_cpu(sb.hash_type);
100 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
101 log_err(cd, _("Unsupported VERITY hash type %d."), params->hash_type);
105 params->data_block_size = le32_to_cpu(sb.data_block_size);
106 params->hash_block_size = le32_to_cpu(sb.hash_block_size);
107 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
108 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
109 log_err(cd, _("Unsupported VERITY block size."));
112 params->data_size = le64_to_cpu(sb.data_blocks);
114 /* Update block size to be used for loop devices */
115 device_set_block_size(crypt_metadata_device(cd), params->hash_block_size);
116 device_set_block_size(crypt_data_device(cd), params->data_block_size);
118 params->hash_name = strndup((const char*)sb.algorithm, sizeof(sb.algorithm));
119 if (!params->hash_name)
121 if (crypt_hash_size(params->hash_name) <= 0) {
122 log_err(cd, _("Hash algorithm %s not supported."),
124 free(CONST_CAST(char*)params->hash_name);
125 params->hash_name = NULL;
129 params->salt_size = le16_to_cpu(sb.salt_size);
130 if (params->salt_size > sizeof(sb.salt)) {
131 log_err(cd, _("VERITY header corrupted."));
132 free(CONST_CAST(char*)params->hash_name);
133 params->hash_name = NULL;
136 params->salt = malloc(params->salt_size);
138 free(CONST_CAST(char*)params->hash_name);
139 params->hash_name = NULL;
142 memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
144 if ((*uuid_string = malloc(40)))
145 uuid_unparse(sb.uuid, *uuid_string);
147 params->hash_area_offset = sb_offset;
151 static void _to_lower(char *str)
155 *str = tolower(*str);
158 /* Write verity superblock to disk */
159 int VERITY_write_sb(struct crypt_device *cd,
161 const char *uuid_string,
162 struct crypt_params_verity *params)
164 struct device *device = crypt_metadata_device(cd);
165 struct verity_sb sb = {};
166 ssize_t hdr_size = sizeof(struct verity_sb);
172 log_dbg(cd, "Updating VERITY header of size %zu on device %s, offset %" PRIu64 ".",
173 sizeof(struct verity_sb), device_path(device), sb_offset);
175 if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
176 log_err(cd, _("Wrong VERITY UUID format provided on device %s."),
177 device_path(device));
181 if (params->flags & CRYPT_VERITY_NO_HEADER) {
182 log_err(cd, _("Verity device %s does not use on-disk header."),
183 device_path(device));
187 /* Avoid possible increasing of image size - FEC could fail later because of it */
188 block_size = device_block_size(cd, device);
189 if (block_size > params->hash_block_size) {
190 device_disable_direct_io(device);
191 block_size = params->hash_block_size;
194 devfd = device_open(cd, device, O_RDWR);
196 log_err(cd, _("Cannot open device %s."), device_path(device));
200 memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
201 sb.version = cpu_to_le32(1);
202 sb.hash_type = cpu_to_le32(params->hash_type);
203 sb.data_block_size = cpu_to_le32(params->data_block_size);
204 sb.hash_block_size = cpu_to_le32(params->hash_block_size);
205 sb.salt_size = cpu_to_le16(params->salt_size);
206 sb.data_blocks = cpu_to_le64(params->data_size);
208 /* Kernel always use lower-case */
209 algorithm = (char *)sb.algorithm;
210 strncpy(algorithm, params->hash_name, sizeof(sb.algorithm)-1);
211 algorithm[sizeof(sb.algorithm)-1] = '\0';
212 _to_lower(algorithm);
214 memcpy(sb.salt, params->salt, params->salt_size);
215 memcpy(sb.uuid, uuid, sizeof(sb.uuid));
217 r = write_lseek_blockwise(devfd, block_size, device_alignment(device),
218 (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
220 log_err(cd, _("Error during update of verity header on device %s."),
221 device_path(device));
223 device_sync(cd, device);
228 /* Calculate hash offset in hash blocks */
229 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
231 uint64_t hash_offset = params->hash_area_offset;
233 if (params->flags & CRYPT_VERITY_NO_HEADER)
234 return hash_offset / params->hash_block_size;
236 hash_offset += sizeof(struct verity_sb);
237 hash_offset += params->hash_block_size - 1;
239 return hash_offset / params->hash_block_size;
242 int VERITY_UUID_generate(char **uuid_string)
246 *uuid_string = malloc(40);
250 uuid_unparse(uuid, *uuid_string);
254 /* Activate verity device in kernel device-mapper */
255 int VERITY_activate(struct crypt_device *cd,
257 const char *root_hash,
258 size_t root_hash_size,
259 const char *signature_description,
260 struct device *fec_device,
261 struct crypt_params_verity *verity_hdr,
262 uint32_t activation_flags)
265 unsigned int fec_errors = 0;
267 struct crypt_dm_active_device dmd = {
268 .size = verity_hdr->data_size * verity_hdr->data_block_size / 512,
269 .flags = activation_flags,
270 .uuid = crypt_get_uuid(cd),
273 log_dbg(cd, "Trying to activate VERITY device %s using hash %s.",
274 name ?: "[none]", verity_hdr->hash_name);
276 if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
277 if (signature_description) {
278 log_err(cd, _("Root hash signature verification is not supported."));
282 log_dbg(cd, "Verification of data in userspace required.");
283 r = VERITY_verify(cd, verity_hdr, root_hash, root_hash_size);
285 if ((r == -EPERM || r == -EFAULT) && fec_device) {
287 log_dbg(cd, "Verification failed, trying to repair with FEC device.");
288 r = VERITY_FEC_process(cd, verity_hdr, fec_device, 1, &fec_errors);
290 log_err(cd, _("Errors cannot be repaired with FEC device."));
291 else if (fec_errors) {
292 log_err(cd, _("Found %u repairable errors with FEC device."),
294 /* If root hash failed, we cannot be sure it was properly repaired */
307 r = device_block_adjust(cd, crypt_metadata_device(cd), DEV_OK,
312 r = device_block_adjust(cd, crypt_data_device(cd), DEV_EXCL,
313 0, &dmd.size, &dmd.flags);
318 r = device_block_adjust(cd, fec_device, DEV_OK,
324 r = dm_verity_target_set(&dmd.segment, 0, dmd.size, crypt_data_device(cd),
325 crypt_metadata_device(cd), fec_device, root_hash,
326 root_hash_size, signature_description,
327 VERITY_hash_offset_block(verity_hdr),
328 VERITY_FEC_blocks(cd, fec_device, verity_hdr), verity_hdr);
333 r = dm_create_device(cd, name, CRYPT_VERITY, &dmd);
334 if (r < 0 && (dm_flags(cd, DM_VERITY, &dmv_flags) || !(dmv_flags & DM_VERITY_SUPPORTED))) {
335 log_err(cd, _("Kernel does not support dm-verity mapping."));
338 if (r < 0 && signature_description && !(dmv_flags & DM_VERITY_SIGNATURE_SUPPORTED)) {
339 log_err(cd, _("Kernel does not support dm-verity signature option."));
345 r = dm_status_verity_ok(cd, name);
350 log_err(cd, _("Verity device detected corruption after activation."));
354 dm_targets_free(cd, &dmd);
358 int VERITY_dump(struct crypt_device *cd,
359 struct crypt_params_verity *verity_hdr,
360 const char *root_hash,
361 unsigned int root_hash_size,
362 struct device *fec_device)
364 uint64_t hash_blocks, verity_blocks, fec_blocks = 0, rs_blocks = 0;
365 bool fec_on_hash_device = false;
367 hash_blocks = VERITY_hash_blocks(cd, verity_hdr);
368 verity_blocks = VERITY_hash_offset_block(verity_hdr) + hash_blocks;
370 if (fec_device && verity_hdr->fec_roots) {
371 fec_blocks = VERITY_FEC_blocks(cd, fec_device, verity_hdr);
372 rs_blocks = VERITY_FEC_RS_blocks(fec_blocks, verity_hdr->fec_roots);
373 fec_on_hash_device = device_is_identical(crypt_metadata_device(cd), fec_device) > 0;
375 * No way to access fec_area_offset directly.
376 * Assume FEC area starts directly after hash blocks.
378 if (fec_on_hash_device)
379 verity_blocks += rs_blocks;
382 log_std(cd, "VERITY header information for %s\n", device_path(crypt_metadata_device(cd)));
383 log_std(cd, "UUID: \t%s\n", crypt_get_uuid(cd) ?: "");
384 log_std(cd, "Hash type: \t%u\n", verity_hdr->hash_type);
385 log_std(cd, "Data blocks: \t%" PRIu64 "\n", verity_hdr->data_size);
386 log_std(cd, "Data block size: \t%u\n", verity_hdr->data_block_size);
387 log_std(cd, "Hash blocks: \t%" PRIu64 "\n", hash_blocks);
388 log_std(cd, "Hash block size: \t%u\n", verity_hdr->hash_block_size);
389 log_std(cd, "Hash algorithm: \t%s\n", verity_hdr->hash_name);
390 if (fec_device && fec_blocks) {
391 log_std(cd, "FEC RS roots: \t%" PRIu32 "\n", verity_hdr->fec_roots);
392 log_std(cd, "FEC blocks: \t%" PRIu64 "\n", rs_blocks);
395 log_std(cd, "Salt: \t");
396 if (verity_hdr->salt_size)
397 crypt_log_hex(cd, verity_hdr->salt, verity_hdr->salt_size, "", 0, NULL);
403 log_std(cd, "Root hash: \t");
404 crypt_log_hex(cd, root_hash, root_hash_size, "", 0, NULL);
408 /* As dump can take only hash device, we have no idea about offsets here. */
409 if (verity_hdr->hash_area_offset == 0)
410 log_std(cd, "Hash device size: \t%" PRIu64 " [bytes]\n", verity_blocks * verity_hdr->hash_block_size);
412 if (fec_device && verity_hdr->fec_area_offset == 0 && fec_blocks && !fec_on_hash_device)
413 log_std(cd, "FEC device size: \t%" PRIu64 " [bytes]\n", rs_blocks * verity_hdr->data_block_size);