dm-verity-target.c 25 KB

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