blob: b0a53c3b926dad417f0a7040a64ccf32cbea8353 [file] [log] [blame]
Mikulas Patockaa4ffc152012-03-28 18:43:38 +01001/*
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>
Sami Tolvanen65ff5b72015-03-18 15:52:14 +000021#include <linux/reboot.h>
Mikulas Patockaa4ffc152012-03-28 18:43:38 +010022#include <crypto/hash.h>
23
24#define DM_MSG_PREFIX "verity"
25
Sami Tolvanen65ff5b72015-03-18 15:52:14 +000026#define DM_VERITY_ENV_LENGTH 42
27#define DM_VERITY_ENV_VAR_NAME "DM_VERITY_ERR_BLOCK_NR"
28
Mikulas Patockaa4ffc152012-03-28 18:43:38 +010029#define DM_VERITY_DEFAULT_PREFETCH_SIZE 262144
30
31#define DM_VERITY_MAX_LEVELS 63
Sami Tolvanen65ff5b72015-03-18 15:52:14 +000032#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"
Mikulas Patockaa4ffc152012-03-28 18:43:38 +010036
Sami Tolvanen753c1fd2015-11-05 02:02:32 +000037#define DM_VERITY_OPTS_MAX 1
38
Mikulas Patockaa4ffc152012-03-28 18:43:38 +010039static 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
Sami Tolvanen65ff5b72015-03-18 15:52:14 +000043enum 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
Mikulas Patockaa4ffc152012-03-28 18:43:38 +010054struct 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 */
Sami Tolvanen65ff5b72015-03-18 15:52:14 +000076 enum verity_mode mode; /* mode for handling verification errors */
77 unsigned corrupted_errs;/* Number of errors for corrupted blocks */
Mikulas Patockaa4ffc152012-03-28 18:43:38 +010078
Mikulas Patockaa4ffc152012-03-28 18:43:38 +010079 struct workqueue_struct *verify_wq;
80
81 /* starting blocks for each tree level. 0 is the lowest level. */
82 sector_t hash_level_block[DM_VERITY_MAX_LEVELS];
83};
84
85struct dm_verity_io {
86 struct dm_verity *v;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +010087
Mikulas Patockafe3265b2015-11-25 16:03:31 -050088 /* original value of bio->bi_end_io */
Mikulas Patockaa4ffc152012-03-28 18:43:38 +010089 bio_end_io_t *orig_bi_end_io;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +010090
91 sector_t block;
92 unsigned n_blocks;
93
Kent Overstreet003b5c52013-10-11 15:45:43 -070094 struct bvec_iter iter;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +010095
96 struct work_struct work;
97
Mikulas Patockaa4ffc152012-03-28 18:43:38 +010098 /*
99 * Three variably-size fields follow this struct:
100 *
101 * u8 hash_desc[v->shash_descsize];
102 * u8 real_digest[v->digest_size];
103 * u8 want_digest[v->digest_size];
104 *
105 * To access them use: io_hash_desc(), io_real_digest() and io_want_digest().
106 */
107};
108
Mikulas Patocka3b6b7812013-03-20 17:21:25 +0000109struct dm_verity_prefetch_work {
110 struct work_struct work;
111 struct dm_verity *v;
112 sector_t block;
113 unsigned n_blocks;
114};
115
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100116static struct shash_desc *io_hash_desc(struct dm_verity *v, struct dm_verity_io *io)
117{
118 return (struct shash_desc *)(io + 1);
119}
120
121static u8 *io_real_digest(struct dm_verity *v, struct dm_verity_io *io)
122{
123 return (u8 *)(io + 1) + v->shash_descsize;
124}
125
126static u8 *io_want_digest(struct dm_verity *v, struct dm_verity_io *io)
127{
128 return (u8 *)(io + 1) + v->shash_descsize + v->digest_size;
129}
130
131/*
132 * Auxiliary structure appended to each dm-bufio buffer. If the value
133 * hash_verified is nonzero, hash of the block has been verified.
134 *
135 * The variable hash_verified is set to 0 when allocating the buffer, then
136 * it can be changed to 1 and it is never reset to 0 again.
137 *
138 * There is no lock around this value, a race condition can at worst cause
139 * that multiple processes verify the hash of the same buffer simultaneously
140 * and write 1 to hash_verified simultaneously.
141 * This condition is harmless, so we don't need locking.
142 */
143struct buffer_aux {
144 int hash_verified;
145};
146
147/*
148 * Initialize struct buffer_aux for a freshly created buffer.
149 */
150static void dm_bufio_alloc_callback(struct dm_buffer *buf)
151{
152 struct buffer_aux *aux = dm_bufio_get_aux_data(buf);
153
154 aux->hash_verified = 0;
155}
156
157/*
158 * Translate input sector number to the sector number on the target device.
159 */
160static sector_t verity_map_sector(struct dm_verity *v, sector_t bi_sector)
161{
162 return v->data_start + dm_target_offset(v->ti, bi_sector);
163}
164
165/*
166 * Return hash position of a specified block at a specified tree level
167 * (0 is the lowest level).
168 * The lowest "hash_per_block_bits"-bits of the result denote hash position
169 * inside a hash block. The remaining bits denote location of the hash block.
170 */
171static sector_t verity_position_at_level(struct dm_verity *v, sector_t block,
172 int level)
173{
174 return block >> (level * v->hash_per_block_bits);
175}
176
Sami Tolvanen6dbeda32015-11-05 02:02:31 +0000177/*
178 * Wrapper for crypto_shash_init, which handles verity salting.
179 */
180static int verity_hash_init(struct dm_verity *v, struct shash_desc *desc)
181{
182 int r;
183
184 desc->tfm = v->tfm;
185 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
186
187 r = crypto_shash_init(desc);
188
189 if (unlikely(r < 0)) {
190 DMERR("crypto_shash_init failed: %d", r);
191 return r;
192 }
193
194 if (likely(v->version >= 1)) {
195 r = crypto_shash_update(desc, v->salt, v->salt_size);
196
197 if (unlikely(r < 0)) {
198 DMERR("crypto_shash_update failed: %d", r);
199 return r;
200 }
201 }
202
203 return 0;
204}
205
206static int verity_hash_update(struct dm_verity *v, struct shash_desc *desc,
207 const u8 *data, size_t len)
208{
209 int r = crypto_shash_update(desc, data, len);
210
211 if (unlikely(r < 0))
212 DMERR("crypto_shash_update failed: %d", r);
213
214 return r;
215}
216
217static int verity_hash_final(struct dm_verity *v, struct shash_desc *desc,
218 u8 *digest)
219{
220 int r;
221
222 if (unlikely(!v->version)) {
223 r = crypto_shash_update(desc, v->salt, v->salt_size);
224
225 if (r < 0) {
226 DMERR("crypto_shash_update failed: %d", r);
227 return r;
228 }
229 }
230
231 r = crypto_shash_final(desc, digest);
232
233 if (unlikely(r < 0))
234 DMERR("crypto_shash_final failed: %d", r);
235
236 return r;
237}
238
239static int verity_hash(struct dm_verity *v, struct shash_desc *desc,
240 const u8 *data, size_t len, u8 *digest)
241{
242 int r;
243
244 r = verity_hash_init(v, desc);
245 if (unlikely(r < 0))
246 return r;
247
248 r = verity_hash_update(v, desc, data, len);
249 if (unlikely(r < 0))
250 return r;
251
252 return verity_hash_final(v, desc, digest);
253}
254
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100255static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level,
256 sector_t *hash_block, unsigned *offset)
257{
258 sector_t position = verity_position_at_level(v, block, level);
259 unsigned idx;
260
261 *hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits);
262
263 if (!offset)
264 return;
265
266 idx = position & ((1 << v->hash_per_block_bits) - 1);
267 if (!v->version)
268 *offset = idx * v->digest_size;
269 else
270 *offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits);
271}
272
273/*
Sami Tolvanen65ff5b72015-03-18 15:52:14 +0000274 * Handle verification errors.
275 */
276static int verity_handle_err(struct dm_verity *v, enum verity_block_type type,
277 unsigned long long block)
278{
279 char verity_env[DM_VERITY_ENV_LENGTH];
280 char *envp[] = { verity_env, NULL };
281 const char *type_str = "";
282 struct mapped_device *md = dm_table_get_md(v->ti->table);
283
284 /* Corruption should be visible in device status in all modes */
285 v->hash_failed = 1;
286
287 if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS)
288 goto out;
289
290 v->corrupted_errs++;
291
292 switch (type) {
293 case DM_VERITY_BLOCK_TYPE_DATA:
294 type_str = "data";
295 break;
296 case DM_VERITY_BLOCK_TYPE_METADATA:
297 type_str = "metadata";
298 break;
299 default:
300 BUG();
301 }
302
303 DMERR("%s: %s block %llu is corrupted", v->data_dev->name, type_str,
304 block);
305
306 if (v->corrupted_errs == DM_VERITY_MAX_CORRUPTED_ERRS)
307 DMERR("%s: reached maximum errors", v->data_dev->name);
308
309 snprintf(verity_env, DM_VERITY_ENV_LENGTH, "%s=%d,%llu",
310 DM_VERITY_ENV_VAR_NAME, type, block);
311
312 kobject_uevent_env(&disk_to_dev(dm_disk(md))->kobj, KOBJ_CHANGE, envp);
313
314out:
315 if (v->mode == DM_VERITY_MODE_LOGGING)
316 return 0;
317
318 if (v->mode == DM_VERITY_MODE_RESTART)
319 kernel_restart("dm-verity device corrupted");
320
321 return 1;
322}
323
324/*
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100325 * Verify hash of a metadata block pertaining to the specified data block
326 * ("block" argument) at a specified level ("level" argument).
327 *
328 * On successful return, io_want_digest(v, io) contains the hash value for
329 * a lower tree level or for the data block (if we're at the lowest leve).
330 *
331 * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned.
332 * If "skip_unverified" is false, unverified buffer is hashed and verified
333 * against current value of io_want_digest(v, io).
334 */
Sami Tolvanen6dbeda32015-11-05 02:02:31 +0000335static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,
336 sector_t block, int level, bool skip_unverified,
337 u8 *want_digest)
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100338{
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100339 struct dm_buffer *buf;
340 struct buffer_aux *aux;
341 u8 *data;
342 int r;
343 sector_t hash_block;
344 unsigned offset;
345
346 verity_hash_at_level(v, block, level, &hash_block, &offset);
347
348 data = dm_bufio_read(v->bufio, hash_block, &buf);
viresh kumarfc0a4462015-08-10 11:42:26 +0530349 if (IS_ERR(data))
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100350 return PTR_ERR(data);
351
352 aux = dm_bufio_get_aux_data(buf);
353
354 if (!aux->hash_verified) {
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100355 if (skip_unverified) {
356 r = 1;
357 goto release_ret_r;
358 }
359
Sami Tolvanen6dbeda32015-11-05 02:02:31 +0000360 r = verity_hash(v, io_hash_desc(v, io),
361 data, 1 << v->hash_dev_block_bits,
362 io_real_digest(v, io));
363 if (unlikely(r < 0))
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100364 goto release_ret_r;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100365
Sami Tolvanen6dbeda32015-11-05 02:02:31 +0000366 if (likely(memcmp(io_real_digest(v, io), want_digest,
367 v->digest_size) == 0))
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100368 aux->hash_verified = 1;
Sami Tolvanen6dbeda32015-11-05 02:02:31 +0000369 else if (verity_handle_err(v,
370 DM_VERITY_BLOCK_TYPE_METADATA,
371 hash_block)) {
372 r = -EIO;
373 goto release_ret_r;
374 }
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100375 }
376
377 data += offset;
Sami Tolvanen6dbeda32015-11-05 02:02:31 +0000378 memcpy(want_digest, data, v->digest_size);
379 r = 0;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100380
381release_ret_r:
382 dm_bufio_release(buf);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100383 return r;
384}
385
386/*
Sami Tolvanen6dbeda32015-11-05 02:02:31 +0000387 * Find a hash for a given block, write it to digest and verify the integrity
388 * of the hash tree if necessary.
389 */
390static int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
391 sector_t block, u8 *digest)
392{
393 int i;
394 int r;
395
396 if (likely(v->levels)) {
397 /*
398 * First, we try to get the requested hash for
399 * the current block. If the hash block itself is
400 * verified, zero is returned. If it isn't, this
401 * function returns 1 and we fall back to whole
402 * chain verification.
403 */
404 r = verity_verify_level(v, io, block, 0, true, digest);
405 if (likely(r <= 0))
406 return r;
407 }
408
409 memcpy(digest, v->root_digest, v->digest_size);
410
411 for (i = v->levels - 1; i >= 0; i--) {
412 r = verity_verify_level(v, io, block, i, false, digest);
413 if (unlikely(r))
414 return r;
415 }
416
417 return 0;
418}
419
420/*
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100421 * Verify one "dm_verity_io" structure.
422 */
423static int verity_verify_io(struct dm_verity_io *io)
424{
425 struct dm_verity *v = io->v;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700426 struct bio *bio = dm_bio_from_per_bio_data(io,
427 v->ti->per_bio_data_size);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100428 unsigned b;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100429
430 for (b = 0; b < io->n_blocks; b++) {
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100431 int r;
432 unsigned todo;
Sami Tolvanen6dbeda32015-11-05 02:02:31 +0000433 struct shash_desc *desc = io_hash_desc(v, io);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100434
Sami Tolvanen6dbeda32015-11-05 02:02:31 +0000435 r = verity_hash_for_block(v, io, io->block + b,
436 io_want_digest(v, io));
437 if (unlikely(r < 0))
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100438 return r;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100439
Sami Tolvanen6dbeda32015-11-05 02:02:31 +0000440 r = verity_hash_init(v, desc);
441 if (unlikely(r < 0))
442 return r;
443
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100444 todo = 1 << v->data_dev_block_bits;
Milan Broz3a774522014-04-14 22:02:30 +0200445 do {
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100446 u8 *page;
Milan Broz3a774522014-04-14 22:02:30 +0200447 unsigned len;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700448 struct bio_vec bv = bio_iter_iovec(bio, io->iter);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100449
Kent Overstreet003b5c52013-10-11 15:45:43 -0700450 page = kmap_atomic(bv.bv_page);
Milan Broz3a774522014-04-14 22:02:30 +0200451 len = bv.bv_len;
452 if (likely(len >= todo))
453 len = todo;
Sami Tolvanen6dbeda32015-11-05 02:02:31 +0000454 r = verity_hash_update(v, desc, page + bv.bv_offset,
455 len);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100456 kunmap_atomic(page);
Kent Overstreet003b5c52013-10-11 15:45:43 -0700457
Sami Tolvanen6dbeda32015-11-05 02:02:31 +0000458 if (unlikely(r < 0))
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100459 return r;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700460
Milan Broz3a774522014-04-14 22:02:30 +0200461 bio_advance_iter(bio, &io->iter, len);
462 todo -= len;
463 } while (todo);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100464
Sami Tolvanen6dbeda32015-11-05 02:02:31 +0000465 r = verity_hash_final(v, desc, io_real_digest(v, io));
466 if (unlikely(r < 0))
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100467 return r;
Sami Tolvanen6dbeda32015-11-05 02:02:31 +0000468
469 if (likely(memcmp(io_real_digest(v, io),
470 io_want_digest(v, io), v->digest_size) == 0))
471 continue;
472 else if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_DATA,
473 io->block + b))
474 return -EIO;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100475 }
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100476
477 return 0;
478}
479
480/*
481 * End one "io" structure with a given error.
482 */
483static void verity_finish_io(struct dm_verity_io *io, int error)
484{
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100485 struct dm_verity *v = io->v;
Mikulas Patockae42c3f92012-12-21 20:23:38 +0000486 struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_bio_data_size);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100487
488 bio->bi_end_io = io->orig_bi_end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200489 bio->bi_error = error;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100490
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200491 bio_endio(bio);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100492}
493
494static void verity_work(struct work_struct *w)
495{
496 struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
497
498 verity_finish_io(io, verity_verify_io(io));
499}
500
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200501static void verity_end_io(struct bio *bio)
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100502{
503 struct dm_verity_io *io = bio->bi_private;
504
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200505 if (bio->bi_error) {
506 verity_finish_io(io, bio->bi_error);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100507 return;
508 }
509
510 INIT_WORK(&io->work, verity_work);
511 queue_work(io->v->verify_wq, &io->work);
512}
513
514/*
515 * Prefetch buffers for the specified io.
516 * The root buffer is not prefetched, it is assumed that it will be cached
517 * all the time.
518 */
Mikulas Patocka3b6b7812013-03-20 17:21:25 +0000519static void verity_prefetch_io(struct work_struct *work)
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100520{
Mikulas Patocka3b6b7812013-03-20 17:21:25 +0000521 struct dm_verity_prefetch_work *pw =
522 container_of(work, struct dm_verity_prefetch_work, work);
523 struct dm_verity *v = pw->v;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100524 int i;
525
526 for (i = v->levels - 2; i >= 0; i--) {
527 sector_t hash_block_start;
528 sector_t hash_block_end;
Mikulas Patocka3b6b7812013-03-20 17:21:25 +0000529 verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL);
530 verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100531 if (!i) {
Mikulas Patockafe5fe902012-10-12 16:59:46 +0100532 unsigned cluster = ACCESS_ONCE(dm_verity_prefetch_cluster);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100533
534 cluster >>= v->data_dev_block_bits;
535 if (unlikely(!cluster))
536 goto no_prefetch_cluster;
537
538 if (unlikely(cluster & (cluster - 1)))
Mikulas Patocka553d8fe2013-07-10 23:41:17 +0100539 cluster = 1 << __fls(cluster);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100540
541 hash_block_start &= ~(sector_t)(cluster - 1);
542 hash_block_end |= cluster - 1;
543 if (unlikely(hash_block_end >= v->hash_blocks))
544 hash_block_end = v->hash_blocks - 1;
545 }
546no_prefetch_cluster:
547 dm_bufio_prefetch(v->bufio, hash_block_start,
548 hash_block_end - hash_block_start + 1);
549 }
Mikulas Patocka3b6b7812013-03-20 17:21:25 +0000550
551 kfree(pw);
552}
553
554static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io)
555{
556 struct dm_verity_prefetch_work *pw;
557
558 pw = kmalloc(sizeof(struct dm_verity_prefetch_work),
559 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
560
561 if (!pw)
562 return;
563
564 INIT_WORK(&pw->work, verity_prefetch_io);
565 pw->v = v;
566 pw->block = io->block;
567 pw->n_blocks = io->n_blocks;
568 queue_work(v->verify_wq, &pw->work);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100569}
570
571/*
572 * Bio map function. It allocates dm_verity_io structure and bio vector and
573 * fills them. Then it issues prefetches and the I/O.
574 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000575static int verity_map(struct dm_target *ti, struct bio *bio)
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100576{
577 struct dm_verity *v = ti->private;
578 struct dm_verity_io *io;
579
580 bio->bi_bdev = v->data_dev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700581 bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100582
Kent Overstreet4f024f32013-10-11 15:44:27 -0700583 if (((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100584 ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) {
585 DMERR_LIMIT("unaligned io");
586 return -EIO;
587 }
588
Kent Overstreetf73a1c72012-09-25 15:05:12 -0700589 if (bio_end_sector(bio) >>
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100590 (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
591 DMERR_LIMIT("io out of range");
592 return -EIO;
593 }
594
595 if (bio_data_dir(bio) == WRITE)
596 return -EIO;
597
Mikulas Patockae42c3f92012-12-21 20:23:38 +0000598 io = dm_per_bio_data(bio, ti->per_bio_data_size);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100599 io->v = v;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100600 io->orig_bi_end_io = bio->bi_end_io;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700601 io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
602 io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100603
604 bio->bi_end_io = verity_end_io;
605 bio->bi_private = io;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700606 io->iter = bio->bi_iter;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100607
Mikulas Patocka3b6b7812013-03-20 17:21:25 +0000608 verity_submit_prefetch(v, io);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100609
610 generic_make_request(bio);
611
612 return DM_MAPIO_SUBMITTED;
613}
614
615/*
616 * Status: V (valid) or C (corruption found)
617 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +0000618static void verity_status(struct dm_target *ti, status_type_t type,
619 unsigned status_flags, char *result, unsigned maxlen)
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100620{
621 struct dm_verity *v = ti->private;
622 unsigned sz = 0;
623 unsigned x;
624
625 switch (type) {
626 case STATUSTYPE_INFO:
627 DMEMIT("%c", v->hash_failed ? 'C' : 'V');
628 break;
629 case STATUSTYPE_TABLE:
630 DMEMIT("%u %s %s %u %u %llu %llu %s ",
631 v->version,
632 v->data_dev->name,
633 v->hash_dev->name,
634 1 << v->data_dev_block_bits,
635 1 << v->hash_dev_block_bits,
636 (unsigned long long)v->data_blocks,
637 (unsigned long long)v->hash_start,
638 v->alg_name
639 );
640 for (x = 0; x < v->digest_size; x++)
641 DMEMIT("%02x", v->root_digest[x]);
642 DMEMIT(" ");
643 if (!v->salt_size)
644 DMEMIT("-");
645 else
646 for (x = 0; x < v->salt_size; x++)
647 DMEMIT("%02x", v->salt[x]);
Sami Tolvanen65ff5b72015-03-18 15:52:14 +0000648 if (v->mode != DM_VERITY_MODE_EIO) {
649 DMEMIT(" 1 ");
650 switch (v->mode) {
651 case DM_VERITY_MODE_LOGGING:
652 DMEMIT(DM_VERITY_OPT_LOGGING);
653 break;
654 case DM_VERITY_MODE_RESTART:
655 DMEMIT(DM_VERITY_OPT_RESTART);
656 break;
657 default:
658 BUG();
659 }
660 }
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100661 break;
662 }
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100663}
664
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200665static int verity_prepare_ioctl(struct dm_target *ti,
666 struct block_device **bdev, fmode_t *mode)
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100667{
668 struct dm_verity *v = ti->private;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200669
670 *bdev = v->data_dev->bdev;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100671
672 if (v->data_start ||
673 ti->len != i_size_read(v->data_dev->bdev->bd_inode) >> SECTOR_SHIFT)
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200674 return 1;
675 return 0;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100676}
677
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100678static int verity_iterate_devices(struct dm_target *ti,
679 iterate_devices_callout_fn fn, void *data)
680{
681 struct dm_verity *v = ti->private;
682
683 return fn(ti, v->data_dev, v->data_start, ti->len, data);
684}
685
686static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits)
687{
688 struct dm_verity *v = ti->private;
689
690 if (limits->logical_block_size < 1 << v->data_dev_block_bits)
691 limits->logical_block_size = 1 << v->data_dev_block_bits;
692
693 if (limits->physical_block_size < 1 << v->data_dev_block_bits)
694 limits->physical_block_size = 1 << v->data_dev_block_bits;
695
696 blk_limits_io_min(limits, limits->logical_block_size);
697}
698
699static void verity_dtr(struct dm_target *ti)
700{
701 struct dm_verity *v = ti->private;
702
703 if (v->verify_wq)
704 destroy_workqueue(v->verify_wq);
705
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100706 if (v->bufio)
707 dm_bufio_client_destroy(v->bufio);
708
709 kfree(v->salt);
710 kfree(v->root_digest);
711
712 if (v->tfm)
713 crypto_free_shash(v->tfm);
714
715 kfree(v->alg_name);
716
717 if (v->hash_dev)
718 dm_put_device(ti, v->hash_dev);
719
720 if (v->data_dev)
721 dm_put_device(ti, v->data_dev);
722
723 kfree(v);
724}
725
Sami Tolvanen753c1fd2015-11-05 02:02:32 +0000726static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v)
727{
728 int r;
729 unsigned argc;
730 struct dm_target *ti = v->ti;
731 const char *arg_name;
732
733 static struct dm_arg _args[] = {
734 {0, DM_VERITY_OPTS_MAX, "Invalid number of feature args"},
735 };
736
737 r = dm_read_arg_group(_args, as, &argc, &ti->error);
738 if (r)
739 return -EINVAL;
740
741 if (!argc)
742 return 0;
743
744 do {
745 arg_name = dm_shift_arg(as);
746 argc--;
747
748 if (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING)) {
749 v->mode = DM_VERITY_MODE_LOGGING;
750 continue;
751
752 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_RESTART)) {
753 v->mode = DM_VERITY_MODE_RESTART;
754 continue;
755 }
756
757 ti->error = "Unrecognized verity feature request";
758 return -EINVAL;
759 } while (argc && !r);
760
761 return r;
762}
763
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100764/*
765 * Target parameters:
766 * <version> The current format is version 1.
767 * Vsn 0 is compatible with original Chromium OS releases.
768 * <data device>
769 * <hash device>
770 * <data block size>
771 * <hash block size>
772 * <the number of data blocks>
773 * <hash start block>
774 * <algorithm>
775 * <digest>
776 * <salt> Hex string or "-" if no salt.
777 */
778static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
779{
780 struct dm_verity *v;
Sami Tolvanen65ff5b72015-03-18 15:52:14 +0000781 struct dm_arg_set as;
Sami Tolvanen753c1fd2015-11-05 02:02:32 +0000782 unsigned int num;
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100783 unsigned long long num_ll;
784 int r;
785 int i;
786 sector_t hash_position;
787 char dummy;
788
789 v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
790 if (!v) {
791 ti->error = "Cannot allocate verity structure";
792 return -ENOMEM;
793 }
794 ti->private = v;
795 v->ti = ti;
796
797 if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) {
798 ti->error = "Device must be readonly";
799 r = -EINVAL;
800 goto bad;
801 }
802
Sami Tolvanen65ff5b72015-03-18 15:52:14 +0000803 if (argc < 10) {
804 ti->error = "Not enough arguments";
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100805 r = -EINVAL;
806 goto bad;
807 }
808
Mikulas Patocka5d8be842013-07-10 23:41:17 +0100809 if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
810 num > 1) {
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100811 ti->error = "Invalid version";
812 r = -EINVAL;
813 goto bad;
814 }
815 v->version = num;
816
817 r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev);
818 if (r) {
819 ti->error = "Data device lookup failed";
820 goto bad;
821 }
822
823 r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev);
824 if (r) {
825 ti->error = "Data device lookup failed";
826 goto bad;
827 }
828
829 if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
830 !num || (num & (num - 1)) ||
831 num < bdev_logical_block_size(v->data_dev->bdev) ||
832 num > PAGE_SIZE) {
833 ti->error = "Invalid data device block size";
834 r = -EINVAL;
835 goto bad;
836 }
Mikulas Patocka553d8fe2013-07-10 23:41:17 +0100837 v->data_dev_block_bits = __ffs(num);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100838
839 if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
840 !num || (num & (num - 1)) ||
841 num < bdev_logical_block_size(v->hash_dev->bdev) ||
842 num > INT_MAX) {
843 ti->error = "Invalid hash device block size";
844 r = -EINVAL;
845 goto bad;
846 }
Mikulas Patocka553d8fe2013-07-10 23:41:17 +0100847 v->hash_dev_block_bits = __ffs(num);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100848
849 if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
Mikulas Patocka1d55f6b2012-09-26 23:45:48 +0100850 (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
851 >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100852 ti->error = "Invalid data blocks";
853 r = -EINVAL;
854 goto bad;
855 }
856 v->data_blocks = num_ll;
857
858 if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
859 ti->error = "Data device is too small";
860 r = -EINVAL;
861 goto bad;
862 }
863
864 if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
Mikulas Patocka1d55f6b2012-09-26 23:45:48 +0100865 (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
866 >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100867 ti->error = "Invalid hash start";
868 r = -EINVAL;
869 goto bad;
870 }
871 v->hash_start = num_ll;
872
873 v->alg_name = kstrdup(argv[7], GFP_KERNEL);
874 if (!v->alg_name) {
875 ti->error = "Cannot allocate algorithm name";
876 r = -ENOMEM;
877 goto bad;
878 }
879
880 v->tfm = crypto_alloc_shash(v->alg_name, 0, 0);
881 if (IS_ERR(v->tfm)) {
882 ti->error = "Cannot initialize hash function";
883 r = PTR_ERR(v->tfm);
884 v->tfm = NULL;
885 goto bad;
886 }
887 v->digest_size = crypto_shash_digestsize(v->tfm);
888 if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
889 ti->error = "Digest size too big";
890 r = -EINVAL;
891 goto bad;
892 }
893 v->shash_descsize =
894 sizeof(struct shash_desc) + crypto_shash_descsize(v->tfm);
895
896 v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
897 if (!v->root_digest) {
898 ti->error = "Cannot allocate root digest";
899 r = -ENOMEM;
900 goto bad;
901 }
902 if (strlen(argv[8]) != v->digest_size * 2 ||
903 hex2bin(v->root_digest, argv[8], v->digest_size)) {
904 ti->error = "Invalid root digest";
905 r = -EINVAL;
906 goto bad;
907 }
908
909 if (strcmp(argv[9], "-")) {
910 v->salt_size = strlen(argv[9]) / 2;
911 v->salt = kmalloc(v->salt_size, GFP_KERNEL);
912 if (!v->salt) {
913 ti->error = "Cannot allocate salt";
914 r = -ENOMEM;
915 goto bad;
916 }
917 if (strlen(argv[9]) != v->salt_size * 2 ||
918 hex2bin(v->salt, argv[9], v->salt_size)) {
919 ti->error = "Invalid salt";
920 r = -EINVAL;
921 goto bad;
922 }
923 }
924
Sami Tolvanen65ff5b72015-03-18 15:52:14 +0000925 argv += 10;
926 argc -= 10;
927
928 /* Optional parameters */
929 if (argc) {
930 as.argc = argc;
931 as.argv = argv;
932
Sami Tolvanen753c1fd2015-11-05 02:02:32 +0000933 r = verity_parse_opt_args(&as, v);
934 if (r < 0)
Sami Tolvanen65ff5b72015-03-18 15:52:14 +0000935 goto bad;
Sami Tolvanen65ff5b72015-03-18 15:52:14 +0000936 }
937
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100938 v->hash_per_block_bits =
Mikulas Patocka553d8fe2013-07-10 23:41:17 +0100939 __fls((1 << v->hash_dev_block_bits) / v->digest_size);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100940
941 v->levels = 0;
942 if (v->data_blocks)
943 while (v->hash_per_block_bits * v->levels < 64 &&
944 (unsigned long long)(v->data_blocks - 1) >>
945 (v->hash_per_block_bits * v->levels))
946 v->levels++;
947
948 if (v->levels > DM_VERITY_MAX_LEVELS) {
949 ti->error = "Too many tree levels";
950 r = -E2BIG;
951 goto bad;
952 }
953
954 hash_position = v->hash_start;
955 for (i = v->levels - 1; i >= 0; i--) {
956 sector_t s;
957 v->hash_level_block[i] = hash_position;
Mikulas Patockab1bf2de2013-07-10 23:41:16 +0100958 s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
959 >> ((i + 1) * v->hash_per_block_bits);
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100960 if (hash_position + s < hash_position) {
961 ti->error = "Hash device offset overflow";
962 r = -E2BIG;
963 goto bad;
964 }
965 hash_position += s;
966 }
967 v->hash_blocks = hash_position;
968
969 v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
970 1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
971 dm_bufio_alloc_callback, NULL);
972 if (IS_ERR(v->bufio)) {
973 ti->error = "Cannot initialize dm-bufio";
974 r = PTR_ERR(v->bufio);
975 v->bufio = NULL;
976 goto bad;
977 }
978
979 if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
980 ti->error = "Hash device is too small";
981 r = -E2BIG;
982 goto bad;
983 }
984
Mikulas Patockae42c3f92012-12-21 20:23:38 +0000985 ti->per_bio_data_size = roundup(sizeof(struct dm_verity_io) + v->shash_descsize + v->digest_size * 2, __alignof__(struct dm_verity_io));
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100986
Mikulas Patockaa4ffc152012-03-28 18:43:38 +0100987 /* WQ_UNBOUND greatly improves performance when running on ramdisk */
988 v->verify_wq = alloc_workqueue("kverityd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, num_online_cpus());
989 if (!v->verify_wq) {
990 ti->error = "Cannot allocate workqueue";
991 r = -ENOMEM;
992 goto bad;
993 }
994
995 return 0;
996
997bad:
998 verity_dtr(ti);
999
1000 return r;
1001}
1002
1003static struct target_type verity_target = {
1004 .name = "verity",
Mikulas Patocka3b6b7812013-03-20 17:21:25 +00001005 .version = {1, 2, 0},
Mikulas Patockaa4ffc152012-03-28 18:43:38 +01001006 .module = THIS_MODULE,
1007 .ctr = verity_ctr,
1008 .dtr = verity_dtr,
1009 .map = verity_map,
1010 .status = verity_status,
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001011 .prepare_ioctl = verity_prepare_ioctl,
Mikulas Patockaa4ffc152012-03-28 18:43:38 +01001012 .iterate_devices = verity_iterate_devices,
1013 .io_hints = verity_io_hints,
1014};
1015
1016static int __init dm_verity_init(void)
1017{
1018 int r;
1019
1020 r = dm_register_target(&verity_target);
1021 if (r < 0)
1022 DMERR("register failed %d", r);
1023
1024 return r;
1025}
1026
1027static void __exit dm_verity_exit(void)
1028{
1029 dm_unregister_target(&verity_target);
1030}
1031
1032module_init(dm_verity_init);
1033module_exit(dm_verity_exit);
1034
1035MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
1036MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>");
1037MODULE_AUTHOR("Will Drewry <wad@chromium.org>");
1038MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking");
1039MODULE_LICENSE("GPL");