blob: 36d00b31030045f06684cd3d5088a45719abcda8 [file] [log] [blame]
Qu Wenruo90eb4c02017-10-09 01:51:02 +00001/*
2 * Copyright (C) Qu Wenruo 2017. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program.
15 */
16
17/*
18 * The module is used to catch unexpected/corrupted tree block data.
19 * Such behavior can be caused either by a fuzzed image or bugs.
20 *
21 * The objective is to do leaf/node validation checks when tree block is read
22 * from disk, and check *every* possible member, so other code won't
23 * need to checking them again.
24 *
25 * Due to the potential and unwanted damage, every checker needs to be
26 * carefully reviewed otherwise so it does not prevent mount of valid images.
27 */
28
29#include "ctree.h"
30#include "tree-checker.h"
31#include "disk-io.h"
32#include "compression.h"
33
34#define CORRUPT(reason, eb, root, slot) \
35 btrfs_crit(root->fs_info, \
36 "corrupt %s, %s: block=%llu, root=%llu, slot=%d", \
37 btrfs_header_level(eb) == 0 ? "leaf" : "node", \
38 reason, btrfs_header_bytenr(eb), root->objectid, slot)
39
Qu Wenruof4209d92017-10-09 01:51:03 +000040/*
41 * Error message should follow the following format:
42 * corrupt <type>: <identifier>, <reason>[, <bad_value>]
43 *
44 * @type: leaf or node
45 * @identifier: the necessary info to locate the leaf/node.
46 * It's recommened to decode key.objecitd/offset if it's
47 * meaningful.
48 * @reason: describe the error
49 * @bad_value: optional, it's recommened to output bad value and its
50 * expected value (range).
51 *
52 * Since comma is used to separate the components, only space is allowed
53 * inside each component.
54 */
55
56/*
57 * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
58 * Allows callers to customize the output.
59 */
60__printf(4, 5)
61static void generic_err(const struct btrfs_root *root,
62 const struct extent_buffer *eb, int slot,
63 const char *fmt, ...)
64{
65 struct va_format vaf;
66 va_list args;
67
68 va_start(args, fmt);
69
70 vaf.fmt = fmt;
71 vaf.va = &args;
72
73 btrfs_crit(root->fs_info,
74 "corrupt %s: root=%llu block=%llu slot=%d, %pV",
75 btrfs_header_level(eb) == 0 ? "leaf" : "node",
76 root->objectid, btrfs_header_bytenr(eb), slot, &vaf);
77 va_end(args);
78}
79
Qu Wenruo90eb4c02017-10-09 01:51:02 +000080static int check_extent_data_item(struct btrfs_root *root,
81 struct extent_buffer *leaf,
82 struct btrfs_key *key, int slot)
83{
84 struct btrfs_file_extent_item *fi;
85 u32 sectorsize = root->sectorsize;
86 u32 item_size = btrfs_item_size_nr(leaf, slot);
87
88 if (!IS_ALIGNED(key->offset, sectorsize)) {
89 CORRUPT("unaligned key offset for file extent",
90 leaf, root, slot);
91 return -EUCLEAN;
92 }
93
94 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
95
96 if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) {
97 CORRUPT("invalid file extent type", leaf, root, slot);
98 return -EUCLEAN;
99 }
100
101 /*
102 * Support for new compression/encrption must introduce incompat flag,
103 * and must be caught in open_ctree().
104 */
105 if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) {
106 CORRUPT("invalid file extent compression", leaf, root, slot);
107 return -EUCLEAN;
108 }
109 if (btrfs_file_extent_encryption(leaf, fi)) {
110 CORRUPT("invalid file extent encryption", leaf, root, slot);
111 return -EUCLEAN;
112 }
113 if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
114 /* Inline extent must have 0 as key offset */
115 if (key->offset) {
116 CORRUPT("inline extent has non-zero key offset",
117 leaf, root, slot);
118 return -EUCLEAN;
119 }
120
121 /* Compressed inline extent has no on-disk size, skip it */
122 if (btrfs_file_extent_compression(leaf, fi) !=
123 BTRFS_COMPRESS_NONE)
124 return 0;
125
126 /* Uncompressed inline extent size must match item size */
127 if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
128 btrfs_file_extent_ram_bytes(leaf, fi)) {
129 CORRUPT("plaintext inline extent has invalid size",
130 leaf, root, slot);
131 return -EUCLEAN;
132 }
133 return 0;
134 }
135
136 /* Regular or preallocated extent has fixed item size */
137 if (item_size != sizeof(*fi)) {
138 CORRUPT(
139 "regluar or preallocated extent data item size is invalid",
140 leaf, root, slot);
141 return -EUCLEAN;
142 }
143 if (!IS_ALIGNED(btrfs_file_extent_ram_bytes(leaf, fi), sectorsize) ||
144 !IS_ALIGNED(btrfs_file_extent_disk_bytenr(leaf, fi), sectorsize) ||
145 !IS_ALIGNED(btrfs_file_extent_disk_num_bytes(leaf, fi), sectorsize) ||
146 !IS_ALIGNED(btrfs_file_extent_offset(leaf, fi), sectorsize) ||
147 !IS_ALIGNED(btrfs_file_extent_num_bytes(leaf, fi), sectorsize)) {
148 CORRUPT(
149 "regular or preallocated extent data item has unaligned value",
150 leaf, root, slot);
151 return -EUCLEAN;
152 }
153
154 return 0;
155}
156
157static int check_csum_item(struct btrfs_root *root, struct extent_buffer *leaf,
158 struct btrfs_key *key, int slot)
159{
160 u32 sectorsize = root->sectorsize;
161 u32 csumsize = btrfs_super_csum_size(root->fs_info->super_copy);
162
163 if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) {
164 CORRUPT("invalid objectid for csum item", leaf, root, slot);
165 return -EUCLEAN;
166 }
167 if (!IS_ALIGNED(key->offset, sectorsize)) {
168 CORRUPT("unaligned key offset for csum item", leaf, root, slot);
169 return -EUCLEAN;
170 }
171 if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) {
172 CORRUPT("unaligned csum item size", leaf, root, slot);
173 return -EUCLEAN;
174 }
175 return 0;
176}
177
178/*
179 * Common point to switch the item-specific validation.
180 */
181static int check_leaf_item(struct btrfs_root *root,
182 struct extent_buffer *leaf,
183 struct btrfs_key *key, int slot)
184{
185 int ret = 0;
186
187 switch (key->type) {
188 case BTRFS_EXTENT_DATA_KEY:
189 ret = check_extent_data_item(root, leaf, key, slot);
190 break;
191 case BTRFS_EXTENT_CSUM_KEY:
192 ret = check_csum_item(root, leaf, key, slot);
193 break;
194 }
195 return ret;
196}
197
Qu Wenruof7438e62017-11-08 08:54:24 +0800198static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf,
199 bool check_item_data)
Qu Wenruo90eb4c02017-10-09 01:51:02 +0000200{
201 struct btrfs_fs_info *fs_info = root->fs_info;
202 /* No valid key type is 0, so all key should be larger than this key */
203 struct btrfs_key prev_key = {0, 0, 0};
204 struct btrfs_key key;
205 u32 nritems = btrfs_header_nritems(leaf);
206 int slot;
207
208 /*
209 * Extent buffers from a relocation tree have a owner field that
210 * corresponds to the subvolume tree they are based on. So just from an
211 * extent buffer alone we can not find out what is the id of the
212 * corresponding subvolume tree, so we can not figure out if the extent
213 * buffer corresponds to the root of the relocation tree or not. So
214 * skip this check for relocation trees.
215 */
216 if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
217 struct btrfs_root *check_root;
218
219 key.objectid = btrfs_header_owner(leaf);
220 key.type = BTRFS_ROOT_ITEM_KEY;
221 key.offset = (u64)-1;
222
223 check_root = btrfs_get_fs_root(fs_info, &key, false);
224 /*
225 * The only reason we also check NULL here is that during
226 * open_ctree() some roots has not yet been set up.
227 */
228 if (!IS_ERR_OR_NULL(check_root)) {
229 struct extent_buffer *eb;
230
231 eb = btrfs_root_node(check_root);
232 /* if leaf is the root, then it's fine */
233 if (leaf != eb) {
234 CORRUPT("non-root leaf's nritems is 0",
235 leaf, check_root, 0);
236 free_extent_buffer(eb);
237 return -EUCLEAN;
238 }
239 free_extent_buffer(eb);
240 }
241 return 0;
242 }
243
244 if (nritems == 0)
245 return 0;
246
247 /*
248 * Check the following things to make sure this is a good leaf, and
249 * leaf users won't need to bother with similar sanity checks:
250 *
251 * 1) key ordering
252 * 2) item offset and size
253 * No overlap, no hole, all inside the leaf.
254 * 3) item content
255 * If possible, do comprehensive sanity check.
256 * NOTE: All checks must only rely on the item data itself.
257 */
258 for (slot = 0; slot < nritems; slot++) {
259 u32 item_end_expected;
260 int ret;
261
262 btrfs_item_key_to_cpu(leaf, &key, slot);
263
264 /* Make sure the keys are in the right order */
265 if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) {
266 CORRUPT("bad key order", leaf, root, slot);
267 return -EUCLEAN;
268 }
269
270 /*
271 * Make sure the offset and ends are right, remember that the
272 * item data starts at the end of the leaf and grows towards the
273 * front.
274 */
275 if (slot == 0)
276 item_end_expected = BTRFS_LEAF_DATA_SIZE(root);
277 else
278 item_end_expected = btrfs_item_offset_nr(leaf,
279 slot - 1);
280 if (btrfs_item_end_nr(leaf, slot) != item_end_expected) {
281 CORRUPT("slot offset bad", leaf, root, slot);
282 return -EUCLEAN;
283 }
284
285 /*
286 * Check to make sure that we don't point outside of the leaf,
287 * just in case all the items are consistent to each other, but
288 * all point outside of the leaf.
289 */
290 if (btrfs_item_end_nr(leaf, slot) >
291 BTRFS_LEAF_DATA_SIZE(root)) {
292 CORRUPT("slot end outside of leaf", leaf, root, slot);
293 return -EUCLEAN;
294 }
295
296 /* Also check if the item pointer overlaps with btrfs item. */
297 if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) >
298 btrfs_item_ptr_offset(leaf, slot)) {
299 CORRUPT("slot overlap with its data", leaf, root, slot);
300 return -EUCLEAN;
301 }
302
Qu Wenruof7438e62017-11-08 08:54:24 +0800303 if (check_item_data) {
304 /*
305 * Check if the item size and content meet other
306 * criteria
307 */
308 ret = check_leaf_item(root, leaf, &key, slot);
309 if (ret < 0)
310 return ret;
311 }
Qu Wenruo90eb4c02017-10-09 01:51:02 +0000312
313 prev_key.objectid = key.objectid;
314 prev_key.type = key.type;
315 prev_key.offset = key.offset;
316 }
317
318 return 0;
319}
320
Qu Wenruof7438e62017-11-08 08:54:24 +0800321int btrfs_check_leaf_full(struct btrfs_root *root, struct extent_buffer *leaf)
322{
323 return check_leaf(root, leaf, true);
324}
325
326int btrfs_check_leaf_relaxed(struct btrfs_root *root,
327 struct extent_buffer *leaf)
328{
329 return check_leaf(root, leaf, false);
330}
331
Qu Wenruo90eb4c02017-10-09 01:51:02 +0000332int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node)
333{
334 unsigned long nr = btrfs_header_nritems(node);
335 struct btrfs_key key, next_key;
336 int slot;
337 u64 bytenr;
338 int ret = 0;
339
340 if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(root)) {
341 btrfs_crit(root->fs_info,
Qu Wenruof4209d92017-10-09 01:51:03 +0000342"corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
343 root->objectid, node->start,
344 nr == 0 ? "small" : "large", nr,
345 BTRFS_NODEPTRS_PER_BLOCK(root));
346 return -EUCLEAN;
Qu Wenruo90eb4c02017-10-09 01:51:02 +0000347 }
348
349 for (slot = 0; slot < nr - 1; slot++) {
350 bytenr = btrfs_node_blockptr(node, slot);
351 btrfs_node_key_to_cpu(node, &key, slot);
352 btrfs_node_key_to_cpu(node, &next_key, slot + 1);
353
354 if (!bytenr) {
Qu Wenruof4209d92017-10-09 01:51:03 +0000355 generic_err(root, node, slot,
356 "invalid NULL node pointer");
357 ret = -EUCLEAN;
358 goto out;
359 }
360 if (!IS_ALIGNED(bytenr, root->sectorsize)) {
361 generic_err(root, node, slot,
362 "unaligned pointer, have %llu should be aligned to %u",
363 bytenr, root->sectorsize);
364 ret = -EUCLEAN;
Qu Wenruo90eb4c02017-10-09 01:51:02 +0000365 goto out;
366 }
367
368 if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
Qu Wenruof4209d92017-10-09 01:51:03 +0000369 generic_err(root, node, slot,
370 "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
371 key.objectid, key.type, key.offset,
372 next_key.objectid, next_key.type,
373 next_key.offset);
374 ret = -EUCLEAN;
Qu Wenruo90eb4c02017-10-09 01:51:02 +0000375 goto out;
376 }
377 }
378out:
379 return ret;
380}