From: void0red Date: Wed, 23 Nov 2022 14:39:45 +0000 (+0800) Subject: btrfs: fix extent map use-after-free when handling missing device in read_one_chunk X-Git-Tag: v5.15.92~554 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b8e7ed42bc3ca0d0e4191ee394d34962d3624c22;p=platform%2Fkernel%2Flinux-rpi.git btrfs: fix extent map use-after-free when handling missing device in read_one_chunk [ Upstream commit 1742e1c90c3da344f3bb9b1f1309b3f47482756a ] Store the error code before freeing the extent_map. Though it's reference counted structure, in that function it's the first and last allocation so this would lead to a potential use-after-free. The error can happen eg. when chunk is stored on a missing device and the degraded mount option is missing. Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=216721 Reported-by: eriri <1527030098@qq.com> Fixes: adfb69af7d8c ("btrfs: add_missing_dev() should return the actual error") CC: stable@vger.kernel.org # 4.9+ Signed-off-by: void0red Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index c773ecb..6b86a3c 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -7155,8 +7155,9 @@ static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf, map->stripes[i].dev = handle_missing_device(fs_info, devid, uuid); if (IS_ERR(map->stripes[i].dev)) { + ret = PTR_ERR(map->stripes[i].dev); free_extent_map(em); - return PTR_ERR(map->stripes[i].dev); + return ret; } }