[PATCH v3] hfs: validate B-tree record cursor before arithmetic
From: Shawn Shao
Date: Wed Dec 02 2026 - 02:57:01 EST
hfs_brec_goto() uses the record cursor to calculate the distance to a
sibling before validating the cursor. It also follows sibling links
without a traversal bound. Corrupt record counts or an empty leaf with a
cyclic link can therefore cause invalid cursor arithmetic or a walk that
does not terminate.
Validate the cursor before arithmetic and route forward and reverse moves
through one helper. Keep the remaining count signed so INT_MIN is never
negated, and bound node transitions by the smaller of the B-tree's node
count and the complete nodes in its file. Validate the final cursor before
using it to calculate record offsets. Preserve the -1 cursor for forward
movement from before the first record.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Shawn Shao <shawnq1@xxxxxxx>
---
Changes in v3:
- Share forward and reverse cursor movement in one signed helper.
- Check the cursor before distance calculations and before record offsets.
- Bound sibling traversal by both the declared node count and the number
of complete nodes in the B-tree file.
Testing:
- x86-64 bzImage build passed with CONFIG_HFS_FS=y.
- Fixed cycle fixture: the first getdents64() returned 48 bytes and the
second returned -EIO promptly.
- Terminal empty-leaf control: the first getdents64() returned 48 bytes
and the second returned -ENOENT promptly.
- git apply --check, git diff --check, and checkpatch --strict passed.
Reproducer, configuration, and both console logs:
https://gist.github.com/shaos26/06dc92e4840aef9766cd82b8c644fe16
fs/hfs/bfind.c | 120 ++++++++++++++++++++++++++++++++++++++-------------------
1 file changed, 81 insertions(+), 39 deletions(-)
diff --git a/fs/hfs/bfind.c b/fs/hfs/bfind.c
index d56e47bdc517d..5a9af6ea5aa3f 100644
--- a/fs/hfs/bfind.c
+++ b/fs/hfs/bfind.c
@@ -180,55 +180,97 @@ int hfs_brec_read(struct hfs_find_data *fd, void *rec, u32 rec_len)
return 0;
}
-int hfs_brec_goto(struct hfs_find_data *fd, int cnt)
+/* Move a record cursor while checking every node transition. */
+static int hfs_brec_move(struct hfs_find_data *fd, int cnt)
{
- struct hfs_btree *tree;
- struct hfs_bnode *bnode;
- int idx, res = 0;
- u16 off, len, keylen;
+ struct hfs_bnode *bnode = fd->bnode;
+ struct hfs_btree *tree = bnode->tree;
+ s64 remaining = cnt;
+ u64 node_limit;
+ u64 nodes;
+ bool forward = cnt >= 0;
- bnode = fd->bnode;
- tree = bnode->tree;
+ node_limit = (u64)tree->inode->i_size >> tree->node_size_shift;
+ node_limit = min_t(u64, tree->node_count, node_limit);
+ if (!node_limit || bnode->this >= node_limit)
+ return -EIO;
- if (cnt < 0) {
- cnt = -cnt;
- while (cnt > fd->record) {
- cnt -= fd->record + 1;
- fd->record = bnode->num_recs - 1;
- idx = bnode->prev;
- if (!idx) {
- res = -ENOENT;
- goto out;
- }
- hfs_bnode_put(bnode);
- bnode = hfs_bnode_find(tree, idx);
- if (IS_ERR(bnode)) {
- res = PTR_ERR(bnode);
- bnode = NULL;
- goto out;
- }
+ for (nodes = 0; nodes < node_limit; nodes++) {
+ u32 idx;
+
+ if (!bnode->num_recs) {
+ if (fd->record != -1 && fd->record != 0)
+ return -EIO;
+ return forward ? (bnode->next ? -EIO : -ENOENT) :
+ (bnode->prev ? -EIO : -ENOENT);
}
- fd->record -= cnt;
- } else {
- while (cnt >= bnode->num_recs - fd->record) {
- cnt -= bnode->num_recs - fd->record;
+
+ if (fd->record < -1 || fd->record >= bnode->num_recs ||
+ (fd->record == -1 && !forward))
+ return -EIO;
+
+ if (!remaining)
+ goto valid;
+
+ if (forward) {
+ s64 available = bnode->num_recs - fd->record;
+
+ if (remaining < available) {
+ fd->record += (int)remaining;
+ goto valid;
+ }
+
+ remaining -= available;
fd->record = 0;
idx = bnode->next;
- if (!idx) {
- res = -ENOENT;
- goto out;
- }
- hfs_bnode_put(bnode);
- bnode = hfs_bnode_find(tree, idx);
- if (IS_ERR(bnode)) {
- res = PTR_ERR(bnode);
- bnode = NULL;
- goto out;
+ } else {
+ if (remaining >= -fd->record) {
+ fd->record += (int)remaining;
+ goto valid;
}
+
+ remaining += fd->record + 1;
+ idx = bnode->prev;
}
- fd->record += cnt;
+
+ if (!idx)
+ return -ENOENT;
+ if (idx >= node_limit || nodes + 1 >= node_limit)
+ return -EIO;
+
+ hfs_bnode_put(bnode);
+ bnode = hfs_bnode_find(tree, idx);
+ if (IS_ERR(bnode)) {
+ int err = PTR_ERR(bnode);
+
+ fd->bnode = NULL;
+ return err;
+ }
+
+ fd->bnode = bnode;
+ fd->record = forward ? 0 : bnode->num_recs - 1;
}
+ return -EIO;
+
+valid:
+ if (fd->record < 0 || fd->record >= bnode->num_recs)
+ return -EIO;
+
+ return 0;
+}
+
+int hfs_brec_goto(struct hfs_find_data *fd, int cnt)
+{
+ struct hfs_bnode *bnode = fd->bnode;
+ int res;
+ u16 off, len, keylen;
+
+ res = hfs_brec_move(fd, cnt);
+ if (res)
+ return res;
+
+ bnode = fd->bnode;
len = hfs_brec_lenoff(bnode, fd->record, &off);
keylen = hfs_brec_keylen(bnode, fd->record);
if (keylen == 0) {