1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) Qu Wenruo 2017. All rights reserved.
7 * The module is used to catch unexpected/corrupted tree block data.
8 * Such behavior can be caused either by a fuzzed image or bugs.
10 * The objective is to do leaf/node validation checks when tree block is read
11 * from disk, and check *every* possible member, so other code won't
12 * need to checking them again.
14 * Due to the potential and unwanted damage, every checker needs to be
15 * carefully reviewed otherwise so it does not prevent mount of valid images.
19 #include "tree-checker.h"
21 #include "compression.h"
24 * Error message should follow the following format:
25 * corrupt <type>: <identifier>, <reason>[, <bad_value>]
28 * @identifier: the necessary info to locate the leaf/node.
29 * It's recommened to decode key.objecitd/offset if it's
31 * @reason: describe the error
32 * @bad_value: optional, it's recommened to output bad value and its
33 * expected value (range).
35 * Since comma is used to separate the components, only space is allowed
36 * inside each component.
40 * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
41 * Allows callers to customize the output.
45 static void generic_err(const struct btrfs_fs_info *fs_info,
46 const struct extent_buffer *eb, int slot,
58 "corrupt %s: root=%llu block=%llu slot=%d, %pV",
59 btrfs_header_level(eb) == 0 ? "leaf" : "node",
60 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf);
65 * Customized reporter for extent data item, since its key objectid and
66 * offset has its own meaning.
70 static void file_extent_err(const struct btrfs_fs_info *fs_info,
71 const struct extent_buffer *eb, int slot,
78 btrfs_item_key_to_cpu(eb, &key, slot);
85 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
86 btrfs_header_level(eb) == 0 ? "leaf" : "node",
87 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
88 key.objectid, key.offset, &vaf);
93 * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
96 #define CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, name, alignment) \
98 if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \
99 file_extent_err((fs_info), (leaf), (slot), \
100 "invalid %s for file extent, have %llu, should be aligned to %u", \
101 (#name), btrfs_file_extent_##name((leaf), (fi)), \
103 (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \
106 static int check_extent_data_item(struct btrfs_fs_info *fs_info,
107 struct extent_buffer *leaf,
108 struct btrfs_key *key, int slot)
110 struct btrfs_file_extent_item *fi;
111 u32 sectorsize = fs_info->sectorsize;
112 u32 item_size = btrfs_item_size_nr(leaf, slot);
114 if (!IS_ALIGNED(key->offset, sectorsize)) {
115 file_extent_err(fs_info, leaf, slot,
116 "unaligned file_offset for file extent, have %llu should be aligned to %u",
117 key->offset, sectorsize);
121 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
123 if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) {
124 file_extent_err(fs_info, leaf, slot,
125 "invalid type for file extent, have %u expect range [0, %u]",
126 btrfs_file_extent_type(leaf, fi),
127 BTRFS_FILE_EXTENT_TYPES);
132 * Support for new compression/encrption must introduce incompat flag,
133 * and must be caught in open_ctree().
135 if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) {
136 file_extent_err(fs_info, leaf, slot,
137 "invalid compression for file extent, have %u expect range [0, %u]",
138 btrfs_file_extent_compression(leaf, fi),
139 BTRFS_COMPRESS_TYPES);
142 if (btrfs_file_extent_encryption(leaf, fi)) {
143 file_extent_err(fs_info, leaf, slot,
144 "invalid encryption for file extent, have %u expect 0",
145 btrfs_file_extent_encryption(leaf, fi));
148 if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
149 /* Inline extent must have 0 as key offset */
151 file_extent_err(fs_info, leaf, slot,
152 "invalid file_offset for inline file extent, have %llu expect 0",
157 /* Compressed inline extent has no on-disk size, skip it */
158 if (btrfs_file_extent_compression(leaf, fi) !=
162 /* Uncompressed inline extent size must match item size */
163 if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
164 btrfs_file_extent_ram_bytes(leaf, fi)) {
165 file_extent_err(fs_info, leaf, slot,
166 "invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
167 item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START +
168 btrfs_file_extent_ram_bytes(leaf, fi));
174 /* Regular or preallocated extent has fixed item size */
175 if (item_size != sizeof(*fi)) {
176 file_extent_err(fs_info, leaf, slot,
177 "invalid item size for reg/prealloc file extent, have %u expect %zu",
178 item_size, sizeof(*fi));
181 if (CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, ram_bytes, sectorsize) ||
182 CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_bytenr, sectorsize) ||
183 CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_num_bytes, sectorsize) ||
184 CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, offset, sectorsize) ||
185 CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, num_bytes, sectorsize))
190 static int check_csum_item(struct btrfs_fs_info *fs_info,
191 struct extent_buffer *leaf, struct btrfs_key *key,
194 u32 sectorsize = fs_info->sectorsize;
195 u32 csumsize = btrfs_super_csum_size(fs_info->super_copy);
197 if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) {
198 generic_err(fs_info, leaf, slot,
199 "invalid key objectid for csum item, have %llu expect %llu",
200 key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
203 if (!IS_ALIGNED(key->offset, sectorsize)) {
204 generic_err(fs_info, leaf, slot,
205 "unaligned key offset for csum item, have %llu should be aligned to %u",
206 key->offset, sectorsize);
209 if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) {
210 generic_err(fs_info, leaf, slot,
211 "unaligned item size for csum item, have %u should be aligned to %u",
212 btrfs_item_size_nr(leaf, slot), csumsize);
219 * Customized reported for dir_item, only important new info is key->objectid,
220 * which represents inode number
224 static void dir_item_err(const struct btrfs_fs_info *fs_info,
225 const struct extent_buffer *eb, int slot,
226 const char *fmt, ...)
228 struct btrfs_key key;
229 struct va_format vaf;
232 btrfs_item_key_to_cpu(eb, &key, slot);
239 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
240 btrfs_header_level(eb) == 0 ? "leaf" : "node",
241 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
246 static int check_dir_item(struct btrfs_fs_info *fs_info,
247 struct extent_buffer *leaf,
248 struct btrfs_key *key, int slot)
250 struct btrfs_dir_item *di;
251 u32 item_size = btrfs_item_size_nr(leaf, slot);
254 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
255 while (cur < item_size) {
263 /* header itself should not cross item boundary */
264 if (cur + sizeof(*di) > item_size) {
265 dir_item_err(fs_info, leaf, slot,
266 "dir item header crosses item boundary, have %zu boundary %u",
267 cur + sizeof(*di), item_size);
272 dir_type = btrfs_dir_type(leaf, di);
273 if (dir_type >= BTRFS_FT_MAX) {
274 dir_item_err(fs_info, leaf, slot,
275 "invalid dir item type, have %u expect [0, %u)",
276 dir_type, BTRFS_FT_MAX);
280 if (key->type == BTRFS_XATTR_ITEM_KEY &&
281 dir_type != BTRFS_FT_XATTR) {
282 dir_item_err(fs_info, leaf, slot,
283 "invalid dir item type for XATTR key, have %u expect %u",
284 dir_type, BTRFS_FT_XATTR);
287 if (dir_type == BTRFS_FT_XATTR &&
288 key->type != BTRFS_XATTR_ITEM_KEY) {
289 dir_item_err(fs_info, leaf, slot,
290 "xattr dir type found for non-XATTR key");
293 if (dir_type == BTRFS_FT_XATTR)
294 max_name_len = XATTR_NAME_MAX;
296 max_name_len = BTRFS_NAME_LEN;
298 /* Name/data length check */
299 name_len = btrfs_dir_name_len(leaf, di);
300 data_len = btrfs_dir_data_len(leaf, di);
301 if (name_len > max_name_len) {
302 dir_item_err(fs_info, leaf, slot,
303 "dir item name len too long, have %u max %u",
304 name_len, max_name_len);
307 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info)) {
308 dir_item_err(fs_info, leaf, slot,
309 "dir item name and data len too long, have %u max %u",
311 BTRFS_MAX_XATTR_SIZE(fs_info));
315 if (data_len && dir_type != BTRFS_FT_XATTR) {
316 dir_item_err(fs_info, leaf, slot,
317 "dir item with invalid data len, have %u expect 0",
322 total_size = sizeof(*di) + name_len + data_len;
324 /* header and name/data should not cross item boundary */
325 if (cur + total_size > item_size) {
326 dir_item_err(fs_info, leaf, slot,
327 "dir item data crosses item boundary, have %u boundary %u",
328 cur + total_size, item_size);
333 * Special check for XATTR/DIR_ITEM, as key->offset is name
334 * hash, should match its name
336 if (key->type == BTRFS_DIR_ITEM_KEY ||
337 key->type == BTRFS_XATTR_ITEM_KEY) {
338 char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
340 read_extent_buffer(leaf, namebuf,
341 (unsigned long)(di + 1), name_len);
342 name_hash = btrfs_name_hash(namebuf, name_len);
343 if (key->offset != name_hash) {
344 dir_item_err(fs_info, leaf, slot,
345 "name hash mismatch with key, have 0x%016x expect 0x%016llx",
346 name_hash, key->offset);
351 di = (struct btrfs_dir_item *)((void *)di + total_size);
357 * Common point to switch the item-specific validation.
359 static int check_leaf_item(struct btrfs_fs_info *fs_info,
360 struct extent_buffer *leaf,
361 struct btrfs_key *key, int slot)
366 case BTRFS_EXTENT_DATA_KEY:
367 ret = check_extent_data_item(fs_info, leaf, key, slot);
369 case BTRFS_EXTENT_CSUM_KEY:
370 ret = check_csum_item(fs_info, leaf, key, slot);
372 case BTRFS_DIR_ITEM_KEY:
373 case BTRFS_DIR_INDEX_KEY:
374 case BTRFS_XATTR_ITEM_KEY:
375 ret = check_dir_item(fs_info, leaf, key, slot);
381 static int check_leaf(struct btrfs_fs_info *fs_info, struct extent_buffer *leaf,
382 bool check_item_data)
384 /* No valid key type is 0, so all key should be larger than this key */
385 struct btrfs_key prev_key = {0, 0, 0};
386 struct btrfs_key key;
387 u32 nritems = btrfs_header_nritems(leaf);
391 * Extent buffers from a relocation tree have a owner field that
392 * corresponds to the subvolume tree they are based on. So just from an
393 * extent buffer alone we can not find out what is the id of the
394 * corresponding subvolume tree, so we can not figure out if the extent
395 * buffer corresponds to the root of the relocation tree or not. So
396 * skip this check for relocation trees.
398 if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
399 struct btrfs_root *check_root;
401 key.objectid = btrfs_header_owner(leaf);
402 key.type = BTRFS_ROOT_ITEM_KEY;
403 key.offset = (u64)-1;
405 check_root = btrfs_get_fs_root(fs_info, &key, false);
407 * The only reason we also check NULL here is that during
408 * open_ctree() some roots has not yet been set up.
410 if (!IS_ERR_OR_NULL(check_root)) {
411 struct extent_buffer *eb;
413 eb = btrfs_root_node(check_root);
414 /* if leaf is the root, then it's fine */
416 generic_err(fs_info, leaf, 0,
417 "invalid nritems, have %u should not be 0 for non-root leaf",
419 free_extent_buffer(eb);
422 free_extent_buffer(eb);
431 * Check the following things to make sure this is a good leaf, and
432 * leaf users won't need to bother with similar sanity checks:
435 * 2) item offset and size
436 * No overlap, no hole, all inside the leaf.
438 * If possible, do comprehensive sanity check.
439 * NOTE: All checks must only rely on the item data itself.
441 for (slot = 0; slot < nritems; slot++) {
442 u32 item_end_expected;
445 btrfs_item_key_to_cpu(leaf, &key, slot);
447 /* Make sure the keys are in the right order */
448 if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) {
449 generic_err(fs_info, leaf, slot,
450 "bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
451 prev_key.objectid, prev_key.type,
452 prev_key.offset, key.objectid, key.type,
458 * Make sure the offset and ends are right, remember that the
459 * item data starts at the end of the leaf and grows towards the
463 item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
465 item_end_expected = btrfs_item_offset_nr(leaf,
467 if (btrfs_item_end_nr(leaf, slot) != item_end_expected) {
468 generic_err(fs_info, leaf, slot,
469 "unexpected item end, have %u expect %u",
470 btrfs_item_end_nr(leaf, slot),
476 * Check to make sure that we don't point outside of the leaf,
477 * just in case all the items are consistent to each other, but
478 * all point outside of the leaf.
480 if (btrfs_item_end_nr(leaf, slot) >
481 BTRFS_LEAF_DATA_SIZE(fs_info)) {
482 generic_err(fs_info, leaf, slot,
483 "slot end outside of leaf, have %u expect range [0, %u]",
484 btrfs_item_end_nr(leaf, slot),
485 BTRFS_LEAF_DATA_SIZE(fs_info));
489 /* Also check if the item pointer overlaps with btrfs item. */
490 if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) >
491 btrfs_item_ptr_offset(leaf, slot)) {
492 generic_err(fs_info, leaf, slot,
493 "slot overlaps with its data, item end %lu data start %lu",
494 btrfs_item_nr_offset(slot) +
495 sizeof(struct btrfs_item),
496 btrfs_item_ptr_offset(leaf, slot));
500 if (check_item_data) {
502 * Check if the item size and content meet other
505 ret = check_leaf_item(fs_info, leaf, &key, slot);
510 prev_key.objectid = key.objectid;
511 prev_key.type = key.type;
512 prev_key.offset = key.offset;
518 int btrfs_check_leaf_full(struct btrfs_fs_info *fs_info,
519 struct extent_buffer *leaf)
521 return check_leaf(fs_info, leaf, true);
524 int btrfs_check_leaf_relaxed(struct btrfs_fs_info *fs_info,
525 struct extent_buffer *leaf)
527 return check_leaf(fs_info, leaf, false);
530 int btrfs_check_node(struct btrfs_fs_info *fs_info, struct extent_buffer *node)
532 unsigned long nr = btrfs_header_nritems(node);
533 struct btrfs_key key, next_key;
538 if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) {
540 "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
541 btrfs_header_owner(node), node->start,
542 nr == 0 ? "small" : "large", nr,
543 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
547 for (slot = 0; slot < nr - 1; slot++) {
548 bytenr = btrfs_node_blockptr(node, slot);
549 btrfs_node_key_to_cpu(node, &key, slot);
550 btrfs_node_key_to_cpu(node, &next_key, slot + 1);
553 generic_err(fs_info, node, slot,
554 "invalid NULL node pointer");
558 if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) {
559 generic_err(fs_info, node, slot,
560 "unaligned pointer, have %llu should be aligned to %u",
561 bytenr, fs_info->sectorsize);
566 if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
567 generic_err(fs_info, node, slot,
568 "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
569 key.objectid, key.type, key.offset,
570 next_key.objectid, next_key.type,