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

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