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.
5 #include "content/common/sandbox_mac.h"
7 #import <Cocoa/Cocoa.h>
9 #include <CoreFoundation/CFTimeZone.h>
14 #include <sys/param.h>
16 #include "base/basictypes.h"
17 #include "base/command_line.h"
18 #include "base/compiler_specific.h"
19 #include "base/files/file_util.h"
20 #include "base/files/scoped_file.h"
21 #include "base/mac/bundle_locations.h"
22 #include "base/mac/foundation_util.h"
23 #include "base/mac/mac_util.h"
24 #include "base/mac/scoped_cftyperef.h"
25 #include "base/mac/scoped_nsautorelease_pool.h"
26 #include "base/mac/scoped_nsobject.h"
27 #include "base/rand_util.h"
28 #include "base/strings/string16.h"
29 #include "base/strings/string_piece.h"
30 #include "base/strings/string_util.h"
31 #include "base/strings/stringprintf.h"
32 #include "base/strings/sys_string_conversions.h"
33 #include "base/strings/utf_string_conversions.h"
34 #include "base/sys_info.h"
35 #include "content/common/gpu/media/vt_video_decode_accelerator.h"
36 #include "content/grit/content_resources.h"
37 #include "content/public/common/content_client.h"
38 #include "content/public/common/content_switches.h"
39 #include "third_party/icu/source/common/unicode/uchar.h"
40 #include "ui/base/layout.h"
41 #include "ui/gl/gl_surface.h"
44 void CGSSetDenyWindowServerConnections(bool);
45 void CGSShutdownServerConnections();
51 // Is the sandbox currently active.
52 bool gSandboxIsActive = false;
54 struct SandboxTypeToResourceIDMapping {
55 SandboxType sandbox_type;
56 int sandbox_profile_resource_id;
59 // Mapping from sandbox process types to resource IDs containing the sandbox
60 // profile for all process types known to content.
61 SandboxTypeToResourceIDMapping kDefaultSandboxTypeToResourceIDMapping[] = {
62 { SANDBOX_TYPE_RENDERER, IDR_RENDERER_SANDBOX_PROFILE },
63 { SANDBOX_TYPE_UTILITY, IDR_UTILITY_SANDBOX_PROFILE },
64 { SANDBOX_TYPE_GPU, IDR_GPU_SANDBOX_PROFILE },
65 { SANDBOX_TYPE_PPAPI, IDR_PPAPI_SANDBOX_PROFILE },
68 static_assert(arraysize(kDefaultSandboxTypeToResourceIDMapping) == \
69 size_t(SANDBOX_TYPE_AFTER_LAST_TYPE), \
70 "sandbox type to resource id mapping incorrect");
72 // Try to escape |c| as a "SingleEscapeCharacter" (\n, etc). If successful,
73 // returns true and appends the escape sequence to |dst|.
74 bool EscapeSingleChar(char c, std::string* dst) {
75 const char *append = NULL;
108 // Errors quoting strings for the Sandbox profile are always fatal, report them
109 // in a central place.
110 NOINLINE void FatalStringQuoteException(const std::string& str) {
111 // Copy bad string to the stack so it's recorded in the crash dump.
112 char bad_string[256] = {0};
113 base::strlcpy(bad_string, str.c_str(), arraysize(bad_string));
114 DLOG(FATAL) << "String quoting failed " << bad_string;
120 NSString* Sandbox::AllowMetadataForPath(const base::FilePath& allowed_path) {
121 // Collect a list of all parent directories.
122 base::FilePath last_path = allowed_path;
123 std::vector<base::FilePath> subpaths;
124 for (base::FilePath path = allowed_path;
125 path.value() != last_path.value();
126 path = path.DirName()) {
127 subpaths.push_back(path);
131 // Iterate through all parents and allow stat() on them explicitly.
132 NSString* sandbox_command = @"(allow file-read-metadata ";
133 for (std::vector<base::FilePath>::reverse_iterator i = subpaths.rbegin();
134 i != subpaths.rend();
136 std::string subdir_escaped;
137 if (!QuotePlainString(i->value(), &subdir_escaped)) {
138 FatalStringQuoteException(i->value());
142 NSString* subdir_escaped_ns =
143 base::SysUTF8ToNSString(subdir_escaped.c_str());
145 [sandbox_command stringByAppendingFormat:@"(literal \"%@\")",
149 return [sandbox_command stringByAppendingString:@")"];
153 bool Sandbox::QuotePlainString(const std::string& src_utf8, std::string* dst) {
156 const char* src = src_utf8.c_str();
157 int32_t length = src_utf8.length();
158 int32_t position = 0;
159 while (position < length) {
161 U8_NEXT(src, position, length, c); // Macro increments |position|.
166 if (c < 128) { // EscapeSingleChar only handles ASCII.
167 char as_char = static_cast<char>(c);
168 if (EscapeSingleChar(as_char, dst)) {
173 if (c < 32 || c > 126) {
174 // Any characters that aren't printable ASCII get the \u treatment.
175 unsigned int as_uint = static_cast<unsigned int>(c);
176 base::StringAppendF(dst, "\\u%04X", as_uint);
180 // If we got here we know that the character in question is strictly
181 // in the ASCII range so there's no need to do any kind of encoding
183 dst->push_back(static_cast<char>(c));
189 bool Sandbox::QuoteStringForRegex(const std::string& str_utf8,
191 // Characters with special meanings in sandbox profile syntax.
192 const char regex_special_chars[] = {
213 // Anchor regex at start of path.
216 const char* src = str_utf8.c_str();
217 int32_t length = str_utf8.length();
218 int32_t position = 0;
219 while (position < length) {
221 U8_NEXT(src, position, length, c); // Macro increments |position|.
226 // The Mac sandbox regex parser only handles printable ASCII characters.
228 if (c < 32 || c > 125) {
232 for (size_t i = 0; i < arraysize(regex_special_chars); ++i) {
233 if (c == regex_special_chars[i]) {
234 dst->push_back('\\');
239 dst->push_back(static_cast<char>(c));
242 // Make sure last element of path is interpreted as a directory. Leaving this
243 // off would allow access to files if they start with the same name as the
245 dst->append("(/|$)");
250 // Warm up System APIs that empirically need to be accessed before the Sandbox
252 // This method is layed out in blocks, each one containing a separate function
253 // that needs to be warmed up. The OS version on which we found the need to
254 // enable the function is also noted.
255 // This function is tested on the following OS versions:
259 void Sandbox::SandboxWarmup(int sandbox_type) {
260 base::mac::ScopedNSAutoreleasePool scoped_pool;
262 { // CGColorSpaceCreateWithName(), CGBitmapContextCreate() - 10.5.6
263 base::ScopedCFTypeRef<CGColorSpaceRef> rgb_colorspace(
264 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
266 // Allocate a 1x1 image.
268 base::ScopedCFTypeRef<CGContextRef> context(CGBitmapContextCreate(
275 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
277 // Load in the color profiles we'll need (as a side effect).
278 ignore_result(base::mac::GetSRGBColorSpace());
279 ignore_result(base::mac::GetSystemColorSpace());
281 // CGColorSpaceCreateSystemDefaultCMYK - 10.6
282 base::ScopedCFTypeRef<CGColorSpaceRef> cmyk_colorspace(
283 CGColorSpaceCreateWithName(kCGColorSpaceGenericCMYK));
286 { // localtime() - 10.5.6
291 { // Gestalt() tries to read /System/Library/CoreServices/SystemVersion.plist
294 base::SysInfo::OperatingSystemVersionNumbers(&tmp, &tmp, &tmp);
297 { // CGImageSourceGetStatus() - 10.6
298 // Create a png with just enough data to get everything warmed up...
299 char png_header[] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
300 NSData* data = [NSData dataWithBytes:png_header
301 length:arraysize(png_header)];
302 base::ScopedCFTypeRef<CGImageSourceRef> img(
303 CGImageSourceCreateWithData((CFDataRef)data, NULL));
304 CGImageSourceGetStatus(img);
308 // Allow access to /dev/urandom.
309 base::GetUrandomFD();
312 { // IOSurfaceLookup() - 10.7
313 // Needed by zero-copy texture update framework - crbug.com/323338
314 base::ScopedCFTypeRef<IOSurfaceRef> io_surface(IOSurfaceLookup(0));
317 // Process-type dependent warm-up.
318 if (sandbox_type == SANDBOX_TYPE_UTILITY) {
319 // CFTimeZoneCopyZone() tries to read /etc and /private/etc/localtime - 10.8
320 // Needed by Media Galleries API Picasa - crbug.com/151701
321 CFTimeZoneCopySystem();
324 if (sandbox_type == SANDBOX_TYPE_GPU) {
325 // Preload either the desktop GL or the osmesa so, depending on the
327 gfx::GLSurface::InitializeOneOff();
329 // Preload VideoToolbox.
330 InitializeVideoToolbox();
333 if (sandbox_type == SANDBOX_TYPE_PPAPI) {
334 // Preload AppKit color spaces used for Flash/ppapi. http://crbug.com/348304
335 NSColor* color = [NSColor controlTextColor];
336 [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
339 if (sandbox_type == SANDBOX_TYPE_RENDERER &&
340 base::mac::IsOSMountainLionOrLater()) {
341 // Now disconnect from WindowServer, after all objects have been warmed up.
342 // Shutting down the connection requires connecting to WindowServer,
343 // so do this before actually engaging the sandbox. This is only done on
344 // 10.8 and higher because doing it on earlier OSes causes layout tests to
345 // fail <http://crbug.com/397642#c48>. This may cause two log messages to
346 // be printed to the system logger on certain OS versions.
347 CGSSetDenyWindowServerConnections(true);
348 CGSShutdownServerConnections();
353 NSString* Sandbox::BuildAllowDirectoryAccessSandboxString(
354 const base::FilePath& allowed_dir,
355 SandboxVariableSubstitions* substitutions) {
356 // A whitelist is used to determine which directories can be statted
357 // This means that in the case of an /a/b/c/d/ directory, we may be able to
358 // stat the leaf directory, but not its parent.
359 // The extension code in Chrome calls realpath() which fails if it can't call
360 // stat() on one of the parent directories in the path.
361 // The solution to this is to allow statting the parent directories themselves
362 // but not their contents. We need to add a separate rule for each parent
365 // The sandbox only understands "real" paths. This resolving step is
366 // needed so the caller doesn't need to worry about things like /var
367 // being a link to /private/var (like in the paths CreateNewTempDirectory()
369 base::FilePath allowed_dir_canonical = GetCanonicalSandboxPath(allowed_dir);
371 NSString* sandbox_command = AllowMetadataForPath(allowed_dir_canonical);
372 sandbox_command = [sandbox_command
373 substringToIndex:[sandbox_command length] - 1]; // strip trailing ')'
375 // Finally append the leaf directory. Unlike its parents (for which only
376 // stat() should be allowed), the leaf directory needs full access.
377 (*substitutions)["ALLOWED_DIR"] =
378 SandboxSubstring(allowed_dir_canonical.value(),
379 SandboxSubstring::REGEX);
382 stringByAppendingString:@") (allow file-read* file-write*"
383 " (regex #\"@ALLOWED_DIR@\") )"];
384 return sandbox_command;
387 // Load the appropriate template for the given sandbox type.
388 // Returns the template as an NSString or nil on error.
389 NSString* LoadSandboxTemplate(int sandbox_type) {
390 // We use a custom sandbox definition to lock things down as tightly as
392 int sandbox_profile_resource_id = -1;
394 // Find resource id for sandbox profile to use for the specific sandbox type.
396 i < arraysize(kDefaultSandboxTypeToResourceIDMapping);
398 if (kDefaultSandboxTypeToResourceIDMapping[i].sandbox_type ==
400 sandbox_profile_resource_id =
401 kDefaultSandboxTypeToResourceIDMapping[i].sandbox_profile_resource_id;
405 if (sandbox_profile_resource_id == -1) {
406 // Check if the embedder knows about this sandbox process type.
407 bool sandbox_type_found =
408 GetContentClient()->GetSandboxProfileForSandboxType(
409 sandbox_type, &sandbox_profile_resource_id);
410 CHECK(sandbox_type_found) << "Unknown sandbox type " << sandbox_type;
413 base::StringPiece sandbox_definition =
414 GetContentClient()->GetDataResource(
415 sandbox_profile_resource_id, ui::SCALE_FACTOR_NONE);
416 if (sandbox_definition.empty()) {
417 LOG(FATAL) << "Failed to load the sandbox profile (resource id "
418 << sandbox_profile_resource_id << ")";
422 base::StringPiece common_sandbox_definition =
423 GetContentClient()->GetDataResource(
424 IDR_COMMON_SANDBOX_PROFILE, ui::SCALE_FACTOR_NONE);
425 if (common_sandbox_definition.empty()) {
426 LOG(FATAL) << "Failed to load the common sandbox profile";
430 base::scoped_nsobject<NSString> common_sandbox_prefix_data(
431 [[NSString alloc] initWithBytes:common_sandbox_definition.data()
432 length:common_sandbox_definition.length()
433 encoding:NSUTF8StringEncoding]);
435 base::scoped_nsobject<NSString> sandbox_data(
436 [[NSString alloc] initWithBytes:sandbox_definition.data()
437 length:sandbox_definition.length()
438 encoding:NSUTF8StringEncoding]);
440 // Prefix sandbox_data with common_sandbox_prefix_data.
441 return [common_sandbox_prefix_data stringByAppendingString:sandbox_data];
445 bool Sandbox::PostProcessSandboxProfile(
446 NSString* sandbox_template,
447 NSArray* comments_to_remove,
448 SandboxVariableSubstitions& substitutions,
449 std::string *final_sandbox_profile_str) {
450 NSString* sandbox_data = [[sandbox_template copy] autorelease];
452 // Remove comments, e.g. ;10.7_OR_ABOVE .
453 for (NSString* to_remove in comments_to_remove) {
454 sandbox_data = [sandbox_data stringByReplacingOccurrencesOfString:to_remove
458 // Split string on "@" characters.
459 std::vector<std::string> raw_sandbox_pieces;
460 if (Tokenize([sandbox_data UTF8String], "@", &raw_sandbox_pieces) == 0) {
461 DLOG(FATAL) << "Bad Sandbox profile, should contain at least one token ("
462 << [sandbox_data UTF8String]
467 // Iterate over string pieces and substitute variables, escaping as necessary.
468 size_t output_string_length = 0;
469 std::vector<std::string> processed_sandbox_pieces(raw_sandbox_pieces.size());
470 for (std::vector<std::string>::iterator it = raw_sandbox_pieces.begin();
471 it != raw_sandbox_pieces.end();
473 std::string new_piece;
474 SandboxVariableSubstitions::iterator replacement_it =
475 substitutions.find(*it);
476 if (replacement_it == substitutions.end()) {
479 // Found something to substitute.
480 SandboxSubstring& replacement = replacement_it->second;
481 switch (replacement.type()) {
482 case SandboxSubstring::PLAIN:
483 new_piece = replacement.value();
486 case SandboxSubstring::LITERAL:
487 if (!QuotePlainString(replacement.value(), &new_piece))
488 FatalStringQuoteException(replacement.value());
491 case SandboxSubstring::REGEX:
492 if (!QuoteStringForRegex(replacement.value(), &new_piece))
493 FatalStringQuoteException(replacement.value());
497 output_string_length += new_piece.size();
498 processed_sandbox_pieces.push_back(new_piece);
501 // Build final output string.
502 final_sandbox_profile_str->reserve(output_string_length);
504 for (std::vector<std::string>::iterator it = processed_sandbox_pieces.begin();
505 it != processed_sandbox_pieces.end();
507 final_sandbox_profile_str->append(*it);
513 // Turns on the OS X sandbox for this process.
516 bool Sandbox::EnableSandbox(int sandbox_type,
517 const base::FilePath& allowed_dir) {
518 // Sanity - currently only SANDBOX_TYPE_UTILITY supports a directory being
520 if (sandbox_type < SANDBOX_TYPE_AFTER_LAST_TYPE &&
521 sandbox_type != SANDBOX_TYPE_UTILITY) {
522 DCHECK(allowed_dir.empty())
523 << "Only SANDBOX_TYPE_UTILITY allows a custom directory parameter.";
526 NSString* sandbox_data = LoadSandboxTemplate(sandbox_type);
531 SandboxVariableSubstitions substitutions;
532 if (!allowed_dir.empty()) {
533 // Add the sandbox commands necessary to access the given directory.
534 // Note: this function must be called before PostProcessSandboxProfile()
535 // since the string it inserts contains variables that need substitution.
536 NSString* allowed_dir_sandbox_command =
537 BuildAllowDirectoryAccessSandboxString(allowed_dir, &substitutions);
539 if (allowed_dir_sandbox_command) { // May be nil if function fails.
540 sandbox_data = [sandbox_data
541 stringByReplacingOccurrencesOfString:@";ENABLE_DIRECTORY_ACCESS"
542 withString:allowed_dir_sandbox_command];
546 NSMutableArray* tokens_to_remove = [NSMutableArray array];
548 // Enable verbose logging if enabled on the command line. (See common.sb
550 const base::CommandLine* command_line =
551 base::CommandLine::ForCurrentProcess();
552 bool enable_logging =
553 command_line->HasSwitch(switches::kEnableSandboxLogging);;
554 if (enable_logging) {
555 [tokens_to_remove addObject:@";ENABLE_LOGGING"];
558 bool lion_or_later = base::mac::IsOSLionOrLater();
560 // Without this, the sandbox will print a message to the system log every
561 // time it denies a request. This floods the console with useless spew.
562 if (!enable_logging) {
563 substitutions["DISABLE_SANDBOX_DENIAL_LOGGING"] =
564 SandboxSubstring("(with no-log)");
566 substitutions["DISABLE_SANDBOX_DENIAL_LOGGING"] = SandboxSubstring("");
569 // Splice the path of the user's home directory into the sandbox profile
570 // (see renderer.sb for details).
571 std::string home_dir = [NSHomeDirectory() fileSystemRepresentation];
573 base::FilePath home_dir_canonical =
574 GetCanonicalSandboxPath(base::FilePath(home_dir));
576 substitutions["USER_HOMEDIR_AS_LITERAL"] =
577 SandboxSubstring(home_dir_canonical.value(),
578 SandboxSubstring::LITERAL);
581 // >=10.7 Sandbox rules.
582 [tokens_to_remove addObject:@";10.7_OR_ABOVE"];
585 substitutions["COMPONENT_BUILD_WORKAROUND"] = SandboxSubstring("");
586 #if defined(COMPONENT_BUILD)
587 // dlopen() fails without file-read-metadata access if the executable image
588 // contains LC_RPATH load commands. The components build uses those.
589 // See http://crbug.com/127465
590 if (base::mac::IsOSSnowLeopard()) {
591 base::FilePath bundle_executable = base::mac::NSStringToFilePath(
592 [base::mac::MainBundle() executablePath]);
593 NSString* sandbox_command = AllowMetadataForPath(
594 GetCanonicalSandboxPath(bundle_executable));
595 substitutions["COMPONENT_BUILD_WORKAROUND"] =
596 SandboxSubstring(base::SysNSStringToUTF8(sandbox_command));
600 // All information needed to assemble the final profile has been collected.
601 // Merge it all together.
602 std::string final_sandbox_profile_str;
603 if (!PostProcessSandboxProfile(sandbox_data, tokens_to_remove, substitutions,
604 &final_sandbox_profile_str)) {
608 // Initialize sandbox.
609 char* error_buff = NULL;
610 int error = sandbox_init(final_sandbox_profile_str.c_str(), 0, &error_buff);
611 bool success = (error == 0 && error_buff == NULL);
612 DLOG_IF(FATAL, !success) << "Failed to initialize sandbox: "
616 sandbox_free_error(error_buff);
617 gSandboxIsActive = success;
622 bool Sandbox::SandboxIsCurrentlyActive() {
623 return gSandboxIsActive;
627 base::FilePath Sandbox::GetCanonicalSandboxPath(const base::FilePath& path) {
628 base::ScopedFD fd(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY)));
629 if (!fd.is_valid()) {
630 DPLOG(FATAL) << "GetCanonicalSandboxPath() failed for: "
635 base::FilePath::CharType canonical_path[MAXPATHLEN];
636 if (HANDLE_EINTR(fcntl(fd.get(), F_GETPATH, canonical_path)) != 0) {
637 DPLOG(FATAL) << "GetCanonicalSandboxPath() failed for: "
642 return base::FilePath(canonical_path);
645 } // namespace content