blob: 5c0061b4c39143c12d1ab898ed9b61e71a4eef01 [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]4d36536b2010-08-20 06:23:2730#include "base/process_util.h"
[email protected]4378a822009-07-08 01:15:1431#include "base/rand_util.h"
[email protected]1831acf2009-10-05 16:38:4132#include "base/scoped_ptr.h"
[email protected]80a086c52009-08-04 17:52:0433#include "base/sys_info.h"
[email protected]cc8f1462009-06-12 17:36:5534#include "base/unix_domain_socket_posix.h"
[email protected]8015de32009-11-18 04:13:3335#include "build/build_config.h"
[email protected]cc8f1462009-06-12 17:36:5536
37#include "chrome/browser/zygote_host_linux.h"
38#include "chrome/common/chrome_descriptors.h"
[email protected]4730db92009-07-22 00:40:4839#include "chrome/common/chrome_switches.h"
[email protected]cc8f1462009-06-12 17:36:5540#include "chrome/common/main_function_params.h"
[email protected]17773042010-07-28 17:35:0741#include "chrome/common/pepper_plugin_registry.h"
[email protected]cc8f1462009-06-12 17:36:5542#include "chrome/common/process_watcher.h"
[email protected]73fa63992009-07-20 20:30:0743#include "chrome/common/sandbox_methods_linux.h"
[email protected]cc8f1462009-06-12 17:36:5544
[email protected]c9e45da02009-08-05 17:35:0845#include "media/base/media.h"
46
[email protected]abe3ad92009-06-15 18:15:0847#include "skia/ext/SkFontHost_fontconfig_control.h"
48
[email protected]808ea572010-09-01 16:22:0149#include "seccompsandbox/sandbox.h"
[email protected]e8c916a2009-11-04 17:52:4750
[email protected]1831acf2009-10-05 16:38:4151#include "unicode/timezone.h"
52
[email protected]36ea6c6f2010-03-17 20:08:0153#if defined(ARCH_CPU_X86_FAMILY) && !defined(CHROMIUM_SELINUX)
54// The seccomp sandbox is enabled on all ia32 and x86-64 processor as long as
55// we aren't using SELinux.
56#define SECCOMP_SANDBOX
57#endif
58
[email protected]cc8f1462009-06-12 17:36:5559// http://code.google.com/p/chromium/wiki/LinuxZygote
60
[email protected]8ecd3aad2009-11-04 08:32:2261static const int kBrowserDescriptor = 3;
[email protected]73fa63992009-07-20 20:30:0762static const int kMagicSandboxIPCDescriptor = 5;
[email protected]8ecd3aad2009-11-04 08:32:2263static const int kZygoteIdDescriptor = 7;
64static bool g_suid_sandbox_active = false;
[email protected]36ea6c6f2010-03-17 20:08:0165#if defined(SECCOMP_SANDBOX)
[email protected]4f03cbc2009-11-19 00:50:4866// |g_proc_fd| is used only by the seccomp sandbox.
[email protected]a9c54a172009-11-07 06:09:3867static int g_proc_fd = -1;
[email protected]4f03cbc2009-11-19 00:50:4868#endif
[email protected]73fa63992009-07-20 20:30:0769
[email protected]a89a55dd2010-04-19 14:51:1370#if defined(CHROMIUM_SELINUX)
71static void SELinuxTransitionToTypeOrDie(const char* type) {
72 security_context_t security_context;
73 if (getcon(&security_context))
74 LOG(FATAL) << "Cannot get SELinux context";
75
76 context_t context = context_new(security_context);
77 context_type_set(context, type);
78 const int r = setcon(context_str(context));
79 context_free(context);
80 freecon(security_context);
81
82 if (r) {
83 LOG(FATAL) << "dynamic transition to type '" << type << "' failed. "
84 "(this binary has been built with SELinux support, but maybe "
85 "the policies haven't been loaded into the kernel?)";
86 }
87}
88#endif // CHROMIUM_SELINUX
89
[email protected]cc8f1462009-06-12 17:36:5590// This is the object which implements the zygote. The ZygoteMain function,
[email protected]61883442010-07-12 15:14:3491// which is called from ChromeMain, simply constructs one of these objects and
92// runs it.
[email protected]cc8f1462009-06-12 17:36:5593class Zygote {
94 public:
[email protected]715b4f262010-07-13 14:17:2895 explicit Zygote(int sandbox_flags)
96 : sandbox_flags_(sandbox_flags) {
97 }
98
[email protected]cc8f1462009-06-12 17:36:5599 bool ProcessRequests() {
100 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
101 // browser on it.
[email protected]8ecd3aad2009-11-04 08:32:22102 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
[email protected]abe3ad92009-06-15 18:15:08103 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:55104
105 // We need to accept SIGCHLD, even though our handler is a no-op because
106 // otherwise we cannot wait on children. (According to POSIX 2001.)
107 struct sigaction action;
108 memset(&action, 0, sizeof(action));
109 action.sa_handler = SIGCHLDHandler;
110 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
111
[email protected]8ecd3aad2009-11-04 08:32:22112 if (g_suid_sandbox_active) {
113 // Let the ZygoteHost know we are ready to go.
114 // The receiving code is in chrome/browser/zygote_host_linux.cc.
115 std::vector<int> empty;
116 bool r = base::SendMsg(kBrowserDescriptor, kZygoteMagic,
117 sizeof(kZygoteMagic), empty);
118 CHECK(r) << "Sending zygote magic failed";
119 }
120
[email protected]cc8f1462009-06-12 17:36:55121 for (;;) {
[email protected]c548be22010-03-08 12:55:57122 // This function call can return multiple times, once per fork().
[email protected]8ecd3aad2009-11-04 08:32:22123 if (HandleRequestFromBrowser(kBrowserDescriptor))
[email protected]cc8f1462009-06-12 17:36:55124 return true;
125 }
126 }
127
128 private:
129 // See comment below, where sigaction is called.
130 static void SIGCHLDHandler(int signal) { }
131
132 // ---------------------------------------------------------------------------
133 // Requests from the browser...
134
135 // Read and process a request from the browser. Returns true if we are in a
136 // new process and thus need to unwind back into ChromeMain.
137 bool HandleRequestFromBrowser(int fd) {
138 std::vector<int> fds;
[email protected]abe3ad92009-06-15 18:15:08139 static const unsigned kMaxMessageLength = 1024;
[email protected]cc8f1462009-06-12 17:36:55140 char buf[kMaxMessageLength];
141 const ssize_t len = base::RecvMsg(fd, buf, sizeof(buf), &fds);
142 if (len == -1) {
143 LOG(WARNING) << "Error reading message from browser: " << errno;
144 return false;
145 }
146
147 if (len == 0) {
148 // EOF from the browser. We should die.
149 _exit(0);
150 return false;
151 }
152
153 Pickle pickle(buf, len);
154 void* iter = NULL;
155
156 int kind;
[email protected]57113ea2009-06-18 02:23:16157 if (pickle.ReadInt(&iter, &kind)) {
158 switch (kind) {
159 case ZygoteHost::kCmdFork:
[email protected]c548be22010-03-08 12:55:57160 // This function call can return multiple times, once per fork().
[email protected]57113ea2009-06-18 02:23:16161 return HandleForkRequest(fd, pickle, iter, fds);
162 case ZygoteHost::kCmdReap:
163 if (!fds.empty())
164 break;
[email protected]c548be22010-03-08 12:55:57165 HandleReapRequest(fd, pickle, iter);
166 return false;
[email protected]57113ea2009-06-18 02:23:16167 case ZygoteHost::kCmdDidProcessCrash:
168 if (!fds.empty())
169 break;
[email protected]c548be22010-03-08 12:55:57170 HandleDidProcessCrash(fd, pickle, iter);
171 return false;
[email protected]715b4f262010-07-13 14:17:28172 case ZygoteHost::kCmdGetSandboxStatus:
173 HandleGetSandboxStatus(fd, pickle, iter);
174 return false;
[email protected]57113ea2009-06-18 02:23:16175 default:
176 NOTREACHED();
177 break;
178 }
[email protected]cc8f1462009-06-12 17:36:55179 }
180
[email protected]cc8f1462009-06-12 17:36:55181 LOG(WARNING) << "Error parsing message from browser";
182 for (std::vector<int>::const_iterator
183 i = fds.begin(); i != fds.end(); ++i)
184 close(*i);
185 return false;
186 }
187
[email protected]c548be22010-03-08 12:55:57188 void HandleReapRequest(int fd, const Pickle& pickle, void* iter) {
[email protected]8ecd3aad2009-11-04 08:32:22189 base::ProcessId child;
190 base::ProcessId actual_child;
[email protected]cc8f1462009-06-12 17:36:55191
192 if (!pickle.ReadInt(&iter, &child)) {
193 LOG(WARNING) << "Error parsing reap request from browser";
[email protected]c548be22010-03-08 12:55:57194 return;
[email protected]cc8f1462009-06-12 17:36:55195 }
196
[email protected]8ecd3aad2009-11-04 08:32:22197 if (g_suid_sandbox_active) {
198 actual_child = real_pids_to_sandbox_pids[child];
199 if (!actual_child)
[email protected]c548be22010-03-08 12:55:57200 return;
[email protected]8ecd3aad2009-11-04 08:32:22201 real_pids_to_sandbox_pids.erase(child);
202 } else {
203 actual_child = child;
204 }
205
206 ProcessWatcher::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55207 }
208
[email protected]c548be22010-03-08 12:55:57209 void HandleDidProcessCrash(int fd, const Pickle& pickle, void* iter) {
[email protected]57113ea2009-06-18 02:23:16210 base::ProcessHandle child;
211
212 if (!pickle.ReadInt(&iter, &child)) {
213 LOG(WARNING) << "Error parsing DidProcessCrash request from browser";
[email protected]c548be22010-03-08 12:55:57214 return;
[email protected]57113ea2009-06-18 02:23:16215 }
216
217 bool child_exited;
[email protected]8ecd3aad2009-11-04 08:32:22218 bool did_crash;
219 if (g_suid_sandbox_active)
220 child = real_pids_to_sandbox_pids[child];
221 if (child)
222 did_crash = base::DidProcessCrash(&child_exited, child);
223 else
224 did_crash = child_exited = false;
[email protected]57113ea2009-06-18 02:23:16225
226 Pickle write_pickle;
227 write_pickle.WriteBool(did_crash);
228 write_pickle.WriteBool(child_exited);
[email protected]4b809bc2010-04-19 16:12:05229 if (HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size())) !=
230 write_pickle.size()) {
[email protected]19cb9292010-04-16 23:00:15231 PLOG(ERROR) << "write";
[email protected]4b809bc2010-04-19 16:12:05232 }
[email protected]57113ea2009-06-18 02:23:16233 }
234
[email protected]cc8f1462009-06-12 17:36:55235 // Handle a 'fork' request from the browser: this means that the browser
236 // wishes to start a new renderer.
[email protected]8ecd3aad2009-11-04 08:32:22237 bool HandleForkRequest(int fd, const Pickle& pickle, void* iter,
[email protected]cc8f1462009-06-12 17:36:55238 std::vector<int>& fds) {
239 std::vector<std::string> args;
240 int argc, numfds;
241 base::GlobalDescriptors::Mapping mapping;
[email protected]8ecd3aad2009-11-04 08:32:22242 base::ProcessId child;
243 uint64_t dummy_inode = 0;
244 int dummy_fd = -1;
[email protected]cc8f1462009-06-12 17:36:55245
246 if (!pickle.ReadInt(&iter, &argc))
247 goto error;
248
249 for (int i = 0; i < argc; ++i) {
250 std::string arg;
251 if (!pickle.ReadString(&iter, &arg))
252 goto error;
253 args.push_back(arg);
254 }
255
256 if (!pickle.ReadInt(&iter, &numfds))
257 goto error;
258 if (numfds != static_cast<int>(fds.size()))
259 goto error;
260
261 for (int i = 0; i < numfds; ++i) {
262 base::GlobalDescriptors::Key key;
263 if (!pickle.ReadUInt32(&iter, &key))
264 goto error;
265 mapping.push_back(std::make_pair(key, fds[i]));
266 }
267
[email protected]abe3ad92009-06-15 18:15:08268 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22269 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
270
271 if (g_suid_sandbox_active) {
272 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
273 if (dummy_fd < 0)
274 goto error;
275
276 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd))
277 goto error;
278 }
[email protected]abe3ad92009-06-15 18:15:08279
[email protected]cc8f1462009-06-12 17:36:55280 child = fork();
281
282 if (!child) {
[email protected]36ea6c6f2010-03-17 20:08:01283#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38284 // Try to open /proc/self/maps as the seccomp sandbox needs access to it
285 if (g_proc_fd >= 0) {
286 int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY);
287 if (proc_self_maps >= 0) {
288 SeccompSandboxSetProcSelfMaps(proc_self_maps);
289 }
290 close(g_proc_fd);
291 g_proc_fd = -1;
292 }
[email protected]8015de32009-11-18 04:13:33293#endif
[email protected]a9c54a172009-11-07 06:09:38294
[email protected]8ecd3aad2009-11-04 08:32:22295 close(kBrowserDescriptor); // our socket from the browser
[email protected]cbcf9cc32010-02-17 04:05:59296 if (g_suid_sandbox_active)
297 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]cc8f1462009-06-12 17:36:55298 Singleton<base::GlobalDescriptors>()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39299
[email protected]a89a55dd2010-04-19 14:51:13300#if defined(CHROMIUM_SELINUX)
301 SELinuxTransitionToTypeOrDie("chromium_renderer_t");
302#endif
303
[email protected]0189bbd2009-10-12 22:50:39304 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55305 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39306 CommandLine::Init(0, NULL);
307 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]7f113f32009-09-10 18:02:17308 CommandLine::SetProcTitle();
[email protected]c548be22010-03-08 12:55:57309 // The fork() request is handled further up the call stack.
[email protected]cc8f1462009-06-12 17:36:55310 return true;
[email protected]8ecd3aad2009-11-04 08:32:22311 } else if (child < 0) {
[email protected]466cffd2010-06-09 18:10:56312 LOG(ERROR) << "Zygote could not fork: " << errno;
[email protected]8ecd3aad2009-11-04 08:32:22313 goto error;
[email protected]cc8f1462009-06-12 17:36:55314 }
315
[email protected]8ecd3aad2009-11-04 08:32:22316 {
317 base::ProcessId proc_id;
318 if (g_suid_sandbox_active) {
319 close(dummy_fd);
320 dummy_fd = -1;
321 uint8_t reply_buf[512];
322 Pickle request;
323 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
324 request.WriteUInt64(dummy_inode);
[email protected]83a0cbe2009-11-04 04:22:47325
[email protected]8ecd3aad2009-11-04 08:32:22326 const ssize_t r = base::SendRecvMsg(kMagicSandboxIPCDescriptor,
327 reply_buf, sizeof(reply_buf),
328 NULL, request);
329 if (r == -1)
330 goto error;
331
332 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
333 void* iter2 = NULL;
334 if (!reply.ReadInt(&iter2, &proc_id))
335 goto error;
336 real_pids_to_sandbox_pids[proc_id] = child;
337 } else {
338 proc_id = child;
339 }
340
341 for (std::vector<int>::const_iterator
342 i = fds.begin(); i != fds.end(); ++i)
343 close(*i);
344
[email protected]19cb9292010-04-16 23:00:15345 if (HANDLE_EINTR(write(fd, &proc_id, sizeof(proc_id))) < 0)
346 PLOG(ERROR) << "write";
[email protected]8ecd3aad2009-11-04 08:32:22347 return false;
348 }
[email protected]83a0cbe2009-11-04 04:22:47349
350 error:
[email protected]8ecd3aad2009-11-04 08:32:22351 LOG(ERROR) << "Error parsing fork request from browser";
[email protected]83a0cbe2009-11-04 04:22:47352 for (std::vector<int>::const_iterator
353 i = fds.begin(); i != fds.end(); ++i)
354 close(*i);
[email protected]8ecd3aad2009-11-04 08:32:22355 if (dummy_fd >= 0)
356 close(dummy_fd);
[email protected]cc8f1462009-06-12 17:36:55357 return false;
358 }
[email protected]8ecd3aad2009-11-04 08:32:22359
[email protected]715b4f262010-07-13 14:17:28360 bool HandleGetSandboxStatus(int fd, const Pickle& pickle, void* iter) {
361 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_)) !=
362 sizeof(sandbox_flags_))) {
363 PLOG(ERROR) << "write";
364 }
365
366 return false;
367 }
368
[email protected]8ecd3aad2009-11-04 08:32:22369 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
370 // fork() returns are not the real PIDs, so we need to map the Real PIDS
371 // into the sandbox PID namespace.
372 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
373 ProcessMap real_pids_to_sandbox_pids;
[email protected]715b4f262010-07-13 14:17:28374
375 const int sandbox_flags_;
[email protected]cc8f1462009-06-12 17:36:55376};
377
[email protected]ad6d2c42009-09-15 20:13:38378// With SELinux we can carve out a precise sandbox, so we don't have to play
379// with intercepting libc calls.
380#if !defined(CHROMIUM_SELINUX)
381
[email protected]a0f200ea2009-08-06 18:44:06382static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
383 char* timezone_out,
384 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07385 Pickle request;
386 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
387 request.WriteString(
388 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
389
390 uint8_t reply_buf[512];
391 const ssize_t r = base::SendRecvMsg(
392 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
393 if (r == -1) {
394 memset(output, 0, sizeof(struct tm));
395 return;
396 }
397
398 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
399 void* iter = NULL;
[email protected]9debcda52009-07-22 20:06:51400 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07401 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51402 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07403 result.size() != sizeof(struct tm)) {
404 memset(output, 0, sizeof(struct tm));
405 return;
406 }
407
408 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51409 if (timezone_out_len) {
410 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
411 memcpy(timezone_out, timezone.data(), copy_len);
412 timezone_out[copy_len] = 0;
413 output->tm_zone = timezone_out;
414 } else {
415 output->tm_zone = NULL;
416 }
[email protected]73fa63992009-07-20 20:30:07417}
418
[email protected]a0f200ea2009-08-06 18:44:06419static bool g_am_zygote_or_renderer = false;
420
421// Sandbox interception of libc calls.
422//
423// Because we are running in a sandbox certain libc calls will fail (localtime
424// being the motivating example - it needs to read /etc/localtime). We need to
425// intercept these calls and proxy them to the browser. However, these calls
426// may come from us or from our libraries. In some cases we can't just change
427// our code.
428//
429// It's for these cases that we have the following setup:
430//
431// We define global functions for those functions which we wish to override.
432// Since we will be first in the dynamic resolution order, the dynamic linker
433// will point callers to our versions of these functions. However, we have the
434// same binary for both the browser and the renderers, which means that our
435// overrides will apply in the browser too.
436//
437// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
438// renderer process. It's set in ZygoteMain and inherited by the renderers when
439// they fork. (This means that it'll be incorrect for global constructor
440// functions and before ZygoteMain is called - beware).
441//
442// Our replacement functions can check this global and either proxy
443// the call to the browser over the sandbox IPC
444// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
445// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
446// current module.
447//
448// Other avenues:
449//
450// Our first attempt involved some assembly to patch the GOT of the current
451// module. This worked, but was platform specific and doesn't catch the case
452// where a library makes a call rather than current module.
453//
454// We also considered patching the function in place, but this would again by
455// platform specific and the above technique seems to work well enough.
456
[email protected]a5d7bb82009-09-08 22:30:58457static void WarnOnceAboutBrokenDlsym();
458
[email protected]73fa63992009-07-20 20:30:07459struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06460 if (g_am_zygote_or_renderer) {
461 static struct tm time_struct;
462 static char timezone_string[64];
463 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
464 sizeof(timezone_string));
465 return &time_struct;
466 } else {
467 typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
[email protected]16b71232010-09-15 17:34:25468 // This static declaration is desugared by the compiler into a locked
469 // initialisation which will only call dlsym once, across all threads.
470 // See: http://gcc.gnu.org/ml/gcc-patches/2004-08/msg01598.html
471 static LocaltimeFunction libc_localtime =
472 (LocaltimeFunction) dlsym(RTLD_NEXT, "localtime");
[email protected]a5d7bb82009-09-08 22:30:58473
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]0dc32322010-09-16 09:46:59563 if (sandbox_fd_string) {
564 // Use the SUID sandbox. This still allows the seccomp sandbox to
565 // be enabled by the process later.
[email protected]8ecd3aad2009-11-04 08:32:22566 g_suid_sandbox_active = true;
567
[email protected]ad6d2c42009-09-15 20:13:38568 char* endptr;
569 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
570 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
571 return false;
572 const int fd = fd_long;
573
574 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08575
[email protected]eaebefaf2010-02-23 22:58:18576 static const char kMsgChrootMe = 'C';
577 static const char kMsgChrootSuccessful = 'O';
[email protected]abe3ad92009-06-15 18:15:08578
[email protected]eaebefaf2010-02-23 22:58:18579 if (HANDLE_EINTR(write(fd, &kMsgChrootMe, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32580 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08581 return false;
[email protected]0e1611122009-07-10 18:17:32582 }
[email protected]abe3ad92009-06-15 18:15:08583
[email protected]87ed6952009-07-16 02:52:15584 // We need to reap the chroot helper process in any event:
585 wait(NULL);
586
[email protected]abe3ad92009-06-15 18:15:08587 char reply;
[email protected]87f8ce62009-07-10 19:14:31588 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32589 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08590 return false;
[email protected]0e1611122009-07-10 18:17:32591 }
[email protected]87f8ce62009-07-10 19:14:31592
[email protected]eaebefaf2010-02-23 22:58:18593 if (reply != kMsgChrootSuccessful) {
[email protected]0e1611122009-07-10 18:17:32594 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08595 return false;
[email protected]0e1611122009-07-10 18:17:32596 }
[email protected]abe3ad92009-06-15 18:15:08597
[email protected]abe3ad92009-06-15 18:15:08598 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
599
[email protected]415493be2009-07-10 17:50:24600 // Previously, we required that the binary be non-readable. This causes the
601 // kernel to mark the process as non-dumpable at startup. The thinking was
602 // that, although we were putting the renderers into a PID namespace (with
603 // the SUID sandbox), they would nonetheless be in the /same/ PID
604 // namespace. So they could ptrace each other unless they were non-dumpable.
605 //
606 // If the binary was readable, then there would be a window between process
607 // startup and the point where we set the non-dumpable flag in which a
608 // compromised renderer could ptrace attach.
609 //
610 // However, now that we have a zygote model, only the (trusted) zygote
611 // exists at this point and we can set the non-dumpable flag which is
612 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48613 //
614 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
615 // issues, one can specify --allow-sandbox-debugging to let the process be
616 // dumpable.
617 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
618 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
619 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
620 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
621 LOG(ERROR) << "Failed to set non-dumpable flag";
622 return false;
623 }
[email protected]0e1611122009-07-10 18:17:32624 }
[email protected]0dc32322010-09-16 09:46:59625 } else if (switches::SeccompSandboxEnabled()) {
626 PreSandboxInit();
627 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
[email protected]abe3ad92009-06-15 18:15:08628 } else {
629 SkiaFontConfigUseDirectImplementation();
630 }
631
632 return true;
633}
[email protected]ad6d2c42009-09-15 20:13:38634#else // CHROMIUM_SELINUX
635
636static bool EnterSandbox() {
637 PreSandboxInit();
638 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
[email protected]ad6d2c42009-09-15 20:13:38639 return true;
640}
641
642#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08643
[email protected]cc8f1462009-06-12 17:36:55644bool ZygoteMain(const MainFunctionParams& params) {
[email protected]ad6d2c42009-09-15 20:13:38645#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06646 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38647#endif
[email protected]a0f200ea2009-08-06 18:44:06648
[email protected]36ea6c6f2010-03-17 20:08:01649#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38650 // The seccomp sandbox needs access to files in /proc, which might be denied
651 // after one of the other sandboxes have been started. So, obtain a suitable
652 // file handle in advance.
[email protected]39c4e1a82010-03-30 19:47:41653 if (switches::SeccompSandboxEnabled()) {
[email protected]a9c54a172009-11-07 06:09:38654 g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY);
655 if (g_proc_fd < 0) {
656 LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp "
657 "sandboxing.";
658 }
659 }
[email protected]36ea6c6f2010-03-17 20:08:01660#endif // SECCOMP_SANDBOX
[email protected]a9c54a172009-11-07 06:09:38661
662 // Turn on the SELinux or SUID sandbox
663 if (!EnterSandbox()) {
664 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
665 << errno << ")";
666 return false;
667 }
668
[email protected]715b4f262010-07-13 14:17:28669 int sandbox_flags = 0;
670 if (getenv("SBX_D"))
671 sandbox_flags |= ZygoteHost::kSandboxSUID;
672 if (getenv("SBX_PID_NS"))
673 sandbox_flags |= ZygoteHost::kSandboxPIDNS;
674 if (getenv("SBX_NET_NS"))
675 sandbox_flags |= ZygoteHost::kSandboxNetNS;
676
[email protected]36ea6c6f2010-03-17 20:08:01677#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38678 // The seccomp sandbox will be turned on when the renderers start. But we can
679 // already check if sufficient support is available so that we only need to
680 // print one error message for the entire browser session.
[email protected]39c4e1a82010-03-30 19:47:41681 if (g_proc_fd >= 0 && switches::SeccompSandboxEnabled()) {
[email protected]a9c54a172009-11-07 06:09:38682 if (!SupportsSeccompSandbox(g_proc_fd)) {
[email protected]e8c916a2009-11-04 17:52:47683 // There are a good number of users who cannot use the seccomp sandbox
684 // (e.g. because their distribution does not enable seccomp mode by
685 // default). While we would prefer to deny execution in this case, it
686 // seems more realistic to continue in degraded mode.
[email protected]1b5d28f2010-02-18 15:53:36687 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
688 "Seccomp sandbox. Running renderers with Seccomp "
689 "sandboxing disabled.";
[email protected]e8c916a2009-11-04 17:52:47690 } else {
691 LOG(INFO) << "Enabling experimental Seccomp sandbox.";
[email protected]715b4f262010-07-13 14:17:28692 sandbox_flags |= ZygoteHost::kSandboxSeccomp;
[email protected]e8c916a2009-11-04 17:52:47693 }
694 }
[email protected]36ea6c6f2010-03-17 20:08:01695#endif // SECCOMP_SANDBOX
[email protected]e8c916a2009-11-04 17:52:47696
[email protected]715b4f262010-07-13 14:17:28697 Zygote zygote(sandbox_flags);
[email protected]c548be22010-03-08 12:55:57698 // This function call can return multiple times, once per fork().
[email protected]cc8f1462009-06-12 17:36:55699 return zygote.ProcessRequests();
700}