Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 383736b

Browse files
ruben-ayrapetyanjkotas
authored andcommitted
Fix handling of incorrect assemblies on Unix (#16747)
* Return DPTR from PEDecoder::FindFirstSection() Change type of the function's return value to PTR_IMAGE_SECTION_HEADER instead of (IMAGE_SECTION_HEADER *) * Fix handling of incorrect assemblies on Unix This fixes the regression that was introduced by #10772 and is caused by a missing check for validity of loaded assembly file. Related issue: #15544
1 parent 5b553b8 commit 383736b

File tree

7 files changed

+77
-7
lines changed

7 files changed

+77
-7
lines changed

src/debug/daccess/nidump.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ NativeImageDumper::DumpNativeImage()
720720

721721
for (COUNT_T i = 0; i < m_decoder.GetNumberOfSections(); i++)
722722
{
723-
PTR_IMAGE_SECTION_HEADER section = dptr_add(m_decoder.FindFirstSection(), i);
723+
PTR_IMAGE_SECTION_HEADER section = m_decoder.FindFirstSection() + i;
724724
m_display->Section(reinterpret_cast<char *>(section->Name),
725725
section->VirtualAddress,
726726
section->SizeOfRawData);

src/inc/pedecoder.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class PEDecoder
182182
UINT32 GetWin32VersionValue() const;
183183
COUNT_T GetNumberOfRvaAndSizes() const;
184184
COUNT_T GetNumberOfSections() const;
185-
IMAGE_SECTION_HEADER *FindFirstSection() const;
185+
PTR_IMAGE_SECTION_HEADER FindFirstSection() const;
186186
IMAGE_SECTION_HEADER *FindSection(LPCSTR sectionName) const;
187187

188188
DWORD GetImageIdentity() const;

src/inc/pedecoder.inl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ inline DWORD PEDecoder::GetImageIdentity() const
11781178
}
11791179

11801180

1181-
inline IMAGE_SECTION_HEADER *PEDecoder::FindFirstSection() const
1181+
inline PTR_IMAGE_SECTION_HEADER PEDecoder::FindFirstSection() const
11821182
{
11831183
CONTRACT(IMAGE_SECTION_HEADER *)
11841184
{

src/utilcode/pedecoder.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ BOOL PEDecoder::HasWriteableSections() const
445445
CONTRACT_CHECK
446446
{
447447
INSTANCE_CHECK;
448+
PRECONDITION(CheckNTHeaders());
448449
PRECONDITION(CheckFormat());
449450
NOTHROW;
450451
GC_NOTRIGGER;
@@ -453,7 +454,7 @@ BOOL PEDecoder::HasWriteableSections() const
453454
}
454455
CONTRACT_CHECK_END;
455456

456-
PTR_IMAGE_SECTION_HEADER pSection = FindFirstSection(FindNTHeaders());
457+
PTR_IMAGE_SECTION_HEADER pSection = FindFirstSection();
457458
_ASSERTE(pSection != NULL);
458459

459460
PTR_IMAGE_SECTION_HEADER pSectionEnd = pSection + VAL16(FindNTHeaders()->FileHeader.NumberOfSections);

src/vm/peimage.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,9 @@ PTR_PEImageLayout PEImage::CreateLayoutFlat(BOOL bPermitWriteableSections)
10291029

10301030
PTR_PEImageLayout pFlatLayout = PEImageLayout::LoadFlat(GetFileHandle(),this);
10311031

1032-
if (!bPermitWriteableSections && pFlatLayout->HasWriteableSections())
1032+
if (!bPermitWriteableSections
1033+
&& pFlatLayout->CheckNTHeaders()
1034+
&& pFlatLayout->HasWriteableSections())
10331035
{
10341036
pFlatLayout->Release();
10351037

@@ -1114,8 +1116,7 @@ void PEImage::Load()
11141116

11151117
#ifdef PLATFORM_UNIX
11161118
if (m_pLayouts[IMAGE_FLAT] != NULL
1117-
&& m_pLayouts[IMAGE_FLAT]->CheckFormat()
1118-
&& m_pLayouts[IMAGE_FLAT]->IsILOnly()
1119+
&& m_pLayouts[IMAGE_FLAT]->CheckILOnlyFormat()
11191120
&& !m_pLayouts[IMAGE_FLAT]->HasWriteableSections())
11201121
{
11211122
// IL-only images with writeable sections are mapped in general way,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
//
5+
6+
using System;
7+
using System.IO;
8+
using System.Reflection;
9+
10+
public class CMain{
11+
public static int Main(String[] args) {
12+
string tempFileName = Path.GetTempFileName();
13+
14+
bool isThrown = false;
15+
16+
try
17+
{
18+
AssemblyName.GetAssemblyName(tempFileName);
19+
}
20+
catch (BadImageFormatException)
21+
{
22+
isThrown = true;
23+
}
24+
25+
File.Delete(tempFileName);
26+
27+
if (isThrown) {
28+
Console.WriteLine("PASS");
29+
30+
return 100;
31+
} else {
32+
Console.WriteLine("FAIL");
33+
34+
return 101;
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{AC75380E-F196-4F32-9BCF-F0589AF864E6}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
12+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
13+
</PropertyGroup>
14+
<!-- Default configurations to help VS understand the configurations -->
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
18+
</PropertyGroup>
19+
<ItemGroup>
20+
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
21+
<Visible>False</Visible>
22+
</CodeAnalysisDependentAssemblyPaths>
23+
</ItemGroup>
24+
<ItemGroup>
25+
<Compile Include="main.cs" />
26+
</ItemGroup>
27+
<ItemGroup>
28+
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
29+
</ItemGroup>
30+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
31+
</Project>

0 commit comments

Comments
 (0)