From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796359240; b=vgGKkQ8RR/Konka9WnxM9kJdn+Rmh6edKHvDsQ52WOAELDwFDz4gJmb0a4QzrthWJxZL oJPDyO5+Vo8IsHcNhEjf6bBjKkHtaECFasF31ojSraFzNAgdJtIYQ05lINbv/SsHllzeo dA9Afy4ZEtRVFsULMciO8S9Tie+m5dJqkapTqBSwLmtdy3Is9HzxbKddoVFZ992Id2NL1 iua79Fm4HwoHK6XmwFXWfU22VWd9gsZgkg2mSrXL4dLOvTgvaFibtPLZbnIFTqIjSMTSh OoSlM7ZIZRCwoYsIhXCHaUm6W8tJGwxZSsYrgp3vQQv9QvTSCCwkQRz7Hv614SA/nUg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796359240; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=n8ZvMsg/iaeKWvOPJ1DxgV4wvU3n/E3uZjq5aaEJy3c=; b=XYcOyK8zoQVDOYvf7PryTze7Dcb31LAQ+IM/G73Z6On57pJvylR4/XkhExVnME3hnLl3 3NTJmkHscstIiOn7cb3FciomVx1MzAPnAOv6lRw3hpkC9iV5vnjCXFsgO8gI2Q4O+KS8d XaQstAKp0K7vw3NZzRwU47pFdUTF6x+h6nDvneA3gMmmZs4LDyKDdgqTJHshJejMG2EV3 m5LBMbHM/mMmbT9tHHNiH3hqQxGKc4tXAn5QO8NzOUQgxHcdsRZUGIY49lASos2BC32y2 iQiQ+QIwtWI8zo1JLOXGRWiFTTe9jzZx86467N12XYy6XPU8TvKsiq/A4fY25Dp11Zg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=dOzOCk9c; arc=none smtp.remote-ip=209.85.214.173 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E835A606A32C for ; Fri, 4 Dec 2026 04:40:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E835A606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=dOzOCk9c Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2f066274caeso3633625ad.2 for ; Thu, 03 Dec 2026 20:40:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796359239; x=1796964039; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=n8ZvMsg/iaeKWvOPJ1DxgV4wvU3n/E3uZjq5aaEJy3c=; b=dOzOCk9cPOA3fUXQPaTixnEzuLoPxLyf7OhTMnskkJnYRP+Y3FWfS7z1HSc9ACqk2U Sowk/jZb2I5atL9PMCeLLAhtyRN7FQd8DwX2XhwowuPzR1QepRnjP7lPm0/srObctV7+ TVKHZl9tVl1mrDElPPK0em9LZzoKGX4Q+2YbhxtSWyabAYDbjpevnCUhLYsTygfjoLs+ RgTVGH8ySBkojUBzKBrnsJ44OTb3KMTu17FK+yAoe7kpCiP/lczMhE3blKunJp0GCAUn a9Ovfnl6vGdvTJ8Xn42jPk0CWknLQGa0Vw7gU5usz3R+JzY/C8irQkqvs3OW1KiSsSG7 qRRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796359239; x=1796964039; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=n8ZvMsg/iaeKWvOPJ1DxgV4wvU3n/E3uZjq5aaEJy3c=; b=xDXebQfIOg6qVzgtS3MWyhuOedqGDVgLbaf1A6yRO352IdOIZQp3anaM9nMWMOOR3s AfSXb+Usk6J3XysYLQtmuq0nnLSbLZsz4MCS5y9Zxv9ChRNGt29XP9szn86cp8xOuxsn 7sGE1TxmefyxI3RI1MwB0KHC3rdGoP1688jIR0ZvK06RK0c+NNnBTaGoFzr14GlJPuIZ q7djfWmwkQVkklJ4FmOXweJu3swikT4FwboaMQgX4DglBINq4Z4ZuTd7xY20R3WbwSHB zVuSpjmEV8Cgy9ibIOL5H/w2urspI495vISjCinfKMS7TL6jymmK9AA4DAWA1pH36rLW UFWQ== X-Forwarded-Encrypted: i=1; AL+hErGLnNl96EPQXljBMTysr9928L/RTq2E4Pmq/KMjlFOg5siB2o5F9q2fqqjnF70jpElu+P1+chLx7tLxBWE=@vger.kernel.org X-Gm-Message-State: APuU4Z9AUzUiWRH7HYHE6wPFX3WBodRKWN1l3jB7y/IazA/Iih58D37m KV6zF+oG+Y8hd8zyIbYW8R1RTA4zuOvDqDflt0ZGLcPrcfDilEYeKR1n X-Gm-Gg: AZFdTZfniBB+MbDwja/j1PJekuIy0JOJvW9ziP54306vrkDIdY5+hDkKUW81/YRcBvw fM/vv6LoKN1xe1JtwbI7wzvpeTdx37HyrZBHbfm10iF7euLGxIfrEtieLDJWSJog2Q9wn6IlcJn ydZPVP2fa4PvSOjHiFnBgzVz7v6oXwbRBakskHV0rZdLEcZedsSJ0lMBVJ+N+w8siV22QQloh/e g8FfHNXJHWAJ9mIkTACk6dQ8O3wsnGXhK4I0rQqTVzUaE5ZhBH1tUSpVZovuoqiMNFlnPcmtp8r BafGp3JDgd/YGZK1LQVuelC5SzVQBQHHRPMTN6xIo/RjF60kpFgMdbFKJiw/OBSItbTTgvNiavj f5+gUrWfNGgzTxmclCz2yAA+aFVm11HRW2hA1Gwq1Y1ttIXODynCmW43lliotUOit6TfxAKDNJU HA0iQnykYUbP2wBGW4+urG2A9RbBYoQ+q2PvoZsEdMPSxVPrm9JHJX+jphOQSh82E9K3S12hPVH kgb4BOrXyzoEsqgPeTRHwAZjj8kgolCMXd3XUDhnOdbiLSjrxWhLUMP5+kIuF4JAhsnxSw984MY /7px6745EfXEkQ7j8QOHmeBrirtG5SlArQVMJqVIjBVN X-Received: by 2002:a17:903:46cd:b0:2f0:22b2:bfec with SMTP id d9443c01a7336-2f1ba17352cmr7518905ad.29.1796359238978; Thu, 03 Dec 2026 20:40:38 -0800 (PST) Received: from localhost.localdomain ([61.255.248.240]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f1b7967c6bsm4269715ad.70.2026.12.03.20.40.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 20:40:38 -0800 (PST) From: Song jeonghak To: Viacheslav Dubeyko Cc: John Paul Adrian Glaubitz , Yangtao Li , Shardul Bankar , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+2bf21610eea63cb2ce93@syzkaller.appspotmail.com, Song jeonghak Subject: [PATCH] hfsplus: keep a volume with a corrupted b-tree map read-only on remount Date: Fri, 4 Dec 2026 13:38:15 +0900 Message-ID: <20261204043815.368179-1-thdwjdgkr123@gmail.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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