Re: [PATCH] ocfs2: fix listxattr handling when the buffer is full

Joseph Qi <[email protected]>
Newsgroups dev.linux.lists.ocfs2-devel,org.kernel.vger.linux-kernel
Message-ID <[email protected]>

On 4/10/26 12:03 PM, ZhengYuan Huang wrote:
> [BUG]
> If an OCFS2 inode has both inline and block-based xattrs, listxattr()
> can return a size larger than the caller's buffer when the inline names
> consume that buffer exactly.
> 
> kernel BUG at mm/usercopy.c:102!
> Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
> RIP: 0010:usercopy_abort+0xb7/0xd0 mm/usercopy.c:102
> Call Trace:
>  __check_heap_object+0xe3/0x120 mm/slub.c:8243
>  check_heap_object mm/usercopy.c:196 [inline]
>  __check_object_size mm/usercopy.c:250 [inline]
>  __check_object_size+0x5c5/0x780 mm/usercopy.c:215
>  check_object_size include/linux/ucopysize.h:22 [inline]
>  check_copy_size include/linux/ucopysize.h:59 [inline]
>  copy_to_user include/linux/uaccess.h:219 [inline]
>  listxattr+0xb0/0x170 fs/xattr.c:926
>  filename_listxattr fs/xattr.c:958 [inline]
>  path_listxattrat+0x137/0x320 fs/xattr.c:988
>  __do_sys_listxattr fs/xattr.c:1001 [inline]
>  __se_sys_listxattr fs/xattr.c:998 [inline]
>  __x64_sys_listxattr+0x7f/0xd0 fs/xattr.c:998
>  ...
> 
> [CAUSE]
> Commit 936b8834366e ("ocfs2: Refactor xattr list and remove
> ocfs2_xattr_handler().") replaced the old per-handler list accounting
> with ocfs2_xattr_list_entry(), but it kept using size == 0 to detect
> probe mode.
> 
> That assumption stops being true once ocfs2_listxattr() finishes the
> inline-xattr pass. If the inline names fill the caller buffer exactly,
> the block-xattr pass runs with a non-NULL buffer and a remaining size of
> zero. ocfs2_xattr_list_entry() then skips the bounds check, keeps
> counting block names, and returns a positive size larger than the
> supplied buffer.
> 
> [FIX]
> Detect probe mode by testing whether the destination buffer pointer is
> NULL instead of whether the remaining size is zero.
> 
> That restores the pre-refactor behavior and matches the OCFS2 getxattr
> helpers. Once the remaining buffer reaches zero while more names are
> left, the block-xattr pass now returns -ERANGE instead of reporting a
> size larger than the allocated list buffer.
> 
> Fixes: 936b8834366e ("ocfs2: Refactor xattr list and remove ocfs2_xattr_handler().")
> Signed-off-by: ZhengYuan Huang <[email protected]>

Looks good.
Reviewed-by: Joseph Qi <[email protected]>

> ---
>  fs/ocfs2/xattr.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c
> --- a/fs/ocfs2/xattr.c
> +++ b/fs/ocfs2/xattr.c
> @@ -906,8 +906,8 @@ static int ocfs2_xattr_list_entry(struct super_block *sb,
>  	prefix_len = strlen(prefix);
>  	total_len = prefix_len + name_len + 1;
>  	*result += total_len;
> 
> -	/* we are just looking for how big our buffer needs to be */
> -	if (!size)
> +	/* No buffer means we are only looking for the required size. */
> +	if (!buffer)
>  		return 0;
>  
>  	if (*result > size)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.