blob: 506825c00bf4e83317a98c1e02e8bd44b085c4bb [file] [log] [blame]
[email protected]17773042010-07-28 17:35:071// Copyright (c) 2010 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]a0f200ea2009-08-06 18:44:065#include <dlfcn.h>
[email protected]a9c54a172009-11-07 06:09:386#include <fcntl.h>
[email protected]83a0cbe2009-11-04 04:22:477#include <sys/epoll.h>
[email protected]83a0cbe2009-11-04 04:22:478#include <sys/prctl.h>
[email protected]8ecd3aad2009-11-04 08:32:229#include <sys/signal.h>
10#include <sys/socket.h>
[email protected]a9c54a172009-11-07 06:09:3811#include <sys/stat.h>
[email protected]8ecd3aad2009-11-04 08:32:2212#include <sys/types.h>
[email protected]83a0cbe2009-11-04 04:22:4713#include <sys/wait.h>
[email protected]8ecd3aad2009-11-04 08:32:2214#include <unistd.h>
15
16#if defined(CHROMIUM_SELINUX)
17#include <selinux/selinux.h>
18#include <selinux/context.h>
19#endif
[email protected]cc8f1462009-06-12 17:36:5520
[email protected]789f70c72009-07-24 13:55:4321#include "base/basictypes.h"
[email protected]cc8f1462009-06-12 17:36:5522#include "base/command_line.h"
23#include "base/eintr_wrapper.h"
[email protected]5d91c9e2010-07-28 17:25:2824#include "base/file_path.h"
[email protected]cc8f1462009-06-12 17:36:5525#include "base/global_descriptors_posix.h"
[email protected]8ecd3aad2009-11-04 08:32:2226#include "base/hash_tables.h"
27#include "base/linux_util.h"
[email protected]c9e45da02009-08-05 17:35:0828#include "base/path_service.h"
[email protected]cc8f1462009-06-12 17:36:5529#include "base/pickle.h"
[email protected]4378a822009-07-08 01:15:1430#include "base/rand_util.h"
[email protected]1831acf2009-10-05 16:38:4131#include "base/scoped_ptr.h"
[email protected]80a086c52009-08-04 17:52:0432#include "base/sys_info.h"
[email protected]cc8f1462009-06-12 17:36:5533#include "base/unix_domain_socket_posix.h"
[email protected]8015de32009-11-18 04:13:3334#include "build/build_config.h"
[email protected]cc8f1462009-06-12 17:36:5535
36#include "chrome/browser/zygote_host_linux.h"
37#include "chrome/common/chrome_descriptors.h"
[email protected]4730db92009-07-22 00:40:4838#include "chrome/common/chrome_switches.h"
[email protected]cc8f1462009-06-12 17:36:5539#include "chrome/common/main_function_params.h"
[email protected]17773042010-07-28 17:35:0740#include "chrome/common/pepper_plugin_registry.h"
[email protected]cc8f1462009-06-12 17:36:5541#include "chrome/common/process_watcher.h"
[email protected]73fa63992009-07-20 20:30:0742#include "chrome/common/sandbox_methods_linux.h"
[email protected]cc8f1462009-06-12 17:36:5543
[email protected]c9e45da02009-08-05 17:35:0844#include "media/base/media.h"
45
[email protected]abe3ad92009-06-15 18:15:0846#include "skia/ext/SkFontHost_fontconfig_control.h"
47
[email protected]e8c916a2009-11-04 17:52:4748#include "sandbox/linux/seccomp/sandbox.h"
49
[email protected]1831acf2009-10-05 16:38:4150#include "unicode/timezone.h"
51
[email protected]36ea6c6f2010-03-17 20:08:0152#if defined(ARCH_CPU_X86_FAMILY) && !defined(CHROMIUM_SELINUX)
53// The seccomp sandbox is enabled on all ia32 and x86-64 processor as long as
54// we aren't using SELinux.
55#define SECCOMP_SANDBOX
56#endif
57
[email protected]cc8f1462009-06-12 17:36:5558// http://code.google.com/p/chromium/wiki/LinuxZygote
59
[email protected]8ecd3aad2009-11-04 08:32:2260static const int kBrowserDescriptor = 3;
[email protected]73fa63992009-07-20 20:30:0761static const int kMagicSandboxIPCDescriptor = 5;
[email protected]8ecd3aad2009-11-04 08:32:2262static const int kZygoteIdDescriptor = 7;
63static bool g_suid_sandbox_active = false;
[email protected]36ea6c6f2010-03-17 20:08:0164#if defined(SECCOMP_SANDBOX)
[email protected]4f03cbc2009-11-19 00:50:4865// |g_proc_fd| is used only by the seccomp sandbox.
[email protected]a9c54a172009-11-07 06:09:3866static int g_proc_fd = -1;
[email protected]4f03cbc2009-11-19 00:50:4867#endif
[email protected]73fa63992009-07-20 20:30:0768
[email protected]a89a55dd2010-04-19 14:51:1369#if defined(CHROMIUM_SELINUX)
70static void SELinuxTransitionToTypeOrDie(const char* type) {
71 security_context_t security_context;
72 if (getcon(&security_context))
73 LOG(FATAL) << "Cannot get SELinux context";
74
75 context_t context = context_new(security_context);
76 context_type_set(context, type);
77 const int r = setcon(context_str(context));
78 context_free(context);
79 freecon(security_context);
80
81 if (r) {
82 LOG(FATAL) << "dynamic transition to type '" << type << "' failed. "
83 "(this binary has been built with SELinux support, but maybe "
84 "the policies haven't been loaded into the kernel?)";
85 }
86}
87#endif // CHROMIUM_SELINUX
88
[email protected]cc8f1462009-06-12 17:36:5589// This is the object which implements the zygote. The ZygoteMain function,
[email protected]61883442010-07-12 15:14:3490// which is called from ChromeMain, simply constructs one of these objects and
91// runs it.
[email protected]cc8f1462009-06-12 17:36:5592class Zygote {
93 public:
[email protected]715b4f262010-07-13 14:17:2894 explicit Zygote(int sandbox_flags)
95 : sandbox_flags_(sandbox_flags) {
96 }
97
[email protected]cc8f1462009-06-12 17:36:5598 bool ProcessRequests() {
99 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
100 // browser on it.
[email protected]8ecd3aad2009-11-04 08:32:22101 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
[email protected]abe3ad92009-06-15 18:15:08102 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:55103
104 // We need to accept SIGCHLD, even though our handler is a no-op because
105 // otherwise we cannot wait on children. (According to POSIX 2001.)
106 struct sigaction action;
107 memset(&action, 0, sizeof(action));
108 action.sa_handler = SIGCHLDHandler;
109 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
110
[email protected]8ecd3aad2009-11-04 08:32:22111 if (g_suid_sandbox_active) {
112 // Let the ZygoteHost know we are ready to go.
113 // The receiving code is in chrome/browser/zygote_host_linux.cc.
114 std::vector<int> empty;
115 bool r = base::SendMsg(kBrowserDescriptor, kZygoteMagic,
116 sizeof(kZygoteMagic), empty);
117 CHECK(r) << "Sending zygote magic failed";
118 }
119
[email protected]cc8f1462009-06-12 17:36:55120 for (;;) {
[email protected]c548be22010-03-08 12:55:57121 // This function call can return multiple times, once per fork().
[email protected]8ecd3aad2009-11-04 08:32:22122 if (HandleRequestFromBrowser(kBrowserDescriptor))
[email protected]cc8f1462009-06-12 17:36:55123 return true;
124 }
125 }
126
127 private:
128 // See comment below, where sigaction is called.
129 static void SIGCHLDHandler(int signal) { }
130
131 // ---------------------------------------------------------------------------
132 // Requests from the browser...
133
134 // Read and process a request from the browser. Returns true if we are in a
135 // new process and thus need to unwind back into ChromeMain.
136 bool HandleRequestFromBrowser(int fd) {
137 std::vector<int> fds;
[email protected]abe3ad92009-06-15 18:15:08138 static const unsigned kMaxMessageLength = 1024;
[email protected]cc8f1462009-06-12 17:36:55139 char buf[kMaxMessageLength];
140 const ssize_t len = base::RecvMsg(fd, buf, sizeof(buf), &fds);
141 if (len == -1) {
142 LOG(WARNING) << "Error reading message from browser: " << errno;
143 return false;
144 }
145
146 if (len == 0) {
147 // EOF from the browser. We should die.
148 _exit(0);
149 return false;
150 }
151
152 Pickle pickle(buf, len);
153 void* iter = NULL;
154
155 int kind;
[email protected]57113ea2009-06-18 02:23:16156 if (pickle.ReadInt(&iter, &kind)) {
157 switch (kind) {
158 case ZygoteHost::kCmdFork:
[email protected]c548be22010-03-08 12:55:57159 // This function call can return multiple times, once per fork().
[email protected]57113ea2009-06-18 02:23:16160 return HandleForkRequest(fd, pickle, iter, fds);
161 case ZygoteHost::kCmdReap:
162 if (!fds.empty())
163 break;
[email protected]c548be22010-03-08 12:55:57164 HandleReapRequest(fd, pickle, iter);
165 return false;
[email protected]57113ea2009-06-18 02:23:16166 case ZygoteHost::kCmdDidProcessCrash:
167 if (!fds.empty())
168 break;
[email protected]c548be22010-03-08 12:55:57169 HandleDidProcessCrash(fd, pickle, iter);
170 return false;
[email protected]715b4f262010-07-13 14:17:28171 case ZygoteHost::kCmdGetSandboxStatus:
172 HandleGetSandboxStatus(fd, pickle, iter);
173 return false;
[email protected]57113ea2009-06-18 02:23:16174 default:
175 NOTREACHED();
176 break;
177 }
[email protected]cc8f1462009-06-12 17:36:55178 }
179
[email protected]cc8f1462009-06-12 17:36:55180 LOG(WARNING) << "Error parsing message from browser";
181 for (std::vector<int>::const_iterator
182 i = fds.begin(); i != fds.end(); ++i)
183 close(*i);
184 return false;
185 }
186
[email protected]c548be22010-03-08 12:55:57187 void HandleReapRequest(int fd, const Pickle& pickle, void* iter) {
[email protected]8ecd3aad2009-11-04 08:32:22188 base::ProcessId child;
189 base::ProcessId actual_child;
[email protected]cc8f1462009-06-12 17:36:55190
191 if (!pickle.ReadInt(&iter, &child)) {
192 LOG(WARNING) << "Error parsing reap request from browser";
[email protected]c548be22010-03-08 12:55:57193 return;
[email protected]cc8f1462009-06-12 17:36:55194 }
195
[email protected]8ecd3aad2009-11-04 08:32:22196 if (g_suid_sandbox_active) {
197 actual_child = real_pids_to_sandbox_pids[child];
198 if (!actual_child)
[email protected]c548be22010-03-08 12:55:57199 return;
[email protected]8ecd3aad2009-11-04 08:32:22200 real_pids_to_sandbox_pids.erase(child);
201 } else {
202 actual_child = child;
203 }
204
205 ProcessWatcher::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55206 }
207
[email protected]c548be22010-03-08 12:55:57208 void HandleDidProcessCrash(int fd, const Pickle& pickle, void* iter) {
[email protected]57113ea2009-06-18 02:23:16209 base::ProcessHandle child;
210
211 if (!pickle.ReadInt(&iter, &child)) {
212 LOG(WARNING) << "Error parsing DidProcessCrash request from browser";
[email protected]c548be22010-03-08 12:55:57213 return;
[email protected]57113ea2009-06-18 02:23:16214 }
215
216 bool child_exited;
[email protected]8ecd3aad2009-11-04 08:32:22217 bool did_crash;
218 if (g_suid_sandbox_active)
219 child = real_pids_to_sandbox_pids[child];
220 if (child)
221 did_crash = base::DidProcessCrash(&child_exited, child);
222 else
223 did_crash = child_exited = false;
[email protected]57113ea2009-06-18 02:23:16224
225 Pickle write_pickle;
226 write_pickle.WriteBool(did_crash);
227 write_pickle.WriteBool(child_exited);
[email protected]4b809bc2010-04-19 16:12:05228 if (HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size())) !=
229 write_pickle.size()) {
[email protected]19cb9292010-04-16 23:00:15230 PLOG(ERROR) << "write";
[email protected]4b809bc2010-04-19 16:12:05231 }
[email protected]57113ea2009-06-18 02:23:16232 }
233
[email protected]cc8f1462009-06-12 17:36:55234 // Handle a 'fork' request from the browser: this means that the browser
235 // wishes to start a new renderer.
[email protected]8ecd3aad2009-11-04 08:32:22236 bool HandleForkRequest(int fd, const Pickle& pickle, void* iter,
[email protected]cc8f1462009-06-12 17:36:55237 std::vector<int>& fds) {
238 std::vector<std::string> args;
239 int argc, numfds;
240 base::GlobalDescriptors::Mapping mapping;
[email protected]8ecd3aad2009-11-04 08:32:22241 base::ProcessId child;
242 uint64_t dummy_inode = 0;
243 int dummy_fd = -1;
[email protected]cc8f1462009-06-12 17:36:55244
245 if (!pickle.ReadInt(&iter, &argc))
246 goto error;
247
248 for (int i = 0; i < argc; ++i) {
249 std::string arg;
250 if (!pickle.ReadString(&iter, &arg))
251 goto error;
252 args.push_back(arg);
253 }
254
255 if (!pickle.ReadInt(&iter, &numfds))
256 goto error;
257 if (numfds != static_cast<int>(fds.size()))
258 goto error;
259
260 for (int i = 0; i < numfds; ++i) {
261 base::GlobalDescriptors::Key key;
262 if (!pickle.ReadUInt32(&iter, &key))
263 goto error;
264 mapping.push_back(std::make_pair(key, fds[i]));
265 }
266
[email protected]abe3ad92009-06-15 18:15:08267 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22268 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
269
270 if (g_suid_sandbox_active) {
271 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
272 if (dummy_fd < 0)
273 goto error;
274
275 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd))
276 goto error;
277 }
[email protected]abe3ad92009-06-15 18:15:08278
[email protected]cc8f1462009-06-12 17:36:55279 child = fork();
280
281 if (!child) {
[email protected]36ea6c6f2010-03-17 20:08:01282#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38283 // Try to open /proc/self/maps as the seccomp sandbox needs access to it
284 if (g_proc_fd >= 0) {
285 int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY);
286 if (proc_self_maps >= 0) {
287 SeccompSandboxSetProcSelfMaps(proc_self_maps);
288 }
289 close(g_proc_fd);
290 g_proc_fd = -1;
291 }
[email protected]8015de32009-11-18 04:13:33292#endif
[email protected]a9c54a172009-11-07 06:09:38293
[email protected]8ecd3aad2009-11-04 08:32:22294 close(kBrowserDescriptor); // our socket from the browser
[email protected]cbcf9cc32010-02-17 04:05:59295 if (g_suid_sandbox_active)
296 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]cc8f1462009-06-12 17:36:55297 Singleton<base::GlobalDescriptors>()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39298
[email protected]a89a55dd2010-04-19 14:51:13299#if defined(CHROMIUM_SELINUX)
300 SELinuxTransitionToTypeOrDie("chromium_renderer_t");
301#endif
302
[email protected]0189bbd2009-10-12 22:50:39303 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55304 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39305 CommandLine::Init(0, NULL);
306 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]7f113f32009-09-10 18:02:17307 CommandLine::SetProcTitle();
[email protected]c548be22010-03-08 12:55:57308 // The fork() request is handled further up the call stack.
[email protected]cc8f1462009-06-12 17:36:55309 return true;
[email protected]8ecd3aad2009-11-04 08:32:22310 } else if (child < 0) {
[email protected]466cffd2010-06-09 18:10:56311 LOG(ERROR) << "Zygote could not fork: " << errno;
[email protected]8ecd3aad2009-11-04 08:32:22312 goto error;
[email protected]cc8f1462009-06-12 17:36:55313 }
314
[email protected]8ecd3aad2009-11-04 08:32:22315 {
316 base::ProcessId proc_id;
317 if (g_suid_sandbox_active) {
318 close(dummy_fd);
319 dummy_fd = -1;
320 uint8_t reply_buf[512];
321 Pickle request;
322 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
323 request.WriteUInt64(dummy_inode);
[email protected]83a0cbe2009-11-04 04:22:47324
[email protected]8ecd3aad2009-11-04 08:32:22325 const ssize_t r = base::SendRecvMsg(kMagicSandboxIPCDescriptor,
326 reply_buf, sizeof(reply_buf),
327 NULL, request);
328 if (r == -1)
329 goto error;
330
331 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
332 void* iter2 = NULL;
333 if (!reply.ReadInt(&iter2, &proc_id))
334 goto error;
335 real_pids_to_sandbox_pids[proc_id] = child;
336 } else {
337 proc_id = child;
338 }
339
340 for (std::vector<int>::const_iterator
341 i = fds.begin(); i != fds.end(); ++i)
342 close(*i);
343
[email protected]19cb9292010-04-16 23:00:15344 if (HANDLE_EINTR(write(fd, &proc_id, sizeof(proc_id))) < 0)
345 PLOG(ERROR) << "write";
[email protected]8ecd3aad2009-11-04 08:32:22346 return false;
347 }
[email protected]83a0cbe2009-11-04 04:22:47348
349 error:
[email protected]8ecd3aad2009-11-04 08:32:22350 LOG(ERROR) << "Error parsing fork request from browser";
[email protected]83a0cbe2009-11-04 04:22:47351 for (std::vector<int>::const_iterator
352 i = fds.begin(); i != fds.end(); ++i)
353 close(*i);
[email protected]8ecd3aad2009-11-04 08:32:22354 if (dummy_fd >= 0)
355 close(dummy_fd);
[email protected]cc8f1462009-06-12 17:36:55356 return false;
357 }
[email protected]8ecd3aad2009-11-04 08:32:22358
[email protected]715b4f262010-07-13 14:17:28359 bool HandleGetSandboxStatus(int fd, const Pickle& pickle, void* iter) {
360 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_)) !=
361 sizeof(sandbox_flags_))) {
362 PLOG(ERROR) << "write";
363 }
364
365 return false;
366 }
367
[email protected]8ecd3aad2009-11-04 08:32:22368 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
369 // fork() returns are not the real PIDs, so we need to map the Real PIDS
370 // into the sandbox PID namespace.
371 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
372 ProcessMap real_pids_to_sandbox_pids;
[email protected]715b4f262010-07-13 14:17:28373
374 const int sandbox_flags_;
[email protected]cc8f1462009-06-12 17:36:55375};
376
[email protected]ad6d2c42009-09-15 20:13:38377// With SELinux we can carve out a precise sandbox, so we don't have to play
378// with intercepting libc calls.
379#if !defined(CHROMIUM_SELINUX)
380
[email protected]a0f200ea2009-08-06 18:44:06381static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
382 char* timezone_out,
383 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07384 Pickle request;
385 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
386 request.WriteString(
387 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
388
389 uint8_t reply_buf[512];
390 const ssize_t r = base::SendRecvMsg(
391 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
392 if (r == -1) {
393 memset(output, 0, sizeof(struct tm));
394 return;
395 }
396
397 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
398 void* iter = NULL;
[email protected]9debcda52009-07-22 20:06:51399 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07400 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51401 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07402 result.size() != sizeof(struct tm)) {
403 memset(output, 0, sizeof(struct tm));
404 return;
405 }
406
407 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51408 if (timezone_out_len) {
409 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
410 memcpy(timezone_out, timezone.data(), copy_len);
411 timezone_out[copy_len] = 0;
412 output->tm_zone = timezone_out;
413 } else {
414 output->tm_zone = NULL;
415 }
[email protected]73fa63992009-07-20 20:30:07416}
417
[email protected]a0f200ea2009-08-06 18:44:06418static bool g_am_zygote_or_renderer = false;
419
420// Sandbox interception of libc calls.
421//
422// Because we are running in a sandbox certain libc calls will fail (localtime
423// being the motivating example - it needs to read /etc/localtime). We need to
424// intercept these calls and proxy them to the browser. However, these calls
425// may come from us or from our libraries. In some cases we can't just change
426// our code.
427//
428// It's for these cases that we have the following setup:
429//
430// We define global functions for those functions which we wish to override.
431// Since we will be first in the dynamic resolution order, the dynamic linker
432// will point callers to our versions of these functions. However, we have the
433// same binary for both the browser and the renderers, which means that our
434// overrides will apply in the browser too.
435//
436// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
437// renderer process. It's set in ZygoteMain and inherited by the renderers when
438// they fork. (This means that it'll be incorrect for global constructor
439// functions and before ZygoteMain is called - beware).
440//
441// Our replacement functions can check this global and either proxy
442// the call to the browser over the sandbox IPC
443// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
444// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
445// current module.
446//
447// Other avenues:
448//
449// Our first attempt involved some assembly to patch the GOT of the current
450// module. This worked, but was platform specific and doesn't catch the case
451// where a library makes a call rather than current module.
452//
453// We also considered patching the function in place, but this would again by
454// platform specific and the above technique seems to work well enough.
455
[email protected]a5d7bb82009-09-08 22:30:58456static void WarnOnceAboutBrokenDlsym();
457
[email protected]73fa63992009-07-20 20:30:07458struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06459 if (g_am_zygote_or_renderer) {
460 static struct tm time_struct;
461 static char timezone_string[64];
462 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
463 sizeof(timezone_string));
464 return &time_struct;
465 } else {
466 typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
467 static LocaltimeFunction libc_localtime;
[email protected]a5d7bb82009-09-08 22:30:58468 static bool have_libc_localtime = false;
469 if (!have_libc_localtime) {
[email protected]a0f200ea2009-08-06 18:44:06470 libc_localtime = (LocaltimeFunction) dlsym(RTLD_NEXT, "localtime");
[email protected]a5d7bb82009-09-08 22:30:58471 have_libc_localtime = true;
472 }
473
474 if (!libc_localtime) {
475 // http://code.google.com/p/chromium/issues/detail?id=16800
476 //
477 // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces
478 // it with a version which doesn't work. In this case we'll get a NULL
479 // result. There's not a lot we can do at this point, so we just bodge it!
480 WarnOnceAboutBrokenDlsym();
481
482 return gmtime(timep);
483 }
[email protected]a0f200ea2009-08-06 18:44:06484
485 return libc_localtime(timep);
486 }
[email protected]73fa63992009-07-20 20:30:07487}
488
489struct tm* localtime_r(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06490 if (g_am_zygote_or_renderer) {
491 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
492 return result;
493 } else {
494 typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
495 struct tm* result);
496 static LocaltimeRFunction libc_localtime_r;
[email protected]a5d7bb82009-09-08 22:30:58497 static bool have_libc_localtime_r = false;
498 if (!have_libc_localtime_r) {
[email protected]a0f200ea2009-08-06 18:44:06499 libc_localtime_r = (LocaltimeRFunction) dlsym(RTLD_NEXT, "localtime_r");
[email protected]a5d7bb82009-09-08 22:30:58500 have_libc_localtime_r = true;
501 }
502
503 if (!libc_localtime_r) {
504 // See |localtime|, above.
505 WarnOnceAboutBrokenDlsym();
506
507 return gmtime_r(timep, result);
508 }
[email protected]a0f200ea2009-08-06 18:44:06509
510 return libc_localtime_r(timep, result);
511 }
[email protected]73fa63992009-07-20 20:30:07512}
513
[email protected]a5d7bb82009-09-08 22:30:58514// See the comments at the callsite in |localtime| about this function.
515static void WarnOnceAboutBrokenDlsym() {
516 static bool have_shown_warning = false;
517 if (!have_shown_warning) {
518 LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been "
519 "reported to be caused by Nvidia's libGL. You should expect "
520 "time related functions to misbehave. "
521 "http://code.google.com/p/chromium/issues/detail?id=16800";
522 have_shown_warning = true;
523 }
524}
[email protected]ad6d2c42009-09-15 20:13:38525#endif // !CHROMIUM_SELINUX
[email protected]a5d7bb82009-09-08 22:30:58526
[email protected]ad6d2c42009-09-15 20:13:38527// This function triggers the static and lazy construction of objects that need
528// to be created before imposing the sandbox.
529static void PreSandboxInit() {
[email protected]b8419412010-03-29 20:18:29530 base::RandUint64();
[email protected]4378a822009-07-08 01:15:14531
[email protected]b8419412010-03-29 20:18:29532 base::SysInfo::MaxSharedMemorySize();
[email protected]80a086c52009-08-04 17:52:04533
[email protected]b8419412010-03-29 20:18:29534 // To make wcstombs/mbstowcs work in a renderer, setlocale() has to be
535 // called before the sandbox is triggered. It's possible to avoid calling
536 // setlocale() by pulling out the conversion between FilePath and
537 // WebCore String out of the renderer and using string16 in place of
538 // FilePath for IPC.
539 const char* locale = setlocale(LC_ALL, "");
540 LOG_IF(WARNING, locale == NULL) << "setlocale failed.";
[email protected]e6acd672009-07-24 21:51:33541
[email protected]b8419412010-03-29 20:18:29542 // ICU DateFormat class (used in base/time_format.cc) needs to get the
543 // Olson timezone ID by accessing the zoneinfo files on disk. After
544 // TimeZone::createDefault is called once here, the timezone ID is
545 // cached and there's no more need to access the file system.
546 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
[email protected]1831acf2009-10-05 16:38:41547
[email protected]b8419412010-03-29 20:18:29548 FilePath module_path;
549 if (PathService::Get(base::DIR_MODULE, &module_path))
550 media::InitializeMediaLibrary(module_path);
[email protected]17773042010-07-28 17:35:07551
552 // Ensure access to the Pepper plugins before the sandbox is turned on.
553 PepperPluginRegistry::PreloadModules();
[email protected]ad6d2c42009-09-15 20:13:38554}
555
556#if !defined(CHROMIUM_SELINUX)
557static bool EnterSandbox() {
[email protected]b8419412010-03-29 20:18:29558 // The SUID sandbox sets this environment variable to a file descriptor
559 // over which we can signal that we have completed our startup and can be
560 // chrooted.
[email protected]ad6d2c42009-09-15 20:13:38561 const char* const sandbox_fd_string = getenv("SBX_D");
[email protected]ad6d2c42009-09-15 20:13:38562
[email protected]39c4e1a82010-03-30 19:47:41563 if (switches::SeccompSandboxEnabled()) {
[email protected]b8419412010-03-29 20:18:29564 PreSandboxInit();
565 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
566 } else if (sandbox_fd_string) { // Use the SUID sandbox.
[email protected]8ecd3aad2009-11-04 08:32:22567 g_suid_sandbox_active = true;
568
[email protected]ad6d2c42009-09-15 20:13:38569 char* endptr;
570 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
571 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
572 return false;
573 const int fd = fd_long;
574
575 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08576
[email protected]eaebefaf2010-02-23 22:58:18577 static const char kMsgChrootMe = 'C';
578 static const char kMsgChrootSuccessful = 'O';
[email protected]abe3ad92009-06-15 18:15:08579
[email protected]eaebefaf2010-02-23 22:58:18580 if (HANDLE_EINTR(write(fd, &kMsgChrootMe, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32581 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08582 return false;
[email protected]0e1611122009-07-10 18:17:32583 }
[email protected]abe3ad92009-06-15 18:15:08584
[email protected]87ed6952009-07-16 02:52:15585 // We need to reap the chroot helper process in any event:
586 wait(NULL);
587
[email protected]abe3ad92009-06-15 18:15:08588 char reply;
[email protected]87f8ce62009-07-10 19:14:31589 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32590 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08591 return false;
[email protected]0e1611122009-07-10 18:17:32592 }
[email protected]87f8ce62009-07-10 19:14:31593
[email protected]eaebefaf2010-02-23 22:58:18594 if (reply != kMsgChrootSuccessful) {
[email protected]0e1611122009-07-10 18:17:32595 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08596 return false;
[email protected]0e1611122009-07-10 18:17:32597 }
[email protected]abe3ad92009-06-15 18:15:08598
[email protected]abe3ad92009-06-15 18:15:08599 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
600
[email protected]415493be2009-07-10 17:50:24601 // Previously, we required that the binary be non-readable. This causes the
602 // kernel to mark the process as non-dumpable at startup. The thinking was
603 // that, although we were putting the renderers into a PID namespace (with
604 // the SUID sandbox), they would nonetheless be in the /same/ PID
605 // namespace. So they could ptrace each other unless they were non-dumpable.
606 //
607 // If the binary was readable, then there would be a window between process
608 // startup and the point where we set the non-dumpable flag in which a
609 // compromised renderer could ptrace attach.
610 //
611 // However, now that we have a zygote model, only the (trusted) zygote
612 // exists at this point and we can set the non-dumpable flag which is
613 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48614 //
615 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
616 // issues, one can specify --allow-sandbox-debugging to let the process be
617 // dumpable.
618 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
619 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
620 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
621 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
622 LOG(ERROR) << "Failed to set non-dumpable flag";
623 return false;
624 }
[email protected]0e1611122009-07-10 18:17:32625 }
[email protected]abe3ad92009-06-15 18:15:08626 } else {
627 SkiaFontConfigUseDirectImplementation();
628 }
629
630 return true;
631}
[email protected]ad6d2c42009-09-15 20:13:38632#else // CHROMIUM_SELINUX
633
634static bool EnterSandbox() {
635 PreSandboxInit();
636 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
[email protected]ad6d2c42009-09-15 20:13:38637 return true;
638}
639
640#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08641
[email protected]cc8f1462009-06-12 17:36:55642bool ZygoteMain(const MainFunctionParams& params) {
[email protected]ad6d2c42009-09-15 20:13:38643#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06644 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38645#endif
[email protected]a0f200ea2009-08-06 18:44:06646
[email protected]36ea6c6f2010-03-17 20:08:01647#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38648 // The seccomp sandbox needs access to files in /proc, which might be denied
649 // after one of the other sandboxes have been started. So, obtain a suitable
650 // file handle in advance.
[email protected]39c4e1a82010-03-30 19:47:41651 if (switches::SeccompSandboxEnabled()) {
[email protected]a9c54a172009-11-07 06:09:38652 g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY);
653 if (g_proc_fd < 0) {
654 LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp "
655 "sandboxing.";
656 }
657 }
[email protected]36ea6c6f2010-03-17 20:08:01658#endif // SECCOMP_SANDBOX
[email protected]a9c54a172009-11-07 06:09:38659
660 // Turn on the SELinux or SUID sandbox
661 if (!EnterSandbox()) {
662 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
663 << errno << ")";
664 return false;
665 }
666
[email protected]715b4f262010-07-13 14:17:28667 int sandbox_flags = 0;
668 if (getenv("SBX_D"))
669 sandbox_flags |= ZygoteHost::kSandboxSUID;
670 if (getenv("SBX_PID_NS"))
671 sandbox_flags |= ZygoteHost::kSandboxPIDNS;
672 if (getenv("SBX_NET_NS"))
673 sandbox_flags |= ZygoteHost::kSandboxNetNS;
674
[email protected]36ea6c6f2010-03-17 20:08:01675#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38676 // The seccomp sandbox will be turned on when the renderers start. But we can
677 // already check if sufficient support is available so that we only need to
678 // print one error message for the entire browser session.
[email protected]39c4e1a82010-03-30 19:47:41679 if (g_proc_fd >= 0 && switches::SeccompSandboxEnabled()) {
[email protected]a9c54a172009-11-07 06:09:38680 if (!SupportsSeccompSandbox(g_proc_fd)) {
[email protected]e8c916a2009-11-04 17:52:47681 // There are a good number of users who cannot use the seccomp sandbox
682 // (e.g. because their distribution does not enable seccomp mode by
683 // default). While we would prefer to deny execution in this case, it
684 // seems more realistic to continue in degraded mode.
[email protected]1b5d28f2010-02-18 15:53:36685 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
686 "Seccomp sandbox. Running renderers with Seccomp "
687 "sandboxing disabled.";
[email protected]e8c916a2009-11-04 17:52:47688 } else {
689 LOG(INFO) << "Enabling experimental Seccomp sandbox.";
[email protected]715b4f262010-07-13 14:17:28690 sandbox_flags |= ZygoteHost::kSandboxSeccomp;
[email protected]e8c916a2009-11-04 17:52:47691 }
692 }
[email protected]36ea6c6f2010-03-17 20:08:01693#endif // SECCOMP_SANDBOX
[email protected]e8c916a2009-11-04 17:52:47694
[email protected]715b4f262010-07-13 14:17:28695 Zygote zygote(sandbox_flags);
[email protected]c548be22010-03-08 12:55:57696 // This function call can return multiple times, once per fork().
[email protected]cc8f1462009-06-12 17:36:55697 return zygote.ProcessRequests();
698}