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