dm-verity.c 22 KB

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