• Zhao Lei's avatar
    btrfs: Avoid NULL pointer dereference of free_extent_buffer when read_tree_block() fail · 95ab1f64
    Zhao Lei authored
    When read_tree_block() failed, we can see following dmesg:
     [  134.371389] BUG: unable to handle kernel NULL pointer dereference at 0000000000000063
     [  134.372236] IP: [<ffffffff813a4a51>] free_extent_buffer+0x21/0x90
     [  134.372236] PGD 0
     [  134.372236] Oops: 0000 [#1] SMP
     [  134.372236] Modules linked in:
     [  134.372236] CPU: 0 PID: 2289 Comm: mount Not tainted 4.2.0-rc1_HEAD_c65b99f0_+ #115
     [  134.372236] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.7.5.1-0-g8936dbb-20141113_115728-nilsson.home.kraxel.org 04/01/2014
     [  134.372236] task: ffff88003b6e1a00 ti: ffff880011e60000 task.ti: ffff880011e60000
     [  134.372236] RIP: 0010:[<ffffffff813a4a51>]  [<ffffffff813a4a51>] free_extent_buffer+0x21/0x90
     ...
     [  134.372236] Call Trace:
     [  134.372236]  [<ffffffff81379aa1>] free_root_extent_buffers+0x91/0xb0
     [  134.372236]  [<ffffffff81379c3d>] free_root_pointers+0x17d/0x190
     [  134.372236]  [<ffffffff813801b0>] open_ctree+0x1ca0/0x25b0
     [  134.372236]  [<ffffffff8144d017>] ? disk_name+0x97/0xb0
     [  134.372236]  [<ffffffff813558aa>] btrfs_mount+0x8fa/0xab0
     ...
    
    Reason:
     read_tree_block() changed to return error number on fail,
     and this value(not NULL) is set to tree_root->node, then subsequent
     code will run to:
      free_root_pointers()
      ->free_root_extent_buffers()
      ->free_extent_buffer()
      ->atomic_read((extent_buffer *)(-E_XXX)->refs);
     and trigger above error.
    
    Fix:
     Set tree_root->node to NULL on fail to make error_handle code
     happy.
    Signed-off-by: default avatarZhao Lei <zhaolei@cn.fujitsu.com>
    Signed-off-by: default avatarChris Mason <clm@fb.com>
    95ab1f64
disk-io.c 120 KB