2 * dm-verity volume handling
4 * Copyright (C) 2012, 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>
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 int bsize = device_block_size(device);
62 struct verity_sb sb = {};
63 ssize_t hdr_size = sizeof(struct verity_sb);
64 int devfd = 0, sb_version;
66 log_dbg("Reading VERITY header of size %zu on device %s, offset %" PRIu64 ".",
67 sizeof(struct verity_sb), device_path(device), sb_offset);
69 if (params->flags & CRYPT_VERITY_NO_HEADER) {
70 log_err(cd, _("Verity device %s doesn't use on-disk header.\n"),
75 if (sb_offset % 512) {
76 log_err(cd, _("Unsupported VERITY hash offset.\n"));
80 devfd = device_open(device, O_RDONLY);
82 log_err(cd, _("Cannot open device %s.\n"), device_path(device));
86 if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
87 read_blockwise(devfd, bsize, &sb, hdr_size) < hdr_size) {
93 if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
94 log_err(cd, _("Device %s is not a valid VERITY device.\n"),
99 sb_version = le32_to_cpu(sb.version);
100 if (sb_version != 1) {
101 log_err(cd, _("Unsupported VERITY version %d.\n"), sb_version);
104 params->hash_type = le32_to_cpu(sb.hash_type);
105 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
106 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
110 params->data_block_size = le32_to_cpu(sb.data_block_size);
111 params->hash_block_size = le32_to_cpu(sb.hash_block_size);
112 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
113 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
114 log_err(cd, _("Unsupported VERITY block size.\n"));
117 params->data_size = le64_to_cpu(sb.data_blocks);
119 params->hash_name = strndup((const char*)sb.algorithm, sizeof(sb.algorithm));
120 if (!params->hash_name)
122 if (crypt_hash_size(params->hash_name) <= 0) {
123 log_err(cd, _("Hash algorithm %s not supported.\n"),
125 free(CONST_CAST(char*)params->hash_name);
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.\n"));
132 free(CONST_CAST(char*)params->hash_name);
135 params->salt = malloc(params->salt_size);
137 free(CONST_CAST(char*)params->hash_name);
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 /* Write verity superblock to disk */
150 int VERITY_write_sb(struct crypt_device *cd,
152 const char *uuid_string,
153 struct crypt_params_verity *params)
155 struct device *device = crypt_metadata_device(cd);
156 int bsize = device_block_size(device);
157 struct verity_sb sb = {};
158 ssize_t hdr_size = sizeof(struct verity_sb);
162 log_dbg("Updating VERITY header of size %zu on device %s, offset %" PRIu64 ".",
163 sizeof(struct verity_sb), device_path(device), sb_offset);
165 if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
166 log_err(cd, _("Wrong VERITY UUID format provided on device %s.\n"),
167 device_path(device));
171 if (params->flags & CRYPT_VERITY_NO_HEADER) {
172 log_err(cd, _("Verity device %s doesn't use on-disk header.\n"),
173 device_path(device));
177 devfd = device_open(device, O_RDWR);
179 log_err(cd, _("Cannot open device %s.\n"), device_path(device));
183 memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
184 sb.version = cpu_to_le32(1);
185 sb.hash_type = cpu_to_le32(params->hash_type);
186 sb.data_block_size = cpu_to_le32(params->data_block_size);
187 sb.hash_block_size = cpu_to_le32(params->hash_block_size);
188 sb.salt_size = cpu_to_le16(params->salt_size);
189 sb.data_blocks = cpu_to_le64(params->data_size);
190 strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
191 memcpy(sb.salt, params->salt, params->salt_size);
192 memcpy(sb.uuid, uuid, sizeof(sb.uuid));
194 r = write_lseek_blockwise(devfd, bsize, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
196 log_err(cd, _("Error during update of verity header on device %s.\n"),
197 device_path(device));
203 /* Calculate hash offset in hash blocks */
204 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
206 uint64_t hash_offset = params->hash_area_offset;
208 if (params->flags & CRYPT_VERITY_NO_HEADER)
209 return hash_offset / params->hash_block_size;
211 hash_offset += sizeof(struct verity_sb);
212 hash_offset += params->hash_block_size - 1;
214 return hash_offset / params->hash_block_size;
217 int VERITY_UUID_generate(struct crypt_device *cd, char **uuid_string)
221 if (!(*uuid_string = malloc(40)))
224 uuid_unparse(uuid, *uuid_string);
228 /* Activate verity device in kernel device-mapper */
229 int VERITY_activate(struct crypt_device *cd,
231 const char *root_hash,
232 size_t root_hash_size,
233 struct crypt_params_verity *verity_hdr,
234 uint32_t activation_flags)
236 struct crypt_dm_active_device dmd;
239 log_dbg("Trying to activate VERITY device %s using hash %s.",
240 name ?: "[none]", verity_hdr->hash_name);
242 if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
243 log_dbg("Verification of data in userspace required.");
244 r = VERITY_verify(cd, verity_hdr,
245 root_hash, root_hash_size);
253 dmd.target = DM_VERITY;
254 dmd.data_device = crypt_data_device(cd);
255 dmd.u.verity.hash_device = crypt_metadata_device(cd);
256 dmd.u.verity.root_hash = root_hash;
257 dmd.u.verity.root_hash_size = root_hash_size;
258 dmd.u.verity.hash_offset = VERITY_hash_offset_block(verity_hdr),
259 dmd.flags = activation_flags;
260 dmd.size = verity_hdr->data_size * verity_hdr->data_block_size / 512;
261 dmd.uuid = crypt_get_uuid(cd);
262 dmd.u.verity.vp = verity_hdr;
264 r = device_block_adjust(cd, dmd.u.verity.hash_device, DEV_OK,
269 r = device_block_adjust(cd, dmd.data_device, DEV_EXCL,
270 0, &dmd.size, &dmd.flags);
274 r = dm_create_device(cd, name, CRYPT_VERITY, &dmd, 0);
275 if (r < 0 && !(dm_flags() & DM_VERITY_SUPPORTED)) {
276 log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
282 r = dm_status_verity_ok(cd, name);
287 log_err(cd, _("Verity device detected corruption after activation.\n"));