2008-07-11 08:49:08

by Manish Katiyar

[permalink] [raw]
Subject: [PATCH] e2fsprogs : Add stricter checks for blocksize in ext2fs_open

Below patch adds stricter checks in ext2fs_open() so that we catch bad
block sizes earlier than later.

============================================================================

Signed-off-by: "Manish Katiyar" <[email protected]>

---
lib/ext2fs/openfs.c | 4 +++-
1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/lib/ext2fs/openfs.c b/lib/ext2fs/openfs.c
index fc54afe..670cc7c 100644
--- a/lib/ext2fs/openfs.c
+++ b/lib/ext2fs/openfs.c
@@ -233,7 +233,9 @@ errcode_t ext2fs_open2(const char *name, const
char *io_options,
}

fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
- if (fs->blocksize == 0) {
+ if ((fs->blocksize < EXT2_MIN_BLOCK_SIZE) ||
+ (fs->blocksize > EXT2_MAX_BLOCK_SIZE) ||
+ (fs->blocksize % EXT2_MIN_BLOCK_SIZE != 0)) {
retval = EXT2_ET_CORRUPT_SUPERBLOCK;
goto cleanup;
}
--
1.5.4.3


============================================================================

--
Thanks & Regards,
********************************************
Manish Katiyar ( http://mkatiyar.googlepages.com )
3rd Floor, Fair Winds Block
EGL Software Park
Off Intermediate Ring Road
Bangalore 560071, India
***********************************************


2008-07-11 12:43:37

by Peter Staubach

[permalink] [raw]
Subject: Re: [PATCH] e2fsprogs : Add stricter checks for blocksize in ext2fs_open

Manish Katiyar wrote:
> Below patch adds stricter checks in ext2fs_open() so that we catch bad
> block sizes earlier than later.
>
> ============================================================================
>
> Signed-off-by: "Manish Katiyar" <[email protected]>
>
> ---
> lib/ext2fs/openfs.c | 4 +++-
> 1 files changed, 3 insertions(+), 1 deletions(-)
>
> diff --git a/lib/ext2fs/openfs.c b/lib/ext2fs/openfs.c
> index fc54afe..670cc7c 100644
> --- a/lib/ext2fs/openfs.c
> +++ b/lib/ext2fs/openfs.c
> @@ -233,7 +233,9 @@ errcode_t ext2fs_open2(const char *name, const
> char *io_options,
> }
>
> fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
> - if (fs->blocksize == 0) {
> + if ((fs->blocksize < EXT2_MIN_BLOCK_SIZE) ||
> + (fs->blocksize > EXT2_MAX_BLOCK_SIZE) ||
> + (fs->blocksize % EXT2_MIN_BLOCK_SIZE != 0)) {
>

It seems to me that this would read more clearly as:

((fs->blocksize % EXT2_MIN_BLOCK_SIZE) != 0)) {

Thanx!

ps

> retval = EXT2_ET_CORRUPT_SUPERBLOCK;
> goto cleanup;
> }
>


2008-07-11 12:55:08

by Theodore Ts'o

[permalink] [raw]
Subject: Re: [PATCH] e2fsprogs : Add stricter checks for blocksize in ext2fs_open

On Fri, Jul 11, 2008 at 02:19:06PM +0530, Manish Katiyar wrote:
> Below patch adds stricter checks in ext2fs_open() so that we catch bad
> block sizes earlier than later.

That concept seems fine; I'm curious why you found this necessary?
Did you have a corrupted filesystem where this caused major problems?
If so, can I have more details?

> fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
> - if (fs->blocksize == 0) {
> + if ((fs->blocksize < EXT2_MIN_BLOCK_SIZE) ||
> + (fs->blocksize > EXT2_MAX_BLOCK_SIZE) ||
> + (fs->blocksize % EXT2_MIN_BLOCK_SIZE != 0)) {

The first and last check is not necessary, given that EXT2_bLOCK_SIZE
is defined as:

#define EXT2_BLOCK_SIZE(s) (EXT2_MIN_BLOCK_SIZE << (s)->s_log_block_size)

So by definition, the blocksize will *always* be greater than or equal
to MIN_BLOCK_SIZE, and it always will be a multiple of EXT2_MIN_BLOCK_SIZE.

The more direct check which we could do would be something like this:

if ((fs->super->s_log_block_size < EXT2_MIN_BLOCK_LOG_SIZE) ||
(fs->super->s_log_block_size > EXT2_MAX_BLOCK_LOG_SIZE))
retval = EXT2_ET_CORRUPT_SUPERBLOCK;
goto cleanup;
}

... before setting fs->blocksize.

I'm curious what problem you were worried about that might happen if
fs->blocksize were greater than 64k, though.

- Ted

2008-07-11 21:47:03

by Theodore Ts'o

[permalink] [raw]
Subject: Re: [PATCH] e2fsprogs : Add stricter checks for blocksize in ext2fs_open

This is what I ended up checking into my tree:

From: Manish Katiyar <[email protected]>
Date: Fri, 11 Jul 2008 17:45:07 -0400
Subject: [PATCH] libext2fs: Add stricter/earlier tests for blocksize in ext2fs_open()

Signed-off-by: "Manish Katiyar" <[email protected]>
Signed-off-by: "Theodore Ts'o" <[email protected]>
---
lib/ext2fs/openfs.c | 5 +++--
1 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/ext2fs/openfs.c b/lib/ext2fs/openfs.c
index fc54afe..525693e 100644
--- a/lib/ext2fs/openfs.c
+++ b/lib/ext2fs/openfs.c
@@ -232,11 +232,12 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
}
}

- fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
- if (fs->blocksize == 0) {
+ if ((fs->super->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
+ EXT2_MAX_BLOCK_LOG_SIZE) {
retval = EXT2_ET_CORRUPT_SUPERBLOCK;
goto cleanup;
}
+ fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
if (EXT2_INODE_SIZE(fs->super) < EXT2_GOOD_OLD_INODE_SIZE) {
retval = EXT2_ET_CORRUPT_SUPERBLOCK;
goto cleanup;
--
1.5.6.1.205.ge2c7.dirty