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