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"
41 uint8_t data_block_bits;
42 uint8_t hash_block_bits;
46 uint32_t data_blocks_hi;
47 uint32_t data_blocks_lo;
48 uint8_t algorithm[16];
55 uint8_t signature[8]; /* "verity\0\0" */
56 uint32_t version; /* superblock version */
57 uint32_t hash_type; /* 0 - Chrome OS, 1 - normal */
58 uint8_t uuid[16]; /* UUID of hash device */
59 uint8_t algorithm[32];/* hash algorithm name */
60 uint64_t data_block_size; /* data block in bytes */
61 uint64_t hash_block_size; /* hash block in bytes */
62 uint64_t data_blocks; /* number of data blocks */
63 uint64_t salt_size; /* salt size */
64 uint8_t salt[256]; /* salt */
66 } __attribute__((packed));
69 /* Read verity superblock from disk */
70 int VERITY_read_sb(struct crypt_device *cd,
74 struct crypt_params_verity *params)
76 struct verity_sb sb = {};
77 ssize_t hdr_size = sizeof(struct verity_sb);
78 int devfd = 0, sb_version;
79 uint64_t sb_data_blocks;
81 log_dbg("Reading VERITY header of size %u on device %s, offset %" PRIu64 ".",
82 sizeof(struct verity_sb), device, sb_offset);
84 if (params->flags & CRYPT_VERITY_NO_HEADER) {
85 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
89 devfd = open(device ,O_RDONLY | O_DIRECT);
91 log_err(cd, _("Cannot open device %s.\n"), device);
95 if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
96 read_blockwise(devfd, &sb, hdr_size) < hdr_size) {
102 if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
103 log_err(cd, _("Device %s is not a valid VERITY device.\n"), device);
107 if (sb.version > 1) {
108 log_err(cd, _("Unsupported VERITY version %d.\n"), sb.version);
112 if (sb.data_block_bits < 9 || sb.data_block_bits >= 31 ||
113 sb.hash_block_bits < 9 || sb.hash_block_bits >= 31 ||
114 !memchr(sb.algorithm, 0, sizeof(sb.algorithm)) ||
115 ntohs(sb.salt_size) > 256) {
116 log_err(cd, _("VERITY header corrupted.\n"));
120 sb_data_blocks = ((uint64_t)ntohl(sb.data_blocks_hi) << 31 << 1) |
121 ntohl(sb.data_blocks_lo);
123 params->hash_name = strdup((const char*)sb.algorithm);
124 if (!params->hash_name)
126 params->data_block_size = 1 << sb.data_block_bits;
127 params->hash_block_size = 1 << sb.hash_block_bits;
128 params->data_size = sb_data_blocks;
129 params->salt_size = ntohs(sb.salt_size);
130 params->salt = malloc(params->salt_size);
133 memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
134 params->hash_type = sb.version;
136 sb_version = le32_to_cpu(sb.version);
137 if (sb_version != 1) {
138 log_err(cd, _("Unsupported VERITY version %d.\n"), sb_version);
141 params->hash_type = le32_to_cpu(sb.hash_type);
142 if (params->hash_type > 1) {
143 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
147 params->data_block_size = le64_to_cpu(sb.data_block_size);
148 params->hash_block_size = le64_to_cpu(sb.hash_block_size);
149 if (params->data_block_size % 512 || params->hash_block_size % 512) {
150 log_err(cd, _("Unsupported VERITY block size.\n"));
153 params->data_size = le64_to_cpu(sb.data_blocks);
155 params->hash_name = strndup((const char*)sb.algorithm, sizeof(sb.algorithm));
156 if (!params->hash_name)
159 params->salt_size = le64_to_cpu(sb.salt_size);
160 if (params->salt_size > sizeof(sb.salt)) {
161 log_err(cd, _("VERITY header corrupted.\n"));
162 free(CONST_CAST(char*)params->hash_name);
165 params->salt = malloc(params->salt_size);
167 free(CONST_CAST(char*)params->hash_name);
170 memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
172 if ((*uuid_string = malloc(40)))
173 uuid_unparse(sb.uuid, *uuid_string);
175 params->hash_area_offset = sb_offset;
179 /* Write verity superblock to disk */
180 int VERITY_write_sb(struct crypt_device *cd,
183 const char *uuid_string,
184 struct crypt_params_verity *params)
186 struct verity_sb sb = {};
187 ssize_t hdr_size = sizeof(struct verity_sb);
191 log_dbg("Updating VERITY header of size %u on device %s, offset %" PRIu64 ".",
192 sizeof(struct verity_sb), device, sb_offset);
194 if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
195 log_err(cd, _("Wrong VERITY UUID format provided.\n"), device);
199 if (params->flags & CRYPT_VERITY_NO_HEADER) {
200 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
204 devfd = open(device, O_RDWR | O_DIRECT);
206 log_err(cd, _("Cannot open device %s.\n"), device);
210 memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
212 sb.version = params->hash_type;
213 sb.data_block_bits = ffs(params->data_block_size) - 1;
214 sb.hash_block_bits = ffs(params->hash_block_size) - 1;
215 sb.salt_size = htons(params->salt_size);
216 sb.data_blocks_hi = htonl(params->data_size >> 31 >> 1);
217 sb.data_blocks_lo = htonl(params->data_size & 0xFFFFFFFF);
218 strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
219 memcpy(sb.salt, params->salt, params->salt_size);
221 sb.version = cpu_to_le32(1);
222 sb.hash_type = cpu_to_le32(params->hash_type);
223 sb.data_block_size = cpu_to_le64(params->data_block_size);
224 sb.hash_block_size = cpu_to_le64(params->hash_block_size);
225 sb.salt_size = cpu_to_le64(params->salt_size);
226 sb.data_blocks = cpu_to_le64(params->data_size);
227 strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
228 memcpy(sb.salt, params->salt, params->salt_size);
229 memcpy(sb.uuid, uuid, sizeof(sb.uuid));
231 r = write_lseek_blockwise(devfd, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
233 log_err(cd, _("Error during update of verity header on device %s.\n"), device);
239 /* Calculate hash offset in hash blocks */
240 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
242 uint64_t hash_offset = params->hash_area_offset;
244 if (params->flags & CRYPT_VERITY_NO_HEADER)
245 return hash_offset / params->hash_block_size;
247 hash_offset += sizeof(struct verity_sb);
248 hash_offset += params->hash_block_size - 1;
250 return hash_offset / params->hash_block_size;
253 int VERITY_UUID_generate(struct crypt_device *cd, char **uuid_string)
257 if (!(*uuid_string = malloc(40)))
260 uuid_unparse(uuid, *uuid_string);
264 /* Activate verity device in kernel device-mapper */
265 int VERITY_activate(struct crypt_device *cd,
267 const char *hash_device,
268 const char *root_hash,
269 size_t root_hash_size,
270 struct crypt_params_verity *verity_hdr,
271 uint32_t activation_flags)
273 struct crypt_dm_active_device dmd;
277 log_dbg("Trying to activate VERITY device %s using hash %s.",
278 name ?: "[none]", verity_hdr->hash_name);
280 if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
281 r = VERITY_verify(cd, verity_hdr,
282 crypt_get_device_name(cd), hash_device,
283 root_hash, root_hash_size);
291 dmd.target = DM_VERITY;
292 dmd.data_device = crypt_get_device_name(cd);
293 dmd.u.verity.hash_device = hash_device;
294 dmd.u.verity.root_hash = root_hash;
295 dmd.u.verity.root_hash_size = root_hash_size;
296 dmd.u.verity.hash_offset = VERITY_hash_offset_block(verity_hdr),
297 dmd.flags = activation_flags;
298 dmd.size = verity_hdr->data_size * verity_hdr->data_block_size / 512;
299 dmd.uuid = crypt_get_uuid(cd);
300 dmd.u.verity.vp = verity_hdr;
302 r = device_check_and_adjust(cd, dmd.data_device, DEV_EXCL,
303 &dmd.size, &offset, &dmd.flags);
307 r = dm_create_device(name, CRYPT_VERITY, &dmd, 0);
308 if (!r && !(dm_flags() & DM_VERITY_SUPPORTED)) {
309 log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
315 r = dm_status_verity_ok(name);
320 log_err(cd, _("Verity device detected corruption after activation.\n"));