[email protected] | 13f1d5c | 2012-03-06 03:10:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 16caf2c2 | 2013-04-13 01:26:07 | [diff] [blame] | 5 | #include "content/zygote/zygote_main.h" |
| 6 | |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 7 | #include <dlfcn.h> |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 8 | #include <fcntl.h> |
[email protected] | 724df99 | 2010-09-21 18:19:10 | [diff] [blame] | 9 | #include <pthread.h> |
[email protected] | 66c84c6a | 2014-05-16 05:29:57 | [diff] [blame] | 10 | #include <string.h> |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 11 | #include <sys/socket.h> |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 12 | #include <sys/types.h> |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 13 | #include <unistd.h> |
| 14 | |
[email protected] | 789f70c7 | 2009-07-24 13:55:43 | [diff] [blame] | 15 | #include "base/basictypes.h" |
[email protected] | 97692ca | 2013-11-27 05:14:55 | [diff] [blame] | 16 | #include "base/bind.h" |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 17 | #include "base/command_line.h" |
[email protected] | fd42e0b | 2014-02-17 16:41:21 | [diff] [blame] | 18 | #include "base/compiler_specific.h" |
[email protected] | 54ad01a | 2014-05-11 01:17:47 | [diff] [blame] | 19 | #include "base/memory/scoped_vector.h" |
[email protected] | dac6a5a | 2013-07-25 05:06:48 | [diff] [blame] | 20 | #include "base/native_library.h" |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 21 | #include "base/pickle.h" |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 22 | #include "base/posix/eintr_wrapper.h" |
[email protected] | 8c89c59 | 2013-01-18 10:34:29 | [diff] [blame] | 23 | #include "base/posix/unix_domain_socket_linux.h" |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 24 | #include "base/rand_util.h" |
[email protected] | 356b5202 | 2014-06-03 21:57:38 | [diff] [blame] | 25 | #include "base/strings/string_number_conversions.h" |
[email protected] | 80a086c5 | 2009-08-04 17:52:04 | [diff] [blame] | 26 | #include "base/sys_info.h" |
[email protected] | 8015de3 | 2009-11-18 04:13:33 | [diff] [blame] | 27 | #include "build/build_config.h" |
[email protected] | a0c900e | 2013-09-11 18:16:10 | [diff] [blame] | 28 | #include "content/common/child_process_sandbox_support_impl_linux.h" |
[email protected] | 797c355 | 2011-03-17 00:26:18 | [diff] [blame] | 29 | #include "content/common/font_config_ipc_linux.h" |
[email protected] | dac6a5a | 2013-07-25 05:06:48 | [diff] [blame] | 30 | #include "content/common/pepper_plugin_list.h" |
[email protected] | 5c9cddd | 2013-12-13 07:03:29 | [diff] [blame] | 31 | #include "content/common/sandbox_linux/sandbox_linux.h" |
[email protected] | 4cbc5788 | 2012-05-23 00:54:14 | [diff] [blame] | 32 | #include "content/common/zygote_commands_linux.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 33 | #include "content/public/common/content_switches.h" |
[email protected] | 4573fbd | 2011-10-31 20:25:18 | [diff] [blame] | 34 | #include "content/public/common/main_function_params.h" |
[email protected] | dac6a5a | 2013-07-25 05:06:48 | [diff] [blame] | 35 | #include "content/public/common/pepper_plugin_info.h" |
[email protected] | a31fe0f | 2012-05-08 19:28:51 | [diff] [blame] | 36 | #include "content/public/common/sandbox_linux.h" |
[email protected] | f8ba13d | 2011-10-25 16:08:52 | [diff] [blame] | 37 | #include "content/public/common/zygote_fork_delegate_linux.h" |
[email protected] | b75346c | 2012-05-13 03:48:38 | [diff] [blame] | 38 | #include "content/zygote/zygote_linux.h" |
[email protected] | 8c89c59 | 2013-01-18 10:34:29 | [diff] [blame] | 39 | #include "crypto/nss_util.h" |
[email protected] | 5174d000 | 2014-03-03 21:04:43 | [diff] [blame] | 40 | #include "sandbox/linux/services/init_process_reaper.h" |
[email protected] | 59d816b | 2012-07-13 01:28:07 | [diff] [blame] | 41 | #include "sandbox/linux/services/libc_urandom_override.h" |
[email protected] | a73ed20 | 2012-07-23 23:36:39 | [diff] [blame] | 42 | #include "sandbox/linux/suid/client/setuid_sandbox_client.h" |
[email protected] | 8bbf619 | 2013-07-18 11:14:04 | [diff] [blame] | 43 | #include "third_party/icu/source/i18n/unicode/timezone.h" |
[email protected] | 9628e0d | 2013-03-12 13:11:54 | [diff] [blame] | 44 | #include "third_party/skia/include/ports/SkFontConfigInterface.h" |
[email protected] | 1831acf | 2009-10-05 16:38:41 | [diff] [blame] | 45 | |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 46 | #if defined(OS_LINUX) |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 47 | #include <sys/prctl.h> |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 48 | #endif |
| 49 | |
[email protected] | 91feba4 | 2013-07-02 08:23:01 | [diff] [blame] | 50 | #if defined(ENABLE_WEBRTC) |
| 51 | #include "third_party/libjingle/overrides/init_webrtc.h" |
| 52 | #endif |
| 53 | |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 54 | #if defined(ADDRESS_SANITIZER) |
| 55 | #include <sanitizer/asan_interface.h> |
| 56 | #endif |
| 57 | |
[email protected] | a31fe0f | 2012-05-08 19:28:51 | [diff] [blame] | 58 | namespace content { |
| 59 | |
[email protected] | b75346c | 2012-05-13 03:48:38 | [diff] [blame] | 60 | // See http://code.google.com/p/chromium/wiki/LinuxZygote |
[email protected] | 2bc039a | 2011-07-29 23:05:41 | [diff] [blame] | 61 | |
[email protected] | ad2c7f2 | 2013-08-09 02:31:28 | [diff] [blame] | 62 | static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output, |
| 63 | char* timezone_out, |
| 64 | size_t timezone_out_len) { |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 65 | Pickle request; |
| 66 | request.WriteInt(LinuxSandbox::METHOD_LOCALTIME); |
| 67 | request.WriteString( |
| 68 | std::string(reinterpret_cast<char*>(&input), sizeof(input))); |
| 69 | |
| 70 | uint8_t reply_buf[512]; |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 71 | const ssize_t r = UnixDomainSocket::SendRecvMsg( |
[email protected] | a0c900e | 2013-09-11 18:16:10 | [diff] [blame] | 72 | GetSandboxFD(), reply_buf, sizeof(reply_buf), NULL, |
[email protected] | b75346c | 2012-05-13 03:48:38 | [diff] [blame] | 73 | request); |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 74 | if (r == -1) { |
| 75 | memset(output, 0, sizeof(struct tm)); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | Pickle reply(reinterpret_cast<char*>(reply_buf), r); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 80 | PickleIterator iter(reply); |
[email protected] | 9debcda5 | 2009-07-22 20:06:51 | [diff] [blame] | 81 | std::string result, timezone; |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 82 | if (!reply.ReadString(&iter, &result) || |
[email protected] | 9debcda5 | 2009-07-22 20:06:51 | [diff] [blame] | 83 | !reply.ReadString(&iter, &timezone) || |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 84 | result.size() != sizeof(struct tm)) { |
| 85 | memset(output, 0, sizeof(struct tm)); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | memcpy(output, result.data(), sizeof(struct tm)); |
[email protected] | 9debcda5 | 2009-07-22 20:06:51 | [diff] [blame] | 90 | if (timezone_out_len) { |
| 91 | const size_t copy_len = std::min(timezone_out_len - 1, timezone.size()); |
| 92 | memcpy(timezone_out, timezone.data(), copy_len); |
| 93 | timezone_out[copy_len] = 0; |
| 94 | output->tm_zone = timezone_out; |
| 95 | } else { |
| 96 | output->tm_zone = NULL; |
| 97 | } |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 100 | static bool g_am_zygote_or_renderer = false; |
| 101 | |
| 102 | // Sandbox interception of libc calls. |
| 103 | // |
| 104 | // Because we are running in a sandbox certain libc calls will fail (localtime |
| 105 | // being the motivating example - it needs to read /etc/localtime). We need to |
| 106 | // intercept these calls and proxy them to the browser. However, these calls |
| 107 | // may come from us or from our libraries. In some cases we can't just change |
| 108 | // our code. |
| 109 | // |
| 110 | // It's for these cases that we have the following setup: |
| 111 | // |
| 112 | // We define global functions for those functions which we wish to override. |
| 113 | // Since we will be first in the dynamic resolution order, the dynamic linker |
| 114 | // will point callers to our versions of these functions. However, we have the |
| 115 | // same binary for both the browser and the renderers, which means that our |
| 116 | // overrides will apply in the browser too. |
| 117 | // |
| 118 | // The global |g_am_zygote_or_renderer| is true iff we are in a zygote or |
| 119 | // renderer process. It's set in ZygoteMain and inherited by the renderers when |
| 120 | // they fork. (This means that it'll be incorrect for global constructor |
| 121 | // functions and before ZygoteMain is called - beware). |
| 122 | // |
| 123 | // Our replacement functions can check this global and either proxy |
| 124 | // the call to the browser over the sandbox IPC |
| 125 | // (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use |
| 126 | // dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the |
| 127 | // current module. |
| 128 | // |
| 129 | // Other avenues: |
| 130 | // |
| 131 | // Our first attempt involved some assembly to patch the GOT of the current |
| 132 | // module. This worked, but was platform specific and doesn't catch the case |
| 133 | // where a library makes a call rather than current module. |
| 134 | // |
| 135 | // We also considered patching the function in place, but this would again by |
| 136 | // platform specific and the above technique seems to work well enough. |
| 137 | |
[email protected] | 724df99 | 2010-09-21 18:19:10 | [diff] [blame] | 138 | typedef struct tm* (*LocaltimeFunction)(const time_t* timep); |
| 139 | typedef struct tm* (*LocaltimeRFunction)(const time_t* timep, |
| 140 | struct tm* result); |
| 141 | |
| 142 | static pthread_once_t g_libc_localtime_funcs_guard = PTHREAD_ONCE_INIT; |
| 143 | static LocaltimeFunction g_libc_localtime; |
[email protected] | a817246 | 2012-05-16 23:46:19 | [diff] [blame] | 144 | static LocaltimeFunction g_libc_localtime64; |
[email protected] | 724df99 | 2010-09-21 18:19:10 | [diff] [blame] | 145 | static LocaltimeRFunction g_libc_localtime_r; |
[email protected] | a817246 | 2012-05-16 23:46:19 | [diff] [blame] | 146 | static LocaltimeRFunction g_libc_localtime64_r; |
[email protected] | 724df99 | 2010-09-21 18:19:10 | [diff] [blame] | 147 | |
[email protected] | ad2c7f2 | 2013-08-09 02:31:28 | [diff] [blame] | 148 | static void InitLibcLocaltimeFunctions() { |
[email protected] | 724df99 | 2010-09-21 18:19:10 | [diff] [blame] | 149 | g_libc_localtime = reinterpret_cast<LocaltimeFunction>( |
[email protected] | c17a8952 | 2014-02-12 00:06:53 | [diff] [blame] | 150 | dlsym(RTLD_NEXT, "localtime")); |
[email protected] | bf1580b | 2014-02-11 03:55:48 | [diff] [blame] | 151 | g_libc_localtime64 = reinterpret_cast<LocaltimeFunction>( |
[email protected] | c17a8952 | 2014-02-12 00:06:53 | [diff] [blame] | 152 | dlsym(RTLD_NEXT, "localtime64")); |
| 153 | g_libc_localtime_r = reinterpret_cast<LocaltimeRFunction>( |
| 154 | dlsym(RTLD_NEXT, "localtime_r")); |
[email protected] | a817246 | 2012-05-16 23:46:19 | [diff] [blame] | 155 | g_libc_localtime64_r = reinterpret_cast<LocaltimeRFunction>( |
[email protected] | c17a8952 | 2014-02-12 00:06:53 | [diff] [blame] | 156 | dlsym(RTLD_NEXT, "localtime64_r")); |
[email protected] | 724df99 | 2010-09-21 18:19:10 | [diff] [blame] | 157 | |
| 158 | if (!g_libc_localtime || !g_libc_localtime_r) { |
| 159 | // http://code.google.com/p/chromium/issues/detail?id=16800 |
| 160 | // |
| 161 | // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces |
| 162 | // it with a version which doesn't work. In this case we'll get a NULL |
| 163 | // result. There's not a lot we can do at this point, so we just bodge it! |
| 164 | LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been " |
| 165 | "reported to be caused by Nvidia's libGL. You should expect" |
| 166 | " time related functions to misbehave. " |
| 167 | "http://code.google.com/p/chromium/issues/detail?id=16800"; |
| 168 | } |
| 169 | |
| 170 | if (!g_libc_localtime) |
| 171 | g_libc_localtime = gmtime; |
[email protected] | a817246 | 2012-05-16 23:46:19 | [diff] [blame] | 172 | if (!g_libc_localtime64) |
| 173 | g_libc_localtime64 = g_libc_localtime; |
[email protected] | 724df99 | 2010-09-21 18:19:10 | [diff] [blame] | 174 | if (!g_libc_localtime_r) |
| 175 | g_libc_localtime_r = gmtime_r; |
[email protected] | a817246 | 2012-05-16 23:46:19 | [diff] [blame] | 176 | if (!g_libc_localtime64_r) |
| 177 | g_libc_localtime64_r = g_libc_localtime_r; |
[email protected] | 724df99 | 2010-09-21 18:19:10 | [diff] [blame] | 178 | } |
[email protected] | a5d7bb8 | 2009-09-08 22:30:58 | [diff] [blame] | 179 | |
[email protected] | a817246 | 2012-05-16 23:46:19 | [diff] [blame] | 180 | // Define localtime_override() function with asm name "localtime", so that all |
| 181 | // references to localtime() will resolve to this function. Notice that we need |
| 182 | // to set visibility attribute to "default" to export the symbol, as it is set |
| 183 | // to "hidden" by default in chrome per build/common.gypi. |
| 184 | __attribute__ ((__visibility__("default"))) |
| 185 | struct tm* localtime_override(const time_t* timep) __asm__ ("localtime"); |
| 186 | |
| 187 | __attribute__ ((__visibility__("default"))) |
| 188 | struct tm* localtime_override(const time_t* timep) { |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 189 | if (g_am_zygote_or_renderer) { |
| 190 | static struct tm time_struct; |
| 191 | static char timezone_string[64]; |
| 192 | ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string, |
| 193 | sizeof(timezone_string)); |
| 194 | return &time_struct; |
| 195 | } else { |
[email protected] | 724df99 | 2010-09-21 18:19:10 | [diff] [blame] | 196 | CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard, |
| 197 | InitLibcLocaltimeFunctions)); |
[email protected] | fd42e0b | 2014-02-17 16:41:21 | [diff] [blame] | 198 | struct tm* res = g_libc_localtime(timep); |
| 199 | #if defined(MEMORY_SANITIZER) |
| 200 | if (res) __msan_unpoison(res, sizeof(*res)); |
[email protected] | 00f38351 | 2014-05-13 16:50:36 | [diff] [blame] | 201 | if (res->tm_zone) __msan_unpoison_string(res->tm_zone); |
[email protected] | fd42e0b | 2014-02-17 16:41:21 | [diff] [blame] | 202 | #endif |
| 203 | return res; |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 204 | } |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 205 | } |
| 206 | |
[email protected] | a817246 | 2012-05-16 23:46:19 | [diff] [blame] | 207 | // Use same trick to override localtime64(), localtime_r() and localtime64_r(). |
| 208 | __attribute__ ((__visibility__("default"))) |
| 209 | struct tm* localtime64_override(const time_t* timep) __asm__ ("localtime64"); |
| 210 | |
| 211 | __attribute__ ((__visibility__("default"))) |
| 212 | struct tm* localtime64_override(const time_t* timep) { |
| 213 | if (g_am_zygote_or_renderer) { |
| 214 | static struct tm time_struct; |
| 215 | static char timezone_string[64]; |
| 216 | ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string, |
| 217 | sizeof(timezone_string)); |
| 218 | return &time_struct; |
| 219 | } else { |
| 220 | CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard, |
| 221 | InitLibcLocaltimeFunctions)); |
[email protected] | fd42e0b | 2014-02-17 16:41:21 | [diff] [blame] | 222 | struct tm* res = g_libc_localtime64(timep); |
| 223 | #if defined(MEMORY_SANITIZER) |
| 224 | if (res) __msan_unpoison(res, sizeof(*res)); |
[email protected] | 00f38351 | 2014-05-13 16:50:36 | [diff] [blame] | 225 | if (res->tm_zone) __msan_unpoison_string(res->tm_zone); |
[email protected] | fd42e0b | 2014-02-17 16:41:21 | [diff] [blame] | 226 | #endif |
| 227 | return res; |
[email protected] | a817246 | 2012-05-16 23:46:19 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | __attribute__ ((__visibility__("default"))) |
| 232 | struct tm* localtime_r_override(const time_t* timep, |
| 233 | struct tm* result) __asm__ ("localtime_r"); |
| 234 | |
| 235 | __attribute__ ((__visibility__("default"))) |
| 236 | struct tm* localtime_r_override(const time_t* timep, struct tm* result) { |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 237 | if (g_am_zygote_or_renderer) { |
| 238 | ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0); |
| 239 | return result; |
| 240 | } else { |
[email protected] | 724df99 | 2010-09-21 18:19:10 | [diff] [blame] | 241 | CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard, |
| 242 | InitLibcLocaltimeFunctions)); |
[email protected] | fd42e0b | 2014-02-17 16:41:21 | [diff] [blame] | 243 | struct tm* res = g_libc_localtime_r(timep, result); |
| 244 | #if defined(MEMORY_SANITIZER) |
| 245 | if (res) __msan_unpoison(res, sizeof(*res)); |
[email protected] | 00f38351 | 2014-05-13 16:50:36 | [diff] [blame] | 246 | if (res->tm_zone) __msan_unpoison_string(res->tm_zone); |
[email protected] | fd42e0b | 2014-02-17 16:41:21 | [diff] [blame] | 247 | #endif |
| 248 | return res; |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 249 | } |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 250 | } |
| 251 | |
[email protected] | a817246 | 2012-05-16 23:46:19 | [diff] [blame] | 252 | __attribute__ ((__visibility__("default"))) |
| 253 | struct tm* localtime64_r_override(const time_t* timep, |
| 254 | struct tm* result) __asm__ ("localtime64_r"); |
| 255 | |
| 256 | __attribute__ ((__visibility__("default"))) |
| 257 | struct tm* localtime64_r_override(const time_t* timep, struct tm* result) { |
| 258 | if (g_am_zygote_or_renderer) { |
| 259 | ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0); |
| 260 | return result; |
| 261 | } else { |
| 262 | CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard, |
| 263 | InitLibcLocaltimeFunctions)); |
[email protected] | fd42e0b | 2014-02-17 16:41:21 | [diff] [blame] | 264 | struct tm* res = g_libc_localtime64_r(timep, result); |
| 265 | #if defined(MEMORY_SANITIZER) |
| 266 | if (res) __msan_unpoison(res, sizeof(*res)); |
[email protected] | 00f38351 | 2014-05-13 16:50:36 | [diff] [blame] | 267 | if (res->tm_zone) __msan_unpoison_string(res->tm_zone); |
[email protected] | fd42e0b | 2014-02-17 16:41:21 | [diff] [blame] | 268 | #endif |
| 269 | return res; |
[email protected] | a817246 | 2012-05-16 23:46:19 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
[email protected] | dac6a5a | 2013-07-25 05:06:48 | [diff] [blame] | 273 | #if defined(ENABLE_PLUGINS) |
| 274 | // Loads the (native) libraries but does not initialize them (i.e., does not |
| 275 | // call PPP_InitializeModule). This is needed by the zygote on Linux to get |
| 276 | // access to the plugins before entering the sandbox. |
| 277 | void PreloadPepperPlugins() { |
| 278 | std::vector<PepperPluginInfo> plugins; |
| 279 | ComputePepperPluginList(&plugins); |
| 280 | for (size_t i = 0; i < plugins.size(); ++i) { |
| 281 | if (!plugins[i].is_internal && plugins[i].is_sandboxed) { |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 282 | base::NativeLibraryLoadError error; |
[email protected] | dac6a5a | 2013-07-25 05:06:48 | [diff] [blame] | 283 | base::NativeLibrary library = base::LoadNativeLibrary(plugins[i].path, |
| 284 | &error); |
[email protected] | 132bca8 | 2013-12-10 09:14:47 | [diff] [blame] | 285 | VLOG_IF(1, !library) << "Unable to load plugin " |
| 286 | << plugins[i].path.value() << " " |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 287 | << error.ToString(); |
[email protected] | 132bca8 | 2013-12-10 09:14:47 | [diff] [blame] | 288 | |
[email protected] | dac6a5a | 2013-07-25 05:06:48 | [diff] [blame] | 289 | (void)library; // Prevent release-mode warning. |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | #endif |
| 294 | |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 295 | // This function triggers the static and lazy construction of objects that need |
| 296 | // to be created before imposing the sandbox. |
[email protected] | 5174d000 | 2014-03-03 21:04:43 | [diff] [blame] | 297 | static void ZygotePreSandboxInit() { |
[email protected] | b841941 | 2010-03-29 20:18:29 | [diff] [blame] | 298 | base::RandUint64(); |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 299 | |
[email protected] | 849447a | 2013-11-04 20:50:14 | [diff] [blame] | 300 | base::SysInfo::AmountOfPhysicalMemory(); |
[email protected] | b841941 | 2010-03-29 20:18:29 | [diff] [blame] | 301 | base::SysInfo::MaxSharedMemorySize(); |
[email protected] | c15d1e77 | 2013-11-20 11:14:51 | [diff] [blame] | 302 | base::SysInfo::NumberOfProcessors(); |
[email protected] | 80a086c5 | 2009-08-04 17:52:04 | [diff] [blame] | 303 | |
[email protected] | b841941 | 2010-03-29 20:18:29 | [diff] [blame] | 304 | // ICU DateFormat class (used in base/time_format.cc) needs to get the |
| 305 | // Olson timezone ID by accessing the zoneinfo files on disk. After |
| 306 | // TimeZone::createDefault is called once here, the timezone ID is |
| 307 | // cached and there's no more need to access the file system. |
| 308 | scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); |
[email protected] | 1831acf | 2009-10-05 16:38:41 | [diff] [blame] | 309 | |
[email protected] | eeb0855 | 2011-03-18 11:15:39 | [diff] [blame] | 310 | #if defined(USE_NSS) |
[email protected] | f6a67b4 | 2011-03-17 23:49:21 | [diff] [blame] | 311 | // NSS libraries are loaded before sandbox is activated. This is to allow |
| 312 | // successful initialization of NSS which tries to load extra library files. |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 313 | crypto::LoadNSSLibraries(); |
[email protected] | 1f554845 | 2011-10-06 06:46:15 | [diff] [blame] | 314 | #elif defined(USE_OPENSSL) |
| 315 | // OpenSSL is intentionally not supported in the sandboxed processes, see |
| 316 | // http://crbug.com/99163. If that ever changes we'll likely need to init |
| 317 | // OpenSSL here (at least, load the library and error strings). |
[email protected] | eeb0855 | 2011-03-18 11:15:39 | [diff] [blame] | 318 | #else |
[email protected] | 1f554845 | 2011-10-06 06:46:15 | [diff] [blame] | 319 | // It's possible that another hypothetical crypto stack would not require |
| 320 | // pre-sandbox init, but more likely this is just a build configuration error. |
| 321 | #error Which SSL library are you using? |
[email protected] | f6a67b4 | 2011-03-17 23:49:21 | [diff] [blame] | 322 | #endif |
[email protected] | 0106cde | 2013-02-27 02:16:38 | [diff] [blame] | 323 | #if defined(ENABLE_PLUGINS) |
[email protected] | 1777304 | 2010-07-28 17:35:07 | [diff] [blame] | 324 | // Ensure access to the Pepper plugins before the sandbox is turned on. |
[email protected] | dac6a5a | 2013-07-25 05:06:48 | [diff] [blame] | 325 | PreloadPepperPlugins(); |
[email protected] | 0106cde | 2013-02-27 02:16:38 | [diff] [blame] | 326 | #endif |
[email protected] | e4aa290b | 2013-05-01 18:54:19 | [diff] [blame] | 327 | #if defined(ENABLE_WEBRTC) |
| 328 | InitializeWebRtcModule(); |
| 329 | #endif |
[email protected] | 576748e8 | 2013-09-05 00:41:35 | [diff] [blame] | 330 | SkFontConfigInterface::SetGlobal( |
[email protected] | a0c900e | 2013-09-11 18:16:10 | [diff] [blame] | 331 | new FontConfigIPC(GetSandboxFD()))->unref(); |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 332 | } |
| 333 | |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 334 | static bool CreateInitProcessReaper(base::Closure* post_fork_parent_callback) { |
[email protected] | 5174d000 | 2014-03-03 21:04:43 | [diff] [blame] | 335 | // The current process becomes init(1), this function returns from a |
| 336 | // newly created process. |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 337 | const bool init_created = |
| 338 | sandbox::CreateInitProcessReaper(post_fork_parent_callback); |
[email protected] | 5174d000 | 2014-03-03 21:04:43 | [diff] [blame] | 339 | if (!init_created) { |
| 340 | LOG(ERROR) << "Error creating an init process to reap zombies"; |
| 341 | return false; |
| 342 | } |
| 343 | return true; |
| 344 | } |
[email protected] | 576748e8 | 2013-09-05 00:41:35 | [diff] [blame] | 345 | |
[email protected] | 5174d000 | 2014-03-03 21:04:43 | [diff] [blame] | 346 | // Enter the setuid sandbox. This requires the current process to have been |
| 347 | // created through the setuid sandbox. |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 348 | static bool EnterSuidSandbox(sandbox::SetuidSandboxClient* setuid_sandbox, |
| 349 | base::Closure* post_fork_parent_callback) { |
[email protected] | 5174d000 | 2014-03-03 21:04:43 | [diff] [blame] | 350 | DCHECK(setuid_sandbox); |
| 351 | DCHECK(setuid_sandbox->IsSuidSandboxChild()); |
[email protected] | 576748e8 | 2013-09-05 00:41:35 | [diff] [blame] | 352 | |
[email protected] | 5174d000 | 2014-03-03 21:04:43 | [diff] [blame] | 353 | // Use the SUID sandbox. This still allows the seccomp sandbox to |
| 354 | // be enabled by the process later. |
| 355 | |
| 356 | if (!setuid_sandbox->IsSuidSandboxUpToDate()) { |
| 357 | LOG(WARNING) << |
| 358 | "You are using a wrong version of the setuid binary!\n" |
| 359 | "Please read " |
| 360 | "https://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment." |
| 361 | "\n\n"; |
| 362 | } |
| 363 | |
| 364 | if (!setuid_sandbox->ChrootMe()) |
[email protected] | a73ed20 | 2012-07-23 23:36:39 | [diff] [blame] | 365 | return false; |
[email protected] | b75346c | 2012-05-13 03:48:38 | [diff] [blame] | 366 | |
[email protected] | 5174d000 | 2014-03-03 21:04:43 | [diff] [blame] | 367 | if (setuid_sandbox->IsInNewPIDNamespace()) { |
| 368 | CHECK_EQ(1, getpid()) |
| 369 | << "The SUID sandbox created a new PID namespace but Zygote " |
| 370 | "is not the init process. Please, make sure the SUID " |
| 371 | "binary is up to date."; |
| 372 | } |
| 373 | |
| 374 | if (getpid() == 1) { |
| 375 | // The setuid sandbox has created a new PID namespace and we need |
| 376 | // to assume the role of init. |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 377 | CHECK(CreateInitProcessReaper(post_fork_parent_callback)); |
[email protected] | 5174d000 | 2014-03-03 21:04:43 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | #if !defined(OS_OPENBSD) |
| 381 | // Previously, we required that the binary be non-readable. This causes the |
| 382 | // kernel to mark the process as non-dumpable at startup. The thinking was |
| 383 | // that, although we were putting the renderers into a PID namespace (with |
| 384 | // the SUID sandbox), they would nonetheless be in the /same/ PID |
| 385 | // namespace. So they could ptrace each other unless they were non-dumpable. |
| 386 | // |
| 387 | // If the binary was readable, then there would be a window between process |
| 388 | // startup and the point where we set the non-dumpable flag in which a |
| 389 | // compromised renderer could ptrace attach. |
| 390 | // |
| 391 | // However, now that we have a zygote model, only the (trusted) zygote |
| 392 | // exists at this point and we can set the non-dumpable flag which is |
| 393 | // inherited by all our renderer children. |
| 394 | // |
| 395 | // Note: a non-dumpable process can't be debugged. To debug sandbox-related |
| 396 | // issues, one can specify --allow-sandbox-debugging to let the process be |
| 397 | // dumpable. |
| 398 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 399 | if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) { |
| 400 | prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); |
| 401 | if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { |
| 402 | LOG(ERROR) << "Failed to set non-dumpable flag"; |
| 403 | return false; |
| 404 | } |
| 405 | } |
| 406 | #endif |
| 407 | |
| 408 | return true; |
| 409 | } |
| 410 | |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 411 | #if defined(ADDRESS_SANITIZER) |
| 412 | const size_t kSanitizerMaxMessageLength = 1 * 1024 * 1024; |
| 413 | |
| 414 | // A helper process which collects code coverage data from the renderers over a |
| 415 | // socket and dumps it to a file. See http://crbug.com/336212 for discussion. |
| 416 | static void SanitizerCoverageHelper(int socket_fd, int file_fd) { |
| 417 | scoped_ptr<char[]> buffer(new char[kSanitizerMaxMessageLength]); |
| 418 | while (true) { |
| 419 | ssize_t received_size = HANDLE_EINTR( |
| 420 | recv(socket_fd, buffer.get(), kSanitizerMaxMessageLength, 0)); |
| 421 | PCHECK(received_size >= 0); |
| 422 | if (received_size == 0) |
| 423 | // All clients have closed the socket. We should die. |
| 424 | _exit(0); |
| 425 | PCHECK(file_fd >= 0); |
| 426 | ssize_t written_size = 0; |
| 427 | while (written_size < received_size) { |
| 428 | ssize_t write_res = |
| 429 | HANDLE_EINTR(write(file_fd, buffer.get() + written_size, |
| 430 | received_size - written_size)); |
| 431 | PCHECK(write_res >= 0); |
| 432 | written_size += write_res; |
| 433 | } |
| 434 | PCHECK(0 == HANDLE_EINTR(fsync(file_fd))); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // fds[0] is the read end, fds[1] is the write end. |
| 439 | static void CreateSanitizerCoverageSocketPair(int fds[2]) { |
| 440 | PCHECK(0 == socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
| 441 | PCHECK(0 == shutdown(fds[0], SHUT_WR)); |
| 442 | PCHECK(0 == shutdown(fds[1], SHUT_RD)); |
| 443 | } |
| 444 | |
| 445 | static pid_t ForkSanitizerCoverageHelper(int child_fd, int parent_fd, |
| 446 | base::ScopedFD file_fd) { |
| 447 | pid_t pid = fork(); |
| 448 | PCHECK(pid >= 0); |
| 449 | if (pid == 0) { |
| 450 | // In the child. |
| 451 | PCHECK(0 == IGNORE_EINTR(close(parent_fd))); |
| 452 | SanitizerCoverageHelper(child_fd, file_fd.get()); |
| 453 | _exit(0); |
| 454 | } else { |
| 455 | // In the parent. |
| 456 | PCHECK(0 == IGNORE_EINTR(close(child_fd))); |
| 457 | return pid; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | void CloseFdPair(const int fds[2]) { |
| 462 | PCHECK(0 == IGNORE_EINTR(close(fds[0]))); |
| 463 | PCHECK(0 == IGNORE_EINTR(close(fds[1]))); |
| 464 | } |
| 465 | #endif // defined(ADDRESS_SANITIZER) |
| 466 | |
[email protected] | 21add09 | 2014-04-17 01:07:34 | [diff] [blame] | 467 | // If |is_suid_sandbox_child|, then make sure that the setuid sandbox is |
| 468 | // engaged. |
| 469 | static void EnterLayerOneSandbox(LinuxSandbox* linux_sandbox, |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 470 | bool is_suid_sandbox_child, |
| 471 | base::Closure* post_fork_parent_callback) { |
[email protected] | 5174d000 | 2014-03-03 21:04:43 | [diff] [blame] | 472 | DCHECK(linux_sandbox); |
| 473 | |
| 474 | ZygotePreSandboxInit(); |
[email protected] | 576748e8 | 2013-09-05 00:41:35 | [diff] [blame] | 475 | |
| 476 | // Check that the pre-sandbox initialization didn't spawn threads. |
[email protected] | 1ffd85d | 2014-01-29 17:06:11 | [diff] [blame] | 477 | #if !defined(THREAD_SANITIZER) |
[email protected] | 576748e8 | 2013-09-05 00:41:35 | [diff] [blame] | 478 | DCHECK(linux_sandbox->IsSingleThreaded()); |
[email protected] | 1ffd85d | 2014-01-29 17:06:11 | [diff] [blame] | 479 | #endif |
[email protected] | e14b4d34 | 2012-04-16 22:01:50 | [diff] [blame] | 480 | |
[email protected] | 5174d000 | 2014-03-03 21:04:43 | [diff] [blame] | 481 | sandbox::SetuidSandboxClient* setuid_sandbox = |
| 482 | linux_sandbox->setuid_sandbox_client(); |
| 483 | |
[email protected] | 21add09 | 2014-04-17 01:07:34 | [diff] [blame] | 484 | if (is_suid_sandbox_child) { |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 485 | CHECK(EnterSuidSandbox(setuid_sandbox, post_fork_parent_callback)) |
| 486 | << "Failed to enter setuid sandbox"; |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 487 | } |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 488 | } |
| 489 | |
[email protected] | a31fe0f | 2012-05-08 19:28:51 | [diff] [blame] | 490 | bool ZygoteMain(const MainFunctionParams& params, |
[email protected] | 54ad01a | 2014-05-11 01:17:47 | [diff] [blame] | 491 | ScopedVector<ZygoteForkDelegate> fork_delegates) { |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 492 | g_am_zygote_or_renderer = true; |
[email protected] | 59d816b | 2012-07-13 01:28:07 | [diff] [blame] | 493 | sandbox::InitLibcUrandomOverrides(); |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 494 | |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 495 | base::Closure *post_fork_parent_callback = NULL; |
| 496 | |
[email protected] | 3426ce1 | 2012-08-02 23:14:21 | [diff] [blame] | 497 | LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance(); |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 498 | |
| 499 | #if defined(ADDRESS_SANITIZER) |
[email protected] | 356b5202 | 2014-06-03 21:57:38 | [diff] [blame] | 500 | const std::string sancov_file_name = |
| 501 | "zygote." + base::Uint64ToString(base::RandUint64()); |
| 502 | base::ScopedFD sancov_file_fd( |
| 503 | __sanitizer_maybe_open_cov_file(sancov_file_name.c_str())); |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 504 | int sancov_socket_fds[2] = {-1, -1}; |
| 505 | CreateSanitizerCoverageSocketPair(sancov_socket_fds); |
| 506 | linux_sandbox->sanitizer_args()->coverage_sandboxed = 1; |
| 507 | linux_sandbox->sanitizer_args()->coverage_fd = sancov_socket_fds[1]; |
| 508 | linux_sandbox->sanitizer_args()->coverage_max_block_size = |
| 509 | kSanitizerMaxMessageLength; |
| 510 | // Zygote termination will block until the helper process exits, which will |
| 511 | // not happen until the write end of the socket is closed everywhere. Make |
| 512 | // sure the init process does not hold on to it. |
| 513 | base::Closure close_sancov_socket_fds = |
| 514 | base::Bind(&CloseFdPair, sancov_socket_fds); |
| 515 | post_fork_parent_callback = &close_sancov_socket_fds; |
| 516 | #endif |
| 517 | |
[email protected] | 3426ce1 | 2012-08-02 23:14:21 | [diff] [blame] | 518 | // This will pre-initialize the various sandboxes that need it. |
[email protected] | c6c9f501 | 2013-05-01 00:04:18 | [diff] [blame] | 519 | linux_sandbox->PreinitializeSandbox(); |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 520 | |
[email protected] | 21add09 | 2014-04-17 01:07:34 | [diff] [blame] | 521 | const bool must_enable_setuid_sandbox = |
| 522 | linux_sandbox->setuid_sandbox_client()->IsSuidSandboxChild(); |
[email protected] | 74722e7 | 2014-04-28 19:07:56 | [diff] [blame] | 523 | if (must_enable_setuid_sandbox) { |
[email protected] | f9d2a4f | 2014-05-02 04:38:02 | [diff] [blame] | 524 | linux_sandbox->setuid_sandbox_client()->CloseDummyFile(); |
[email protected] | bc89a49a | 2014-05-01 06:03:40 | [diff] [blame] | 525 | |
| 526 | // Let the ZygoteHost know we're booting up. |
| 527 | CHECK(UnixDomainSocket::SendMsg(kZygoteSocketPairFd, |
| 528 | kZygoteBootMessage, |
| 529 | sizeof(kZygoteBootMessage), |
| 530 | std::vector<int>())); |
[email protected] | 74722e7 | 2014-04-28 19:07:56 | [diff] [blame] | 531 | } |
[email protected] | 21add09 | 2014-04-17 01:07:34 | [diff] [blame] | 532 | |
[email protected] | 54ad01a | 2014-05-11 01:17:47 | [diff] [blame] | 533 | VLOG(1) << "ZygoteMain: initializing " << fork_delegates.size() |
| 534 | << " fork delegates"; |
| 535 | for (ScopedVector<ZygoteForkDelegate>::iterator i = fork_delegates.begin(); |
| 536 | i != fork_delegates.end(); |
| 537 | ++i) { |
| 538 | (*i)->Init(GetSandboxFD(), must_enable_setuid_sandbox); |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 539 | } |
| 540 | |
[email protected] | 5174d000 | 2014-03-03 21:04:43 | [diff] [blame] | 541 | // Turn on the first layer of the sandbox if the configuration warrants it. |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 542 | EnterLayerOneSandbox(linux_sandbox, must_enable_setuid_sandbox, |
| 543 | post_fork_parent_callback); |
| 544 | |
| 545 | std::vector<pid_t> extra_children; |
| 546 | std::vector<int> extra_fds; |
| 547 | |
| 548 | #if defined(ADDRESS_SANITIZER) |
| 549 | pid_t sancov_helper_pid = ForkSanitizerCoverageHelper( |
| 550 | sancov_socket_fds[0], sancov_socket_fds[1], sancov_file_fd.Pass()); |
| 551 | // It's important that the zygote reaps the helper before dying. Otherwise, |
| 552 | // the destruction of the PID namespace could kill the helper before it |
| 553 | // completes its I/O tasks. |sancov_helper_pid| will exit once the last |
| 554 | // renderer holding the write end of |sancov_socket_fds| closes it. |
| 555 | extra_children.push_back(sancov_helper_pid); |
| 556 | // Sanitizer code in the renderers will inherit the write end of the socket |
| 557 | // from the zygote. We must keep it open until the very end of the zygote's |
| 558 | // lifetime, even though we don't explicitly use it. |
| 559 | extra_fds.push_back(sancov_socket_fds[1]); |
| 560 | #endif |
[email protected] | 715b4f26 | 2010-07-13 14:17:28 | [diff] [blame] | 561 | |
[email protected] | 3426ce1 | 2012-08-02 23:14:21 | [diff] [blame] | 562 | int sandbox_flags = linux_sandbox->GetStatus(); |
[email protected] | 21add09 | 2014-04-17 01:07:34 | [diff] [blame] | 563 | bool setuid_sandbox_engaged = sandbox_flags & kSandboxLinuxSUID; |
| 564 | CHECK_EQ(must_enable_setuid_sandbox, setuid_sandbox_engaged); |
[email protected] | e8c916a | 2009-11-04 17:52:47 | [diff] [blame] | 565 | |
[email protected] | 655abd52 | 2014-06-02 15:23:43 | [diff] [blame] | 566 | Zygote zygote(sandbox_flags, fork_delegates.Pass(), extra_children, |
| 567 | extra_fds); |
[email protected] | c548be2 | 2010-03-08 12:55:57 | [diff] [blame] | 568 | // This function call can return multiple times, once per fork(). |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 569 | return zygote.ProcessRequests(); |
| 570 | } |
[email protected] | a31fe0f | 2012-05-08 19:28:51 | [diff] [blame] | 571 | |
| 572 | } // namespace content |