dm-verity-target.c 27 KB

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