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