2 * dm-verity volume handling
4 * Copyright (C) 2012-2021 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 <netinet/in.h>
30 #include <uuid/uuid.h>
32 #include "libcryptsetup.h"
36 #define VERITY_SIGNATURE "verity\0\0"
38 /* https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity#verity-superblock-format */
40 uint8_t signature[8]; /* "verity\0\0" */
41 uint32_t version; /* superblock version */
42 uint32_t hash_type; /* 0 - Chrome OS, 1 - normal */
43 uint8_t uuid[16]; /* UUID of hash device */
44 uint8_t algorithm[32];/* hash algorithm name */
45 uint32_t data_block_size; /* data block in bytes */
46 uint32_t hash_block_size; /* hash block in bytes */
47 uint64_t data_blocks; /* number of data blocks */
48 uint16_t salt_size; /* salt size */
50 uint8_t salt[256]; /* salt */
52 } __attribute__((packed));
54 /* Read verity superblock from disk */
55 int VERITY_read_sb(struct crypt_device *cd,
58 struct crypt_params_verity *params)
60 struct device *device = crypt_metadata_device(cd);
61 struct verity_sb sb = {};
62 ssize_t hdr_size = sizeof(struct verity_sb);
63 int devfd, sb_version;
65 log_dbg(cd, "Reading VERITY header of size %zu on device %s, offset %" PRIu64 ".",
66 sizeof(struct verity_sb), device_path(device), sb_offset);
68 if (params->flags & CRYPT_VERITY_NO_HEADER) {
69 log_err(cd, _("Verity device %s does not use on-disk header."),
74 if (MISALIGNED_512(sb_offset)) {
75 log_err(cd, _("Unsupported VERITY hash offset."));
79 devfd = device_open(cd, device, O_RDONLY);
81 log_err(cd, _("Cannot open device %s."), device_path(device));
85 if (read_lseek_blockwise(devfd, device_block_size(cd, device),
86 device_alignment(device), &sb, hdr_size,
87 sb_offset) < hdr_size)
90 if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
91 log_err(cd, _("Device %s is not a valid VERITY device."),
96 sb_version = le32_to_cpu(sb.version);
97 if (sb_version != 1) {
98 log_err(cd, _("Unsupported VERITY version %d."), sb_version);
101 params->hash_type = le32_to_cpu(sb.hash_type);
102 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
103 log_err(cd, _("Unsupported VERITY hash type %d."), params->hash_type);
107 params->data_block_size = le32_to_cpu(sb.data_block_size);
108 params->hash_block_size = le32_to_cpu(sb.hash_block_size);
109 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
110 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
111 log_err(cd, _("Unsupported VERITY block size."));
114 params->data_size = le64_to_cpu(sb.data_blocks);
116 params->hash_name = strndup((const char*)sb.algorithm, sizeof(sb.algorithm));
117 if (!params->hash_name)
119 if (crypt_hash_size(params->hash_name) <= 0) {
120 log_err(cd, _("Hash algorithm %s not supported."),
122 free(CONST_CAST(char*)params->hash_name);
123 params->hash_name = NULL;
127 params->salt_size = le16_to_cpu(sb.salt_size);
128 if (params->salt_size > sizeof(sb.salt)) {
129 log_err(cd, _("VERITY header corrupted."));
130 free(CONST_CAST(char*)params->hash_name);
131 params->hash_name = NULL;
134 params->salt = malloc(params->salt_size);
136 free(CONST_CAST(char*)params->hash_name);
137 params->hash_name = NULL;
140 memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
142 if ((*uuid_string = malloc(40)))
143 uuid_unparse(sb.uuid, *uuid_string);
145 params->hash_area_offset = sb_offset;
149 static void _to_lower(char *str)
153 *str = tolower(*str);
156 /* Write verity superblock to disk */
157 int VERITY_write_sb(struct crypt_device *cd,
159 const char *uuid_string,
160 struct crypt_params_verity *params)
162 struct device *device = crypt_metadata_device(cd);
163 struct verity_sb sb = {};
164 ssize_t hdr_size = sizeof(struct verity_sb);
170 log_dbg(cd, "Updating VERITY header of size %zu on device %s, offset %" PRIu64 ".",
171 sizeof(struct verity_sb), device_path(device), sb_offset);
173 if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
174 log_err(cd, _("Wrong VERITY UUID format provided on device %s."),
175 device_path(device));
179 if (params->flags & CRYPT_VERITY_NO_HEADER) {
180 log_err(cd, _("Verity device %s does not use on-disk header."),
181 device_path(device));
185 /* Avoid possible increasing of image size - FEC could fail later because of it */
186 block_size = device_block_size(cd, device);
187 if (block_size > params->hash_block_size) {
188 device_disable_direct_io(device);
189 block_size = params->hash_block_size;
192 devfd = device_open(cd, device, O_RDWR);
194 log_err(cd, _("Cannot open device %s."), device_path(device));
198 memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
199 sb.version = cpu_to_le32(1);
200 sb.hash_type = cpu_to_le32(params->hash_type);
201 sb.data_block_size = cpu_to_le32(params->data_block_size);
202 sb.hash_block_size = cpu_to_le32(params->hash_block_size);
203 sb.salt_size = cpu_to_le16(params->salt_size);
204 sb.data_blocks = cpu_to_le64(params->data_size);
206 /* Kernel always use lower-case */
207 algorithm = (char *)sb.algorithm;
208 strncpy(algorithm, params->hash_name, sizeof(sb.algorithm)-1);
209 algorithm[sizeof(sb.algorithm)-1] = '\0';
210 _to_lower(algorithm);
212 memcpy(sb.salt, params->salt, params->salt_size);
213 memcpy(sb.uuid, uuid, sizeof(sb.uuid));
215 r = write_lseek_blockwise(devfd, block_size, device_alignment(device),
216 (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
218 log_err(cd, _("Error during update of verity header on device %s."),
219 device_path(device));
221 device_sync(cd, device);
226 /* Calculate hash offset in hash blocks */
227 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
229 uint64_t hash_offset = params->hash_area_offset;
231 if (params->flags & CRYPT_VERITY_NO_HEADER)
232 return hash_offset / params->hash_block_size;
234 hash_offset += sizeof(struct verity_sb);
235 hash_offset += params->hash_block_size - 1;
237 return hash_offset / params->hash_block_size;
240 int VERITY_UUID_generate(struct crypt_device *cd, char **uuid_string)
244 *uuid_string = malloc(40);
248 uuid_unparse(uuid, *uuid_string);
252 /* Activate verity device in kernel device-mapper */
253 int VERITY_activate(struct crypt_device *cd,
255 const char *root_hash,
256 size_t root_hash_size,
257 const char *signature_description,
258 struct device *fec_device,
259 struct crypt_params_verity *verity_hdr,
260 uint32_t activation_flags)
263 unsigned int fec_errors = 0;
265 struct crypt_dm_active_device dmd = {
266 .size = verity_hdr->data_size * verity_hdr->data_block_size / 512,
267 .flags = activation_flags,
268 .uuid = crypt_get_uuid(cd),
271 log_dbg(cd, "Trying to activate VERITY device %s using hash %s.",
272 name ?: "[none]", verity_hdr->hash_name);
274 if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
275 if (signature_description) {
276 log_err(cd, _("Root hash signature verification is not supported."));
280 log_dbg(cd, "Verification of data in userspace required.");
281 r = VERITY_verify(cd, verity_hdr, root_hash, root_hash_size);
283 if ((r == -EPERM || r == -EFAULT) && fec_device) {
285 log_dbg(cd, "Verification failed, trying to repair with FEC device.");
286 r = VERITY_FEC_process(cd, verity_hdr, fec_device, 1, &fec_errors);
288 log_err(cd, _("Errors cannot be repaired with FEC device."));
289 else if (fec_errors) {
290 log_err(cd, _("Found %u repairable errors with FEC device."),
292 /* If root hash failed, we cannot be sure it was properly repaired */
305 r = device_block_adjust(cd, crypt_metadata_device(cd), DEV_OK,
310 r = device_block_adjust(cd, crypt_data_device(cd), DEV_EXCL,
311 0, &dmd.size, &dmd.flags);
316 r = device_block_adjust(cd, fec_device, DEV_OK,
322 r = dm_verity_target_set(&dmd.segment, 0, dmd.size, crypt_data_device(cd),
323 crypt_metadata_device(cd), fec_device, root_hash,
324 root_hash_size, signature_description,
325 VERITY_hash_offset_block(verity_hdr),
326 VERITY_FEC_blocks(cd, fec_device, verity_hdr), verity_hdr);
331 r = dm_create_device(cd, name, CRYPT_VERITY, &dmd);
332 if (r < 0 && (dm_flags(cd, DM_VERITY, &dmv_flags) || !(dmv_flags & DM_VERITY_SUPPORTED))) {
333 log_err(cd, _("Kernel does not support dm-verity mapping."));
336 if (r < 0 && signature_description && !(dmv_flags & DM_VERITY_SIGNATURE_SUPPORTED)) {
337 log_err(cd, _("Kernel does not support dm-verity signature option."));
343 r = dm_status_verity_ok(cd, name);
348 log_err(cd, _("Verity device detected corruption after activation."));
352 dm_targets_free(cd, &dmd);