2 * dm-verity volume handling
4 * Copyright (C) 2012-2021 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(cd, "EIO while reading spare area.");
57 for (i = 0; i < bytes; i++)
59 log_err(cd, _("Spare area is not zeroed at position %" PRIu64 "."),
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 hash_levels(size_t hash_block_size, size_t digest_size,
93 uint64_t data_file_blocks, uint64_t *hash_position, int *levels,
94 uint64_t *hash_level_block, uint64_t *hash_level_size)
96 size_t hash_per_block_bits;
103 hash_per_block_bits = get_bits_down(hash_block_size / digest_size);
104 if (!hash_per_block_bits)
108 while (hash_per_block_bits * *levels < 64 &&
109 (data_file_blocks - 1) >> (hash_per_block_bits * *levels))
112 if (*levels > VERITY_MAX_LEVELS)
115 for (i = *levels - 1; i >= 0; i--) {
116 if (hash_level_block)
117 hash_level_block[i] = *hash_position;
118 // verity position of block data_file_blocks at level i
119 s_shift = (i + 1) * hash_per_block_bits;
122 s = (data_file_blocks + ((uint64_t)1 << s_shift) - 1) >> ((i + 1) * hash_per_block_bits);
124 hash_level_size[i] = s;
125 if ((*hash_position + s) < *hash_position)
133 static int create_or_verify(struct crypt_device *cd, FILE *rd, FILE *wr,
134 uint64_t data_block, size_t data_block_size,
135 uint64_t hash_block, size_t hash_block_size,
136 uint64_t blocks, int version,
137 const char *hash_name, int verify,
138 char *calculated_digest, size_t digest_size,
139 const char *salt, size_t salt_size)
141 char left_block[hash_block_size];
142 char data_buffer[data_block_size];
143 char read_digest[digest_size];
144 size_t hash_per_block = 1 << get_bits_down(hash_block_size / digest_size);
145 size_t digest_size_full = 1 << get_bits_up(digest_size);
146 uint64_t blocks_to_write = (blocks + hash_per_block - 1) / hash_per_block;
147 uint64_t seek_rd, seek_wr;
152 if (uint64_mult_overflow(&seek_rd, data_block, data_block_size) ||
153 uint64_mult_overflow(&seek_wr, hash_block, hash_block_size)) {
154 log_err(cd, _("Device offset overflow."));
158 if (fseeko(rd, seek_rd, SEEK_SET)) {
159 log_dbg(cd, "Cannot seek to requested position in data device.");
163 if (wr && fseeko(wr, seek_wr, SEEK_SET)) {
164 log_dbg(cd, "Cannot seek to requested position in hash device.");
168 memset(left_block, 0, hash_block_size);
169 while (blocks_to_write--) {
170 left_bytes = hash_block_size;
171 for (i = 0; i < hash_per_block; i++) {
175 if (fread(data_buffer, data_block_size, 1, rd) != 1) {
176 log_dbg(cd, "Cannot read data device block.");
180 if (verify_hash_block(hash_name, version,
181 calculated_digest, digest_size,
182 data_buffer, data_block_size,
189 if (fread(read_digest, digest_size, 1, wr) != 1) {
190 log_dbg(cd, "Cannot read digest form hash device.");
193 if (memcmp(read_digest, calculated_digest, digest_size)) {
194 log_err(cd, _("Verification failed at position %" PRIu64 "."),
195 ftello(rd) - data_block_size);
199 if (fwrite(calculated_digest, digest_size, 1, wr) != 1) {
200 log_dbg(cd, "Cannot write digest to hash device.");
205 left_bytes -= digest_size;
207 if (digest_size_full - digest_size) {
209 r = verify_zero(cd, wr, digest_size_full - digest_size);
212 } else if (fwrite(left_block, digest_size_full - digest_size, 1, wr) != 1) {
213 log_dbg(cd, "Cannot write spare area to hash device.");
217 left_bytes -= digest_size_full;
220 if (wr && left_bytes) {
222 r = verify_zero(cd , wr, left_bytes);
225 } else if (fwrite(left_block, left_bytes, 1, wr) != 1) {
226 log_dbg(cd, "Cannot write remaining spare area to hash device.");
235 static int VERITY_create_or_verify_hash(struct crypt_device *cd, bool verify,
236 struct crypt_params_verity *params,
237 char *root_hash, size_t digest_size)
239 char calculated_digest[digest_size];
240 FILE *data_file = NULL;
241 FILE *hash_file = NULL, *hash_file_2;
242 uint64_t hash_level_block[VERITY_MAX_LEVELS];
243 uint64_t hash_level_size[VERITY_MAX_LEVELS];
244 uint64_t data_file_blocks;
245 uint64_t data_device_offset_max = 0, hash_device_offset_max = 0;
246 uint64_t hash_position = VERITY_hash_offset_block(params);
250 log_dbg(cd, "Hash %s %s, data device %s, data blocks %" PRIu64
251 ", hash_device %s, offset %" PRIu64 ".",
252 verify ? "verification" : "creation", params->hash_name,
253 device_path(crypt_data_device(cd)), params->data_size,
254 device_path(crypt_metadata_device(cd)), hash_position);
256 if (!params->data_size) {
257 r = device_size(crypt_data_device(cd), &dev_size);
261 data_file_blocks = dev_size / params->data_block_size;
263 data_file_blocks = params->data_size;
265 if (uint64_mult_overflow(&data_device_offset_max, params->data_size, params->data_block_size)) {
266 log_err(cd, _("Device offset overflow."));
269 log_dbg(cd, "Data device size required: %" PRIu64 " bytes.", data_device_offset_max);
271 if (hash_levels(params->hash_block_size, digest_size, data_file_blocks, &hash_position,
272 &levels, &hash_level_block[0], &hash_level_size[0])) {
273 log_err(cd, _("Hash area overflow."));
276 if (uint64_mult_overflow(&hash_device_offset_max, hash_position, params->hash_block_size)) {
277 log_err(cd, _("Device offset overflow."));
280 log_dbg(cd, "Hash device size required: %" PRIu64 " bytes.",
281 hash_device_offset_max - params->hash_area_offset);
282 log_dbg(cd, "Using %d hash levels.", levels);
284 data_file = fopen(device_path(crypt_data_device(cd)), "r");
286 log_err(cd, _("Cannot open device %s."),
287 device_path(crypt_data_device(cd))
293 hash_file = fopen(device_path(crypt_metadata_device(cd)), verify ? "r" : "r+");
295 log_err(cd, _("Cannot open device %s."),
296 device_path(crypt_metadata_device(cd)));
301 memset(calculated_digest, 0, digest_size);
303 for (i = 0; i < levels; i++) {
305 r = create_or_verify(cd, data_file, hash_file,
306 0, params->data_block_size,
307 hash_level_block[i], params->hash_block_size,
308 data_file_blocks, params->hash_type, params->hash_name, verify,
309 calculated_digest, digest_size, params->salt, params->salt_size);
313 hash_file_2 = fopen(device_path(crypt_metadata_device(cd)), "r");
315 log_err(cd, _("Cannot open device %s."),
316 device_path(crypt_metadata_device(cd)));
320 r = create_or_verify(cd, hash_file_2, hash_file,
321 hash_level_block[i - 1], params->hash_block_size,
322 hash_level_block[i], params->hash_block_size,
323 hash_level_size[i - 1], params->hash_type, params->hash_name, verify,
324 calculated_digest, digest_size, params->salt, params->salt_size);
332 r = create_or_verify(cd, hash_file, NULL,
333 hash_level_block[levels - 1], params->hash_block_size,
334 0, params->hash_block_size,
335 1, params->hash_type, params->hash_name, verify,
336 calculated_digest, digest_size, params->salt, params->salt_size);
338 r = create_or_verify(cd, data_file, NULL,
339 0, params->data_block_size,
340 0, params->hash_block_size,
341 data_file_blocks, params->hash_type, params->hash_name, verify,
342 calculated_digest, digest_size, params->salt, params->salt_size);
346 log_err(cd, _("Verification of data area failed."));
348 log_dbg(cd, "Verification of data area succeeded.");
349 r = memcmp(root_hash, calculated_digest, digest_size) ? -EFAULT : 0;
351 log_err(cd, _("Verification of root hash failed."));
353 log_dbg(cd, "Verification of root hash succeeded.");
357 log_err(cd, _("Input/output error while creating hash area."));
359 log_err(cd, _("Creation of hash area failed."));
361 fsync(fileno(hash_file));
362 memcpy(root_hash, calculated_digest, digest_size);
373 /* Verify verity device using userspace crypto backend */
374 int VERITY_verify(struct crypt_device *cd,
375 struct crypt_params_verity *verity_hdr,
376 const char *root_hash,
377 size_t root_hash_size)
379 return VERITY_create_or_verify_hash(cd, 1, verity_hdr, CONST_CAST(char*)root_hash, root_hash_size);
382 /* Create verity hash */
383 int VERITY_create(struct crypt_device *cd,
384 struct crypt_params_verity *verity_hdr,
385 const char *root_hash,
386 size_t root_hash_size)
388 unsigned pgsize = (unsigned)crypt_getpagesize();
390 if (verity_hdr->salt_size > 256)
393 if (verity_hdr->data_block_size > pgsize)
394 log_err(cd, _("WARNING: Kernel cannot activate device if data "
395 "block size exceeds page size (%u)."), pgsize);
397 return VERITY_create_or_verify_hash(cd, 0, verity_hdr, CONST_CAST(char*)root_hash, root_hash_size);
400 uint64_t VERITY_hash_blocks(struct crypt_device *cd, struct crypt_params_verity *params)
402 uint64_t hash_position = 0;
405 if (hash_levels(params->hash_block_size, crypt_get_volume_key_size(cd),
406 params->data_size, &hash_position, &levels, NULL, NULL))
409 return (uint64_t)hash_position;