dm-verity-target.c 30 KB

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