-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Perform additional checks in PEHeaders.
#113048
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 3 commits
847b009
130cde8
4dd238f
10f1670
f3c8a97
2d1c06f
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 |
|---|---|---|
|
|
@@ -93,13 +93,26 @@ public PEHeaders(Stream peStream, int size, bool isLoadedImage) | |
| _coffHeaderStartOffset = reader.Offset; | ||
| _coffHeader = new CoffHeader(ref reader); | ||
|
|
||
| if (!isCoffOnly) | ||
| if (isCoffOnly) | ||
| { | ||
| // These characteristics bits are documented in https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#coff-file-header-object-and-image | ||
| // that should be set only on image files. | ||
| const Characteristics ImageOnlyCharacteristics = Characteristics.RelocsStripped | Characteristics.ExecutableImage; | ||
| // Because COFF files do not have a magic number, perform some additional checks to catch outright invalid files. | ||
| if (_coffHeader.SizeOfOptionalHeader != 0 | ||
| || _coffHeader.PointerToSymbolTable >= actualSize | ||
| || (_coffHeader.Characteristics & ImageOnlyCharacteristics) != 0) | ||
| { | ||
| throw new BadImageFormatException(SR.UnknownFileFormat); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| _peHeaderStartOffset = reader.Offset; | ||
| _peHeader = new PEHeader(ref reader); | ||
| } | ||
|
|
||
| _sectionHeaders = ReadSectionHeaders(ref reader); | ||
| _sectionHeaders = ReadSectionHeaders(ref reader, actualSize, isCoffOnly); | ||
|
|
||
| if (!isCoffOnly) | ||
| { | ||
|
|
@@ -289,10 +302,10 @@ private static void SkipDosHeader(ref PEBinaryReader reader, out bool isCOFFOnly | |
| } | ||
| } | ||
|
|
||
| private ImmutableArray<SectionHeader> ReadSectionHeaders(ref PEBinaryReader reader) | ||
| private ImmutableArray<SectionHeader> ReadSectionHeaders(ref PEBinaryReader reader, int size, bool isCoffOnly) | ||
| { | ||
| int numberOfSections = _coffHeader.NumberOfSections; | ||
| if (numberOfSections < 0) | ||
| if (numberOfSections < 0 || numberOfSections * SectionHeader.Size > size - reader.Offset) | ||
| { | ||
| throw new BadImageFormatException(SR.InvalidNumberOfSections); | ||
| } | ||
|
|
@@ -301,7 +314,12 @@ private ImmutableArray<SectionHeader> ReadSectionHeaders(ref PEBinaryReader read | |
|
|
||
| for (int i = 0; i < numberOfSections; i++) | ||
| { | ||
| builder.Add(new SectionHeader(ref reader)); | ||
| var sectionHeader = new SectionHeader(ref reader); | ||
| if (isCoffOnly && sectionHeader.VirtualSize != 0) | ||
|
||
| { | ||
| throw new BadImageFormatException(SR.UnknownFileFormat); | ||
| } | ||
| builder.Add(sectionHeader); | ||
| } | ||
|
|
||
| return builder.MoveToImmutable(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,13 @@ public void Ctor_Streams() | |
| Assert.Equal(0, s.Position); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Ctor_InvalidFile() | ||
|
||
| { | ||
| using var stream = new MemoryStream(Misc.KeyPair); | ||
| Assert.Throws<BadImageFormatException>(() => new PEHeaders(stream)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void FromEmptyStream() | ||
| { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.