OLD | NEW |
1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <windows.h> | 5 #include <windows.h> |
6 #include <winioctl.h> | 6 #include <winioctl.h> |
7 | 7 |
8 #include "base/scoped_handle_win.h" | 8 #include "base/scoped_handle_win.h" |
9 #include "sandbox/src/nt_internals.h" | 9 #include "sandbox/src/nt_internals.h" |
10 #include "sandbox/src/sandbox.h" | 10 #include "sandbox/src/sandbox.h" |
11 #include "sandbox/src/sandbox_factory.h" | 11 #include "sandbox/src/sandbox_factory.h" |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 runner.SetTestState(BEFORE_REVERT); | 255 runner.SetTestState(BEFORE_REVERT); |
256 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"File_CreateSys32 calc.exe")); | 256 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"File_CreateSys32 calc.exe")); |
257 } | 257 } |
258 | 258 |
259 TEST(FilePolicyTest, AllowReadOnly) { | 259 TEST(FilePolicyTest, AllowReadOnly) { |
260 TestRunner runner; | 260 TestRunner runner; |
261 | 261 |
262 // Create a temp file because we need write access to it. | 262 // Create a temp file because we need write access to it. |
263 wchar_t temp_directory[MAX_PATH]; | 263 wchar_t temp_directory[MAX_PATH]; |
264 wchar_t temp_file_name[MAX_PATH]; | 264 wchar_t temp_file_name[MAX_PATH]; |
265 ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0); | 265 ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); |
266 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name), 0); | 266 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name), 0u); |
267 | 267 |
268 EXPECT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_READONLY, | 268 EXPECT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_READONLY, |
269 temp_file_name)); | 269 temp_file_name)); |
270 | 270 |
271 wchar_t command_read[MAX_PATH + 20] = {0}; | 271 wchar_t command_read[MAX_PATH + 20] = {0}; |
272 wsprintf(command_read, L"File_Create Read \"%ls\"", temp_file_name); | 272 wsprintf(command_read, L"File_Create Read \"%ls\"", temp_file_name); |
273 wchar_t command_write[MAX_PATH + 20] = {0}; | 273 wchar_t command_write[MAX_PATH + 20] = {0}; |
274 wsprintf(command_write, L"File_Create Write \"%ls\"", temp_file_name); | 274 wsprintf(command_write, L"File_Create Write \"%ls\"", temp_file_name); |
275 | 275 |
276 // Verify that we have read access after revert. | 276 // Verify that we have read access after revert. |
277 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(command_read)); | 277 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(command_read)); |
278 | 278 |
279 // Verify that we don't have write access after revert. | 279 // Verify that we don't have write access after revert. |
280 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(command_write)); | 280 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(command_write)); |
281 | 281 |
282 // Verify that we really have write access to the file. | 282 // Verify that we really have write access to the file. |
283 runner.SetTestState(BEFORE_REVERT); | 283 runner.SetTestState(BEFORE_REVERT); |
284 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(command_write)); | 284 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(command_write)); |
285 | 285 |
286 DeleteFile(temp_file_name); | 286 DeleteFile(temp_file_name); |
287 } | 287 } |
288 | 288 |
289 TEST(FilePolicyTest, AllowWildcard) { | 289 TEST(FilePolicyTest, AllowWildcard) { |
290 TestRunner runner; | 290 TestRunner runner; |
291 | 291 |
292 // Create a temp file because we need write access to it. | 292 // Create a temp file because we need write access to it. |
293 wchar_t temp_directory[MAX_PATH]; | 293 wchar_t temp_directory[MAX_PATH]; |
294 wchar_t temp_file_name[MAX_PATH]; | 294 wchar_t temp_file_name[MAX_PATH]; |
295 ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0); | 295 ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); |
296 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name), 0); | 296 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name), 0u); |
297 | 297 |
298 wcscat_s(temp_directory, MAX_PATH, L"*"); | 298 wcscat_s(temp_directory, MAX_PATH, L"*"); |
299 EXPECT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_ANY, temp_directory)); | 299 EXPECT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_ANY, temp_directory)); |
300 | 300 |
301 wchar_t command_write[MAX_PATH + 20] = {0}; | 301 wchar_t command_write[MAX_PATH + 20] = {0}; |
302 wsprintf(command_write, L"File_Create Write \"%ls\"", temp_file_name); | 302 wsprintf(command_write, L"File_Create Write \"%ls\"", temp_file_name); |
303 | 303 |
304 // Verify that we have write access after revert. | 304 // Verify that we have write access after revert. |
305 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(command_write)); | 305 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(command_write)); |
306 | 306 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 // Give access to the temp directory. | 377 // Give access to the temp directory. |
378 wchar_t temp_directory[MAX_PATH]; | 378 wchar_t temp_directory[MAX_PATH]; |
379 wchar_t temp_file_name1[MAX_PATH]; | 379 wchar_t temp_file_name1[MAX_PATH]; |
380 wchar_t temp_file_name2[MAX_PATH]; | 380 wchar_t temp_file_name2[MAX_PATH]; |
381 wchar_t temp_file_name3[MAX_PATH]; | 381 wchar_t temp_file_name3[MAX_PATH]; |
382 wchar_t temp_file_name4[MAX_PATH]; | 382 wchar_t temp_file_name4[MAX_PATH]; |
383 wchar_t temp_file_name5[MAX_PATH]; | 383 wchar_t temp_file_name5[MAX_PATH]; |
384 wchar_t temp_file_name6[MAX_PATH]; | 384 wchar_t temp_file_name6[MAX_PATH]; |
385 wchar_t temp_file_name7[MAX_PATH]; | 385 wchar_t temp_file_name7[MAX_PATH]; |
386 wchar_t temp_file_name8[MAX_PATH]; | 386 wchar_t temp_file_name8[MAX_PATH]; |
387 ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0); | 387 ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); |
388 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name1), 0); | 388 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name1), 0u); |
389 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name2), 0); | 389 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name2), 0u); |
390 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name3), 0); | 390 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name3), 0u); |
391 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name4), 0); | 391 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name4), 0u); |
392 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name5), 0); | 392 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name5), 0u); |
393 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name6), 0); | 393 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name6), 0u); |
394 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name7), 0); | 394 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name7), 0u); |
395 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name8), 0); | 395 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name8), 0u); |
396 | 396 |
397 | 397 |
398 // Add rules to make file1->file2 succeed. | 398 // Add rules to make file1->file2 succeed. |
399 ASSERT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_ANY, temp_file_name1)); | 399 ASSERT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_ANY, temp_file_name1)); |
400 ASSERT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_ANY, temp_file_name2)); | 400 ASSERT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_ANY, temp_file_name2)); |
401 | 401 |
402 // Add rules to make file3->file4 fail. | 402 // Add rules to make file3->file4 fail. |
403 ASSERT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_ANY, temp_file_name3)); | 403 ASSERT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_ANY, temp_file_name3)); |
404 ASSERT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_READONLY, | 404 ASSERT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_READONLY, |
405 temp_file_name4)); | 405 temp_file_name4)); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"File_GetDiskSpace")); | 493 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"File_GetDiskSpace")); |
494 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"File_Win32Create notepad.exe")); | 494 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"File_Win32Create notepad.exe")); |
495 } | 495 } |
496 | 496 |
497 TEST(FilePolicyTest, TestReparsePoint) { | 497 TEST(FilePolicyTest, TestReparsePoint) { |
498 TestRunner runner; | 498 TestRunner runner; |
499 | 499 |
500 // Create a temp file because we need write access to it. | 500 // Create a temp file because we need write access to it. |
501 wchar_t temp_directory[MAX_PATH]; | 501 wchar_t temp_directory[MAX_PATH]; |
502 wchar_t temp_file_name[MAX_PATH]; | 502 wchar_t temp_file_name[MAX_PATH]; |
503 ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0); | 503 ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); |
504 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name), 0); | 504 ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, temp_file_name), 0u); |
505 | 505 |
506 // Delete the file and create a directory instead. | 506 // Delete the file and create a directory instead. |
507 ASSERT_TRUE(::DeleteFile(temp_file_name)); | 507 ASSERT_TRUE(::DeleteFile(temp_file_name)); |
508 ASSERT_TRUE(::CreateDirectory(temp_file_name, NULL)); | 508 ASSERT_TRUE(::CreateDirectory(temp_file_name, NULL)); |
509 | 509 |
510 // Create a temporary file in the subfolder. | 510 // Create a temporary file in the subfolder. |
511 std::wstring subfolder = temp_file_name; | 511 std::wstring subfolder = temp_file_name; |
512 std::wstring temp_file_title = subfolder.substr(subfolder.rfind(L"\\") + 1); | 512 std::wstring temp_file_title = subfolder.substr(subfolder.rfind(L"\\") + 1); |
513 std::wstring temp_file = subfolder + L"\\file_" + temp_file_title; | 513 std::wstring temp_file = subfolder + L"\\file_" + temp_file_title; |
514 | 514 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 EXPECT_TRUE(INVALID_HANDLE_VALUE != dir); | 565 EXPECT_TRUE(INVALID_HANDLE_VALUE != dir); |
566 EXPECT_TRUE(DeleteReparsePoint(dir)); | 566 EXPECT_TRUE(DeleteReparsePoint(dir)); |
567 EXPECT_TRUE(::CloseHandle(dir)); | 567 EXPECT_TRUE(::CloseHandle(dir)); |
568 | 568 |
569 // Cleanup. | 569 // Cleanup. |
570 EXPECT_TRUE(::DeleteFile(temp_file_in_temp.c_str())); | 570 EXPECT_TRUE(::DeleteFile(temp_file_in_temp.c_str())); |
571 EXPECT_TRUE(::RemoveDirectory(subfolder.c_str())); | 571 EXPECT_TRUE(::RemoveDirectory(subfolder.c_str())); |
572 } | 572 } |
573 | 573 |
574 } // namespace sandbox | 574 } // namespace sandbox |
OLD | NEW |