OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome_elf/create_file/chrome_create_file.h" | 5 #include "chrome_elf/create_file/chrome_create_file.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include <bitset> | 9 #include <bitset> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/base_paths_win.h" | 12 #include "base/base_paths_win.h" |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
17 #include "base/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
18 #include "base/win/iat_patch_function.h" | 18 #include "base/win/iat_patch_function.h" |
19 #include "base/win/scoped_handle.h" | 19 #include "base/win/scoped_handle.h" |
20 #include "base/win/windows_version.h" | 20 #include "base/win/windows_version.h" |
21 #include "chrome_elf/chrome_elf_constants.h" | 21 #include "chrome_elf/chrome_elf_constants.h" |
22 #include "chrome_elf/ntdll_cache.h" | 22 #include "chrome_elf/ntdll_cache.h" |
| 23 #include "sandbox/win/src/interception_internal.h" |
23 #include "sandbox/win/src/nt_internals.h" | 24 #include "sandbox/win/src/nt_internals.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 #include "testing/platform_test.h" | 26 #include "testing/platform_test.h" |
26 | 27 |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 // Test fixtures ------------------------------------------------------------- | 31 // Test fixtures ------------------------------------------------------------- |
31 | 32 |
32 class ChromeCreateFileTest : public PlatformTest { | 33 class ChromeCreateFileTest : public PlatformTest { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 InitCache(); | 81 InitCache(); |
81 PlatformTest::SetUp(); | 82 PlatformTest::SetUp(); |
82 | 83 |
83 base::FilePath user_data_dir; | 84 base::FilePath user_data_dir; |
84 PathService::Get(base::DIR_LOCAL_APP_DATA, &user_data_dir); | 85 PathService::Get(base::DIR_LOCAL_APP_DATA, &user_data_dir); |
85 ASSERT_TRUE(temp_dir_.CreateUniqueTempDirUnderPath(user_data_dir)); | 86 ASSERT_TRUE(temp_dir_.CreateUniqueTempDirUnderPath(user_data_dir)); |
86 ASSERT_TRUE(temp_dir2_.CreateUniqueTempDir()); | 87 ASSERT_TRUE(temp_dir2_.CreateUniqueTempDir()); |
87 self_ = this; | 88 self_ = this; |
88 } | 89 } |
89 | 90 |
| 91 void UnsetThunkStorage() { |
| 92 DWORD old_protect = 0; |
| 93 EXPECT_TRUE(::VirtualProtect(&g_nt_thunk_storage, |
| 94 sizeof(g_nt_thunk_storage), |
| 95 PAGE_EXECUTE_READWRITE, |
| 96 &old_protect)); |
| 97 memset(&g_nt_thunk_storage, 0, sizeof(g_nt_thunk_storage)); |
| 98 |
| 99 EXPECT_TRUE(::VirtualProtect(&g_nt_thunk_storage, |
| 100 sizeof(g_nt_thunk_storage), |
| 101 PAGE_EXECUTE_READ, |
| 102 &old_protect)); |
| 103 } |
| 104 |
90 void RedirectNtCreateFileCalls() { | 105 void RedirectNtCreateFileCalls() { |
| 106 UnsetThunkStorage(); |
91 old_func_ptr_ = | 107 old_func_ptr_ = |
92 reinterpret_cast<NtCreateFileFunction>(g_ntdll_lookup["NtCreateFile"]); | 108 reinterpret_cast<NtCreateFileFunction>(g_ntdll_lookup["NtCreateFile"]); |
93 | 109 |
94 // KernelBase.dll only exists for Win7 and later, prior to that, kernel32 | 110 // KernelBase.dll only exists for Win7 and later, prior to that, kernel32 |
95 // imports from ntdll directly. | 111 // imports from ntdll directly. |
96 if (base::win::GetVersion() < base::win::VERSION_WIN7) { | 112 if (base::win::GetVersion() < base::win::VERSION_WIN7) { |
97 patcher_.Patch(L"kernel32.dll", "ntdll.dll", "NtCreateFile", | 113 patcher_.Patch(L"kernel32.dll", "ntdll.dll", "NtCreateFile", |
98 reinterpret_cast<void(*)()>(&FakeNtCreateFile<KERNEL>)); | 114 reinterpret_cast<void(*)()>(&FakeNtCreateFile<KERNEL>)); |
99 } else { | 115 } else { |
100 patcher_.Patch(L"kernelbase.dll", "ntdll.dll", "NtCreateFile", | 116 patcher_.Patch(L"kernelbase.dll", "ntdll.dll", "NtCreateFile", |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 // Don't redirect if file is not in UserData directory. | 385 // Don't redirect if file is not in UserData directory. |
370 EXPECT_FALSE(ShouldBypass(desktop_junk_path.value().c_str())); | 386 EXPECT_FALSE(ShouldBypass(desktop_junk_path.value().c_str())); |
371 EXPECT_FALSE(ShouldBypass(desktop_prefs_path.value().c_str())); | 387 EXPECT_FALSE(ShouldBypass(desktop_prefs_path.value().c_str())); |
372 | 388 |
373 // Only redirect "Preferences" and "Local State" files. | 389 // Only redirect "Preferences" and "Local State" files. |
374 EXPECT_TRUE(ShouldBypass(local_prefs_path.value().c_str())); | 390 EXPECT_TRUE(ShouldBypass(local_prefs_path.value().c_str())); |
375 EXPECT_TRUE(ShouldBypass(local_state_path.value().c_str())); | 391 EXPECT_TRUE(ShouldBypass(local_state_path.value().c_str())); |
376 EXPECT_FALSE(ShouldBypass(local_junk_path.value().c_str())); | 392 EXPECT_FALSE(ShouldBypass(local_junk_path.value().c_str())); |
377 } | 393 } |
378 | 394 |
379 TEST_F(ChromeCreateFileTest, NtCreateFileAddressCheck) { | |
380 HMODULE ntdll_handle = ::GetModuleHandle(L"ntdll.dll"); | |
381 EXPECT_EQ(::GetProcAddress(ntdll_handle, "NtCreateFile"), | |
382 g_ntdll_lookup["NtCreateFile"]); | |
383 } | |
384 | |
385 TEST_F(ChromeCreateFileTest, ReadWriteFromNtDll) { | 395 TEST_F(ChromeCreateFileTest, ReadWriteFromNtDll) { |
| 396 UnsetThunkStorage(); |
386 base::FilePath file_name = temp_dir_.path().Append(L"some_file.txt"); | 397 base::FilePath file_name = temp_dir_.path().Append(L"some_file.txt"); |
387 DoWriteCheck(file_name, FILE_ATTRIBUTE_NORMAL, false); | 398 DoWriteCheck(file_name, FILE_ATTRIBUTE_NORMAL, false); |
388 DoReadCheck(file_name, FILE_ATTRIBUTE_NORMAL, false); | 399 DoReadCheck(file_name, FILE_ATTRIBUTE_NORMAL, false); |
| 400 } |
| 401 |
| 402 TEST_F(ChromeCreateFileTest, ReadWriteFromThunk) { |
| 403 base::FilePath file_name = temp_dir_.path().Append(L"some_file.txt"); |
| 404 DoWriteCheck(file_name, FILE_ATTRIBUTE_NORMAL, false); |
| 405 DoReadCheck(file_name, FILE_ATTRIBUTE_NORMAL, false); |
389 } | 406 } |
390 | 407 |
391 } // namespace | 408 } // namespace |
OLD | NEW |