Skip to content

Commit 688078e

Browse files
Randall HuangJaegeuk Kim
authored andcommitted
f2fs: fix to avoid memory leakage in f2fs_listxattr
In f2fs_listxattr, there is no boundary check before memcpy e_name to buffer. If the e_name_len is corrupted, unexpected memory contents may be returned to the buffer. Signed-off-by: Randall Huang <huangrandall@google.com> Reviewed-by: Chao Yu <yuchao0@huawei.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent 9f701f6 commit 688078e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

fs/f2fs/xattr.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,9 @@ int f2fs_getxattr(struct inode *inode, int index, const char *name,
539539
ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
540540
{
541541
struct inode *inode = d_inode(dentry);
542+
nid_t xnid = F2FS_I(inode)->i_xattr_nid;
542543
struct f2fs_xattr_entry *entry;
543-
void *base_addr;
544+
void *base_addr, *last_base_addr;
544545
int error = 0;
545546
size_t rest = buffer_size;
546547

@@ -550,13 +551,24 @@ ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
550551
if (error)
551552
return error;
552553

554+
last_base_addr = (void *)base_addr + XATTR_SIZE(xnid, inode);
555+
553556
list_for_each_xattr(entry, base_addr) {
554557
const struct xattr_handler *handler =
555558
f2fs_xattr_handler(entry->e_name_index);
556559
const char *prefix;
557560
size_t prefix_len;
558561
size_t size;
559562

563+
if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
564+
(void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
565+
f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr",
566+
inode->i_ino);
567+
set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
568+
error = -EFSCORRUPTED;
569+
goto cleanup;
570+
}
571+
560572
if (!handler || (handler->list && !handler->list(dentry)))
561573
continue;
562574

0 commit comments

Comments
 (0)