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,
58 struct crypt_params_verity *params)
60 struct verity_sb sb = {};
61 ssize_t hdr_size = sizeof(struct verity_sb);
62 int devfd = 0, sb_version;
64 log_dbg("Reading VERITY header of size %u on device %s, offset %" PRIu64 ".",
65 sizeof(struct verity_sb), device, sb_offset);
67 if (params->flags & CRYPT_VERITY_NO_HEADER) {
68 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
72 if (sb_offset % 512) {
73 log_err(cd, _("Unsupported VERITY hash offset.\n"));
77 devfd = open(device ,O_RDONLY | O_DIRECT);
79 log_err(cd, _("Cannot open device %s.\n"), device);
83 if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
84 read_blockwise(devfd, &sb, hdr_size) < hdr_size) {
90 if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
91 log_err(cd, _("Device %s is not a valid VERITY device.\n"), device);
95 sb_version = le32_to_cpu(sb.version);
96 if (sb_version != 1) {
97 log_err(cd, _("Unsupported VERITY version %d.\n"), sb_version);
100 params->hash_type = le32_to_cpu(sb.hash_type);
101 if (params->hash_type > VERITY_MAX_HASH_TYPE) {
102 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
106 params->data_block_size = le32_to_cpu(sb.data_block_size);
107 params->hash_block_size = le32_to_cpu(sb.hash_block_size);
108 if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
109 VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
110 log_err(cd, _("Unsupported VERITY block size.\n"));
113 params->data_size = le64_to_cpu(sb.data_blocks);
115 params->hash_name = strndup((const char*)sb.algorithm, sizeof(sb.algorithm));
116 if (!params->hash_name)
118 if (crypt_hash_size(params->hash_name) <= 0) {
119 log_err(cd, _("Hash algorithm %s not supported.\n"),
121 free(CONST_CAST(char*)params->hash_name);
125 params->salt_size = le16_to_cpu(sb.salt_size);
126 if (params->salt_size > sizeof(sb.salt)) {
127 log_err(cd, _("VERITY header corrupted.\n"));
128 free(CONST_CAST(char*)params->hash_name);
131 params->salt = malloc(params->salt_size);
133 free(CONST_CAST(char*)params->hash_name);
136 memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
138 if ((*uuid_string = malloc(40)))
139 uuid_unparse(sb.uuid, *uuid_string);
141 params->hash_area_offset = sb_offset;
145 /* Write verity superblock to disk */
146 int VERITY_write_sb(struct crypt_device *cd,
149 const char *uuid_string,
150 struct crypt_params_verity *params)
152 struct verity_sb sb = {};
153 ssize_t hdr_size = sizeof(struct verity_sb);
157 log_dbg("Updating VERITY header of size %u on device %s, offset %" PRIu64 ".",
158 sizeof(struct verity_sb), device, sb_offset);
160 if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
161 log_err(cd, _("Wrong VERITY UUID format provided.\n"), device);
165 if (params->flags & CRYPT_VERITY_NO_HEADER) {
166 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
170 devfd = open(device, O_RDWR | O_DIRECT);
172 log_err(cd, _("Cannot open device %s.\n"), device);
176 memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
177 sb.version = cpu_to_le32(1);
178 sb.hash_type = cpu_to_le32(params->hash_type);
179 sb.data_block_size = cpu_to_le32(params->data_block_size);
180 sb.hash_block_size = cpu_to_le32(params->hash_block_size);
181 sb.salt_size = cpu_to_le16(params->salt_size);
182 sb.data_blocks = cpu_to_le64(params->data_size);
183 strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
184 memcpy(sb.salt, params->salt, params->salt_size);
185 memcpy(sb.uuid, uuid, sizeof(sb.uuid));
187 r = write_lseek_blockwise(devfd, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
189 log_err(cd, _("Error during update of verity header on device %s.\n"), device);
195 /* Calculate hash offset in hash blocks */
196 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
198 uint64_t hash_offset = params->hash_area_offset;
200 if (params->flags & CRYPT_VERITY_NO_HEADER)
201 return hash_offset / params->hash_block_size;
203 hash_offset += sizeof(struct verity_sb);
204 hash_offset += params->hash_block_size - 1;
206 return hash_offset / params->hash_block_size;
209 int VERITY_UUID_generate(struct crypt_device *cd, char **uuid_string)
213 if (!(*uuid_string = malloc(40)))
216 uuid_unparse(uuid, *uuid_string);
220 /* Activate verity device in kernel device-mapper */
221 int VERITY_activate(struct crypt_device *cd,
223 const char *hash_device,
224 const char *root_hash,
225 size_t root_hash_size,
226 struct crypt_params_verity *verity_hdr,
227 uint32_t activation_flags)
229 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 crypt_get_device_name(cd), hash_device,
240 root_hash, root_hash_size);
248 dmd.target = DM_VERITY;
249 dmd.data_device = crypt_get_device_name(cd);
250 dmd.u.verity.hash_device = hash_device;
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_check_and_adjust(cd, dmd.data_device, DEV_EXCL,
260 &dmd.size, &offset, &dmd.flags);
264 r = dm_create_device(name, CRYPT_VERITY, &dmd, 0);
265 if (!r && !(dm_flags() & DM_VERITY_SUPPORTED)) {
266 log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
272 r = dm_status_verity_ok(name);
277 log_err(cd, _("Verity device detected corruption after activation.\n"));