2 * dm-verity volume handling
4 * Copyright (C) 2012, Red Hat, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #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 /* http://code.google.com/p/cryptsetup/wiki/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 const char *device = device_path(crypt_metadata_device(cd));
60 int bsize = device_block_size(crypt_metadata_device(cd));
61 struct verity_sb sb = {};
62 ssize_t hdr_size = sizeof(struct verity_sb);
63 int devfd = 0, sb_version;
65 log_dbg("Reading VERITY header of size %u on device %s, offset %" PRIu64 ".",
66 sizeof(struct verity_sb), device, sb_offset);
68 if (params->flags & CRYPT_VERITY_NO_HEADER) {
69 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
73 if (sb_offset % 512) {
74 log_err(cd, _("Unsupported VERITY hash offset.\n"));
78 devfd = open(device ,O_RDONLY | O_DIRECT);
80 log_err(cd, _("Cannot open device %s.\n"), device);
84 if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
85 read_blockwise(devfd, bsize, &sb, hdr_size) < hdr_size) {
91 if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
92 log_err(cd, _("Device %s is not a valid VERITY device.\n"), device);
96 sb_version = le32_to_cpu(sb.version);
97 if (sb_version != 1) {
98 log_err(cd, _("Unsupported VERITY version %d.\n"), 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.\n"), 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.\n"));
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.\n"),
122 free(CONST_CAST(char*)params->hash_name);
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.\n"));
129 free(CONST_CAST(char*)params->hash_name);
132 params->salt = malloc(params->salt_size);
134 free(CONST_CAST(char*)params->hash_name);
137 memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
139 if ((*uuid_string = malloc(40)))
140 uuid_unparse(sb.uuid, *uuid_string);
142 params->hash_area_offset = sb_offset;
146 /* Write verity superblock to disk */
147 int VERITY_write_sb(struct crypt_device *cd,
149 const char *uuid_string,
150 struct crypt_params_verity *params)
152 const char *device = device_path(crypt_metadata_device(cd));
153 int bsize = device_block_size(crypt_metadata_device(cd));
154 struct verity_sb sb = {};
155 ssize_t hdr_size = sizeof(struct verity_sb);
159 log_dbg("Updating VERITY header of size %u on device %s, offset %" PRIu64 ".",
160 sizeof(struct verity_sb), device, sb_offset);
162 if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
163 log_err(cd, _("Wrong VERITY UUID format provided.\n"), device);
167 if (params->flags & CRYPT_VERITY_NO_HEADER) {
168 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
172 devfd = open(device, O_RDWR | O_DIRECT);
174 log_err(cd, _("Cannot open device %s.\n"), device);
178 memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
179 sb.version = cpu_to_le32(1);
180 sb.hash_type = cpu_to_le32(params->hash_type);
181 sb.data_block_size = cpu_to_le32(params->data_block_size);
182 sb.hash_block_size = cpu_to_le32(params->hash_block_size);
183 sb.salt_size = cpu_to_le16(params->salt_size);
184 sb.data_blocks = cpu_to_le64(params->data_size);
185 strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
186 memcpy(sb.salt, params->salt, params->salt_size);
187 memcpy(sb.uuid, uuid, sizeof(sb.uuid));
189 r = write_lseek_blockwise(devfd, bsize, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
191 log_err(cd, _("Error during update of verity header on device %s.\n"), device);
197 /* Calculate hash offset in hash blocks */
198 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
200 uint64_t hash_offset = params->hash_area_offset;
202 if (params->flags & CRYPT_VERITY_NO_HEADER)
203 return hash_offset / params->hash_block_size;
205 hash_offset += sizeof(struct verity_sb);
206 hash_offset += params->hash_block_size - 1;
208 return hash_offset / params->hash_block_size;
211 int VERITY_UUID_generate(struct crypt_device *cd, char **uuid_string)
215 if (!(*uuid_string = malloc(40)))
218 uuid_unparse(uuid, *uuid_string);
222 /* Activate verity device in kernel device-mapper */
223 int VERITY_activate(struct crypt_device *cd,
225 const char *root_hash,
226 size_t root_hash_size,
227 struct crypt_params_verity *verity_hdr,
228 uint32_t activation_flags)
230 struct crypt_dm_active_device dmd;
233 log_dbg("Trying to activate VERITY device %s using hash %s.",
234 name ?: "[none]", verity_hdr->hash_name);
236 if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
237 log_dbg("Verification of data in userspace required.");
238 r = VERITY_verify(cd, verity_hdr,
239 root_hash, root_hash_size);
247 dmd.target = DM_VERITY;
248 dmd.data_device = crypt_data_device(cd);
249 dmd.u.verity.hash_device = crypt_metadata_device(cd);
250 dmd.u.verity.root_hash = root_hash;
251 dmd.u.verity.root_hash_size = root_hash_size;
252 dmd.u.verity.hash_offset = VERITY_hash_offset_block(verity_hdr),
253 dmd.flags = activation_flags;
254 dmd.size = verity_hdr->data_size * verity_hdr->data_block_size / 512;
255 dmd.uuid = crypt_get_uuid(cd);
256 dmd.u.verity.vp = verity_hdr;
258 r = device_block_adjust(cd, dmd.u.verity.hash_device, DEV_OK,
263 r = device_block_adjust(cd, dmd.data_device, DEV_EXCL,
264 0, &dmd.size, &dmd.flags);
268 r = dm_create_device(cd, name, CRYPT_VERITY, &dmd, 0);
269 if (!r && !(dm_flags() & DM_VERITY_SUPPORTED)) {
270 log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
276 r = dm_status_verity_ok(cd, name);
281 log_err(cd, _("Verity device detected corruption after activation.\n"));