Skip to content

Commit 0790812

Browse files
committed
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 <aeb@debian.org>
1 parent 39ce90f commit 0790812

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/fat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ void set_fat(DOS_FS * fs, uint32_t cluster, int32_t new)
205205
data[1] = new >> 4;
206206
} else {
207207
FAT_ENTRY subseqEntry;
208-
if (cluster != fs->clusters - 1)
208+
if (cluster != fs->clusters + 1)
209209
get_fat(&subseqEntry, fs->fat, cluster + 1, fs);
210210
else
211211
subseqEntry.value = 0;

0 commit comments

Comments
 (0)