Skip to contentSkip to conversation

xfs: don't allow UAF in xlog_recover_iget

PATCH In review Darrick J. Wong 3 participants 8 messages Mar 19 – Mar 23 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 xfs: don't allow UAF in xlog_recover_iget

From: Darrick J. Wong <[email protected]>

Fix this function to avoid touching the passed in ipp argument until we're 100% certain that we're returning zero. This avoids creating a dangling pointer in the caller. The xfs_irele call in xfs_attri_recover_work was never correct and should go away.

Cc <[email protected]> # v5.15
Fixes 4bc619833f738f ("xfs: refactor xfs_iget calls from log intent recovery")
---
 fs/xfs/xfs_log_recover.c |   12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
fs/xfs/xfs_log_recover.c +7 -5
index 09e6678ca4878e..0e91a62348eb79 100644
@@ -1745,21 +1745,23 @@ xlog_recover_iget(
 	xfs_ino_t		ino,
 	struct xfs_inode	**ipp)
 {
+	struct xfs_inode	*ip;
 	int			error;
 
-	error = xfs_iget(mp, NULL, ino, 0, 0, ipp);
+	error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
 	if (error)
 		return error;
 
-	error = xfs_qm_dqattach(*ipp);
+	error = xfs_qm_dqattach(ip);
 	if (error) {
-		xfs_irele(*ipp);
+		xfs_irele(ip);
 		return error;
 	}
 
-	if (VFS_I(*ipp)->i_nlink == 0)
-		xfs_iflags_set(*ipp, XFS_IRECOVERY);
+	if (VFS_I(ip)->i_nlink == 0)
+		xfs_iflags_set(ip, XFS_IRECOVERY);
 
+	*ipp = ip;
 	return 0;
 }
 
#1 lore raw

PATCH 2/2 xfs: don't irele after failing to iget in xfs_attri_recover_work

From: Darrick J. Wong <[email protected]>

xlog_recovery_iget* never set @ip to a valid pointer if they return zero, so this irele will walk off a dangling pointer. Fix that.

Cc <[email protected]> # v6.10
Fixes ae673f534a3097 ("xfs: record inode generation in xattr update log intent items")
---
 fs/xfs/xfs_attr_item.c |    1 -
 1 file changed, 1 deletion(-)
fs/xfs/xfs_attr_item.c +0 -1
index 354472bf45f145..fe909bc44c3a79 100644
@@ -653,7 +653,6 @@ xfs_attri_recover_work(
 		break;
 	}
 	if (error) {
-		xfs_irele(ip);
 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, attrp,
 				sizeof(*attrp));
 		return ERR_PTR(-EFSCORRUPTED);
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] xfs: don't irele after failing to iget in xfs_attri_recover_work

Quoted 18 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

xlog_recovery_iget* never set @ip to a valid pointer if they return zero, so this irele will walk off a dangling pointer. Fix that.

Cc <[email protected]> # v6.10
Fixes ae673f534a3097 ("xfs: record inode generation in xattr update log intent items")
---
 fs/xfs/xfs_attr_item.c |    1 -
 1 file changed, 1 deletion(-)
fs/xfs/xfs_attr_item.c +0 -0
index 354472bf45f145..fe909bc44c3a79 100644

fs/xfs/xfs_attr_item.c xfs_attri_recover_work( · line 658

Quoted 7 lines
fs/xfs/xfs_attr_item.c +0 -1
@@ -653,7 +653,6 @@ xfs_attri_recover_work(
 		break;
 	}
 	if (error) {
-		xfs_irele(ip);
 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, attrp,
 				sizeof(*attrp));
 		return ERR_PTR(-EFSCORRUPTED);

Looks good

Reviewed-by on v1 1/2
#4 lore raw
In reply to Darrick J. Wong
Quoted 38 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Fix this function to avoid touching the passed in ipp argument until we're 100% certain that we're returning zero. This avoids creating a dangling pointer in the caller. The xfs_irele call in xfs_attri_recover_work was never correct and should go away.

Cc <[email protected]> # v5.15
Fixes 4bc619833f738f ("xfs: refactor xfs_iget calls from log intent recovery")
---
 fs/xfs/xfs_log_recover.c |   12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
fs/xfs/xfs_log_recover.c +4 -3
index 09e6678ca4878e..0e91a62348eb79 100644
@@ -1745,21 +1745,23 @@ xlog_recover_iget(
 	xfs_ino_t		ino,
 	struct xfs_inode	**ipp)
 {
+	struct xfs_inode	*ip;
 	int			error;
 
-	error = xfs_iget(mp, NULL, ino, 0, 0, ipp);
+	error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
 	if (error)
 		return error;
 
-	error = xfs_qm_dqattach(*ipp);
+	error = xfs_qm_dqattach(ip);
 	if (error) {
-		xfs_irele(*ipp);
+		xfs_irele(ip);
 		return error;
 	}

fs/xfs/xfs_log_recover.c xlog_recover_iget( · line 1767

Quoted 10 lines
fs/xfs/xfs_log_recover.c +3 -2
 
-	if (VFS_I(*ipp)->i_nlink == 0)
-		xfs_iflags_set(*ipp, XFS_IRECOVERY);
+	if (VFS_I(ip)->i_nlink == 0)
+		xfs_iflags_set(ip, XFS_IRECOVERY);
 
+	*ipp = ip;
 	return 0;
 }
 

Looks good

#5 lore raw
In reply to Darrick J. Wong

"Don't allow UAF" reads rally giberrish.

Quoted 5 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Fix this function to avoid touching the passed in ipp argument until we're 100% certain that we're returning zero. This avoids creating a dangling pointer in the caller.

Why does this matter? The caller need to check the return value.

Quoted 2 lines

The xfs_irele call in
xfs_attri_recover_work was never correct and should go away.

No idea what this is supposed to say, seem unrelated to this patch.

#6 lore raw
Quoted 10 lines · Christoph Hellwig

"Don't allow UAF" reads rally giberrish.

Quoted 5 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Fix this function to avoid touching the passed in ipp argument until we're 100% certain that we're returning zero. This avoids creating a dangling pointer in the caller.

Why does this matter? The caller need to check the return value.

I think the fundamental problem here is that Long Li ran a static checker, and the static checker suggested that the callsite in xfs_attri_recover_work should be fixed by guarding the xfs_irele call in the error handler with a null pointer check. That's completely wrong, the xfs_irele should never have been put in the error path at all.

So the proper fix is the one in the next patch that removes the xfs_irele. At the same time, xlog_recover_iget should get fixed so that it never exposes stale pointers in the first place.

"Fix this function to avoid exposing a stale pointer to the caller when returning an error code after dqattach fails." ?

Quoted 4 lines
Quoted 2 lines

The xfs_irele call in
xfs_attri_recover_work was never correct and should go away.

No idea what this is supposed to say, seem unrelated to this patch.

Yeah, I'll just drop that.

--D

#7 lore raw
Quoted 7 lines · Darrick J. Wong
Quoted 1 line

Why does this matter? The caller need to check the return value.

I think the fundamental problem here is that Long Li ran a static checker, and the static checker suggested that the callsite in xfs_attri_recover_work should be fixed by guarding the xfs_irele call in the error handler with a null pointer check. That's completely wrong, the xfs_irele should never have been put in the error path at all.

Yes.

Quoted 3 lines

So the proper fix is the one in the next patch that removes the xfs_irele. At the same time, xlog_recover_iget should get fixed so that it never exposes stale pointers in the first place.

Agreed.

Quoted 2 lines

"Fix this function to avoid exposing a stale pointer to the caller when returning an error code after dqattach fails." ?

I'd just drop this patch.