OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/public/common/sandbox_init.h" | 5 #include "content/public/common/sandbox_init.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "content/common/sandbox_mac.h" | 10 #include "content/common/sandbox_mac.h" |
11 #include "content/public/common/content_switches.h" | 11 #include "content/public/common/content_switches.h" |
12 | 12 |
13 namespace content { | 13 namespace content { |
14 | 14 |
15 bool InitializeSandbox() { | 15 bool InitializeSandbox(int sandbox_type, const FilePath& allowed_dir) { |
16 using sandbox::Sandbox; | 16 // Warm up APIs before turning on the sandbox. |
17 sandbox::Sandbox::SandboxWarmup(sandbox_type); | |
18 | |
19 // Actually sandbox the process. | |
20 return sandbox::Sandbox::EnableSandbox(sandbox_type, allowed_dir); | |
21 } | |
22 | |
23 // Derives the SandboxType and the allowed directory from the current process' | |
24 // process type. Returns false, if the current process type isn't sandboxed. | |
jeremy
2011/11/24 16:42:04
How about:
Fill in |sandbox_type| and |allowed_dir
jochen (gone - plz use gerrit)
2011/11/24 20:08:35
Done.
| |
25 bool GetSandboxTypeFromCommandLine(int* sandbox_type, | |
26 FilePath* allowed_dir) { | |
27 DCHECK(sandbox_type); | |
28 DCHECK(allowed_dir); | |
29 | |
30 *sandbox_type = -1; | |
31 *allowed_dir = FilePath(); // Empty by default. | |
17 | 32 |
18 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 33 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
19 if (command_line.HasSwitch(switches::kNoSandbox)) | 34 if (command_line.HasSwitch(switches::kNoSandbox)) |
20 return true; | 35 return false; |
21 | |
22 Sandbox::SandboxProcessType sandbox_process_type; | |
23 FilePath allowed_dir; // Empty by default. | |
24 | 36 |
25 std::string process_type = | 37 std::string process_type = |
26 command_line.GetSwitchValueASCII(switches::kProcessType); | 38 command_line.GetSwitchValueASCII(switches::kProcessType); |
27 if (process_type.empty()) { | 39 if (process_type.empty()) { |
28 // Browser process isn't sandboxed. | 40 // Browser process isn't sandboxed. |
29 return true; | 41 return false; |
30 } else if (process_type == switches::kRendererProcess) { | 42 } else if (process_type == switches::kRendererProcess) { |
31 if (!command_line.HasSwitch(switches::kDisable3DAPIs) && | 43 if (!command_line.HasSwitch(switches::kDisable3DAPIs) && |
32 !command_line.HasSwitch(switches::kDisableExperimentalWebGL) && | 44 !command_line.HasSwitch(switches::kDisableExperimentalWebGL) && |
33 command_line.HasSwitch(switches::kInProcessWebGL)) { | 45 command_line.HasSwitch(switches::kInProcessWebGL)) { |
34 // TODO(kbr): this check seems to be necessary only on this | 46 // TODO(kbr): this check seems to be necessary only on this |
35 // platform because the sandbox is initialized later. Remove | 47 // platform because the sandbox is initialized later. Remove |
36 // this once this flag is removed. | 48 // this once this flag is removed. |
37 return true; | 49 return false; |
38 } else { | 50 } else { |
39 sandbox_process_type = Sandbox::SANDBOX_TYPE_RENDERER; | 51 *sandbox_type = SANDBOX_TYPE_RENDERER; |
40 } | 52 } |
41 } else if (process_type == switches::kUtilityProcess) { | 53 } else if (process_type == switches::kUtilityProcess) { |
42 // Utility process sandbox. | 54 // Utility process sandbox. |
43 sandbox_process_type = Sandbox::SANDBOX_TYPE_UTILITY; | 55 *sandbox_type = SANDBOX_TYPE_UTILITY; |
44 allowed_dir = | 56 *allowed_dir = |
45 command_line.GetSwitchValuePath(switches::kUtilityProcessAllowedDir); | 57 command_line.GetSwitchValuePath(switches::kUtilityProcessAllowedDir); |
46 } else if (process_type == switches::kWorkerProcess) { | 58 } else if (process_type == switches::kWorkerProcess) { |
47 // Worker process sandbox. | 59 // Worker process sandbox. |
48 sandbox_process_type = Sandbox::SANDBOX_TYPE_WORKER; | 60 *sandbox_type = SANDBOX_TYPE_WORKER; |
49 } else if (process_type == switches::kNaClLoaderProcess) { | |
50 // Native Client sel_ldr (user untrusted code) sandbox. | |
51 sandbox_process_type = Sandbox::SANDBOX_TYPE_NACL_LOADER; | |
52 } else if (process_type == switches::kGpuProcess) { | 61 } else if (process_type == switches::kGpuProcess) { |
53 sandbox_process_type = Sandbox::SANDBOX_TYPE_GPU; | 62 *sandbox_type = SANDBOX_TYPE_GPU; |
54 } else if ((process_type == switches::kPluginProcess) || | 63 } else if ((process_type == switches::kPluginProcess) || |
55 (process_type == switches::kServiceProcess) || | 64 (process_type == switches::kServiceProcess) || |
56 (process_type == switches::kPpapiBrokerProcess)) { | 65 (process_type == switches::kPpapiBrokerProcess)) { |
57 return true; | 66 return false; |
58 } else if (process_type == switches::kPpapiPluginProcess) { | 67 } else if (process_type == switches::kPpapiPluginProcess) { |
59 sandbox_process_type = Sandbox::SANDBOX_TYPE_PPAPI; | 68 *sandbox_type = SANDBOX_TYPE_PPAPI; |
60 } else { | 69 } else { |
61 // Failsafe: If you hit an unreached here, is your new process type in need | 70 // Failsafe: If you hit an unreached here, is your new process type in need |
62 // of sandboxing? | 71 // of sandboxing? |
63 NOTREACHED() << "Unknown process type " << process_type; | 72 NOTREACHED() << "Unknown process type " << process_type; |
73 return false; | |
74 } | |
75 return true; | |
76 } | |
77 | |
78 bool InitializeSandbox() { | |
79 int sandbox_type = 0; | |
80 FilePath allowed_dir; | |
81 if (!GetSandboxTypeFromCommandLine(&sandbox_type, &allowed_dir)) | |
64 return true; | 82 return true; |
65 } | 83 return InitializeSandbox(sandbox_type, allowed_dir); |
66 | |
67 // Warm up APIs before turning on the sandbox. | |
68 Sandbox::SandboxWarmup(sandbox_process_type); | |
69 | |
70 // Actually sandbox the process. | |
71 return Sandbox::EnableSandbox(sandbox_process_type, allowed_dir); | |
72 } | 84 } |
73 | 85 |
74 } // namespace content | 86 } // namespace content |
OLD | NEW |