OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <algorithm> | 5 #include <algorithm> |
6 #include <cctype> | 6 #include <cctype> |
7 | 7 |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <winioctl.h> | 9 #include <winioctl.h> |
10 | 10 |
11 #include "base/win/scoped_handle.h" | 11 #include "base/win/scoped_handle.h" |
| 12 #include "base/win/windows_version.h" |
12 #include "sandbox/win/src/filesystem_policy.h" | 13 #include "sandbox/win/src/filesystem_policy.h" |
13 #include "sandbox/win/src/nt_internals.h" | 14 #include "sandbox/win/src/nt_internals.h" |
14 #include "sandbox/win/src/sandbox.h" | 15 #include "sandbox/win/src/sandbox.h" |
15 #include "sandbox/win/src/sandbox_factory.h" | 16 #include "sandbox/win/src/sandbox_factory.h" |
16 #include "sandbox/win/src/sandbox_policy.h" | 17 #include "sandbox/win/src/sandbox_policy.h" |
17 #include "sandbox/win/src/win_utils.h" | 18 #include "sandbox/win/src/win_utils.h" |
18 #include "sandbox/win/tests/common/controller.h" | 19 #include "sandbox/win/tests/common/controller.h" |
19 #include "sandbox/win/tests/common/test_utils.h" | 20 #include "sandbox/win/tests/common/test_utils.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 SBOX_TESTS_COMMAND int File_CreateSys32(int argc, wchar_t **argv) { | 103 SBOX_TESTS_COMMAND int File_CreateSys32(int argc, wchar_t **argv) { |
103 BINDNTDLL(NtCreateFile); | 104 BINDNTDLL(NtCreateFile); |
104 BINDNTDLL(RtlInitUnicodeString); | 105 BINDNTDLL(RtlInitUnicodeString); |
105 if (!NtCreateFile || !RtlInitUnicodeString) | 106 if (!NtCreateFile || !RtlInitUnicodeString) |
106 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | 107 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; |
107 | 108 |
108 if (argc != 1) | 109 if (argc != 1) |
109 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | 110 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; |
110 | 111 |
111 base::string16 file(argv[0]); | 112 base::string16 file(argv[0]); |
112 if (0 != _wcsnicmp(file.c_str(), kNTObjManPrefix, kNTObjManPrefixLen)) | 113 if (0 != _wcsnicmp(file.c_str(), kNTDevicePrefix, kNTDevicePrefixLen)) |
113 file = MakePathToSys(argv[0], true); | 114 file = MakePathToSys(argv[0], true); |
114 | 115 |
115 UNICODE_STRING object_name; | 116 UNICODE_STRING object_name; |
116 RtlInitUnicodeString(&object_name, file.c_str()); | 117 RtlInitUnicodeString(&object_name, file.c_str()); |
117 | 118 |
118 OBJECT_ATTRIBUTES obj_attributes = {0}; | 119 OBJECT_ATTRIBUTES obj_attributes = {0}; |
119 InitializeObjectAttributes(&obj_attributes, &object_name, | 120 InitializeObjectAttributes(&obj_attributes, &object_name, |
120 OBJ_CASE_INSENSITIVE, NULL, NULL); | 121 OBJ_CASE_INSENSITIVE, NULL, NULL); |
121 | 122 |
122 HANDLE handle; | 123 HANDLE handle; |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 TestRunner runner; | 273 TestRunner runner; |
273 EXPECT_TRUE(runner.AddRuleSys32(TargetPolicy::FILES_ALLOW_ANY, L"calc.exe")); | 274 EXPECT_TRUE(runner.AddRuleSys32(TargetPolicy::FILES_ALLOW_ANY, L"calc.exe")); |
274 | 275 |
275 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"File_CreateSys32 calc.exe")); | 276 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"File_CreateSys32 calc.exe")); |
276 | 277 |
277 runner.SetTestState(BEFORE_REVERT); | 278 runner.SetTestState(BEFORE_REVERT); |
278 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"File_CreateSys32 calc.exe")); | 279 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"File_CreateSys32 calc.exe")); |
279 } | 280 } |
280 | 281 |
281 TEST(FilePolicyTest, AllowNtCreateWithNativePath) { | 282 TEST(FilePolicyTest, AllowNtCreateWithNativePath) { |
| 283 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
| 284 return; |
| 285 |
282 base::string16 calc = MakePathToSys(L"calc.exe", false); | 286 base::string16 calc = MakePathToSys(L"calc.exe", false); |
283 base::string16 nt_path; | 287 base::string16 nt_path; |
284 ASSERT_TRUE(GetNtPathFromWin32Path(calc, &nt_path)); | 288 ASSERT_TRUE(GetNtPathFromWin32Path(calc, &nt_path)); |
285 TestRunner runner; | 289 TestRunner runner; |
286 runner.AddFsRule(TargetPolicy::FILES_ALLOW_READONLY, nt_path.c_str()); | 290 runner.AddFsRule(TargetPolicy::FILES_ALLOW_READONLY, nt_path.c_str()); |
287 | 291 |
288 wchar_t buff[MAX_PATH]; | 292 wchar_t buff[MAX_PATH]; |
289 ::wsprintfW(buff, L"File_CreateSys32 %s", nt_path.c_str()); | 293 ::wsprintfW(buff, L"File_CreateSys32 %s", nt_path.c_str()); |
290 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(buff)); | 294 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(buff)); |
291 | 295 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 // Verify that we have read access after revert. | 327 // Verify that we have read access after revert. |
324 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(command_read)); | 328 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(command_read)); |
325 | 329 |
326 // Verify that we really have write access to the file. | 330 // Verify that we really have write access to the file. |
327 runner.SetTestState(BEFORE_REVERT); | 331 runner.SetTestState(BEFORE_REVERT); |
328 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(command_write)); | 332 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(command_write)); |
329 | 333 |
330 DeleteFile(temp_file_name); | 334 DeleteFile(temp_file_name); |
331 } | 335 } |
332 | 336 |
| 337 // Tests support of "\\\\.\\DeviceName" kind of paths. |
| 338 TEST(FilePolicyTest, AllowImplicitDeviceName) { |
| 339 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
| 340 return; |
| 341 |
| 342 TestRunner runner; |
| 343 |
| 344 wchar_t temp_directory[MAX_PATH]; |
| 345 wchar_t temp_file_name[MAX_PATH]; |
| 346 ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); |
| 347 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name), 0u); |
| 348 |
| 349 std::wstring path; |
| 350 EXPECT_TRUE(ConvertToLongPath(temp_file_name, &path)); |
| 351 EXPECT_TRUE(GetNtPathFromWin32Path(path, &path)); |
| 352 path = path.substr(sandbox::kNTDevicePrefixLen); |
| 353 |
| 354 wchar_t command[MAX_PATH + 20] = {0}; |
| 355 wsprintf(command, L"File_Create Read \"\\\\.\\%ls\"", path.c_str()); |
| 356 path = std::wstring(kNTPrefix) + path; |
| 357 |
| 358 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(command)); |
| 359 EXPECT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_ANY, path.c_str())); |
| 360 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(command)); |
| 361 |
| 362 DeleteFile(temp_file_name); |
| 363 } |
| 364 |
333 TEST(FilePolicyTest, AllowWildcard) { | 365 TEST(FilePolicyTest, AllowWildcard) { |
334 TestRunner runner; | 366 TestRunner runner; |
335 | 367 |
336 // Create a temp file because we need write access to it. | 368 // Create a temp file because we need write access to it. |
337 wchar_t temp_directory[MAX_PATH]; | 369 wchar_t temp_directory[MAX_PATH]; |
338 wchar_t temp_file_name[MAX_PATH]; | 370 wchar_t temp_file_name[MAX_PATH]; |
339 ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); | 371 ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); |
340 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name), 0u); | 372 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name), 0u); |
341 | 373 |
342 wcscat_s(temp_directory, MAX_PATH, L"*"); | 374 wcscat_s(temp_directory, MAX_PATH, L"*"); |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
633 | 665 |
634 TEST(FilePolicyTest, CheckMissingNTPrefixEscape) { | 666 TEST(FilePolicyTest, CheckMissingNTPrefixEscape) { |
635 base::string16 name = L"C:\\NAME"; | 667 base::string16 name = L"C:\\NAME"; |
636 | 668 |
637 base::string16 result = FixNTPrefixForMatch(name); | 669 base::string16 result = FixNTPrefixForMatch(name); |
638 | 670 |
639 EXPECT_STREQ(result.c_str(), L"\\/?/?\\C:\\NAME"); | 671 EXPECT_STREQ(result.c_str(), L"\\/?/?\\C:\\NAME"); |
640 } | 672 } |
641 | 673 |
642 } // namespace sandbox | 674 } // namespace sandbox |
OLD | NEW |