Skip to contentSkip to conversation

Btrfs: dereferencing free'd memory in panic message

PATCH In review Dan Carpenter 3 participants 9 messages Jun 22, 2012 – Jun 26, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: dereferencing free'd memory in panic message

We free "node" and then dereference it in the panic message on the next line. I considered moving the kfree() after the panic given that panic can return under certain configurations, but in the end I decided it doesn't matter if we leak a bit after a panic.

fs/btrfs/relocation.c +0 -1
index 790f492..c50d80a 100644
@@ -1239,7 +1239,6 @@ static int __must_check __add_reloc_root(struct btrfs_root *root)
 			      node->bytenr, &node->rb_node);
 	spin_unlock(&rc->reloc_root_tree.lock);
 	if (rb_node) {
-		kfree(node);
 		btrfs_panic(root->fs_info, -EEXIST, "Duplicate root found "
 			    "for start=%llu while inserting into relocation "
 			    "tree\n", node->bytenr);
#1 lore raw
Quoted 12 lines · Dan Carpenter

We free "node" and then dereference it in the panic message on the next line. I considered moving the kfree() after the panic given that panic can return under certain configurations, but in the end I decided it doesn't matter if we leak a bit after a panic.

fs/btrfs/relocation.c +0 -0
index 790f492..c50d80a 100644

fs/btrfs/relocation.c static int __must_check __add_reloc_root(struct btrfs_root *root) · line 1244

Quoted 7 lines
fs/btrfs/relocation.c +0 -1
@@ -1239,7 +1239,6 @@ static int __must_check __add_reloc_root(struct btrfs_root *root)
  			      node->bytenr, &node->rb_node);
  	spin_unlock(&rc->reloc_root_tree.lock);
  	if (rb_node) {
-		kfree(node);
  		btrfs_panic(root->fs_info, -EEXIST, "Duplicate root found "
  			    "for start=%llu while inserting into relocation "
  			    "tree\n", node->bytenr);

Except btrfs_panic can not panic the box if it's mounted to not panic on errors, so we still need to do the kfree afterwards. Thanks,

Josef

#2 lore raw
Quoted 8 lines · Josef Bacik
Quoted 7 lines · Dan Carpenter

We free "node" and then dereference it in the panic message on the next line. I considered moving the kfree() after the panic given that panic can return under certain configurations, but in the end I decided it doesn't matter if we leak a bit after a panic.

Quoted 16 lines

fs/btrfs/relocation.c static int __must_check __add_reloc_root(struct btrfs_root *root) · line 1244

Quoted 12 lines
fs/btrfs/relocation.c +0 -1
index 790f492..c50d80a 100644
@@ -1239,7 +1239,6 @@ static int __must_check __add_reloc_root(struct btrfs_root *root)
 			      node->bytenr, &node->rb_node);
 	spin_unlock(&rc->reloc_root_tree.lock);
 	if (rb_node) {
-		kfree(node);
 		btrfs_panic(root->fs_info, -EEXIST, "Duplicate root found "
 			    "for start=%llu while inserting into relocation "
 			    "tree\n", node->bytenr);

Except btrfs_panic can not panic the box if it's mounted to not panic on errors, so we still need to do the kfree afterwards. Thanks,

Right. I mentioned that in my change log, but I figured a one time memory leak was the least of our concerns in that case. I will resend. This should probably return -EEXIST here as well yes?

regards,
dan carpenter

#3 lore raw
Quoted 26 lines · Dan Carpenter
Quoted 8 lines · Josef Bacik
Quoted 7 lines · Dan Carpenter

We free "node" and then dereference it in the panic message on the next line. I considered moving the kfree() after the panic given that panic can return under certain configurations, but in the end I decided it doesn't matter if we leak a bit after a panic.

Quoted 16 lines

fs/btrfs/relocation.c static int __must_check __add_reloc_root(struct btrfs_root *root) · line 1244

Quoted 12 lines
fs/btrfs/relocation.c +0 -1
index 790f492..c50d80a 100644
@@ -1239,7 +1239,6 @@ static int __must_check __add_reloc_root(struct btrfs_root *root)
  			      node->bytenr, &node->rb_node);
  	spin_unlock(&rc->reloc_root_tree.lock);
  	if (rb_node) {
-		kfree(node);
  		btrfs_panic(root->fs_info, -EEXIST, "Duplicate root found "
  			    "for start=%llu while inserting into relocation "
  			    "tree\n", node->bytenr);

Except btrfs_panic can not panic the box if it's mounted to not panic on errors, so we still need to do the kfree afterwards. Thanks,

Quoted 4 lines

Right. I mentioned that in my change log, but I figured a one time memory leak was the least of our concerns in that case. I will resend. This should probably return -EEXIST here as well yes?

Ah sorry I thought you were talking about BUG_ON() not actually stopping the box in some cases. But yes I'd like to not leak anyway and return -EEXIST. Thanks,

Josef

#4 lore raw

PATCH v2 1/1 Btrfs: fix error handling in __add_reloc_root()

We dereferenced "node" in the error message after freeing it. Also btrfs_panic() can return so we should return an error code instead of continuing.

---
v2: in the first version I just deleted the kfree().

fs/btrfs/relocation.c +2 -1
index 790f492..4da0865 100644
@@ -1239,10 +1239,11 @@ static int __must_check __add_reloc_root(struct btrfs_root *root)
 			      node->bytenr, &node->rb_node);
 	spin_unlock(&rc->reloc_root_tree.lock);
 	if (rb_node) {
-		kfree(node);
 		btrfs_panic(root->fs_info, -EEXIST, "Duplicate root found "
 			    "for start=%llu while inserting into relocation "
 			    "tree\n", node->bytenr);
+		kfree(node);
+		return -EEXIST;
 	}
 
 	list_add_tail(&root->root_list, &rc->reloc_roots);
#5 lore raw

Re: [patch v2] Btrfs: fix error handling in __add_reloc_root()

Quoted 15 lines · Dan Carpenter

We dereferenced "node" in the error message after freeing it. Also btrfs_panic() can return so we should return an error code instead of continuing.

---
v2: in the first version I just deleted the kfree().

fs/btrfs/relocation.c +0 -0
index 790f492..4da0865 100644
@@ -1239,10 +1239,11 @@ static int __must_check __add_reloc_root(struct btrfs_root *root)
 			      node->bytenr, &node->rb_node);
 	spin_unlock(&rc->reloc_root_tree.lock);

fs/btrfs/relocation.c static int __must_check __add_reloc_root(struct btrfs_root *root) · line 1249

Quoted 10 lines
fs/btrfs/relocation.c +2 -1
 	if (rb_node) {
-		kfree(node);
 		btrfs_panic(root->fs_info, -EEXIST, "Duplicate root found "
 			    "for start=%llu while inserting into relocation "
 			    "tree\n", node->bytenr);
+		kfree(node);
+		return -EEXIST;
 	}
 
 	list_add_tail(&root->root_list, &rc->reloc_roots);

I'm not sure why but it seems like this patch is wrapped. At first I thought it was thunderbird but then I opened it up and saved it from mutt and I got the same problem. I will fix it up, but you may want to check your mail client. Thanks,

Josef

#6 lore raw

Re: [patch v2] Btrfs: fix error handling in __add_reloc_root()

Quoted 25 lines · Josef Bacik
Quoted 24 lines · Dan Carpenter

We dereferenced "node" in the error message after freeing it. Also btrfs_panic() can return so we should return an error code instead of continuing.

---
v2: in the first version I just deleted the kfree().

fs/btrfs/relocation.c +2 -1
index 790f492..4da0865 100644
@@ -1239,10 +1239,11 @@ static int __must_check __add_reloc_root(struct btrfs_root *root)
 			      node->bytenr, &node->rb_node);
 	spin_unlock(&rc->reloc_root_tree.lock);
 	if (rb_node) {
-		kfree(node);
 		btrfs_panic(root->fs_info, -EEXIST, "Duplicate root found "
 			    "for start=%llu while inserting into relocation "
 			    "tree\n", node->bytenr);
+		kfree(node);
+		return -EEXIST;
 	}
 
Quoted 5 lines

fs/btrfs/relocation.c static int __must_check __add_reloc_root(struct btrfs_root *root) · line 1249

Quoted 1 line
fs/btrfs/relocation.c +0 -0
 	list_add_tail(&root->root_list, &rc->reloc_roots);

I'm not sure why but it seems like this patch is wrapped. At first I thought it was thunderbird but then I opened it up and saved it from mutt and I got the same problem. I will fix it up, but you may want to check your mail client.

The message that went through to kernel-janitors applies fine for me. I'm not sure what to say.

regards,
dan carpenter

#7 lore raw

Re: [patch v2] Btrfs: fix error handling in __add_reloc_root()

I am also facing similar issue while applying this patch.

[santosh@localhost linux-next]$ sudo git am mail_Dan.txt Patch format detection failed. [santosh@localhost linux-next]$

regards
santosh

Quoted 42 lines · Dan Carpenter
Quoted 25 lines · Josef Bacik
Quoted 24 lines · Dan Carpenter

We dereferenced "node" in the error message after freeing it.  Also btrfs_panic() can return so we should return an error code instead of continuing.

---
v2: in the first version I just deleted the kfree().

fs/btrfs/relocation.c +0 -1
index 790f492..4da0865 100644
@@ -1239,10 +1239,11 @@ static int __must_check __add_reloc_root(struct btrfs_root *root)
                           node->bytenr, &node->rb_node);
     spin_unlock(&rc->reloc_root_tree.lock);
     if (rb_node) {
-           kfree(node);

            btrfs_panic(root->fs_info, -EEXIST, "Duplicate root found " "for start=%llu while inserting into relocation " "tree\n", node->bytenr);
+           kfree(node);
+           return -EEXIST;
    }

Quoted 5 lines
Quoted 1 line

    list_add_tail(&root->root_list, &rc->reloc_roots);

I'm not sure why but it seems like this patch is wrapped.  At first I thought it was thunderbird but then I opened it up and saved it from mutt and I got the same problem.  I will fix it up, but you may want to check your mail client.

The message that went through to kernel-janitors applies fine for me.  I'm not sure what to say.

regards,
dan carpenter

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

#8 lore raw

Re: [patch v2] Btrfs: fix error handling in __add_reloc_root()

Quoted 6 lines · santosh prasad nayak

I am also facing similar issue while applying this patch.

[santosh@localhost linux-next]$ sudo git am mail_Dan.txt Patch format detection failed. [santosh@localhost linux-next]$

The problem is not on my end. It made it to marc.info without getting corrupted. Marc.info strips out the From: and Subject:
headers so you'd have to add them in manually.

1) Save this file:
http://marc.info/?l=kernel-janitors&m4062314509635&q=raw

2) Edit the file and add these two lines add the top:

From: Dan Carpenter <[email protected]>
Subject: [patch v2] Btrfs: fix error handling in __add_reloc_root()

3) $ git am /home/dcarpenter/tmp/html2/btrfs_raw.txt
Applying: Btrfs: fix error handling in __add_reloc_root()

regards,
dan carpenter