named_pipe_policy_test.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (c) 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. #include "base/win/windows_version.h"
  5. #include "sandbox/win/src/sandbox.h"
  6. #include "sandbox/win/src/sandbox_factory.h"
  7. #include "sandbox/win/src/sandbox_policy.h"
  8. #include "sandbox/win/src/win_utils.h"
  9. #include "sandbox/win/tests/common/controller.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace sandbox {
  12. SBOX_TESTS_COMMAND int NamedPipe_Create(int argc, wchar_t** argv) {
  13. if (argc < 1 || argc > 2 || !argv || !argv[0])
  14. return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
  15. HANDLE pipe = ::CreateNamedPipeW(
  16. argv[0], PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
  17. PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, 4096, 4096, 2000, nullptr);
  18. if (INVALID_HANDLE_VALUE == pipe)
  19. return SBOX_TEST_DENIED;
  20. // The second parameter allows us to enforce an allowlist for where the
  21. // pipe should be in the object namespace after creation.
  22. if (argc == 2) {
  23. std::wstring handle_name;
  24. if (GetPathFromHandle(pipe, &handle_name)) {
  25. if (handle_name.compare(0, wcslen(argv[1]), argv[1]) != 0)
  26. return SBOX_TEST_FAILED;
  27. } else {
  28. return SBOX_TEST_FAILED;
  29. }
  30. }
  31. OVERLAPPED overlapped = {0};
  32. overlapped.hEvent = ::CreateEvent(nullptr, true, true, nullptr);
  33. bool result = ::ConnectNamedPipe(pipe, &overlapped);
  34. if (!result) {
  35. DWORD error = ::GetLastError();
  36. if (ERROR_PIPE_CONNECTED != error && ERROR_IO_PENDING != error) {
  37. return SBOX_TEST_FAILED;
  38. }
  39. }
  40. if (!::CloseHandle(pipe))
  41. return SBOX_TEST_FAILED;
  42. ::CloseHandle(overlapped.hEvent);
  43. return SBOX_TEST_SUCCEEDED;
  44. }
  45. std::unique_ptr<TestRunner> CreatePipeRunner() {
  46. auto runner = std::make_unique<TestRunner>();
  47. // TODO(nsylvain): This policy is wrong because "*" is a valid char in a
  48. // namedpipe name. Here we apply it like a wildcard. http://b/893603
  49. runner->AddRule(SubSystem::kNamedPipes, Semantics::kNamedPipesAllowAny,
  50. L"\\\\.\\pipe\\test*");
  51. return runner;
  52. }
  53. // Tests if we can create a pipe in the sandbox.
  54. TEST(NamedPipePolicyTest, CreatePipe) {
  55. auto runner = CreatePipeRunner();
  56. EXPECT_EQ(SBOX_TEST_SUCCEEDED,
  57. runner->RunTest(L"NamedPipe_Create \\\\.\\pipe\\testbleh"));
  58. runner = CreatePipeRunner();
  59. EXPECT_EQ(SBOX_TEST_DENIED,
  60. runner->RunTest(L"NamedPipe_Create \\\\.\\pipe\\bleh"));
  61. }
  62. std::unique_ptr<TestRunner> PipeTraversalRunner() {
  63. auto runner = std::make_unique<TestRunner>();
  64. // TODO(nsylvain): This policy is wrong because "*" is a valid char in a
  65. // namedpipe name. Here we apply it like a wildcard. http://b/893603
  66. runner->AddRule(SubSystem::kNamedPipes, Semantics::kNamedPipesAllowAny,
  67. L"\\\\.\\pipe\\test*");
  68. return runner;
  69. }
  70. // Tests if we can create a pipe with a path traversal in the sandbox.
  71. TEST(NamedPipePolicyTest, CreatePipeTraversal) {
  72. auto runner = PipeTraversalRunner();
  73. EXPECT_EQ(SBOX_TEST_DENIED,
  74. runner->RunTest(L"NamedPipe_Create \\\\.\\pipe\\test\\..\\bleh"));
  75. runner = PipeTraversalRunner();
  76. EXPECT_EQ(SBOX_TEST_DENIED,
  77. runner->RunTest(L"NamedPipe_Create \\\\.\\pipe\\test/../bleh"));
  78. runner = PipeTraversalRunner();
  79. EXPECT_EQ(SBOX_TEST_DENIED,
  80. runner->RunTest(L"NamedPipe_Create \\\\.\\pipe\\test\\../bleh"));
  81. runner = PipeTraversalRunner();
  82. EXPECT_EQ(SBOX_TEST_DENIED,
  83. runner->RunTest(L"NamedPipe_Create \\\\.\\pipe\\test/..\\bleh"));
  84. }
  85. // This tests that path canonicalization is actually disabled if we use \\?\
  86. // syntax.
  87. TEST(NamedPipePolicyTest, CreatePipeCanonicalization) {
  88. // "For file I/O, the "\\?\" prefix to a path string tells the Windows APIs to
  89. // disable all string parsing and to send the string that follows it straight
  90. // to the file system."
  91. // http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx
  92. const wchar_t* argv[2] = {L"\\\\?\\pipe\\test\\..\\bleh",
  93. L"\\Device\\NamedPipe\\test"};
  94. EXPECT_EQ(SBOX_TEST_SUCCEEDED,
  95. NamedPipe_Create(2, const_cast<wchar_t**>(argv)));
  96. }
  97. std::unique_ptr<TestRunner> StrictInterceptionsRunner() {
  98. auto runner = std::make_unique<TestRunner>();
  99. runner->GetPolicy()->SetStrictInterceptions();
  100. // TODO(nsylvain): This policy is wrong because "*" is a valid char in a
  101. // namedpipe name. Here we apply it like a wildcard. http://b/893603
  102. runner->AddRule(SubSystem::kNamedPipes, Semantics::kNamedPipesAllowAny,
  103. L"\\\\.\\pipe\\test*");
  104. return runner;
  105. }
  106. // The same test as CreatePipe but this time using strict interceptions.
  107. TEST(NamedPipePolicyTest, CreatePipeStrictInterceptions) {
  108. auto runner = StrictInterceptionsRunner();
  109. EXPECT_EQ(SBOX_TEST_SUCCEEDED,
  110. runner->RunTest(L"NamedPipe_Create \\\\.\\pipe\\testbleh"));
  111. runner = StrictInterceptionsRunner();
  112. EXPECT_EQ(SBOX_TEST_DENIED,
  113. runner->RunTest(L"NamedPipe_Create \\\\.\\pipe\\bleh"));
  114. }
  115. } // namespace sandbox