blob: b0f9817c85e46a75b8f59ed57956a9f05b68990f [file] [log] [blame]
[email protected]af8b34002012-02-02 08:03:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]df8e899b2011-02-22 22:58:222// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]b9535422012-02-09 01:47:595#ifndef CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_
6#define CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_
[email protected]df8e899b2011-02-22 22:58:227
[email protected]d9c2e512012-10-25 18:54:368
[email protected]df8e899b2011-02-22 22:58:229#include <map>
10#include <set>
11#include <string>
12
[email protected]b9535422012-02-09 01:47:5913#include "base/compiler_specific.h"
[email protected]df8e899b2011-02-22 22:58:2214#include "base/gtest_prod_util.h"
[email protected]3b63f8f42011-03-28 01:54:1515#include "base/memory/singleton.h"
[email protected]df8e899b2011-02-22 22:58:2216#include "base/synchronization/lock.h"
[email protected]b9535422012-02-09 01:47:5917#include "content/public/browser/child_process_security_policy.h"
[email protected]7491ad02014-07-05 19:10:0718#include "content/public/common/resource_type.h"
[email protected]ce5cbed82013-07-01 11:52:3119#include "webkit/common/fileapi/file_system_types.h"
[email protected]df8e899b2011-02-22 22:58:2220
[email protected]df8e899b2011-02-22 22:58:2221class GURL;
22
[email protected]a3ef4832013-02-02 05:12:3323namespace base {
24class FilePath;
25}
26
[email protected]cd501a72014-08-22 19:58:3127namespace storage {
[email protected]ce5cbed82013-07-01 11:52:3128class FileSystemURL;
29}
30
[email protected]46488322012-10-30 03:22:2031namespace content {
32
[email protected]b9535422012-02-09 01:47:5933class CONTENT_EXPORT ChildProcessSecurityPolicyImpl
[email protected]46488322012-10-30 03:22:2034 : NON_EXPORTED_BASE(public ChildProcessSecurityPolicy) {
[email protected]df8e899b2011-02-22 22:58:2235 public:
36 // Object can only be created through GetInstance() so the constructor is
37 // private.
[email protected]b9535422012-02-09 01:47:5938 virtual ~ChildProcessSecurityPolicyImpl();
[email protected]df8e899b2011-02-22 22:58:2239
[email protected]b9535422012-02-09 01:47:5940 static ChildProcessSecurityPolicyImpl* GetInstance();
[email protected]df8e899b2011-02-22 22:58:2241
[email protected]b9535422012-02-09 01:47:5942 // ChildProcessSecurityPolicy implementation.
43 virtual void RegisterWebSafeScheme(const std::string& scheme) OVERRIDE;
44 virtual bool IsWebSafeScheme(const std::string& scheme) OVERRIDE;
[email protected]a3ef4832013-02-02 05:12:3345 virtual void GrantReadFile(int child_id, const base::FilePath& file) OVERRIDE;
[email protected]bfcf1e92013-07-11 04:37:2546 virtual void GrantCreateReadWriteFile(int child_id,
[email protected]9f104312013-07-23 23:18:1947 const base::FilePath& file) OVERRIDE;
[email protected]65486ad2014-03-28 21:06:4848 virtual void GrantCopyInto(int child_id, const base::FilePath& dir) OVERRIDE;
49 virtual void GrantDeleteFrom(int child_id,
50 const base::FilePath& dir) OVERRIDE;
[email protected]aa7cfbf2012-06-01 08:48:0951 virtual void GrantReadFileSystem(
52 int child_id,
53 const std::string& filesystem_id) OVERRIDE;
[email protected]398d77b2013-01-17 01:31:0654 virtual void GrantWriteFileSystem(
55 int child_id,
56 const std::string& filesystem_id) OVERRIDE;
57 virtual void GrantCreateFileForFileSystem(
[email protected]aa7cfbf2012-06-01 08:48:0958 int child_id,
59 const std::string& filesystem_id) OVERRIDE;
[email protected]724173c2013-11-13 09:14:0760 virtual void GrantCreateReadWriteFileSystem(
61 int child_id,
62 const std::string& filesystem_id) OVERRIDE;
[email protected]b78c188fa62013-07-23 18:04:4563 virtual void GrantCopyIntoFileSystem(
64 int child_id,
65 const std::string& filesystem_id) OVERRIDE;
[email protected]5a65fde32013-10-22 05:15:3466 virtual void GrantDeleteFromFileSystem(
67 int child_id,
68 const std::string& filesystem_id) OVERRIDE;
[email protected]b9535422012-02-09 01:47:5969 virtual void GrantScheme(int child_id, const std::string& scheme) OVERRIDE;
[email protected]a3ef4832013-02-02 05:12:3370 virtual bool CanReadFile(int child_id, const base::FilePath& file) OVERRIDE;
[email protected]d4c797f2013-09-26 08:18:5371 virtual bool CanCreateReadWriteFile(int child_id,
72 const base::FilePath& file) OVERRIDE;
[email protected]ffc7b4d2012-06-08 00:05:3273 virtual bool CanReadFileSystem(int child_id,
74 const std::string& filesystem_id) OVERRIDE;
[email protected]d8d1cbe2012-07-19 05:32:1075 virtual bool CanReadWriteFileSystem(
76 int child_id,
77 const std::string& filesystem_id) OVERRIDE;
[email protected]b78c188fa62013-07-23 18:04:4578 virtual bool CanCopyIntoFileSystem(int child_id,
79 const std::string& filesystem_id) OVERRIDE;
[email protected]5a65fde32013-10-22 05:15:3480 virtual bool CanDeleteFromFileSystem(
81 int child_id,
82 const std::string& filesystem_id) OVERRIDE;
[email protected]2c6e3b04c2014-07-24 12:48:0983 virtual bool HasWebUIBindings(int child_id) OVERRIDE;
[email protected]df8e899b2011-02-22 22:58:2284
85 // Pseudo schemes are treated differently than other schemes because they
86 // cannot be requested like normal URLs. There is no mechanism for revoking
87 // pseudo schemes.
88 void RegisterPseudoScheme(const std::string& scheme);
89
90 // Returns true iff |scheme| has been registered as pseudo scheme.
91 bool IsPseudoScheme(const std::string& scheme);
92
93 // Upon creation, child processes should register themselves by calling this
94 // this method exactly once.
95 void Add(int child_id);
96
[email protected]cee64fd32011-05-02 18:59:0797 // Upon creation, worker thread child processes should register themselves by
98 // calling this this method exactly once. Workers that are not shared will
99 // inherit permissions from their parent renderer process identified with
100 // |main_render_process_id|.
101 void AddWorker(int worker_child_id, int main_render_process_id);
102
[email protected]df8e899b2011-02-22 22:58:22103 // Upon destruction, child processess should unregister themselves by caling
104 // this method exactly once.
105 void Remove(int child_id);
106
107 // Whenever the browser processes commands the child process to request a URL,
108 // it should call this method to grant the child process the capability to
[email protected]dc67e1c32012-06-08 00:10:40109 // request the URL, along with permission to request all URLs of the same
110 // scheme.
[email protected]df8e899b2011-02-22 22:58:22111 void GrantRequestURL(int child_id, const GURL& url);
112
[email protected]dc67e1c32012-06-08 00:10:40113 // Whenever the browser process drops a file icon on a tab, it should call
114 // this method to grant the child process the capability to request this one
115 // file:// URL, but not all urls of the file:// scheme.
116 void GrantRequestSpecificFileURL(int child_id, const GURL& url);
117
[email protected]df8e899b2011-02-22 22:58:22118 // Revokes all permissions granted to the given file.
[email protected]a3ef4832013-02-02 05:12:33119 void RevokeAllPermissionsForFile(int child_id, const base::FilePath& file);
[email protected]df8e899b2011-02-22 22:58:22120
[email protected]df8e899b2011-02-22 22:58:22121 // Grant the child process the ability to use Web UI Bindings.
122 void GrantWebUIBindings(int child_id);
123
[email protected]df8e899b2011-02-22 22:58:22124 // Grant the child process the ability to read raw cookies.
125 void GrantReadRawCookies(int child_id);
126
127 // Revoke read raw cookies permission.
128 void RevokeReadRawCookies(int child_id);
129
[email protected]4ca7cf0f2013-08-28 14:19:03130 // Grants permission to send system exclusive message to any MIDI devices.
[email protected]6e068ea2014-02-04 07:05:47131 void GrantSendMidiSysExMessage(int child_id);
[email protected]4ca7cf0f2013-08-28 14:19:03132
[email protected]df8e899b2011-02-22 22:58:22133 // Before servicing a child process's request for a URL, the browser should
134 // call this method to determine whether the process has the capability to
135 // request the URL.
136 bool CanRequestURL(int child_id, const GURL& url);
137
[email protected]bbdd1b20b2012-12-11 21:24:13138 // Returns true if the process is permitted to load pages from
139 // the given origin in main frames or subframes.
140 // Only might return false if --site-per-process flag is used.
141 bool CanLoadPage(int child_id,
142 const GURL& url,
[email protected]6c1e05212014-07-31 00:59:40143 ResourceType resource_type);
[email protected]bbdd1b20b2012-12-11 21:24:13144
[email protected]9f104312013-07-23 23:18:19145 // Explicit permissions checks for FileSystemURL specified files.
[email protected]cd501a72014-08-22 19:58:31146 bool CanReadFileSystemFile(int child_id, const storage::FileSystemURL& url);
147 bool CanWriteFileSystemFile(int child_id, const storage::FileSystemURL& url);
148 bool CanCreateFileSystemFile(int child_id, const storage::FileSystemURL& url);
[email protected]d4c797f2013-09-26 08:18:53149 bool CanCreateReadWriteFileSystemFile(int child_id,
[email protected]cd501a72014-08-22 19:58:31150 const storage::FileSystemURL& url);
[email protected]4b9d0862013-10-15 19:49:41151 bool CanCopyIntoFileSystemFile(int child_id,
[email protected]cd501a72014-08-22 19:58:31152 const storage::FileSystemURL& url);
153 bool CanDeleteFileSystemFile(int child_id, const storage::FileSystemURL& url);
[email protected]9f104312013-07-23 23:18:19154
[email protected]df8e899b2011-02-22 22:58:22155 // Returns true if the specified child_id has been granted ReadRawCookies.
156 bool CanReadRawCookies(int child_id);
157
[email protected]c6f2e672012-11-15 01:47:02158 // Returns true if the process is permitted to read and modify the cookies for
159 // the given origin. Does not affect cookies attached to or set by network
160 // requests.
[email protected]313b80bd2011-11-23 03:49:10161 // Only might return false if the very experimental
[email protected]c6f2e672012-11-15 01:47:02162 // --enable-strict-site-isolation or --site-per-process flags are used.
163 bool CanAccessCookiesForOrigin(int child_id, const GURL& gurl);
164
165 // Returns true if the process is permitted to attach cookies to (or have
166 // cookies set by) network requests.
167 // Only might return false if the very experimental
168 // --enable-strict-site-isolation or --site-per-process flags are used.
169 bool CanSendCookiesForOrigin(int child_id, const GURL& gurl);
[email protected]313b80bd2011-11-23 03:49:10170
171 // Sets the process as only permitted to use and see the cookies for the
172 // given origin.
[email protected]c6f2e672012-11-15 01:47:02173 // Only used if the very experimental --enable-strict-site-isolation or
174 // --site-per-process flags are used.
[email protected]313b80bd2011-11-23 03:49:10175 void LockToOrigin(int child_id, const GURL& gurl);
176
[email protected]ce5cbed82013-07-01 11:52:31177 // Register FileSystem type and permission policy which should be used
178 // for the type. The |policy| must be a bitwise-or'd value of
[email protected]cd501a72014-08-22 19:58:31179 // storage::FilePermissionPolicy.
180 void RegisterFileSystemPermissionPolicy(storage::FileSystemType type,
181 int policy);
[email protected]ce5cbed82013-07-01 11:52:31182
[email protected]4ca7cf0f2013-08-28 14:19:03183 // Returns true if sending system exclusive messages is allowed.
[email protected]6e068ea2014-02-04 07:05:47184 bool CanSendMidiSysExMessage(int child_id);
[email protected]4ca7cf0f2013-08-28 14:19:03185
[email protected]df8e899b2011-02-22 22:58:22186 private:
187 friend class ChildProcessSecurityPolicyInProcessBrowserTest;
[email protected]bfcf1e92013-07-11 04:37:25188 friend class ChildProcessSecurityPolicyTest;
[email protected]df8e899b2011-02-22 22:58:22189 FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyInProcessBrowserTest,
190 NoLeak);
[email protected]bee16c0b2013-09-16 20:30:48191 FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyTest, FilePermissions);
[email protected]df8e899b2011-02-22 22:58:22192
193 class SecurityState;
194
195 typedef std::set<std::string> SchemeSet;
196 typedef std::map<int, SecurityState*> SecurityStateMap;
[email protected]cee64fd32011-05-02 18:59:07197 typedef std::map<int, int> WorkerToMainProcessMap;
[email protected]cd501a72014-08-22 19:58:31198 typedef std::map<storage::FileSystemType, int> FileSystemPermissionPolicyMap;
[email protected]df8e899b2011-02-22 22:58:22199
[email protected]b9535422012-02-09 01:47:59200 // Obtain an instance of ChildProcessSecurityPolicyImpl via GetInstance().
201 ChildProcessSecurityPolicyImpl();
202 friend struct DefaultSingletonTraits<ChildProcessSecurityPolicyImpl>;
[email protected]df8e899b2011-02-22 22:58:22203
[email protected]cee64fd32011-05-02 18:59:07204 // Adds child process during registration.
205 void AddChild(int child_id);
206
207 // Determines if certain permissions were granted for a file to given child
[email protected]322a6eb2013-11-12 06:13:09208 // process. |permissions| is an internally defined bit-set.
[email protected]cee64fd32011-05-02 18:59:07209 bool ChildProcessHasPermissionsForFile(int child_id,
[email protected]a3ef4832013-02-02 05:12:33210 const base::FilePath& file,
[email protected]cee64fd32011-05-02 18:59:07211 int permissions);
212
[email protected]322a6eb2013-11-12 06:13:09213 // Grant a particular permission set for a file. |permissions| is an
214 // internally defined bit-set.
[email protected]bfcf1e92013-07-11 04:37:25215 void GrantPermissionsForFile(int child_id,
216 const base::FilePath& file,
217 int permissions);
218
219 // Grants access permission to the given isolated file system
220 // identified by |filesystem_id|. See comments for
221 // ChildProcessSecurityPolicy::GrantReadFileSystem() for more details.
222 void GrantPermissionsForFileSystem(
223 int child_id,
224 const std::string& filesystem_id,
225 int permission);
226
[email protected]bee16c0b2013-09-16 20:30:48227 // Determines if certain permissions were granted for a file. |permissions|
[email protected]322a6eb2013-11-12 06:13:09228 // is an internally defined bit-set. If |child_id| is a worker process,
229 // this returns true if either the worker process or its parent renderer
230 // has permissions for the file.
[email protected]bee16c0b2013-09-16 20:30:48231 bool HasPermissionsForFile(int child_id,
232 const base::FilePath& file,
233 int permissions);
234
[email protected]bee16c0b2013-09-16 20:30:48235 // Determines if certain permissions were granted for a file in FileSystem
[email protected]322a6eb2013-11-12 06:13:09236 // API. |permissions| is an internally defined bit-set.
[email protected]bee16c0b2013-09-16 20:30:48237 bool HasPermissionsForFileSystemFile(int child_id,
[email protected]cd501a72014-08-22 19:58:31238 const storage::FileSystemURL& url,
[email protected]bee16c0b2013-09-16 20:30:48239 int permissions);
240
[email protected]322a6eb2013-11-12 06:13:09241 // Determines if certain permissions were granted for a file system.
242 // |permissions| is an internally defined bit-set.
243 bool HasPermissionsForFileSystem(
244 int child_id,
245 const std::string& filesystem_id,
246 int permission);
247
[email protected]826ad7112011-09-02 21:39:03248 // You must acquire this lock before reading or writing any members of this
[email protected]df8e899b2011-02-22 22:58:22249 // class. You must not block while holding this lock.
250 base::Lock lock_;
251
252 // These schemes are white-listed for all child processes. This set is
253 // protected by |lock_|.
254 SchemeSet web_safe_schemes_;
255
256 // These schemes do not actually represent retrievable URLs. For example,
257 // the the URLs in the "about" scheme are aliases to other URLs. This set is
258 // protected by |lock_|.
259 SchemeSet pseudo_schemes_;
260
[email protected]df8e899b2011-02-22 22:58:22261 // This map holds a SecurityState for each child process. The key for the
262 // map is the ID of the ChildProcessHost. The SecurityState objects are
263 // owned by this object and are protected by |lock_|. References to them must
264 // not escape this class.
265 SecurityStateMap security_state_;
266
[email protected]cee64fd32011-05-02 18:59:07267 // This maps keeps the record of which js worker thread child process
268 // corresponds to which main js thread child process.
269 WorkerToMainProcessMap worker_map_;
270
[email protected]ce5cbed82013-07-01 11:52:31271 FileSystemPermissionPolicyMap file_system_policy_map_;
272
[email protected]b9535422012-02-09 01:47:59273 DISALLOW_COPY_AND_ASSIGN(ChildProcessSecurityPolicyImpl);
[email protected]df8e899b2011-02-22 22:58:22274};
275
[email protected]46488322012-10-30 03:22:20276} // namespace content
277
[email protected]b9535422012-02-09 01:47:59278#endif // CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_