mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yue Sun <samsun1006219@gmail.com>
To: slava@dubeyko.com, glaubitz@physik.fu-berlin.de, frank.li@vivo.com
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Yue Sun <samsun1006219@gmail.com>
Subject: [PATCH v2] hfs: prevent MDB and bitmap buffer_head aliasing
Date: Fri, 29 May 2026 18:34:17 +0800	[thread overview]
Message-ID: <20260529103428.99099-1-samsun1006219@gmail.com> (raw)
In-Reply-To: <1d26872db999da42b2d8452ace8a98b79e569cc8.camel@dubeyko.com>

hfs_mdb_commit() writes the volume bitmap while HFS_SB(sb)->mdb_bh is
locked. A crafted image can set drVBMSt so that the bitmap block resolves
to the same buffer_head as the MDB. When writeback later calls
lock_buffer() for that bitmap block, the task tries to lock mdb_bh again
and self-deadlocks in __lock_buffer().

Reject images whose volume bitmap starts at or before the MDB during
mount. Also guard the bitmap writeback path itself: if the bitmap block
would resolve to mdb_bh, force the filesystem read-only and stop bitmap
writeback before taking the buffer lock. This keeps the deadlock fix in
the MDB commit path and reuses the existing bitmap size/writeback logic.

Reported-by: Yue Sun <samsun1006219@gmail.com>
Closes: https://lore.kernel.org/all/CAEkJfYMB47v1yOWHB8q2dc8kf=uj-rLO=+yMyudwPguJ8Kd3jA@mail.gmail.com/
Signed-off-by: Yue Sun <samsun1006219@gmail.com>
---
Changes in v2:
- Add a commit-time guard before locking bitmap buffer_heads.
- Replace the mount-time byte-range check with a simple drVBMSt check.
- Reuse the existing bitmap writeback size calculation.

 fs/hfs/mdb.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/fs/hfs/mdb.c b/fs/hfs/mdb.c
index a97cea35ca2e..53cd137892b5 100644
--- a/fs/hfs/mdb.c
+++ b/fs/hfs/mdb.c
@@ -185,6 +185,11 @@ int hfs_mdb_get(struct super_block *sb)
 		sb->s_flags |= SB_RDONLY;
 	}
 
+	if (be16_to_cpu(mdb->drVBMSt) <= HFS_MDB_BLK) {
+		pr_err("volume bitmap overlaps MDB\n");
+		return -EIO;
+	}
+
 	/* TRY to get the alternate (backup) MDB. */
 	sect = part_start + part_size - 2;
 	bh = sb_bread512(sb, sect, mdb2);
@@ -341,6 +346,11 @@ void hfs_mdb_commit(struct super_block *sb)
 		size = (HFS_SB(sb)->fs_ablocks + 7) / 8;
 		ptr = (u8 *)HFS_SB(sb)->bitmap;
 		while (size) {
+			if (unlikely(block == HFS_SB(sb)->mdb_bh->b_blocknr)) {
+				pr_err("volume bitmap overlaps MDB, forcing read-only\n");
+				sb->s_flags |= SB_RDONLY;
+				break;
+			}
 			bh = sb_bread(sb, block);
 			if (!bh) {
 				pr_err("unable to read volume bitmap\n");
-- 
2.34.1

  reply	other threads:[~2026-05-29 10:34 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-27 15:32 [PATCH] hfs: reject volume bitmaps overlapping the MDB Yue Sun
2026-05-27 19:49 ` Viacheslav Dubeyko
2026-05-29 10:34   ` Yue Sun [this message]
2026-05-29 21:29     ` [PATCH v2] hfs: prevent MDB and bitmap buffer_head aliasing Viacheslav Dubeyko
2026-05-29 23:23       ` [EXTERNAL] " Viacheslav Dubeyko
2026-06-01 14:01         ` Sam Sun
2026-06-01 18:46           ` slava
2026-06-03 15:23             ` Yue Sun
2026-06-03 16:53               ` Viacheslav Dubeyko
2026-06-04 18:02                 ` Yue Sun
2026-06-04 21:25                   ` Viacheslav Dubeyko
2026-06-04 23:52                     ` Viacheslav Dubeyko
2026-06-05  4:00                       ` Sam Sun
2026-06-05 18:56                         ` Viacheslav Dubeyko
2026-06-05  3:57                     ` Sam Sun
2026-06-13  5:47     ` [PATCH v3] hfs: avoid deadlock in hfs_mdb_commit() Yue Sun
2026-06-16  6:18       ` Viacheslav Dubeyko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260529103428.99099-1-samsun1006219@gmail.com \
    --to=samsun1006219@gmail.com \
    --cc=frank.li@vivo.com \
    --cc=glaubitz@physik.fu-berlin.de \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=slava@dubeyko.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®