Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7b92d03c authored by OGAWA Hirofumi's avatar OGAWA Hirofumi Committed by Linus Torvalds
Browse files

fat: fix possible overflow for fat_clusters



Intermediate value of fat_clusters can be overflowed on 32bits arch.

Reported-by: default avatarKrzysztof Strasburger <strasbur@chkw386.ch.pwr.wroc.pl>
Signed-off-by: default avatarOGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@vger.kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 5eeb9293
Loading
Loading
Loading
Loading
+14 −1
Original line number Original line Diff line number Diff line
@@ -1229,6 +1229,19 @@ static int fat_read_root(struct inode *inode)
	return 0;
	return 0;
}
}


static unsigned long calc_fat_clusters(struct super_block *sb)
{
	struct msdos_sb_info *sbi = MSDOS_SB(sb);

	/* Divide first to avoid overflow */
	if (sbi->fat_bits != 12) {
		unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits;
		return ent_per_sec * sbi->fat_length;
	}

	return sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
}

/*
/*
 * Read the super block of an MS-DOS FS.
 * Read the super block of an MS-DOS FS.
 */
 */
@@ -1434,7 +1447,7 @@ int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
		sbi->dirty = b->fat16.state & FAT_STATE_DIRTY;
		sbi->dirty = b->fat16.state & FAT_STATE_DIRTY;


	/* check that FAT table does not overflow */
	/* check that FAT table does not overflow */
	fat_clusters = sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
	fat_clusters = calc_fat_clusters(sb);
	total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
	total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
	if (total_clusters > MAX_FAT(sb)) {
	if (total_clusters > MAX_FAT(sb)) {
		if (!silent)
		if (!silent)