OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "sandbox/win/src/process_mitigations.h" |
| 6 |
| 7 #include <d3d9.h> |
| 8 #include <initguid.h> |
| 9 #include <opmapi.h> |
| 10 #include <windows.h> |
| 11 |
| 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/win/windows_version.h" |
| 15 #include "sandbox/win/src/nt_internals.h" |
| 16 #include "sandbox/win/src/process_mitigations_win32k_policy.h" |
| 17 #include "sandbox/win/tests/common/controller.h" |
| 18 #include "sandbox/win/tests/integration_tests/integration_tests_common.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 //------------------------------------------------------------------------------ |
| 24 // Internal Functions |
| 25 //------------------------------------------------------------------------------ |
| 26 |
| 27 BOOL CALLBACK MonitorEnumCallback(HMONITOR monitor, |
| 28 HDC hdc_monitor, |
| 29 LPRECT rect_monitor, |
| 30 LPARAM data) { |
| 31 std::map<HMONITOR, base::string16>& monitors = |
| 32 *reinterpret_cast<std::map<HMONITOR, base::string16>*>(data); |
| 33 MONITORINFOEXW monitor_info = {}; |
| 34 monitor_info.cbSize = sizeof(monitor_info); |
| 35 |
| 36 if (!::GetMonitorInfoW(monitor, |
| 37 reinterpret_cast<MONITORINFO*>(&monitor_info))) |
| 38 return FALSE; |
| 39 monitors[monitor] = monitor_info.szDevice; |
| 40 return TRUE; |
| 41 } |
| 42 |
| 43 std::map<HMONITOR, std::wstring> EnumerateMonitors() { |
| 44 std::map<HMONITOR, std::wstring> result; |
| 45 ::EnumDisplayMonitors(nullptr, nullptr, MonitorEnumCallback, |
| 46 reinterpret_cast<LPARAM>(&result)); |
| 47 return result; |
| 48 } |
| 49 |
| 50 #define HMONITOR_ENTRY(monitor) \ |
| 51 result[reinterpret_cast<HMONITOR>(monitor)] = \ |
| 52 base::StringPrintf(L"\\\\.\\DISPLAY%X", monitor) |
| 53 |
| 54 std::map<HMONITOR, std::wstring> GetTestMonitors() { |
| 55 std::map<HMONITOR, std::wstring> result; |
| 56 |
| 57 HMONITOR_ENTRY(0x11111111); |
| 58 HMONITOR_ENTRY(0x22222222); |
| 59 HMONITOR_ENTRY(0x44444444); |
| 60 HMONITOR_ENTRY(0x88888888); |
| 61 return result; |
| 62 } |
| 63 |
| 64 std::wstring UnicodeStringToString(PUNICODE_STRING name) { |
| 65 return std::wstring(name->Buffer, |
| 66 name->Buffer + (name->Length / sizeof(name->Buffer[0]))); |
| 67 } |
| 68 |
| 69 // Returns an index 1, 2, 4 or 8 depening on the device. 0 on error. |
| 70 DWORD GetTestDeviceMonitorIndex(PUNICODE_STRING device_name) { |
| 71 std::wstring name = UnicodeStringToString(device_name); |
| 72 std::map<HMONITOR, std::wstring> monitors = GetTestMonitors(); |
| 73 for (const auto& monitor : monitors) { |
| 74 if (name == monitor.second) |
| 75 return static_cast<DWORD>(reinterpret_cast<uintptr_t>(monitor.first)) & |
| 76 0xF; |
| 77 } |
| 78 return 0; |
| 79 } |
| 80 |
| 81 NTSTATUS WINAPI GetSuggestedOPMProtectedOutputArraySizeTest( |
| 82 PUNICODE_STRING device_name, |
| 83 DWORD* suggested_output_array_size) { |
| 84 DWORD monitor = GetTestDeviceMonitorIndex(device_name); |
| 85 if (!monitor) |
| 86 return STATUS_OBJECT_NAME_NOT_FOUND; |
| 87 *suggested_output_array_size = monitor; |
| 88 return STATUS_SUCCESS; |
| 89 } |
| 90 |
| 91 NTSTATUS WINAPI |
| 92 CreateOPMProtectedOutputsTest(PUNICODE_STRING device_name, |
| 93 DXGKMDT_OPM_VIDEO_OUTPUT_SEMANTICS vos, |
| 94 DWORD output_array_size, |
| 95 DWORD* num_in_output_array, |
| 96 OPM_PROTECTED_OUTPUT_HANDLE* output_array) { |
| 97 DWORD monitor = GetTestDeviceMonitorIndex(device_name); |
| 98 if (!monitor) |
| 99 return STATUS_OBJECT_NAME_NOT_FOUND; |
| 100 if (vos != DXGKMDT_OPM_VOS_OPM_SEMANTICS) |
| 101 return STATUS_INVALID_PARAMETER; |
| 102 if (output_array_size != monitor) |
| 103 return STATUS_INVALID_PARAMETER; |
| 104 *num_in_output_array = monitor - 1; |
| 105 for (DWORD index = 0; index < monitor - 1; ++index) { |
| 106 output_array[index] = |
| 107 reinterpret_cast<OPM_PROTECTED_OUTPUT_HANDLE>((monitor << 4) + index); |
| 108 } |
| 109 return STATUS_SUCCESS; |
| 110 } |
| 111 |
| 112 ULONG CalculateCertLength(ULONG monitor) { |
| 113 return (monitor * 0x800) + 0xabc; |
| 114 } |
| 115 |
| 116 NTSTATUS WINAPI GetCertificateTest(PUNICODE_STRING device_name, |
| 117 DXGKMDT_CERTIFICATE_TYPE certificate_type, |
| 118 BYTE* certificate, |
| 119 ULONG certificate_length) { |
| 120 DWORD monitor = GetTestDeviceMonitorIndex(device_name); |
| 121 if (!monitor) |
| 122 return STATUS_OBJECT_NAME_NOT_FOUND; |
| 123 if (certificate_type != DXGKMDT_OPM_CERTIFICATE) |
| 124 return STATUS_INVALID_PARAMETER; |
| 125 if (certificate_length != CalculateCertLength(monitor)) |
| 126 return STATUS_INVALID_PARAMETER; |
| 127 memset(certificate, 'A' + monitor, certificate_length); |
| 128 return STATUS_SUCCESS; |
| 129 } |
| 130 |
| 131 NTSTATUS WINAPI |
| 132 GetCertificateSizeTest(PUNICODE_STRING device_name, |
| 133 DXGKMDT_CERTIFICATE_TYPE certificate_type, |
| 134 ULONG* certificate_length) { |
| 135 DWORD monitor = GetTestDeviceMonitorIndex(device_name); |
| 136 if (!monitor) |
| 137 return STATUS_OBJECT_NAME_NOT_FOUND; |
| 138 if (certificate_type != DXGKMDT_OPM_CERTIFICATE) |
| 139 return STATUS_INVALID_PARAMETER; |
| 140 *certificate_length = CalculateCertLength(monitor); |
| 141 return STATUS_SUCCESS; |
| 142 } |
| 143 |
| 144 // Check for valid output handle and return the monitor index. |
| 145 DWORD IsValidProtectedOutput(OPM_PROTECTED_OUTPUT_HANDLE protected_output) { |
| 146 uintptr_t handle = reinterpret_cast<uintptr_t>(protected_output); |
| 147 uintptr_t monitor = handle >> 4; |
| 148 uintptr_t index = handle & 0xF; |
| 149 switch (monitor) { |
| 150 case 1: |
| 151 case 2: |
| 152 case 4: |
| 153 case 8: |
| 154 break; |
| 155 default: |
| 156 return 0; |
| 157 } |
| 158 if (index >= (monitor - 1)) |
| 159 return 0; |
| 160 return static_cast<DWORD>(monitor); |
| 161 } |
| 162 |
| 163 NTSTATUS WINAPI |
| 164 GetCertificateByHandleTest(OPM_PROTECTED_OUTPUT_HANDLE protected_output, |
| 165 DXGKMDT_CERTIFICATE_TYPE certificate_type, |
| 166 BYTE* certificate, |
| 167 ULONG certificate_length) { |
| 168 DWORD monitor = IsValidProtectedOutput(protected_output); |
| 169 if (!monitor) |
| 170 return STATUS_INVALID_HANDLE; |
| 171 if (certificate_type != DXGKMDT_OPM_CERTIFICATE) |
| 172 return STATUS_INVALID_PARAMETER; |
| 173 if (certificate_length != CalculateCertLength(monitor)) |
| 174 return STATUS_INVALID_PARAMETER; |
| 175 memset(certificate, 'A' + monitor, certificate_length); |
| 176 return STATUS_SUCCESS; |
| 177 } |
| 178 |
| 179 NTSTATUS WINAPI |
| 180 GetCertificateSizeByHandleTest(OPM_PROTECTED_OUTPUT_HANDLE protected_output, |
| 181 DXGKMDT_CERTIFICATE_TYPE certificate_type, |
| 182 ULONG* certificate_length) { |
| 183 DWORD monitor = IsValidProtectedOutput(protected_output); |
| 184 if (!monitor) |
| 185 return STATUS_INVALID_HANDLE; |
| 186 if (certificate_type != DXGKMDT_OPM_CERTIFICATE) |
| 187 return STATUS_INVALID_PARAMETER; |
| 188 *certificate_length = CalculateCertLength(monitor); |
| 189 return STATUS_SUCCESS; |
| 190 } |
| 191 |
| 192 NTSTATUS WINAPI |
| 193 DestroyOPMProtectedOutputTest(OPM_PROTECTED_OUTPUT_HANDLE protected_output) { |
| 194 if (!IsValidProtectedOutput(protected_output)) |
| 195 return STATUS_INVALID_HANDLE; |
| 196 return STATUS_SUCCESS; |
| 197 } |
| 198 |
| 199 NTSTATUS WINAPI ConfigureOPMProtectedOutputTest( |
| 200 OPM_PROTECTED_OUTPUT_HANDLE protected_output, |
| 201 const DXGKMDT_OPM_CONFIGURE_PARAMETERS* parameters, |
| 202 ULONG additional_parameters_size, |
| 203 const BYTE* additional_parameters) { |
| 204 if (!IsValidProtectedOutput(protected_output)) |
| 205 return STATUS_INVALID_HANDLE; |
| 206 if (additional_parameters && additional_parameters_size) |
| 207 return STATUS_INVALID_PARAMETER; |
| 208 return STATUS_SUCCESS; |
| 209 } |
| 210 |
| 211 NTSTATUS WINAPI GetOPMInformationTest( |
| 212 OPM_PROTECTED_OUTPUT_HANDLE protected_output, |
| 213 const DXGKMDT_OPM_GET_INFO_PARAMETERS* parameters, |
| 214 DXGKMDT_OPM_REQUESTED_INFORMATION* requested_information) { |
| 215 DWORD monitor = IsValidProtectedOutput(protected_output); |
| 216 if (!monitor) |
| 217 return STATUS_INVALID_HANDLE; |
| 218 memset(requested_information, '0' + monitor, |
| 219 sizeof(DXGKMDT_OPM_REQUESTED_INFORMATION)); |
| 220 return STATUS_SUCCESS; |
| 221 } |
| 222 |
| 223 NTSTATUS WINAPI |
| 224 GetOPMRandomNumberTest(OPM_PROTECTED_OUTPUT_HANDLE protected_output, |
| 225 DXGKMDT_OPM_RANDOM_NUMBER* random_number) { |
| 226 DWORD monitor = IsValidProtectedOutput(protected_output); |
| 227 if (!monitor) |
| 228 return STATUS_INVALID_HANDLE; |
| 229 memset(random_number->abRandomNumber, '!' + monitor, |
| 230 sizeof(random_number->abRandomNumber)); |
| 231 return STATUS_SUCCESS; |
| 232 } |
| 233 |
| 234 NTSTATUS WINAPI SetOPMSigningKeyAndSequenceNumbersTest( |
| 235 OPM_PROTECTED_OUTPUT_HANDLE protected_output, |
| 236 const DXGKMDT_OPM_ENCRYPTED_PARAMETERS* parameters) { |
| 237 DWORD monitor = IsValidProtectedOutput(protected_output); |
| 238 if (!monitor) |
| 239 return STATUS_INVALID_HANDLE; |
| 240 DXGKMDT_OPM_ENCRYPTED_PARAMETERS test_params = {}; |
| 241 memset(test_params.abEncryptedParameters, 'a' + monitor, |
| 242 sizeof(test_params.abEncryptedParameters)); |
| 243 if (memcmp(test_params.abEncryptedParameters, |
| 244 parameters->abEncryptedParameters, |
| 245 sizeof(test_params.abEncryptedParameters)) != 0) |
| 246 return STATUS_INVALID_PARAMETER; |
| 247 return STATUS_SUCCESS; |
| 248 } |
| 249 |
| 250 BOOL WINAPI EnumDisplayMonitorsTest(HDC hdc, |
| 251 LPCRECT clip_rect, |
| 252 MONITORENUMPROC enum_function, |
| 253 LPARAM data) { |
| 254 RECT rc = {}; |
| 255 for (const auto& monitor : GetTestMonitors()) { |
| 256 if (!enum_function(monitor.first, hdc, &rc, data)) |
| 257 return FALSE; |
| 258 } |
| 259 return TRUE; |
| 260 } |
| 261 |
| 262 BOOL WINAPI GetMonitorInfoWTest(HMONITOR monitor, LPMONITORINFO monitor_info) { |
| 263 std::map<HMONITOR, std::wstring> monitors = GetTestMonitors(); |
| 264 if (monitor_info->cbSize != sizeof(MONITORINFO) && |
| 265 monitor_info->cbSize != sizeof(MONITORINFOEXW)) |
| 266 return FALSE; |
| 267 auto it = monitors.find(monitor); |
| 268 if (it == monitors.end()) |
| 269 return FALSE; |
| 270 if (monitor_info->cbSize == sizeof(MONITORINFOEXW)) { |
| 271 MONITORINFOEXW* monitor_info_ex = |
| 272 reinterpret_cast<MONITORINFOEXW*>(monitor_info); |
| 273 size_t copy_size = (it->second.size() + 1) * sizeof(WCHAR); |
| 274 if (copy_size > sizeof(monitor_info_ex->szDevice) - sizeof(WCHAR)) |
| 275 copy_size = sizeof(monitor_info_ex->szDevice) - sizeof(WCHAR); |
| 276 memset(monitor_info_ex->szDevice, 0, sizeof(monitor_info_ex->szDevice)); |
| 277 memcpy(monitor_info_ex->szDevice, it->second.c_str(), copy_size); |
| 278 } |
| 279 return TRUE; |
| 280 } |
| 281 |
| 282 #define RETURN_TEST_FUNC(n) \ |
| 283 if (strcmp(name, #n) == 0) { \ |
| 284 return n##Test; \ |
| 285 } |
| 286 |
| 287 void* FunctionOverrideForTest(const char* name) { |
| 288 RETURN_TEST_FUNC(GetSuggestedOPMProtectedOutputArraySize); |
| 289 RETURN_TEST_FUNC(CreateOPMProtectedOutputs); |
| 290 RETURN_TEST_FUNC(GetCertificate); |
| 291 RETURN_TEST_FUNC(GetCertificateSize); |
| 292 RETURN_TEST_FUNC(DestroyOPMProtectedOutput); |
| 293 RETURN_TEST_FUNC(ConfigureOPMProtectedOutput); |
| 294 RETURN_TEST_FUNC(GetOPMInformation); |
| 295 RETURN_TEST_FUNC(GetOPMRandomNumber); |
| 296 RETURN_TEST_FUNC(SetOPMSigningKeyAndSequenceNumbers); |
| 297 RETURN_TEST_FUNC(EnumDisplayMonitors); |
| 298 RETURN_TEST_FUNC(GetMonitorInfoW); |
| 299 RETURN_TEST_FUNC(GetCertificateByHandle); |
| 300 RETURN_TEST_FUNC(GetCertificateSizeByHandle); |
| 301 NOTREACHED(); |
| 302 return nullptr; |
| 303 } |
| 304 |
| 305 bool RunTestsOnVideoOutputConfigure(uintptr_t monitor_index, |
| 306 IOPMVideoOutput* video_output) { |
| 307 OPM_CONFIGURE_PARAMETERS config_params = {}; |
| 308 OPM_SET_PROTECTION_LEVEL_PARAMETERS* protection_level = |
| 309 reinterpret_cast<OPM_SET_PROTECTION_LEVEL_PARAMETERS*>( |
| 310 config_params.abParameters); |
| 311 protection_level->ulProtectionType = OPM_PROTECTION_TYPE_HDCP; |
| 312 protection_level->ulProtectionLevel = OPM_HDCP_ON; |
| 313 config_params.guidSetting = OPM_SET_PROTECTION_LEVEL; |
| 314 config_params.cbParametersSize = sizeof(OPM_SET_PROTECTION_LEVEL_PARAMETERS); |
| 315 HRESULT hr = video_output->Configure(&config_params, 0, nullptr); |
| 316 if (FAILED(hr)) |
| 317 return false; |
| 318 protection_level->ulProtectionType = OPM_PROTECTION_TYPE_DPCP; |
| 319 hr = video_output->Configure(&config_params, 0, nullptr); |
| 320 if (FAILED(hr)) |
| 321 return false; |
| 322 protection_level->ulProtectionLevel = OPM_HDCP_OFF; |
| 323 hr = video_output->Configure(&config_params, 0, nullptr); |
| 324 if (FAILED(hr)) |
| 325 return false; |
| 326 BYTE dummy_byte = 0; |
| 327 hr = video_output->Configure(&config_params, 1, &dummy_byte); |
| 328 if (SUCCEEDED(hr)) |
| 329 return false; |
| 330 protection_level->ulProtectionType = 0xFFFFFFFF; |
| 331 hr = video_output->Configure(&config_params, 0, nullptr); |
| 332 if (SUCCEEDED(hr)) |
| 333 return false; |
| 334 // Invalid protection level test. |
| 335 protection_level->ulProtectionType = OPM_PROTECTION_TYPE_HDCP; |
| 336 protection_level->ulProtectionLevel = OPM_HDCP_ON + 1; |
| 337 hr = video_output->Configure(&config_params, 0, nullptr); |
| 338 if (SUCCEEDED(hr)) |
| 339 return false; |
| 340 hr = video_output->Configure(&config_params, 0, nullptr); |
| 341 if (SUCCEEDED(hr)) |
| 342 return false; |
| 343 config_params.guidSetting = OPM_SET_HDCP_SRM; |
| 344 OPM_SET_HDCP_SRM_PARAMETERS* srm_parameters = |
| 345 reinterpret_cast<OPM_SET_HDCP_SRM_PARAMETERS*>( |
| 346 config_params.abParameters); |
| 347 srm_parameters->ulSRMVersion = 1; |
| 348 config_params.cbParametersSize = sizeof(OPM_SET_HDCP_SRM_PARAMETERS); |
| 349 hr = video_output->Configure(&config_params, 0, nullptr); |
| 350 if (SUCCEEDED(hr)) |
| 351 return false; |
| 352 return true; |
| 353 } |
| 354 |
| 355 bool RunTestsOnVideoOutputFinishInitialization(uintptr_t monitor_index, |
| 356 IOPMVideoOutput* video_output) { |
| 357 OPM_ENCRYPTED_INITIALIZATION_PARAMETERS init_params = {}; |
| 358 memset(init_params.abEncryptedInitializationParameters, 'a' + monitor_index, |
| 359 sizeof(init_params.abEncryptedInitializationParameters)); |
| 360 HRESULT hr = video_output->FinishInitialization(&init_params); |
| 361 if (FAILED(hr)) |
| 362 return false; |
| 363 memset(init_params.abEncryptedInitializationParameters, 'Z' + monitor_index, |
| 364 sizeof(init_params.abEncryptedInitializationParameters)); |
| 365 hr = video_output->FinishInitialization(&init_params); |
| 366 if (SUCCEEDED(hr)) |
| 367 return false; |
| 368 return true; |
| 369 } |
| 370 |
| 371 bool RunTestsOnVideoOutputStartInitialization(uintptr_t monitor_index, |
| 372 IOPMVideoOutput* video_output) { |
| 373 OPM_RANDOM_NUMBER random_number = {}; |
| 374 BYTE* certificate = nullptr; |
| 375 ULONG certificate_length = 0; |
| 376 |
| 377 HRESULT hr = video_output->StartInitialization(&random_number, &certificate, |
| 378 &certificate_length); |
| 379 if (FAILED(hr)) |
| 380 return false; |
| 381 |
| 382 if (certificate_length != CalculateCertLength(monitor_index)) |
| 383 return false; |
| 384 |
| 385 for (ULONG i = 0; i < certificate_length; ++i) { |
| 386 if (certificate[i] != 'A' + monitor_index) |
| 387 return false; |
| 388 } |
| 389 |
| 390 for (ULONG i = 0; i < sizeof(random_number.abRandomNumber); ++i) { |
| 391 if (random_number.abRandomNumber[i] != '!' + monitor_index) |
| 392 return false; |
| 393 } |
| 394 |
| 395 return true; |
| 396 } |
| 397 |
| 398 static bool SendSingleGetInfoRequest(uintptr_t monitor_index, |
| 399 IOPMVideoOutput* video_output, |
| 400 const GUID& request, |
| 401 ULONG data_length, |
| 402 void* data) { |
| 403 OPM_GET_INFO_PARAMETERS params = {}; |
| 404 OPM_REQUESTED_INFORMATION requested_information = {}; |
| 405 BYTE* requested_information_ptr = |
| 406 reinterpret_cast<BYTE*>(&requested_information); |
| 407 params.guidInformation = request; |
| 408 params.cbParametersSize = data_length; |
| 409 memcpy(params.abParameters, data, data_length); |
| 410 HRESULT hr = video_output->GetInformation(¶ms, &requested_information); |
| 411 if (FAILED(hr)) |
| 412 return false; |
| 413 for (size_t i = 0; i < sizeof(OPM_REQUESTED_INFORMATION); ++i) { |
| 414 if (requested_information_ptr[i] != '0' + monitor_index) |
| 415 return false; |
| 416 } |
| 417 return true; |
| 418 } |
| 419 |
| 420 bool RunTestsOnVideoOutputGetInformation(uintptr_t monitor_index, |
| 421 IOPMVideoOutput* video_output) { |
| 422 ULONG dummy = 0; |
| 423 if (!SendSingleGetInfoRequest(monitor_index, video_output, |
| 424 OPM_GET_CONNECTOR_TYPE, 0, nullptr)) { |
| 425 return false; |
| 426 } |
| 427 if (!SendSingleGetInfoRequest(monitor_index, video_output, |
| 428 OPM_GET_SUPPORTED_PROTECTION_TYPES, 0, |
| 429 nullptr)) { |
| 430 return false; |
| 431 } |
| 432 // These should fail due to invalid parameter sizes. |
| 433 if (SendSingleGetInfoRequest(monitor_index, video_output, |
| 434 OPM_GET_CONNECTOR_TYPE, sizeof(dummy), &dummy)) { |
| 435 return false; |
| 436 } |
| 437 if (SendSingleGetInfoRequest(monitor_index, video_output, |
| 438 OPM_GET_SUPPORTED_PROTECTION_TYPES, |
| 439 sizeof(dummy), &dummy)) { |
| 440 return false; |
| 441 } |
| 442 ULONG protection_type = OPM_PROTECTION_TYPE_HDCP; |
| 443 if (!SendSingleGetInfoRequest(monitor_index, video_output, |
| 444 OPM_GET_ACTUAL_PROTECTION_LEVEL, |
| 445 sizeof(protection_type), &protection_type)) { |
| 446 return false; |
| 447 } |
| 448 protection_type = OPM_PROTECTION_TYPE_DPCP; |
| 449 if (!SendSingleGetInfoRequest(monitor_index, video_output, |
| 450 OPM_GET_ACTUAL_PROTECTION_LEVEL, |
| 451 sizeof(protection_type), &protection_type)) { |
| 452 return false; |
| 453 } |
| 454 // These should fail as unsupported or invalid parameters. |
| 455 protection_type = OPM_PROTECTION_TYPE_ACP; |
| 456 if (SendSingleGetInfoRequest(monitor_index, video_output, |
| 457 OPM_GET_ACTUAL_PROTECTION_LEVEL, |
| 458 sizeof(protection_type), &protection_type)) { |
| 459 return false; |
| 460 } |
| 461 if (SendSingleGetInfoRequest(monitor_index, video_output, |
| 462 OPM_GET_ACTUAL_PROTECTION_LEVEL, 0, nullptr)) { |
| 463 return false; |
| 464 } |
| 465 protection_type = OPM_PROTECTION_TYPE_HDCP; |
| 466 if (!SendSingleGetInfoRequest(monitor_index, video_output, |
| 467 OPM_GET_VIRTUAL_PROTECTION_LEVEL, |
| 468 sizeof(protection_type), &protection_type)) { |
| 469 return false; |
| 470 } |
| 471 protection_type = OPM_PROTECTION_TYPE_DPCP; |
| 472 if (!SendSingleGetInfoRequest(monitor_index, video_output, |
| 473 OPM_GET_VIRTUAL_PROTECTION_LEVEL, |
| 474 sizeof(protection_type), &protection_type)) { |
| 475 return false; |
| 476 } |
| 477 // These should fail as unsupported or invalid parameters. |
| 478 protection_type = OPM_PROTECTION_TYPE_ACP; |
| 479 if (SendSingleGetInfoRequest(monitor_index, video_output, |
| 480 OPM_GET_VIRTUAL_PROTECTION_LEVEL, |
| 481 sizeof(protection_type), &protection_type)) { |
| 482 return false; |
| 483 } |
| 484 if (SendSingleGetInfoRequest(monitor_index, video_output, |
| 485 OPM_GET_VIRTUAL_PROTECTION_LEVEL, 0, nullptr)) { |
| 486 return false; |
| 487 } |
| 488 // This should fail with unsupported request. |
| 489 if (SendSingleGetInfoRequest(monitor_index, video_output, OPM_GET_CODEC_INFO, |
| 490 0, nullptr)) { |
| 491 return false; |
| 492 } |
| 493 return true; |
| 494 } |
| 495 |
| 496 int RunTestsOnVideoOutput(uintptr_t monitor_index, |
| 497 IOPMVideoOutput* video_output) { |
| 498 if (!RunTestsOnVideoOutputStartInitialization(monitor_index, video_output)) |
| 499 return sandbox::SBOX_TEST_FIRST_ERROR; |
| 500 |
| 501 if (!RunTestsOnVideoOutputFinishInitialization(monitor_index, video_output)) |
| 502 return sandbox::SBOX_TEST_SECOND_ERROR; |
| 503 |
| 504 if (!RunTestsOnVideoOutputConfigure(monitor_index, video_output)) |
| 505 return sandbox::SBOX_TEST_THIRD_ERROR; |
| 506 |
| 507 if (!RunTestsOnVideoOutputGetInformation(monitor_index, video_output)) |
| 508 return sandbox::SBOX_TEST_FOURTH_ERROR; |
| 509 |
| 510 return sandbox::SBOX_TEST_SUCCEEDED; |
| 511 } |
| 512 |
| 513 } // namespace |
| 514 |
| 515 namespace sandbox { |
| 516 |
| 517 //------------------------------------------------------------------------------ |
| 518 // Exported functions called by child test processes. |
| 519 //------------------------------------------------------------------------------ |
| 520 |
| 521 SBOX_TESTS_COMMAND int CheckWin8MonitorsRedirection(int argc, wchar_t** argv) { |
| 522 std::map<HMONITOR, base::string16> monitors = EnumerateMonitors(); |
| 523 std::map<HMONITOR, base::string16> monitors_to_test = GetTestMonitors(); |
| 524 if (monitors.size() != monitors_to_test.size()) |
| 525 return SBOX_TEST_FIRST_ERROR; |
| 526 |
| 527 for (const auto& monitor : monitors) { |
| 528 auto result = monitors_to_test.find(monitor.first); |
| 529 if (result == monitors_to_test.end()) |
| 530 return SBOX_TEST_SECOND_ERROR; |
| 531 if (result->second != monitor.second) |
| 532 return SBOX_TEST_THIRD_ERROR; |
| 533 } |
| 534 return SBOX_TEST_SUCCEEDED; |
| 535 } |
| 536 |
| 537 SBOX_TESTS_COMMAND int CheckWin8MonitorInfo(int argc, wchar_t** argv) { |
| 538 std::map<HMONITOR, base::string16> monitors_to_test = GetTestMonitors(); |
| 539 MONITORINFO monitor_info = {}; |
| 540 MONITORINFOEXW monitor_info_exw = {}; |
| 541 MONITORINFOEXA monitor_info_exa = {}; |
| 542 HMONITOR valid_monitor = monitors_to_test.begin()->first; |
| 543 std::wstring valid_device = monitors_to_test.begin()->second; |
| 544 monitor_info.cbSize = sizeof(MONITORINFO); |
| 545 if (!::GetMonitorInfoW(valid_monitor, &monitor_info)) |
| 546 return SBOX_TEST_FIRST_ERROR; |
| 547 monitor_info.cbSize = sizeof(MONITORINFO); |
| 548 if (!::GetMonitorInfoA(valid_monitor, &monitor_info)) |
| 549 return SBOX_TEST_SECOND_ERROR; |
| 550 monitor_info_exw.cbSize = sizeof(MONITORINFOEXW); |
| 551 if (!::GetMonitorInfoW(valid_monitor, |
| 552 reinterpret_cast<MONITORINFO*>(&monitor_info_exw)) || |
| 553 valid_device != monitor_info_exw.szDevice) { |
| 554 return SBOX_TEST_THIRD_ERROR; |
| 555 } |
| 556 monitor_info_exa.cbSize = sizeof(MONITORINFOEXA); |
| 557 if (!::GetMonitorInfoA(valid_monitor, |
| 558 reinterpret_cast<MONITORINFO*>(&monitor_info_exa)) || |
| 559 valid_device != base::ASCIIToUTF16(monitor_info_exa.szDevice)) { |
| 560 return SBOX_TEST_FOURTH_ERROR; |
| 561 } |
| 562 |
| 563 // Invalid size checks. |
| 564 monitor_info.cbSize = 0; |
| 565 if (::GetMonitorInfoW(valid_monitor, &monitor_info)) |
| 566 return SBOX_TEST_FIFTH_ERROR; |
| 567 monitor_info.cbSize = 0x10000; |
| 568 if (::GetMonitorInfoW(valid_monitor, &monitor_info)) |
| 569 return SBOX_TEST_SIXTH_ERROR; |
| 570 |
| 571 // Check that an invalid handle isn't accepted. |
| 572 HMONITOR invalid_monitor = reinterpret_cast<HMONITOR>(-1); |
| 573 monitor_info.cbSize = sizeof(MONITORINFO); |
| 574 if (::GetMonitorInfoW(invalid_monitor, &monitor_info)) |
| 575 return SBOX_TEST_SEVENTH_ERROR; |
| 576 |
| 577 return SBOX_TEST_SUCCEEDED; |
| 578 } |
| 579 |
| 580 SBOX_TESTS_COMMAND int CheckWin8OPMApis(int argc, wchar_t** argv) { |
| 581 std::map<HMONITOR, base::string16> monitors = GetTestMonitors(); |
| 582 for (const auto& monitor : monitors) { |
| 583 ULONG output_count = 0; |
| 584 IOPMVideoOutput** outputs = nullptr; |
| 585 uintptr_t monitor_index = reinterpret_cast<uintptr_t>(monitor.first) & 0xF; |
| 586 HRESULT hr = OPMGetVideoOutputsFromHMONITOR( |
| 587 monitor.first, OPM_VOS_OPM_SEMANTICS, &output_count, &outputs); |
| 588 if (monitor_index > 4) { |
| 589 // These should fail because the certificate is too large. |
| 590 if (SUCCEEDED(hr)) |
| 591 return SBOX_TEST_FIRST_ERROR; |
| 592 continue; |
| 593 } |
| 594 if (FAILED(hr)) |
| 595 return SBOX_TEST_SECOND_ERROR; |
| 596 if (output_count != monitor_index - 1) |
| 597 return SBOX_TEST_THIRD_ERROR; |
| 598 for (ULONG output_index = 0; output_index < output_count; ++output_index) { |
| 599 int result = RunTestsOnVideoOutput(monitor_index, outputs[output_index]); |
| 600 outputs[output_index]->Release(); |
| 601 if (result != SBOX_TEST_SUCCEEDED) |
| 602 return result; |
| 603 } |
| 604 ::CoTaskMemFree(outputs); |
| 605 } |
| 606 return SBOX_TEST_SUCCEEDED; |
| 607 } |
| 608 |
| 609 //------------------------------------------------------------------------------ |
| 610 // Exported Win32k Lockdown Tests |
| 611 //------------------------------------------------------------------------------ |
| 612 |
| 613 // This test validates that setting the MITIGATION_WIN32K_DISABLE mitigation on |
| 614 // the target process causes the launch to fail in process initialization. |
| 615 // The test process itself links against user32/gdi32. |
| 616 TEST(ProcessMitigationsWin32kTest, CheckWin8LockDownFailure) { |
| 617 if (base::win::GetVersion() < base::win::VERSION_WIN8) |
| 618 return; |
| 619 |
| 620 std::wstring test_policy_command = L"CheckPolicy "; |
| 621 test_policy_command += std::to_wstring(TESTPOLICY_WIN32K); |
| 622 |
| 623 TestRunner runner; |
| 624 sandbox::TargetPolicy* policy = runner.GetPolicy(); |
| 625 |
| 626 EXPECT_EQ(policy->SetProcessMitigations(MITIGATION_WIN32K_DISABLE), |
| 627 SBOX_ALL_OK); |
| 628 EXPECT_NE(SBOX_TEST_SUCCEEDED, runner.RunTest(test_policy_command.c_str())); |
| 629 } |
| 630 |
| 631 // This test validates that setting the MITIGATION_WIN32K_DISABLE mitigation |
| 632 // along with the policy to fake user32 and gdi32 initialization successfully |
| 633 // launches the target process. |
| 634 // The test process itself links against user32/gdi32. |
| 635 TEST(ProcessMitigationsWin32kTest, CheckWin8LockDownSuccess) { |
| 636 if (base::win::GetVersion() < base::win::VERSION_WIN8) |
| 637 return; |
| 638 |
| 639 std::wstring test_policy_command = L"CheckPolicy "; |
| 640 test_policy_command += std::to_wstring(TESTPOLICY_WIN32K); |
| 641 |
| 642 TestRunner runner; |
| 643 sandbox::TargetPolicy* policy = runner.GetPolicy(); |
| 644 ProcessMitigationsWin32KLockdownPolicy::SetOverrideForTestCallback( |
| 645 FunctionOverrideForTest); |
| 646 |
| 647 EXPECT_EQ(policy->SetProcessMitigations(MITIGATION_WIN32K_DISABLE), |
| 648 SBOX_ALL_OK); |
| 649 EXPECT_EQ(policy->AddRule(sandbox::TargetPolicy::SUBSYS_WIN32K_LOCKDOWN, |
| 650 sandbox::TargetPolicy::FAKE_USER_GDI_INIT, NULL), |
| 651 sandbox::SBOX_ALL_OK); |
| 652 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(test_policy_command.c_str())); |
| 653 EXPECT_NE(SBOX_TEST_SUCCEEDED, |
| 654 runner.RunTest(L"CheckWin8MonitorsRedirection")); |
| 655 EXPECT_NE(SBOX_TEST_SUCCEEDED, runner.RunTest(L"CheckWin8MonitorInfo")); |
| 656 EXPECT_NE(SBOX_TEST_SUCCEEDED, runner.RunTest(L"CheckWin8OPMApis")); |
| 657 } |
| 658 |
| 659 // This test validates the even though we're running under win32k lockdown |
| 660 // we can use the IPC redirection to enumerate the list of monitors. |
| 661 TEST(ProcessMitigationsWin32kTest, CheckWin8Redirection) { |
| 662 if (base::win::GetVersion() < base::win::VERSION_WIN8) |
| 663 return; |
| 664 |
| 665 std::wstring test_policy_command = L"CheckPolicy "; |
| 666 test_policy_command += std::to_wstring(TESTPOLICY_WIN32K); |
| 667 |
| 668 TestRunner runner; |
| 669 sandbox::TargetPolicy* policy = runner.GetPolicy(); |
| 670 ProcessMitigationsWin32KLockdownPolicy::SetOverrideForTestCallback( |
| 671 FunctionOverrideForTest); |
| 672 |
| 673 EXPECT_EQ(policy->SetProcessMitigations(MITIGATION_WIN32K_DISABLE), |
| 674 SBOX_ALL_OK); |
| 675 EXPECT_EQ(policy->AddRule(sandbox::TargetPolicy::SUBSYS_WIN32K_LOCKDOWN, |
| 676 sandbox::TargetPolicy::IMPLEMENT_OPM_APIS, NULL), |
| 677 sandbox::SBOX_ALL_OK); |
| 678 policy->SetEnableOPMRedirection(); |
| 679 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(test_policy_command.c_str())); |
| 680 EXPECT_EQ(SBOX_TEST_SUCCEEDED, |
| 681 runner.RunTest(L"CheckWin8MonitorsRedirection")); |
| 682 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"CheckWin8MonitorInfo")); |
| 683 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"CheckWin8OPMApis")); |
| 684 } |
| 685 |
| 686 } // namespace sandbox |
OLD | NEW |