dm-verity.c 23 KB

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