blob: 60f08b6ad636760ee03bb8d26df13e9075b109a1 [file] [log] [blame]
[email protected]13f1d5c2012-03-06 03:10:331// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]cc8f1462009-06-12 17:36:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]16caf2c22013-04-13 01:26:075#include "content/zygote/zygote_main.h"
6
[email protected]a0f200ea2009-08-06 18:44:067#include <dlfcn.h>
[email protected]655abd522014-06-02 15:23:438#include <fcntl.h>
[email protected]724df992010-09-21 18:19:109#include <pthread.h>
[email protected]66c84c6a2014-05-16 05:29:5710#include <string.h>
[email protected]655abd522014-06-02 15:23:4311#include <sys/socket.h>
[email protected]8ecd3aad2009-11-04 08:32:2212#include <sys/types.h>
[email protected]8ecd3aad2009-11-04 08:32:2213#include <unistd.h>
14
[email protected]789f70c72009-07-24 13:55:4315#include "base/basictypes.h"
[email protected]97692ca2013-11-27 05:14:5516#include "base/bind.h"
[email protected]cc8f1462009-06-12 17:36:5517#include "base/command_line.h"
[email protected]fd42e0b2014-02-17 16:41:2118#include "base/compiler_specific.h"
[email protected]54ad01a2014-05-11 01:17:4719#include "base/memory/scoped_vector.h"
[email protected]dac6a5a2013-07-25 05:06:4820#include "base/native_library.h"
[email protected]cc8f1462009-06-12 17:36:5521#include "base/pickle.h"
[email protected]655abd522014-06-02 15:23:4322#include "base/posix/eintr_wrapper.h"
[email protected]8c89c592013-01-18 10:34:2923#include "base/posix/unix_domain_socket_linux.h"
[email protected]4378a822009-07-08 01:15:1424#include "base/rand_util.h"
[email protected]356b52022014-06-03 21:57:3825#include "base/strings/string_number_conversions.h"
[email protected]80a086c52009-08-04 17:52:0426#include "base/sys_info.h"
[email protected]8015de32009-11-18 04:13:3327#include "build/build_config.h"
[email protected]a0c900e2013-09-11 18:16:1028#include "content/common/child_process_sandbox_support_impl_linux.h"
[email protected]797c3552011-03-17 00:26:1829#include "content/common/font_config_ipc_linux.h"
[email protected]dac6a5a2013-07-25 05:06:4830#include "content/common/pepper_plugin_list.h"
[email protected]5c9cddd2013-12-13 07:03:2931#include "content/common/sandbox_linux/sandbox_linux.h"
[email protected]4cbc57882012-05-23 00:54:1432#include "content/common/zygote_commands_linux.h"
[email protected]c08950d22011-10-13 22:20:2933#include "content/public/common/content_switches.h"
[email protected]4573fbd2011-10-31 20:25:1834#include "content/public/common/main_function_params.h"
[email protected]dac6a5a2013-07-25 05:06:4835#include "content/public/common/pepper_plugin_info.h"
[email protected]a31fe0f2012-05-08 19:28:5136#include "content/public/common/sandbox_linux.h"
[email protected]f8ba13d2011-10-25 16:08:5237#include "content/public/common/zygote_fork_delegate_linux.h"
[email protected]b75346c2012-05-13 03:48:3838#include "content/zygote/zygote_linux.h"
[email protected]8c89c592013-01-18 10:34:2939#include "crypto/nss_util.h"
[email protected]5174d0002014-03-03 21:04:4340#include "sandbox/linux/services/init_process_reaper.h"
[email protected]59d816b2012-07-13 01:28:0741#include "sandbox/linux/services/libc_urandom_override.h"
[email protected]a73ed202012-07-23 23:36:3942#include "sandbox/linux/suid/client/setuid_sandbox_client.h"
[email protected]8bbf6192013-07-18 11:14:0443#include "third_party/icu/source/i18n/unicode/timezone.h"
[email protected]9628e0d2013-03-12 13:11:5444#include "third_party/skia/include/ports/SkFontConfigInterface.h"
[email protected]1831acf2009-10-05 16:38:4145
[email protected]e63c4d72011-05-31 22:38:2946#if defined(OS_LINUX)
[email protected]e63c4d72011-05-31 22:38:2947#include <sys/prctl.h>
[email protected]e63c4d72011-05-31 22:38:2948#endif
49
[email protected]91feba42013-07-02 08:23:0150#if defined(ENABLE_WEBRTC)
51#include "third_party/libjingle/overrides/init_webrtc.h"
52#endif
53
[email protected]655abd522014-06-02 15:23:4354#if defined(ADDRESS_SANITIZER)
55#include <sanitizer/asan_interface.h>
56#endif
57
[email protected]a31fe0f2012-05-08 19:28:5158namespace content {
59
[email protected]b75346c2012-05-13 03:48:3860// See http://code.google.com/p/chromium/wiki/LinuxZygote
[email protected]2bc039a2011-07-29 23:05:4161
[email protected]ad2c7f22013-08-09 02:31:2862static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
63 char* timezone_out,
64 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:0765 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]cf3ac3972010-12-22 20:02:2971 const ssize_t r = UnixDomainSocket::SendRecvMsg(
[email protected]a0c900e2013-09-11 18:16:1072 GetSandboxFD(), reply_buf, sizeof(reply_buf), NULL,
[email protected]b75346c2012-05-13 03:48:3873 request);
[email protected]73fa63992009-07-20 20:30:0774 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]ce208f872012-03-07 20:42:5680 PickleIterator iter(reply);
[email protected]9debcda52009-07-22 20:06:5181 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:0782 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:5183 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:0784 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]9debcda52009-07-22 20:06:5190 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]73fa63992009-07-20 20:30:0798}
99
[email protected]a0f200ea2009-08-06 18:44:06100static 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]724df992010-09-21 18:19:10138typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
139typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
140 struct tm* result);
141
142static pthread_once_t g_libc_localtime_funcs_guard = PTHREAD_ONCE_INIT;
143static LocaltimeFunction g_libc_localtime;
[email protected]a8172462012-05-16 23:46:19144static LocaltimeFunction g_libc_localtime64;
[email protected]724df992010-09-21 18:19:10145static LocaltimeRFunction g_libc_localtime_r;
[email protected]a8172462012-05-16 23:46:19146static LocaltimeRFunction g_libc_localtime64_r;
[email protected]724df992010-09-21 18:19:10147
[email protected]ad2c7f22013-08-09 02:31:28148static void InitLibcLocaltimeFunctions() {
[email protected]724df992010-09-21 18:19:10149 g_libc_localtime = reinterpret_cast<LocaltimeFunction>(
[email protected]c17a89522014-02-12 00:06:53150 dlsym(RTLD_NEXT, "localtime"));
[email protected]bf1580b2014-02-11 03:55:48151 g_libc_localtime64 = reinterpret_cast<LocaltimeFunction>(
[email protected]c17a89522014-02-12 00:06:53152 dlsym(RTLD_NEXT, "localtime64"));
153 g_libc_localtime_r = reinterpret_cast<LocaltimeRFunction>(
154 dlsym(RTLD_NEXT, "localtime_r"));
[email protected]a8172462012-05-16 23:46:19155 g_libc_localtime64_r = reinterpret_cast<LocaltimeRFunction>(
[email protected]c17a89522014-02-12 00:06:53156 dlsym(RTLD_NEXT, "localtime64_r"));
[email protected]724df992010-09-21 18:19:10157
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]a8172462012-05-16 23:46:19172 if (!g_libc_localtime64)
173 g_libc_localtime64 = g_libc_localtime;
[email protected]724df992010-09-21 18:19:10174 if (!g_libc_localtime_r)
175 g_libc_localtime_r = gmtime_r;
[email protected]a8172462012-05-16 23:46:19176 if (!g_libc_localtime64_r)
177 g_libc_localtime64_r = g_libc_localtime_r;
[email protected]724df992010-09-21 18:19:10178}
[email protected]a5d7bb82009-09-08 22:30:58179
[email protected]a8172462012-05-16 23:46:19180// 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")))
185struct tm* localtime_override(const time_t* timep) __asm__ ("localtime");
186
187__attribute__ ((__visibility__("default")))
188struct tm* localtime_override(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06189 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]724df992010-09-21 18:19:10196 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
197 InitLibcLocaltimeFunctions));
[email protected]fd42e0b2014-02-17 16:41:21198 struct tm* res = g_libc_localtime(timep);
199#if defined(MEMORY_SANITIZER)
200 if (res) __msan_unpoison(res, sizeof(*res));
[email protected]00f383512014-05-13 16:50:36201 if (res->tm_zone) __msan_unpoison_string(res->tm_zone);
[email protected]fd42e0b2014-02-17 16:41:21202#endif
203 return res;
[email protected]a0f200ea2009-08-06 18:44:06204 }
[email protected]73fa63992009-07-20 20:30:07205}
206
[email protected]a8172462012-05-16 23:46:19207// Use same trick to override localtime64(), localtime_r() and localtime64_r().
208__attribute__ ((__visibility__("default")))
209struct tm* localtime64_override(const time_t* timep) __asm__ ("localtime64");
210
211__attribute__ ((__visibility__("default")))
212struct 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]fd42e0b2014-02-17 16:41:21222 struct tm* res = g_libc_localtime64(timep);
223#if defined(MEMORY_SANITIZER)
224 if (res) __msan_unpoison(res, sizeof(*res));
[email protected]00f383512014-05-13 16:50:36225 if (res->tm_zone) __msan_unpoison_string(res->tm_zone);
[email protected]fd42e0b2014-02-17 16:41:21226#endif
227 return res;
[email protected]a8172462012-05-16 23:46:19228 }
229}
230
231__attribute__ ((__visibility__("default")))
232struct tm* localtime_r_override(const time_t* timep,
233 struct tm* result) __asm__ ("localtime_r");
234
235__attribute__ ((__visibility__("default")))
236struct tm* localtime_r_override(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06237 if (g_am_zygote_or_renderer) {
238 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
239 return result;
240 } else {
[email protected]724df992010-09-21 18:19:10241 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
242 InitLibcLocaltimeFunctions));
[email protected]fd42e0b2014-02-17 16:41:21243 struct tm* res = g_libc_localtime_r(timep, result);
244#if defined(MEMORY_SANITIZER)
245 if (res) __msan_unpoison(res, sizeof(*res));
[email protected]00f383512014-05-13 16:50:36246 if (res->tm_zone) __msan_unpoison_string(res->tm_zone);
[email protected]fd42e0b2014-02-17 16:41:21247#endif
248 return res;
[email protected]a0f200ea2009-08-06 18:44:06249 }
[email protected]73fa63992009-07-20 20:30:07250}
251
[email protected]a8172462012-05-16 23:46:19252__attribute__ ((__visibility__("default")))
253struct tm* localtime64_r_override(const time_t* timep,
254 struct tm* result) __asm__ ("localtime64_r");
255
256__attribute__ ((__visibility__("default")))
257struct 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]fd42e0b2014-02-17 16:41:21264 struct tm* res = g_libc_localtime64_r(timep, result);
265#if defined(MEMORY_SANITIZER)
266 if (res) __msan_unpoison(res, sizeof(*res));
[email protected]00f383512014-05-13 16:50:36267 if (res->tm_zone) __msan_unpoison_string(res->tm_zone);
[email protected]fd42e0b2014-02-17 16:41:21268#endif
269 return res;
[email protected]a8172462012-05-16 23:46:19270 }
271}
272
[email protected]dac6a5a2013-07-25 05:06:48273#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.
277void 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]0f998442014-03-25 01:59:09282 base::NativeLibraryLoadError error;
[email protected]dac6a5a2013-07-25 05:06:48283 base::NativeLibrary library = base::LoadNativeLibrary(plugins[i].path,
284 &error);
[email protected]132bca82013-12-10 09:14:47285 VLOG_IF(1, !library) << "Unable to load plugin "
286 << plugins[i].path.value() << " "
[email protected]0f998442014-03-25 01:59:09287 << error.ToString();
[email protected]132bca82013-12-10 09:14:47288
[email protected]dac6a5a2013-07-25 05:06:48289 (void)library; // Prevent release-mode warning.
290 }
291 }
292}
293#endif
294
[email protected]ad6d2c42009-09-15 20:13:38295// This function triggers the static and lazy construction of objects that need
296// to be created before imposing the sandbox.
[email protected]5174d0002014-03-03 21:04:43297static void ZygotePreSandboxInit() {
[email protected]b8419412010-03-29 20:18:29298 base::RandUint64();
[email protected]4378a822009-07-08 01:15:14299
[email protected]849447a2013-11-04 20:50:14300 base::SysInfo::AmountOfPhysicalMemory();
[email protected]b8419412010-03-29 20:18:29301 base::SysInfo::MaxSharedMemorySize();
[email protected]c15d1e772013-11-20 11:14:51302 base::SysInfo::NumberOfProcessors();
[email protected]80a086c52009-08-04 17:52:04303
[email protected]b8419412010-03-29 20:18:29304 // 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]1831acf2009-10-05 16:38:41309
[email protected]eeb08552011-03-18 11:15:39310#if defined(USE_NSS)
[email protected]f6a67b42011-03-17 23:49:21311 // 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]4b559b4d2011-04-14 17:37:14313 crypto::LoadNSSLibraries();
[email protected]1f5548452011-10-06 06:46:15314#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]eeb08552011-03-18 11:15:39318#else
[email protected]1f5548452011-10-06 06:46:15319 // 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]f6a67b42011-03-17 23:49:21322#endif
[email protected]0106cde2013-02-27 02:16:38323#if defined(ENABLE_PLUGINS)
[email protected]17773042010-07-28 17:35:07324 // Ensure access to the Pepper plugins before the sandbox is turned on.
[email protected]dac6a5a2013-07-25 05:06:48325 PreloadPepperPlugins();
[email protected]0106cde2013-02-27 02:16:38326#endif
[email protected]e4aa290b2013-05-01 18:54:19327#if defined(ENABLE_WEBRTC)
328 InitializeWebRtcModule();
329#endif
[email protected]576748e82013-09-05 00:41:35330 SkFontConfigInterface::SetGlobal(
[email protected]a0c900e2013-09-11 18:16:10331 new FontConfigIPC(GetSandboxFD()))->unref();
[email protected]ad6d2c42009-09-15 20:13:38332}
333
[email protected]655abd522014-06-02 15:23:43334static bool CreateInitProcessReaper(base::Closure* post_fork_parent_callback) {
[email protected]5174d0002014-03-03 21:04:43335 // The current process becomes init(1), this function returns from a
336 // newly created process.
[email protected]655abd522014-06-02 15:23:43337 const bool init_created =
338 sandbox::CreateInitProcessReaper(post_fork_parent_callback);
[email protected]5174d0002014-03-03 21:04:43339 if (!init_created) {
340 LOG(ERROR) << "Error creating an init process to reap zombies";
341 return false;
342 }
343 return true;
344}
[email protected]576748e82013-09-05 00:41:35345
[email protected]5174d0002014-03-03 21:04:43346// Enter the setuid sandbox. This requires the current process to have been
347// created through the setuid sandbox.
[email protected]655abd522014-06-02 15:23:43348static bool EnterSuidSandbox(sandbox::SetuidSandboxClient* setuid_sandbox,
349 base::Closure* post_fork_parent_callback) {
[email protected]5174d0002014-03-03 21:04:43350 DCHECK(setuid_sandbox);
351 DCHECK(setuid_sandbox->IsSuidSandboxChild());
[email protected]576748e82013-09-05 00:41:35352
[email protected]5174d0002014-03-03 21:04:43353 // 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]a73ed202012-07-23 23:36:39365 return false;
[email protected]b75346c2012-05-13 03:48:38366
[email protected]5174d0002014-03-03 21:04:43367 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]655abd522014-06-02 15:23:43377 CHECK(CreateInitProcessReaper(post_fork_parent_callback));
[email protected]5174d0002014-03-03 21:04:43378 }
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]655abd522014-06-02 15:23:43411#if defined(ADDRESS_SANITIZER)
412const 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.
416static 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.
439static 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
445static 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
461void 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]21add092014-04-17 01:07:34467// If |is_suid_sandbox_child|, then make sure that the setuid sandbox is
468// engaged.
469static void EnterLayerOneSandbox(LinuxSandbox* linux_sandbox,
[email protected]655abd522014-06-02 15:23:43470 bool is_suid_sandbox_child,
471 base::Closure* post_fork_parent_callback) {
[email protected]5174d0002014-03-03 21:04:43472 DCHECK(linux_sandbox);
473
474 ZygotePreSandboxInit();
[email protected]576748e82013-09-05 00:41:35475
476 // Check that the pre-sandbox initialization didn't spawn threads.
[email protected]1ffd85d2014-01-29 17:06:11477#if !defined(THREAD_SANITIZER)
[email protected]576748e82013-09-05 00:41:35478 DCHECK(linux_sandbox->IsSingleThreaded());
[email protected]1ffd85d2014-01-29 17:06:11479#endif
[email protected]e14b4d342012-04-16 22:01:50480
[email protected]5174d0002014-03-03 21:04:43481 sandbox::SetuidSandboxClient* setuid_sandbox =
482 linux_sandbox->setuid_sandbox_client();
483
[email protected]21add092014-04-17 01:07:34484 if (is_suid_sandbox_child) {
[email protected]655abd522014-06-02 15:23:43485 CHECK(EnterSuidSandbox(setuid_sandbox, post_fork_parent_callback))
486 << "Failed to enter setuid sandbox";
[email protected]abe3ad92009-06-15 18:15:08487 }
[email protected]abe3ad92009-06-15 18:15:08488}
489
[email protected]a31fe0f2012-05-08 19:28:51490bool ZygoteMain(const MainFunctionParams& params,
[email protected]54ad01a2014-05-11 01:17:47491 ScopedVector<ZygoteForkDelegate> fork_delegates) {
[email protected]a0f200ea2009-08-06 18:44:06492 g_am_zygote_or_renderer = true;
[email protected]59d816b2012-07-13 01:28:07493 sandbox::InitLibcUrandomOverrides();
[email protected]a0f200ea2009-08-06 18:44:06494
[email protected]655abd522014-06-02 15:23:43495 base::Closure *post_fork_parent_callback = NULL;
496
[email protected]3426ce12012-08-02 23:14:21497 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance();
[email protected]655abd522014-06-02 15:23:43498
499#if defined(ADDRESS_SANITIZER)
[email protected]356b52022014-06-03 21:57:38500 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]655abd522014-06-02 15:23:43504 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]3426ce12012-08-02 23:14:21518 // This will pre-initialize the various sandboxes that need it.
[email protected]c6c9f5012013-05-01 00:04:18519 linux_sandbox->PreinitializeSandbox();
[email protected]a9c54a172009-11-07 06:09:38520
[email protected]21add092014-04-17 01:07:34521 const bool must_enable_setuid_sandbox =
522 linux_sandbox->setuid_sandbox_client()->IsSuidSandboxChild();
[email protected]74722e72014-04-28 19:07:56523 if (must_enable_setuid_sandbox) {
[email protected]f9d2a4f2014-05-02 04:38:02524 linux_sandbox->setuid_sandbox_client()->CloseDummyFile();
[email protected]bc89a49a2014-05-01 06:03:40525
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]74722e72014-04-28 19:07:56531 }
[email protected]21add092014-04-17 01:07:34532
[email protected]54ad01a2014-05-11 01:17:47533 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]ce2ae442011-07-18 16:13:41539 }
540
[email protected]5174d0002014-03-03 21:04:43541 // Turn on the first layer of the sandbox if the configuration warrants it.
[email protected]655abd522014-06-02 15:23:43542 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]715b4f262010-07-13 14:17:28561
[email protected]3426ce12012-08-02 23:14:21562 int sandbox_flags = linux_sandbox->GetStatus();
[email protected]21add092014-04-17 01:07:34563 bool setuid_sandbox_engaged = sandbox_flags & kSandboxLinuxSUID;
564 CHECK_EQ(must_enable_setuid_sandbox, setuid_sandbox_engaged);
[email protected]e8c916a2009-11-04 17:52:47565
[email protected]655abd522014-06-02 15:23:43566 Zygote zygote(sandbox_flags, fork_delegates.Pass(), extra_children,
567 extra_fds);
[email protected]c548be22010-03-08 12:55:57568 // This function call can return multiple times, once per fork().
[email protected]cc8f1462009-06-12 17:36:55569 return zygote.ProcessRequests();
570}
[email protected]a31fe0f2012-05-08 19:28:51571
572} // namespace content