dm-verity.c 25 KB

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