summaryrefslogtreecommitdiff
path: root/packaging/0032-Fix-handling-of-incorrect-assemblies-on-Unix-16747.patch
blob: d18df3e22c3228c92e66a8c925931b0b27283b51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
From d47f1d334d67db1b28b9d55e7b6eccf71403ab0e Mon Sep 17 00:00:00 2001
From: Ruben Ayrapetyan <ruben-ayrapetyan@users.noreply.github.com>
Date: Tue, 6 Mar 2018 06:37:43 +0000
Subject: [PATCH 32/32] 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
---
 src/debug/daccess/nidump.cpp                       |  2 +-
 src/inc/pedecoder.h                                |  2 +-
 src/inc/pedecoder.inl                              |  2 +-
 src/utilcode/pedecoder.cpp                         |  3 +-
 src/vm/peimage.cpp                                 |  7 ++--
 tests/src/Loader/regressions/GitHub_15544/main.cs  | 37 ++++++++++++++++++++++
 .../Loader/regressions/GitHub_15544/main.csproj    | 31 ++++++++++++++++++
 7 files changed, 77 insertions(+), 7 deletions(-)
 create mode 100644 tests/src/Loader/regressions/GitHub_15544/main.cs
 create mode 100644 tests/src/Loader/regressions/GitHub_15544/main.csproj

diff --git a/src/debug/daccess/nidump.cpp b/src/debug/daccess/nidump.cpp
index ef4725f..18bef97 100644
--- a/src/debug/daccess/nidump.cpp
+++ b/src/debug/daccess/nidump.cpp
@@ -720,7 +720,7 @@ NativeImageDumper::DumpNativeImage()
 
     for (COUNT_T i = 0; i < m_decoder.GetNumberOfSections(); i++)
     {
-        PTR_IMAGE_SECTION_HEADER section = dptr_add(m_decoder.FindFirstSection(), i);
+        PTR_IMAGE_SECTION_HEADER section = m_decoder.FindFirstSection() + i;
         m_display->Section(reinterpret_cast<char *>(section->Name),
                            section->VirtualAddress,
                            section->SizeOfRawData);
diff --git a/src/inc/pedecoder.h b/src/inc/pedecoder.h
index 01375e6..8163fff 100644
--- a/src/inc/pedecoder.h
+++ b/src/inc/pedecoder.h
@@ -182,7 +182,7 @@ class PEDecoder
     UINT32 GetWin32VersionValue() const;
     COUNT_T GetNumberOfRvaAndSizes() const;
     COUNT_T GetNumberOfSections() const;
-    IMAGE_SECTION_HEADER *FindFirstSection() const;
+    PTR_IMAGE_SECTION_HEADER FindFirstSection() const;
     IMAGE_SECTION_HEADER *FindSection(LPCSTR sectionName) const;
 
     DWORD GetImageIdentity() const;
diff --git a/src/inc/pedecoder.inl b/src/inc/pedecoder.inl
index b75c495..4199a5b 100644
--- a/src/inc/pedecoder.inl
+++ b/src/inc/pedecoder.inl
@@ -1178,7 +1178,7 @@ inline DWORD PEDecoder::GetImageIdentity() const
 }
 
 
-inline IMAGE_SECTION_HEADER *PEDecoder::FindFirstSection() const
+inline PTR_IMAGE_SECTION_HEADER PEDecoder::FindFirstSection() const
 {
     CONTRACT(IMAGE_SECTION_HEADER *)
     {
diff --git a/src/utilcode/pedecoder.cpp b/src/utilcode/pedecoder.cpp
index babe374..e0f441c 100644
--- a/src/utilcode/pedecoder.cpp
+++ b/src/utilcode/pedecoder.cpp
@@ -445,6 +445,7 @@ BOOL PEDecoder::HasWriteableSections() const
     CONTRACT_CHECK
     {
         INSTANCE_CHECK;
+        PRECONDITION(CheckNTHeaders());
         PRECONDITION(CheckFormat());
         NOTHROW;
         GC_NOTRIGGER;
@@ -453,7 +454,7 @@ BOOL PEDecoder::HasWriteableSections() const
     }
     CONTRACT_CHECK_END;
 
-    PTR_IMAGE_SECTION_HEADER pSection = FindFirstSection(FindNTHeaders());
+    PTR_IMAGE_SECTION_HEADER pSection = FindFirstSection();
     _ASSERTE(pSection != NULL);
 
     PTR_IMAGE_SECTION_HEADER pSectionEnd = pSection + VAL16(FindNTHeaders()->FileHeader.NumberOfSections);
diff --git a/src/vm/peimage.cpp b/src/vm/peimage.cpp
index bd5ad7f..95f32e3 100644
--- a/src/vm/peimage.cpp
+++ b/src/vm/peimage.cpp
@@ -1029,7 +1029,9 @@ PTR_PEImageLayout PEImage::CreateLayoutFlat(BOOL bPermitWriteableSections)
 
     PTR_PEImageLayout pFlatLayout = PEImageLayout::LoadFlat(GetFileHandle(),this);
 
-    if (!bPermitWriteableSections && pFlatLayout->HasWriteableSections())
+    if (!bPermitWriteableSections
+        && pFlatLayout->CheckNTHeaders()
+        && pFlatLayout->HasWriteableSections())
     {
         pFlatLayout->Release();
 
@@ -1108,8 +1110,7 @@ void PEImage::Load()
 
 #ifdef PLATFORM_UNIX
     if (m_pLayouts[IMAGE_FLAT] != NULL
-        && m_pLayouts[IMAGE_FLAT]->CheckFormat()
-        && m_pLayouts[IMAGE_FLAT]->IsILOnly()
+        && m_pLayouts[IMAGE_FLAT]->CheckILOnlyFormat()
         && !m_pLayouts[IMAGE_FLAT]->HasWriteableSections())
     {
         // IL-only images with writeable sections are mapped in general way,
diff --git a/tests/src/Loader/regressions/GitHub_15544/main.cs b/tests/src/Loader/regressions/GitHub_15544/main.cs
new file mode 100644
index 0000000..25e7d79
--- /dev/null
+++ b/tests/src/Loader/regressions/GitHub_15544/main.cs
@@ -0,0 +1,37 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+//
+
+using System;
+using System.IO;
+using System.Reflection;
+
+public class CMain{
+    public static int Main(String[] args) {
+        string tempFileName = Path.GetTempFileName();
+
+        bool isThrown = false;
+
+        try
+        {
+            AssemblyName.GetAssemblyName(tempFileName);
+        }
+        catch (BadImageFormatException)
+        {
+            isThrown = true;
+        }
+
+        File.Delete(tempFileName);
+
+        if (isThrown) {
+            Console.WriteLine("PASS");
+
+            return 100;
+        } else {
+            Console.WriteLine("FAIL");
+
+            return 101;
+        }
+    }
+}
diff --git a/tests/src/Loader/regressions/GitHub_15544/main.csproj b/tests/src/Loader/regressions/GitHub_15544/main.csproj
new file mode 100644
index 0000000..e46a44c
--- /dev/null
+++ b/tests/src/Loader/regressions/GitHub_15544/main.csproj
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{AC75380E-F196-4F32-9BCF-F0589AF864E6}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+  </PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="main.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>
-- 
2.7.4