From: Joseph Qi <joseph.qi@linux.alibaba.com>
To: Zhan Xusheng <zhanxusheng1024@gmail.com>,
Andrew Morton <akpm@linux-foundation.org>
Cc: Mark Fasheh <mark@fasheh.com>, Joel Becker <jlbec@evilplan.org>,
Junxiao Bi <junxiao.bi@oracle.com>,
Changwei Ge <gechangwei@live.cn>, Jun Piao <piaojun@huawei.com>,
Heming Zhao <heming.zhao@suse.com>,
ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org,
zhanxusheng@xiaomi.com
Subject: Re: [PATCH v2 1/2] ocfs2: bound-check dir entries in the readdir re-validation scan
Date: Tue, 11 Aug 2026 14:59:15 +0800 [thread overview]
Message-ID: <cdfcc004-b4fe-485b-8e22-8f8ca8c1ce68@linux.alibaba.com> (raw)
In-Reply-To: <20260811024337.3972976-2-zhanxusheng@xiaomi.com>
On 8/11/26 10:43 AM, Zhan Xusheng wrote:
> When the inode version changed since the last readdir(),
> ocfs2_dir_foreach_blk_el() re-scans the directory block from its start to
> relocate the current position:
>
> for (i = 0; i < sb->s_blocksize && i < offset; ) {
> de = (struct ocfs2_dir_entry *)(bh->b_data + i);
> if (le16_to_cpu(de->rec_len) < OCFS2_DIR_REC_LEN(1))
> break;
> i += le16_to_cpu(de->rec_len);
> }
>
> i walks the block on rec_len values taken from the block itself and the
> only thing tested is that rec_len is not too small, so a single bogus
> rec_len
> leaves i anywhere in the block, including its last OCFS2_DIR_REC_LEN(1) - 1
> bytes. @offset comes from ctx->pos, which userspace moves with lseek() on
> the directory fd, and decides how far the walk gets.
>
> Two bounds are missing, both of which ocfs2_check_dir_entry() applies for
> the emit loop below.
>
> de->rec_len sits at byte offset 8 within the entry, so dereferencing de in
> that tail reads past the s_blocksize buffer. ocfs2_check_dir_entry()
> declines to look at an entry that close to the end:
>
> size - buf_offset < OCFS2_DIR_REC_LEN(1)
>
> Nothing bounds i += rec_len either, so i can end up past the block. The
> emit loop that follows is guarded by offset < sb->s_blocksize and does not
> run, but
>
> offset = i;
> ctx->pos = (ctx->pos & ~((loff_t)sb->s_blocksize - 1)) | offset;
>
> runs first and ORs a value with bits above the block mask into ctx->pos,
> corrupting the block number readdir() resumes from.
> ocfs2_check_dir_entry() rejects that as "directory entry overrun":
>
> next_offset = buf_offset + rlen;
> ... next_offset > size
>
> Apply both bounds. For a consistent directory this changes nothing:
> entries are at least OCFS2_DIR_REC_LEN(1) bytes and do not cross the end of
> the block, so no valid entry is skipped.
>
> Found by the sashiko review tool; fix approach suggested by Joseph Qi.
>
> Link: https://sashiko.dev/#/patchset/20260806022044.167962-1-zhanxusheng@xiaomi.com
> Suggested-by: Joseph Qi <joseph.qi@linux.alibaba.com>
> Cc: Mark Fasheh <mark@fasheh.com>
> Cc: Joel Becker <jlbec@evilplan.org>
> Cc: Junxiao Bi <junxiao.bi@oracle.com>
> Cc: Changwei Ge <gechangwei@live.cn>
> Cc: Jun Piao <piaojun@huawei.com>
> Cc: Heming Zhao <heming.zhao@suse.com>
> Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
Looks fine.
Reviewed-by: Joseph Qi <joseph.qi@linux.alibaba.com>
> ---
> fs/ocfs2/dir.c | 12 ++++++++----
> 1 file changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/fs/ocfs2/dir.c b/fs/ocfs2/dir.c
> index c30a86856d5b..39ce60874e17 100644
> --- a/fs/ocfs2/dir.c
> +++ b/fs/ocfs2/dir.c
> @@ -1903,7 +1903,10 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode,
> * dirent right now. Scan from the start of the block
> * to make sure. */
> if (!inode_eq_iversion(inode, *f_version)) {
> - for (i = 0; i < sb->s_blocksize && i < offset; ) {
> + for (i = 0; i + OCFS2_DIR_REC_LEN(1) <= sb->s_blocksize &&
> + i < offset;) {
> + unsigned int rec_len;
> +
> de = (struct ocfs2_dir_entry *) (bh->b_data + i);
> /* It's too expensive to do a full
> * dirent test each time round this
> @@ -1911,10 +1914,11 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode,
> * least that it is non-zero. A
> * failure will be detected in the
> * dirent test below. */
> - if (le16_to_cpu(de->rec_len) <
> - OCFS2_DIR_REC_LEN(1))
> + rec_len = le16_to_cpu(de->rec_len);
> + if (rec_len < OCFS2_DIR_REC_LEN(1) ||
> + i + rec_len > sb->s_blocksize)
> break;
> - i += le16_to_cpu(de->rec_len);
> + i += rec_len;
> }
> offset = i;
> ctx->pos = (ctx->pos & ~((loff_t)sb->s_blocksize - 1))
next prev parent reply other threads:[~2026-08-11 6:59 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-11 2:43 [PATCH v2 0/2] ocfs2: bound-check both readdir re-validation scans Zhan Xusheng
2026-08-11 2:43 ` [PATCH v2 1/2] ocfs2: bound-check dir entries in the readdir re-validation scan Zhan Xusheng
2026-08-11 6:59 ` Joseph Qi [this message]
2026-08-12 2:22 ` Andrew Morton
2026-08-12 2:37 ` Zhan Xusheng
2026-08-11 2:43 ` [PATCH v2 2/2] ocfs2: bound-check dir entries in the inline-data " Zhan Xusheng
2026-08-11 6:59 ` Joseph Qi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cdfcc004-b4fe-485b-8e22-8f8ca8c1ce68@linux.alibaba.com \
--to=joseph.qi@linux.alibaba.com \
--cc=akpm@linux-foundation.org \
--cc=gechangwei@live.cn \
--cc=heming.zhao@suse.com \
--cc=jlbec@evilplan.org \
--cc=junxiao.bi@oracle.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mark@fasheh.com \
--cc=ocfs2-devel@lists.linux.dev \
--cc=piaojun@huawei.com \
--cc=zhanxusheng1024@gmail.com \
--cc=zhanxusheng@xiaomi.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®