dm-verity-target.c 28 KB

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