dm-verity.c 23 KB

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