Skip to content

[IIS] Manually parse exe bitness #61894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 59 additions & 11 deletions src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxrResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ HostFxrResolver::TryGetHostFxrPath(
size_t size = MAX_PATH * 2;
hostfxrPath.resize(size);

get_hostfxr_parameters params;
get_hostfxr_parameters params{};
params.size = sizeof(get_hostfxr_parameters);
params.assembly_path = applicationPath.c_str();
params.dotnet_root = dotnetRoot.c_str();
Expand Down Expand Up @@ -393,7 +393,7 @@ HostFxrResolver::GetAbsolutePathToDotnetFromHostfxr(const fs::path& hostfxrPath)
// Tries to call where.exe to find the location of dotnet.exe.
// Will check that the bitness of dotnet matches the current
// worker process bitness.
// Returns true if a valid dotnet was found, else false.R
// Returns true if a valid dotnet was found, else false.
//
std::optional<fs::path>
HostFxrResolver::InvokeWhereToFindDotnet()
Expand All @@ -416,7 +416,6 @@ HostFxrResolver::InvokeWhereToFindDotnet()
STRU struDotnetSubstring;
STRU struDotnetLocationsString;
DWORD dwNumBytesRead = 0;
DWORD dwBinaryType = 0;
INT index = 0;
INT prevIndex = 0;
std::optional<fs::path> result;
Expand Down Expand Up @@ -521,14 +520,7 @@ HostFxrResolver::InvokeWhereToFindDotnet()

LOG_INFOF(L"Processing entry '%ls'", struDotnetSubstring.QueryStr());

if (LOG_LAST_ERROR_IF(!GetBinaryTypeW(struDotnetSubstring.QueryStr(), &dwBinaryType)))
{
continue;
}

LOG_INFOF(L"Binary type %d", dwBinaryType);

if (fIsCurrentProcess64Bit == (dwBinaryType == SCS_64BIT_BINARY))
if (fIsCurrentProcess64Bit == IsX64(struDotnetSubstring.QueryStr()))
{
// The bitness of dotnet matched with the current worker process bitness.
return std::make_optional(struDotnetSubstring.QueryStr());
Expand All @@ -539,6 +531,62 @@ HostFxrResolver::InvokeWhereToFindDotnet()
return result;
}

BOOL HostFxrResolver::IsX64(const WCHAR* dotnetPath)
{
// Errors while reading from the file shouldn't throw unless
// file.exception(bits) is set
std::ifstream file(dotnetPath, std::ios::binary);
if (!file.is_open())
{
LOG_TRACEF(L"Failed to open file %ls", dotnetPath);
return false;
}

// Read the DOS header
IMAGE_DOS_HEADER dosHeader{};
file.read(reinterpret_cast<char*>(&dosHeader), sizeof(dosHeader));
Copy link
Preview

Copilot AI May 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider verifying that the file.read operation successfully read the expected number of bytes for the DOS header to handle cases of incomplete or corrupted files.

Suggested change
file.read(reinterpret_cast<char*>(&dosHeader), sizeof(dosHeader));
if (!file.read(reinterpret_cast<char*>(&dosHeader), sizeof(dosHeader)))
{
LOG_INFOF(L"Failed to read DOS header from %ls", dotnetPath);
return false;
}

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem too interesting, worst case there is nothing left to read and dosHeader.e_magic will be zero and not match IMAGE_DOS_SIGNATURE.

if (dosHeader.e_magic != IMAGE_DOS_SIGNATURE) // 'MZ'
{
LOG_TRACEF(L"%ls is not a valid executable file (missing MZ header).", dotnetPath);
return false;
}

// Seek to the PE header
file.seekg(dosHeader.e_lfanew, std::ios::beg);
Copy link
Preview

Copilot AI May 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the result of file.seekg to ensure that the PE header offset is valid before proceeding with reading the PE header.

Suggested change
file.seekg(dosHeader.e_lfanew, std::ios::beg);
file.seekg(dosHeader.e_lfanew, std::ios::beg);
if (file.fail())
{
LOG_INFOF(L"Failed to seek to PE header in file %ls. Invalid e_lfanew offset: %ld", dotnetPath, dosHeader.e_lfanew);
return false;
}

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, not interesting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming something interesting does happen here, what does the failure mode look like? Does something higher up handle the exception gracefully?


// Read the PE header
DWORD peSignature{};
file.read(reinterpret_cast<char*>(&peSignature), sizeof(peSignature));
if (peSignature != IMAGE_NT_SIGNATURE) // 'PE\0\0'
{
LOG_TRACEF(L"%ls is not a valid PE file (missing PE header).", dotnetPath);
return false;
}

// Read the file header
IMAGE_FILE_HEADER fileHeader{};
file.read(reinterpret_cast<char*>(&fileHeader), sizeof(fileHeader));

// Read the optional header magic field
WORD magic{};
file.read(reinterpret_cast<char*>(&magic), sizeof(magic));

// Determine the architecture based on the magic value
if (magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
{
LOG_INFOF(L"%ls is 32-bit", dotnetPath);
return false;
}
else if (magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
{
LOG_INFOF(L"%ls is 64-bit", dotnetPath);
return true;
}

LOG_INFOF(L"%ls is unknown architecture %i", dotnetPath, fileHeader.Machine);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? include the magic

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic only really has 3 values. So by default this is the 3rd case so won't have an interesting value to log.

return false;
}

std::optional<fs::path>
HostFxrResolver::GetAbsolutePathToDotnetFromProgramFiles()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class HostFxrResolver
const std::filesystem::path & requestedPath
);

static BOOL IsX64(const WCHAR* dotnetPath);

struct LocalFreeDeleter
{
void operator ()(_In_ LPWSTR* ptr) const
Expand Down
2 changes: 1 addition & 1 deletion src/Servers/IIS/build/Build.Lib.Settings
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemDefinitionGroup>
<ClCompile>
<SDLCheck>false</SDLCheck>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
Expand Down
Loading