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

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