61424300e87fb80457141a9a4f0e18c23c5267fd
[platform/upstream/cryptsetup.git] / lib / verity / verity_hash.c
1 /*
2  * dm-verity volume handling
3  *
4  * Copyright (C) 2012-2020 Red Hat, Inc. All rights reserved.
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdint.h>
26
27 #include "verity.h"
28 #include "internal.h"
29
30 #define VERITY_MAX_LEVELS       63
31
32 static unsigned get_bits_up(size_t u)
33 {
34         unsigned i = 0;
35         while ((1U << i) < u)
36                 i++;
37         return i;
38 }
39
40 static unsigned get_bits_down(size_t u)
41 {
42         unsigned i = 0;
43         while ((u >> i) > 1U)
44                 i++;
45         return i;
46 }
47
48 static int verify_zero(struct crypt_device *cd, FILE *wr, size_t bytes)
49 {
50         char block[bytes];
51         size_t i;
52
53         if (fread(block, bytes, 1, wr) != 1) {
54                 log_dbg(cd, "EIO while reading spare area.");
55                 return -EIO;
56         }
57         for (i = 0; i < bytes; i++)
58                 if (block[i]) {
59                         log_err(cd, _("Spare area is not zeroed at position %" PRIu64 "."),
60                                 ftello(wr) - bytes);
61                         return -EPERM;
62                 }
63         return 0;
64 }
65
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)
70 {
71         struct crypt_hash *ctx = NULL;
72         int r;
73
74         if (crypt_hash_init(&ctx, hash_name))
75                 return -EINVAL;
76
77         if (version == 1 && (r = crypt_hash_write(ctx, salt, salt_size)))
78                 goto out;
79
80         if ((r = crypt_hash_write(ctx, data, data_size)))
81                 goto out;
82
83         if (version == 0 && (r = crypt_hash_write(ctx, salt, salt_size)))
84                 goto out;
85
86         r = crypt_hash_final(ctx, hash, hash_size);
87 out:
88         crypt_hash_destroy(ctx);
89         return r;
90 }
91
92 static int mult_overflow(off_t *u, off_t b, size_t size)
93 {
94         *u = (uint64_t)b * size;
95         if ((off_t)(*u / size) != b || (off_t)*u < 0)
96                 return 1;
97         return 0;
98 }
99
100 static int hash_levels(size_t hash_block_size, size_t digest_size,
101                        off_t data_file_blocks, off_t *hash_position, int *levels,
102                        off_t *hash_level_block, off_t *hash_level_size)
103 {
104         size_t hash_per_block_bits;
105         off_t s, s_shift;
106         int i;
107
108         if (!digest_size)
109                 return -EINVAL;
110
111         hash_per_block_bits = get_bits_down(hash_block_size / digest_size);
112         if (!hash_per_block_bits)
113                 return -EINVAL;
114
115         *levels = 0;
116         while (hash_per_block_bits * *levels < 64 &&
117                (data_file_blocks - 1) >> (hash_per_block_bits * *levels))
118                 (*levels)++;
119
120         if (*levels > VERITY_MAX_LEVELS)
121                 return -EINVAL;
122
123         for (i = *levels - 1; i >= 0; i--) {
124                 if (hash_level_block)
125                         hash_level_block[i] = *hash_position;
126                 // verity position of block data_file_blocks at level i
127                 s_shift = (i + 1) * hash_per_block_bits;
128                 if (s_shift > 63)
129                         return -EINVAL;
130                 s = (data_file_blocks + ((off_t)1 << s_shift) - 1) >> ((i + 1) * hash_per_block_bits);
131                 if (hash_level_size)
132                         hash_level_size[i] = s;
133                 if ((*hash_position + s) < *hash_position ||
134                     (*hash_position + s) < 0)
135                         return -EINVAL;
136                 *hash_position += s;
137         }
138
139         return 0;
140 }
141
142 static int create_or_verify(struct crypt_device *cd, FILE *rd, FILE *wr,
143                                    off_t data_block, size_t data_block_size,
144                                    off_t hash_block, size_t hash_block_size,
145                                    off_t blocks, int version,
146                                    const char *hash_name, int verify,
147                                    char *calculated_digest, size_t digest_size,
148                                    const char *salt, size_t salt_size)
149 {
150         char left_block[hash_block_size];
151         char data_buffer[data_block_size];
152         char read_digest[digest_size];
153         size_t hash_per_block = 1 << get_bits_down(hash_block_size / digest_size);
154         size_t digest_size_full = 1 << get_bits_up(digest_size);
155         off_t blocks_to_write = (blocks + hash_per_block - 1) / hash_per_block;
156         off_t seek_rd, seek_wr;
157         size_t left_bytes;
158         unsigned i;
159         int r;
160
161         if (mult_overflow(&seek_rd, data_block, data_block_size) ||
162             mult_overflow(&seek_wr, hash_block, hash_block_size)) {
163                 log_err(cd, _("Device offset overflow."));
164                 return -EINVAL;
165         }
166
167         if (fseeko(rd, seek_rd, SEEK_SET)) {
168                 log_dbg(cd, "Cannot seek to requested position in data device.");
169                 return -EIO;
170         }
171
172         if (wr && fseeko(wr, seek_wr, SEEK_SET)) {
173                 log_dbg(cd, "Cannot seek to requested position in hash device.");
174                 return -EIO;
175         }
176
177         memset(left_block, 0, hash_block_size);
178         while (blocks_to_write--) {
179                 left_bytes = hash_block_size;
180                 for (i = 0; i < hash_per_block; i++) {
181                         if (!blocks)
182                                 break;
183                         blocks--;
184                         if (fread(data_buffer, data_block_size, 1, rd) != 1) {
185                                 log_dbg(cd, "Cannot read data device block.");
186                                 return -EIO;
187                         }
188
189                         if (verify_hash_block(hash_name, version,
190                                         calculated_digest, digest_size,
191                                         data_buffer, data_block_size,
192                                         salt, salt_size))
193                                 return -EINVAL;
194
195                         if (!wr)
196                                 break;
197                         if (verify) {
198                                 if (fread(read_digest, digest_size, 1, wr) != 1) {
199                                         log_dbg(cd, "Cannot read digest form hash device.");
200                                         return -EIO;
201                                 }
202                                 if (memcmp(read_digest, calculated_digest, digest_size)) {
203                                         log_err(cd, _("Verification failed at position %" PRIu64 "."),
204                                                 ftello(rd) - data_block_size);
205                                         return -EPERM;
206                                 }
207                         } else {
208                                 if (fwrite(calculated_digest, digest_size, 1, wr) != 1) {
209                                         log_dbg(cd, "Cannot write digest to hash device.");
210                                         return -EIO;
211                                 }
212                         }
213                         if (version == 0) {
214                                 left_bytes -= digest_size;
215                         } else {
216                                 if (digest_size_full - digest_size) {
217                                         if (verify) {
218                                                 r = verify_zero(cd, wr, digest_size_full - digest_size);
219                                                 if (r)
220                                                         return r;
221                                         } else if (fwrite(left_block, digest_size_full - digest_size, 1, wr) != 1) {
222                                                 log_dbg(cd, "Cannot write spare area to hash device.");
223                                                 return -EIO;
224                                         }
225                                 }
226                                 left_bytes -= digest_size_full;
227                         }
228                 }
229                 if (wr && left_bytes) {
230                         if (verify) {
231                                 r = verify_zero(cd , wr, left_bytes);
232                                 if (r)
233                                         return r;
234                         } else if (fwrite(left_block, left_bytes, 1, wr) != 1) {
235                                 log_dbg(cd, "Cannot write remaining spare area to hash device.");
236                                 return -EIO;
237                         }
238                 }
239         }
240
241         return 0;
242 }
243
244 static int VERITY_create_or_verify_hash(struct crypt_device *cd,
245         int verify,
246         int version,
247         const char *hash_name,
248         struct device *hash_device,
249         struct device *data_device,
250         size_t hash_block_size,
251         size_t data_block_size,
252         off_t data_blocks,
253         off_t hash_position,
254         char *root_hash,
255         size_t digest_size,
256         const char *salt,
257         size_t salt_size)
258 {
259         char calculated_digest[digest_size];
260         FILE *data_file = NULL;
261         FILE *hash_file = NULL, *hash_file_2;
262         off_t hash_level_block[VERITY_MAX_LEVELS];
263         off_t hash_level_size[VERITY_MAX_LEVELS];
264         off_t data_file_blocks;
265         off_t data_device_size = 0, hash_device_size = 0;
266         uint64_t dev_size;
267         int levels, i, r;
268
269         log_dbg(cd, "Hash %s %s, data device %s, data blocks %" PRIu64
270                 ", hash_device %s, offset %" PRIu64 ".",
271                 verify ? "verification" : "creation", hash_name,
272                 device_path(data_device), data_blocks,
273                 device_path(hash_device), hash_position);
274
275         if (data_blocks < 0 || hash_position < 0) {
276                 log_err(cd, _("Invalid size parameters for verity device."));
277                 return -EINVAL;
278         }
279
280         if (!data_blocks) {
281                 r = device_size(data_device, &dev_size);
282                 if (r < 0)
283                         return r;
284
285                 data_file_blocks = dev_size / data_block_size;
286         } else
287                 data_file_blocks = data_blocks;
288
289         if (mult_overflow(&data_device_size, data_blocks, data_block_size)) {
290                 log_err(cd, _("Device offset overflow."));
291                 return -EINVAL;
292         }
293
294         if (hash_levels(hash_block_size, digest_size, data_file_blocks, &hash_position,
295                 &levels, &hash_level_block[0], &hash_level_size[0])) {
296                 log_err(cd, _("Hash area overflow."));
297                 return -EINVAL;
298         }
299
300         log_dbg(cd, "Using %d hash levels.", levels);
301
302         if (mult_overflow(&hash_device_size, hash_position, hash_block_size)) {
303                 log_err(cd, _("Device offset overflow."));
304                 return -EINVAL;
305         }
306
307         log_dbg(cd, "Data device size required: %" PRIu64 " bytes.",
308                 data_device_size);
309         data_file = fopen(device_path(data_device), "r");
310         if (!data_file) {
311                 log_err(cd, _("Cannot open device %s."),
312                         device_path(data_device)
313                 );
314                 r = -EIO;
315                 goto out;
316         }
317
318         log_dbg(cd, "Hash device size required: %" PRIu64 " bytes.",
319                 hash_device_size);
320         hash_file = fopen(device_path(hash_device), verify ? "r" : "r+");
321         if (!hash_file) {
322                 log_err(cd, _("Cannot open device %s."),
323                         device_path(hash_device));
324                 r = -EIO;
325                 goto out;
326         }
327
328         memset(calculated_digest, 0, digest_size);
329
330         for (i = 0; i < levels; i++) {
331                 if (!i) {
332                         r = create_or_verify(cd, data_file, hash_file,
333                                                     0, data_block_size,
334                                                     hash_level_block[i], hash_block_size,
335                                                     data_file_blocks, version, hash_name, verify,
336                                                     calculated_digest, digest_size, salt, salt_size);
337                         if (r)
338                                 goto out;
339                 } else {
340                         hash_file_2 = fopen(device_path(hash_device), "r");
341                         if (!hash_file_2) {
342                                 log_err(cd, _("Cannot open device %s."),
343                                         device_path(hash_device));
344                                 r = -EIO;
345                                 goto out;
346                         }
347                         r = create_or_verify(cd, hash_file_2, hash_file,
348                                                     hash_level_block[i - 1], hash_block_size,
349                                                     hash_level_block[i], hash_block_size,
350                                                     hash_level_size[i - 1], version, hash_name, verify,
351                                                     calculated_digest, digest_size, salt, salt_size);
352                         fclose(hash_file_2);
353                         if (r)
354                                 goto out;
355                 }
356         }
357
358         if (levels)
359                 r = create_or_verify(cd, hash_file, NULL,
360                                             hash_level_block[levels - 1], hash_block_size,
361                                             0, hash_block_size,
362                                             1, version, hash_name, verify,
363                                             calculated_digest, digest_size, salt, salt_size);
364         else
365                 r = create_or_verify(cd, data_file, NULL,
366                                             0, data_block_size,
367                                             0, hash_block_size,
368                                             data_file_blocks, version, hash_name, verify,
369                                             calculated_digest, digest_size, salt, salt_size);
370 out:
371         if (verify) {
372                 if (r)
373                         log_err(cd, _("Verification of data area failed."));
374                 else {
375                         log_dbg(cd, "Verification of data area succeeded.");
376                         r = memcmp(root_hash, calculated_digest, digest_size) ? -EPERM : 0;
377                         if (r)
378                                 log_err(cd, _("Verification of root hash failed."));
379                         else
380                                 log_dbg(cd, "Verification of root hash succeeded.");
381                 }
382         } else {
383                 if (r == -EIO)
384                         log_err(cd, _("Input/output error while creating hash area."));
385                 else if (r)
386                         log_err(cd, _("Creation of hash area failed."));
387                 else {
388                         fsync(fileno(hash_file));
389                         memcpy(root_hash, calculated_digest, digest_size);
390                 }
391         }
392
393         if (data_file)
394                 fclose(data_file);
395         if (hash_file)
396                 fclose(hash_file);
397         return r;
398 }
399
400 /* Verify verity device using userspace crypto backend */
401 int VERITY_verify(struct crypt_device *cd,
402                   struct crypt_params_verity *verity_hdr,
403                   const char *root_hash,
404                   size_t root_hash_size)
405 {
406         return VERITY_create_or_verify_hash(cd, 1,
407                 verity_hdr->hash_type,
408                 verity_hdr->hash_name,
409                 crypt_metadata_device(cd),
410                 crypt_data_device(cd),
411                 verity_hdr->hash_block_size,
412                 verity_hdr->data_block_size,
413                 verity_hdr->data_size,
414                 VERITY_hash_offset_block(verity_hdr),
415                 CONST_CAST(char*)root_hash,
416                 root_hash_size,
417                 verity_hdr->salt,
418                 verity_hdr->salt_size);
419 }
420
421 /* Create verity hash */
422 int VERITY_create(struct crypt_device *cd,
423                   struct crypt_params_verity *verity_hdr,
424                   const char *root_hash,
425                   size_t root_hash_size)
426 {
427         unsigned pgsize = (unsigned)crypt_getpagesize();
428
429         if (verity_hdr->salt_size > 256)
430                 return -EINVAL;
431
432         if (verity_hdr->data_block_size > pgsize)
433                 log_err(cd, _("WARNING: Kernel cannot activate device if data "
434                               "block size exceeds page size (%u)."), pgsize);
435
436         return VERITY_create_or_verify_hash(cd, 0,
437                 verity_hdr->hash_type,
438                 verity_hdr->hash_name,
439                 crypt_metadata_device(cd),
440                 crypt_data_device(cd),
441                 verity_hdr->hash_block_size,
442                 verity_hdr->data_block_size,
443                 verity_hdr->data_size,
444                 VERITY_hash_offset_block(verity_hdr),
445                 CONST_CAST(char*)root_hash,
446                 root_hash_size,
447                 verity_hdr->salt,
448                 verity_hdr->salt_size);
449 }
450
451 uint64_t VERITY_hash_blocks(struct crypt_device *cd, struct crypt_params_verity *params)
452 {
453         off_t hash_position = 0;
454         int levels = 0;
455
456         if (hash_levels(params->hash_block_size, crypt_get_volume_key_size(cd),
457                 params->data_size, &hash_position, &levels, NULL, NULL))
458                 return 0;
459
460         return (uint64_t)hash_position;
461 }