Module Name:    src
Committed By:   jmcneill
Date:           Sat Oct 23 15:20:26 UTC 2021

Modified Files:
        src/sys/external/bsd/gnu-efi/dist/lib: sread.c

Log Message:
The device path passed to EFI_BOOT_SERVICES.LocateDevicePath() may be
modified and the resulting device path may not be aligned in such a way
that the PathName string is not 16-bit aligned.

Fix OpenSimpleFileRead to make a copy of the device path to ensure
alignment before attempting to open a file. Idea from Tianocore's EFI
shell Library/FileIO.c LibOpenFile().


To generate a diff of this commit:
cvs rdiff -u -r1.1.1.1 -r1.2 src/sys/external/bsd/gnu-efi/dist/lib/sread.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/external/bsd/gnu-efi/dist/lib/sread.c
diff -u src/sys/external/bsd/gnu-efi/dist/lib/sread.c:1.1.1.1 src/sys/external/bsd/gnu-efi/dist/lib/sread.c:1.2
--- src/sys/external/bsd/gnu-efi/dist/lib/sread.c:1.1.1.1	Tue Apr  1 16:16:07 2014
+++ src/sys/external/bsd/gnu-efi/dist/lib/sread.c	Sat Oct 23 15:20:26 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: sread.c,v 1.1.1.1 2014/04/01 16:16:07 jakllsch Exp $	*/
+/*	$NetBSD: sread.c,v 1.2 2021/10/23 15:20:26 jmcneill Exp $	*/
 
 /*++
 
@@ -61,11 +61,13 @@ Returns:
     EFI_DEVICE_PATH             *TempFilePath;
     EFI_DEVICE_PATH             *TempFilePathPtr;
     FILEPATH_DEVICE_PATH        *FilePathNode;
+    EFI_DEVICE_PATH_PROTOCOL    *AlignedFilePath;
     EFI_FILE_HANDLE             FileHandle, LastHandle;
     EFI_STATUS                  Status;
     EFI_LOAD_FILE_INTERFACE     *LoadFile;
   
     FHand = NULL;
+    AlignedFilePath = NULL;
     UserFilePath = *FilePath;
 
     //
@@ -106,12 +108,24 @@ Returns:
     Status = FileHandle ? EFI_SUCCESS : EFI_UNSUPPORTED;
 
     //
+    // Duplicate FilePath to make sure it is aligned so that
+    // FilePathNode->PathName below is 16-bit aligned.
+    //
+    AlignedFilePath = DuplicateDevicePath(*FilePath);
+    if (AlignedFilePath == NULL) {
+        if (FileHandle != NULL) {
+            uefi_call_wrapper(FileHandle->Close, 1, FileHandle);
+        }
+        return EFI_OUT_OF_RESOURCES;
+    }
+
+    //
     // To access as a filesystem, the filepath should only
     // contain filepath components.  Follow the filepath nodes
     // and find the target file
     //
 
-    FilePathNode = (FILEPATH_DEVICE_PATH *) *FilePath;
+    FilePathNode = (FILEPATH_DEVICE_PATH *)AlignedFilePath;
     while (!IsDevicePathEnd(&FilePathNode->Header)) {
 
         //
@@ -262,6 +276,10 @@ Returns:
 
 Done:
 
+    if (AlignedFilePath) {
+        FreePool (AlignedFilePath);
+    }
+
     //
     // If the file was not accessed, clean up
     //

Reply via email to