dm verity: clean up duplicate hashing code
[linux-2.6-block.git] / drivers / md / dm-verity.c
CommitLineData
a4ffc152
MP
1/*
2 * Copyright (C) 2012 Red Hat, Inc.
3 *
4 * Author: Mikulas Patocka <mpatocka@redhat.com>
5 *
6 * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
7 *
8 * This file is released under the GPLv2.
9 *
10 * In the file "/sys/module/dm_verity/parameters/prefetch_cluster" you can set
11 * default prefetch value. Data are read in "prefetch_cluster" chunks from the
12 * hash device. Setting this greatly improves performance when data and hash
13 * are on the same disk on different partitions on devices with poor random
14 * access behavior.
15 */
16
17#include "dm-bufio.h"
18
19#include <linux/module.h>
20#include <linux/device-mapper.h>
65ff5b7d 21#include <linux/reboot.h>
a4ffc152
MP
22#include <crypto/hash.h>
23
24#define DM_MSG_PREFIX "verity"
25
65ff5b7d
ST
26#define DM_VERITY_ENV_LENGTH 42
27#define DM_VERITY_ENV_VAR_NAME "DM_VERITY_ERR_BLOCK_NR"
28
a4ffc152
MP
29#define DM_VERITY_DEFAULT_PREFETCH_SIZE 262144
30
31#define DM_VERITY_MAX_LEVELS 63
65ff5b7d
ST
32#define DM_VERITY_MAX_CORRUPTED_ERRS 100
33
34#define DM_VERITY_OPT_LOGGING "ignore_corruption"
35#define DM_VERITY_OPT_RESTART "restart_on_corruption"
a4ffc152
MP
36
37static unsigned dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE;
38
39module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, S_IRUGO | S_IWUSR);
40
65ff5b7d
ST
41enum verity_mode {
42 DM_VERITY_MODE_EIO,
43 DM_VERITY_MODE_LOGGING,
44 DM_VERITY_MODE_RESTART
45};
46
47enum verity_block_type {
48 DM_VERITY_BLOCK_TYPE_DATA,
49 DM_VERITY_BLOCK_TYPE_METADATA
50};
51
a4ffc152
MP
52struct dm_verity {
53 struct dm_dev *data_dev;
54 struct dm_dev *hash_dev;
55 struct dm_target *ti;
56 struct dm_bufio_client *bufio;
57 char *alg_name;
58 struct crypto_shash *tfm;
59 u8 *root_digest; /* digest of the root block */
60 u8 *salt; /* salt: its size is salt_size */
61 unsigned salt_size;
62 sector_t data_start; /* data offset in 512-byte sectors */
63 sector_t hash_start; /* hash start in blocks */
64 sector_t data_blocks; /* the number of data blocks */
65 sector_t hash_blocks; /* the number of hash blocks */
66 unsigned char data_dev_block_bits; /* log2(data blocksize) */
67 unsigned char hash_dev_block_bits; /* log2(hash blocksize) */
68 unsigned char hash_per_block_bits; /* log2(hashes in hash block) */
69 unsigned char levels; /* the number of tree levels */
70 unsigned char version;
71 unsigned digest_size; /* digest size for the current hash algorithm */
72 unsigned shash_descsize;/* the size of temporary space for crypto */
73 int hash_failed; /* set to 1 if hash of any block failed */
65ff5b7d
ST
74 enum verity_mode mode; /* mode for handling verification errors */
75 unsigned corrupted_errs;/* Number of errors for corrupted blocks */
a4ffc152 76
a4ffc152
MP
77 struct workqueue_struct *verify_wq;
78
79 /* starting blocks for each tree level. 0 is the lowest level. */
80 sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
81};
82
83struct dm_verity_io {
84 struct dm_verity *v;
a4ffc152 85
fe3265b1 86 /* original value of bio->bi_end_io */
a4ffc152 87 bio_end_io_t *orig_bi_end_io;
a4ffc152
MP
88
89 sector_t block;
90 unsigned n_blocks;
91
003b5c57 92 struct bvec_iter iter;
a4ffc152
MP
93
94 struct work_struct work;
95
a4ffc152
MP
96 /*
97 * Three variably-size fields follow this struct:
98 *
99 * u8 hash_desc[v->shash_descsize];
100 * u8 real_digest[v->digest_size];
101 * u8 want_digest[v->digest_size];
102 *
103 * To access them use: io_hash_desc(), io_real_digest() and io_want_digest().
104 */
105};
106
3b6b7813
MP
107struct dm_verity_prefetch_work {
108 struct work_struct work;
109 struct dm_verity *v;
110 sector_t block;
111 unsigned n_blocks;
112};
113
a4ffc152
MP
114static struct shash_desc *io_hash_desc(struct dm_verity *v, struct dm_verity_io *io)
115{
116 return (struct shash_desc *)(io + 1);
117}
118
119static u8 *io_real_digest(struct dm_verity *v, struct dm_verity_io *io)
120{
121 return (u8 *)(io + 1) + v->shash_descsize;
122}
123
124static u8 *io_want_digest(struct dm_verity *v, struct dm_verity_io *io)
125{
126 return (u8 *)(io + 1) + v->shash_descsize + v->digest_size;
127}
128
129/*
130 * Auxiliary structure appended to each dm-bufio buffer. If the value
131 * hash_verified is nonzero, hash of the block has been verified.
132 *
133 * The variable hash_verified is set to 0 when allocating the buffer, then
134 * it can be changed to 1 and it is never reset to 0 again.
135 *
136 * There is no lock around this value, a race condition can at worst cause
137 * that multiple processes verify the hash of the same buffer simultaneously
138 * and write 1 to hash_verified simultaneously.
139 * This condition is harmless, so we don't need locking.
140 */
141struct buffer_aux {
142 int hash_verified;
143};
144
145/*
146 * Initialize struct buffer_aux for a freshly created buffer.
147 */
148static void dm_bufio_alloc_callback(struct dm_buffer *buf)
149{
150 struct buffer_aux *aux = dm_bufio_get_aux_data(buf);
151
152 aux->hash_verified = 0;
153}
154
155/*
156 * Translate input sector number to the sector number on the target device.
157 */
158static sector_t verity_map_sector(struct dm_verity *v, sector_t bi_sector)
159{
160 return v->data_start + dm_target_offset(v->ti, bi_sector);
161}
162
163/*
164 * Return hash position of a specified block at a specified tree level
165 * (0 is the lowest level).
166 * The lowest "hash_per_block_bits"-bits of the result denote hash position
167 * inside a hash block. The remaining bits denote location of the hash block.
168 */
169static sector_t verity_position_at_level(struct dm_verity *v, sector_t block,
170 int level)
171{
172 return block >> (level * v->hash_per_block_bits);
173}
174
6dbeda34
ST
175/*
176 * Wrapper for crypto_shash_init, which handles verity salting.
177 */
178static int verity_hash_init(struct dm_verity *v, struct shash_desc *desc)
179{
180 int r;
181
182 desc->tfm = v->tfm;
183 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
184
185 r = crypto_shash_init(desc);
186
187 if (unlikely(r < 0)) {
188 DMERR("crypto_shash_init failed: %d", r);
189 return r;
190 }
191
192 if (likely(v->version >= 1)) {
193 r = crypto_shash_update(desc, v->salt, v->salt_size);
194
195 if (unlikely(r < 0)) {
196 DMERR("crypto_shash_update failed: %d", r);
197 return r;
198 }
199 }
200
201 return 0;
202}
203
204static int verity_hash_update(struct dm_verity *v, struct shash_desc *desc,
205 const u8 *data, size_t len)
206{
207 int r = crypto_shash_update(desc, data, len);
208
209 if (unlikely(r < 0))
210 DMERR("crypto_shash_update failed: %d", r);
211
212 return r;
213}
214
215static int verity_hash_final(struct dm_verity *v, struct shash_desc *desc,
216 u8 *digest)
217{
218 int r;
219
220 if (unlikely(!v->version)) {
221 r = crypto_shash_update(desc, v->salt, v->salt_size);
222
223 if (r < 0) {
224 DMERR("crypto_shash_update failed: %d", r);
225 return r;
226 }
227 }
228
229 r = crypto_shash_final(desc, digest);
230
231 if (unlikely(r < 0))
232 DMERR("crypto_shash_final failed: %d", r);
233
234 return r;
235}
236
237static int verity_hash(struct dm_verity *v, struct shash_desc *desc,
238 const u8 *data, size_t len, u8 *digest)
239{
240 int r;
241
242 r = verity_hash_init(v, desc);
243 if (unlikely(r < 0))
244 return r;
245
246 r = verity_hash_update(v, desc, data, len);
247 if (unlikely(r < 0))
248 return r;
249
250 return verity_hash_final(v, desc, digest);
251}
252
a4ffc152
MP
253static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level,
254 sector_t *hash_block, unsigned *offset)
255{
256 sector_t position = verity_position_at_level(v, block, level);
257 unsigned idx;
258
259 *hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits);
260
261 if (!offset)
262 return;
263
264 idx = position & ((1 << v->hash_per_block_bits) - 1);
265 if (!v->version)
266 *offset = idx * v->digest_size;
267 else
268 *offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits);
269}
270
65ff5b7d
ST
271/*
272 * Handle verification errors.
273 */
274static int verity_handle_err(struct dm_verity *v, enum verity_block_type type,
275 unsigned long long block)
276{
277 char verity_env[DM_VERITY_ENV_LENGTH];
278 char *envp[] = { verity_env, NULL };
279 const char *type_str = "";
280 struct mapped_device *md = dm_table_get_md(v->ti->table);
281
282 /* Corruption should be visible in device status in all modes */
283 v->hash_failed = 1;
284
285 if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS)
286 goto out;
287
288 v->corrupted_errs++;
289
290 switch (type) {
291 case DM_VERITY_BLOCK_TYPE_DATA:
292 type_str = "data";
293 break;
294 case DM_VERITY_BLOCK_TYPE_METADATA:
295 type_str = "metadata";
296 break;
297 default:
298 BUG();
299 }
300
301 DMERR("%s: %s block %llu is corrupted", v->data_dev->name, type_str,
302 block);
303
304 if (v->corrupted_errs == DM_VERITY_MAX_CORRUPTED_ERRS)
305 DMERR("%s: reached maximum errors", v->data_dev->name);
306
307 snprintf(verity_env, DM_VERITY_ENV_LENGTH, "%s=%d,%llu",
308 DM_VERITY_ENV_VAR_NAME, type, block);
309
310 kobject_uevent_env(&disk_to_dev(dm_disk(md))->kobj, KOBJ_CHANGE, envp);
311
312out:
313 if (v->mode == DM_VERITY_MODE_LOGGING)
314 return 0;
315
316 if (v->mode == DM_VERITY_MODE_RESTART)
317 kernel_restart("dm-verity device corrupted");
318
319 return 1;
320}
321
a4ffc152
MP
322/*
323 * Verify hash of a metadata block pertaining to the specified data block
324 * ("block" argument) at a specified level ("level" argument).
325 *
326 * On successful return, io_want_digest(v, io) contains the hash value for
327 * a lower tree level or for the data block (if we're at the lowest leve).
328 *
329 * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned.
330 * If "skip_unverified" is false, unverified buffer is hashed and verified
331 * against current value of io_want_digest(v, io).
332 */
6dbeda34
ST
333static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,
334 sector_t block, int level, bool skip_unverified,
335 u8 *want_digest)
a4ffc152 336{
a4ffc152
MP
337 struct dm_buffer *buf;
338 struct buffer_aux *aux;
339 u8 *data;
340 int r;
341 sector_t hash_block;
342 unsigned offset;
343
344 verity_hash_at_level(v, block, level, &hash_block, &offset);
345
346 data = dm_bufio_read(v->bufio, hash_block, &buf);
fc0a4461 347 if (IS_ERR(data))
a4ffc152
MP
348 return PTR_ERR(data);
349
350 aux = dm_bufio_get_aux_data(buf);
351
352 if (!aux->hash_verified) {
a4ffc152
MP
353 if (skip_unverified) {
354 r = 1;
355 goto release_ret_r;
356 }
357
6dbeda34
ST
358 r = verity_hash(v, io_hash_desc(v, io),
359 data, 1 << v->hash_dev_block_bits,
360 io_real_digest(v, io));
361 if (unlikely(r < 0))
a4ffc152 362 goto release_ret_r;
a4ffc152 363
6dbeda34
ST
364 if (likely(memcmp(io_real_digest(v, io), want_digest,
365 v->digest_size) == 0))
366 aux->hash_verified = 1;
367 else if (verity_handle_err(v,
368 DM_VERITY_BLOCK_TYPE_METADATA,
369 hash_block)) {
370 r = -EIO;
a4ffc152
MP
371 goto release_ret_r;
372 }
a4ffc152
MP
373 }
374
375 data += offset;
6dbeda34
ST
376 memcpy(want_digest, data, v->digest_size);
377 r = 0;
a4ffc152
MP
378
379release_ret_r:
380 dm_bufio_release(buf);
a4ffc152
MP
381 return r;
382}
383
6dbeda34
ST
384/*
385 * Find a hash for a given block, write it to digest and verify the integrity
386 * of the hash tree if necessary.
387 */
388static int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
389 sector_t block, u8 *digest)
390{
391 int i;
392 int r;
393
394 if (likely(v->levels)) {
395 /*
396 * First, we try to get the requested hash for
397 * the current block. If the hash block itself is
398 * verified, zero is returned. If it isn't, this
399 * function returns 1 and we fall back to whole
400 * chain verification.
401 */
402 r = verity_verify_level(v, io, block, 0, true, digest);
403 if (likely(r <= 0))
404 return r;
405 }
406
407 memcpy(digest, v->root_digest, v->digest_size);
408
409 for (i = v->levels - 1; i >= 0; i--) {
410 r = verity_verify_level(v, io, block, i, false, digest);
411 if (unlikely(r))
412 return r;
413 }
414
415 return 0;
416}
417
a4ffc152
MP
418/*
419 * Verify one "dm_verity_io" structure.
420 */
421static int verity_verify_io(struct dm_verity_io *io)
422{
423 struct dm_verity *v = io->v;
003b5c57
KO
424 struct bio *bio = dm_bio_from_per_bio_data(io,
425 v->ti->per_bio_data_size);
a4ffc152 426 unsigned b;
a4ffc152
MP
427
428 for (b = 0; b < io->n_blocks; b++) {
a4ffc152
MP
429 int r;
430 unsigned todo;
6dbeda34 431 struct shash_desc *desc = io_hash_desc(v, io);
a4ffc152 432
6dbeda34
ST
433 r = verity_hash_for_block(v, io, io->block + b,
434 io_want_digest(v, io));
435 if (unlikely(r < 0))
436 return r;
a4ffc152 437
6dbeda34
ST
438 r = verity_hash_init(v, desc);
439 if (unlikely(r < 0))
a4ffc152 440 return r;
a4ffc152 441
a4ffc152 442 todo = 1 << v->data_dev_block_bits;
3a774521 443 do {
a4ffc152 444 u8 *page;
3a774521 445 unsigned len;
003b5c57
KO
446 struct bio_vec bv = bio_iter_iovec(bio, io->iter);
447
448 page = kmap_atomic(bv.bv_page);
3a774521
MB
449 len = bv.bv_len;
450 if (likely(len >= todo))
451 len = todo;
6dbeda34
ST
452 r = verity_hash_update(v, desc, page + bv.bv_offset,
453 len);
a4ffc152 454 kunmap_atomic(page);
003b5c57 455
6dbeda34 456 if (unlikely(r < 0))
a4ffc152 457 return r;
003b5c57 458
3a774521
MB
459 bio_advance_iter(bio, &io->iter, len);
460 todo -= len;
461 } while (todo);
a4ffc152 462
6dbeda34
ST
463 r = verity_hash_final(v, desc, io_real_digest(v, io));
464 if (unlikely(r < 0))
a4ffc152 465 return r;
6dbeda34
ST
466
467 if (likely(memcmp(io_real_digest(v, io),
468 io_want_digest(v, io), v->digest_size) == 0))
469 continue;
470 else if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_DATA,
471 io->block + b))
472 return -EIO;
a4ffc152 473 }
a4ffc152
MP
474
475 return 0;
476}
477
478/*
479 * End one "io" structure with a given error.
480 */
481static void verity_finish_io(struct dm_verity_io *io, int error)
482{
a4ffc152 483 struct dm_verity *v = io->v;
e42c3f91 484 struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_bio_data_size);
a4ffc152
MP
485
486 bio->bi_end_io = io->orig_bi_end_io;
4246a0b6 487 bio->bi_error = error;
a4ffc152 488
4246a0b6 489 bio_endio(bio);
a4ffc152
MP
490}
491
492static void verity_work(struct work_struct *w)
493{
494 struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
495
496 verity_finish_io(io, verity_verify_io(io));
497}
498
4246a0b6 499static void verity_end_io(struct bio *bio)
a4ffc152
MP
500{
501 struct dm_verity_io *io = bio->bi_private;
502
4246a0b6
CH
503 if (bio->bi_error) {
504 verity_finish_io(io, bio->bi_error);
a4ffc152
MP
505 return;
506 }
507
508 INIT_WORK(&io->work, verity_work);
509 queue_work(io->v->verify_wq, &io->work);
510}
511
512/*
513 * Prefetch buffers for the specified io.
514 * The root buffer is not prefetched, it is assumed that it will be cached
515 * all the time.
516 */
3b6b7813 517static void verity_prefetch_io(struct work_struct *work)
a4ffc152 518{
3b6b7813
MP
519 struct dm_verity_prefetch_work *pw =
520 container_of(work, struct dm_verity_prefetch_work, work);
521 struct dm_verity *v = pw->v;
a4ffc152
MP
522 int i;
523
524 for (i = v->levels - 2; i >= 0; i--) {
525 sector_t hash_block_start;
526 sector_t hash_block_end;
3b6b7813
MP
527 verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL);
528 verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL);
a4ffc152 529 if (!i) {
fe5fe906 530 unsigned cluster = ACCESS_ONCE(dm_verity_prefetch_cluster);
a4ffc152
MP
531
532 cluster >>= v->data_dev_block_bits;
533 if (unlikely(!cluster))
534 goto no_prefetch_cluster;
535
536 if (unlikely(cluster & (cluster - 1)))
553d8fe0 537 cluster = 1 << __fls(cluster);
a4ffc152
MP
538
539 hash_block_start &= ~(sector_t)(cluster - 1);
540 hash_block_end |= cluster - 1;
541 if (unlikely(hash_block_end >= v->hash_blocks))
542 hash_block_end = v->hash_blocks - 1;
543 }
544no_prefetch_cluster:
545 dm_bufio_prefetch(v->bufio, hash_block_start,
546 hash_block_end - hash_block_start + 1);
547 }
3b6b7813
MP
548
549 kfree(pw);
550}
551
552static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io)
553{
554 struct dm_verity_prefetch_work *pw;
555
556 pw = kmalloc(sizeof(struct dm_verity_prefetch_work),
557 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
558
559 if (!pw)
560 return;
561
562 INIT_WORK(&pw->work, verity_prefetch_io);
563 pw->v = v;
564 pw->block = io->block;
565 pw->n_blocks = io->n_blocks;
566 queue_work(v->verify_wq, &pw->work);
a4ffc152
MP
567}
568
569/*
570 * Bio map function. It allocates dm_verity_io structure and bio vector and
571 * fills them. Then it issues prefetches and the I/O.
572 */
7de3ee57 573static int verity_map(struct dm_target *ti, struct bio *bio)
a4ffc152
MP
574{
575 struct dm_verity *v = ti->private;
576 struct dm_verity_io *io;
577
578 bio->bi_bdev = v->data_dev->bdev;
4f024f37 579 bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector);
a4ffc152 580
4f024f37 581 if (((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
a4ffc152
MP
582 ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) {
583 DMERR_LIMIT("unaligned io");
584 return -EIO;
585 }
586
f73a1c7d 587 if (bio_end_sector(bio) >>
a4ffc152
MP
588 (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
589 DMERR_LIMIT("io out of range");
590 return -EIO;
591 }
592
593 if (bio_data_dir(bio) == WRITE)
594 return -EIO;
595
e42c3f91 596 io = dm_per_bio_data(bio, ti->per_bio_data_size);
a4ffc152 597 io->v = v;
a4ffc152 598 io->orig_bi_end_io = bio->bi_end_io;
4f024f37
KO
599 io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
600 io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits;
a4ffc152
MP
601
602 bio->bi_end_io = verity_end_io;
603 bio->bi_private = io;
003b5c57 604 io->iter = bio->bi_iter;
a4ffc152 605
3b6b7813 606 verity_submit_prefetch(v, io);
a4ffc152
MP
607
608 generic_make_request(bio);
609
610 return DM_MAPIO_SUBMITTED;
611}
612
613/*
614 * Status: V (valid) or C (corruption found)
615 */
fd7c092e
MP
616static void verity_status(struct dm_target *ti, status_type_t type,
617 unsigned status_flags, char *result, unsigned maxlen)
a4ffc152
MP
618{
619 struct dm_verity *v = ti->private;
620 unsigned sz = 0;
621 unsigned x;
622
623 switch (type) {
624 case STATUSTYPE_INFO:
625 DMEMIT("%c", v->hash_failed ? 'C' : 'V');
626 break;
627 case STATUSTYPE_TABLE:
628 DMEMIT("%u %s %s %u %u %llu %llu %s ",
629 v->version,
630 v->data_dev->name,
631 v->hash_dev->name,
632 1 << v->data_dev_block_bits,
633 1 << v->hash_dev_block_bits,
634 (unsigned long long)v->data_blocks,
635 (unsigned long long)v->hash_start,
636 v->alg_name
637 );
638 for (x = 0; x < v->digest_size; x++)
639 DMEMIT("%02x", v->root_digest[x]);
640 DMEMIT(" ");
641 if (!v->salt_size)
642 DMEMIT("-");
643 else
644 for (x = 0; x < v->salt_size; x++)
645 DMEMIT("%02x", v->salt[x]);
65ff5b7d
ST
646 if (v->mode != DM_VERITY_MODE_EIO) {
647 DMEMIT(" 1 ");
648 switch (v->mode) {
649 case DM_VERITY_MODE_LOGGING:
650 DMEMIT(DM_VERITY_OPT_LOGGING);
651 break;
652 case DM_VERITY_MODE_RESTART:
653 DMEMIT(DM_VERITY_OPT_RESTART);
654 break;
655 default:
656 BUG();
657 }
658 }
a4ffc152
MP
659 break;
660 }
a4ffc152
MP
661}
662
e56f81e0
CH
663static int verity_prepare_ioctl(struct dm_target *ti,
664 struct block_device **bdev, fmode_t *mode)
a4ffc152
MP
665{
666 struct dm_verity *v = ti->private;
e56f81e0
CH
667
668 *bdev = v->data_dev->bdev;
a4ffc152
MP
669
670 if (v->data_start ||
671 ti->len != i_size_read(v->data_dev->bdev->bd_inode) >> SECTOR_SHIFT)
e56f81e0
CH
672 return 1;
673 return 0;
a4ffc152
MP
674}
675
a4ffc152
MP
676static int verity_iterate_devices(struct dm_target *ti,
677 iterate_devices_callout_fn fn, void *data)
678{
679 struct dm_verity *v = ti->private;
680
681 return fn(ti, v->data_dev, v->data_start, ti->len, data);
682}
683
684static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits)
685{
686 struct dm_verity *v = ti->private;
687
688 if (limits->logical_block_size < 1 << v->data_dev_block_bits)
689 limits->logical_block_size = 1 << v->data_dev_block_bits;
690
691 if (limits->physical_block_size < 1 << v->data_dev_block_bits)
692 limits->physical_block_size = 1 << v->data_dev_block_bits;
693
694 blk_limits_io_min(limits, limits->logical_block_size);
695}
696
697static void verity_dtr(struct dm_target *ti)
698{
699 struct dm_verity *v = ti->private;
700
701 if (v->verify_wq)
702 destroy_workqueue(v->verify_wq);
703
a4ffc152
MP
704 if (v->bufio)
705 dm_bufio_client_destroy(v->bufio);
706
707 kfree(v->salt);
708 kfree(v->root_digest);
709
710 if (v->tfm)
711 crypto_free_shash(v->tfm);
712
713 kfree(v->alg_name);
714
715 if (v->hash_dev)
716 dm_put_device(ti, v->hash_dev);
717
718 if (v->data_dev)
719 dm_put_device(ti, v->data_dev);
720
721 kfree(v);
722}
723
724/*
725 * Target parameters:
726 * <version> The current format is version 1.
727 * Vsn 0 is compatible with original Chromium OS releases.
728 * <data device>
729 * <hash device>
730 * <data block size>
731 * <hash block size>
732 * <the number of data blocks>
733 * <hash start block>
734 * <algorithm>
735 * <digest>
736 * <salt> Hex string or "-" if no salt.
737 */
738static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
739{
740 struct dm_verity *v;
65ff5b7d
ST
741 struct dm_arg_set as;
742 const char *opt_string;
743 unsigned int num, opt_params;
a4ffc152
MP
744 unsigned long long num_ll;
745 int r;
746 int i;
747 sector_t hash_position;
748 char dummy;
749
65ff5b7d
ST
750 static struct dm_arg _args[] = {
751 {0, 1, "Invalid number of feature args"},
752 };
753
a4ffc152
MP
754 v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
755 if (!v) {
756 ti->error = "Cannot allocate verity structure";
757 return -ENOMEM;
758 }
759 ti->private = v;
760 v->ti = ti;
761
762 if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) {
763 ti->error = "Device must be readonly";
764 r = -EINVAL;
765 goto bad;
766 }
767
65ff5b7d
ST
768 if (argc < 10) {
769 ti->error = "Not enough arguments";
a4ffc152
MP
770 r = -EINVAL;
771 goto bad;
772 }
773
5d8be843
MP
774 if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
775 num > 1) {
a4ffc152
MP
776 ti->error = "Invalid version";
777 r = -EINVAL;
778 goto bad;
779 }
780 v->version = num;
781
782 r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev);
783 if (r) {
784 ti->error = "Data device lookup failed";
785 goto bad;
786 }
787
788 r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev);
789 if (r) {
790 ti->error = "Data device lookup failed";
791 goto bad;
792 }
793
794 if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
795 !num || (num & (num - 1)) ||
796 num < bdev_logical_block_size(v->data_dev->bdev) ||
797 num > PAGE_SIZE) {
798 ti->error = "Invalid data device block size";
799 r = -EINVAL;
800 goto bad;
801 }
553d8fe0 802 v->data_dev_block_bits = __ffs(num);
a4ffc152
MP
803
804 if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
805 !num || (num & (num - 1)) ||
806 num < bdev_logical_block_size(v->hash_dev->bdev) ||
807 num > INT_MAX) {
808 ti->error = "Invalid hash device block size";
809 r = -EINVAL;
810 goto bad;
811 }
553d8fe0 812 v->hash_dev_block_bits = __ffs(num);
a4ffc152
MP
813
814 if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
1d55f6bc
MP
815 (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
816 >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
a4ffc152
MP
817 ti->error = "Invalid data blocks";
818 r = -EINVAL;
819 goto bad;
820 }
821 v->data_blocks = num_ll;
822
823 if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
824 ti->error = "Data device is too small";
825 r = -EINVAL;
826 goto bad;
827 }
828
829 if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
1d55f6bc
MP
830 (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
831 >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
a4ffc152
MP
832 ti->error = "Invalid hash start";
833 r = -EINVAL;
834 goto bad;
835 }
836 v->hash_start = num_ll;
837
838 v->alg_name = kstrdup(argv[7], GFP_KERNEL);
839 if (!v->alg_name) {
840 ti->error = "Cannot allocate algorithm name";
841 r = -ENOMEM;
842 goto bad;
843 }
844
845 v->tfm = crypto_alloc_shash(v->alg_name, 0, 0);
846 if (IS_ERR(v->tfm)) {
847 ti->error = "Cannot initialize hash function";
848 r = PTR_ERR(v->tfm);
849 v->tfm = NULL;
850 goto bad;
851 }
852 v->digest_size = crypto_shash_digestsize(v->tfm);
853 if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
854 ti->error = "Digest size too big";
855 r = -EINVAL;
856 goto bad;
857 }
858 v->shash_descsize =
859 sizeof(struct shash_desc) + crypto_shash_descsize(v->tfm);
860
861 v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
862 if (!v->root_digest) {
863 ti->error = "Cannot allocate root digest";
864 r = -ENOMEM;
865 goto bad;
866 }
867 if (strlen(argv[8]) != v->digest_size * 2 ||
868 hex2bin(v->root_digest, argv[8], v->digest_size)) {
869 ti->error = "Invalid root digest";
870 r = -EINVAL;
871 goto bad;
872 }
873
874 if (strcmp(argv[9], "-")) {
875 v->salt_size = strlen(argv[9]) / 2;
876 v->salt = kmalloc(v->salt_size, GFP_KERNEL);
877 if (!v->salt) {
878 ti->error = "Cannot allocate salt";
879 r = -ENOMEM;
880 goto bad;
881 }
882 if (strlen(argv[9]) != v->salt_size * 2 ||
883 hex2bin(v->salt, argv[9], v->salt_size)) {
884 ti->error = "Invalid salt";
885 r = -EINVAL;
886 goto bad;
887 }
888 }
889
65ff5b7d
ST
890 argv += 10;
891 argc -= 10;
892
893 /* Optional parameters */
894 if (argc) {
895 as.argc = argc;
896 as.argv = argv;
897
898 r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
899 if (r)
900 goto bad;
901
902 while (opt_params) {
903 opt_params--;
904 opt_string = dm_shift_arg(&as);
905 if (!opt_string) {
906 ti->error = "Not enough feature arguments";
907 r = -EINVAL;
908 goto bad;
909 }
910
911 if (!strcasecmp(opt_string, DM_VERITY_OPT_LOGGING))
912 v->mode = DM_VERITY_MODE_LOGGING;
913 else if (!strcasecmp(opt_string, DM_VERITY_OPT_RESTART))
914 v->mode = DM_VERITY_MODE_RESTART;
915 else {
916 ti->error = "Invalid feature arguments";
917 r = -EINVAL;
918 goto bad;
919 }
920 }
921 }
922
a4ffc152 923 v->hash_per_block_bits =
553d8fe0 924 __fls((1 << v->hash_dev_block_bits) / v->digest_size);
a4ffc152
MP
925
926 v->levels = 0;
927 if (v->data_blocks)
928 while (v->hash_per_block_bits * v->levels < 64 &&
929 (unsigned long long)(v->data_blocks - 1) >>
930 (v->hash_per_block_bits * v->levels))
931 v->levels++;
932
933 if (v->levels > DM_VERITY_MAX_LEVELS) {
934 ti->error = "Too many tree levels";
935 r = -E2BIG;
936 goto bad;
937 }
938
939 hash_position = v->hash_start;
940 for (i = v->levels - 1; i >= 0; i--) {
941 sector_t s;
942 v->hash_level_block[i] = hash_position;
b1bf2de0
MP
943 s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
944 >> ((i + 1) * v->hash_per_block_bits);
a4ffc152
MP
945 if (hash_position + s < hash_position) {
946 ti->error = "Hash device offset overflow";
947 r = -E2BIG;
948 goto bad;
949 }
950 hash_position += s;
951 }
952 v->hash_blocks = hash_position;
953
954 v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
955 1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
956 dm_bufio_alloc_callback, NULL);
957 if (IS_ERR(v->bufio)) {
958 ti->error = "Cannot initialize dm-bufio";
959 r = PTR_ERR(v->bufio);
960 v->bufio = NULL;
961 goto bad;
962 }
963
964 if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
965 ti->error = "Hash device is too small";
966 r = -E2BIG;
967 goto bad;
968 }
969
e42c3f91 970 ti->per_bio_data_size = roundup(sizeof(struct dm_verity_io) + v->shash_descsize + v->digest_size * 2, __alignof__(struct dm_verity_io));
a4ffc152 971
a4ffc152
MP
972 /* WQ_UNBOUND greatly improves performance when running on ramdisk */
973 v->verify_wq = alloc_workqueue("kverityd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, num_online_cpus());
974 if (!v->verify_wq) {
975 ti->error = "Cannot allocate workqueue";
976 r = -ENOMEM;
977 goto bad;
978 }
979
980 return 0;
981
982bad:
983 verity_dtr(ti);
984
985 return r;
986}
987
988static struct target_type verity_target = {
989 .name = "verity",
3b6b7813 990 .version = {1, 2, 0},
a4ffc152
MP
991 .module = THIS_MODULE,
992 .ctr = verity_ctr,
993 .dtr = verity_dtr,
994 .map = verity_map,
995 .status = verity_status,
e56f81e0 996 .prepare_ioctl = verity_prepare_ioctl,
a4ffc152
MP
997 .iterate_devices = verity_iterate_devices,
998 .io_hints = verity_io_hints,
999};
1000
1001static int __init dm_verity_init(void)
1002{
1003 int r;
1004
1005 r = dm_register_target(&verity_target);
1006 if (r < 0)
1007 DMERR("register failed %d", r);
1008
1009 return r;
1010}
1011
1012static void __exit dm_verity_exit(void)
1013{
1014 dm_unregister_target(&verity_target);
1015}
1016
1017module_init(dm_verity_init);
1018module_exit(dm_verity_exit);
1019
1020MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
1021MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>");
1022MODULE_AUTHOR("Will Drewry <wad@chromium.org>");
1023MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking");
1024MODULE_LICENSE("GPL");