Skip to contentSkip to conversation

xfs: avoid mount-time deadlock in CoW extent recovery

PATCH Superseded Darrick J. Wong 2 participants 3 messages May 17, 2017 – May 18, 2017 XFS filesystem

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xfs: avoid mount-time deadlock in CoW extent recovery

If a malicious user corrupts the refcount btree to cause a cycle between different levels of the tree, the next mount attempt will deadlock in the CoW recovery routine while grabbing buffer locks. We can use the ability to re-grab a buffer that was previous locked to a transaction to avoid deadlocks, so do that here.

---
v2: leave a comment discussing why we use an empty transaction

---
 fs/xfs/libxfs/xfs_refcount.c |   39 +++++++++++++++++++++++++++++----------
 1 file changed, 29 insertions(+), 10 deletions(-)
fs/xfs/libxfs/xfs_refcount.c +29 -10
index b177ef3..3d2b29b 100644
@@ -1629,10 +1629,24 @@ xfs_refcount_recover_cow_leftovers(
 	if (mp->m_sb.sb_agblocks >= XFS_REFC_COW_START)
 		return -EOPNOTSUPP;
 
-	error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp);
+	/*
+	 * In this first part, we use an empty transaction to gather up
+	 * all the leftover CoW extents so that we can subsequently
+	 * delete them.  The empty transaction is used to avoid
+	 * a buffer lock deadlock if there happens to be a loop in the
+	 * refcountbt because we're allowed to re-grab a buffer that is
+	 * already attached to our transaction.  When we're done
+	 * recording the CoW debris we cancel the (empty) transaction
+	 * and everything goes away cleanly.
+	 */
+	error = xfs_trans_alloc_empty(mp, &tp);
 	if (error)
 		return error;
-	cur = xfs_refcountbt_init_cursor(mp, NULL, agbp, agno, NULL);
+
+	error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
+	if (error)
+		goto out_trans;
+	cur = xfs_refcountbt_init_cursor(mp, tp, agbp, agno, NULL);
 
 	/* Find all the leftover CoW staging extents. */
 	INIT_LIST_HEAD(&debris);
@@ -1645,10 +1659,11 @@ xfs_refcount_recover_cow_leftovers(
 	if (error)
 		goto out_cursor;
 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
-	xfs_buf_relse(agbp);
+	xfs_trans_brelse(tp, agbp);
+	xfs_trans_cancel(tp);
 
 	/* Now iterate the list to free the leftovers */
-	list_for_each_entry(rr, &debris, rr_list) {
+	list_for_each_entry_safe(rr, n, &debris, rr_list) {
 		/* Set up transaction. */
 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
 		if (error)
@@ -1676,8 +1691,15 @@ xfs_refcount_recover_cow_leftovers(
 		error = xfs_trans_commit(tp);
 		if (error)
 			goto out_free;
+
+		list_del(&rr->rr_list);
+		kmem_free(rr);
 	}
 
+	return error;
+out_defer:
+	xfs_defer_cancel(&dfops);
+	xfs_trans_cancel(tp);
 out_free:
 	/* Free the leftover list */
 	list_for_each_entry_safe(rr, n, &debris, rr_list) {
@@ -1688,11 +1710,8 @@ xfs_refcount_recover_cow_leftovers(
 
 out_cursor:
 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
-	xfs_buf_relse(agbp);
-	goto out_free;
-
-out_defer:
-	xfs_defer_cancel(&dfops);
+	xfs_trans_brelse(tp, agbp);
+out_trans:
 	xfs_trans_cancel(tp);
-	goto out_free;
+	return error;
 }
#1 lore raw
Quoted 17 lines · Darrick J. Wong

If a malicious user corrupts the refcount btree to cause a cycle between different levels of the tree, the next mount attempt will deadlock in the CoW recovery routine while grabbing buffer locks. We can use the ability to re-grab a buffer that was previous locked to a transaction to avoid deadlocks, so do that here.

---
v2: leave a comment discussing why we use an empty transaction

---
 fs/xfs/libxfs/xfs_refcount.c |   39 +++++++++++++++++++++++++++++----------
 1 file changed, 29 insertions(+), 10 deletions(-)
fs/xfs/libxfs/xfs_refcount.c +0 -0
index b177ef3..3d2b29b 100644

...

Quoted 17 lines
fs/xfs/libxfs/xfs_refcount.c +7 -0
@@ -1676,8 +1691,15 @@ xfs_refcount_recover_cow_leftovers(
 		error = xfs_trans_commit(tp);
 		if (error)
 			goto out_free;
+
+		list_del(&rr->rr_list);
+		kmem_free(rr);
 	}
 
+	return error;
+out_defer:
+	xfs_defer_cancel(&dfops);
+	xfs_trans_cancel(tp);
 out_free:
 	/* Free the leftover list */
 	list_for_each_entry_safe(rr, n, &debris, rr_list) {

fs/xfs/libxfs/xfs_refcount.c xfs_refcount_recover_cow_leftovers( · line 1693

Quoted 6 lines
fs/xfs/libxfs/xfs_refcount.c +0 -3
@@ -1688,11 +1710,8 @@ xfs_refcount_recover_cow_leftovers(
 
 out_cursor:
 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
-	xfs_buf_relse(agbp);
-	goto out_free;
-

Previously we had this jump to out_free here which presumably would handle the case of a query range error with a populated list. Now it looks like we just break down the cursor/transaction and return. Is that case not possible?

Brian

Quoted 12 lines
fs/xfs/libxfs/xfs_refcount.c +3 -4
-out_defer:
-	xfs_defer_cancel(&dfops);
+	xfs_trans_brelse(tp, agbp);
+out_trans:
 	xfs_trans_cancel(tp);
-	goto out_free;
+	return error;
 }
--

To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 37 lines · Brian Foster
Quoted 17 lines · Darrick J. Wong

If a malicious user corrupts the refcount btree to cause a cycle between different levels of the tree, the next mount attempt will deadlock in the CoW recovery routine while grabbing buffer locks. We can use the ability to re-grab a buffer that was previous locked to a transaction to avoid deadlocks, so do that here.

---
v2: leave a comment discussing why we use an empty transaction

---
 fs/xfs/libxfs/xfs_refcount.c |   39 +++++++++++++++++++++++++++++----------
 1 file changed, 29 insertions(+), 10 deletions(-)
fs/xfs/libxfs/xfs_refcount.c +0 -0
index b177ef3..3d2b29b 100644

...

Quoted 18 lines
fs/xfs/libxfs/xfs_refcount.c +7 -0
@@ -1676,8 +1691,15 @@ xfs_refcount_recover_cow_leftovers(
 		error = xfs_trans_commit(tp);
 		if (error)
 			goto out_free;
+
+		list_del(&rr->rr_list);
+		kmem_free(rr);
 	}
 
+	return error;
+out_defer:
+	xfs_defer_cancel(&dfops);
+	xfs_trans_cancel(tp);
 out_free:
 	/* Free the leftover list */
 	list_for_each_entry_safe(rr, n, &debris, rr_list) {
@@ -1688,11 +1710,8 @@ xfs_refcount_recover_cow_leftovers(
 
Quoted 10 lines

fs/xfs/libxfs/xfs_refcount.c xfs_refcount_recover_cow_leftovers( · line 1693

Quoted 5 lines
fs/xfs/libxfs/xfs_refcount.c +0 -3
 out_cursor:
 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
-	xfs_buf_relse(agbp);
-	goto out_free;
-

Previously we had this jump to out_free here which presumably would handle the case of a query range error with a populated list. Now it looks like we just break down the cursor/transaction and return. Is that case not possible?

Oops, yeah, I'll go refactor the exit paths again. Thanks for catching this.

--D

Quoted 19 lines

Brian

Quoted 9 lines
fs/xfs/libxfs/xfs_refcount.c +3 -4
-out_defer:
-	xfs_defer_cancel(&dfops);
+	xfs_trans_brelse(tp, agbp);
+out_trans:
 	xfs_trans_cancel(tp);
-	goto out_free;
+	return error;
 }
--
Quoted 3 lines

To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html