All of lore.kernel.org
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo@cn.fujitsu.com>
To: <linux-btrfs@vger.kernel.org>
Cc: <calestyo@scientia.net>, <chris@colorremedies.com>, <dsterba@suse.cz>
Subject: [PATCH v2 01/12] btrfs-progs: lowmem check: Fix several bugs related to afterward search
Date: Mon, 6 Feb 2017 13:47:24 +0800	[thread overview]
Message-ID: <20170206054735.5227-2-quwenruo@cn.fujitsu.com> (raw)
In-Reply-To: <20170206054735.5227-1-quwenruo@cn.fujitsu.com>

Since btrfs_search_slot() can points to the slot which is beyond the
leaves' capacity, we should pay extra attention when doing afterward
search.

While for lowmem check, several places uses afterward search:
1) Block group item used space check
2) Device item used space check
3) Data extent backref check.

In the following case for block group item check, btrfs lowmem mode
check will skip the block group and report false alert:

leaf 29405184 items 37 free space 1273 generation 11 owner 2
...
        item 36 key (77594624 EXTENT_ITEM 2097152)
                extent refs 1 gen 8 flags DATA
                extent data backref root 5 objectid 265 offset 0 count 1
leaf 29409280 items 43 free space 670 generation 11 owner 2
        item 0 key (96468992 EXTENT_ITEM 2097152)
                extent refs 1 gen 8 flags DATA
                extent data backref root 5 objectid 274 offset 0 count 1
        item 1 key (96468992 BLOCK_GROUP_ITEM 33554432)
                block group used 2265088 chunk_objectid 256 flags DATA

When checking block group item, we will search key(96468992 0 0) to
start from the first item in the block group.

While search_slot() will point to leaf 29405184, slot 37 which is beyond
leaf capacity.

And when reading key from slot 37, uninitialized data can be read out
and cause us to exit block group item check, leading to false alert.

Fix it by checking path.slot[0] before reading out the key.

Reported-by: Christoph Anton Mitterer <calestyo@scientia.net>
Reported-by: Chris Murphy <chris@colorremedies.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 cmds-check.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/cmds-check.c b/cmds-check.c
index 37e5ff18..699753fb 100644
--- a/cmds-check.c
+++ b/cmds-check.c
@@ -10591,6 +10591,8 @@ static int check_extent_data_backref(struct btrfs_fs_info *fs_info,
 		leaf = path.nodes[0];
 		slot = path.slots[0];
 
+		if (slot >= btrfs_header_nritems(leaf))
+			goto next;
 		btrfs_item_key_to_cpu(leaf, &key, slot);
 		if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
 			break;
@@ -10606,6 +10608,7 @@ static int check_extent_data_backref(struct btrfs_fs_info *fs_info,
 		    offset)
 			found_count++;
 
+next:
 		ret = btrfs_next_item(root, &path);
 		if (ret)
 			break;
@@ -10878,8 +10881,10 @@ static int check_dev_item(struct btrfs_fs_info *fs_info,
 
 	/* Iterate dev_extents to calculate the used space of a device */
 	while (1) {
-		btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
+		if (path.slots[0] >= btrfs_header_nritems(path.nodes[0]))
+			goto next;
 
+		btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
 		if (key.objectid > dev_id)
 			break;
 		if (key.type != BTRFS_DEV_EXTENT_KEY || key.objectid != dev_id)
@@ -10976,6 +10981,11 @@ static int check_block_group_item(struct btrfs_fs_info *fs_info,
 	/* Iterate extent tree to account used space */
 	while (1) {
 		leaf = path.nodes[0];
+
+		/* Search slot can point to the last item beyond leaf nritems */
+		if (path.slots[0] >= btrfs_header_nritems(leaf))
+			goto next;
+
 		btrfs_item_key_to_cpu(leaf, &extent_key, path.slots[0]);
 		if (extent_key.objectid >= bg_key.objectid + bg_key.offset)
 			break;
-- 
2.11.0




  reply	other threads:[~2017-02-06  5:47 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-06  5:47 [PATCH v2 00/12] Variant lowmem mode fixes Qu Wenruo
2017-02-06  5:47 ` Qu Wenruo [this message]
2017-02-06  5:47 ` [PATCH v2 02/12] btrfs-progs: check: Output verbose error when fsck found a bug in any tree Qu Wenruo
2017-02-06  5:47 ` [PATCH v2 03/12] btrfs-progs: lowmem check: Fix false alert in checking data extent csums Qu Wenruo
2017-02-06  5:47 ` [PATCH v2 04/12] btrfs-progs: lowmem check: Fix extent item size false alert Qu Wenruo
2017-02-06  5:47 ` [PATCH v2 05/12] btrfs-progs: lowmem check: Fix false alert on inline compressed extent Qu Wenruo
2017-02-06  5:47 ` [PATCH v2 06/12] btrfs-progs: lowmem check: Fix silent error if first inode item missing Qu Wenruo
2017-02-06  5:47 ` [PATCH v2 07/12] btrfs-progs: tests: Move fsck-tests/015 to fuzz tests Qu Wenruo
2017-02-06  5:47 ` [PATCH v2 08/12] btrfs-progs: fsck-test: Add test image for lowmem mode block group false alert Qu Wenruo
2017-02-06  5:47 ` [PATCH v2 09/12] btrfs-progs: fsck-test: Make 013 compatible with lowmem mode Qu Wenruo
2017-02-06  5:47 ` [PATCH v2 10/12] btrfs-progs: fsck-test: Add new test case for file extent false alerts Qu Wenruo
2017-02-06  5:47 ` [PATCH v2 11/12] btrfs-progs: fsck: Fix lowmem mode override to allow it skip repair work Qu Wenruo
2017-02-06  5:47 ` [PATCH v2 12/12] btrfs-progs: cmds-check.c: walk_down_tree_v2 break cause of leaf process Qu Wenruo
2017-02-06  8:51   ` 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=20170206054735.5227-2-quwenruo@cn.fujitsu.com \
    --to=quwenruo@cn.fujitsu.com \
    --cc=calestyo@scientia.net \
    --cc=chris@colorremedies.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.