dm-verity.c 24 KB

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