Skip to contentSkip to conversation

ocfs2: validate xattr entry count in ocfs2_xattr_list_entries

PATCH In review Deepanshu Kartikey 2 participants 3 messages Nov 17, 2025 OCFS2

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ocfs2: validate xattr entry count in ocfs2_xattr_list_entries

Add validation of xattr entry count before accessing entries to prevent out-of-bounds array access and use-after-free bugs. A corrupted filesystem with an invalid xh_count value can cause the loop to access memory beyond the allocated block, potentially reaching freed memory pages.

The validation calculates the maximum number of entries that can fit in the available space and rejects counts that exceed this limit. This prevents the subsequent loop from accessing invalid memory addresses.

Without this check, the code directly uses xh_count from disk in array indexing operations like &header->xh_entries[i], which can point outside the block when xh_count is corrupted, triggering KASAN use-after-free detection.

---
Changes in v2:
- Changed return value from -EUCLEAN to -EFSCORRUPTED for consistency with existing xattr error handling (suggested by Heming Zhao)

 fs/ocfs2/xattr.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
fs/ocfs2/xattr.c +16 -1
index d70a20d29e3e..f7341e3d8a6c 100644
@@ -928,8 +928,23 @@ static int ocfs2_xattr_list_entries(struct inode *inode,
 	size_t result = 0;
 	int i, type, ret;
 	const char *name;
+	u16 count;
+	size_t max_entries;
+	struct super_block *sb = inode->i_sb;
+
+	count = le16_to_cpu(header->xh_count);
+	max_entries = (sb->s_blocksize - sizeof(struct ocfs2_xattr_header)) /
+		       sizeof(struct ocfs2_xattr_entry);
 
-	for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
+	if (count > max_entries) {
+		ocfs2_error(sb,
+			    "xattr entry count %u exceeds maximum %zu in inode %llu\n",
+			    count, max_entries,
+			    (unsigned long long)OCFS2_I(inode)->ip_blkno);
+		return -EFSCORRUPTED;
+	}
+
+	for (i = 0; i < count; i++) {
 		struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
 		type = ocfs2_xattr_get_type(entry);
 		name = (const char *)header +
Signature · 1 line
2.43.0
reviewer
#1 lore raw
Quoted 42 lines · Deepanshu Kartikey

Add validation of xattr entry count before accessing entries to prevent out-of-bounds array access and use-after-free bugs. A corrupted filesystem with an invalid xh_count value can cause the loop to access memory beyond the allocated block, potentially reaching freed memory pages.

The validation calculates the maximum number of entries that can fit in the available space and rejects counts that exceed this limit. This prevents the subsequent loop from accessing invalid memory addresses.

Without this check, the code directly uses xh_count from disk in array indexing operations like &header->xh_entries[i], which can point outside the block when xh_count is corrupted, triggering KASAN use-after-free detection.

---
Changes in v2:
- Changed return value from -EUCLEAN to -EFSCORRUPTED for consistency with existing xattr error handling (suggested by Heming Zhao)

 fs/ocfs2/xattr.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
fs/ocfs2/xattr.c +5 -0
index d70a20d29e3e..f7341e3d8a6c 100644
@@ -928,8 +928,23 @@ static int ocfs2_xattr_list_entries(struct inode *inode,
 	size_t result = 0;
 	int i, type, ret;
 	const char *name;
+	u16 count;
+	size_t max_entries;
+	struct super_block *sb = inode->i_sb;
+
+	count = le16_to_cpu(header->xh_count);

fs/ocfs2/xattr.c static int ocfs2_xattr_list_entries(struct inode *inode, · line 944

Quoted 10 lines
fs/ocfs2/xattr.c +8 -1
+	max_entries = (sb->s_blocksize - sizeof(struct ocfs2_xattr_header)) /
+		       sizeof(struct ocfs2_xattr_entry);
 
-	for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
+	if (count > max_entries) {
+		ocfs2_error(sb,
+			    "xattr entry count %u exceeds maximum %zu in inode %llu\n",
+			    count, max_entries,
+			    (unsigned long long)OCFS2_I(inode)->ip_blkno);
+		return -EFSCORRUPTED;

The errno (-EFSCORRUPTED) looks good to me.

Sorry for my carelessness in reviewing the v1 patch.
It seems the patch code only handles the xattr inline mode. For the xattr extra block mode, the calacating above for the max_entries is wrong. Btw, even for inline mode, the codes are also need to be revised. Please check the 'header' value in the caller ocfs2_xattr_ibody_list().
For this syzbot bug, you can put the sanity check code in ocfs2_xattr_ibody_list(). For the extra xattr block mode, ocfs2_validate_xattr_block() seems like a good place to put the fix.

For the minimal code change to satisfy syzbot, we can just put the santity check in ocfs2_xattr_ibody_list().

Thanks,
Heming

Quoted 9 lines
fs/ocfs2/xattr.c +3 -0
+	}
+
+	for (i = 0; i < count; i++) {
 		struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
 		type = ocfs2_xattr_get_type(entry);
 		name = (const char *)header +
Signature · 1 line
2.43.0