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 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 #include <dirent.h> | 6 #include <dirent.h> |
7 | 7 |
8 extern "C" { | 8 extern "C" { |
9 #include <sandbox.h> | 9 #include <sandbox.h> |
10 } | 10 } |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 | 176 |
177 EXPECT_TRUE(CheckSandbox(sandbox_dir.value())); | 177 EXPECT_TRUE(CheckSandbox(sandbox_dir.value())); |
178 } | 178 } |
179 } | 179 } |
180 | 180 |
181 MULTIPROCESS_TEST_MAIN(mac_sandbox_path_access) { | 181 MULTIPROCESS_TEST_MAIN(mac_sandbox_path_access) { |
182 char *sandbox_allowed_dir = getenv(kSandboxAccessPathKey); | 182 char *sandbox_allowed_dir = getenv(kSandboxAccessPathKey); |
183 if (!sandbox_allowed_dir) | 183 if (!sandbox_allowed_dir) |
184 return -1; | 184 return -1; |
185 | 185 |
| 186 std::string final_allowed_dir; |
| 187 EXPECT_TRUE( |
| 188 Sandbox::QuoteStringForRegex(sandbox_allowed_dir, &final_allowed_dir)); |
| 189 |
186 // Build up a sandbox profile that only allows access to a single directory. | 190 // Build up a sandbox profile that only allows access to a single directory. |
187 NSString *sandbox_profile = | 191 std::string sandbox_profile = |
188 @"(version 1)" \ | 192 "(version 1)" |
189 "(deny default)" \ | 193 "(define perm_dir (param \"PERMITTED_DIR\"))" |
190 "(allow signal (target self))" \ | 194 "(deny default)" |
191 "(allow sysctl-read)" \ | 195 "(allow signal (target self))" |
192 ";ENABLE_DIRECTORY_ACCESS"; | 196 "(allow sysctl-read)" |
| 197 "(if (string? perm_dir)" |
| 198 " (begin" |
| 199 " (allow file-read-metadata )" |
| 200 " (allow file-read* file-write* (regex (string-append #\"\" " |
| 201 "perm_dir)))))"; |
193 | 202 |
194 std::string allowed_dir(sandbox_allowed_dir); | 203 // Setup the parameters to pass to the sandbox. |
195 Sandbox::SandboxVariableSubstitions substitutions; | 204 SandboxCompiler compiler(sandbox_profile); |
196 NSString* allow_dir_sandbox_code = | 205 CHECK(compiler.InsertStringParam("PERMITTED_DIR", final_allowed_dir)); |
197 Sandbox::BuildAllowDirectoryAccessSandboxString( | |
198 base::FilePath(sandbox_allowed_dir), | |
199 &substitutions); | |
200 sandbox_profile = [sandbox_profile | |
201 stringByReplacingOccurrencesOfString:@";ENABLE_DIRECTORY_ACCESS" | |
202 withString:allow_dir_sandbox_code]; | |
203 | 206 |
204 std::string final_sandbox_profile_str; | 207 // Enable Sandbox. |
205 if (!Sandbox::PostProcessSandboxProfile(sandbox_profile, | 208 std::string error_str; |
206 [NSArray array], | 209 if (!compiler.CompileAndApplyProfile(&error_str)) { |
207 substitutions, | 210 LOG(ERROR) << "Failed to Initialize Sandbox: " << error_str; |
208 &final_sandbox_profile_str)) { | |
209 LOG(ERROR) << "Call to PostProcessSandboxProfile() failed"; | |
210 return -1; | 211 return -1; |
211 } | 212 } |
212 | 213 |
213 // Enable Sandbox. | |
214 char* error_buff = NULL; | |
215 int error = sandbox_init(final_sandbox_profile_str.c_str(), 0, &error_buff); | |
216 if (error == -1) { | |
217 LOG(ERROR) << "Failed to Initialize Sandbox: " << error_buff; | |
218 return -1; | |
219 } | |
220 sandbox_free_error(error_buff); | |
221 | |
222 // Test Sandbox. | 214 // Test Sandbox. |
223 | 215 |
224 // We should be able to list the contents of the sandboxed directory. | 216 // We should be able to list the contents of the sandboxed directory. |
225 DIR *file_list = NULL; | 217 DIR *file_list = NULL; |
226 file_list = opendir(sandbox_allowed_dir); | 218 file_list = opendir(sandbox_allowed_dir); |
227 if (!file_list) { | 219 if (!file_list) { |
228 PLOG(ERROR) << "Sandbox overly restrictive: call to opendir(" | 220 PLOG(ERROR) << "Sandbox overly restrictive: call to opendir(" |
229 << sandbox_allowed_dir | 221 << sandbox_allowed_dir |
230 << ") failed"; | 222 << ") failed"; |
231 return -1; | 223 return -1; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 PLOG(ERROR) << "Sandbox breach: was able to write (" | 292 PLOG(ERROR) << "Sandbox breach: was able to write (" |
301 << denied_file2.value() | 293 << denied_file2.value() |
302 << ")"; | 294 << ")"; |
303 return -1; | 295 return -1; |
304 } | 296 } |
305 | 297 |
306 return 0; | 298 return 0; |
307 } | 299 } |
308 | 300 |
309 } // namespace content | 301 } // namespace content |
OLD | NEW |