This source file includes following definitions.
- hash_at_level
- extract_hash
- cmp_hashes
- verify_page
- fsverity_verify_page
- fsverity_verify_bio
- fsverity_enqueue_verify_work
- fsverity_init_workqueue
- fsverity_exit_workqueue
1
2
3
4
5
6
7
8 #include "fsverity_private.h"
9
10 #include <crypto/hash.h>
11 #include <linux/bio.h>
12 #include <linux/ratelimit.h>
13
14 static struct workqueue_struct *fsverity_read_workqueue;
15
16
17
18
19
20
21
22
23
24
25 static void hash_at_level(const struct merkle_tree_params *params,
26 pgoff_t dindex, unsigned int level, pgoff_t *hindex,
27 unsigned int *hoffset)
28 {
29 pgoff_t position;
30
31
32 position = dindex >> (level * params->log_arity);
33
34
35 *hindex = params->level_start[level] + (position >> params->log_arity);
36
37
38 *hoffset = (position & ((1 << params->log_arity) - 1)) <<
39 (params->log_blocksize - params->log_arity);
40 }
41
42
43 static void extract_hash(struct page *hpage, unsigned int hoffset,
44 unsigned int hsize, u8 *out)
45 {
46 void *virt = kmap_atomic(hpage);
47
48 memcpy(out, virt + hoffset, hsize);
49 kunmap_atomic(virt);
50 }
51
52 static inline int cmp_hashes(const struct fsverity_info *vi,
53 const u8 *want_hash, const u8 *real_hash,
54 pgoff_t index, int level)
55 {
56 const unsigned int hsize = vi->tree_params.digest_size;
57
58 if (memcmp(want_hash, real_hash, hsize) == 0)
59 return 0;
60
61 fsverity_err(vi->inode,
62 "FILE CORRUPTED! index=%lu, level=%d, want_hash=%s:%*phN, real_hash=%s:%*phN",
63 index, level,
64 vi->tree_params.hash_alg->name, hsize, want_hash,
65 vi->tree_params.hash_alg->name, hsize, real_hash);
66 return -EBADMSG;
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 static bool verify_page(struct inode *inode, const struct fsverity_info *vi,
87 struct ahash_request *req, struct page *data_page)
88 {
89 const struct merkle_tree_params *params = &vi->tree_params;
90 const unsigned int hsize = params->digest_size;
91 const pgoff_t index = data_page->index;
92 int level;
93 u8 _want_hash[FS_VERITY_MAX_DIGEST_SIZE];
94 const u8 *want_hash;
95 u8 real_hash[FS_VERITY_MAX_DIGEST_SIZE];
96 struct page *hpages[FS_VERITY_MAX_LEVELS];
97 unsigned int hoffsets[FS_VERITY_MAX_LEVELS];
98 int err;
99
100 if (WARN_ON_ONCE(!PageLocked(data_page) || PageUptodate(data_page)))
101 return false;
102
103 pr_debug_ratelimited("Verifying data page %lu...\n", index);
104
105
106
107
108
109
110 for (level = 0; level < params->num_levels; level++) {
111 pgoff_t hindex;
112 unsigned int hoffset;
113 struct page *hpage;
114
115 hash_at_level(params, index, level, &hindex, &hoffset);
116
117 pr_debug_ratelimited("Level %d: hindex=%lu, hoffset=%u\n",
118 level, hindex, hoffset);
119
120 hpage = inode->i_sb->s_vop->read_merkle_tree_page(inode,
121 hindex);
122 if (IS_ERR(hpage)) {
123 err = PTR_ERR(hpage);
124 fsverity_err(inode,
125 "Error %d reading Merkle tree page %lu",
126 err, hindex);
127 goto out;
128 }
129
130 if (PageChecked(hpage)) {
131 extract_hash(hpage, hoffset, hsize, _want_hash);
132 want_hash = _want_hash;
133 put_page(hpage);
134 pr_debug_ratelimited("Hash page already checked, want %s:%*phN\n",
135 params->hash_alg->name,
136 hsize, want_hash);
137 goto descend;
138 }
139 pr_debug_ratelimited("Hash page not yet checked\n");
140 hpages[level] = hpage;
141 hoffsets[level] = hoffset;
142 }
143
144 want_hash = vi->root_hash;
145 pr_debug("Want root hash: %s:%*phN\n",
146 params->hash_alg->name, hsize, want_hash);
147 descend:
148
149 for (; level > 0; level--) {
150 struct page *hpage = hpages[level - 1];
151 unsigned int hoffset = hoffsets[level - 1];
152
153 err = fsverity_hash_page(params, inode, req, hpage, real_hash);
154 if (err)
155 goto out;
156 err = cmp_hashes(vi, want_hash, real_hash, index, level - 1);
157 if (err)
158 goto out;
159 SetPageChecked(hpage);
160 extract_hash(hpage, hoffset, hsize, _want_hash);
161 want_hash = _want_hash;
162 put_page(hpage);
163 pr_debug("Verified hash page at level %d, now want %s:%*phN\n",
164 level - 1, params->hash_alg->name, hsize, want_hash);
165 }
166
167
168 err = fsverity_hash_page(params, inode, req, data_page, real_hash);
169 if (err)
170 goto out;
171 err = cmp_hashes(vi, want_hash, real_hash, index, -1);
172 out:
173 for (; level > 0; level--)
174 put_page(hpages[level - 1]);
175
176 return err == 0;
177 }
178
179
180
181
182
183
184
185
186
187 bool fsverity_verify_page(struct page *page)
188 {
189 struct inode *inode = page->mapping->host;
190 const struct fsverity_info *vi = inode->i_verity_info;
191 struct ahash_request *req;
192 bool valid;
193
194 req = ahash_request_alloc(vi->tree_params.hash_alg->tfm, GFP_NOFS);
195 if (unlikely(!req))
196 return false;
197
198 valid = verify_page(inode, vi, req, page);
199
200 ahash_request_free(req);
201
202 return valid;
203 }
204 EXPORT_SYMBOL_GPL(fsverity_verify_page);
205
206 #ifdef CONFIG_BLOCK
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 void fsverity_verify_bio(struct bio *bio)
222 {
223 struct inode *inode = bio_first_page_all(bio)->mapping->host;
224 const struct fsverity_info *vi = inode->i_verity_info;
225 struct ahash_request *req;
226 struct bio_vec *bv;
227 struct bvec_iter_all iter_all;
228
229 req = ahash_request_alloc(vi->tree_params.hash_alg->tfm, GFP_NOFS);
230 if (unlikely(!req)) {
231 bio_for_each_segment_all(bv, bio, iter_all)
232 SetPageError(bv->bv_page);
233 return;
234 }
235
236 bio_for_each_segment_all(bv, bio, iter_all) {
237 struct page *page = bv->bv_page;
238
239 if (!PageError(page) && !verify_page(inode, vi, req, page))
240 SetPageError(page);
241 }
242
243 ahash_request_free(req);
244 }
245 EXPORT_SYMBOL_GPL(fsverity_verify_bio);
246 #endif
247
248
249
250
251
252
253 void fsverity_enqueue_verify_work(struct work_struct *work)
254 {
255 queue_work(fsverity_read_workqueue, work);
256 }
257 EXPORT_SYMBOL_GPL(fsverity_enqueue_verify_work);
258
259 int __init fsverity_init_workqueue(void)
260 {
261
262
263
264
265
266
267
268
269 fsverity_read_workqueue = alloc_workqueue("fsverity_read_queue",
270 WQ_UNBOUND | WQ_HIGHPRI,
271 num_online_cpus());
272 if (!fsverity_read_workqueue)
273 return -ENOMEM;
274 return 0;
275 }
276
277 void __init fsverity_exit_workqueue(void)
278 {
279 destroy_workqueue(fsverity_read_workqueue);
280 fsverity_read_workqueue = NULL;
281 }