Make content::StartSandboxedProcess return a Process instead of a handle.
[chromium-blink-merge.git] / content / common / sandbox_win.cc
blobe185217b789b0ec20de6020b508671903ebab8d6
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 "content/common/sandbox_win.h"
7 #include <string>
9 #include "base/base_switches.h"
10 #include "base/command_line.h"
11 #include "base/debug/profiler.h"
12 #include "base/debug/trace_event.h"
13 #include "base/files/file_util.h"
14 #include "base/hash.h"
15 #include "base/path_service.h"
16 #include "base/process/launch.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/win/iat_patch_function.h"
20 #include "base/win/scoped_handle.h"
21 #include "base/win/scoped_process_information.h"
22 #include "base/win/windows_version.h"
23 #include "content/public/common/content_client.h"
24 #include "content/public/common/content_switches.h"
25 #include "content/public/common/sandbox_init.h"
26 #include "content/public/common/sandboxed_process_launcher_delegate.h"
27 #include "sandbox/win/src/process_mitigations.h"
28 #include "sandbox/win/src/sandbox.h"
29 #include "sandbox/win/src/sandbox_nt_util.h"
30 #include "sandbox/win/src/win_utils.h"
31 #include "ui/gfx/win/direct_write.h"
33 static sandbox::BrokerServices* g_broker_services = NULL;
34 static sandbox::TargetServices* g_target_services = NULL;
36 namespace content {
37 namespace {
39 // The DLLs listed here are known (or under strong suspicion) of causing crashes
40 // when they are loaded in the renderer. Note: at runtime we generate short
41 // versions of the dll name only if the dll has an extension.
42 // For more information about how this list is generated, and how to get off
43 // of it, see:
44 // https://sites.google.com/a/chromium.org/dev/Home/third-party-developers
45 const wchar_t* const kTroublesomeDlls[] = {
46 L"adialhk.dll", // Kaspersky Internet Security.
47 L"acpiz.dll", // Unknown.
48 L"airfoilinject3.dll", // Airfoil.
49 L"akinsofthook32.dll", // Akinsoft Software Engineering.
50 L"assistant_x64.dll", // Unknown.
51 L"avcuf64.dll", // Bit Defender Internet Security x64.
52 L"avgrsstx.dll", // AVG 8.
53 L"babylonchromepi.dll", // Babylon translator.
54 L"btkeyind.dll", // Widcomm Bluetooth.
55 L"cmcsyshk.dll", // CMC Internet Security.
56 L"cmsetac.dll", // Unknown (suspected malware).
57 L"cooliris.dll", // CoolIris.
58 L"dockshellhook.dll", // Stardock Objectdock.
59 L"easyhook32.dll", // GDIPP and others.
60 L"googledesktopnetwork3.dll", // Google Desktop Search v5.
61 L"fwhook.dll", // PC Tools Firewall Plus.
62 L"hookprocesscreation.dll", // Blumentals Program protector.
63 L"hookterminateapis.dll", // Blumentals and Cyberprinter.
64 L"hookprintapis.dll", // Cyberprinter.
65 L"imon.dll", // NOD32 Antivirus.
66 L"ioloHL.dll", // Iolo (System Mechanic).
67 L"kloehk.dll", // Kaspersky Internet Security.
68 L"lawenforcer.dll", // Spyware-Browser AntiSpyware (Spybro).
69 L"libdivx.dll", // DivX.
70 L"lvprcinj01.dll", // Logitech QuickCam.
71 L"madchook.dll", // Madshi (generic hooking library).
72 L"mdnsnsp.dll", // Bonjour.
73 L"moonsysh.dll", // Moon Secure Antivirus.
74 L"mpk.dll", // KGB Spy.
75 L"npdivx32.dll", // DivX.
76 L"npggNT.des", // GameGuard 2008.
77 L"npggNT.dll", // GameGuard (older).
78 L"oawatch.dll", // Online Armor.
79 L"pavhook.dll", // Panda Internet Security.
80 L"pavlsphook.dll", // Panda Antivirus.
81 L"pavshook.dll", // Panda Antivirus.
82 L"pavshookwow.dll", // Panda Antivirus.
83 L"pctavhook.dll", // PC Tools Antivirus.
84 L"pctgmhk.dll", // PC Tools Spyware Doctor.
85 L"picrmi32.dll", // PicRec.
86 L"picrmi64.dll", // PicRec.
87 L"prntrack.dll", // Pharos Systems.
88 L"protector.dll", // Unknown (suspected malware).
89 L"radhslib.dll", // Radiant Naomi Internet Filter.
90 L"radprlib.dll", // Radiant Naomi Internet Filter.
91 L"rapportnikko.dll", // Trustware Rapport.
92 L"rlhook.dll", // Trustware Bufferzone.
93 L"rooksdol.dll", // Trustware Rapport.
94 L"rndlpepperbrowserrecordhelper.dll", // RealPlayer.
95 L"rpchromebrowserrecordhelper.dll", // RealPlayer.
96 L"r3hook.dll", // Kaspersky Internet Security.
97 L"sahook.dll", // McAfee Site Advisor.
98 L"sbrige.dll", // Unknown.
99 L"sc2hook.dll", // Supercopier 2.
100 L"sdhook32.dll", // Spybot - Search & Destroy Live Protection.
101 L"sguard.dll", // Iolo (System Guard).
102 L"smum32.dll", // Spyware Doctor version 6.
103 L"smumhook.dll", // Spyware Doctor version 5.
104 L"ssldivx.dll", // DivX.
105 L"syncor11.dll", // SynthCore Midi interface.
106 L"systools.dll", // Panda Antivirus.
107 L"tfwah.dll", // Threatfire (PC tools).
108 L"wblind.dll", // Stardock Object desktop.
109 L"wbhelp.dll", // Stardock Object desktop.
110 L"winstylerthemehelper.dll" // Tuneup utilities 2006.
113 // Adds the policy rules for the path and path\ with the semantic |access|.
114 // If |children| is set to true, we need to add the wildcard rules to also
115 // apply the rule to the subfiles and subfolders.
116 bool AddDirectory(int path, const wchar_t* sub_dir, bool children,
117 sandbox::TargetPolicy::Semantics access,
118 sandbox::TargetPolicy* policy) {
119 base::FilePath directory;
120 if (!PathService::Get(path, &directory))
121 return false;
123 if (sub_dir)
124 directory = base::MakeAbsoluteFilePath(directory.Append(sub_dir));
126 sandbox::ResultCode result;
127 result = policy->AddRule(sandbox::TargetPolicy::SUBSYS_FILES, access,
128 directory.value().c_str());
129 if (result != sandbox::SBOX_ALL_OK)
130 return false;
132 std::wstring directory_str = directory.value() + L"\\";
133 if (children)
134 directory_str += L"*";
135 // Otherwise, add the version of the path that ends with a separator.
137 result = policy->AddRule(sandbox::TargetPolicy::SUBSYS_FILES, access,
138 directory_str.c_str());
139 if (result != sandbox::SBOX_ALL_OK)
140 return false;
142 return true;
145 // Adds the policy rules for the path and path\* with the semantic |access|.
146 // We need to add the wildcard rules to also apply the rule to the subkeys.
147 bool AddKeyAndSubkeys(std::wstring key,
148 sandbox::TargetPolicy::Semantics access,
149 sandbox::TargetPolicy* policy) {
150 sandbox::ResultCode result;
151 result = policy->AddRule(sandbox::TargetPolicy::SUBSYS_REGISTRY, access,
152 key.c_str());
153 if (result != sandbox::SBOX_ALL_OK)
154 return false;
156 key += L"\\*";
157 result = policy->AddRule(sandbox::TargetPolicy::SUBSYS_REGISTRY, access,
158 key.c_str());
159 if (result != sandbox::SBOX_ALL_OK)
160 return false;
162 return true;
165 // Compares the loaded |module| file name matches |module_name|.
166 bool IsExpandedModuleName(HMODULE module, const wchar_t* module_name) {
167 wchar_t path[MAX_PATH];
168 DWORD sz = ::GetModuleFileNameW(module, path, arraysize(path));
169 if ((sz == arraysize(path)) || (sz == 0)) {
170 // XP does not set the last error properly, so we bail out anyway.
171 return false;
173 if (!::GetLongPathName(path, path, arraysize(path)))
174 return false;
175 base::FilePath fname(path);
176 return (fname.BaseName().value() == module_name);
179 // Adds a single dll by |module_name| into the |policy| blacklist.
180 // If |check_in_browser| is true we only add an unload policy only if the dll
181 // is also loaded in this process.
182 void BlacklistAddOneDll(const wchar_t* module_name,
183 bool check_in_browser,
184 sandbox::TargetPolicy* policy) {
185 HMODULE module = check_in_browser ? ::GetModuleHandleW(module_name) : NULL;
186 if (!module) {
187 // The module could have been loaded with a 8.3 short name. We check
188 // the three most common cases: 'thelongname.dll' becomes
189 // 'thelon~1.dll', 'thelon~2.dll' and 'thelon~3.dll'.
190 std::wstring name(module_name);
191 size_t period = name.rfind(L'.');
192 DCHECK_NE(std::string::npos, period);
193 DCHECK_LE(3U, (name.size() - period));
194 if (period <= 8)
195 return;
196 for (wchar_t ix = '1'; ix <= '3'; ++ix) {
197 const wchar_t suffix[] = {'~', ix, 0};
198 std::wstring alt_name = name.substr(0, 6) + suffix;
199 alt_name += name.substr(period, name.size());
200 if (check_in_browser) {
201 module = ::GetModuleHandleW(alt_name.c_str());
202 if (!module)
203 return;
204 // We found it, but because it only has 6 significant letters, we
205 // want to make sure it is the right one.
206 if (!IsExpandedModuleName(module, module_name))
207 return;
209 // Found a match. We add both forms to the policy.
210 policy->AddDllToUnload(alt_name.c_str());
213 policy->AddDllToUnload(module_name);
214 DVLOG(1) << "dll to unload found: " << module_name;
215 return;
218 // Adds policy rules for unloaded the known dlls that cause chrome to crash.
219 // Eviction of injected DLLs is done by the sandbox so that the injected module
220 // does not get a chance to execute any code.
221 void AddGenericDllEvictionPolicy(sandbox::TargetPolicy* policy) {
222 for (int ix = 0; ix != arraysize(kTroublesomeDlls); ++ix)
223 BlacklistAddOneDll(kTroublesomeDlls[ix], true, policy);
226 // Returns the object path prepended with the current logon session.
227 base::string16 PrependWindowsSessionPath(const base::char16* object) {
228 // Cache this because it can't change after process creation.
229 static uintptr_t s_session_id = 0;
230 if (s_session_id == 0) {
231 HANDLE token;
232 DWORD session_id_length;
233 DWORD session_id = 0;
235 CHECK(::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token));
236 CHECK(::GetTokenInformation(token, TokenSessionId, &session_id,
237 sizeof(session_id), &session_id_length));
238 CloseHandle(token);
239 if (session_id)
240 s_session_id = session_id;
243 return base::StringPrintf(L"\\Sessions\\%d%ls", s_session_id, object);
246 // Checks if the sandbox should be let to run without a job object assigned.
247 bool ShouldSetJobLevel(const base::CommandLine& cmd_line) {
248 if (!cmd_line.HasSwitch(switches::kAllowNoSandboxJob))
249 return true;
251 // Windows 8 allows nested jobs so we don't need to check if we are in other
252 // job.
253 if (base::win::GetVersion() >= base::win::VERSION_WIN8)
254 return true;
256 BOOL in_job = true;
257 // Either there is no job yet associated so we must add our job,
258 if (!::IsProcessInJob(::GetCurrentProcess(), NULL, &in_job))
259 NOTREACHED() << "IsProcessInJob failed. " << GetLastError();
260 if (!in_job)
261 return true;
263 // ...or there is a job but the JOB_OBJECT_LIMIT_BREAKAWAY_OK limit is set.
264 JOBOBJECT_EXTENDED_LIMIT_INFORMATION job_info = {0};
265 if (!::QueryInformationJobObject(NULL,
266 JobObjectExtendedLimitInformation, &job_info,
267 sizeof(job_info), NULL)) {
268 NOTREACHED() << "QueryInformationJobObject failed. " << GetLastError();
269 return true;
271 if (job_info.BasicLimitInformation.LimitFlags & JOB_OBJECT_LIMIT_BREAKAWAY_OK)
272 return true;
274 return false;
277 // Adds the generic policy rules to a sandbox TargetPolicy.
278 bool AddGenericPolicy(sandbox::TargetPolicy* policy) {
279 sandbox::ResultCode result;
281 // Renderers need to copy sections for plugin DIBs and GPU.
282 // GPU needs to copy sections to renderers.
283 result = policy->AddRule(sandbox::TargetPolicy::SUBSYS_HANDLES,
284 sandbox::TargetPolicy::HANDLES_DUP_ANY,
285 L"Section");
286 if (result != sandbox::SBOX_ALL_OK)
287 return false;
289 // Add the policy for the client side of a pipe. It is just a file
290 // in the \pipe\ namespace. We restrict it to pipes that start with
291 // "chrome." so the sandboxed process cannot connect to system services.
292 result = policy->AddRule(sandbox::TargetPolicy::SUBSYS_FILES,
293 sandbox::TargetPolicy::FILES_ALLOW_ANY,
294 L"\\??\\pipe\\chrome.*");
295 if (result != sandbox::SBOX_ALL_OK)
296 return false;
298 // Add the policy for the server side of nacl pipe. It is just a file
299 // in the \pipe\ namespace. We restrict it to pipes that start with
300 // "chrome.nacl" so the sandboxed process cannot connect to
301 // system services.
302 result = policy->AddRule(sandbox::TargetPolicy::SUBSYS_NAMED_PIPES,
303 sandbox::TargetPolicy::NAMEDPIPES_ALLOW_ANY,
304 L"\\\\.\\pipe\\chrome.nacl.*");
305 if (result != sandbox::SBOX_ALL_OK)
306 return false;
308 // Allow the server side of sync sockets, which are pipes that have
309 // the "chrome.sync" namespace and a randomly generated suffix.
310 result = policy->AddRule(sandbox::TargetPolicy::SUBSYS_NAMED_PIPES,
311 sandbox::TargetPolicy::NAMEDPIPES_ALLOW_ANY,
312 L"\\\\.\\pipe\\chrome.sync.*");
313 if (result != sandbox::SBOX_ALL_OK)
314 return false;
316 // Add the policy for debug message only in debug
317 #ifndef NDEBUG
318 base::FilePath app_dir;
319 if (!PathService::Get(base::DIR_MODULE, &app_dir))
320 return false;
322 wchar_t long_path_buf[MAX_PATH];
323 DWORD long_path_return_value = GetLongPathName(app_dir.value().c_str(),
324 long_path_buf,
325 MAX_PATH);
326 if (long_path_return_value == 0 || long_path_return_value >= MAX_PATH)
327 return false;
329 base::FilePath debug_message(long_path_buf);
330 debug_message = debug_message.AppendASCII("debug_message.exe");
331 result = policy->AddRule(sandbox::TargetPolicy::SUBSYS_PROCESS,
332 sandbox::TargetPolicy::PROCESS_MIN_EXEC,
333 debug_message.value().c_str());
334 if (result != sandbox::SBOX_ALL_OK)
335 return false;
336 #endif // NDEBUG
338 AddGenericDllEvictionPolicy(policy);
339 return true;
342 bool AddPolicyForSandboxedProcess(sandbox::TargetPolicy* policy) {
343 sandbox::ResultCode result;
344 // Renderers need to share events with plugins.
345 result = policy->AddRule(sandbox::TargetPolicy::SUBSYS_HANDLES,
346 sandbox::TargetPolicy::HANDLES_DUP_ANY,
347 L"Event");
348 if (result != sandbox::SBOX_ALL_OK)
349 return false;
351 // Win8+ adds a device DeviceApi that we don't need.
352 if (base::win::GetVersion() > base::win::VERSION_WIN7)
353 result = policy->AddKernelObjectToClose(L"File", L"\\Device\\DeviceApi");
354 if (result != sandbox::SBOX_ALL_OK)
355 return false;
357 // Close the proxy settings on XP.
358 if (base::win::GetVersion() <= base::win::VERSION_SERVER_2003)
359 result = policy->AddKernelObjectToClose(L"Key",
360 L"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\" \
361 L"CurrentVersion\\Internet Settings");
362 if (result != sandbox::SBOX_ALL_OK)
363 return false;
366 sandbox::TokenLevel initial_token = sandbox::USER_UNPROTECTED;
367 if (base::win::GetVersion() > base::win::VERSION_XP) {
368 // On 2003/Vista the initial token has to be restricted if the main
369 // token is restricted.
370 initial_token = sandbox::USER_RESTRICTED_SAME_ACCESS;
373 policy->SetTokenLevel(initial_token, sandbox::USER_LOCKDOWN);
374 // Prevents the renderers from manipulating low-integrity processes.
375 policy->SetDelayedIntegrityLevel(sandbox::INTEGRITY_LEVEL_UNTRUSTED);
376 policy->SetIntegrityLevel(sandbox::INTEGRITY_LEVEL_LOW);
378 if (sandbox::SBOX_ALL_OK != policy->SetAlternateDesktop(true)) {
379 DLOG(WARNING) << "Failed to apply desktop security to the renderer";
382 return true;
385 // Updates the command line arguments with debug-related flags. If debug flags
386 // have been used with this process, they will be filtered and added to
387 // command_line as needed.
388 void ProcessDebugFlags(base::CommandLine* command_line) {
389 const base::CommandLine& current_cmd_line =
390 *base::CommandLine::ForCurrentProcess();
391 std::string type = command_line->GetSwitchValueASCII(switches::kProcessType);
392 if (current_cmd_line.HasSwitch(switches::kWaitForDebuggerChildren)) {
393 // Look to pass-on the kWaitForDebugger flag.
394 std::string value = current_cmd_line.GetSwitchValueASCII(
395 switches::kWaitForDebuggerChildren);
396 if (value.empty() || value == type) {
397 command_line->AppendSwitch(switches::kWaitForDebugger);
399 command_line->AppendSwitchASCII(switches::kWaitForDebuggerChildren, value);
403 // This code is test only, and attempts to catch unsafe uses of
404 // DuplicateHandle() that copy privileged handles into sandboxed processes.
405 #ifndef OFFICIAL_BUILD
406 base::win::IATPatchFunction g_iat_patch_duplicate_handle;
408 typedef BOOL (WINAPI *DuplicateHandleFunctionPtr)(HANDLE source_process_handle,
409 HANDLE source_handle,
410 HANDLE target_process_handle,
411 LPHANDLE target_handle,
412 DWORD desired_access,
413 BOOL inherit_handle,
414 DWORD options);
416 DuplicateHandleFunctionPtr g_iat_orig_duplicate_handle;
418 NtQueryObject g_QueryObject = NULL;
420 static const char* kDuplicateHandleWarning =
421 "You are attempting to duplicate a privileged handle into a sandboxed"
422 " process.\n Please use the sandbox::BrokerDuplicateHandle API or"
423 " contact security@chromium.org for assistance.";
425 void CheckDuplicateHandle(HANDLE handle) {
426 // Get the object type (32 characters is safe; current max is 14).
427 BYTE buffer[sizeof(OBJECT_TYPE_INFORMATION) + 32 * sizeof(wchar_t)];
428 OBJECT_TYPE_INFORMATION* type_info =
429 reinterpret_cast<OBJECT_TYPE_INFORMATION*>(buffer);
430 ULONG size = sizeof(buffer) - sizeof(wchar_t);
431 NTSTATUS error;
432 error = g_QueryObject(handle, ObjectTypeInformation, type_info, size, &size);
433 CHECK(NT_SUCCESS(error));
434 type_info->Name.Buffer[type_info->Name.Length / sizeof(wchar_t)] = L'\0';
436 // Get the object basic information.
437 OBJECT_BASIC_INFORMATION basic_info;
438 size = sizeof(basic_info);
439 error = g_QueryObject(handle, ObjectBasicInformation, &basic_info, size,
440 &size);
441 CHECK(NT_SUCCESS(error));
443 CHECK(!(basic_info.GrantedAccess & WRITE_DAC)) <<
444 kDuplicateHandleWarning;
446 if (0 == _wcsicmp(type_info->Name.Buffer, L"Process")) {
447 const ACCESS_MASK kDangerousMask =
448 ~static_cast<DWORD>(PROCESS_QUERY_LIMITED_INFORMATION | SYNCHRONIZE);
449 CHECK(!(basic_info.GrantedAccess & kDangerousMask)) <<
450 kDuplicateHandleWarning;
454 BOOL WINAPI DuplicateHandlePatch(HANDLE source_process_handle,
455 HANDLE source_handle,
456 HANDLE target_process_handle,
457 LPHANDLE target_handle,
458 DWORD desired_access,
459 BOOL inherit_handle,
460 DWORD options) {
461 // Duplicate the handle so we get the final access mask.
462 if (!g_iat_orig_duplicate_handle(source_process_handle, source_handle,
463 target_process_handle, target_handle,
464 desired_access, inherit_handle, options))
465 return FALSE;
467 // We're not worried about broker handles or not crossing process boundaries.
468 if (source_process_handle == target_process_handle ||
469 target_process_handle == ::GetCurrentProcess())
470 return TRUE;
472 // Only sandboxed children are placed in jobs, so just check them.
473 BOOL is_in_job = FALSE;
474 if (!::IsProcessInJob(target_process_handle, NULL, &is_in_job)) {
475 // We need a handle with permission to check the job object.
476 if (ERROR_ACCESS_DENIED == ::GetLastError()) {
477 HANDLE temp_handle;
478 CHECK(g_iat_orig_duplicate_handle(::GetCurrentProcess(),
479 target_process_handle,
480 ::GetCurrentProcess(),
481 &temp_handle,
482 PROCESS_QUERY_INFORMATION,
483 FALSE, 0));
484 base::win::ScopedHandle process(temp_handle);
485 CHECK(::IsProcessInJob(process.Get(), NULL, &is_in_job));
489 if (is_in_job) {
490 // We never allow inheritable child handles.
491 CHECK(!inherit_handle) << kDuplicateHandleWarning;
493 // Duplicate the handle again, to get the final permissions.
494 HANDLE temp_handle;
495 CHECK(g_iat_orig_duplicate_handle(target_process_handle, *target_handle,
496 ::GetCurrentProcess(), &temp_handle,
497 0, FALSE, DUPLICATE_SAME_ACCESS));
498 base::win::ScopedHandle handle(temp_handle);
500 // Callers use CHECK macro to make sure we get the right stack.
501 CheckDuplicateHandle(handle.Get());
504 return TRUE;
506 #endif
508 } // namespace
510 void SetJobLevel(const base::CommandLine& cmd_line,
511 sandbox::JobLevel job_level,
512 uint32 ui_exceptions,
513 sandbox::TargetPolicy* policy) {
514 if (ShouldSetJobLevel(cmd_line)) {
515 #ifdef _WIN64
516 policy->SetJobMemoryLimit(4ULL * 1024 * 1024 * 1024);
517 #endif
518 policy->SetJobLevel(job_level, ui_exceptions);
519 } else {
520 policy->SetJobLevel(sandbox::JOB_NONE, 0);
524 // TODO(jschuh): Need get these restrictions applied to NaCl and Pepper.
525 // Just have to figure out what needs to be warmed up first.
526 void AddBaseHandleClosePolicy(sandbox::TargetPolicy* policy) {
527 // TODO(cpu): Add back the BaseNamedObjects policy.
528 base::string16 object_path = PrependWindowsSessionPath(
529 L"\\BaseNamedObjects\\windows_shell_global_counters");
530 policy->AddKernelObjectToClose(L"Section", object_path.data());
533 bool InitBrokerServices(sandbox::BrokerServices* broker_services) {
534 // TODO(abarth): DCHECK(CalledOnValidThread());
535 // See <http://b/1287166>.
536 DCHECK(broker_services);
537 DCHECK(!g_broker_services);
538 sandbox::ResultCode result = broker_services->Init();
539 g_broker_services = broker_services;
541 // In non-official builds warn about dangerous uses of DuplicateHandle.
542 #ifndef OFFICIAL_BUILD
543 BOOL is_in_job = FALSE;
544 CHECK(::IsProcessInJob(::GetCurrentProcess(), NULL, &is_in_job));
545 // In a Syzygy-profiled binary, instrumented for import profiling, this
546 // patch will end in infinite recursion on the attempted delegation to the
547 // original function.
548 if (!base::debug::IsBinaryInstrumented() &&
549 !is_in_job && !g_iat_patch_duplicate_handle.is_patched()) {
550 HMODULE module = NULL;
551 wchar_t module_name[MAX_PATH];
552 CHECK(::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
553 reinterpret_cast<LPCWSTR>(InitBrokerServices),
554 &module));
555 DWORD result = ::GetModuleFileNameW(module, module_name, MAX_PATH);
556 if (result && (result != MAX_PATH)) {
557 ResolveNTFunctionPtr("NtQueryObject", &g_QueryObject);
558 result = g_iat_patch_duplicate_handle.Patch(
559 module_name, "kernel32.dll", "DuplicateHandle",
560 DuplicateHandlePatch);
561 CHECK(result == 0);
562 g_iat_orig_duplicate_handle =
563 reinterpret_cast<DuplicateHandleFunctionPtr>(
564 g_iat_patch_duplicate_handle.original_function());
567 #endif
569 return sandbox::SBOX_ALL_OK == result;
572 bool InitTargetServices(sandbox::TargetServices* target_services) {
573 DCHECK(target_services);
574 DCHECK(!g_target_services);
575 sandbox::ResultCode result = target_services->Init();
576 g_target_services = target_services;
577 return sandbox::SBOX_ALL_OK == result;
580 base::Process StartSandboxedProcess(
581 SandboxedProcessLauncherDelegate* delegate,
582 base::CommandLine* cmd_line) {
583 const base::CommandLine& browser_command_line =
584 *base::CommandLine::ForCurrentProcess();
585 std::string type_str = cmd_line->GetSwitchValueASCII(switches::kProcessType);
587 TRACE_EVENT_BEGIN_ETW("StartProcessWithAccess", 0, type_str);
589 // Propagate the --allow-no-job flag if present.
590 if (browser_command_line.HasSwitch(switches::kAllowNoSandboxJob) &&
591 !cmd_line->HasSwitch(switches::kAllowNoSandboxJob)) {
592 cmd_line->AppendSwitch(switches::kAllowNoSandboxJob);
595 ProcessDebugFlags(cmd_line);
597 // Prefetch hints on windows:
598 // Using a different prefetch profile per process type will allow Windows
599 // to create separate pretetch settings for browser, renderer etc.
600 cmd_line->AppendArg(base::StringPrintf("/prefetch:%d", base::Hash(type_str)));
602 if ((delegate && !delegate->ShouldSandbox()) ||
603 browser_command_line.HasSwitch(switches::kNoSandbox) ||
604 cmd_line->HasSwitch(switches::kNoSandbox)) {
605 base::ProcessHandle handle = 0;
606 base::LaunchProcess(*cmd_line, base::LaunchOptions(), &handle);
607 // TODO(rvargas) crbug.com/417532: Don't share a raw handle.
608 g_broker_services->AddTargetPeer(handle);
609 return base::Process(handle);
612 sandbox::TargetPolicy* policy = g_broker_services->CreatePolicy();
614 sandbox::MitigationFlags mitigations = sandbox::MITIGATION_HEAP_TERMINATE |
615 sandbox::MITIGATION_BOTTOM_UP_ASLR |
616 sandbox::MITIGATION_DEP |
617 sandbox::MITIGATION_DEP_NO_ATL_THUNK |
618 sandbox::MITIGATION_SEHOP;
620 if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
621 type_str == switches::kRendererProcess &&
622 browser_command_line.HasSwitch(
623 switches::kEnableWin32kRendererLockDown)) {
624 if (policy->AddRule(sandbox::TargetPolicy::SUBSYS_WIN32K_LOCKDOWN,
625 sandbox::TargetPolicy::FAKE_USER_GDI_INIT,
626 NULL) != sandbox::SBOX_ALL_OK) {
627 return base::Process();
629 mitigations |= sandbox::MITIGATION_WIN32K_DISABLE;
632 if (policy->SetProcessMitigations(mitigations) != sandbox::SBOX_ALL_OK)
633 return base::Process();
635 mitigations = sandbox::MITIGATION_STRICT_HANDLE_CHECKS |
636 sandbox::MITIGATION_DLL_SEARCH_ORDER;
638 if (policy->SetDelayedProcessMitigations(mitigations) != sandbox::SBOX_ALL_OK)
639 return base::Process();
641 SetJobLevel(*cmd_line, sandbox::JOB_LOCKDOWN, 0, policy);
643 bool disable_default_policy = false;
644 base::FilePath exposed_dir;
645 if (delegate)
646 delegate->PreSandbox(&disable_default_policy, &exposed_dir);
648 if (!disable_default_policy && !AddPolicyForSandboxedProcess(policy))
649 return base::Process();
651 if (type_str == switches::kRendererProcess) {
652 #if !defined(NACL_WIN64)
653 if (gfx::win::ShouldUseDirectWrite()) {
654 AddDirectory(base::DIR_WINDOWS_FONTS,
655 NULL,
656 true,
657 sandbox::TargetPolicy::FILES_ALLOW_READONLY,
658 policy);
660 #endif
661 } else {
662 // Hack for Google Desktop crash. Trick GD into not injecting its DLL into
663 // this subprocess. See
664 // http://code.google.com/p/chromium/issues/detail?id=25580
665 cmd_line->AppendSwitchASCII("ignored", " --type=renderer ");
668 sandbox::ResultCode result;
669 if (!exposed_dir.empty()) {
670 result = policy->AddRule(sandbox::TargetPolicy::SUBSYS_FILES,
671 sandbox::TargetPolicy::FILES_ALLOW_ANY,
672 exposed_dir.value().c_str());
673 if (result != sandbox::SBOX_ALL_OK)
674 return base::Process();
676 base::FilePath exposed_files = exposed_dir.AppendASCII("*");
677 result = policy->AddRule(sandbox::TargetPolicy::SUBSYS_FILES,
678 sandbox::TargetPolicy::FILES_ALLOW_ANY,
679 exposed_files.value().c_str());
680 if (result != sandbox::SBOX_ALL_OK)
681 return base::Process();
684 if (!AddGenericPolicy(policy)) {
685 NOTREACHED();
686 return base::Process();
689 if (browser_command_line.HasSwitch(switches::kEnableLogging)) {
690 // If stdout/stderr point to a Windows console, these calls will
691 // have no effect.
692 policy->SetStdoutHandle(GetStdHandle(STD_OUTPUT_HANDLE));
693 policy->SetStderrHandle(GetStdHandle(STD_ERROR_HANDLE));
696 if (delegate) {
697 bool success = true;
698 delegate->PreSpawnTarget(policy, &success);
699 if (!success)
700 return base::Process();
703 TRACE_EVENT_BEGIN_ETW("StartProcessWithAccess::LAUNCHPROCESS", 0, 0);
705 PROCESS_INFORMATION temp_process_info = {};
706 result = g_broker_services->SpawnTarget(
707 cmd_line->GetProgram().value().c_str(),
708 cmd_line->GetCommandLineString().c_str(),
709 policy, &temp_process_info);
710 policy->Release();
711 base::win::ScopedProcessInformation target(temp_process_info);
713 TRACE_EVENT_END_ETW("StartProcessWithAccess::LAUNCHPROCESS", 0, 0);
715 if (sandbox::SBOX_ALL_OK != result) {
716 if (result == sandbox::SBOX_ERROR_GENERIC)
717 DPLOG(ERROR) << "Failed to launch process";
718 else
719 DLOG(ERROR) << "Failed to launch process. Error: " << result;
720 return base::Process();
723 if (delegate)
724 delegate->PostSpawnTarget(target.process_handle());
726 CHECK(ResumeThread(target.thread_handle()) != -1);
727 TRACE_EVENT_END_ETW("StartProcessWithAccess", 0, type_str);
728 return base::Process(target.TakeProcessHandle());
731 bool BrokerDuplicateHandle(HANDLE source_handle,
732 DWORD target_process_id,
733 HANDLE* target_handle,
734 DWORD desired_access,
735 DWORD options) {
736 // If our process is the target just duplicate the handle.
737 if (::GetCurrentProcessId() == target_process_id) {
738 return !!::DuplicateHandle(::GetCurrentProcess(), source_handle,
739 ::GetCurrentProcess(), target_handle,
740 desired_access, FALSE, options);
744 // Try the broker next
745 if (g_target_services &&
746 g_target_services->DuplicateHandle(source_handle, target_process_id,
747 target_handle, desired_access,
748 options) == sandbox::SBOX_ALL_OK) {
749 return true;
752 // Finally, see if we already have access to the process.
753 base::win::ScopedHandle target_process;
754 target_process.Set(::OpenProcess(PROCESS_DUP_HANDLE, FALSE,
755 target_process_id));
756 if (target_process.IsValid()) {
757 return !!::DuplicateHandle(::GetCurrentProcess(), source_handle,
758 target_process.Get(), target_handle,
759 desired_access, FALSE, options);
762 return false;
765 bool BrokerAddTargetPeer(HANDLE peer_process) {
766 return g_broker_services->AddTargetPeer(peer_process) == sandbox::SBOX_ALL_OK;
769 } // namespace content