2004-11-08 20:00:11

by Andries E. Brouwer

[permalink] [raw]
Subject: [PATCH] don't divide by 0 when trying to mount ext3

Not surprisingly, the ext3 code crashes in the same way
the ext2 code does when dividing by zero.

diff -uprN -X /linux/dontdiff a/fs/ext3/super.c b/fs/ext3/super.c
--- a/fs/ext3/super.c 2004-10-30 21:44:02.000000000 +0200
+++ b/fs/ext3/super.c 2004-11-08 20:55:30.000000000 +0100
@@ -1259,13 +1259,8 @@ static int ext3_fill_super (struct super
es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
sbi->s_es = es;
sb->s_magic = le16_to_cpu(es->s_magic);
- if (sb->s_magic != EXT3_SUPER_MAGIC) {
- if (!silent)
- printk(KERN_ERR
- "VFS: Can't find ext3 filesystem on dev %s.\n",
- sb->s_id);
- goto failed_mount;
- }
+ if (sb->s_magic != EXT3_SUPER_MAGIC)
+ goto cantfind_ext3;

/* Set defaults before we parse the mount options */
def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
@@ -1397,8 +1392,13 @@ static int ext3_fill_super (struct super
sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
+ if (EXT3_INODE_SIZE(sb) == 0)
+ goto cantfind_ext3;
sbi->s_inodes_per_block = blocksize / EXT3_INODE_SIZE(sb);
- sbi->s_itb_per_group = sbi->s_inodes_per_group /sbi->s_inodes_per_block;
+ if (sbi->s_inodes_per_block == 0)
+ goto cantfind_ext3;
+ sbi->s_itb_per_group = sbi->s_inodes_per_group /
+ sbi->s_inodes_per_block;
sbi->s_desc_per_block = blocksize / sizeof(struct ext3_group_desc);
sbi->s_sbh = bh;
sbi->s_mount_state = le16_to_cpu(es->s_state);
@@ -1427,6 +1427,8 @@ static int ext3_fill_super (struct super
goto failed_mount;
}

+ if (EXT3_BLOCKS_PER_GROUP(sb) == 0)
+ goto cantfind_ext3;
sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
le32_to_cpu(es->s_first_data_block) +
EXT3_BLOCKS_PER_GROUP(sb) - 1) /
@@ -1579,6 +1581,12 @@ static int ext3_fill_super (struct super

return 0;

+cantfind_ext3:
+ if (!silent)
+ printk(KERN_ERR "VFS: Can't find ext3 filesystem on dev %s.\n",
+ sb->s_id);
+ goto failed_mount;
+
failed_mount3:
journal_destroy(sbi->s_journal);
failed_mount2:
@@ -1588,10 +1596,8 @@ failed_mount2:
kfree(sbi->s_group_desc);
failed_mount:
#ifdef CONFIG_QUOTA
- for (i = 0; i < MAXQUOTAS; i++) {
- if (sbi->s_qf_names[i])
- kfree(sbi->s_qf_names[i]);
- }
+ for (i = 0; i < MAXQUOTAS; i++)
+ kfree(sbi->s_qf_names[i]);
#endif
ext3_blkdev_remove(sbi);
brelse(bh);


2004-11-08 21:27:37

by Rogier Wolff

[permalink] [raw]
Subject: Re: [PATCH] don't divide by 0 when trying to mount ext3

On Mon, Nov 08, 2004 at 08:59:35PM +0100, Andries Brouwer wrote:
> Not surprisingly, the ext3 code crashes in the same way
> the ext2 code does when dividing by zero.


> + if (sb->s_magic != EXT3_SUPER_MAGIC)
> + goto cantfind_ext3;
[...]
> + if (EXT3_INODE_SIZE(sb) == 0)
> + goto cantfind_ext3;
[...]
> + if (EXT3_BLOCKS_PER_GROUP(sb) == 0)
> + goto cantfind_ext3;

[...]
> +cantfind_ext3:
> + if (!silent)
> + printk(KERN_ERR "VFS: Can't find ext3 filesystem on dev %s.\n",
> + sb->s_id);
> + goto failed_mount;

There are now three cases that end up with the same message and
same error from userspace viewpoint. There are many cases where
debugging a problem is helped when it's possible to find out exactly
which test determined that the filesystem could not be mounted.

How about:

[ ... ] {
errstr = "no magic";
goto cantfind_ext3;
}

--
** [email protected] ** http://www.BitWizard.nl/ ** +31-15-2600998 **
*-- BitWizard writes Linux device drivers for any device you may have! --*
Q: It doesn't work. A: Look buddy, doesn't work is an ambiguous statement.
Does it sit on the couch all day? Is it unemployed? Please be specific!
Define 'it' and what it isn't doing. --------- Adapted from lxrbot FAQ

2004-11-08 21:56:04

by Andries Brouwer

[permalink] [raw]
Subject: Re: [PATCH] don't divide by 0 when trying to mount ext3

On Mon, Nov 08, 2004 at 10:27:11PM +0100, Rogier Wolff wrote:

> There are now three cases that end up with the same message and
> same error from userspace viewpoint. There are many cases where
> debugging a problem is helped when it's possible to find out exactly
> which test determined that the filesystem could not be mounted.

Strings are expensive. Don't like to add worthless code.
We lived without this for years, so it is not a frequent occurrence.
If you have a bad ext2/ext3 system, e2fsck will find what is wrong.

2004-11-08 22:10:45

by Rogier Wolff

[permalink] [raw]
Subject: Re: [PATCH] don't divide by 0 when trying to mount ext3

On Mon, Nov 08, 2004 at 10:54:02PM +0100, Andries Brouwer wrote:
> On Mon, Nov 08, 2004 at 10:27:11PM +0100, Rogier Wolff wrote:
>
> > There are now three cases that end up with the same message and
> > same error from userspace viewpoint. There are many cases where
> > debugging a problem is helped when it's possible to find out exactly
> > which test determined that the filesystem could not be mounted.
>
> Strings are expensive. Don't like to add worthless code.
> We lived without this for years, so it is not a frequent occurrence.
> If you have a bad ext2/ext3 system, e2fsck will find what is wrong.

int cpos=0;

if (++cpos && (errorposibility1 ) ) goto error_handling;
[...]
if (++cpos && (errorposibility2 ) ) goto error_handling;
[...]
if (++cpos && (errorposibility3 ) ) goto error_handling;
[...]
error_handling:

printk (".... %d ...", cpos);

Rogier.
--
** [email protected] ** http://www.BitWizard.nl/ ** +31-15-2600998 **
*-- BitWizard writes Linux device drivers for any device you may have! --*
Q: It doesn't work. A: Look buddy, doesn't work is an ambiguous statement.
Does it sit on the couch all day? Is it unemployed? Please be specific!
Define 'it' and what it isn't doing. --------- Adapted from lxrbot FAQ