Skip to contentSkip to conversation

xfs: libxfs: move xfs_perag_put late

PATCH Superseded Pan Bian 3 participants 5 messages Nov 24, 2018 – Nov 27, 2018 XFS filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Pan Bian

author
#0 lore raw

PATCH 1/1 xfs: libxfs: move xfs_perag_put late

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.

---
 fs/xfs/libxfs/xfs_alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/xfs/libxfs/xfs_alloc.c +1 -1
index e1c0c0d..4be387d 100644
@@ -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;
Signature · 1 line
2.7.4
#1 lore raw
Quoted 5 lines · 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.

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

Quoted 29 lines
---
 fs/xfs/libxfs/xfs_alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/xfs/libxfs/xfs_alloc.c +1 -1
index e1c0c0d..4be387d 100644
@@ -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;
Signature · 1 line
2.7.4
Signature · 1 line
Carlos
author
#2 lore raw
Quoted 9 lines · Carlos Maiolino
Quoted 5 lines · 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.

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

#3 lore raw
Quoted 8 lines · PanBian
Quoted 7 lines · Carlos Maiolino
Quoted 5 lines · 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.

Quoted 8 lines
Quoted 2 lines

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

Quoted 3 lines

Best regards,
Pan Bian

author
#4 lore raw
Quoted 18 lines · Brian Foster
Quoted 8 lines · PanBian
Quoted 7 lines · Carlos Maiolino
Quoted 5 lines · 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.

Quoted 8 lines
Quoted 2 lines

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.

Quoted 8 lines

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

Quoted 9 lines

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

Brian

Quoted 3 lines

Best regards,
Pan Bian