Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(598)

Side by Side Diff: docs/design/sandbox.md

Issue 2827383002: Port sandbox, sandbox faq, and startup design docs to markdown. (Closed)
Patch Set: Fix a trivial typo Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « docs/design/README.md ('k') | docs/design/sandbox_faq.md » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Sandbox
2
3 [TOC]
4
5 ## Overview
6
7 Security is one of the most important goals for Chromium. The key to security is
8 understanding: we can only truly secure a system if we fully understand its
9 behaviors with respect to the combination of all possible inputs in all possible
10 states. For a codebase as large and diverse as Chromium, reasoning about the
11 combined behavior of all its parts is nearly impossible. The sandbox objective
12 is to provide hard guarantees about what ultimately a piece of code can or
13 cannot do no matter what its inputs are.
14
15 Sandbox leverages the OS-provided security to allow code execution that cannot
16 make persistent changes to the computer or access information that is
17 confidential. The architecture and exact assurances that the sandbox provides
18 are dependent on the operating system. This document covers the Windows
19 implementation as well as the general design. The Linux implementation is
20 described [here](../linux_sandboxing.md), the OSX implementation
21 [here](http://dev.chromium.org/developers/design-documents/sandbox/osx-sandboxin g-design).
22
23 If you don't feel like reading this whole document you can read the
24 [Sandbox FAQ](sandbox_faq.md) instead. A
25 description of what the sandbox does and doesn't protect against may also be
26 found in the FAQ.
27
28 ## Design principles
29
30 * **Do not re-invent the wheel:** It is tempting to extend the OS kernel with a
31 better security model. Don't. Let the operating system apply its security to
32 the objects it controls. On the other hand, it is OK to create
33 application-level objects (abstractions) that have a custom security model.
34 * **Principle of least privilege:** This should be applied both to the sandboxed
35 code and to the code that controls the sandbox. In other words, the sandbox
36 should work even if the user cannot elevate to super-user.
37 * **Assume sandboxed code is malicious code:** For threat-modeling purposes, we
38 consider the sandbox compromised (that is, running malicious code) once the
39 execution path reaches past a few early calls in the `main()` function. In
40 practice, it could happen as soon as the first external input is accepted, or
41 right before the main loop is entered.
42 * **Be nimble:** Non-malicious code does not try to access resources it cannot
43 obtain. In this case the sandbox should impose near-zero performance
44 impact. It's ok to have performance penalties for exceptional cases when a
45 sensitive resource needs to be touched once in a controlled manner. This is
46 usually the case if the OS security is used properly.
47 * **Emulation is not security:** Emulation and virtual machine solutions do not
48 by themselves provide security. The sandbox should not rely on code emulation,
49 code translation, or patching to provide security.
50
51 ## Sandbox windows architecture
52
53 The Windows sandbox is a user-mode only sandbox. There are no special kernel
54 mode drivers, and the user does not need to be an administrator in order for the
55 sandbox to operate correctly. The sandbox is designed for both 32-bit and 64-bit
56 processes and has been tested on all Windows OS flavors between Windows 7 and
57 Windows 10, both 32-bit and 64-bit.
58
59 Sandbox operates at process-level granularity. Anything that needs to be
60 sandboxed needs to live on a separate process. The minimal sandbox configuration
61 has two processes: one that is a privileged controller known as the _broker_,
62 and one or more sandboxed processes known as the _target_. Throughout the
63 documentation and the code these two terms are used with that precise
64 connotation. The sandbox is provided as a static library that must be linked to
65 both the broker and the target executables.
66
67 ### The broker process
68
69 In Chromium, the broker is always the browser process. The broker, is in broad
70 terms, a privileged controller/supervisor of the activities of the sandboxed
71 processes. The responsibilities of the broker process are:
72
73 1. Specify the policy for each target process
74 1. Spawn the target processes
75 1. Host the sandbox policy engine service
76 1. Host the sandbox interception manager
77 1. Host the sandbox IPC service (to the target processes)
78 1. Perform the policy-allowed actions on behalf of the target process
79
80 The broker should always outlive all the target processes that it spawned. The
81 sandbox IPC is a low-level mechanism (different from Chromium's IPC) that is
82 used to transparently forward certain windows API calls from the target to the
83 broker: these calls are evaluated against the policy. The policy-allowed calls
84 are then executed by the broker and the results returned to the target process
85 via the same IPC. The job of the interceptions manager is to patch the windows
86 API calls that should be forwarded via IPC to the broker.
87
88 ### The target process
89
90 In Chromium, the renderers are always target processes, unless the
91 `--no-sandbox` command line has been specified for the browser process. The
92 target process hosts all the code that is going to run inside the sandbox, plus
93 the sandbox infrastructure client side:
94
95 1. All code to be sandboxed
96 1. The sandbox IPC client
97 1. The sandbox policy engine client
98 1. The sandbox interceptions
99
100 Items 2,3 and 4 are part of the sandbox library that is linked with the code to
101 be sandboxed.
102
103 The interceptions (also known as hooks) are how Windows API calls are forwarded
104 via the sandbox IPC to the broker. It is up to the broker to re-issue the API
105 calls and return the results or simply fail the calls. The interception + IPC
106 mechanism does not provide security; it is designed to provide compatibility
107 when code inside the sandbox cannot be modified to cope with sandbox
108 restrictions. To save unnecessary IPCs, policy is also evaluated in the target
109 process before making an IPC call, although this is not used as a security
110 guarantee but merely a speed optimization.
111
112 It is the expectation that in the future most plugins will run inside a target
113 process.
114
115 ![Sandbox Top Level Box Diagram](sandbox_top_diagram.png)
116
117 ## Sandbox restrictions
118
119 At its core, the sandbox relies on the protection provided by four Windows
120 mechanisms:
121
122 * A restricted token
123 * The Windows _job_ object
124 * The Windows _desktop_ object
125 * Windows Vista and above: The integrity levels
126
127 These mechanisms are highly effective at protecting the OS, its configuration,
128 and the user's data provided that:
129
130 * All the securable resources have a better than null security descriptor. In
131 other words, there are no critical resources with misconfigured security.
132 * The computer is not already compromised by malware.
133 * Third party software does not weaken the security of the system.
134
135 ** Note that extra mitigations above and beyond this base/core will be described
136 in the "Process Mitigations" section below.
137
138 ### The token
139
140 One issue that other similar sandbox projects face is how restricted can the
141 token and job be while still having a properly functioning process. For the
142 Chromium sandbox, the most restrictive token for Windows XP takes the following
143 form:
144
145 #### Regular Groups
146 * Logon SID : mandatory
147 * All other SIDs : deny only, mandatory
148 #### Restricted Groups
149 * S-1-0-0 : mandatory
150 #### Privileges
151 * None
152
153 With the caveats described above, it is near impossible to find an existing
154 resource that the OS will grant access with such a token. As long as the disk
155 root directories have non-null security, even files with null security cannot be
156 accessed. In Vista, the most restrictive token is the same but it also includes
157 the low integrity level label. The Chromium renderer normally runs with this
158 token, which means that almost all resources that the renderer process uses have
159 been acquired by the Browser and their handles duplicated into the renderer
160 process.
161
162 Note that the token is not derived from anonymous or from the guest token; it is
163 derived from the user's token and thus associated to the user logon. As a
164 result, any auditing that the system or the domain has in place can still be
165 used.
166
167 By design, the sandbox token cannot protect the following non-securable
168 resources:
169
170 * Mounted FAT or FAT32 volumes: The security descriptor on them is effectively
171 null. Malware running in the target can read and write to these volumes as
172 long it can guess or deduce their paths.
173 * TCP/IP: The security of TCP/IP sockets in Windows 2000 and Windows XP (but not
174 in Vista) is effectively null. It might be possible for malicious code in the
175 target to send and receive network packets to any host.
176
177 More information about the Windows token object can be
178 found
179 [here](http://alt.pluralsight.com/wiki/default.aspx/Keith.GuideBook/WhatIsAToken .htm)
180
181 ### The Job object
182
183 The target process also runs under a Job object. Using this Windows mechanism,
184 some interesting global restrictions that do not have a traditional object or
185 security descriptor associated with them are enforced:
186
187 * Forbid per-use system-wide changes using `SystemParametersInfo()`, which can
188 be used to swap the mouse buttons or set the screen saver timeout
189 * Forbid the creation or switch of Desktops
190 * Forbid changes to the per-user display configuration such as resolution and
191 primary display
192 * No read or write to the clipboard
193 * Forbid Windows message broadcasts
194 * Forbid setting global Windows hooks (using `SetWindowsHookEx()`)
195 * Forbid access to the global atoms table
196 * Forbid access to USER handles created outside the Job object
197 * One active process limit (disallows creating child processes)
198
199 Chromium renderers normally run with all these restrictions active. Each
200 renderers run in its own Job object. Using the Job object, the sandbox can (but
201 currently does not) prevent:
202
203 * Excessive use of CPU cycles
204 * Excessive use of memory
205 * Excessive use of IO
206
207 More information about Windows Job Objects can be
208 found [here](http://www.microsoft.com/msj/0399/jobkernelobj/jobkernelobj.aspx)
209
210 ### The alternate desktop
211
212 The token and the job object define a security boundary: that is, all processes
213 with the same token and in the same job object are effectively in the same
214 security context. However, one not-well-understood fact is that applications
215 that have windows on the same desktop are also effectively in the same security
216 context because the sending and receiving of window messages is not subject to
217 any security checks. Sending messages across desktops is not allowed. This is
218 the source of the infamous "shatter" attacks, which is why services should not
219 host windows on the interactive desktop. A Windows desktop is a regular kernel
220 object that can be created and assigned a security descriptor.
221
222 In a standard Windows installation, at least two desktops are attached to the
223 interactive window station; the regular (default) desktop, and the logon
224 desktop. The sandbox creates a third desktop that is associated to all target
225 processes. This desktop is never visible or interactive and effectively isolates
226 the sandboxed processes from snooping the user's interaction and from sending
227 messages to windows operating at more privileged contexts.
228
229 The only disadvantage of an alternate desktop is that it uses approximately 4MB
230 of RAM from a separate pool, possibly more on Vista.
231
232 More information about Window Stations
233
234 ### The integrity levels
235
236 Integrity levels are available on Windows Vista and later versions. They don't
237 define a security boundary in the strict sense, but they do provide a form of
238 mandatory access control (MAC) and act as the basis of Microsoft's Internet
239 Explorer sandbox.
240
241 Integrity levels are implemented as a special set of SID and ACL entries
242 representing five levels of increasing privilege: untrusted, low, medium, high,
243 system. Access to an object may be restricted if the object is at a higher
244 integrity level than the requesting token. Integrity levels also implement User
245 Interface Privilege Isolation, which applies the rules of integrity levels to
246 window messages exchanged between different processes on the same desktop.
247
248 By default, a token can read an object of a higher integrity level, but not
249 write to it. Most desktop applications run at medium integrity (MI), while less
250 trusted processes like Internet Explorer's protected mode and our own sandbox
251 run at low integrity (LI). A low integrity mode token can access only the
252 following shared resources:
253
254 * Read access to most files
255 * Write access to `%USER PROFILE%\AppData\LocalLow`
256 * Read access to most of the registry
257 * Write access to `HKEY_CURRENT_USER\Software\AppDataLow`
258 * Clipboard (copy and paste for certain formats)
259 * Remote procedure call (RPC)
260 * TCP/IP Sockets
261 * Window messages exposed via `ChangeWindowMessageFilter`
262 * Shared memory exposed via LI (low integrity) labels
263 * COM interfaces with LI (low integrity) launch activation rights
264 * Named pipes exposed via LI (low integrity) labels
265
266 You'll notice that the previously described attributes of the token, job object,
267 and alternate desktop are more restrictive, and would in fact block access to
268 everything allowed in the above list. So, the integrity level is a bit redundant
269 with the other measures, but it can be seen as an additional degree of
270 defense-in-depth, and its use has no visible impact on performance or resource
271 usage.
272
273 More information on integrity levels can be
274 found [here](http://msdn.microsoft.com/en-us/library/bb625963.aspx).
275
276 ### Process mitigation policies
277
278 Most process mitigation policies can be applied to the target process by means
279 of SetProcessMitigationPolicy. The sandbox uses this API to set various
280 policies on the target process for enforcing security characteristics.
281
282 #### Relocate Images:
283
284 * >= Win8
285 * Address-load randomization (ASLR) on all images in process (and must be
286 supported by all images).
287
288 #### Heap Terminate:
289
290 * >= Win8
291 * Terminates the process on Windows heap corruption.
292
293 #### Bottom-up ASLR:
294
295 * >= Win8
296 * Sets random lower bound as minimum user address for the process.
297
298 #### High-entropy ASLR:
299
300 * >= Win8
301 * Increases randomness range for bottom-up ASLR to 1TB.
302
303 #### Strict Handle Checks:
304
305 * >= Win8
306 * Immediately raises an exception on a bad handle reference.
307
308 #### Win32k.sys lockdown:
309
310 * >= Win8
311 * `ProcessSystemCallDisablePolicy`, which allows selective disabling of system
312 calls available from the target process.
313 * Renderer processes now have this set to `DisallowWin32kSystemCalls` which
314 means that calls from user mode that are serviced by `win32k.sys` are no
315 longer permitted. This significantly reduces the kernel attack surface
316 available from a renderer. See
317 [here](https://docs.google.com/document/d/1gJDlk-9xkh6_8M_awrczWCaUuyr0Zd2TKjN BCiPO_G4)
318 for more details.
319
320 #### App Container (low box token):
321
322 * >= Win8
323 * In Windows this is implemented at the kernel level by a Low Box token which is
324 a stripped version of a normal token with limited privilege (normally just
325 `SeChangeNotifyPrivilege` and `SeIncreaseWorkingSetPrivilege`), running at Low
326 integrity level and an array of "Capabilities" which can be mapped to
327 allow/deny what the process is allowed to do (see
328 [MSDN](https://msdn.microsoft.com/en-us/library/windows/apps/hh464936.aspx)
329 for a high level description). The capability most interesting from a sandbox
330 perspective is denying is access to the network, as it turns out network
331 checks are enforced if the token is a Low Box token and the `INTERNET_CLIENT`
332 Capability is not present.
333 * The sandbox therefore takes the existing restricted token and adds the Low Box
334 attributes, without granting any Capabilities, so as to gain the additional
335 protection of no network access from the sandboxed process.
336
337 #### Disable Extension Points (legacy hooking):
338
339 * >= Win8
340 * `ProcessExtensionPointDisablePolicy`
341 * The following injection vectors are blocked:
342 * AppInit DLLs Winsock Layered Service Providers (LSPs)
343 * Global Window Hooks (not thread-targeted hooks)
344 * Legacy Input Method Editors (IMEs)
345
346 #### Control Flow Guard (CFG):
347
348 * >= Win8.1 Update 3 (KB3000850)
349 * Enabled in all chrome.exe processes. Not compiled into all chrome binaries.
350 * Takes advantage of CFG security in Microsoft system DLLs in our processes.
351 * Compiler/Linker opt-in, not a run-time policy opt-in. See
352 [MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/mt637065(v=vs.85 ).aspx).
353
354 #### Disable Font Loading:
355
356 * >= Win10
357 * `ProcessFontDisablePolicy`
358
359 #### Disable Image Load from Remote Devices:
360
361 * >= Win10 TH2
362 * `ProcessImageLoadPolicy`
363 * E.g. UNC path to network resource.
364
365 #### Disable Image Load of "mandatory low" (low integrity level):
366
367 * >= Win10 TH2
368 * `ProcessImageLoadPolicy`
369 * E.g. temporary internet files.
370
371 #### Extra Disable Child Process Creation:
372
373 * >= Win10 TH2
374 * If the Job level <= `JOB_LIMITED_USER`, set
375 `PROC_THREAD_ATTRIBUTE_CHILD_PROCESS_POLICY` to
376 `PROCESS_CREATION_CHILD_PROCESS_RESTRICTED` via `UpdateProcThreadAttribute()`.
377 * This is an extra layer of defense, given that Job levels can be broken out of.
378 See also:
379 [ticket](https://bugs.chromium.org/p/project-zero/issues/detail?id=213&redir=1),
380 [Project Zero blog](http://googleprojectzero.blogspot.co.uk/2015/05/in-console-a ble.html).
381
382 ### Other caveats
383
384 The operating system might have bugs. Of interest are bugs in the Windows API
385 that allow the bypass of the regular security checks. If such a bug exists,
386 malware will be able to bypass the sandbox restrictions and broker policy and
387 possibly compromise the computer. Under Windows, there is no practical way to
388 prevent code in the sandbox from calling a system service.
389
390 In addition, third party software, particularly anti-malware solutions, can
391 create new attack vectors. The most troublesome are applications that inject
392 dlls in order to enable some (usually unwanted) capability. These dlls will also
393 get injected in the sandbox process. In the best case they will malfunction, and
394 in the worst case can create backdoors to other processes or to the file system
395 itself, enabling specially crafted malware to escape the sandbox.
396
397 ## Sandbox policy
398
399 The actual restrictions applied to a target process are configured by a
400 policy. The policy is just a programmatic interface that the broker calls to
401 define the restrictions and allowances. Four functions control the restrictions,
402 roughly corresponding to the four Windows mechanisms:
403
404 * `TargetPolicy::SetTokenLevel()`
405 * `TargetPolicy::SetJobLevel()`
406 * `TargetPolicy::SetIntegrityLevel()`
407 * `TargetPolicy::SetDesktop()`
408
409 The first three calls take an integer level parameter that goes from very strict
410 to very loose; for example, the token level has 7 levels and the job level has 5
411 levels. Chromium renderers are typically run with the most strict level in all
412 four mechanisms. Finally, the last (desktop) policy is binary and can only be
413 used to indicate if a target is run on an alternate desktop or not.
414
415 The restrictions are by design coarse in that they affect all securable
416 resources that the target can touch, but sometimes a more finely-grained
417 resolution is needed. The policy interface allows the broker to specify
418 exceptions. An exception is a way to take a specific Windows API call issued in
419 the target and proxy it over to the broker. The broker can inspect the
420 parameters and re-issue the call as is, re-issue the call with different
421 parameters, or simply deny the call. To specify exceptions there is a single
422 call: `AddRule`. The following kinds of rules for different Windows subsystems
423 are supported at this time:
424
425 * Files
426 * Named pipes
427 * Process creation
428 * Registry
429 * Synchronization objects
430
431 The exact form of the rules for each subsystem varies, but in general rules are
432 triggered based on a string pattern. For example, a possible file rule is:
433
434 AddRule(SUBSYS_FILES, FILES_ALLOW_READONLY, L"c:\\temp\\app_log\\d*.dmp")
435
436 This rule specifies that access will be granted if a target wants to open a
437 file, for read-only access as long as the file matches the pattern expression;
438 for example `c:\temp\app_log\domino.dmp` is a file that satisfies the
439 pattern. Consult the header files for an up-to-date list of supported objects
440 and supported actions.
441
442 Rules can only be added before each target process is spawned, and cannot be
443 modified while a target is running, but different targets can have different
444 rules.
445
446 ## Target bootstrapping
447
448 Targets do not start executing with the restrictions specified by policy. They
449 start executing with a token that is very close to the token the regular user
450 processes have. The reason is that during process bootstrapping the OS loader
451 accesses a lot of resources, most of them are actually undocumented and can
452 change at any time. Also, most applications use the standard CRT provided with
453 the standard development tools; after the process is bootstrapped the CRT needs
454 to initialize as well and there again the internals of the CRT initialization
455 are undocumented.
456
457 Therefore, during the bootstrapping phase the process actually uses two tokens:
458 the lockdown token which is the process token as is and the initial token which
459 is set as the impersonation token of the initial thread. In fact the actual
460 `SetTokenLevel` definition is:
461
462 SetTokenLevel(TokenLevel initial, TokenLevel lockdown)
463
464 After all the critical initialization is done, execution continues at `main()`
465 or `WinMain()`, here the two tokens are still active, but only the initial
466 thread can use the more powerful initial token. It is the target's
467 responsibility to discard the initial token when ready. This is done with a
468 single call:
469
470 LowerToken()
471
472 After this call is issued by the target the only token available is the lockdown
473 token and the full sandbox restrictions go into effect. The effects of this call
474 cannot be undone. Note that the initial token is a impersonation token only
475 valid for the main thread, other threads created in the target process use only
476 the lockdown token and therefore should not attempt to obtain any system
477 resources subject to a security check.
478
479 The fact that the target starts with a privileged token simplifies the explicit
480 policy since anything privileged that needs to be done once, at process startup
481 can be done before the `LowerToken()` call and does not require to have rules in
482 the policy.
483
484 **Important**
485
486 Make sure any sensitive OS handles obtained with the initial token are closed
487 before calling LowerToken(). Any leaked handle can be abused by malware to
488 escape the sandbox.
489
490
491
OLDNEW
« no previous file with comments | « docs/design/README.md ('k') | docs/design/sandbox_faq.md » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698