Skip to content


Browse files Browse the repository at this point in the history
set_fat(): Fix off-by-2 error leading to corruption in FAT12
In FAT12 two 12 bit entries are combined to a 24 bit value (three
bytes). Therefore, when an even numbered FAT entry is set in FAT12, it
must be be combined with the following entry. To prevent accessing
beyond the end of the FAT array, it must be checked that the cluster is
not the last one.

Previously, the check tested that the requested cluster was equal to
fs->clusters - 1. However, fs->clusters is the number of data clusters
not including the two reserved FAT entries at the start so the test
triggered two clusters early.

If the third to last entry was written on a FAT12 filesystem with an
odd number of clusters, the second to last entry would be corrupted.
This corruption may also lead to invalid memory accesses when the
corrupted entry becomes out of bounds and is used later.

Change the test to fs->clusters + 1 to fix.

Reported-by: Hanno Böck
Signed-off-by: Andreas Bombe <>
  • Loading branch information
andreasbombe committed Sep 11, 2015
1 parent 39ce90f commit 0790812
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/fat.c
Expand Up @@ -205,7 +205,7 @@ void set_fat(DOS_FS * fs, uint32_t cluster, int32_t new)
data[1] = new >> 4;
} else {
FAT_ENTRY subseqEntry;
if (cluster != fs->clusters - 1)
if (cluster != fs->clusters + 1)
get_fat(&subseqEntry, fs->fat, cluster + 1, fs);
subseqEntry.value = 0;
Expand Down

0 comments on commit 0790812

Please sign in to comment.