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.
29 #define VERITY_MAX_LEVELS 63
31 static unsigned get_bits_up(size_t u)
39 static unsigned get_bits_down(size_t u)
47 static int verify_zero(struct crypt_device *cd, FILE *wr, size_t bytes)
52 if (fread(block, bytes, 1, wr) != 1) {
53 log_dbg("EIO while reading spare area.");
56 for (i = 0; i < bytes; i++)
58 log_err(cd, _("Spare area is not zeroed at position %" PRIu64 ".\n"),
65 static int verify_hash_block(const char *hash_name, int version,
66 char *hash, size_t hash_size,
67 const char *data, size_t data_size,
68 const char *salt, size_t salt_size)
70 struct crypt_hash *ctx = NULL;
73 if (crypt_hash_init(&ctx, hash_name))
76 if (version == 1 && (r = crypt_hash_write(ctx, salt, salt_size)))
79 if ((r = crypt_hash_write(ctx, data, data_size)))
82 if (version == 0 && (r = crypt_hash_write(ctx, salt, salt_size)))
85 r = crypt_hash_final(ctx, hash, hash_size);
87 crypt_hash_destroy(ctx);
91 static int create_or_verify(struct crypt_device *cd, FILE *rd, FILE *wr,
92 off_t data_block, size_t data_block_size,
93 off_t hash_block, size_t hash_block_size,
94 off_t blocks, int version,
95 const char *hash_name, int verify,
96 char *calculated_digest, size_t digest_size,
97 const char *salt, size_t salt_size)
99 char left_block[hash_block_size];
100 char data_buffer[data_block_size];
101 char read_digest[digest_size];
102 size_t hash_per_block = 1 << get_bits_down(hash_block_size / digest_size);
103 size_t digest_size_full = 1 << get_bits_up(digest_size);
104 off_t blocks_to_write = (blocks + hash_per_block - 1) / hash_per_block;
109 if (fseeko(rd, data_block * data_block_size, SEEK_SET)) {
110 log_dbg("Cannot seek to requested position in data device.");
114 if (wr && fseeko(wr, hash_block * hash_block_size, SEEK_SET)) {
115 log_dbg("Cannot seek to requested position in hash device.");
119 memset(left_block, 0, hash_block_size);
120 while (blocks_to_write--) {
121 left_bytes = hash_block_size;
122 for (i = 0; i < hash_per_block; i++) {
126 if (fread(data_buffer, data_block_size, 1, rd) != 1) {
127 log_dbg("Cannot read data device block.");
131 if (verify_hash_block(hash_name, version,
132 calculated_digest, digest_size,
133 data_buffer, data_block_size,
140 if (fread(read_digest, digest_size, 1, wr) != 1) {
141 log_dbg("Cannot read digest form hash device.");
144 if (memcmp(read_digest, calculated_digest, digest_size)) {
145 log_err(cd, _("Verification failed at position %" PRIu64 ".\n"),
146 ftello(rd) - data_block_size);
150 if (fwrite(calculated_digest, digest_size, 1, wr) != 1) {
151 log_dbg("Cannot write digest to hash device.");
156 left_bytes -= digest_size;
158 if (digest_size_full - digest_size) {
160 r = verify_zero(cd, wr, digest_size_full - digest_size);
163 } else if (fwrite(left_block, digest_size_full - digest_size, 1, wr) != 1) {
164 log_dbg("Cannot write spare area to hash device.");
168 left_bytes -= digest_size_full;
171 if (wr && left_bytes) {
173 r = verify_zero(cd , wr, left_bytes);
176 } else if (fwrite(left_block, left_bytes, 1, wr) != 1) {
177 log_dbg("Cannot write remaining spare area to hash device.");
186 static int VERITY_create_or_verify_hash(struct crypt_device *cd,
189 const char *hash_name,
190 const char *hash_device,
191 const char *data_device,
192 size_t hash_block_size,
193 size_t data_block_size,
201 char calculated_digest[digest_size];
202 FILE *data_file = NULL;
203 FILE *hash_file = NULL, *hash_file_2;
204 off_t hash_level_block[VERITY_MAX_LEVELS];
205 off_t hash_level_size[VERITY_MAX_LEVELS];
206 off_t data_file_blocks, s;
207 size_t hash_per_block, hash_per_block_bits;
208 uint64_t data_device_size = 0, hash_device_size = 0;
211 log_dbg("Hash %s %s, data device %s, data blocks %" PRIu64
212 ", hash_device %s, offset %" PRIu64 ".",
213 verify ? "verification" : "creation", hash_name,
214 data_device, data_blocks, hash_device, hash_position);
217 r = device_size(data_device, &data_device_size);
221 data_file_blocks = data_device_size / data_block_size;
223 data_device_size = data_blocks * data_block_size;
224 data_file_blocks = data_blocks;
227 hash_per_block_bits = get_bits_down(hash_block_size / digest_size);
228 hash_per_block = 1 << hash_per_block_bits;
229 if (!hash_per_block_bits)
233 if (data_file_blocks) {
234 while (hash_per_block_bits * levels < 64 &&
235 (data_file_blocks - 1) >> (hash_per_block_bits * levels))
238 log_dbg("Using %d hash levels.", levels);
240 if (levels > VERITY_MAX_LEVELS) {
241 log_err(cd, _("Too many tree levels for verity volume.\n"));
245 for (i = levels - 1; i >= 0; i--) {
246 hash_level_block[i] = hash_position;
247 // verity position of block data_file_blocks at level i
248 s = data_file_blocks >> (i * hash_per_block_bits);
249 s = (s + hash_per_block - 1) / hash_per_block;
250 hash_level_size[i] = s;
251 if (hash_position + s < hash_position ||
252 (hash_position + s) < 0 ||
253 (hash_position + s) != hash_position + s) {
254 log_dbg("Hash device offset overflow.");
259 hash_device_size = hash_position * hash_block_size;
261 log_dbg("Data device size required: %" PRIu64 " bytes.",
263 data_file = fopen(data_device, "r");
265 log_err(cd, _("Cannot open device %s.\n"), data_device);
270 log_dbg("Hash device size required: %" PRIu64 " bytes.",
272 hash_file = fopen(hash_device, verify ? "r" : "r+");
274 log_err(cd, _("Cannot open device %s.\n"), hash_device);
279 memset(calculated_digest, 0, digest_size);
281 for (i = 0; i < levels; i++) {
283 r = create_or_verify(cd, data_file, hash_file,
285 hash_level_block[i], hash_block_size,
286 data_file_blocks, version, hash_name, verify,
287 calculated_digest, digest_size, salt, salt_size);
291 hash_file_2 = fopen(hash_device, "r");
293 log_err(cd, _("Cannot open device %s.\n"), hash_device);
297 r = create_or_verify(cd, hash_file_2, hash_file,
298 hash_level_block[i - 1], hash_block_size,
299 hash_level_block[i], hash_block_size,
300 hash_level_size[i - 1], version, hash_name, verify,
301 calculated_digest, digest_size, salt, salt_size);
309 r = create_or_verify(cd, hash_file, NULL,
310 hash_level_block[levels - 1], hash_block_size,
312 1, version, hash_name, verify,
313 calculated_digest, digest_size, salt, salt_size);
315 r = create_or_verify(cd, data_file, NULL,
318 data_file_blocks, version, hash_name, verify,
319 calculated_digest, digest_size, salt, salt_size);
323 log_err(cd, _("Verification of data area failed.\n"));
325 log_dbg("Verification of data area succeeded.");
326 r = memcmp(root_hash, calculated_digest, digest_size) ? -EPERM : 0;
328 log_err(cd, _("Verification of root hash failed.\n"));
330 log_dbg("Verification of root hash succeeded.");
334 log_err(cd, _("Input/output error while creating hash area.\n"));
336 log_err(cd, _("Creation of hash area failed.\n"));
338 fsync(fileno(hash_file));
339 memcpy(root_hash, calculated_digest, digest_size);
350 /* Verify verity device using userspace crypto backend */
351 int VERITY_verify(struct crypt_device *cd,
352 struct crypt_params_verity *verity_hdr,
353 const char *data_device,
354 const char *hash_device,
355 const char *root_hash,
356 size_t root_hash_size)
358 return VERITY_create_or_verify_hash(cd, 1,
359 verity_hdr->hash_type,
360 verity_hdr->hash_name,
363 verity_hdr->hash_block_size,
364 verity_hdr->data_block_size,
365 verity_hdr->data_size,
366 VERITY_hash_offset_block(verity_hdr),
367 CONST_CAST(char*)root_hash,
370 verity_hdr->salt_size);
373 /* Create verity hash */
374 int VERITY_create(struct crypt_device *cd,
375 struct crypt_params_verity *verity_hdr,
376 const char *data_device,
377 const char *hash_device,
379 size_t root_hash_size)
381 unsigned pgsize = crypt_getpagesize();
383 if (verity_hdr->salt_size > 256)
386 if (verity_hdr->hash_block_size > pgsize ||
387 verity_hdr->data_block_size > pgsize)
388 log_err(cd, _("WARNING: Kernel cannot activate device if block "
389 "size exceeds page size (%u).\n"), pgsize);
391 return VERITY_create_or_verify_hash(cd, 0,
392 verity_hdr->hash_type,
393 verity_hdr->hash_name,
396 verity_hdr->hash_block_size,
397 verity_hdr->data_block_size,
398 verity_hdr->data_size,
399 VERITY_hash_offset_block(verity_hdr),
403 verity_hdr->salt_size);