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

[IIS] Manually parse exe bitness #61894

merged 3 commits into from
May 20, 2025

Conversation

BrennanConroy
Copy link
Member

@BrennanConroy BrennanConroy commented May 12, 2025

GetBinaryTypeW seems to load the exe into executable space which might trigger custom windows policies. Switch to parsing the PE header to find the bitness.

Tested running x64 app on ARM, ARM x64 emulation mode, and x64 machine.
Tested running x86 app on ARM and x64 machine.
Tested with invalid dotnet.exe file and empty dotnet.exe file.

@BrennanConroy BrennanConroy added feature-iis Includes: IIS, ANCM area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions labels May 12, 2025
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.

@BrennanConroy BrennanConroy requested a review from Copilot May 13, 2025 21:20
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR switches from using GetBinaryTypeW to manually parsing the PE header, avoiding side effects from loading the executable.

  • Introduces a new static function IsX64 to determine executable bitness via PE header parsing.
  • Replaces the GetBinaryTypeW call in InvokeWhereToFindDotnet with a call to IsX64.
  • Implements detailed PE header checks and logging in HostFxrResolver.cpp.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxrResolver.h Declares the new IsX64 method.
src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxrResolver.cpp Replaces GetBinaryTypeW with IsX64 and implements PE header parsing for bitness detection.


// 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.

}

// 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?

@BrennanConroy BrennanConroy marked this pull request as ready for review May 14, 2025 04:19
@BrennanConroy BrennanConroy merged commit fc3994f into main May 20, 2025
24 of 27 checks passed
@BrennanConroy BrennanConroy deleted the brecon/iisbitness branch May 20, 2025 03:40
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview5 milestone May 20, 2025
@BrennanConroy
Copy link
Member Author

/backport to release/8.0

Copy link
Contributor

Started backporting to release/8.0: https://github.com/dotnet/aspnetcore/actions/runs/15143703516

@BrennanConroy
Copy link
Member Author

/backport to release/9.0

Copy link
Contributor

Started backporting to release/9.0: https://github.com/dotnet/aspnetcore/actions/runs/15143709929

Copy link
Contributor

@BrennanConroy backporting to "release/8.0" failed, the patch most likely resulted in conflicts:

$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: [IIS] Manually parse exe bitness
Using index info to reconstruct a base tree...
M	src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxrResolver.cpp
Falling back to patching base and 3-way merge...
Auto-merging src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxrResolver.cpp
CONFLICT (content): Merge conflict in src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxrResolver.cpp
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 [IIS] Manually parse exe bitness
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

Copy link
Contributor

@BrennanConroy backporting to "release/9.0" failed, the patch most likely resulted in conflicts:

$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: [IIS] Manually parse exe bitness
Using index info to reconstruct a base tree...
M	src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxrResolver.cpp
Falling back to patching base and 3-way merge...
Auto-merging src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxrResolver.cpp
CONFLICT (content): Merge conflict in src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxrResolver.cpp
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 [IIS] Manually parse exe bitness
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions feature-iis Includes: IIS, ANCM
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants