blob: 140143f90e9a5b7cf49fba3445ba9bf350b5ec20 [file] [log] [blame] [view]
justincarlsonb4730a0c2017-04-20 20:22:131# Sandbox
2
3[TOC]
4
5## Overview
6
7Security is one of the most important goals for Chromium. The key to security is
8understanding: we can only truly secure a system if we fully understand its
9behaviors with respect to the combination of all possible inputs in all possible
10states. For a codebase as large and diverse as Chromium, reasoning about the
11combined behavior of all its parts is nearly impossible. The sandbox objective
12is to provide hard guarantees about what ultimately a piece of code can or
13cannot do no matter what its inputs are.
14
15Sandbox leverages the OS-provided security to allow code execution that cannot
16make persistent changes to the computer or access information that is
17confidential. The architecture and exact assurances that the sandbox provides
18are dependent on the operating system. This document covers the Windows
19implementation as well as the general design. The Linux implementation is
Tom Anderson93e49e492019-12-23 19:55:3720described [here](../linux/sandboxing.md), the OSX implementation
justincarlsonb4730a0c2017-04-20 20:22:1321[here](http://dev.chromium.org/developers/design-documents/sandbox/osx-sandboxing-design).
22
23If you don't feel like reading this whole document you can read the
24[Sandbox FAQ](sandbox_faq.md) instead. A
25description of what the sandbox does and doesn't protect against may also be
26found 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,
Alex Goughfec822f2019-06-17 22:06:2949 code translation, or patching to provide security.
50
51## Sandbox Windows architecture
justincarlsonb4730a0c2017-04-20 20:22:1352
53The Windows sandbox is a user-mode only sandbox. There are no special kernel
54mode drivers, and the user does not need to be an administrator in order for the
55sandbox to operate correctly. The sandbox is designed for both 32-bit and 64-bit
56processes and has been tested on all Windows OS flavors between Windows 7 and
57Windows 10, both 32-bit and 64-bit.
58
59Sandbox operates at process-level granularity. Anything that needs to be
60sandboxed needs to live on a separate process. The minimal sandbox configuration
61has two processes: one that is a privileged controller known as the _broker_,
62and one or more sandboxed processes known as the _target_. Throughout the
63documentation and the code these two terms are used with that precise
64connotation. The sandbox is provided as a static library that must be linked to
65both the broker and the target executables.
66
67### The broker process
68
69In Chromium, the broker is always the browser process. The broker, is in broad
70terms, a privileged controller/supervisor of the activities of the sandboxed
71processes. The responsibilities of the broker process are:
72
731. Specify the policy for each target process
741. Spawn the target processes
751. Host the sandbox policy engine service
761. Host the sandbox interception manager
771. Host the sandbox IPC service (to the target processes)
781. Perform the policy-allowed actions on behalf of the target process
79
80The broker should always outlive all the target processes that it spawned. The
81sandbox IPC is a low-level mechanism (different from Chromium's IPC) that is
Alex Goughfec822f2019-06-17 22:06:2982used to transparently forward certain Windows API calls from the target to the
justincarlsonb4730a0c2017-04-20 20:22:1383broker: these calls are evaluated against the policy. The policy-allowed calls
84are then executed by the broker and the results returned to the target process
Alex Goughfec822f2019-06-17 22:06:2985via the same IPC. The job of the interceptions manager is to patch the Windows
justincarlsonb4730a0c2017-04-20 20:22:1386API calls that should be forwarded via IPC to the broker.
87
88### The target process
89
90In Chromium, the renderers are always target processes, unless the
91`--no-sandbox` command line has been specified for the browser process. The
92target process hosts all the code that is going to run inside the sandbox, plus
93the sandbox infrastructure client side:
94
951. All code to be sandboxed
961. The sandbox IPC client
971. The sandbox policy engine client
981. The sandbox interceptions
99
100Items 2,3 and 4 are part of the sandbox library that is linked with the code to
101be sandboxed.
102
103The interceptions (also known as hooks) are how Windows API calls are forwarded
104via the sandbox IPC to the broker. It is up to the broker to re-issue the API
105calls and return the results or simply fail the calls. The interception + IPC
106mechanism does not provide security; it is designed to provide compatibility
107when code inside the sandbox cannot be modified to cope with sandbox
108restrictions. To save unnecessary IPCs, policy is also evaluated in the target
109process before making an IPC call, although this is not used as a security
110guarantee but merely a speed optimization.
111
112It is the expectation that in the future most plugins will run inside a target
113process.
114
115![Sandbox Top Level Box Diagram](sandbox_top_diagram.png)
116
117## Sandbox restrictions
118
119At its core, the sandbox relies on the protection provided by four Windows
120mechanisms:
121
122* A restricted token
123* The Windows _job_ object
124* The Windows _desktop_ object
Alex Goughfec822f2019-06-17 22:06:29125* Integrity levels
justincarlsonb4730a0c2017-04-20 20:22:13126
127These mechanisms are highly effective at protecting the OS, its configuration,
128and 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
136in the "Process Mitigations" section below.
137
138### The token
139
140One issue that other similar sandbox projects face is how restricted can the
141token and job be while still having a properly functioning process. For the
Alex Goughfec822f2019-06-17 22:06:29142Chromium sandbox, the most restrictive token takes the following form:
justincarlsonb4730a0c2017-04-20 20:22:13143
144#### Regular Groups
145* Logon SID : mandatory
146* All other SIDs : deny only, mandatory
147#### Restricted Groups
148* S-1-0-0 : mandatory
149#### Privileges
150* None
Alex Goughfec822f2019-06-17 22:06:29151#### Integrity
152* Untrusted integrity level label (S-1-16-0x0)
justincarlsonb4730a0c2017-04-20 20:22:13153
154With the caveats described above, it is near impossible to find an existing
155resource that the OS will grant access with such a token. As long as the disk
156root directories have non-null security, even files with null security cannot be
Alex Goughfec822f2019-06-17 22:06:29157accessed. The Chromium renderer runs with this token, which means that almost
158all resources that the renderer process uses have been acquired by the Browser
159and their handles duplicated into the renderer process.
justincarlsonb4730a0c2017-04-20 20:22:13160
161Note that the token is not derived from anonymous or from the guest token; it is
162derived from the user's token and thus associated to the user logon. As a
163result, any auditing that the system or the domain has in place can still be
164used.
165
Alex Goughfec822f2019-06-17 22:06:29166By design, the sandbox token cannot protect the non-securable resources such as:
justincarlsonb4730a0c2017-04-20 20:22:13167
168* Mounted FAT or FAT32 volumes: The security descriptor on them is effectively
169 null. Malware running in the target can read and write to these volumes as
170 long it can guess or deduce their paths.
171* TCP/IP: The security of TCP/IP sockets in Windows 2000 and Windows XP (but not
172 in Vista) is effectively null. It might be possible for malicious code in the
173 target to send and receive network packets to any host.
Alex Goughfec822f2019-06-17 22:06:29174* Some unlabelled objects, such as anonymous shared memory sections (e.g.
175 [bug 338538](https://crbug.com/338538))
justincarlsonb4730a0c2017-04-20 20:22:13176
Alex Goughfec822f2019-06-17 22:06:29177See NULL DACLs and Other Dangerous ACE Types, _Secure Coding Techniques_, 195-199
178for more information.
justincarlsonb4730a0c2017-04-20 20:22:13179
180### The Job object
181
182The target process also runs under a Job object. Using this Windows mechanism,
183some interesting global restrictions that do not have a traditional object or
184security descriptor associated with them are enforced:
185
186* Forbid per-use system-wide changes using `SystemParametersInfo()`, which can
187 be used to swap the mouse buttons or set the screen saver timeout
188* Forbid the creation or switch of Desktops
189* Forbid changes to the per-user display configuration such as resolution and
190 primary display
191* No read or write to the clipboard
192* Forbid Windows message broadcasts
193* Forbid setting global Windows hooks (using `SetWindowsHookEx()`)
194* Forbid access to the global atoms table
195* Forbid access to USER handles created outside the Job object
196* One active process limit (disallows creating child processes)
197
198Chromium renderers normally run with all these restrictions active. Each
Alex Goughfec822f2019-06-17 22:06:29199renderer runs in its own Job object. Using the Job object, the sandbox can (but
justincarlsonb4730a0c2017-04-20 20:22:13200currently does not) prevent:
201
202* Excessive use of CPU cycles
203* Excessive use of memory
204* Excessive use of IO
205
206More information about Windows Job Objects can be
Alex Goughfec822f2019-06-17 22:06:29207found [here](https://docs.microsoft.com/en-us/windows/desktop/procthread/job-objects).
justincarlsonb4730a0c2017-04-20 20:22:13208
209### The alternate desktop
210
211The token and the job object define a security boundary: that is, all processes
212with the same token and in the same job object are effectively in the same
213security context. However, one not-well-understood fact is that applications
214that have windows on the same desktop are also effectively in the same security
215context because the sending and receiving of window messages is not subject to
216any security checks. Sending messages across desktops is not allowed. This is
217the source of the infamous "shatter" attacks, which is why services should not
218host windows on the interactive desktop. A Windows desktop is a regular kernel
219object that can be created and assigned a security descriptor.
220
221In a standard Windows installation, at least two desktops are attached to the
222interactive window station; the regular (default) desktop, and the logon
223desktop. The sandbox creates a third desktop that is associated to all target
224processes. This desktop is never visible or interactive and effectively isolates
225the sandboxed processes from snooping the user's interaction and from sending
226messages to windows operating at more privileged contexts.
227
228The only disadvantage of an alternate desktop is that it uses approximately 4MB
229of RAM from a separate pool, possibly more on Vista.
230
231More information about Window Stations
232
233### The integrity levels
234
235Integrity levels are available on Windows Vista and later versions. They don't
236define a security boundary in the strict sense, but they do provide a form of
237mandatory access control (MAC) and act as the basis of Microsoft's Internet
238Explorer sandbox.
239
240Integrity levels are implemented as a special set of SID and ACL entries
241representing five levels of increasing privilege: untrusted, low, medium, high,
242system. Access to an object may be restricted if the object is at a higher
243integrity level than the requesting token. Integrity levels also implement User
244Interface Privilege Isolation, which applies the rules of integrity levels to
245window messages exchanged between different processes on the same desktop.
246
247By default, a token can read an object of a higher integrity level, but not
248write to it. Most desktop applications run at medium integrity (MI), while less
Alex Goughfec822f2019-06-17 22:06:29249trusted processes like Internet Explorer's protected mode and our GPU sandbox
250run at low integrity (LI), while our renderer processes run at the lowest
251Untrusted integrity level.
252
253A low integrity level token can access only the following shared resources:
justincarlsonb4730a0c2017-04-20 20:22:13254
255* Read access to most files
256* Write access to `%USER PROFILE%\AppData\LocalLow`
257* Read access to most of the registry
258* Write access to `HKEY_CURRENT_USER\Software\AppDataLow`
259* Clipboard (copy and paste for certain formats)
260* Remote procedure call (RPC)
261* TCP/IP Sockets
262* Window messages exposed via `ChangeWindowMessageFilter`
263* Shared memory exposed via LI (low integrity) labels
264* COM interfaces with LI (low integrity) launch activation rights
265* Named pipes exposed via LI (low integrity) labels
266
Alex Goughfec822f2019-06-17 22:06:29267While an Untrusted integrity level can only write to resources which
268have a null DACL or an explicit Untrusted Mandatory Level.
269
justincarlsonb4730a0c2017-04-20 20:22:13270You'll notice that the previously described attributes of the token, job object,
271and alternate desktop are more restrictive, and would in fact block access to
272everything allowed in the above list. So, the integrity level is a bit redundant
273with the other measures, but it can be seen as an additional degree of
274defense-in-depth, and its use has no visible impact on performance or resource
275usage.
276
Alex Goughfec822f2019-06-17 22:06:29277The integrity level of different Chrome components will change over
278time as functionality is split into smaller services. At M75 the
279browser, crash handler, and network utility processes run at Medium
280integrity, the GPU process at Low and most remaining services
281including isolated renderers at Untrusted.
282
justincarlsonb4730a0c2017-04-20 20:22:13283More information on integrity levels can be
Alex Goughfec822f2019-06-17 22:06:29284found [here](http://msdn.microsoft.com/en-us/library/bb625963.aspx)
285and in Chapter 7 of *Windows Internals, Part 1, 7th Ed.*.
justincarlsonb4730a0c2017-04-20 20:22:13286
287### Process mitigation policies
288
289Most process mitigation policies can be applied to the target process by means
290of SetProcessMitigationPolicy. The sandbox uses this API to set various
291policies on the target process for enforcing security characteristics.
292
293#### Relocate Images:
294
295* >= Win8
296* Address-load randomization (ASLR) on all images in process (and must be
297 supported by all images).
298
299#### Heap Terminate:
300
301* >= Win8
302* Terminates the process on Windows heap corruption.
303
304#### Bottom-up ASLR:
305
306* >= Win8
307* Sets random lower bound as minimum user address for the process.
308
309#### High-entropy ASLR:
310
311* >= Win8
312* Increases randomness range for bottom-up ASLR to 1TB.
313
314#### Strict Handle Checks:
315
316* >= Win8
317* Immediately raises an exception on a bad handle reference.
318
319#### Win32k.sys lockdown:
320
321* >= Win8
322* `ProcessSystemCallDisablePolicy`, which allows selective disabling of system
323 calls available from the target process.
324* Renderer processes now have this set to `DisallowWin32kSystemCalls` which
325 means that calls from user mode that are serviced by `win32k.sys` are no
326 longer permitted. This significantly reduces the kernel attack surface
327 available from a renderer. See
328 [here](https://docs.google.com/document/d/1gJDlk-9xkh6_8M_awrczWCaUuyr0Zd2TKjNBCiPO_G4)
329 for more details.
330
justincarlsonb4730a0c2017-04-20 20:22:13331#### Disable Extension Points (legacy hooking):
332
333* >= Win8
334* `ProcessExtensionPointDisablePolicy`
335* The following injection vectors are blocked:
336 * AppInit DLLs Winsock Layered Service Providers (LSPs)
337 * Global Window Hooks (not thread-targeted hooks)
338 * Legacy Input Method Editors (IMEs)
339
340#### Control Flow Guard (CFG):
341
342* >= Win8.1 Update 3 (KB3000850)
343* Enabled in all chrome.exe processes. Not compiled into all chrome binaries.
344* Takes advantage of CFG security in Microsoft system DLLs in our processes.
345* Compiler/Linker opt-in, not a run-time policy opt-in. See
346[MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/mt637065(v=vs.85).aspx).
347
Vitaly Buka5987fd42020-10-20 22:58:05348#### CET Shadow Stack:
349
Alex Goughea32df5c2021-01-20 06:06:39350* Available in Windows 10 2004 December Update.
351* Is not enabled in the renderer. See
Vitaly Buka5987fd42020-10-20 22:58:05352[ticket](https://bugs.chromium.org/p/chromium/issues/detail?id=1136224),
353[MSDN](https://docs.microsoft.com/en-us/cpp/build/reference/cetcompat?view=vs-2019).
354
justincarlsonb4730a0c2017-04-20 20:22:13355#### Disable Font Loading:
356
357* >= Win10
358* `ProcessFontDisablePolicy`
359
Will Harris0dbe1dc2019-08-23 02:25:48360#### Disable Loading of Unsigned Code (CIG):
361
362* >= Win10 TH2
363* `ProcessSignaturePolicy`
364* Prevents loading unsigned code into our processes. This means attackers can't just LoadLibrary a DLL after gaining execution (which shouldn't be possible anyway due to other sandbox mitigations), but more importantly, prevents third party DLLs from being injected into our processes, which can affect stability and our ability to enable other security mitigations.
365* Enabled (post-startup) for all sandboxed child processes.
366* Enabled (pre-startup) for sandboxed renderer processes. This eliminates a process launch time gap where local injection of improperly signed DLLs into a renderer process could occur.
367* See [msedgedev blog](https://blogs.windows.com/msedgedev/2017/02/23/mitigating-arbitrary-native-code-execution/) for more background on this mitigation.
368
justincarlsonb4730a0c2017-04-20 20:22:13369#### Disable Image Load from Remote Devices:
370
371* >= Win10 TH2
372* `ProcessImageLoadPolicy`
373* E.g. UNC path to network resource.
374
375#### Disable Image Load of "mandatory low" (low integrity level):
376
377* >= Win10 TH2
378* `ProcessImageLoadPolicy`
379* E.g. temporary internet files.
380
381#### Extra Disable Child Process Creation:
382
383* >= Win10 TH2
384* If the Job level <= `JOB_LIMITED_USER`, set
385 `PROC_THREAD_ATTRIBUTE_CHILD_PROCESS_POLICY` to
386 `PROCESS_CREATION_CHILD_PROCESS_RESTRICTED` via `UpdateProcThreadAttribute()`.
387* This is an extra layer of defense, given that Job levels can be broken out of.
388 See also:
389[ticket](https://bugs.chromium.org/p/project-zero/issues/detail?id=213&redir=1),
390[Project Zero blog](http://googleprojectzero.blogspot.co.uk/2015/05/in-console-able.html).
391
Will Harris7dba8812021-12-07 16:13:07392### App Container (low box token):
393
394* In Windows this is implemented at the kernel level by a Low Box token which is
395 a stripped version of a normal token with limited privilege (normally just
396 `SeChangeNotifyPrivilege` and `SeIncreaseWorkingSetPrivilege`), running at Low
397 integrity level and an array of "Capabilities" which can be mapped to
398 allow/deny what the process is allowed to do (see
399 [MSDN](https://msdn.microsoft.com/en-us/library/windows/apps/hh464936.aspx)
400 for a high level description). The capability most interesting from a sandbox
401 perspective is denying is access to the network, as it turns out network
402 checks are enforced if the token is a Low Box token and the `INTERNET_CLIENT`
403 Capability is not present.
404* The sandbox therefore takes the existing restricted token and adds the Low Box
405 attributes, without granting any Capabilities, so as to gain the additional
406 protection of no network access from the sandboxed process.
407
408### Less Privileged App Container (LPAC)
409
410* An extension of the App Container (see above) available on later versions of
411 Windows 10 (RS2 and greater), the Less Privileged App Container (LPAC) runs
412 at a lower privilege level than normal App Container, with access granted by
413 default to only those kernel, filesystem and registry objects marked with the
414 `ALL RESTRICTED APPLICATION PACKAGES` or a specific package SID. This is
415 opposed to App Container which uses `ALL APPLICATION PACKAGES`.
416* A key characteristic of the LPAC is that specific named capabilities can be
417 added such as those based on well known SIDs (defined in
418 [`base/win/sid.h`](https://cs.chromium.org/chromium/src/base/win/sid.h)) or
419 via 'named capabilities' resolved through call to
420 [DeriveCapabilitySidsFromName](https://docs.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-derivecapabilitysidsfromname)
421 which are not really strictly defined anywhere but can be found in various
422 [places](https://social.technet.microsoft.com/Forums/scriptcenter/en-US/3e7d85e3-d0e1-4e79-8141-0bbf8faf3644/windows-10-anniversary-update-the-case-of-the-mysterious-account-sid-causing-the-flood-of-dcom?forum=win10itprosetup)
423 and include capabilities such as:
424 * `lpacCom`
425 * `registryRead`
426 * `lpacWebPlatform`
427 * `lpacClipboard`
428 * etc...
429 * Each LPAC process can have a process-specific SID created for it and this
430 can be used to protect files specific to that particular sandbox, and there
431 can be multiple different overlapping sets of access rights depending on
432 the interactions between services running in different sandboxes.
433
434#### LPAC File System Permissions
435 * Importantly, all locations in the filesystem and registry that the LPAC
436 process will access during its lifetime need to have the right ACLs on
437 them. `registryRead` is important for registry read access, and Windows
438 system files have `ALL RESTRICTED APPLICATION PACKAGES` ACE on them already,
439 but other files that the sandbox process needs access to including the
440 binaries (e.g. chrome.exe, chrome.dll) and also any data files need ACLs to
441 be laid down. This is typically done by the installer, and also done
442 automatically for tests. However, if the LPAC sandbox is to be used in other
443 environments then these filesystem permissions need to be manually laid down
444 using `icacls`, the installer, or a similar tool. An example of a ACE that
445 could be used can be found in
446 [`testing/scripts/common.py`](https://cs.chromium.org/chromium/src/testing/scripts/common.py)
447 however in high security environments a more restrictive SID should be used
448 such as one from the
449 [installer](https://source.chromium.org/chromium/chromium/src/+/main:chrome/installer/setup/install_worker.cc;l=74).
450
justincarlsonb4730a0c2017-04-20 20:22:13451### Other caveats
452
453The operating system might have bugs. Of interest are bugs in the Windows API
454that allow the bypass of the regular security checks. If such a bug exists,
455malware will be able to bypass the sandbox restrictions and broker policy and
456possibly compromise the computer. Under Windows, there is no practical way to
457prevent code in the sandbox from calling a system service.
458
459In addition, third party software, particularly anti-malware solutions, can
460create new attack vectors. The most troublesome are applications that inject
461dlls in order to enable some (usually unwanted) capability. These dlls will also
462get injected in the sandbox process. In the best case they will malfunction, and
463in the worst case can create backdoors to other processes or to the file system
464itself, enabling specially crafted malware to escape the sandbox.
465
466## Sandbox policy
467
468The actual restrictions applied to a target process are configured by a
469policy. The policy is just a programmatic interface that the broker calls to
470define the restrictions and allowances. Four functions control the restrictions,
471roughly corresponding to the four Windows mechanisms:
472
473* `TargetPolicy::SetTokenLevel()`
474* `TargetPolicy::SetJobLevel()`
475* `TargetPolicy::SetIntegrityLevel()`
476* `TargetPolicy::SetDesktop()`
477
478The first three calls take an integer level parameter that goes from very strict
479to very loose; for example, the token level has 7 levels and the job level has 5
480levels. Chromium renderers are typically run with the most strict level in all
481four mechanisms. Finally, the last (desktop) policy is binary and can only be
482used to indicate if a target is run on an alternate desktop or not.
483
484The restrictions are by design coarse in that they affect all securable
485resources that the target can touch, but sometimes a more finely-grained
486resolution is needed. The policy interface allows the broker to specify
487exceptions. An exception is a way to take a specific Windows API call issued in
488the target and proxy it over to the broker. The broker can inspect the
489parameters and re-issue the call as is, re-issue the call with different
490parameters, or simply deny the call. To specify exceptions there is a single
491call: `AddRule`. The following kinds of rules for different Windows subsystems
492are supported at this time:
493
494* Files
495* Named pipes
496* Process creation
497* Registry
498* Synchronization objects
499
500The exact form of the rules for each subsystem varies, but in general rules are
501triggered based on a string pattern. For example, a possible file rule is:
502
503 AddRule(SUBSYS_FILES, FILES_ALLOW_READONLY, L"c:\\temp\\app_log\\d*.dmp")
504
505This rule specifies that access will be granted if a target wants to open a
506file, for read-only access as long as the file matches the pattern expression;
507for example `c:\temp\app_log\domino.dmp` is a file that satisfies the
508pattern. Consult the header files for an up-to-date list of supported objects
509and supported actions.
510
511Rules can only be added before each target process is spawned, and cannot be
512modified while a target is running, but different targets can have different
513rules.
514
Alex Gough9e88d862020-06-05 18:20:39515### Diagnostics
516
517In Chromium, the policies associated with active processes can be viewed at
518chrome://sandbox. Tracing of the `sandbox` category will output the policy used
519when a process is launched. Tracing can be enabled using chrome://tracing or by
520using the `--trace-startup=-*,disabled-by-default-sandbox` command line flag.
521Trace output can be investigated with `//tools/win/trace-sandbox-viewer.py`.
522
justincarlsonb4730a0c2017-04-20 20:22:13523## Target bootstrapping
524
525Targets do not start executing with the restrictions specified by policy. They
526start executing with a token that is very close to the token the regular user
527processes have. The reason is that during process bootstrapping the OS loader
528accesses a lot of resources, most of them are actually undocumented and can
529change at any time. Also, most applications use the standard CRT provided with
530the standard development tools; after the process is bootstrapped the CRT needs
531to initialize as well and there again the internals of the CRT initialization
532are undocumented.
533
534Therefore, during the bootstrapping phase the process actually uses two tokens:
535the lockdown token which is the process token as is and the initial token which
536is set as the impersonation token of the initial thread. In fact the actual
537`SetTokenLevel` definition is:
538
539 SetTokenLevel(TokenLevel initial, TokenLevel lockdown)
540
541After all the critical initialization is done, execution continues at `main()`
542or `WinMain()`, here the two tokens are still active, but only the initial
543thread can use the more powerful initial token. It is the target's
544responsibility to discard the initial token when ready. This is done with a
545single call:
546
547 LowerToken()
548
549After this call is issued by the target the only token available is the lockdown
550token and the full sandbox restrictions go into effect. The effects of this call
551cannot be undone. Note that the initial token is a impersonation token only
552valid for the main thread, other threads created in the target process use only
553the lockdown token and therefore should not attempt to obtain any system
554resources subject to a security check.
555
556The fact that the target starts with a privileged token simplifies the explicit
557policy since anything privileged that needs to be done once, at process startup
558can be done before the `LowerToken()` call and does not require to have rules in
559the policy.
560
561**Important**
562
563Make sure any sensitive OS handles obtained with the initial token are closed
564before calling LowerToken(). Any leaked handle can be abused by malware to
565escape the sandbox.
566
567
568