[PATCH 1/3] hfs: fix b-tree node writes past the first page
From: Mar Dominguez
Date: Fri Dec 04 2026 - 00:38:40 EST
hfs_btree_open() accepts any power-of-two node size from the b-tree
header, so a catalog or extents tree can have 8, 16 or 32 KiB nodes,
which span several pages on 4 KiB page systems. hfs_bnode_find() and
hfs_bnode_read() handle such nodes: commit 54a5ead6f5e2 ("hfs: fix high
memory mapping in hfs_bnode_read") made the read side walk
node->page[]. hfs_bnode_write(), hfs_bnode_clear(), hfs_bnode_copy()
and hfs_bnode_move() still use only node->page[0], with offsets and
lengths that are bounded by the node size, not by the page size. Every
change to such a tree then writes up to 28 KiB past the end of that
page, into whatever memory follows it. For example, hfs_brec_insert()
updates the record offset table at the end of the node, and
hfs_bnode_put() clears the whole node when a node is deleted. The
node's other pages are never updated, so the tree on disk is left
inconsistent too.
With CONFIG_DEBUG_VM, mkdir on a volume whose catalog tree has 8 KiB
nodes trips the check in memcpy_to_page():
kernel BUG at include/linux/highmem.h:552!
RIP: 0010:hfs_bnode_write+0x4c5/0x560
Call Trace:
hfs_bnode_write_u16+0x6d/0xa0
hfs_brec_insert+0x3c4/0xb60
hfs_cat_create+0x3bd/0xb00
hfs_mkdir+0x59/0xc0
vfs_mkdir+0x2b4/0x710
Without CONFIG_DEBUG_VM the pages after node->page[0] are silently
overwritten or zeroed.
Walk the node's pages in all four helpers, as hfs_bnode_read() and the
hfsplus helpers do. hfs_bnode_move() copies backwards when the
destination is above the source, because the ranges may overlap across
pages.
Found and written with an LLM coding assistant.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Mar Dominguez <mar@xxxxxxxxxxxx>
---
fs/hfs/bnode.c | 124 ++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 106 insertions(+), 18 deletions(-)
diff --git a/fs/hfs/bnode.c b/fs/hfs/bnode.c
index ec1b0189a4..d12be972ce 100644
--- a/fs/hfs/bnode.c
+++ b/fs/hfs/bnode.c
@@ -95,6 +95,9 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len)
{
struct page *page;
+ u32 pagenum;
+ u32 bytes_written;
+ u32 bytes_to_write;
if (!is_bnode_offset_valid(node, off))
return;
@@ -111,10 +114,22 @@ void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len)
len = check_and_correct_requested_length(node, off, len);
off += node->page_offset;
- page = node->page[0];
+ pagenum = off >> PAGE_SHIFT;
+ off &= ~PAGE_MASK; /* compute page offset for the first page */
+
+ for (bytes_written = 0; bytes_written < len;
+ bytes_written += bytes_to_write) {
+ if (pagenum >= node->tree->pages_per_bnode)
+ break;
+ page = node->page[pagenum];
+ bytes_to_write = min_t(u32, len - bytes_written, PAGE_SIZE - off);
+
+ memcpy_to_page(page, off, buf + bytes_written, bytes_to_write);
+ set_page_dirty(page);
- memcpy_to_page(page, off, buf, len);
- set_page_dirty(page);
+ pagenum++;
+ off = 0; /* page offset only applies to the first page */
+ }
}
void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data)
@@ -133,6 +148,9 @@ void hfs_bnode_write_u8(struct hfs_bnode *node, u32 off, u8 data)
void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len)
{
struct page *page;
+ u32 pagenum;
+ u32 bytes_cleared;
+ u32 bytes_to_clear;
if (!is_bnode_offset_valid(node, off))
return;
@@ -149,16 +167,65 @@ void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len)
len = check_and_correct_requested_length(node, off, len);
off += node->page_offset;
- page = node->page[0];
+ pagenum = off >> PAGE_SHIFT;
+ off &= ~PAGE_MASK; /* compute page offset for the first page */
+
+ for (bytes_cleared = 0; bytes_cleared < len;
+ bytes_cleared += bytes_to_clear) {
+ if (pagenum >= node->tree->pages_per_bnode)
+ break;
+ page = node->page[pagenum];
+ bytes_to_clear = min_t(u32, len - bytes_cleared, PAGE_SIZE - off);
+
+ memzero_page(page, off, bytes_to_clear);
+ set_page_dirty(page);
+
+ pagenum++;
+ off = 0; /* page offset only applies to the first page */
+ }
+}
- memzero_page(page, off, len);
- set_page_dirty(page);
+/* Number of bytes from @off to the end of the page that contains @off */
+static inline u32 hfs_bnode_page_tail(u32 off)
+{
+ return PAGE_SIZE - (off & ~PAGE_MASK);
+}
+
+/* Number of bytes before @end in the page that contains byte @end - 1 */
+static inline u32 hfs_bnode_page_head(u32 end)
+{
+ return ((end - 1) & ~PAGE_MASK) + 1;
+}
+
+/*
+ * Copy @len bytes from offset @src of @src_node to offset @dst of @dst_node.
+ * The offsets include the nodes' page_offset, and neither range may cross
+ * a page boundary.
+ */
+static void hfs_bnode_copy_chunk(struct hfs_bnode *dst_node, u32 dst,
+ struct hfs_bnode *src_node, u32 src, u32 len)
+{
+ struct page *src_page = src_node->page[src >> PAGE_SHIFT];
+ struct page *dst_page = dst_node->page[dst >> PAGE_SHIFT];
+ void *ptr;
+
+ src &= ~PAGE_MASK;
+ dst &= ~PAGE_MASK;
+
+ if (src_page == dst_page) {
+ ptr = kmap_local_page(dst_page);
+ memmove(ptr + dst, ptr + src, len);
+ kunmap_local(ptr);
+ } else {
+ memcpy_page(dst_page, dst, src_page, src, len);
+ }
+ set_page_dirty(dst_page);
}
void hfs_bnode_copy(struct hfs_bnode *dst_node, u32 dst,
struct hfs_bnode *src_node, u32 src, u32 len)
{
- struct page *src_page, *dst_page;
+ u32 chunk;
hfs_dbg("dst %u, src %u, len %u\n", dst, src, len);
if (!len)
@@ -169,17 +236,20 @@ void hfs_bnode_copy(struct hfs_bnode *dst_node, u32 dst,
src += src_node->page_offset;
dst += dst_node->page_offset;
- src_page = src_node->page[0];
- dst_page = dst_node->page[0];
- memcpy_page(dst_page, dst, src_page, src, len);
- set_page_dirty(dst_page);
+ while (len) {
+ chunk = min3(len, hfs_bnode_page_tail(src),
+ hfs_bnode_page_tail(dst));
+ hfs_bnode_copy_chunk(dst_node, dst, src_node, src, chunk);
+ src += chunk;
+ dst += chunk;
+ len -= chunk;
+ }
}
void hfs_bnode_move(struct hfs_bnode *node, u32 dst, u32 src, u32 len)
{
- struct page *page;
- void *ptr;
+ u32 chunk;
hfs_dbg("dst %u, src %u, len %u\n", dst, src, len);
if (!len)
@@ -190,11 +260,29 @@ void hfs_bnode_move(struct hfs_bnode *node, u32 dst, u32 src, u32 len)
src += node->page_offset;
dst += node->page_offset;
- page = node->page[0];
- ptr = kmap_local_page(page);
- memmove(ptr + dst, ptr + src, len);
- kunmap_local(ptr);
- set_page_dirty(page);
+
+ if (dst > src) {
+ /* The ranges may overlap: move the end first */
+ src += len;
+ dst += len;
+ while (len) {
+ chunk = min3(len, hfs_bnode_page_head(src),
+ hfs_bnode_page_head(dst));
+ src -= chunk;
+ dst -= chunk;
+ len -= chunk;
+ hfs_bnode_copy_chunk(node, dst, node, src, chunk);
+ }
+ } else {
+ while (len) {
+ chunk = min3(len, hfs_bnode_page_tail(src),
+ hfs_bnode_page_tail(dst));
+ hfs_bnode_copy_chunk(node, dst, node, src, chunk);
+ src += chunk;
+ dst += chunk;
+ len -= chunk;
+ }
+ }
}
void hfs_bnode_dump(struct hfs_bnode *node)
--
2.53.0