Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: sandbox/win/src/process_mitigations_test.cc

Issue 1856993003: Implement sandbox hooks to forward OPM related GDI system calls. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Replaced shared memory implementation. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <map>
6 #include <string>
7
5 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
6 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
7 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
8 #include "base/path_service.h" 11 #include "base/path_service.h"
9 #include "base/process/launch.h" 12 #include "base/process/launch.h"
10 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
11 #include "base/win/scoped_handle.h" 14 #include "base/win/scoped_handle.h"
12 #include "base/win/windows_version.h" 15 #include "base/win/windows_version.h"
13 #include "sandbox/win/src/nt_internals.h" 16 #include "sandbox/win/src/nt_internals.h"
14 #include "sandbox/win/src/process_mitigations.h" 17 #include "sandbox/win/src/process_mitigations.h"
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 } 196 }
194 197
195 std::wstring test = L"TestChildProcess "; 198 std::wstring test = L"TestChildProcess ";
196 test += new_path.value().c_str(); 199 test += new_path.value().c_str();
197 200
198 EXPECT_EQ((is_success_test ? sandbox::SBOX_TEST_SUCCEEDED 201 EXPECT_EQ((is_success_test ? sandbox::SBOX_TEST_SUCCEEDED
199 : sandbox::SBOX_TEST_FAILED), 202 : sandbox::SBOX_TEST_FAILED),
200 runner.RunTest(test.c_str())); 203 runner.RunTest(test.c_str()));
201 } 204 }
202 205
206 BOOL CALLBACK MonitorEnumCallback(HMONITOR monitor,
207 HDC hdc_monitor,
208 LPRECT rect_monitor,
209 LPARAM data) {
210 std::map<HMONITOR, base::string16>& monitors =
211 *reinterpret_cast<std::map<HMONITOR, base::string16>*>(data);
212 MONITORINFOEXW monitor_info = {};
213 monitor_info.cbSize = sizeof(monitor_info);
214
215 if (!GetMonitorInfoW(monitor, reinterpret_cast<MONITORINFO*>(&monitor_info)))
216 return FALSE;
217 monitors[monitor] = monitor_info.szDevice;
218 return TRUE;
219 }
220
221 std::map<HMONITOR, std::wstring> EnumerateMonitors() {
222 std::map<HMONITOR, std::wstring> result;
223 ::EnumDisplayMonitors(nullptr, nullptr, MonitorEnumCallback,
224 reinterpret_cast<LPARAM>(&result));
225 return result;
226 }
227
228 std::wstring MonitorListToString(
229 const std::map<HMONITOR, std::wstring>& monitors) {
230 std::wstring monitors_string;
231 for (const auto& monitor : monitors) {
232 base::StringAppendF(&monitors_string, L" %p %s", monitor.first,
233 monitor.second.c_str());
234 }
235 return monitors_string;
236 }
237
203 } // namespace 238 } // namespace
204 239
205 namespace sandbox { 240 namespace sandbox {
206 241
207 // A shared helper test command that will attempt to CreateProcess with a given 242 // A shared helper test command that will attempt to CreateProcess with a given
208 // command line. The second optional parameter will cause the child process to 243 // command line. The second optional parameter will cause the child process to
209 // return that as an exit code on termination. 244 // return that as an exit code on termination.
210 // 245 //
211 // ***Make sure you've enabled basic process creation in the 246 // ***Make sure you've enabled basic process creation in the
212 // test sandbox settings via: 247 // test sandbox settings via:
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 ::GetProcAddress(::GetModuleHandleW(L"kernel32.dll"), 408 ::GetProcAddress(::GetModuleHandleW(L"kernel32.dll"),
374 "GetProcessMitigationPolicy")); 409 "GetProcessMitigationPolicy"));
375 if (!get_process_mitigation_policy) 410 if (!get_process_mitigation_policy)
376 return SBOX_TEST_NOT_FOUND; 411 return SBOX_TEST_NOT_FOUND;
377 412
378 if (!CheckWin8Win32CallPolicy()) 413 if (!CheckWin8Win32CallPolicy())
379 return SBOX_TEST_FIRST_ERROR; 414 return SBOX_TEST_FIRST_ERROR;
380 return SBOX_TEST_SUCCEEDED; 415 return SBOX_TEST_SUCCEEDED;
381 } 416 }
382 417
418 SBOX_TESTS_COMMAND int CheckOPMGetDisplayMonitors(int argc, wchar_t** argv) {
419 if (argc == 0 || (argc & 1) != 0)
420 return SBOX_TEST_FIRST_ERROR;
421
422 std::map<HMONITOR, base::string16> monitors = EnumerateMonitors();
423 std::map<HMONITOR, base::string16> monitors_to_test;
424
425 for (int index = 0; index < argc; index += 2) {
426 HMONITOR monitor =
427 reinterpret_cast<HMONITOR>(wcstoull(argv[index], nullptr, 16));
428 monitors_to_test[monitor] = argv[index + 1];
429 }
430
431 if (monitors.size() != monitors_to_test.size())
432 return SBOX_TEST_SECOND_ERROR;
433
434 for (auto it = monitors.begin(); it != monitors.end(); ++it) {
435 auto result = monitors_to_test.find(it->first);
436 if (result == monitors_to_test.end())
437 return SBOX_TEST_THIRD_ERROR;
438 if (result->second != it->second)
439 return SBOX_TEST_FOURTH_ERROR;
440 }
441 return SBOX_TEST_SUCCEEDED;
442 }
443
383 // This test validates that setting the MITIGATION_WIN32K_DISABLE mitigation on 444 // This test validates that setting the MITIGATION_WIN32K_DISABLE mitigation on
384 // the target process causes the launch to fail in process initialization. 445 // the target process causes the launch to fail in process initialization.
385 // The test process itself links against user32/gdi32. 446 // The test process itself links against user32/gdi32.
386 TEST(ProcessMitigationsTest, CheckWin8Win32KLockDownFailure) { 447 TEST(ProcessMitigationsTest, CheckWin8Win32KLockDownFailure) {
387 if (base::win::GetVersion() < base::win::VERSION_WIN8) 448 if (base::win::GetVersion() < base::win::VERSION_WIN8)
388 return; 449 return;
389 450
390 TestRunner runner; 451 TestRunner runner;
391 sandbox::TargetPolicy* policy = runner.GetPolicy(); 452 sandbox::TargetPolicy* policy = runner.GetPolicy();
392 453
(...skipping 12 matching lines...) Expand all
405 466
406 TestRunner runner; 467 TestRunner runner;
407 sandbox::TargetPolicy* policy = runner.GetPolicy(); 468 sandbox::TargetPolicy* policy = runner.GetPolicy();
408 469
409 EXPECT_EQ(policy->SetProcessMitigations(MITIGATION_WIN32K_DISABLE), 470 EXPECT_EQ(policy->SetProcessMitigations(MITIGATION_WIN32K_DISABLE),
410 SBOX_ALL_OK); 471 SBOX_ALL_OK);
411 EXPECT_EQ(policy->AddRule(sandbox::TargetPolicy::SUBSYS_WIN32K_LOCKDOWN, 472 EXPECT_EQ(policy->AddRule(sandbox::TargetPolicy::SUBSYS_WIN32K_LOCKDOWN,
412 sandbox::TargetPolicy::FAKE_USER_GDI_INIT, NULL), 473 sandbox::TargetPolicy::FAKE_USER_GDI_INIT, NULL),
413 sandbox::SBOX_ALL_OK); 474 sandbox::SBOX_ALL_OK);
414 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"CheckWin8Lockdown")); 475 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"CheckWin8Lockdown"));
476
477 // Also check that we can't access redirected APIs.
478 std::map<HMONITOR, std::wstring> monitors = EnumerateMonitors();
479 std::wstring test_command =
480 L"CheckOPMGetDisplayMonitors" + MonitorListToString(monitors);
481 EXPECT_NE(SBOX_TEST_SUCCEEDED, runner.RunTest(test_command.c_str()));
482 }
483
484 // This test validates the even though we're running under win32k lockdown
485 // we can use the IPC redirection to enumerate the list of monitors.
486 TEST(ProcessMitigationsTest, CheckWin8OpmRedirectionSuccess) {
487 if (base::win::GetVersion() < base::win::VERSION_WIN8)
488 return;
489
490 TestRunner runner;
491 sandbox::TargetPolicy* policy = runner.GetPolicy();
492
493 EXPECT_EQ(policy->SetProcessMitigations(MITIGATION_WIN32K_DISABLE),
494 SBOX_ALL_OK);
495 EXPECT_EQ(policy->AddRule(sandbox::TargetPolicy::SUBSYS_WIN32K_LOCKDOWN,
496 sandbox::TargetPolicy::IMPLEMENT_OPM_APIS, NULL),
497 sandbox::SBOX_ALL_OK);
498 policy->SetEnableOPMRedirection();
499 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"CheckWin8Lockdown"));
500
501 std::map<HMONITOR, std::wstring> monitors = EnumerateMonitors();
502 std::wstring test_command =
503 L"CheckOPMGetDisplayMonitors" + MonitorListToString(monitors);
504 printf("%ls\n", test_command.c_str());
505 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(test_command.c_str()));
415 } 506 }
416 507
417 //------------------------------------------------------------------------------ 508 //------------------------------------------------------------------------------
418 // Disable non-system font loads (MITIGATION_NONSYSTEM_FONT_DISABLE) 509 // Disable non-system font loads (MITIGATION_NONSYSTEM_FONT_DISABLE)
419 // >= Win10 510 // >= Win10
420 //------------------------------------------------------------------------------ 511 //------------------------------------------------------------------------------
421 512
422 SBOX_TESTS_COMMAND int CheckWin10FontLockDown(int argc, wchar_t** argv) { 513 SBOX_TESTS_COMMAND int CheckWin10FontLockDown(int argc, wchar_t** argv) {
423 get_process_mitigation_policy = 514 get_process_mitigation_policy =
424 reinterpret_cast<GetProcessMitigationPolicyFunction>(::GetProcAddress( 515 reinterpret_cast<GetProcessMitigationPolicyFunction>(::GetProcAddress(
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
726 cmd = cmd.Append(L"calc.exe"); 817 cmd = cmd.Append(L"calc.exe");
727 818
728 std::wstring test_command(base::StringPrintf(L"TestChildProcess %ls 0x%08X", 819 std::wstring test_command(base::StringPrintf(L"TestChildProcess %ls 0x%08X",
729 cmd.value().c_str(), 820 cmd.value().c_str(),
730 STATUS_ACCESS_VIOLATION)); 821 STATUS_ACCESS_VIOLATION));
731 822
732 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(test_command.c_str())); 823 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(test_command.c_str()));
733 } 824 }
734 825
735 } // namespace sandbox 826 } // namespace sandbox
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698