Skip to contentSkip to conversation

ext4: validate EA inode i_nlink in ext4_xattr_inode_iget

PATCH Superseded Yun Zhou 1 participant 1 message Jun 13 ext4 file system

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Yun Zhou

author
#0 lore raw

PATCH v2 1/1 ext4: validate EA inode i_nlink in ext4_xattr_inode_iget

Validate EA inode i_nlink early in ext4_xattr_inode_iget() to convert a WARN_ONCE in ext4_xattr_inode_update_ref() into a graceful error return.

When a corrupted ext4 image has an EA inode with inconsistent i_nlink and ref_count values, the code currently allows it through and later hits WARN_ONCE when ref_count transitions cross the 0/1 boundary. While this is not a security or stability issue -- it only fires on crafted filesystem images and merely prints a call trace -- it is better handled as an early sanity check that returns -EFSCORRUPTED, consistent with how ext4 treats other on-disk corruption.

The valid states for an EA inode are:
- i_nlink=0, ref_count=0: orphaned, pending deletion
- i_nlink=1, ref_count>0: active, referenced

Reject any EA inode that does not match one of these states at iget time.

Reported-by [email protected]
Fixes dec214d00e0d ("ext4: xattr inode deduplication")
---
 fs/ext4/xattr.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
fs/ext4/xattr.c +17 -0
index 982a1f831e22..4deb17b7bcbe 100644
@@ -448,6 +448,23 @@ static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
 	}
 	ext4_xattr_inode_set_class(inode);
 
+	/*
+	 * EA inode has two valid states:
+	 *   i_nlink=0, ref_count=0: orphaned, pending deletion
+	 *   i_nlink=1, ref_count>0: active, referenced by one or more inodes
+	 * Anything else indicates on-disk corruption.
+	 */
+	if (inode->i_nlink > 1 ||
+	    (inode->i_nlink && !ext4_xattr_inode_get_ref(inode)) ||
+	    (!inode->i_nlink && ext4_xattr_inode_get_ref(inode))) {
+		ext4_error(parent->i_sb,
+			   "EA inode %lu has unexpected i_nlink=%u ref_count=%llu",
+			   ea_ino, inode->i_nlink,
+			   ext4_xattr_inode_get_ref(inode));
+		iput(inode);
+		return -EFSCORRUPTED;
+	}
+
 	/*
 	 * Check whether this is an old Lustre-style xattr inode. Lustre
 	 * implementation does not have hash validation, rather it has a
Signature · 1 line
2.43.0