dm-verity.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. struct bvec_iter iter;
  62. struct work_struct work;
  63. /*
  64. * Three variably-size fields follow this struct:
  65. *
  66. * u8 hash_desc[v->shash_descsize];
  67. * u8 real_digest[v->digest_size];
  68. * u8 want_digest[v->digest_size];
  69. *
  70. * To access them use: io_hash_desc(), io_real_digest() and io_want_digest().
  71. */
  72. };
  73. struct dm_verity_prefetch_work {
  74. struct work_struct work;
  75. struct dm_verity *v;
  76. sector_t block;
  77. unsigned n_blocks;
  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. struct bio *bio = dm_bio_from_per_bio_data(io,
  236. v->ti->per_bio_data_size);
  237. unsigned b;
  238. int i;
  239. for (b = 0; b < io->n_blocks; b++) {
  240. struct shash_desc *desc;
  241. u8 *result;
  242. int r;
  243. unsigned todo;
  244. if (likely(v->levels)) {
  245. /*
  246. * First, we try to get the requested hash for
  247. * the current block. If the hash block itself is
  248. * verified, zero is returned. If it isn't, this
  249. * function returns 0 and we fall back to whole
  250. * chain verification.
  251. */
  252. int r = verity_verify_level(io, io->block + b, 0, true);
  253. if (likely(!r))
  254. goto test_block_hash;
  255. if (r < 0)
  256. return r;
  257. }
  258. memcpy(io_want_digest(v, io), v->root_digest, v->digest_size);
  259. for (i = v->levels - 1; i >= 0; i--) {
  260. int r = verity_verify_level(io, io->block + b, i, false);
  261. if (unlikely(r))
  262. return r;
  263. }
  264. test_block_hash:
  265. desc = io_hash_desc(v, io);
  266. desc->tfm = v->tfm;
  267. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  268. r = crypto_shash_init(desc);
  269. if (r < 0) {
  270. DMERR("crypto_shash_init failed: %d", r);
  271. return r;
  272. }
  273. if (likely(v->version >= 1)) {
  274. r = crypto_shash_update(desc, v->salt, v->salt_size);
  275. if (r < 0) {
  276. DMERR("crypto_shash_update failed: %d", r);
  277. return r;
  278. }
  279. }
  280. todo = 1 << v->data_dev_block_bits;
  281. do {
  282. u8 *page;
  283. unsigned len;
  284. struct bio_vec bv = bio_iter_iovec(bio, io->iter);
  285. page = kmap_atomic(bv.bv_page);
  286. len = bv.bv_len;
  287. if (likely(len >= todo))
  288. len = todo;
  289. r = crypto_shash_update(desc, page + bv.bv_offset, len);
  290. kunmap_atomic(page);
  291. if (r < 0) {
  292. DMERR("crypto_shash_update failed: %d", r);
  293. return r;
  294. }
  295. bio_advance_iter(bio, &io->iter, len);
  296. todo -= len;
  297. } while (todo);
  298. if (!v->version) {
  299. r = crypto_shash_update(desc, v->salt, v->salt_size);
  300. if (r < 0) {
  301. DMERR("crypto_shash_update failed: %d", r);
  302. return r;
  303. }
  304. }
  305. result = io_real_digest(v, io);
  306. r = crypto_shash_final(desc, result);
  307. if (r < 0) {
  308. DMERR("crypto_shash_final failed: %d", r);
  309. return r;
  310. }
  311. if (unlikely(memcmp(result, io_want_digest(v, io), v->digest_size))) {
  312. DMERR_LIMIT("data block %llu is corrupted",
  313. (unsigned long long)(io->block + b));
  314. v->hash_failed = 1;
  315. return -EIO;
  316. }
  317. }
  318. return 0;
  319. }
  320. /*
  321. * End one "io" structure with a given error.
  322. */
  323. static void verity_finish_io(struct dm_verity_io *io, int error)
  324. {
  325. struct dm_verity *v = io->v;
  326. struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_bio_data_size);
  327. bio->bi_end_io = io->orig_bi_end_io;
  328. bio->bi_private = io->orig_bi_private;
  329. bio_endio_nodec(bio, error);
  330. }
  331. static void verity_work(struct work_struct *w)
  332. {
  333. struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
  334. verity_finish_io(io, verity_verify_io(io));
  335. }
  336. static void verity_end_io(struct bio *bio, int error)
  337. {
  338. struct dm_verity_io *io = bio->bi_private;
  339. if (error) {
  340. verity_finish_io(io, error);
  341. return;
  342. }
  343. INIT_WORK(&io->work, verity_work);
  344. queue_work(io->v->verify_wq, &io->work);
  345. }
  346. /*
  347. * Prefetch buffers for the specified io.
  348. * The root buffer is not prefetched, it is assumed that it will be cached
  349. * all the time.
  350. */
  351. static void verity_prefetch_io(struct work_struct *work)
  352. {
  353. struct dm_verity_prefetch_work *pw =
  354. container_of(work, struct dm_verity_prefetch_work, work);
  355. struct dm_verity *v = pw->v;
  356. int i;
  357. for (i = v->levels - 2; i >= 0; i--) {
  358. sector_t hash_block_start;
  359. sector_t hash_block_end;
  360. verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL);
  361. verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL);
  362. if (!i) {
  363. unsigned cluster = ACCESS_ONCE(dm_verity_prefetch_cluster);
  364. cluster >>= v->data_dev_block_bits;
  365. if (unlikely(!cluster))
  366. goto no_prefetch_cluster;
  367. if (unlikely(cluster & (cluster - 1)))
  368. cluster = 1 << __fls(cluster);
  369. hash_block_start &= ~(sector_t)(cluster - 1);
  370. hash_block_end |= cluster - 1;
  371. if (unlikely(hash_block_end >= v->hash_blocks))
  372. hash_block_end = v->hash_blocks - 1;
  373. }
  374. no_prefetch_cluster:
  375. dm_bufio_prefetch(v->bufio, hash_block_start,
  376. hash_block_end - hash_block_start + 1);
  377. }
  378. kfree(pw);
  379. }
  380. static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io)
  381. {
  382. struct dm_verity_prefetch_work *pw;
  383. pw = kmalloc(sizeof(struct dm_verity_prefetch_work),
  384. GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
  385. if (!pw)
  386. return;
  387. INIT_WORK(&pw->work, verity_prefetch_io);
  388. pw->v = v;
  389. pw->block = io->block;
  390. pw->n_blocks = io->n_blocks;
  391. queue_work(v->verify_wq, &pw->work);
  392. }
  393. /*
  394. * Bio map function. It allocates dm_verity_io structure and bio vector and
  395. * fills them. Then it issues prefetches and the I/O.
  396. */
  397. static int verity_map(struct dm_target *ti, struct bio *bio)
  398. {
  399. struct dm_verity *v = ti->private;
  400. struct dm_verity_io *io;
  401. bio->bi_bdev = v->data_dev->bdev;
  402. bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector);
  403. if (((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
  404. ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) {
  405. DMERR_LIMIT("unaligned io");
  406. return -EIO;
  407. }
  408. if (bio_end_sector(bio) >>
  409. (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
  410. DMERR_LIMIT("io out of range");
  411. return -EIO;
  412. }
  413. if (bio_data_dir(bio) == WRITE)
  414. return -EIO;
  415. io = dm_per_bio_data(bio, ti->per_bio_data_size);
  416. io->v = v;
  417. io->orig_bi_end_io = bio->bi_end_io;
  418. io->orig_bi_private = bio->bi_private;
  419. io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
  420. io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits;
  421. bio->bi_end_io = verity_end_io;
  422. bio->bi_private = io;
  423. io->iter = bio->bi_iter;
  424. verity_submit_prefetch(v, io);
  425. generic_make_request(bio);
  426. return DM_MAPIO_SUBMITTED;
  427. }
  428. /*
  429. * Status: V (valid) or C (corruption found)
  430. */
  431. static void verity_status(struct dm_target *ti, status_type_t type,
  432. unsigned status_flags, char *result, unsigned maxlen)
  433. {
  434. struct dm_verity *v = ti->private;
  435. unsigned sz = 0;
  436. unsigned x;
  437. switch (type) {
  438. case STATUSTYPE_INFO:
  439. DMEMIT("%c", v->hash_failed ? 'C' : 'V');
  440. break;
  441. case STATUSTYPE_TABLE:
  442. DMEMIT("%u %s %s %u %u %llu %llu %s ",
  443. v->version,
  444. v->data_dev->name,
  445. v->hash_dev->name,
  446. 1 << v->data_dev_block_bits,
  447. 1 << v->hash_dev_block_bits,
  448. (unsigned long long)v->data_blocks,
  449. (unsigned long long)v->hash_start,
  450. v->alg_name
  451. );
  452. for (x = 0; x < v->digest_size; x++)
  453. DMEMIT("%02x", v->root_digest[x]);
  454. DMEMIT(" ");
  455. if (!v->salt_size)
  456. DMEMIT("-");
  457. else
  458. for (x = 0; x < v->salt_size; x++)
  459. DMEMIT("%02x", v->salt[x]);
  460. break;
  461. }
  462. }
  463. static int verity_ioctl(struct dm_target *ti, unsigned cmd,
  464. unsigned long arg)
  465. {
  466. struct dm_verity *v = ti->private;
  467. int r = 0;
  468. if (v->data_start ||
  469. ti->len != i_size_read(v->data_dev->bdev->bd_inode) >> SECTOR_SHIFT)
  470. r = scsi_verify_blk_ioctl(NULL, cmd);
  471. return r ? : __blkdev_driver_ioctl(v->data_dev->bdev, v->data_dev->mode,
  472. cmd, arg);
  473. }
  474. static int verity_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  475. struct bio_vec *biovec, int max_size)
  476. {
  477. struct dm_verity *v = ti->private;
  478. struct request_queue *q = bdev_get_queue(v->data_dev->bdev);
  479. if (!q->merge_bvec_fn)
  480. return max_size;
  481. bvm->bi_bdev = v->data_dev->bdev;
  482. bvm->bi_sector = verity_map_sector(v, bvm->bi_sector);
  483. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  484. }
  485. static int verity_iterate_devices(struct dm_target *ti,
  486. iterate_devices_callout_fn fn, void *data)
  487. {
  488. struct dm_verity *v = ti->private;
  489. return fn(ti, v->data_dev, v->data_start, ti->len, data);
  490. }
  491. static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits)
  492. {
  493. struct dm_verity *v = ti->private;
  494. if (limits->logical_block_size < 1 << v->data_dev_block_bits)
  495. limits->logical_block_size = 1 << v->data_dev_block_bits;
  496. if (limits->physical_block_size < 1 << v->data_dev_block_bits)
  497. limits->physical_block_size = 1 << v->data_dev_block_bits;
  498. blk_limits_io_min(limits, limits->logical_block_size);
  499. }
  500. static void verity_dtr(struct dm_target *ti)
  501. {
  502. struct dm_verity *v = ti->private;
  503. if (v->verify_wq)
  504. destroy_workqueue(v->verify_wq);
  505. if (v->vec_mempool)
  506. mempool_destroy(v->vec_mempool);
  507. if (v->bufio)
  508. dm_bufio_client_destroy(v->bufio);
  509. kfree(v->salt);
  510. kfree(v->root_digest);
  511. if (v->tfm)
  512. crypto_free_shash(v->tfm);
  513. kfree(v->alg_name);
  514. if (v->hash_dev)
  515. dm_put_device(ti, v->hash_dev);
  516. if (v->data_dev)
  517. dm_put_device(ti, v->data_dev);
  518. kfree(v);
  519. }
  520. /*
  521. * Target parameters:
  522. * <version> The current format is version 1.
  523. * Vsn 0 is compatible with original Chromium OS releases.
  524. * <data device>
  525. * <hash device>
  526. * <data block size>
  527. * <hash block size>
  528. * <the number of data blocks>
  529. * <hash start block>
  530. * <algorithm>
  531. * <digest>
  532. * <salt> Hex string or "-" if no salt.
  533. */
  534. static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv)
  535. {
  536. struct dm_verity *v;
  537. unsigned num;
  538. unsigned long long num_ll;
  539. int r;
  540. int i;
  541. sector_t hash_position;
  542. char dummy;
  543. v = kzalloc(sizeof(struct dm_verity), GFP_KERNEL);
  544. if (!v) {
  545. ti->error = "Cannot allocate verity structure";
  546. return -ENOMEM;
  547. }
  548. ti->private = v;
  549. v->ti = ti;
  550. if ((dm_table_get_mode(ti->table) & ~FMODE_READ)) {
  551. ti->error = "Device must be readonly";
  552. r = -EINVAL;
  553. goto bad;
  554. }
  555. if (argc != 10) {
  556. ti->error = "Invalid argument count: exactly 10 arguments required";
  557. r = -EINVAL;
  558. goto bad;
  559. }
  560. if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
  561. num > 1) {
  562. ti->error = "Invalid version";
  563. r = -EINVAL;
  564. goto bad;
  565. }
  566. v->version = num;
  567. r = dm_get_device(ti, argv[1], FMODE_READ, &v->data_dev);
  568. if (r) {
  569. ti->error = "Data device lookup failed";
  570. goto bad;
  571. }
  572. r = dm_get_device(ti, argv[2], FMODE_READ, &v->hash_dev);
  573. if (r) {
  574. ti->error = "Data device lookup failed";
  575. goto bad;
  576. }
  577. if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
  578. !num || (num & (num - 1)) ||
  579. num < bdev_logical_block_size(v->data_dev->bdev) ||
  580. num > PAGE_SIZE) {
  581. ti->error = "Invalid data device block size";
  582. r = -EINVAL;
  583. goto bad;
  584. }
  585. v->data_dev_block_bits = __ffs(num);
  586. if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
  587. !num || (num & (num - 1)) ||
  588. num < bdev_logical_block_size(v->hash_dev->bdev) ||
  589. num > INT_MAX) {
  590. ti->error = "Invalid hash device block size";
  591. r = -EINVAL;
  592. goto bad;
  593. }
  594. v->hash_dev_block_bits = __ffs(num);
  595. if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
  596. (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
  597. >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
  598. ti->error = "Invalid data blocks";
  599. r = -EINVAL;
  600. goto bad;
  601. }
  602. v->data_blocks = num_ll;
  603. if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
  604. ti->error = "Data device is too small";
  605. r = -EINVAL;
  606. goto bad;
  607. }
  608. if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
  609. (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
  610. >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
  611. ti->error = "Invalid hash start";
  612. r = -EINVAL;
  613. goto bad;
  614. }
  615. v->hash_start = num_ll;
  616. v->alg_name = kstrdup(argv[7], GFP_KERNEL);
  617. if (!v->alg_name) {
  618. ti->error = "Cannot allocate algorithm name";
  619. r = -ENOMEM;
  620. goto bad;
  621. }
  622. v->tfm = crypto_alloc_shash(v->alg_name, 0, 0);
  623. if (IS_ERR(v->tfm)) {
  624. ti->error = "Cannot initialize hash function";
  625. r = PTR_ERR(v->tfm);
  626. v->tfm = NULL;
  627. goto bad;
  628. }
  629. v->digest_size = crypto_shash_digestsize(v->tfm);
  630. if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
  631. ti->error = "Digest size too big";
  632. r = -EINVAL;
  633. goto bad;
  634. }
  635. v->shash_descsize =
  636. sizeof(struct shash_desc) + crypto_shash_descsize(v->tfm);
  637. v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
  638. if (!v->root_digest) {
  639. ti->error = "Cannot allocate root digest";
  640. r = -ENOMEM;
  641. goto bad;
  642. }
  643. if (strlen(argv[8]) != v->digest_size * 2 ||
  644. hex2bin(v->root_digest, argv[8], v->digest_size)) {
  645. ti->error = "Invalid root digest";
  646. r = -EINVAL;
  647. goto bad;
  648. }
  649. if (strcmp(argv[9], "-")) {
  650. v->salt_size = strlen(argv[9]) / 2;
  651. v->salt = kmalloc(v->salt_size, GFP_KERNEL);
  652. if (!v->salt) {
  653. ti->error = "Cannot allocate salt";
  654. r = -ENOMEM;
  655. goto bad;
  656. }
  657. if (strlen(argv[9]) != v->salt_size * 2 ||
  658. hex2bin(v->salt, argv[9], v->salt_size)) {
  659. ti->error = "Invalid salt";
  660. r = -EINVAL;
  661. goto bad;
  662. }
  663. }
  664. v->hash_per_block_bits =
  665. __fls((1 << v->hash_dev_block_bits) / v->digest_size);
  666. v->levels = 0;
  667. if (v->data_blocks)
  668. while (v->hash_per_block_bits * v->levels < 64 &&
  669. (unsigned long long)(v->data_blocks - 1) >>
  670. (v->hash_per_block_bits * v->levels))
  671. v->levels++;
  672. if (v->levels > DM_VERITY_MAX_LEVELS) {
  673. ti->error = "Too many tree levels";
  674. r = -E2BIG;
  675. goto bad;
  676. }
  677. hash_position = v->hash_start;
  678. for (i = v->levels - 1; i >= 0; i--) {
  679. sector_t s;
  680. v->hash_level_block[i] = hash_position;
  681. s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
  682. >> ((i + 1) * v->hash_per_block_bits);
  683. if (hash_position + s < hash_position) {
  684. ti->error = "Hash device offset overflow";
  685. r = -E2BIG;
  686. goto bad;
  687. }
  688. hash_position += s;
  689. }
  690. v->hash_blocks = hash_position;
  691. v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
  692. 1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
  693. dm_bufio_alloc_callback, NULL);
  694. if (IS_ERR(v->bufio)) {
  695. ti->error = "Cannot initialize dm-bufio";
  696. r = PTR_ERR(v->bufio);
  697. v->bufio = NULL;
  698. goto bad;
  699. }
  700. if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
  701. ti->error = "Hash device is too small";
  702. r = -E2BIG;
  703. goto bad;
  704. }
  705. ti->per_bio_data_size = roundup(sizeof(struct dm_verity_io) + v->shash_descsize + v->digest_size * 2, __alignof__(struct dm_verity_io));
  706. v->vec_mempool = mempool_create_kmalloc_pool(DM_VERITY_MEMPOOL_SIZE,
  707. BIO_MAX_PAGES * sizeof(struct bio_vec));
  708. if (!v->vec_mempool) {
  709. ti->error = "Cannot allocate vector mempool";
  710. r = -ENOMEM;
  711. goto bad;
  712. }
  713. /* WQ_UNBOUND greatly improves performance when running on ramdisk */
  714. v->verify_wq = alloc_workqueue("kverityd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, num_online_cpus());
  715. if (!v->verify_wq) {
  716. ti->error = "Cannot allocate workqueue";
  717. r = -ENOMEM;
  718. goto bad;
  719. }
  720. return 0;
  721. bad:
  722. verity_dtr(ti);
  723. return r;
  724. }
  725. static struct target_type verity_target = {
  726. .name = "verity",
  727. .version = {1, 2, 0},
  728. .module = THIS_MODULE,
  729. .ctr = verity_ctr,
  730. .dtr = verity_dtr,
  731. .map = verity_map,
  732. .status = verity_status,
  733. .ioctl = verity_ioctl,
  734. .merge = verity_merge,
  735. .iterate_devices = verity_iterate_devices,
  736. .io_hints = verity_io_hints,
  737. };
  738. static int __init dm_verity_init(void)
  739. {
  740. int r;
  741. r = dm_register_target(&verity_target);
  742. if (r < 0)
  743. DMERR("register failed %d", r);
  744. return r;
  745. }
  746. static void __exit dm_verity_exit(void)
  747. {
  748. dm_unregister_target(&verity_target);
  749. }
  750. module_init(dm_verity_init);
  751. module_exit(dm_verity_exit);
  752. MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
  753. MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>");
  754. MODULE_AUTHOR("Will Drewry <wad@chromium.org>");
  755. MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking");
  756. MODULE_LICENSE("GPL");