OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef CHROME_COMMON_SANDBOX_MAC_H_ | 5 #ifndef CHROME_COMMON_SANDBOX_MAC_H_ |
6 #define CHROME_COMMON_SANDBOX_MAC_H_ | 6 #define CHROME_COMMON_SANDBOX_MAC_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | |
12 #include "base/hash_tables.h" | |
13 #include "base/gtest_prod_util.h" | |
14 | |
11 class FilePath; | 15 class FilePath; |
12 | 16 |
17 #if __OBJC__ | |
18 @class NSArray; | |
19 @class NSString; | |
20 #else | |
21 class NSArray; | |
22 class NSString; | |
23 #endif | |
24 | |
13 namespace sandbox { | 25 namespace sandbox { |
14 | 26 |
15 enum SandboxProcessType { | |
16 SANDBOX_TYPE_FIRST_TYPE, // Placeholder to ease iteration. | |
17 | |
18 SANDBOX_TYPE_RENDERER = SANDBOX_TYPE_FIRST_TYPE, | |
19 | |
20 // The worker processes uses the most restrictive sandbox which has almost | |
21 // *everything* locked down. Only a couple of /System/Library/ paths and | |
22 // some other very basic operations (e.g., reading metadata to allow | |
23 // following symlinks) are permitted. | |
24 SANDBOX_TYPE_WORKER, | |
25 | |
26 // Utility process is as restrictive as the worker process except full access | |
27 // is allowed to one configurable directory. | |
28 SANDBOX_TYPE_UTILITY, | |
29 | |
30 // Native Client sandbox for the user's untrusted code. | |
31 SANDBOX_TYPE_NACL_LOADER, | |
32 | |
33 SANDBOX_AFTER_TYPE_LAST_TYPE, // Placeholder to ease iteration. | |
34 }; | |
35 | |
36 // Warm up System APIs that empirically need to be accessed before the Sandbox | |
37 // is turned on. | |
38 void SandboxWarmup(); | |
39 | |
40 // Turns on the OS X sandbox for this process. | |
41 // |sandbox_type| - type of Sandbox to use. | |
42 // |allowed_dir| - directory to allow access to, currently the only sandbox | |
43 // profile that supports this is SANDBOX_TYPE_UTILITY . | |
44 // | |
45 // |allowed_dir| must be a "simple" string since it's placed as is in a regex | |
46 // i.e. it must not contain quotation characters, escaping or any characters | |
47 // that might have special meaning when blindly substituted into a regular | |
48 // expression - crbug.com/26492 . | |
49 // Returns true on success, false if an error occurred enabling the sandbox. | |
50 bool EnableSandbox(SandboxProcessType sandbox_type, | |
51 const FilePath& allowed_dir); | |
52 | |
53 // Convert provided path into a "canonical" path matching what the Sandbox | |
54 // expects i.e. one without symlinks. | |
55 // This path is not necessarily unique e.g. in the face of hardlinks. | |
56 void GetCanonicalSandboxPath(FilePath* path); | |
57 | |
58 // Exposed for testing. | |
59 | |
60 // Class representing a substring of the sandbox profile tagged with its type. | 27 // Class representing a substring of the sandbox profile tagged with its type. |
61 class SandboxSubstring { | 28 class SandboxSubstring { |
62 public: | 29 public: |
63 enum SandboxSubstringType { | 30 enum SandboxSubstringType { |
64 PLAIN, // Just a plain string, no escaping necessary. | 31 PLAIN, // Just a plain string, no escaping necessary. |
65 LITERAL, // Escape for use in (literal ...) expression. | 32 LITERAL, // Escape for use in (literal ...) expression. |
66 REGEX, // Escape for use in (regex ...) expression. | 33 REGEX, // Escape for use in (regex ...) expression. |
67 }; | 34 }; |
68 | 35 |
69 SandboxSubstring() {} | 36 SandboxSubstring() {} |
70 | 37 |
71 explicit SandboxSubstring(const std::string& value) | 38 explicit SandboxSubstring(const std::string& value) |
72 : value_(value), | 39 : value_(value), |
73 type_(PLAIN) {} | 40 type_(PLAIN) {} |
74 | 41 |
75 SandboxSubstring(const std::string& value, SandboxSubstringType type) | 42 SandboxSubstring(const std::string& value, SandboxSubstringType type) |
76 : value_(value), | 43 : value_(value), |
77 type_(type) {} | 44 type_(type) {} |
78 | 45 |
79 const std::string& value() { return value_; } | 46 const std::string& value() { return value_; } |
80 SandboxSubstringType type() { return type_; } | 47 SandboxSubstringType type() { return type_; } |
81 | 48 |
82 private: | 49 private: |
83 std::string value_; | 50 std::string value_; |
84 SandboxSubstringType type_; | 51 SandboxSubstringType type_; |
85 }; | 52 }; |
86 | 53 |
54 class Sandbox { | |
55 public: | |
56 // A map of variable name -> string to substitute in its place. | |
57 typedef base::hash_map<std::string, SandboxSubstring> | |
58 SandboxVariableSubstitions; | |
Mark Mentovai
2010/11/04 16:19:30
The hanging continuation indent is 4 spaces.
| |
59 | |
60 enum SandboxProcessType { | |
61 SANDBOX_TYPE_FIRST_TYPE, // Placeholder to ease iteration. | |
62 | |
63 SANDBOX_TYPE_RENDERER = SANDBOX_TYPE_FIRST_TYPE, | |
64 | |
65 // The worker processes uses the most restrictive sandbox which has almost | |
Mark Mentovai
2010/11/04 16:19:30
“processes use” or “process uses.”
| |
66 // *everything* locked down. Only a couple of /System/Library/ paths and | |
67 // some other very basic operations (e.g., reading metadata to allow | |
68 // following symlinks) are permitted. | |
69 SANDBOX_TYPE_WORKER, | |
70 | |
71 // Utility process is as restrictive as the worker process except full | |
72 // access is allowed to one configurable directory. | |
73 SANDBOX_TYPE_UTILITY, | |
74 | |
75 // Native Client sandbox for the user's untrusted code. | |
76 SANDBOX_TYPE_NACL_LOADER, | |
77 | |
78 SANDBOX_AFTER_TYPE_LAST_TYPE, // Placeholder to ease iteration. | |
79 }; | |
80 | |
81 // Warm up System APIs that empirically need to be accessed before the Sandbox | |
82 // is turned on. | |
83 static void SandboxWarmup(); | |
84 | |
85 // Turns on the OS X sandbox for this process. | |
86 // |sandbox_type| - type of Sandbox to use. | |
87 // |allowed_dir| - directory to allow access to, currently the only sandbox | |
88 // profile that supports this is SANDBOX_TYPE_UTILITY . | |
89 // | |
90 // Returns true on success, false if an error occurred enabling the sandbox. | |
91 static bool EnableSandbox(SandboxProcessType sandbox_type, | |
92 const FilePath& allowed_dir); | |
93 | |
94 | |
95 // Exposed for testing purposes, used by an accessory function of our tests | |
96 // so we can't use FRIEND_TEST. | |
97 | |
98 // Build the Sandbox command necessary to allow access to a named directory | |
99 // indicated by |allowed_dir|. | |
100 // Returns a string containing the sandbox profile commands necessary to allow | |
101 // access to that directory or nil if an error occured. | |
102 | |
103 // The header comment for PostProcessSandboxProfile() explains how variable | |
104 // substition works in sandbox templates. | |
105 // The returned string contains embedded variables. The function fills in | |
106 // |substitutions| to contain the values for these variables. | |
107 static NSString* BuildAllowDirectoryAccessSandboxString( | |
108 const FilePath& allowed_dir, | |
109 SandboxVariableSubstitions* substitutions); | |
110 | |
111 // Assemble the final sandbox profile from a template by removing comments | |
112 // and substituting variables. | |
113 // | |
114 // |sandbox_template| is a string which contains 2 entitites to operate on: | |
115 // | |
116 // - Comments - The sandbox comment syntax is used to make the OS sandbox | |
117 // optionally ignore commands it doesn't support. e.g. | |
118 // ;10.6_ONLY (foo) | |
119 // Where (foo) is some command that is only supported on OS X 10.6. | |
120 // The ;10.6_ONLY comment can then be removed from the template to enable (foo ) | |
Mark Mentovai
2010/11/04 16:19:30
80
| |
121 // as appropriate. | |
122 // | |
123 // - Variables - denoted by @variable_name@ . These are defined in the | |
124 // sandbox template in cases where another string needs to be substituted at | |
125 // runtime. e.g. @HOMEDIR_AS_LITERAL@ is substituted at runtime for the user's | |
126 // home directory escaped appropriately for a (literal ...) expression. | |
127 // | |
128 // |comments_to_remove| is a list of NSStrings containing the comments to | |
129 // remove. | |
130 // |substitutions| is a hash of "variable name" -> "string to substitute". | |
131 // Where the replacement string is tagged with information on how it is to be | |
132 // escaped e.g. used as part of a regex string or a literal. | |
133 // | |
134 // On output |final_sandbox_profile_str| contains the final sandbox profile. | |
135 // Returns true on success, false otherwise. | |
136 static bool PostProcessSandboxProfile( | |
137 NSString* in_sandbox_data, | |
138 NSArray* comments_to_remove, | |
139 SandboxVariableSubstitions& substitutions, | |
140 std::string *final_sandbox_profile_str); | |
141 | |
142 private: | |
143 // Escape |str_utf8| for use in a plain string variable in a sandbox | |
Mark Mentovai
2010/11/04 16:19:30
In these functions, str_utf8 doesn’t seem like a g
| |
144 // configuraton file. On return |dst| is set to the utf-8 encoded quoted | |
145 // output. | |
146 // Returns: true on success, false otherwise. | |
147 static bool QuotePlainString(const std::string& str_utf8, std::string* dst); | |
148 | |
149 // Escape |str_utf8| for use in a regex literal in a sandbox | |
150 // configuraton file. On return |dst| is set to the utf-8 encoded quoted | |
151 // output. | |
152 // | |
153 // The implementation of this function is based on empirical testing of the | |
154 // OS X sandbox on 10.5.8 & 10.6.2 which is undocumented and subject to | |
155 // change. | |
156 // | |
157 // Note: If str_utf8 contains any characters < 32 || >125 then the function | |
158 // fails and false is returned. | |
159 // | |
160 // Returns: true on success, false otherwise. | |
161 static bool QuoteStringForRegex(const std::string& str_utf8, | |
162 std::string* dst); | |
163 | |
164 // Convert provided path into a "canonical" path matching what the Sandbox | |
165 // expects i.e. one without symlinks. | |
166 // This path is not necessarily unique e.g. in the face of hardlinks. | |
167 static void GetCanonicalSandboxPath(FilePath* path); | |
168 | |
169 FRIEND_TEST(MacDirAccessSandboxTest, StringEscape); | |
170 FRIEND_TEST(MacDirAccessSandboxTest, RegexEscape); | |
171 FRIEND_TEST(MacDirAccessSandboxTest, SandboxAccess); | |
172 | |
173 DISALLOW_COPY_AND_ASSIGN(Sandbox); | |
Mark Mentovai
2010/11/04 16:19:30
Use DISALLOW_IMPLICIT_CONSTRUCTORS on this one ins
| |
174 }; | |
175 | |
87 } // namespace sandbox | 176 } // namespace sandbox |
88 | 177 |
89 #endif // CHROME_COMMON_SANDBOX_MAC_H_ | 178 #endif // CHROME_COMMON_SANDBOX_MAC_H_ |
OLD | NEW |