linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ext4: avoid arithemetic overflow that can trigger a BUG
@ 2018-08-31 17:41 Theodore Ts'o
  2018-08-31 19:31 ` Andreas Dilger
  2018-08-31 19:31 ` Jiri Slaby
  0 siblings, 2 replies; 6+ messages in thread
From: Theodore Ts'o @ 2018-08-31 17:41 UTC (permalink / raw)
  To: Ext4 Developers List; +Cc: Theodore Ts'o, stable

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;
 
 	if (flags & IOMAP_REPORT) {
 		ret = ext4_map_blocks(NULL, inode, &map, 0);
-- 
2.18.0.rc0

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

* Re: [PATCH] ext4: avoid arithemetic overflow that can trigger a BUG
  2018-08-31 17:41 [PATCH] ext4: avoid arithemetic overflow that can trigger a BUG Theodore Ts'o
@ 2018-08-31 19:31 ` Andreas Dilger
  2018-09-01  2:49   ` Theodore Y. Ts'o
  2018-08-31 19:31 ` Jiri Slaby
  1 sibling, 1 reply; 6+ messages in thread
From: Andreas Dilger @ 2018-08-31 19:31 UTC (permalink / raw)
  To: Theodore Ts'o; +Cc: Ext4 Developers List, stable

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

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






[-- Attachment #2: Message signed with OpenPGP --]
[-- Type: application/pgp-signature, Size: 873 bytes --]

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

* Re: [PATCH] ext4: avoid arithemetic overflow that can trigger a BUG
  2018-08-31 17:41 [PATCH] ext4: avoid arithemetic overflow that can trigger a BUG Theodore Ts'o
  2018-08-31 19:31 ` Andreas Dilger
@ 2018-08-31 19:31 ` Jiri Slaby
  2018-09-01  2:50   ` Theodore Y. Ts'o
  1 sibling, 1 reply; 6+ messages in thread
From: Jiri Slaby @ 2018-08-31 19:31 UTC (permalink / raw)
  To: Theodore Ts'o, Ext4 Developers List; +Cc: stable

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,
-- 
js
suse labs

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

* Re: [PATCH] ext4: avoid arithemetic overflow that can trigger a BUG
  2018-08-31 19:31 ` Andreas Dilger
@ 2018-09-01  2:49   ` Theodore Y. Ts'o
  0 siblings, 0 replies; 6+ messages in thread
From: Theodore Y. Ts'o @ 2018-09-01  2:49 UTC (permalink / raw)
  To: Andreas Dilger; +Cc: Ext4 Developers List, stable

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

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

* Re: [PATCH] ext4: avoid arithemetic overflow that can trigger a BUG
  2018-08-31 19:31 ` Jiri Slaby
@ 2018-09-01  2:50   ` Theodore Y. Ts'o
  2018-09-01 16:49     ` [PATCH -v2] " Theodore Ts'o
  0 siblings, 1 reply; 6+ messages in thread
From: Theodore Y. Ts'o @ 2018-09-01  2:50 UTC (permalink / raw)
  To: Jiri Slaby; +Cc: Ext4 Developers List, stable

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

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

* [PATCH -v2] ext4: avoid arithemetic overflow that can trigger a BUG
  2018-09-01  2:50   ` Theodore Y. Ts'o
@ 2018-09-01 16:49     ` Theodore Ts'o
  0 siblings, 0 replies; 6+ messages in thread
From: Theodore Ts'o @ 2018-09-01 16:49 UTC (permalink / raw)
  To: Ext4 Developers List; +Cc: jslaby, adilger, Theodore Ts'o, stable

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
---
This is a revised version which checks for overflows when caluclating
first_block and last_block.  This catches more overflow scenarios than
just checking for an overflow when calculating map.m_len.

 fs/ext4/ext4.h  | 3 +++
 fs/ext4/inode.c | 8 ++++++--
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 249bcee4d7b2..ac05bd86643a 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -686,6 +686,9 @@ enum {
 /* Max physical block we can address w/o extents */
 #define EXT4_MAX_BLOCK_FILE_PHYS	0xFFFFFFFF
 
+/* Max logical block we can support */
+#define EXT4_MAX_LOGICAL_BLOCK		0xFFFFFFFF
+
 /*
  * Structure of an inode on the disk
  */
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 8f6ad7667974..694f31364206 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -3412,12 +3412,16 @@ static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 {
 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
 	unsigned int blkbits = inode->i_blkbits;
-	unsigned long first_block = offset >> blkbits;
-	unsigned long last_block = (offset + length - 1) >> blkbits;
+	unsigned long first_block, last_block;
 	struct ext4_map_blocks map;
 	bool delalloc = false;
 	int ret;
 
+	if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
+		return -EINVAL;
+	first_block = offset >> blkbits;
+	last_block = min_t(loff_t, (offset + length - 1) >> blkbits,
+			   EXT4_MAX_LOGICAL_BLOCK);
 
 	if (flags & IOMAP_REPORT) {
 		if (ext4_has_inline_data(inode)) {
-- 
2.18.0.rc0

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

end of thread, other threads:[~2018-09-01 16:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-08-31 17:41 [PATCH] ext4: avoid arithemetic overflow that can trigger a BUG Theodore Ts'o
2018-08-31 19:31 ` Andreas Dilger
2018-09-01  2:49   ` Theodore Y. Ts'o
2018-08-31 19:31 ` Jiri Slaby
2018-09-01  2:50   ` Theodore Y. Ts'o
2018-09-01 16:49     ` [PATCH -v2] " Theodore Ts'o

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).