Since commit 8ad2c6a36ac4 ("hfsplus: validate b-tree node 0 bitmap at mount time"), hfs_btree_open() mounts the volume read-only when the map record of a b-tree does not mark the header node (node 0) as used. Keeping the volume read-only stops the b-tree allocator, which looks in the map for free nodes, from using a map that is known to be wrong. That decision is only kept in SB_RDONLY. On "mount -o remount,rw", reconfigure_super() calls hfsplus_reconfigure() and then copies the flags requested in fc->sb_flags into sb->s_flags, so SB_RDONLY is cleared unless hfsplus_reconfigure() sets it in fc->sb_flags again. hfsplus_reconfigure() only does that for the conditions it checks from the volume header: a volume that was not cleanly unmounted, and a locked or journaled volume without the force option. Nothing records that a b-tree map was found corrupted at mount time, so hfsplus_reconfigure() returns 0 and the volume becomes writable again. The allocator then finds node 0 free in the map, hands it out as a new node and hfs_bnode_create() hits the header node that is still hashed. With the syzbot reproducer, the mount reports "Catalog File (cnid 0x4) map record invalid or bitmap corruption detected, forcing read-only", the reproducer remounts read-write and calls mknod(): hfsplus: new node 0 already hashed? WARNING: fs/hfsplus/bnode.c:642 at hfsplus_bnode_create+0xa8/0x2d0 hfsplus_bmap_alloc+0x45d/0x4b0 hfs_bnode_split+0x4e/0x4c0 hfsplus_brec_insert+0x104/0x500 hfsplus_create_cat+0x212/0x6b0 hfsplus_mknod+0xea/0x1f0 In the test below, fsck.hfsplus reported "Invalid map node" for the damaged image before it was used, and "Invalid B-tree header" after it had been written to read-write. Remember the corruption in a new HFSPLUS_SB_BAD_BTREE_MAP flag when hfs_btree_open() forces the volume read-only, and check it in hfsplus_reconfigure() like the other conditions that keep a volume read-only. The check comes before the force option, as for a volume that was not cleanly unmounted: force only skips the checks for journaled and locked volumes, and this is damaged metadata like an unclean unmount. As for those other conditions, the remount succeeds and the volume stays read-only with a warning. Reading files is not affected, since the map record is only used to allocate and free b-tree nodes, so the data can still be copied off before running fsck.hfsplus. Tested in QEMU (x86_64): - On next-20261130 with the syzbot C reproducer: the warning above showed up in 3 of 3 runs before the change. After it, the remount leaves the volume read-only, mknod() fails with EROFS and there was no warning in 5 of 5 runs, two of them with slub_debug=FZPU. - On the hfs tree's for-next branch the syzbot image is now rejected at mount for an unrelated reason, the size of its attributes file. So I also used a freshly created volume with 301 files in which only the node 0 bit of one b-tree map record was cleared. With the catalog, the extents or the attributes tree damaged that way, "remount,rw" and a remount after "mount -o ro" made the volume writable before the change, and the catalog case hit the warning above. After the change the volume stays read-only for those and for "remount,rw,force", the image is byte for byte unchanged and all 301 files read back with the right checksums. - An undamaged volume can still be remounted read-write, written to and remounted read-only, and fsck.hfsplus reports it as OK afterwards. After fsck.hfsplus repairs one of the damaged images, it mounts and remounts read-write normally again. checkpatch.pl --strict and sparse report nothing for the changed files. Claude (Anthropic) was used for the root cause analysis, the test images and scripts, and drafting this changelog. Fixes: 8ad2c6a36ac4 ("hfsplus: validate b-tree node 0 bitmap at mount time") Reported-by: syzbot+2bf21610eea63cb2ce93@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=2bf21610eea63cb2ce93 Signed-off-by: Song jeonghak --- This is based on the for-next branch of the hfs tree (a2c27efbbd43). On linux-next, commit 6998f4bc12f2 ("hfsplus: don't set SB_RDONLY when creating the attributes tree") from Christian Brauner's series moves the node 0 check into hfsplus_btree_check_map(), and hfsplus_reconfigure() is older there. The same three changes apply in those places; that version is what I tested with the syzbot reproducer on next-20261130. On for-next, commit d4821af67733 ("hfsplus: reject special files larger than their allocation at mount") rejects the syzbot image because its attributes file size is also corrupted, so the reproducer no longer gets to the remount there. A volume whose only damage is the cleared node 0 bit still mounts read-only and could be remounted read-write before this change. The earlier patches for this report changed the allocator ("[PATCH 2/2] hfs/hfsplus: stop btree allocators from reusing node 0") and removed the WARN_ON() ("[PATCH] hfs, hfsplus: remove WARN_ON when new bnode already hashed"). The allocator change was declined because the mount-time check already makes the volume read-only, and removing the WARN_ON() because it would hide the problem. This patch keeps that check as the place where the corruption is handled and closes the remount that undoes it. It only covers what the mount-time check detects, i.e. a cleared node 0 bit. Other damage to a b-tree map that the check does not see may still make the allocator hand out a node that is in use; I have not tested that. To reproduce without the syzbot image: create an HFS+ volume with mkfs.hfsplus, add some files, then clear the most significant bit of the first byte of the map record (record 2) in the header node of the catalog file. Mount it, remount it read-write and create a few hundred files. fs/hfsplus/btree.c | 1 + fs/hfsplus/hfsplus_fs.h | 1 + fs/hfsplus/super.c | 4 ++++ 3 files changed, 6 insertions(+) diff --git a/fs/hfsplus/btree.c b/fs/hfsplus/btree.c index 0a9b32d5c5bf..9c30bc77cd40 100644 --- a/fs/hfsplus/btree.c +++ b/fs/hfsplus/btree.c @@ -412,6 +412,7 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id) sb->s_id, hfs_btree_name(id), id); pr_warn("Run fsck.hfsplus to repair.\n"); sb->s_flags |= SB_RDONLY; + set_bit(HFSPLUS_SB_BAD_BTREE_MAP, &HFSPLUS_SB(sb)->flags); } hfs_bnode_put(node); diff --git a/fs/hfsplus/hfsplus_fs.h b/fs/hfsplus/hfsplus_fs.h index dd8f7179431a..049d79380518 100644 --- a/fs/hfsplus/hfsplus_fs.h +++ b/fs/hfsplus/hfsplus_fs.h @@ -187,6 +187,7 @@ struct hfsplus_sb_info { #define HFSPLUS_SB_NOBARRIER 5 #define HFSPLUS_SB_UID 6 #define HFSPLUS_SB_GID 7 +#define HFSPLUS_SB_BAD_BTREE_MAP 8 /* a b-tree map record is corrupted */ static inline struct hfsplus_sb_info *HFSPLUS_SB(struct super_block *sb) { diff --git a/fs/hfsplus/super.c b/fs/hfsplus/super.c index 71ca14cef3c4..64ace4eda5cb 100644 --- a/fs/hfsplus/super.c +++ b/fs/hfsplus/super.c @@ -566,6 +566,10 @@ static int hfsplus_reconfigure(struct fs_context *fc) pr_warn("filesystem was not cleanly unmounted, running fsck.hfsplus is recommended. leaving read-only.\n"); sb->s_flags |= SB_RDONLY; fc->sb_flags |= SB_RDONLY; + } else if (test_bit(HFSPLUS_SB_BAD_BTREE_MAP, &sbi->flags)) { + pr_warn("filesystem has a corrupted b-tree map, running fsck.hfsplus is recommended. leaving read-only.\n"); + sb->s_flags |= SB_RDONLY; + fc->sb_flags |= SB_RDONLY; } else if (test_bit(HFSPLUS_SB_FORCE, &sbi->flags)) { /* nothing */ } else if (vhdr->attributes & -- 2.43.0