linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs: btrfs: fix possible use-after-free bug in error handling code of btrfs_get_root_ref()
@ 2022-03-24 13:44 Jia-Ju Bai
  2022-03-24 18:19 ` David Sterba
  0 siblings, 1 reply; 6+ messages in thread
From: Jia-Ju Bai @ 2022-03-24 13:44 UTC (permalink / raw)
  To: clm, josef, dsterba; +Cc: linux-btrfs, linux-kernel, 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".

Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
---
 fs/btrfs/disk-io.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index b30309f187cf..126f244cdf88 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -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;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] fs: btrfs: fix possible use-after-free bug in error handling code of btrfs_get_root_ref()
  2022-03-24 13:44 [PATCH] fs: btrfs: fix possible use-after-free bug in error handling code of btrfs_get_root_ref() Jia-Ju Bai
@ 2022-03-24 18:19 ` David Sterba
  2022-03-25  8:04   ` Jia-Ju Bai
  0 siblings, 1 reply; 6+ messages in thread
From: David Sterba @ 2022-03-24 18:19 UTC (permalink / raw)
  To: Jia-Ju Bai; +Cc: clm, josef, dsterba, linux-btrfs, linux-kernel

On Thu, Mar 24, 2022 at 06:44:54AM -0700, Jia-Ju Bai wrote:
> 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".
> 
> Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
> ---
>  fs/btrfs/disk-io.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
> index b30309f187cf..126f244cdf88 100644
> --- a/fs/btrfs/disk-io.c
> +++ b/fs/btrfs/disk-io.c
> @@ -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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fs: btrfs: fix possible use-after-free bug in error handling code of btrfs_get_root_ref()
  2022-03-24 18:19 ` David Sterba
@ 2022-03-25  8:04   ` Jia-Ju Bai
  2022-04-01 15:54     ` David Sterba
  0 siblings, 1 reply; 6+ messages in thread
From: Jia-Ju Bai @ 2022-03-25  8:04 UTC (permalink / raw)
  To: dsterba; +Cc: clm, josef, dsterba, linux-btrfs, linux-kernel



On 2022/3/25 2:19, David Sterba wrote:
> On Thu, Mar 24, 2022 at 06:44:54AM -0700, Jia-Ju Bai wrote:
>> 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".
>>
>> Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
>> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
>> ---
>>   fs/btrfs/disk-io.c | 5 +++--
>>   1 file changed, 3 insertions(+), 2 deletions(-)
>>
>> diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
>> index b30309f187cf..126f244cdf88 100644
>> --- a/fs/btrfs/disk-io.c
>> +++ b/fs/btrfs/disk-io.c
>> @@ -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.

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fs: btrfs: fix possible use-after-free bug in error handling code of btrfs_get_root_ref()
  2022-03-25  8:04   ` Jia-Ju Bai
@ 2022-04-01 15:54     ` David Sterba
  2023-08-23  8:00       ` Lee Jones
  0 siblings, 1 reply; 6+ messages in thread
From: David Sterba @ 2022-04-01 15:54 UTC (permalink / raw)
  To: Jia-Ju Bai; +Cc: dsterba, clm, josef, dsterba, linux-btrfs, linux-kernel

On Fri, Mar 25, 2022 at 04:04:17PM +0800, Jia-Ju Bai wrote:
> >> @@ -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.
> 
> 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?

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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fs: btrfs: fix possible use-after-free bug in error handling code of btrfs_get_root_ref()
  2022-04-01 15:54     ` David Sterba
@ 2023-08-23  8:00       ` Lee Jones
  2023-08-23  8:07         ` Lee Jones
  0 siblings, 1 reply; 6+ messages in thread
From: Lee Jones @ 2023-08-23  8:00 UTC (permalink / raw)
  To: dsterba, Jia-Ju Bai, clm, josef, dsterba, linux-btrfs, linux-kernel

On Fri, 01 Apr 2022, David Sterba wrote:

> On Fri, Mar 25, 2022 at 04:04:17PM +0800, Jia-Ju Bai wrote:
> > >> @@ -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.
> > 
> > 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?
> 
> 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?

-- 
Lee Jones [李琼斯]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fs: btrfs: fix possible use-after-free bug in error handling code of btrfs_get_root_ref()
  2023-08-23  8:00       ` Lee Jones
@ 2023-08-23  8:07         ` Lee Jones
  0 siblings, 0 replies; 6+ messages in thread
From: Lee Jones @ 2023-08-23  8:07 UTC (permalink / raw)
  To: dsterba, Jia-Ju Bai, clm, josef, dsterba, linux-btrfs, linux-kernel

On Wed, 23 Aug 2023, Lee Jones wrote:

> On Fri, 01 Apr 2022, David Sterba wrote:
> 
> > On Fri, Mar 25, 2022 at 04:04:17PM +0800, Jia-Ju Bai wrote:
> > > >> @@ -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.
> > > 
> > > 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?
> > 
> > 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")

-- 
Lee Jones [李琼斯]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-08-23  8:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-24 13:44 [PATCH] fs: btrfs: fix possible use-after-free bug in error handling code of btrfs_get_root_ref() Jia-Ju Bai
2022-03-24 18:19 ` David Sterba
2022-03-25  8:04   ` Jia-Ju Bai
2022-04-01 15:54     ` David Sterba
2023-08-23  8:00       ` Lee Jones
2023-08-23  8:07         ` Lee Jones

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).