blob: 7d877bd8abd3a73735eb5df8dd4103116be5f0eb [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"
24#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]cc8f1462009-06-12 17:36:5567// This is the object which implements the zygote. The ZygoteMain function,
68// which is called from ChromeMain, at the the bottom and simple constructs one
69// of these objects and runs it.
70class Zygote {
71 public:
72 bool ProcessRequests() {
73 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
74 // browser on it.
[email protected]8ecd3aad2009-11-04 08:32:2275 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
[email protected]abe3ad92009-06-15 18:15:0876 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:5577
78 // We need to accept SIGCHLD, even though our handler is a no-op because
79 // otherwise we cannot wait on children. (According to POSIX 2001.)
80 struct sigaction action;
81 memset(&action, 0, sizeof(action));
82 action.sa_handler = SIGCHLDHandler;
83 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
84
[email protected]8ecd3aad2009-11-04 08:32:2285 if (g_suid_sandbox_active) {
86 // Let the ZygoteHost know we are ready to go.
87 // The receiving code is in chrome/browser/zygote_host_linux.cc.
88 std::vector<int> empty;
89 bool r = base::SendMsg(kBrowserDescriptor, kZygoteMagic,
90 sizeof(kZygoteMagic), empty);
91 CHECK(r) << "Sending zygote magic failed";
92 }
93
[email protected]cc8f1462009-06-12 17:36:5594 for (;;) {
[email protected]c548be22010-03-08 12:55:5795 // This function call can return multiple times, once per fork().
[email protected]8ecd3aad2009-11-04 08:32:2296 if (HandleRequestFromBrowser(kBrowserDescriptor))
[email protected]cc8f1462009-06-12 17:36:5597 return true;
98 }
99 }
100
101 private:
102 // See comment below, where sigaction is called.
103 static void SIGCHLDHandler(int signal) { }
104
105 // ---------------------------------------------------------------------------
106 // Requests from the browser...
107
108 // Read and process a request from the browser. Returns true if we are in a
109 // new process and thus need to unwind back into ChromeMain.
110 bool HandleRequestFromBrowser(int fd) {
111 std::vector<int> fds;
[email protected]abe3ad92009-06-15 18:15:08112 static const unsigned kMaxMessageLength = 1024;
[email protected]cc8f1462009-06-12 17:36:55113 char buf[kMaxMessageLength];
114 const ssize_t len = base::RecvMsg(fd, buf, sizeof(buf), &fds);
115 if (len == -1) {
116 LOG(WARNING) << "Error reading message from browser: " << errno;
117 return false;
118 }
119
120 if (len == 0) {
121 // EOF from the browser. We should die.
122 _exit(0);
123 return false;
124 }
125
126 Pickle pickle(buf, len);
127 void* iter = NULL;
128
129 int kind;
[email protected]57113ea2009-06-18 02:23:16130 if (pickle.ReadInt(&iter, &kind)) {
131 switch (kind) {
132 case ZygoteHost::kCmdFork:
[email protected]c548be22010-03-08 12:55:57133 // This function call can return multiple times, once per fork().
[email protected]57113ea2009-06-18 02:23:16134 return HandleForkRequest(fd, pickle, iter, fds);
135 case ZygoteHost::kCmdReap:
136 if (!fds.empty())
137 break;
[email protected]c548be22010-03-08 12:55:57138 HandleReapRequest(fd, pickle, iter);
139 return false;
[email protected]57113ea2009-06-18 02:23:16140 case ZygoteHost::kCmdDidProcessCrash:
141 if (!fds.empty())
142 break;
[email protected]c548be22010-03-08 12:55:57143 HandleDidProcessCrash(fd, pickle, iter);
144 return false;
[email protected]57113ea2009-06-18 02:23:16145 default:
146 NOTREACHED();
147 break;
148 }
[email protected]cc8f1462009-06-12 17:36:55149 }
150
[email protected]cc8f1462009-06-12 17:36:55151 LOG(WARNING) << "Error parsing message from browser";
152 for (std::vector<int>::const_iterator
153 i = fds.begin(); i != fds.end(); ++i)
154 close(*i);
155 return false;
156 }
157
[email protected]c548be22010-03-08 12:55:57158 void HandleReapRequest(int fd, const Pickle& pickle, void* iter) {
[email protected]8ecd3aad2009-11-04 08:32:22159 base::ProcessId child;
160 base::ProcessId actual_child;
[email protected]cc8f1462009-06-12 17:36:55161
162 if (!pickle.ReadInt(&iter, &child)) {
163 LOG(WARNING) << "Error parsing reap request from browser";
[email protected]c548be22010-03-08 12:55:57164 return;
[email protected]cc8f1462009-06-12 17:36:55165 }
166
[email protected]8ecd3aad2009-11-04 08:32:22167 if (g_suid_sandbox_active) {
168 actual_child = real_pids_to_sandbox_pids[child];
169 if (!actual_child)
[email protected]c548be22010-03-08 12:55:57170 return;
[email protected]8ecd3aad2009-11-04 08:32:22171 real_pids_to_sandbox_pids.erase(child);
172 } else {
173 actual_child = child;
174 }
175
176 ProcessWatcher::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55177 }
178
[email protected]c548be22010-03-08 12:55:57179 void HandleDidProcessCrash(int fd, const Pickle& pickle, void* iter) {
[email protected]57113ea2009-06-18 02:23:16180 base::ProcessHandle child;
181
182 if (!pickle.ReadInt(&iter, &child)) {
183 LOG(WARNING) << "Error parsing DidProcessCrash request from browser";
[email protected]c548be22010-03-08 12:55:57184 return;
[email protected]57113ea2009-06-18 02:23:16185 }
186
187 bool child_exited;
[email protected]8ecd3aad2009-11-04 08:32:22188 bool did_crash;
189 if (g_suid_sandbox_active)
190 child = real_pids_to_sandbox_pids[child];
191 if (child)
192 did_crash = base::DidProcessCrash(&child_exited, child);
193 else
194 did_crash = child_exited = false;
[email protected]57113ea2009-06-18 02:23:16195
196 Pickle write_pickle;
197 write_pickle.WriteBool(did_crash);
198 write_pickle.WriteBool(child_exited);
199 HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size()));
[email protected]57113ea2009-06-18 02:23:16200 }
201
[email protected]cc8f1462009-06-12 17:36:55202 // Handle a 'fork' request from the browser: this means that the browser
203 // wishes to start a new renderer.
[email protected]8ecd3aad2009-11-04 08:32:22204 bool HandleForkRequest(int fd, const Pickle& pickle, void* iter,
[email protected]cc8f1462009-06-12 17:36:55205 std::vector<int>& fds) {
206 std::vector<std::string> args;
207 int argc, numfds;
208 base::GlobalDescriptors::Mapping mapping;
[email protected]8ecd3aad2009-11-04 08:32:22209 base::ProcessId child;
210 uint64_t dummy_inode = 0;
211 int dummy_fd = -1;
[email protected]cc8f1462009-06-12 17:36:55212
213 if (!pickle.ReadInt(&iter, &argc))
214 goto error;
215
216 for (int i = 0; i < argc; ++i) {
217 std::string arg;
218 if (!pickle.ReadString(&iter, &arg))
219 goto error;
220 args.push_back(arg);
221 }
222
223 if (!pickle.ReadInt(&iter, &numfds))
224 goto error;
225 if (numfds != static_cast<int>(fds.size()))
226 goto error;
227
228 for (int i = 0; i < numfds; ++i) {
229 base::GlobalDescriptors::Key key;
230 if (!pickle.ReadUInt32(&iter, &key))
231 goto error;
232 mapping.push_back(std::make_pair(key, fds[i]));
233 }
234
[email protected]abe3ad92009-06-15 18:15:08235 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22236 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
237
238 if (g_suid_sandbox_active) {
239 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
240 if (dummy_fd < 0)
241 goto error;
242
243 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd))
244 goto error;
245 }
[email protected]abe3ad92009-06-15 18:15:08246
[email protected]cc8f1462009-06-12 17:36:55247 child = fork();
248
249 if (!child) {
[email protected]36ea6c6f2010-03-17 20:08:01250#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38251 // Try to open /proc/self/maps as the seccomp sandbox needs access to it
252 if (g_proc_fd >= 0) {
253 int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY);
254 if (proc_self_maps >= 0) {
255 SeccompSandboxSetProcSelfMaps(proc_self_maps);
256 }
257 close(g_proc_fd);
258 g_proc_fd = -1;
259 }
[email protected]8015de32009-11-18 04:13:33260#endif
[email protected]a9c54a172009-11-07 06:09:38261
[email protected]8ecd3aad2009-11-04 08:32:22262 close(kBrowserDescriptor); // our socket from the browser
[email protected]cbcf9cc32010-02-17 04:05:59263 if (g_suid_sandbox_active)
264 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]cc8f1462009-06-12 17:36:55265 Singleton<base::GlobalDescriptors>()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39266
267 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55268 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39269 CommandLine::Init(0, NULL);
270 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]7f113f32009-09-10 18:02:17271 CommandLine::SetProcTitle();
[email protected]c548be22010-03-08 12:55:57272 // The fork() request is handled further up the call stack.
[email protected]cc8f1462009-06-12 17:36:55273 return true;
[email protected]8ecd3aad2009-11-04 08:32:22274 } else if (child < 0) {
275 LOG(ERROR) << "Zygote could not fork";
276 goto error;
[email protected]cc8f1462009-06-12 17:36:55277 }
278
[email protected]8ecd3aad2009-11-04 08:32:22279 {
280 base::ProcessId proc_id;
281 if (g_suid_sandbox_active) {
282 close(dummy_fd);
283 dummy_fd = -1;
284 uint8_t reply_buf[512];
285 Pickle request;
286 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
287 request.WriteUInt64(dummy_inode);
[email protected]83a0cbe2009-11-04 04:22:47288
[email protected]8ecd3aad2009-11-04 08:32:22289 const ssize_t r = base::SendRecvMsg(kMagicSandboxIPCDescriptor,
290 reply_buf, sizeof(reply_buf),
291 NULL, request);
292 if (r == -1)
293 goto error;
294
295 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
296 void* iter2 = NULL;
297 if (!reply.ReadInt(&iter2, &proc_id))
298 goto error;
299 real_pids_to_sandbox_pids[proc_id] = child;
300 } else {
301 proc_id = child;
302 }
303
304 for (std::vector<int>::const_iterator
305 i = fds.begin(); i != fds.end(); ++i)
306 close(*i);
307
308 HANDLE_EINTR(write(fd, &proc_id, sizeof(proc_id)));
309 return false;
310 }
[email protected]83a0cbe2009-11-04 04:22:47311
312 error:
[email protected]8ecd3aad2009-11-04 08:32:22313 LOG(ERROR) << "Error parsing fork request from browser";
[email protected]83a0cbe2009-11-04 04:22:47314 for (std::vector<int>::const_iterator
315 i = fds.begin(); i != fds.end(); ++i)
316 close(*i);
[email protected]8ecd3aad2009-11-04 08:32:22317 if (dummy_fd >= 0)
318 close(dummy_fd);
[email protected]cc8f1462009-06-12 17:36:55319 return false;
320 }
[email protected]8ecd3aad2009-11-04 08:32:22321
322 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
323 // fork() returns are not the real PIDs, so we need to map the Real PIDS
324 // into the sandbox PID namespace.
325 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
326 ProcessMap real_pids_to_sandbox_pids;
[email protected]cc8f1462009-06-12 17:36:55327};
328
[email protected]ad6d2c42009-09-15 20:13:38329// With SELinux we can carve out a precise sandbox, so we don't have to play
330// with intercepting libc calls.
331#if !defined(CHROMIUM_SELINUX)
332
[email protected]a0f200ea2009-08-06 18:44:06333static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
334 char* timezone_out,
335 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07336 Pickle request;
337 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
338 request.WriteString(
339 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
340
341 uint8_t reply_buf[512];
342 const ssize_t r = base::SendRecvMsg(
343 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
344 if (r == -1) {
345 memset(output, 0, sizeof(struct tm));
346 return;
347 }
348
349 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
350 void* iter = NULL;
[email protected]9debcda52009-07-22 20:06:51351 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07352 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51353 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07354 result.size() != sizeof(struct tm)) {
355 memset(output, 0, sizeof(struct tm));
356 return;
357 }
358
359 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51360 if (timezone_out_len) {
361 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
362 memcpy(timezone_out, timezone.data(), copy_len);
363 timezone_out[copy_len] = 0;
364 output->tm_zone = timezone_out;
365 } else {
366 output->tm_zone = NULL;
367 }
[email protected]73fa63992009-07-20 20:30:07368}
369
[email protected]a0f200ea2009-08-06 18:44:06370static bool g_am_zygote_or_renderer = false;
371
372// Sandbox interception of libc calls.
373//
374// Because we are running in a sandbox certain libc calls will fail (localtime
375// being the motivating example - it needs to read /etc/localtime). We need to
376// intercept these calls and proxy them to the browser. However, these calls
377// may come from us or from our libraries. In some cases we can't just change
378// our code.
379//
380// It's for these cases that we have the following setup:
381//
382// We define global functions for those functions which we wish to override.
383// Since we will be first in the dynamic resolution order, the dynamic linker
384// will point callers to our versions of these functions. However, we have the
385// same binary for both the browser and the renderers, which means that our
386// overrides will apply in the browser too.
387//
388// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
389// renderer process. It's set in ZygoteMain and inherited by the renderers when
390// they fork. (This means that it'll be incorrect for global constructor
391// functions and before ZygoteMain is called - beware).
392//
393// Our replacement functions can check this global and either proxy
394// the call to the browser over the sandbox IPC
395// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
396// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
397// current module.
398//
399// Other avenues:
400//
401// Our first attempt involved some assembly to patch the GOT of the current
402// module. This worked, but was platform specific and doesn't catch the case
403// where a library makes a call rather than current module.
404//
405// We also considered patching the function in place, but this would again by
406// platform specific and the above technique seems to work well enough.
407
[email protected]a5d7bb82009-09-08 22:30:58408static void WarnOnceAboutBrokenDlsym();
409
[email protected]73fa63992009-07-20 20:30:07410struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06411 if (g_am_zygote_or_renderer) {
412 static struct tm time_struct;
413 static char timezone_string[64];
414 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
415 sizeof(timezone_string));
416 return &time_struct;
417 } else {
418 typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
419 static LocaltimeFunction libc_localtime;
[email protected]a5d7bb82009-09-08 22:30:58420 static bool have_libc_localtime = false;
421 if (!have_libc_localtime) {
[email protected]a0f200ea2009-08-06 18:44:06422 libc_localtime = (LocaltimeFunction) dlsym(RTLD_NEXT, "localtime");
[email protected]a5d7bb82009-09-08 22:30:58423 have_libc_localtime = true;
424 }
425
426 if (!libc_localtime) {
427 // http://code.google.com/p/chromium/issues/detail?id=16800
428 //
429 // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces
430 // it with a version which doesn't work. In this case we'll get a NULL
431 // result. There's not a lot we can do at this point, so we just bodge it!
432 WarnOnceAboutBrokenDlsym();
433
434 return gmtime(timep);
435 }
[email protected]a0f200ea2009-08-06 18:44:06436
437 return libc_localtime(timep);
438 }
[email protected]73fa63992009-07-20 20:30:07439}
440
441struct tm* localtime_r(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06442 if (g_am_zygote_or_renderer) {
443 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
444 return result;
445 } else {
446 typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
447 struct tm* result);
448 static LocaltimeRFunction libc_localtime_r;
[email protected]a5d7bb82009-09-08 22:30:58449 static bool have_libc_localtime_r = false;
450 if (!have_libc_localtime_r) {
[email protected]a0f200ea2009-08-06 18:44:06451 libc_localtime_r = (LocaltimeRFunction) dlsym(RTLD_NEXT, "localtime_r");
[email protected]a5d7bb82009-09-08 22:30:58452 have_libc_localtime_r = true;
453 }
454
455 if (!libc_localtime_r) {
456 // See |localtime|, above.
457 WarnOnceAboutBrokenDlsym();
458
459 return gmtime_r(timep, result);
460 }
[email protected]a0f200ea2009-08-06 18:44:06461
462 return libc_localtime_r(timep, result);
463 }
[email protected]73fa63992009-07-20 20:30:07464}
465
[email protected]a5d7bb82009-09-08 22:30:58466// See the comments at the callsite in |localtime| about this function.
467static void WarnOnceAboutBrokenDlsym() {
468 static bool have_shown_warning = false;
469 if (!have_shown_warning) {
470 LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been "
471 "reported to be caused by Nvidia's libGL. You should expect "
472 "time related functions to misbehave. "
473 "http://code.google.com/p/chromium/issues/detail?id=16800";
474 have_shown_warning = true;
475 }
476}
[email protected]ad6d2c42009-09-15 20:13:38477#endif // !CHROMIUM_SELINUX
[email protected]a5d7bb82009-09-08 22:30:58478
[email protected]ad6d2c42009-09-15 20:13:38479// This function triggers the static and lazy construction of objects that need
480// to be created before imposing the sandbox.
481static void PreSandboxInit() {
[email protected]b8419412010-03-29 20:18:29482 base::RandUint64();
[email protected]4378a822009-07-08 01:15:14483
[email protected]b8419412010-03-29 20:18:29484 base::SysInfo::MaxSharedMemorySize();
[email protected]80a086c52009-08-04 17:52:04485
[email protected]b8419412010-03-29 20:18:29486 // To make wcstombs/mbstowcs work in a renderer, setlocale() has to be
487 // called before the sandbox is triggered. It's possible to avoid calling
488 // setlocale() by pulling out the conversion between FilePath and
489 // WebCore String out of the renderer and using string16 in place of
490 // FilePath for IPC.
491 const char* locale = setlocale(LC_ALL, "");
492 LOG_IF(WARNING, locale == NULL) << "setlocale failed.";
[email protected]e6acd672009-07-24 21:51:33493
[email protected]b8419412010-03-29 20:18:29494 // ICU DateFormat class (used in base/time_format.cc) needs to get the
495 // Olson timezone ID by accessing the zoneinfo files on disk. After
496 // TimeZone::createDefault is called once here, the timezone ID is
497 // cached and there's no more need to access the file system.
498 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
[email protected]1831acf2009-10-05 16:38:41499
[email protected]b8419412010-03-29 20:18:29500 FilePath module_path;
501 if (PathService::Get(base::DIR_MODULE, &module_path))
502 media::InitializeMediaLibrary(module_path);
[email protected]ad6d2c42009-09-15 20:13:38503}
504
505#if !defined(CHROMIUM_SELINUX)
506static bool EnterSandbox() {
[email protected]b8419412010-03-29 20:18:29507 // The SUID sandbox sets this environment variable to a file descriptor
508 // over which we can signal that we have completed our startup and can be
509 // chrooted.
[email protected]ad6d2c42009-09-15 20:13:38510 const char* const sandbox_fd_string = getenv("SBX_D");
[email protected]ad6d2c42009-09-15 20:13:38511
[email protected]b8419412010-03-29 20:18:29512 if (CommandLine::ForCurrentProcess()->HasSwitch(
513 switches::kEnableSeccompSandbox)) {
514 PreSandboxInit();
515 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
516 } else if (sandbox_fd_string) { // Use the SUID sandbox.
[email protected]8ecd3aad2009-11-04 08:32:22517 g_suid_sandbox_active = true;
518
[email protected]ad6d2c42009-09-15 20:13:38519 char* endptr;
520 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
521 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
522 return false;
523 const int fd = fd_long;
524
525 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08526
[email protected]eaebefaf2010-02-23 22:58:18527 static const char kMsgChrootMe = 'C';
528 static const char kMsgChrootSuccessful = 'O';
[email protected]abe3ad92009-06-15 18:15:08529
[email protected]eaebefaf2010-02-23 22:58:18530 if (HANDLE_EINTR(write(fd, &kMsgChrootMe, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32531 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08532 return false;
[email protected]0e1611122009-07-10 18:17:32533 }
[email protected]abe3ad92009-06-15 18:15:08534
[email protected]87ed6952009-07-16 02:52:15535 // We need to reap the chroot helper process in any event:
536 wait(NULL);
537
[email protected]abe3ad92009-06-15 18:15:08538 char reply;
[email protected]87f8ce62009-07-10 19:14:31539 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32540 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08541 return false;
[email protected]0e1611122009-07-10 18:17:32542 }
[email protected]87f8ce62009-07-10 19:14:31543
[email protected]eaebefaf2010-02-23 22:58:18544 if (reply != kMsgChrootSuccessful) {
[email protected]0e1611122009-07-10 18:17:32545 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08546 return false;
[email protected]0e1611122009-07-10 18:17:32547 }
[email protected]abe3ad92009-06-15 18:15:08548
[email protected]abe3ad92009-06-15 18:15:08549 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
550
[email protected]415493be2009-07-10 17:50:24551 // Previously, we required that the binary be non-readable. This causes the
552 // kernel to mark the process as non-dumpable at startup. The thinking was
553 // that, although we were putting the renderers into a PID namespace (with
554 // the SUID sandbox), they would nonetheless be in the /same/ PID
555 // namespace. So they could ptrace each other unless they were non-dumpable.
556 //
557 // If the binary was readable, then there would be a window between process
558 // startup and the point where we set the non-dumpable flag in which a
559 // compromised renderer could ptrace attach.
560 //
561 // However, now that we have a zygote model, only the (trusted) zygote
562 // exists at this point and we can set the non-dumpable flag which is
563 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48564 //
565 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
566 // issues, one can specify --allow-sandbox-debugging to let the process be
567 // dumpable.
568 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
569 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
570 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
571 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
572 LOG(ERROR) << "Failed to set non-dumpable flag";
573 return false;
574 }
[email protected]0e1611122009-07-10 18:17:32575 }
[email protected]abe3ad92009-06-15 18:15:08576 } else {
577 SkiaFontConfigUseDirectImplementation();
578 }
579
580 return true;
581}
[email protected]ad6d2c42009-09-15 20:13:38582#else // CHROMIUM_SELINUX
583
584static bool EnterSandbox() {
585 PreSandboxInit();
586 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
587
588 security_context_t security_context;
589 if (getcon(&security_context)) {
590 LOG(ERROR) << "Cannot get SELinux context";
591 return false;
592 }
593
594 context_t context = context_new(security_context);
[email protected]36ea6c6f2010-03-17 20:08:01595 context_type_set(context, "chromium_zygote_t");
[email protected]ad6d2c42009-09-15 20:13:38596 const int r = setcon(context_str(context));
597 context_free(context);
598 freecon(security_context);
599
600 if (r) {
[email protected]36ea6c6f2010-03-17 20:08:01601 LOG(ERROR) << "dynamic transition to type 'chromium_zygote_t' failed. "
[email protected]ad6d2c42009-09-15 20:13:38602 "(this binary has been built with SELinux support, but maybe "
[email protected]36ea6c6f2010-03-17 20:08:01603 "the policies haven't been loaded into the kernel?)";
[email protected]ad6d2c42009-09-15 20:13:38604 return false;
605 }
606
607 return true;
608}
609
610#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08611
[email protected]cc8f1462009-06-12 17:36:55612bool ZygoteMain(const MainFunctionParams& params) {
[email protected]ad6d2c42009-09-15 20:13:38613#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06614 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38615#endif
[email protected]a0f200ea2009-08-06 18:44:06616
[email protected]36ea6c6f2010-03-17 20:08:01617#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38618 // The seccomp sandbox needs access to files in /proc, which might be denied
619 // after one of the other sandboxes have been started. So, obtain a suitable
620 // file handle in advance.
[email protected]1b5d28f2010-02-18 15:53:36621 if (CommandLine::ForCurrentProcess()->HasSwitch(
622 switches::kEnableSeccompSandbox)) {
[email protected]a9c54a172009-11-07 06:09:38623 g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY);
624 if (g_proc_fd < 0) {
625 LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp "
626 "sandboxing.";
627 }
628 }
[email protected]36ea6c6f2010-03-17 20:08:01629#endif // SECCOMP_SANDBOX
[email protected]a9c54a172009-11-07 06:09:38630
631 // Turn on the SELinux or SUID sandbox
632 if (!EnterSandbox()) {
633 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
634 << errno << ")";
635 return false;
636 }
637
[email protected]36ea6c6f2010-03-17 20:08:01638#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38639 // The seccomp sandbox will be turned on when the renderers start. But we can
640 // already check if sufficient support is available so that we only need to
641 // print one error message for the entire browser session.
642 if (g_proc_fd >= 0 &&
[email protected]1b5d28f2010-02-18 15:53:36643 CommandLine::ForCurrentProcess()->HasSwitch(
644 switches::kEnableSeccompSandbox)) {
[email protected]a9c54a172009-11-07 06:09:38645 if (!SupportsSeccompSandbox(g_proc_fd)) {
[email protected]e8c916a2009-11-04 17:52:47646 // There are a good number of users who cannot use the seccomp sandbox
647 // (e.g. because their distribution does not enable seccomp mode by
648 // default). While we would prefer to deny execution in this case, it
649 // seems more realistic to continue in degraded mode.
[email protected]1b5d28f2010-02-18 15:53:36650 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
651 "Seccomp sandbox. Running renderers with Seccomp "
652 "sandboxing disabled.";
[email protected]e8c916a2009-11-04 17:52:47653 } else {
654 LOG(INFO) << "Enabling experimental Seccomp sandbox.";
655 }
656 }
[email protected]36ea6c6f2010-03-17 20:08:01657#endif // SECCOMP_SANDBOX
[email protected]e8c916a2009-11-04 17:52:47658
[email protected]cc8f1462009-06-12 17:36:55659 Zygote zygote;
[email protected]c548be22010-03-08 12:55:57660 // This function call can return multiple times, once per fork().
[email protected]cc8f1462009-06-12 17:36:55661 return zygote.ProcessRequests();
662}