blob: e1dbf1cbcd39d92be5c23ef7fc644e057c27a93e [file] [log] [blame]
[email protected]13f1d5c2012-03-06 03:10:331// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]cc8f1462009-06-12 17:36:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]c2c68b1f2012-02-25 00:29:155#include "content/browser/zygote_host_impl_linux.h"
[email protected]e63c4d72011-05-31 22:38:296
[email protected]a0f200ea2009-08-06 18:44:067#include <dlfcn.h>
[email protected]a9c54a172009-11-07 06:09:388#include <fcntl.h>
[email protected]724df992010-09-21 18:19:109#include <pthread.h>
[email protected]8ecd3aad2009-11-04 08:32:2210#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
[email protected]789f70c72009-07-24 13:55:4316#include "base/basictypes.h"
[email protected]cc8f1462009-06-12 17:36:5517#include "base/command_line.h"
18#include "base/eintr_wrapper.h"
[email protected]5d91c9e2010-07-28 17:25:2819#include "base/file_path.h"
[email protected]e1d67a882011-08-31 21:11:0420#include "base/file_util.h"
[email protected]cc8f1462009-06-12 17:36:5521#include "base/global_descriptors_posix.h"
[email protected]8ecd3aad2009-11-04 08:32:2222#include "base/hash_tables.h"
23#include "base/linux_util.h"
[email protected]3b63f8f42011-03-28 01:54:1524#include "base/memory/scoped_ptr.h"
[email protected]cc8f1462009-06-12 17:36:5525#include "base/pickle.h"
[email protected]4d36536b2010-08-20 06:23:2726#include "base/process_util.h"
[email protected]4378a822009-07-08 01:15:1427#include "base/rand_util.h"
[email protected]80a086c52009-08-04 17:52:0428#include "base/sys_info.h"
[email protected]8015de32009-11-18 04:13:3329#include "build/build_config.h"
[email protected]4b559b4d2011-04-14 17:37:1430#include "crypto/nss_util.h"
[email protected]acb94722011-03-18 01:33:3431#include "content/common/chrome_descriptors.h"
[email protected]797c3552011-03-17 00:26:1832#include "content/common/font_config_ipc_linux.h"
[email protected]cebc3dc2011-04-18 17:15:0033#include "content/common/pepper_plugin_registry.h"
[email protected]4dd57932011-03-17 06:06:1234#include "content/common/sandbox_methods_linux.h"
[email protected]65f607f2011-08-03 21:08:1235#include "content/common/seccomp_sandbox.h"
[email protected]4dd57932011-03-17 06:06:1236#include "content/common/set_process_title.h"
[email protected]797c3552011-03-17 00:26:1837#include "content/common/unix_domain_socket_posix.h"
[email protected]c08950d22011-10-13 22:20:2938#include "content/public/common/content_switches.h"
[email protected]4573fbd2011-10-31 20:25:1839#include "content/public/common/main_function_params.h"
[email protected]b39ef1cb2011-10-25 04:46:5540#include "content/public/common/result_codes.h"
[email protected]f8ba13d2011-10-25 16:08:5241#include "content/public/common/zygote_fork_delegate_linux.h"
[email protected]cf3ac3972010-12-22 20:02:2942#include "skia/ext/SkFontHost_fontconfig_control.h"
[email protected]1831acf2009-10-05 16:38:4143#include "unicode/timezone.h"
[email protected]e1d67a882011-08-31 21:11:0444#include "ipc/ipc_channel.h"
[email protected]ce2ae442011-07-18 16:13:4145#include "ipc/ipc_switches.h"
[email protected]1831acf2009-10-05 16:38:4146
[email protected]e63c4d72011-05-31 22:38:2947#if defined(OS_LINUX)
48#include <sys/epoll.h>
49#include <sys/prctl.h>
50#include <sys/signal.h>
51#else
52#include <signal.h>
53#endif
54
55#if defined(CHROMIUM_SELINUX)
56#include <selinux/selinux.h>
57#include <selinux/context.h>
58#endif
59
[email protected]cc8f1462009-06-12 17:36:5560// http://code.google.com/p/chromium/wiki/LinuxZygote
61
[email protected]8ecd3aad2009-11-04 08:32:2262static const int kBrowserDescriptor = 3;
[email protected]73fa63992009-07-20 20:30:0763static const int kMagicSandboxIPCDescriptor = 5;
[email protected]8ecd3aad2009-11-04 08:32:2264static const int kZygoteIdDescriptor = 7;
65static bool g_suid_sandbox_active = false;
[email protected]2bc039a2011-07-29 23:05:4166
[email protected]2bc039a2011-07-29 23:05:4167#if defined(SECCOMP_SANDBOX)
[email protected]2bc039a2011-07-29 23:05:4168static int g_proc_fd = -1;
[email protected]65f607f2011-08-03 21:08:1269#endif
[email protected]2bc039a2011-07-29 23:05:4170
[email protected]a89a55dd2010-04-19 14:51:1371#if defined(CHROMIUM_SELINUX)
72static void SELinuxTransitionToTypeOrDie(const char* type) {
73 security_context_t security_context;
74 if (getcon(&security_context))
75 LOG(FATAL) << "Cannot get SELinux context";
76
77 context_t context = context_new(security_context);
78 context_type_set(context, type);
79 const int r = setcon(context_str(context));
80 context_free(context);
81 freecon(security_context);
82
83 if (r) {
84 LOG(FATAL) << "dynamic transition to type '" << type << "' failed. "
85 "(this binary has been built with SELinux support, but maybe "
86 "the policies haven't been loaded into the kernel?)";
87 }
88}
89#endif // CHROMIUM_SELINUX
90
[email protected]cc8f1462009-06-12 17:36:5591// This is the object which implements the zygote. The ZygoteMain function,
[email protected]61883442010-07-12 15:14:3492// which is called from ChromeMain, simply constructs one of these objects and
93// runs it.
[email protected]cc8f1462009-06-12 17:36:5594class Zygote {
95 public:
[email protected]f8ba13d2011-10-25 16:08:5296 Zygote(int sandbox_flags, content::ZygoteForkDelegate* helper)
[email protected]5abe6302011-12-20 23:44:3297 : sandbox_flags_(sandbox_flags),
98 helper_(helper),
99 initial_uma_sample_(0),
100 initial_uma_boundary_value_(0) {
[email protected]8d5d7612011-10-20 18:23:14101 if (helper_)
102 helper_->InitialUMA(&initial_uma_name_,
103 &initial_uma_sample_,
104 &initial_uma_boundary_value_);
[email protected]715b4f262010-07-13 14:17:28105 }
106
[email protected]cc8f1462009-06-12 17:36:55107 bool ProcessRequests() {
108 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
109 // browser on it.
[email protected]8ecd3aad2009-11-04 08:32:22110 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
[email protected]abe3ad92009-06-15 18:15:08111 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:55112
113 // We need to accept SIGCHLD, even though our handler is a no-op because
114 // otherwise we cannot wait on children. (According to POSIX 2001.)
115 struct sigaction action;
116 memset(&action, 0, sizeof(action));
117 action.sa_handler = SIGCHLDHandler;
118 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
119
[email protected]8ecd3aad2009-11-04 08:32:22120 if (g_suid_sandbox_active) {
121 // Let the ZygoteHost know we are ready to go.
[email protected]8a282712011-08-23 19:28:00122 // The receiving code is in content/browser/zygote_host_linux.cc.
[email protected]8ecd3aad2009-11-04 08:32:22123 std::vector<int> empty;
[email protected]cf3ac3972010-12-22 20:02:29124 bool r = UnixDomainSocket::SendMsg(kBrowserDescriptor, kZygoteMagic,
125 sizeof(kZygoteMagic), empty);
[email protected]bfc672a52011-07-20 07:51:27126#if defined(OS_CHROMEOS)
[email protected]13f1d5c2012-03-06 03:10:33127 LOG_IF(WARNING, !r) << "Sending zygote magic failed";
[email protected]bfc672a52011-07-20 07:51:27128 // Exit normally on chromeos because session manager may send SIGTERM
129 // right after the process starts and it may fail to send zygote magic
130 // number to browser process.
[email protected]a45f1832011-07-20 18:01:15131 if (!r)
132 _exit(content::RESULT_CODE_NORMAL_EXIT);
[email protected]bfc672a52011-07-20 07:51:27133#else
[email protected]8ecd3aad2009-11-04 08:32:22134 CHECK(r) << "Sending zygote magic failed";
[email protected]bfc672a52011-07-20 07:51:27135#endif
[email protected]8ecd3aad2009-11-04 08:32:22136 }
137
[email protected]cc8f1462009-06-12 17:36:55138 for (;;) {
[email protected]c548be22010-03-08 12:55:57139 // This function call can return multiple times, once per fork().
[email protected]8ecd3aad2009-11-04 08:32:22140 if (HandleRequestFromBrowser(kBrowserDescriptor))
[email protected]cc8f1462009-06-12 17:36:55141 return true;
142 }
143 }
144
145 private:
146 // See comment below, where sigaction is called.
147 static void SIGCHLDHandler(int signal) { }
148
149 // ---------------------------------------------------------------------------
150 // Requests from the browser...
151
152 // Read and process a request from the browser. Returns true if we are in a
153 // new process and thus need to unwind back into ChromeMain.
154 bool HandleRequestFromBrowser(int fd) {
155 std::vector<int> fds;
[email protected]2752bf62011-03-23 21:06:06156 static const unsigned kMaxMessageLength = 2048;
[email protected]cc8f1462009-06-12 17:36:55157 char buf[kMaxMessageLength];
[email protected]cf3ac3972010-12-22 20:02:29158 const ssize_t len = UnixDomainSocket::RecvMsg(fd, buf, sizeof(buf), &fds);
[email protected]7ae27b92010-09-24 17:33:36159
160 if (len == 0 || (len == -1 && errno == ECONNRESET)) {
161 // EOF from the browser. We should die.
162 _exit(0);
[email protected]cc8f1462009-06-12 17:36:55163 return false;
164 }
165
[email protected]7ae27b92010-09-24 17:33:36166 if (len == -1) {
167 PLOG(ERROR) << "Error reading message from browser";
[email protected]cc8f1462009-06-12 17:36:55168 return false;
169 }
170
171 Pickle pickle(buf, len);
[email protected]ce208f872012-03-07 20:42:56172 PickleIterator iter(pickle);
[email protected]cc8f1462009-06-12 17:36:55173
174 int kind;
[email protected]57113ea2009-06-18 02:23:16175 if (pickle.ReadInt(&iter, &kind)) {
176 switch (kind) {
[email protected]c2c68b1f2012-02-25 00:29:15177 case ZygoteHostImpl::kCmdFork:
[email protected]c548be22010-03-08 12:55:57178 // This function call can return multiple times, once per fork().
[email protected]57113ea2009-06-18 02:23:16179 return HandleForkRequest(fd, pickle, iter, fds);
[email protected]ce2ae442011-07-18 16:13:41180
[email protected]c2c68b1f2012-02-25 00:29:15181 case ZygoteHostImpl::kCmdReap:
[email protected]57113ea2009-06-18 02:23:16182 if (!fds.empty())
183 break;
[email protected]c548be22010-03-08 12:55:57184 HandleReapRequest(fd, pickle, iter);
185 return false;
[email protected]c2c68b1f2012-02-25 00:29:15186 case ZygoteHostImpl::kCmdGetTerminationStatus:
[email protected]57113ea2009-06-18 02:23:16187 if (!fds.empty())
188 break;
[email protected]443b80e2010-12-14 00:42:23189 HandleGetTerminationStatus(fd, pickle, iter);
[email protected]c548be22010-03-08 12:55:57190 return false;
[email protected]c2c68b1f2012-02-25 00:29:15191 case ZygoteHostImpl::kCmdGetSandboxStatus:
[email protected]715b4f262010-07-13 14:17:28192 HandleGetSandboxStatus(fd, pickle, iter);
193 return false;
[email protected]57113ea2009-06-18 02:23:16194 default:
195 NOTREACHED();
196 break;
197 }
[email protected]cc8f1462009-06-12 17:36:55198 }
199
[email protected]cc8f1462009-06-12 17:36:55200 LOG(WARNING) << "Error parsing message from browser";
201 for (std::vector<int>::const_iterator
202 i = fds.begin(); i != fds.end(); ++i)
203 close(*i);
204 return false;
205 }
206
[email protected]ce208f872012-03-07 20:42:56207 void HandleReapRequest(int fd, const Pickle& pickle, PickleIterator iter) {
[email protected]8ecd3aad2009-11-04 08:32:22208 base::ProcessId child;
209 base::ProcessId actual_child;
[email protected]cc8f1462009-06-12 17:36:55210
211 if (!pickle.ReadInt(&iter, &child)) {
212 LOG(WARNING) << "Error parsing reap request from browser";
[email protected]c548be22010-03-08 12:55:57213 return;
[email protected]cc8f1462009-06-12 17:36:55214 }
215
[email protected]8ecd3aad2009-11-04 08:32:22216 if (g_suid_sandbox_active) {
217 actual_child = real_pids_to_sandbox_pids[child];
218 if (!actual_child)
[email protected]c548be22010-03-08 12:55:57219 return;
[email protected]8ecd3aad2009-11-04 08:32:22220 real_pids_to_sandbox_pids.erase(child);
221 } else {
222 actual_child = child;
223 }
224
[email protected]eaac71592011-11-23 18:32:00225 base::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55226 }
227
[email protected]ce208f872012-03-07 20:42:56228 void HandleGetTerminationStatus(int fd,
229 const Pickle& pickle,
230 PickleIterator iter) {
[email protected]57113ea2009-06-18 02:23:16231 base::ProcessHandle child;
232
233 if (!pickle.ReadInt(&iter, &child)) {
[email protected]443b80e2010-12-14 00:42:23234 LOG(WARNING) << "Error parsing GetTerminationStatus request "
235 << "from browser";
[email protected]c548be22010-03-08 12:55:57236 return;
[email protected]57113ea2009-06-18 02:23:16237 }
238
[email protected]443b80e2010-12-14 00:42:23239 base::TerminationStatus status;
240 int exit_code;
[email protected]8ecd3aad2009-11-04 08:32:22241 if (g_suid_sandbox_active)
242 child = real_pids_to_sandbox_pids[child];
[email protected]443b80e2010-12-14 00:42:23243 if (child) {
244 status = base::GetTerminationStatus(child, &exit_code);
245 } else {
246 // Assume that if we can't find the child in the sandbox, then
247 // it terminated normally.
248 status = base::TERMINATION_STATUS_NORMAL_TERMINATION;
[email protected]1fcfb202011-07-19 19:53:14249 exit_code = content::RESULT_CODE_NORMAL_EXIT;
[email protected]443b80e2010-12-14 00:42:23250 }
[email protected]57113ea2009-06-18 02:23:16251
252 Pickle write_pickle;
[email protected]443b80e2010-12-14 00:42:23253 write_pickle.WriteInt(static_cast<int>(status));
254 write_pickle.WriteInt(exit_code);
[email protected]8a861402011-01-28 19:59:11255 ssize_t written =
256 HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size()));
257 if (written != static_cast<ssize_t>(write_pickle.size()))
[email protected]19cb9292010-04-16 23:00:15258 PLOG(ERROR) << "write";
[email protected]57113ea2009-06-18 02:23:16259 }
260
[email protected]28ecba32010-09-16 10:03:09261 // This is equivalent to fork(), except that, when using the SUID
262 // sandbox, it returns the real PID of the child process as it
263 // appears outside the sandbox, rather than returning the PID inside
[email protected]8d5d7612011-10-20 18:23:14264 // the sandbox. Optionally, it fills in uma_name et al with a report
265 // the helper wants to make via UMA_HISTOGRAM_ENUMERATION.
[email protected]ce2ae442011-07-18 16:13:41266 int ForkWithRealPid(const std::string& process_type, std::vector<int>& fds,
[email protected]8d5d7612011-10-20 18:23:14267 const std::string& channel_switch,
268 std::string* uma_name,
269 int* uma_sample, int* uma_boundary_value) {
270 const bool use_helper = (helper_ && helper_->CanHelp(process_type,
271 uma_name,
272 uma_sample,
273 uma_boundary_value));
[email protected]ce2ae442011-07-18 16:13:41274 if (!(use_helper || g_suid_sandbox_active)) {
[email protected]28ecba32010-09-16 10:03:09275 return fork();
[email protected]ce2ae442011-07-18 16:13:41276 }
[email protected]28ecba32010-09-16 10:03:09277
278 int dummy_fd;
279 ino_t dummy_inode;
280 int pipe_fds[2] = { -1, -1 };
[email protected]073040b2010-09-26 02:35:45281 base::ProcessId pid = 0;
[email protected]28ecba32010-09-16 10:03:09282
283 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
284 if (dummy_fd < 0) {
285 LOG(ERROR) << "Failed to create dummy FD";
286 goto error;
287 }
288 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd)) {
289 LOG(ERROR) << "Failed to get inode for dummy FD";
290 goto error;
291 }
292 if (pipe(pipe_fds) != 0) {
293 LOG(ERROR) << "Failed to create pipe";
294 goto error;
295 }
296
[email protected]ce2ae442011-07-18 16:13:41297 if (use_helper) {
298 fds.push_back(dummy_fd);
299 fds.push_back(pipe_fds[0]);
300 pid = helper_->Fork(fds);
301 } else {
302 pid = fork();
303 }
[email protected]28ecba32010-09-16 10:03:09304 if (pid < 0) {
305 goto error;
306 } else if (pid == 0) {
307 // In the child process.
308 close(pipe_fds[1]);
[email protected]e1d67a882011-08-31 21:11:04309 base::ProcessId real_pid;
[email protected]28ecba32010-09-16 10:03:09310 // Wait until the parent process has discovered our PID. We
311 // should not fork any child processes (which the seccomp
312 // sandbox does) until then, because that can interfere with the
313 // parent's discovery of our PID.
[email protected]e1d67a882011-08-31 21:11:04314 if (!file_util::ReadFromFD(pipe_fds[0],
315 reinterpret_cast<char*>(&real_pid),
316 sizeof(real_pid))) {
[email protected]28ecba32010-09-16 10:03:09317 LOG(FATAL) << "Failed to synchronise with parent zygote process";
318 }
[email protected]e1d67a882011-08-31 21:11:04319 if (real_pid <= 0) {
320 LOG(FATAL) << "Invalid pid from parent zygote";
321 }
322#if defined(OS_LINUX)
323 // Sandboxed processes need to send the global, non-namespaced PID when
324 // setting up an IPC channel to their parent.
325 IPC::Channel::SetGlobalPid(real_pid);
326#endif
[email protected]28ecba32010-09-16 10:03:09327 close(pipe_fds[0]);
328 close(dummy_fd);
329 return 0;
330 } else {
331 // In the parent process.
332 close(dummy_fd);
333 dummy_fd = -1;
334 close(pipe_fds[0]);
335 pipe_fds[0] = -1;
[email protected]4229b5d2011-07-16 02:47:37336 base::ProcessId real_pid;
[email protected]ce2ae442011-07-18 16:13:41337 if (g_suid_sandbox_active) {
338 uint8_t reply_buf[512];
339 Pickle request;
340 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
341 request.WriteUInt64(dummy_inode);
342
343 const ssize_t r = UnixDomainSocket::SendRecvMsg(
344 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL,
345 request);
346 if (r == -1) {
347 LOG(ERROR) << "Failed to get child process's real PID";
348 goto error;
349 }
350
351 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
[email protected]ce208f872012-03-07 20:42:56352 PickleIterator iter(reply);
[email protected]ce2ae442011-07-18 16:13:41353 if (!reply.ReadInt(&iter, &real_pid))
354 goto error;
355 if (real_pid <= 0) {
356 // METHOD_GET_CHILD_WITH_INODE failed. Did the child die already?
357 LOG(ERROR) << "METHOD_GET_CHILD_WITH_INODE failed";
358 goto error;
359 }
360 real_pids_to_sandbox_pids[real_pid] = pid;
[email protected]4229b5d2011-07-16 02:47:37361 }
[email protected]ce2ae442011-07-18 16:13:41362 if (use_helper) {
363 real_pid = pid;
364 if (!helper_->AckChild(pipe_fds[1], channel_switch)) {
365 LOG(ERROR) << "Failed to synchronise with zygote fork helper";
366 goto error;
367 }
368 } else {
[email protected]e1d67a882011-08-31 21:11:04369 int written =
370 HANDLE_EINTR(write(pipe_fds[1], &real_pid, sizeof(real_pid)));
371 if (written != sizeof(real_pid)) {
[email protected]ce2ae442011-07-18 16:13:41372 LOG(ERROR) << "Failed to synchronise with child process";
373 goto error;
374 }
[email protected]28ecba32010-09-16 10:03:09375 }
376 close(pipe_fds[1]);
377 return real_pid;
378 }
379
380 error:
[email protected]72f891f42011-01-12 17:00:52381 if (pid > 0) {
382 if (waitpid(pid, NULL, WNOHANG) == -1)
383 LOG(ERROR) << "Failed to wait for process";
384 }
[email protected]28ecba32010-09-16 10:03:09385 if (dummy_fd >= 0)
386 close(dummy_fd);
387 if (pipe_fds[0] >= 0)
388 close(pipe_fds[0]);
389 if (pipe_fds[1] >= 0)
390 close(pipe_fds[1]);
391 return -1;
392 }
393
[email protected]35d86242011-09-30 15:29:08394 // Unpacks process type and arguments from |pickle| and forks a new process.
395 // Returns -1 on error, otherwise returns twice, returning 0 to the child
396 // process and the child process ID to the parent process, like fork().
397 base::ProcessId ReadArgsAndFork(const Pickle& pickle,
[email protected]ce208f872012-03-07 20:42:56398 PickleIterator iter,
[email protected]8d5d7612011-10-20 18:23:14399 std::vector<int>& fds,
400 std::string* uma_name,
401 int* uma_sample,
402 int* uma_boundary_value) {
[email protected]cc8f1462009-06-12 17:36:55403 std::vector<std::string> args;
[email protected]35d86242011-09-30 15:29:08404 int argc = 0;
405 int numfds = 0;
[email protected]cc8f1462009-06-12 17:36:55406 base::GlobalDescriptors::Mapping mapping;
[email protected]ce2ae442011-07-18 16:13:41407 std::string process_type;
408 std::string channel_id;
409 const std::string channel_id_prefix = std::string("--")
410 + switches::kProcessChannelID + std::string("=");
411
412 if (!pickle.ReadString(&iter, &process_type))
[email protected]35d86242011-09-30 15:29:08413 return -1;
[email protected]cc8f1462009-06-12 17:36:55414 if (!pickle.ReadInt(&iter, &argc))
[email protected]35d86242011-09-30 15:29:08415 return -1;
[email protected]cc8f1462009-06-12 17:36:55416
417 for (int i = 0; i < argc; ++i) {
418 std::string arg;
419 if (!pickle.ReadString(&iter, &arg))
[email protected]35d86242011-09-30 15:29:08420 return -1;
[email protected]cc8f1462009-06-12 17:36:55421 args.push_back(arg);
[email protected]ce2ae442011-07-18 16:13:41422 if (arg.compare(0, channel_id_prefix.length(), channel_id_prefix) == 0)
423 channel_id = arg;
[email protected]cc8f1462009-06-12 17:36:55424 }
425
426 if (!pickle.ReadInt(&iter, &numfds))
[email protected]35d86242011-09-30 15:29:08427 return -1;
[email protected]cc8f1462009-06-12 17:36:55428 if (numfds != static_cast<int>(fds.size()))
[email protected]35d86242011-09-30 15:29:08429 return -1;
[email protected]cc8f1462009-06-12 17:36:55430
431 for (int i = 0; i < numfds; ++i) {
432 base::GlobalDescriptors::Key key;
433 if (!pickle.ReadUInt32(&iter, &key))
[email protected]35d86242011-09-30 15:29:08434 return -1;
[email protected]cc8f1462009-06-12 17:36:55435 mapping.push_back(std::make_pair(key, fds[i]));
436 }
437
[email protected]abe3ad92009-06-15 18:15:08438 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22439 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
440
[email protected]35d86242011-09-30 15:29:08441 // Returns twice, once per process.
[email protected]8d5d7612011-10-20 18:23:14442 base::ProcessId child_pid = ForkWithRealPid(process_type, fds, channel_id,
443 uma_name, uma_sample,
444 uma_boundary_value);
[email protected]35d86242011-09-30 15:29:08445 if (!child_pid) {
446 // This is the child process.
[email protected]36ea6c6f2010-03-17 20:08:01447#if defined(SECCOMP_SANDBOX)
[email protected]65f607f2011-08-03 21:08:12448 if (SeccompSandboxEnabled() && g_proc_fd >= 0) {
[email protected]2bc039a2011-07-29 23:05:41449 // Try to open /proc/self/maps as the seccomp sandbox needs access to it
[email protected]a9c54a172009-11-07 06:09:38450 int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY);
451 if (proc_self_maps >= 0) {
452 SeccompSandboxSetProcSelfMaps(proc_self_maps);
[email protected]2bc039a2011-07-29 23:05:41453 } else {
454 PLOG(ERROR) << "openat(/proc/self/maps)";
[email protected]a9c54a172009-11-07 06:09:38455 }
456 close(g_proc_fd);
457 g_proc_fd = -1;
458 }
[email protected]8015de32009-11-18 04:13:33459#endif
[email protected]a9c54a172009-11-07 06:09:38460
[email protected]8ecd3aad2009-11-04 08:32:22461 close(kBrowserDescriptor); // our socket from the browser
[email protected]cbcf9cc32010-02-17 04:05:59462 if (g_suid_sandbox_active)
463 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]9b85081af2010-12-07 21:26:47464 base::GlobalDescriptors::GetInstance()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39465
[email protected]a89a55dd2010-04-19 14:51:13466#if defined(CHROMIUM_SELINUX)
467 SELinuxTransitionToTypeOrDie("chromium_renderer_t");
468#endif
469
[email protected]0189bbd2009-10-12 22:50:39470 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55471 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39472 CommandLine::Init(0, NULL);
473 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]74e9fa22010-12-29 21:06:43474
475 // Update the process title. The argv was already cached by the call to
476 // SetProcessTitleFromCommandLine in ChromeMain, so we can pass NULL here
477 // (we don't have the original argv at this point).
478 SetProcessTitleFromCommandLine(NULL);
[email protected]35d86242011-09-30 15:29:08479 } else if (child_pid < 0) {
480 LOG(ERROR) << "Zygote could not fork: process_type " << process_type
481 << " numfds " << numfds << " child_pid " << child_pid;
[email protected]cc8f1462009-06-12 17:36:55482 }
[email protected]35d86242011-09-30 15:29:08483 return child_pid;
484 }
[email protected]cc8f1462009-06-12 17:36:55485
[email protected]35d86242011-09-30 15:29:08486 // Handle a 'fork' request from the browser: this means that the browser
487 // wishes to start a new renderer. Returns true if we are in a new process,
488 // otherwise writes the child_pid back to the browser via |fd|. Writes a
489 // child_pid of -1 on error.
490 bool HandleForkRequest(int fd, const Pickle& pickle,
[email protected]ce208f872012-03-07 20:42:56491 PickleIterator iter, std::vector<int>& fds) {
[email protected]8d5d7612011-10-20 18:23:14492 std::string uma_name;
493 int uma_sample;
494 int uma_boundary_value;
495 base::ProcessId child_pid = ReadArgsAndFork(pickle, iter, fds,
496 &uma_name, &uma_sample,
497 &uma_boundary_value);
[email protected]35d86242011-09-30 15:29:08498 if (child_pid == 0)
499 return true;
[email protected]28ecba32010-09-16 10:03:09500 for (std::vector<int>::const_iterator
501 i = fds.begin(); i != fds.end(); ++i)
502 close(*i);
[email protected]8d5d7612011-10-20 18:23:14503 if (uma_name.empty()) {
504 // There is no UMA report from this particular fork.
505 // Use the initial UMA report if any, and clear that record for next time.
506 // Note the swap method here is the efficient way to do this, since
507 // we know uma_name is empty.
508 uma_name.swap(initial_uma_name_);
509 uma_sample = initial_uma_sample_;
510 uma_boundary_value = initial_uma_boundary_value_;
511 }
[email protected]35d86242011-09-30 15:29:08512 // Must always send reply, as ZygoteHost blocks while waiting for it.
[email protected]8d5d7612011-10-20 18:23:14513 Pickle reply_pickle;
514 reply_pickle.WriteInt(child_pid);
515 reply_pickle.WriteString(uma_name);
516 if (!uma_name.empty()) {
517 reply_pickle.WriteInt(uma_sample);
518 reply_pickle.WriteInt(uma_boundary_value);
519 }
520 if (HANDLE_EINTR(write(fd, reply_pickle.data(), reply_pickle.size())) !=
521 static_cast<ssize_t> (reply_pickle.size()))
[email protected]28ecba32010-09-16 10:03:09522 PLOG(ERROR) << "write";
523 return false;
[email protected]cc8f1462009-06-12 17:36:55524 }
[email protected]8ecd3aad2009-11-04 08:32:22525
[email protected]ce208f872012-03-07 20:42:56526 bool HandleGetSandboxStatus(int fd,
527 const Pickle& pickle,
528 PickleIterator iter) {
[email protected]715b4f262010-07-13 14:17:28529 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_)) !=
530 sizeof(sandbox_flags_))) {
531 PLOG(ERROR) << "write";
532 }
533
534 return false;
535 }
536
[email protected]8ecd3aad2009-11-04 08:32:22537 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
538 // fork() returns are not the real PIDs, so we need to map the Real PIDS
539 // into the sandbox PID namespace.
540 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
541 ProcessMap real_pids_to_sandbox_pids;
[email protected]715b4f262010-07-13 14:17:28542
543 const int sandbox_flags_;
[email protected]f8ba13d2011-10-25 16:08:52544 content::ZygoteForkDelegate* helper_;
[email protected]8d5d7612011-10-20 18:23:14545
546 // These might be set by helper_->InitialUMA. They supply a UMA
547 // enumeration sample we should report on the first fork.
548 std::string initial_uma_name_;
549 int initial_uma_sample_;
550 int initial_uma_boundary_value_;
[email protected]cc8f1462009-06-12 17:36:55551};
552
[email protected]ad6d2c42009-09-15 20:13:38553// With SELinux we can carve out a precise sandbox, so we don't have to play
554// with intercepting libc calls.
555#if !defined(CHROMIUM_SELINUX)
556
[email protected]a0f200ea2009-08-06 18:44:06557static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
558 char* timezone_out,
559 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07560 Pickle request;
561 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
562 request.WriteString(
563 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
564
565 uint8_t reply_buf[512];
[email protected]cf3ac3972010-12-22 20:02:29566 const ssize_t r = UnixDomainSocket::SendRecvMsg(
[email protected]73fa63992009-07-20 20:30:07567 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
568 if (r == -1) {
569 memset(output, 0, sizeof(struct tm));
570 return;
571 }
572
573 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
[email protected]ce208f872012-03-07 20:42:56574 PickleIterator iter(reply);
[email protected]9debcda52009-07-22 20:06:51575 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07576 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51577 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07578 result.size() != sizeof(struct tm)) {
579 memset(output, 0, sizeof(struct tm));
580 return;
581 }
582
583 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51584 if (timezone_out_len) {
585 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
586 memcpy(timezone_out, timezone.data(), copy_len);
587 timezone_out[copy_len] = 0;
588 output->tm_zone = timezone_out;
589 } else {
590 output->tm_zone = NULL;
591 }
[email protected]73fa63992009-07-20 20:30:07592}
593
[email protected]a0f200ea2009-08-06 18:44:06594static bool g_am_zygote_or_renderer = false;
595
596// Sandbox interception of libc calls.
597//
598// Because we are running in a sandbox certain libc calls will fail (localtime
599// being the motivating example - it needs to read /etc/localtime). We need to
600// intercept these calls and proxy them to the browser. However, these calls
601// may come from us or from our libraries. In some cases we can't just change
602// our code.
603//
604// It's for these cases that we have the following setup:
605//
606// We define global functions for those functions which we wish to override.
607// Since we will be first in the dynamic resolution order, the dynamic linker
608// will point callers to our versions of these functions. However, we have the
609// same binary for both the browser and the renderers, which means that our
610// overrides will apply in the browser too.
611//
612// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
613// renderer process. It's set in ZygoteMain and inherited by the renderers when
614// they fork. (This means that it'll be incorrect for global constructor
615// functions and before ZygoteMain is called - beware).
616//
617// Our replacement functions can check this global and either proxy
618// the call to the browser over the sandbox IPC
619// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
620// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
621// current module.
622//
623// Other avenues:
624//
625// Our first attempt involved some assembly to patch the GOT of the current
626// module. This worked, but was platform specific and doesn't catch the case
627// where a library makes a call rather than current module.
628//
629// We also considered patching the function in place, but this would again by
630// platform specific and the above technique seems to work well enough.
631
[email protected]724df992010-09-21 18:19:10632typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
633typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
634 struct tm* result);
635
636static pthread_once_t g_libc_localtime_funcs_guard = PTHREAD_ONCE_INIT;
637static LocaltimeFunction g_libc_localtime;
638static LocaltimeRFunction g_libc_localtime_r;
639
640static void InitLibcLocaltimeFunctions() {
641 g_libc_localtime = reinterpret_cast<LocaltimeFunction>(
642 dlsym(RTLD_NEXT, "localtime"));
643 g_libc_localtime_r = reinterpret_cast<LocaltimeRFunction>(
644 dlsym(RTLD_NEXT, "localtime_r"));
645
646 if (!g_libc_localtime || !g_libc_localtime_r) {
647 // http://code.google.com/p/chromium/issues/detail?id=16800
648 //
649 // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces
650 // it with a version which doesn't work. In this case we'll get a NULL
651 // result. There's not a lot we can do at this point, so we just bodge it!
652 LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been "
653 "reported to be caused by Nvidia's libGL. You should expect"
654 " time related functions to misbehave. "
655 "http://code.google.com/p/chromium/issues/detail?id=16800";
656 }
657
658 if (!g_libc_localtime)
659 g_libc_localtime = gmtime;
660 if (!g_libc_localtime_r)
661 g_libc_localtime_r = gmtime_r;
662}
[email protected]a5d7bb82009-09-08 22:30:58663
[email protected]73fa63992009-07-20 20:30:07664struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06665 if (g_am_zygote_or_renderer) {
666 static struct tm time_struct;
667 static char timezone_string[64];
668 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
669 sizeof(timezone_string));
670 return &time_struct;
671 } else {
[email protected]724df992010-09-21 18:19:10672 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
673 InitLibcLocaltimeFunctions));
674 return g_libc_localtime(timep);
[email protected]a0f200ea2009-08-06 18:44:06675 }
[email protected]73fa63992009-07-20 20:30:07676}
677
678struct tm* localtime_r(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06679 if (g_am_zygote_or_renderer) {
680 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
681 return result;
682 } else {
[email protected]724df992010-09-21 18:19:10683 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
684 InitLibcLocaltimeFunctions));
685 return g_libc_localtime_r(timep, result);
[email protected]a0f200ea2009-08-06 18:44:06686 }
[email protected]73fa63992009-07-20 20:30:07687}
688
[email protected]ad6d2c42009-09-15 20:13:38689#endif // !CHROMIUM_SELINUX
[email protected]a5d7bb82009-09-08 22:30:58690
[email protected]ad6d2c42009-09-15 20:13:38691// This function triggers the static and lazy construction of objects that need
692// to be created before imposing the sandbox.
693static void PreSandboxInit() {
[email protected]b8419412010-03-29 20:18:29694 base::RandUint64();
[email protected]4378a822009-07-08 01:15:14695
[email protected]b8419412010-03-29 20:18:29696 base::SysInfo::MaxSharedMemorySize();
[email protected]80a086c52009-08-04 17:52:04697
[email protected]b8419412010-03-29 20:18:29698 // ICU DateFormat class (used in base/time_format.cc) needs to get the
699 // Olson timezone ID by accessing the zoneinfo files on disk. After
700 // TimeZone::createDefault is called once here, the timezone ID is
701 // cached and there's no more need to access the file system.
702 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
[email protected]1831acf2009-10-05 16:38:41703
[email protected]eeb08552011-03-18 11:15:39704#if defined(USE_NSS)
[email protected]f6a67b42011-03-17 23:49:21705 // NSS libraries are loaded before sandbox is activated. This is to allow
706 // successful initialization of NSS which tries to load extra library files.
[email protected]4b559b4d2011-04-14 17:37:14707 crypto::LoadNSSLibraries();
[email protected]1f5548452011-10-06 06:46:15708#elif defined(USE_OPENSSL)
709 // OpenSSL is intentionally not supported in the sandboxed processes, see
710 // http://crbug.com/99163. If that ever changes we'll likely need to init
711 // OpenSSL here (at least, load the library and error strings).
[email protected]eeb08552011-03-18 11:15:39712#else
[email protected]1f5548452011-10-06 06:46:15713 // It's possible that another hypothetical crypto stack would not require
714 // pre-sandbox init, but more likely this is just a build configuration error.
715 #error Which SSL library are you using?
[email protected]f6a67b42011-03-17 23:49:21716#endif
[email protected]ed450f32011-03-16 01:26:49717
[email protected]17773042010-07-28 17:35:07718 // Ensure access to the Pepper plugins before the sandbox is turned on.
719 PepperPluginRegistry::PreloadModules();
[email protected]ad6d2c42009-09-15 20:13:38720}
721
722#if !defined(CHROMIUM_SELINUX)
723static bool EnterSandbox() {
[email protected]b8419412010-03-29 20:18:29724 // The SUID sandbox sets this environment variable to a file descriptor
725 // over which we can signal that we have completed our startup and can be
726 // chrooted.
[email protected]ad6d2c42009-09-15 20:13:38727 const char* const sandbox_fd_string = getenv("SBX_D");
[email protected]ad6d2c42009-09-15 20:13:38728
[email protected]0dc32322010-09-16 09:46:59729 if (sandbox_fd_string) {
730 // Use the SUID sandbox. This still allows the seccomp sandbox to
731 // be enabled by the process later.
[email protected]8ecd3aad2009-11-04 08:32:22732 g_suid_sandbox_active = true;
733
[email protected]ad6d2c42009-09-15 20:13:38734 char* endptr;
735 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
736 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
737 return false;
738 const int fd = fd_long;
739
740 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08741
[email protected]eaebefaf2010-02-23 22:58:18742 static const char kMsgChrootMe = 'C';
743 static const char kMsgChrootSuccessful = 'O';
[email protected]abe3ad92009-06-15 18:15:08744
[email protected]eaebefaf2010-02-23 22:58:18745 if (HANDLE_EINTR(write(fd, &kMsgChrootMe, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32746 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08747 return false;
[email protected]0e1611122009-07-10 18:17:32748 }
[email protected]abe3ad92009-06-15 18:15:08749
[email protected]87ed6952009-07-16 02:52:15750 // We need to reap the chroot helper process in any event:
751 wait(NULL);
752
[email protected]abe3ad92009-06-15 18:15:08753 char reply;
[email protected]87f8ce62009-07-10 19:14:31754 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32755 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08756 return false;
[email protected]0e1611122009-07-10 18:17:32757 }
[email protected]87f8ce62009-07-10 19:14:31758
[email protected]eaebefaf2010-02-23 22:58:18759 if (reply != kMsgChrootSuccessful) {
[email protected]0e1611122009-07-10 18:17:32760 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08761 return false;
[email protected]0e1611122009-07-10 18:17:32762 }
[email protected]abe3ad92009-06-15 18:15:08763
[email protected]cf3ac3972010-12-22 20:02:29764 SkiaFontConfigSetImplementation(
765 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08766
[email protected]e60c0232011-11-11 19:56:35767#if !defined(OS_OPENBSD)
[email protected]415493be2009-07-10 17:50:24768 // Previously, we required that the binary be non-readable. This causes the
769 // kernel to mark the process as non-dumpable at startup. The thinking was
770 // that, although we were putting the renderers into a PID namespace (with
771 // the SUID sandbox), they would nonetheless be in the /same/ PID
772 // namespace. So they could ptrace each other unless they were non-dumpable.
773 //
774 // If the binary was readable, then there would be a window between process
775 // startup and the point where we set the non-dumpable flag in which a
776 // compromised renderer could ptrace attach.
777 //
778 // However, now that we have a zygote model, only the (trusted) zygote
779 // exists at this point and we can set the non-dumpable flag which is
780 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48781 //
782 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
783 // issues, one can specify --allow-sandbox-debugging to let the process be
784 // dumpable.
785 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
786 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
787 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
788 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
789 LOG(ERROR) << "Failed to set non-dumpable flag";
790 return false;
791 }
[email protected]0e1611122009-07-10 18:17:32792 }
[email protected]e60c0232011-11-11 19:56:35793#endif
[email protected]2bc039a2011-07-29 23:05:41794#if defined(SECCOMP_SANDBOX)
[email protected]65f607f2011-08-03 21:08:12795 } else if (SeccompSandboxEnabled()) {
[email protected]0dc32322010-09-16 09:46:59796 PreSandboxInit();
[email protected]cf3ac3972010-12-22 20:02:29797 SkiaFontConfigSetImplementation(
798 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]2bc039a2011-07-29 23:05:41799#endif
[email protected]abe3ad92009-06-15 18:15:08800 } else {
801 SkiaFontConfigUseDirectImplementation();
802 }
803
804 return true;
805}
[email protected]ad6d2c42009-09-15 20:13:38806#else // CHROMIUM_SELINUX
807
808static bool EnterSandbox() {
809 PreSandboxInit();
810 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
[email protected]ad6d2c42009-09-15 20:13:38811 return true;
812}
813
814#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08815
[email protected]4573fbd2011-10-31 20:25:18816bool ZygoteMain(const content::MainFunctionParams& params,
[email protected]f8ba13d2011-10-25 16:08:52817 content::ZygoteForkDelegate* forkdelegate) {
[email protected]ad6d2c42009-09-15 20:13:38818#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06819 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38820#endif
[email protected]a0f200ea2009-08-06 18:44:06821
[email protected]36ea6c6f2010-03-17 20:08:01822#if defined(SECCOMP_SANDBOX)
[email protected]65f607f2011-08-03 21:08:12823 if (SeccompSandboxEnabled()) {
[email protected]2bc039a2011-07-29 23:05:41824 // The seccomp sandbox needs access to files in /proc, which might be denied
825 // after one of the other sandboxes have been started. So, obtain a suitable
826 // file handle in advance.
[email protected]a9c54a172009-11-07 06:09:38827 g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY);
828 if (g_proc_fd < 0) {
829 LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp "
[email protected]2bc039a2011-07-29 23:05:41830 "sandboxing.";
[email protected]a9c54a172009-11-07 06:09:38831 }
832 }
[email protected]36ea6c6f2010-03-17 20:08:01833#endif // SECCOMP_SANDBOX
[email protected]a9c54a172009-11-07 06:09:38834
[email protected]ce2ae442011-07-18 16:13:41835 if (forkdelegate != NULL) {
836 VLOG(1) << "ZygoteMain: initializing fork delegate";
837 forkdelegate->Init(getenv("SBX_D") != NULL, // g_suid_sandbox_active,
838 kBrowserDescriptor,
839 kMagicSandboxIPCDescriptor);
840 } else {
841 VLOG(1) << "ZygoteMain: fork delegate is NULL";
842 }
843
[email protected]a9c54a172009-11-07 06:09:38844 // Turn on the SELinux or SUID sandbox
845 if (!EnterSandbox()) {
846 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
847 << errno << ")";
848 return false;
849 }
850
[email protected]715b4f262010-07-13 14:17:28851 int sandbox_flags = 0;
852 if (getenv("SBX_D"))
[email protected]c2c68b1f2012-02-25 00:29:15853 sandbox_flags |= ZygoteHostImpl::kSandboxSUID;
[email protected]715b4f262010-07-13 14:17:28854 if (getenv("SBX_PID_NS"))
[email protected]c2c68b1f2012-02-25 00:29:15855 sandbox_flags |= ZygoteHostImpl::kSandboxPIDNS;
[email protected]715b4f262010-07-13 14:17:28856 if (getenv("SBX_NET_NS"))
[email protected]c2c68b1f2012-02-25 00:29:15857 sandbox_flags |= ZygoteHostImpl::kSandboxNetNS;
[email protected]715b4f262010-07-13 14:17:28858
[email protected]36ea6c6f2010-03-17 20:08:01859#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38860 // The seccomp sandbox will be turned on when the renderers start. But we can
861 // already check if sufficient support is available so that we only need to
862 // print one error message for the entire browser session.
[email protected]65f607f2011-08-03 21:08:12863 if (g_proc_fd >= 0 && SeccompSandboxEnabled()) {
[email protected]a9c54a172009-11-07 06:09:38864 if (!SupportsSeccompSandbox(g_proc_fd)) {
[email protected]e8c916a2009-11-04 17:52:47865 // There are a good number of users who cannot use the seccomp sandbox
866 // (e.g. because their distribution does not enable seccomp mode by
867 // default). While we would prefer to deny execution in this case, it
868 // seems more realistic to continue in degraded mode.
[email protected]1b5d28f2010-02-18 15:53:36869 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
870 "Seccomp sandbox. Running renderers with Seccomp "
871 "sandboxing disabled.";
[email protected]e8c916a2009-11-04 17:52:47872 } else {
[email protected]0b4610a92011-08-04 18:08:09873 VLOG(1) << "Enabling experimental Seccomp sandbox.";
[email protected]c2c68b1f2012-02-25 00:29:15874 sandbox_flags |= ZygoteHostImpl::kSandboxSeccomp;
[email protected]e8c916a2009-11-04 17:52:47875 }
876 }
[email protected]36ea6c6f2010-03-17 20:08:01877#endif // SECCOMP_SANDBOX
[email protected]e8c916a2009-11-04 17:52:47878
[email protected]ce2ae442011-07-18 16:13:41879 Zygote zygote(sandbox_flags, forkdelegate);
[email protected]c548be22010-03-08 12:55:57880 // This function call can return multiple times, once per fork().
[email protected]cc8f1462009-06-12 17:36:55881 return zygote.ProcessRequests();
882}