blob: 82f0e9be22c660dd30f0c11eb4c58e775405cf78 [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]df8e899b2011-02-22 22:58:228#include <map>
avi6f9a1d412016-08-16 16:07:319#include <memory>
[email protected]df8e899b2011-02-22 22:58:2210#include <set>
11#include <string>
lukasza43c38f122016-06-17 20:07:3812#include <vector>
[email protected]df8e899b2011-02-22 22:58:2213
[email protected]b9535422012-02-09 01:47:5914#include "base/compiler_specific.h"
[email protected]df8e899b2011-02-22 22:58:2215#include "base/gtest_prod_util.h"
avib7348942015-12-25 20:57:1016#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1517#include "base/memory/singleton.h"
[email protected]df8e899b2011-02-22 22:58:2218#include "base/synchronization/lock.h"
[email protected]b9535422012-02-09 01:47:5919#include "content/public/browser/child_process_security_policy.h"
[email protected]7491ad02014-07-05 19:10:0720#include "content/public/common/resource_type.h"
pilgrim16330552014-09-10 01:32:2221#include "storage/common/fileapi/file_system_types.h"
[email protected]df8e899b2011-02-22 22:58:2222
[email protected]df8e899b2011-02-22 22:58:2223class GURL;
24
[email protected]a3ef4832013-02-02 05:12:3325namespace base {
26class FilePath;
27}
28
[email protected]cd501a72014-08-22 19:58:3129namespace storage {
[email protected]ce5cbed82013-07-01 11:52:3130class FileSystemURL;
31}
32
[email protected]46488322012-10-30 03:22:2033namespace content {
34
[email protected]b9535422012-02-09 01:47:5935class CONTENT_EXPORT ChildProcessSecurityPolicyImpl
[email protected]46488322012-10-30 03:22:2036 : NON_EXPORTED_BASE(public ChildProcessSecurityPolicy) {
[email protected]df8e899b2011-02-22 22:58:2237 public:
38 // Object can only be created through GetInstance() so the constructor is
39 // private.
dchengc2282aa2014-10-21 12:07:5840 ~ChildProcessSecurityPolicyImpl() override;
[email protected]df8e899b2011-02-22 22:58:2241
[email protected]b9535422012-02-09 01:47:5942 static ChildProcessSecurityPolicyImpl* GetInstance();
[email protected]df8e899b2011-02-22 22:58:2243
[email protected]b9535422012-02-09 01:47:5944 // ChildProcessSecurityPolicy implementation.
dchengc2282aa2014-10-21 12:07:5845 void RegisterWebSafeScheme(const std::string& scheme) override;
nick2a8ba8c2016-10-03 18:51:3946 void RegisterWebSafeIsolatedScheme(
47 const std::string& scheme,
48 bool always_allow_in_origin_headers) override;
dchengc2282aa2014-10-21 12:07:5849 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]aa7cfbf2012-06-01 08:48:0962 int child_id,
mohan.reddy7fc3ac72014-10-09 05:24:1363 const std::string& filesystem_id) override;
dchengc2282aa2014-10-21 12:07:5864 void GrantCopyIntoFileSystem(int child_id,
65 const std::string& filesystem_id) override;
66 void GrantDeleteFromFileSystem(int child_id,
mohan.reddy7fc3ac72014-10-09 05:24:1367 const std::string& filesystem_id) override;
paulmeyer1eefa26e2015-10-01 02:11:1368 void GrantOrigin(int child_id, const url::Origin& origin) override;
dchengc2282aa2014-10-21 12:07:5869 void GrantScheme(int child_id, const std::string& scheme) override;
nick2a8ba8c2016-10-03 18:51:3970 bool CanRequestURL(int child_id, const GURL& url) override;
71 bool CanCommitURL(int child_id, const GURL& url) override;
dchengc2282aa2014-10-21 12:07:5872 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;
mlamouri97de25e42014-11-25 10:50:2384 void GrantSendMidiSysExMessage(int child_id) override;
lfg24d083792015-07-17 20:45:3585 bool CanAccessDataForOrigin(int child_id, const GURL& url) override;
alexmosf7af1352016-10-21 20:00:4186 bool HasSpecificPermissionForOrigin(int child_id,
87 const url::Origin& origin) override;
[email protected]df8e899b2011-02-22 22:58:2288
lukasza43c38f122016-06-17 20:07:3889 // 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]df8e899b2011-02-22 22:58:2292 // 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]cee64fd32011-05-02 18:59:07104 // 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]df8e899b2011-02-22 22:58:22110 // 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]dc67e1c32012-06-08 00:10:40116 // request the URL, along with permission to request all URLs of the same
117 // scheme.
[email protected]df8e899b2011-02-22 22:58:22118 void GrantRequestURL(int child_id, const GURL& url);
119
[email protected]dc67e1c32012-06-08 00:10:40120 // 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]df8e899b2011-02-22 22:58:22125 // Revokes all permissions granted to the given file.
[email protected]a3ef4832013-02-02 05:12:33126 void RevokeAllPermissionsForFile(int child_id, const base::FilePath& file);
[email protected]df8e899b2011-02-22 22:58:22127
[email protected]df8e899b2011-02-22 22:58:22128 // Grant the child process the ability to use Web UI Bindings.
129 void GrantWebUIBindings(int child_id);
130
[email protected]df8e899b2011-02-22 22:58:22131 // 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
jww2cdad9e2016-09-24 05:42:02137 // 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]9f104312013-07-23 23:18:19141 // Explicit permissions checks for FileSystemURL specified files.
nickb3c1e272016-10-07 22:56:37142 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]4b9d0862013-10-15 19:49:41151 bool CanCopyIntoFileSystemFile(int child_id,
nickb3c1e272016-10-07 22:56:37152 const storage::FileSystemURL& filesystem_url);
153 bool CanDeleteFileSystemFile(int child_id,
154 const storage::FileSystemURL& filesystem_url);
[email protected]9f104312013-07-23 23:18:19155
[email protected]df8e899b2011-02-22 22:58:22156 // Returns true if the specified child_id has been granted ReadRawCookies.
157 bool CanReadRawCookies(int child_id);
158
[email protected]313b80bd2011-11-23 03:49:10159 // Sets the process as only permitted to use and see the cookies for the
160 // given origin.
nicka23a06d2015-06-09 18:05:44161 // Origin lock is applied only if the --site-per-process flag is used.
[email protected]313b80bd2011-11-23 03:49:10162 void LockToOrigin(int child_id, const GURL& gurl);
163
[email protected]ce5cbed82013-07-01 11:52:31164 // 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]cd501a72014-08-22 19:58:31166 // storage::FilePermissionPolicy.
167 void RegisterFileSystemPermissionPolicy(storage::FileSystemType type,
168 int policy);
[email protected]ce5cbed82013-07-01 11:52:31169
[email protected]4ca7cf0f2013-08-28 14:19:03170 // Returns true if sending system exclusive messages is allowed.
[email protected]6e068ea2014-02-04 07:05:47171 bool CanSendMidiSysExMessage(int child_id);
[email protected]4ca7cf0f2013-08-28 14:19:03172
[email protected]df8e899b2011-02-22 22:58:22173 private:
174 friend class ChildProcessSecurityPolicyInProcessBrowserTest;
[email protected]bfcf1e92013-07-11 04:37:25175 friend class ChildProcessSecurityPolicyTest;
[email protected]df8e899b2011-02-22 22:58:22176 FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyInProcessBrowserTest,
177 NoLeak);
[email protected]bee16c0b2013-09-16 20:30:48178 FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyTest, FilePermissions);
[email protected]df8e899b2011-02-22 22:58:22179
180 class SecurityState;
181
182 typedef std::set<std::string> SchemeSet;
avi6f9a1d412016-08-16 16:07:31183 typedef std::map<int, std::unique_ptr<SecurityState>> SecurityStateMap;
[email protected]cee64fd32011-05-02 18:59:07184 typedef std::map<int, int> WorkerToMainProcessMap;
[email protected]cd501a72014-08-22 19:58:31185 typedef std::map<storage::FileSystemType, int> FileSystemPermissionPolicyMap;
[email protected]df8e899b2011-02-22 22:58:22186
[email protected]b9535422012-02-09 01:47:59187 // Obtain an instance of ChildProcessSecurityPolicyImpl via GetInstance().
188 ChildProcessSecurityPolicyImpl();
olli.raula36aa8be2015-09-10 11:14:22189 friend struct base::DefaultSingletonTraits<ChildProcessSecurityPolicyImpl>;
[email protected]df8e899b2011-02-22 22:58:22190
[email protected]cee64fd32011-05-02 18:59:07191 // 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]322a6eb2013-11-12 06:13:09195 // process. |permissions| is an internally defined bit-set.
[email protected]cee64fd32011-05-02 18:59:07196 bool ChildProcessHasPermissionsForFile(int child_id,
[email protected]a3ef4832013-02-02 05:12:33197 const base::FilePath& file,
[email protected]cee64fd32011-05-02 18:59:07198 int permissions);
199
[email protected]322a6eb2013-11-12 06:13:09200 // Grant a particular permission set for a file. |permissions| is an
201 // internally defined bit-set.
[email protected]bfcf1e92013-07-11 04:37:25202 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]bee16c0b2013-09-16 20:30:48214 // Determines if certain permissions were granted for a file. |permissions|
[email protected]322a6eb2013-11-12 06:13:09215 // 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]bee16c0b2013-09-16 20:30:48218 bool HasPermissionsForFile(int child_id,
219 const base::FilePath& file,
220 int permissions);
221
[email protected]bee16c0b2013-09-16 20:30:48222 // Determines if certain permissions were granted for a file in FileSystem
[email protected]322a6eb2013-11-12 06:13:09223 // API. |permissions| is an internally defined bit-set.
nickb3c1e272016-10-07 22:56:37224 bool HasPermissionsForFileSystemFile(
225 int child_id,
226 const storage::FileSystemURL& filesystem_url,
227 int permissions);
[email protected]bee16c0b2013-09-16 20:30:48228
[email protected]322a6eb2013-11-12 06:13:09229 // 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]826ad7112011-09-02 21:39:03236 // You must acquire this lock before reading or writing any members of this
[email protected]df8e899b2011-02-22 22:58:22237 // class. You must not block while holding this lock.
238 base::Lock lock_;
239
nick2a8ba8c2016-10-03 18:51:39240 // 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]df8e899b2011-02-22 22:58:22245
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]df8e899b2011-02-22 22:58:22251 // 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]cee64fd32011-05-02 18:59:07257 // 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]ce5cbed82013-07-01 11:52:31261 FileSystemPermissionPolicyMap file_system_policy_map_;
262
[email protected]b9535422012-02-09 01:47:59263 DISALLOW_COPY_AND_ASSIGN(ChildProcessSecurityPolicyImpl);
[email protected]df8e899b2011-02-22 22:58:22264};
265
[email protected]46488322012-10-30 03:22:20266} // namespace content
267
[email protected]b9535422012-02-09 01:47:59268#endif // CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_