Skip to content

Commit 222055e

Browse files
hsiangkaogregkh
authored andcommitted
erofs: fix the out-of-bounds nameoff handling for trailing dirents
commit d18a3b5 upstream. Currently we already have boundary-checks for nameoffs, but the trailing dirents are special since the namelens are calculated with strnlen() with unchecked nameoffs. If a crafted EROFS has a trailing dirent with nameoff >= maxsize, maxsize - nameoff can underflow, causing strnlen() to read past the directory block. nameoff0 should also be verified to be a multiple of `sizeof(struct erofs_dirent)` as well [1]. [1] https://sashiko.dev/#/patchset/20260416063511.3173774-1-hsiangkao%40linux.alibaba.com Fixes: 3aa8ec7 ("staging: erofs: add directory operations") Fixes: 33bac91 ("staging: erofs: keep corrupted fs from crashing kernel in erofs_readdir()") Reported-by: Yuhao Jiang <danisjiang@gmail.com> Reported-by: Junrui Luo <moonafterrain@outlook.com> Closes: https://lore.kernel.org/r/A0FD7E0F-7558-49B0-8BC8-EB1ECDB2479A@outlook.com Cc: stable@vger.kernel.org Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com> Reviewed-by: Chao Yu <chao@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8555d69 commit 222055e

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

fs/erofs/dir.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,18 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
2222
nameoff = le16_to_cpu(de->nameoff);
2323
de_name = (char *)dentry_blk + nameoff;
2424

25-
/* the last dirent in the block? */
26-
if (de + 1 >= end)
27-
de_namelen = strnlen(de_name, maxsize - nameoff);
28-
else
25+
/* non-trailing dirent in the directory block? */
26+
if (de + 1 < end)
2927
de_namelen = le16_to_cpu(de[1].nameoff) - nameoff;
28+
else if (maxsize <= nameoff)
29+
goto err_bogus;
30+
else
31+
de_namelen = strnlen(de_name, maxsize - nameoff);
3032

31-
/* a corrupted entry is found */
32-
if (nameoff + de_namelen > maxsize ||
33-
de_namelen > EROFS_NAME_LEN) {
34-
erofs_err(dir->i_sb, "bogus dirent @ nid %llu",
35-
EROFS_I(dir)->nid);
36-
DBG_BUGON(1);
37-
return -EFSCORRUPTED;
38-
}
33+
/* a corrupted entry is found (including negative namelen) */
34+
if (!in_range32(de_namelen, 1, EROFS_NAME_LEN) ||
35+
nameoff + de_namelen > maxsize)
36+
goto err_bogus;
3937

4038
if (!dir_emit(ctx, de_name, de_namelen,
4139
le64_to_cpu(de->nid), d_type))
@@ -44,6 +42,10 @@ static int erofs_fill_dentries(struct inode *dir, struct dir_context *ctx,
4442
ctx->pos += sizeof(struct erofs_dirent);
4543
}
4644
return 0;
45+
err_bogus:
46+
erofs_err(dir->i_sb, "bogus dirent @ nid %llu", EROFS_I(dir)->nid);
47+
DBG_BUGON(1);
48+
return -EFSCORRUPTED;
4749
}
4850

4951
static int erofs_readdir(struct file *f, struct dir_context *ctx)
@@ -72,7 +74,7 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)
7274
}
7375

7476
nameoff = le16_to_cpu(de->nameoff);
75-
if (nameoff < sizeof(struct erofs_dirent) || nameoff >= bsz) {
77+
if (!nameoff || nameoff >= bsz || (nameoff % sizeof(*de))) {
7678
erofs_err(sb, "invalid de[0].nameoff %u @ nid %llu",
7779
nameoff, EROFS_I(dir)->nid);
7880
err = -EFSCORRUPTED;

0 commit comments

Comments
 (0)