security/sandbox/win/src/named_pipe_policy_test.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "testing/gtest/include/gtest/gtest.h"
michael@0 6 #include "sandbox/win/src/sandbox.h"
michael@0 7 #include "sandbox/win/src/sandbox_policy.h"
michael@0 8 #include "sandbox/win/src/sandbox_factory.h"
michael@0 9 #include "sandbox/win/tests/common/controller.h"
michael@0 10
michael@0 11 namespace sandbox {
michael@0 12
michael@0 13
michael@0 14 SBOX_TESTS_COMMAND int NamedPipe_Create(int argc, wchar_t **argv) {
michael@0 15 if (argc != 1) {
michael@0 16 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
michael@0 17 }
michael@0 18 if ((NULL == argv) || (NULL == argv[0])) {
michael@0 19 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND;
michael@0 20 }
michael@0 21
michael@0 22 HANDLE pipe = ::CreateNamedPipeW(argv[0],
michael@0 23 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
michael@0 24 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, 4096,
michael@0 25 4096, 2000, NULL);
michael@0 26 if (INVALID_HANDLE_VALUE == pipe)
michael@0 27 return SBOX_TEST_DENIED;
michael@0 28
michael@0 29 OVERLAPPED overlapped = {0};
michael@0 30 overlapped.hEvent = ::CreateEvent(NULL, TRUE, TRUE, NULL);
michael@0 31 BOOL result = ::ConnectNamedPipe(pipe, &overlapped);
michael@0 32
michael@0 33 if (!result) {
michael@0 34 DWORD error = ::GetLastError();
michael@0 35 if (ERROR_PIPE_CONNECTED != error &&
michael@0 36 ERROR_IO_PENDING != error) {
michael@0 37 return SBOX_TEST_FAILED;
michael@0 38 }
michael@0 39 }
michael@0 40
michael@0 41 if (!::CloseHandle(pipe))
michael@0 42 return SBOX_TEST_FAILED;
michael@0 43
michael@0 44 ::CloseHandle(overlapped.hEvent);
michael@0 45 return SBOX_TEST_SUCCEEDED;
michael@0 46 }
michael@0 47
michael@0 48 // Tests if we can create a pipe in the sandbox. On XP, the sandbox can create
michael@0 49 // a pipe without any help but it fails on Vista, this is why we do not test
michael@0 50 // the "denied" case.
michael@0 51 TEST(NamedPipePolicyTest, CreatePipe) {
michael@0 52 TestRunner runner;
michael@0 53 // TODO(nsylvain): This policy is wrong because "*" is a valid char in a
michael@0 54 // namedpipe name. Here we apply it like a wildcard. http://b/893603
michael@0 55 EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_NAMED_PIPES,
michael@0 56 TargetPolicy::NAMEDPIPES_ALLOW_ANY,
michael@0 57 L"\\\\.\\pipe\\test*"));
michael@0 58
michael@0 59 EXPECT_EQ(SBOX_TEST_SUCCEEDED,
michael@0 60 runner.RunTest(L"NamedPipe_Create \\\\.\\pipe\\testbleh"));
michael@0 61 }
michael@0 62
michael@0 63 // The same test as CreatePipe but this time using strict interceptions.
michael@0 64 TEST(NamedPipePolicyTest, CreatePipeStrictInterceptions) {
michael@0 65 TestRunner runner;
michael@0 66 runner.GetPolicy()->SetStrictInterceptions();
michael@0 67
michael@0 68 // TODO(nsylvain): This policy is wrong because "*" is a valid char in a
michael@0 69 // namedpipe name. Here we apply it like a wildcard. http://b/893603
michael@0 70 EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_NAMED_PIPES,
michael@0 71 TargetPolicy::NAMEDPIPES_ALLOW_ANY,
michael@0 72 L"\\\\.\\pipe\\test*"));
michael@0 73
michael@0 74 EXPECT_EQ(SBOX_TEST_SUCCEEDED,
michael@0 75 runner.RunTest(L"NamedPipe_Create \\\\.\\pipe\\testbleh"));
michael@0 76 }
michael@0 77
michael@0 78 } // namespace sandbox

mercurial