linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] xfs: libxfs: move xfs_perag_put late
@ 2018-11-24  9:44 Pan Bian
  2018-11-26  9:31 ` Carlos Maiolino
  0 siblings, 1 reply; 5+ messages in thread
From: Pan Bian @ 2018-11-24  9:44 UTC (permalink / raw)
  To: Darrick J. Wong
  Cc: linux-xfs, Brian Foster, Dave Chinner, Carlos Maiolino,
	linux-kernel, Pan Bian

The function xfs_alloc_get_freelist calls xfs_perag_put to drop the
reference. In this case, pag may be released. However,
pag->pagf_btreeblks is read and write after the put operation. This may
result in a use-after-free bug. This patch moves the put operation late.

Signed-off-by: Pan Bian <bianpan2016@163.com>
---
 fs/xfs/libxfs/xfs_alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/xfs/libxfs/xfs_alloc.c b/fs/xfs/libxfs/xfs_alloc.c
index e1c0c0d..4be387d 100644
--- a/fs/xfs/libxfs/xfs_alloc.c
+++ b/fs/xfs/libxfs/xfs_alloc.c
@@ -2435,7 +2435,6 @@ xfs_alloc_get_freelist(
 	be32_add_cpu(&agf->agf_flcount, -1);
 	xfs_trans_agflist_delta(tp, -1);
 	pag->pagf_flcount--;
-	xfs_perag_put(pag);
 
 	logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
 	if (btreeblk) {
@@ -2443,6 +2442,7 @@ xfs_alloc_get_freelist(
 		pag->pagf_btreeblks++;
 		logflags |= XFS_AGF_BTREEBLKS;
 	}
+	xfs_perag_put(pag);
 
 	xfs_alloc_log_agf(tp, agbp, logflags);
 	*bnop = bno;
-- 
2.7.4



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

* Re: [PATCH] xfs: libxfs: move xfs_perag_put late
  2018-11-24  9:44 [PATCH] xfs: libxfs: move xfs_perag_put late Pan Bian
@ 2018-11-26  9:31 ` Carlos Maiolino
  2018-11-26 10:36   ` PanBian
  0 siblings, 1 reply; 5+ messages in thread
From: Carlos Maiolino @ 2018-11-26  9:31 UTC (permalink / raw)
  To: Pan Bian
  Cc: Darrick J. Wong, linux-xfs, Brian Foster, Dave Chinner, linux-kernel

On Sat, Nov 24, 2018 at 05:44:20PM +0800, Pan Bian wrote:
> The function xfs_alloc_get_freelist calls xfs_perag_put to drop the
> reference. In this case, pag may be released. However,
> pag->pagf_btreeblks is read and write after the put operation. This may
> result in a use-after-free bug. This patch moves the put operation late.
> 

The patch looks reasonable, can you detail more how did you find it? Via code
inspection of you hit this user-after-free in some way?

Cheers

> Signed-off-by: Pan Bian <bianpan2016@163.com>
> ---
>  fs/xfs/libxfs/xfs_alloc.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/xfs/libxfs/xfs_alloc.c b/fs/xfs/libxfs/xfs_alloc.c
> index e1c0c0d..4be387d 100644
> --- a/fs/xfs/libxfs/xfs_alloc.c
> +++ b/fs/xfs/libxfs/xfs_alloc.c
> @@ -2435,7 +2435,6 @@ xfs_alloc_get_freelist(
>  	be32_add_cpu(&agf->agf_flcount, -1);
>  	xfs_trans_agflist_delta(tp, -1);
>  	pag->pagf_flcount--;
> -	xfs_perag_put(pag);
>  
>  	logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
>  	if (btreeblk) {
> @@ -2443,6 +2442,7 @@ xfs_alloc_get_freelist(
>  		pag->pagf_btreeblks++;
>  		logflags |= XFS_AGF_BTREEBLKS;
>  	}
> +	xfs_perag_put(pag);
>  
>  	xfs_alloc_log_agf(tp, agbp, logflags);
>  	*bnop = bno;
> -- 
> 2.7.4
> 
> 

-- 
Carlos

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

* Re: [PATCH] xfs: libxfs: move xfs_perag_put late
  2018-11-26  9:31 ` Carlos Maiolino
@ 2018-11-26 10:36   ` PanBian
  2018-11-26 14:17     ` Brian Foster
  0 siblings, 1 reply; 5+ messages in thread
From: PanBian @ 2018-11-26 10:36 UTC (permalink / raw)
  To: Carlos Maiolino
  Cc: Darrick J. Wong, linux-xfs, Brian Foster, Dave Chinner, linux-kernel

On Mon, Nov 26, 2018 at 10:31:39AM +0100, Carlos Maiolino wrote:
> On Sat, Nov 24, 2018 at 05:44:20PM +0800, Pan Bian wrote:
> > The function xfs_alloc_get_freelist calls xfs_perag_put to drop the
> > reference. In this case, pag may be released. However,
> > pag->pagf_btreeblks is read and write after the put operation. This may
> > result in a use-after-free bug. This patch moves the put operation late.
> > 
> 
> The patch looks reasonable, can you detail more how did you find it? Via code
> inspection of you hit this user-after-free in some way?

I wrote a tool to check such bugs statically. It first scans the source code 
to extract paired alloc/free functions. Equipped with such functions, it
performs an intra-procedural data flow analysis to detect mismatched
alloc/free bugs and use-after-free bugs.

Best regards,
Pan Bian


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

* Re: [PATCH] xfs: libxfs: move xfs_perag_put late
  2018-11-26 10:36   ` PanBian
@ 2018-11-26 14:17     ` Brian Foster
  2018-11-27  0:27       ` PanBian
  0 siblings, 1 reply; 5+ messages in thread
From: Brian Foster @ 2018-11-26 14:17 UTC (permalink / raw)
  To: PanBian
  Cc: Carlos Maiolino, Darrick J. Wong, linux-xfs, Dave Chinner, linux-kernel

On Mon, Nov 26, 2018 at 06:36:19PM +0800, PanBian wrote:
> On Mon, Nov 26, 2018 at 10:31:39AM +0100, Carlos Maiolino wrote:
> > On Sat, Nov 24, 2018 at 05:44:20PM +0800, Pan Bian wrote:
> > > The function xfs_alloc_get_freelist calls xfs_perag_put to drop the
> > > reference. In this case, pag may be released. However,
> > > pag->pagf_btreeblks is read and write after the put operation. This may
> > > result in a use-after-free bug. This patch moves the put operation late.
> > > 
> > 
> > The patch looks reasonable, can you detail more how did you find it? Via code
> > inspection of you hit this user-after-free in some way?
> 
> I wrote a tool to check such bugs statically. It first scans the source code 
> to extract paired alloc/free functions. Equipped with such functions, it
> performs an intra-procedural data flow analysis to detect mismatched
> alloc/free bugs and use-after-free bugs.
> 

You should probably drop the "use after free" text from your commit log
because that's not how the perag reference counting works. If you look
at xfs_perag_put(), you'll see it only drops a reference count and
returns. We only ever free the perag structs on unmount (or mount
failure), where we assert the refcount is zero. It looks like some other
serialization mechanism would have to break down for that assert to fail
due to this error in the allocation code because the fs is mostly shut
down at this point in time.

Misleading commit log aside, the change seems fine to me. I think it's
appropriate to follow the traditional/implied _get()/_put() pattern.

Brian

> Best regards,
> Pan Bian
> 

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

* Re: [PATCH] xfs: libxfs: move xfs_perag_put late
  2018-11-26 14:17     ` Brian Foster
@ 2018-11-27  0:27       ` PanBian
  0 siblings, 0 replies; 5+ messages in thread
From: PanBian @ 2018-11-27  0:27 UTC (permalink / raw)
  To: Brian Foster
  Cc: Carlos Maiolino, Darrick J. Wong, linux-xfs, Dave Chinner, linux-kernel

On Mon, Nov 26, 2018 at 09:17:50AM -0500, Brian Foster wrote:
> On Mon, Nov 26, 2018 at 06:36:19PM +0800, PanBian wrote:
> > On Mon, Nov 26, 2018 at 10:31:39AM +0100, Carlos Maiolino wrote:
> > > On Sat, Nov 24, 2018 at 05:44:20PM +0800, Pan Bian wrote:
> > > > The function xfs_alloc_get_freelist calls xfs_perag_put to drop the
> > > > reference. In this case, pag may be released. However,
> > > > pag->pagf_btreeblks is read and write after the put operation. This may
> > > > result in a use-after-free bug. This patch moves the put operation late.
> > > > 
> > > 
> > > The patch looks reasonable, can you detail more how did you find it? Via code
> > > inspection of you hit this user-after-free in some way?
> > 
> > I wrote a tool to check such bugs statically. It first scans the source code 
> > to extract paired alloc/free functions. Equipped with such functions, it
> > performs an intra-procedural data flow analysis to detect mismatched
> > alloc/free bugs and use-after-free bugs.
> > 
> 
> You should probably drop the "use after free" text from your commit log
> because that's not how the perag reference counting works. If you look
> at xfs_perag_put(), you'll see it only drops a reference count and
> returns. We only ever free the perag structs on unmount (or mount
> failure), where we assert the refcount is zero. It looks like some other
> serialization mechanism would have to break down for that assert to fail
> due to this error in the allocation code because the fs is mostly shut
> down at this point in time.

I will follow your guidance to correct the commit log and resubmit the
patch.

Thanks,
Pan

> 
> Misleading commit log aside, the change seems fine to me. I think it's
> appropriate to follow the traditional/implied _get()/_put() pattern.
> 
> Brian
> 
> > Best regards,
> > Pan Bian
> > 


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

end of thread, other threads:[~2018-11-27  0:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-24  9:44 [PATCH] xfs: libxfs: move xfs_perag_put late Pan Bian
2018-11-26  9:31 ` Carlos Maiolino
2018-11-26 10:36   ` PanBian
2018-11-26 14:17     ` Brian Foster
2018-11-27  0:27       ` 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).