| 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 if (!compiler.Init()) { |
| 197 Sandbox::BuildAllowDirectoryAccessSandboxString( | 206 LOG(ERROR) << "Could not initialize sandbox compiler"; |
| 198 base::FilePath(sandbox_allowed_dir), | 207 return -1; |
| 199 &substitutions); | 208 } |
| 200 sandbox_profile = [sandbox_profile | 209 compiler.InsertStringParam("PERMITTED_DIR", final_allowed_dir); |
| 201 stringByReplacingOccurrencesOfString:@";ENABLE_DIRECTORY_ACCESS" | |
| 202 withString:allow_dir_sandbox_code]; | |
| 203 | 210 |
| 204 std::string final_sandbox_profile_str; | 211 // Enable Sandbox. |
| 205 if (!Sandbox::PostProcessSandboxProfile(sandbox_profile, | 212 std::string error_str; |
| 206 [NSArray array], | 213 int error = compiler.CompileAndApplyProfile(&error_str); |
| 207 substitutions, | 214 if (error != 0) { |
| 208 &final_sandbox_profile_str)) { | 215 LOG(ERROR) << "Failed to Initialize Sandbox: " << error_str; |
| 209 LOG(ERROR) << "Call to PostProcessSandboxProfile() failed"; | |
| 210 return -1; | 216 return -1; |
| 211 } | 217 } |
| 212 | 218 |
| 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. | 219 // Test Sandbox. |
| 223 | 220 |
| 224 // We should be able to list the contents of the sandboxed directory. | 221 // We should be able to list the contents of the sandboxed directory. |
| 225 DIR *file_list = NULL; | 222 DIR *file_list = NULL; |
| 226 file_list = opendir(sandbox_allowed_dir); | 223 file_list = opendir(sandbox_allowed_dir); |
| 227 if (!file_list) { | 224 if (!file_list) { |
| 228 PLOG(ERROR) << "Sandbox overly restrictive: call to opendir(" | 225 PLOG(ERROR) << "Sandbox overly restrictive: call to opendir(" |
| 229 << sandbox_allowed_dir | 226 << sandbox_allowed_dir |
| 230 << ") failed"; | 227 << ") failed"; |
| 231 return -1; | 228 return -1; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 PLOG(ERROR) << "Sandbox breach: was able to write (" | 297 PLOG(ERROR) << "Sandbox breach: was able to write (" |
| 301 << denied_file2.value() | 298 << denied_file2.value() |
| 302 << ")"; | 299 << ")"; |
| 303 return -1; | 300 return -1; |
| 304 } | 301 } |
| 305 | 302 |
| 306 return 0; | 303 return 0; |
| 307 } | 304 } |
| 308 | 305 |
| 309 } // namespace content | 306 } // namespace content |
| OLD | NEW |