| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/common/sandbox_mac.h" | 5 #include "chrome/common/sandbox_mac.h" |
| 6 | 6 |
| 7 #include "base/debug_util.h" | 7 #include "base/debug_util.h" |
| 8 | 8 |
| 9 #import <Cocoa/Cocoa.h> | 9 #import <Cocoa/Cocoa.h> |
| 10 extern "C" { | 10 extern "C" { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 NSData* data = [NSData dataWithBytes:png_header | 77 NSData* data = [NSData dataWithBytes:png_header |
| 78 length:arraysize(png_header)]; | 78 length:arraysize(png_header)]; |
| 79 scoped_cftyperef<CGImageSourceRef> img( | 79 scoped_cftyperef<CGImageSourceRef> img( |
| 80 CGImageSourceCreateWithData((CFDataRef)data, | 80 CGImageSourceCreateWithData((CFDataRef)data, |
| 81 NULL)); | 81 NULL)); |
| 82 CGImageSourceGetStatus(img); | 82 CGImageSourceGetStatus(img); |
| 83 } | 83 } |
| 84 } | 84 } |
| 85 | 85 |
| 86 // Turns on the OS X sandbox for this process. | 86 // Turns on the OS X sandbox for this process. |
| 87 bool EnableSandbox() { | 87 bool EnableSandbox(SandboxProcessType sandbox_type, |
| 88 // For the renderer, we give it a custom sandbox to lock things down as | 88 const FilePath& allowed_dir) { |
| 89 // tightly as possible, while still enabling drawing. | 89 // Sanity - currently only SANDBOX_TYPE_UTILITY supports a directory being |
| 90 // passed in. |
| 91 if (sandbox_type != SANDBOX_TYPE_UTILITY) { |
| 92 DCHECK(allowed_dir.empty()) |
| 93 << "Only SANDBOX_TYPE_UTILITY allows a custom directory parameter."; |
| 94 } else { |
| 95 DCHECK(!allowed_dir.empty()) |
| 96 << "SANDBOX_TYPE_UTILITY " |
| 97 << "needs a custom directory parameter, but an empty one was provided."; |
| 98 } |
| 99 |
| 100 // We use a custom sandbox definition file to lock things down as |
| 101 // tightly as possible. |
| 102 // TODO(jeremy): Look at using include syntax to unify common parts of sandbox |
| 103 // definition files. |
| 104 NSString* sandbox_config_filename = nil; |
| 105 switch (sandbox_type) { |
| 106 case SANDBOX_TYPE_RENDERER: |
| 107 sandbox_config_filename = @"renderer"; |
| 108 break; |
| 109 case SANDBOX_TYPE_WORKER: |
| 110 sandbox_config_filename = @"worker"; |
| 111 break; |
| 112 case SANDBOX_TYPE_UTILITY: |
| 113 sandbox_config_filename = @"utility"; |
| 114 break; |
| 115 default: |
| 116 NOTREACHED(); |
| 117 return false; |
| 118 } |
| 119 |
| 90 NSString* sandbox_profile_path = | 120 NSString* sandbox_profile_path = |
| 91 [mac_util::MainAppBundle() pathForResource:@"renderer" ofType:@"sb"]; | 121 [mac_util::MainAppBundle() pathForResource:sandbox_config_filename |
| 122 ofType:@"sb"]; |
| 92 NSString* sandbox_data = [NSString | 123 NSString* sandbox_data = [NSString |
| 93 stringWithContentsOfFile:sandbox_profile_path | 124 stringWithContentsOfFile:sandbox_profile_path |
| 94 encoding:NSUTF8StringEncoding | 125 encoding:NSUTF8StringEncoding |
| 95 error:nil]; | 126 error:nil]; |
| 96 | 127 |
| 97 if (!sandbox_data) { | 128 if (!sandbox_data) { |
| 98 LOG(ERROR) << "Failed to find the sandbox profile on disk"; | 129 LOG(ERROR) << "Failed to find the sandbox profile on disk"; |
| 99 return false; | 130 return false; |
| 100 } | 131 } |
| 101 | 132 |
| 102 // Enable verbose logging if enabled on the command line. | 133 // Enable verbose logging if enabled on the command line. |
| 103 // (see renderer.sb for details). | 134 // (see renderer.sb for details). |
| 104 const CommandLine *command_line = CommandLine::ForCurrentProcess(); | 135 const CommandLine *command_line = CommandLine::ForCurrentProcess(); |
| 105 if (command_line->HasSwitch(switches::kEnableSandboxLogging)) { | 136 if (command_line->HasSwitch(switches::kEnableSandboxLogging)) { |
| 106 sandbox_data = [sandbox_data | 137 sandbox_data = [sandbox_data |
| 107 stringByReplacingOccurrencesOfString:@";ENABLE_LOGGING" | 138 stringByReplacingOccurrencesOfString:@";ENABLE_LOGGING" |
| 108 withString:@""]; | 139 withString:@""]; |
| 109 } | 140 } |
| 110 | 141 |
| 142 if (!allowed_dir.empty()) { |
| 143 NSString* allowed_dir_ns = base::SysUTF8ToNSString(allowed_dir.value()); |
| 144 sandbox_data = [sandbox_data |
| 145 stringByReplacingOccurrencesOfString:@"DIR_TO_ALLOW_ACCESS" |
| 146 withString:allowed_dir_ns]; |
| 147 } |
| 148 |
| 111 int32 major_version, minor_version, bugfix_version; | 149 int32 major_version, minor_version, bugfix_version; |
| 112 base::SysInfo::OperatingSystemVersionNumbers(&major_version, | 150 base::SysInfo::OperatingSystemVersionNumbers(&major_version, |
| 113 &minor_version, &bugfix_version); | 151 &minor_version, &bugfix_version); |
| 114 | 152 |
| 115 if (major_version > 10 || (major_version == 10 && minor_version >= 6)) { | 153 if (major_version > 10 || (major_version == 10 && minor_version >= 6)) { |
| 116 // 10.6-only Sandbox rules. | 154 // 10.6-only Sandbox rules. |
| 117 sandbox_data = [sandbox_data | 155 sandbox_data = [sandbox_data |
| 118 stringByReplacingOccurrencesOfString:@";10.6_ONLY" | 156 stringByReplacingOccurrencesOfString:@";10.6_ONLY" |
| 119 withString:@""]; | 157 withString:@""]; |
| 120 // Splice the path of the user's home directory into the sandbox profile | 158 // Splice the path of the user's home directory into the sandbox profile |
| (...skipping 15 matching lines...) Expand all Loading... |
| 136 int error = sandbox_init([sandbox_data UTF8String], 0, &error_buff); | 174 int error = sandbox_init([sandbox_data UTF8String], 0, &error_buff); |
| 137 bool success = (error == 0 && error_buff == NULL); | 175 bool success = (error == 0 && error_buff == NULL); |
| 138 if (error == -1) { | 176 if (error == -1) { |
| 139 LOG(ERROR) << "Failed to Initialize Sandbox: " << error_buff; | 177 LOG(ERROR) << "Failed to Initialize Sandbox: " << error_buff; |
| 140 } | 178 } |
| 141 sandbox_free_error(error_buff); | 179 sandbox_free_error(error_buff); |
| 142 return success; | 180 return success; |
| 143 } | 181 } |
| 144 | 182 |
| 145 } // namespace sandbox | 183 } // namespace sandbox |
| OLD | NEW |