1 // Copyright (c) 2012 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.
7 #define _ATL_NO_EXCEPTIONS
9 #include <atlsecurity.h>
11 #include "base/strings/string16.h"
12 #include "base/win/scoped_handle.h"
13 #include "base/win/windows_version.h"
14 #include "sandbox/win/src/sync_policy_test.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 const wchar_t kAppContainerName
[] = L
"sbox_test";
20 const wchar_t kAppContainerSid
[] =
21 L
"S-1-15-2-3251537155-1984446955-2931258699-841473695-1938553385-"
22 L
"924012148-2839372144";
24 const ULONG kSharing
= FILE_SHARE_WRITE
| FILE_SHARE_READ
| FILE_SHARE_DELETE
;
26 HANDLE
CreateTaggedEvent(const base::string16
& name
,
27 const base::string16
& sid
) {
28 base::win::ScopedHandle
event(CreateEvent(NULL
, FALSE
, FALSE
, name
.c_str()));
32 wchar_t file_name
[MAX_PATH
] = {};
33 wchar_t temp_directory
[MAX_PATH
] = {};
34 GetTempPath(MAX_PATH
, temp_directory
);
35 GetTempFileName(temp_directory
, L
"test", 0, file_name
);
37 base::win::ScopedHandle file
;
38 file
.Set(CreateFile(file_name
, GENERIC_READ
| STANDARD_RIGHTS_READ
, kSharing
,
39 NULL
, OPEN_EXISTING
, 0, NULL
));
40 DeleteFile(file_name
);
45 if (!AtlGetSecurityDescriptor(file
.Get(), SE_FILE_OBJECT
, &sd
,
46 OWNER_SECURITY_INFORMATION
|
47 GROUP_SECURITY_INFORMATION
|
48 DACL_SECURITY_INFORMATION
)) {
53 if (!ConvertStringSidToSid(sid
.c_str(), &local_sid
))
57 sd
.GetDacl(&new_dacl
);
58 CSid
csid(reinterpret_cast<SID
*>(local_sid
));
59 new_dacl
.AddAllowedAce(csid
, EVENT_ALL_ACCESS
);
60 if (!AtlSetDacl(event
.Get(), SE_KERNEL_OBJECT
, new_dacl
))
64 return event
.IsValid() ? event
.Take() : NULL
;
71 TEST(AppContainerTest
, AllowOpenEvent
) {
72 if (base::win::OSInfo::GetInstance()->version() < base::win::VERSION_WIN8
)
75 TestRunner
runner(JOB_UNPROTECTED
, USER_UNPROTECTED
, USER_UNPROTECTED
);
77 const wchar_t capability
[] = L
"S-1-15-3-12345678-87654321";
78 base::win::ScopedHandle
handle(CreateTaggedEvent(L
"test", capability
));
79 ASSERT_TRUE(handle
.IsValid());
81 EXPECT_EQ(SBOX_ALL_OK
,
82 runner
.broker()->InstallAppContainer(kAppContainerSid
,
84 EXPECT_EQ(SBOX_ALL_OK
, runner
.GetPolicy()->SetCapability(capability
));
85 EXPECT_EQ(SBOX_ALL_OK
, runner
.GetPolicy()->SetAppContainer(kAppContainerSid
));
87 EXPECT_EQ(SBOX_TEST_SUCCEEDED
, runner
.RunTest(L
"Event_Open f test"));
89 runner
.SetTestState(BEFORE_REVERT
);
90 EXPECT_EQ(SBOX_TEST_SUCCEEDED
, runner
.RunTest(L
"Event_Open f test"));
91 EXPECT_EQ(SBOX_ALL_OK
,
92 runner
.broker()->UninstallAppContainer(kAppContainerSid
));
95 TEST(AppContainerTest
, DenyOpenEvent
) {
96 if (base::win::OSInfo::GetInstance()->version() < base::win::VERSION_WIN8
)
99 TestRunner
runner(JOB_UNPROTECTED
, USER_UNPROTECTED
, USER_UNPROTECTED
);
101 const wchar_t capability
[] = L
"S-1-15-3-12345678-87654321";
102 base::win::ScopedHandle
handle(CreateTaggedEvent(L
"test", capability
));
103 ASSERT_TRUE(handle
.IsValid());
105 EXPECT_EQ(SBOX_ALL_OK
,
106 runner
.broker()->InstallAppContainer(kAppContainerSid
,
108 EXPECT_EQ(SBOX_ALL_OK
, runner
.GetPolicy()->SetAppContainer(kAppContainerSid
));
110 EXPECT_EQ(SBOX_TEST_DENIED
, runner
.RunTest(L
"Event_Open f test"));
112 runner
.SetTestState(BEFORE_REVERT
);
113 EXPECT_EQ(SBOX_TEST_DENIED
, runner
.RunTest(L
"Event_Open f test"));
114 EXPECT_EQ(SBOX_ALL_OK
,
115 runner
.broker()->UninstallAppContainer(kAppContainerSid
));
118 TEST(AppContainerTest
, NoImpersonation
) {
119 if (base::win::OSInfo::GetInstance()->version() < base::win::VERSION_WIN8
)
122 TestRunner
runner(JOB_UNPROTECTED
, USER_LIMITED
, USER_LIMITED
);
123 EXPECT_EQ(SBOX_ALL_OK
, runner
.GetPolicy()->SetAppContainer(kAppContainerSid
));
126 TEST(AppContainerTest
, WantsImpersonation
) {
127 if (base::win::OSInfo::GetInstance()->version() < base::win::VERSION_WIN8
)
130 TestRunner
runner(JOB_UNPROTECTED
, USER_UNPROTECTED
, USER_NON_ADMIN
);
131 EXPECT_EQ(SBOX_ERROR_CANNOT_INIT_APPCONTAINER
,
132 runner
.GetPolicy()->SetAppContainer(kAppContainerSid
));
135 TEST(AppContainerTest
, RequiresImpersonation
) {
136 if (base::win::OSInfo::GetInstance()->version() < base::win::VERSION_WIN8
)
139 TestRunner
runner(JOB_UNPROTECTED
, USER_RESTRICTED
, USER_RESTRICTED
);
140 EXPECT_EQ(SBOX_ERROR_CANNOT_INIT_APPCONTAINER
,
141 runner
.GetPolicy()->SetAppContainer(kAppContainerSid
));
144 TEST(AppContainerTest
, DenyOpenEventForLowBox
) {
145 if (base::win::OSInfo::GetInstance()->version() < base::win::VERSION_WIN8
)
148 TestRunner
runner(JOB_UNPROTECTED
, USER_UNPROTECTED
, USER_UNPROTECTED
);
150 base::win::ScopedHandle
event(CreateEvent(NULL
, FALSE
, FALSE
, L
"test"));
151 ASSERT_TRUE(event
.IsValid());
153 EXPECT_EQ(SBOX_ALL_OK
, runner
.GetPolicy()->SetLowBox(kAppContainerSid
));
155 EXPECT_EQ(SBOX_TEST_DENIED
, runner
.RunTest(L
"Event_Open f test"));
158 // TODO(shrikant): Please add some tests to prove usage of lowbox token like
159 // socket connection to local server in lock down mode.
161 } // namespace sandbox