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

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

Issue 2944493002: [Windows Sandbox Tests] Process Mitigations. (Closed)
Patch Set: Created 3 years, 6 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
(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 <psapi.h>
8 #include <windows.h>
grt (UTC plus 2) 2017/06/15 20:58:19 nit: <windows.h> should always precede other windo
penny 2017/06/27 20:43:50 DONE. Fixed in win32k unittest file as well. This
9
10 #include "base/scoped_native_library.h"
11 #include "base/win/registry.h"
12 #include "base/win/startup_information.h"
13 #include "base/win/win_util.h"
14 #include "base/win/windows_version.h"
15 #include "sandbox/win/tests/common/controller.h"
16 #include "sandbox/win/tests/integration_tests/integration_tests_common.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 // Function definitions from hooking dll.
20 LRESULT HookProc(int code, WPARAM wParam, LPARAM lParam);
21 bool WasHookCalled();
22 void SetHook(HHOOK hook_handle);
23
24 namespace {
25
26 //------------------------------------------------------------------------------
27 // Internal Defines & Functions
28 //------------------------------------------------------------------------------
29
30 using WasHookCalledFunction = decltype(&WasHookCalled);
31 using SetHookFunction = decltype(&SetHook);
32
33 //------------------------------------------------------------------------------
34 // ExtensionPoint test helper function.
35 //
36 // Spawn Windows process (with or without mitigation enabled).
37 //------------------------------------------------------------------------------
38 bool SpawnWinProc(PROCESS_INFORMATION* pi, bool success_test, HANDLE* event) {
39 base::win::StartupInformation startup_info;
40 DWORD creation_flags = 0;
41
42 if (!success_test) {
43 DWORD64 flags =
44 PROCESS_CREATION_MITIGATION_POLICY_EXTENSION_POINT_DISABLE_ALWAYS_ON;
45 // This test only runs on >= Win8, so don't have to handle
46 // illegal 64-bit flags on 32-bit <= Win7.
47 size_t flags_size = sizeof(flags);
48
49 if (!startup_info.InitializeProcThreadAttributeList(1) ||
50 !startup_info.UpdateProcThreadAttribute(
51 PROC_THREAD_ATTRIBUTE_MITIGATION_POLICY, &flags, flags_size)) {
52 ADD_FAILURE();
53 return false;
54 }
55 creation_flags = EXTENDED_STARTUPINFO_PRESENT;
56 }
57
58 // Command line must be writable.
59 base::string16 cmd_writeable(sandbox::g_winproc_file);
60
61 if (!::CreateProcessW(NULL, &cmd_writeable[0], NULL, NULL, FALSE,
62 creation_flags, NULL, NULL, startup_info.startup_info(),
63 pi)) {
64 ADD_FAILURE();
65 return false;
66 }
67 EXPECT_EQ(WAIT_OBJECT_0,
68 ::WaitForSingleObject(*event, sandbox::SboxTestEventTimeout()));
69
70 return true;
71 }
72
73 //------------------------------------------------------------------------------
74 // ExtensionPoint test helper function.
75 //
76 // 1. Spawn a Windows process (with or without mitigation enabled).
77 // 2. Load the hook Dll locally.
78 // 3. Create a global named event for the hook to trigger.
79 // 4. Start the hook (for the specific WinProc or globally).
80 // 5. Send a keystroke event.
81 // 6. Ask the hook Dll if it received a hook callback.
82 // 7. Cleanup the hooking.
83 // 8. Signal the Windows process to shutdown.
84 //
85 // Do NOT use any ASSERTs in this function. Cleanup required.
86 //------------------------------------------------------------------------------
87 void TestWin8ExtensionPointHookWrapper(bool is_success_test, bool global_hook) {
88 // Set up a couple global events that this test will use.
89 HANDLE winproc_event =
90 ::CreateEventW(NULL, FALSE, FALSE, sandbox::g_winproc_event);
91 if (winproc_event == NULL || winproc_event == INVALID_HANDLE_VALUE) {
92 ADD_FAILURE();
93 return;
94 }
95 base::win::ScopedHandle scoped_winproc_event(winproc_event);
96
97 HANDLE hook_event = ::CreateEventW(NULL, FALSE, FALSE, sandbox::g_hook_event);
98 if (hook_event == NULL || hook_event == INVALID_HANDLE_VALUE) {
99 ADD_FAILURE();
100 return;
101 }
102 base::win::ScopedHandle scoped_hook_event(hook_event);
103
104 // 1. Spawn WinProc.
105 PROCESS_INFORMATION proc_info = {};
106 if (!SpawnWinProc(&proc_info, is_success_test, &winproc_event))
107 return;
108
109 // From this point on, no return on failure. Cleanup required.
110 bool all_good = true;
111
112 // 2. Load the hook DLL.
113 base::FilePath hook_dll_path(sandbox::g_hook_dll_file);
114 base::ScopedNativeLibrary dll(hook_dll_path);
115 EXPECT_TRUE(dll.is_valid());
116
117 HOOKPROC hook_proc = reinterpret_cast<HOOKPROC>(
118 dll.GetFunctionPointer(sandbox::g_hook_handler_func));
119 WasHookCalledFunction was_hook_called =
120 reinterpret_cast<WasHookCalledFunction>(
121 dll.GetFunctionPointer(sandbox::g_was_hook_called_func));
122 SetHookFunction set_hook = reinterpret_cast<SetHookFunction>(
123 dll.GetFunctionPointer(sandbox::g_set_hook_func));
124 if (!hook_proc || !was_hook_called || !set_hook) {
125 ADD_FAILURE();
126 all_good = false;
127 }
128
129 // 3. Try installing the hook (either on a remote target thread,
130 // or globally).
131 HHOOK hook = nullptr;
132 if (all_good) {
133 DWORD target = 0;
134 if (!global_hook)
135 target = proc_info.dwThreadId;
136 hook = ::SetWindowsHookExW(WH_KEYBOARD, hook_proc, dll.get(), target);
137 if (!hook) {
138 ADD_FAILURE();
139 all_good = false;
140 } else
141 // Pass the hook DLL the hook handle.
142 set_hook(hook);
143 }
144
145 // 4. Inject a keyboard event.
146 if (all_good) {
147 // Note: that PostThreadMessage and SendMessage APIs will not deliver
148 // a keystroke in such a way that triggers a "legitimate" hook.
149 // Have to use targetless SendInput or keybd_event. The latter is
150 // less code and easier to work with.
151 keybd_event(VkKeyScan(L'A'), 0, 0, 0);
152 keybd_event(VkKeyScan(L'A'), 0, KEYEVENTF_KEYUP, 0);
153 // Give it a chance to hit the hook handler...
154 ::WaitForSingleObject(hook_event, sandbox::SboxTestEventTimeout());
155
156 // 5. Did the hook get hit? Was it expected to?
157 if (global_hook)
158 EXPECT_EQ((is_success_test ? true : false), was_hook_called());
159 else
160 // ***IMPORTANT: when targeting a specific thread id, the
161 // PROCESS_CREATION_MITIGATION_POLICY_EXTENSION_POINT_DISABLE
162 // mitigation does NOT disable the hook API. It ONLY
163 // stops global hooks from running in a process. Hence,
164 // the hook will hit (TRUE) even in the "failure"
165 // case for a non-global/targeted hook.
166 EXPECT_EQ((is_success_test ? true : true), was_hook_called());
167 }
168
169 // 6. Disable hook.
170 if (hook)
171 EXPECT_TRUE(::UnhookWindowsHookEx(hook));
172
173 // 7. Trigger shutdown of WinProc.
174 if (proc_info.hProcess) {
175 if (::PostThreadMessageW(proc_info.dwThreadId, WM_QUIT, 0, 0)) {
176 // Note: The combination/perfect-storm of a Global Hook, in a
177 // WinProc that has the EXTENSION_POINT_DISABLE mitigation ON, and the
178 // use of the SendInput or keybd_event API to inject a keystroke,
179 // results in the target becoming unresponsive. If any one of these
180 // states are changed, the problem does not occur. This means the WM_QUIT
181 // message is not handled and the call to WaitForSingleObject times out.
182 // Therefore not checking the return val.
183 ::WaitForSingleObject(winproc_event, sandbox::SboxTestEventTimeout());
184 } else {
185 // Ensure no strays.
186 ::TerminateProcess(proc_info.hProcess, 0);
187 ADD_FAILURE();
188 }
189 EXPECT_TRUE(::CloseHandle(proc_info.hThread));
190 EXPECT_TRUE(::CloseHandle(proc_info.hProcess));
191 }
192 }
193
194 //------------------------------------------------------------------------------
195 // ExtensionPoint test helper function.
196 //
197 // 1. Set up the AppInit Dll in registry settings. (Enable)
198 // 2. Spawn a Windows process (with or without mitigation enabled).
199 // 3. Check if the AppInit Dll got loaded in the Windows process or not.
200 // 4. Signal the Windows process to shutdown.
201 // 5. Restore original reg settings.
202 //
203 // Do NOT use any ASSERTs in this function. Cleanup required.
204 //------------------------------------------------------------------------------
205 void TestWin8ExtensionPointAppInitWrapper(bool is_success_test) {
206 // 0.5 Get path of current module. The appropriate build of the
207 // AppInit DLL will be in the same directory (and the
208 // full path is needed for reg).
209 wchar_t path[MAX_PATH];
210 if (!::GetModuleFileNameW(NULL, path, MAX_PATH)) {
211 ADD_FAILURE();
212 return;
213 }
214 // Only want the directory. Switch file name for the AppInit DLL.
215 base::FilePath full_dll_path(path);
216 full_dll_path = full_dll_path.DirName();
217 full_dll_path = full_dll_path.Append(sandbox::g_hook_dll_file);
218 wchar_t* non_const = const_cast<wchar_t*>(full_dll_path.value().c_str());
219 // Now make sure the path is in "short-name" form for registry.
220 DWORD length = ::GetShortPathNameW(non_const, NULL, 0);
221 std::vector<wchar_t> short_name(length);
222 if (!::GetShortPathNameW(non_const, &short_name[0], length)) {
223 ADD_FAILURE();
224 return;
225 }
226
227 // 1. Reg setup.
228 const wchar_t* app_init_reg_path =
229 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows";
230 const wchar_t* dlls_value_name = L"AppInit_DLLs";
231 const wchar_t* enabled_value_name = L"LoadAppInit_DLLs";
232 const wchar_t* signing_value_name = L"RequireSignedAppInit_DLLs";
233 std::wstring orig_dlls;
234 std::wstring new_dlls;
235 DWORD orig_enabled_value = 0;
236 DWORD orig_signing_value = 0;
237 base::win::RegKey app_init_key(HKEY_LOCAL_MACHINE, app_init_reg_path,
238 KEY_QUERY_VALUE | KEY_SET_VALUE);
239 // Backup the existing settings.
240 if (!app_init_key.Valid() || !app_init_key.HasValue(dlls_value_name) ||
241 !app_init_key.HasValue(enabled_value_name) ||
242 ERROR_SUCCESS != app_init_key.ReadValue(dlls_value_name, &orig_dlls) ||
243 ERROR_SUCCESS !=
244 app_init_key.ReadValueDW(enabled_value_name, &orig_enabled_value)) {
245 ADD_FAILURE();
246 return;
247 }
248 if (app_init_key.HasValue(signing_value_name)) {
249 if (ERROR_SUCCESS !=
250 app_init_key.ReadValueDW(signing_value_name, &orig_signing_value)) {
251 ADD_FAILURE();
252 return;
253 }
254 }
255
256 // Set the new settings (obviously requires local admin privileges).
257 new_dlls = orig_dlls;
258 if (!orig_dlls.empty())
259 new_dlls.append(L",");
260 new_dlls.append(short_name.data());
261
262 // From this point on, no return on failure. Cleanup required.
263 bool all_good = true;
264
265 if (app_init_key.HasValue(signing_value_name)) {
266 if (ERROR_SUCCESS !=
267 app_init_key.WriteValue(signing_value_name, static_cast<DWORD>(0))) {
268 ADD_FAILURE();
269 all_good = false;
270 }
271 }
272 if (ERROR_SUCCESS !=
273 app_init_key.WriteValue(dlls_value_name, new_dlls.c_str()) ||
274 ERROR_SUCCESS !=
275 app_init_key.WriteValue(enabled_value_name, static_cast<DWORD>(1))) {
276 ADD_FAILURE();
277 all_good = false;
278 }
279
280 // 2. Spawn WinProc.
281 HANDLE winproc_event = INVALID_HANDLE_VALUE;
282 base::win::ScopedHandle scoped_event;
283 PROCESS_INFORMATION proc_info = {};
284 if (all_good) {
285 winproc_event =
286 ::CreateEventW(NULL, FALSE, FALSE, sandbox::g_winproc_event);
287 if (winproc_event == NULL || winproc_event == INVALID_HANDLE_VALUE) {
288 ADD_FAILURE();
289 all_good = false;
290 } else {
291 scoped_event.Set(winproc_event);
292 if (!SpawnWinProc(&proc_info, is_success_test, &winproc_event))
293 all_good = false;
294 }
295 }
296
297 // 3. Check loaded modules in WinProc to see if the AppInit dll is loaded.
298 bool dll_loaded = false;
299 if (all_good) {
300 std::vector<HMODULE>(modules);
301 if (!base::win::GetLoadedModulesSnapshot(proc_info.hProcess, &modules)) {
302 ADD_FAILURE();
303 all_good = false;
304 } else {
305 for (HMODULE module : modules) {
306 wchar_t name[MAX_PATH] = {};
307 if (::GetModuleFileNameExW(proc_info.hProcess, module, name,
308 MAX_PATH) &&
309 ::wcsstr(name, sandbox::g_hook_dll_file)) {
310 // Found it.
311 dll_loaded = true;
312 break;
313 }
314 }
315 }
316 }
317
318 // Was the test result as expected?
319 if (all_good)
320 EXPECT_EQ((is_success_test ? true : false), dll_loaded);
321
322 // 4. Trigger shutdown of WinProc.
323 if (proc_info.hProcess) {
324 if (::PostThreadMessageW(proc_info.dwThreadId, WM_QUIT, 0, 0)) {
325 ::WaitForSingleObject(winproc_event, sandbox::SboxTestEventTimeout());
326 } else {
327 // Ensure no strays.
328 ::TerminateProcess(proc_info.hProcess, 0);
329 ADD_FAILURE();
330 }
331 EXPECT_TRUE(::CloseHandle(proc_info.hThread));
332 EXPECT_TRUE(::CloseHandle(proc_info.hProcess));
333 }
334
335 // 5. Reg Restore
336 EXPECT_EQ(ERROR_SUCCESS,
337 app_init_key.WriteValue(enabled_value_name, orig_enabled_value));
338 if (app_init_key.HasValue(signing_value_name))
339 EXPECT_EQ(ERROR_SUCCESS,
340 app_init_key.WriteValue(signing_value_name, orig_signing_value));
341 EXPECT_EQ(ERROR_SUCCESS,
342 app_init_key.WriteValue(dlls_value_name, orig_dlls.c_str()));
343 }
344
345 } // namespace
346
347 namespace sandbox {
348
349 const wchar_t g_extension_point_test_mutex[] = L"ChromeExtensionTestMutex";
grt (UTC plus 2) 2017/06/15 20:58:19 can these globals be in an unnamed namespace so th
Will Harris 2017/06/15 22:32:13 they only seem to be used in one place, so could j
350 const wchar_t g_hook_dll_file[] = L"sbox_integration_test_hook_dll.dll";
351 const char g_hook_handler_func[] = "HookProc";
352 const char g_was_hook_called_func[] = "WasHookCalled";
353 const char g_set_hook_func[] = "SetHook";
354
355 //------------------------------------------------------------------------------
356 // Exported Extension Point Tests
357 //------------------------------------------------------------------------------
358
359 //------------------------------------------------------------------------------
360 // Disable extension points (MITIGATION_EXTENSION_POINT_DISABLE).
361 // >= Win8
362 //------------------------------------------------------------------------------
363
364 // This test validates that setting the MITIGATION_EXTENSION_POINT_DISABLE
365 // mitigation enables the setting on a process.
366 TEST(ProcessMitigationsTest, CheckWin8ExtensionPointPolicySuccess) {
367 if (base::win::GetVersion() < base::win::VERSION_WIN8)
368 return;
369
370 std::wstring test_command = L"CheckPolicy ";
371 test_command += std::to_wstring(sandbox::TESTPOLICY_EXTENSIONPOINT);
372
373 //---------------------------------
374 // 1) Test setting pre-startup.
375 //---------------------------------
376 TestRunner runner;
377 sandbox::TargetPolicy* policy = runner.GetPolicy();
378
379 EXPECT_EQ(policy->SetProcessMitigations(MITIGATION_EXTENSION_POINT_DISABLE),
380 SBOX_ALL_OK);
381 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(test_command.c_str()));
382
383 //---------------------------------
384 // 2) Test setting post-startup.
385 //---------------------------------
386 TestRunner runner2;
387 sandbox::TargetPolicy* policy2 = runner2.GetPolicy();
388
389 EXPECT_EQ(
390 policy2->SetDelayedProcessMitigations(MITIGATION_EXTENSION_POINT_DISABLE),
391 SBOX_ALL_OK);
392 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner2.RunTest(test_command.c_str()));
393 }
394
395 // This test validates that a "legitimate" global hook CAN be set on the
396 // sandboxed proc/thread if the MITIGATION_EXTENSION_POINT_DISABLE
397 // mitigation is not set.
398 //
399 // MANUAL testing only.
400 TEST(ProcessMitigationsTest,
401 DISABLED_CheckWin8ExtensionPoint_GlobalHook_Success) {
402 if (base::win::GetVersion() < base::win::VERSION_WIN8)
403 return;
404
405 HANDLE mutex = ::CreateMutexW(NULL, FALSE, g_extension_point_test_mutex);
406 EXPECT_TRUE(mutex != NULL);
407 EXPECT_EQ(WAIT_OBJECT_0,
408 ::WaitForSingleObject(mutex, SboxTestEventTimeout()));
409
410 // (is_success_test, global_hook)
411 TestWin8ExtensionPointHookWrapper(true, true);
412
413 EXPECT_TRUE(::ReleaseMutex(mutex));
414 EXPECT_TRUE(::CloseHandle(mutex));
415 }
416
417 // This test validates that setting the MITIGATION_EXTENSION_POINT_DISABLE
418 // mitigation prevents a global hook on WinProc.
419 //
420 // MANUAL testing only.
421 TEST(ProcessMitigationsTest,
422 DISABLED_CheckWin8ExtensionPoint_GlobalHook_Failure) {
423 if (base::win::GetVersion() < base::win::VERSION_WIN8)
424 return;
425
426 HANDLE mutex = ::CreateMutexW(NULL, FALSE, g_extension_point_test_mutex);
427 EXPECT_TRUE(mutex != NULL);
428 EXPECT_EQ(WAIT_OBJECT_0,
429 ::WaitForSingleObject(mutex, SboxTestEventTimeout()));
430
431 // (is_success_test, global_hook)
432 TestWin8ExtensionPointHookWrapper(false, true);
433
434 EXPECT_TRUE(::ReleaseMutex(mutex));
435 EXPECT_TRUE(::CloseHandle(mutex));
436 }
437
438 // This test validates that a "legitimate" hook CAN be set on the sandboxed
439 // proc/thread if the MITIGATION_EXTENSION_POINT_DISABLE mitigation is not set.
440 //
441 // MANUAL testing only.
442 TEST(ProcessMitigationsTest, DISABLED_CheckWin8ExtensionPoint_Hook_Success) {
443 if (base::win::GetVersion() < base::win::VERSION_WIN8)
444 return;
445
446 HANDLE mutex = ::CreateMutexW(NULL, FALSE, g_extension_point_test_mutex);
447 EXPECT_TRUE(mutex != NULL);
448 EXPECT_EQ(WAIT_OBJECT_0,
449 ::WaitForSingleObject(mutex, SboxTestEventTimeout()));
450
451 // (is_success_test, global_hook)
452 TestWin8ExtensionPointHookWrapper(true, false);
453
454 EXPECT_TRUE(::ReleaseMutex(mutex));
455 EXPECT_TRUE(::CloseHandle(mutex));
456 }
457
458 // *** Important: MITIGATION_EXTENSION_POINT_DISABLE does NOT prevent
459 // hooks targetted at a specific thread id. It only prevents
460 // global hooks. So this test does NOT actually expect the hook
461 // to fail (see TestWin8ExtensionPointHookWrapper function) even
462 // with the mitigation on.
463 //
464 // MANUAL testing only.
465 TEST(ProcessMitigationsTest, DISABLED_CheckWin8ExtensionPoint_Hook_Failure) {
466 if (base::win::GetVersion() < base::win::VERSION_WIN8)
467 return;
468
469 HANDLE mutex = ::CreateMutexW(NULL, FALSE, g_extension_point_test_mutex);
470 EXPECT_TRUE(mutex != NULL);
471 EXPECT_EQ(WAIT_OBJECT_0,
472 ::WaitForSingleObject(mutex, SboxTestEventTimeout()));
473
474 // (is_success_test, global_hook)
475 TestWin8ExtensionPointHookWrapper(false, false);
476
477 EXPECT_TRUE(::ReleaseMutex(mutex));
478 EXPECT_TRUE(::CloseHandle(mutex));
479 }
480
481 // This test validates that an AppInit Dll CAN be added to a target
482 // WinProc if the MITIGATION_EXTENSION_POINT_DISABLE mitigation is not set.
483 //
484 // MANUAL testing only.
485 // Must run this test as admin/elevated.
486 TEST(ProcessMitigationsTest, DISABLED_CheckWin8ExtensionPoint_AppInit_Success) {
487 if (base::win::GetVersion() < base::win::VERSION_WIN8)
488 return;
489
490 HANDLE mutex = ::CreateMutexW(NULL, FALSE, g_extension_point_test_mutex);
491 EXPECT_TRUE(mutex != NULL);
492 EXPECT_EQ(WAIT_OBJECT_0,
493 ::WaitForSingleObject(mutex, SboxTestEventTimeout()));
494
495 TestWin8ExtensionPointAppInitWrapper(true);
496
497 EXPECT_TRUE(::ReleaseMutex(mutex));
498 EXPECT_TRUE(::CloseHandle(mutex));
499 }
500
501 // This test validates that setting the MITIGATION_EXTENSION_POINT_DISABLE
502 // mitigation prevents the loading of any AppInit Dll into WinProc.
503 //
504 // MANUAL testing only.
505 // Must run this test as admin/elevated.
506 TEST(ProcessMitigationsTest, DISABLED_CheckWin8ExtensionPoint_AppInit_Failure) {
507 if (base::win::GetVersion() < base::win::VERSION_WIN8)
508 return;
509
510 HANDLE mutex = ::CreateMutexW(NULL, FALSE, g_extension_point_test_mutex);
511 EXPECT_TRUE(mutex != NULL);
512 EXPECT_EQ(WAIT_OBJECT_0,
513 ::WaitForSingleObject(mutex, SboxTestEventTimeout()));
514
515 TestWin8ExtensionPointAppInitWrapper(false);
516
517 EXPECT_TRUE(::ReleaseMutex(mutex));
518 EXPECT_TRUE(::CloseHandle(mutex));
519 }
520
521 } // namespace sandbox
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698