dm-verity-target.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Red Hat, Inc.
  4. *
  5. * Author: Mikulas Patocka <[email protected]>
  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. #include <linux/scatterlist.h>
  21. #include <linux/string.h>
  22. #include <linux/jump_label.h>
  23. #define DM_MSG_PREFIX "verity"
  24. #define DM_VERITY_ENV_LENGTH 42
  25. #define DM_VERITY_ENV_VAR_NAME "DM_VERITY_ERR_BLOCK_NR"
  26. #define DM_VERITY_DEFAULT_PREFETCH_SIZE 262144
  27. #define DM_VERITY_MAX_CORRUPTED_ERRS 100
  28. #define DM_VERITY_OPT_LOGGING "ignore_corruption"
  29. #define DM_VERITY_OPT_RESTART "restart_on_corruption"
  30. #define DM_VERITY_OPT_PANIC "panic_on_corruption"
  31. #define DM_VERITY_OPT_IGN_ZEROES "ignore_zero_blocks"
  32. #define DM_VERITY_OPT_AT_MOST_ONCE "check_at_most_once"
  33. #define DM_VERITY_OPT_TASKLET_VERIFY "try_verify_in_tasklet"
  34. #define DM_VERITY_OPTS_MAX (4 + DM_VERITY_OPTS_FEC + \
  35. DM_VERITY_ROOT_HASH_VERIFICATION_OPTS)
  36. static unsigned int dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE;
  37. module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, S_IRUGO | S_IWUSR);
  38. static DEFINE_STATIC_KEY_FALSE(use_tasklet_enabled);
  39. struct dm_verity_prefetch_work {
  40. struct work_struct work;
  41. struct dm_verity *v;
  42. sector_t block;
  43. unsigned int n_blocks;
  44. };
  45. /*
  46. * Auxiliary structure appended to each dm-bufio buffer. If the value
  47. * hash_verified is nonzero, hash of the block has been verified.
  48. *
  49. * The variable hash_verified is set to 0 when allocating the buffer, then
  50. * it can be changed to 1 and it is never reset to 0 again.
  51. *
  52. * There is no lock around this value, a race condition can at worst cause
  53. * that multiple processes verify the hash of the same buffer simultaneously
  54. * and write 1 to hash_verified simultaneously.
  55. * This condition is harmless, so we don't need locking.
  56. */
  57. struct buffer_aux {
  58. int hash_verified;
  59. };
  60. /*
  61. * Initialize struct buffer_aux for a freshly created buffer.
  62. */
  63. static void dm_bufio_alloc_callback(struct dm_buffer *buf)
  64. {
  65. struct buffer_aux *aux = dm_bufio_get_aux_data(buf);
  66. aux->hash_verified = 0;
  67. }
  68. /*
  69. * Translate input sector number to the sector number on the target device.
  70. */
  71. static sector_t verity_map_sector(struct dm_verity *v, sector_t bi_sector)
  72. {
  73. return v->data_start + dm_target_offset(v->ti, bi_sector);
  74. }
  75. /*
  76. * Return hash position of a specified block at a specified tree level
  77. * (0 is the lowest level).
  78. * The lowest "hash_per_block_bits"-bits of the result denote hash position
  79. * inside a hash block. The remaining bits denote location of the hash block.
  80. */
  81. static sector_t verity_position_at_level(struct dm_verity *v, sector_t block,
  82. int level)
  83. {
  84. return block >> (level * v->hash_per_block_bits);
  85. }
  86. static int verity_hash_update(struct dm_verity *v, struct ahash_request *req,
  87. const u8 *data, size_t len,
  88. struct crypto_wait *wait)
  89. {
  90. struct scatterlist sg;
  91. if (likely(!is_vmalloc_addr(data))) {
  92. sg_init_one(&sg, data, len);
  93. ahash_request_set_crypt(req, &sg, NULL, len);
  94. return crypto_wait_req(crypto_ahash_update(req), wait);
  95. } else {
  96. do {
  97. int r;
  98. size_t this_step = min_t(size_t, len, PAGE_SIZE - offset_in_page(data));
  99. flush_kernel_vmap_range((void *)data, this_step);
  100. sg_init_table(&sg, 1);
  101. sg_set_page(&sg, vmalloc_to_page(data), this_step, offset_in_page(data));
  102. ahash_request_set_crypt(req, &sg, NULL, this_step);
  103. r = crypto_wait_req(crypto_ahash_update(req), wait);
  104. if (unlikely(r))
  105. return r;
  106. data += this_step;
  107. len -= this_step;
  108. } while (len);
  109. return 0;
  110. }
  111. }
  112. /*
  113. * Wrapper for crypto_ahash_init, which handles verity salting.
  114. */
  115. static int verity_hash_init(struct dm_verity *v, struct ahash_request *req,
  116. struct crypto_wait *wait, bool may_sleep)
  117. {
  118. int r;
  119. ahash_request_set_tfm(req, v->tfm);
  120. ahash_request_set_callback(req,
  121. may_sleep ? CRYPTO_TFM_REQ_MAY_SLEEP | CRYPTO_TFM_REQ_MAY_BACKLOG : 0,
  122. crypto_req_done, (void *)wait);
  123. crypto_init_wait(wait);
  124. r = crypto_wait_req(crypto_ahash_init(req), wait);
  125. if (unlikely(r < 0)) {
  126. if (r != -ENOMEM)
  127. DMERR("crypto_ahash_init failed: %d", r);
  128. return r;
  129. }
  130. if (likely(v->salt_size && (v->version >= 1)))
  131. r = verity_hash_update(v, req, v->salt, v->salt_size, wait);
  132. return r;
  133. }
  134. static int verity_hash_final(struct dm_verity *v, struct ahash_request *req,
  135. u8 *digest, struct crypto_wait *wait)
  136. {
  137. int r;
  138. if (unlikely(v->salt_size && (!v->version))) {
  139. r = verity_hash_update(v, req, v->salt, v->salt_size, wait);
  140. if (r < 0) {
  141. DMERR("verity_hash_final failed updating salt: %d", r);
  142. goto out;
  143. }
  144. }
  145. ahash_request_set_crypt(req, NULL, digest, 0);
  146. r = crypto_wait_req(crypto_ahash_final(req), wait);
  147. out:
  148. return r;
  149. }
  150. int verity_hash(struct dm_verity *v, struct ahash_request *req,
  151. const u8 *data, size_t len, u8 *digest, bool may_sleep)
  152. {
  153. int r;
  154. struct crypto_wait wait;
  155. r = verity_hash_init(v, req, &wait, may_sleep);
  156. if (unlikely(r < 0))
  157. goto out;
  158. r = verity_hash_update(v, req, data, len, &wait);
  159. if (unlikely(r < 0))
  160. goto out;
  161. r = verity_hash_final(v, req, digest, &wait);
  162. out:
  163. return r;
  164. }
  165. static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level,
  166. sector_t *hash_block, unsigned int *offset)
  167. {
  168. sector_t position = verity_position_at_level(v, block, level);
  169. unsigned int idx;
  170. *hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits);
  171. if (!offset)
  172. return;
  173. idx = position & ((1 << v->hash_per_block_bits) - 1);
  174. if (!v->version)
  175. *offset = idx * v->digest_size;
  176. else
  177. *offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits);
  178. }
  179. /*
  180. * Handle verification errors.
  181. */
  182. static int verity_handle_err(struct dm_verity *v, enum verity_block_type type,
  183. unsigned long long block)
  184. {
  185. char verity_env[DM_VERITY_ENV_LENGTH];
  186. char *envp[] = { verity_env, NULL };
  187. const char *type_str = "";
  188. struct mapped_device *md = dm_table_get_md(v->ti->table);
  189. /* Corruption should be visible in device status in all modes */
  190. v->hash_failed = true;
  191. if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS)
  192. goto out;
  193. v->corrupted_errs++;
  194. switch (type) {
  195. case DM_VERITY_BLOCK_TYPE_DATA:
  196. type_str = "data";
  197. break;
  198. case DM_VERITY_BLOCK_TYPE_METADATA:
  199. type_str = "metadata";
  200. break;
  201. default:
  202. BUG();
  203. }
  204. DMERR_LIMIT("%s: %s block %llu is corrupted", v->data_dev->name,
  205. type_str, block);
  206. if (v->corrupted_errs == DM_VERITY_MAX_CORRUPTED_ERRS)
  207. DMERR("%s: reached maximum errors", v->data_dev->name);
  208. snprintf(verity_env, DM_VERITY_ENV_LENGTH, "%s=%d,%llu",
  209. DM_VERITY_ENV_VAR_NAME, type, block);
  210. kobject_uevent_env(&disk_to_dev(dm_disk(md))->kobj, KOBJ_CHANGE, envp);
  211. out:
  212. if (v->mode == DM_VERITY_MODE_LOGGING)
  213. return 0;
  214. if (v->mode == DM_VERITY_MODE_RESTART)
  215. kernel_restart("dm-verity device corrupted");
  216. if (v->mode == DM_VERITY_MODE_PANIC)
  217. panic("dm-verity device corrupted");
  218. return 1;
  219. }
  220. /*
  221. * Verify hash of a metadata block pertaining to the specified data block
  222. * ("block" argument) at a specified level ("level" argument).
  223. *
  224. * On successful return, verity_io_want_digest(v, io) contains the hash value
  225. * for a lower tree level or for the data block (if we're at the lowest level).
  226. *
  227. * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned.
  228. * If "skip_unverified" is false, unverified buffer is hashed and verified
  229. * against current value of verity_io_want_digest(v, io).
  230. */
  231. static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,
  232. sector_t block, int level, bool skip_unverified,
  233. u8 *want_digest)
  234. {
  235. struct dm_buffer *buf;
  236. struct buffer_aux *aux;
  237. u8 *data;
  238. int r;
  239. sector_t hash_block;
  240. unsigned int offset;
  241. verity_hash_at_level(v, block, level, &hash_block, &offset);
  242. if (static_branch_unlikely(&use_tasklet_enabled) && io->in_tasklet) {
  243. data = dm_bufio_get(v->bufio, hash_block, &buf);
  244. if (data == NULL) {
  245. /*
  246. * In tasklet and the hash was not in the bufio cache.
  247. * Return early and resume execution from a work-queue
  248. * to read the hash from disk.
  249. */
  250. return -EAGAIN;
  251. }
  252. } else
  253. data = dm_bufio_read(v->bufio, hash_block, &buf);
  254. if (IS_ERR(data))
  255. return PTR_ERR(data);
  256. aux = dm_bufio_get_aux_data(buf);
  257. if (!aux->hash_verified) {
  258. if (skip_unverified) {
  259. r = 1;
  260. goto release_ret_r;
  261. }
  262. r = verity_hash(v, verity_io_hash_req(v, io),
  263. data, 1 << v->hash_dev_block_bits,
  264. verity_io_real_digest(v, io), !io->in_tasklet);
  265. if (unlikely(r < 0))
  266. goto release_ret_r;
  267. if (likely(memcmp(verity_io_real_digest(v, io), want_digest,
  268. v->digest_size) == 0))
  269. aux->hash_verified = 1;
  270. else if (static_branch_unlikely(&use_tasklet_enabled) &&
  271. io->in_tasklet) {
  272. /*
  273. * Error handling code (FEC included) cannot be run in a
  274. * tasklet since it may sleep, so fallback to work-queue.
  275. */
  276. r = -EAGAIN;
  277. goto release_ret_r;
  278. }
  279. else if (verity_fec_decode(v, io,
  280. DM_VERITY_BLOCK_TYPE_METADATA,
  281. hash_block, data, NULL) == 0)
  282. aux->hash_verified = 1;
  283. else if (verity_handle_err(v,
  284. DM_VERITY_BLOCK_TYPE_METADATA,
  285. hash_block)) {
  286. r = -EIO;
  287. goto release_ret_r;
  288. }
  289. }
  290. data += offset;
  291. memcpy(want_digest, data, v->digest_size);
  292. r = 0;
  293. release_ret_r:
  294. dm_bufio_release(buf);
  295. return r;
  296. }
  297. /*
  298. * Find a hash for a given block, write it to digest and verify the integrity
  299. * of the hash tree if necessary.
  300. */
  301. int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
  302. sector_t block, u8 *digest, bool *is_zero)
  303. {
  304. int r = 0, i;
  305. if (likely(v->levels)) {
  306. /*
  307. * First, we try to get the requested hash for
  308. * the current block. If the hash block itself is
  309. * verified, zero is returned. If it isn't, this
  310. * function returns 1 and we fall back to whole
  311. * chain verification.
  312. */
  313. r = verity_verify_level(v, io, block, 0, true, digest);
  314. if (likely(r <= 0))
  315. goto out;
  316. }
  317. memcpy(digest, v->root_digest, v->digest_size);
  318. for (i = v->levels - 1; i >= 0; i--) {
  319. r = verity_verify_level(v, io, block, i, false, digest);
  320. if (unlikely(r))
  321. goto out;
  322. }
  323. out:
  324. if (!r && v->zero_digest)
  325. *is_zero = !memcmp(v->zero_digest, digest, v->digest_size);
  326. else
  327. *is_zero = false;
  328. return r;
  329. }
  330. /*
  331. * Calculates the digest for the given bio
  332. */
  333. static int verity_for_io_block(struct dm_verity *v, struct dm_verity_io *io,
  334. struct bvec_iter *iter, struct crypto_wait *wait)
  335. {
  336. unsigned int todo = 1 << v->data_dev_block_bits;
  337. struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
  338. struct scatterlist sg;
  339. struct ahash_request *req = verity_io_hash_req(v, io);
  340. do {
  341. int r;
  342. unsigned int len;
  343. struct bio_vec bv = bio_iter_iovec(bio, *iter);
  344. sg_init_table(&sg, 1);
  345. len = bv.bv_len;
  346. if (likely(len >= todo))
  347. len = todo;
  348. /*
  349. * Operating on a single page at a time looks suboptimal
  350. * until you consider the typical block size is 4,096B.
  351. * Going through this loops twice should be very rare.
  352. */
  353. sg_set_page(&sg, bv.bv_page, len, bv.bv_offset);
  354. ahash_request_set_crypt(req, &sg, NULL, len);
  355. r = crypto_wait_req(crypto_ahash_update(req), wait);
  356. if (unlikely(r < 0)) {
  357. DMERR("verity_for_io_block crypto op failed: %d", r);
  358. return r;
  359. }
  360. bio_advance_iter(bio, iter, len);
  361. todo -= len;
  362. } while (todo);
  363. return 0;
  364. }
  365. /*
  366. * Calls function process for 1 << v->data_dev_block_bits bytes in the bio_vec
  367. * starting from iter.
  368. */
  369. int verity_for_bv_block(struct dm_verity *v, struct dm_verity_io *io,
  370. struct bvec_iter *iter,
  371. int (*process)(struct dm_verity *v,
  372. struct dm_verity_io *io, u8 *data,
  373. size_t len))
  374. {
  375. unsigned int todo = 1 << v->data_dev_block_bits;
  376. struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
  377. do {
  378. int r;
  379. u8 *page;
  380. unsigned int len;
  381. struct bio_vec bv = bio_iter_iovec(bio, *iter);
  382. page = bvec_kmap_local(&bv);
  383. len = bv.bv_len;
  384. if (likely(len >= todo))
  385. len = todo;
  386. r = process(v, io, page, len);
  387. kunmap_local(page);
  388. if (r < 0)
  389. return r;
  390. bio_advance_iter(bio, iter, len);
  391. todo -= len;
  392. } while (todo);
  393. return 0;
  394. }
  395. static int verity_bv_zero(struct dm_verity *v, struct dm_verity_io *io,
  396. u8 *data, size_t len)
  397. {
  398. memset(data, 0, len);
  399. return 0;
  400. }
  401. /*
  402. * Moves the bio iter one data block forward.
  403. */
  404. static inline void verity_bv_skip_block(struct dm_verity *v,
  405. struct dm_verity_io *io,
  406. struct bvec_iter *iter)
  407. {
  408. struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
  409. bio_advance_iter(bio, iter, 1 << v->data_dev_block_bits);
  410. }
  411. /*
  412. * Verify one "dm_verity_io" structure.
  413. */
  414. static int verity_verify_io(struct dm_verity_io *io)
  415. {
  416. bool is_zero;
  417. struct dm_verity *v = io->v;
  418. #if defined(CONFIG_DM_VERITY_FEC)
  419. struct bvec_iter start;
  420. #endif
  421. struct bvec_iter iter_copy;
  422. struct bvec_iter *iter;
  423. struct crypto_wait wait;
  424. struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
  425. unsigned int b;
  426. if (static_branch_unlikely(&use_tasklet_enabled) && io->in_tasklet) {
  427. /*
  428. * Copy the iterator in case we need to restart
  429. * verification in a work-queue.
  430. */
  431. iter_copy = io->iter;
  432. iter = &iter_copy;
  433. } else
  434. iter = &io->iter;
  435. for (b = 0; b < io->n_blocks; b++) {
  436. int r;
  437. sector_t cur_block = io->block + b;
  438. struct ahash_request *req = verity_io_hash_req(v, io);
  439. if (v->validated_blocks && bio->bi_status == BLK_STS_OK &&
  440. likely(test_bit(cur_block, v->validated_blocks))) {
  441. verity_bv_skip_block(v, io, iter);
  442. continue;
  443. }
  444. r = verity_hash_for_block(v, io, cur_block,
  445. verity_io_want_digest(v, io),
  446. &is_zero);
  447. if (unlikely(r < 0))
  448. return r;
  449. if (is_zero) {
  450. /*
  451. * If we expect a zero block, don't validate, just
  452. * return zeros.
  453. */
  454. r = verity_for_bv_block(v, io, iter,
  455. verity_bv_zero);
  456. if (unlikely(r < 0))
  457. return r;
  458. continue;
  459. }
  460. r = verity_hash_init(v, req, &wait, !io->in_tasklet);
  461. if (unlikely(r < 0))
  462. return r;
  463. #if defined(CONFIG_DM_VERITY_FEC)
  464. if (verity_fec_is_enabled(v))
  465. start = *iter;
  466. #endif
  467. r = verity_for_io_block(v, io, iter, &wait);
  468. if (unlikely(r < 0))
  469. return r;
  470. r = verity_hash_final(v, req, verity_io_real_digest(v, io),
  471. &wait);
  472. if (unlikely(r < 0))
  473. return r;
  474. if (likely(memcmp(verity_io_real_digest(v, io),
  475. verity_io_want_digest(v, io), v->digest_size) == 0)) {
  476. if (v->validated_blocks)
  477. set_bit(cur_block, v->validated_blocks);
  478. continue;
  479. } else if (static_branch_unlikely(&use_tasklet_enabled) &&
  480. io->in_tasklet) {
  481. /*
  482. * Error handling code (FEC included) cannot be run in a
  483. * tasklet since it may sleep, so fallback to work-queue.
  484. */
  485. return -EAGAIN;
  486. #if defined(CONFIG_DM_VERITY_FEC)
  487. } else if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_DATA,
  488. cur_block, NULL, &start) == 0) {
  489. continue;
  490. #endif
  491. } else {
  492. if (bio->bi_status) {
  493. /*
  494. * Error correction failed; Just return error
  495. */
  496. return -EIO;
  497. }
  498. if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_DATA,
  499. cur_block))
  500. return -EIO;
  501. }
  502. }
  503. return 0;
  504. }
  505. /*
  506. * Skip verity work in response to I/O error when system is shutting down.
  507. */
  508. static inline bool verity_is_system_shutting_down(void)
  509. {
  510. return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
  511. || system_state == SYSTEM_RESTART;
  512. }
  513. /*
  514. * End one "io" structure with a given error.
  515. */
  516. static void verity_finish_io(struct dm_verity_io *io, blk_status_t status)
  517. {
  518. struct dm_verity *v = io->v;
  519. struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
  520. bio->bi_end_io = io->orig_bi_end_io;
  521. bio->bi_status = status;
  522. if (!static_branch_unlikely(&use_tasklet_enabled) || !io->in_tasklet)
  523. verity_fec_finish_io(io);
  524. bio_endio(bio);
  525. }
  526. static void verity_work(struct work_struct *w)
  527. {
  528. struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
  529. io->in_tasklet = false;
  530. verity_finish_io(io, errno_to_blk_status(verity_verify_io(io)));
  531. }
  532. static void verity_tasklet(unsigned long data)
  533. {
  534. struct dm_verity_io *io = (struct dm_verity_io *)data;
  535. int err;
  536. io->in_tasklet = true;
  537. err = verity_verify_io(io);
  538. if (err == -EAGAIN || err == -ENOMEM) {
  539. /* fallback to retrying with work-queue */
  540. INIT_WORK(&io->work, verity_work);
  541. queue_work(io->v->verify_wq, &io->work);
  542. return;
  543. }
  544. verity_finish_io(io, errno_to_blk_status(err));
  545. }
  546. static void verity_end_io(struct bio *bio)
  547. {
  548. struct dm_verity_io *io = bio->bi_private;
  549. if (bio->bi_status &&
  550. (!verity_fec_is_enabled(io->v) ||
  551. verity_is_system_shutting_down() ||
  552. (bio->bi_opf & REQ_RAHEAD))) {
  553. verity_finish_io(io, bio->bi_status);
  554. return;
  555. }
  556. if (static_branch_unlikely(&use_tasklet_enabled) && io->v->use_tasklet) {
  557. tasklet_init(&io->tasklet, verity_tasklet, (unsigned long)io);
  558. tasklet_schedule(&io->tasklet);
  559. } else {
  560. INIT_WORK(&io->work, verity_work);
  561. queue_work(io->v->verify_wq, &io->work);
  562. }
  563. }
  564. /*
  565. * Prefetch buffers for the specified io.
  566. * The root buffer is not prefetched, it is assumed that it will be cached
  567. * all the time.
  568. */
  569. static void verity_prefetch_io(struct work_struct *work)
  570. {
  571. struct dm_verity_prefetch_work *pw =
  572. container_of(work, struct dm_verity_prefetch_work, work);
  573. struct dm_verity *v = pw->v;
  574. int i;
  575. for (i = v->levels - 2; i >= 0; i--) {
  576. sector_t hash_block_start;
  577. sector_t hash_block_end;
  578. verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL);
  579. verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL);
  580. if (!i) {
  581. unsigned int cluster = READ_ONCE(dm_verity_prefetch_cluster);
  582. cluster >>= v->data_dev_block_bits;
  583. if (unlikely(!cluster))
  584. goto no_prefetch_cluster;
  585. if (unlikely(cluster & (cluster - 1)))
  586. cluster = 1 << __fls(cluster);
  587. hash_block_start &= ~(sector_t)(cluster - 1);
  588. hash_block_end |= cluster - 1;
  589. if (unlikely(hash_block_end >= v->hash_blocks))
  590. hash_block_end = v->hash_blocks - 1;
  591. }
  592. no_prefetch_cluster:
  593. dm_bufio_prefetch(v->bufio, hash_block_start,
  594. hash_block_end - hash_block_start + 1);
  595. }
  596. kfree(pw);
  597. }
  598. static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io)
  599. {
  600. sector_t block = io->block;
  601. unsigned int n_blocks = io->n_blocks;
  602. struct dm_verity_prefetch_work *pw;
  603. if (v->validated_blocks) {
  604. while (n_blocks && test_bit(block, v->validated_blocks)) {
  605. block++;
  606. n_blocks--;
  607. }
  608. while (n_blocks && test_bit(block + n_blocks - 1,
  609. v->validated_blocks))
  610. n_blocks--;
  611. if (!n_blocks)
  612. return;
  613. }
  614. pw = kmalloc(sizeof(struct dm_verity_prefetch_work),
  615. GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
  616. if (!pw)
  617. return;
  618. INIT_WORK(&pw->work, verity_prefetch_io);
  619. pw->v = v;
  620. pw->block = block;
  621. pw->n_blocks = n_blocks;
  622. queue_work(v->verify_wq, &pw->work);
  623. }
  624. /*
  625. * Bio map function. It allocates dm_verity_io structure and bio vector and
  626. * fills them. Then it issues prefetches and the I/O.
  627. */
  628. static int verity_map(struct dm_target *ti, struct bio *bio)
  629. {
  630. struct dm_verity *v = ti->private;
  631. struct dm_verity_io *io;
  632. bio_set_dev(bio, v->data_dev->bdev);
  633. bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector);
  634. if (((unsigned int)bio->bi_iter.bi_sector | bio_sectors(bio)) &
  635. ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) {
  636. DMERR_LIMIT("unaligned io");
  637. return DM_MAPIO_KILL;
  638. }
  639. if (bio_end_sector(bio) >>
  640. (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
  641. DMERR_LIMIT("io out of range");
  642. return DM_MAPIO_KILL;
  643. }
  644. if (bio_data_dir(bio) == WRITE)
  645. return DM_MAPIO_KILL;
  646. io = dm_per_bio_data(bio, ti->per_io_data_size);
  647. io->v = v;
  648. io->orig_bi_end_io = bio->bi_end_io;
  649. io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
  650. io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits;
  651. bio->bi_end_io = verity_end_io;
  652. bio->bi_private = io;
  653. io->iter = bio->bi_iter;
  654. verity_fec_init_io(io);
  655. verity_submit_prefetch(v, io);
  656. submit_bio_noacct(bio);
  657. return DM_MAPIO_SUBMITTED;
  658. }
  659. /*
  660. * Status: V (valid) or C (corruption found)
  661. */
  662. static void verity_status(struct dm_target *ti, status_type_t type,
  663. unsigned int status_flags, char *result, unsigned int maxlen)
  664. {
  665. struct dm_verity *v = ti->private;
  666. unsigned int args = 0;
  667. unsigned int sz = 0;
  668. unsigned int x;
  669. switch (type) {
  670. case STATUSTYPE_INFO:
  671. DMEMIT("%c", v->hash_failed ? 'C' : 'V');
  672. break;
  673. case STATUSTYPE_TABLE:
  674. DMEMIT("%u %s %s %u %u %llu %llu %s ",
  675. v->version,
  676. v->data_dev->name,
  677. v->hash_dev->name,
  678. 1 << v->data_dev_block_bits,
  679. 1 << v->hash_dev_block_bits,
  680. (unsigned long long)v->data_blocks,
  681. (unsigned long long)v->hash_start,
  682. v->alg_name
  683. );
  684. for (x = 0; x < v->digest_size; x++)
  685. DMEMIT("%02x", v->root_digest[x]);
  686. DMEMIT(" ");
  687. if (!v->salt_size)
  688. DMEMIT("-");
  689. else
  690. for (x = 0; x < v->salt_size; x++)
  691. DMEMIT("%02x", v->salt[x]);
  692. if (v->mode != DM_VERITY_MODE_EIO)
  693. args++;
  694. if (verity_fec_is_enabled(v))
  695. args += DM_VERITY_OPTS_FEC;
  696. if (v->zero_digest)
  697. args++;
  698. if (v->validated_blocks)
  699. args++;
  700. if (v->use_tasklet)
  701. args++;
  702. if (v->signature_key_desc)
  703. args += DM_VERITY_ROOT_HASH_VERIFICATION_OPTS;
  704. if (!args)
  705. return;
  706. DMEMIT(" %u", args);
  707. if (v->mode != DM_VERITY_MODE_EIO) {
  708. DMEMIT(" ");
  709. switch (v->mode) {
  710. case DM_VERITY_MODE_LOGGING:
  711. DMEMIT(DM_VERITY_OPT_LOGGING);
  712. break;
  713. case DM_VERITY_MODE_RESTART:
  714. DMEMIT(DM_VERITY_OPT_RESTART);
  715. break;
  716. case DM_VERITY_MODE_PANIC:
  717. DMEMIT(DM_VERITY_OPT_PANIC);
  718. break;
  719. default:
  720. BUG();
  721. }
  722. }
  723. if (v->zero_digest)
  724. DMEMIT(" " DM_VERITY_OPT_IGN_ZEROES);
  725. if (v->validated_blocks)
  726. DMEMIT(" " DM_VERITY_OPT_AT_MOST_ONCE);
  727. if (v->use_tasklet)
  728. DMEMIT(" " DM_VERITY_OPT_TASKLET_VERIFY);
  729. sz = verity_fec_status_table(v, sz, result, maxlen);
  730. if (v->signature_key_desc)
  731. DMEMIT(" " DM_VERITY_ROOT_HASH_VERIFICATION_OPT_SIG_KEY
  732. " %s", v->signature_key_desc);
  733. break;
  734. case STATUSTYPE_IMA:
  735. DMEMIT_TARGET_NAME_VERSION(ti->type);
  736. DMEMIT(",hash_failed=%c", v->hash_failed ? 'C' : 'V');
  737. DMEMIT(",verity_version=%u", v->version);
  738. DMEMIT(",data_device_name=%s", v->data_dev->name);
  739. DMEMIT(",hash_device_name=%s", v->hash_dev->name);
  740. DMEMIT(",verity_algorithm=%s", v->alg_name);
  741. DMEMIT(",root_digest=");
  742. for (x = 0; x < v->digest_size; x++)
  743. DMEMIT("%02x", v->root_digest[x]);
  744. DMEMIT(",salt=");
  745. if (!v->salt_size)
  746. DMEMIT("-");
  747. else
  748. for (x = 0; x < v->salt_size; x++)
  749. DMEMIT("%02x", v->salt[x]);
  750. DMEMIT(",ignore_zero_blocks=%c", v->zero_digest ? 'y' : 'n');
  751. DMEMIT(",check_at_most_once=%c", v->validated_blocks ? 'y' : 'n');
  752. if (v->signature_key_desc)
  753. DMEMIT(",root_hash_sig_key_desc=%s", v->signature_key_desc);
  754. if (v->mode != DM_VERITY_MODE_EIO) {
  755. DMEMIT(",verity_mode=");
  756. switch (v->mode) {
  757. case DM_VERITY_MODE_LOGGING:
  758. DMEMIT(DM_VERITY_OPT_LOGGING);
  759. break;
  760. case DM_VERITY_MODE_RESTART:
  761. DMEMIT(DM_VERITY_OPT_RESTART);
  762. break;
  763. case DM_VERITY_MODE_PANIC:
  764. DMEMIT(DM_VERITY_OPT_PANIC);
  765. break;
  766. default:
  767. DMEMIT("invalid");
  768. }
  769. }
  770. DMEMIT(";");
  771. break;
  772. }
  773. }
  774. static int verity_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
  775. {
  776. struct dm_verity *v = ti->private;
  777. *bdev = v->data_dev->bdev;
  778. if (v->data_start || ti->len != bdev_nr_sectors(v->data_dev->bdev))
  779. return 1;
  780. return 0;
  781. }
  782. static int verity_iterate_devices(struct dm_target *ti,
  783. iterate_devices_callout_fn fn, void *data)
  784. {
  785. struct dm_verity *v = ti->private;
  786. return fn(ti, v->data_dev, v->data_start, ti->len, data);
  787. }
  788. static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits)
  789. {
  790. struct dm_verity *v = ti->private;
  791. if (limits->logical_block_size < 1 << v->data_dev_block_bits)
  792. limits->logical_block_size = 1 << v->data_dev_block_bits;
  793. if (limits->physical_block_size < 1 << v->data_dev_block_bits)
  794. limits->physical_block_size = 1 << v->data_dev_block_bits;
  795. blk_limits_io_min(limits, limits->logical_block_size);
  796. }
  797. static void verity_dtr(struct dm_target *ti)
  798. {
  799. struct dm_verity *v = ti->private;
  800. if (v->verify_wq)
  801. destroy_workqueue(v->verify_wq);
  802. if (v->bufio)
  803. dm_bufio_client_destroy(v->bufio);
  804. kvfree(v->validated_blocks);
  805. kfree(v->salt);
  806. kfree(v->root_digest);
  807. kfree(v->zero_digest);
  808. if (v->tfm)
  809. crypto_free_ahash(v->tfm);
  810. kfree(v->alg_name);
  811. if (v->hash_dev)
  812. dm_put_device(ti, v->hash_dev);
  813. if (v->data_dev)
  814. dm_put_device(ti, v->data_dev);
  815. verity_fec_dtr(v);
  816. kfree(v->signature_key_desc);
  817. if (v->use_tasklet)
  818. static_branch_dec(&use_tasklet_enabled);
  819. kfree(v);
  820. }
  821. static int verity_alloc_most_once(struct dm_verity *v)
  822. {
  823. struct dm_target *ti = v->ti;
  824. /* the bitset can only handle INT_MAX blocks */
  825. if (v->data_blocks > INT_MAX) {
  826. ti->error = "device too large to use check_at_most_once";
  827. return -E2BIG;
  828. }
  829. v->validated_blocks = kvcalloc(BITS_TO_LONGS(v->data_blocks),
  830. sizeof(unsigned long),
  831. GFP_KERNEL);
  832. if (!v->validated_blocks) {
  833. ti->error = "failed to allocate bitset for check_at_most_once";
  834. return -ENOMEM;
  835. }
  836. return 0;
  837. }
  838. static int verity_alloc_zero_digest(struct dm_verity *v)
  839. {
  840. int r = -ENOMEM;
  841. struct ahash_request *req;
  842. u8 *zero_data;
  843. v->zero_digest = kmalloc(v->digest_size, GFP_KERNEL);
  844. if (!v->zero_digest)
  845. return r;
  846. req = kmalloc(v->ahash_reqsize, GFP_KERNEL);
  847. if (!req)
  848. return r; /* verity_dtr will free zero_digest */
  849. zero_data = kzalloc(1 << v->data_dev_block_bits, GFP_KERNEL);
  850. if (!zero_data)
  851. goto out;
  852. r = verity_hash(v, req, zero_data, 1 << v->data_dev_block_bits,
  853. v->zero_digest, true);
  854. out:
  855. kfree(req);
  856. kfree(zero_data);
  857. return r;
  858. }
  859. static inline bool verity_is_verity_mode(const char *arg_name)
  860. {
  861. return (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING) ||
  862. !strcasecmp(arg_name, DM_VERITY_OPT_RESTART) ||
  863. !strcasecmp(arg_name, DM_VERITY_OPT_PANIC));
  864. }
  865. static int verity_parse_verity_mode(struct dm_verity *v, const char *arg_name)
  866. {
  867. if (v->mode)
  868. return -EINVAL;
  869. if (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING))
  870. v->mode = DM_VERITY_MODE_LOGGING;
  871. else if (!strcasecmp(arg_name, DM_VERITY_OPT_RESTART))
  872. v->mode = DM_VERITY_MODE_RESTART;
  873. else if (!strcasecmp(arg_name, DM_VERITY_OPT_PANIC))
  874. v->mode = DM_VERITY_MODE_PANIC;
  875. return 0;
  876. }
  877. static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v,
  878. struct dm_verity_sig_opts *verify_args,
  879. bool only_modifier_opts)
  880. {
  881. int r = 0;
  882. unsigned int argc;
  883. struct dm_target *ti = v->ti;
  884. const char *arg_name;
  885. static const struct dm_arg _args[] = {
  886. {0, DM_VERITY_OPTS_MAX, "Invalid number of feature args"},
  887. };
  888. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  889. if (r)
  890. return -EINVAL;
  891. if (!argc)
  892. return 0;
  893. do {
  894. arg_name = dm_shift_arg(as);
  895. argc--;
  896. if (verity_is_verity_mode(arg_name)) {
  897. if (only_modifier_opts)
  898. continue;
  899. r = verity_parse_verity_mode(v, arg_name);
  900. if (r) {
  901. ti->error = "Conflicting error handling parameters";
  902. return r;
  903. }
  904. continue;
  905. } else if (!strcasecmp(arg_name, DM_VERITY_OPT_IGN_ZEROES)) {
  906. if (only_modifier_opts)
  907. continue;
  908. r = verity_alloc_zero_digest(v);
  909. if (r) {
  910. ti->error = "Cannot allocate zero digest";
  911. return r;
  912. }
  913. continue;
  914. } else if (!strcasecmp(arg_name, DM_VERITY_OPT_AT_MOST_ONCE)) {
  915. if (only_modifier_opts)
  916. continue;
  917. r = verity_alloc_most_once(v);
  918. if (r)
  919. return r;
  920. continue;
  921. } else if (!strcasecmp(arg_name, DM_VERITY_OPT_TASKLET_VERIFY)) {
  922. v->use_tasklet = true;
  923. static_branch_inc(&use_tasklet_enabled);
  924. continue;
  925. } else if (verity_is_fec_opt_arg(arg_name)) {
  926. if (only_modifier_opts)
  927. continue;
  928. r = verity_fec_parse_opt_args(as, v, &argc, arg_name);
  929. if (r)
  930. return r;
  931. continue;
  932. } else if (verity_verify_is_sig_opt_arg(arg_name)) {
  933. if (only_modifier_opts)
  934. continue;
  935. r = verity_verify_sig_parse_opt_args(as, v,
  936. verify_args,
  937. &argc, arg_name);
  938. if (r)
  939. return r;
  940. continue;
  941. } else if (only_modifier_opts) {
  942. /*
  943. * Ignore unrecognized opt, could easily be an extra
  944. * argument to an option whose parsing was skipped.
  945. * Normal parsing (@only_modifier_opts=false) will
  946. * properly parse all options (and their extra args).
  947. */
  948. continue;
  949. #if defined(CONFIG_DM_VERITY_SIG_VALUE)
  950. } else if (verity_verify_is_sig_value_opt_arg(arg_name)) {
  951. r = verity_verify_sig_value_parse_opt_args(
  952. as, v, verify_args, &argc, arg_name);
  953. if (r)
  954. return r;
  955. continue;
  956. #endif
  957. }
  958. DMERR("Unrecognized verity feature request: %s", arg_name);
  959. ti->error = "Unrecognized verity feature request";
  960. return -EINVAL;
  961. } while (argc && !r);
  962. return r;
  963. }
  964. /*
  965. * Target parameters:
  966. * <version> The current format is version 1.
  967. * Vsn 0 is compatible with original Chromium OS releases.
  968. * <data device>
  969. * <hash device>
  970. * <data block size>
  971. * <hash block size>
  972. * <the number of data blocks>
  973. * <hash start block>
  974. * <algorithm>
  975. * <digest>
  976. * <salt> Hex string or "-" if no salt.
  977. */
  978. static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  979. {
  980. struct dm_verity *v;
  981. struct dm_verity_sig_opts verify_args = {0};
  982. struct dm_arg_set as;
  983. unsigned int num;
  984. unsigned long long num_ll;
  985. int r;
  986. int i;
  987. sector_t hash_position;
  988. char dummy;
  989. char *root_hash_digest_to_validate;
  990. v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
  991. if (!v) {
  992. ti->error = "Cannot allocate verity structure";
  993. return -ENOMEM;
  994. }
  995. ti->private = v;
  996. v->ti = ti;
  997. r = verity_fec_ctr_alloc(v);
  998. if (r)
  999. goto bad;
  1000. if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) {
  1001. ti->error = "Device must be readonly";
  1002. r = -EINVAL;
  1003. goto bad;
  1004. }
  1005. if (argc < 10) {
  1006. ti->error = "Not enough arguments";
  1007. r = -EINVAL;
  1008. goto bad;
  1009. }
  1010. /* Parse optional parameters that modify primary args */
  1011. if (argc > 10) {
  1012. as.argc = argc - 10;
  1013. as.argv = argv + 10;
  1014. r = verity_parse_opt_args(&as, v, &verify_args, true);
  1015. if (r < 0)
  1016. goto bad;
  1017. }
  1018. if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
  1019. num > 1) {
  1020. ti->error = "Invalid version";
  1021. r = -EINVAL;
  1022. goto bad;
  1023. }
  1024. v->version = num;
  1025. r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev);
  1026. if (r) {
  1027. ti->error = "Data device lookup failed";
  1028. goto bad;
  1029. }
  1030. r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev);
  1031. if (r) {
  1032. ti->error = "Hash device lookup failed";
  1033. goto bad;
  1034. }
  1035. if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
  1036. !num || (num & (num - 1)) ||
  1037. num < bdev_logical_block_size(v->data_dev->bdev) ||
  1038. num > PAGE_SIZE) {
  1039. ti->error = "Invalid data device block size";
  1040. r = -EINVAL;
  1041. goto bad;
  1042. }
  1043. v->data_dev_block_bits = __ffs(num);
  1044. if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
  1045. !num || (num & (num - 1)) ||
  1046. num < bdev_logical_block_size(v->hash_dev->bdev) ||
  1047. num > INT_MAX) {
  1048. ti->error = "Invalid hash device block size";
  1049. r = -EINVAL;
  1050. goto bad;
  1051. }
  1052. v->hash_dev_block_bits = __ffs(num);
  1053. if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
  1054. (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
  1055. >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
  1056. ti->error = "Invalid data blocks";
  1057. r = -EINVAL;
  1058. goto bad;
  1059. }
  1060. v->data_blocks = num_ll;
  1061. if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
  1062. ti->error = "Data device is too small";
  1063. r = -EINVAL;
  1064. goto bad;
  1065. }
  1066. if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
  1067. (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
  1068. >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
  1069. ti->error = "Invalid hash start";
  1070. r = -EINVAL;
  1071. goto bad;
  1072. }
  1073. v->hash_start = num_ll;
  1074. v->alg_name = kstrdup(argv[7], GFP_KERNEL);
  1075. if (!v->alg_name) {
  1076. ti->error = "Cannot allocate algorithm name";
  1077. r = -ENOMEM;
  1078. goto bad;
  1079. }
  1080. v->tfm = crypto_alloc_ahash(v->alg_name, 0,
  1081. v->use_tasklet ? CRYPTO_ALG_ASYNC : 0);
  1082. if (IS_ERR(v->tfm)) {
  1083. ti->error = "Cannot initialize hash function";
  1084. r = PTR_ERR(v->tfm);
  1085. v->tfm = NULL;
  1086. goto bad;
  1087. }
  1088. /*
  1089. * dm-verity performance can vary greatly depending on which hash
  1090. * algorithm implementation is used. Help people debug performance
  1091. * problems by logging the ->cra_driver_name.
  1092. */
  1093. DMINFO("%s using implementation \"%s\"", v->alg_name,
  1094. crypto_hash_alg_common(v->tfm)->base.cra_driver_name);
  1095. v->digest_size = crypto_ahash_digestsize(v->tfm);
  1096. if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
  1097. ti->error = "Digest size too big";
  1098. r = -EINVAL;
  1099. goto bad;
  1100. }
  1101. v->ahash_reqsize = sizeof(struct ahash_request) +
  1102. crypto_ahash_reqsize(v->tfm);
  1103. v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
  1104. if (!v->root_digest) {
  1105. ti->error = "Cannot allocate root digest";
  1106. r = -ENOMEM;
  1107. goto bad;
  1108. }
  1109. if (strlen(argv[8]) != v->digest_size * 2 ||
  1110. hex2bin(v->root_digest, argv[8], v->digest_size)) {
  1111. ti->error = "Invalid root digest";
  1112. r = -EINVAL;
  1113. goto bad;
  1114. }
  1115. root_hash_digest_to_validate = argv[8];
  1116. if (strcmp(argv[9], "-")) {
  1117. v->salt_size = strlen(argv[9]) / 2;
  1118. v->salt = kmalloc(v->salt_size, GFP_KERNEL);
  1119. if (!v->salt) {
  1120. ti->error = "Cannot allocate salt";
  1121. r = -ENOMEM;
  1122. goto bad;
  1123. }
  1124. if (strlen(argv[9]) != v->salt_size * 2 ||
  1125. hex2bin(v->salt, argv[9], v->salt_size)) {
  1126. ti->error = "Invalid salt";
  1127. r = -EINVAL;
  1128. goto bad;
  1129. }
  1130. }
  1131. argv += 10;
  1132. argc -= 10;
  1133. /* Optional parameters */
  1134. if (argc) {
  1135. as.argc = argc;
  1136. as.argv = argv;
  1137. r = verity_parse_opt_args(&as, v, &verify_args, false);
  1138. if (r < 0)
  1139. goto bad;
  1140. }
  1141. /* Root hash signature is a optional parameter*/
  1142. r = verity_verify_root_hash(root_hash_digest_to_validate,
  1143. strlen(root_hash_digest_to_validate),
  1144. verify_args.sig,
  1145. verify_args.sig_size);
  1146. if (r < 0) {
  1147. ti->error = "Root hash verification failed";
  1148. goto bad;
  1149. }
  1150. v->hash_per_block_bits =
  1151. __fls((1 << v->hash_dev_block_bits) / v->digest_size);
  1152. v->levels = 0;
  1153. if (v->data_blocks)
  1154. while (v->hash_per_block_bits * v->levels < 64 &&
  1155. (unsigned long long)(v->data_blocks - 1) >>
  1156. (v->hash_per_block_bits * v->levels))
  1157. v->levels++;
  1158. if (v->levels > DM_VERITY_MAX_LEVELS) {
  1159. ti->error = "Too many tree levels";
  1160. r = -E2BIG;
  1161. goto bad;
  1162. }
  1163. hash_position = v->hash_start;
  1164. for (i = v->levels - 1; i >= 0; i--) {
  1165. sector_t s;
  1166. v->hash_level_block[i] = hash_position;
  1167. s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
  1168. >> ((i + 1) * v->hash_per_block_bits);
  1169. if (hash_position + s < hash_position) {
  1170. ti->error = "Hash device offset overflow";
  1171. r = -E2BIG;
  1172. goto bad;
  1173. }
  1174. hash_position += s;
  1175. }
  1176. v->hash_blocks = hash_position;
  1177. v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
  1178. 1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
  1179. dm_bufio_alloc_callback, NULL,
  1180. v->use_tasklet ? DM_BUFIO_CLIENT_NO_SLEEP : 0);
  1181. if (IS_ERR(v->bufio)) {
  1182. ti->error = "Cannot initialize dm-bufio";
  1183. r = PTR_ERR(v->bufio);
  1184. v->bufio = NULL;
  1185. goto bad;
  1186. }
  1187. if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
  1188. ti->error = "Hash device is too small";
  1189. r = -E2BIG;
  1190. goto bad;
  1191. }
  1192. /*
  1193. * Using WQ_HIGHPRI improves throughput and completion latency by
  1194. * reducing wait times when reading from a dm-verity device.
  1195. *
  1196. * Also as required for the "try_verify_in_tasklet" feature: WQ_HIGHPRI
  1197. * allows verify_wq to preempt softirq since verification in tasklet
  1198. * will fall-back to using it for error handling (or if the bufio cache
  1199. * doesn't have required hashes).
  1200. */
  1201. v->verify_wq = alloc_workqueue("kverityd", WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
  1202. if (!v->verify_wq) {
  1203. ti->error = "Cannot allocate workqueue";
  1204. r = -ENOMEM;
  1205. goto bad;
  1206. }
  1207. ti->per_io_data_size = sizeof(struct dm_verity_io) +
  1208. v->ahash_reqsize + v->digest_size * 2;
  1209. r = verity_fec_ctr(v);
  1210. if (r)
  1211. goto bad;
  1212. ti->per_io_data_size = roundup(ti->per_io_data_size,
  1213. __alignof__(struct dm_verity_io));
  1214. verity_verify_sig_opts_cleanup(&verify_args);
  1215. return 0;
  1216. bad:
  1217. verity_verify_sig_opts_cleanup(&verify_args);
  1218. verity_dtr(ti);
  1219. return r;
  1220. }
  1221. /*
  1222. * Check whether a DM target is a verity target.
  1223. */
  1224. bool dm_is_verity_target(struct dm_target *ti)
  1225. {
  1226. return ti->type->module == THIS_MODULE;
  1227. }
  1228. /*
  1229. * Get the verity mode (error behavior) of a verity target.
  1230. *
  1231. * Returns the verity mode of the target, or -EINVAL if 'ti' is not a verity
  1232. * target.
  1233. */
  1234. int dm_verity_get_mode(struct dm_target *ti)
  1235. {
  1236. struct dm_verity *v = ti->private;
  1237. if (!dm_is_verity_target(ti))
  1238. return -EINVAL;
  1239. return v->mode;
  1240. }
  1241. /*
  1242. * Get the root digest of a verity target.
  1243. *
  1244. * Returns a copy of the root digest, the caller is responsible for
  1245. * freeing the memory of the digest.
  1246. */
  1247. int dm_verity_get_root_digest(struct dm_target *ti, u8 **root_digest, unsigned int *digest_size)
  1248. {
  1249. struct dm_verity *v = ti->private;
  1250. if (!dm_is_verity_target(ti))
  1251. return -EINVAL;
  1252. *root_digest = kmemdup(v->root_digest, v->digest_size, GFP_KERNEL);
  1253. if (*root_digest == NULL)
  1254. return -ENOMEM;
  1255. *digest_size = v->digest_size;
  1256. return 0;
  1257. }
  1258. static struct target_type verity_target = {
  1259. .name = "verity",
  1260. .features = DM_TARGET_IMMUTABLE,
  1261. .version = {1, 9, 0},
  1262. .module = THIS_MODULE,
  1263. .ctr = verity_ctr,
  1264. .dtr = verity_dtr,
  1265. .map = verity_map,
  1266. .status = verity_status,
  1267. .prepare_ioctl = verity_prepare_ioctl,
  1268. .iterate_devices = verity_iterate_devices,
  1269. .io_hints = verity_io_hints,
  1270. };
  1271. static int __init dm_verity_init(void)
  1272. {
  1273. int r;
  1274. r = dm_register_target(&verity_target);
  1275. if (r < 0)
  1276. DMERR("register failed %d", r);
  1277. return r;
  1278. }
  1279. static void __exit dm_verity_exit(void)
  1280. {
  1281. dm_unregister_target(&verity_target);
  1282. }
  1283. module_init(dm_verity_init);
  1284. module_exit(dm_verity_exit);
  1285. MODULE_AUTHOR("Mikulas Patocka <[email protected]>");
  1286. MODULE_AUTHOR("Mandeep Baines <[email protected]>");
  1287. MODULE_AUTHOR("Will Drewry <[email protected]>");
  1288. MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking");
  1289. MODULE_LICENSE("GPL");