public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Robinson, Herbie" <Herbie.Robinson@stratus.com>
To: "edk2-devel@lists.01.org" <edk2-devel@lists.01.org>
Subject: [PATCH 1/1] FatPkg/EnhancedFatDxe Fix Double Cluster Allocation
Date: Fri, 7 Sep 2018 00:07:09 +0000	[thread overview]
Message-ID: <DM6PR08MB5436DB2CAC9C0878B5AFEFC1E6000@DM6PR08MB5436.namprd08.prod.outlook.com> (raw)

This is a fix for a double cluster allocation when the disk is full.  The double allocation happens because FatGrowEof calls FatAllocateCluster without immediately marking the each returned cluster as allocated.  The fix is to move the FatSetFatEntry call inside the loop.  I've also include some improvements to the sanity checks that I added while tracking this down.  They are optional.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by:Herbie Robinson <Herbie.Robinson@stratus.com>
---
diff --git a/FatPkg/EnhancedFatDxe/FileSpace.c b/FatPkg/EnhancedFatDxe/FileSpace.c
index 1254cd6..e17d3b6 100644
--- a/FatPkg/EnhancedFatDxe/FileSpace.c
+++ b/FatPkg/EnhancedFatDxe/FileSpace.c
@@ -467,7 +467,7 @@ FatGrowEof (
       ClusterCount  = 0;

       while (!FAT_END_OF_FAT_CHAIN (Cluster)) {
-        if (Cluster == FAT_CLUSTER_FREE || Cluster >= FAT_CLUSTER_SPECIAL) {
+        if (Cluster < FAT_MIN_CLUSTER || Cluster > Volume->MaxCluster + 1) {

           DEBUG (
             (EFI_D_INIT | EFI_D_ERROR,
@@ -509,6 +509,11 @@ FatGrowEof (
         goto Done;
       }

+      if (NewCluster < FAT_MIN_CLUSTER || NewCluster > Volume->MaxCluster + 1) {
+        Status = EFI_VOLUME_CORRUPTED;
+        goto Done;
+      }
+
       if (LastCluster != 0) {
         FatSetFatEntry (Volume, LastCluster, NewCluster);
       } else {
@@ -518,12 +523,21 @@ FatGrowEof (

       LastCluster = NewCluster;
       CurSize += 1;
+
+      //
+      // Terminate the cluster list
+      //
+      // Note that we must do this EVERY time we allocate a cluster, because
+      // FatAllocateCluster scans the FAT looking for a free cluster and
+      // "LastCluster" is no longer free!  Usually, FatAllocateCluster will
+      // start looking with the cluster after "LastCluster"; however, when
+      // there is only one free cluster left, it will find "LastCluster"
+      // a second time.  There are other, less predictable scenarios
+      // where this could happen, as well.
+      //
+      FatSetFatEntry (Volume, LastCluster, (UINTN) FAT_CLUSTER_LAST);
+      OFile->FileLastCluster = LastCluster;
     }
-    //
-    // Terminate the cluster list
-    //
-    FatSetFatEntry (Volume, LastCluster, (UINTN) FAT_CLUSTER_LAST);
-    OFile->FileLastCluster = LastCluster;
   }

   OFile->FileSize = (UINTN) NewSizeInBytes;
@@ -603,7 +617,7 @@ FatOFilePosition (
       Cluster = FatGetFatEntry (Volume, Cluster);
     }

-    if (Cluster < FAT_MIN_CLUSTER) {
+    if (Cluster < FAT_MIN_CLUSTER || Cluster > Volume->MaxCluster + 1) {
       return EFI_VOLUME_CORRUPTED;
     }



             reply	other threads:[~2018-09-07  0:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-07  0:07 Robinson, Herbie [this message]
2018-09-11 16:02 ` [PATCH 1/1] FatPkg/EnhancedFatDxe Fix Double Cluster Allocation Laszlo Ersek
2018-09-13  2:45 ` Ni, Ruiyu
  -- strict thread matches above, loose matches on Subject: below --
2018-09-07  3:22 Robinson, Herbie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DM6PR08MB5436DB2CAC9C0878B5AFEFC1E6000@DM6PR08MB5436.namprd08.prod.outlook.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox