| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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. | |
| 4 | |
| 5 #include "build/build_config.h" | |
| 6 | |
| 7 #if defined(OS_WIN) | |
| 8 #include <objbase.h> | |
| 9 #include <windows.h> | |
| 10 #endif | |
| 11 | |
| 12 #include "base/command_line.h" | |
| 13 #include "base/message_loop.h" | |
| 14 #include "base/string_util.h" | |
| 15 #include "base/threading/platform_thread.h" | |
| 16 #include "chrome/plugin/plugin_thread.h" | |
| 17 #include "content/common/child_process.h" | |
| 18 #include "content/common/content_switches.h" | |
| 19 #include "content/common/hi_res_timer_manager.h" | |
| 20 #include "content/common/main_function_params.h" | |
| 21 #include "ui/base/system_monitor/system_monitor.h" | |
| 22 | |
| 23 #if defined(OS_WIN) | |
| 24 #include "content/common/injection_test_dll.h" | |
| 25 #include "sandbox/src/sandbox.h" | |
| 26 #elif defined(OS_POSIX) && !defined(OS_MACOSX) | |
| 27 #include "base/global_descriptors_posix.h" | |
| 28 #include "ipc/ipc_descriptors.h" | |
| 29 #endif | |
| 30 | |
| 31 #if defined(OS_MACOSX) | |
| 32 // Removes our Carbon library interposing from the environment so that it | |
| 33 // doesn't carry into any processes that plugins might start. | |
| 34 void TrimInterposeEnvironment(); | |
| 35 | |
| 36 // Initializes the global Cocoa application object. | |
| 37 void InitializeChromeApplication(); | |
| 38 #elif defined(OS_LINUX) | |
| 39 // Work around an unimplemented instruction in 64-bit Flash. | |
| 40 void WorkaroundFlashLAHF(); | |
| 41 #endif | |
| 42 | |
| 43 #if defined(OS_WIN) | |
| 44 // This function is provided so that the built-in flash can lock down the | |
| 45 // sandbox by calling DelayedLowerToken(0). | |
| 46 extern "C" DWORD __declspec(dllexport) __stdcall DelayedLowerToken(void* ts) { | |
| 47 // s_ts is only set the first time the function is called, which happens | |
| 48 // in PluginMain. | |
| 49 static sandbox::TargetServices* s_ts = | |
| 50 reinterpret_cast<sandbox::TargetServices*>(ts); | |
| 51 if (ts) | |
| 52 return 0; | |
| 53 s_ts->LowerToken(); | |
| 54 return 1; | |
| 55 }; | |
| 56 | |
| 57 // Returns true if the plugin to be loaded is the internal flash. | |
| 58 bool IsPluginBuiltInFlash(const CommandLine& cmd_line) { | |
| 59 FilePath path = cmd_line.GetSwitchValuePath(switches::kPluginPath); | |
| 60 return (path.BaseName() == FilePath(L"gcswf32.dll")); | |
| 61 } | |
| 62 | |
| 63 // Before we lock down the flash sandbox, we need to activate | |
| 64 // the IME machinery. After lock down it seems it is unable | |
| 65 // to start. Note that we leak the IME context on purpose. | |
| 66 int PreloadIMEForFlash() { | |
| 67 HIMC imc = ::ImmCreateContext(); | |
| 68 if (!imc) | |
| 69 return 0; | |
| 70 if (::ImmGetOpenStatus(imc)) | |
| 71 return 1; | |
| 72 return 2; | |
| 73 } | |
| 74 | |
| 75 #endif | |
| 76 | |
| 77 // main() routine for running as the plugin process. | |
| 78 int PluginMain(const MainFunctionParams& parameters) { | |
| 79 // The main thread of the plugin services UI. | |
| 80 #if defined(OS_MACOSX) | |
| 81 #if !defined(__LP64__) | |
| 82 TrimInterposeEnvironment(); | |
| 83 #endif | |
| 84 InitializeChromeApplication(); | |
| 85 #endif | |
| 86 MessageLoop main_message_loop(MessageLoop::TYPE_UI); | |
| 87 base::PlatformThread::SetName("CrPluginMain"); | |
| 88 | |
| 89 ui::SystemMonitor system_monitor; | |
| 90 HighResolutionTimerManager high_resolution_timer_manager; | |
| 91 | |
| 92 const CommandLine& parsed_command_line = parameters.command_line_; | |
| 93 | |
| 94 #if defined(OS_LINUX) | |
| 95 | |
| 96 #if defined(ARCH_CPU_64_BITS) | |
| 97 WorkaroundFlashLAHF(); | |
| 98 #endif | |
| 99 | |
| 100 #elif defined(OS_WIN) | |
| 101 sandbox::TargetServices* target_services = | |
| 102 parameters.sandbox_info_.TargetServices(); | |
| 103 | |
| 104 CoInitialize(NULL); | |
| 105 DVLOG(1) << "Started plugin with " | |
| 106 << parsed_command_line.command_line_string(); | |
| 107 | |
| 108 HMODULE sandbox_test_module = NULL; | |
| 109 bool no_sandbox = parsed_command_line.HasSwitch(switches::kNoSandbox); | |
| 110 | |
| 111 if (target_services && !no_sandbox) { | |
| 112 // The command line might specify a test plugin to load. | |
| 113 if (parsed_command_line.HasSwitch(switches::kTestSandbox)) { | |
| 114 std::wstring test_plugin_name = | |
| 115 parsed_command_line.GetSwitchValueNative(switches::kTestSandbox); | |
| 116 sandbox_test_module = LoadLibrary(test_plugin_name.c_str()); | |
| 117 DCHECK(sandbox_test_module); | |
| 118 } | |
| 119 } | |
| 120 #endif | |
| 121 if (parsed_command_line.HasSwitch(switches::kPluginStartupDialog)) { | |
| 122 ChildProcess::WaitForDebugger("Plugin"); | |
| 123 } | |
| 124 | |
| 125 { | |
| 126 ChildProcess plugin_process; | |
| 127 plugin_process.set_main_thread(new PluginThread()); | |
| 128 #if defined(OS_WIN) | |
| 129 if (!no_sandbox && target_services) { | |
| 130 // We are sandboxing the plugin. If it is a generic plug-in, we lock down | |
| 131 // the sandbox right away, but if it is the built-in flash we let flash | |
| 132 // start elevated and it will call DelayedLowerToken(0) when it's ready. | |
| 133 if (IsPluginBuiltInFlash(parsed_command_line)) { | |
| 134 DVLOG(1) << "Sandboxing flash"; | |
| 135 if (!PreloadIMEForFlash()) | |
| 136 DVLOG(1) << "IME preload failed"; | |
| 137 DelayedLowerToken(target_services); | |
| 138 } else { | |
| 139 target_services->LowerToken(); | |
| 140 } | |
| 141 } | |
| 142 if (sandbox_test_module) { | |
| 143 RunPluginTests run_security_tests = | |
| 144 reinterpret_cast<RunPluginTests>(GetProcAddress(sandbox_test_module, | |
| 145 kPluginTestCall)); | |
| 146 DCHECK(run_security_tests); | |
| 147 if (run_security_tests) { | |
| 148 int test_count = 0; | |
| 149 DVLOG(1) << "Running plugin security tests"; | |
| 150 BOOL result = run_security_tests(&test_count); | |
| 151 DCHECK(result) << "Test number " << test_count << " has failed."; | |
| 152 // If we are in release mode, crash or debug the process. | |
| 153 if (!result) { | |
| 154 __debugbreak(); | |
| 155 _exit(1); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 FreeLibrary(sandbox_test_module); | |
| 160 } | |
| 161 #endif | |
| 162 | |
| 163 MessageLoop::current()->Run(); | |
| 164 } | |
| 165 | |
| 166 #if defined(OS_WIN) | |
| 167 CoUninitialize(); | |
| 168 #endif | |
| 169 | |
| 170 return 0; | |
| 171 } | |
| OLD | NEW |