blob: bf72711d2a467d5d056159f9c365fba3eabb85b4 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 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]e63c4d72011-05-31 22:38:295#include "content/browser/zygote_host_linux.h"
6
[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]cc8f1462009-06-12 17:36:5520#include "base/global_descriptors_posix.h"
[email protected]8ecd3aad2009-11-04 08:32:2221#include "base/hash_tables.h"
22#include "base/linux_util.h"
[email protected]3b63f8f42011-03-28 01:54:1523#include "base/memory/scoped_ptr.h"
[email protected]cc8f1462009-06-12 17:36:5524#include "base/pickle.h"
[email protected]4d36536b2010-08-20 06:23:2725#include "base/process_util.h"
[email protected]4378a822009-07-08 01:15:1426#include "base/rand_util.h"
[email protected]80a086c52009-08-04 17:52:0427#include "base/sys_info.h"
[email protected]8015de32009-11-18 04:13:3328#include "build/build_config.h"
[email protected]4b559b4d2011-04-14 17:37:1429#include "crypto/nss_util.h"
[email protected]acb94722011-03-18 01:33:3430#include "content/common/chrome_descriptors.h"
[email protected]4287a3d2011-06-13 23:56:5131#include "content/common/content_switches.h"
[email protected]797c3552011-03-17 00:26:1832#include "content/common/font_config_ipc_linux.h"
[email protected]415c2cd2011-03-11 21:56:1133#include "content/common/main_function_params.h"
[email protected]cebc3dc2011-04-18 17:15:0034#include "content/common/pepper_plugin_registry.h"
[email protected]4dd57932011-03-17 06:06:1235#include "content/common/process_watcher.h"
36#include "content/common/result_codes.h"
37#include "content/common/sandbox_methods_linux.h"
38#include "content/common/set_process_title.h"
[email protected]797c3552011-03-17 00:26:1839#include "content/common/unix_domain_socket_posix.h"
[email protected]ce2ae442011-07-18 16:13:4140#include "content/common/zygote_fork_delegate_linux.h"
[email protected]808ea572010-09-01 16:22:0141#include "seccompsandbox/sandbox.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]ce2ae442011-07-18 16:13:4144#include "ipc/ipc_switches.h"
[email protected]1831acf2009-10-05 16:38:4145
[email protected]e63c4d72011-05-31 22:38:2946#if defined(OS_LINUX)
47#include <sys/epoll.h>
48#include <sys/prctl.h>
49#include <sys/signal.h>
50#else
51#include <signal.h>
52#endif
53
54#if defined(CHROMIUM_SELINUX)
55#include <selinux/selinux.h>
56#include <selinux/context.h>
57#endif
58
[email protected]58680ce2010-09-18 00:09:1559#if defined(ARCH_CPU_X86_FAMILY) && !defined(CHROMIUM_SELINUX) && \
60 !defined(__clang__)
[email protected]36ea6c6f2010-03-17 20:08:0161// The seccomp sandbox is enabled on all ia32 and x86-64 processor as long as
[email protected]58680ce2010-09-18 00:09:1562// we aren't using SELinux or clang.
[email protected]36ea6c6f2010-03-17 20:08:0163#define SECCOMP_SANDBOX
64#endif
65
[email protected]cc8f1462009-06-12 17:36:5566// http://code.google.com/p/chromium/wiki/LinuxZygote
67
[email protected]8ecd3aad2009-11-04 08:32:2268static const int kBrowserDescriptor = 3;
[email protected]73fa63992009-07-20 20:30:0769static const int kMagicSandboxIPCDescriptor = 5;
[email protected]8ecd3aad2009-11-04 08:32:2270static const int kZygoteIdDescriptor = 7;
71static bool g_suid_sandbox_active = false;
[email protected]36ea6c6f2010-03-17 20:08:0172#if defined(SECCOMP_SANDBOX)
[email protected]4f03cbc2009-11-19 00:50:4873// |g_proc_fd| is used only by the seccomp sandbox.
[email protected]a9c54a172009-11-07 06:09:3874static int g_proc_fd = -1;
[email protected]4f03cbc2009-11-19 00:50:4875#endif
[email protected]73fa63992009-07-20 20:30:0776
[email protected]a89a55dd2010-04-19 14:51:1377#if defined(CHROMIUM_SELINUX)
78static void SELinuxTransitionToTypeOrDie(const char* type) {
79 security_context_t security_context;
80 if (getcon(&security_context))
81 LOG(FATAL) << "Cannot get SELinux context";
82
83 context_t context = context_new(security_context);
84 context_type_set(context, type);
85 const int r = setcon(context_str(context));
86 context_free(context);
87 freecon(security_context);
88
89 if (r) {
90 LOG(FATAL) << "dynamic transition to type '" << type << "' failed. "
91 "(this binary has been built with SELinux support, but maybe "
92 "the policies haven't been loaded into the kernel?)";
93 }
94}
95#endif // CHROMIUM_SELINUX
96
[email protected]cc8f1462009-06-12 17:36:5597// This is the object which implements the zygote. The ZygoteMain function,
[email protected]61883442010-07-12 15:14:3498// which is called from ChromeMain, simply constructs one of these objects and
99// runs it.
[email protected]cc8f1462009-06-12 17:36:55100class Zygote {
101 public:
[email protected]ce2ae442011-07-18 16:13:41102 explicit Zygote(int sandbox_flags, ZygoteForkDelegate* helper)
103 : sandbox_flags_(sandbox_flags),
104 helper_(helper) {
[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.
122 // The receiving code is in chrome/browser/zygote_host_linux.cc.
123 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)
127 LOG_IF(WARNING, r) << "Sending zygote magic failed";
128 // 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);
172 void* iter = NULL;
173
174 int kind;
[email protected]57113ea2009-06-18 02:23:16175 if (pickle.ReadInt(&iter, &kind)) {
176 switch (kind) {
177 case ZygoteHost::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]57113ea2009-06-18 02:23:16181 case ZygoteHost::kCmdReap:
182 if (!fds.empty())
183 break;
[email protected]c548be22010-03-08 12:55:57184 HandleReapRequest(fd, pickle, iter);
185 return false;
[email protected]443b80e2010-12-14 00:42:23186 case ZygoteHost::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]715b4f262010-07-13 14:17:28191 case ZygoteHost::kCmdGetSandboxStatus:
192 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]c548be22010-03-08 12:55:57207 void HandleReapRequest(int fd, const Pickle& pickle, void* 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
225 ProcessWatcher::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55226 }
227
[email protected]443b80e2010-12-14 00:42:23228 void HandleGetTerminationStatus(int fd, const Pickle& pickle, void* iter) {
[email protected]57113ea2009-06-18 02:23:16229 base::ProcessHandle child;
230
231 if (!pickle.ReadInt(&iter, &child)) {
[email protected]443b80e2010-12-14 00:42:23232 LOG(WARNING) << "Error parsing GetTerminationStatus request "
233 << "from browser";
[email protected]c548be22010-03-08 12:55:57234 return;
[email protected]57113ea2009-06-18 02:23:16235 }
236
[email protected]443b80e2010-12-14 00:42:23237 base::TerminationStatus status;
238 int exit_code;
[email protected]8ecd3aad2009-11-04 08:32:22239 if (g_suid_sandbox_active)
240 child = real_pids_to_sandbox_pids[child];
[email protected]443b80e2010-12-14 00:42:23241 if (child) {
242 status = base::GetTerminationStatus(child, &exit_code);
243 } else {
244 // Assume that if we can't find the child in the sandbox, then
245 // it terminated normally.
246 status = base::TERMINATION_STATUS_NORMAL_TERMINATION;
[email protected]1fcfb202011-07-19 19:53:14247 exit_code = content::RESULT_CODE_NORMAL_EXIT;
[email protected]443b80e2010-12-14 00:42:23248 }
[email protected]57113ea2009-06-18 02:23:16249
250 Pickle write_pickle;
[email protected]443b80e2010-12-14 00:42:23251 write_pickle.WriteInt(static_cast<int>(status));
252 write_pickle.WriteInt(exit_code);
[email protected]8a861402011-01-28 19:59:11253 ssize_t written =
254 HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size()));
255 if (written != static_cast<ssize_t>(write_pickle.size()))
[email protected]19cb9292010-04-16 23:00:15256 PLOG(ERROR) << "write";
[email protected]57113ea2009-06-18 02:23:16257 }
258
[email protected]28ecba32010-09-16 10:03:09259 // This is equivalent to fork(), except that, when using the SUID
260 // sandbox, it returns the real PID of the child process as it
261 // appears outside the sandbox, rather than returning the PID inside
262 // the sandbox.
[email protected]ce2ae442011-07-18 16:13:41263 int ForkWithRealPid(const std::string& process_type, std::vector<int>& fds,
264 const std::string& channel_switch) {
265 const bool use_helper = (helper_ && helper_->CanHelp(process_type));
266 if (!(use_helper || g_suid_sandbox_active)) {
[email protected]28ecba32010-09-16 10:03:09267 return fork();
[email protected]ce2ae442011-07-18 16:13:41268 }
[email protected]28ecba32010-09-16 10:03:09269
270 int dummy_fd;
271 ino_t dummy_inode;
272 int pipe_fds[2] = { -1, -1 };
[email protected]073040b2010-09-26 02:35:45273 base::ProcessId pid = 0;
[email protected]28ecba32010-09-16 10:03:09274
275 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
276 if (dummy_fd < 0) {
277 LOG(ERROR) << "Failed to create dummy FD";
278 goto error;
279 }
280 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd)) {
281 LOG(ERROR) << "Failed to get inode for dummy FD";
282 goto error;
283 }
284 if (pipe(pipe_fds) != 0) {
285 LOG(ERROR) << "Failed to create pipe";
286 goto error;
287 }
288
[email protected]ce2ae442011-07-18 16:13:41289 if (use_helper) {
290 fds.push_back(dummy_fd);
291 fds.push_back(pipe_fds[0]);
292 pid = helper_->Fork(fds);
293 } else {
294 pid = fork();
295 }
[email protected]28ecba32010-09-16 10:03:09296 if (pid < 0) {
297 goto error;
298 } else if (pid == 0) {
299 // In the child process.
300 close(pipe_fds[1]);
301 char buffer[1];
302 // Wait until the parent process has discovered our PID. We
303 // should not fork any child processes (which the seccomp
304 // sandbox does) until then, because that can interfere with the
305 // parent's discovery of our PID.
306 if (HANDLE_EINTR(read(pipe_fds[0], buffer, 1)) != 1 ||
307 buffer[0] != 'x') {
308 LOG(FATAL) << "Failed to synchronise with parent zygote process";
309 }
310 close(pipe_fds[0]);
311 close(dummy_fd);
312 return 0;
313 } else {
314 // In the parent process.
315 close(dummy_fd);
316 dummy_fd = -1;
317 close(pipe_fds[0]);
318 pipe_fds[0] = -1;
[email protected]4229b5d2011-07-16 02:47:37319 base::ProcessId real_pid;
[email protected]ce2ae442011-07-18 16:13:41320 if (g_suid_sandbox_active) {
321 uint8_t reply_buf[512];
322 Pickle request;
323 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
324 request.WriteUInt64(dummy_inode);
325
326 const ssize_t r = UnixDomainSocket::SendRecvMsg(
327 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL,
328 request);
329 if (r == -1) {
330 LOG(ERROR) << "Failed to get child process's real PID";
331 goto error;
332 }
333
334 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
335 void* iter = NULL;
336 if (!reply.ReadInt(&iter, &real_pid))
337 goto error;
338 if (real_pid <= 0) {
339 // METHOD_GET_CHILD_WITH_INODE failed. Did the child die already?
340 LOG(ERROR) << "METHOD_GET_CHILD_WITH_INODE failed";
341 goto error;
342 }
343 real_pids_to_sandbox_pids[real_pid] = pid;
[email protected]4229b5d2011-07-16 02:47:37344 }
[email protected]ce2ae442011-07-18 16:13:41345 if (use_helper) {
346 real_pid = pid;
347 if (!helper_->AckChild(pipe_fds[1], channel_switch)) {
348 LOG(ERROR) << "Failed to synchronise with zygote fork helper";
349 goto error;
350 }
351 } else {
352 if (HANDLE_EINTR(write(pipe_fds[1], "x", 1)) != 1) {
353 LOG(ERROR) << "Failed to synchronise with child process";
354 goto error;
355 }
[email protected]28ecba32010-09-16 10:03:09356 }
357 close(pipe_fds[1]);
358 return real_pid;
359 }
360
361 error:
[email protected]72f891f42011-01-12 17:00:52362 if (pid > 0) {
363 if (waitpid(pid, NULL, WNOHANG) == -1)
364 LOG(ERROR) << "Failed to wait for process";
365 }
[email protected]28ecba32010-09-16 10:03:09366 if (dummy_fd >= 0)
367 close(dummy_fd);
368 if (pipe_fds[0] >= 0)
369 close(pipe_fds[0]);
370 if (pipe_fds[1] >= 0)
371 close(pipe_fds[1]);
372 return -1;
373 }
374
[email protected]cc8f1462009-06-12 17:36:55375 // Handle a 'fork' request from the browser: this means that the browser
376 // wishes to start a new renderer.
[email protected]ce2ae442011-07-18 16:13:41377 bool HandleForkRequest(int fd, const Pickle& pickle,
378 void* iter, std::vector<int>& fds) {
[email protected]cc8f1462009-06-12 17:36:55379 std::vector<std::string> args;
380 int argc, numfds;
381 base::GlobalDescriptors::Mapping mapping;
[email protected]8ecd3aad2009-11-04 08:32:22382 base::ProcessId child;
[email protected]ce2ae442011-07-18 16:13:41383 std::string process_type;
384 std::string channel_id;
385 const std::string channel_id_prefix = std::string("--")
386 + switches::kProcessChannelID + std::string("=");
387
388 if (!pickle.ReadString(&iter, &process_type))
389 goto error;
[email protected]cc8f1462009-06-12 17:36:55390
391 if (!pickle.ReadInt(&iter, &argc))
392 goto error;
393
394 for (int i = 0; i < argc; ++i) {
395 std::string arg;
396 if (!pickle.ReadString(&iter, &arg))
397 goto error;
398 args.push_back(arg);
[email protected]ce2ae442011-07-18 16:13:41399 if (arg.compare(0, channel_id_prefix.length(), channel_id_prefix) == 0)
400 channel_id = arg;
[email protected]cc8f1462009-06-12 17:36:55401 }
402
403 if (!pickle.ReadInt(&iter, &numfds))
404 goto error;
405 if (numfds != static_cast<int>(fds.size()))
406 goto error;
407
408 for (int i = 0; i < numfds; ++i) {
409 base::GlobalDescriptors::Key key;
410 if (!pickle.ReadUInt32(&iter, &key))
411 goto error;
412 mapping.push_back(std::make_pair(key, fds[i]));
413 }
414
[email protected]abe3ad92009-06-15 18:15:08415 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22416 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
417
[email protected]ce2ae442011-07-18 16:13:41418 child = ForkWithRealPid(process_type, fds, channel_id);
[email protected]cc8f1462009-06-12 17:36:55419
420 if (!child) {
[email protected]36ea6c6f2010-03-17 20:08:01421#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38422 // Try to open /proc/self/maps as the seccomp sandbox needs access to it
423 if (g_proc_fd >= 0) {
424 int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY);
425 if (proc_self_maps >= 0) {
426 SeccompSandboxSetProcSelfMaps(proc_self_maps);
427 }
428 close(g_proc_fd);
429 g_proc_fd = -1;
430 }
[email protected]8015de32009-11-18 04:13:33431#endif
[email protected]a9c54a172009-11-07 06:09:38432
[email protected]8ecd3aad2009-11-04 08:32:22433 close(kBrowserDescriptor); // our socket from the browser
[email protected]cbcf9cc32010-02-17 04:05:59434 if (g_suid_sandbox_active)
435 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]9b85081af2010-12-07 21:26:47436 base::GlobalDescriptors::GetInstance()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39437
[email protected]a89a55dd2010-04-19 14:51:13438#if defined(CHROMIUM_SELINUX)
439 SELinuxTransitionToTypeOrDie("chromium_renderer_t");
440#endif
441
[email protected]0189bbd2009-10-12 22:50:39442 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55443 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39444 CommandLine::Init(0, NULL);
445 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]74e9fa22010-12-29 21:06:43446
447 // Update the process title. The argv was already cached by the call to
448 // SetProcessTitleFromCommandLine in ChromeMain, so we can pass NULL here
449 // (we don't have the original argv at this point).
450 SetProcessTitleFromCommandLine(NULL);
451
[email protected]c548be22010-03-08 12:55:57452 // The fork() request is handled further up the call stack.
[email protected]cc8f1462009-06-12 17:36:55453 return true;
[email protected]8ecd3aad2009-11-04 08:32:22454 } else if (child < 0) {
[email protected]466cffd2010-06-09 18:10:56455 LOG(ERROR) << "Zygote could not fork: " << errno;
[email protected]8ecd3aad2009-11-04 08:32:22456 goto error;
[email protected]cc8f1462009-06-12 17:36:55457 }
458
[email protected]28ecba32010-09-16 10:03:09459 for (std::vector<int>::const_iterator
460 i = fds.begin(); i != fds.end(); ++i)
461 close(*i);
[email protected]83a0cbe2009-11-04 04:22:47462
[email protected]28ecba32010-09-16 10:03:09463 if (HANDLE_EINTR(write(fd, &child, sizeof(child))) < 0)
464 PLOG(ERROR) << "write";
465 return false;
[email protected]83a0cbe2009-11-04 04:22:47466
467 error:
[email protected]8ecd3aad2009-11-04 08:32:22468 LOG(ERROR) << "Error parsing fork request from browser";
[email protected]83a0cbe2009-11-04 04:22:47469 for (std::vector<int>::const_iterator
470 i = fds.begin(); i != fds.end(); ++i)
471 close(*i);
[email protected]cc8f1462009-06-12 17:36:55472 return false;
473 }
[email protected]8ecd3aad2009-11-04 08:32:22474
[email protected]715b4f262010-07-13 14:17:28475 bool HandleGetSandboxStatus(int fd, const Pickle& pickle, void* iter) {
476 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_)) !=
477 sizeof(sandbox_flags_))) {
478 PLOG(ERROR) << "write";
479 }
480
481 return false;
482 }
483
[email protected]8ecd3aad2009-11-04 08:32:22484 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
485 // fork() returns are not the real PIDs, so we need to map the Real PIDS
486 // into the sandbox PID namespace.
487 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
488 ProcessMap real_pids_to_sandbox_pids;
[email protected]715b4f262010-07-13 14:17:28489
490 const int sandbox_flags_;
[email protected]ce2ae442011-07-18 16:13:41491 ZygoteForkDelegate* helper_;
[email protected]cc8f1462009-06-12 17:36:55492};
493
[email protected]ad6d2c42009-09-15 20:13:38494// With SELinux we can carve out a precise sandbox, so we don't have to play
495// with intercepting libc calls.
496#if !defined(CHROMIUM_SELINUX)
497
[email protected]a0f200ea2009-08-06 18:44:06498static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
499 char* timezone_out,
500 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07501 Pickle request;
502 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
503 request.WriteString(
504 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
505
506 uint8_t reply_buf[512];
[email protected]cf3ac3972010-12-22 20:02:29507 const ssize_t r = UnixDomainSocket::SendRecvMsg(
[email protected]73fa63992009-07-20 20:30:07508 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
509 if (r == -1) {
510 memset(output, 0, sizeof(struct tm));
511 return;
512 }
513
514 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
515 void* iter = NULL;
[email protected]9debcda52009-07-22 20:06:51516 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07517 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51518 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07519 result.size() != sizeof(struct tm)) {
520 memset(output, 0, sizeof(struct tm));
521 return;
522 }
523
524 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51525 if (timezone_out_len) {
526 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
527 memcpy(timezone_out, timezone.data(), copy_len);
528 timezone_out[copy_len] = 0;
529 output->tm_zone = timezone_out;
530 } else {
531 output->tm_zone = NULL;
532 }
[email protected]73fa63992009-07-20 20:30:07533}
534
[email protected]a0f200ea2009-08-06 18:44:06535static bool g_am_zygote_or_renderer = false;
536
537// Sandbox interception of libc calls.
538//
539// Because we are running in a sandbox certain libc calls will fail (localtime
540// being the motivating example - it needs to read /etc/localtime). We need to
541// intercept these calls and proxy them to the browser. However, these calls
542// may come from us or from our libraries. In some cases we can't just change
543// our code.
544//
545// It's for these cases that we have the following setup:
546//
547// We define global functions for those functions which we wish to override.
548// Since we will be first in the dynamic resolution order, the dynamic linker
549// will point callers to our versions of these functions. However, we have the
550// same binary for both the browser and the renderers, which means that our
551// overrides will apply in the browser too.
552//
553// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
554// renderer process. It's set in ZygoteMain and inherited by the renderers when
555// they fork. (This means that it'll be incorrect for global constructor
556// functions and before ZygoteMain is called - beware).
557//
558// Our replacement functions can check this global and either proxy
559// the call to the browser over the sandbox IPC
560// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
561// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
562// current module.
563//
564// Other avenues:
565//
566// Our first attempt involved some assembly to patch the GOT of the current
567// module. This worked, but was platform specific and doesn't catch the case
568// where a library makes a call rather than current module.
569//
570// We also considered patching the function in place, but this would again by
571// platform specific and the above technique seems to work well enough.
572
[email protected]724df992010-09-21 18:19:10573typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
574typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
575 struct tm* result);
576
577static pthread_once_t g_libc_localtime_funcs_guard = PTHREAD_ONCE_INIT;
578static LocaltimeFunction g_libc_localtime;
579static LocaltimeRFunction g_libc_localtime_r;
580
581static void InitLibcLocaltimeFunctions() {
582 g_libc_localtime = reinterpret_cast<LocaltimeFunction>(
583 dlsym(RTLD_NEXT, "localtime"));
584 g_libc_localtime_r = reinterpret_cast<LocaltimeRFunction>(
585 dlsym(RTLD_NEXT, "localtime_r"));
586
587 if (!g_libc_localtime || !g_libc_localtime_r) {
588 // http://code.google.com/p/chromium/issues/detail?id=16800
589 //
590 // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces
591 // it with a version which doesn't work. In this case we'll get a NULL
592 // result. There's not a lot we can do at this point, so we just bodge it!
593 LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been "
594 "reported to be caused by Nvidia's libGL. You should expect"
595 " time related functions to misbehave. "
596 "http://code.google.com/p/chromium/issues/detail?id=16800";
597 }
598
599 if (!g_libc_localtime)
600 g_libc_localtime = gmtime;
601 if (!g_libc_localtime_r)
602 g_libc_localtime_r = gmtime_r;
603}
[email protected]a5d7bb82009-09-08 22:30:58604
[email protected]73fa63992009-07-20 20:30:07605struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06606 if (g_am_zygote_or_renderer) {
607 static struct tm time_struct;
608 static char timezone_string[64];
609 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
610 sizeof(timezone_string));
611 return &time_struct;
612 } else {
[email protected]724df992010-09-21 18:19:10613 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
614 InitLibcLocaltimeFunctions));
615 return g_libc_localtime(timep);
[email protected]a0f200ea2009-08-06 18:44:06616 }
[email protected]73fa63992009-07-20 20:30:07617}
618
619struct tm* localtime_r(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06620 if (g_am_zygote_or_renderer) {
621 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
622 return result;
623 } else {
[email protected]724df992010-09-21 18:19:10624 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
625 InitLibcLocaltimeFunctions));
626 return g_libc_localtime_r(timep, result);
[email protected]a0f200ea2009-08-06 18:44:06627 }
[email protected]73fa63992009-07-20 20:30:07628}
629
[email protected]ad6d2c42009-09-15 20:13:38630#endif // !CHROMIUM_SELINUX
[email protected]a5d7bb82009-09-08 22:30:58631
[email protected]ad6d2c42009-09-15 20:13:38632// This function triggers the static and lazy construction of objects that need
633// to be created before imposing the sandbox.
634static void PreSandboxInit() {
[email protected]b8419412010-03-29 20:18:29635 base::RandUint64();
[email protected]4378a822009-07-08 01:15:14636
[email protected]b8419412010-03-29 20:18:29637 base::SysInfo::MaxSharedMemorySize();
[email protected]80a086c52009-08-04 17:52:04638
[email protected]b8419412010-03-29 20:18:29639 // ICU DateFormat class (used in base/time_format.cc) needs to get the
640 // Olson timezone ID by accessing the zoneinfo files on disk. After
641 // TimeZone::createDefault is called once here, the timezone ID is
642 // cached and there's no more need to access the file system.
643 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
[email protected]1831acf2009-10-05 16:38:41644
[email protected]eeb08552011-03-18 11:15:39645#if defined(USE_NSS)
[email protected]f6a67b42011-03-17 23:49:21646 // NSS libraries are loaded before sandbox is activated. This is to allow
647 // successful initialization of NSS which tries to load extra library files.
648 // Doing so will allow NSS to be used within sandbox for chromoting.
[email protected]4b559b4d2011-04-14 17:37:14649 crypto::LoadNSSLibraries();
[email protected]eeb08552011-03-18 11:15:39650#else
651 // TODO(bulach): implement openssl support.
652 NOTREACHED() << "Remoting is not supported for openssl";
[email protected]f6a67b42011-03-17 23:49:21653#endif
[email protected]ed450f32011-03-16 01:26:49654
[email protected]17773042010-07-28 17:35:07655 // Ensure access to the Pepper plugins before the sandbox is turned on.
656 PepperPluginRegistry::PreloadModules();
[email protected]ad6d2c42009-09-15 20:13:38657}
658
659#if !defined(CHROMIUM_SELINUX)
660static bool EnterSandbox() {
[email protected]b8419412010-03-29 20:18:29661 // The SUID sandbox sets this environment variable to a file descriptor
662 // over which we can signal that we have completed our startup and can be
663 // chrooted.
[email protected]ad6d2c42009-09-15 20:13:38664 const char* const sandbox_fd_string = getenv("SBX_D");
[email protected]ad6d2c42009-09-15 20:13:38665
[email protected]0dc32322010-09-16 09:46:59666 if (sandbox_fd_string) {
667 // Use the SUID sandbox. This still allows the seccomp sandbox to
668 // be enabled by the process later.
[email protected]8ecd3aad2009-11-04 08:32:22669 g_suid_sandbox_active = true;
670
[email protected]ad6d2c42009-09-15 20:13:38671 char* endptr;
672 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
673 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
674 return false;
675 const int fd = fd_long;
676
677 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08678
[email protected]eaebefaf2010-02-23 22:58:18679 static const char kMsgChrootMe = 'C';
680 static const char kMsgChrootSuccessful = 'O';
[email protected]abe3ad92009-06-15 18:15:08681
[email protected]eaebefaf2010-02-23 22:58:18682 if (HANDLE_EINTR(write(fd, &kMsgChrootMe, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32683 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08684 return false;
[email protected]0e1611122009-07-10 18:17:32685 }
[email protected]abe3ad92009-06-15 18:15:08686
[email protected]87ed6952009-07-16 02:52:15687 // We need to reap the chroot helper process in any event:
688 wait(NULL);
689
[email protected]abe3ad92009-06-15 18:15:08690 char reply;
[email protected]87f8ce62009-07-10 19:14:31691 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32692 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08693 return false;
[email protected]0e1611122009-07-10 18:17:32694 }
[email protected]87f8ce62009-07-10 19:14:31695
[email protected]eaebefaf2010-02-23 22:58:18696 if (reply != kMsgChrootSuccessful) {
[email protected]0e1611122009-07-10 18:17:32697 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08698 return false;
[email protected]0e1611122009-07-10 18:17:32699 }
[email protected]abe3ad92009-06-15 18:15:08700
[email protected]cf3ac3972010-12-22 20:02:29701 SkiaFontConfigSetImplementation(
702 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08703
[email protected]415493be2009-07-10 17:50:24704 // Previously, we required that the binary be non-readable. This causes the
705 // kernel to mark the process as non-dumpable at startup. The thinking was
706 // that, although we were putting the renderers into a PID namespace (with
707 // the SUID sandbox), they would nonetheless be in the /same/ PID
708 // namespace. So they could ptrace each other unless they were non-dumpable.
709 //
710 // If the binary was readable, then there would be a window between process
711 // startup and the point where we set the non-dumpable flag in which a
712 // compromised renderer could ptrace attach.
713 //
714 // However, now that we have a zygote model, only the (trusted) zygote
715 // exists at this point and we can set the non-dumpable flag which is
716 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48717 //
718 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
719 // issues, one can specify --allow-sandbox-debugging to let the process be
720 // dumpable.
721 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
722 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
723 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
724 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
725 LOG(ERROR) << "Failed to set non-dumpable flag";
726 return false;
727 }
[email protected]0e1611122009-07-10 18:17:32728 }
[email protected]cc9a9a882011-03-03 20:09:10729 } else if (CommandLine::ForCurrentProcess()->HasSwitch(
730 switches::kEnableSeccompSandbox)) {
[email protected]0dc32322010-09-16 09:46:59731 PreSandboxInit();
[email protected]cf3ac3972010-12-22 20:02:29732 SkiaFontConfigSetImplementation(
733 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08734 } else {
735 SkiaFontConfigUseDirectImplementation();
736 }
737
738 return true;
739}
[email protected]ad6d2c42009-09-15 20:13:38740#else // CHROMIUM_SELINUX
741
742static bool EnterSandbox() {
743 PreSandboxInit();
744 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
[email protected]ad6d2c42009-09-15 20:13:38745 return true;
746}
747
748#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08749
[email protected]ce2ae442011-07-18 16:13:41750bool ZygoteMain(const MainFunctionParams& params,
751 ZygoteForkDelegate* forkdelegate) {
[email protected]ad6d2c42009-09-15 20:13:38752#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06753 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38754#endif
[email protected]a0f200ea2009-08-06 18:44:06755
[email protected]36ea6c6f2010-03-17 20:08:01756#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38757 // The seccomp sandbox needs access to files in /proc, which might be denied
758 // after one of the other sandboxes have been started. So, obtain a suitable
759 // file handle in advance.
[email protected]cc9a9a882011-03-03 20:09:10760 if (CommandLine::ForCurrentProcess()->HasSwitch(
761 switches::kEnableSeccompSandbox)) {
[email protected]a9c54a172009-11-07 06:09:38762 g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY);
763 if (g_proc_fd < 0) {
764 LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp "
765 "sandboxing.";
766 }
767 }
[email protected]36ea6c6f2010-03-17 20:08:01768#endif // SECCOMP_SANDBOX
[email protected]a9c54a172009-11-07 06:09:38769
[email protected]ce2ae442011-07-18 16:13:41770 if (forkdelegate != NULL) {
771 VLOG(1) << "ZygoteMain: initializing fork delegate";
772 forkdelegate->Init(getenv("SBX_D") != NULL, // g_suid_sandbox_active,
773 kBrowserDescriptor,
774 kMagicSandboxIPCDescriptor);
775 } else {
776 VLOG(1) << "ZygoteMain: fork delegate is NULL";
777 }
778
[email protected]a9c54a172009-11-07 06:09:38779 // Turn on the SELinux or SUID sandbox
780 if (!EnterSandbox()) {
781 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
782 << errno << ")";
783 return false;
784 }
785
[email protected]715b4f262010-07-13 14:17:28786 int sandbox_flags = 0;
787 if (getenv("SBX_D"))
788 sandbox_flags |= ZygoteHost::kSandboxSUID;
789 if (getenv("SBX_PID_NS"))
790 sandbox_flags |= ZygoteHost::kSandboxPIDNS;
791 if (getenv("SBX_NET_NS"))
792 sandbox_flags |= ZygoteHost::kSandboxNetNS;
793
[email protected]36ea6c6f2010-03-17 20:08:01794#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38795 // The seccomp sandbox will be turned on when the renderers start. But we can
796 // already check if sufficient support is available so that we only need to
797 // print one error message for the entire browser session.
[email protected]cc9a9a882011-03-03 20:09:10798 if (g_proc_fd >= 0 && CommandLine::ForCurrentProcess()->HasSwitch(
799 switches::kEnableSeccompSandbox)) {
[email protected]a9c54a172009-11-07 06:09:38800 if (!SupportsSeccompSandbox(g_proc_fd)) {
[email protected]e8c916a2009-11-04 17:52:47801 // There are a good number of users who cannot use the seccomp sandbox
802 // (e.g. because their distribution does not enable seccomp mode by
803 // default). While we would prefer to deny execution in this case, it
804 // seems more realistic to continue in degraded mode.
[email protected]1b5d28f2010-02-18 15:53:36805 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
806 "Seccomp sandbox. Running renderers with Seccomp "
807 "sandboxing disabled.";
[email protected]e8c916a2009-11-04 17:52:47808 } else {
[email protected]8e96e502010-10-21 20:57:12809 VLOG(1) << "Enabling experimental Seccomp sandbox.";
[email protected]715b4f262010-07-13 14:17:28810 sandbox_flags |= ZygoteHost::kSandboxSeccomp;
[email protected]e8c916a2009-11-04 17:52:47811 }
812 }
[email protected]36ea6c6f2010-03-17 20:08:01813#endif // SECCOMP_SANDBOX
[email protected]e8c916a2009-11-04 17:52:47814
[email protected]ce2ae442011-07-18 16:13:41815 Zygote zygote(sandbox_flags, forkdelegate);
[email protected]c548be22010-03-08 12:55:57816 // This function call can return multiple times, once per fork().
[email protected]cc8f1462009-06-12 17:36:55817 return zygote.ProcessRequests();
818}