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 /* http://code.google.com/p/cryptsetup/wiki/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 const char *device = device_path(crypt_metadata_device(cd));
61 int bsize = device_block_size(crypt_metadata_device(cd));
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 %u on device %s, offset %" PRIu64 ".",
67 sizeof(struct verity_sb), device, sb_offset);
69 if (params->flags & CRYPT_VERITY_NO_HEADER) {
70 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
74 if (sb_offset % 512) {
75 log_err(cd, _("Unsupported VERITY hash offset.\n"));
79 devfd = open(device ,O_RDONLY | O_DIRECT);
81 log_err(cd, _("Cannot open device %s.\n"), device);
85 if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
86 read_blockwise(devfd, bsize, &sb, hdr_size) < hdr_size) {
92 if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
93 log_err(cd, _("Device %s is not a valid VERITY device.\n"), device);
97 sb_version = le32_to_cpu(sb.version);
98 if (sb_version != 1) {
99 log_err(cd, _("Unsupported VERITY version %d.\n"), sb_version);
102 params->hash_type = le32_to_cpu(sb.hash_type);
103 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
104 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
108 params->data_block_size = le32_to_cpu(sb.data_block_size);
109 params->hash_block_size = le32_to_cpu(sb.hash_block_size);
110 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
111 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
112 log_err(cd, _("Unsupported VERITY block size.\n"));
115 params->data_size = le64_to_cpu(sb.data_blocks);
117 params->hash_name = strndup((const char*)sb.algorithm, sizeof(sb.algorithm));
118 if (!params->hash_name)
120 if (crypt_hash_size(params->hash_name) <= 0) {
121 log_err(cd, _("Hash algorithm %s not supported.\n"),
123 free(CONST_CAST(char*)params->hash_name);
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.\n"));
130 free(CONST_CAST(char*)params->hash_name);
133 params->salt = malloc(params->salt_size);
135 free(CONST_CAST(char*)params->hash_name);
138 memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
140 if ((*uuid_string = malloc(40)))
141 uuid_unparse(sb.uuid, *uuid_string);
143 params->hash_area_offset = sb_offset;
147 /* Write verity superblock to disk */
148 int VERITY_write_sb(struct crypt_device *cd,
150 const char *uuid_string,
151 struct crypt_params_verity *params)
153 const char *device = device_path(crypt_metadata_device(cd));
154 int bsize = device_block_size(crypt_metadata_device(cd));
155 struct verity_sb sb = {};
156 ssize_t hdr_size = sizeof(struct verity_sb);
160 log_dbg("Updating VERITY header of size %u on device %s, offset %" PRIu64 ".",
161 sizeof(struct verity_sb), device, sb_offset);
163 if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
164 log_err(cd, _("Wrong VERITY UUID format provided.\n"), device);
168 if (params->flags & CRYPT_VERITY_NO_HEADER) {
169 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
173 devfd = open(device, O_RDWR | O_DIRECT);
175 log_err(cd, _("Cannot open device %s.\n"), device);
179 memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
180 sb.version = cpu_to_le32(1);
181 sb.hash_type = cpu_to_le32(params->hash_type);
182 sb.data_block_size = cpu_to_le32(params->data_block_size);
183 sb.hash_block_size = cpu_to_le32(params->hash_block_size);
184 sb.salt_size = cpu_to_le16(params->salt_size);
185 sb.data_blocks = cpu_to_le64(params->data_size);
186 strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
187 memcpy(sb.salt, params->salt, params->salt_size);
188 memcpy(sb.uuid, uuid, sizeof(sb.uuid));
190 r = write_lseek_blockwise(devfd, bsize, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
192 log_err(cd, _("Error during update of verity header on device %s.\n"), device);
198 /* Calculate hash offset in hash blocks */
199 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
201 uint64_t hash_offset = params->hash_area_offset;
203 if (params->flags & CRYPT_VERITY_NO_HEADER)
204 return hash_offset / params->hash_block_size;
206 hash_offset += sizeof(struct verity_sb);
207 hash_offset += params->hash_block_size - 1;
209 return hash_offset / params->hash_block_size;
212 int VERITY_UUID_generate(struct crypt_device *cd, char **uuid_string)
216 if (!(*uuid_string = malloc(40)))
219 uuid_unparse(uuid, *uuid_string);
223 /* Activate verity device in kernel device-mapper */
224 int VERITY_activate(struct crypt_device *cd,
226 const char *root_hash,
227 size_t root_hash_size,
228 struct crypt_params_verity *verity_hdr,
229 uint32_t activation_flags)
231 struct crypt_dm_active_device dmd;
234 log_dbg("Trying to activate VERITY device %s using hash %s.",
235 name ?: "[none]", verity_hdr->hash_name);
237 if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
238 log_dbg("Verification of data in userspace required.");
239 r = VERITY_verify(cd, verity_hdr,
240 root_hash, root_hash_size);
248 dmd.target = DM_VERITY;
249 dmd.data_device = crypt_data_device(cd);
250 dmd.u.verity.hash_device = crypt_metadata_device(cd);
251 dmd.u.verity.root_hash = root_hash;
252 dmd.u.verity.root_hash_size = root_hash_size;
253 dmd.u.verity.hash_offset = VERITY_hash_offset_block(verity_hdr),
254 dmd.flags = activation_flags;
255 dmd.size = verity_hdr->data_size * verity_hdr->data_block_size / 512;
256 dmd.uuid = crypt_get_uuid(cd);
257 dmd.u.verity.vp = verity_hdr;
259 r = device_block_adjust(cd, dmd.u.verity.hash_device, DEV_OK,
264 r = device_block_adjust(cd, dmd.data_device, DEV_EXCL,
265 0, &dmd.size, &dmd.flags);
269 r = dm_create_device(cd, name, CRYPT_VERITY, &dmd, 0);
270 if (!r && !(dm_flags() & DM_VERITY_SUPPORTED)) {
271 log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
277 r = dm_status_verity_ok(cd, name);
282 log_err(cd, _("Verity device detected corruption after activation.\n"));