^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Data verification functions, i.e. hooks for ->readpages()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2019 Google LLC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "fsverity_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static struct workqueue_struct *fsverity_read_workqueue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * hash_at_level() - compute the location of the block's hash at the given level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * @params: (in) the Merkle tree parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * @dindex: (in) the index of the data block being verified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * @level: (in) the level of hash we want (0 is leaf level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @hindex: (out) the index of the hash block containing the wanted hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * @hoffset: (out) the byte offset to the wanted hash within the hash block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static void hash_at_level(const struct merkle_tree_params *params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) pgoff_t dindex, unsigned int level, pgoff_t *hindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned int *hoffset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) pgoff_t position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /* Offset of the hash within the level's region, in hashes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) position = dindex >> (level * params->log_arity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* Index of the hash block in the tree overall */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) *hindex = params->level_start[level] + (position >> params->log_arity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* Offset of the wanted hash (in bytes) within the hash block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) *hoffset = (position & ((1 << params->log_arity) - 1)) <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) (params->log_blocksize - params->log_arity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* Extract a hash from a hash page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static void extract_hash(struct page *hpage, unsigned int hoffset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned int hsize, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) void *virt = kmap_atomic(hpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) memcpy(out, virt + hoffset, hsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) kunmap_atomic(virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static inline int cmp_hashes(const struct fsverity_info *vi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) const u8 *want_hash, const u8 *real_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) pgoff_t index, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) const unsigned int hsize = vi->tree_params.digest_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (memcmp(want_hash, real_hash, hsize) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) fsverity_err(vi->inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) "FILE CORRUPTED! index=%lu, level=%d, want_hash=%s:%*phN, real_hash=%s:%*phN",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) index, level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) vi->tree_params.hash_alg->name, hsize, want_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) vi->tree_params.hash_alg->name, hsize, real_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * Verify a single data page against the file's Merkle tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * In principle, we need to verify the entire path to the root node. However,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * for efficiency the filesystem may cache the hash pages. Therefore we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * only ascend the tree until an already-verified page is seen, as indicated by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * the PageChecked bit being set; then verify the path to that page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * This code currently only supports the case where the verity block size is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * equal to PAGE_SIZE. Doing otherwise would be possible but tricky, since we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * wouldn't be able to use the PageChecked bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * Note that multiple processes may race to verify a hash page and mark it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * Checked, but it doesn't matter; the result will be the same either way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * Return: true if the page is valid, else false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static bool verify_page(struct inode *inode, const struct fsverity_info *vi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct ahash_request *req, struct page *data_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) unsigned long level0_ra_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) const struct merkle_tree_params *params = &vi->tree_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) const unsigned int hsize = params->digest_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) const pgoff_t index = data_page->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) u8 _want_hash[FS_VERITY_MAX_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) const u8 *want_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) u8 real_hash[FS_VERITY_MAX_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct page *hpages[FS_VERITY_MAX_LEVELS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) unsigned int hoffsets[FS_VERITY_MAX_LEVELS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (WARN_ON_ONCE(!PageLocked(data_page) || PageUptodate(data_page)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) pr_debug_ratelimited("Verifying data page %lu...\n", index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * Starting at the leaf level, ascend the tree saving hash pages along
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * the way until we find a verified hash page, indicated by PageChecked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * or until we reach the root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) for (level = 0; level < params->num_levels; level++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) pgoff_t hindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) unsigned int hoffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct page *hpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) hash_at_level(params, index, level, &hindex, &hoffset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) pr_debug_ratelimited("Level %d: hindex=%lu, hoffset=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) level, hindex, hoffset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) hpage = inode->i_sb->s_vop->read_merkle_tree_page(inode, hindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) level == 0 ? level0_ra_pages : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (IS_ERR(hpage)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) err = PTR_ERR(hpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) fsverity_err(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) "Error %d reading Merkle tree page %lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) err, hindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (PageChecked(hpage)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) extract_hash(hpage, hoffset, hsize, _want_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) want_hash = _want_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) put_page(hpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) pr_debug_ratelimited("Hash page already checked, want %s:%*phN\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) params->hash_alg->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) hsize, want_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) goto descend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) pr_debug_ratelimited("Hash page not yet checked\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) hpages[level] = hpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) hoffsets[level] = hoffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) want_hash = vi->root_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) pr_debug("Want root hash: %s:%*phN\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) params->hash_alg->name, hsize, want_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) descend:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* Descend the tree verifying hash pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) for (; level > 0; level--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct page *hpage = hpages[level - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) unsigned int hoffset = hoffsets[level - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) err = fsverity_hash_page(params, inode, req, hpage, real_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) err = cmp_hashes(vi, want_hash, real_hash, index, level - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) SetPageChecked(hpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) extract_hash(hpage, hoffset, hsize, _want_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) want_hash = _want_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) put_page(hpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) pr_debug("Verified hash page at level %d, now want %s:%*phN\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) level - 1, params->hash_alg->name, hsize, want_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /* Finally, verify the data page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) err = fsverity_hash_page(params, inode, req, data_page, real_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) err = cmp_hashes(vi, want_hash, real_hash, index, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) for (; level > 0; level--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) put_page(hpages[level - 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return err == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * fsverity_verify_page() - verify a data page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * @page: the page to verity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * Verify a page that has just been read from a verity file. The page must be a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * pagecache page that is still locked and not yet uptodate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * Return: true if the page is valid, else false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) bool fsverity_verify_page(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) const struct fsverity_info *vi = inode->i_verity_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct ahash_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) bool valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /* This allocation never fails, since it's mempool-backed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) req = fsverity_alloc_hash_request(vi->tree_params.hash_alg, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) valid = verify_page(inode, vi, req, page, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) fsverity_free_hash_request(vi->tree_params.hash_alg, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) EXPORT_SYMBOL_GPL(fsverity_verify_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) #ifdef CONFIG_BLOCK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * fsverity_verify_bio() - verify a 'read' bio that has just completed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * @bio: the bio to verify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * Verify a set of pages that have just been read from a verity file. The pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * must be pagecache pages that are still locked and not yet uptodate. Pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * that fail verification are set to the Error state. Verification is skipped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * for pages already in the Error state, e.g. due to fscrypt decryption failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * This is a helper function for use by the ->readpages() method of filesystems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * that issue bios to read data directly into the page cache. Filesystems that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * populate the page cache without issuing bios (e.g. non block-based
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * filesystems) must instead call fsverity_verify_page() directly on each page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * All filesystems must also call fsverity_verify_page() on holes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) void fsverity_verify_bio(struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct inode *inode = bio_first_page_all(bio)->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) const struct fsverity_info *vi = inode->i_verity_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) const struct merkle_tree_params *params = &vi->tree_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) struct ahash_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct bio_vec *bv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) struct bvec_iter_all iter_all;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) unsigned long max_ra_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /* This allocation never fails, since it's mempool-backed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) req = fsverity_alloc_hash_request(params->hash_alg, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (bio->bi_opf & REQ_RAHEAD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * If this bio is for data readahead, then we also do readahead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * of the first (largest) level of the Merkle tree. Namely,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * when a Merkle tree page is read, we also try to piggy-back on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * some additional pages -- up to 1/4 the number of data pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * This improves sequential read performance, as it greatly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * reduces the number of I/O requests made to the Merkle tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) bio_for_each_segment_all(bv, bio, iter_all)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) max_ra_pages++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) max_ra_pages /= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) bio_for_each_segment_all(bv, bio, iter_all) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct page *page = bv->bv_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) unsigned long level0_index = page->index >> params->log_arity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) unsigned long level0_ra_pages =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) min(max_ra_pages, params->level0_blocks - level0_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (!PageError(page) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) !verify_page(inode, vi, req, page, level0_ra_pages))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) fsverity_free_hash_request(params->hash_alg, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) EXPORT_SYMBOL_GPL(fsverity_verify_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) #endif /* CONFIG_BLOCK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * fsverity_enqueue_verify_work() - enqueue work on the fs-verity workqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * @work: the work to enqueue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * Enqueue verification work for asynchronous processing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) void fsverity_enqueue_verify_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) queue_work(fsverity_read_workqueue, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) EXPORT_SYMBOL_GPL(fsverity_enqueue_verify_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) int __init fsverity_init_workqueue(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * Use an unbound workqueue to allow bios to be verified in parallel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * even when they happen to complete on the same CPU. This sacrifices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * locality, but it's worthwhile since hashing is CPU-intensive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * Also use a high-priority workqueue to prioritize verification work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * which blocks reads from completing, over regular application tasks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) fsverity_read_workqueue = alloc_workqueue("fsverity_read_queue",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) WQ_UNBOUND | WQ_HIGHPRI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) num_online_cpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (!fsverity_read_workqueue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) void __init fsverity_exit_workqueue(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) destroy_workqueue(fsverity_read_workqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) fsverity_read_workqueue = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }