dm-verity-target.c 28 KB

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