Don't preload rarely seen large images
[chromium-blink-merge.git] / sandbox / win / src / target_services.cc
blob7c0afc210af5f5152adf9c1fbb315fba2d40b700
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sandbox/win/src/target_services.h"
7 #include <new>
9 #include <process.h>
11 #include "base/basictypes.h"
12 #include "base/win/windows_version.h"
13 #include "sandbox/win/src/crosscall_client.h"
14 #include "sandbox/win/src/handle_closer_agent.h"
15 #include "sandbox/win/src/handle_interception.h"
16 #include "sandbox/win/src/ipc_tags.h"
17 #include "sandbox/win/src/process_mitigations.h"
18 #include "sandbox/win/src/restricted_token_utils.h"
19 #include "sandbox/win/src/sandbox.h"
20 #include "sandbox/win/src/sandbox_types.h"
21 #include "sandbox/win/src/sharedmem_ipc_client.h"
22 #include "sandbox/win/src/sandbox_nt_util.h"
24 namespace {
26 // Flushing a cached key is triggered by just opening the key and closing the
27 // resulting handle. RegDisablePredefinedCache() is the documented way to flush
28 // HKCU so do not use it with this function.
29 bool FlushRegKey(HKEY root) {
30 HKEY key;
31 if (ERROR_SUCCESS == ::RegOpenKeyExW(root, NULL, 0, MAXIMUM_ALLOWED, &key)) {
32 if (ERROR_SUCCESS != ::RegCloseKey(key))
33 return false;
35 return true;
38 // This function forces advapi32.dll to release some internally cached handles
39 // that were made during calls to RegOpenkey and RegOpenKeyEx if it is called
40 // with a more restrictive token. Returns true if the flushing is succesful
41 // although this behavior is undocumented and there is no guarantee that in
42 // fact this will happen in future versions of windows.
43 bool FlushCachedRegHandles() {
44 return (FlushRegKey(HKEY_LOCAL_MACHINE) &&
45 FlushRegKey(HKEY_CLASSES_ROOT) &&
46 FlushRegKey(HKEY_USERS));
49 // Checks if we have handle entries pending and runs the closer.
50 bool CloseOpenHandles() {
51 if (sandbox::HandleCloserAgent::NeedsHandlesClosed()) {
52 sandbox::HandleCloserAgent handle_closer;
54 handle_closer.InitializeHandlesToClose();
55 if (!handle_closer.CloseHandles())
56 return false;
59 return true;
62 // Used as storage for g_target_services, because other allocation facilities
63 // are not available early. We can't use a regular function static because on
64 // VS2015, because the CRT tries to acquire a lock to guard initialization, but
65 // this code runs before the CRT is initialized.
66 char g_target_services_memory[sizeof(sandbox::TargetServicesBase)];
67 sandbox::TargetServicesBase* g_target_services = nullptr;
69 } // namespace
71 namespace sandbox {
73 SANDBOX_INTERCEPT IntegrityLevel g_shared_delayed_integrity_level =
74 INTEGRITY_LEVEL_LAST;
75 SANDBOX_INTERCEPT MitigationFlags g_shared_delayed_mitigations = 0;
77 TargetServicesBase::TargetServicesBase() {
80 ResultCode TargetServicesBase::Init() {
81 process_state_.SetInitCalled();
82 return SBOX_ALL_OK;
85 // Failure here is a breach of security so the process is terminated.
86 void TargetServicesBase::LowerToken() {
87 if (ERROR_SUCCESS !=
88 SetProcessIntegrityLevel(g_shared_delayed_integrity_level))
89 ::TerminateProcess(::GetCurrentProcess(), SBOX_FATAL_INTEGRITY);
90 process_state_.SetRevertedToSelf();
91 // If the client code as called RegOpenKey, advapi32.dll has cached some
92 // handles. The following code gets rid of them.
93 if (!::RevertToSelf())
94 ::TerminateProcess(::GetCurrentProcess(), SBOX_FATAL_DROPTOKEN);
95 if (!FlushCachedRegHandles())
96 ::TerminateProcess(::GetCurrentProcess(), SBOX_FATAL_FLUSHANDLES);
97 if (ERROR_SUCCESS != ::RegDisablePredefinedCache())
98 ::TerminateProcess(::GetCurrentProcess(), SBOX_FATAL_CACHEDISABLE);
99 if (!CloseOpenHandles())
100 ::TerminateProcess(::GetCurrentProcess(), SBOX_FATAL_CLOSEHANDLES);
101 // Enabling mitigations must happen last otherwise handle closing breaks
102 if (g_shared_delayed_mitigations &&
103 !ApplyProcessMitigationsToCurrentProcess(g_shared_delayed_mitigations))
104 ::TerminateProcess(::GetCurrentProcess(), SBOX_FATAL_MITIGATION);
107 ProcessState* TargetServicesBase::GetState() {
108 return &process_state_;
111 TargetServicesBase* TargetServicesBase::GetInstance() {
112 // Leak on purpose TargetServicesBase.
113 if (!g_target_services)
114 g_target_services = new (g_target_services_memory) TargetServicesBase;
115 return g_target_services;
118 // The broker services a 'test' IPC service with the IPC_PING_TAG tag.
119 bool TargetServicesBase::TestIPCPing(int version) {
120 void* memory = GetGlobalIPCMemory();
121 if (NULL == memory) {
122 return false;
124 SharedMemIPCClient ipc(memory);
125 CrossCallReturn answer = {0};
127 if (1 == version) {
128 uint32 tick1 = ::GetTickCount();
129 uint32 cookie = 717115;
130 ResultCode code = CrossCall(ipc, IPC_PING1_TAG, cookie, &answer);
132 if (SBOX_ALL_OK != code) {
133 return false;
135 // We should get two extended returns values from the IPC, one is the
136 // tick count on the broker and the other is the cookie times two.
137 if ((answer.extended_count != 2)) {
138 return false;
140 // We test the first extended answer to be within the bounds of the tick
141 // count only if there was no tick count wraparound.
142 uint32 tick2 = ::GetTickCount();
143 if (tick2 >= tick1) {
144 if ((answer.extended[0].unsigned_int < tick1) ||
145 (answer.extended[0].unsigned_int > tick2)) {
146 return false;
150 if (answer.extended[1].unsigned_int != cookie * 2) {
151 return false;
153 } else if (2 == version) {
154 uint32 cookie = 717111;
155 InOutCountedBuffer counted_buffer(&cookie, sizeof(cookie));
156 ResultCode code = CrossCall(ipc, IPC_PING2_TAG, counted_buffer, &answer);
158 if (SBOX_ALL_OK != code) {
159 return false;
161 if (cookie != 717111 * 3) {
162 return false;
164 } else {
165 return false;
167 return true;
170 ProcessState::ProcessState() : process_state_(0) {
173 bool ProcessState::IsKernel32Loaded() const {
174 return process_state_ != 0;
177 bool ProcessState::InitCalled() const {
178 return process_state_ > 1;
181 bool ProcessState::RevertedToSelf() const {
182 return process_state_ > 2;
185 void ProcessState::SetKernel32Loaded() {
186 if (!process_state_)
187 process_state_ = 1;
190 void ProcessState::SetInitCalled() {
191 if (process_state_ < 2)
192 process_state_ = 2;
195 void ProcessState::SetRevertedToSelf() {
196 if (process_state_ < 3)
197 process_state_ = 3;
200 ResultCode TargetServicesBase::DuplicateHandle(HANDLE source_handle,
201 DWORD target_process_id,
202 HANDLE* target_handle,
203 DWORD desired_access,
204 DWORD options) {
205 return sandbox::DuplicateHandleProxy(source_handle, target_process_id,
206 target_handle, desired_access, options);
209 } // namespace sandbox