hfsplus: keep a volume with a corrupted b-tree map read-only on remount
Subsystems (2)
PATCH 1/1 hfsplus: keep a volume with a corrupted b-tree map read-only on remount
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.
---
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(+)
fs/hfsplus/btree.c +1 -0
@@ -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);
fs/hfsplus/hfsplus_fs.h +1 -0
@@ -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) {
fs/hfsplus/super.c +4 -0
@@ -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 &
Signature · 1 line
2.43.0
Viacheslav Dubeyko
maintainerQuoted 145 lines · Song jeonghak
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()callshfsplus_reconfigure()and then copies the flags requested infc->sb_flagsintosb->s_flags, soSB_RDONLYis cleared unlesshfsplus_reconfigure()sets it infc->sb_flagsagain.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, sohfsplus_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 andhfs_bnode_create()hits the header node that is still hashed.
With the syzbot reproducer, the mount reports "Catalog File (cnid0x4)
map record invalid or bitmap corruption detected, forcing read-only", the reproducer remounts read-write and callsmknod():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/0x1f0In 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_MAPflag whenhfs_btree_open()forces the volume read-only, and check it inhfsplus_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 mounttime")
Reported-by [email protected]Signed-off-by Song jeonghak <[email protected]>---
This is based on the for-next branch of the hfs tree (a2c27efbbd43). On
linux-next, commit6998f4bc12f2("hfsplus: don't setSB_RDONLYwhen creating the attributes tree") from Christian Brauner's series moves the
node 0 check intohfsplus_btree_check_map(), andhfsplus_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 theWARN_ON()("[PATCH] hfs, hfsplus: removeWARN_ONwhen new bnode already
hashed"). The allocator change was declined because the mount-time check
already makes the volume read-only, and removing theWARN_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.
Quoted 7 lines
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.
Thanks a lot for the patch and for the very careful analysis and testing. You are right: hfs_btree_open() is the only place that forces the read-only state at mount time without hfsplus_reconfigure() checking the same condition again, so remount,rw silently undoes the protection added by 8ad2c6a36ac4. The fix looks correct to me, and I agree that the check should go before the force option.
I have several comments, though.
Quoted 29 lines
fs/hfsplus/btree.c | 1 +
fs/hfsplus/hfsplus_fs.h | 1 +
fs/hfsplus/super.c | 4 ++++3 files changed, 6 insertions(+)fs/hfsplus/btree.c +1
-0@@ -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);fs/hfsplus/hfsplus_fs.h +1
-0@@ -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
Quoted 1 line
record is corrupted */
Basically, this flag means "metadata corruption has been detected at mount time, keep the volume read-only". I expect that we will need the same for other mount-time checks in the future. What do you think about a more generic name (for example, HFSPLUS_SB_CORRUPTED) and a more generic message in hfsplus_reconfigure()?
Quoted 20 lines
static inline structhfsplus_sb_info*HFSPLUS_SB(structsuper_block*sb)
{fs/hfsplus/super.c +2
-0@@ -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");
fs/hfsplus/super.c +2
-0+ sb->s_flags |=SB_RDONLY ; + fc->sb_flags |=SB_RDONLY ;
Quoted 3 lines
} else if (
test_bit(HFSPLUS_SB_FORCE, &sbi->flags)) { /* nothing */
} else if (vhdr->attributes&
HFS has exactly the same issue. fs/hfs/btree.c has the same node 0 check, and hfs_mdb_get() forces read-only mode for invalid CNID counts and for a corrupted primary or alternate MDB. But hfs_reconfigure() re-checks only the "not cleanly unmounted" and "locked" states, so remount,rw makes such volumes writable again. Could you please add a patch for HFS too (with a HFS_FLG_* bit in HFS_SB(sb)->flags), so that we have a two-patch series?
Also, the commit message is pretty long. I think the detailed test description would be better placed below the "---" line, keeping the root cause, the fix, and the syzbot trace in the commit message itself.
Regarding commit 6998f4bc12f2 ("hfsplus: don't set SB_RDONLY when creating the attributes tree") from Christian's series: I am going to take your patch through the HFS tree, so please keep v2 based on the for-next branch.
Thanks,
Slava.