1 ext4: avoid arithemetic overflow that can trigger a BUG
3 A maliciously crafted file system can cause an overflow when the
4 results of a 64-bit calculation is stored into a 32-bit length
7 https://bugzilla.kernel.org/show_bug.cgi?id=200623
9 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
10 Reported-by: Wen Xu <wen.xu@gatech.edu>
11 Cc: stable@vger.kernel.org
13 fs/ext4/ext4.h | 3 +++
14 fs/ext4/inode.c | 8 ++++++--
15 2 files changed, 9 insertions(+), 2 deletions(-)
17 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
18 index 249bcee4d7b2..ac05bd86643a 100644
21 @@ -686,6 +686,9 @@ enum {
22 /* Max physical block we can address w/o extents */
23 #define EXT4_MAX_BLOCK_FILE_PHYS 0xFFFFFFFF
25 +/* Max logical block we can support */
26 +#define EXT4_MAX_LOGICAL_BLOCK 0xFFFFFFFF
29 * Structure of an inode on the disk
31 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
32 index 8f6ad7667974..694f31364206 100644
35 @@ -3412,12 +3412,16 @@ static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
37 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
38 unsigned int blkbits = inode->i_blkbits;
39 - unsigned long first_block = offset >> blkbits;
40 - unsigned long last_block = (offset + length - 1) >> blkbits;
41 + unsigned long first_block, last_block;
42 struct ext4_map_blocks map;
43 bool delalloc = false;
46 + if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
48 + first_block = offset >> blkbits;
49 + last_block = min_t(loff_t, (offset + length - 1) >> blkbits,
50 + EXT4_MAX_LOGICAL_BLOCK);
52 if (flags & IOMAP_REPORT) {
53 if (ext4_has_inline_data(inode)) {