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
14static struct workqueue_struct *fsverity_read_workqueue;
15
16
17
18
19
20
21
22
23
24
25static 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
43static 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
52static 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
86static bool verify_page(struct inode *inode, const struct fsverity_info *vi,
87 struct ahash_request *req, struct page *data_page,
88 unsigned long level0_ra_pages)
89{
90 const struct merkle_tree_params *params = &vi->tree_params;
91 const unsigned int hsize = params->digest_size;
92 const pgoff_t index = data_page->index;
93 int level;
94 u8 _want_hash[FS_VERITY_MAX_DIGEST_SIZE];
95 const u8 *want_hash;
96 u8 real_hash[FS_VERITY_MAX_DIGEST_SIZE];
97 struct page *hpages[FS_VERITY_MAX_LEVELS];
98 unsigned int hoffsets[FS_VERITY_MAX_LEVELS];
99 int err;
100
101 if (WARN_ON_ONCE(!PageLocked(data_page) || PageUptodate(data_page)))
102 return false;
103
104 pr_debug_ratelimited("Verifying data page %lu...\n", index);
105
106
107
108
109
110
111 for (level = 0; level < params->num_levels; level++) {
112 pgoff_t hindex;
113 unsigned int hoffset;
114 struct page *hpage;
115
116 hash_at_level(params, index, level, &hindex, &hoffset);
117
118 pr_debug_ratelimited("Level %d: hindex=%lu, hoffset=%u\n",
119 level, hindex, hoffset);
120
121 hpage = inode->i_sb->s_vop->read_merkle_tree_page(inode, hindex,
122 level == 0 ? level0_ra_pages : 0);
123 if (IS_ERR(hpage)) {
124 err = PTR_ERR(hpage);
125 fsverity_err(inode,
126 "Error %d reading Merkle tree page %lu",
127 err, hindex);
128 goto out;
129 }
130
131 if (PageChecked(hpage)) {
132 extract_hash(hpage, hoffset, hsize, _want_hash);
133 want_hash = _want_hash;
134 put_page(hpage);
135 pr_debug_ratelimited("Hash page already checked, want %s:%*phN\n",
136 params->hash_alg->name,
137 hsize, want_hash);
138 goto descend;
139 }
140 pr_debug_ratelimited("Hash page not yet checked\n");
141 hpages[level] = hpage;
142 hoffsets[level] = hoffset;
143 }
144
145 want_hash = vi->root_hash;
146 pr_debug("Want root hash: %s:%*phN\n",
147 params->hash_alg->name, hsize, want_hash);
148descend:
149
150 for (; level > 0; level--) {
151 struct page *hpage = hpages[level - 1];
152 unsigned int hoffset = hoffsets[level - 1];
153
154 err = fsverity_hash_page(params, inode, req, hpage, real_hash);
155 if (err)
156 goto out;
157 err = cmp_hashes(vi, want_hash, real_hash, index, level - 1);
158 if (err)
159 goto out;
160 SetPageChecked(hpage);
161 extract_hash(hpage, hoffset, hsize, _want_hash);
162 want_hash = _want_hash;
163 put_page(hpage);
164 pr_debug("Verified hash page at level %d, now want %s:%*phN\n",
165 level - 1, params->hash_alg->name, hsize, want_hash);
166 }
167
168
169 err = fsverity_hash_page(params, inode, req, data_page, real_hash);
170 if (err)
171 goto out;
172 err = cmp_hashes(vi, want_hash, real_hash, index, -1);
173out:
174 for (; level > 0; level--)
175 put_page(hpages[level - 1]);
176
177 return err == 0;
178}
179
180
181
182
183
184
185
186
187
188
189bool fsverity_verify_page(struct page *page)
190{
191 struct inode *inode = page->mapping->host;
192 const struct fsverity_info *vi = inode->i_verity_info;
193 struct ahash_request *req;
194 bool valid;
195
196
197 req = fsverity_alloc_hash_request(vi->tree_params.hash_alg, GFP_NOFS);
198
199 valid = verify_page(inode, vi, req, page, 0);
200
201 fsverity_free_hash_request(vi->tree_params.hash_alg, req);
202
203 return valid;
204}
205EXPORT_SYMBOL_GPL(fsverity_verify_page);
206
207#ifdef CONFIG_BLOCK
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223void fsverity_verify_bio(struct bio *bio)
224{
225 struct inode *inode = bio_first_page_all(bio)->mapping->host;
226 const struct fsverity_info *vi = inode->i_verity_info;
227 const struct merkle_tree_params *params = &vi->tree_params;
228 struct ahash_request *req;
229 struct bio_vec *bv;
230 struct bvec_iter_all iter_all;
231 unsigned long max_ra_pages = 0;
232
233
234 req = fsverity_alloc_hash_request(params->hash_alg, GFP_NOFS);
235
236 if (bio->bi_opf & REQ_RAHEAD) {
237
238
239
240
241
242
243
244
245
246 bio_for_each_segment_all(bv, bio, iter_all)
247 max_ra_pages++;
248 max_ra_pages /= 4;
249 }
250
251 bio_for_each_segment_all(bv, bio, iter_all) {
252 struct page *page = bv->bv_page;
253 unsigned long level0_index = page->index >> params->log_arity;
254 unsigned long level0_ra_pages =
255 min(max_ra_pages, params->level0_blocks - level0_index);
256
257 if (!PageError(page) &&
258 !verify_page(inode, vi, req, page, level0_ra_pages))
259 SetPageError(page);
260 }
261
262 fsverity_free_hash_request(params->hash_alg, req);
263}
264EXPORT_SYMBOL_GPL(fsverity_verify_bio);
265#endif
266
267
268
269
270
271
272
273void fsverity_enqueue_verify_work(struct work_struct *work)
274{
275 queue_work(fsverity_read_workqueue, work);
276}
277EXPORT_SYMBOL_GPL(fsverity_enqueue_verify_work);
278
279int __init fsverity_init_workqueue(void)
280{
281
282
283
284
285
286
287
288
289 fsverity_read_workqueue = alloc_workqueue("fsverity_read_queue",
290 WQ_UNBOUND | WQ_HIGHPRI,
291 num_online_cpus());
292 if (!fsverity_read_workqueue)
293 return -ENOMEM;
294 return 0;
295}
296
297void __init fsverity_exit_workqueue(void)
298{
299 destroy_workqueue(fsverity_read_workqueue);
300 fsverity_read_workqueue = NULL;
301}
302