blob: 2b9d5fbd3b98fbaa41544d93ff99b79b904c6708 [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]83a0cbe2009-11-04 04:22:476#include <sys/epoll.h>
[email protected]83a0cbe2009-11-04 04:22:477#include <sys/prctl.h>
[email protected]8ecd3aad2009-11-04 08:32:228#include <sys/signal.h>
9#include <sys/socket.h>
10#include <sys/types.h>
[email protected]83a0cbe2009-11-04 04:22:4711#include <sys/wait.h>
[email protected]8ecd3aad2009-11-04 08:32:2212#include <unistd.h>
13
14#if defined(CHROMIUM_SELINUX)
15#include <selinux/selinux.h>
16#include <selinux/context.h>
17#endif
[email protected]cc8f1462009-06-12 17:36:5518
[email protected]789f70c72009-07-24 13:55:4319#include "base/basictypes.h"
[email protected]cc8f1462009-06-12 17:36:5520#include "base/command_line.h"
21#include "base/eintr_wrapper.h"
22#include "base/global_descriptors_posix.h"
[email protected]8ecd3aad2009-11-04 08:32:2223#include "base/hash_tables.h"
24#include "base/linux_util.h"
[email protected]c9e45da02009-08-05 17:35:0825#include "base/path_service.h"
[email protected]cc8f1462009-06-12 17:36:5526#include "base/pickle.h"
[email protected]4378a822009-07-08 01:15:1427#include "base/rand_util.h"
[email protected]1831acf2009-10-05 16:38:4128#include "base/scoped_ptr.h"
[email protected]80a086c52009-08-04 17:52:0429#include "base/sys_info.h"
[email protected]cc8f1462009-06-12 17:36:5530#include "base/unix_domain_socket_posix.h"
31
32#include "chrome/browser/zygote_host_linux.h"
33#include "chrome/common/chrome_descriptors.h"
[email protected]4730db92009-07-22 00:40:4834#include "chrome/common/chrome_switches.h"
[email protected]cc8f1462009-06-12 17:36:5535#include "chrome/common/main_function_params.h"
36#include "chrome/common/process_watcher.h"
[email protected]73fa63992009-07-20 20:30:0737#include "chrome/common/sandbox_methods_linux.h"
[email protected]cc8f1462009-06-12 17:36:5538
[email protected]c9e45da02009-08-05 17:35:0839#include "media/base/media.h"
40
[email protected]abe3ad92009-06-15 18:15:0841#include "skia/ext/SkFontHost_fontconfig_control.h"
42
[email protected]e8c916a2009-11-04 17:52:4743#include "sandbox/linux/seccomp/sandbox.h"
44
[email protected]1831acf2009-10-05 16:38:4145#include "unicode/timezone.h"
46
[email protected]cc8f1462009-06-12 17:36:5547// http://code.google.com/p/chromium/wiki/LinuxZygote
48
[email protected]8ecd3aad2009-11-04 08:32:2249static const int kBrowserDescriptor = 3;
[email protected]73fa63992009-07-20 20:30:0750static const int kMagicSandboxIPCDescriptor = 5;
[email protected]8ecd3aad2009-11-04 08:32:2251static const int kZygoteIdDescriptor = 7;
52static bool g_suid_sandbox_active = false;
[email protected]73fa63992009-07-20 20:30:0753
[email protected]cc8f1462009-06-12 17:36:5554// This is the object which implements the zygote. The ZygoteMain function,
55// which is called from ChromeMain, at the the bottom and simple constructs one
56// of these objects and runs it.
57class Zygote {
58 public:
59 bool ProcessRequests() {
60 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
61 // browser on it.
[email protected]8ecd3aad2009-11-04 08:32:2262 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
[email protected]abe3ad92009-06-15 18:15:0863 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:5564
65 // We need to accept SIGCHLD, even though our handler is a no-op because
66 // otherwise we cannot wait on children. (According to POSIX 2001.)
67 struct sigaction action;
68 memset(&action, 0, sizeof(action));
69 action.sa_handler = SIGCHLDHandler;
70 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
71
[email protected]8ecd3aad2009-11-04 08:32:2272 if (g_suid_sandbox_active) {
73 // Let the ZygoteHost know we are ready to go.
74 // The receiving code is in chrome/browser/zygote_host_linux.cc.
75 std::vector<int> empty;
76 bool r = base::SendMsg(kBrowserDescriptor, kZygoteMagic,
77 sizeof(kZygoteMagic), empty);
78 CHECK(r) << "Sending zygote magic failed";
79 }
80
[email protected]cc8f1462009-06-12 17:36:5581 for (;;) {
[email protected]8ecd3aad2009-11-04 08:32:2282 if (HandleRequestFromBrowser(kBrowserDescriptor))
[email protected]cc8f1462009-06-12 17:36:5583 return true;
84 }
85 }
86
87 private:
88 // See comment below, where sigaction is called.
89 static void SIGCHLDHandler(int signal) { }
90
91 // ---------------------------------------------------------------------------
92 // Requests from the browser...
93
94 // Read and process a request from the browser. Returns true if we are in a
95 // new process and thus need to unwind back into ChromeMain.
96 bool HandleRequestFromBrowser(int fd) {
97 std::vector<int> fds;
[email protected]abe3ad92009-06-15 18:15:0898 static const unsigned kMaxMessageLength = 1024;
[email protected]cc8f1462009-06-12 17:36:5599 char buf[kMaxMessageLength];
100 const ssize_t len = base::RecvMsg(fd, buf, sizeof(buf), &fds);
101 if (len == -1) {
102 LOG(WARNING) << "Error reading message from browser: " << errno;
103 return false;
104 }
105
106 if (len == 0) {
107 // EOF from the browser. We should die.
108 _exit(0);
109 return false;
110 }
111
112 Pickle pickle(buf, len);
113 void* iter = NULL;
114
115 int kind;
[email protected]57113ea2009-06-18 02:23:16116 if (pickle.ReadInt(&iter, &kind)) {
117 switch (kind) {
118 case ZygoteHost::kCmdFork:
119 return HandleForkRequest(fd, pickle, iter, fds);
120 case ZygoteHost::kCmdReap:
121 if (!fds.empty())
122 break;
123 return HandleReapRequest(fd, pickle, iter);
124 case ZygoteHost::kCmdDidProcessCrash:
125 if (!fds.empty())
126 break;
127 return HandleDidProcessCrash(fd, pickle, iter);
128 default:
129 NOTREACHED();
130 break;
131 }
[email protected]cc8f1462009-06-12 17:36:55132 }
133
[email protected]cc8f1462009-06-12 17:36:55134 LOG(WARNING) << "Error parsing message from browser";
135 for (std::vector<int>::const_iterator
136 i = fds.begin(); i != fds.end(); ++i)
137 close(*i);
138 return false;
139 }
140
[email protected]8ecd3aad2009-11-04 08:32:22141 bool HandleReapRequest(int fd, const Pickle& pickle, void* iter) {
142 base::ProcessId child;
143 base::ProcessId actual_child;
[email protected]cc8f1462009-06-12 17:36:55144
145 if (!pickle.ReadInt(&iter, &child)) {
146 LOG(WARNING) << "Error parsing reap request from browser";
147 return false;
148 }
149
[email protected]8ecd3aad2009-11-04 08:32:22150 if (g_suid_sandbox_active) {
151 actual_child = real_pids_to_sandbox_pids[child];
152 if (!actual_child)
153 return false;
154 real_pids_to_sandbox_pids.erase(child);
155 } else {
156 actual_child = child;
157 }
158
159 ProcessWatcher::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55160
161 return false;
162 }
163
[email protected]8ecd3aad2009-11-04 08:32:22164 bool HandleDidProcessCrash(int fd, const Pickle& pickle, void* iter) {
[email protected]57113ea2009-06-18 02:23:16165 base::ProcessHandle child;
166
167 if (!pickle.ReadInt(&iter, &child)) {
168 LOG(WARNING) << "Error parsing DidProcessCrash request from browser";
169 return false;
170 }
171
172 bool child_exited;
[email protected]8ecd3aad2009-11-04 08:32:22173 bool did_crash;
174 if (g_suid_sandbox_active)
175 child = real_pids_to_sandbox_pids[child];
176 if (child)
177 did_crash = base::DidProcessCrash(&child_exited, child);
178 else
179 did_crash = child_exited = false;
[email protected]57113ea2009-06-18 02:23:16180
181 Pickle write_pickle;
182 write_pickle.WriteBool(did_crash);
183 write_pickle.WriteBool(child_exited);
184 HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size()));
185
186 return false;
187 }
188
[email protected]cc8f1462009-06-12 17:36:55189 // Handle a 'fork' request from the browser: this means that the browser
190 // wishes to start a new renderer.
[email protected]8ecd3aad2009-11-04 08:32:22191 bool HandleForkRequest(int fd, const Pickle& pickle, void* iter,
[email protected]cc8f1462009-06-12 17:36:55192 std::vector<int>& fds) {
193 std::vector<std::string> args;
194 int argc, numfds;
195 base::GlobalDescriptors::Mapping mapping;
[email protected]8ecd3aad2009-11-04 08:32:22196 base::ProcessId child;
197 uint64_t dummy_inode = 0;
198 int dummy_fd = -1;
[email protected]cc8f1462009-06-12 17:36:55199
200 if (!pickle.ReadInt(&iter, &argc))
201 goto error;
202
203 for (int i = 0; i < argc; ++i) {
204 std::string arg;
205 if (!pickle.ReadString(&iter, &arg))
206 goto error;
207 args.push_back(arg);
208 }
209
210 if (!pickle.ReadInt(&iter, &numfds))
211 goto error;
212 if (numfds != static_cast<int>(fds.size()))
213 goto error;
214
215 for (int i = 0; i < numfds; ++i) {
216 base::GlobalDescriptors::Key key;
217 if (!pickle.ReadUInt32(&iter, &key))
218 goto error;
219 mapping.push_back(std::make_pair(key, fds[i]));
220 }
221
[email protected]abe3ad92009-06-15 18:15:08222 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22223 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
224
225 if (g_suid_sandbox_active) {
226 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
227 if (dummy_fd < 0)
228 goto error;
229
230 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd))
231 goto error;
232 }
[email protected]abe3ad92009-06-15 18:15:08233
[email protected]cc8f1462009-06-12 17:36:55234 child = fork();
235
236 if (!child) {
[email protected]8ecd3aad2009-11-04 08:32:22237 close(kBrowserDescriptor); // our socket from the browser
238 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]cc8f1462009-06-12 17:36:55239 Singleton<base::GlobalDescriptors>()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39240
241 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55242 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39243 CommandLine::Init(0, NULL);
244 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]7f113f32009-09-10 18:02:17245 CommandLine::SetProcTitle();
[email protected]cc8f1462009-06-12 17:36:55246 return true;
[email protected]8ecd3aad2009-11-04 08:32:22247 } else if (child < 0) {
248 LOG(ERROR) << "Zygote could not fork";
249 goto error;
[email protected]cc8f1462009-06-12 17:36:55250 }
251
[email protected]8ecd3aad2009-11-04 08:32:22252 {
253 base::ProcessId proc_id;
254 if (g_suid_sandbox_active) {
255 close(dummy_fd);
256 dummy_fd = -1;
257 uint8_t reply_buf[512];
258 Pickle request;
259 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
260 request.WriteUInt64(dummy_inode);
[email protected]83a0cbe2009-11-04 04:22:47261
[email protected]8ecd3aad2009-11-04 08:32:22262 const ssize_t r = base::SendRecvMsg(kMagicSandboxIPCDescriptor,
263 reply_buf, sizeof(reply_buf),
264 NULL, request);
265 if (r == -1)
266 goto error;
267
268 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
269 void* iter2 = NULL;
270 if (!reply.ReadInt(&iter2, &proc_id))
271 goto error;
272 real_pids_to_sandbox_pids[proc_id] = child;
273 } else {
274 proc_id = child;
275 }
276
277 for (std::vector<int>::const_iterator
278 i = fds.begin(); i != fds.end(); ++i)
279 close(*i);
280
281 HANDLE_EINTR(write(fd, &proc_id, sizeof(proc_id)));
282 return false;
283 }
[email protected]83a0cbe2009-11-04 04:22:47284
285 error:
[email protected]8ecd3aad2009-11-04 08:32:22286 LOG(ERROR) << "Error parsing fork request from browser";
[email protected]83a0cbe2009-11-04 04:22:47287 for (std::vector<int>::const_iterator
288 i = fds.begin(); i != fds.end(); ++i)
289 close(*i);
[email protected]8ecd3aad2009-11-04 08:32:22290 if (dummy_fd >= 0)
291 close(dummy_fd);
[email protected]cc8f1462009-06-12 17:36:55292 return false;
293 }
[email protected]8ecd3aad2009-11-04 08:32:22294
295 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
296 // fork() returns are not the real PIDs, so we need to map the Real PIDS
297 // into the sandbox PID namespace.
298 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
299 ProcessMap real_pids_to_sandbox_pids;
[email protected]cc8f1462009-06-12 17:36:55300};
301
[email protected]ad6d2c42009-09-15 20:13:38302// With SELinux we can carve out a precise sandbox, so we don't have to play
303// with intercepting libc calls.
304#if !defined(CHROMIUM_SELINUX)
305
[email protected]a0f200ea2009-08-06 18:44:06306static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
307 char* timezone_out,
308 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07309 Pickle request;
310 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
311 request.WriteString(
312 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
313
314 uint8_t reply_buf[512];
315 const ssize_t r = base::SendRecvMsg(
316 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
317 if (r == -1) {
318 memset(output, 0, sizeof(struct tm));
319 return;
320 }
321
322 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
323 void* iter = NULL;
[email protected]9debcda52009-07-22 20:06:51324 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07325 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51326 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07327 result.size() != sizeof(struct tm)) {
328 memset(output, 0, sizeof(struct tm));
329 return;
330 }
331
332 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51333 if (timezone_out_len) {
334 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
335 memcpy(timezone_out, timezone.data(), copy_len);
336 timezone_out[copy_len] = 0;
337 output->tm_zone = timezone_out;
338 } else {
339 output->tm_zone = NULL;
340 }
[email protected]73fa63992009-07-20 20:30:07341}
342
[email protected]a0f200ea2009-08-06 18:44:06343static bool g_am_zygote_or_renderer = false;
344
345// Sandbox interception of libc calls.
346//
347// Because we are running in a sandbox certain libc calls will fail (localtime
348// being the motivating example - it needs to read /etc/localtime). We need to
349// intercept these calls and proxy them to the browser. However, these calls
350// may come from us or from our libraries. In some cases we can't just change
351// our code.
352//
353// It's for these cases that we have the following setup:
354//
355// We define global functions for those functions which we wish to override.
356// Since we will be first in the dynamic resolution order, the dynamic linker
357// will point callers to our versions of these functions. However, we have the
358// same binary for both the browser and the renderers, which means that our
359// overrides will apply in the browser too.
360//
361// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
362// renderer process. It's set in ZygoteMain and inherited by the renderers when
363// they fork. (This means that it'll be incorrect for global constructor
364// functions and before ZygoteMain is called - beware).
365//
366// Our replacement functions can check this global and either proxy
367// the call to the browser over the sandbox IPC
368// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
369// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
370// current module.
371//
372// Other avenues:
373//
374// Our first attempt involved some assembly to patch the GOT of the current
375// module. This worked, but was platform specific and doesn't catch the case
376// where a library makes a call rather than current module.
377//
378// We also considered patching the function in place, but this would again by
379// platform specific and the above technique seems to work well enough.
380
[email protected]a5d7bb82009-09-08 22:30:58381static void WarnOnceAboutBrokenDlsym();
382
[email protected]73fa63992009-07-20 20:30:07383struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06384 if (g_am_zygote_or_renderer) {
385 static struct tm time_struct;
386 static char timezone_string[64];
387 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
388 sizeof(timezone_string));
389 return &time_struct;
390 } else {
391 typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
392 static LocaltimeFunction libc_localtime;
[email protected]a5d7bb82009-09-08 22:30:58393 static bool have_libc_localtime = false;
394 if (!have_libc_localtime) {
[email protected]a0f200ea2009-08-06 18:44:06395 libc_localtime = (LocaltimeFunction) dlsym(RTLD_NEXT, "localtime");
[email protected]a5d7bb82009-09-08 22:30:58396 have_libc_localtime = true;
397 }
398
399 if (!libc_localtime) {
400 // http://code.google.com/p/chromium/issues/detail?id=16800
401 //
402 // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces
403 // it with a version which doesn't work. In this case we'll get a NULL
404 // result. There's not a lot we can do at this point, so we just bodge it!
405 WarnOnceAboutBrokenDlsym();
406
407 return gmtime(timep);
408 }
[email protected]a0f200ea2009-08-06 18:44:06409
410 return libc_localtime(timep);
411 }
[email protected]73fa63992009-07-20 20:30:07412}
413
414struct tm* localtime_r(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06415 if (g_am_zygote_or_renderer) {
416 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
417 return result;
418 } else {
419 typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
420 struct tm* result);
421 static LocaltimeRFunction libc_localtime_r;
[email protected]a5d7bb82009-09-08 22:30:58422 static bool have_libc_localtime_r = false;
423 if (!have_libc_localtime_r) {
[email protected]a0f200ea2009-08-06 18:44:06424 libc_localtime_r = (LocaltimeRFunction) dlsym(RTLD_NEXT, "localtime_r");
[email protected]a5d7bb82009-09-08 22:30:58425 have_libc_localtime_r = true;
426 }
427
428 if (!libc_localtime_r) {
429 // See |localtime|, above.
430 WarnOnceAboutBrokenDlsym();
431
432 return gmtime_r(timep, result);
433 }
[email protected]a0f200ea2009-08-06 18:44:06434
435 return libc_localtime_r(timep, result);
436 }
[email protected]73fa63992009-07-20 20:30:07437}
438
[email protected]a5d7bb82009-09-08 22:30:58439// See the comments at the callsite in |localtime| about this function.
440static void WarnOnceAboutBrokenDlsym() {
441 static bool have_shown_warning = false;
442 if (!have_shown_warning) {
443 LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been "
444 "reported to be caused by Nvidia's libGL. You should expect "
445 "time related functions to misbehave. "
446 "http://code.google.com/p/chromium/issues/detail?id=16800";
447 have_shown_warning = true;
448 }
449}
[email protected]ad6d2c42009-09-15 20:13:38450#endif // !CHROMIUM_SELINUX
[email protected]a5d7bb82009-09-08 22:30:58451
[email protected]ad6d2c42009-09-15 20:13:38452// This function triggers the static and lazy construction of objects that need
453// to be created before imposing the sandbox.
454static void PreSandboxInit() {
[email protected]4378a822009-07-08 01:15:14455 base::RandUint64();
456
[email protected]80a086c52009-08-04 17:52:04457 base::SysInfo::MaxSharedMemorySize();
458
[email protected]e6acd672009-07-24 21:51:33459 // To make wcstombs/mbstowcs work in a renderer, setlocale() has to be
460 // called before the sandbox is triggered. It's possible to avoid calling
461 // setlocale() by pulling out the conversion between FilePath and
462 // WebCore String out of the renderer and using string16 in place of
463 // FilePath for IPC.
464 const char* locale = setlocale(LC_ALL, "");
465 LOG_IF(WARNING, locale == NULL) << "setlocale failed.";
466
[email protected]1831acf2009-10-05 16:38:41467 // ICU DateFormat class (used in base/time_format.cc) needs to get the
468 // Olson timezone ID by accessing the zoneinfo files on disk. After
469 // TimeZone::createDefault is called once here, the timezone ID is
470 // cached and there's no more need to access the file system.
471 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
472
[email protected]c9e45da02009-08-05 17:35:08473 FilePath module_path;
474 if (PathService::Get(base::DIR_MODULE, &module_path))
475 media::InitializeMediaLibrary(module_path);
[email protected]ad6d2c42009-09-15 20:13:38476}
477
478#if !defined(CHROMIUM_SELINUX)
479static bool EnterSandbox() {
480 const char* const sandbox_fd_string = getenv("SBX_D");
481 if (sandbox_fd_string) {
482 // The SUID sandbox sets this environment variable to a file descriptor
483 // over which we can signal that we have completed our startup and can be
484 // chrooted.
485
[email protected]8ecd3aad2009-11-04 08:32:22486 g_suid_sandbox_active = true;
487
[email protected]ad6d2c42009-09-15 20:13:38488 char* endptr;
489 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
490 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
491 return false;
492 const int fd = fd_long;
493
494 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08495
[email protected]abe3ad92009-06-15 18:15:08496 static const char kChrootMe = 'C';
497 static const char kChrootMeSuccess = 'O';
498
[email protected]0e1611122009-07-10 18:17:32499 if (HANDLE_EINTR(write(fd, &kChrootMe, 1)) != 1) {
500 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08501 return false;
[email protected]0e1611122009-07-10 18:17:32502 }
[email protected]abe3ad92009-06-15 18:15:08503
[email protected]87ed6952009-07-16 02:52:15504 // We need to reap the chroot helper process in any event:
505 wait(NULL);
506
[email protected]abe3ad92009-06-15 18:15:08507 char reply;
[email protected]87f8ce62009-07-10 19:14:31508 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32509 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08510 return false;
[email protected]0e1611122009-07-10 18:17:32511 }
[email protected]87f8ce62009-07-10 19:14:31512
[email protected]0e1611122009-07-10 18:17:32513 if (reply != kChrootMeSuccess) {
514 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08515 return false;
[email protected]0e1611122009-07-10 18:17:32516 }
[email protected]abe3ad92009-06-15 18:15:08517
[email protected]abe3ad92009-06-15 18:15:08518 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
519
[email protected]415493be2009-07-10 17:50:24520 // Previously, we required that the binary be non-readable. This causes the
521 // kernel to mark the process as non-dumpable at startup. The thinking was
522 // that, although we were putting the renderers into a PID namespace (with
523 // the SUID sandbox), they would nonetheless be in the /same/ PID
524 // namespace. So they could ptrace each other unless they were non-dumpable.
525 //
526 // If the binary was readable, then there would be a window between process
527 // startup and the point where we set the non-dumpable flag in which a
528 // compromised renderer could ptrace attach.
529 //
530 // However, now that we have a zygote model, only the (trusted) zygote
531 // exists at this point and we can set the non-dumpable flag which is
532 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48533 //
534 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
535 // issues, one can specify --allow-sandbox-debugging to let the process be
536 // dumpable.
537 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
538 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
539 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
540 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
541 LOG(ERROR) << "Failed to set non-dumpable flag";
542 return false;
543 }
[email protected]0e1611122009-07-10 18:17:32544 }
[email protected]abe3ad92009-06-15 18:15:08545 } else {
546 SkiaFontConfigUseDirectImplementation();
547 }
548
549 return true;
550}
[email protected]ad6d2c42009-09-15 20:13:38551#else // CHROMIUM_SELINUX
552
553static bool EnterSandbox() {
554 PreSandboxInit();
555 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
556
557 security_context_t security_context;
558 if (getcon(&security_context)) {
559 LOG(ERROR) << "Cannot get SELinux context";
560 return false;
561 }
562
563 context_t context = context_new(security_context);
564 context_type_set(context, "chromium_renderer_t");
565 const int r = setcon(context_str(context));
566 context_free(context);
567 freecon(security_context);
568
569 if (r) {
570 LOG(ERROR) << "dynamic transition to type 'chromium_renderer_t' failed. "
571 "(this binary has been built with SELinux support, but maybe "
572 "the policies haven't been loaded into the kernel?";
573 return false;
574 }
575
576 return true;
577}
578
579#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08580
[email protected]cc8f1462009-06-12 17:36:55581bool ZygoteMain(const MainFunctionParams& params) {
[email protected]ad6d2c42009-09-15 20:13:38582#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06583 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38584#endif
[email protected]a0f200ea2009-08-06 18:44:06585
[email protected]e8c916a2009-11-04 17:52:47586 if (CommandLine::ForCurrentProcess()->HasSwitch(
587 switches::kEnableSeccompSandbox)) {
588 if (!SupportsSeccompSandbox()) {
589 // There are a good number of users who cannot use the seccomp sandbox
590 // (e.g. because their distribution does not enable seccomp mode by
591 // default). While we would prefer to deny execution in this case, it
592 // seems more realistic to continue in degraded mode.
593 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
594 "Seccomp sandbox. Running renderers with Seccomp "
595 "sandboxing disabled.";
596 } else {
597 LOG(INFO) << "Enabling experimental Seccomp sandbox.";
598 }
599 }
600
[email protected]ad6d2c42009-09-15 20:13:38601 if (!EnterSandbox()) {
[email protected]abe3ad92009-06-15 18:15:08602 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
603 << errno << ")";
604 return false;
605 }
606
[email protected]cc8f1462009-06-12 17:36:55607 Zygote zygote;
608 return zygote.ProcessRequests();
609}