btrfs-progs: Enhance read_tree_block to avoid memory corruption
authorQu Wenruo <quwenruo@cn.fujitsu.com>
Fri, 22 May 2015 01:01:23 +0000 (09:01 +0800)
committerDavid Sterba <dsterba@suse.cz>
Mon, 25 May 2015 12:58:58 +0000 (14:58 +0200)
commit6c9e4dacb2467944b306cafe977514fae076f0da
tree881adf7bf7cf9e819ebf22234bde304d96df331f
parent9e3e423d688b5c825db83efd9c6ac5aa0355be55
btrfs-progs: Enhance read_tree_block to avoid memory corruption

Add the following tree block check to avoid memory corruption on hostile
image:
1) Check level.
Level >= BTRFS_MAX_LEVEL won't be read out.

2) Nritems.
For nr_items > max_nritems, the tree_block won't be read out.
Max nritems is calculated in a easy method.
For node, it's straightforward, just (nodesize - header size) /
(btrfs_key_ptr)
For leaf, (nodesize - header size) / (btrfs_item), as btrfs support zero
item size

This fixes 3 kernel bugs: BZ#97171, BZ#97191, BZ#97271.

Reported-by: Lukas Lueg <lukas.lueg@gmail.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
disk-io.c