michael@0: // Copyright (c) 2011 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/strings/stringprintf.h" michael@0: #include "base/win/scoped_handle.h" michael@0: #include "sandbox/win/src/handle_closer_agent.h" michael@0: #include "sandbox/win/src/sandbox.h" michael@0: #include "sandbox/win/src/sandbox_factory.h" michael@0: #include "sandbox/win/src/target_services.h" michael@0: #include "sandbox/win/tests/common/controller.h" michael@0: #include "testing/gtest/include/gtest/gtest.h" michael@0: michael@0: namespace { michael@0: michael@0: const wchar_t *kFileExtensions[] = { L".1", L".2", L".3", L".4" }; michael@0: michael@0: // Returns a handle to a unique marker file that can be retrieved between runs. michael@0: HANDLE GetMarkerFile(const wchar_t *extension) { michael@0: wchar_t path_buffer[MAX_PATH + 1]; michael@0: CHECK(::GetTempPath(MAX_PATH, path_buffer)); michael@0: string16 marker_path = path_buffer; michael@0: marker_path += L"\\sbox_marker_"; michael@0: michael@0: // Generate a unique value from the exe's size and timestamp. michael@0: CHECK(::GetModuleFileName(NULL, path_buffer, MAX_PATH)); michael@0: base::win::ScopedHandle module(::CreateFile(path_buffer, michael@0: FILE_READ_ATTRIBUTES, FILE_SHARE_READ, NULL, michael@0: OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); michael@0: CHECK(module.IsValid()); michael@0: FILETIME timestamp; michael@0: CHECK(::GetFileTime(module, ×tamp, NULL, NULL)); michael@0: marker_path += base::StringPrintf(L"%08x%08x%08x", michael@0: ::GetFileSize(module, NULL), michael@0: timestamp.dwLowDateTime, michael@0: timestamp.dwHighDateTime); michael@0: marker_path += extension; michael@0: michael@0: // Make the file delete-on-close so cleanup is automatic. michael@0: return CreateFile(marker_path.c_str(), FILE_ALL_ACCESS, michael@0: FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, michael@0: NULL, OPEN_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL); michael@0: } michael@0: michael@0: // Used by the thread pool tests. michael@0: HANDLE finish_event; michael@0: const int kWaitCount = 20; michael@0: michael@0: } // namespace michael@0: michael@0: namespace sandbox { michael@0: michael@0: // Checks for the presence of a list of files (in object path form). michael@0: // Format: CheckForFileHandle (Y|N) \path\to\file1 [\path\to\file2 ...] michael@0: // - Y or N depending if the file should exist or not. michael@0: SBOX_TESTS_COMMAND int CheckForFileHandles(int argc, wchar_t **argv) { michael@0: if (argc < 2) michael@0: return SBOX_TEST_FAILED_TO_RUN_TEST; michael@0: bool should_find = argv[0][0] == L'Y'; michael@0: if (argv[0][1] != L'\0' || !should_find && argv[0][0] != L'N') michael@0: return SBOX_TEST_FAILED_TO_RUN_TEST; michael@0: michael@0: static int state = BEFORE_INIT; michael@0: switch (state++) { michael@0: case BEFORE_INIT: michael@0: // Create a unique marker file that is open while the test is running. michael@0: // The handles leak, but it will be closed by the test or on exit. michael@0: for (int i = 0; i < arraysize(kFileExtensions); ++i) michael@0: EXPECT_NE(GetMarkerFile(kFileExtensions[i]), INVALID_HANDLE_VALUE); michael@0: return SBOX_TEST_SUCCEEDED; michael@0: michael@0: case AFTER_REVERT: { michael@0: // Brute force the handle table to find what we're looking for. michael@0: DWORD handle_count = UINT_MAX; michael@0: const int kInvalidHandleThreshold = 100; michael@0: const size_t kHandleOffset = sizeof(HANDLE); michael@0: HANDLE handle = NULL; michael@0: int invalid_count = 0; michael@0: string16 handle_name; michael@0: michael@0: if (!::GetProcessHandleCount(::GetCurrentProcess(), &handle_count)) michael@0: return SBOX_TEST_FAILED_TO_RUN_TEST; michael@0: michael@0: while (handle_count && invalid_count < kInvalidHandleThreshold) { michael@0: reinterpret_cast(handle) += kHandleOffset; michael@0: if (GetHandleName(handle, &handle_name)) { michael@0: for (int i = 1; i < argc; ++i) { michael@0: if (handle_name == argv[i]) michael@0: return should_find ? SBOX_TEST_SUCCEEDED : SBOX_TEST_FAILED; michael@0: } michael@0: --handle_count; michael@0: } else { michael@0: ++invalid_count; michael@0: } michael@0: } michael@0: michael@0: return should_find ? SBOX_TEST_FAILED : SBOX_TEST_SUCCEEDED; michael@0: } michael@0: michael@0: default: // Do nothing. michael@0: break; michael@0: } michael@0: michael@0: return SBOX_TEST_SUCCEEDED; michael@0: } michael@0: michael@0: TEST(HandleCloserTest, CheckForMarkerFiles) { michael@0: TestRunner runner; michael@0: runner.SetTimeout(2000); michael@0: runner.SetTestState(EVERY_STATE); michael@0: sandbox::TargetPolicy* policy = runner.GetPolicy(); michael@0: michael@0: string16 command = string16(L"CheckForFileHandles Y"); michael@0: for (int i = 0; i < arraysize(kFileExtensions); ++i) { michael@0: string16 handle_name; michael@0: base::win::ScopedHandle marker(GetMarkerFile(kFileExtensions[i])); michael@0: CHECK(marker.IsValid()); michael@0: CHECK(sandbox::GetHandleName(marker, &handle_name)); michael@0: command += (L" "); michael@0: command += handle_name; michael@0: } michael@0: michael@0: EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(command.c_str())) << michael@0: "Failed: " << command; michael@0: } michael@0: michael@0: TEST(HandleCloserTest, CloseMarkerFiles) { michael@0: TestRunner runner; michael@0: runner.SetTimeout(2000); michael@0: runner.SetTestState(EVERY_STATE); michael@0: sandbox::TargetPolicy* policy = runner.GetPolicy(); michael@0: michael@0: string16 command = string16(L"CheckForFileHandles N"); michael@0: for (int i = 0; i < arraysize(kFileExtensions); ++i) { michael@0: string16 handle_name; michael@0: base::win::ScopedHandle marker(GetMarkerFile(kFileExtensions[i])); michael@0: CHECK(marker.IsValid()); michael@0: CHECK(sandbox::GetHandleName(marker, &handle_name)); michael@0: CHECK_EQ(policy->AddKernelObjectToClose(L"File", handle_name.c_str()), michael@0: SBOX_ALL_OK); michael@0: command += (L" "); michael@0: command += handle_name; michael@0: } michael@0: michael@0: EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(command.c_str())) << michael@0: "Failed: " << command; michael@0: } michael@0: michael@0: void WINAPI ThreadPoolTask(void* event, BOOLEAN timeout) { michael@0: static volatile LONG waiters_remaining = kWaitCount; michael@0: CHECK(!timeout); michael@0: CHECK(::CloseHandle(event)); michael@0: if (::InterlockedDecrement(&waiters_remaining) == 0) michael@0: CHECK(::SetEvent(finish_event)); michael@0: } michael@0: michael@0: // Run a thread pool inside a sandbox without a CSRSS connection. michael@0: SBOX_TESTS_COMMAND int RunThreadPool(int argc, wchar_t **argv) { michael@0: HANDLE wait_list[20]; michael@0: CHECK(finish_event = ::CreateEvent(NULL, TRUE, FALSE, NULL)); michael@0: michael@0: // Set up a bunch of waiters. michael@0: HANDLE pool = NULL; michael@0: for (int i = 0; i < kWaitCount; ++i) { michael@0: HANDLE event = ::CreateEvent(NULL, TRUE, FALSE, NULL); michael@0: CHECK(event); michael@0: CHECK(::RegisterWaitForSingleObject(&pool, event, ThreadPoolTask, event, michael@0: INFINITE, WT_EXECUTEONLYONCE)); michael@0: wait_list[i] = event; michael@0: } michael@0: michael@0: // Signal all the waiters. michael@0: for (int i = 0; i < kWaitCount; ++i) michael@0: CHECK(::SetEvent(wait_list[i])); michael@0: michael@0: CHECK_EQ(::WaitForSingleObject(finish_event, INFINITE), WAIT_OBJECT_0); michael@0: CHECK(::CloseHandle(finish_event)); michael@0: michael@0: return SBOX_TEST_SUCCEEDED; michael@0: } michael@0: michael@0: TEST(HandleCloserTest, RunThreadPool) { michael@0: TestRunner runner; michael@0: runner.SetTimeout(2000); michael@0: runner.SetTestState(AFTER_REVERT); michael@0: sandbox::TargetPolicy* policy = runner.GetPolicy(); michael@0: michael@0: // Sever the CSRSS connection by closing ALPC ports inside the sandbox. michael@0: CHECK_EQ(policy->AddKernelObjectToClose(L"ALPC Port", NULL), SBOX_ALL_OK); michael@0: michael@0: EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"RunThreadPool")); michael@0: } michael@0: michael@0: } // namespace sandbox