[PATCH] jfs: fix out-of-bounds write in jfs_readdir() with multibyte names

From: Shuai Liu

Date: Wed Dec 02 2026 - 23:35:36 EST


jfs_readdir() converts each on-disk UTF-16 name into a struct jfs_dirent
in a PAGE_SIZE buffer and only fills the next entry when it believes it
fits:

if (((long) jfs_dirent + d->namlen + 1) >
((long)dirent_buf + PAGE_SIZE)) {

The check undercounts twice. The name is written at jfs_dirent->name,
past the 14-byte header, not at jfs_dirent. And d->namlen counts UTF-16
units, while jfs_strfromUCS_le() lets codepage->uni2char() emit up to
NLS_MAX_CHARSET_SIZE bytes per unit; with iocharset=utf8 a non-ASCII
unit takes two or three bytes. An entry that passes the check can thus
be written past the end of the buffer, which KASAN reports as a
slab-out-of-bounds write in utf32_to_utf8():

BUG: KASAN: slab-out-of-bounds in utf32_to_utf8+0x24d/0x3d0 fs/nls/nls_base.c:111
Write of size 1 at addr ffff888039ad7000 by task syz-executor304/5946
utf32_to_utf8+0x24d/0x3d0 fs/nls/nls_base.c:111
uni2char+0x35/0xa0 fs/nls/nls_utf8.c:21
jfs_strfromUCS_le+0xd2/0x3d0 fs/jfs/jfs_unicode.c:31
jfs_readdir+0x16fd/0x33f0 fs/jfs/jfs_dtree.c:2984
The buggy address is located 0 bytes to the right of
allocated 4096-byte region

Check against the worst case instead: the name starts at
jfs_dirent->name and each unit can take NLS_MAX_CHARSET_SIZE bytes.
The largest entry is 255 * NLS_MAX_CHARSET_SIZE +
sizeof(struct jfs_dirent) bytes, so an empty buffer always takes at
least one entry and readdir keeps making progress.

Two fixes for this bug were posted
(https://lore.kernel.org/all/20261006072656.11800-1-kartikey406@xxxxxxxxx/
and
https://lore.kernel.org/all/20261006124112.1703-1-yogeshgaur.83@xxxxxxxxx/)
but received no maintainer response in over two months and the bug is
still reproducing. The buffer-fit check follows Yogesh's patch, which
anchors the bound at jfs_dirent->name.

Verified with the syzbot C reproducer under QEMU with KASAN: the
unpatched kernel reports slab-out-of-bounds in utf32_to_utf8 via
jfs_readdir(), the patched kernel runs the same reproducer cleanly
(two runs, no KASAN/Oops/WARNING).

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-by: syzbot+a2748ba908c108e7e525@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=a2748ba908c108e7e525
Suggested-by: Yogesh Gaur <yogeshgaur.83@xxxxxxxxx>
Signed-off-by: Shuai Liu <847807867@xxxxxx>
---
fs/jfs/jfs_dtree.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/jfs/jfs_dtree.c b/fs/jfs/jfs_dtree.c
index 8ce6e4458cc25..7de2faf33ff99 100644
--- a/fs/jfs/jfs_dtree.c
+++ b/fs/jfs/jfs_dtree.c
@@ -2910,9 +2910,9 @@ int jfs_readdir(struct file *file, struct dir_context *ctx)

d = (struct ldtentry *) & p->slot[stbl[i]];

- if (((long) jfs_dirent + d->namlen + 1) >
- ((long)dirent_buf + PAGE_SIZE)) {
- /* DBCS codepages could overrun dirent_buf */
+ if ((long)jfs_dirent->name +
+ d->namlen * NLS_MAX_CHARSET_SIZE + 1 >
+ (long)dirent_buf + PAGE_SIZE) {
index = i;
overflow = 1;
break;
--
2.43.0