dm-verity-target.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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 (3 + 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. * Skip verity work in response to I/O error when system is shutting down.
  444. */
  445. static inline bool verity_is_system_shutting_down(void)
  446. {
  447. return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
  448. || system_state == SYSTEM_RESTART;
  449. }
  450. /*
  451. * End one "io" structure with a given error.
  452. */
  453. static void verity_finish_io(struct dm_verity_io *io, blk_status_t status)
  454. {
  455. struct dm_verity *v = io->v;
  456. struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
  457. bio->bi_end_io = io->orig_bi_end_io;
  458. bio->bi_status = status;
  459. verity_fec_finish_io(io);
  460. bio_endio(bio);
  461. }
  462. static void verity_work(struct work_struct *w)
  463. {
  464. struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
  465. verity_finish_io(io, errno_to_blk_status(verity_verify_io(io)));
  466. }
  467. static void verity_end_io(struct bio *bio)
  468. {
  469. struct dm_verity_io *io = bio->bi_private;
  470. if (bio->bi_status &&
  471. (!verity_fec_is_enabled(io->v) || verity_is_system_shutting_down())) {
  472. verity_finish_io(io, bio->bi_status);
  473. return;
  474. }
  475. INIT_WORK(&io->work, verity_work);
  476. queue_work(io->v->verify_wq, &io->work);
  477. }
  478. /*
  479. * Prefetch buffers for the specified io.
  480. * The root buffer is not prefetched, it is assumed that it will be cached
  481. * all the time.
  482. */
  483. static void verity_prefetch_io(struct work_struct *work)
  484. {
  485. struct dm_verity_prefetch_work *pw =
  486. container_of(work, struct dm_verity_prefetch_work, work);
  487. struct dm_verity *v = pw->v;
  488. int i;
  489. for (i = v->levels - 2; i >= 0; i--) {
  490. sector_t hash_block_start;
  491. sector_t hash_block_end;
  492. verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL);
  493. verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL);
  494. if (!i) {
  495. unsigned cluster = READ_ONCE(dm_verity_prefetch_cluster);
  496. cluster >>= v->data_dev_block_bits;
  497. if (unlikely(!cluster))
  498. goto no_prefetch_cluster;
  499. if (unlikely(cluster & (cluster - 1)))
  500. cluster = 1 << __fls(cluster);
  501. hash_block_start &= ~(sector_t)(cluster - 1);
  502. hash_block_end |= cluster - 1;
  503. if (unlikely(hash_block_end >= v->hash_blocks))
  504. hash_block_end = v->hash_blocks - 1;
  505. }
  506. no_prefetch_cluster:
  507. dm_bufio_prefetch(v->bufio, hash_block_start,
  508. hash_block_end - hash_block_start + 1);
  509. }
  510. kfree(pw);
  511. }
  512. static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io)
  513. {
  514. struct dm_verity_prefetch_work *pw;
  515. pw = kmalloc(sizeof(struct dm_verity_prefetch_work),
  516. GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
  517. if (!pw)
  518. return;
  519. INIT_WORK(&pw->work, verity_prefetch_io);
  520. pw->v = v;
  521. pw->block = io->block;
  522. pw->n_blocks = io->n_blocks;
  523. queue_work(v->verify_wq, &pw->work);
  524. }
  525. /*
  526. * Bio map function. It allocates dm_verity_io structure and bio vector and
  527. * fills them. Then it issues prefetches and the I/O.
  528. */
  529. static int verity_map(struct dm_target *ti, struct bio *bio)
  530. {
  531. struct dm_verity *v = ti->private;
  532. struct dm_verity_io *io;
  533. bio_set_dev(bio, v->data_dev->bdev);
  534. bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector);
  535. if (((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
  536. ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) {
  537. DMERR_LIMIT("unaligned io");
  538. return DM_MAPIO_KILL;
  539. }
  540. if (bio_end_sector(bio) >>
  541. (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
  542. DMERR_LIMIT("io out of range");
  543. return DM_MAPIO_KILL;
  544. }
  545. if (bio_data_dir(bio) == WRITE)
  546. return DM_MAPIO_KILL;
  547. io = dm_per_bio_data(bio, ti->per_io_data_size);
  548. io->v = v;
  549. io->orig_bi_end_io = bio->bi_end_io;
  550. io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
  551. io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits;
  552. bio->bi_end_io = verity_end_io;
  553. bio->bi_private = io;
  554. io->iter = bio->bi_iter;
  555. verity_fec_init_io(io);
  556. verity_submit_prefetch(v, io);
  557. generic_make_request(bio);
  558. return DM_MAPIO_SUBMITTED;
  559. }
  560. /*
  561. * Status: V (valid) or C (corruption found)
  562. */
  563. static void verity_status(struct dm_target *ti, status_type_t type,
  564. unsigned status_flags, char *result, unsigned maxlen)
  565. {
  566. struct dm_verity *v = ti->private;
  567. unsigned args = 0;
  568. unsigned sz = 0;
  569. unsigned x;
  570. switch (type) {
  571. case STATUSTYPE_INFO:
  572. DMEMIT("%c", v->hash_failed ? 'C' : 'V');
  573. break;
  574. case STATUSTYPE_TABLE:
  575. DMEMIT("%u %s %s %u %u %llu %llu %s ",
  576. v->version,
  577. v->data_dev->name,
  578. v->hash_dev->name,
  579. 1 << v->data_dev_block_bits,
  580. 1 << v->hash_dev_block_bits,
  581. (unsigned long long)v->data_blocks,
  582. (unsigned long long)v->hash_start,
  583. v->alg_name
  584. );
  585. for (x = 0; x < v->digest_size; x++)
  586. DMEMIT("%02x", v->root_digest[x]);
  587. DMEMIT(" ");
  588. if (!v->salt_size)
  589. DMEMIT("-");
  590. else
  591. for (x = 0; x < v->salt_size; x++)
  592. DMEMIT("%02x", v->salt[x]);
  593. if (v->mode != DM_VERITY_MODE_EIO)
  594. args++;
  595. if (verity_fec_is_enabled(v))
  596. args += DM_VERITY_OPTS_FEC;
  597. if (v->zero_digest)
  598. args++;
  599. if (v->validated_blocks)
  600. args++;
  601. if (!args)
  602. return;
  603. DMEMIT(" %u", args);
  604. if (v->mode != DM_VERITY_MODE_EIO) {
  605. DMEMIT(" ");
  606. switch (v->mode) {
  607. case DM_VERITY_MODE_LOGGING:
  608. DMEMIT(DM_VERITY_OPT_LOGGING);
  609. break;
  610. case DM_VERITY_MODE_RESTART:
  611. DMEMIT(DM_VERITY_OPT_RESTART);
  612. break;
  613. default:
  614. BUG();
  615. }
  616. }
  617. if (v->zero_digest)
  618. DMEMIT(" " DM_VERITY_OPT_IGN_ZEROES);
  619. if (v->validated_blocks)
  620. DMEMIT(" " DM_VERITY_OPT_AT_MOST_ONCE);
  621. sz = verity_fec_status_table(v, sz, result, maxlen);
  622. break;
  623. }
  624. }
  625. static int verity_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
  626. {
  627. struct dm_verity *v = ti->private;
  628. *bdev = v->data_dev->bdev;
  629. if (v->data_start ||
  630. ti->len != i_size_read(v->data_dev->bdev->bd_inode) >> SECTOR_SHIFT)
  631. return 1;
  632. return 0;
  633. }
  634. static int verity_iterate_devices(struct dm_target *ti,
  635. iterate_devices_callout_fn fn, void *data)
  636. {
  637. struct dm_verity *v = ti->private;
  638. return fn(ti, v->data_dev, v->data_start, ti->len, data);
  639. }
  640. static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits)
  641. {
  642. struct dm_verity *v = ti->private;
  643. if (limits->logical_block_size < 1 << v->data_dev_block_bits)
  644. limits->logical_block_size = 1 << v->data_dev_block_bits;
  645. if (limits->physical_block_size < 1 << v->data_dev_block_bits)
  646. limits->physical_block_size = 1 << v->data_dev_block_bits;
  647. blk_limits_io_min(limits, limits->logical_block_size);
  648. }
  649. static void verity_dtr(struct dm_target *ti)
  650. {
  651. struct dm_verity *v = ti->private;
  652. if (v->verify_wq)
  653. destroy_workqueue(v->verify_wq);
  654. if (v->bufio)
  655. dm_bufio_client_destroy(v->bufio);
  656. kvfree(v->validated_blocks);
  657. kfree(v->salt);
  658. kfree(v->root_digest);
  659. kfree(v->zero_digest);
  660. if (v->tfm)
  661. crypto_free_ahash(v->tfm);
  662. kfree(v->alg_name);
  663. if (v->hash_dev)
  664. dm_put_device(ti, v->hash_dev);
  665. if (v->data_dev)
  666. dm_put_device(ti, v->data_dev);
  667. verity_fec_dtr(v);
  668. kfree(v);
  669. }
  670. static int verity_alloc_most_once(struct dm_verity *v)
  671. {
  672. struct dm_target *ti = v->ti;
  673. /* the bitset can only handle INT_MAX blocks */
  674. if (v->data_blocks > INT_MAX) {
  675. ti->error = "device too large to use check_at_most_once";
  676. return -E2BIG;
  677. }
  678. v->validated_blocks = kvcalloc(BITS_TO_LONGS(v->data_blocks),
  679. sizeof(unsigned long),
  680. GFP_KERNEL);
  681. if (!v->validated_blocks) {
  682. ti->error = "failed to allocate bitset for check_at_most_once";
  683. return -ENOMEM;
  684. }
  685. return 0;
  686. }
  687. static int verity_alloc_zero_digest(struct dm_verity *v)
  688. {
  689. int r = -ENOMEM;
  690. struct ahash_request *req;
  691. u8 *zero_data;
  692. v->zero_digest = kmalloc(v->digest_size, GFP_KERNEL);
  693. if (!v->zero_digest)
  694. return r;
  695. req = kmalloc(v->ahash_reqsize, GFP_KERNEL);
  696. if (!req)
  697. return r; /* verity_dtr will free zero_digest */
  698. zero_data = kzalloc(1 << v->data_dev_block_bits, GFP_KERNEL);
  699. if (!zero_data)
  700. goto out;
  701. r = verity_hash(v, req, zero_data, 1 << v->data_dev_block_bits,
  702. v->zero_digest);
  703. out:
  704. kfree(req);
  705. kfree(zero_data);
  706. return r;
  707. }
  708. static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v)
  709. {
  710. int r;
  711. unsigned argc;
  712. struct dm_target *ti = v->ti;
  713. const char *arg_name;
  714. static const struct dm_arg _args[] = {
  715. {0, DM_VERITY_OPTS_MAX, "Invalid number of feature args"},
  716. };
  717. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  718. if (r)
  719. return -EINVAL;
  720. if (!argc)
  721. return 0;
  722. do {
  723. arg_name = dm_shift_arg(as);
  724. argc--;
  725. if (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING)) {
  726. v->mode = DM_VERITY_MODE_LOGGING;
  727. continue;
  728. } else if (!strcasecmp(arg_name, DM_VERITY_OPT_RESTART)) {
  729. v->mode = DM_VERITY_MODE_RESTART;
  730. continue;
  731. } else if (!strcasecmp(arg_name, DM_VERITY_OPT_IGN_ZEROES)) {
  732. r = verity_alloc_zero_digest(v);
  733. if (r) {
  734. ti->error = "Cannot allocate zero digest";
  735. return r;
  736. }
  737. continue;
  738. } else if (!strcasecmp(arg_name, DM_VERITY_OPT_AT_MOST_ONCE)) {
  739. r = verity_alloc_most_once(v);
  740. if (r)
  741. return r;
  742. continue;
  743. } else if (verity_is_fec_opt_arg(arg_name)) {
  744. r = verity_fec_parse_opt_args(as, v, &argc, arg_name);
  745. if (r)
  746. return r;
  747. continue;
  748. }
  749. ti->error = "Unrecognized verity feature request";
  750. return -EINVAL;
  751. } while (argc && !r);
  752. return r;
  753. }
  754. /*
  755. * Target parameters:
  756. * <version> The current format is version 1.
  757. * Vsn 0 is compatible with original Chromium OS releases.
  758. * <data device>
  759. * <hash device>
  760. * <data block size>
  761. * <hash block size>
  762. * <the number of data blocks>
  763. * <hash start block>
  764. * <algorithm>
  765. * <digest>
  766. * <salt> Hex string or "-" if no salt.
  767. */
  768. static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
  769. {
  770. struct dm_verity *v;
  771. struct dm_arg_set as;
  772. unsigned int num;
  773. unsigned long long num_ll;
  774. int r;
  775. int i;
  776. sector_t hash_position;
  777. char dummy;
  778. v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
  779. if (!v) {
  780. ti->error = "Cannot allocate verity structure";
  781. return -ENOMEM;
  782. }
  783. ti->private = v;
  784. v->ti = ti;
  785. r = verity_fec_ctr_alloc(v);
  786. if (r)
  787. goto bad;
  788. if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) {
  789. ti->error = "Device must be readonly";
  790. r = -EINVAL;
  791. goto bad;
  792. }
  793. if (argc < 10) {
  794. ti->error = "Not enough arguments";
  795. r = -EINVAL;
  796. goto bad;
  797. }
  798. if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
  799. num > 1) {
  800. ti->error = "Invalid version";
  801. r = -EINVAL;
  802. goto bad;
  803. }
  804. v->version = num;
  805. r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev);
  806. if (r) {
  807. ti->error = "Data device lookup failed";
  808. goto bad;
  809. }
  810. r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev);
  811. if (r) {
  812. ti->error = "Hash device lookup failed";
  813. goto bad;
  814. }
  815. if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
  816. !num || (num & (num - 1)) ||
  817. num < bdev_logical_block_size(v->data_dev->bdev) ||
  818. num > PAGE_SIZE) {
  819. ti->error = "Invalid data device block size";
  820. r = -EINVAL;
  821. goto bad;
  822. }
  823. v->data_dev_block_bits = __ffs(num);
  824. if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
  825. !num || (num & (num - 1)) ||
  826. num < bdev_logical_block_size(v->hash_dev->bdev) ||
  827. num > INT_MAX) {
  828. ti->error = "Invalid hash device block size";
  829. r = -EINVAL;
  830. goto bad;
  831. }
  832. v->hash_dev_block_bits = __ffs(num);
  833. if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
  834. (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
  835. >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
  836. ti->error = "Invalid data blocks";
  837. r = -EINVAL;
  838. goto bad;
  839. }
  840. v->data_blocks = num_ll;
  841. if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
  842. ti->error = "Data device is too small";
  843. r = -EINVAL;
  844. goto bad;
  845. }
  846. if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
  847. (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
  848. >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
  849. ti->error = "Invalid hash start";
  850. r = -EINVAL;
  851. goto bad;
  852. }
  853. v->hash_start = num_ll;
  854. v->alg_name = kstrdup(argv[7], GFP_KERNEL);
  855. if (!v->alg_name) {
  856. ti->error = "Cannot allocate algorithm name";
  857. r = -ENOMEM;
  858. goto bad;
  859. }
  860. v->tfm = crypto_alloc_ahash(v->alg_name, 0, 0);
  861. if (IS_ERR(v->tfm)) {
  862. ti->error = "Cannot initialize hash function";
  863. r = PTR_ERR(v->tfm);
  864. v->tfm = NULL;
  865. goto bad;
  866. }
  867. v->digest_size = crypto_ahash_digestsize(v->tfm);
  868. if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
  869. ti->error = "Digest size too big";
  870. r = -EINVAL;
  871. goto bad;
  872. }
  873. v->ahash_reqsize = sizeof(struct ahash_request) +
  874. crypto_ahash_reqsize(v->tfm);
  875. v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
  876. if (!v->root_digest) {
  877. ti->error = "Cannot allocate root digest";
  878. r = -ENOMEM;
  879. goto bad;
  880. }
  881. if (strlen(argv[8]) != v->digest_size * 2 ||
  882. hex2bin(v->root_digest, argv[8], v->digest_size)) {
  883. ti->error = "Invalid root digest";
  884. r = -EINVAL;
  885. goto bad;
  886. }
  887. if (strcmp(argv[9], "-")) {
  888. v->salt_size = strlen(argv[9]) / 2;
  889. v->salt = kmalloc(v->salt_size, GFP_KERNEL);
  890. if (!v->salt) {
  891. ti->error = "Cannot allocate salt";
  892. r = -ENOMEM;
  893. goto bad;
  894. }
  895. if (strlen(argv[9]) != v->salt_size * 2 ||
  896. hex2bin(v->salt, argv[9], v->salt_size)) {
  897. ti->error = "Invalid salt";
  898. r = -EINVAL;
  899. goto bad;
  900. }
  901. }
  902. argv += 10;
  903. argc -= 10;
  904. /* Optional parameters */
  905. if (argc) {
  906. as.argc = argc;
  907. as.argv = argv;
  908. r = verity_parse_opt_args(&as, v);
  909. if (r < 0)
  910. goto bad;
  911. }
  912. v->hash_per_block_bits =
  913. __fls((1 << v->hash_dev_block_bits) / v->digest_size);
  914. v->levels = 0;
  915. if (v->data_blocks)
  916. while (v->hash_per_block_bits * v->levels < 64 &&
  917. (unsigned long long)(v->data_blocks - 1) >>
  918. (v->hash_per_block_bits * v->levels))
  919. v->levels++;
  920. if (v->levels > DM_VERITY_MAX_LEVELS) {
  921. ti->error = "Too many tree levels";
  922. r = -E2BIG;
  923. goto bad;
  924. }
  925. hash_position = v->hash_start;
  926. for (i = v->levels - 1; i >= 0; i--) {
  927. sector_t s;
  928. v->hash_level_block[i] = hash_position;
  929. s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
  930. >> ((i + 1) * v->hash_per_block_bits);
  931. if (hash_position + s < hash_position) {
  932. ti->error = "Hash device offset overflow";
  933. r = -E2BIG;
  934. goto bad;
  935. }
  936. hash_position += s;
  937. }
  938. v->hash_blocks = hash_position;
  939. v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
  940. 1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
  941. dm_bufio_alloc_callback, NULL);
  942. if (IS_ERR(v->bufio)) {
  943. ti->error = "Cannot initialize dm-bufio";
  944. r = PTR_ERR(v->bufio);
  945. v->bufio = NULL;
  946. goto bad;
  947. }
  948. if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
  949. ti->error = "Hash device is too small";
  950. r = -E2BIG;
  951. goto bad;
  952. }
  953. /* WQ_UNBOUND greatly improves performance when running on ramdisk */
  954. v->verify_wq = alloc_workqueue("kverityd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, num_online_cpus());
  955. if (!v->verify_wq) {
  956. ti->error = "Cannot allocate workqueue";
  957. r = -ENOMEM;
  958. goto bad;
  959. }
  960. ti->per_io_data_size = sizeof(struct dm_verity_io) +
  961. v->ahash_reqsize + v->digest_size * 2;
  962. r = verity_fec_ctr(v);
  963. if (r)
  964. goto bad;
  965. ti->per_io_data_size = roundup(ti->per_io_data_size,
  966. __alignof__(struct dm_verity_io));
  967. return 0;
  968. bad:
  969. verity_dtr(ti);
  970. return r;
  971. }
  972. static struct target_type verity_target = {
  973. .name = "verity",
  974. .version = {1, 4, 0},
  975. .module = THIS_MODULE,
  976. .ctr = verity_ctr,
  977. .dtr = verity_dtr,
  978. .map = verity_map,
  979. .status = verity_status,
  980. .prepare_ioctl = verity_prepare_ioctl,
  981. .iterate_devices = verity_iterate_devices,
  982. .io_hints = verity_io_hints,
  983. };
  984. static int __init dm_verity_init(void)
  985. {
  986. int r;
  987. r = dm_register_target(&verity_target);
  988. if (r < 0)
  989. DMERR("register failed %d", r);
  990. return r;
  991. }
  992. static void __exit dm_verity_exit(void)
  993. {
  994. dm_unregister_target(&verity_target);
  995. }
  996. module_init(dm_verity_init);
  997. module_exit(dm_verity_exit);
  998. MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
  999. MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>");
  1000. MODULE_AUTHOR("Will Drewry <wad@chromium.org>");
  1001. MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking");
  1002. MODULE_LICENSE("GPL");