From: Viacheslav Dubeyko <slava@dubeyko.com>
To: Shawn Shao <shawnq1@163.com>
Cc: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>,
Yangtao Li <frank.li@vivo.com>,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] hfs: validate B-tree record cursor before arithmetic
Date: Tue, 01 Dec 2026 15:33:30 -0800 [thread overview]
Message-ID: <2869f63754ce8c44e7af164a868525f24e087fb6.camel@dubeyko.com> (raw)
In-Reply-To: <179613469562.187341.11056225505429771913.1796134695.5-1-shawnq1@163.com>
On Tue, 2026-12-01 at 14:18 +0000, Shawn Shao wrote:
> Hello Slava,
>
> You are right that the v1 check was too late: the loop condition had
> already subtracted the record cursor before validating it. This
> version
I cannot accept the patch with such commit message. Please, send it as
formal patch.
> checks the record count and cursor before the arithmetic in both
> traversal
> directions. It preserves the -1 cursor used when forward traversal
> starts
> before the first record and avoids an unsigned comparison when moving
> backward from that cursor.
>
> The cycle image now returns 48 bytes for `.` and `..` on the first
> `getdents64()` call, then returns `-EIO` on the second call. The
> control
> image, which changes only the empty leaf's `next` link to zero,
> returns the
> dot entries and then `-ENOENT` promptly.
>
> Tested on Linux 7.4-rc4, commit
> 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046, tree
> 194227446ae61b4971eea1b7bed06856fe3cc477, with gcc 11.4.0 and QEMU
> 6.2.0.
> The x86-64 bzImage build completed. The initramfs, kernel
> configuration,
> updated patch, and console logs are collected here:
> https://gist.github.com/shaos26/06dc92e4840aef9766cd82b8c644fe16
>
> Changes in v2:
> - Check the record cursor before arithmetic in both traversal
> directions.
> - Preserve the initial -1 cursor for forward traversal and handle it
> without
> unsigned arithmetic in the backward path.
> - Widen the negative count before negating it.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Thanks,
> Shawn Shao
>
> Signed-off-by: Shawn Shao <shawnq1@163.com>
> ---
> diff --git a/fs/hfs/bfind.c b/fs/hfs/bfind.c
> index d56e47bdc517d..92dab12dff40a 100644
> --- a/fs/hfs/bfind.c
> +++ b/fs/hfs/bfind.c
> @@ -185,16 +185,34 @@ int hfs_brec_goto(struct hfs_find_data *fd, int
> cnt)
> struct hfs_btree *tree;
> struct hfs_bnode *bnode;
> int idx, res = 0;
> + u32 move;
> u16 off, len, keylen;
>
> bnode = fd->bnode;
> tree = bnode->tree;
>
> if (cnt < 0) {
> - cnt = -cnt;
> - while (cnt > fd->record) {
> - cnt -= fd->record + 1;
> - fd->record = bnode->num_recs - 1;
> + move = -(s64)cnt;
The move variable is u32 data type. What this -(s64)cnt means?
> + for (;;) {
I don't like the infinite loop. Please, return back the loop limited by
max value.
> + if (unlikely(!bnode->num_recs)) {
> + if (fd->record != -1 && fd->record
> != 0) {
> + res = -EIO;
> + goto out;
> + }
> + res = bnode->prev ? -EIO : -ENOENT;
> + goto out;
> + }
> + if (unlikely(fd->record < -1 ||
> + fd->record >= bnode->num_recs))
> {
> + res = -EIO;
> + goto out;
> + }
> + if (fd->record >= 0 && move <= (u32)fd-
> >record) {
> + fd->record -= move;
> + break;
> + }
> + if (fd->record >= 0)
> + move -= fd->record + 1;
The logic is complicated and not clear. Why do we check the same
variables multiple times and pretty for the same values?
> idx = bnode->prev;
> if (!idx) {
> res = -ENOENT;
> @@ -207,11 +225,30 @@ int hfs_brec_goto(struct hfs_find_data *fd, int
> cnt)
> bnode = NULL;
> goto out;
> }
> + fd->record = bnode->num_recs - 1;
> }
> - fd->record -= cnt;
> } else {
> - while (cnt >= bnode->num_recs - fd->record) {
> - cnt -= bnode->num_recs - fd->record;
> + move = cnt;
> + for (;;) {
> + if (unlikely(!bnode->num_recs)) {
> + if (fd->record != -1 && fd->record
> != 0) {
> + res = -EIO;
> + goto out;
> + }
> + res = bnode->next ? -EIO : -ENOENT;
> + goto out;
> + }
> + if (unlikely(fd->record < -1 ||
> + fd->record >= bnode->num_recs
> ||
> + (fd->record == -1 && !move))) {
> + res = -EIO;
> + goto out;
> + }
> + if (move < bnode->num_recs - fd->record) {
> + fd->record += move;
> + break;
> + }
> + move -= bnode->num_recs - fd->record;
You repeat the same code twice. Please, rework it by introducing some
function.
Thanks,
Slava.
>
> fd->record = 0;
> idx = bnode->next;
> if (!idx) {
> @@ -226,7 +263,6 @@ int hfs_brec_goto(struct hfs_find_data *fd, int
> cnt)
> goto out;
> }
> }
> - fd->record += cnt;
> }
>
> len = hfs_brec_lenoff(bnode, fd->record, &off);
prev parent reply other threads:[~2026-12-01 23:33 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 6:56 [PATCH] hfs: reject empty nodes in forward record traversal Shawn Shao
2026-11-30 19:47 ` Viacheslav Dubeyko
[not found] ` <fcb208044e60bbeb29eb8a6f7eeb137fd83227b.camel@dubeyko.com>
2026-12-01 14:18 ` [PATCH v2] hfs: validate B-tree record cursor before arithmetic Shawn Shao
2026-12-01 23:33 ` Viacheslav Dubeyko [this message]
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=2869f63754ce8c44e7af164a868525f24e087fb6.camel@dubeyko.com \
--to=slava@dubeyko.com \
--cc=frank.li@vivo.com \
--cc=glaubitz@physik.fu-berlin.de \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=shawnq1@163.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®