btrfs: fix root ref counts in error handling in btrfs_get_root_ref
authorJia-Ju Bai <baijiaju1990@gmail.com>
Thu, 24 Mar 2022 13:44:54 +0000 (06:44 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 20 Apr 2022 07:34:19 +0000 (09:34 +0200)
commit252db93fd0bd5ca07c9b933ed94e93a4a43e8901
tree3f777f5092f3d83ab9fa3c0debb4cd12364b823d
parent9b81c2c147e14abf6181271758c4c29353e20367
btrfs: fix root ref counts in error handling in btrfs_get_root_ref

commit 168a2f776b9762f4021421008512dd7ab7474df1 upstream.

In btrfs_get_root_ref(), when btrfs_insert_fs_root() fails,
btrfs_put_root() can happen for two reasons:

- the root already exists in the tree, in that case it returns the
  reference obtained in btrfs_lookup_fs_root()

- another error so the cleanup is done in the fail label

Calling btrfs_put_root() unconditionally would lead to double decrement
of the root reference possibly freeing it in the second case.

Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
Fixes: bc44d7c4b2b1 ("btrfs: push btrfs_grab_fs_root into btrfs_get_fs_root")
CC: stable@vger.kernel.org # 5.10+
Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/btrfs/disk-io.c