diff options
Diffstat (limited to '')
| -rw-r--r-- | src/burn/test/BurnUnitTest/ApprovedExeTest.cpp | 312 |
1 files changed, 312 insertions, 0 deletions
diff --git a/src/burn/test/BurnUnitTest/ApprovedExeTest.cpp b/src/burn/test/BurnUnitTest/ApprovedExeTest.cpp new file mode 100644 index 00000000..da51f1f8 --- /dev/null +++ b/src/burn/test/BurnUnitTest/ApprovedExeTest.cpp | |||
| @@ -0,0 +1,312 @@ | |||
| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
| 2 | |||
| 3 | #include "precomp.h" | ||
| 4 | |||
| 5 | namespace Microsoft | ||
| 6 | { | ||
| 7 | namespace Tools | ||
| 8 | { | ||
| 9 | namespace WindowsInstallerXml | ||
| 10 | { | ||
| 11 | namespace Test | ||
| 12 | { | ||
| 13 | namespace Bootstrapper | ||
| 14 | { | ||
| 15 | using namespace System; | ||
| 16 | using namespace System::IO; | ||
| 17 | using namespace Xunit; | ||
| 18 | |||
| 19 | public ref class ApprovedExeTest : BurnUnitTest | ||
| 20 | { | ||
| 21 | public: | ||
| 22 | ApprovedExeTest(BurnTestFixture^ fixture) : BurnUnitTest(fixture) | ||
| 23 | { | ||
| 24 | } | ||
| 25 | |||
| 26 | [Fact] | ||
| 27 | void ApprovedExesVerifyPFilesTest() | ||
| 28 | { | ||
| 29 | HRESULT hr = S_OK; | ||
| 30 | BURN_CACHE cache = { }; | ||
| 31 | BURN_ENGINE_COMMAND internalCommand = { }; | ||
| 32 | BURN_VARIABLES variables = { }; | ||
| 33 | LPWSTR scz = NULL; | ||
| 34 | LPWSTR scz2 = NULL; | ||
| 35 | |||
| 36 | try | ||
| 37 | { | ||
| 38 | hr = VariableInitialize(&variables); | ||
| 39 | NativeAssert::Succeeded(hr, L"Failed to initialize variables."); | ||
| 40 | |||
| 41 | hr = CacheInitialize(&cache, &internalCommand); | ||
| 42 | NativeAssert::Succeeded(hr, "Failed to initialize cache."); | ||
| 43 | |||
| 44 | hr = VariableGetString(&variables, L"ProgramFilesFolder", &scz); | ||
| 45 | NativeAssert::Succeeded(hr, "Failed to get variable ProgramFilesFolder."); | ||
| 46 | |||
| 47 | hr = PathConcat(scz, L"a.exe", &scz2); | ||
| 48 | NativeAssert::Succeeded(hr, "Failed to combine paths"); | ||
| 49 | |||
| 50 | hr = ApprovedExesVerifySecureLocation(&cache, &variables, scz2, 0, NULL); | ||
| 51 | NativeAssert::Succeeded(hr, "Failed to test secure location under ProgramFilesFolder"); | ||
| 52 | Assert::True((hr == S_OK), "Path under ProgramFilesFolder was expected to be safe"); | ||
| 53 | } | ||
| 54 | finally | ||
| 55 | { | ||
| 56 | ReleaseStr(internalCommand.sczEngineWorkingDirectory); | ||
| 57 | ReleaseStr(scz); | ||
| 58 | ReleaseStr(scz2); | ||
| 59 | |||
| 60 | CacheUninitialize(&cache); | ||
| 61 | VariablesUninitialize(&variables); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | [Fact] | ||
| 66 | void ApprovedExesVerifyPFilesWithRelativeTest() | ||
| 67 | { | ||
| 68 | HRESULT hr = S_OK; | ||
| 69 | BURN_CACHE cache = { }; | ||
| 70 | BURN_ENGINE_COMMAND internalCommand = { }; | ||
| 71 | BURN_VARIABLES variables = { }; | ||
| 72 | LPWSTR scz = NULL; | ||
| 73 | LPWSTR scz2 = NULL; | ||
| 74 | |||
| 75 | try | ||
| 76 | { | ||
| 77 | hr = VariableInitialize(&variables); | ||
| 78 | NativeAssert::Succeeded(hr, L"Failed to initialize variables."); | ||
| 79 | |||
| 80 | hr = CacheInitialize(&cache, &internalCommand); | ||
| 81 | NativeAssert::Succeeded(hr, "Failed to initialize cache."); | ||
| 82 | cache.fPerMachineCacheRootVerified = TRUE; | ||
| 83 | cache.fOriginalPerMachineCacheRootVerified = TRUE; | ||
| 84 | |||
| 85 | hr = VariableGetString(&variables, L"ProgramFilesFolder", &scz); | ||
| 86 | NativeAssert::Succeeded(hr, "Failed to get variable ProgramFilesFolder."); | ||
| 87 | |||
| 88 | hr = PathConcat(scz, L"..\\a.exe", &scz2); | ||
| 89 | NativeAssert::Succeeded(hr, "Failed to combine paths"); | ||
| 90 | |||
| 91 | hr = ApprovedExesVerifySecureLocation(&cache, &variables, scz2, 0, NULL); | ||
| 92 | NativeAssert::Succeeded(hr, "Failed to test secure location under ProgramFilesFolder"); | ||
| 93 | Assert::True((hr == S_FALSE), "Path pretending to be under ProgramFilesFolder was expected to be unsafe"); | ||
| 94 | } | ||
| 95 | finally | ||
| 96 | { | ||
| 97 | ReleaseStr(internalCommand.sczEngineWorkingDirectory); | ||
| 98 | ReleaseStr(scz); | ||
| 99 | ReleaseStr(scz2); | ||
| 100 | |||
| 101 | CacheUninitialize(&cache); | ||
| 102 | VariablesUninitialize(&variables); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | [Fact] | ||
| 107 | void ApprovedExesVerifyPFiles64Test() | ||
| 108 | { | ||
| 109 | HRESULT hr = S_OK; | ||
| 110 | BURN_CACHE cache = { }; | ||
| 111 | BURN_ENGINE_COMMAND internalCommand = { }; | ||
| 112 | BURN_VARIABLES variables = { }; | ||
| 113 | LPWSTR scz = NULL; | ||
| 114 | LPWSTR scz2 = NULL; | ||
| 115 | |||
| 116 | try | ||
| 117 | { | ||
| 118 | hr = VariableInitialize(&variables); | ||
| 119 | NativeAssert::Succeeded(hr, L"Failed to initialize variables."); | ||
| 120 | |||
| 121 | hr = CacheInitialize(&cache, &internalCommand); | ||
| 122 | NativeAssert::Succeeded(hr, "Failed to initialize cache."); | ||
| 123 | |||
| 124 | hr = VariableGetString(&variables, L"ProgramFiles64Folder", &scz); | ||
| 125 | NativeAssert::Succeeded(hr, "Failed to get variable ProgramFiles64Folder."); | ||
| 126 | |||
| 127 | hr = PathConcat(scz, L"a.exe", &scz2); | ||
| 128 | NativeAssert::Succeeded(hr, "Failed to combine paths"); | ||
| 129 | |||
| 130 | hr = ApprovedExesVerifySecureLocation(&cache, &variables, scz2, 0, NULL); | ||
| 131 | NativeAssert::Succeeded(hr, "Failed to test secure location under ProgramFiles64Folder"); | ||
| 132 | Assert::True((hr == S_OK), "Path under ProgramFiles64Folder was expected to be safe"); | ||
| 133 | } | ||
| 134 | finally | ||
| 135 | { | ||
| 136 | ReleaseStr(internalCommand.sczEngineWorkingDirectory); | ||
| 137 | ReleaseStr(scz); | ||
| 138 | ReleaseStr(scz2); | ||
| 139 | |||
| 140 | CacheUninitialize(&cache); | ||
| 141 | VariablesUninitialize(&variables); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | [Fact] | ||
| 146 | void ApprovedExesVerifySys64FolderTest() | ||
| 147 | { | ||
| 148 | HRESULT hr = S_OK; | ||
| 149 | BURN_CACHE cache = { }; | ||
| 150 | BURN_ENGINE_COMMAND internalCommand = { }; | ||
| 151 | BURN_VARIABLES variables = { }; | ||
| 152 | LPWSTR scz = NULL; | ||
| 153 | LPWSTR scz2 = NULL; | ||
| 154 | |||
| 155 | try | ||
| 156 | { | ||
| 157 | hr = VariableInitialize(&variables); | ||
| 158 | NativeAssert::Succeeded(hr, L"Failed to initialize variables."); | ||
| 159 | |||
| 160 | hr = CacheInitialize(&cache, &internalCommand); | ||
| 161 | NativeAssert::Succeeded(hr, "Failed to initialize cache."); | ||
| 162 | cache.fPerMachineCacheRootVerified = TRUE; | ||
| 163 | cache.fOriginalPerMachineCacheRootVerified = TRUE; | ||
| 164 | |||
| 165 | hr = VariableGetString(&variables, L"System64Folder", &scz); | ||
| 166 | NativeAssert::Succeeded(hr, "Failed to get variable System64Folder."); | ||
| 167 | |||
| 168 | hr = PathConcat(scz, L"a.exe", &scz2); | ||
| 169 | NativeAssert::Succeeded(hr, "Failed to combine paths"); | ||
| 170 | |||
| 171 | hr = ApprovedExesVerifySecureLocation(&cache, &variables, scz2, 0, NULL); | ||
| 172 | NativeAssert::Succeeded(hr, "Failed to test secure location under System64Folder"); | ||
| 173 | Assert::True((hr == S_FALSE), "Path under System64Folder was expected to be unsafe"); | ||
| 174 | } | ||
| 175 | finally | ||
| 176 | { | ||
| 177 | ReleaseStr(internalCommand.sczEngineWorkingDirectory); | ||
| 178 | ReleaseStr(scz); | ||
| 179 | ReleaseStr(scz2); | ||
| 180 | |||
| 181 | CacheUninitialize(&cache); | ||
| 182 | VariablesUninitialize(&variables); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | [Fact] | ||
| 187 | void ApprovedExesVerifySys64Rundll32UnsafeTest() | ||
| 188 | { | ||
| 189 | HRESULT hr = S_OK; | ||
| 190 | BURN_CACHE cache = { }; | ||
| 191 | BURN_ENGINE_COMMAND internalCommand = { }; | ||
| 192 | BURN_VARIABLES variables = { }; | ||
| 193 | LPWSTR scz = NULL; | ||
| 194 | LPWSTR scz2 = NULL; | ||
| 195 | LPWSTR szArgs = NULL; | ||
| 196 | |||
| 197 | try | ||
| 198 | { | ||
| 199 | hr = VariableInitialize(&variables); | ||
| 200 | NativeAssert::Succeeded(hr, L"Failed to initialize variables."); | ||
| 201 | |||
| 202 | hr = CacheInitialize(&cache, &internalCommand); | ||
| 203 | NativeAssert::Succeeded(hr, "Failed to initialize cache."); | ||
| 204 | cache.fPerMachineCacheRootVerified = TRUE; | ||
| 205 | cache.fOriginalPerMachineCacheRootVerified = TRUE; | ||
| 206 | |||
| 207 | hr = VariableGetString(&variables, L"System64Folder", &scz); | ||
| 208 | NativeAssert::Succeeded(hr, "Failed to get variable System64Folder."); | ||
| 209 | |||
| 210 | hr = PathConcat(scz, L"rundll32.exe", &scz2); | ||
| 211 | NativeAssert::Succeeded(hr, "Failed to combine paths"); | ||
| 212 | |||
| 213 | hr = ApprovedExesVerifySecureLocation(&cache, &variables, scz2, 1, const_cast<LPCWSTR*>(&scz2)); | ||
| 214 | NativeAssert::Succeeded(hr, "Failed to test secure location under System64Folder"); | ||
| 215 | Assert::True((hr == S_FALSE), "Path under System64Folder was expected to be unsafe for rundll32 target"); | ||
| 216 | } | ||
| 217 | finally | ||
| 218 | { | ||
| 219 | ReleaseStr(internalCommand.sczEngineWorkingDirectory); | ||
| 220 | ReleaseStr(scz); | ||
| 221 | ReleaseStr(scz2); | ||
| 222 | ReleaseStr(szArgs); | ||
| 223 | |||
| 224 | CacheUninitialize(&cache); | ||
| 225 | VariablesUninitialize(&variables); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | [Fact] | ||
| 230 | void ApprovedExesVerifySys64Rundll32SafeTest() | ||
| 231 | { | ||
| 232 | HRESULT hr = S_OK; | ||
| 233 | BURN_CACHE cache = { }; | ||
| 234 | BURN_ENGINE_COMMAND internalCommand = { }; | ||
| 235 | BURN_VARIABLES variables = { }; | ||
| 236 | LPWSTR scz = NULL; | ||
| 237 | LPWSTR scz2 = NULL; | ||
| 238 | LPWSTR scz3 = NULL; | ||
| 239 | |||
| 240 | try | ||
| 241 | { | ||
| 242 | hr = VariableInitialize(&variables); | ||
| 243 | NativeAssert::Succeeded(hr, L"Failed to initialize variables."); | ||
| 244 | |||
| 245 | hr = CacheInitialize(&cache, &internalCommand); | ||
| 246 | NativeAssert::Succeeded(hr, "Failed to initialize cache."); | ||
| 247 | cache.fPerMachineCacheRootVerified = TRUE; | ||
| 248 | cache.fOriginalPerMachineCacheRootVerified = TRUE; | ||
| 249 | |||
| 250 | // System64Folder | ||
| 251 | hr = VariableGetString(&variables, L"System64Folder", &scz); | ||
| 252 | NativeAssert::Succeeded(hr, "Failed to get variable System64Folder."); | ||
| 253 | |||
| 254 | hr = PathConcat(scz, L"rundll32.exe", &scz2); | ||
| 255 | NativeAssert::Succeeded(hr, "Failed to combine paths"); | ||
| 256 | |||
| 257 | hr = VariableGetString(&variables, L"ProgramFiles64Folder", &scz); | ||
| 258 | NativeAssert::Succeeded(hr, "Failed to get variable ProgramFiles64Folder."); | ||
| 259 | |||
| 260 | hr = PathConcat(scz, L"a.dll", &scz3); | ||
| 261 | NativeAssert::Succeeded(hr, "Failed to combine paths"); | ||
| 262 | |||
| 263 | hr = ApprovedExesVerifySecureLocation(&cache, &variables, scz2, 1, const_cast<LPCWSTR*>(&scz3)); | ||
| 264 | NativeAssert::Succeeded(hr, "Failed to test secure location under ProgramFiles64Folder for System64Folder/rundll32 target"); | ||
| 265 | Assert::True((hr == S_OK), "Path under ProgramFiles64Folder was expected to be safe for System64Folder/rundll32 target"); | ||
| 266 | |||
| 267 | hr = PathConcat(scz, L"a.dll,somthing else", &scz3); | ||
| 268 | NativeAssert::Succeeded(hr, "Failed to combine paths"); | ||
| 269 | |||
| 270 | hr = ApprovedExesVerifySecureLocation(&cache, &variables, scz2, 1, const_cast<LPCWSTR*>(&scz3)); | ||
| 271 | NativeAssert::Succeeded(hr, "Failed to test secure location under ProgramFiles64Folder for rundll32 target"); | ||
| 272 | Assert::True((hr == S_OK), "Path under ProgramFiles64Folder was expected to be safe for System64Folder/rundll32 target"); | ||
| 273 | |||
| 274 | // SystemFolder | ||
| 275 | hr = VariableGetString(&variables, L"SystemFolder", &scz); | ||
| 276 | NativeAssert::Succeeded(hr, "Failed to get variable System64Folder."); | ||
| 277 | |||
| 278 | hr = PathConcat(scz, L"rundll32.exe", &scz2); | ||
| 279 | NativeAssert::Succeeded(hr, "Failed to combine paths"); | ||
| 280 | |||
| 281 | hr = ApprovedExesVerifySecureLocation(&cache, &variables, scz2, 1, const_cast<LPCWSTR*>(&scz3)); | ||
| 282 | NativeAssert::Succeeded(hr, "Failed to test secure location under ProgramFiles64Folder for SystemFolder/rundll32 target"); | ||
| 283 | Assert::True((hr == S_OK), "Path under ProgramFiles64Folder was expected to be safe for SystemFolder/rundll32 target"); | ||
| 284 | |||
| 285 | // Sysnative | ||
| 286 | hr = PathSystemWindowsSubdirectory(L"SysNative\\", &scz); | ||
| 287 | NativeAssert::Succeeded(hr, "Failed to get SysNative Folder."); | ||
| 288 | |||
| 289 | hr = PathConcat(scz, L"rundll32.exe", &scz2); | ||
| 290 | NativeAssert::Succeeded(hr, "Failed to combine paths"); | ||
| 291 | |||
| 292 | hr = ApprovedExesVerifySecureLocation(&cache, &variables, scz2, 1, const_cast<LPCWSTR*>(&scz3)); | ||
| 293 | NativeAssert::Succeeded(hr, "Failed to test secure location under ProgramFiles64Folder for Sysnative/rundll32 target"); | ||
| 294 | Assert::True((hr == S_OK), "Path under ProgramFiles64Folder was expected to be safe for Sysnative/rundll32 target"); | ||
| 295 | } | ||
| 296 | finally | ||
| 297 | { | ||
| 298 | ReleaseStr(internalCommand.sczEngineWorkingDirectory); | ||
| 299 | ReleaseStr(scz); | ||
| 300 | ReleaseStr(scz2); | ||
| 301 | ReleaseStr(scz3); | ||
| 302 | |||
| 303 | CacheUninitialize(&cache); | ||
| 304 | VariablesUninitialize(&variables); | ||
| 305 | } | ||
| 306 | } | ||
| 307 | }; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | } | ||
| 311 | } | ||
| 312 | } | ||
