OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <memory> | 5 #include <memory> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
10 #include "base/win/scoped_handle.h" | 10 #include "base/win/scoped_handle.h" |
11 #include "base/win/scoped_process_information.h" | 11 #include "base/win/scoped_process_information.h" |
12 #include "base/win/windows_version.h" | 12 #include "base/win/windows_version.h" |
| 13 #include "sandbox/win/src/process_thread_interception.h" |
13 #include "sandbox/win/src/sandbox.h" | 14 #include "sandbox/win/src/sandbox.h" |
14 #include "sandbox/win/src/sandbox_factory.h" | 15 #include "sandbox/win/src/sandbox_factory.h" |
15 #include "sandbox/win/src/sandbox_policy.h" | 16 #include "sandbox/win/src/sandbox_policy.h" |
16 #include "sandbox/win/tests/common/controller.h" | 17 #include "sandbox/win/tests/common/controller.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 // While the shell API provides better calls than this home brew function | 22 // While the shell API provides better calls than this home brew function |
22 // we use GetSystemWindowsDirectoryW which does not query the registry so | 23 // we use GetSystemWindowsDirectoryW which does not query the registry so |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 return SBOX_TEST_DENIED; | 252 return SBOX_TEST_DENIED; |
252 } | 253 } |
253 } else { | 254 } else { |
254 ::CloseHandle(token); | 255 ::CloseHandle(token); |
255 return SBOX_TEST_SUCCEEDED; | 256 return SBOX_TEST_SUCCEEDED; |
256 } | 257 } |
257 | 258 |
258 return SBOX_TEST_FAILED; | 259 return SBOX_TEST_FAILED; |
259 } | 260 } |
260 | 261 |
| 262 // Generate a event name, used to test thread creation. |
| 263 std::wstring GenerateEventName(DWORD pid) { |
| 264 wchar_t buff[30] = {0}; |
| 265 int res = swprintf_s(buff, sizeof(buff) / sizeof(buff[0]), |
| 266 L"ProcessPolicyTest_%08x", pid); |
| 267 if (-1 != res) { |
| 268 return std::wstring(buff); |
| 269 } |
| 270 return std::wstring(); |
| 271 } |
| 272 |
| 273 // This is the function that is called when testing thread creation. |
| 274 // It is expected to set an event that the caller is waiting on. |
| 275 DWORD TestThreadFunc(LPVOID lpdwThreadParam) { |
| 276 std::wstring event_name = GenerateEventName((DWORD)lpdwThreadParam); |
| 277 if (!event_name.length()) { |
| 278 return 1; |
| 279 } |
| 280 HANDLE event = ::OpenEvent(EVENT_ALL_ACCESS | EVENT_MODIFY_STATE, FALSE, |
| 281 event_name.c_str()); |
| 282 if (!event) { |
| 283 return 1; |
| 284 } |
| 285 if (!SetEvent(event)) { |
| 286 return 1; |
| 287 } |
| 288 return 0; |
| 289 } |
| 290 |
| 291 SBOX_TESTS_COMMAND int Process_CreateThread(int argc, wchar_t** argv) { |
| 292 DWORD pid = ::GetCurrentProcessId(); |
| 293 std::wstring event_name = GenerateEventName(pid); |
| 294 if (!event_name.length()) { |
| 295 return SBOX_TEST_FIRST_ERROR; |
| 296 } |
| 297 HANDLE event = ::CreateEvent(NULL, TRUE, FALSE, event_name.c_str()); |
| 298 if (!event) { |
| 299 return SBOX_TEST_SECOND_ERROR; |
| 300 } |
| 301 |
| 302 DWORD thread_id = 0; |
| 303 HANDLE thread = NULL; |
| 304 thread = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&TestThreadFunc, |
| 305 (LPVOID)pid, 0, &thread_id); |
| 306 |
| 307 if (!thread) { |
| 308 return SBOX_TEST_THIRD_ERROR; |
| 309 } |
| 310 if (!thread_id) { |
| 311 return SBOX_TEST_FOURTH_ERROR; |
| 312 } |
| 313 if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0) { |
| 314 return SBOX_TEST_FIFTH_ERROR; |
| 315 } |
| 316 DWORD exit_code = 0; |
| 317 if (!GetExitCodeThread(thread, &exit_code)) { |
| 318 return SBOX_TEST_SIXTH_ERROR; |
| 319 } |
| 320 if (exit_code) { |
| 321 return SBOX_TEST_SEVENTH_ERROR; |
| 322 } |
| 323 if (WaitForSingleObject(event, INFINITE) != WAIT_OBJECT_0) { |
| 324 return SBOX_TEST_FAILED; |
| 325 } |
| 326 return SBOX_TEST_SUCCEEDED; |
| 327 } |
| 328 |
261 TEST(ProcessPolicyTest, TestAllAccess) { | 329 TEST(ProcessPolicyTest, TestAllAccess) { |
262 // Check if the "all access" rule fails to be added when the token is too | 330 // Check if the "all access" rule fails to be added when the token is too |
263 // powerful. | 331 // powerful. |
264 TestRunner runner; | 332 TestRunner runner; |
265 | 333 |
266 // Check the failing case. | 334 // Check the failing case. |
267 runner.GetPolicy()->SetTokenLevel(USER_INTERACTIVE, USER_LOCKDOWN); | 335 runner.GetPolicy()->SetTokenLevel(USER_INTERACTIVE, USER_LOCKDOWN); |
268 EXPECT_EQ(SBOX_ERROR_UNSUPPORTED, | 336 EXPECT_EQ(SBOX_ERROR_UNSUPPORTED, |
269 runner.GetPolicy()->AddRule(TargetPolicy::SUBSYS_PROCESS, | 337 runner.GetPolicy()->AddRule(TargetPolicy::SUBSYS_PROCESS, |
270 TargetPolicy::PROCESS_ALL_EXEC, | 338 TargetPolicy::PROCESS_ALL_EXEC, |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 base::string16 exe_path = MakeFullPathToSystem32(L"findstr.exe"); | 443 base::string16 exe_path = MakeFullPathToSystem32(L"findstr.exe"); |
376 ASSERT_TRUE(!exe_path.empty()); | 444 ASSERT_TRUE(!exe_path.empty()); |
377 EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_PROCESS, | 445 EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_PROCESS, |
378 TargetPolicy::PROCESS_ALL_EXEC, | 446 TargetPolicy::PROCESS_ALL_EXEC, |
379 exe_path.c_str())); | 447 exe_path.c_str())); |
380 | 448 |
381 EXPECT_EQ(SBOX_TEST_SUCCEEDED, | 449 EXPECT_EQ(SBOX_TEST_SUCCEEDED, |
382 runner.RunTest(L"Process_GetChildProcessToken findstr.exe")); | 450 runner.RunTest(L"Process_GetChildProcessToken findstr.exe")); |
383 } | 451 } |
384 | 452 |
| 453 // This tests that the CreateThread works with CSRSS not locked down. |
| 454 // In other words, that the interception passes through OK. |
| 455 TEST(ProcessPolicyTest, TestCreateThreadWithCsrss) { |
| 456 TestRunner runner(JOB_NONE, USER_INTERACTIVE, USER_INTERACTIVE); |
| 457 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Process_CreateThread")); |
| 458 } |
| 459 |
| 460 // This tests that the CreateThread works with CSRSS locked down. |
| 461 // In other words, that the interception correctly works. |
| 462 TEST(ProcessPolicyTest, TestCreateThreadWithoutCsrss) { |
| 463 TestRunner runner(JOB_NONE, USER_INTERACTIVE, USER_INTERACTIVE); |
| 464 |
| 465 EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_PROCESS, |
| 466 TargetPolicy::PROCESS_ALL_EXEC, |
| 467 L"this is not important")); |
| 468 |
| 469 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Process_CreateThread")); |
| 470 } |
| 471 |
| 472 // This tests that our CreateThread interceptors works when called directly. |
| 473 TEST(ProcessPolicyTest, TestCreateThreadOutsideSandbox) { |
| 474 DWORD pid = ::GetCurrentProcessId(); |
| 475 std::wstring event_name = GenerateEventName(pid); |
| 476 ASSERT_STRNE(NULL, event_name.c_str()); |
| 477 HANDLE event = ::CreateEvent(NULL, TRUE, FALSE, event_name.c_str()); |
| 478 EXPECT_NE(NULL, int(event)); |
| 479 |
| 480 DWORD thread_id = 0; |
| 481 HANDLE thread = NULL; |
| 482 thread = TargetCreateThread(::CreateThread, NULL, 0, |
| 483 (LPTHREAD_START_ROUTINE)&TestThreadFunc, |
| 484 (LPVOID)pid, 0, &thread_id); |
| 485 EXPECT_NE(NULL, int(thread)); |
| 486 EXPECT_EQ(WAIT_OBJECT_0, WaitForSingleObject(thread, INFINITE)); |
| 487 EXPECT_EQ(WAIT_OBJECT_0, WaitForSingleObject(event, INFINITE)); |
| 488 } |
| 489 |
385 } // namespace sandbox | 490 } // namespace sandbox |
OLD | NEW |