[PATCH v2] hfs: validate B-tree record cursor before arithmetic

From: Shawn Shao

Date: Tue Dec 01 2026 - 09:18:52 EST


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
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@xxxxxxx>
---
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;
+ for (;;) {
+ 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;
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;
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);