Skip to content

Commit 7b92d03

Browse files
OGAWAHirofumitorvalds
authored andcommitted
fat: fix possible overflow for fat_clusters
Intermediate value of fat_clusters can be overflowed on 32bits arch. Reported-by: Krzysztof Strasburger <strasbur@chkw386.ch.pwr.wroc.pl> Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5eeb929 commit 7b92d03

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

‎fs/fat/inode.c‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,19 @@ static int fat_read_root(struct inode *inode)
12291229
return 0;
12301230
}
12311231

1232+
static unsigned long calc_fat_clusters(struct super_block *sb)
1233+
{
1234+
struct msdos_sb_info *sbi = MSDOS_SB(sb);
1235+
1236+
/* Divide first to avoid overflow */
1237+
if (sbi->fat_bits != 12) {
1238+
unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits;
1239+
return ent_per_sec * sbi->fat_length;
1240+
}
1241+
1242+
return sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
1243+
}
1244+
12321245
/*
12331246
* Read the super block of an MS-DOS FS.
12341247
*/
@@ -1434,7 +1447,7 @@ int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
14341447
sbi->dirty = b->fat16.state & FAT_STATE_DIRTY;
14351448

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

0 commit comments

Comments
 (0)