Skip to contentSkip to conversation

ext4: avoid arithemetic overflow that can trigger a BUG

PATCH In review Theodore Ts'o 3 participants 6 messages Aug 31, 2018 – Sep 1, 2018

Subsystems (2)
lore
Share & tools
lore

Theodore Ts'o

author maintainer
#0 lore raw

PATCH 1/1 ext4: avoid arithemetic overflow that can trigger a BUG

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

---
 fs/ext4/inode.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/ext4/inode.c +3 -1
index 8f6ad7667974..1134c3473673 100644
@@ -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);
Signature · 1 line
2.18.0.rc0
#1 lore raw
Quoted 27 lines · Theodore Ts'o

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

---
fs/ext4/inode.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
fs/ext4/inode.c +1 -0
index 8f6ad7667974..1134c3473673 100644
@@ -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;

fs/ext4/inode.c static int ext4_iomap_begin(struct inode *inode, · line 3439

Quoted 6 lines
fs/ext4/inode.c +2 -1
@@ -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

author maintainer
#2 lore raw
Quoted 8 lines · Andreas Dilger

fs/ext4/inode.c static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, · line 3439

Quoted 4 lines
	map.m_lblk = first_block;
fs/ext4/inode.c +2 -1
-	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

#3 lore raw
In reply to Theodore Ts'o
Quoted 26 lines · Theodore Ts'o

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

---
 fs/ext4/inode.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/ext4/inode.c +1 -0
index 8f6ad7667974..1134c3473673 100644
@@ -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;

fs/ext4/inode.c static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, · line 3439

Quoted 6 lines
fs/ext4/inode.c +2 -1
@@ -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)?

fs/ext4/inode.c static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,

Quoted 3 lines
fs/ext4/inode.c +0 -0
 	if (flags & IOMAP_REPORT) {
 		ret = ext4_map_blocks(NULL, inode, &map, 0);
 

thanks,

Signature · 2 lines
js
suse labs
author maintainer
#4 lore raw
Quoted 4 lines · Jiri Slaby

Can't this be just

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

Yes, good point, thanks.

- Ted

author maintainer
#5 lore raw

PATCH 1/1 ext4: avoid arithemetic overflow that can trigger a BUG

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

---
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(-)
fs/ext4/ext4.h +3 -0
index 249bcee4d7b2..ac05bd86643a 100644
@@ -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
  */
fs/ext4/inode.c +6 -2
index 8f6ad7667974..694f31364206 100644
@@ -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)) {
Signature · 1 line
2.18.0.rc0