dm-verity-target.c 30 KB

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