Correct .modinfo section parsing
All checks were successful
Builds / ExectOS (amd64, debug) (push) Successful in 34s
Builds / ExectOS (amd64, release) (push) Successful in 32s
Builds / ExectOS (i686, debug) (push) Successful in 44s
Builds / ExectOS (i686, release) (push) Successful in 44s

This commit is contained in:
Aiken Harris 2025-08-25 12:07:49 +02:00
parent 0b40a3fb10
commit e99e563aff
Signed by: harraiken
GPG Key ID: C40F06CB7493C1F5

View File

@ -749,100 +749,96 @@ BlpGetModuleInfoStrings(IN PWCHAR SectionData,
EFI_STATUS Status; EFI_STATUS Status;
PWCHAR *Array; PWCHAR *Array;
PWCHAR String; PWCHAR String;
ULONG DataSize;
/* Check input parameters */ /* Check input parameters */
InfoStrings = SectionData; InfoStrings = SectionData;
if(!InfoStrings || !SectionSize) if(!InfoStrings || !SectionSize)
{ {
/* Invalid input parameters */ /* Invalid input parameters */
*ModInfo = NULL;
*InfoCount = 0;
return STATUS_EFI_INVALID_PARAMETER; return STATUS_EFI_INVALID_PARAMETER;
} }
/* Skip zero padding */ /* Calculate the size of the data based on the size of the section */
while(InfoStrings[0] == L'\0' && SectionSize > 1) DataSize = SectionSize / sizeof(WCHAR);
/* Skip zero padding at the beginning */
while(DataSize > 0 && *InfoStrings == L'\0')
{ {
/* Get next character and decrement section size */
InfoStrings++; InfoStrings++;
SectionSize--; DataSize--;
} }
/* Make sure there is at least one string available */ /* Make sure there is at least one string available */
if(SectionSize <= 1) if(DataSize < 1)
{ {
/* No strings found */ /* No strings found */
*ModInfo = NULL;
*InfoCount = 0;
return STATUS_EFI_END_OF_FILE; return STATUS_EFI_END_OF_FILE;
} }
/* Count number of strings */ /* Count number of strings */
Index = 0; Index = 0;
Count = 0; Count = 0;
while(Index < SectionSize) while(Index < DataSize)
{ {
/* Get to the next string */ /* Found start of a new string */
if(InfoStrings[Index] != L'\0') Count++;
{
/* Get next character */
Index++;
continue;
}
/* Skip zero padding */ /* Go to the end of the string */
while(InfoStrings[Index] == L'\0' && Index < SectionSize) while(Index < DataSize && InfoStrings[Index] != L'\0')
{
Index++;
}
/* Skip all null terminators */
while(Index < DataSize && InfoStrings[Index] == L'\0')
{ {
/* Get next character */
Index++; Index++;
} }
/* New string found, increment counter */
Count++;
} }
/* Make sure there is no missing string */ /* Allocate memory for the pointer array and the string data */
if(InfoStrings[Index - 1] != L'\0') Status = BlAllocateMemoryPool(sizeof(PWCHAR) * (Count + 1) + (DataSize + 1) * sizeof(WCHAR), (PVOID *)&Array);
{
/* One more string available */
Count++;
}
/* Allocate memory for array of strings */
Status = BlAllocateMemoryPool(SectionSize + 1 + sizeof(PWCHAR) * (Count + 1), (PVOID *)&Array);
if(Status != STATUS_EFI_SUCCESS) if(Status != STATUS_EFI_SUCCESS)
{ {
/* Failed to allocate memory */ /* Failed to allocate memory */
return STATUS_EFI_OUT_OF_RESOURCES; return STATUS_EFI_OUT_OF_RESOURCES;
} }
/* Allocate memory and copy strings read from '.modinfo' section */ /* The string buffer is located right after the pointer array */
BlAllocateMemoryPool(SectionSize, (PVOID*)&String); String = (PWCHAR)(Array + Count + 1);
RtlCopyMemory(String, InfoStrings, SectionSize);
/* Make sure last string is NULL-terminated */ /* Copy the raw string data */
RtlCopyMemory(String, InfoStrings, DataSize * sizeof(WCHAR));
/* Ensure the entire buffer is null-terminated for safety */
String[DataSize] = L'\0';
/* Set the last element of the pointer array to NULL */
Array[Count] = NULL; Array[Count] = NULL;
Array[0] = String;
/* Parse strings into array */ /* Populate the array with pointers to the strings within the buffer */
Index = 0; Index = 0;
ArrayIndex = 1; ArrayIndex = 0;
while(Index < SectionSize && ArrayIndex < Count) while(Index < DataSize && ArrayIndex < Count)
{ {
/* Get to the next string */ /* Set pointer to the beginning of the string */
if(String[Index] != L'\0') Array[ArrayIndex++] = &String[Index];
{
/* Get next character */
Index++;
continue;
}
/* Skip zero padding */ /* Find the end of the current string */
while(InfoStrings[Index] == L'\0' && Index < SectionSize) while(Index < DataSize && String[Index] != L'\0')
{ {
/* Get next character */
Index++; Index++;
} }
/* Push string into array */ /* Skip all null terminators to find the beginning of the next string */
Array[ArrayIndex] = &String[Index]; while(Index < DataSize && String[Index] == L'\0')
ArrayIndex++; {
Index++;
}
} }
/* Return array of strings and its size */ /* Return array of strings and its size */