[email protected] | af8b3400 | 2012-02-02 08:03:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 5 | #ifndef CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_ |
| 6 | #define CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_ |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 7 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 8 | #include <map> |
avi | 6f9a1d41 | 2016-08-16 16:07:31 | [diff] [blame] | 9 | #include <memory> |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 10 | #include <set> |
| 11 | #include <string> |
lukasza | 43c38f12 | 2016-06-17 20:07:38 | [diff] [blame] | 12 | #include <vector> |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 13 | |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 14 | #include "base/compiler_specific.h" |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 15 | #include "base/gtest_prod_util.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 16 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 17 | #include "base/memory/singleton.h" |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 18 | #include "base/synchronization/lock.h" |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 19 | #include "content/public/browser/child_process_security_policy.h" |
[email protected] | 7491ad0 | 2014-07-05 19:10:07 | [diff] [blame] | 20 | #include "content/public/common/resource_type.h" |
pilgrim | 1633055 | 2014-09-10 01:32:22 | [diff] [blame] | 21 | #include "storage/common/fileapi/file_system_types.h" |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 22 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 23 | class GURL; |
| 24 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 25 | namespace base { |
| 26 | class FilePath; |
| 27 | } |
| 28 | |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 29 | namespace storage { |
[email protected] | ce5cbed8 | 2013-07-01 11:52:31 | [diff] [blame] | 30 | class FileSystemURL; |
| 31 | } |
| 32 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 33 | namespace content { |
| 34 | |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 35 | class CONTENT_EXPORT ChildProcessSecurityPolicyImpl |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 36 | : NON_EXPORTED_BASE(public ChildProcessSecurityPolicy) { |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 37 | public: |
| 38 | // Object can only be created through GetInstance() so the constructor is |
| 39 | // private. |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 40 | ~ChildProcessSecurityPolicyImpl() override; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 41 | |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 42 | static ChildProcessSecurityPolicyImpl* GetInstance(); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 43 | |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 44 | // ChildProcessSecurityPolicy implementation. |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 45 | void RegisterWebSafeScheme(const std::string& scheme) override; |
nick | 2a8ba8c | 2016-10-03 18:51:39 | [diff] [blame] | 46 | void RegisterWebSafeIsolatedScheme( |
| 47 | const std::string& scheme, |
| 48 | bool always_allow_in_origin_headers) override; |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 49 | bool IsWebSafeScheme(const std::string& scheme) override; |
| 50 | void GrantReadFile(int child_id, const base::FilePath& file) override; |
| 51 | void GrantCreateReadWriteFile(int child_id, |
| 52 | const base::FilePath& file) override; |
| 53 | void GrantCopyInto(int child_id, const base::FilePath& dir) override; |
| 54 | void GrantDeleteFrom(int child_id, const base::FilePath& dir) override; |
| 55 | void GrantReadFileSystem(int child_id, |
| 56 | const std::string& filesystem_id) override; |
| 57 | void GrantWriteFileSystem(int child_id, |
| 58 | const std::string& filesystem_id) override; |
| 59 | void GrantCreateFileForFileSystem(int child_id, |
| 60 | const std::string& filesystem_id) override; |
| 61 | void GrantCreateReadWriteFileSystem( |
[email protected] | aa7cfbf | 2012-06-01 08:48:09 | [diff] [blame] | 62 | int child_id, |
mohan.reddy | 7fc3ac7 | 2014-10-09 05:24:13 | [diff] [blame] | 63 | const std::string& filesystem_id) override; |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 64 | void GrantCopyIntoFileSystem(int child_id, |
| 65 | const std::string& filesystem_id) override; |
| 66 | void GrantDeleteFromFileSystem(int child_id, |
mohan.reddy | 7fc3ac7 | 2014-10-09 05:24:13 | [diff] [blame] | 67 | const std::string& filesystem_id) override; |
paulmeyer | 1eefa26e | 2015-10-01 02:11:13 | [diff] [blame] | 68 | void GrantOrigin(int child_id, const url::Origin& origin) override; |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 69 | void GrantScheme(int child_id, const std::string& scheme) override; |
nick | 2a8ba8c | 2016-10-03 18:51:39 | [diff] [blame] | 70 | bool CanRequestURL(int child_id, const GURL& url) override; |
| 71 | bool CanCommitURL(int child_id, const GURL& url) override; |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 72 | bool CanReadFile(int child_id, const base::FilePath& file) override; |
| 73 | bool CanCreateReadWriteFile(int child_id, |
| 74 | const base::FilePath& file) override; |
| 75 | bool CanReadFileSystem(int child_id, |
| 76 | const std::string& filesystem_id) override; |
| 77 | bool CanReadWriteFileSystem(int child_id, |
| 78 | const std::string& filesystem_id) override; |
| 79 | bool CanCopyIntoFileSystem(int child_id, |
| 80 | const std::string& filesystem_id) override; |
| 81 | bool CanDeleteFromFileSystem(int child_id, |
| 82 | const std::string& filesystem_id) override; |
| 83 | bool HasWebUIBindings(int child_id) override; |
mlamouri | 97de25e4 | 2014-11-25 10:50:23 | [diff] [blame] | 84 | void GrantSendMidiSysExMessage(int child_id) override; |
lfg | 24d08379 | 2015-07-17 20:45:35 | [diff] [blame] | 85 | bool CanAccessDataForOrigin(int child_id, const GURL& url) override; |
alexmos | f7af135 | 2016-10-21 20:00:41 | [diff] [blame] | 86 | bool HasSpecificPermissionForOrigin(int child_id, |
| 87 | const url::Origin& origin) override; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 88 | |
lukasza | 43c38f12 | 2016-06-17 20:07:38 | [diff] [blame] | 89 | // Returns if |child_id| can read all of the |files|. |
| 90 | bool CanReadAllFiles(int child_id, const std::vector<base::FilePath>& files); |
| 91 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 92 | // Pseudo schemes are treated differently than other schemes because they |
| 93 | // cannot be requested like normal URLs. There is no mechanism for revoking |
| 94 | // pseudo schemes. |
| 95 | void RegisterPseudoScheme(const std::string& scheme); |
| 96 | |
| 97 | // Returns true iff |scheme| has been registered as pseudo scheme. |
| 98 | bool IsPseudoScheme(const std::string& scheme); |
| 99 | |
| 100 | // Upon creation, child processes should register themselves by calling this |
| 101 | // this method exactly once. |
| 102 | void Add(int child_id); |
| 103 | |
[email protected] | cee64fd3 | 2011-05-02 18:59:07 | [diff] [blame] | 104 | // Upon creation, worker thread child processes should register themselves by |
| 105 | // calling this this method exactly once. Workers that are not shared will |
| 106 | // inherit permissions from their parent renderer process identified with |
| 107 | // |main_render_process_id|. |
| 108 | void AddWorker(int worker_child_id, int main_render_process_id); |
| 109 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 110 | // Upon destruction, child processess should unregister themselves by caling |
| 111 | // this method exactly once. |
| 112 | void Remove(int child_id); |
| 113 | |
| 114 | // Whenever the browser processes commands the child process to request a URL, |
| 115 | // it should call this method to grant the child process the capability to |
[email protected] | dc67e1c3 | 2012-06-08 00:10:40 | [diff] [blame] | 116 | // request the URL, along with permission to request all URLs of the same |
| 117 | // scheme. |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 118 | void GrantRequestURL(int child_id, const GURL& url); |
| 119 | |
[email protected] | dc67e1c3 | 2012-06-08 00:10:40 | [diff] [blame] | 120 | // Whenever the browser process drops a file icon on a tab, it should call |
| 121 | // this method to grant the child process the capability to request this one |
| 122 | // file:// URL, but not all urls of the file:// scheme. |
| 123 | void GrantRequestSpecificFileURL(int child_id, const GURL& url); |
| 124 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 125 | // Revokes all permissions granted to the given file. |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 126 | void RevokeAllPermissionsForFile(int child_id, const base::FilePath& file); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 127 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 128 | // Grant the child process the ability to use Web UI Bindings. |
| 129 | void GrantWebUIBindings(int child_id); |
| 130 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 131 | // Grant the child process the ability to read raw cookies. |
| 132 | void GrantReadRawCookies(int child_id); |
| 133 | |
| 134 | // Revoke read raw cookies permission. |
| 135 | void RevokeReadRawCookies(int child_id); |
| 136 | |
jww | 2cdad9e | 2016-09-24 05:42:02 | [diff] [blame] | 137 | // Whether the given origin is valid for an origin header. Valid origin |
| 138 | // headers are commitable URLs plus suborigin URLs. |
| 139 | bool CanSetAsOriginHeader(int child_id, const GURL& url); |
| 140 | |
[email protected] | 9f10431 | 2013-07-23 23:18:19 | [diff] [blame] | 141 | // Explicit permissions checks for FileSystemURL specified files. |
nick | b3c1e27 | 2016-10-07 22:56:37 | [diff] [blame] | 142 | bool CanReadFileSystemFile(int child_id, |
| 143 | const storage::FileSystemURL& filesystem_url); |
| 144 | bool CanWriteFileSystemFile(int child_id, |
| 145 | const storage::FileSystemURL& filesystem_url); |
| 146 | bool CanCreateFileSystemFile(int child_id, |
| 147 | const storage::FileSystemURL& filesystem_url); |
| 148 | bool CanCreateReadWriteFileSystemFile( |
| 149 | int child_id, |
| 150 | const storage::FileSystemURL& filesystem_url); |
[email protected] | 4b9d086 | 2013-10-15 19:49:41 | [diff] [blame] | 151 | bool CanCopyIntoFileSystemFile(int child_id, |
nick | b3c1e27 | 2016-10-07 22:56:37 | [diff] [blame] | 152 | const storage::FileSystemURL& filesystem_url); |
| 153 | bool CanDeleteFileSystemFile(int child_id, |
| 154 | const storage::FileSystemURL& filesystem_url); |
[email protected] | 9f10431 | 2013-07-23 23:18:19 | [diff] [blame] | 155 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 156 | // Returns true if the specified child_id has been granted ReadRawCookies. |
| 157 | bool CanReadRawCookies(int child_id); |
| 158 | |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 159 | // Sets the process as only permitted to use and see the cookies for the |
| 160 | // given origin. |
nick | a23a06d | 2015-06-09 18:05:44 | [diff] [blame] | 161 | // Origin lock is applied only if the --site-per-process flag is used. |
[email protected] | 313b80bd | 2011-11-23 03:49:10 | [diff] [blame] | 162 | void LockToOrigin(int child_id, const GURL& gurl); |
| 163 | |
[email protected] | ce5cbed8 | 2013-07-01 11:52:31 | [diff] [blame] | 164 | // Register FileSystem type and permission policy which should be used |
| 165 | // for the type. The |policy| must be a bitwise-or'd value of |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 166 | // storage::FilePermissionPolicy. |
| 167 | void RegisterFileSystemPermissionPolicy(storage::FileSystemType type, |
| 168 | int policy); |
[email protected] | ce5cbed8 | 2013-07-01 11:52:31 | [diff] [blame] | 169 | |
[email protected] | 4ca7cf0f | 2013-08-28 14:19:03 | [diff] [blame] | 170 | // Returns true if sending system exclusive messages is allowed. |
[email protected] | 6e068ea | 2014-02-04 07:05:47 | [diff] [blame] | 171 | bool CanSendMidiSysExMessage(int child_id); |
[email protected] | 4ca7cf0f | 2013-08-28 14:19:03 | [diff] [blame] | 172 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 173 | private: |
| 174 | friend class ChildProcessSecurityPolicyInProcessBrowserTest; |
[email protected] | bfcf1e9 | 2013-07-11 04:37:25 | [diff] [blame] | 175 | friend class ChildProcessSecurityPolicyTest; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 176 | FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyInProcessBrowserTest, |
| 177 | NoLeak); |
[email protected] | bee16c0b | 2013-09-16 20:30:48 | [diff] [blame] | 178 | FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyTest, FilePermissions); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 179 | |
| 180 | class SecurityState; |
| 181 | |
| 182 | typedef std::set<std::string> SchemeSet; |
avi | 6f9a1d41 | 2016-08-16 16:07:31 | [diff] [blame] | 183 | typedef std::map<int, std::unique_ptr<SecurityState>> SecurityStateMap; |
[email protected] | cee64fd3 | 2011-05-02 18:59:07 | [diff] [blame] | 184 | typedef std::map<int, int> WorkerToMainProcessMap; |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 185 | typedef std::map<storage::FileSystemType, int> FileSystemPermissionPolicyMap; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 186 | |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 187 | // Obtain an instance of ChildProcessSecurityPolicyImpl via GetInstance(). |
| 188 | ChildProcessSecurityPolicyImpl(); |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 189 | friend struct base::DefaultSingletonTraits<ChildProcessSecurityPolicyImpl>; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 190 | |
[email protected] | cee64fd3 | 2011-05-02 18:59:07 | [diff] [blame] | 191 | // Adds child process during registration. |
| 192 | void AddChild(int child_id); |
| 193 | |
| 194 | // Determines if certain permissions were granted for a file to given child |
[email protected] | 322a6eb | 2013-11-12 06:13:09 | [diff] [blame] | 195 | // process. |permissions| is an internally defined bit-set. |
[email protected] | cee64fd3 | 2011-05-02 18:59:07 | [diff] [blame] | 196 | bool ChildProcessHasPermissionsForFile(int child_id, |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 197 | const base::FilePath& file, |
[email protected] | cee64fd3 | 2011-05-02 18:59:07 | [diff] [blame] | 198 | int permissions); |
| 199 | |
[email protected] | 322a6eb | 2013-11-12 06:13:09 | [diff] [blame] | 200 | // Grant a particular permission set for a file. |permissions| is an |
| 201 | // internally defined bit-set. |
[email protected] | bfcf1e9 | 2013-07-11 04:37:25 | [diff] [blame] | 202 | void GrantPermissionsForFile(int child_id, |
| 203 | const base::FilePath& file, |
| 204 | int permissions); |
| 205 | |
| 206 | // Grants access permission to the given isolated file system |
| 207 | // identified by |filesystem_id|. See comments for |
| 208 | // ChildProcessSecurityPolicy::GrantReadFileSystem() for more details. |
| 209 | void GrantPermissionsForFileSystem( |
| 210 | int child_id, |
| 211 | const std::string& filesystem_id, |
| 212 | int permission); |
| 213 | |
[email protected] | bee16c0b | 2013-09-16 20:30:48 | [diff] [blame] | 214 | // Determines if certain permissions were granted for a file. |permissions| |
[email protected] | 322a6eb | 2013-11-12 06:13:09 | [diff] [blame] | 215 | // is an internally defined bit-set. If |child_id| is a worker process, |
| 216 | // this returns true if either the worker process or its parent renderer |
| 217 | // has permissions for the file. |
[email protected] | bee16c0b | 2013-09-16 20:30:48 | [diff] [blame] | 218 | bool HasPermissionsForFile(int child_id, |
| 219 | const base::FilePath& file, |
| 220 | int permissions); |
| 221 | |
[email protected] | bee16c0b | 2013-09-16 20:30:48 | [diff] [blame] | 222 | // Determines if certain permissions were granted for a file in FileSystem |
[email protected] | 322a6eb | 2013-11-12 06:13:09 | [diff] [blame] | 223 | // API. |permissions| is an internally defined bit-set. |
nick | b3c1e27 | 2016-10-07 22:56:37 | [diff] [blame] | 224 | bool HasPermissionsForFileSystemFile( |
| 225 | int child_id, |
| 226 | const storage::FileSystemURL& filesystem_url, |
| 227 | int permissions); |
[email protected] | bee16c0b | 2013-09-16 20:30:48 | [diff] [blame] | 228 | |
[email protected] | 322a6eb | 2013-11-12 06:13:09 | [diff] [blame] | 229 | // Determines if certain permissions were granted for a file system. |
| 230 | // |permissions| is an internally defined bit-set. |
| 231 | bool HasPermissionsForFileSystem( |
| 232 | int child_id, |
| 233 | const std::string& filesystem_id, |
| 234 | int permission); |
| 235 | |
[email protected] | 826ad711 | 2011-09-02 21:39:03 | [diff] [blame] | 236 | // You must acquire this lock before reading or writing any members of this |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 237 | // class. You must not block while holding this lock. |
| 238 | base::Lock lock_; |
| 239 | |
nick | 2a8ba8c | 2016-10-03 18:51:39 | [diff] [blame] | 240 | // These schemes are white-listed for all child processes in various contexts. |
| 241 | // These sets are protected by |lock_|. |
| 242 | SchemeSet schemes_okay_to_commit_in_any_process_; |
| 243 | SchemeSet schemes_okay_to_request_in_any_process_; |
| 244 | SchemeSet schemes_okay_to_appear_as_origin_headers_; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 245 | |
| 246 | // These schemes do not actually represent retrievable URLs. For example, |
| 247 | // the the URLs in the "about" scheme are aliases to other URLs. This set is |
| 248 | // protected by |lock_|. |
| 249 | SchemeSet pseudo_schemes_; |
| 250 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 251 | // This map holds a SecurityState for each child process. The key for the |
| 252 | // map is the ID of the ChildProcessHost. The SecurityState objects are |
| 253 | // owned by this object and are protected by |lock_|. References to them must |
| 254 | // not escape this class. |
| 255 | SecurityStateMap security_state_; |
| 256 | |
[email protected] | cee64fd3 | 2011-05-02 18:59:07 | [diff] [blame] | 257 | // This maps keeps the record of which js worker thread child process |
| 258 | // corresponds to which main js thread child process. |
| 259 | WorkerToMainProcessMap worker_map_; |
| 260 | |
[email protected] | ce5cbed8 | 2013-07-01 11:52:31 | [diff] [blame] | 261 | FileSystemPermissionPolicyMap file_system_policy_map_; |
| 262 | |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 263 | DISALLOW_COPY_AND_ASSIGN(ChildProcessSecurityPolicyImpl); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 264 | }; |
| 265 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 266 | } // namespace content |
| 267 | |
[email protected] | b953542 | 2012-02-09 01:47:59 | [diff] [blame] | 268 | #endif // CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_ |