dm-verity.c 23 KB

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