All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ext4: Fix loff_t overflow in ext4_max_bitmap_size()
@ 2021-06-05  5:09 Ritesh Harjani
  2021-06-07 10:00 ` Jan Kara
  2021-10-01  3:11 ` Theodore Ts'o
  0 siblings, 2 replies; 4+ messages in thread
From: Ritesh Harjani @ 2021-06-05  5:09 UTC (permalink / raw)
  To: linux-ext4; +Cc: Theodore Ts'o, Andreas Dilger, Jan Kara, Ritesh Harjani

We should use unsigned long long rather than loff_t to avoid
overflow in ext4_max_bitmap_size() for comparison before returning.
w/o this patch sbi->s_bitmap_maxbytes was becoming a negative
value due to overflow of upper_limit (with has_huge_files as true)

Below is a quick test to trigger it on a 64KB pagesize system.

sudo mkfs.ext4 -b 65536 -O ^has_extents,^64bit /dev/loop2
sudo mount /dev/loop2 /mnt
sudo echo "hello" > /mnt/hello 	-> This will error out with
				"echo: write error: File too large"

Signed-off-by: Ritesh Harjani <riteshh@linux.ibm.com>
---
 fs/ext4/super.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 7dc94f3e18e6..bedb66386966 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -3189,17 +3189,17 @@ static loff_t ext4_max_size(int blkbits, int has_huge_files)
  */
 static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
 {
-	loff_t res = EXT4_NDIR_BLOCKS;
+	unsigned long long upper_limit, res = EXT4_NDIR_BLOCKS;
 	int meta_blocks;
-	loff_t upper_limit;
-	/* This is calculated to be the largest file size for a dense, block
+
+	/*
+	 * This is calculated to be the largest file size for a dense, block
 	 * mapped file such that the file's total number of 512-byte sectors,
 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
 	 *
 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
 	 * number of 512-byte sectors of the file.
 	 */
-
 	if (!has_huge_files) {
 		/*
 		 * !has_huge_files or implies that the inode i_block field
@@ -3242,7 +3242,7 @@ static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
 	if (res > MAX_LFS_FILESIZE)
 		res = MAX_LFS_FILESIZE;
 
-	return res;
+	return (loff_t)res;
 }
 
 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] ext4: Fix loff_t overflow in ext4_max_bitmap_size()
  2021-06-05  5:09 [PATCH] ext4: Fix loff_t overflow in ext4_max_bitmap_size() Ritesh Harjani
@ 2021-06-07 10:00 ` Jan Kara
  2021-06-19  1:41   ` Ritesh Harjani
  2021-10-01  3:11 ` Theodore Ts'o
  1 sibling, 1 reply; 4+ messages in thread
From: Jan Kara @ 2021-06-07 10:00 UTC (permalink / raw)
  To: Ritesh Harjani; +Cc: linux-ext4, Theodore Ts'o, Andreas Dilger, Jan Kara

On Sat 05-06-21 10:39:32, Ritesh Harjani wrote:
> We should use unsigned long long rather than loff_t to avoid
> overflow in ext4_max_bitmap_size() for comparison before returning.
> w/o this patch sbi->s_bitmap_maxbytes was becoming a negative
> value due to overflow of upper_limit (with has_huge_files as true)
> 
> Below is a quick test to trigger it on a 64KB pagesize system.
> 
> sudo mkfs.ext4 -b 65536 -O ^has_extents,^64bit /dev/loop2
> sudo mount /dev/loop2 /mnt
> sudo echo "hello" > /mnt/hello 	-> This will error out with
> 				"echo: write error: File too large"
> 
> Signed-off-by: Ritesh Harjani <riteshh@linux.ibm.com>

OK, this works (although it's really tight ;). Won't it be somewhat safer
if we compared upper_limit and res before shifting both by blocksize_bits
to the left? Basically we need to shift only for comparison with
MAX_LFS_FILESIZE which is in bytes... But either way feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
>  fs/ext4/super.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> index 7dc94f3e18e6..bedb66386966 100644
> --- a/fs/ext4/super.c
> +++ b/fs/ext4/super.c
> @@ -3189,17 +3189,17 @@ static loff_t ext4_max_size(int blkbits, int has_huge_files)
>   */
>  static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
>  {
> -	loff_t res = EXT4_NDIR_BLOCKS;
> +	unsigned long long upper_limit, res = EXT4_NDIR_BLOCKS;
>  	int meta_blocks;
> -	loff_t upper_limit;
> -	/* This is calculated to be the largest file size for a dense, block
> +
> +	/*
> +	 * This is calculated to be the largest file size for a dense, block
>  	 * mapped file such that the file's total number of 512-byte sectors,
>  	 * including data and all indirect blocks, does not exceed (2^48 - 1).
>  	 *
>  	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
>  	 * number of 512-byte sectors of the file.
>  	 */
> -
>  	if (!has_huge_files) {
>  		/*
>  		 * !has_huge_files or implies that the inode i_block field
> @@ -3242,7 +3242,7 @@ static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
>  	if (res > MAX_LFS_FILESIZE)
>  		res = MAX_LFS_FILESIZE;
>  
> -	return res;
> +	return (loff_t)res;
>  }
>  
>  static ext4_fsblk_t descriptor_loc(struct super_block *sb,
> -- 
> 2.31.1
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ext4: Fix loff_t overflow in ext4_max_bitmap_size()
  2021-06-07 10:00 ` Jan Kara
@ 2021-06-19  1:41   ` Ritesh Harjani
  0 siblings, 0 replies; 4+ messages in thread
From: Ritesh Harjani @ 2021-06-19  1:41 UTC (permalink / raw)
  To: Jan Kara; +Cc: linux-ext4, Theodore Ts'o, Andreas Dilger

On 21/06/07 12:00PM, Jan Kara wrote:
> On Sat 05-06-21 10:39:32, Ritesh Harjani wrote:
> > We should use unsigned long long rather than loff_t to avoid
> > overflow in ext4_max_bitmap_size() for comparison before returning.
> > w/o this patch sbi->s_bitmap_maxbytes was becoming a negative
> > value due to overflow of upper_limit (with has_huge_files as true)
> >
> > Below is a quick test to trigger it on a 64KB pagesize system.
> >
> > sudo mkfs.ext4 -b 65536 -O ^has_extents,^64bit /dev/loop2
> > sudo mount /dev/loop2 /mnt
> > sudo echo "hello" > /mnt/hello 	-> This will error out with
> > 				"echo: write error: File too large"
> >
> > Signed-off-by: Ritesh Harjani <riteshh@linux.ibm.com>
>
> OK, this works (although it's really tight ;). Won't it be somewhat safer
> if we compared upper_limit and res before shifting both by blocksize_bits
> to the left? Basically we need to shift only for comparison with
> MAX_LFS_FILESIZE which is in bytes... But either way feel free to add:

Yes, at 1st I did think that, but since for comparing "res" against
MAX_LFS_FILESIZE we will be anyway doing the bit shifting and since this logic
too was (just) fitting into the limits so I thought of keeping it the same.
But, if absolutely required, I can make those changes.

>
> Reviewed-by: Jan Kara <jack@suse.cz>


Thanks for the review :)

-ritesh

>
> 								Honza
>
> > ---
> >  fs/ext4/super.c | 10 +++++-----
> >  1 file changed, 5 insertions(+), 5 deletions(-)
> >
> > diff --git a/fs/ext4/super.c b/fs/ext4/super.c
> > index 7dc94f3e18e6..bedb66386966 100644
> > --- a/fs/ext4/super.c
> > +++ b/fs/ext4/super.c
> > @@ -3189,17 +3189,17 @@ static loff_t ext4_max_size(int blkbits, int has_huge_files)
> >   */
> >  static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
> >  {
> > -	loff_t res = EXT4_NDIR_BLOCKS;
> > +	unsigned long long upper_limit, res = EXT4_NDIR_BLOCKS;
> >  	int meta_blocks;
> > -	loff_t upper_limit;
> > -	/* This is calculated to be the largest file size for a dense, block
> > +
> > +	/*
> > +	 * This is calculated to be the largest file size for a dense, block
> >  	 * mapped file such that the file's total number of 512-byte sectors,
> >  	 * including data and all indirect blocks, does not exceed (2^48 - 1).
> >  	 *
> >  	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
> >  	 * number of 512-byte sectors of the file.
> >  	 */
> > -
> >  	if (!has_huge_files) {
> >  		/*
> >  		 * !has_huge_files or implies that the inode i_block field
> > @@ -3242,7 +3242,7 @@ static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
> >  	if (res > MAX_LFS_FILESIZE)
> >  		res = MAX_LFS_FILESIZE;
> >
> > -	return res;
> > +	return (loff_t)res;
> >  }
> >
> >  static ext4_fsblk_t descriptor_loc(struct super_block *sb,
> > --
> > 2.31.1
> >
> --
> Jan Kara <jack@suse.com>
> SUSE Labs, CR

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ext4: Fix loff_t overflow in ext4_max_bitmap_size()
  2021-06-05  5:09 [PATCH] ext4: Fix loff_t overflow in ext4_max_bitmap_size() Ritesh Harjani
  2021-06-07 10:00 ` Jan Kara
@ 2021-10-01  3:11 ` Theodore Ts'o
  1 sibling, 0 replies; 4+ messages in thread
From: Theodore Ts'o @ 2021-10-01  3:11 UTC (permalink / raw)
  To: Ritesh Harjani, linux-ext4; +Cc: Theodore Ts'o, Jan Kara, Andreas Dilger

On Sat, 5 Jun 2021 10:39:32 +0530, Ritesh Harjani wrote:
> We should use unsigned long long rather than loff_t to avoid
> overflow in ext4_max_bitmap_size() for comparison before returning.
> w/o this patch sbi->s_bitmap_maxbytes was becoming a negative
> value due to overflow of upper_limit (with has_huge_files as true)
> 
> Below is a quick test to trigger it on a 64KB pagesize system.
> 
> [...]

Applied, thanks!

[1/1] ext4: Fix loff_t overflow in ext4_max_bitmap_size()
      commit: f9b9e1afe996e8b4a0a2ea8481c41756fff53d08

Best regards,
-- 
Theodore Ts'o <tytso@mit.edu>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-10-01  3:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-05  5:09 [PATCH] ext4: Fix loff_t overflow in ext4_max_bitmap_size() Ritesh Harjani
2021-06-07 10:00 ` Jan Kara
2021-06-19  1:41   ` Ritesh Harjani
2021-10-01  3:11 ` Theodore Ts'o

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.