verify.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Data verification functions, i.e. hooks for ->readahead()
  4. *
  5. * Copyright 2019 Google LLC
  6. */
  7. #include "fsverity_private.h"
  8. #include <crypto/hash.h>
  9. #include <linux/bio.h>
  10. static struct workqueue_struct *fsverity_read_workqueue;
  11. /*
  12. * Returns true if the hash block with index @hblock_idx in the tree, located in
  13. * @hpage, has already been verified.
  14. */
  15. static bool is_hash_block_verified(struct fsverity_info *vi, struct page *hpage,
  16. unsigned long hblock_idx)
  17. {
  18. unsigned int blocks_per_page;
  19. unsigned int i;
  20. /*
  21. * When the Merkle tree block size and page size are the same, then the
  22. * ->hash_block_verified bitmap isn't allocated, and we use PG_checked
  23. * to directly indicate whether the page's block has been verified.
  24. *
  25. * Using PG_checked also guarantees that we re-verify hash pages that
  26. * get evicted and re-instantiated from the backing storage, as new
  27. * pages always start out with PG_checked cleared.
  28. */
  29. if (!vi->hash_block_verified)
  30. return PageChecked(hpage);
  31. /*
  32. * When the Merkle tree block size and page size differ, we use a bitmap
  33. * to indicate whether each hash block has been verified.
  34. *
  35. * However, we still need to ensure that hash pages that get evicted and
  36. * re-instantiated from the backing storage are re-verified. To do
  37. * this, we use PG_checked again, but now it doesn't really mean
  38. * "checked". Instead, now it just serves as an indicator for whether
  39. * the hash page is newly instantiated or not. If the page is new, as
  40. * indicated by PG_checked=0, we clear the bitmap bits for the page's
  41. * blocks since they are untrustworthy, then set PG_checked=1.
  42. * Otherwise we return the bitmap bit for the requested block.
  43. *
  44. * Multiple threads may execute this code concurrently on the same page.
  45. * This is safe because we use memory barriers to ensure that if a
  46. * thread sees PG_checked=1, then it also sees the associated bitmap
  47. * clearing to have occurred. Also, all writes and their corresponding
  48. * reads are atomic, and all writes are safe to repeat in the event that
  49. * multiple threads get into the PG_checked=0 section. (Clearing a
  50. * bitmap bit again at worst causes a hash block to be verified
  51. * redundantly. That event should be very rare, so it's not worth using
  52. * a lock to avoid. Setting PG_checked again has no effect.)
  53. */
  54. if (PageChecked(hpage)) {
  55. /*
  56. * A read memory barrier is needed here to give ACQUIRE
  57. * semantics to the above PageChecked() test.
  58. */
  59. smp_rmb();
  60. return test_bit(hblock_idx, vi->hash_block_verified);
  61. }
  62. blocks_per_page = vi->tree_params.blocks_per_page;
  63. hblock_idx = round_down(hblock_idx, blocks_per_page);
  64. for (i = 0; i < blocks_per_page; i++)
  65. clear_bit(hblock_idx + i, vi->hash_block_verified);
  66. /*
  67. * A write memory barrier is needed here to give RELEASE semantics to
  68. * the below SetPageChecked() operation.
  69. */
  70. smp_wmb();
  71. SetPageChecked(hpage);
  72. return false;
  73. }
  74. /*
  75. * Verify a single data block against the file's Merkle tree.
  76. *
  77. * In principle, we need to verify the entire path to the root node. However,
  78. * for efficiency the filesystem may cache the hash blocks. Therefore we need
  79. * only ascend the tree until an already-verified hash block is seen, and then
  80. * verify the path to that block.
  81. *
  82. * Return: %true if the data block is valid, else %false.
  83. */
  84. static bool
  85. verify_data_block(struct inode *inode, struct fsverity_info *vi,
  86. const void *data, u64 data_pos, unsigned long max_ra_pages)
  87. {
  88. const struct merkle_tree_params *params = &vi->tree_params;
  89. const unsigned int hsize = params->digest_size;
  90. int level;
  91. u8 _want_hash[FS_VERITY_MAX_DIGEST_SIZE];
  92. const u8 *want_hash;
  93. u8 real_hash[FS_VERITY_MAX_DIGEST_SIZE];
  94. /* The hash blocks that are traversed, indexed by level */
  95. struct {
  96. /* Page containing the hash block */
  97. struct page *page;
  98. /* Mapped address of the hash block (will be within @page) */
  99. const void *addr;
  100. /* Index of the hash block in the tree overall */
  101. unsigned long index;
  102. /* Byte offset of the wanted hash relative to @addr */
  103. unsigned int hoffset;
  104. } hblocks[FS_VERITY_MAX_LEVELS];
  105. /*
  106. * The index of the previous level's block within that level; also the
  107. * index of that block's hash within the current level.
  108. */
  109. u64 hidx = data_pos >> params->log_blocksize;
  110. /* Up to 1 + FS_VERITY_MAX_LEVELS pages may be mapped at once */
  111. BUILD_BUG_ON(1 + FS_VERITY_MAX_LEVELS > KM_MAX_IDX);
  112. if (unlikely(data_pos >= inode->i_size)) {
  113. /*
  114. * This can happen in the data page spanning EOF when the Merkle
  115. * tree block size is less than the page size. The Merkle tree
  116. * doesn't cover data blocks fully past EOF. But the entire
  117. * page spanning EOF can be visible to userspace via a mmap, and
  118. * any part past EOF should be all zeroes. Therefore, we need
  119. * to verify that any data blocks fully past EOF are all zeroes.
  120. */
  121. if (memchr_inv(data, 0, params->block_size)) {
  122. fsverity_err(inode,
  123. "FILE CORRUPTED! Data past EOF is not zeroed");
  124. return false;
  125. }
  126. return true;
  127. }
  128. /*
  129. * Starting at the leaf level, ascend the tree saving hash blocks along
  130. * the way until we find a hash block that has already been verified, or
  131. * until we reach the root.
  132. */
  133. for (level = 0; level < params->num_levels; level++) {
  134. unsigned long next_hidx;
  135. unsigned long hblock_idx;
  136. pgoff_t hpage_idx;
  137. unsigned int hblock_offset_in_page;
  138. unsigned int hoffset;
  139. struct page *hpage;
  140. const void *haddr;
  141. /*
  142. * The index of the block in the current level; also the index
  143. * of that block's hash within the next level.
  144. */
  145. next_hidx = hidx >> params->log_arity;
  146. /* Index of the hash block in the tree overall */
  147. hblock_idx = params->level_start[level] + next_hidx;
  148. /* Index of the hash page in the tree overall */
  149. hpage_idx = hblock_idx >> params->log_blocks_per_page;
  150. /* Byte offset of the hash block within the page */
  151. hblock_offset_in_page =
  152. (hblock_idx << params->log_blocksize) & ~PAGE_MASK;
  153. /* Byte offset of the hash within the block */
  154. hoffset = (hidx << params->log_digestsize) &
  155. (params->block_size - 1);
  156. hpage = inode->i_sb->s_vop->read_merkle_tree_page(inode,
  157. hpage_idx, level == 0 ? min(max_ra_pages,
  158. params->tree_pages - hpage_idx) : 0);
  159. if (IS_ERR(hpage)) {
  160. fsverity_err(inode,
  161. "Error %ld reading Merkle tree page %lu",
  162. PTR_ERR(hpage), hpage_idx);
  163. goto error;
  164. }
  165. haddr = kmap_local_page(hpage) + hblock_offset_in_page;
  166. if (is_hash_block_verified(vi, hpage, hblock_idx)) {
  167. memcpy(_want_hash, haddr + hoffset, hsize);
  168. want_hash = _want_hash;
  169. kunmap_local(haddr);
  170. put_page(hpage);
  171. goto descend;
  172. }
  173. hblocks[level].page = hpage;
  174. hblocks[level].addr = haddr;
  175. hblocks[level].index = hblock_idx;
  176. hblocks[level].hoffset = hoffset;
  177. hidx = next_hidx;
  178. }
  179. want_hash = vi->root_hash;
  180. descend:
  181. /* Descend the tree verifying hash blocks. */
  182. for (; level > 0; level--) {
  183. struct page *hpage = hblocks[level - 1].page;
  184. const void *haddr = hblocks[level - 1].addr;
  185. unsigned long hblock_idx = hblocks[level - 1].index;
  186. unsigned int hoffset = hblocks[level - 1].hoffset;
  187. if (fsverity_hash_block(params, inode, haddr, real_hash) != 0)
  188. goto error;
  189. if (memcmp(want_hash, real_hash, hsize) != 0)
  190. goto corrupted;
  191. /*
  192. * Mark the hash block as verified. This must be atomic and
  193. * idempotent, as the same hash block might be verified by
  194. * multiple threads concurrently.
  195. */
  196. if (vi->hash_block_verified)
  197. set_bit(hblock_idx, vi->hash_block_verified);
  198. else
  199. SetPageChecked(hpage);
  200. memcpy(_want_hash, haddr + hoffset, hsize);
  201. want_hash = _want_hash;
  202. kunmap_local(haddr);
  203. put_page(hpage);
  204. }
  205. /* Finally, verify the data block. */
  206. if (fsverity_hash_block(params, inode, data, real_hash) != 0)
  207. goto error;
  208. if (memcmp(want_hash, real_hash, hsize) != 0)
  209. goto corrupted;
  210. return true;
  211. corrupted:
  212. fsverity_err(inode,
  213. "FILE CORRUPTED! pos=%llu, level=%d, want_hash=%s:%*phN, real_hash=%s:%*phN",
  214. data_pos, level - 1,
  215. params->hash_alg->name, hsize, want_hash,
  216. params->hash_alg->name, hsize, real_hash);
  217. error:
  218. for (; level > 0; level--) {
  219. kunmap_local(hblocks[level - 1].addr);
  220. put_page(hblocks[level - 1].page);
  221. }
  222. return false;
  223. }
  224. static bool
  225. verify_data_blocks(struct folio *data_folio, size_t len, size_t offset,
  226. unsigned long max_ra_pages)
  227. {
  228. struct inode *inode = data_folio->mapping->host;
  229. struct fsverity_info *vi = inode->i_verity_info;
  230. const unsigned int block_size = vi->tree_params.block_size;
  231. u64 pos = (u64)data_folio->index << PAGE_SHIFT;
  232. if (WARN_ON_ONCE(len <= 0 || !IS_ALIGNED(len | offset, block_size)))
  233. return false;
  234. if (WARN_ON_ONCE(!folio_test_locked(data_folio) ||
  235. folio_test_uptodate(data_folio)))
  236. return false;
  237. do {
  238. void *data;
  239. bool valid;
  240. data = kmap_local_folio(data_folio, offset);
  241. valid = verify_data_block(inode, vi, data, pos + offset,
  242. max_ra_pages);
  243. kunmap_local(data);
  244. if (!valid)
  245. return false;
  246. offset += block_size;
  247. len -= block_size;
  248. } while (len);
  249. return true;
  250. }
  251. /**
  252. * fsverity_verify_blocks() - verify data in a folio
  253. * @folio: the folio containing the data to verify
  254. * @len: the length of the data to verify in the folio
  255. * @offset: the offset of the data to verify in the folio
  256. *
  257. * Verify data that has just been read from a verity file. The data must be
  258. * located in a pagecache folio that is still locked and not yet uptodate. The
  259. * length and offset of the data must be Merkle tree block size aligned.
  260. *
  261. * Return: %true if the data is valid, else %false.
  262. */
  263. bool fsverity_verify_blocks(struct folio *folio, size_t len, size_t offset)
  264. {
  265. return verify_data_blocks(folio, len, offset, 0);
  266. }
  267. EXPORT_SYMBOL_GPL(fsverity_verify_blocks);
  268. #ifdef CONFIG_BLOCK
  269. /**
  270. * fsverity_verify_bio() - verify a 'read' bio that has just completed
  271. * @bio: the bio to verify
  272. *
  273. * Verify the bio's data against the file's Merkle tree. All bio data segments
  274. * must be aligned to the file's Merkle tree block size. If any data fails
  275. * verification, then bio->bi_status is set to an error status.
  276. *
  277. * This is a helper function for use by the ->readahead() method of filesystems
  278. * that issue bios to read data directly into the page cache. Filesystems that
  279. * populate the page cache without issuing bios (e.g. non block-based
  280. * filesystems) must instead call fsverity_verify_page() directly on each page.
  281. * All filesystems must also call fsverity_verify_page() on holes.
  282. */
  283. void fsverity_verify_bio(struct bio *bio)
  284. {
  285. struct folio_iter fi;
  286. unsigned long max_ra_pages = 0;
  287. if (bio->bi_opf & REQ_RAHEAD) {
  288. /*
  289. * If this bio is for data readahead, then we also do readahead
  290. * of the first (largest) level of the Merkle tree. Namely,
  291. * when a Merkle tree page is read, we also try to piggy-back on
  292. * some additional pages -- up to 1/4 the number of data pages.
  293. *
  294. * This improves sequential read performance, as it greatly
  295. * reduces the number of I/O requests made to the Merkle tree.
  296. */
  297. max_ra_pages = bio->bi_iter.bi_size >> (PAGE_SHIFT + 2);
  298. }
  299. bio_for_each_folio_all(fi, bio) {
  300. if (!verify_data_blocks(fi.folio, fi.length, fi.offset,
  301. max_ra_pages)) {
  302. bio->bi_status = BLK_STS_IOERR;
  303. break;
  304. }
  305. }
  306. }
  307. EXPORT_SYMBOL_GPL(fsverity_verify_bio);
  308. #endif /* CONFIG_BLOCK */
  309. /**
  310. * fsverity_enqueue_verify_work() - enqueue work on the fs-verity workqueue
  311. * @work: the work to enqueue
  312. *
  313. * Enqueue verification work for asynchronous processing.
  314. */
  315. void fsverity_enqueue_verify_work(struct work_struct *work)
  316. {
  317. queue_work(fsverity_read_workqueue, work);
  318. }
  319. EXPORT_SYMBOL_GPL(fsverity_enqueue_verify_work);
  320. void __init fsverity_init_workqueue(void)
  321. {
  322. /*
  323. * Use a high-priority workqueue to prioritize verification work, which
  324. * blocks reads from completing, over regular application tasks.
  325. *
  326. * For performance reasons, don't use an unbound workqueue. Using an
  327. * unbound workqueue for crypto operations causes excessive scheduler
  328. * latency on ARM64.
  329. */
  330. fsverity_read_workqueue = alloc_workqueue("fsverity_read_queue",
  331. WQ_HIGHPRI,
  332. num_online_cpus());
  333. if (!fsverity_read_workqueue)
  334. panic("failed to allocate fsverity_read_queue");
  335. }