All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Kara <jack@suse.cz>
To: linux-ext4@vger.kernel.org
Cc: Jan Kara <jack@suse.cz>,
	syzbot+0f2f7e65a3007d39539f@syzkaller.appspotmail.com
Subject: Re: [PATCH 1/2] ext2: Add sanity checks for group and filesystem size
Date: Wed, 21 Sep 2022 09:52:27 +0200	[thread overview]
Message-ID: <20220921075227.iyhfsdnulzhtfbzd@quack3> (raw)
In-Reply-To: <20220914154728.20280-1-jack@suse.cz>

[-- Attachment #1: Type: text/plain, Size: 772 bytes --]

On Wed 14-09-22 17:47:22, Jan Kara wrote:
> Add sanity check that filesystem size does not exceed the underlying
> device size and that group size is big enough so that metadata can fit
> into it. This avoid trying to mount some crafted filesystems with
> extremely large group counts.
> 
> Reported-by: syzbot+0f2f7e65a3007d39539f@syzkaller.appspotmail.com
> Signed-off-by: Jan Kara <jack@suse.cz>
...
> +	/* At least inode table, bitmaps, and sb have to fit in one group */
> +	if (sbi->s_blocks_per_group <= sbi->s_inodes_per_group + 3) {

Indeed this should have been comparing against number of inode *table
blocks*, not number of inodes... I've fixed the patch locally, the result
is attached for reference.

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

[-- Attachment #2: 0001-ext2-Add-sanity-checks-for-group-and-filesystem-size.patch --]
[-- Type: text/x-patch, Size: 2115 bytes --]

From b6490b29942fed4366c45e2fe72ae8701f6a1415 Mon Sep 17 00:00:00 2001
From: Jan Kara <jack@suse.cz>
Date: Wed, 14 Sep 2022 17:24:42 +0200
Subject: [PATCH 1/2] ext2: Add sanity checks for group and filesystem size

Add sanity check that filesystem size does not exceed the underlying
device size and that group size is big enough so that metadata can fit
into it. This avoid trying to mount some crafted filesystems with
extremely large group counts.

Reported-by: syzbot+0f2f7e65a3007d39539f@syzkaller.appspotmail.com
Reported-by: kernel test robot <oliver.sang@intel.com> # Test fixup
CC: stable@vger.kernel.org
Signed-off-by: Jan Kara <jack@suse.cz>
---
 fs/ext2/super.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/fs/ext2/super.c b/fs/ext2/super.c
index 252c742379cf..afb31af9302d 100644
--- a/fs/ext2/super.c
+++ b/fs/ext2/super.c
@@ -1052,6 +1052,13 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
 			sbi->s_blocks_per_group);
 		goto failed_mount;
 	}
+	/* At least inode table, bitmaps, and sb have to fit in one group */
+	if (sbi->s_blocks_per_group <= sbi->s_itb_per_group + 3) {
+		ext2_msg(sb, KERN_ERR,
+			"error: #blocks per group smaller than metadata size: %lu <= %lu",
+			sbi->s_blocks_per_group, sbi->s_inodes_per_group + 3);
+		goto failed_mount;
+	}
 	if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
 		ext2_msg(sb, KERN_ERR,
 			"error: #fragments per group too big: %lu",
@@ -1065,9 +1072,14 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
 			sbi->s_inodes_per_group);
 		goto failed_mount;
 	}
+	if (sb_bdev_nr_blocks(sb) < le32_to_cpu(es->s_blocks_count)) {
+		ext2_msg(sb, KERN_ERR,
+			 "bad geometry: block count %u exceeds size of device (%u blocks)",
+			 le32_to_cpu(es->s_blocks_count),
+			 (unsigned)sb_bdev_nr_blocks(sb));
+		goto failed_mount;
+	}
 
-	if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
-		goto cantfind_ext2;
 	sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
 				le32_to_cpu(es->s_first_data_block) - 1)
 					/ EXT2_BLOCKS_PER_GROUP(sb)) + 1;
-- 
2.35.3


  parent reply	other threads:[~2022-09-21  7:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-14 15:47 [PATCH 0/2] ext2: Handle corrupted sb better Jan Kara
2022-09-14 15:47 ` [PATCH 1/2] ext2: Add sanity checks for group and filesystem size Jan Kara
2022-09-21  6:39   ` [ext2] 16339bc257: xfstests.generic.226.fail kernel test robot
2022-09-21  6:39     ` kernel test robot
2022-09-21  7:52   ` Jan Kara [this message]
2022-09-14 15:47 ` [PATCH 2/2] ext2: Use kvmalloc() for group descriptor array Jan Kara

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=20220921075227.iyhfsdnulzhtfbzd@quack3 \
    --to=jack@suse.cz \
    --cc=linux-ext4@vger.kernel.org \
    --cc=syzbot+0f2f7e65a3007d39539f@syzkaller.appspotmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.