dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.91k stars 4.64k forks source link

PEReader does not throw BadImageFormatException for some invalid PE files #48419

Open FiniteReality opened 3 years ago

FiniteReality commented 3 years ago

Description

Creating a PEReader for an invalid file type, such as an ELF can sometimes lead to a situation where the PEReader thinks the file is valid and it will return garbage data, causing errors to occur downstream.

Attached is a zipfile containing an ELF which reproduces the issue, trimmed using dd to reduce its filesize while still reproducing the issue.

Here's the code I'm running:

using System.Reflection.PortableExecutable;

var reader = new PEReader(File.OpenRead("path/to/invalid.so"));

Console.WriteLine(reader.HasMetadata);

Running the same code on a different ELF file throws a BadImageFormatException, as expected.

Configuration

.NET SDK/Runtime info ``` $ dotnet --info .NET SDK (reflecting any global.json): Version: 5.0.102 Commit: 71365b4d42 Runtime Environment: OS Name: debian OS Version: OS Platform: Linux RID: debian-x64 Base Path: /usr/share/dotnet/sdk/5.0.102/ Host (useful for support): Version: 5.0.2 Commit: cb5f173b96 .NET SDKs installed: 3.1.405 [/usr/share/dotnet/sdk] 5.0.102 [/usr/share/dotnet/sdk] .NET runtimes installed: Microsoft.AspNetCore.App 3.1.11 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.2 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 3.1.11 [/usr/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App] ```
dotnet-issue-labeler[bot] commented 3 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ericstj commented 1 year ago

This is a good suggestion. Maybe we could see how the runtime or linker (or dumpbin) does it and match that algorithm here. Relevant info: https://learn.microsoft.com/en-us/windows/win32/debug/pe-format