btrfs-progs: Use more strict check to read out tree root
authorQu Wenruo <quwenruo@cn.fujitsu.com>
Tue, 25 Apr 2017 08:40:16 +0000 (16:40 +0800)
committerDavid Sterba <dsterba@suse.com>
Tue, 2 May 2017 14:33:04 +0000 (16:33 +0200)
Fuzzed image bko-156811-bad-parent-ref-qgroup-verify.raw causes qgroup
to report -ENOMEM.

But the fact is, such image is heavily damaged so there is no valid root
item for the extent tree.

Normal extent tree key in root tree should be (EXTENT_TREE ROOT_ITEM 0),
while in that fuzzed image, we got (EXTENT_TREE EXXTENT_DATA SOME_NUMBER).

It's btrfs_find_last_root() that only checks the objectid, not caring
about the key type leading to such problem.

Fix it by doing extra check on key type.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
[ edit changelog ]
Signed-off-by: David Sterba <dsterba@suse.com>
root-tree.c

index ab01a14..6b8f8c1 100644 (file)
@@ -51,7 +51,8 @@ int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
        l = path->nodes[0];
        slot = path->slots[0] - 1;
        btrfs_item_key_to_cpu(l, &found_key, slot);
-       if (found_key.objectid != objectid) {
+       if (found_key.type != BTRFS_ROOT_ITEM_KEY ||
+           found_key.objectid != objectid) {
                ret = -ENOENT;
                goto out;
        }