18a1d0f9cSEric Biggers // SPDX-License-Identifier: GPL-2.0
28a1d0f9cSEric Biggers /*
3704528d8SMatthew Wilcox (Oracle) * Data verification functions, i.e. hooks for ->readahead()
48a1d0f9cSEric Biggers *
58a1d0f9cSEric Biggers * Copyright 2019 Google LLC
68a1d0f9cSEric Biggers */
78a1d0f9cSEric Biggers
88a1d0f9cSEric Biggers #include "fsverity_private.h"
98a1d0f9cSEric Biggers
108a1d0f9cSEric Biggers #include <crypto/hash.h>
118a1d0f9cSEric Biggers #include <linux/bio.h>
128a1d0f9cSEric Biggers
138a1d0f9cSEric Biggers static struct workqueue_struct *fsverity_read_workqueue;
148a1d0f9cSEric Biggers
158a1d0f9cSEric Biggers /*
165306892aSEric Biggers * Returns true if the hash block with index @hblock_idx in the tree, located in
175306892aSEric Biggers * @hpage, has already been verified.
185306892aSEric Biggers */
is_hash_block_verified(struct fsverity_info * vi,struct page * hpage,unsigned long hblock_idx)195306892aSEric Biggers static bool is_hash_block_verified(struct fsverity_info *vi, struct page *hpage,
205306892aSEric Biggers unsigned long hblock_idx)
215306892aSEric Biggers {
225306892aSEric Biggers bool verified;
235306892aSEric Biggers unsigned int blocks_per_page;
245306892aSEric Biggers unsigned int i;
255306892aSEric Biggers
265306892aSEric Biggers /*
275306892aSEric Biggers * When the Merkle tree block size and page size are the same, then the
285306892aSEric Biggers * ->hash_block_verified bitmap isn't allocated, and we use PG_checked
295306892aSEric Biggers * to directly indicate whether the page's block has been verified.
305306892aSEric Biggers *
315306892aSEric Biggers * Using PG_checked also guarantees that we re-verify hash pages that
325306892aSEric Biggers * get evicted and re-instantiated from the backing storage, as new
335306892aSEric Biggers * pages always start out with PG_checked cleared.
345306892aSEric Biggers */
355306892aSEric Biggers if (!vi->hash_block_verified)
365306892aSEric Biggers return PageChecked(hpage);
375306892aSEric Biggers
385306892aSEric Biggers /*
395306892aSEric Biggers * When the Merkle tree block size and page size differ, we use a bitmap
405306892aSEric Biggers * to indicate whether each hash block has been verified.
415306892aSEric Biggers *
425306892aSEric Biggers * However, we still need to ensure that hash pages that get evicted and
435306892aSEric Biggers * re-instantiated from the backing storage are re-verified. To do
445306892aSEric Biggers * this, we use PG_checked again, but now it doesn't really mean
455306892aSEric Biggers * "checked". Instead, now it just serves as an indicator for whether
465306892aSEric Biggers * the hash page is newly instantiated or not.
475306892aSEric Biggers *
485306892aSEric Biggers * The first thread that sees PG_checked=0 must clear the corresponding
495306892aSEric Biggers * bitmap bits, then set PG_checked=1. This requires a spinlock. To
505306892aSEric Biggers * avoid having to take this spinlock in the common case of
515306892aSEric Biggers * PG_checked=1, we start with an opportunistic lockless read.
525306892aSEric Biggers */
535306892aSEric Biggers if (PageChecked(hpage)) {
545306892aSEric Biggers /*
555306892aSEric Biggers * A read memory barrier is needed here to give ACQUIRE
565306892aSEric Biggers * semantics to the above PageChecked() test.
575306892aSEric Biggers */
585306892aSEric Biggers smp_rmb();
595306892aSEric Biggers return test_bit(hblock_idx, vi->hash_block_verified);
605306892aSEric Biggers }
615306892aSEric Biggers spin_lock(&vi->hash_page_init_lock);
625306892aSEric Biggers if (PageChecked(hpage)) {
635306892aSEric Biggers verified = test_bit(hblock_idx, vi->hash_block_verified);
645306892aSEric Biggers } else {
655306892aSEric Biggers blocks_per_page = vi->tree_params.blocks_per_page;
665306892aSEric Biggers hblock_idx = round_down(hblock_idx, blocks_per_page);
675306892aSEric Biggers for (i = 0; i < blocks_per_page; i++)
685306892aSEric Biggers clear_bit(hblock_idx + i, vi->hash_block_verified);
695306892aSEric Biggers /*
705306892aSEric Biggers * A write memory barrier is needed here to give RELEASE
715306892aSEric Biggers * semantics to the below SetPageChecked() operation.
725306892aSEric Biggers */
735306892aSEric Biggers smp_wmb();
745306892aSEric Biggers SetPageChecked(hpage);
755306892aSEric Biggers verified = false;
765306892aSEric Biggers }
775306892aSEric Biggers spin_unlock(&vi->hash_page_init_lock);
785306892aSEric Biggers return verified;
795306892aSEric Biggers }
805306892aSEric Biggers
815306892aSEric Biggers /*
825306892aSEric Biggers * Verify a single data block against the file's Merkle tree.
838a1d0f9cSEric Biggers *
848a1d0f9cSEric Biggers * In principle, we need to verify the entire path to the root node. However,
855306892aSEric Biggers * for efficiency the filesystem may cache the hash blocks. Therefore we need
865306892aSEric Biggers * only ascend the tree until an already-verified hash block is seen, and then
875306892aSEric Biggers * verify the path to that block.
888a1d0f9cSEric Biggers *
895306892aSEric Biggers * Return: %true if the data block is valid, else %false.
908a1d0f9cSEric Biggers */
915306892aSEric Biggers static bool
verify_data_block(struct inode * inode,struct fsverity_info * vi,const void * data,u64 data_pos,unsigned long max_ra_pages)925306892aSEric Biggers verify_data_block(struct inode *inode, struct fsverity_info *vi,
938fcd94adSEric Biggers const void *data, u64 data_pos, unsigned long max_ra_pages)
948a1d0f9cSEric Biggers {
958a1d0f9cSEric Biggers const struct merkle_tree_params *params = &vi->tree_params;
968a1d0f9cSEric Biggers const unsigned int hsize = params->digest_size;
978a1d0f9cSEric Biggers int level;
988a1d0f9cSEric Biggers u8 _want_hash[FS_VERITY_MAX_DIGEST_SIZE];
998a1d0f9cSEric Biggers const u8 *want_hash;
1008a1d0f9cSEric Biggers u8 real_hash[FS_VERITY_MAX_DIGEST_SIZE];
1015306892aSEric Biggers /* The hash blocks that are traversed, indexed by level */
1025306892aSEric Biggers struct {
1035306892aSEric Biggers /* Page containing the hash block */
1045306892aSEric Biggers struct page *page;
1058fcd94adSEric Biggers /* Mapped address of the hash block (will be within @page) */
1068fcd94adSEric Biggers const void *addr;
1075306892aSEric Biggers /* Index of the hash block in the tree overall */
1085306892aSEric Biggers unsigned long index;
1098fcd94adSEric Biggers /* Byte offset of the wanted hash relative to @addr */
1105306892aSEric Biggers unsigned int hoffset;
1115306892aSEric Biggers } hblocks[FS_VERITY_MAX_LEVELS];
1125306892aSEric Biggers /*
1135306892aSEric Biggers * The index of the previous level's block within that level; also the
1145306892aSEric Biggers * index of that block's hash within the current level.
1155306892aSEric Biggers */
1165306892aSEric Biggers u64 hidx = data_pos >> params->log_blocksize;
1178a1d0f9cSEric Biggers
1188fcd94adSEric Biggers /* Up to 1 + FS_VERITY_MAX_LEVELS pages may be mapped at once */
1198fcd94adSEric Biggers BUILD_BUG_ON(1 + FS_VERITY_MAX_LEVELS > KM_MAX_IDX);
1208fcd94adSEric Biggers
1215306892aSEric Biggers if (unlikely(data_pos >= inode->i_size)) {
1225306892aSEric Biggers /*
1235306892aSEric Biggers * This can happen in the data page spanning EOF when the Merkle
1245306892aSEric Biggers * tree block size is less than the page size. The Merkle tree
1255306892aSEric Biggers * doesn't cover data blocks fully past EOF. But the entire
1265306892aSEric Biggers * page spanning EOF can be visible to userspace via a mmap, and
1275306892aSEric Biggers * any part past EOF should be all zeroes. Therefore, we need
1285306892aSEric Biggers * to verify that any data blocks fully past EOF are all zeroes.
1295306892aSEric Biggers */
1308fcd94adSEric Biggers if (memchr_inv(data, 0, params->block_size)) {
1318fcd94adSEric Biggers fsverity_err(inode,
1328fcd94adSEric Biggers "FILE CORRUPTED! Data past EOF is not zeroed");
1338fcd94adSEric Biggers return false;
1348fcd94adSEric Biggers }
1358fcd94adSEric Biggers return true;
1365306892aSEric Biggers }
1378a1d0f9cSEric Biggers
1388a1d0f9cSEric Biggers /*
1395306892aSEric Biggers * Starting at the leaf level, ascend the tree saving hash blocks along
1405306892aSEric Biggers * the way until we find a hash block that has already been verified, or
1415306892aSEric Biggers * until we reach the root.
1428a1d0f9cSEric Biggers */
1438a1d0f9cSEric Biggers for (level = 0; level < params->num_levels; level++) {
1445306892aSEric Biggers unsigned long next_hidx;
1455306892aSEric Biggers unsigned long hblock_idx;
1465306892aSEric Biggers pgoff_t hpage_idx;
1475306892aSEric Biggers unsigned int hblock_offset_in_page;
1488a1d0f9cSEric Biggers unsigned int hoffset;
1498a1d0f9cSEric Biggers struct page *hpage;
1508fcd94adSEric Biggers const void *haddr;
1518a1d0f9cSEric Biggers
1525306892aSEric Biggers /*
1535306892aSEric Biggers * The index of the block in the current level; also the index
1545306892aSEric Biggers * of that block's hash within the next level.
1555306892aSEric Biggers */
1565306892aSEric Biggers next_hidx = hidx >> params->log_arity;
1578a1d0f9cSEric Biggers
1585306892aSEric Biggers /* Index of the hash block in the tree overall */
1595306892aSEric Biggers hblock_idx = params->level_start[level] + next_hidx;
1605306892aSEric Biggers
1615306892aSEric Biggers /* Index of the hash page in the tree overall */
1625306892aSEric Biggers hpage_idx = hblock_idx >> params->log_blocks_per_page;
1635306892aSEric Biggers
1645306892aSEric Biggers /* Byte offset of the hash block within the page */
1655306892aSEric Biggers hblock_offset_in_page =
1665306892aSEric Biggers (hblock_idx << params->log_blocksize) & ~PAGE_MASK;
1675306892aSEric Biggers
1688fcd94adSEric Biggers /* Byte offset of the hash within the block */
1698fcd94adSEric Biggers hoffset = (hidx << params->log_digestsize) &
1708fcd94adSEric Biggers (params->block_size - 1);
1715306892aSEric Biggers
1725306892aSEric Biggers hpage = inode->i_sb->s_vop->read_merkle_tree_page(inode,
1735306892aSEric Biggers hpage_idx, level == 0 ? min(max_ra_pages,
1745306892aSEric Biggers params->tree_pages - hpage_idx) : 0);
1758a1d0f9cSEric Biggers if (IS_ERR(hpage)) {
1768a1d0f9cSEric Biggers fsverity_err(inode,
17713e2408dSEric Biggers "Error %ld reading Merkle tree page %lu",
17813e2408dSEric Biggers PTR_ERR(hpage), hpage_idx);
17913e2408dSEric Biggers goto error;
1808a1d0f9cSEric Biggers }
1818fcd94adSEric Biggers haddr = kmap_local_page(hpage) + hblock_offset_in_page;
1825306892aSEric Biggers if (is_hash_block_verified(vi, hpage, hblock_idx)) {
1838fcd94adSEric Biggers memcpy(_want_hash, haddr + hoffset, hsize);
1848a1d0f9cSEric Biggers want_hash = _want_hash;
1858fcd94adSEric Biggers kunmap_local(haddr);
1868a1d0f9cSEric Biggers put_page(hpage);
1878a1d0f9cSEric Biggers goto descend;
1888a1d0f9cSEric Biggers }
1895306892aSEric Biggers hblocks[level].page = hpage;
1908fcd94adSEric Biggers hblocks[level].addr = haddr;
1915306892aSEric Biggers hblocks[level].index = hblock_idx;
1925306892aSEric Biggers hblocks[level].hoffset = hoffset;
1935306892aSEric Biggers hidx = next_hidx;
1948a1d0f9cSEric Biggers }
1958a1d0f9cSEric Biggers
1968a1d0f9cSEric Biggers want_hash = vi->root_hash;
1978a1d0f9cSEric Biggers descend:
198f45555bfSEric Biggers /* Descend the tree verifying hash blocks. */
1998a1d0f9cSEric Biggers for (; level > 0; level--) {
2005306892aSEric Biggers struct page *hpage = hblocks[level - 1].page;
2018fcd94adSEric Biggers const void *haddr = hblocks[level - 1].addr;
2025306892aSEric Biggers unsigned long hblock_idx = hblocks[level - 1].index;
2035306892aSEric Biggers unsigned int hoffset = hblocks[level - 1].hoffset;
2048a1d0f9cSEric Biggers
20513e2408dSEric Biggers if (fsverity_hash_block(params, inode, haddr, real_hash) != 0)
20613e2408dSEric Biggers goto error;
20713e2408dSEric Biggers if (memcmp(want_hash, real_hash, hsize) != 0)
20813e2408dSEric Biggers goto corrupted;
2095306892aSEric Biggers /*
2105306892aSEric Biggers * Mark the hash block as verified. This must be atomic and
2115306892aSEric Biggers * idempotent, as the same hash block might be verified by
2125306892aSEric Biggers * multiple threads concurrently.
2135306892aSEric Biggers */
2145306892aSEric Biggers if (vi->hash_block_verified)
2155306892aSEric Biggers set_bit(hblock_idx, vi->hash_block_verified);
2165306892aSEric Biggers else
2178a1d0f9cSEric Biggers SetPageChecked(hpage);
2188fcd94adSEric Biggers memcpy(_want_hash, haddr + hoffset, hsize);
2198a1d0f9cSEric Biggers want_hash = _want_hash;
2208fcd94adSEric Biggers kunmap_local(haddr);
2218a1d0f9cSEric Biggers put_page(hpage);
2228a1d0f9cSEric Biggers }
2238a1d0f9cSEric Biggers
224f45555bfSEric Biggers /* Finally, verify the data block. */
22513e2408dSEric Biggers if (fsverity_hash_block(params, inode, data, real_hash) != 0)
22613e2408dSEric Biggers goto error;
22713e2408dSEric Biggers if (memcmp(want_hash, real_hash, hsize) != 0)
22813e2408dSEric Biggers goto corrupted;
22913e2408dSEric Biggers return true;
23013e2408dSEric Biggers
23113e2408dSEric Biggers corrupted:
23213e2408dSEric Biggers fsverity_err(inode,
23313e2408dSEric Biggers "FILE CORRUPTED! pos=%llu, level=%d, want_hash=%s:%*phN, real_hash=%s:%*phN",
23413e2408dSEric Biggers data_pos, level - 1,
23513e2408dSEric Biggers params->hash_alg->name, hsize, want_hash,
23613e2408dSEric Biggers params->hash_alg->name, hsize, real_hash);
23713e2408dSEric Biggers error:
2388fcd94adSEric Biggers for (; level > 0; level--) {
2398fcd94adSEric Biggers kunmap_local(hblocks[level - 1].addr);
2405306892aSEric Biggers put_page(hblocks[level - 1].page);
2418fcd94adSEric Biggers }
24213e2408dSEric Biggers return false;
2438a1d0f9cSEric Biggers }
2448a1d0f9cSEric Biggers
2455306892aSEric Biggers static bool
verify_data_blocks(struct folio * data_folio,size_t len,size_t offset,unsigned long max_ra_pages)246d1f0c5eaSEric Biggers verify_data_blocks(struct folio *data_folio, size_t len, size_t offset,
247d1f0c5eaSEric Biggers unsigned long max_ra_pages)
2485306892aSEric Biggers {
249d1f0c5eaSEric Biggers struct inode *inode = data_folio->mapping->host;
2508fcd94adSEric Biggers struct fsverity_info *vi = inode->i_verity_info;
2515306892aSEric Biggers const unsigned int block_size = vi->tree_params.block_size;
2525d0f0e57SEric Biggers u64 pos = (u64)data_folio->index << PAGE_SHIFT;
2535306892aSEric Biggers
2545306892aSEric Biggers if (WARN_ON_ONCE(len <= 0 || !IS_ALIGNED(len | offset, block_size)))
2555306892aSEric Biggers return false;
2565d0f0e57SEric Biggers if (WARN_ON_ONCE(!folio_test_locked(data_folio) ||
2575d0f0e57SEric Biggers folio_test_uptodate(data_folio)))
2585306892aSEric Biggers return false;
2595306892aSEric Biggers do {
2608fcd94adSEric Biggers void *data;
2618fcd94adSEric Biggers bool valid;
2625d0f0e57SEric Biggers
2638fcd94adSEric Biggers data = kmap_local_folio(data_folio, offset);
2648fcd94adSEric Biggers valid = verify_data_block(inode, vi, data, pos + offset,
2658fcd94adSEric Biggers max_ra_pages);
2668fcd94adSEric Biggers kunmap_local(data);
2678fcd94adSEric Biggers if (!valid)
2685306892aSEric Biggers return false;
2695306892aSEric Biggers offset += block_size;
2705306892aSEric Biggers len -= block_size;
2715306892aSEric Biggers } while (len);
2725306892aSEric Biggers return true;
2735306892aSEric Biggers }
2745306892aSEric Biggers
2758a1d0f9cSEric Biggers /**
2765d0f0e57SEric Biggers * fsverity_verify_blocks() - verify data in a folio
2775d0f0e57SEric Biggers * @folio: the folio containing the data to verify
2785d0f0e57SEric Biggers * @len: the length of the data to verify in the folio
2795d0f0e57SEric Biggers * @offset: the offset of the data to verify in the folio
2808a1d0f9cSEric Biggers *
2815306892aSEric Biggers * Verify data that has just been read from a verity file. The data must be
2825d0f0e57SEric Biggers * located in a pagecache folio that is still locked and not yet uptodate. The
2835306892aSEric Biggers * length and offset of the data must be Merkle tree block size aligned.
2848a1d0f9cSEric Biggers *
2855306892aSEric Biggers * Return: %true if the data is valid, else %false.
2868a1d0f9cSEric Biggers */
fsverity_verify_blocks(struct folio * folio,size_t len,size_t offset)2875d0f0e57SEric Biggers bool fsverity_verify_blocks(struct folio *folio, size_t len, size_t offset)
2888a1d0f9cSEric Biggers {
289d1f0c5eaSEric Biggers return verify_data_blocks(folio, len, offset, 0);
2908a1d0f9cSEric Biggers }
2915306892aSEric Biggers EXPORT_SYMBOL_GPL(fsverity_verify_blocks);
2928a1d0f9cSEric Biggers
2938a1d0f9cSEric Biggers #ifdef CONFIG_BLOCK
2948a1d0f9cSEric Biggers /**
2958a1d0f9cSEric Biggers * fsverity_verify_bio() - verify a 'read' bio that has just completed
2966377a38bSEric Biggers * @bio: the bio to verify
2978a1d0f9cSEric Biggers *
2985306892aSEric Biggers * Verify the bio's data against the file's Merkle tree. All bio data segments
2995306892aSEric Biggers * must be aligned to the file's Merkle tree block size. If any data fails
3005306892aSEric Biggers * verification, then bio->bi_status is set to an error status.
3018a1d0f9cSEric Biggers *
302704528d8SMatthew Wilcox (Oracle) * This is a helper function for use by the ->readahead() method of filesystems
3038a1d0f9cSEric Biggers * that issue bios to read data directly into the page cache. Filesystems that
3048a1d0f9cSEric Biggers * populate the page cache without issuing bios (e.g. non block-based
3058a1d0f9cSEric Biggers * filesystems) must instead call fsverity_verify_page() directly on each page.
3068a1d0f9cSEric Biggers * All filesystems must also call fsverity_verify_page() on holes.
3078a1d0f9cSEric Biggers */
fsverity_verify_bio(struct bio * bio)3088a1d0f9cSEric Biggers void fsverity_verify_bio(struct bio *bio)
3098a1d0f9cSEric Biggers {
3105d0f0e57SEric Biggers struct folio_iter fi;
311fd39073dSEric Biggers unsigned long max_ra_pages = 0;
3128a1d0f9cSEric Biggers
313fd39073dSEric Biggers if (bio->bi_opf & REQ_RAHEAD) {
314fd39073dSEric Biggers /*
315fd39073dSEric Biggers * If this bio is for data readahead, then we also do readahead
316fd39073dSEric Biggers * of the first (largest) level of the Merkle tree. Namely,
317fd39073dSEric Biggers * when a Merkle tree page is read, we also try to piggy-back on
318fd39073dSEric Biggers * some additional pages -- up to 1/4 the number of data pages.
319fd39073dSEric Biggers *
320fd39073dSEric Biggers * This improves sequential read performance, as it greatly
321fd39073dSEric Biggers * reduces the number of I/O requests made to the Merkle tree.
322fd39073dSEric Biggers */
3239098f36bSEric Biggers max_ra_pages = bio->bi_iter.bi_size >> (PAGE_SHIFT + 2);
324fd39073dSEric Biggers }
325fd39073dSEric Biggers
3265d0f0e57SEric Biggers bio_for_each_folio_all(fi, bio) {
327d1f0c5eaSEric Biggers if (!verify_data_blocks(fi.folio, fi.length, fi.offset,
3288fcd94adSEric Biggers max_ra_pages)) {
32998dc08baSEric Biggers bio->bi_status = BLK_STS_IOERR;
33098dc08baSEric Biggers break;
33198dc08baSEric Biggers }
3328a1d0f9cSEric Biggers }
3338a1d0f9cSEric Biggers }
3348a1d0f9cSEric Biggers EXPORT_SYMBOL_GPL(fsverity_verify_bio);
3358a1d0f9cSEric Biggers #endif /* CONFIG_BLOCK */
3368a1d0f9cSEric Biggers
3378a1d0f9cSEric Biggers /**
3388a1d0f9cSEric Biggers * fsverity_enqueue_verify_work() - enqueue work on the fs-verity workqueue
3396377a38bSEric Biggers * @work: the work to enqueue
3408a1d0f9cSEric Biggers *
3418a1d0f9cSEric Biggers * Enqueue verification work for asynchronous processing.
3428a1d0f9cSEric Biggers */
fsverity_enqueue_verify_work(struct work_struct * work)3438a1d0f9cSEric Biggers void fsverity_enqueue_verify_work(struct work_struct *work)
3448a1d0f9cSEric Biggers {
3458a1d0f9cSEric Biggers queue_work(fsverity_read_workqueue, work);
3468a1d0f9cSEric Biggers }
3478a1d0f9cSEric Biggers EXPORT_SYMBOL_GPL(fsverity_enqueue_verify_work);
3488a1d0f9cSEric Biggers
fsverity_init_workqueue(void)349*e77000ccSEric Biggers void __init fsverity_init_workqueue(void)
3508a1d0f9cSEric Biggers {
3518a1d0f9cSEric Biggers /*
352f959325eSNathan Huckleberry * Use a high-priority workqueue to prioritize verification work, which
353f959325eSNathan Huckleberry * blocks reads from completing, over regular application tasks.
3548a1d0f9cSEric Biggers *
355f959325eSNathan Huckleberry * For performance reasons, don't use an unbound workqueue. Using an
356f959325eSNathan Huckleberry * unbound workqueue for crypto operations causes excessive scheduler
357f959325eSNathan Huckleberry * latency on ARM64.
3588a1d0f9cSEric Biggers */
3598a1d0f9cSEric Biggers fsverity_read_workqueue = alloc_workqueue("fsverity_read_queue",
360f959325eSNathan Huckleberry WQ_HIGHPRI,
3618a1d0f9cSEric Biggers num_online_cpus());
3628a1d0f9cSEric Biggers if (!fsverity_read_workqueue)
363*e77000ccSEric Biggers panic("failed to allocate fsverity_read_queue");
364432434c9SEric Biggers }
365