| 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 "content/common/sandbox_mac.h" | 5 #include "content/common/sandbox_mac.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 } | 147 } |
| 148 | 148 |
| 149 // If we got here we know that the character in question is strictly | 149 // If we got here we know that the character in question is strictly |
| 150 // in the ASCII range so there's no need to do any kind of encoding | 150 // in the ASCII range so there's no need to do any kind of encoding |
| 151 // conversion. | 151 // conversion. |
| 152 dst->push_back(static_cast<char>(c)); | 152 dst->push_back(static_cast<char>(c)); |
| 153 } | 153 } |
| 154 return true; | 154 return true; |
| 155 } | 155 } |
| 156 | 156 |
| 157 // static | |
| 158 bool Sandbox::QuoteStringForRegex(const std::string& str_utf8, | |
| 159 std::string* dst) { | |
| 160 // Characters with special meanings in sandbox profile syntax. | |
| 161 const char regex_special_chars[] = { | |
| 162 '\\', | |
| 163 | |
| 164 // Metacharacters | |
| 165 '^', | |
| 166 '.', | |
| 167 '[', | |
| 168 ']', | |
| 169 '$', | |
| 170 '(', | |
| 171 ')', | |
| 172 '|', | |
| 173 | |
| 174 // Quantifiers | |
| 175 '*', | |
| 176 '+', | |
| 177 '?', | |
| 178 '{', | |
| 179 '}', | |
| 180 }; | |
| 181 | |
| 182 // Anchor regex at start of path. | |
| 183 dst->assign("^"); | |
| 184 | |
| 185 const char* src = str_utf8.c_str(); | |
| 186 int32_t length = str_utf8.length(); | |
| 187 int32_t position = 0; | |
| 188 while (position < length) { | |
| 189 UChar32 c; | |
| 190 U8_NEXT(src, position, length, c); // Macro increments |position|. | |
| 191 DCHECK_GE(c, 0); | |
| 192 if (c < 0) | |
| 193 return false; | |
| 194 | |
| 195 // The Mac sandbox regex parser only handles printable ASCII characters. | |
| 196 // 33 >= c <= 126 | |
| 197 if (c < 32 || c > 125) { | |
| 198 return false; | |
| 199 } | |
| 200 | |
| 201 for (size_t i = 0; i < arraysize(regex_special_chars); ++i) { | |
| 202 if (c == regex_special_chars[i]) { | |
| 203 dst->push_back('\\'); | |
| 204 break; | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 dst->push_back(static_cast<char>(c)); | |
| 209 } | |
| 210 | |
| 211 // Make sure last element of path is interpreted as a directory. Leaving this | |
| 212 // off would allow access to files if they start with the same name as the | |
| 213 // directory. | |
| 214 dst->append("(/|$)"); | |
| 215 | |
| 216 return true; | |
| 217 } | |
| 218 | |
| 219 // Warm up System APIs that empirically need to be accessed before the Sandbox | 157 // Warm up System APIs that empirically need to be accessed before the Sandbox |
| 220 // is turned on. | 158 // is turned on. |
| 221 // This method is layed out in blocks, each one containing a separate function | 159 // This method is layed out in blocks, each one containing a separate function |
| 222 // that needs to be warmed up. The OS version on which we found the need to | 160 // that needs to be warmed up. The OS version on which we found the need to |
| 223 // enable the function is also noted. | 161 // enable the function is also noted. |
| 224 // This function is tested on the following OS versions: | 162 // This function is tested on the following OS versions: |
| 225 // 10.5.6, 10.6.0 | 163 // 10.5.6, 10.6.0 |
| 226 | 164 |
| 227 // static | 165 // static |
| 228 void Sandbox::SandboxWarmup(int sandbox_type) { | 166 void Sandbox::SandboxWarmup(int sandbox_type) { |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 std::string sandbox_data = LoadSandboxTemplate(sandbox_type); | 324 std::string sandbox_data = LoadSandboxTemplate(sandbox_type); |
| 387 if (sandbox_data.empty()) { | 325 if (sandbox_data.empty()) { |
| 388 return false; | 326 return false; |
| 389 } | 327 } |
| 390 | 328 |
| 391 sandbox::SandboxCompiler compiler(sandbox_data); | 329 sandbox::SandboxCompiler compiler(sandbox_data); |
| 392 | 330 |
| 393 if (!allowed_dir.empty()) { | 331 if (!allowed_dir.empty()) { |
| 394 // Add the sandbox parameters necessary to access the given directory. | 332 // Add the sandbox parameters necessary to access the given directory. |
| 395 base::FilePath allowed_dir_canonical = GetCanonicalSandboxPath(allowed_dir); | 333 base::FilePath allowed_dir_canonical = GetCanonicalSandboxPath(allowed_dir); |
| 396 std::string regex; | 334 std::string quoted_dir; |
| 397 if (!QuoteStringForRegex(allowed_dir_canonical.value(), ®ex)) { | 335 if (!QuotePlainString(allowed_dir_canonical.value(), "ed_dir)) { |
| 398 FatalStringQuoteException(allowed_dir_canonical.value()); | 336 FatalStringQuoteException(allowed_dir_canonical.value()); |
| 399 return false; | 337 return false; |
| 400 } | 338 } |
| 401 if (!compiler.InsertStringParam("PERMITTED_DIR", regex)) | 339 if (!compiler.InsertStringParam("PERMITTED_DIR", quoted_dir)) |
| 402 return false; | 340 return false; |
| 403 } | 341 } |
| 404 | 342 |
| 405 // Enable verbose logging if enabled on the command line. (See common.sb | 343 // Enable verbose logging if enabled on the command line. (See common.sb |
| 406 // for details). | 344 // for details). |
| 407 const base::CommandLine* command_line = | 345 const base::CommandLine* command_line = |
| 408 base::CommandLine::ForCurrentProcess(); | 346 base::CommandLine::ForCurrentProcess(); |
| 409 bool enable_logging = | 347 bool enable_logging = |
| 410 command_line->HasSwitch(switches::kEnableSandboxLogging);; | 348 command_line->HasSwitch(switches::kEnableSandboxLogging);; |
| 411 if (!compiler.InsertBooleanParam("ENABLE_LOGGING", enable_logging)) | 349 if (!compiler.InsertBooleanParam("ENABLE_LOGGING", enable_logging)) |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 if (HANDLE_EINTR(fcntl(fd.get(), F_GETPATH, canonical_path)) != 0) { | 401 if (HANDLE_EINTR(fcntl(fd.get(), F_GETPATH, canonical_path)) != 0) { |
| 464 DPLOG(FATAL) << "GetCanonicalSandboxPath() failed for: " | 402 DPLOG(FATAL) << "GetCanonicalSandboxPath() failed for: " |
| 465 << path.value(); | 403 << path.value(); |
| 466 return path; | 404 return path; |
| 467 } | 405 } |
| 468 | 406 |
| 469 return base::FilePath(canonical_path); | 407 return base::FilePath(canonical_path); |
| 470 } | 408 } |
| 471 | 409 |
| 472 } // namespace content | 410 } // namespace content |
| OLD | NEW |