[xenial 4.4.0-188.218][PATCH 2/2] f2fs: fix to avoid memory leakage in f2fs_listxattr

Benjamin M Romer benjamin.romer at canonical.com
Mon Aug 10 14:57:24 UTC 2020


From: Randall Huang <huangrandall at google.com>

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 at google.com>
Reviewed-by: Chao Yu <yuchao0 at huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk at kernel.org>

CVE-2020-0067
(backported from commit 688078e7f36c293dae25b338ddc9e0a2790f6e06)
[ ben_r: modified error code to older value ]
Signed-off-by: Benjamin M Romer <benjamin.romer at canonical.com>
---
 fs/f2fs/xattr.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/xattr.c b/fs/f2fs/xattr.c
index 80e98235720e..3143546d1392 100644
--- a/fs/f2fs/xattr.c
+++ b/fs/f2fs/xattr.c
@@ -451,8 +451,9 @@ cleanup:
 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
 {
 	struct inode *inode = d_inode(dentry);
+	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
 	struct f2fs_xattr_entry *entry;
-	void *base_addr;
+	void *base_addr, *last_base_addr;
 	int error = 0;
 	size_t rest = buffer_size;
 
@@ -460,11 +461,23 @@ ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
 	if (!base_addr)
 		return -ENOMEM;
 
+	last_base_addr = (void *)base_addr + XATTR_SIZE(xnid, inode);
+
 	list_for_each_xattr(entry, base_addr) {
 		const struct xattr_handler *handler =
 			f2fs_xattr_handler(entry->e_name_index);
 		size_t size;
 
+		if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
+			(void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
+			f2fs_msg(F2FS_I_SB(inode), KERN_ERR,
+				 "inode (%lu) has corrupted xattr",
+				inode->i_ino);
+			set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
+			error = -EUCLEAN;
+			goto cleanup;
+		}
+
 		if (!handler)
 			continue;
 
-- 
2.25.1




More information about the kernel-team mailing list