blob: 009ba00d9dc552661b8732cef5c0cb04c68f038f [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]cc8f1462009-06-12 17:36:5550// http://code.google.com/p/chromium/wiki/LinuxZygote
51
[email protected]8ecd3aad2009-11-04 08:32:2252static const int kBrowserDescriptor = 3;
[email protected]73fa63992009-07-20 20:30:0753static const int kMagicSandboxIPCDescriptor = 5;
[email protected]8ecd3aad2009-11-04 08:32:2254static const int kZygoteIdDescriptor = 7;
55static bool g_suid_sandbox_active = false;
[email protected]4f03cbc2009-11-19 00:50:4856#if defined(ARCH_CPU_X86_FAMILY)
57// |g_proc_fd| is used only by the seccomp sandbox.
[email protected]a9c54a172009-11-07 06:09:3858static int g_proc_fd = -1;
[email protected]4f03cbc2009-11-19 00:50:4859#endif
[email protected]73fa63992009-07-20 20:30:0760
[email protected]cc8f1462009-06-12 17:36:5561// This is the object which implements the zygote. The ZygoteMain function,
62// which is called from ChromeMain, at the the bottom and simple constructs one
63// of these objects and runs it.
64class Zygote {
65 public:
66 bool ProcessRequests() {
67 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
68 // browser on it.
[email protected]8ecd3aad2009-11-04 08:32:2269 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
[email protected]abe3ad92009-06-15 18:15:0870 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:5571
72 // We need to accept SIGCHLD, even though our handler is a no-op because
73 // otherwise we cannot wait on children. (According to POSIX 2001.)
74 struct sigaction action;
75 memset(&action, 0, sizeof(action));
76 action.sa_handler = SIGCHLDHandler;
77 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
78
[email protected]8ecd3aad2009-11-04 08:32:2279 if (g_suid_sandbox_active) {
80 // Let the ZygoteHost know we are ready to go.
81 // The receiving code is in chrome/browser/zygote_host_linux.cc.
82 std::vector<int> empty;
83 bool r = base::SendMsg(kBrowserDescriptor, kZygoteMagic,
84 sizeof(kZygoteMagic), empty);
85 CHECK(r) << "Sending zygote magic failed";
86 }
87
[email protected]cc8f1462009-06-12 17:36:5588 for (;;) {
[email protected]8ecd3aad2009-11-04 08:32:2289 if (HandleRequestFromBrowser(kBrowserDescriptor))
[email protected]cc8f1462009-06-12 17:36:5590 return true;
91 }
92 }
93
94 private:
95 // See comment below, where sigaction is called.
96 static void SIGCHLDHandler(int signal) { }
97
98 // ---------------------------------------------------------------------------
99 // Requests from the browser...
100
101 // Read and process a request from the browser. Returns true if we are in a
102 // new process and thus need to unwind back into ChromeMain.
103 bool HandleRequestFromBrowser(int fd) {
104 std::vector<int> fds;
[email protected]abe3ad92009-06-15 18:15:08105 static const unsigned kMaxMessageLength = 1024;
[email protected]cc8f1462009-06-12 17:36:55106 char buf[kMaxMessageLength];
107 const ssize_t len = base::RecvMsg(fd, buf, sizeof(buf), &fds);
108 if (len == -1) {
109 LOG(WARNING) << "Error reading message from browser: " << errno;
110 return false;
111 }
112
113 if (len == 0) {
114 // EOF from the browser. We should die.
115 _exit(0);
116 return false;
117 }
118
119 Pickle pickle(buf, len);
120 void* iter = NULL;
121
122 int kind;
[email protected]57113ea2009-06-18 02:23:16123 if (pickle.ReadInt(&iter, &kind)) {
124 switch (kind) {
125 case ZygoteHost::kCmdFork:
126 return HandleForkRequest(fd, pickle, iter, fds);
127 case ZygoteHost::kCmdReap:
128 if (!fds.empty())
129 break;
130 return HandleReapRequest(fd, pickle, iter);
131 case ZygoteHost::kCmdDidProcessCrash:
132 if (!fds.empty())
133 break;
134 return HandleDidProcessCrash(fd, pickle, iter);
135 default:
136 NOTREACHED();
137 break;
138 }
[email protected]cc8f1462009-06-12 17:36:55139 }
140
[email protected]cc8f1462009-06-12 17:36:55141 LOG(WARNING) << "Error parsing message from browser";
142 for (std::vector<int>::const_iterator
143 i = fds.begin(); i != fds.end(); ++i)
144 close(*i);
145 return false;
146 }
147
[email protected]8ecd3aad2009-11-04 08:32:22148 bool HandleReapRequest(int fd, const Pickle& pickle, void* iter) {
149 base::ProcessId child;
150 base::ProcessId actual_child;
[email protected]cc8f1462009-06-12 17:36:55151
152 if (!pickle.ReadInt(&iter, &child)) {
153 LOG(WARNING) << "Error parsing reap request from browser";
154 return false;
155 }
156
[email protected]8ecd3aad2009-11-04 08:32:22157 if (g_suid_sandbox_active) {
158 actual_child = real_pids_to_sandbox_pids[child];
159 if (!actual_child)
160 return false;
161 real_pids_to_sandbox_pids.erase(child);
162 } else {
163 actual_child = child;
164 }
165
166 ProcessWatcher::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55167
168 return false;
169 }
170
[email protected]8ecd3aad2009-11-04 08:32:22171 bool HandleDidProcessCrash(int fd, const Pickle& pickle, void* iter) {
[email protected]57113ea2009-06-18 02:23:16172 base::ProcessHandle child;
173
174 if (!pickle.ReadInt(&iter, &child)) {
175 LOG(WARNING) << "Error parsing DidProcessCrash request from browser";
176 return false;
177 }
178
179 bool child_exited;
[email protected]8ecd3aad2009-11-04 08:32:22180 bool did_crash;
181 if (g_suid_sandbox_active)
182 child = real_pids_to_sandbox_pids[child];
183 if (child)
184 did_crash = base::DidProcessCrash(&child_exited, child);
185 else
186 did_crash = child_exited = false;
[email protected]57113ea2009-06-18 02:23:16187
188 Pickle write_pickle;
189 write_pickle.WriteBool(did_crash);
190 write_pickle.WriteBool(child_exited);
191 HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size()));
192
193 return false;
194 }
195
[email protected]cc8f1462009-06-12 17:36:55196 // Handle a 'fork' request from the browser: this means that the browser
197 // wishes to start a new renderer.
[email protected]8ecd3aad2009-11-04 08:32:22198 bool HandleForkRequest(int fd, const Pickle& pickle, void* iter,
[email protected]cc8f1462009-06-12 17:36:55199 std::vector<int>& fds) {
200 std::vector<std::string> args;
201 int argc, numfds;
202 base::GlobalDescriptors::Mapping mapping;
[email protected]8ecd3aad2009-11-04 08:32:22203 base::ProcessId child;
204 uint64_t dummy_inode = 0;
205 int dummy_fd = -1;
[email protected]cc8f1462009-06-12 17:36:55206
207 if (!pickle.ReadInt(&iter, &argc))
208 goto error;
209
210 for (int i = 0; i < argc; ++i) {
211 std::string arg;
212 if (!pickle.ReadString(&iter, &arg))
213 goto error;
214 args.push_back(arg);
215 }
216
217 if (!pickle.ReadInt(&iter, &numfds))
218 goto error;
219 if (numfds != static_cast<int>(fds.size()))
220 goto error;
221
222 for (int i = 0; i < numfds; ++i) {
223 base::GlobalDescriptors::Key key;
224 if (!pickle.ReadUInt32(&iter, &key))
225 goto error;
226 mapping.push_back(std::make_pair(key, fds[i]));
227 }
228
[email protected]abe3ad92009-06-15 18:15:08229 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22230 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
231
232 if (g_suid_sandbox_active) {
233 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
234 if (dummy_fd < 0)
235 goto error;
236
237 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd))
238 goto error;
239 }
[email protected]abe3ad92009-06-15 18:15:08240
[email protected]cc8f1462009-06-12 17:36:55241 child = fork();
242
243 if (!child) {
[email protected]8015de32009-11-18 04:13:33244#if defined(ARCH_CPU_X86_FAMILY)
[email protected]a9c54a172009-11-07 06:09:38245 // Try to open /proc/self/maps as the seccomp sandbox needs access to it
246 if (g_proc_fd >= 0) {
247 int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY);
248 if (proc_self_maps >= 0) {
249 SeccompSandboxSetProcSelfMaps(proc_self_maps);
250 }
251 close(g_proc_fd);
252 g_proc_fd = -1;
253 }
[email protected]8015de32009-11-18 04:13:33254#endif
[email protected]a9c54a172009-11-07 06:09:38255
[email protected]8ecd3aad2009-11-04 08:32:22256 close(kBrowserDescriptor); // our socket from the browser
[email protected]cbcf9cc32010-02-17 04:05:59257 if (g_suid_sandbox_active)
258 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]cc8f1462009-06-12 17:36:55259 Singleton<base::GlobalDescriptors>()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39260
261 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55262 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39263 CommandLine::Init(0, NULL);
264 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]7f113f32009-09-10 18:02:17265 CommandLine::SetProcTitle();
[email protected]cc8f1462009-06-12 17:36:55266 return true;
[email protected]8ecd3aad2009-11-04 08:32:22267 } else if (child < 0) {
268 LOG(ERROR) << "Zygote could not fork";
269 goto error;
[email protected]cc8f1462009-06-12 17:36:55270 }
271
[email protected]8ecd3aad2009-11-04 08:32:22272 {
273 base::ProcessId proc_id;
274 if (g_suid_sandbox_active) {
275 close(dummy_fd);
276 dummy_fd = -1;
277 uint8_t reply_buf[512];
278 Pickle request;
279 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
280 request.WriteUInt64(dummy_inode);
[email protected]83a0cbe2009-11-04 04:22:47281
[email protected]8ecd3aad2009-11-04 08:32:22282 const ssize_t r = base::SendRecvMsg(kMagicSandboxIPCDescriptor,
283 reply_buf, sizeof(reply_buf),
284 NULL, request);
285 if (r == -1)
286 goto error;
287
288 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
289 void* iter2 = NULL;
290 if (!reply.ReadInt(&iter2, &proc_id))
291 goto error;
292 real_pids_to_sandbox_pids[proc_id] = child;
293 } else {
294 proc_id = child;
295 }
296
297 for (std::vector<int>::const_iterator
298 i = fds.begin(); i != fds.end(); ++i)
299 close(*i);
300
301 HANDLE_EINTR(write(fd, &proc_id, sizeof(proc_id)));
302 return false;
303 }
[email protected]83a0cbe2009-11-04 04:22:47304
305 error:
[email protected]8ecd3aad2009-11-04 08:32:22306 LOG(ERROR) << "Error parsing fork request from browser";
[email protected]83a0cbe2009-11-04 04:22:47307 for (std::vector<int>::const_iterator
308 i = fds.begin(); i != fds.end(); ++i)
309 close(*i);
[email protected]8ecd3aad2009-11-04 08:32:22310 if (dummy_fd >= 0)
311 close(dummy_fd);
[email protected]cc8f1462009-06-12 17:36:55312 return false;
313 }
[email protected]8ecd3aad2009-11-04 08:32:22314
315 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
316 // fork() returns are not the real PIDs, so we need to map the Real PIDS
317 // into the sandbox PID namespace.
318 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
319 ProcessMap real_pids_to_sandbox_pids;
[email protected]cc8f1462009-06-12 17:36:55320};
321
[email protected]ad6d2c42009-09-15 20:13:38322// With SELinux we can carve out a precise sandbox, so we don't have to play
323// with intercepting libc calls.
324#if !defined(CHROMIUM_SELINUX)
325
[email protected]a0f200ea2009-08-06 18:44:06326static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
327 char* timezone_out,
328 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07329 Pickle request;
330 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
331 request.WriteString(
332 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
333
334 uint8_t reply_buf[512];
335 const ssize_t r = base::SendRecvMsg(
336 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
337 if (r == -1) {
338 memset(output, 0, sizeof(struct tm));
339 return;
340 }
341
342 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
343 void* iter = NULL;
[email protected]9debcda52009-07-22 20:06:51344 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07345 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51346 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07347 result.size() != sizeof(struct tm)) {
348 memset(output, 0, sizeof(struct tm));
349 return;
350 }
351
352 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51353 if (timezone_out_len) {
354 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
355 memcpy(timezone_out, timezone.data(), copy_len);
356 timezone_out[copy_len] = 0;
357 output->tm_zone = timezone_out;
358 } else {
359 output->tm_zone = NULL;
360 }
[email protected]73fa63992009-07-20 20:30:07361}
362
[email protected]a0f200ea2009-08-06 18:44:06363static bool g_am_zygote_or_renderer = false;
364
365// Sandbox interception of libc calls.
366//
367// Because we are running in a sandbox certain libc calls will fail (localtime
368// being the motivating example - it needs to read /etc/localtime). We need to
369// intercept these calls and proxy them to the browser. However, these calls
370// may come from us or from our libraries. In some cases we can't just change
371// our code.
372//
373// It's for these cases that we have the following setup:
374//
375// We define global functions for those functions which we wish to override.
376// Since we will be first in the dynamic resolution order, the dynamic linker
377// will point callers to our versions of these functions. However, we have the
378// same binary for both the browser and the renderers, which means that our
379// overrides will apply in the browser too.
380//
381// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
382// renderer process. It's set in ZygoteMain and inherited by the renderers when
383// they fork. (This means that it'll be incorrect for global constructor
384// functions and before ZygoteMain is called - beware).
385//
386// Our replacement functions can check this global and either proxy
387// the call to the browser over the sandbox IPC
388// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
389// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
390// current module.
391//
392// Other avenues:
393//
394// Our first attempt involved some assembly to patch the GOT of the current
395// module. This worked, but was platform specific and doesn't catch the case
396// where a library makes a call rather than current module.
397//
398// We also considered patching the function in place, but this would again by
399// platform specific and the above technique seems to work well enough.
400
[email protected]a5d7bb82009-09-08 22:30:58401static void WarnOnceAboutBrokenDlsym();
402
[email protected]73fa63992009-07-20 20:30:07403struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06404 if (g_am_zygote_or_renderer) {
405 static struct tm time_struct;
406 static char timezone_string[64];
407 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
408 sizeof(timezone_string));
409 return &time_struct;
410 } else {
411 typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
412 static LocaltimeFunction libc_localtime;
[email protected]a5d7bb82009-09-08 22:30:58413 static bool have_libc_localtime = false;
414 if (!have_libc_localtime) {
[email protected]a0f200ea2009-08-06 18:44:06415 libc_localtime = (LocaltimeFunction) dlsym(RTLD_NEXT, "localtime");
[email protected]a5d7bb82009-09-08 22:30:58416 have_libc_localtime = true;
417 }
418
419 if (!libc_localtime) {
420 // http://code.google.com/p/chromium/issues/detail?id=16800
421 //
422 // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces
423 // it with a version which doesn't work. In this case we'll get a NULL
424 // result. There's not a lot we can do at this point, so we just bodge it!
425 WarnOnceAboutBrokenDlsym();
426
427 return gmtime(timep);
428 }
[email protected]a0f200ea2009-08-06 18:44:06429
430 return libc_localtime(timep);
431 }
[email protected]73fa63992009-07-20 20:30:07432}
433
434struct tm* localtime_r(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06435 if (g_am_zygote_or_renderer) {
436 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
437 return result;
438 } else {
439 typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
440 struct tm* result);
441 static LocaltimeRFunction libc_localtime_r;
[email protected]a5d7bb82009-09-08 22:30:58442 static bool have_libc_localtime_r = false;
443 if (!have_libc_localtime_r) {
[email protected]a0f200ea2009-08-06 18:44:06444 libc_localtime_r = (LocaltimeRFunction) dlsym(RTLD_NEXT, "localtime_r");
[email protected]a5d7bb82009-09-08 22:30:58445 have_libc_localtime_r = true;
446 }
447
448 if (!libc_localtime_r) {
449 // See |localtime|, above.
450 WarnOnceAboutBrokenDlsym();
451
452 return gmtime_r(timep, result);
453 }
[email protected]a0f200ea2009-08-06 18:44:06454
455 return libc_localtime_r(timep, result);
456 }
[email protected]73fa63992009-07-20 20:30:07457}
458
[email protected]a5d7bb82009-09-08 22:30:58459// See the comments at the callsite in |localtime| about this function.
460static void WarnOnceAboutBrokenDlsym() {
461 static bool have_shown_warning = false;
462 if (!have_shown_warning) {
463 LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been "
464 "reported to be caused by Nvidia's libGL. You should expect "
465 "time related functions to misbehave. "
466 "http://code.google.com/p/chromium/issues/detail?id=16800";
467 have_shown_warning = true;
468 }
469}
[email protected]ad6d2c42009-09-15 20:13:38470#endif // !CHROMIUM_SELINUX
[email protected]a5d7bb82009-09-08 22:30:58471
[email protected]ad6d2c42009-09-15 20:13:38472// This function triggers the static and lazy construction of objects that need
473// to be created before imposing the sandbox.
474static void PreSandboxInit() {
[email protected]4378a822009-07-08 01:15:14475 base::RandUint64();
476
[email protected]80a086c52009-08-04 17:52:04477 base::SysInfo::MaxSharedMemorySize();
478
[email protected]e6acd672009-07-24 21:51:33479 // To make wcstombs/mbstowcs work in a renderer, setlocale() has to be
480 // called before the sandbox is triggered. It's possible to avoid calling
481 // setlocale() by pulling out the conversion between FilePath and
482 // WebCore String out of the renderer and using string16 in place of
483 // FilePath for IPC.
484 const char* locale = setlocale(LC_ALL, "");
485 LOG_IF(WARNING, locale == NULL) << "setlocale failed.";
486
[email protected]1831acf2009-10-05 16:38:41487 // ICU DateFormat class (used in base/time_format.cc) needs to get the
488 // Olson timezone ID by accessing the zoneinfo files on disk. After
489 // TimeZone::createDefault is called once here, the timezone ID is
490 // cached and there's no more need to access the file system.
491 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
492
[email protected]c9e45da02009-08-05 17:35:08493 FilePath module_path;
494 if (PathService::Get(base::DIR_MODULE, &module_path))
495 media::InitializeMediaLibrary(module_path);
[email protected]ad6d2c42009-09-15 20:13:38496}
497
498#if !defined(CHROMIUM_SELINUX)
499static bool EnterSandbox() {
500 const char* const sandbox_fd_string = getenv("SBX_D");
501 if (sandbox_fd_string) {
502 // The SUID sandbox sets this environment variable to a file descriptor
503 // over which we can signal that we have completed our startup and can be
504 // chrooted.
505
[email protected]8ecd3aad2009-11-04 08:32:22506 g_suid_sandbox_active = true;
507
[email protected]ad6d2c42009-09-15 20:13:38508 char* endptr;
509 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
510 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
511 return false;
512 const int fd = fd_long;
513
514 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08515
[email protected]abe3ad92009-06-15 18:15:08516 static const char kChrootMe = 'C';
517 static const char kChrootMeSuccess = 'O';
518
[email protected]0e1611122009-07-10 18:17:32519 if (HANDLE_EINTR(write(fd, &kChrootMe, 1)) != 1) {
520 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08521 return false;
[email protected]0e1611122009-07-10 18:17:32522 }
[email protected]abe3ad92009-06-15 18:15:08523
[email protected]87ed6952009-07-16 02:52:15524 // We need to reap the chroot helper process in any event:
525 wait(NULL);
526
[email protected]abe3ad92009-06-15 18:15:08527 char reply;
[email protected]87f8ce62009-07-10 19:14:31528 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32529 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08530 return false;
[email protected]0e1611122009-07-10 18:17:32531 }
[email protected]87f8ce62009-07-10 19:14:31532
[email protected]0e1611122009-07-10 18:17:32533 if (reply != kChrootMeSuccess) {
534 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08535 return false;
[email protected]0e1611122009-07-10 18:17:32536 }
[email protected]abe3ad92009-06-15 18:15:08537
[email protected]abe3ad92009-06-15 18:15:08538 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
539
[email protected]415493be2009-07-10 17:50:24540 // Previously, we required that the binary be non-readable. This causes the
541 // kernel to mark the process as non-dumpable at startup. The thinking was
542 // that, although we were putting the renderers into a PID namespace (with
543 // the SUID sandbox), they would nonetheless be in the /same/ PID
544 // namespace. So they could ptrace each other unless they were non-dumpable.
545 //
546 // If the binary was readable, then there would be a window between process
547 // startup and the point where we set the non-dumpable flag in which a
548 // compromised renderer could ptrace attach.
549 //
550 // However, now that we have a zygote model, only the (trusted) zygote
551 // exists at this point and we can set the non-dumpable flag which is
552 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48553 //
554 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
555 // issues, one can specify --allow-sandbox-debugging to let the process be
556 // dumpable.
557 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
558 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
559 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
560 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
561 LOG(ERROR) << "Failed to set non-dumpable flag";
562 return false;
563 }
[email protected]0e1611122009-07-10 18:17:32564 }
[email protected]abe3ad92009-06-15 18:15:08565 } else {
566 SkiaFontConfigUseDirectImplementation();
567 }
568
569 return true;
570}
[email protected]ad6d2c42009-09-15 20:13:38571#else // CHROMIUM_SELINUX
572
573static bool EnterSandbox() {
574 PreSandboxInit();
575 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
576
577 security_context_t security_context;
578 if (getcon(&security_context)) {
579 LOG(ERROR) << "Cannot get SELinux context";
580 return false;
581 }
582
583 context_t context = context_new(security_context);
584 context_type_set(context, "chromium_renderer_t");
585 const int r = setcon(context_str(context));
586 context_free(context);
587 freecon(security_context);
588
589 if (r) {
590 LOG(ERROR) << "dynamic transition to type 'chromium_renderer_t' failed. "
591 "(this binary has been built with SELinux support, but maybe "
592 "the policies haven't been loaded into the kernel?";
593 return false;
594 }
595
596 return true;
597}
598
599#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08600
[email protected]cc8f1462009-06-12 17:36:55601bool ZygoteMain(const MainFunctionParams& params) {
[email protected]ad6d2c42009-09-15 20:13:38602#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06603 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38604#endif
[email protected]a0f200ea2009-08-06 18:44:06605
[email protected]8015de32009-11-18 04:13:33606#if defined(ARCH_CPU_X86_FAMILY)
[email protected]a9c54a172009-11-07 06:09:38607 // The seccomp sandbox needs access to files in /proc, which might be denied
608 // after one of the other sandboxes have been started. So, obtain a suitable
609 // file handle in advance.
[email protected]e8c916a2009-11-04 17:52:47610 if (CommandLine::ForCurrentProcess()->HasSwitch(
611 switches::kEnableSeccompSandbox)) {
[email protected]a9c54a172009-11-07 06:09:38612 g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY);
613 if (g_proc_fd < 0) {
614 LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp "
615 "sandboxing.";
616 }
617 }
[email protected]8015de32009-11-18 04:13:33618#endif // ARCH_CPU_X86_FAMILY
[email protected]a9c54a172009-11-07 06:09:38619
620 // Turn on the SELinux or SUID sandbox
621 if (!EnterSandbox()) {
622 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
623 << errno << ")";
624 return false;
625 }
626
[email protected]8015de32009-11-18 04:13:33627#if defined(ARCH_CPU_X86_FAMILY)
[email protected]a9c54a172009-11-07 06:09:38628 // The seccomp sandbox will be turned on when the renderers start. But we can
629 // already check if sufficient support is available so that we only need to
630 // print one error message for the entire browser session.
631 if (g_proc_fd >= 0 &&
632 CommandLine::ForCurrentProcess()->HasSwitch(
633 switches::kEnableSeccompSandbox)) {
634 if (!SupportsSeccompSandbox(g_proc_fd)) {
[email protected]e8c916a2009-11-04 17:52:47635 // There are a good number of users who cannot use the seccomp sandbox
636 // (e.g. because their distribution does not enable seccomp mode by
637 // default). While we would prefer to deny execution in this case, it
638 // seems more realistic to continue in degraded mode.
639 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
640 "Seccomp sandbox. Running renderers with Seccomp "
641 "sandboxing disabled.";
642 } else {
643 LOG(INFO) << "Enabling experimental Seccomp sandbox.";
644 }
645 }
[email protected]8015de32009-11-18 04:13:33646#endif // ARCH_CPU_X86_FAMILY
[email protected]e8c916a2009-11-04 17:52:47647
[email protected]cc8f1462009-06-12 17:36:55648 Zygote zygote;
649 return zygote.ProcessRequests();
650}