From 7cc2cc9ac19afe9bf556a3876e9a541032546ef2 Mon Sep 17 00:00:00 2001 From: Jaegeuk Kim Date: Tue, 19 Nov 2019 17:46:47 +0800 Subject: [PATCH] f2fs: check memory boundary by insane namelen mainline inclusion from mainline-v5.0-rc1 commit 4e240d1bab1ead280ddf5eb05058dba6bbd57d10 category: bugfix bugzilla: 13690 CVE: CVE-2019-9445 ------------------------------------------------- If namelen is corrupted to have very long value, fill_dentries can copy wrong memory area. Reviewed-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Yang Yingliang Reviewed-by: Wei Fang Signed-off-by: Yang Yingliang --- fs/f2fs/dir.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c index bd0348cc860f..36961c9b3163 100644 --- a/fs/f2fs/dir.c +++ b/fs/f2fs/dir.c @@ -810,6 +810,17 @@ int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d, de_name.name = d->filename[bit_pos]; de_name.len = le16_to_cpu(de->name_len); + /* check memory boundary before moving forward */ + bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); + if (unlikely(bit_pos > d->max)) { + f2fs_msg(sbi->sb, KERN_WARNING, + "%s: corrupted namelen=%d, run fsck to fix.", + __func__, le16_to_cpu(de->name_len)); + set_sbi_flag(sbi, SBI_NEED_FSCK); + err = -EINVAL; + goto out; + } + if (f2fs_encrypted_inode(d->inode)) { int save_len = fstr->len; @@ -832,7 +843,6 @@ int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d, if (readdir_ra) f2fs_ra_node_page(sbi, le32_to_cpu(de->ino)); - bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); ctx->pos = start_pos + bit_pos; } out: -- GitLab