-
Notifications
You must be signed in to change notification settings - Fork 10.3k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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(); | ||||||||||||||||
|
@@ -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() | ||||||||||||||||
|
@@ -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; | ||||||||||||||||
|
@@ -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()); | ||||||||||||||||
|
@@ -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)); | ||||||||||||||||
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); | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, not interesting. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? include the magic There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||||||||||||||||
{ | ||||||||||||||||
|
There was a problem hiding this comment.
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.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
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 matchIMAGE_DOS_SIGNATURE
.