Skip to contentSkip to conversation

fs: fat: bound getsize in the FAT write-path cache fills

PATCH In review Shubham Antil 2 participants 3 messages Nov 5 – Nov 18 VFAT/FAT/msdos filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: fat: bound getsize in the FAT write-path cache fills

set_fatent_value() and flush_dirty_fat_buffer() in fs/fat/fat_write.c fill the FAT cache (mydata->fatbuf, FATBUFSIZE bytes) using a sector count, getsize, derived from the on-disk fatlength:

	if (startblock + getsize > fatlength)
		getsize = fatlength - startblock;

getsize is a signed int and fatlength comes from the BPB without validation, so when startblock > fatlength this underflows and the wrapped value is passed to disk_read()/disk_write() as a __u32 block count. In set_fatent_value() disk_read() has no bound of its own, so a crafted FAT (tiny fatlength plus a directory entry with a large start cluster, reached on the overwrite/truncate path) makes it read far past the fixed-size fatbuf -- an out-of-bounds write of attacker disk contents. flush_dirty_fat_buffer() has the identical computation feeding disk_write().

Commit 87d85139a96a ("fs: fat: Perform sanity checks on getsize in get_fatent()") added the same clamp to the read path in fs/fat/fat.c; it did not touch fs/fat/fat_write.c. Make getsize unsigned and clamp it to FATBUFBLOCKS at both write-path sites.

Fixes 87d85139a96a ("fs: fat: Perform sanity checks on getsize in get_fatent()")
Cc Tom Rini <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Simon Glass <[email protected]>
Cc Maxin B. John <[email protected]>
---
 fs/fat/fat_write.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
fs/fat/fat_write.c +12 -2
index a1d28b4e..02b2d313 100644
@@ -219,7 +219,7 @@ static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
  */
 static int flush_dirty_fat_buffer(fsdata *mydata)
 {
-	int getsize = FATBUFBLOCKS;
+	__u32 getsize = FATBUFBLOCKS;
 	__u32 fatlength = mydata->fatlength;
 	__u8 *bufptr = mydata->fatbuf;
 	__u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
@@ -236,6 +236,11 @@ static int flush_dirty_fat_buffer(fsdata *mydata)
 
 	startblock += mydata->fat_sect;
 
+	if (getsize > FATBUFBLOCKS) {
+		debug("getsize is too large for bufptr\n");
+		getsize = FATBUFBLOCKS;
+	}
+
 	/* Write FAT buf */
 	if (disk_write(startblock, getsize, bufptr) < 0) {
 		debug("error: writing FAT blocks\n");
@@ -465,7 +470,7 @@ static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
 
 	/* Read a new block of FAT entries into the cache. */
 	if (bufnum != mydata->fatbufnum) {
-		int getsize = FATBUFBLOCKS;
+		__u32 getsize = FATBUFBLOCKS;
 		__u8 *bufptr = mydata->fatbuf;
 		__u32 fatlength = mydata->fatlength;
 		__u32 startblock = bufnum * FATBUFBLOCKS;
@@ -479,6 +484,11 @@ static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
 
 		startblock += mydata->fat_sect;
 
+		if (getsize > FATBUFBLOCKS) {
+			debug("getsize is too large for bufptr\n");
+			getsize = FATBUFBLOCKS;
+		}
+
 		if (disk_read(startblock, getsize, bufptr) < 0) {
 			debug("Error reading FAT blocks\n");
 			return -1;
Signature · 1 line
2.43.0
#1 lore raw

Hello,

This is a short note on the security issue behind the patch we sent today (Message-ID:
<[email protected]>).

In set_fatent_value(), getsize is a signed int. The existing "cap length if fatlength is not a multiple of FATBUFBLOCKS" line underflows when startblock > fatlength (getsize = fatlength - startblock, both __u32). The wrapped value is passed to disk_read() as the block count. disk_read() has no bound of its own, so a crafted FAT (tiny fatlength from the BPB, plus a directory entry with a large start cluster) makes the block layer read far past the fixed 3072-byte fatbuf -- a heap out-of-bounds write of attacker disk contents. The path is reached by overwriting an existing file with a smaller one (set_contents() truncation).

That is a controlled-content out-of-bounds write. We score the proven impact as CVSS 3.1 AV:L/AC:L/PR:N/UI:R/S:U/C:N/I:H/A:H = 7.1.

This is a missing sibling of CVE-2025-24857. 87d85139a96a ("fs:
fat: Perform sanity checks on getsize in get_fatent()") added the same clamp on the read path in fs/fat/fat.c only. The identical cache-fill in fs/fat/fat_write.c was never touched. A clamp that lives only in get_fatent() does not close this site. flush_dirty_fat_buffer() has the same getsize computation feeding disk_write(); that path fails safe today via the write overflow check, and the posted patch bounds both sites.

On QEMU-TCG the same spray can be steered into bootloader code
execution: TCG re-translates DMA-written code pages, so the overwrite of the relocated image becomes fetchable. That is an emulator property, not something we score for hardware. Physical ARM64 keeps executing cached .text after a DMA write (no icache maintenance from the image), so the expected hardware outcome remains the heap overwrite and a crash or reset.

Affected: CONFIG_FAT_WRITE (not enabled by sandbox_defconfig or qemu_arm64_defconfig; shipped in roughly two dozen in-tree defconfigs). The posted patch makes getsize unsigned and clamps it to FATBUFBLOCKS before the transfer, as get_fatent() already does.

We are not attaching a PoC. Happy to answer review comments on the patch.

Thanks,
Shubham Antil
Octane Security

#2 lore raw
In reply to Shubham Antil
Quoted 8 lines · Shubham Antil

set_fatent_value() and flush_dirty_fat_buffer() in fs/fat/fat_write.c fill the FAT cache (mydata->fatbuf, FATBUFSIZE bytes) using a sector count, getsize, derived from the on-disk fatlength:

	if (startblock + getsize > fatlength)
		getsize = fatlength - startblock;

[...]

Applied to u-boot/main, thanks!

[1/1] fs: fat: bound getsize in the FAT write-path cache fills

      commit: f6e8f09ed39347359ebddd983b96993c78b4ec7d
Signature · 1 line
Tom