2 * Copyright (C) Qu Wenruo 2017. All rights reserved.
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.
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.
13 * You should have received a copy of the GNU General Public
14 * License along with this program.
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.
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.
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.
30 #include "tree-checker.h"
32 #include "compression.h"
36 #define CORRUPT(reason, eb, root, slot) \
37 btrfs_crit(root->fs_info, \
38 "corrupt %s, %s: block=%llu, root=%llu, slot=%d", \
39 btrfs_header_level(eb) == 0 ? "leaf" : "node", \
40 reason, btrfs_header_bytenr(eb), root->objectid, slot)
43 * Error message should follow the following format:
44 * corrupt <type>: <identifier>, <reason>[, <bad_value>]
47 * @identifier: the necessary info to locate the leaf/node.
48 * It's recommened to decode key.objecitd/offset if it's
50 * @reason: describe the error
51 * @bad_value: optional, it's recommened to output bad value and its
52 * expected value (range).
54 * Since comma is used to separate the components, only space is allowed
55 * inside each component.
59 * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
60 * Allows callers to customize the output.
63 static void generic_err(const struct btrfs_root
*root
,
64 const struct extent_buffer
*eb
, int slot
,
75 btrfs_crit(root
->fs_info
,
76 "corrupt %s: root=%llu block=%llu slot=%d, %pV",
77 btrfs_header_level(eb
) == 0 ? "leaf" : "node",
78 root
->objectid
, btrfs_header_bytenr(eb
), slot
, &vaf
);
82 static int check_extent_data_item(struct btrfs_root
*root
,
83 struct extent_buffer
*leaf
,
84 struct btrfs_key
*key
, int slot
)
86 struct btrfs_file_extent_item
*fi
;
87 u32 sectorsize
= root
->sectorsize
;
88 u32 item_size
= btrfs_item_size_nr(leaf
, slot
);
90 if (!IS_ALIGNED(key
->offset
, sectorsize
)) {
91 CORRUPT("unaligned key offset for file extent",
96 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
98 if (btrfs_file_extent_type(leaf
, fi
) > BTRFS_FILE_EXTENT_TYPES
) {
99 CORRUPT("invalid file extent type", leaf
, root
, slot
);
104 * Support for new compression/encrption must introduce incompat flag,
105 * and must be caught in open_ctree().
107 if (btrfs_file_extent_compression(leaf
, fi
) > BTRFS_COMPRESS_TYPES
) {
108 CORRUPT("invalid file extent compression", leaf
, root
, slot
);
111 if (btrfs_file_extent_encryption(leaf
, fi
)) {
112 CORRUPT("invalid file extent encryption", leaf
, root
, slot
);
115 if (btrfs_file_extent_type(leaf
, fi
) == BTRFS_FILE_EXTENT_INLINE
) {
116 /* Inline extent must have 0 as key offset */
118 CORRUPT("inline extent has non-zero key offset",
123 /* Compressed inline extent has no on-disk size, skip it */
124 if (btrfs_file_extent_compression(leaf
, fi
) !=
128 /* Uncompressed inline extent size must match item size */
129 if (item_size
!= BTRFS_FILE_EXTENT_INLINE_DATA_START
+
130 btrfs_file_extent_ram_bytes(leaf
, fi
)) {
131 CORRUPT("plaintext inline extent has invalid size",
138 /* Regular or preallocated extent has fixed item size */
139 if (item_size
!= sizeof(*fi
)) {
141 "regluar or preallocated extent data item size is invalid",
145 if (!IS_ALIGNED(btrfs_file_extent_ram_bytes(leaf
, fi
), sectorsize
) ||
146 !IS_ALIGNED(btrfs_file_extent_disk_bytenr(leaf
, fi
), sectorsize
) ||
147 !IS_ALIGNED(btrfs_file_extent_disk_num_bytes(leaf
, fi
), sectorsize
) ||
148 !IS_ALIGNED(btrfs_file_extent_offset(leaf
, fi
), sectorsize
) ||
149 !IS_ALIGNED(btrfs_file_extent_num_bytes(leaf
, fi
), sectorsize
)) {
151 "regular or preallocated extent data item has unaligned value",
159 static int check_csum_item(struct btrfs_root
*root
, struct extent_buffer
*leaf
,
160 struct btrfs_key
*key
, int slot
)
162 u32 sectorsize
= root
->sectorsize
;
163 u32 csumsize
= btrfs_super_csum_size(root
->fs_info
->super_copy
);
165 if (key
->objectid
!= BTRFS_EXTENT_CSUM_OBJECTID
) {
166 CORRUPT("invalid objectid for csum item", leaf
, root
, slot
);
169 if (!IS_ALIGNED(key
->offset
, sectorsize
)) {
170 CORRUPT("unaligned key offset for csum item", leaf
, root
, slot
);
173 if (!IS_ALIGNED(btrfs_item_size_nr(leaf
, slot
), csumsize
)) {
174 CORRUPT("unaligned csum item size", leaf
, root
, slot
);
181 * Customized reported for dir_item, only important new info is key->objectid,
182 * which represents inode number
185 static void dir_item_err(const struct btrfs_root
*root
,
186 const struct extent_buffer
*eb
, int slot
,
187 const char *fmt
, ...)
189 struct btrfs_key key
;
190 struct va_format vaf
;
193 btrfs_item_key_to_cpu(eb
, &key
, slot
);
199 btrfs_crit(root
->fs_info
,
200 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
201 btrfs_header_level(eb
) == 0 ? "leaf" : "node", root
->objectid
,
202 btrfs_header_bytenr(eb
), slot
, key
.objectid
, &vaf
);
206 static int check_dir_item(struct btrfs_root
*root
,
207 struct extent_buffer
*leaf
,
208 struct btrfs_key
*key
, int slot
)
210 struct btrfs_dir_item
*di
;
211 u32 item_size
= btrfs_item_size_nr(leaf
, slot
);
214 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
215 while (cur
< item_size
) {
223 /* header itself should not cross item boundary */
224 if (cur
+ sizeof(*di
) > item_size
) {
225 dir_item_err(root
, leaf
, slot
,
226 "dir item header crosses item boundary, have %zu boundary %u",
227 cur
+ sizeof(*di
), item_size
);
232 dir_type
= btrfs_dir_type(leaf
, di
);
233 if (dir_type
>= BTRFS_FT_MAX
) {
234 dir_item_err(root
, leaf
, slot
,
235 "invalid dir item type, have %u expect [0, %u)",
236 dir_type
, BTRFS_FT_MAX
);
240 if (key
->type
== BTRFS_XATTR_ITEM_KEY
&&
241 dir_type
!= BTRFS_FT_XATTR
) {
242 dir_item_err(root
, leaf
, slot
,
243 "invalid dir item type for XATTR key, have %u expect %u",
244 dir_type
, BTRFS_FT_XATTR
);
247 if (dir_type
== BTRFS_FT_XATTR
&&
248 key
->type
!= BTRFS_XATTR_ITEM_KEY
) {
249 dir_item_err(root
, leaf
, slot
,
250 "xattr dir type found for non-XATTR key");
253 if (dir_type
== BTRFS_FT_XATTR
)
254 max_name_len
= XATTR_NAME_MAX
;
256 max_name_len
= BTRFS_NAME_LEN
;
258 /* Name/data length check */
259 name_len
= btrfs_dir_name_len(leaf
, di
);
260 data_len
= btrfs_dir_data_len(leaf
, di
);
261 if (name_len
> max_name_len
) {
262 dir_item_err(root
, leaf
, slot
,
263 "dir item name len too long, have %u max %u",
264 name_len
, max_name_len
);
267 if (name_len
+ data_len
> BTRFS_MAX_XATTR_SIZE(root
)) {
268 dir_item_err(root
, leaf
, slot
,
269 "dir item name and data len too long, have %u max %zu",
271 BTRFS_MAX_XATTR_SIZE(root
));
275 if (data_len
&& dir_type
!= BTRFS_FT_XATTR
) {
276 dir_item_err(root
, leaf
, slot
,
277 "dir item with invalid data len, have %u expect 0",
282 total_size
= sizeof(*di
) + name_len
+ data_len
;
284 /* header and name/data should not cross item boundary */
285 if (cur
+ total_size
> item_size
) {
286 dir_item_err(root
, leaf
, slot
,
287 "dir item data crosses item boundary, have %u boundary %u",
288 cur
+ total_size
, item_size
);
293 * Special check for XATTR/DIR_ITEM, as key->offset is name
294 * hash, should match its name
296 if (key
->type
== BTRFS_DIR_ITEM_KEY
||
297 key
->type
== BTRFS_XATTR_ITEM_KEY
) {
298 char namebuf
[max(BTRFS_NAME_LEN
, XATTR_NAME_MAX
)];
300 read_extent_buffer(leaf
, namebuf
,
301 (unsigned long)(di
+ 1), name_len
);
302 name_hash
= btrfs_name_hash(namebuf
, name_len
);
303 if (key
->offset
!= name_hash
) {
304 dir_item_err(root
, leaf
, slot
,
305 "name hash mismatch with key, have 0x%016x expect 0x%016llx",
306 name_hash
, key
->offset
);
311 di
= (struct btrfs_dir_item
*)((void *)di
+ total_size
);
318 static void block_group_err(const struct btrfs_fs_info
*fs_info
,
319 const struct extent_buffer
*eb
, int slot
,
320 const char *fmt
, ...)
322 struct btrfs_key key
;
323 struct va_format vaf
;
326 btrfs_item_key_to_cpu(eb
, &key
, slot
);
333 "corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
334 btrfs_header_level(eb
) == 0 ? "leaf" : "node",
335 btrfs_header_owner(eb
), btrfs_header_bytenr(eb
), slot
,
336 key
.objectid
, key
.offset
, &vaf
);
340 static int check_block_group_item(struct btrfs_fs_info
*fs_info
,
341 struct extent_buffer
*leaf
,
342 struct btrfs_key
*key
, int slot
)
344 struct btrfs_block_group_item bgi
;
345 u32 item_size
= btrfs_item_size_nr(leaf
, slot
);
350 * Here we don't really care about alignment since extent allocator can
351 * handle it. We care more about the size, as if one block group is
352 * larger than maximum size, it's must be some obvious corruption.
354 if (key
->offset
> BTRFS_MAX_DATA_CHUNK_SIZE
|| key
->offset
== 0) {
355 block_group_err(fs_info
, leaf
, slot
,
356 "invalid block group size, have %llu expect (0, %llu]",
357 key
->offset
, BTRFS_MAX_DATA_CHUNK_SIZE
);
361 if (item_size
!= sizeof(bgi
)) {
362 block_group_err(fs_info
, leaf
, slot
,
363 "invalid item size, have %u expect %zu",
364 item_size
, sizeof(bgi
));
368 read_extent_buffer(leaf
, &bgi
, btrfs_item_ptr_offset(leaf
, slot
),
370 if (btrfs_block_group_chunk_objectid(&bgi
) !=
371 BTRFS_FIRST_CHUNK_TREE_OBJECTID
) {
372 block_group_err(fs_info
, leaf
, slot
,
373 "invalid block group chunk objectid, have %llu expect %llu",
374 btrfs_block_group_chunk_objectid(&bgi
),
375 BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
379 if (btrfs_block_group_used(&bgi
) > key
->offset
) {
380 block_group_err(fs_info
, leaf
, slot
,
381 "invalid block group used, have %llu expect [0, %llu)",
382 btrfs_block_group_used(&bgi
), key
->offset
);
386 flags
= btrfs_block_group_flags(&bgi
);
387 if (hweight64(flags
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) > 1) {
388 block_group_err(fs_info
, leaf
, slot
,
389 "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
390 flags
& BTRFS_BLOCK_GROUP_PROFILE_MASK
,
391 hweight64(flags
& BTRFS_BLOCK_GROUP_PROFILE_MASK
));
395 type
= flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
;
396 if (type
!= BTRFS_BLOCK_GROUP_DATA
&&
397 type
!= BTRFS_BLOCK_GROUP_METADATA
&&
398 type
!= BTRFS_BLOCK_GROUP_SYSTEM
&&
399 type
!= (BTRFS_BLOCK_GROUP_METADATA
|
400 BTRFS_BLOCK_GROUP_DATA
)) {
401 block_group_err(fs_info
, leaf
, slot
,
402 "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
403 type
, hweight64(type
),
404 BTRFS_BLOCK_GROUP_DATA
, BTRFS_BLOCK_GROUP_METADATA
,
405 BTRFS_BLOCK_GROUP_SYSTEM
,
406 BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
);
413 * Common point to switch the item-specific validation.
415 static int check_leaf_item(struct btrfs_root
*root
,
416 struct extent_buffer
*leaf
,
417 struct btrfs_key
*key
, int slot
)
422 case BTRFS_EXTENT_DATA_KEY
:
423 ret
= check_extent_data_item(root
, leaf
, key
, slot
);
425 case BTRFS_EXTENT_CSUM_KEY
:
426 ret
= check_csum_item(root
, leaf
, key
, slot
);
428 case BTRFS_DIR_ITEM_KEY
:
429 case BTRFS_DIR_INDEX_KEY
:
430 case BTRFS_XATTR_ITEM_KEY
:
431 ret
= check_dir_item(root
, leaf
, key
, slot
);
433 case BTRFS_BLOCK_GROUP_ITEM_KEY
:
434 ret
= check_block_group_item(root
->fs_info
, leaf
, key
, slot
);
440 static int check_leaf(struct btrfs_root
*root
, struct extent_buffer
*leaf
,
441 bool check_item_data
)
443 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
444 /* No valid key type is 0, so all key should be larger than this key */
445 struct btrfs_key prev_key
= {0, 0, 0};
446 struct btrfs_key key
;
447 u32 nritems
= btrfs_header_nritems(leaf
);
450 if (btrfs_header_level(leaf
) != 0) {
451 generic_err(root
, leaf
, 0,
452 "invalid level for leaf, have %d expect 0",
453 btrfs_header_level(leaf
));
458 * Extent buffers from a relocation tree have a owner field that
459 * corresponds to the subvolume tree they are based on. So just from an
460 * extent buffer alone we can not find out what is the id of the
461 * corresponding subvolume tree, so we can not figure out if the extent
462 * buffer corresponds to the root of the relocation tree or not. So
463 * skip this check for relocation trees.
465 if (nritems
== 0 && !btrfs_header_flag(leaf
, BTRFS_HEADER_FLAG_RELOC
)) {
466 u64 owner
= btrfs_header_owner(leaf
);
467 struct btrfs_root
*check_root
;
469 /* These trees must never be empty */
470 if (owner
== BTRFS_ROOT_TREE_OBJECTID
||
471 owner
== BTRFS_CHUNK_TREE_OBJECTID
||
472 owner
== BTRFS_EXTENT_TREE_OBJECTID
||
473 owner
== BTRFS_DEV_TREE_OBJECTID
||
474 owner
== BTRFS_FS_TREE_OBJECTID
||
475 owner
== BTRFS_DATA_RELOC_TREE_OBJECTID
) {
476 generic_err(root
, leaf
, 0,
477 "invalid root, root %llu must never be empty",
481 key
.objectid
= owner
;
482 key
.type
= BTRFS_ROOT_ITEM_KEY
;
483 key
.offset
= (u64
)-1;
485 check_root
= btrfs_get_fs_root(fs_info
, &key
, false);
487 * The only reason we also check NULL here is that during
488 * open_ctree() some roots has not yet been set up.
490 if (!IS_ERR_OR_NULL(check_root
)) {
491 struct extent_buffer
*eb
;
493 eb
= btrfs_root_node(check_root
);
494 /* if leaf is the root, then it's fine */
496 CORRUPT("non-root leaf's nritems is 0",
497 leaf
, check_root
, 0);
498 free_extent_buffer(eb
);
501 free_extent_buffer(eb
);
510 * Check the following things to make sure this is a good leaf, and
511 * leaf users won't need to bother with similar sanity checks:
514 * 2) item offset and size
515 * No overlap, no hole, all inside the leaf.
517 * If possible, do comprehensive sanity check.
518 * NOTE: All checks must only rely on the item data itself.
520 for (slot
= 0; slot
< nritems
; slot
++) {
521 u32 item_end_expected
;
524 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
526 /* Make sure the keys are in the right order */
527 if (btrfs_comp_cpu_keys(&prev_key
, &key
) >= 0) {
528 CORRUPT("bad key order", leaf
, root
, slot
);
533 * Make sure the offset and ends are right, remember that the
534 * item data starts at the end of the leaf and grows towards the
538 item_end_expected
= BTRFS_LEAF_DATA_SIZE(root
);
540 item_end_expected
= btrfs_item_offset_nr(leaf
,
542 if (btrfs_item_end_nr(leaf
, slot
) != item_end_expected
) {
543 CORRUPT("slot offset bad", leaf
, root
, slot
);
548 * Check to make sure that we don't point outside of the leaf,
549 * just in case all the items are consistent to each other, but
550 * all point outside of the leaf.
552 if (btrfs_item_end_nr(leaf
, slot
) >
553 BTRFS_LEAF_DATA_SIZE(root
)) {
554 CORRUPT("slot end outside of leaf", leaf
, root
, slot
);
558 /* Also check if the item pointer overlaps with btrfs item. */
559 if (btrfs_item_nr_offset(slot
) + sizeof(struct btrfs_item
) >
560 btrfs_item_ptr_offset(leaf
, slot
)) {
561 CORRUPT("slot overlap with its data", leaf
, root
, slot
);
565 if (check_item_data
) {
567 * Check if the item size and content meet other
570 ret
= check_leaf_item(root
, leaf
, &key
, slot
);
575 prev_key
.objectid
= key
.objectid
;
576 prev_key
.type
= key
.type
;
577 prev_key
.offset
= key
.offset
;
583 int btrfs_check_leaf_full(struct btrfs_root
*root
, struct extent_buffer
*leaf
)
585 return check_leaf(root
, leaf
, true);
588 int btrfs_check_leaf_relaxed(struct btrfs_root
*root
,
589 struct extent_buffer
*leaf
)
591 return check_leaf(root
, leaf
, false);
594 int btrfs_check_node(struct btrfs_root
*root
, struct extent_buffer
*node
)
596 unsigned long nr
= btrfs_header_nritems(node
);
597 struct btrfs_key key
, next_key
;
599 int level
= btrfs_header_level(node
);
603 if (level
<= 0 || level
>= BTRFS_MAX_LEVEL
) {
604 generic_err(root
, node
, 0,
605 "invalid level for node, have %d expect [1, %d]",
606 level
, BTRFS_MAX_LEVEL
- 1);
609 if (nr
== 0 || nr
> BTRFS_NODEPTRS_PER_BLOCK(root
)) {
610 btrfs_crit(root
->fs_info
,
611 "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%zu]",
612 root
->objectid
, node
->start
,
613 nr
== 0 ? "small" : "large", nr
,
614 BTRFS_NODEPTRS_PER_BLOCK(root
));
618 for (slot
= 0; slot
< nr
- 1; slot
++) {
619 bytenr
= btrfs_node_blockptr(node
, slot
);
620 btrfs_node_key_to_cpu(node
, &key
, slot
);
621 btrfs_node_key_to_cpu(node
, &next_key
, slot
+ 1);
624 generic_err(root
, node
, slot
,
625 "invalid NULL node pointer");
629 if (!IS_ALIGNED(bytenr
, root
->sectorsize
)) {
630 generic_err(root
, node
, slot
,
631 "unaligned pointer, have %llu should be aligned to %u",
632 bytenr
, root
->sectorsize
);
637 if (btrfs_comp_cpu_keys(&key
, &next_key
) >= 0) {
638 generic_err(root
, node
, slot
,
639 "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
640 key
.objectid
, key
.type
, key
.offset
,
641 next_key
.objectid
, next_key
.type
,