Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Jan Kara <jack@suse.cz>
To: <linux-fsdevel@vger.kernel.org>
Cc: Jan Kara <jack@suse.cz>,
	stable@vger.kernel.org, Roman Yepishev <rye@keypressure.com>
Subject: [PATCH] udf: Fix validation of unalloc space bitmap
Date: Tue, 24 Nov 2026 16:15:55 +0100	[thread overview]
Message-ID: <20261124151554.42698-2-jack@suse.cz> (raw)

The validation whether bitmap blocks are marked as used in the
unallocated space bitmap was assuming that the bitmap is located at the
beginning of the partition. That is not true e.g. for media generated by
Win 11. Fix the validation to properly take location of the bitmap
within partition into account.

Fixes: 1e0d4adf17e7 ("udf: Check consistency of Space Bitmap Descriptor")
CC: stable@vger.kernel.org
Reported-by: Roman Yepishev <rye@keypressure.com>
Signed-off-by: Jan Kara <jack@suse.cz>
---
 fs/udf/balloc.c | 44 ++++++++++++++++++++++++++------------------
 1 file changed, 26 insertions(+), 18 deletions(-)

I plan to merge this fix through my tree.

diff --git a/fs/udf/balloc.c b/fs/udf/balloc.c
index b6551cc7c6cf..94baaa8fc74d 100644
--- a/fs/udf/balloc.c
+++ b/fs/udf/balloc.c
@@ -33,8 +33,9 @@ static int read_block_bitmap(struct super_block *sb,
 			     unsigned long bitmap_nr)
 {
 	struct buffer_head *bh = NULL;
-	int i;
-	int max_bits, off, count;
+	char *bmp;
+	unsigned int i, start_blk, end_blk, bmp_start;
+	int max_bits;
 	struct kernel_lb_addr loc;
 
 	loc.logicalBlockNum = bitmap->s_extPosition;
@@ -45,27 +46,34 @@ static int read_block_bitmap(struct super_block *sb,
 	if (!bh)
 		return -EIO;
 
-	/* Check consistency of Space Bitmap buffer. */
+	/* Check bitmap blocks are marked as used in the bitmap */
 	max_bits = sb->s_blocksize * 8;
+	bmp = bh->b_data;
+	/* Skip header in the first bitmap block */
 	if (!bitmap_nr) {
-		off = sizeof(struct spaceBitmapDesc) << 3;
-		count = min(max_bits - off, bitmap->s_nr_groups);
+		bmp += sizeof(struct spaceBitmapDesc);
+		max_bits -= sizeof(struct spaceBitmapDesc) * 8;
+		bmp_start = 0;
 	} else {
-		/*
-		 * Rough check if bitmap number is too big to have any bitmap
- 		 * blocks reserved.
-		 */
-		if (bitmap_nr >
-		    (bitmap->s_nr_groups >> (sb->s_blocksize_bits + 3)) + 2)
-			return 0;
-		off = 0;
-		count = bitmap->s_nr_groups - bitmap_nr * max_bits +
-				(sizeof(struct spaceBitmapDesc) << 3);
-		count = min(count, max_bits);
+		bmp_start = bitmap_nr * max_bits -
+				sizeof(struct spaceBitmapDesc) * 8;
 	}
+	start_blk = bitmap->s_extPosition;
+	end_blk = start_blk + bitmap->s_nr_groups;
+	/* No overlap? */
+	if (end_blk <= bmp_start)
+		return 0;
+	if (start_blk < bmp_start)
+		start_blk = 0;
+	else
+		start_blk -= bmp_start;
+	if (end_blk > bmp_start + max_bits)
+		end_blk = max_bits;
+	else
+		end_blk -= bmp_start;
 
-	for (i = 0; i < count; i++)
-		if (udf_test_bit(i + off, bh->b_data)) {
+	for (i = start_blk; i < end_blk; i++)
+		if (udf_test_bit(i, bmp)) {
 			bitmap->s_block_bitmap[bitmap_nr] =
 							ERR_PTR(-EFSCORRUPTED);
 			brelse(bh);
-- 
2.53.0


                 reply	other threads:[~2026-11-24 15:16 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261124151554.42698-2-jack@suse.cz \
    --to=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=rye@keypressure.com \
    --cc=stable@vger.kernel.org \
    /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