OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <memory> | 5 #include <memory> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/string16.h" | |
9 #include "base/sys_string_conversions.h" | 8 #include "base/sys_string_conversions.h" |
10 #include "base/win/scoped_handle.h" | 9 #include "base/win/scoped_handle.h" |
11 #include "base/win/scoped_process_information.h" | 10 #include "base/win/scoped_process_information.h" |
12 #include "sandbox/win/src/sandbox.h" | 11 #include "sandbox/win/src/sandbox.h" |
13 #include "sandbox/win/src/sandbox_policy.h" | 12 #include "sandbox/win/src/sandbox_policy.h" |
14 #include "sandbox/win/src/sandbox_factory.h" | 13 #include "sandbox/win/src/sandbox_factory.h" |
15 #include "sandbox/win/tests/common/controller.h" | 14 #include "sandbox/win/tests/common/controller.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
20 // While the shell API provides better calls than this home brew function | 19 // While the shell API provides better calls than this home brew function |
21 // we use GetSystemWindowsDirectoryW which does not query the registry so | 20 // we use GetSystemWindowsDirectoryW which does not query the registry so |
22 // it is safe to use after revert. | 21 // it is safe to use after revert. |
23 string16 MakeFullPathToSystem32(const wchar_t* name) { | 22 std::wstring MakeFullPathToSystem32(const wchar_t* name) { |
24 wchar_t windows_path[MAX_PATH] = {0}; | 23 wchar_t windows_path[MAX_PATH] = {0}; |
25 ::GetSystemWindowsDirectoryW(windows_path, MAX_PATH); | 24 ::GetSystemWindowsDirectoryW(windows_path, MAX_PATH); |
26 string16 full_path(windows_path); | 25 std::wstring full_path(windows_path); |
27 if (full_path.empty()) { | 26 if (full_path.empty()) { |
28 return full_path; | 27 return full_path; |
29 } | 28 } |
30 full_path += L"\\system32\\"; | 29 full_path += L"\\system32\\"; |
31 full_path += name; | 30 full_path += name; |
32 return full_path; | 31 return full_path; |
33 } | 32 } |
34 | 33 |
35 // Creates a process with the |exe| and |command| parameter using the | 34 // Creates a process with the |exe| and |command| parameter using the |
36 // unicode and ascii version of the api. | 35 // unicode and ascii version of the api. |
37 sandbox::SboxTestResult CreateProcessHelper(const string16& exe, | 36 sandbox::SboxTestResult CreateProcessHelper(const std::wstring &exe, |
38 const string16& command) { | 37 const std::wstring &command) { |
39 base::win::ScopedProcessInformation pi; | 38 base::win::ScopedProcessInformation pi; |
40 STARTUPINFOW si = {sizeof(si)}; | 39 STARTUPINFOW si = {sizeof(si)}; |
41 | 40 |
42 const wchar_t *exe_name = NULL; | 41 const wchar_t *exe_name = NULL; |
43 if (!exe.empty()) | 42 if (!exe.empty()) |
44 exe_name = exe.c_str(); | 43 exe_name = exe.c_str(); |
45 | 44 |
46 const wchar_t *cmd_line = NULL; | 45 const wchar_t *cmd_line = NULL; |
47 if (!command.empty()) | 46 if (!command.empty()) |
48 cmd_line = command.c_str(); | 47 cmd_line = command.c_str(); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 if (ret1 == ret2) | 90 if (ret1 == ret2) |
92 return ret1; | 91 return ret1; |
93 | 92 |
94 return sandbox::SBOX_TEST_FAILED; | 93 return sandbox::SBOX_TEST_FAILED; |
95 } | 94 } |
96 | 95 |
97 } // namespace | 96 } // namespace |
98 | 97 |
99 namespace sandbox { | 98 namespace sandbox { |
100 | 99 |
101 SBOX_TESTS_COMMAND int Process_RunApp1(int argc, wchar_t **argv) { | 100 // Tries to create the process in argv[0] using 7 different ways. |
| 101 // Since we also try the Ansi and Unicode version of the CreateProcess API, |
| 102 // The process referenced by argv[0] will be spawned 14 times. |
| 103 SBOX_TESTS_COMMAND int Process_RunApp(int argc, wchar_t **argv) { |
102 if (argc != 1) { | 104 if (argc != 1) { |
103 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | 105 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; |
104 } | 106 } |
105 if ((NULL == argv) || (NULL == argv[0])) { | 107 if ((NULL == argv) || (NULL == argv[0])) { |
106 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | 108 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; |
107 } | 109 } |
108 string16 path = MakeFullPathToSystem32(argv[0]); | 110 std::wstring path = MakeFullPathToSystem32(argv[0]); |
109 | 111 |
110 // TEST 1: Try with the path in the app_name. | 112 // TEST 1: Try with the path in the app_name. |
111 return CreateProcessHelper(path, string16()); | 113 int result1 = CreateProcessHelper(path, std::wstring()); |
112 } | |
113 | |
114 SBOX_TESTS_COMMAND int Process_RunApp2(int argc, wchar_t **argv) { | |
115 if (argc != 1) { | |
116 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | |
117 } | |
118 if ((NULL == argv) || (NULL == argv[0])) { | |
119 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | |
120 } | |
121 string16 path = MakeFullPathToSystem32(argv[0]); | |
122 | 114 |
123 // TEST 2: Try with the path in the cmd_line. | 115 // TEST 2: Try with the path in the cmd_line. |
124 string16 cmd_line = L"\""; | 116 std::wstring cmd_line = L"\""; |
125 cmd_line += path; | 117 cmd_line += path; |
126 cmd_line += L"\""; | 118 cmd_line += L"\""; |
127 return CreateProcessHelper(string16(), cmd_line); | 119 int result2 = CreateProcessHelper(std::wstring(), cmd_line); |
128 } | |
129 | |
130 SBOX_TESTS_COMMAND int Process_RunApp3(int argc, wchar_t **argv) { | |
131 if (argc != 1) { | |
132 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | |
133 } | |
134 if ((NULL == argv) || (NULL == argv[0])) { | |
135 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | |
136 } | |
137 | 120 |
138 // TEST 3: Try file name in the cmd_line. | 121 // TEST 3: Try file name in the cmd_line. |
139 return CreateProcessHelper(string16(), argv[0]); | 122 int result3 = CreateProcessHelper(std::wstring(), argv[0]); |
140 } | |
141 | |
142 SBOX_TESTS_COMMAND int Process_RunApp4(int argc, wchar_t **argv) { | |
143 if (argc != 1) { | |
144 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | |
145 } | |
146 if ((NULL == argv) || (NULL == argv[0])) { | |
147 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | |
148 } | |
149 | 123 |
150 // TEST 4: Try file name in the app_name and current directory sets correctly. | 124 // TEST 4: Try file name in the app_name and current directory sets correctly. |
151 string16 system32 = MakeFullPathToSystem32(L""); | 125 std::wstring system32 = MakeFullPathToSystem32(L""); |
152 wchar_t current_directory[MAX_PATH + 1]; | 126 wchar_t current_directory[MAX_PATH + 1]; |
153 int result4; | 127 int result4; |
154 bool test_succeeded = false; | 128 bool test_succeeded = false; |
155 DWORD ret = ::GetCurrentDirectory(MAX_PATH, current_directory); | 129 DWORD ret = ::GetCurrentDirectory(MAX_PATH, current_directory); |
156 if (!ret) | 130 if (0 != ret && ret < MAX_PATH) { |
157 return SBOX_TEST_FIRST_ERROR; | |
158 | |
159 if (ret < MAX_PATH) { | |
160 current_directory[ret] = L'\\'; | 131 current_directory[ret] = L'\\'; |
161 current_directory[ret+1] = L'\0'; | 132 current_directory[ret+1] = L'\0'; |
162 if (::SetCurrentDirectory(system32.c_str())) { | 133 if (::SetCurrentDirectory(system32.c_str())) { |
163 result4 = CreateProcessHelper(argv[0], string16()); | 134 result4 = CreateProcessHelper(argv[0], std::wstring()); |
164 if (::SetCurrentDirectory(current_directory)) { | 135 if (::SetCurrentDirectory(current_directory)) { |
165 test_succeeded = true; | 136 test_succeeded = true; |
166 } | 137 } |
167 } else { | |
168 return SBOX_TEST_SECOND_ERROR; | |
169 } | 138 } |
170 } | 139 } |
171 if (!test_succeeded) | 140 if (!test_succeeded) |
172 result4 = SBOX_TEST_FAILED; | 141 result4 = SBOX_TEST_FAILED; |
173 | 142 |
174 return result4; | |
175 } | |
176 | |
177 SBOX_TESTS_COMMAND int Process_RunApp5(int argc, wchar_t **argv) { | |
178 if (argc != 1) { | |
179 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | |
180 } | |
181 if ((NULL == argv) || (NULL == argv[0])) { | |
182 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | |
183 } | |
184 string16 path = MakeFullPathToSystem32(argv[0]); | |
185 | |
186 // TEST 5: Try with the path in the cmd_line and arguments. | 143 // TEST 5: Try with the path in the cmd_line and arguments. |
187 string16 cmd_line = L"\""; | 144 cmd_line = L"\""; |
188 cmd_line += path; | 145 cmd_line += path; |
189 cmd_line += L"\" /I"; | 146 cmd_line += L"\" /INSERT"; |
190 return CreateProcessHelper(string16(), cmd_line); | 147 int result5 = CreateProcessHelper(std::wstring(), cmd_line); |
191 } | |
192 | |
193 SBOX_TESTS_COMMAND int Process_RunApp6(int argc, wchar_t **argv) { | |
194 if (argc != 1) { | |
195 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | |
196 } | |
197 if ((NULL == argv) || (NULL == argv[0])) { | |
198 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | |
199 } | |
200 | 148 |
201 // TEST 6: Try with the file_name in the cmd_line and arguments. | 149 // TEST 6: Try with the file_name in the cmd_line and arguments. |
202 string16 cmd_line = argv[0]; | 150 cmd_line = argv[0]; |
203 cmd_line += L" /I"; | 151 cmd_line += L" /INSERT"; |
204 return CreateProcessHelper(string16(), cmd_line); | 152 int result6 = CreateProcessHelper(std::wstring(), cmd_line); |
| 153 |
| 154 // TEST 7: Try with the path without the drive. |
| 155 cmd_line = path.substr(path.find(L'\\')); |
| 156 int result7 = CreateProcessHelper(std::wstring(), cmd_line); |
| 157 |
| 158 // Check if they all returned the same thing. |
| 159 if ((result1 == result2) && (result2 == result3) && (result3 == result4) && |
| 160 (result4 == result5) && (result5 == result6) && (result6 == result7)) |
| 161 return result1; |
| 162 |
| 163 return SBOX_TEST_FAILED; |
205 } | 164 } |
206 | 165 |
207 // Creates a process and checks if it's possible to get a handle to it's token. | 166 // Creates a process and checks if it's possible to get a handle to it's token. |
208 SBOX_TESTS_COMMAND int Process_GetChildProcessToken(int argc, wchar_t **argv) { | 167 SBOX_TESTS_COMMAND int Process_GetChildProcessToken(int argc, wchar_t **argv) { |
209 if (argc != 1) | 168 if (argc != 1) |
210 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | 169 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; |
211 | 170 |
212 if ((NULL == argv) || (NULL == argv[0])) | 171 if ((NULL == argv) || (NULL == argv[0])) |
213 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; | 172 return SBOX_TEST_FAILED_TO_EXECUTE_COMMAND; |
214 | 173 |
215 string16 path = MakeFullPathToSystem32(argv[0]); | 174 std::wstring path = MakeFullPathToSystem32(argv[0]); |
216 | 175 |
217 base::win::ScopedProcessInformation pi; | 176 base::win::ScopedProcessInformation pi; |
218 STARTUPINFOW si = {sizeof(si)}; | 177 STARTUPINFOW si = {sizeof(si)}; |
219 | 178 |
220 if (!::CreateProcessW(path.c_str(), NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, | 179 if (!::CreateProcessW(path.c_str(), NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, |
221 NULL, NULL, &si, pi.Receive())) { | 180 NULL, NULL, &si, pi.Receive())) { |
222 return SBOX_TEST_FAILED; | 181 return SBOX_TEST_FAILED; |
223 } | 182 } |
224 | 183 |
225 HANDLE token = NULL; | 184 HANDLE token = NULL; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 | 229 |
271 // Check the working case. | 230 // Check the working case. |
272 runner.GetPolicy()->SetTokenLevel(USER_INTERACTIVE, USER_INTERACTIVE); | 231 runner.GetPolicy()->SetTokenLevel(USER_INTERACTIVE, USER_INTERACTIVE); |
273 | 232 |
274 EXPECT_EQ(SBOX_ALL_OK, | 233 EXPECT_EQ(SBOX_ALL_OK, |
275 runner.GetPolicy()->AddRule(TargetPolicy::SUBSYS_PROCESS, | 234 runner.GetPolicy()->AddRule(TargetPolicy::SUBSYS_PROCESS, |
276 TargetPolicy::PROCESS_ALL_EXEC, | 235 TargetPolicy::PROCESS_ALL_EXEC, |
277 L"this is not important")); | 236 L"this is not important")); |
278 } | 237 } |
279 | 238 |
280 TEST(ProcessPolicyTest, CreateProcessAW) { | 239 // This test is disabled. See bug 1305476. |
| 240 TEST(ProcessPolicyTest, DISABLED_RunFindstrExe) { |
281 TestRunner runner; | 241 TestRunner runner; |
282 string16 exe_path = MakeFullPathToSystem32(L"findstr.exe"); | 242 std::wstring exe_path = MakeFullPathToSystem32(L"findstr.exe"); |
283 string16 system32 = MakeFullPathToSystem32(L""); | 243 std::wstring system32 = MakeFullPathToSystem32(L""); |
284 ASSERT_TRUE(!exe_path.empty()); | 244 ASSERT_TRUE(!exe_path.empty()); |
285 EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_PROCESS, | 245 EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_PROCESS, |
286 TargetPolicy::PROCESS_MIN_EXEC, | 246 TargetPolicy::PROCESS_MIN_EXEC, |
287 exe_path.c_str())); | 247 exe_path.c_str())); |
288 | 248 |
289 // Need to add directory rules for the directories that we use in | 249 // Need to add directory rules for the directories that we use in |
290 // SetCurrentDirectory. | 250 // SetCurrentDirectory. |
291 EXPECT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_DIR_ANY, | 251 EXPECT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_DIR_ANY, |
292 system32.c_str())); | 252 system32.c_str())); |
293 | 253 |
294 wchar_t current_directory[MAX_PATH]; | 254 wchar_t current_directory[MAX_PATH]; |
295 DWORD ret = ::GetCurrentDirectory(MAX_PATH, current_directory); | 255 DWORD ret = ::GetCurrentDirectory(MAX_PATH, current_directory); |
296 ASSERT_TRUE(0 != ret && ret < MAX_PATH); | 256 ASSERT_TRUE(0 != ret && ret < MAX_PATH); |
297 | 257 |
298 wcscat_s(current_directory, MAX_PATH, L"\\"); | 258 wcscat_s(current_directory, MAX_PATH, L"\\"); |
299 EXPECT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_DIR_ANY, | 259 EXPECT_TRUE(runner.AddFsRule(TargetPolicy::FILES_ALLOW_DIR_ANY, |
300 current_directory)); | 260 current_directory)); |
301 | 261 |
302 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Process_RunApp1 calc.exe")); | 262 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Process_RunApp findstr.exe")); |
303 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Process_RunApp2 calc.exe")); | 263 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Process_RunApp calc.exe")); |
304 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Process_RunApp3 calc.exe")); | |
305 EXPECT_EQ(SBOX_TEST_SECOND_ERROR, | |
306 runner.RunTest(L"Process_RunApp4 calc.exe")); | |
307 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Process_RunApp5 calc.exe")); | |
308 EXPECT_EQ(SBOX_TEST_DENIED, runner.RunTest(L"Process_RunApp6 calc.exe")); | |
309 | |
310 EXPECT_EQ(SBOX_TEST_SUCCEEDED, | |
311 runner.RunTest(L"Process_RunApp1 findstr.exe")); | |
312 EXPECT_EQ(SBOX_TEST_SUCCEEDED, | |
313 runner.RunTest(L"Process_RunApp2 findstr.exe")); | |
314 EXPECT_EQ(SBOX_TEST_SUCCEEDED, | |
315 runner.RunTest(L"Process_RunApp3 findstr.exe")); | |
316 EXPECT_EQ(SBOX_TEST_SECOND_ERROR, | |
317 runner.RunTest(L"Process_RunApp4 findstr.exe")); | |
318 EXPECT_EQ(SBOX_TEST_SUCCEEDED, | |
319 runner.RunTest(L"Process_RunApp5 findstr.exe")); | |
320 EXPECT_EQ(SBOX_TEST_SUCCEEDED, | |
321 runner.RunTest(L"Process_RunApp6 findstr.exe")); | |
322 } | 264 } |
323 | 265 |
324 TEST(ProcessPolicyTest, OpenToken) { | 266 TEST(ProcessPolicyTest, OpenToken) { |
325 TestRunner runner; | 267 TestRunner runner; |
326 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Process_OpenToken")); | 268 EXPECT_EQ(SBOX_TEST_SUCCEEDED, runner.RunTest(L"Process_OpenToken")); |
327 } | 269 } |
328 | 270 |
329 TEST(ProcessPolicyTest, TestGetProcessTokenMinAccess) { | 271 TEST(ProcessPolicyTest, TestGetProcessTokenMinAccess) { |
330 TestRunner runner; | 272 TestRunner runner; |
331 string16 exe_path = MakeFullPathToSystem32(L"findstr.exe"); | 273 std::wstring exe_path = MakeFullPathToSystem32(L"findstr.exe"); |
332 ASSERT_TRUE(!exe_path.empty()); | 274 ASSERT_TRUE(!exe_path.empty()); |
333 EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_PROCESS, | 275 EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_PROCESS, |
334 TargetPolicy::PROCESS_MIN_EXEC, | 276 TargetPolicy::PROCESS_MIN_EXEC, |
335 exe_path.c_str())); | 277 exe_path.c_str())); |
336 | 278 |
337 EXPECT_EQ(SBOX_TEST_DENIED, | 279 EXPECT_EQ(SBOX_TEST_DENIED, |
338 runner.RunTest(L"Process_GetChildProcessToken findstr.exe")); | 280 runner.RunTest(L"Process_GetChildProcessToken findstr.exe")); |
339 } | 281 } |
340 | 282 |
341 TEST(ProcessPolicyTest, TestGetProcessTokenMaxAccess) { | 283 TEST(ProcessPolicyTest, TestGetProcessTokenMaxAccess) { |
342 TestRunner runner(JOB_UNPROTECTED, USER_INTERACTIVE, USER_INTERACTIVE); | 284 TestRunner runner(JOB_UNPROTECTED, USER_INTERACTIVE, USER_INTERACTIVE); |
343 string16 exe_path = MakeFullPathToSystem32(L"findstr.exe"); | 285 std::wstring exe_path = MakeFullPathToSystem32(L"findstr.exe"); |
344 ASSERT_TRUE(!exe_path.empty()); | 286 ASSERT_TRUE(!exe_path.empty()); |
345 EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_PROCESS, | 287 EXPECT_TRUE(runner.AddRule(TargetPolicy::SUBSYS_PROCESS, |
346 TargetPolicy::PROCESS_ALL_EXEC, | 288 TargetPolicy::PROCESS_ALL_EXEC, |
347 exe_path.c_str())); | 289 exe_path.c_str())); |
348 | 290 |
349 EXPECT_EQ(SBOX_TEST_SUCCEEDED, | 291 EXPECT_EQ(SBOX_TEST_SUCCEEDED, |
350 runner.RunTest(L"Process_GetChildProcessToken findstr.exe")); | 292 runner.RunTest(L"Process_GetChildProcessToken findstr.exe")); |
351 } | 293 } |
352 | 294 |
353 } // namespace sandbox | 295 } // namespace sandbox |
OLD | NEW |