Blame SOURCES/e2fsprogs-1.42.11-Fix-32-64-bit-overflow-when-multiplying-by-blocks-cl.patch

0ef434
From ce342417662c89d09b24a8fe47e9fe942d1a0c43 Mon Sep 17 00:00:00 2001
0ef434
From: Theodore Ts'o <tytso@mit.edu>
0ef434
Date: Sat, 26 Jul 2014 07:40:36 -0400
0ef434
Subject: [PATCH] Fix 32/64-bit overflow when multiplying by blocks/clusters
0ef434
 per group
0ef434
0ef434
There are a number of places where we need convert groups to blocks or
0ef434
clusters by multiply the groups by blocks/clusters per group.
0ef434
Unfortunately, both quantities are 32-bit, but the result needs to be
0ef434
64-bit, and very often the cast to 64-bit gets lost.
0ef434
0ef434
Fix this by adding new macros, EXT2_GROUPS_TO_BLOCKS() and
0ef434
EXT2_GROUPS_TO_CLUSTERS().
0ef434
0ef434
This should fix a bug where resizing a 64bit file system can result in
0ef434
calculate_minimum_resize_size() looping forever.
0ef434
0ef434
Addresses-Launchpad-Bug: #1321958
0ef434
0ef434
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
0ef434
---
0ef434
 e2fsck/pass5.c          |  2 +-
0ef434
 e2fsck/super.c          |  2 +-
0ef434
 lib/ext2fs/blknum.c     |  2 +-
0ef434
 lib/ext2fs/ext2_fs.h    |  5 +++++
0ef434
 lib/ext2fs/imager.c     | 14 +++++++-------
0ef434
 lib/ext2fs/rw_bitmaps.c |  4 ++--
0ef434
 misc/tune2fs.c          |  2 +-
0ef434
 resize/resize2fs.c      | 11 +++++------
0ef434
 8 files changed, 23 insertions(+), 19 deletions(-)
0ef434
0ef434
diff --git a/e2fsck/pass5.c b/e2fsck/pass5.c
0ef434
index 4409d7f..831232b 100644
0ef434
--- a/e2fsck/pass5.c
0ef434
+++ b/e2fsck/pass5.c
0ef434
@@ -858,7 +858,7 @@ static void check_block_end(e2fsck_t ctx)
0ef434
 	clear_problem_context(&pctx);
0ef434
 
0ef434
 	end = ext2fs_get_block_bitmap_start2(fs->block_map) +
0ef434
-		((blk64_t)EXT2_CLUSTERS_PER_GROUP(fs->super) * fs->group_desc_count) - 1;
0ef434
+		EXT2_GROUPS_TO_CLUSTERS(fs->super, fs->group_desc_count) - 1;
0ef434
 	pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map, end,
0ef434
 						     &save_blocks_count);
0ef434
 	if (pctx.errcode) {
0ef434
diff --git a/e2fsck/super.c b/e2fsck/super.c
0ef434
index 2fcb315..a6be3c6 100644
0ef434
--- a/e2fsck/super.c
0ef434
+++ b/e2fsck/super.c
0ef434
@@ -421,7 +421,7 @@ void check_resize_inode(e2fsck_t ctx)
0ef434
 		for (j = 1; j < fs->group_desc_count; j++) {
0ef434
 			if (!ext2fs_bg_has_super(fs, j))
0ef434
 				continue;
0ef434
-			expect = pblk + (j * fs->super->s_blocks_per_group);
0ef434
+			expect = pblk + EXT2_GROUPS_TO_BLOCKS(fs->super, j);
0ef434
 			if (ind_buf[ind_off] != expect)
0ef434
 				goto resize_inode_invalid;
0ef434
 			ind_off++;
0ef434
diff --git a/lib/ext2fs/blknum.c b/lib/ext2fs/blknum.c
0ef434
index 7a2c588..88cc34e 100644
0ef434
--- a/lib/ext2fs/blknum.c
0ef434
+++ b/lib/ext2fs/blknum.c
0ef434
@@ -29,7 +29,7 @@ dgrp_t ext2fs_group_of_blk2(ext2_filsys fs, blk64_t blk)
0ef434
 blk64_t ext2fs_group_first_block2(ext2_filsys fs, dgrp_t group)
0ef434
 {
0ef434
 	return fs->super->s_first_data_block +
0ef434
-		((blk64_t)group * fs->super->s_blocks_per_group);
0ef434
+		EXT2_GROUPS_TO_BLOCKS(fs->super, group);
0ef434
 }
0ef434
 
0ef434
 /*
0ef434
diff --git a/lib/ext2fs/ext2_fs.h b/lib/ext2fs/ext2_fs.h
0ef434
index 930c2a3..d6adfd4 100644
0ef434
--- a/lib/ext2fs/ext2_fs.h
0ef434
+++ b/lib/ext2fs/ext2_fs.h
0ef434
@@ -264,6 +264,11 @@ struct ext2_dx_countlimit {
0ef434
 #define EXT2_DESC_PER_BLOCK(s)		(EXT2_BLOCK_SIZE(s) / EXT2_DESC_SIZE(s))
0ef434
 #endif
0ef434
 
0ef434
+#define EXT2_GROUPS_TO_BLOCKS(s, g)   ((blk64_t) EXT2_BLOCKS_PER_GROUP(s) * \
0ef434
+				       (g))
0ef434
+#define EXT2_GROUPS_TO_CLUSTERS(s, g) ((blk64_t) EXT2_CLUSTERS_PER_GROUP(s) * \
0ef434
+				       (g))
0ef434
+
0ef434
 /*
0ef434
  * Constants relative to the data blocks
0ef434
  */
0ef434
diff --git a/lib/ext2fs/imager.c b/lib/ext2fs/imager.c
0ef434
index 378a3c8..b643cc6 100644
0ef434
--- a/lib/ext2fs/imager.c
0ef434
+++ b/lib/ext2fs/imager.c
0ef434
@@ -286,8 +286,8 @@ errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
0ef434
 	ext2fs_generic_bitmap	bmap;
0ef434
 	errcode_t		retval;
0ef434
 	ssize_t			actual;
0ef434
-	__u32			itr, cnt, size;
0ef434
-	int			c, total_size;
0ef434
+	size_t			c;
0ef434
+	__u64			itr, cnt, size, total_size;
0ef434
 	char			buf[1024];
0ef434
 
0ef434
 	if (flags & IMAGER_FLAG_INODEMAP) {
0ef434
@@ -308,7 +308,7 @@ errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
0ef434
 		}
0ef434
 		bmap = fs->block_map;
0ef434
 		itr = fs->super->s_first_data_block;
0ef434
-		cnt = EXT2_BLOCKS_PER_GROUP(fs->super) * fs->group_desc_count;
0ef434
+		cnt = EXT2_GROUPS_TO_BLOCKS(fs->super, fs->group_desc_count);
0ef434
 		size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
0ef434
 	}
0ef434
 	total_size = size * fs->group_desc_count;
0ef434
@@ -342,9 +342,9 @@ errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
0ef434
 			if (c > (int) sizeof(buf))
0ef434
 				c = sizeof(buf);
0ef434
 			actual = write(fd, buf, c);
0ef434
-			if (actual == -1)
0ef434
+			if (actual < 0)
0ef434
 				return errno;
0ef434
-			if (actual != c)
0ef434
+			if ((size_t) actual != c)
0ef434
 				return EXT2_ET_SHORT_WRITE;
0ef434
 			size -= c;
0ef434
 		}
0ef434
@@ -360,7 +360,7 @@ errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
0ef434
 {
0ef434
 	ext2fs_generic_bitmap	bmap;
0ef434
 	errcode_t		retval;
0ef434
-	__u32			itr, cnt;
0ef434
+	__u64			itr, cnt;
0ef434
 	char			buf[1024];
0ef434
 	unsigned int		size;
0ef434
 	ssize_t			actual;
0ef434
@@ -383,7 +383,7 @@ errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
0ef434
 		}
0ef434
 		bmap = fs->block_map;
0ef434
 		itr = fs->super->s_first_data_block;
0ef434
-		cnt = EXT2_BLOCKS_PER_GROUP(fs->super) * fs->group_desc_count;
0ef434
+		cnt = EXT2_GROUPS_TO_BLOCKS(fs->super, fs->group_desc_count);
0ef434
 		size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
0ef434
 	}
0ef434
 
0ef434
diff --git a/lib/ext2fs/rw_bitmaps.c b/lib/ext2fs/rw_bitmaps.c
0ef434
index b7d65a9..ad1d8c8 100644
0ef434
--- a/lib/ext2fs/rw_bitmaps.c
0ef434
+++ b/lib/ext2fs/rw_bitmaps.c
0ef434
@@ -225,8 +225,8 @@ static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
0ef434
 		}
0ef434
 		blk = (fs->image_header->offset_blockmap /
0ef434
 		       fs->blocksize);
0ef434
-		blk_cnt = (blk64_t)EXT2_CLUSTERS_PER_GROUP(fs->super) *
0ef434
-			fs->group_desc_count;
0ef434
+		blk_cnt = EXT2_GROUPS_TO_CLUSTERS(fs->super,
0ef434
+						  fs->group_desc_count);
0ef434
 		while (block_nbytes > 0) {
0ef434
 			retval = io_channel_read_blk64(fs->image_io, blk++,
0ef434
 						     1, block_bitmap);
0ef434
diff --git a/misc/tune2fs.c b/misc/tune2fs.c
0ef434
index ff72e09..d2aa125 100644
0ef434
--- a/misc/tune2fs.c
0ef434
+++ b/misc/tune2fs.c
0ef434
@@ -1366,7 +1366,7 @@ static int ext2fs_is_block_in_group(ext2_filsys fs, dgrp_t group, blk64_t blk)
0ef434
 {
0ef434
 	blk64_t start_blk, end_blk;
0ef434
 	start_blk = fs->super->s_first_data_block +
0ef434
-			EXT2_BLOCKS_PER_GROUP(fs->super) * group;
0ef434
+			EXT2_GROUPS_TO_BLOCKS(fs->super, group);
0ef434
 	/*
0ef434
 	 * We cannot get new block beyond end_blk for for the last block group
0ef434
 	 * so we can check with EXT2_BLOCKS_PER_GROUP even for last block group
0ef434
diff --git a/resize/resize2fs.c b/resize/resize2fs.c
0ef434
index 375639a..d6fc533 100644
0ef434
--- a/resize/resize2fs.c
0ef434
+++ b/resize/resize2fs.c
0ef434
@@ -408,8 +408,7 @@ retry:
0ef434
 					    fs->inode_map);
0ef434
 	if (retval) goto errout;
0ef434
 
0ef434
-	real_end = (((blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super) *
0ef434
-		     fs->group_desc_count)) - 1 +
0ef434
+	real_end = EXT2_GROUPS_TO_BLOCKS(fs->super, fs->group_desc_count) - 1 +
0ef434
 		fs->super->s_first_data_block;
0ef434
 	retval = ext2fs_resize_block_bitmap2(new_size - 1,
0ef434
 					     real_end, fs->block_map);
0ef434
@@ -2073,7 +2072,7 @@ blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
0ef434
 		fs->super->s_free_inodes_count;
0ef434
 	blks_needed = ext2fs_div_ceil(inode_count,
0ef434
 				      fs->super->s_inodes_per_group) *
0ef434
-		EXT2_BLOCKS_PER_GROUP(fs->super);
0ef434
+		(blk64_t) EXT2_BLOCKS_PER_GROUP(fs->super);
0ef434
 	groups = ext2fs_div64_ceil(blks_needed,
0ef434
 				   EXT2_BLOCKS_PER_GROUP(fs->super));
0ef434
 #ifdef RESIZE2FS_DEBUG
0ef434
@@ -2117,7 +2116,7 @@ blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
0ef434
 	 * figure out how many data blocks we have given the number of groups
0ef434
 	 * we need for our inodes
0ef434
 	 */
0ef434
-	data_blocks = groups * EXT2_BLOCKS_PER_GROUP(fs->super);
0ef434
+	data_blocks = EXT2_GROUPS_TO_BLOCKS(fs->super, groups);
0ef434
 	last_start = 0;
0ef434
 	for (grp = 0; grp < groups; grp++) {
0ef434
 		overhead = calc_group_overhead(fs, grp, old_desc_blocks);
0ef434
@@ -2151,7 +2150,7 @@ blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
0ef434
 		extra_grps = ext2fs_div64_ceil(remainder,
0ef434
 					       EXT2_BLOCKS_PER_GROUP(fs->super));
0ef434
 
0ef434
-		data_blocks += extra_grps * EXT2_BLOCKS_PER_GROUP(fs->super);
0ef434
+		data_blocks += EXT2_GROUPS_TO_BLOCKS(fs->super, extra_grps);
0ef434
 
0ef434
 		/* ok we have to account for the last group */
0ef434
 		overhead = calc_group_overhead(fs, groups-1, old_desc_blocks);
0ef434
@@ -2241,7 +2240,7 @@ blk64_t calculate_minimum_resize_size(ext2_filsys fs, int flags)
0ef434
 	 * only do groups-1, and then add the number of blocks needed to
0ef434
 	 * handle the group descriptor metadata+data that we need
0ef434
 	 */
0ef434
-	blks_needed = (groups-1) * EXT2_BLOCKS_PER_GROUP(fs->super);
0ef434
+	blks_needed = EXT2_GROUPS_TO_BLOCKS(fs->super, groups - 1);
0ef434
 	blks_needed += overhead;
0ef434
 
0ef434
 	/*
0ef434
-- 
0ef434
2.7.5
0ef434