From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org, dsterba@suse.cz
Subject: [PATCH v2] btrfs-progs: ctree: Add extra level check for read_node_slot()
Date: Fri, 9 Feb 2018 15:44:18 +0800 [thread overview]
Message-ID: <20180209074428.29187-2-wqu@suse.com> (raw)
In-Reply-To: <20180209074428.29187-1-wqu@suse.com>
Strangely, we have level check in btrfs_print_tree() while we don't have
the same check in read_node_slot().
That's to say, for the following corruption, btrfs_search_slot() or
btrfs_next_leaf() can return invalid leaf:
Parent eb:
node XXXXXX level 1
^^^^^^^
Child should be leaf (level 0)
...
key (XXX XXX XXX) block YYYYYY
Child eb:
leaf YYYYYY level 1
^^^^^^^
Something went wrong now
And for the corrupted leaf returned, later caller can be screwed up
easily.
Although the root cause (powerloss, but still something wrong breaking
metadata CoW of btrfs) is still unknown, at least enhance btrfs-progs to
avoid SEGV.
Reported-by: Ralph Gauges <ralphgauges@googlemail.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
changlog:
v2:
Check if the extent buffer is up-to-date before checking its level to
avoid possible NULL pointer access.
---
ctree.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/ctree.c b/ctree.c
index 4fc33b14000a..430805e3043f 100644
--- a/ctree.c
+++ b/ctree.c
@@ -22,6 +22,7 @@
#include "repair.h"
#include "internal.h"
#include "sizes.h"
+#include "messages.h"
static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_path *path, int level);
@@ -640,7 +641,9 @@ static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
struct extent_buffer *read_node_slot(struct btrfs_fs_info *fs_info,
struct extent_buffer *parent, int slot)
{
+ struct extent_buffer *ret;
int level = btrfs_header_level(parent);
+
if (slot < 0)
return NULL;
if (slot >= btrfs_header_nritems(parent))
@@ -649,8 +652,19 @@ struct extent_buffer *read_node_slot(struct btrfs_fs_info *fs_info,
if (level == 0)
return NULL;
- return read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
+ ret = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
btrfs_node_ptr_generation(parent, slot));
+ if (!extent_buffer_uptodate(ret))
+ return ERR_PTR(-EIO);
+
+ if (btrfs_header_level(ret) != level - 1) {
+ error("child eb corrupted: parent bytenr=%llu item=%d parent level=%d child level=%d",
+ btrfs_header_bytenr(parent), slot,
+ btrfs_header_level(parent), btrfs_header_level(ret));
+ free_extent_buffer(ret);
+ return ERR_PTR(-EIO);
+ }
+ return ret;
}
static int balance_level(struct btrfs_trans_handle *trans,
--
2.16.1
next prev parent reply other threads:[~2018-02-09 7:44 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-09 7:44 [PATCH v2 00/10] Chunk allocator unification Qu Wenruo
2018-02-09 7:44 ` Qu Wenruo [this message]
2018-02-09 8:09 ` [PATCH v2] btrfs-progs: ctree: Add extra level check for read_node_slot() Nikolay Borisov
2018-02-09 10:02 ` Qu Wenruo
2018-02-09 7:44 ` [PATCH v2 01/10] btrfs-progs: Refactor parameter of BTRFS_MAX_DEVS() from root to fs_info Qu Wenruo
2018-02-09 7:44 ` [PATCH v2 02/10] btrfs-progs: Merge btrfs_alloc_data_chunk into btrfs_alloc_chunk Qu Wenruo
2018-02-09 7:44 ` [PATCH v2 03/10] btrfs-progs: Make btrfs_alloc_chunk to handle block group creation Qu Wenruo
2018-02-09 7:44 ` [PATCH v2 04/10] btrfs-progs: Introduce btrfs_raid_array and related infrastructures Qu Wenruo
2018-02-09 7:44 ` [PATCH v2 05/10] btrfs-progs: volumes: Allow find_free_dev_extent() to return maximum hole size Qu Wenruo
2018-02-09 7:44 ` [PATCH v2 06/10] btrfs-progs: kernel-lib: Port kernel sort() to btrfs-progs Qu Wenruo
2018-02-21 15:40 ` David Sterba
2018-02-22 1:43 ` Qu Wenruo
2018-03-09 18:14 ` David Sterba
2018-02-09 7:44 ` [PATCH v2 07/10] btrfs-progs: volumes: Unify free dev extent search behavior between kernel and btrfs-progs Qu Wenruo
2018-02-09 7:44 ` [PATCH v2 08/10] btrfs-progs: Move chunk stripe size calcution function to volumes.h Qu Wenruo
2018-02-09 7:44 ` [PATCH v2 09/10] btrfs-progs: Use btrfs_device->fs_info to replace btrfs_device->dev_root Qu Wenruo
2018-02-09 7:44 ` [PATCH v2 10/10] btrfs-progs: Refactor btrfs_alloc_chunk to mimic kernel structure and behavior Qu Wenruo
2018-02-09 8:17 ` Nikolay Borisov
2018-02-09 10:01 ` Qu Wenruo
-- strict thread matches above, loose matches on Subject: below --
2018-02-08 0:59 [PATCH v2] btrfs-progs: ctree: Add extra level check for read_node_slot() Qu Wenruo
2018-02-08 7:38 ` Nikolay Borisov
2018-03-19 12:36 ` David Sterba
2018-03-20 5:16 ` Qu Wenruo
2018-03-20 5:23 ` Qu Wenruo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180209074428.29187-2-wqu@suse.com \
--to=wqu@suse.com \
--cc=dsterba@suse.cz \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).