[PATCH 0/3] hfs: fix writes to b-tree nodes larger than a page

From: Mar Dominguez

Date: Fri Dec 04 2026 - 00:36:12 EST


hfs_btree_open() accepts b-tree node sizes up to 32 KiB, and the read
side handles nodes that span several pages, but hfs_bnode_write(),
hfs_bnode_clear(), hfs_bnode_copy() and hfs_bnode_move() only use the
node's first page. Any change to a catalog or extents tree whose nodes
are larger than PAGE_SIZE then writes past that page.

Patch 1 makes the four helpers walk the node's pages. Patch 2 rejects
node sizes below 512 bytes, as hfsplus does. Patch 3 adds KUnit tests
for the four helpers.

Patch 1 fixes the write helpers instead of refusing nodes larger than
512 bytes: hfs_btree_open() has accepted them since 2.6.12, and the read
and node creation paths already support them. With patch 1 the write
helpers do too, as the hfsplus ones do.

Tested with x86_64 KASAN builds under QEMU: hfs/for-next (a2c27efbbd43)
with CONFIG_DEBUG_VM, and mainline (007722b8211f) without it. The test
volumes were made with hformat, with the catalog tree rewritten to use
8 KiB or 32 KiB nodes. Without the series, mkdir and rmdir on them hit
the VM_BUG_ON in memcpy_to_page() with CONFIG_DEBUG_VM. Without
CONFIG_DEBUG_VM, the volume can't be mounted again after mkdir (8 KiB
nodes), and rmdir (32 KiB nodes) crashes init through corrupted
memory. With the series, both work and the volume mounts again with
the expected directories. A volume with the usual 512-byte nodes
behaves the same with and without the series. The hfs KUnit suites
pass on UML with KASAN; without patch 1 the four new cases fail.

I have not tested on 32-bit HIGHMEM, on architectures with pages larger
than 4 KiB, or with volumes written by Mac OS, and I have not run
xfstests. Patches 1 and 2 also apply to mainline; patch 3 needs the
KUnit setup in hfs/for-next. I can send the test images privately.

Found and written with an LLM coding assistant.

Mar Dominguez (3):
hfs: fix b-tree node writes past the first page
hfs: check the b-tree node size range in hfs_btree_open()
hfs: add KUnit tests for b-tree node operations

fs/hfs/Makefile | 2 +-
fs/hfs/bnode.c | 132 ++++++++++++++---
fs/hfs/bnode_test.c | 293 +++++++++++++++++++++++++++++++++++++
fs/hfs/btree.c | 5 +-
include/linux/hfs_common.h | 6 +-
5 files changed, 416 insertions(+), 22 deletions(-)
create mode 100644 fs/hfs/bnode_test.c


base-commit: a2c27efbbd43d2e3756860ebd1bf891d7481f2c5
--
2.53.0