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

Side by Side Diff: chrome_elf/create_file/chrome_create_file_unittest.cc

Issue 138593004: Use an alternate mechanism for CreateFile calls in Chrome (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Flags, tests and some gyp-fu Created 6 years, 11 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome_elf/create_file/chrome_create_file.h"
6
7 #include <windows.h>
8
9 #include <bitset>
10 #include <string>
11
12 #include "base/base_paths_win.h"
13 #include "base/file_util.h"
14 #include "base/files/file_path.h"
15 #include "base/files/scoped_temp_dir.h"
16 #include "base/path_service.h"
17 #include "base/threading/platform_thread.h"
18 #include "base/win/iat_patch_function.h"
19 #include "base/win/scoped_handle.h"
20 #include "base/win/windows_version.h"
21 #include "chrome_elf/chrome_elf_constants.h"
22 #include "chrome_elf/ntdll_cache.h"
23 #include "sandbox/win/src/nt_internals.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "testing/platform_test.h"
26
27
28 namespace {
29
30 // Test fixtures -------------------------------------------------------------
31
32 class ChromeCreateFileTest : public PlatformTest {
33 protected:
34 struct NtCreateFileParams {
35 ACCESS_MASK desired_access;
36 OBJECT_ATTRIBUTES object_attributes;
37 PLARGE_INTEGER allocation_size;
38 ULONG file_attributes;
39 ULONG share_access;
40 ULONG create_disposition;
41 ULONG create_options;
42 PVOID ea_buffer;
43 ULONG ea_length;
44 };
45
46 enum CallPath {
47 ELF,
48 KERNEL
49 };
50
51 template<CallPath path>
52 static NTSTATUS WINAPI FakeNtCreateFile(
53 PHANDLE file_handle,
54 ACCESS_MASK desired_access,
55 POBJECT_ATTRIBUTES object_attributes,
56 PIO_STATUS_BLOCK io_status_block,
57 PLARGE_INTEGER allocation_size,
58 ULONG file_attributes,
59 ULONG share_access,
60 ULONG create_disposition,
61 ULONG create_options,
62 PVOID ea_buffer,
63 ULONG ea_length) {
64 return self_->HandleCreateFileCall(file_handle,
65 desired_access,
66 object_attributes,
67 io_status_block,
68 allocation_size,
69 file_attributes,
70 share_access,
71 create_disposition,
72 create_options,
73 ea_buffer,
74 ea_length,
75 path);
76 }
77
78 virtual void SetUp() OVERRIDE {
79 original_thread_ = base::PlatformThread::CurrentId();
80 InitCache();
81 PlatformTest::SetUp();
82
83 base::FilePath user_data_dir;
84 PathService::Get(base::DIR_LOCAL_APP_DATA, &user_data_dir);
85 ASSERT_TRUE(temp_dir_.CreateUniqueTempDirUnderPath(user_data_dir));
86 ASSERT_TRUE(temp_dir2_.CreateUniqueTempDir());
87 self_ = this;
88 }
89
90 void RedirectNtCreateFileCalls() {
91 old_func_ptr_ =
92 reinterpret_cast<NtCreateFileFunction>(g_ntdll_lookup["NtCreateFile"]);
93
94 // KernelBase.dll only exists for Win7 and later, prior to that, kernel32
95 // imports from ntdll directly.
96 if (base::win::GetVersion() < base::win::VERSION_WIN7) {
97 patcher_.Patch(L"kernel32.dll", "ntdll.dll", "NtCreateFile",
98 reinterpret_cast<void(*)()>(&FakeNtCreateFile<KERNEL>));
99 } else {
100 patcher_.Patch(L"kernelbase.dll", "ntdll.dll", "NtCreateFile",
101 reinterpret_cast<void(*)()>(&FakeNtCreateFile<KERNEL>));
102 }
103
104 g_ntdll_lookup["NtCreateFile"] = reinterpret_cast<void(*)()>(
105 &ChromeCreateFileTest::FakeNtCreateFile<ELF>);
106 }
107
108 void ResetNtCreateFileCalls() {
109 g_ntdll_lookup["NtCreateFile"] = reinterpret_cast<void*>(old_func_ptr_);
110 patcher_.Unpatch();
111 }
112
113 NTSTATUS HandleCreateFileCall(PHANDLE file_handle,
114 ACCESS_MASK desired_access,
115 POBJECT_ATTRIBUTES object_attributes,
116 PIO_STATUS_BLOCK io_status_block,
117 PLARGE_INTEGER allocation_size,
118 ULONG file_attributes,
119 ULONG share_access,
120 ULONG create_disposition,
121 ULONG create_options,
122 PVOID ea_buffer,
123 ULONG ea_length,
124 CallPath call_path) {
125 if (original_thread_ == base::PlatformThread::CurrentId()) {
126 SetParams(desired_access,
127 object_attributes,
128 allocation_size,
129 file_attributes,
130 share_access,
131 create_disposition,
132 create_options,
133 ea_buffer,
134 ea_length,
135 call_path == ELF ? &elf_params_ : &kernel_params_);
136 }
137
138 // Forward the call to the real NTCreateFile.
139 return old_func_ptr_(file_handle,
140 desired_access,
141 object_attributes,
142 io_status_block,
143 allocation_size,
144 file_attributes,
145 share_access,
146 create_disposition,
147 create_options,
148 ea_buffer,
149 ea_length);
150 }
151
152 void SetParams(ACCESS_MASK desired_access,
153 POBJECT_ATTRIBUTES object_attributes,
154 PLARGE_INTEGER allocation_size,
155 ULONG file_attributes,
156 ULONG share_access,
157 ULONG create_disposition,
158 ULONG create_options,
159 PVOID ea_buffer,
160 ULONG ea_length,
161 NtCreateFileParams* params) {
162 params->desired_access = desired_access;
163 params->object_attributes.Length = object_attributes->Length;
164 params->object_attributes.ObjectName = object_attributes->ObjectName;
165 params->object_attributes.RootDirectory = object_attributes->RootDirectory;
166 params->object_attributes.Attributes = object_attributes->Attributes;
167 params->object_attributes.SecurityDescriptor =
168 object_attributes->SecurityDescriptor;
169 params->object_attributes.SecurityQualityOfService =
170 object_attributes->SecurityQualityOfService;
171 params->allocation_size = allocation_size;
172 params->file_attributes = file_attributes;
173 params->share_access = share_access;
174 params->create_disposition = create_disposition;
175 params->create_options = create_options;
176 params->ea_buffer = ea_buffer;
177 params->ea_length = ea_length;
178 }
179
180 void CheckParams() {
181 std::bitset<32> elf((int) elf_params_.desired_access);
182 std::bitset<32> ker((int) kernel_params_.desired_access);
183
184 EXPECT_EQ(kernel_params_.desired_access, elf_params_.desired_access)
185 << elf << "\n" << ker;
186 EXPECT_EQ(kernel_params_.object_attributes.Length,
187 elf_params_.object_attributes.Length);
188 EXPECT_EQ(kernel_params_.object_attributes.RootDirectory,
189 elf_params_.object_attributes.RootDirectory);
190 EXPECT_EQ(kernel_params_.object_attributes.Attributes,
191 elf_params_.object_attributes.Attributes);
192 EXPECT_EQ(kernel_params_.object_attributes.SecurityDescriptor,
193 elf_params_.object_attributes.SecurityDescriptor);
194 EXPECT_EQ(kernel_params_.allocation_size, elf_params_.allocation_size);
195 EXPECT_EQ(kernel_params_.file_attributes, elf_params_.file_attributes);
196 EXPECT_EQ(kernel_params_.share_access, elf_params_.share_access);
197 EXPECT_EQ(kernel_params_.create_disposition,
198 elf_params_.create_disposition);
199 EXPECT_EQ(kernel_params_.create_options, elf_params_.create_options);
200 EXPECT_EQ(kernel_params_.ea_buffer, elf_params_.ea_buffer);
201 EXPECT_EQ(kernel_params_.ea_length, elf_params_.ea_length);
202 }
203
204 void DoWriteCheck(const base::FilePath& path, DWORD flag, bool is_system) {
205 base::win::ScopedHandle file_handle;
206 const char kTestData[] = "0123456789";
207 int buffer_size = sizeof(kTestData) - 1;
208 DWORD bytes_written;
209
210 if (is_system) {
211 file_handle.Set(::CreateFileW(path.value().c_str(),
212 GENERIC_WRITE,
213 FILE_SHARE_READ,
214 NULL,
215 CREATE_ALWAYS,
216 FILE_ATTRIBUTE_NORMAL | flag,
217 NULL));
218 } else {
219 file_handle.Set(CreateFileNTDLL(path.value().c_str(),
220 GENERIC_WRITE,
221 FILE_SHARE_READ,
222 NULL,
223 CREATE_ALWAYS,
224 FILE_ATTRIBUTE_NORMAL | flag,
225 NULL));
226 }
227
228
229 EXPECT_FALSE(file_handle == INVALID_HANDLE_VALUE);
230 ::WriteFile(file_handle, kTestData, buffer_size, &bytes_written, NULL);
231 EXPECT_EQ(buffer_size, bytes_written);
232 }
233
234 void DoReadCheck(const base::FilePath& path, DWORD flag, bool is_system) {
235
236 base::win::ScopedHandle file_handle;
237 const char kTestData[] = "0123456789";
238 int buffer_size = sizeof(kTestData) - 1;
239 DWORD bytes_read;
240 char read_buffer[10];
241
242 if (is_system) {
243 file_handle.Set(::CreateFileW(path.value().c_str(),
244 GENERIC_READ,
245 0,
246 NULL,
247 OPEN_ALWAYS,
248 FILE_ATTRIBUTE_NORMAL | flag,
249 NULL));
250 } else {
251 file_handle.Set(CreateFileNTDLL(path.value().c_str(),
252 GENERIC_READ,
253 0,
254 NULL,
255 OPEN_ALWAYS,
256 FILE_ATTRIBUTE_NORMAL | flag,
257 NULL));
258 }
259
260 EXPECT_FALSE(file_handle == INVALID_HANDLE_VALUE);
261 ::ReadFile(file_handle, read_buffer, buffer_size, &bytes_read, NULL);
262 EXPECT_EQ(buffer_size, bytes_read);
263 EXPECT_EQ(0, memcmp(kTestData, read_buffer, bytes_read));
264 }
265
266 void RunChecks(DWORD flag, bool check_reads) {
267 // Make sure we can write to this file handle when called via the system.
268 base::FilePath junk_path_1 = temp_dir_.path().Append(L"junk_1.txt");
269 base::FilePath junk_path_2 = temp_dir_.path().Append(L"junk_2.txt");
270 DoWriteCheck(junk_path_1, flag, true);
271 DoWriteCheck(junk_path_2, flag, false);
272 CheckParams();
273
274 if (check_reads) {
275 // Make sure we can read from this file handle when called via the system.
276 DoReadCheck(junk_path_1, flag, true);
277 DoReadCheck(junk_path_2, flag, false);
278 CheckParams();
279 }
280 base::DeleteFile(junk_path_1, false);
281 base::DeleteFile(junk_path_2, false);
282
283 }
284
285 static ChromeCreateFileTest* self_;
286
287 NtCreateFileFunction old_func_ptr_;
288 base::ScopedTempDir temp_dir_;
289 base::ScopedTempDir temp_dir2_;
290 base::win::IATPatchFunction patcher_;
291 NtCreateFileParams kernel_params_;
292 NtCreateFileParams elf_params_;
293 base::PlatformThreadId original_thread_;
294 };
295
296 ChromeCreateFileTest* ChromeCreateFileTest::self_ = NULL;
297
298 // Tests ---------------------------------------------------------------------
299 TEST_F(ChromeCreateFileTest, CheckParams_FILE_ATTRIBUTE_NORMAL) {
300 RedirectNtCreateFileCalls();
301 RunChecks(FILE_ATTRIBUTE_NORMAL, true);
302 ResetNtCreateFileCalls();
303 }
304
305 TEST_F(ChromeCreateFileTest, CheckParams_FILE_FLAG_WRITE_THROUGH) {
306 RedirectNtCreateFileCalls();
307 RunChecks(FILE_FLAG_WRITE_THROUGH, true);
308 ResetNtCreateFileCalls();
309 }
310
311 TEST_F(ChromeCreateFileTest, CheckParams_FILE_FLAG_RANDOM_ACCESS) {
312 RedirectNtCreateFileCalls();
313 RunChecks(FILE_FLAG_RANDOM_ACCESS, true);
314 ResetNtCreateFileCalls();
315 }
316
317 TEST_F(ChromeCreateFileTest, CheckParams_FILE_FLAG_SEQUENTIAL_SCAN) {
318 RedirectNtCreateFileCalls();
319 RunChecks(FILE_FLAG_SEQUENTIAL_SCAN, true);
320 ResetNtCreateFileCalls();
321 }
322
323 TEST_F(ChromeCreateFileTest, CheckParams_FILE_FLAG_DELETE_ON_CLOSE) {
324 RedirectNtCreateFileCalls();
325 RunChecks(FILE_FLAG_DELETE_ON_CLOSE, false);
326 ResetNtCreateFileCalls();
327 }
328
329 TEST_F(ChromeCreateFileTest, CheckParams_FILE_FLAG_BACKUP_SEMANTICS) {
330 RedirectNtCreateFileCalls();
331 RunChecks(FILE_FLAG_BACKUP_SEMANTICS, true);
332 ResetNtCreateFileCalls();
333 }
334
335 TEST_F(ChromeCreateFileTest, CheckParams_FILE_FLAG_OPEN_REPARSE_POINT) {
336 RedirectNtCreateFileCalls();
337 RunChecks(FILE_FLAG_OPEN_REPARSE_POINT, true);
338 ResetNtCreateFileCalls();
339 }
340
341 TEST_F(ChromeCreateFileTest, CheckParams_FILE_FLAG_OPEN_NO_RECALL) {
342 RedirectNtCreateFileCalls();
343 RunChecks(FILE_FLAG_OPEN_NO_RECALL, true);
344 ResetNtCreateFileCalls();
345 }
346
347 TEST_F(ChromeCreateFileTest, BypassTest) {
348 std::wstring UNC_filepath_file(L"\\\\.\\some_file.txt");
349
350 base::FilePath local_path;
351 PathService::Get(base::DIR_LOCAL_APP_DATA, &local_path);
352
353 base::FilePath local_prefs_path = local_path.Append(kAppDataDirName).Append(
354 kUserDataDirName).Append(L"default\\Preferences");
355 base::FilePath local_state_path = local_path.Append(kAppDataDirName).Append(
356 kUserDataDirName).Append(L"ninja\\Local State");
357 base::FilePath local_junk_path = local_path.Append(kAppDataDirName).Append(
358 kUserDataDirName).Append(L"default\\Junk");
359
360 base::FilePath desktop_path;
361 PathService::Get(base::DIR_USER_DESKTOP, &desktop_path);
362 base::FilePath desktop_junk_path =
363 desktop_path.Append(L"Downloads\\junk.txt");
364 base::FilePath desktop_prefs_path =
365 desktop_path.Append(L"Downloads\\Preferences");
366
367 // Don't redirect UNC files.
368 EXPECT_FALSE(ShouldBypass(UNC_filepath_file.c_str()));
369
370 // Don't redirect if file is not in UserData directory.
371 EXPECT_FALSE(ShouldBypass(desktop_junk_path.value().c_str()));
372 EXPECT_FALSE(ShouldBypass(desktop_prefs_path.value().c_str()));
373
374 // Only redirect "Preferences" and "Local State" files.
375 EXPECT_TRUE(ShouldBypass(local_prefs_path.value().c_str()));
376 EXPECT_TRUE(ShouldBypass(local_state_path.value().c_str()));
377 EXPECT_FALSE(ShouldBypass(local_junk_path.value().c_str()));
378 }
379
380 TEST_F(ChromeCreateFileTest, NtCreateFileAddressCheck) {
381 HMODULE ntdll_handle = ::GetModuleHandle(L"ntdll.dll");
382 EXPECT_EQ(::GetProcAddress(ntdll_handle, "NtCreateFile"),
383 g_ntdll_lookup["NtCreateFile"]);
384 }
385
386 TEST_F(ChromeCreateFileTest, ReadWriteFromNtDll) {
387 base::FilePath file_name = temp_dir_.path().Append(L"some_file.txt");
388 DoWriteCheck(file_name, FILE_ATTRIBUTE_NORMAL, false);
389 DoReadCheck(file_name, FILE_ATTRIBUTE_NORMAL, false);
390 }
391
392 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698