ArmPlatformPkg/BootMonFs: Fix permission check in SetFileInfo

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Brendan Jackman <brendan.jackman@arm.com>
Reviewed-by: Olivier Martin <olivier.martin@arm.com>



git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15513 6f19259b-4bc3-4df7-8a09-765794883524
diff --git a/ArmPlatformPkg/FileSystem/BootMonFs/BootMonFsDir.c b/ArmPlatformPkg/FileSystem/BootMonFs/BootMonFsDir.c
index 773490e..bf91bf0 100644
--- a/ArmPlatformPkg/FileSystem/BootMonFs/BootMonFsDir.c
+++ b/ArmPlatformPkg/FileSystem/BootMonFs/BootMonFsDir.c
@@ -288,19 +288,21 @@
   }

 

   // If we're changing the file name

-  if (AsciiStrCmp (FileNameAscii, File->HwDescription.Footer.Filename)) {

-    // Check a file with that filename doesn't already exist

-    if (BootMonGetFileFromAsciiFileName (

-          File->Instance,

-          File->HwDescription.Footer.Filename,

-          &SameFile) != EFI_NOT_FOUND) {

-      Status = EFI_ACCESS_DENIED;

-    } else {

-      AsciiStrCpy (FileNameAscii, File->HwDescription.Footer.Filename);

-      Status = EFI_SUCCESS;

-    }

+  if (AsciiStrCmp (FileNameAscii, File->HwDescription.Footer.Filename) == 0) {

+    // No change to filename.

+    Status = EFI_SUCCESS;

+  } else if (!(File->OpenMode & EFI_FILE_MODE_WRITE)) {

+    // You can only change the filename if you open the file for write.

+    Status = EFI_ACCESS_DENIED;

+  } else if (BootMonGetFileFromAsciiFileName (

+                File->Instance,

+                File->HwDescription.Footer.Filename,

+                &SameFile) != EFI_NOT_FOUND) {

+    // A file with that name already exists.

+    Status = EFI_ACCESS_DENIED;

   } else {

-    // No change to filename

+    // OK, change the filename.

+    AsciiStrCpy (FileNameAscii, File->HwDescription.Footer.Filename);

     Status = EFI_SUCCESS;

   }

 

@@ -316,7 +318,7 @@
 SetFileSize (

   IN BOOTMON_FS_INSTANCE *Instance,

   IN BOOTMON_FS_FILE     *BootMonFsFile,

-  IN UINTN               Size

+  IN UINTN                NewSize

   )

 {

   UINT64             StoredPosition;

@@ -324,6 +326,13 @@
   EFI_FILE_PROTOCOL *File;

   CHAR8              Buffer;

   UINTN              BufferSize;

+  UINT32             OldSize;

+

+  OldSize = BootMonFsFile->HwDescription.Region[0].Size;

+

+  if (OldSize == NewSize) {

+    return EFI_SUCCESS;

+  }

 

   Buffer = 0;

   BufferSize = sizeof (Buffer);

@@ -334,8 +343,8 @@
     return EFI_ACCESS_DENIED;

   }

 

-  if (Size <= BootMonFsFile->HwDescription.Region[0].Size) {

-    BootMonFsFile->HwDescription.Region[0].Size = Size;

+  if (NewSize <= OldSize) {

+    OldSize = NewSize;

   } else {

     // Increasing a file's size is potentially complicated as it may require

     // moving the image description on media. The simplest way to do it is to

@@ -348,7 +357,7 @@
       return Status;

     }

 

-    Status = File->SetPosition (File, Size - 1);

+    Status = File->SetPosition (File, NewSize - 1);

     if (EFI_ERROR (Status)) {

       return Status;

     }

@@ -358,7 +367,7 @@
     }

 

     // Restore saved position

-    Status = File->SetPosition (File, Size - 1);

+    Status = File->SetPosition (File, NewSize - 1);

     if (EFI_ERROR (Status)) {

       return Status;

     }

@@ -398,7 +407,7 @@
       return EFI_ACCESS_DENIED;

     }

 

-    SetFileName (File, Info->FileName);

+    Status = SetFileName (File, Info->FileName);

     if (EFI_ERROR (Status)) {

       return Status;

     }