blob: 51a658ef3bae9ae13fc4296bc5da3dbae4d67be8 [file] [log] [blame]
[email protected]cc8f1462009-06-12 17:36:551// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[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]cc8f1462009-06-12 17:36:5524#include "base/global_descriptors_posix.h"
[email protected]8ecd3aad2009-11-04 08:32:2225#include "base/hash_tables.h"
26#include "base/linux_util.h"
[email protected]c9e45da02009-08-05 17:35:0827#include "base/path_service.h"
[email protected]cc8f1462009-06-12 17:36:5528#include "base/pickle.h"
[email protected]4378a822009-07-08 01:15:1429#include "base/rand_util.h"
[email protected]1831acf2009-10-05 16:38:4130#include "base/scoped_ptr.h"
[email protected]80a086c52009-08-04 17:52:0431#include "base/sys_info.h"
[email protected]cc8f1462009-06-12 17:36:5532#include "base/unix_domain_socket_posix.h"
[email protected]8015de32009-11-18 04:13:3333#include "build/build_config.h"
[email protected]cc8f1462009-06-12 17:36:5534
35#include "chrome/browser/zygote_host_linux.h"
36#include "chrome/common/chrome_descriptors.h"
[email protected]4730db92009-07-22 00:40:4837#include "chrome/common/chrome_switches.h"
[email protected]cc8f1462009-06-12 17:36:5538#include "chrome/common/main_function_params.h"
39#include "chrome/common/process_watcher.h"
[email protected]73fa63992009-07-20 20:30:0740#include "chrome/common/sandbox_methods_linux.h"
[email protected]cc8f1462009-06-12 17:36:5541
[email protected]c9e45da02009-08-05 17:35:0842#include "media/base/media.h"
43
[email protected]abe3ad92009-06-15 18:15:0844#include "skia/ext/SkFontHost_fontconfig_control.h"
45
[email protected]e8c916a2009-11-04 17:52:4746#include "sandbox/linux/seccomp/sandbox.h"
47
[email protected]1831acf2009-10-05 16:38:4148#include "unicode/timezone.h"
49
[email protected]36ea6c6f2010-03-17 20:08:0150#if defined(ARCH_CPU_X86_FAMILY) && !defined(CHROMIUM_SELINUX)
51// The seccomp sandbox is enabled on all ia32 and x86-64 processor as long as
52// we aren't using SELinux.
53#define SECCOMP_SANDBOX
54#endif
55
[email protected]cc8f1462009-06-12 17:36:5556// http://code.google.com/p/chromium/wiki/LinuxZygote
57
[email protected]8ecd3aad2009-11-04 08:32:2258static const int kBrowserDescriptor = 3;
[email protected]73fa63992009-07-20 20:30:0759static const int kMagicSandboxIPCDescriptor = 5;
[email protected]8ecd3aad2009-11-04 08:32:2260static const int kZygoteIdDescriptor = 7;
61static bool g_suid_sandbox_active = false;
[email protected]36ea6c6f2010-03-17 20:08:0162#if defined(SECCOMP_SANDBOX)
[email protected]4f03cbc2009-11-19 00:50:4863// |g_proc_fd| is used only by the seccomp sandbox.
[email protected]a9c54a172009-11-07 06:09:3864static int g_proc_fd = -1;
[email protected]4f03cbc2009-11-19 00:50:4865#endif
[email protected]73fa63992009-07-20 20:30:0766
[email protected]a89a55dd2010-04-19 14:51:1367#if defined(CHROMIUM_SELINUX)
68static void SELinuxTransitionToTypeOrDie(const char* type) {
69 security_context_t security_context;
70 if (getcon(&security_context))
71 LOG(FATAL) << "Cannot get SELinux context";
72
73 context_t context = context_new(security_context);
74 context_type_set(context, type);
75 const int r = setcon(context_str(context));
76 context_free(context);
77 freecon(security_context);
78
79 if (r) {
80 LOG(FATAL) << "dynamic transition to type '" << type << "' failed. "
81 "(this binary has been built with SELinux support, but maybe "
82 "the policies haven't been loaded into the kernel?)";
83 }
84}
85#endif // CHROMIUM_SELINUX
86
[email protected]cc8f1462009-06-12 17:36:5587// This is the object which implements the zygote. The ZygoteMain function,
[email protected]61883442010-07-12 15:14:3488// which is called from ChromeMain, simply constructs one of these objects and
89// runs it.
[email protected]cc8f1462009-06-12 17:36:5590class Zygote {
91 public:
[email protected]715b4f262010-07-13 14:17:2892 explicit Zygote(int sandbox_flags)
93 : sandbox_flags_(sandbox_flags) {
94 }
95
[email protected]cc8f1462009-06-12 17:36:5596 bool ProcessRequests() {
97 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
98 // browser on it.
[email protected]8ecd3aad2009-11-04 08:32:2299 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
[email protected]abe3ad92009-06-15 18:15:08100 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:55101
102 // We need to accept SIGCHLD, even though our handler is a no-op because
103 // otherwise we cannot wait on children. (According to POSIX 2001.)
104 struct sigaction action;
105 memset(&action, 0, sizeof(action));
106 action.sa_handler = SIGCHLDHandler;
107 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
108
[email protected]8ecd3aad2009-11-04 08:32:22109 if (g_suid_sandbox_active) {
110 // Let the ZygoteHost know we are ready to go.
111 // The receiving code is in chrome/browser/zygote_host_linux.cc.
112 std::vector<int> empty;
113 bool r = base::SendMsg(kBrowserDescriptor, kZygoteMagic,
114 sizeof(kZygoteMagic), empty);
115 CHECK(r) << "Sending zygote magic failed";
116 }
117
[email protected]cc8f1462009-06-12 17:36:55118 for (;;) {
[email protected]c548be22010-03-08 12:55:57119 // This function call can return multiple times, once per fork().
[email protected]8ecd3aad2009-11-04 08:32:22120 if (HandleRequestFromBrowser(kBrowserDescriptor))
[email protected]cc8f1462009-06-12 17:36:55121 return true;
122 }
123 }
124
125 private:
126 // See comment below, where sigaction is called.
127 static void SIGCHLDHandler(int signal) { }
128
129 // ---------------------------------------------------------------------------
130 // Requests from the browser...
131
132 // Read and process a request from the browser. Returns true if we are in a
133 // new process and thus need to unwind back into ChromeMain.
134 bool HandleRequestFromBrowser(int fd) {
135 std::vector<int> fds;
[email protected]abe3ad92009-06-15 18:15:08136 static const unsigned kMaxMessageLength = 1024;
[email protected]cc8f1462009-06-12 17:36:55137 char buf[kMaxMessageLength];
138 const ssize_t len = base::RecvMsg(fd, buf, sizeof(buf), &fds);
139 if (len == -1) {
140 LOG(WARNING) << "Error reading message from browser: " << errno;
141 return false;
142 }
143
144 if (len == 0) {
145 // EOF from the browser. We should die.
146 _exit(0);
147 return false;
148 }
149
150 Pickle pickle(buf, len);
151 void* iter = NULL;
152
153 int kind;
[email protected]57113ea2009-06-18 02:23:16154 if (pickle.ReadInt(&iter, &kind)) {
155 switch (kind) {
156 case ZygoteHost::kCmdFork:
[email protected]c548be22010-03-08 12:55:57157 // This function call can return multiple times, once per fork().
[email protected]57113ea2009-06-18 02:23:16158 return HandleForkRequest(fd, pickle, iter, fds);
159 case ZygoteHost::kCmdReap:
160 if (!fds.empty())
161 break;
[email protected]c548be22010-03-08 12:55:57162 HandleReapRequest(fd, pickle, iter);
163 return false;
[email protected]57113ea2009-06-18 02:23:16164 case ZygoteHost::kCmdDidProcessCrash:
165 if (!fds.empty())
166 break;
[email protected]c548be22010-03-08 12:55:57167 HandleDidProcessCrash(fd, pickle, iter);
168 return false;
[email protected]715b4f262010-07-13 14:17:28169 case ZygoteHost::kCmdGetSandboxStatus:
170 HandleGetSandboxStatus(fd, pickle, iter);
171 return false;
[email protected]57113ea2009-06-18 02:23:16172 default:
173 NOTREACHED();
174 break;
175 }
[email protected]cc8f1462009-06-12 17:36:55176 }
177
[email protected]cc8f1462009-06-12 17:36:55178 LOG(WARNING) << "Error parsing message from browser";
179 for (std::vector<int>::const_iterator
180 i = fds.begin(); i != fds.end(); ++i)
181 close(*i);
182 return false;
183 }
184
[email protected]c548be22010-03-08 12:55:57185 void HandleReapRequest(int fd, const Pickle& pickle, void* iter) {
[email protected]8ecd3aad2009-11-04 08:32:22186 base::ProcessId child;
187 base::ProcessId actual_child;
[email protected]cc8f1462009-06-12 17:36:55188
189 if (!pickle.ReadInt(&iter, &child)) {
190 LOG(WARNING) << "Error parsing reap request from browser";
[email protected]c548be22010-03-08 12:55:57191 return;
[email protected]cc8f1462009-06-12 17:36:55192 }
193
[email protected]8ecd3aad2009-11-04 08:32:22194 if (g_suid_sandbox_active) {
195 actual_child = real_pids_to_sandbox_pids[child];
196 if (!actual_child)
[email protected]c548be22010-03-08 12:55:57197 return;
[email protected]8ecd3aad2009-11-04 08:32:22198 real_pids_to_sandbox_pids.erase(child);
199 } else {
200 actual_child = child;
201 }
202
203 ProcessWatcher::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55204 }
205
[email protected]c548be22010-03-08 12:55:57206 void HandleDidProcessCrash(int fd, const Pickle& pickle, void* iter) {
[email protected]57113ea2009-06-18 02:23:16207 base::ProcessHandle child;
208
209 if (!pickle.ReadInt(&iter, &child)) {
210 LOG(WARNING) << "Error parsing DidProcessCrash request from browser";
[email protected]c548be22010-03-08 12:55:57211 return;
[email protected]57113ea2009-06-18 02:23:16212 }
213
214 bool child_exited;
[email protected]8ecd3aad2009-11-04 08:32:22215 bool did_crash;
216 if (g_suid_sandbox_active)
217 child = real_pids_to_sandbox_pids[child];
218 if (child)
219 did_crash = base::DidProcessCrash(&child_exited, child);
220 else
221 did_crash = child_exited = false;
[email protected]57113ea2009-06-18 02:23:16222
223 Pickle write_pickle;
224 write_pickle.WriteBool(did_crash);
225 write_pickle.WriteBool(child_exited);
[email protected]4b809bc2010-04-19 16:12:05226 if (HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size())) !=
227 write_pickle.size()) {
[email protected]19cb9292010-04-16 23:00:15228 PLOG(ERROR) << "write";
[email protected]4b809bc2010-04-19 16:12:05229 }
[email protected]57113ea2009-06-18 02:23:16230 }
231
[email protected]cc8f1462009-06-12 17:36:55232 // Handle a 'fork' request from the browser: this means that the browser
233 // wishes to start a new renderer.
[email protected]8ecd3aad2009-11-04 08:32:22234 bool HandleForkRequest(int fd, const Pickle& pickle, void* iter,
[email protected]cc8f1462009-06-12 17:36:55235 std::vector<int>& fds) {
236 std::vector<std::string> args;
237 int argc, numfds;
238 base::GlobalDescriptors::Mapping mapping;
[email protected]8ecd3aad2009-11-04 08:32:22239 base::ProcessId child;
240 uint64_t dummy_inode = 0;
241 int dummy_fd = -1;
[email protected]cc8f1462009-06-12 17:36:55242
243 if (!pickle.ReadInt(&iter, &argc))
244 goto error;
245
246 for (int i = 0; i < argc; ++i) {
247 std::string arg;
248 if (!pickle.ReadString(&iter, &arg))
249 goto error;
250 args.push_back(arg);
251 }
252
253 if (!pickle.ReadInt(&iter, &numfds))
254 goto error;
255 if (numfds != static_cast<int>(fds.size()))
256 goto error;
257
258 for (int i = 0; i < numfds; ++i) {
259 base::GlobalDescriptors::Key key;
260 if (!pickle.ReadUInt32(&iter, &key))
261 goto error;
262 mapping.push_back(std::make_pair(key, fds[i]));
263 }
264
[email protected]abe3ad92009-06-15 18:15:08265 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22266 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
267
268 if (g_suid_sandbox_active) {
269 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
270 if (dummy_fd < 0)
271 goto error;
272
273 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd))
274 goto error;
275 }
[email protected]abe3ad92009-06-15 18:15:08276
[email protected]cc8f1462009-06-12 17:36:55277 child = fork();
278
279 if (!child) {
[email protected]36ea6c6f2010-03-17 20:08:01280#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38281 // Try to open /proc/self/maps as the seccomp sandbox needs access to it
282 if (g_proc_fd >= 0) {
283 int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY);
284 if (proc_self_maps >= 0) {
285 SeccompSandboxSetProcSelfMaps(proc_self_maps);
286 }
287 close(g_proc_fd);
288 g_proc_fd = -1;
289 }
[email protected]8015de32009-11-18 04:13:33290#endif
[email protected]a9c54a172009-11-07 06:09:38291
[email protected]8ecd3aad2009-11-04 08:32:22292 close(kBrowserDescriptor); // our socket from the browser
[email protected]cbcf9cc32010-02-17 04:05:59293 if (g_suid_sandbox_active)
294 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]cc8f1462009-06-12 17:36:55295 Singleton<base::GlobalDescriptors>()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39296
[email protected]a89a55dd2010-04-19 14:51:13297#if defined(CHROMIUM_SELINUX)
298 SELinuxTransitionToTypeOrDie("chromium_renderer_t");
299#endif
300
[email protected]0189bbd2009-10-12 22:50:39301 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55302 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39303 CommandLine::Init(0, NULL);
304 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]7f113f32009-09-10 18:02:17305 CommandLine::SetProcTitle();
[email protected]c548be22010-03-08 12:55:57306 // The fork() request is handled further up the call stack.
[email protected]cc8f1462009-06-12 17:36:55307 return true;
[email protected]8ecd3aad2009-11-04 08:32:22308 } else if (child < 0) {
[email protected]466cffd2010-06-09 18:10:56309 LOG(ERROR) << "Zygote could not fork: " << errno;
[email protected]8ecd3aad2009-11-04 08:32:22310 goto error;
[email protected]cc8f1462009-06-12 17:36:55311 }
312
[email protected]8ecd3aad2009-11-04 08:32:22313 {
314 base::ProcessId proc_id;
315 if (g_suid_sandbox_active) {
316 close(dummy_fd);
317 dummy_fd = -1;
318 uint8_t reply_buf[512];
319 Pickle request;
320 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
321 request.WriteUInt64(dummy_inode);
[email protected]83a0cbe2009-11-04 04:22:47322
[email protected]8ecd3aad2009-11-04 08:32:22323 const ssize_t r = base::SendRecvMsg(kMagicSandboxIPCDescriptor,
324 reply_buf, sizeof(reply_buf),
325 NULL, request);
326 if (r == -1)
327 goto error;
328
329 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
330 void* iter2 = NULL;
331 if (!reply.ReadInt(&iter2, &proc_id))
332 goto error;
333 real_pids_to_sandbox_pids[proc_id] = child;
334 } else {
335 proc_id = child;
336 }
337
338 for (std::vector<int>::const_iterator
339 i = fds.begin(); i != fds.end(); ++i)
340 close(*i);
341
[email protected]19cb9292010-04-16 23:00:15342 if (HANDLE_EINTR(write(fd, &proc_id, sizeof(proc_id))) < 0)
343 PLOG(ERROR) << "write";
[email protected]8ecd3aad2009-11-04 08:32:22344 return false;
345 }
[email protected]83a0cbe2009-11-04 04:22:47346
347 error:
[email protected]8ecd3aad2009-11-04 08:32:22348 LOG(ERROR) << "Error parsing fork request from browser";
[email protected]83a0cbe2009-11-04 04:22:47349 for (std::vector<int>::const_iterator
350 i = fds.begin(); i != fds.end(); ++i)
351 close(*i);
[email protected]8ecd3aad2009-11-04 08:32:22352 if (dummy_fd >= 0)
353 close(dummy_fd);
[email protected]cc8f1462009-06-12 17:36:55354 return false;
355 }
[email protected]8ecd3aad2009-11-04 08:32:22356
[email protected]715b4f262010-07-13 14:17:28357 bool HandleGetSandboxStatus(int fd, const Pickle& pickle, void* iter) {
358 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_)) !=
359 sizeof(sandbox_flags_))) {
360 PLOG(ERROR) << "write";
361 }
362
363 return false;
364 }
365
[email protected]8ecd3aad2009-11-04 08:32:22366 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
367 // fork() returns are not the real PIDs, so we need to map the Real PIDS
368 // into the sandbox PID namespace.
369 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
370 ProcessMap real_pids_to_sandbox_pids;
[email protected]715b4f262010-07-13 14:17:28371
372 const int sandbox_flags_;
[email protected]cc8f1462009-06-12 17:36:55373};
374
[email protected]ad6d2c42009-09-15 20:13:38375// With SELinux we can carve out a precise sandbox, so we don't have to play
376// with intercepting libc calls.
377#if !defined(CHROMIUM_SELINUX)
378
[email protected]a0f200ea2009-08-06 18:44:06379static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
380 char* timezone_out,
381 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07382 Pickle request;
383 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
384 request.WriteString(
385 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
386
387 uint8_t reply_buf[512];
388 const ssize_t r = base::SendRecvMsg(
389 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
390 if (r == -1) {
391 memset(output, 0, sizeof(struct tm));
392 return;
393 }
394
395 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
396 void* iter = NULL;
[email protected]9debcda52009-07-22 20:06:51397 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07398 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51399 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07400 result.size() != sizeof(struct tm)) {
401 memset(output, 0, sizeof(struct tm));
402 return;
403 }
404
405 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51406 if (timezone_out_len) {
407 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
408 memcpy(timezone_out, timezone.data(), copy_len);
409 timezone_out[copy_len] = 0;
410 output->tm_zone = timezone_out;
411 } else {
412 output->tm_zone = NULL;
413 }
[email protected]73fa63992009-07-20 20:30:07414}
415
[email protected]a0f200ea2009-08-06 18:44:06416static bool g_am_zygote_or_renderer = false;
417
418// Sandbox interception of libc calls.
419//
420// Because we are running in a sandbox certain libc calls will fail (localtime
421// being the motivating example - it needs to read /etc/localtime). We need to
422// intercept these calls and proxy them to the browser. However, these calls
423// may come from us or from our libraries. In some cases we can't just change
424// our code.
425//
426// It's for these cases that we have the following setup:
427//
428// We define global functions for those functions which we wish to override.
429// Since we will be first in the dynamic resolution order, the dynamic linker
430// will point callers to our versions of these functions. However, we have the
431// same binary for both the browser and the renderers, which means that our
432// overrides will apply in the browser too.
433//
434// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
435// renderer process. It's set in ZygoteMain and inherited by the renderers when
436// they fork. (This means that it'll be incorrect for global constructor
437// functions and before ZygoteMain is called - beware).
438//
439// Our replacement functions can check this global and either proxy
440// the call to the browser over the sandbox IPC
441// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
442// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
443// current module.
444//
445// Other avenues:
446//
447// Our first attempt involved some assembly to patch the GOT of the current
448// module. This worked, but was platform specific and doesn't catch the case
449// where a library makes a call rather than current module.
450//
451// We also considered patching the function in place, but this would again by
452// platform specific and the above technique seems to work well enough.
453
[email protected]a5d7bb82009-09-08 22:30:58454static void WarnOnceAboutBrokenDlsym();
455
[email protected]73fa63992009-07-20 20:30:07456struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06457 if (g_am_zygote_or_renderer) {
458 static struct tm time_struct;
459 static char timezone_string[64];
460 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
461 sizeof(timezone_string));
462 return &time_struct;
463 } else {
464 typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
465 static LocaltimeFunction libc_localtime;
[email protected]a5d7bb82009-09-08 22:30:58466 static bool have_libc_localtime = false;
467 if (!have_libc_localtime) {
[email protected]a0f200ea2009-08-06 18:44:06468 libc_localtime = (LocaltimeFunction) dlsym(RTLD_NEXT, "localtime");
[email protected]a5d7bb82009-09-08 22:30:58469 have_libc_localtime = true;
470 }
471
472 if (!libc_localtime) {
473 // http://code.google.com/p/chromium/issues/detail?id=16800
474 //
475 // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces
476 // it with a version which doesn't work. In this case we'll get a NULL
477 // result. There's not a lot we can do at this point, so we just bodge it!
478 WarnOnceAboutBrokenDlsym();
479
480 return gmtime(timep);
481 }
[email protected]a0f200ea2009-08-06 18:44:06482
483 return libc_localtime(timep);
484 }
[email protected]73fa63992009-07-20 20:30:07485}
486
487struct tm* localtime_r(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06488 if (g_am_zygote_or_renderer) {
489 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
490 return result;
491 } else {
492 typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
493 struct tm* result);
494 static LocaltimeRFunction libc_localtime_r;
[email protected]a5d7bb82009-09-08 22:30:58495 static bool have_libc_localtime_r = false;
496 if (!have_libc_localtime_r) {
[email protected]a0f200ea2009-08-06 18:44:06497 libc_localtime_r = (LocaltimeRFunction) dlsym(RTLD_NEXT, "localtime_r");
[email protected]a5d7bb82009-09-08 22:30:58498 have_libc_localtime_r = true;
499 }
500
501 if (!libc_localtime_r) {
502 // See |localtime|, above.
503 WarnOnceAboutBrokenDlsym();
504
505 return gmtime_r(timep, result);
506 }
[email protected]a0f200ea2009-08-06 18:44:06507
508 return libc_localtime_r(timep, result);
509 }
[email protected]73fa63992009-07-20 20:30:07510}
511
[email protected]a5d7bb82009-09-08 22:30:58512// See the comments at the callsite in |localtime| about this function.
513static void WarnOnceAboutBrokenDlsym() {
514 static bool have_shown_warning = false;
515 if (!have_shown_warning) {
516 LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been "
517 "reported to be caused by Nvidia's libGL. You should expect "
518 "time related functions to misbehave. "
519 "http://code.google.com/p/chromium/issues/detail?id=16800";
520 have_shown_warning = true;
521 }
522}
[email protected]ad6d2c42009-09-15 20:13:38523#endif // !CHROMIUM_SELINUX
[email protected]a5d7bb82009-09-08 22:30:58524
[email protected]ad6d2c42009-09-15 20:13:38525// This function triggers the static and lazy construction of objects that need
526// to be created before imposing the sandbox.
527static void PreSandboxInit() {
[email protected]b8419412010-03-29 20:18:29528 base::RandUint64();
[email protected]4378a822009-07-08 01:15:14529
[email protected]b8419412010-03-29 20:18:29530 base::SysInfo::MaxSharedMemorySize();
[email protected]80a086c52009-08-04 17:52:04531
[email protected]b8419412010-03-29 20:18:29532 // To make wcstombs/mbstowcs work in a renderer, setlocale() has to be
533 // called before the sandbox is triggered. It's possible to avoid calling
534 // setlocale() by pulling out the conversion between FilePath and
535 // WebCore String out of the renderer and using string16 in place of
536 // FilePath for IPC.
537 const char* locale = setlocale(LC_ALL, "");
538 LOG_IF(WARNING, locale == NULL) << "setlocale failed.";
[email protected]e6acd672009-07-24 21:51:33539
[email protected]b8419412010-03-29 20:18:29540 // ICU DateFormat class (used in base/time_format.cc) needs to get the
541 // Olson timezone ID by accessing the zoneinfo files on disk. After
542 // TimeZone::createDefault is called once here, the timezone ID is
543 // cached and there's no more need to access the file system.
544 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
[email protected]1831acf2009-10-05 16:38:41545
[email protected]b8419412010-03-29 20:18:29546 FilePath module_path;
547 if (PathService::Get(base::DIR_MODULE, &module_path))
548 media::InitializeMediaLibrary(module_path);
[email protected]ad6d2c42009-09-15 20:13:38549}
550
551#if !defined(CHROMIUM_SELINUX)
552static bool EnterSandbox() {
[email protected]b8419412010-03-29 20:18:29553 // The SUID sandbox sets this environment variable to a file descriptor
554 // over which we can signal that we have completed our startup and can be
555 // chrooted.
[email protected]ad6d2c42009-09-15 20:13:38556 const char* const sandbox_fd_string = getenv("SBX_D");
[email protected]ad6d2c42009-09-15 20:13:38557
[email protected]39c4e1a82010-03-30 19:47:41558 if (switches::SeccompSandboxEnabled()) {
[email protected]b8419412010-03-29 20:18:29559 PreSandboxInit();
560 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
561 } else if (sandbox_fd_string) { // Use the SUID sandbox.
[email protected]8ecd3aad2009-11-04 08:32:22562 g_suid_sandbox_active = true;
563
[email protected]ad6d2c42009-09-15 20:13:38564 char* endptr;
565 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
566 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
567 return false;
568 const int fd = fd_long;
569
570 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08571
[email protected]eaebefaf2010-02-23 22:58:18572 static const char kMsgChrootMe = 'C';
573 static const char kMsgChrootSuccessful = 'O';
[email protected]abe3ad92009-06-15 18:15:08574
[email protected]eaebefaf2010-02-23 22:58:18575 if (HANDLE_EINTR(write(fd, &kMsgChrootMe, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32576 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08577 return false;
[email protected]0e1611122009-07-10 18:17:32578 }
[email protected]abe3ad92009-06-15 18:15:08579
[email protected]87ed6952009-07-16 02:52:15580 // We need to reap the chroot helper process in any event:
581 wait(NULL);
582
[email protected]abe3ad92009-06-15 18:15:08583 char reply;
[email protected]87f8ce62009-07-10 19:14:31584 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32585 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08586 return false;
[email protected]0e1611122009-07-10 18:17:32587 }
[email protected]87f8ce62009-07-10 19:14:31588
[email protected]eaebefaf2010-02-23 22:58:18589 if (reply != kMsgChrootSuccessful) {
[email protected]0e1611122009-07-10 18:17:32590 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08591 return false;
[email protected]0e1611122009-07-10 18:17:32592 }
[email protected]abe3ad92009-06-15 18:15:08593
[email protected]abe3ad92009-06-15 18:15:08594 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
595
[email protected]415493be2009-07-10 17:50:24596 // Previously, we required that the binary be non-readable. This causes the
597 // kernel to mark the process as non-dumpable at startup. The thinking was
598 // that, although we were putting the renderers into a PID namespace (with
599 // the SUID sandbox), they would nonetheless be in the /same/ PID
600 // namespace. So they could ptrace each other unless they were non-dumpable.
601 //
602 // If the binary was readable, then there would be a window between process
603 // startup and the point where we set the non-dumpable flag in which a
604 // compromised renderer could ptrace attach.
605 //
606 // However, now that we have a zygote model, only the (trusted) zygote
607 // exists at this point and we can set the non-dumpable flag which is
608 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48609 //
610 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
611 // issues, one can specify --allow-sandbox-debugging to let the process be
612 // dumpable.
613 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
614 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
615 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
616 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
617 LOG(ERROR) << "Failed to set non-dumpable flag";
618 return false;
619 }
[email protected]0e1611122009-07-10 18:17:32620 }
[email protected]abe3ad92009-06-15 18:15:08621 } else {
622 SkiaFontConfigUseDirectImplementation();
623 }
624
625 return true;
626}
[email protected]ad6d2c42009-09-15 20:13:38627#else // CHROMIUM_SELINUX
628
629static bool EnterSandbox() {
630 PreSandboxInit();
631 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
[email protected]ad6d2c42009-09-15 20:13:38632 return true;
633}
634
635#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08636
[email protected]cc8f1462009-06-12 17:36:55637bool ZygoteMain(const MainFunctionParams& params) {
[email protected]ad6d2c42009-09-15 20:13:38638#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06639 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38640#endif
[email protected]a0f200ea2009-08-06 18:44:06641
[email protected]36ea6c6f2010-03-17 20:08:01642#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38643 // The seccomp sandbox needs access to files in /proc, which might be denied
644 // after one of the other sandboxes have been started. So, obtain a suitable
645 // file handle in advance.
[email protected]39c4e1a82010-03-30 19:47:41646 if (switches::SeccompSandboxEnabled()) {
[email protected]a9c54a172009-11-07 06:09:38647 g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY);
648 if (g_proc_fd < 0) {
649 LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp "
650 "sandboxing.";
651 }
652 }
[email protected]36ea6c6f2010-03-17 20:08:01653#endif // SECCOMP_SANDBOX
[email protected]a9c54a172009-11-07 06:09:38654
655 // Turn on the SELinux or SUID sandbox
656 if (!EnterSandbox()) {
657 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
658 << errno << ")";
659 return false;
660 }
661
[email protected]715b4f262010-07-13 14:17:28662 int sandbox_flags = 0;
663 if (getenv("SBX_D"))
664 sandbox_flags |= ZygoteHost::kSandboxSUID;
665 if (getenv("SBX_PID_NS"))
666 sandbox_flags |= ZygoteHost::kSandboxPIDNS;
667 if (getenv("SBX_NET_NS"))
668 sandbox_flags |= ZygoteHost::kSandboxNetNS;
669
[email protected]36ea6c6f2010-03-17 20:08:01670#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38671 // The seccomp sandbox will be turned on when the renderers start. But we can
672 // already check if sufficient support is available so that we only need to
673 // print one error message for the entire browser session.
[email protected]39c4e1a82010-03-30 19:47:41674 if (g_proc_fd >= 0 && switches::SeccompSandboxEnabled()) {
[email protected]a9c54a172009-11-07 06:09:38675 if (!SupportsSeccompSandbox(g_proc_fd)) {
[email protected]e8c916a2009-11-04 17:52:47676 // There are a good number of users who cannot use the seccomp sandbox
677 // (e.g. because their distribution does not enable seccomp mode by
678 // default). While we would prefer to deny execution in this case, it
679 // seems more realistic to continue in degraded mode.
[email protected]1b5d28f2010-02-18 15:53:36680 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
681 "Seccomp sandbox. Running renderers with Seccomp "
682 "sandboxing disabled.";
[email protected]e8c916a2009-11-04 17:52:47683 } else {
684 LOG(INFO) << "Enabling experimental Seccomp sandbox.";
[email protected]715b4f262010-07-13 14:17:28685 sandbox_flags |= ZygoteHost::kSandboxSeccomp;
[email protected]e8c916a2009-11-04 17:52:47686 }
687 }
[email protected]36ea6c6f2010-03-17 20:08:01688#endif // SECCOMP_SANDBOX
[email protected]e8c916a2009-11-04 17:52:47689
[email protected]715b4f262010-07-13 14:17:28690 Zygote zygote(sandbox_flags);
[email protected]c548be22010-03-08 12:55:57691 // This function call can return multiple times, once per fork().
[email protected]cc8f1462009-06-12 17:36:55692 return zygote.ProcessRequests();
693}