[PATCH v2] hfs: reject b-tree node sizes other than 512 bytes

From: Mar Dominguez

Date: Fri Dec 04 2026 - 17:21:20 EST


HFS b-tree nodes are always 512 bytes. Inside Macintosh: Files says
the nodes of a B*-tree file are "512 bytes long", and TN1150 says "In
an HFS B-tree, nodes always have a fixed size of 512 bytes". But
hfs_btree_open() only checks that the node size in the header node is
a power of two, so it accepts any node size from 1 byte to 32 KiB.

hfs_bnode_write(), hfs_bnode_clear(), hfs_bnode_copy() and
hfs_bnode_move() only use a node's first page. On a crafted image
whose catalog or extents tree has nodes larger than PAGE_SIZE, a change
to that tree can write up to 28 KiB past that page (with 4 KiB pages).

Reject any node size other than 512 bytes when the catalog or extents
tree is opened, and log it. A 512-byte node never crosses a page
boundary, so the write helpers stay within node->page[0].

Found and written with an LLM coding assistant.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@xxxxxxxxxxxxxxx
Suggested-by: Viacheslav Dubeyko <slava@xxxxxxxxxxx>
Signed-off-by: Mar Dominguez <mar@xxxxxxxxxxxx>
---
Changes in v2:
- Accept only 512-byte nodes, as Inside Macintosh and TN1150 define
them for HFS (thanks, Slava), instead of making the write helpers
walk the pages of larger nodes. This replaces v1 patches 1 and 2.
- Drop the KUnit patch: its cases were for nodes larger than a page.
- Link to v1: https://lore.kernel.org/linux-fsdevel/MNEd8Lar2cPfPtaOkVL2PispJ3ysmgFipjnu@xxxxxxxxxxxx/

Tested with an x86_64 KASAN build with CONFIG_DEBUG_VM under QEMU on
hfs/for-next: volumes whose catalog tree has 256-byte, 8 KiB or 32 KiB
nodes are now refused at mount ("hfs: invalid node size ..."), and a
volume made with hformat (512-byte nodes) mounts, and mkdir and rmdir
work on it as before. The patch also applies to mainline.

fs/hfs/btree.c | 5 +++--
include/linux/hfs_common.h | 1 +
2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/fs/hfs/btree.c b/fs/hfs/btree.c
index f07038403..21ab54585 100644
--- a/fs/hfs/btree.c
+++ b/fs/hfs/btree.c
@@ -11,7 +11,6 @@

#include <linux/pagemap.h>
#include <linux/slab.h>
-#include <linux/log2.h>

#include "btree.h"

@@ -229,8 +228,10 @@ 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_BTREE_NODE_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 eb119a1c5..7260bda49 100644
--- a/include/linux/hfs_common.h
+++ b/include/linux/hfs_common.h
@@ -532,6 +532,7 @@ struct hfs_btree_header_rec {

/* HFS BTree misc info */
#define HFS_TREE_HEAD 0
+#define HFS_BTREE_NODE_SIZE 512 /* The only HFS node size */
#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 */


base-commit: a2c27efbbd43d2e3756860ebd1bf891d7481f2c5
--
2.53.0