fs/fat: Avoid corruption of sectors following the FAT

The FAT is read/flushed in segments of 6 (FATBUFBLOCKS) disk sectors. The
last segment may be less than 6 sectors, cap the length.

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Reviewed-by: Benoît Thébaudeau <benoit.thebaudeau.dev@gmail.com>
This commit is contained in:
Stefan Brüns 2016-12-17 00:27:50 +01:00 committed by Tom Rini
parent c99d1b3ccf
commit 6c1a808052
2 changed files with 13 additions and 10 deletions

View File

@ -202,6 +202,7 @@ static __u32 get_fatent(fsdata *mydata, __u32 entry)
__u32 fatlength = mydata->fatlength; __u32 fatlength = mydata->fatlength;
__u32 startblock = bufnum * FATBUFBLOCKS; __u32 startblock = bufnum * FATBUFBLOCKS;
/* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
if (startblock + getsize > fatlength) if (startblock + getsize > fatlength)
getsize = fatlength - startblock; getsize = fatlength - startblock;

View File

@ -117,10 +117,11 @@ static int flush_dirty_fat_buffer(fsdata *mydata)
if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1)) if ((!mydata->fat_dirty) || (mydata->fatbufnum == -1))
return 0; return 0;
startblock += mydata->fat_sect; /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
if (startblock + getsize > fatlength)
getsize = fatlength - startblock;
if (getsize > fatlength) startblock += mydata->fat_sect;
getsize = fatlength;
/* Write FAT buf */ /* Write FAT buf */
if (disk_write(startblock, getsize, bufptr) < 0) { if (disk_write(startblock, getsize, bufptr) < 0) {
@ -187,8 +188,9 @@ static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
__u32 fatlength = mydata->fatlength; __u32 fatlength = mydata->fatlength;
__u32 startblock = bufnum * FATBUFBLOCKS; __u32 startblock = bufnum * FATBUFBLOCKS;
if (getsize > fatlength) /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
getsize = fatlength; if (startblock + getsize > fatlength)
getsize = fatlength - startblock;
startblock += mydata->fat_sect; /* Offset from start of disk */ startblock += mydata->fat_sect; /* Offset from start of disk */
@ -499,15 +501,15 @@ static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
__u32 fatlength = mydata->fatlength; __u32 fatlength = mydata->fatlength;
__u32 startblock = bufnum * FATBUFBLOCKS; __u32 startblock = bufnum * FATBUFBLOCKS;
fatlength *= mydata->sect_size; /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
startblock += mydata->fat_sect; if (startblock + getsize > fatlength)
getsize = fatlength - startblock;
if (getsize > fatlength)
getsize = fatlength;
if (flush_dirty_fat_buffer(mydata) < 0) if (flush_dirty_fat_buffer(mydata) < 0)
return -1; return -1;
startblock += mydata->fat_sect;
if (disk_read(startblock, getsize, bufptr) < 0) { if (disk_read(startblock, getsize, bufptr) < 0) {
debug("Error reading FAT blocks\n"); debug("Error reading FAT blocks\n");
return -1; return -1;