diff mbox series

ext4: avoid arithemetic overflow that can trigger a BUG

Message ID 20180831174126.13071-1-tytso@mit.edu
State Superseded
Headers show
Series ext4: avoid arithemetic overflow that can trigger a BUG | expand

Commit Message

Theodore Ts'o Aug. 31, 2018, 5:41 p.m. UTC
A maliciously crafted file system can cause an overflow when the
results of a 64-bit calculation is stored into a 32-bit length
parameter.

https://bugzilla.kernel.org/show_bug.cgi?id=200623

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Reported-by: Wen Xu <wen.xu@gatech.edu>
Cc: stable@vger.kernel.org
---
 fs/ext4/inode.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Andreas Dilger Aug. 31, 2018, 7:31 p.m. UTC | #1
On Aug 31, 2018, at 11:41 AM, Theodore Ts'o <tytso@mit.edu> wrote:
> 
> A maliciously crafted file system can cause an overflow when the
> results of a 64-bit calculation is stored into a 32-bit length
> parameter.
> 
> https://bugzilla.kernel.org/show_bug.cgi?id=200623
> 
> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
> Reported-by: Wen Xu <wen.xu@gatech.edu>
> Cc: stable@vger.kernel.org
> ---
> fs/ext4/inode.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index 8f6ad7667974..1134c3473673 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
> @@ -3414,6 +3414,7 @@ static int ext4_iomap_begin(struct inode *inode,
> 	unsigned int blkbits = inode->i_blkbits;
> 	unsigned long first_block = offset >> blkbits;
> 	unsigned long last_block = (offset + length - 1) >> blkbits;
> +	unsigned long len;
> 	struct ext4_map_blocks map;
> 	bool delalloc = false;
> 	int ret;
> @@ -3434,7 +3435,8 @@ static int ext4_iomap_begin(struct inode *inode,
> 	}
> 
> 	map.m_lblk = first_block;
> -	map.m_len = last_block - first_block + 1;
> +	len = last_block - first_block + 1;
> +	map.m_len = (len < UINT_MAX) ? len : UINT_MAX;

Wouldn't "(len < UINT_MAX)" always be true on a 32-bit system, or is there some
other limitation in that case (e.g. filesystem < 16TB) that prevents it from
being an issue?  Otherwise, this should use "unsigned long long len".

Cheers, Andreas
Jiri Slaby Aug. 31, 2018, 7:31 p.m. UTC | #2
On 08/31/2018, 07:41 PM, Theodore Ts'o wrote:
> A maliciously crafted file system can cause an overflow when the
> results of a 64-bit calculation is stored into a 32-bit length
> parameter.
> 
> https://bugzilla.kernel.org/show_bug.cgi?id=200623
> 
> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
> Reported-by: Wen Xu <wen.xu@gatech.edu>
> Cc: stable@vger.kernel.org
> ---
>  fs/ext4/inode.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index 8f6ad7667974..1134c3473673 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
> @@ -3414,6 +3414,7 @@ static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
>  	unsigned int blkbits = inode->i_blkbits;
>  	unsigned long first_block = offset >> blkbits;
>  	unsigned long last_block = (offset + length - 1) >> blkbits;
> +	unsigned long len;
>  	struct ext4_map_blocks map;
>  	bool delalloc = false;
>  	int ret;
> @@ -3434,7 +3435,8 @@ static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
>  	}
>  
>  	map.m_lblk = first_block;
> -	map.m_len = last_block - first_block + 1;
> +	len = last_block - first_block + 1;
> +	map.m_len = (len < UINT_MAX) ? len : UINT_MAX;

Can't this be just

map.m_len = min_t(unsigned long, last_block - first_block + 1, UINT_MAX)?


>  	if (flags & IOMAP_REPORT) {
>  		ret = ext4_map_blocks(NULL, inode, &map, 0);
> 

thanks,
Theodore Ts'o Sept. 1, 2018, 2:49 a.m. UTC | #3
On Fri, Aug 31, 2018 at 01:31:05PM -0600, Andreas Dilger wrote:
> > 	map.m_lblk = first_block;
> > -	map.m_len = last_block - first_block + 1;
> > +	len = last_block - first_block + 1;
> > +	map.m_len = (len < UINT_MAX) ? len : UINT_MAX;
> 
> Wouldn't "(len < UINT_MAX)" always be true on a 32-bit system, or is there some
> other limitation in that case (e.g. filesystem < 16TB) that prevents it from
> being an issue?  Otherwise, this should use "unsigned long long len".

first_block and last_block are both 32-bit values and defined as
unsigned long.  That's because they are logical block numbers and
should never be more than 2**32.  The fact that last_block had
overflowed was due to i_size being corrupted to being an insanely
large number.

So it's fine that len is an unsigned long, since first_block and
last_block are both unsigned long.

					- Ted
Theodore Ts'o Sept. 1, 2018, 2:50 a.m. UTC | #4
On Fri, Aug 31, 2018 at 09:31:36PM +0200, Jiri Slaby wrote:
> 
> Can't this be just
> 
> map.m_len = min_t(unsigned long, last_block - first_block + 1, UINT_MAX)?

Yes, good point, thanks.

					- Ted
diff mbox series

Patch

diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 8f6ad7667974..1134c3473673 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -3414,6 +3414,7 @@  static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 	unsigned int blkbits = inode->i_blkbits;
 	unsigned long first_block = offset >> blkbits;
 	unsigned long last_block = (offset + length - 1) >> blkbits;
+	unsigned long len;
 	struct ext4_map_blocks map;
 	bool delalloc = false;
 	int ret;
@@ -3434,7 +3435,8 @@  static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 	}
 
 	map.m_lblk = first_block;
-	map.m_len = last_block - first_block + 1;
+	len = last_block - first_block + 1;
+	map.m_len = (len < UINT_MAX) ? len : UINT_MAX;
 
 	if (flags & IOMAP_REPORT) {
 		ret = ext4_map_blocks(NULL, inode, &map, 0);