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