[PATCH 2/3] hfs: check the b-tree node size range in hfs_btree_open()

From: Mar Dominguez

Date: Fri Dec 04 2026 - 00:41:14 EST


hfs_btree_open() only checks that the node size in the b-tree header is
a power of two, so it also accepts nodes of 1 to 256 bytes, which can't
hold a node descriptor and the header node's records. HFS b-tree nodes
are 512 bytes, and hfsplus already rejects node sizes outside 512 to
32768 bytes. Apply the same range to HFS, with the constants moved to
hfs_common.h and shared by both, and log the size that is rejected.

Found and written with an LLM coding assistant.

Signed-off-by: Mar Dominguez <mar@xxxxxxxxxxxx>
---
fs/hfs/btree.c | 5 ++++-
include/linux/hfs_common.h | 6 ++++--
2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/fs/hfs/btree.c b/fs/hfs/btree.c
index f070384031..62c9094ab4 100644
--- a/fs/hfs/btree.c
+++ b/fs/hfs/btree.c
@@ -229,8 +229,11 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id, btree_keycmp ke
tree->depth = be16_to_cpu(head->depth);

size = tree->node_size;
- if (!is_power_of_2(size))
+ if (size < HFS_NODE_MINSZ || size > HFS_NODE_MXSZ ||
+ !is_power_of_2(size)) {
+ pr_err("invalid node size %u\n", size);
goto fail_folio;
+ }
if (!tree->node_count)
goto fail_folio;
switch (id) {
diff --git a/include/linux/hfs_common.h b/include/linux/hfs_common.h
index eb119a1c56..3502552d75 100644
--- a/include/linux/hfs_common.h
+++ b/include/linux/hfs_common.h
@@ -532,13 +532,15 @@ struct hfs_btree_header_rec {

/* HFS BTree misc info */
#define HFS_TREE_HEAD 0
+#define HFS_NODE_MINSZ 512
+#define HFS_NODE_MXSZ 32768
#define HFS_BTREE_HDR_MAP_REC_INDEX 2 /* Map (bitmap) record in Header node */
#define HFS_BTREE_MAP_NODE_REC_INDEX 0 /* Map record in Map Node */

/* HFS+ BTree misc info */
#define HFSPLUS_TREE_HEAD HFS_TREE_HEAD
-#define HFSPLUS_NODE_MXSZ 32768
-#define HFSPLUS_NODE_MINSZ 512
+#define HFSPLUS_NODE_MXSZ HFS_NODE_MXSZ
+#define HFSPLUS_NODE_MINSZ HFS_NODE_MINSZ
#define HFSPLUS_ATTR_TREE_NODE_SIZE 8192
#define HFSPLUS_BTREE_HDR_NODE_RECS_COUNT 3
/* Map (bitmap) record in Header node */
--
2.53.0