linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] nilfs2: fix potential use after free
@ 2018-11-26  3:08 Pan Bian
  2018-12-03  9:10 ` Ryusuke Konishi
  0 siblings, 1 reply; 3+ messages in thread
From: Pan Bian @ 2018-11-26  3:08 UTC (permalink / raw)
  To: Ryusuke Konishi; +Cc: linux-nilfs, linux-kernel, Pan Bian

brelse(bh) is called to drop the reference count of bh when the call
to nilfs_dat_translate fails. If the reference count hits 0, bh may be
freed. However, bh->b_page is unlocked and put after that, which may
result in a use-after-free bug. This patch moves the release operation
after unlocking and putting the page.

Signed-off-by: Pan Bian <bianpan2016@163.com>
---
 fs/nilfs2/gcinode.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/nilfs2/gcinode.c b/fs/nilfs2/gcinode.c
index aa3c328..a24bb29 100644
--- a/fs/nilfs2/gcinode.c
+++ b/fs/nilfs2/gcinode.c
@@ -73,10 +73,8 @@ int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
 		struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
 
 		err = nilfs_dat_translate(nilfs->ns_dat, vbn, &pbn);
-		if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
-			brelse(bh);
+		if (unlikely(err)) /* -EIO, -ENOMEM, -ENOENT */
 			goto failed;
-		}
 	}
 
 	lock_buffer(bh);
@@ -102,6 +100,8 @@ int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
  failed:
 	unlock_page(bh->b_page);
 	put_page(bh->b_page);
+	if (unlikely(err))
+		brelse(bh);
 	return err;
 }
 
-- 
2.7.4



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

* Re: [PATCH] nilfs2: fix potential use after free
  2018-11-26  3:08 [PATCH] nilfs2: fix potential use after free Pan Bian
@ 2018-12-03  9:10 ` Ryusuke Konishi
  2018-12-03  9:14   ` PanBian
  0 siblings, 1 reply; 3+ messages in thread
From: Ryusuke Konishi @ 2018-12-03  9:10 UTC (permalink / raw)
  To: Pan Bian; +Cc: linux-nilfs, linux-kernel

Hi, Pan Bian

Thank you for feeding back this patch.
I reviewed this and am thinking this must be sent to upstream.

Did you see any kernel oops on this bug ?

Regards,
Ryusuke Konishi

On Mon, 26 Nov 2018 11:08:29 +0800, Pan Bian <bianpan2016@163.com> wrote:
> brelse(bh) is called to drop the reference count of bh when the call
> to nilfs_dat_translate fails. If the reference count hits 0, bh may be
> freed. However, bh->b_page is unlocked and put after that, which may
> result in a use-after-free bug. This patch moves the release operation
> after unlocking and putting the page.
> 
> Signed-off-by: Pan Bian <bianpan2016@163.com>
> ---
>  fs/nilfs2/gcinode.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/nilfs2/gcinode.c b/fs/nilfs2/gcinode.c
> index aa3c328..a24bb29 100644
> --- a/fs/nilfs2/gcinode.c
> +++ b/fs/nilfs2/gcinode.c
> @@ -73,10 +73,8 @@ int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
>  		struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
>  
>  		err = nilfs_dat_translate(nilfs->ns_dat, vbn, &pbn);
> -		if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
> -			brelse(bh);
> +		if (unlikely(err)) /* -EIO, -ENOMEM, -ENOENT */
>  			goto failed;
> -		}
>  	}
>  
>  	lock_buffer(bh);
> @@ -102,6 +100,8 @@ int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
>   failed:
>  	unlock_page(bh->b_page);
>  	put_page(bh->b_page);
> +	if (unlikely(err))
> +		brelse(bh);
>  	return err;
>  }
>  
> -- 
> 2.7.4
> 
> 

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

* Re: [PATCH] nilfs2: fix potential use after free
  2018-12-03  9:10 ` Ryusuke Konishi
@ 2018-12-03  9:14   ` PanBian
  0 siblings, 0 replies; 3+ messages in thread
From: PanBian @ 2018-12-03  9:14 UTC (permalink / raw)
  To: Ryusuke Konishi; +Cc: linux-nilfs, linux-kernel

On Mon, Dec 03, 2018 at 06:10:51PM +0900, Ryusuke Konishi wrote:
> Hi, Pan Bian
> 
> Thank you for feeding back this patch.
> I reviewed this and am thinking this must be sent to upstream.
> 
> Did you see any kernel oops on this bug ?

Not yet. In fact, I found it with a static method.

Best regards,
Pan Bian

> 
> Regards,
> Ryusuke Konishi
> 
> On Mon, 26 Nov 2018 11:08:29 +0800, Pan Bian <bianpan2016@163.com> wrote:
> > brelse(bh) is called to drop the reference count of bh when the call
> > to nilfs_dat_translate fails. If the reference count hits 0, bh may be
> > freed. However, bh->b_page is unlocked and put after that, which may
> > result in a use-after-free bug. This patch moves the release operation
> > after unlocking and putting the page.
> > 
> > Signed-off-by: Pan Bian <bianpan2016@163.com>
> > ---
> >  fs/nilfs2/gcinode.c | 6 +++---
> >  1 file changed, 3 insertions(+), 3 deletions(-)
> > 
> > diff --git a/fs/nilfs2/gcinode.c b/fs/nilfs2/gcinode.c
> > index aa3c328..a24bb29 100644
> > --- a/fs/nilfs2/gcinode.c
> > +++ b/fs/nilfs2/gcinode.c
> > @@ -73,10 +73,8 @@ int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
> >  		struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
> >  
> >  		err = nilfs_dat_translate(nilfs->ns_dat, vbn, &pbn);
> > -		if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
> > -			brelse(bh);
> > +		if (unlikely(err)) /* -EIO, -ENOMEM, -ENOENT */
> >  			goto failed;
> > -		}
> >  	}
> >  
> >  	lock_buffer(bh);
> > @@ -102,6 +100,8 @@ int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
> >   failed:
> >  	unlock_page(bh->b_page);
> >  	put_page(bh->b_page);
> > +	if (unlikely(err))
> > +		brelse(bh);
> >  	return err;
> >  }
> >  
> > -- 
> > 2.7.4
> > 
> > 


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

end of thread, other threads:[~2018-12-03  9:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-26  3:08 [PATCH] nilfs2: fix potential use after free Pan Bian
2018-12-03  9:10 ` Ryusuke Konishi
2018-12-03  9:14   ` PanBian

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