Commit 88c602ab authored by Qu Wenruo's avatar Qu Wenruo Committed by David Sterba

btrfs: tree-checker: check extent buffer owner against owner rootid

Btrfs doesn't check whether the tree block respects the root owner.
This means, if a tree block referred by a parent in extent tree, but has
owner of 5, btrfs can still continue reading the tree block, as long as
it doesn't trigger other sanity checks.

Normally this is fine, but combined with the empty tree check in
check_leaf(), if we hit an empty extent tree, but the root node has
csum tree owner, we can let such extent buffer to sneak in.

Shrink the hole by:

- Do extra eb owner check at tree read time

- Make sure the root owner extent buffer exactly matches the root id.

Unfortunately we can't yet completely patch the hole, there are several
call sites can't pass all info we need:

- For reloc/log trees
  Their owner is key::offset, not key::objectid.
  We need the full root key to do that accurate check.

  For now, we just skip the ownership check for those trees.

- For add_data_references() of relocation
  That call site doesn't have any parent/ownership info, as all the
  bytenrs are all from btrfs_find_all_leafs().

- For direct backref items walk
  Direct backref items records the parent bytenr directly, thus unlike
  indirect backref item, we don't do a full tree search.

  Thus in that case, we don't have full parent owner to check.

For the later two cases, they all pass 0 as @owner_root, thus we can
skip those cases if @owner_root is 0.
Signed-off-by: default avatarQu Wenruo <wqu@suse.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 63c34cb4
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "volumes.h" #include "volumes.h"
#include "qgroup.h" #include "qgroup.h"
#include "tree-mod-log.h" #include "tree-mod-log.h"
#include "tree-checker.h"
static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_path *path, int level); *root, struct btrfs_path *path, int level);
...@@ -1456,6 +1457,11 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p, ...@@ -1456,6 +1457,11 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
btrfs_release_path(p); btrfs_release_path(p);
return -EIO; return -EIO;
} }
if (btrfs_check_eb_owner(tmp, root->root_key.objectid)) {
free_extent_buffer(tmp);
btrfs_release_path(p);
return -EUCLEAN;
}
if (unlock_up) if (unlock_up)
ret = -EAGAIN; ret = -EAGAIN;
......
...@@ -1123,6 +1123,10 @@ struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, ...@@ -1123,6 +1123,10 @@ struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
free_extent_buffer_stale(buf); free_extent_buffer_stale(buf);
return ERR_PTR(ret); return ERR_PTR(ret);
} }
if (btrfs_check_eb_owner(buf, owner_root)) {
free_extent_buffer_stale(buf);
return ERR_PTR(-EUCLEAN);
}
return buf; return buf;
} }
...@@ -1562,6 +1566,23 @@ static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root, ...@@ -1562,6 +1566,23 @@ static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root,
ret = -EIO; ret = -EIO;
goto fail; goto fail;
} }
/*
* For real fs, and not log/reloc trees, root owner must
* match its root node owner
*/
if (!test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state) &&
root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID &&
root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
root->root_key.objectid != btrfs_header_owner(root->node)) {
btrfs_crit(fs_info,
"root=%llu block=%llu, tree root owner mismatch, have %llu expect %llu",
root->root_key.objectid, root->node->start,
btrfs_header_owner(root->node),
root->root_key.objectid);
ret = -EUCLEAN;
goto fail;
}
root->commit_root = btrfs_root_node(root); root->commit_root = btrfs_root_node(root);
return root; return root;
fail: fail:
......
...@@ -1855,3 +1855,58 @@ int btrfs_check_node(struct extent_buffer *node) ...@@ -1855,3 +1855,58 @@ int btrfs_check_node(struct extent_buffer *node)
return ret; return ret;
} }
ALLOW_ERROR_INJECTION(btrfs_check_node, ERRNO); ALLOW_ERROR_INJECTION(btrfs_check_node, ERRNO);
int btrfs_check_eb_owner(const struct extent_buffer *eb, u64 root_owner)
{
const bool is_subvol = is_fstree(root_owner);
const u64 eb_owner = btrfs_header_owner(eb);
/*
* Skip dummy fs, as selftests don't create unique ebs for each dummy
* root.
*/
if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &eb->fs_info->fs_state))
return 0;
/*
* There are several call sites (backref walking, qgroup, and data
* reloc) passing 0 as @root_owner, as they are not holding the
* tree root. In that case, we can not do a reliable ownership check,
* so just exit.
*/
if (root_owner == 0)
return 0;
/*
* These trees use key.offset as their owner, our callers don't have
* the extra capacity to pass key.offset here. So we just skip them.
*/
if (root_owner == BTRFS_TREE_LOG_OBJECTID ||
root_owner == BTRFS_TREE_RELOC_OBJECTID)
return 0;
if (!is_subvol) {
/* For non-subvolume trees, the eb owner should match root owner */
if (unlikely(root_owner != eb_owner)) {
btrfs_crit(eb->fs_info,
"corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect %llu",
btrfs_header_level(eb) == 0 ? "leaf" : "node",
root_owner, btrfs_header_bytenr(eb), eb_owner,
root_owner);
return -EUCLEAN;
}
return 0;
}
/*
* For subvolume trees, owners can mismatch, but they should all belong
* to subvolume trees.
*/
if (unlikely(is_subvol != is_fstree(eb_owner))) {
btrfs_crit(eb->fs_info,
"corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect [%llu, %llu]",
btrfs_header_level(eb) == 0 ? "leaf" : "node",
root_owner, btrfs_header_bytenr(eb), eb_owner,
BTRFS_FIRST_FREE_OBJECTID, BTRFS_LAST_FREE_OBJECTID);
return -EUCLEAN;
}
return 0;
}
...@@ -25,5 +25,6 @@ int btrfs_check_node(struct extent_buffer *node); ...@@ -25,5 +25,6 @@ int btrfs_check_node(struct extent_buffer *node);
int btrfs_check_chunk_valid(struct extent_buffer *leaf, int btrfs_check_chunk_valid(struct extent_buffer *leaf,
struct btrfs_chunk *chunk, u64 logical); struct btrfs_chunk *chunk, u64 logical);
int btrfs_check_eb_owner(const struct extent_buffer *eb, u64 root_owner);
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment