Skip to contentSkip to conversation

f2fs: Fix slab-out-of-bounds Read KASAN bug in f2fs_getxattr()

PATCH qasdev 2 participants 2 messages Jan 8, 2025 – Jan 9, 2025

lore
Share & tools
lore

qasdev

author
#0 lore raw
Quoted 20 lines · Chao Yu

Hi Qasim,

Quoted 10 lines · qasdev

In f2fs_getxattr(), the function lookup_all_xattrs() allocates a 12-byte (base_size) buffer for an inline extended attribute. However, when __find_inline_xattr() calls __find_xattr(), it uses the macro "list_for_each_xattr(entry, addr)", which starts by calling XATTR_FIRST_ENTRY(addr). This skips a 24-byte struct f2fs_xattr_header at the beginning of the buffer, causing an immediate out-of-bounds read in a 12-byte allocation. The subsequent !IS_XATTR_LAST_ENTRY(entry) check then dereferences memory outside the allocated region, triggering the slab-out-of bounds read.

Quoted 3 lines

This patch prevents the out-of-bounds read by adding a check to bail out early if inline_size is too small and does not account for the header plus the 4-byte value that IS_XATTR_LAST_ENTRY reads.

Thank you very much for analyzing this issue, the root cause you figured out makes sense to me.

Quoted 6 lines

Can you please check the patch in below link? It seems it can fix this issue as well? IIUC.

https://lore.kernel.org/linux-f2fs-devel/[email protected]/

Thanks,

Hi Chao,

I tested the patch you linked on my machine and with syzbot, and both tests succeeded. The patch you linked works very well.

Here is the link to the results of the testing of both patches: https://syzkaller.appspot.com/bug?extid=f5e74075e096e757bdbf

Would it be possible to include me in the Tested-by header and any other contribution acknowledgments you feel appropriate?

Thanks!

Best regards,
Qasim

Quoted 26 lines
Quoted 25 lines
Fixes 388a2a0640e1 ("f2fs: remove redundant sanity check in sanity_check_inode()")
---
  fs/f2fs/xattr.c | 3 +++
  1 file changed, 3 insertions(+)
fs/f2fs/xattr.c +3 -0
index 3f3874943679..cf82646bca0e 100644
@@ -329,6 +329,9 @@ static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
  	if (!xnid && !inline_size)
  		return -ENODATA;
+	if (inline_size < sizeof(struct f2fs_xattr_header) + sizeof(__u32))
+		return -ENODATA;
+
  	*base_size = XATTR_SIZE(inode) + XATTR_PADDING_SIZE;
  	txattr_addr = xattr_alloc(F2FS_I_SB(inode), *base_size, is_inline);
  	if (!txattr_addr)
#1 lore raw
Quoted 24 lines · qasdev
Quoted 23 lines · Chao Yu

Hi Qasim,

Quoted 10 lines · qasdev

In f2fs_getxattr(), the function lookup_all_xattrs() allocates a 12-byte (base_size) buffer for an inline extended attribute. However, when __find_inline_xattr() calls __find_xattr(), it uses the macro "list_for_each_xattr(entry, addr)", which starts by calling XATTR_FIRST_ENTRY(addr). This skips a 24-byte struct f2fs_xattr_header at the beginning of the buffer, causing an immediate out-of-bounds read in a 12-byte allocation. The subsequent !IS_XATTR_LAST_ENTRY(entry) check then dereferences memory outside the allocated region, triggering the slab-out-of bounds read.

Quoted 3 lines

This patch prevents the out-of-bounds read by adding a check to bail out early if inline_size is too small and does not account for the header plus the 4-byte value that IS_XATTR_LAST_ENTRY reads.

Thank you very much for analyzing this issue, the root cause you figured out makes sense to me.

Can you please check the patch in below link? It seems it can fix this issue as well? IIUC.

Quoted 7 lines
Quoted 3 lines

https://lore.kernel.org/linux-f2fs-devel/[email protected]/

Thanks,

Hi Chao,

I tested the patch you linked on my machine and with syzbot, and both tests succeeded. The patch you linked works very well.

Hi Qasdev,

Thanks for the test!

Quoted 15 lines

Here is the link to the results of the testing of both patches: https://syzkaller.appspot.com/bug?extid=f5e74075e096e757bdbf

Would it be possible to include me in the Tested-by header and any other contribution acknowledgments you feel appropriate?

Quoted 1 line

Thanks!

Best regards,
Qasim

Quoted 6 lines
Quoted 6 lines

IMO, it will be better to quoted your comment description and all above tags into the patch, what do you think?

Thanks,

Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Fixes 388a2a0640e1 ("f2fs: remove redundant sanity check in sanity_check_inode()")
---
   fs/f2fs/xattr.c | 3 +++
   1 file changed, 3 insertions(+)
fs/f2fs/xattr.c +3 -0
index 3f3874943679..cf82646bca0e 100644
@@ -329,6 +329,9 @@ static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
   	if (!xnid && !inline_size)
   		return -ENODATA;
+	if (inline_size < sizeof(struct f2fs_xattr_header) + sizeof(__u32))
+		return -ENODATA;
+
   	*base_size = XATTR_SIZE(inode) + XATTR_PADDING_SIZE;
   	txattr_addr = xattr_alloc(F2FS_I_SB(inode), *base_size, is_inline);
   	if (!txattr_addr)