ocfs2: validate xattr entry count in ocfs2_xattr_list_entries
PATCH 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.
--- fs/ocfs2/xattr.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
fs/ocfs2/xattr.c +16 -1
@@ -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 -EUCLEAN ; + } + +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
Heming Zhao
reviewerQuoted 36 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_countvalue 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_countfrom disk in array indexing operations like &header->xh_entries[i], which can point outside the block whenxh_countis corrupted, triggering KASAN use-after-free detection.Reported-by [email protected]Tested-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- fs/ocfs2/xattr.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)fs/ocfs2/xattr.c +5
-0@@ -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 -EUCLEAN ;
The meaning of EUCLEAN doesn't align with the code logic; it appears that EFAULT or ENOMEM would be more appropriate.
The others look good to me.
Thanks,
Heming
Quoted 10 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
Hi Heming,
Thanks for the review. I will send Patch v2 soon with the required changes.
Thanks
Hi Heming,
Thank you for reviewing the patch! I've sent v2, but I used -EFSCORRUPTED instead of -EFAULT/-ENOMEM. Here's my reasoning:
This code is detecting on-disk filesystem corruption (invalid xh_count value from disk), not a memory error or allocation failure. Using -EFSCORRUPTED:
1. Matches the existing error handling in ocfs2_xattr_find_entry() for similar xattr corruption detection
2. Provides clearer semantics (corruption vs generic fault)
3. Is specifically designed for filesystem corruption cases
I'm happy to change it to -EFAULT if you still prefer, but wanted to explain my reasoning. What do you think?
Thanks
Deepanshu
Heming Zhao
reviewerQuoted 14 lines · Deepanshu Kartikey
Hi Heming,
Thank you for reviewing the patch! I've sent v2, but I used
-EFSCORRUPTEDinstead of -EFAULT/-ENOMEM. Here's my reasoning:This code is detecting on-disk filesystem corruption (invalid
xh_countvalue from disk), not a memory error or allocation failure. Using-EFSCORRUPTED:1. Matches the existing error handling in
ocfs2_xattr_find_entry()for similar xattr corruption detection
2. Provides clearer semantics (corruption vs generic fault)
3. Is specifically designed for filesystem corruption cases
Quoted 6 lines
I'm happy to change it to
-EFAULTif you still prefer, but wanted to explain my reasoning. What do you think?Thanks
Deepanshu
I've noticed that you frequently use separate emails to explain the review comments. I advise against this practice, as this style loses context and makes it difficult for other reviewers to follow what's happening.
Another piece of info (which you may already know) is that the kernel mailing list prefers inline and bottom posting, and discourages top posting.
Thanks
Heming