Skip to contentSkip to conversation

btrfs: relocation: Only remove reloc rb_trees if reloc control has been initialized

PATCH In review Qu Wenruo 4 participants 7 messages Jul 2, 2018 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/1 btrfs: relocation: Only remove reloc rb_trees if reloc control has been initialized

Reported in https://bugzilla.kernel.org/show_bug.cgi?id=199833, where an invalid tree reloc tree can cause kernel NULL pointer dereference when btrfs does some cleanup for reloc roots.

It turns out that fs_info->reloc_ctl can be NULL in btrfs_recover_relocation() as we allocate relocation control after all reloc roots are verified.
So when we hit out: tag, we haven't call set_reloc_control() thus fs_info->reloc_ctl is still NULL.

---
 fs/btrfs/relocation.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
fs/btrfs/relocation.c +12 -11
index 879b76fa881a..be94c65bb4d2 100644
@@ -1321,18 +1321,19 @@ static void __del_reloc_root(struct btrfs_root *root)
 	struct mapping_node *node = NULL;
 	struct reloc_control *rc = fs_info->reloc_ctl;
 
-	spin_lock(&rc->reloc_root_tree.lock);
-	rb_node = tree_search(&rc->reloc_root_tree.rb_root,
-			      root->node->start);
-	if (rb_node) {
-		node = rb_entry(rb_node, struct mapping_node, rb_node);
-		rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
+	if (rc) {
+		spin_lock(&rc->reloc_root_tree.lock);
+		rb_node = tree_search(&rc->reloc_root_tree.rb_root,
+				      root->node->start);
+		if (rb_node) {
+			node = rb_entry(rb_node, struct mapping_node, rb_node);
+			rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
+		}
+		spin_unlock(&rc->reloc_root_tree.lock);
+		if (!node)
+			return;
+		BUG_ON((struct btrfs_root *)node->data != root);
 	}
-	spin_unlock(&rc->reloc_root_tree.lock);
-
-	if (!node)
-		return;
-	BUG_ON((struct btrfs_root *)node->data != root);
 
 	spin_lock(&fs_info->trans_lock);
 	list_del_init(&root->root_list);
Signature · 1 line
2.18.0
#1 lore raw
Quoted 21 lines · Qu Wenruo

Reported in https://bugzilla.kernel.org/show_bug.cgi?id=199833, where an invalid tree reloc tree can cause kernel NULL pointer dereference when btrfs does some cleanup for reloc roots.

It turns out that fs_info->reloc_ctl can be NULL in btrfs_recover_relocation() as we allocate relocation control after all reloc roots are verified.
So when we hit out: tag, we haven't call set_reloc_control() thus fs_info->reloc_ctl is still NULL.

---
 fs/btrfs/relocation.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
fs/btrfs/relocation.c +0 -0
index 879b76fa881a..be94c65bb4d2 100644

fs/btrfs/relocation.c static void __del_reloc_root(struct btrfs_root *root) · line 1328

Quoted 8 lines
fs/btrfs/relocation.c +0 -5
@@ -1321,18 +1321,19 @@ static void __del_reloc_root(struct btrfs_root *root)
 	struct mapping_node *node = NULL;
 	struct reloc_control *rc = fs_info->reloc_ctl;
 > -	spin_lock(&rc->reloc_root_tree.lock);
-	rb_node = tree_search(&rc->reloc_root_tree.rb_root,
-			      root->node->start);
-	if (rb_node) {
-		node = rb_entry(rb_node, struct mapping_node, rb_node);
-		rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);

Just do if (!rc)

                return;

The function is simple enough, no need to indent multiple lines.

Quoted 22 lines
fs/btrfs/relocation.c +12 -5
+	if (rc) {
+		spin_lock(&rc->reloc_root_tree.lock);
+		rb_node = tree_search(&rc->reloc_root_tree.rb_root,
+				      root->node->start);
+		if (rb_node) {
+			node = rb_entry(rb_node, struct mapping_node, rb_node);
+			rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
+		}
+		spin_unlock(&rc->reloc_root_tree.lock);
+		if (!node)
+			return;
+		BUG_ON((struct btrfs_root *)node->data != root);
 	}
-	spin_unlock(&rc->reloc_root_tree.lock);
-
-	if (!node)
-		return;
-	BUG_ON((struct btrfs_root *)node->data != root);
 
 	spin_lock(&fs_info->trans_lock);
 	list_del_init(&root->root_list);
#2 lore raw
Quoted 34 lines · Nikolay Borisov
Quoted 21 lines · Qu Wenruo

Reported in https://bugzilla.kernel.org/show_bug.cgi?id=199833, where an invalid tree reloc tree can cause kernel NULL pointer dereference when btrfs does some cleanup for reloc roots.

It turns out that fs_info->reloc_ctl can be NULL in btrfs_recover_relocation() as we allocate relocation control after all reloc roots are verified.
So when we hit out: tag, we haven't call set_reloc_control() thus fs_info->reloc_ctl is still NULL.

---
 fs/btrfs/relocation.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
fs/btrfs/relocation.c +0 -0
index 879b76fa881a..be94c65bb4d2 100644

fs/btrfs/relocation.c static void __del_reloc_root(struct btrfs_root *root) · line 1328

Quoted 8 lines
fs/btrfs/relocation.c +0 -5
@@ -1321,18 +1321,19 @@ static void __del_reloc_root(struct btrfs_root *root)
 	struct mapping_node *node = NULL;
 	struct reloc_control *rc = fs_info->reloc_ctl;
 > -	spin_lock(&rc->reloc_root_tree.lock);
-	rb_node = tree_search(&rc->reloc_root_tree.rb_root,
-			      root->node->start);
-	if (rb_node) {
-		node = rb_entry(rb_node, struct mapping_node, rb_node);
-		rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);

Just do if (!rc)

Quoted 3 lines
                return;

The function is simple enough, no need to indent multiple lines.

You missed serval lines below, we still have:
---

        spin_lock(&fs_info->trans_lock);
        list_del_init(&root->root_list);
        spin_unlock(&fs_info->trans_lock);
        kfree(node);

---

Which still needs to be called even rc is not initialized.

Thanks,
Qu

Quoted 27 lines
Quoted 12 lines
fs/btrfs/relocation.c +11 -0
+	if (rc) {
+		spin_lock(&rc->reloc_root_tree.lock);
+		rb_node = tree_search(&rc->reloc_root_tree.rb_root,
+				      root->node->start);
+		if (rb_node) {
+			node = rb_entry(rb_node, struct mapping_node, rb_node);
+			rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
+		}
+		spin_unlock(&rc->reloc_root_tree.lock);
+		if (!node)
+			return;

fs/btrfs/relocation.c static void __del_reloc_root(struct btrfs_root *root) · line 1339

Quoted 10 lines
fs/btrfs/relocation.c +1 -5
+		BUG_ON((struct btrfs_root *)node->data != root);
 	}
-	spin_unlock(&rc->reloc_root_tree.lock);
-
-	if (!node)
-		return;
-	BUG_ON((struct btrfs_root *)node->data != root);
 
 	spin_lock(&fs_info->trans_lock);
 	list_del_init(&root->root_list);

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 43 lines · Qu Wenruo
Quoted 34 lines · Nikolay Borisov
Quoted 21 lines · Qu Wenruo

Reported in https://bugzilla.kernel.org/show_bug.cgi?id=199833, where an invalid tree reloc tree can cause kernel NULL pointer dereference when btrfs does some cleanup for reloc roots.

It turns out that fs_info->reloc_ctl can be NULL in btrfs_recover_relocation() as we allocate relocation control after all reloc roots are verified.
So when we hit out: tag, we haven't call set_reloc_control() thus fs_info->reloc_ctl is still NULL.

---
 fs/btrfs/relocation.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
fs/btrfs/relocation.c +0 -0
index 879b76fa881a..be94c65bb4d2 100644

fs/btrfs/relocation.c static void __del_reloc_root(struct btrfs_root *root) · line 1328

Quoted 8 lines
fs/btrfs/relocation.c +0 -5
@@ -1321,18 +1321,19 @@ static void __del_reloc_root(struct btrfs_root *root)
 	struct mapping_node *node = NULL;
 	struct reloc_control *rc = fs_info->reloc_ctl;
 > -	spin_lock(&rc->reloc_root_tree.lock);
-	rb_node = tree_search(&rc->reloc_root_tree.rb_root,
-			      root->node->start);
-	if (rb_node) {
-		node = rb_entry(rb_node, struct mapping_node, rb_node);
-		rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);

Just do if (!rc)

Quoted 3 lines
                return;

The function is simple enough, no need to indent multiple lines.

You missed serval lines below, we still have:
---

Quoted 7 lines
        spin_lock(&fs_info->trans_lock);
        list_del_init(&root->root_list);
        spin_unlock(&fs_info->trans_lock);
        kfree(node);

---

Which still needs to be called even rc is not initialized.

But then isn't the function buggy even with your patch because if node is not initialised then we exit at if (!node) return.

Quoted 32 lines

Thanks,
Qu

Quoted 27 lines
Quoted 12 lines
fs/btrfs/relocation.c +11 -0
+	if (rc) {
+		spin_lock(&rc->reloc_root_tree.lock);
+		rb_node = tree_search(&rc->reloc_root_tree.rb_root,
+				      root->node->start);
+		if (rb_node) {
+			node = rb_entry(rb_node, struct mapping_node, rb_node);
+			rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
+		}
+		spin_unlock(&rc->reloc_root_tree.lock);
+		if (!node)
+			return;

fs/btrfs/relocation.c static void __del_reloc_root(struct btrfs_root *root) · line 1339

Quoted 10 lines
fs/btrfs/relocation.c +1 -5
+		BUG_ON((struct btrfs_root *)node->data != root);
 	}
-	spin_unlock(&rc->reloc_root_tree.lock);
-
-	if (!node)
-		return;
-	BUG_ON((struct btrfs_root *)node->data != root);
 
 	spin_lock(&fs_info->trans_lock);
 	list_del_init(&root->root_list);

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw
Quoted 52 lines · Nikolay Borisov
Quoted 49 lines · Qu Wenruo
Quoted 34 lines · Nikolay Borisov
Quoted 21 lines · Qu Wenruo

Reported in https://bugzilla.kernel.org/show_bug.cgi?id=199833, where an invalid tree reloc tree can cause kernel NULL pointer dereference when btrfs does some cleanup for reloc roots.

It turns out that fs_info->reloc_ctl can be NULL in btrfs_recover_relocation() as we allocate relocation control after all reloc roots are verified.
So when we hit out: tag, we haven't call set_reloc_control() thus fs_info->reloc_ctl is still NULL.

---
 fs/btrfs/relocation.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
fs/btrfs/relocation.c +0 -0
index 879b76fa881a..be94c65bb4d2 100644

fs/btrfs/relocation.c static void __del_reloc_root(struct btrfs_root *root) · line 1328

Quoted 8 lines
fs/btrfs/relocation.c +0 -5
@@ -1321,18 +1321,19 @@ static void __del_reloc_root(struct btrfs_root *root)
 	struct mapping_node *node = NULL;
 	struct reloc_control *rc = fs_info->reloc_ctl;
 > -	spin_lock(&rc->reloc_root_tree.lock);
-	rb_node = tree_search(&rc->reloc_root_tree.rb_root,
-			      root->node->start);
-	if (rb_node) {
-		node = rb_entry(rb_node, struct mapping_node, rb_node);
-		rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);

Just do if (!rc)

Quoted 3 lines
                return;

The function is simple enough, no need to indent multiple lines.

You missed serval lines below, we still have:
---

        spin_lock(&fs_info->trans_lock);
        list_del_init(&root->root_list);
        spin_unlock(&fs_info->trans_lock);
        kfree(node);

---

Quoted 4 lines
Quoted 1 line

Which still needs to be called even rc is not initialized.

But then isn't the function buggy even with your patch because if node is not initialised then we exit at if (!node) return.

Ah, so you've moved the if (!node) check inside the if (rc) branch. Then it will work as you said. Fair enough.

Quoted 34 lines
Quoted 34 lines

Thanks,
Qu

Quoted 27 lines
Quoted 12 lines
fs/btrfs/relocation.c +11 -0
+	if (rc) {
+		spin_lock(&rc->reloc_root_tree.lock);
+		rb_node = tree_search(&rc->reloc_root_tree.rb_root,
+				      root->node->start);
+		if (rb_node) {
+			node = rb_entry(rb_node, struct mapping_node, rb_node);
+			rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
+		}
+		spin_unlock(&rc->reloc_root_tree.lock);
+		if (!node)
+			return;

fs/btrfs/relocation.c static void __del_reloc_root(struct btrfs_root *root) · line 1339

Quoted 10 lines
fs/btrfs/relocation.c +1 -5
+		BUG_ON((struct btrfs_root *)node->data != root);
 	}
-	spin_unlock(&rc->reloc_root_tree.lock);
-
-	if (!node)
-		return;
-	BUG_ON((struct btrfs_root *)node->data != root);
 
 	spin_lock(&fs_info->trans_lock);
 	list_del_init(&root->root_list);

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
In reply to Nikolay Borisov
Quoted 52 lines · Nikolay Borisov
Quoted 49 lines · Qu Wenruo
Quoted 34 lines · Nikolay Borisov
Quoted 21 lines · Qu Wenruo

Reported in https://bugzilla.kernel.org/show_bug.cgi?id=199833, where an invalid tree reloc tree can cause kernel NULL pointer dereference when btrfs does some cleanup for reloc roots.

It turns out that fs_info->reloc_ctl can be NULL in btrfs_recover_relocation() as we allocate relocation control after all reloc roots are verified.
So when we hit out: tag, we haven't call set_reloc_control() thus fs_info->reloc_ctl is still NULL.

---
 fs/btrfs/relocation.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
fs/btrfs/relocation.c +0 -0
index 879b76fa881a..be94c65bb4d2 100644

fs/btrfs/relocation.c static void __del_reloc_root(struct btrfs_root *root) · line 1328

Quoted 8 lines
fs/btrfs/relocation.c +0 -5
@@ -1321,18 +1321,19 @@ static void __del_reloc_root(struct btrfs_root *root)
 	struct mapping_node *node = NULL;
 	struct reloc_control *rc = fs_info->reloc_ctl;
 > -	spin_lock(&rc->reloc_root_tree.lock);
-	rb_node = tree_search(&rc->reloc_root_tree.rb_root,
-			      root->node->start);
-	if (rb_node) {
-		node = rb_entry(rb_node, struct mapping_node, rb_node);
-		rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);

Just do if (!rc)

Quoted 3 lines
                return;

The function is simple enough, no need to indent multiple lines.

You missed serval lines below, we still have:
---

        spin_lock(&fs_info->trans_lock);
        list_del_init(&root->root_list);
        spin_unlock(&fs_info->trans_lock);
        kfree(node);

---

Quoted 4 lines
Quoted 1 line

Which still needs to be called even rc is not initialized.

But then isn't the function buggy even with your patch because if node is not initialised then we exit at if (!node) return.

That means node->data isn't initialized nor its root->root_list.

The patch only needs to call list_del_init() if @rc is not initialized.

Thanks,
Qu

Quoted 37 lines
Quoted 32 lines

Thanks,
Qu

Quoted 27 lines
Quoted 12 lines
fs/btrfs/relocation.c +11 -0
+	if (rc) {
+		spin_lock(&rc->reloc_root_tree.lock);
+		rb_node = tree_search(&rc->reloc_root_tree.rb_root,
+				      root->node->start);
+		if (rb_node) {
+			node = rb_entry(rb_node, struct mapping_node, rb_node);
+			rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
+		}
+		spin_unlock(&rc->reloc_root_tree.lock);
+		if (!node)
+			return;

fs/btrfs/relocation.c static void __del_reloc_root(struct btrfs_root *root) · line 1339

Quoted 10 lines
fs/btrfs/relocation.c +1 -5
+		BUG_ON((struct btrfs_root *)node->data != root);
 	}
-	spin_unlock(&rc->reloc_root_tree.lock);
-
-	if (!node)
-		return;
-	BUG_ON((struct btrfs_root *)node->data != root);
 
 	spin_lock(&fs_info->trans_lock);
 	list_del_init(&root->root_list);

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw
In reply to Qu Wenruo
Quoted 4 lines · Qu Wenruo

Reported in https://bugzilla.kernel.org/show_bug.cgi?id=199833, where an invalid tree reloc tree can cause kernel NULL pointer dereference when btrfs does some cleanup for reloc roots.

Quoted 8 lines

It turns out that fs_info->reloc_ctl can be NULL in btrfs_recover_relocation() as we allocate relocation control after all reloc roots are verified.
So when we hit out: tag, we haven't call set_reloc_control() thus fs_info->reloc_ctl is still NULL.

Thanks for the fix, patch added to the queue. I've added the fuzzed image from bugzilla to btrfs-progs.