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.
30 #define VERITY_MAX_LEVELS 63
32 static unsigned get_bits_up(size_t u)
40 static unsigned get_bits_down(size_t u)
48 static int verify_zero(struct crypt_device *cd, FILE *wr, size_t bytes)
53 if (fread(block, bytes, 1, wr) != 1) {
54 log_dbg("EIO while reading spare area.");
57 for (i = 0; i < bytes; i++)
59 log_err(cd, _("Spare area is not zeroed at position %" PRIu64 ".\n"),
66 static int verify_hash_block(const char *hash_name, int version,
67 char *hash, size_t hash_size,
68 const char *data, size_t data_size,
69 const char *salt, size_t salt_size)
71 struct crypt_hash *ctx = NULL;
74 if (crypt_hash_init(&ctx, hash_name))
77 if (version == 1 && (r = crypt_hash_write(ctx, salt, salt_size)))
80 if ((r = crypt_hash_write(ctx, data, data_size)))
83 if (version == 0 && (r = crypt_hash_write(ctx, salt, salt_size)))
86 r = crypt_hash_final(ctx, hash, hash_size);
88 crypt_hash_destroy(ctx);
92 static int mult_overflow(off_t *u, off_t b, size_t size)
94 *u = (uint64_t)b * size;
95 if ((off_t)(*u / size) != b || (off_t)*u < 0)
100 static int create_or_verify(struct crypt_device *cd, FILE *rd, FILE *wr,
101 off_t data_block, size_t data_block_size,
102 off_t hash_block, size_t hash_block_size,
103 off_t blocks, int version,
104 const char *hash_name, int verify,
105 char *calculated_digest, size_t digest_size,
106 const char *salt, size_t salt_size)
108 char left_block[hash_block_size];
109 char data_buffer[data_block_size];
110 char read_digest[digest_size];
111 size_t hash_per_block = 1 << get_bits_down(hash_block_size / digest_size);
112 size_t digest_size_full = 1 << get_bits_up(digest_size);
113 off_t blocks_to_write = (blocks + hash_per_block - 1) / hash_per_block;
114 off_t seek_rd, seek_wr;
119 if (mult_overflow(&seek_rd, data_block, data_block_size) ||
120 mult_overflow(&seek_wr, hash_block, hash_block_size)) {
121 log_err(cd, _("Device offset overflow.\n"));
125 if (fseeko(rd, seek_rd, SEEK_SET)) {
126 log_dbg("Cannot seek to requested position in data device.");
130 if (wr && fseeko(wr, seek_wr, SEEK_SET)) {
131 log_dbg("Cannot seek to requested position in hash device.");
135 memset(left_block, 0, hash_block_size);
136 while (blocks_to_write--) {
137 left_bytes = hash_block_size;
138 for (i = 0; i < hash_per_block; i++) {
142 if (fread(data_buffer, data_block_size, 1, rd) != 1) {
143 log_dbg("Cannot read data device block.");
147 if (verify_hash_block(hash_name, version,
148 calculated_digest, digest_size,
149 data_buffer, data_block_size,
156 if (fread(read_digest, digest_size, 1, wr) != 1) {
157 log_dbg("Cannot read digest form hash device.");
160 if (memcmp(read_digest, calculated_digest, digest_size)) {
161 log_err(cd, _("Verification failed at position %" PRIu64 ".\n"),
162 ftello(rd) - data_block_size);
166 if (fwrite(calculated_digest, digest_size, 1, wr) != 1) {
167 log_dbg("Cannot write digest to hash device.");
172 left_bytes -= digest_size;
174 if (digest_size_full - digest_size) {
176 r = verify_zero(cd, wr, digest_size_full - digest_size);
179 } else if (fwrite(left_block, digest_size_full - digest_size, 1, wr) != 1) {
180 log_dbg("Cannot write spare area to hash device.");
184 left_bytes -= digest_size_full;
187 if (wr && left_bytes) {
189 r = verify_zero(cd , wr, left_bytes);
192 } else if (fwrite(left_block, left_bytes, 1, wr) != 1) {
193 log_dbg("Cannot write remaining spare area to hash device.");
202 static int VERITY_create_or_verify_hash(struct crypt_device *cd,
205 const char *hash_name,
206 struct device *hash_device,
207 struct device *data_device,
208 size_t hash_block_size,
209 size_t data_block_size,
217 char calculated_digest[digest_size];
218 FILE *data_file = NULL;
219 FILE *hash_file = NULL, *hash_file_2;
220 off_t hash_level_block[VERITY_MAX_LEVELS];
221 off_t hash_level_size[VERITY_MAX_LEVELS];
222 off_t data_file_blocks, s;
223 size_t hash_per_block, hash_per_block_bits;
224 off_t data_device_size = 0, hash_device_size = 0;
228 log_dbg("Hash %s %s, data device %s, data blocks %" PRIu64
229 ", hash_device %s, offset %" PRIu64 ".",
230 verify ? "verification" : "creation", hash_name,
231 device_path(data_device), data_blocks,
232 device_path(hash_device), hash_position);
234 if (data_blocks < 0 || hash_position < 0) {
235 log_err(cd, _("Invalid size parameters for verity device.\n"));
240 r = device_size(data_device, &dev_size);
244 data_file_blocks = dev_size / data_block_size;
246 data_file_blocks = data_blocks;
248 if (mult_overflow(&data_device_size, data_blocks, data_block_size)) {
249 log_err(cd, _("Device offset overflow.\n"));
253 hash_per_block_bits = get_bits_down(hash_block_size / digest_size);
254 hash_per_block = 1 << hash_per_block_bits;
255 if (!hash_per_block_bits)
259 if (data_file_blocks) {
260 while (hash_per_block_bits * levels < 64 &&
261 (data_file_blocks - 1) >> (hash_per_block_bits * levels))
264 log_dbg("Using %d hash levels.", levels);
266 if (levels > VERITY_MAX_LEVELS) {
267 log_err(cd, _("Too many tree levels for verity volume.\n"));
271 for (i = levels - 1; i >= 0; i--) {
272 hash_level_block[i] = hash_position;
273 // verity position of block data_file_blocks at level i
274 s = data_file_blocks >> (i * hash_per_block_bits);
275 s = (s + hash_per_block - 1) / hash_per_block;
276 hash_level_size[i] = s;
277 if ((hash_position + s) < hash_position ||
278 (hash_position + s) < 0) {
279 log_err(cd, _("Device offset overflow.\n"));
285 if (mult_overflow(&hash_device_size, hash_position, hash_block_size)) {
286 log_err(cd, _("Device offset overflow.\n"));
290 log_dbg("Data device size required: %" PRIu64 " bytes.",
292 data_file = fopen(device_path(data_device), "r");
294 log_err(cd, _("Cannot open device %s.\n"),
295 device_path(data_device)
301 log_dbg("Hash device size required: %" PRIu64 " bytes.",
303 hash_file = fopen(device_path(hash_device), verify ? "r" : "r+");
305 log_err(cd, _("Cannot open device %s.\n"),
306 device_path(hash_device));
311 memset(calculated_digest, 0, digest_size);
313 for (i = 0; i < levels; i++) {
315 r = create_or_verify(cd, data_file, hash_file,
317 hash_level_block[i], hash_block_size,
318 data_file_blocks, version, hash_name, verify,
319 calculated_digest, digest_size, salt, salt_size);
323 hash_file_2 = fopen(device_path(hash_device), "r");
325 log_err(cd, _("Cannot open device %s.\n"),
326 device_path(hash_device));
330 r = create_or_verify(cd, hash_file_2, hash_file,
331 hash_level_block[i - 1], hash_block_size,
332 hash_level_block[i], hash_block_size,
333 hash_level_size[i - 1], version, hash_name, verify,
334 calculated_digest, digest_size, salt, salt_size);
342 r = create_or_verify(cd, hash_file, NULL,
343 hash_level_block[levels - 1], hash_block_size,
345 1, version, hash_name, verify,
346 calculated_digest, digest_size, salt, salt_size);
348 r = create_or_verify(cd, data_file, NULL,
351 data_file_blocks, version, hash_name, verify,
352 calculated_digest, digest_size, salt, salt_size);
356 log_err(cd, _("Verification of data area failed.\n"));
358 log_dbg("Verification of data area succeeded.");
359 r = memcmp(root_hash, calculated_digest, digest_size) ? -EPERM : 0;
361 log_err(cd, _("Verification of root hash failed.\n"));
363 log_dbg("Verification of root hash succeeded.");
367 log_err(cd, _("Input/output error while creating hash area.\n"));
369 log_err(cd, _("Creation of hash area failed.\n"));
371 fsync(fileno(hash_file));
372 memcpy(root_hash, calculated_digest, digest_size);
383 /* Verify verity device using userspace crypto backend */
384 int VERITY_verify(struct crypt_device *cd,
385 struct crypt_params_verity *verity_hdr,
386 const char *root_hash,
387 size_t root_hash_size)
389 return VERITY_create_or_verify_hash(cd, 1,
390 verity_hdr->hash_type,
391 verity_hdr->hash_name,
392 crypt_metadata_device(cd),
393 crypt_data_device(cd),
394 verity_hdr->hash_block_size,
395 verity_hdr->data_block_size,
396 verity_hdr->data_size,
397 VERITY_hash_offset_block(verity_hdr),
398 CONST_CAST(char*)root_hash,
401 verity_hdr->salt_size);
404 /* Create verity hash */
405 int VERITY_create(struct crypt_device *cd,
406 struct crypt_params_verity *verity_hdr,
408 size_t root_hash_size)
410 unsigned pgsize = crypt_getpagesize();
412 if (verity_hdr->salt_size > 256)
415 if (verity_hdr->data_block_size > pgsize)
416 log_err(cd, _("WARNING: Kernel cannot activate device if data "
417 "block size exceeds page size (%u).\n"), pgsize);
419 return VERITY_create_or_verify_hash(cd, 0,
420 verity_hdr->hash_type,
421 verity_hdr->hash_name,
422 crypt_metadata_device(cd),
423 crypt_data_device(cd),
424 verity_hdr->hash_block_size,
425 verity_hdr->data_block_size,
426 verity_hdr->data_size,
427 VERITY_hash_offset_block(verity_hdr),
431 verity_hdr->salt_size);