Skip to contentSkip to conversation

fs: btrfs: fix possible use-after-free bug in error handling code of btrfs_get_root_ref()

PATCH In review Jia-Ju Bai 3 participants 6 messages Mar 24, 2022 – Aug 23, 2023 Btrfs file system

lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH 1/1 fs: btrfs: fix possible use-after-free bug in error handling code of btrfs_get_root_ref()

In btrfs_get_root_ref(), when btrfs_insert_fs_root() fails, btrfs_put_root() will be called to possibly free the memory area of the variable root. However, this variable is then used again in error handling code after "goto fail", when ret is not -EEXIST.

To fix this possible bug, btrfs_put_root() is only called when ret is -EEXIST for "goto again".

---
 fs/btrfs/disk-io.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/btrfs/disk-io.c +3 -2
index b30309f187cf..126f244cdf88 100644
@@ -1850,9 +1850,10 @@ static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info,
 
 	ret = btrfs_insert_fs_root(fs_info, root);
 	if (ret) {
-		btrfs_put_root(root);
-		if (ret == -EEXIST)
+		if (ret == -EEXIST) {
+			btrfs_put_root(root);
 			goto again;
+		}
 		goto fail;
 	}
 	return root;
Signature · 1 line
2.17.1
#1 lore raw
Quoted 19 lines · Jia-Ju Bai

In btrfs_get_root_ref(), when btrfs_insert_fs_root() fails, btrfs_put_root() will be called to possibly free the memory area of the variable root. However, this variable is then used again in error handling code after "goto fail", when ret is not -EEXIST.

To fix this possible bug, btrfs_put_root() is only called when ret is -EEXIST for "goto again".

---
 fs/btrfs/disk-io.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/btrfs/disk-io.c +0 -0
index b30309f187cf..126f244cdf88 100644

fs/btrfs/disk-io.c static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, · line 1854

Quoted 7 lines
fs/btrfs/disk-io.c +2 -2
@@ -1850,9 +1850,10 @@ static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info,
 
 	ret = btrfs_insert_fs_root(fs_info, root);
 	if (ret) {
-		btrfs_put_root(root);
-		if (ret == -EEXIST)
+		if (ret == -EEXIST) {
+			btrfs_put_root(root);

I think this fix is correct, though it's not that clear. If you look how the code changed, there was the unconditional put and then followed by a free:

8c38938c7bb0 ("btrfs: move the root freeing stuff into btrfs_put_root")

Here it's putting twice where one will be the final free.

And then the whole refcounting gets updated in

4785e24fa5d2 ("btrfs: don't take an extra root ref at allocation time")

which could be removing the wrong put, I'm not yet sure.

author
#2 lore raw
Quoted 33 lines · David Sterba
Quoted 19 lines · Jia-Ju Bai

In btrfs_get_root_ref(), when btrfs_insert_fs_root() fails, btrfs_put_root() will be called to possibly free the memory area of the variable root. However, this variable is then used again in error handling code after "goto fail", when ret is not -EEXIST.

To fix this possible bug, btrfs_put_root() is only called when ret is -EEXIST for "goto again".

---
  fs/btrfs/disk-io.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
fs/btrfs/disk-io.c +0 -0
index b30309f187cf..126f244cdf88 100644

fs/btrfs/disk-io.c static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, · line 1854

Quoted 7 lines
fs/btrfs/disk-io.c +2 -2
@@ -1850,9 +1850,10 @@ static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info,
 
  	ret = btrfs_insert_fs_root(fs_info, root);
  	if (ret) {
-		btrfs_put_root(root);
-		if (ret == -EEXIST)
+		if (ret == -EEXIST) {
+			btrfs_put_root(root);

I think this fix is correct, though it's not that clear. If you look how the code changed, there was the unconditional put and then followed by a free:

8c38938c7bb0 ("btrfs: move the root freeing stuff into btrfs_put_root")

Quoted 7 lines

Here it's putting twice where one will be the final free.

And then the whole refcounting gets updated in

4785e24fa5d2 ("btrfs: don't take an extra root ref at allocation time")

which could be removing the wrong put, I'm not yet sure.

Thanks for the reply!

I think the bug should be introduced by this commit:
bc44d7c4b2b1 ("btrfs: push btrfs_grab_fs_root into btrfs_get_fs_root")

This commit has a change:
     ret = btrfs_insert_fs_root(fs_info, root);
     if (ret) {
+      btrfs_put_fs_root(root);
         if (ret == -EEXIST) {
             btrfs_free_fs_root(root);
             goto again;
         }

I could add a Fixes tag of this commit in my V2 patch. Is it okay?

Best wishes,
Jia-Ju Bai

#3 lore raw
Quoted 36 lines · Jia-Ju Bai
Quoted 14 lines

fs/btrfs/disk-io.c static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, · line 1854

Quoted 8 lines
fs/btrfs/disk-io.c +2 -2
@@ -1850,9 +1850,10 @@ static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info,
 
  	ret = btrfs_insert_fs_root(fs_info, root);
  	if (ret) {
-		btrfs_put_root(root);
-		if (ret == -EEXIST)
+		if (ret == -EEXIST) {
+			btrfs_put_root(root);

I think this fix is correct, though it's not that clear. If you look how the code changed, there was the unconditional put and then followed by a free:

8c38938c7bb0 ("btrfs: move the root freeing stuff into btrfs_put_root")

Quoted 7 lines

Here it's putting twice where one will be the final free.

And then the whole refcounting gets updated in

4785e24fa5d2 ("btrfs: don't take an extra root ref at allocation time")

which could be removing the wrong put, I'm not yet sure.

Thanks for the reply!

I think the bug should be introduced by this commit:
bc44d7c4b2b1 ("btrfs: push btrfs_grab_fs_root into btrfs_get_fs_root")

This commit has a change:
     ret = btrfs_insert_fs_root(fs_info, root);
     if (ret) {
+      btrfs_put_fs_root(root);
         if (ret == -EEXIST) {
             btrfs_free_fs_root(root);
             goto again;
         }

Quoted 2 lines

I could add a Fixes tag of this commit in my V2 patch. Is it okay?

I can add it myself, that's a minor thing. The fix is correct, I've rewritten the changelog a bit, patch now added to misc-next, thanks.

#4 lore raw
Quoted 37 lines · David Sterba
Quoted 36 lines · Jia-Ju Bai
Quoted 14 lines

fs/btrfs/disk-io.c static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, · line 1854

Quoted 8 lines
fs/btrfs/disk-io.c +2 -2
@@ -1850,9 +1850,10 @@ static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info,
 
  	ret = btrfs_insert_fs_root(fs_info, root);
  	if (ret) {
-		btrfs_put_root(root);
-		if (ret == -EEXIST)
+		if (ret == -EEXIST) {
+			btrfs_put_root(root);

I think this fix is correct, though it's not that clear. If you look how the code changed, there was the unconditional put and then followed by a free:

8c38938c7bb0 ("btrfs: move the root freeing stuff into btrfs_put_root")

Quoted 7 lines

Here it's putting twice where one will be the final free.

And then the whole refcounting gets updated in

4785e24fa5d2 ("btrfs: don't take an extra root ref at allocation time")

which could be removing the wrong put, I'm not yet sure.

Thanks for the reply!

I think the bug should be introduced by this commit:
bc44d7c4b2b1 ("btrfs: push btrfs_grab_fs_root into btrfs_get_fs_root")

This commit has a change:
     ret = btrfs_insert_fs_root(fs_info, root);
     if (ret) {
+      btrfs_put_fs_root(root);
         if (ret == -EEXIST) {
             btrfs_free_fs_root(root);
             goto again;
         }

Quoted 5 lines
Quoted 2 lines

I could add a Fixes tag of this commit in my V2 patch. Is it okay?

I can add it myself, that's a minor thing. The fix is correct, I've rewritten the changelog a bit, patch now added to misc-next, thanks.

Where is 'misc-next' please?

Signature · 1 line
Lee Jones [李琼斯]
#5 lore raw
Quoted 39 lines · Lee Jones
Quoted 37 lines · David Sterba
Quoted 36 lines · Jia-Ju Bai
Quoted 14 lines

fs/btrfs/disk-io.c static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info, · line 1854

Quoted 8 lines
fs/btrfs/disk-io.c +2 -2
@@ -1850,9 +1850,10 @@ static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info,
 
  	ret = btrfs_insert_fs_root(fs_info, root);
  	if (ret) {
-		btrfs_put_root(root);
-		if (ret == -EEXIST)
+		if (ret == -EEXIST) {
+			btrfs_put_root(root);

I think this fix is correct, though it's not that clear. If you look how the code changed, there was the unconditional put and then followed by a free:

8c38938c7bb0 ("btrfs: move the root freeing stuff into btrfs_put_root")

Quoted 7 lines

Here it's putting twice where one will be the final free.

And then the whole refcounting gets updated in

4785e24fa5d2 ("btrfs: don't take an extra root ref at allocation time")

which could be removing the wrong put, I'm not yet sure.

Thanks for the reply!

I think the bug should be introduced by this commit:
bc44d7c4b2b1 ("btrfs: push btrfs_grab_fs_root into btrfs_get_fs_root")

This commit has a change:
     ret = btrfs_insert_fs_root(fs_info, root);
     if (ret) {
+      btrfs_put_fs_root(root);
         if (ret == -EEXIST) {
             btrfs_free_fs_root(root);
             goto again;
         }

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

I could add a Fixes tag of this commit in my V2 patch. Is it okay?

I can add it myself, that's a minor thing. The fix is correct, I've rewritten the changelog a bit, patch now added to misc-next, thanks.

Where is 'misc-next' please?

Nevermind. I've just seen a) how old this thread is and b) the commit subject was changed and subsequently committed as:

168a2f776b976 ("btrfs: fix root ref counts in error handling in btrfs_get_root_ref")

Signature · 1 line
Lee Jones [李琼斯]