xfs: fix inode use-after-free during log recovery
cover xfs: fix inode use-after-free during log recovery
Hi all,
In this series, I try to fix a use-after-free that I discovered during development of the dfops freezer, where BUI recovery releases the inode even if it requeues itself. If the inode gets reclaimed, the fs corrupts memory and explodes. The fix is to make the dfops freezer take over ownership of the inodes if there's any more work to be done.
If you're going to start using this mess, you probably ought to just pull from my git trees, which are linked below.
This is an extraordinary way to destroy everything. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fix-bmap-intent-recovery
--- fs/xfs/libxfs/xfs_defer.c | 57 ++++++++++++++++++++++- fs/xfs/libxfs/xfs_defer.h | 21 ++++++++- fs/xfs/libxfs/xfs_log_recover.h | 14 +++++- fs/xfs/xfs_bmap_item.c | 95 +++++++++++++++------------------------ fs/xfs/xfs_icache.c | 41 +++++++++++++++++ fs/xfs/xfs_log_recover.c | 32 +++++++++++-- fs/xfs/xfs_trans.h | 6 -- 7 files changed, 191 insertions(+), 75 deletions(-)
PATCH 1/3 xfs: clean up bmap intent item recovery checking
From: Darrick J. Wong <[email protected]>
The bmap intent item checking code in xfs_bui_item_recover is spread all over the function. We should check the recovered log item at the top before we allocate any resources or do anything else, so do that.
--- fs/xfs/xfs_bmap_item.c | 57 ++++++++++++++++-------------------------------- 1 file changed, 19 insertions(+), 38 deletions(-)
fs/xfs/xfs_bmap_item.c +19 -38
@@ -437,17 +437,13 @@ xfs_bui_item_recover(xfs_fsblock_t inode_fsb;xfs_filblks_t count;xfs_exntst_t state; -enum xfs_bmap_intent_type type; -bool op_ok;unsigned int bui_type;int whichfork;int error =0 ;/* Only one mapping operation per BUI... */ -if (buip->bui_format.bui_nextents !=XFS_BUI_MAX_FAST_EXTENTS ) { -xfs_bui_release (buip); -return -EFSCORRUPTED ; - } +if (buip->bui_format.bui_nextents !=XFS_BUI_MAX_FAST_EXTENTS ) + goto garbage;/* * First check the validity of the extent described by the
@@ -458,29 +454,26 @@ xfs_bui_item_recover( XFS_FSB_TO_DADDR(mp, bmap->me_startblock)); inode_fsb =XFS_BB_TO_FSB (mp,XFS_FSB_TO_DADDR (mp,XFS_INO_TO_FSB (mp, bmap->me_owner))); -switch (bmap->me_flags &XFS_BMAP_EXTENT_TYPE_MASK ) { + state = (bmap->me_flags &XFS_BMAP_EXTENT_UNWRITTEN ) ? +XFS_EXT_UNWRITTEN :XFS_EXT_NORM ; + whichfork = (bmap->me_flags &XFS_BMAP_EXTENT_ATTR_FORK ) ? +XFS_ATTR_FORK :XFS_DATA_FORK ; + bui_type = bmap->me_flags &XFS_BMAP_EXTENT_TYPE_MASK ; +switch (bui_type) {case XFS_BMAP_MAP :case XFS_BMAP_UNMAP : - op_ok = true;break ;default : - op_ok = false; -break ; + goto garbage; } -if (!op_ok || startblock_fsb ==0 || +if (startblock_fsb ==0 || bmap->me_len ==0 || inode_fsb ==0 || startblock_fsb >= mp->m_sb.sb_dblocks || bmap->me_len >= mp->m_sb.sb_agblocks || inode_fsb >= mp->m_sb.sb_dblocks || - (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS )) { -/* -* This will pull the BUI from the AIL and -* free the memory associated with it. -*/ -xfs_bui_release (buip); -return -EFSCORRUPTED ; - } + (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS )) + goto garbage; error =xfs_trans_alloc (mp, &M_RES (mp)->tr_itruncate,XFS_EXTENTADD_SPACE_RES (mp,XFS_DATA_FORK ),0 ,0 , &tp);
@@ -501,32 +494,17 @@ xfs_bui_item_recover(if (VFS_I (ip)->i_nlink ==0 )xfs_iflags_set (ip,XFS_IRECOVERY ); -/* Process deferred bmap item. */ - state = (bmap->me_flags &XFS_BMAP_EXTENT_UNWRITTEN ) ? -XFS_EXT_UNWRITTEN :XFS_EXT_NORM ; - whichfork = (bmap->me_flags &XFS_BMAP_EXTENT_ATTR_FORK ) ? -XFS_ATTR_FORK :XFS_DATA_FORK ; - bui_type = bmap->me_flags &XFS_BMAP_EXTENT_TYPE_MASK ; -switch (bui_type) { -case XFS_BMAP_MAP : -case XFS_BMAP_UNMAP : - type = bui_type; -break ; -default : -XFS_ERROR_REPORT (__func__,XFS_ERRLEVEL_LOW , mp); - error = -EFSCORRUPTED ; - goto err_inode; - }xfs_trans_ijoin (tp, ip,0 ); count = bmap->me_len; - error =xfs_trans_log_finish_bmap_update (tp, budp, type, ip, whichfork, - bmap->me_startoff, bmap->me_startblock, &count, state); + error =xfs_trans_log_finish_bmap_update (tp, budp, bui_type, ip, + whichfork, bmap->me_startoff, bmap->me_startblock, + &count, state);if (error) goto err_inode;if (count >0 ) { -ASSERT (type ==XFS_BMAP_UNMAP ); +ASSERT (bui_type ==XFS_BMAP_UNMAP ); irec.br_startblock = bmap->me_startblock; irec.br_blockcount = count; irec.br_startoff = bmap->me_startoff;
@@ -546,6 +524,9 @@ xfs_bui_item_recover(xfs_irele (ip); }return error; +garbage: +xfs_bui_release (buip); +return -EFSCORRUPTED ; }STATIC bool
Re: [PATCH 1/3] xfs: clean up bmap intent item recovery checking
Quoted 10 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
The bmap intent item checking code in
xfs_bui_item_recoveris spread all over the function. We should check the recovered log item at the top before we allocate any resources or do anything else, so do that.Signed-off-by Darrick J. Wong <[email protected]>--- fs/xfs/xfs_bmap_item.c | 57 ++++++++++++++++-------------------------------- 1 file changed, 19 insertions(+), 38 deletions(-)
Looks good - I have a very similar patch here....
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH 1/3] xfs: clean up bmap intent item recovery checking
In reply to Darrick J. WongQuoted 23 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
The bmap intent item checking code in
xfs_bui_item_recoveris spread all over the function. We should check the recovered log item at the top before we allocate any resources or do anything else, so do that.Signed-off-by Darrick J. Wong <[email protected]>--- fs/xfs/xfs_bmap_item.c | 57 ++++++++++++++++-------------------------------- 1 file changed, 19 insertions(+), 38 deletions(-)fs/xfs/xfs_bmap_item.c +0
-2@@ -437,17 +437,13 @@ xfs_bui_item_recover(xfs_fsblock_t inode_fsb;xfs_filblks_t count;xfs_exntst_t state; -enum xfs_bmap_intent_type type; -bool op_ok;unsigned int bui_type;
fs/xfs/xfs_bmap_item.c xfs_bui_item_recover( · line 446
Quoted 10 lines
fs/xfs/xfs_bmap_item.c +2
-4int whichfork;int error =0 ;/* Only one mapping operation per BUI... */ -if (buip->bui_format.bui_nextents !=XFS_BUI_MAX_FAST_EXTENTS ) { -xfs_bui_release (buip); -return -EFSCORRUPTED ; - } +if (buip->bui_format.bui_nextents !=XFS_BUI_MAX_FAST_EXTENTS ) + goto garbage;
We don't really need the xfs_bui_release any more, and can stick to plain "return -EFSCORRUPTED" instead of the goto, but I suspect the previous patch has taken care of that and you've rebased already?
Otherwise looks good:
Re: [PATCH 1/3] xfs: clean up bmap intent item recovery checking
Quoted 27 lines · Christoph Hellwig
Quoted 26 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
The bmap intent item checking code in
xfs_bui_item_recoveris spread all over the function. We should check the recovered log item at the top before we allocate any resources or do anything else, so do that.Signed-off-by Darrick J. Wong <[email protected]>--- fs/xfs/xfs_bmap_item.c | 57 ++++++++++++++++-------------------------------- 1 file changed, 19 insertions(+), 38 deletions(-)fs/xfs/xfs_bmap_item.c +0
-2@@ -437,17 +437,13 @@ xfs_bui_item_recover(xfs_fsblock_t inode_fsb;xfs_filblks_t count;xfs_exntst_t state; -enum xfs_bmap_intent_type type; -bool op_ok;unsigned int bui_type;int whichfork;int error =0 ;
Quoted 11 lines
fs/xfs/xfs_bmap_item.c xfs_bui_item_recover( · line 446
Quoted 7 lines
fs/xfs/xfs_bmap_item.c +2
-4/* Only one mapping operation per BUI... */ -if (buip->bui_format.bui_nextents !=XFS_BUI_MAX_FAST_EXTENTS ) { -xfs_bui_release (buip); -return -EFSCORRUPTED ; - } +if (buip->bui_format.bui_nextents !=XFS_BUI_MAX_FAST_EXTENTS ) + goto garbage;We don't really need the
xfs_bui_releaseany more, and can stick to plain "return-EFSCORRUPTED" instead of the goto, but I suspect the previous patch has taken care of that and you've rebased already?
Yep.
--D
Quoted 3 lines
Otherwise looks good:
Reviewed-by Christoph Hellwig <[email protected]>
PATCH 2/3 xfs: clean up xfs_bui_item_recover iget/trans_alloc/ilock ordering
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
In most places in XFS, we have a specific order in which we gather
resources: grab the inode, allocate a transaction, then lock the inode. xfs_bui_item_recover doesn't do it in that order, so fix it to be more consistent. This also makes the error bailout code a bit less weird.
--- fs/xfs/xfs_bmap_item.c | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-)
fs/xfs/xfs_bmap_item.c +21 -19
@@ -475,25 +475,26 @@ xfs_bui_item_recover( (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS)) goto garbage; - error =xfs_trans_alloc (mp, &M_RES (mp)->tr_itruncate, -XFS_EXTENTADD_SPACE_RES (mp,XFS_DATA_FORK ),0 ,0 , &tp); -if (error) -return error; - - budp =xfs_trans_get_bud (tp, buip); -/* Grab the inode. */ - error =xfs_iget (mp, tp, bmap->me_owner,0 ,XFS_ILOCK_EXCL , &ip); + error =xfs_iget (mp,NULL , bmap->me_owner,0 ,0 , &ip);if (error) - goto err_inode; +return error; error =xfs_qm_dqattach (ip);if (error) - goto err_inode; + goto err_rele;if (VFS_I (ip)->i_nlink ==0 )xfs_iflags_set (ip,XFS_IRECOVERY ); +/* Allocate transaction and do the work. */ + error =xfs_trans_alloc (mp, &M_RES (mp)->tr_itruncate, +XFS_EXTENTADD_SPACE_RES (mp,XFS_DATA_FORK ),0 ,0 , &tp); +if (error) + goto err_rele; + + budp =xfs_trans_get_bud (tp, buip); +xfs_ilock (ip,XFS_ILOCK_EXCL );xfs_trans_ijoin (tp, ip,0 ); count = bmap->me_len;
@@ -501,7 +502,7 @@ xfs_bui_item_recover( whichfork, bmap->me_startoff, bmap->me_startblock, &count, state);if (error) - goto err_inode; + goto err_cancel;if (count >0 ) {ASSERT (bui_type ==XFS_BMAP_UNMAP );
@@ -512,18 +513,19 @@ xfs_bui_item_recover(xfs_bmap_unmap_extent (tp, ip, &irec); } +/* Commit transaction, which frees tp. */ error =xlog_recover_trans_commit (tp, dfcp); +if (error) + goto err_unlock; +return 0 ; + +err_cancel: +xfs_trans_cancel (tp); +err_unlock:xfs_iunlock (ip,XFS_ILOCK_EXCL ); +err_rele:xfs_irele (ip);return error; - -err_inode: -xfs_trans_cancel (tp); -if (ip) { -xfs_iunlock (ip,XFS_ILOCK_EXCL ); -xfs_irele (ip); - } -return error; garbage:xfs_bui_release (buip);return -EFSCORRUPTED ;
Re: [PATCH 2/3] xfs: clean up xfs_bui_item_recover iget/trans_alloc/ilock ordering
Quoted 11 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
In most places in XFS, we have a specific order in which we gather
resources: grab the inode, allocate a transaction, then lock the inode.xfs_bui_item_recoverdoesn't do it in that order, so fix it to be more consistent. This also makes the error bailout code a bit less weird.Signed-off-by Darrick J. Wong <[email protected]>--- fs/xfs/xfs_bmap_item.c | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-)
This probably needs to go before the xfs_qm_dqattach() fix, or the dqattach fix need to come after this....
Quoted 27 lines
fs/xfs/xfs_bmap_item.c +2
-10@@ -475,25 +475,26 @@ xfs_bui_item_recover( (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS)) goto garbage; - error =xfs_trans_alloc (mp, &M_RES (mp)->tr_itruncate, -XFS_EXTENTADD_SPACE_RES (mp,XFS_DATA_FORK ),0 ,0 , &tp); -if (error) -return error; - - budp =xfs_trans_get_bud (tp, buip); -/* Grab the inode. */ - error =xfs_iget (mp, tp, bmap->me_owner,0 ,XFS_ILOCK_EXCL , &ip); + error =xfs_iget (mp,NULL , bmap->me_owner,0 ,0 , &ip);if (error) - goto err_inode; +return error; error =xfs_qm_dqattach (ip);if (error) - goto err_inode;
fs/xfs/xfs_bmap_item.c xfs_bui_item_recover( · line 494
Quoted 10 lines
fs/xfs/xfs_bmap_item.c +6
-0+ goto err_rele;if (VFS_I (ip)->i_nlink ==0 )xfs_iflags_set (ip,XFS_IRECOVERY ); +/* Allocate transaction and do the work. */ + error =xfs_trans_alloc (mp, &M_RES (mp)->tr_itruncate, +XFS_EXTENTADD_SPACE_RES (mp,XFS_DATA_FORK ),0 ,0 , &tp); +if (error) + goto err_rele;
Hmmmm - don't all the error cased before we call xfs_trans_get_bud() need to release the bui?
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH 2/3] xfs: clean up xfs_bui_item_recover iget/trans_alloc/ilock ordering
Quoted 8 lines · Dave Chinner
Quoted 7 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
In most places in XFS, we have a specific order in which we gather
resources: grab the inode, allocate a transaction, then lock the inode.xfs_bui_item_recoverdoesn't do it in that order, so fix it to be more consistent. This also makes the error bailout code a bit less weird.
Quoted 7 lines
Quoted 4 lines
Signed-off-by Darrick J. Wong <[email protected]>--- fs/xfs/xfs_bmap_item.c | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-)This probably needs to go before the
xfs_qm_dqattach()fix, or the dqattach fix need to come after this....
<nod> I'll fix the previous patch.
Quoted 31 lines
Quoted 31 lines
fs/xfs/xfs_bmap_item.c +3
-10@@ -475,25 +475,26 @@ xfs_bui_item_recover( (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS)) goto garbage; - error =xfs_trans_alloc (mp, &M_RES (mp)->tr_itruncate, -XFS_EXTENTADD_SPACE_RES (mp,XFS_DATA_FORK ),0 ,0 , &tp); -if (error) -return error; - - budp =xfs_trans_get_bud (tp, buip); -/* Grab the inode. */ - error =xfs_iget (mp, tp, bmap->me_owner,0 ,XFS_ILOCK_EXCL , &ip); + error =xfs_iget (mp,NULL , bmap->me_owner,0 ,0 , &ip);if (error) - goto err_inode; +return error; error =xfs_qm_dqattach (ip);if (error) - goto err_inode; + goto err_rele;if (VFS_I (ip)->i_nlink ==0 )xfs_iflags_set (ip,XFS_IRECOVERY );
Quoted 8 lines
fs/xfs/xfs_bmap_item.c xfs_bui_item_recover( · line 494
Quoted 5 lines
fs/xfs/xfs_bmap_item.c +5
-0+/* Allocate transaction and do the work. */ + error =xfs_trans_alloc (mp, &M_RES (mp)->tr_itruncate, +XFS_EXTENTADD_SPACE_RES (mp,XFS_DATA_FORK ),0 ,0 , &tp); +if (error) + goto err_rele;Hmmmm - don't all the error cased before we call
xfs_trans_get_bud()need to release the bui?
Yes, I think so. Come to think of it, the other intent items seem like they have the same bug.
--D
Quoted 7 lines
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
PATCH v2 2/3 xfs: clean up xfs_bui_item_recover iget/trans_alloc/ilock ordering
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
In most places in XFS, we have a specific order in which we gather
resources: grab the inode, allocate a transaction, then lock the inode. xfs_bui_item_recover doesn't do it in that order, so fix it to be more consistent. This also makes the error bailout code a bit less weird.
---
v2: fix the error-out paths to free the BUI if the BUD hasn't been created
--- fs/xfs/xfs_bmap_item.c | 49 +++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-)
fs/xfs/xfs_bmap_item.c +26 -23
@@ -432,7 +432,7 @@ xfs_bui_item_recover(struct xfs_inode *ip =NULL ;struct xfs_mount *mp = lip->li_mountp;struct xfs_map_extent *bmap; -struct xfs_bud_log_item *budp; +struct xfs_bud_log_item *budp =NULL ;xfs_fsblock_t startblock_fsb;xfs_fsblock_t inode_fsb;xfs_filblks_t count;
@@ -475,27 +475,26 @@ xfs_bui_item_recover( (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS)) goto garbage; - error =xfs_trans_alloc (mp, &M_RES (mp)->tr_itruncate, -XFS_EXTENTADD_SPACE_RES (mp,XFS_DATA_FORK ),0 ,0 , &tp); -if (error) { -xfs_bui_release (buip); -return error; - } - - budp =xfs_trans_get_bud (tp, buip); -/* Grab the inode. */ - error =xfs_iget (mp, tp, bmap->me_owner,0 ,XFS_ILOCK_EXCL , &ip); + error =xfs_iget (mp,NULL , bmap->me_owner,0 ,0 , &ip);if (error) - goto err_inode; + goto err_bui; - error =xfs_qm_dqattach_locked (ip, false); + error =xfs_qm_dqattach (ip);if (error) - goto err_inode; + goto err_rele;if (VFS_I (ip)->i_nlink ==0 )xfs_iflags_set (ip,XFS_IRECOVERY ); +/* Allocate transaction and do the work. */ + error =xfs_trans_alloc (mp, &M_RES (mp)->tr_itruncate, +XFS_EXTENTADD_SPACE_RES (mp,XFS_DATA_FORK ),0 ,0 , &tp); +if (error) + goto err_rele; + + budp =xfs_trans_get_bud (tp, buip); +xfs_ilock (ip,XFS_ILOCK_EXCL );xfs_trans_ijoin (tp, ip,0 ); count = bmap->me_len;
@@ -503,7 +502,7 @@ xfs_bui_item_recover( whichfork, bmap->me_startoff, bmap->me_startblock, &count, state);if (error) - goto err_inode; + goto err_cancel;if (count >0 ) {ASSERT (bui_type ==XFS_BMAP_UNMAP );
@@ -514,17 +513,21 @@ xfs_bui_item_recover(xfs_bmap_unmap_extent (tp, ip, &irec); } +/* Commit transaction, which frees tp. */ error =xlog_recover_trans_commit (tp, dfcp); +if (error) + goto err_unlock; +return 0 ; + +err_cancel: +xfs_trans_cancel (tp); +err_unlock:xfs_iunlock (ip,XFS_ILOCK_EXCL ); +err_rele:xfs_irele (ip); -return error; - -err_inode: -xfs_trans_cancel (tp); -if (ip) { -xfs_iunlock (ip,XFS_ILOCK_EXCL ); -xfs_irele (ip); - } +err_bui: +if (!budp) +xfs_bui_release (buip);return error; garbage:xfs_bui_release (buip);
Re: [PATCH v2 2/3] xfs: clean up xfs_bui_item_recover iget/trans_alloc/ilock ordering
Quoted 37 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
In most places in XFS, we have a specific order in which we gather
resources: grab the inode, allocate a transaction, then lock the inode.xfs_bui_item_recoverdoesn't do it in that order, so fix it to be more consistent. This also makes the error bailout code a bit less weird.Signed-off-by Darrick J. Wong <[email protected]>---
v2: fix the error-out paths to free the BUI if the BUD hasn't been created--- fs/xfs/xfs_bmap_item.c | 49 +++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-)fs/xfs/xfs_bmap_item.c +1
-6@@ -432,7 +432,7 @@ xfs_bui_item_recover(struct xfs_inode *ip =NULL ;struct xfs_mount *mp = lip->li_mountp;struct xfs_map_extent *bmap; -struct xfs_bud_log_item *budp; +struct xfs_bud_log_item *budp =NULL ;xfs_fsblock_t startblock_fsb;xfs_fsblock_t inode_fsb;xfs_filblks_t count;@@ -475,27 +475,26 @@ xfs_bui_item_recover( (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS)) goto garbage; - error =xfs_trans_alloc (mp, &M_RES (mp)->tr_itruncate, -XFS_EXTENTADD_SPACE_RES (mp,XFS_DATA_FORK ),0 ,0 , &tp); -if (error) { -xfs_bui_release (buip); -return error;
fs/xfs/xfs_bmap_item.c xfs_bui_item_recover( · line 481
Quoted 10 lines
fs/xfs/xfs_bmap_item.c +2
-6- } - - budp =xfs_trans_get_bud (tp, buip); -/* Grab the inode. */ - error =xfs_iget (mp, tp, bmap->me_owner,0 ,XFS_ILOCK_EXCL , &ip); + error =xfs_iget (mp,NULL , bmap->me_owner,0 ,0 , &ip);if (error) - goto err_inode; + goto err_bui;
err_bui could be combined with the garbage error return if error is initialised to -EFSCORRUPTED.
Otherwise it looks OK.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH v2 2/3] xfs: clean up xfs_bui_item_recover iget/trans_alloc/ilock ordering
In reply to Darrick J. WongQuoted 8 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
In most places in XFS, we have a specific order in which we gather
resources: grab the inode, allocate a transaction, then lock the inode.xfs_bui_item_recoverdoesn't do it in that order, so fix it to be more consistent. This also makes the error bailout code a bit less weird.Signed-off-by Darrick J. Wong <[email protected]>
Looks good:
PATCH 3/3 xfs: fix an incore inode UAF in xfs_bui_recover
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
In xfs_bui_item_recover, there exists a use-after-free bug with regards to the inode that is involved in the bmap replay operation. If the mapping operation does not complete, we call xfs_bmap_unmap_extent to create a deferred op to finish the unmapping work, and we retain a pointer to the incore inode.
Unfortunately, the very next thing we do is commit the transaction and drop the inode. If reclaim tears down the inode before we try to finish the defer ops, we dereference garbage and blow up. Therefore, create a way to join inodes to the defer ops freezer so that we can maintain the xfs_inode reference until we're done with the inode.
Note: This imposes the requirement that there be enough memory to keep every incore inode in memory throughout recovery.
--- fs/xfs/libxfs/xfs_defer.c | 57 +++++++++++++++++++++++++++++++++++++-- fs/xfs/libxfs/xfs_defer.h | 21 ++++++++++++++ fs/xfs/libxfs/xfs_log_recover.h | 14 ++++++++-- fs/xfs/xfs_bmap_item.c | 8 +---- fs/xfs/xfs_icache.c | 41 ++++++++++++++++++++++++++++ fs/xfs/xfs_log_recover.c | 32 ++++++++++++++++++---- fs/xfs/xfs_trans.h | 6 ---- 7 files changed, 156 insertions(+), 23 deletions(-)
fs/xfs/libxfs/xfs_defer.c +54 -3
@@ -16,6 +16,7 @@#include "xfs_inode.h" #include "xfs_inode_item.h" #include "xfs_trace.h" +#include "xfs_icache.h" /* * Deferred Operations in XFS
@@ -555,6 +556,18 @@ xfs_defer_move(xfs_defer_reset (stp); } +/* Unlock the inodes attached to this dfops capture device. */ +static void +xfs_defer_capture_iunlock ( +struct xfs_defer_capture *dfc) +{ +unsigned int i; + +for (i =0 ; i <XFS_DEFER_OPS_NR_INODES && dfc->dfc_inodes[i]; i++) +xfs_iunlock (dfc->dfc_inodes[i],XFS_ILOCK_EXCL ); + dfc->dfc_ilocked = false; +} +/* * Prepare a chain of fresh deferred ops work items to be completed later. Log * recovery requires the ability to put off until later the actual finishing
@@ -568,14 +581,21 @@ xfs_defer_move(* transaction is committed. * * Note that the capture state is passed up to the caller and must be freed -* even if the transaction commit returns error. +* even if the transaction commit returns error. If inodes were passed in and +* a state capture structure was returned, the inodes are now owned by the +* state capture structure and the caller must not touch the inodes. +* +* If no structure is returned, the caller still owns the inodes. */ int xfs_defer_capture (struct xfs_trans *tp, -struct xfs_defer_capture **dfcp) +struct xfs_defer_capture **dfcp, +struct xfs_inode *ip1, +struct xfs_inode *ip2) {struct xfs_defer_capture *dfc =NULL ; +int error;if (!list_empty (&tp->t_dfops)) { dfc =kmem_zalloc (sizeof (*dfc),KM_NOFS );
@@ -594,10 +614,31 @@ xfs_defer_capture( dfc->dfc_tres.tr_logcount = tp->t_log_count; dfc->dfc_tres.tr_logflags =XFS_TRANS_PERM_LOG_RES ;xfs_defer_reset (tp); + +/* +* Transfer responsibility for unlocking and releasing the +* inodes to the capture structure. +*/ +if (!ip1) + ip1 = ip2; + dfc->dfc_ilocked = true; + dfc->dfc_inodes[0 ] = ip1; +if (ip2 != ip1) + dfc->dfc_inodes[1 ] = ip2; } *dfcp = dfc; -return xfs_trans_commit (tp); + error =xfs_trans_commit (tp); +if (error) +return error; + +/* +* Now that we've committed the transaction, it's safe to unlock the +* inodes that were passed in if we've taken over their ownership. +*/ +if (dfc) +xfs_defer_capture_iunlock (dfc); +return 0 ; }/* Attach a chain of captured deferred ops to a new transaction. */
@@ -609,6 +650,13 @@ xfs_defer_continue(ASSERT (tp->t_flags &XFS_TRANS_PERM_LOG_RES );ASSERT (!(tp->t_flags &XFS_TRANS_DIRTY )); +/* Lock and join the inodes to the new transaction. */ +xfs_defer_continue_inodes (dfc, tp); +if (dfc->dfc_inodes[0 ]) +xfs_trans_ijoin (tp, dfc->dfc_inodes[0 ],0 ); +if (dfc->dfc_inodes[1 ]) +xfs_trans_ijoin (tp, dfc->dfc_inodes[1 ],0 ); +/* Move captured dfops chain and state to the transaction. */ list_splice_init (&dfc->dfc_dfops, &tp->t_dfops); tp->t_flags |= dfc->dfc_tpflags;
@@ -624,5 +672,8 @@ xfs_defer_capture_free( struct xfs_defer_capture *dfc) {xfs_defer_cancel_list (mp, &dfc->dfc_dfops); +if (dfc->dfc_ilocked) +xfs_defer_capture_iunlock (dfc); +xfs_defer_capture_irele (dfc);kmem_free (dfc); }
fs/xfs/libxfs/xfs_defer.h +20 -1
@@ -10,6 +10,12 @@ struct xfs_btree_cur;struct xfs_defer_op_type ;struct xfs_defer_capture ; +/* +* Deferred operation item relogging limits. +*/ +#define XFS_DEFER_OPS_NR_INODES 2/* join up to two inodes */ +#define XFS_DEFER_OPS_NR_BUFS 2/* join up to two buffers */ +/* * Header for deferred operation list. */
@@ -78,15 +84,28 @@ struct xfs_defer_capture {unsigned int dfc_tpflags;unsigned int dfc_blkres;struct xfs_trans_res dfc_tres; + +/* +* Inodes to hold when we want to finish the deferred work items. +* Always set the first element before setting the second. +*/ +bool dfc_ilocked; + struct xfs_inode *dfc_inodes[XFS_DEFER_OPS_NR_INODES]; };/* * Functions to capture a chain of deferred operations and continue them later. * This doesn't normally happen except log recovery. */ -int xfs_defer_capture (struct xfs_trans *tp,struct xfs_defer_capture **dfcp); +int xfs_defer_capture (struct xfs_trans *tp,struct xfs_defer_capture **dfcp, +struct xfs_inode *ip1,struct xfs_inode *ip2);void xfs_defer_continue (struct xfs_defer_capture *dfc,struct xfs_trans *tp);void xfs_defer_capture_free (struct xfs_mount *mp,struct xfs_defer_capture *dfc); +/* These functions must be provided by the xfs implementation. */ +void xfs_defer_continue_inodes (struct xfs_defer_capture *dfc, +struct xfs_trans *tp); +void xfs_defer_capture_irele (struct xfs_defer_capture *dfc); +#endif /* __XFS_DEFER_H__ */
fs/xfs/libxfs/xfs_log_recover.h +12 -2
@@ -127,7 +127,17 @@ void xlog_recover_iodone(struct xfs_buf *bp);void xlog_recover_release_intent (struct xlog *log,unsigned short intent_type,uint64_t intent_id); -int xlog_recover_trans_commit (struct xfs_trans *tp, -struct xfs_defer_capture **dfcp); +int xlog_recover_trans_commit_inodes (struct xfs_trans *tp, +struct xfs_defer_capture **dfcp,struct xfs_inode *ip1, +struct xfs_inode *ip2); + +static inline int +xlog_recover_trans_commit ( +struct xfs_trans *tp, +struct xfs_defer_capture **dfcp) +{ +return xlog_recover_trans_commit_inodes (tp, dfcp,NULL ,NULL ); +} +#endif /* __XFS_LOG_RECOVER_H__ */
fs/xfs/xfs_bmap_item.c +2 -6
@@ -513,15 +513,11 @@ xfs_bui_item_recover(xfs_bmap_unmap_extent (tp, ip, &irec); } -/* Commit transaction, which frees tp. */ - error =xlog_recover_trans_commit (tp, dfcp); -if (error) - goto err_unlock; -return 0 ; +/* Commit transaction, which frees the transaction and the inode. */ +return xlog_recover_trans_commit_inodes (tp, dfcp, ip,NULL ); err_cancel:xfs_trans_cancel (tp); -err_unlock:xfs_iunlock (ip,XFS_ILOCK_EXCL ); err_rele:xfs_irele (ip);
fs/xfs/xfs_icache.c +41 -0
50 lines not on this page · raw message
fs/xfs/xfs_log_recover.c +27 -5
@@ -1791,16 +1791,38 @@ xlog_recover_release_intent(spin_unlock (&ailp->ail_lock); } +static inline void +xlog_recover_irele ( +struct xfs_inode *ip) +{ +xfs_iunlock (ip,XFS_ILOCK_EXCL ); +xfs_irele (ip); +} +/* -* Freeze any deferred ops and commit the transaction. This is the last step -* needed to finish a log intent item that we recovered from the log. +* Freeze any deferred ops, commit the transaction, and deal with the inodes. +* This is the last step needed to finish a log intent item that we recovered +* from the log. If we captured deferred ops, the inodes are attached to it +* and must not be touched. If not, we have to unlock and free them ourselves. */ int -xlog_recover_trans_commit ( +xlog_recover_trans_commit_inodes (struct xfs_trans *tp, -struct xfs_defer_capture **dfcp) +struct xfs_defer_capture **dfcp, +struct xfs_inode *ip1, +struct xfs_inode *ip2) { -return xfs_defer_capture (tp, dfcp); +int error; + + error =xfs_defer_capture (tp, dfcp, ip1, ip2); +if (*dfcp) +return error; + +if (ip2 && ip2 != ip1) +xlog_recover_irele (ip2); +if (ip1) +xlog_recover_irele (ip1); +return error; }/******************************************************************************
fs/xfs/xfs_trans.h +0 -6
@@ -97,12 +97,6 @@ void xfs_log_item_init(struct xfs_mount *mp, struct xfs_log_item *item,#define XFS_ITEM_LOCKED 2#define XFS_ITEM_FLUSHING 3 -/* -* Deferred operation item relogging limits. -*/ -#define XFS_DEFER_OPS_NR_INODES 2/* join up to two inodes */ -#define XFS_DEFER_OPS_NR_BUFS 2/* join up to two buffers */ -/* * This is the structure maintained for every active transaction. */
Re: [PATCH 3/3] xfs: fix an incore inode UAF in xfs_bui_recover
Quoted 8 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
In
xfs_bui_item_recover, there exists a use-after-free bug with regards to the inode that is involved in the bmap replay operation. If the mapping operation does not complete, we callxfs_bmap_unmap_extentto create a deferred op to finish the unmapping work, and we retain a pointer to the incore inode.
Quoted 8 lines
Unfortunately, the very next thing we do is commit the transaction and drop the inode. If reclaim tears down the inode before we try to finish the defer ops, we dereference garbage and blow up. Therefore, create a way to join inodes to the defer ops freezer so that we can maintain the
xfs_inodereference until we're done with the inode.Note: This imposes the requirement that there be enough memory to keep every incore inode in memory throughout recovery.
As in every inode that gets recovered, not every inode in the system. I think the commit log could use a very slight tweak here.
Didn't we think of just storing the inode number for recovery, or did this turn out too complicated? (I'm pretty sure we dicussed this in detail before, but my memory gets foggy).
Re: [PATCH 3/3] xfs: fix an incore inode UAF in xfs_bui_recover
Quoted 18 lines · Christoph Hellwig
Quoted 8 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
In
xfs_bui_item_recover, there exists a use-after-free bug with regards to the inode that is involved in the bmap replay operation. If the mapping operation does not complete, we callxfs_bmap_unmap_extentto create a deferred op to finish the unmapping work, and we retain a pointer to the incore inode.Quoted 8 lines
Unfortunately, the very next thing we do is commit the transaction and drop the inode. If reclaim tears down the inode before we try to finish the defer ops, we dereference garbage and blow up. Therefore, create a way to join inodes to the defer ops freezer so that we can maintain the
xfs_inodereference until we're done with the inode.Note: This imposes the requirement that there be enough memory to keep every incore inode in memory throughout recovery.
Quoted 6 lines
As in every inode that gets recovered, not every inode in the system. I think the commit log could use a very slight tweak here.
Didn't we think of just storing the inode number for recovery, or did this turn out too complicated? (I'm pretty sure we dicussed this in detail before, but my memory gets foggy).
Initially I did just store the inode numbers, but that made the code more clunky due to needing more _iget and _irele calls, and a bunch of error handling for that. Dave suggested on irc that I should retain the reference to the incore inode to simplify the initial patch, and if we run into ENOMEM then we can fix it later.
I wasn't 100% convinced of that, but Dave or Brian or someone (memory foggy, don't remember who) countered that the system recovering the fs is usually the system that crashed in the first place, and it certainly had enough RAM to hold the inodes.
I think the link you're looking for is[1].
--D
[1] https://lore.kernel.org/linux-xfs/158864123329.184729.14504239314355330619.stgit@magnolia/
PATCH v2 3/3 xfs: fix an incore inode UAF in xfs_bui_recover
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
In xfs_bui_item_recover, there exists a use-after-free bug with regards to the inode that is involved in the bmap replay operation. If the mapping operation does not complete, we call xfs_bmap_unmap_extent to create a deferred op to finish the unmapping work, and we retain a pointer to the incore inode.
Unfortunately, the very next thing we do is commit the transaction and drop the inode. If reclaim tears down the inode before we try to finish the defer ops, we dereference garbage and blow up. Therefore, create a way to join inodes to the defer ops freezer so that we can maintain the xfs_inode reference until we're done with the inode.
Note: This imposes the requirement that there be enough memory to keep every incore inode in memory throughout recovery.
---
v2: rebase on v2 of the previous patchset, and make it more clear that xfs_defer_capture takes over ownership of the inodes
--- fs/xfs/libxfs/xfs_defer.c | 57 ++++++++++++++++++++++++++++++++++++++- fs/xfs/libxfs/xfs_defer.h | 21 ++++++++++++++ fs/xfs/libxfs/xfs_log_recover.h | 11 ++++++-- fs/xfs/xfs_bmap_item.c | 8 +---- fs/xfs/xfs_icache.c | 41 ++++++++++++++++++++++++++++ fs/xfs/xfs_log_recover.c | 33 ++++++++++++++++++++--- fs/xfs/xfs_trans.h | 6 ---- 7 files changed, 156 insertions(+), 21 deletions(-)
fs/xfs/libxfs/xfs_defer.c +55 -2
@@ -16,6 +16,7 @@#include "xfs_inode.h" #include "xfs_inode_item.h" #include "xfs_trace.h" +#include "xfs_icache.h" /* * Deferred Operations in XFS
@@ -555,6 +556,18 @@ xfs_defer_move(xfs_defer_reset (stp); } +/* Unlock the inodes attached to this dfops capture device. */ +static void +xfs_defer_capture_iunlock ( +struct xfs_defer_capture *dfc) +{ +unsigned int i; + +for (i =0 ; i <XFS_DEFER_OPS_NR_INODES && dfc->dfc_inodes[i]; i++) +xfs_iunlock (dfc->dfc_inodes[i],XFS_ILOCK_EXCL ); + dfc->dfc_ilocked = false; +} +/* * Prepare a chain of fresh deferred ops work items to be completed later. Log * recovery requires the ability to put off until later the actual finishing
@@ -568,17 +581,26 @@ xfs_defer_move(* transaction is committed. * * Note that the capture state is passed up to the caller and must be freed -* even if the transaction commit returns error. +* even if the transaction commit returns error. If inodes were passed in and +* a state capture structure was returned, the inodes are now owned by the +* state capture structure and the caller must not touch the inodes. +* +* If *ipp1 or *ipp2 remain set, the caller still owns the inodes. */ int xfs_defer_capture (struct xfs_trans *tp, -struct list_head *capture_list) +struct list_head *capture_list, +struct xfs_inode **ipp1, +struct xfs_inode **ipp2) {struct xfs_defer_capture *dfc;struct xfs_mount *mp = tp->t_mountp;int error; +/* Do not pass in an ipp2 without an ipp1. */ +ASSERT (ipp1 || !ipp2); +if (list_empty (&tp->t_dfops))return xfs_trans_commit (tp);
@@ -600,6 +622,21 @@ xfs_defer_capture( dfc->dfc_tres.tr_logflags =XFS_TRANS_PERM_LOG_RES ;xfs_defer_reset (tp); +/* +* Transfer responsibility for unlocking and releasing the inodes to +* the capture structure. +*/ + dfc->dfc_ilocked = true; +if (ipp1) { + dfc->dfc_inodes[0 ] = *ipp1; + *ipp1 =NULL ; + } +if (ipp2) { +if (*ipp2 != dfc->dfc_inodes[0 ]) + dfc->dfc_inodes[1 ] = *ipp2; + *ipp2 =NULL ; + } +/* * Commit the transaction. If that fails, clean up the defer ops and * the dfc that we just created. Otherwise, add the dfc to the list.
@@ -610,6 +647,12 @@ xfs_defer_capture(return error; } +/* +* Now that we've committed the transaction, it's safe to unlock the +* inodes that were passed in if we've taken over their ownership. +*/ +xfs_defer_capture_iunlock (dfc); +list_add_tail (&dfc->dfc_list, capture_list);return 0 ; }
@@ -623,6 +666,13 @@ xfs_defer_continue(ASSERT (tp->t_flags &XFS_TRANS_PERM_LOG_RES );ASSERT (!(tp->t_flags &XFS_TRANS_DIRTY )); +/* Lock and join the inodes to the new transaction. */ +xfs_defer_continue_inodes (dfc, tp); +if (dfc->dfc_inodes[0 ]) +xfs_trans_ijoin (tp, dfc->dfc_inodes[0 ],0 ); +if (dfc->dfc_inodes[1 ]) +xfs_trans_ijoin (tp, dfc->dfc_inodes[1 ],0 ); +/* Move captured dfops chain and state to the transaction. */ list_splice_init (&dfc->dfc_dfops, &tp->t_dfops); tp->t_flags |= dfc->dfc_tpflags;
@@ -638,5 +688,8 @@ xfs_defer_capture_free( struct xfs_defer_capture *dfc) {xfs_defer_cancel_list (mp, &dfc->dfc_dfops); +if (dfc->dfc_ilocked) +xfs_defer_capture_iunlock (dfc); +xfs_defer_capture_irele (dfc);kmem_free (dfc); }
fs/xfs/libxfs/xfs_defer.h +20 -1
@@ -10,6 +10,12 @@ struct xfs_btree_cur;struct xfs_defer_op_type ;struct xfs_defer_capture ; +/* +* Deferred operation item relogging limits. +*/ +#define XFS_DEFER_OPS_NR_INODES 2/* join up to two inodes */ +#define XFS_DEFER_OPS_NR_BUFS 2/* join up to two buffers */ +/* * Header for deferred operation list. */
@@ -78,15 +84,28 @@ struct xfs_defer_capture {unsigned int dfc_tpflags;unsigned int dfc_blkres;struct xfs_trans_res dfc_tres; + +/* +* Inodes to hold when we want to finish the deferred work items. +* Always set the first element before setting the second. +*/ +bool dfc_ilocked; + struct xfs_inode *dfc_inodes[XFS_DEFER_OPS_NR_INODES]; };/* * Functions to capture a chain of deferred operations and continue them later. * This doesn't normally happen except log recovery. */ -int xfs_defer_capture (struct xfs_trans *tp,struct list_head *capture_list); +int xfs_defer_capture (struct xfs_trans *tp,struct list_head *capture_list, +struct xfs_inode **ipp1,struct xfs_inode **ipp2);void xfs_defer_continue (struct xfs_defer_capture *dfc,struct xfs_trans *tp);void xfs_defer_capture_free (struct xfs_mount *mp,struct xfs_defer_capture *dfc); +/* These functions must be provided by the xfs implementation. */ +void xfs_defer_continue_inodes (struct xfs_defer_capture *dfc, +struct xfs_trans *tp); +void xfs_defer_capture_irele (struct xfs_defer_capture *dfc); +#endif /* __XFS_DEFER_H__ */
fs/xfs/libxfs/xfs_log_recover.h +9 -2
@@ -125,7 +125,14 @@ void xlog_recover_iodone(struct xfs_buf *bp);void xlog_recover_release_intent (struct xlog *log,unsigned short intent_type,uint64_t intent_id); -int xlog_recover_trans_commit (struct xfs_trans *tp, -struct list_head *capture_list); +int xlog_recover_trans_commit_inodes (struct xfs_trans *tp, +struct list_head *capture_list,struct xfs_inode *ip1, +struct xfs_inode *ip2); + +static inline int +xlog_recover_trans_commit (struct xfs_trans *tp,struct list_head *capture_list) +{ +return xlog_recover_trans_commit_inodes (tp, capture_list,NULL ,NULL ); +}#endif /* __XFS_LOG_RECOVER_H__ */
fs/xfs/xfs_bmap_item.c +2 -6
@@ -513,15 +513,11 @@ xfs_bui_item_recover(xfs_bmap_unmap_extent (tp, ip, &irec); } -/* Commit transaction, which frees tp. */ - error =xlog_recover_trans_commit (tp, capture_list); -if (error) - goto err_unlock; -return 0 ; +/* Commit transaction, which frees the transaction and the inode. */ +return xlog_recover_trans_commit_inodes (tp, capture_list, ip,NULL ); err_cancel:xfs_trans_cancel (tp); -err_unlock:xfs_iunlock (ip,XFS_ILOCK_EXCL ); err_rele:xfs_irele (ip);
fs/xfs/xfs_icache.c +41 -0
50 lines not on this page · raw message
fs/xfs/xfs_log_recover.c +29 -4
@@ -1791,17 +1791,42 @@ xlog_recover_release_intent(spin_unlock (&ailp->ail_lock); } +static inline void +xlog_recover_irele ( +struct xfs_inode *ip) +{ +xfs_iunlock (ip,XFS_ILOCK_EXCL ); +xfs_irele (ip); +} +/* * Freeze any deferred ops and commit the transaction. This is the last step * needed to finish a log intent item that we recovered from the log, and will -* take care of releasing all the relevant resources. +* take care of releasing all the relevant resources. If we captured deferred +* ops, the inodes are attached to it and must not be touched. If not, we have +* to unlock and free them ourselves. */ int -xlog_recover_trans_commit ( +xlog_recover_trans_commit_inodes (struct xfs_trans *tp, -struct list_head *capture_list) +struct list_head *capture_list, +struct xfs_inode *ip1, +struct xfs_inode *ip2) { -return xfs_defer_capture (tp, capture_list); +int error; + + error =xfs_defer_capture (tp, capture_list, &ip1, &ip2); + +/* +* If we still have references to the inodes, either the capture +* process did nothing or it failed; either way, we still own the +* inodes, so unlock and release them. +*/ +if (ip2 && ip2 != ip1) +xlog_recover_irele (ip2); +if (ip1) +xlog_recover_irele (ip1); +return error; }/******************************************************************************
fs/xfs/xfs_trans.h +0 -6
@@ -96,12 +96,6 @@ void xfs_log_item_init(struct xfs_mount *mp, struct xfs_log_item *item,#define XFS_ITEM_LOCKED 2#define XFS_ITEM_FLUSHING 3 -/* -* Deferred operation item relogging limits. -*/ -#define XFS_DEFER_OPS_NR_INODES 2/* join up to two inodes */ -#define XFS_DEFER_OPS_NR_BUFS 2/* join up to two buffers */ -/* * This is the structure maintained for every active transaction. */