Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

erofs: fix the out-of-bounds nameoff handling for trailing dirents

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: 3aa8ec716e52 ("staging: erofs: add directory operations")
Fixes: 33bac912840f ("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>

+15 -13
+15 -13
fs/erofs/dir.c
··· 19 19 const char *de_name = (char *)dentry_blk + nameoff; 20 20 unsigned int de_namelen; 21 21 22 - /* the last dirent in the block? */ 23 - if (de + 1 >= end) 24 - de_namelen = strnlen(de_name, maxsize - nameoff); 25 - else 22 + /* non-trailing dirent in the directory block? */ 23 + if (de + 1 < end) 26 24 de_namelen = le16_to_cpu(de[1].nameoff) - nameoff; 25 + else if (maxsize <= nameoff) 26 + goto err_bogus; 27 + else 28 + de_namelen = strnlen(de_name, maxsize - nameoff); 27 29 28 - /* a corrupted entry is found */ 29 - if (nameoff + de_namelen > maxsize || 30 - de_namelen > EROFS_NAME_LEN) { 31 - erofs_err(dir->i_sb, "bogus dirent @ nid %llu", 32 - EROFS_I(dir)->nid); 33 - DBG_BUGON(1); 34 - return -EFSCORRUPTED; 35 - } 30 + /* a corrupted entry is found (including negative namelen) */ 31 + if (!in_range32(de_namelen, 1, EROFS_NAME_LEN) || 32 + nameoff + de_namelen > maxsize) 33 + goto err_bogus; 36 34 37 35 if (!dir_emit(ctx, de_name, de_namelen, 38 36 erofs_nid_to_ino64(EROFS_SB(dir->i_sb), ··· 40 42 ctx->pos += sizeof(struct erofs_dirent); 41 43 } 42 44 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; 43 49 } 44 50 45 51 static int erofs_readdir(struct file *f, struct dir_context *ctx) ··· 90 88 } 91 89 92 90 nameoff = le16_to_cpu(de->nameoff); 93 - if (nameoff < sizeof(struct erofs_dirent) || nameoff >= bsz) { 91 + if (!nameoff || nameoff >= bsz || (nameoff % sizeof(*de))) { 94 92 erofs_err(sb, "invalid de[0].nameoff %u @ nid %llu", 95 93 nameoff, EROFS_I(dir)->nid); 96 94 err = -EFSCORRUPTED;