Re: [PATCH v2] xfs: fix a UAF when inode item push

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Jul 26, 2023 at 03:32:19PM +0800, Long Li wrote:
> On Wed, Jul 26, 2023 at 02:27:04PM +1000, Dave Chinner wrote:
> > On Sat, Jul 22, 2023 at 10:57:21AM +0800, Long Li wrote:
> > > Fix the race condition by add XFS_ILOCK_SHARED lock for inode in
> > > xfs_inode_item_push(). The XFS_ILOCK_EXCL lock is held when the inode is
> > > reclaimed, so this prevents the uaf from occurring.
> > 
> > Having reclaim come in and free the inode after we've already
> > aborted and removed from the buffer isn't a problem. The inode
> > flushing code is designed to handle that safely.
> > 
> > The problem is that xfs_inode_item_push() tries to use the inode
> > item after the failure has occurred and we've already aborted the
> > inode item and finished it. i.e. the problem is this line:
> > 
> > 	spin_lock(&lip->li_ailp->ail_lock);
> > 
> > because it is using the log item that has been freed to get the
> > ailp. We can safely store the alip at the start of the function
> > whilst we still hold the ail_lock.
> > 
> 
> Hi Dave,
> 
> That's how I solved it in v1[1], but I found that it doesn't completely
> solve the problem, because it's still possible to reference the freed
> lip in xfsaild_push(). Unless we don't refer to lip in tracepoint after
> xfsaild_push_item().
> 
> xfsaild_push()
>   xfsaild_push_item()
>     lip->li_ops->iop_push()
>       xfs_inode_item_push(lip)
> 	xfs_iflush_cluster(bp)
> 				......
> 					xfs_reclaim_inode(ip)
> 					  ......
> 					  __xfs_inode_free(ip)
> 					    call_rcu(ip, xfs_inode_free_callback)
> 				......		
> 			<rcu grace period expires>
> 			<rcu free callbacks run somewhere>
> 			  xfs_inode_free_callback(ip)
> 			    kmem_cache_free(ip->i_itemp)
> 				......
>   trace_xfs_ail_xxx(lip) //uaf

Then we need to fix that UAF, too!

Seriously: just because a reclaim race exposes more than one UAF
vector, it doesn't mean that the we need to completely change the
lock order and life-cycle/existence guarantees for that object. It
means we need to look at the wider situation and determine if there
are other vectors to those same UAF conditions.

Such as: what happens if other types of log items have the same sort
of "can be freed before returning" situation?

Really, all this means is that returning XFS_ITEM_LOCKED when we've
dropped the AIL lock in .iop_push() is not safe - there is no longer
any guarantee the item is still in the AIL or even whether it exists
on return to xfsaild_push(), so it's just not safe to reference
there at all.

Indeed, I note that xfs_qm_dquot_logitem_push() has exactly the same
potential UAF problem as xfs_inode_item_push() has. i.e. it also
drops the AIL lock to do a flush, which can abort the item and
remove it from the AIL on failure while the AIL lock is dropped,
then it grabs the AIL lock from the log item and returns
XFS_ITEM_LOCKED....

So, yeah, these failure cases need to return something different to
xfsaild_push() so it knows that it is unsafe to reference the log
item, even for tracing purposes. And, further,
xfs_qm_dquot_logitem_push() needs the same "grab ailp before
dropping the ail_lock" for when it is relocking the AIL....

-Dave.
-- 
Dave Chinner
david@xxxxxxxxxxxxx



[Index of Archives]     [XFS Filesystem Development (older mail)]     [Linux Filesystem Development]     [Linux Audio Users]     [Yosemite Trails]     [Linux Kernel]     [Linux RAID]     [Linux SCSI]


  Powered by Linux