dm-verity.c 22 KB

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