blob: bf430e35e21ad5bebcc6220ac7e6cf4e23ab5fc6 [file] [log] [blame]
[email protected]17773042010-07-28 17:35:071// Copyright (c) 2010 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]a0f200ea2009-08-06 18:44:065#include <dlfcn.h>
[email protected]a9c54a172009-11-07 06:09:386#include <fcntl.h>
[email protected]724df992010-09-21 18:19:107#include <pthread.h>
[email protected]83a0cbe2009-11-04 04:22:478#include <sys/epoll.h>
[email protected]83a0cbe2009-11-04 04:22:479#include <sys/prctl.h>
[email protected]8ecd3aad2009-11-04 08:32:2210#include <sys/signal.h>
11#include <sys/socket.h>
[email protected]a9c54a172009-11-07 06:09:3812#include <sys/stat.h>
[email protected]8ecd3aad2009-11-04 08:32:2213#include <sys/types.h>
[email protected]83a0cbe2009-11-04 04:22:4714#include <sys/wait.h>
[email protected]8ecd3aad2009-11-04 08:32:2215#include <unistd.h>
16
17#if defined(CHROMIUM_SELINUX)
18#include <selinux/selinux.h>
19#include <selinux/context.h>
20#endif
[email protected]cc8f1462009-06-12 17:36:5521
[email protected]789f70c72009-07-24 13:55:4322#include "base/basictypes.h"
[email protected]cc8f1462009-06-12 17:36:5523#include "base/command_line.h"
24#include "base/eintr_wrapper.h"
[email protected]5d91c9e2010-07-28 17:25:2825#include "base/file_path.h"
[email protected]cc8f1462009-06-12 17:36:5526#include "base/global_descriptors_posix.h"
[email protected]8ecd3aad2009-11-04 08:32:2227#include "base/hash_tables.h"
28#include "base/linux_util.h"
[email protected]c9e45da02009-08-05 17:35:0829#include "base/path_service.h"
[email protected]cc8f1462009-06-12 17:36:5530#include "base/pickle.h"
[email protected]4d36536b2010-08-20 06:23:2731#include "base/process_util.h"
[email protected]4378a822009-07-08 01:15:1432#include "base/rand_util.h"
[email protected]1831acf2009-10-05 16:38:4133#include "base/scoped_ptr.h"
[email protected]80a086c52009-08-04 17:52:0434#include "base/sys_info.h"
[email protected]8015de32009-11-18 04:13:3335#include "build/build_config.h"
[email protected]cc8f1462009-06-12 17:36:5536#include "chrome/browser/zygote_host_linux.h"
37#include "chrome/common/chrome_descriptors.h"
[email protected]4730db92009-07-22 00:40:4838#include "chrome/common/chrome_switches.h"
[email protected]cf3ac3972010-12-22 20:02:2939#include "chrome/common/font_config_ipc_linux.h"
[email protected]cc8f1462009-06-12 17:36:5540#include "chrome/common/main_function_params.h"
[email protected]17773042010-07-28 17:35:0741#include "chrome/common/pepper_plugin_registry.h"
[email protected]cc8f1462009-06-12 17:36:5542#include "chrome/common/process_watcher.h"
[email protected]443b80e2010-12-14 00:42:2343#include "chrome/common/result_codes.h"
[email protected]73fa63992009-07-20 20:30:0744#include "chrome/common/sandbox_methods_linux.h"
[email protected]74e9fa22010-12-29 21:06:4345#include "chrome/common/set_process_title.h"
[email protected]cf3ac3972010-12-22 20:02:2946#include "chrome/common/unix_domain_socket_posix.h"
[email protected]c9e45da02009-08-05 17:35:0847#include "media/base/media.h"
[email protected]808ea572010-09-01 16:22:0148#include "seccompsandbox/sandbox.h"
[email protected]cf3ac3972010-12-22 20:02:2949#include "skia/ext/SkFontHost_fontconfig_control.h"
[email protected]1831acf2009-10-05 16:38:4150#include "unicode/timezone.h"
51
[email protected]58680ce2010-09-18 00:09:1552#if defined(ARCH_CPU_X86_FAMILY) && !defined(CHROMIUM_SELINUX) && \
53 !defined(__clang__)
[email protected]36ea6c6f2010-03-17 20:08:0154// The seccomp sandbox is enabled on all ia32 and x86-64 processor as long as
[email protected]58680ce2010-09-18 00:09:1555// we aren't using SELinux or clang.
[email protected]36ea6c6f2010-03-17 20:08:0156#define SECCOMP_SANDBOX
57#endif
58
[email protected]cc8f1462009-06-12 17:36:5559// http://code.google.com/p/chromium/wiki/LinuxZygote
60
[email protected]8ecd3aad2009-11-04 08:32:2261static const int kBrowserDescriptor = 3;
[email protected]73fa63992009-07-20 20:30:0762static const int kMagicSandboxIPCDescriptor = 5;
[email protected]8ecd3aad2009-11-04 08:32:2263static const int kZygoteIdDescriptor = 7;
64static bool g_suid_sandbox_active = false;
[email protected]36ea6c6f2010-03-17 20:08:0165#if defined(SECCOMP_SANDBOX)
[email protected]4f03cbc2009-11-19 00:50:4866// |g_proc_fd| is used only by the seccomp sandbox.
[email protected]a9c54a172009-11-07 06:09:3867static int g_proc_fd = -1;
[email protected]4f03cbc2009-11-19 00:50:4868#endif
[email protected]73fa63992009-07-20 20:30:0769
[email protected]a89a55dd2010-04-19 14:51:1370#if defined(CHROMIUM_SELINUX)
71static void SELinuxTransitionToTypeOrDie(const char* type) {
72 security_context_t security_context;
73 if (getcon(&security_context))
74 LOG(FATAL) << "Cannot get SELinux context";
75
76 context_t context = context_new(security_context);
77 context_type_set(context, type);
78 const int r = setcon(context_str(context));
79 context_free(context);
80 freecon(security_context);
81
82 if (r) {
83 LOG(FATAL) << "dynamic transition to type '" << type << "' failed. "
84 "(this binary has been built with SELinux support, but maybe "
85 "the policies haven't been loaded into the kernel?)";
86 }
87}
88#endif // CHROMIUM_SELINUX
89
[email protected]cc8f1462009-06-12 17:36:5590// This is the object which implements the zygote. The ZygoteMain function,
[email protected]61883442010-07-12 15:14:3491// which is called from ChromeMain, simply constructs one of these objects and
92// runs it.
[email protected]cc8f1462009-06-12 17:36:5593class Zygote {
94 public:
[email protected]715b4f262010-07-13 14:17:2895 explicit Zygote(int sandbox_flags)
96 : sandbox_flags_(sandbox_flags) {
97 }
98
[email protected]cc8f1462009-06-12 17:36:5599 bool ProcessRequests() {
100 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
101 // browser on it.
[email protected]8ecd3aad2009-11-04 08:32:22102 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
[email protected]abe3ad92009-06-15 18:15:08103 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:55104
105 // We need to accept SIGCHLD, even though our handler is a no-op because
106 // otherwise we cannot wait on children. (According to POSIX 2001.)
107 struct sigaction action;
108 memset(&action, 0, sizeof(action));
109 action.sa_handler = SIGCHLDHandler;
110 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
111
[email protected]8ecd3aad2009-11-04 08:32:22112 if (g_suid_sandbox_active) {
113 // Let the ZygoteHost know we are ready to go.
114 // The receiving code is in chrome/browser/zygote_host_linux.cc.
115 std::vector<int> empty;
[email protected]cf3ac3972010-12-22 20:02:29116 bool r = UnixDomainSocket::SendMsg(kBrowserDescriptor, kZygoteMagic,
117 sizeof(kZygoteMagic), empty);
[email protected]8ecd3aad2009-11-04 08:32:22118 CHECK(r) << "Sending zygote magic failed";
119 }
120
[email protected]cc8f1462009-06-12 17:36:55121 for (;;) {
[email protected]c548be22010-03-08 12:55:57122 // This function call can return multiple times, once per fork().
[email protected]8ecd3aad2009-11-04 08:32:22123 if (HandleRequestFromBrowser(kBrowserDescriptor))
[email protected]cc8f1462009-06-12 17:36:55124 return true;
125 }
126 }
127
128 private:
129 // See comment below, where sigaction is called.
130 static void SIGCHLDHandler(int signal) { }
131
132 // ---------------------------------------------------------------------------
133 // Requests from the browser...
134
135 // Read and process a request from the browser. Returns true if we are in a
136 // new process and thus need to unwind back into ChromeMain.
137 bool HandleRequestFromBrowser(int fd) {
138 std::vector<int> fds;
[email protected]abe3ad92009-06-15 18:15:08139 static const unsigned kMaxMessageLength = 1024;
[email protected]cc8f1462009-06-12 17:36:55140 char buf[kMaxMessageLength];
[email protected]cf3ac3972010-12-22 20:02:29141 const ssize_t len = UnixDomainSocket::RecvMsg(fd, buf, sizeof(buf), &fds);
[email protected]7ae27b92010-09-24 17:33:36142
143 if (len == 0 || (len == -1 && errno == ECONNRESET)) {
144 // EOF from the browser. We should die.
145 _exit(0);
[email protected]cc8f1462009-06-12 17:36:55146 return false;
147 }
148
[email protected]7ae27b92010-09-24 17:33:36149 if (len == -1) {
150 PLOG(ERROR) << "Error reading message from browser";
[email protected]cc8f1462009-06-12 17:36:55151 return false;
152 }
153
154 Pickle pickle(buf, len);
155 void* iter = NULL;
156
157 int kind;
[email protected]57113ea2009-06-18 02:23:16158 if (pickle.ReadInt(&iter, &kind)) {
159 switch (kind) {
160 case ZygoteHost::kCmdFork:
[email protected]c548be22010-03-08 12:55:57161 // This function call can return multiple times, once per fork().
[email protected]57113ea2009-06-18 02:23:16162 return HandleForkRequest(fd, pickle, iter, fds);
163 case ZygoteHost::kCmdReap:
164 if (!fds.empty())
165 break;
[email protected]c548be22010-03-08 12:55:57166 HandleReapRequest(fd, pickle, iter);
167 return false;
[email protected]443b80e2010-12-14 00:42:23168 case ZygoteHost::kCmdGetTerminationStatus:
[email protected]57113ea2009-06-18 02:23:16169 if (!fds.empty())
170 break;
[email protected]443b80e2010-12-14 00:42:23171 HandleGetTerminationStatus(fd, pickle, iter);
[email protected]c548be22010-03-08 12:55:57172 return false;
[email protected]715b4f262010-07-13 14:17:28173 case ZygoteHost::kCmdGetSandboxStatus:
174 HandleGetSandboxStatus(fd, pickle, iter);
175 return false;
[email protected]57113ea2009-06-18 02:23:16176 default:
177 NOTREACHED();
178 break;
179 }
[email protected]cc8f1462009-06-12 17:36:55180 }
181
[email protected]cc8f1462009-06-12 17:36:55182 LOG(WARNING) << "Error parsing message from browser";
183 for (std::vector<int>::const_iterator
184 i = fds.begin(); i != fds.end(); ++i)
185 close(*i);
186 return false;
187 }
188
[email protected]c548be22010-03-08 12:55:57189 void HandleReapRequest(int fd, const Pickle& pickle, void* iter) {
[email protected]8ecd3aad2009-11-04 08:32:22190 base::ProcessId child;
191 base::ProcessId actual_child;
[email protected]cc8f1462009-06-12 17:36:55192
193 if (!pickle.ReadInt(&iter, &child)) {
194 LOG(WARNING) << "Error parsing reap request from browser";
[email protected]c548be22010-03-08 12:55:57195 return;
[email protected]cc8f1462009-06-12 17:36:55196 }
197
[email protected]8ecd3aad2009-11-04 08:32:22198 if (g_suid_sandbox_active) {
199 actual_child = real_pids_to_sandbox_pids[child];
200 if (!actual_child)
[email protected]c548be22010-03-08 12:55:57201 return;
[email protected]8ecd3aad2009-11-04 08:32:22202 real_pids_to_sandbox_pids.erase(child);
203 } else {
204 actual_child = child;
205 }
206
207 ProcessWatcher::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55208 }
209
[email protected]443b80e2010-12-14 00:42:23210 void HandleGetTerminationStatus(int fd, const Pickle& pickle, void* iter) {
[email protected]57113ea2009-06-18 02:23:16211 base::ProcessHandle child;
212
213 if (!pickle.ReadInt(&iter, &child)) {
[email protected]443b80e2010-12-14 00:42:23214 LOG(WARNING) << "Error parsing GetTerminationStatus request "
215 << "from browser";
[email protected]c548be22010-03-08 12:55:57216 return;
[email protected]57113ea2009-06-18 02:23:16217 }
218
[email protected]443b80e2010-12-14 00:42:23219 base::TerminationStatus status;
220 int exit_code;
[email protected]8ecd3aad2009-11-04 08:32:22221 if (g_suid_sandbox_active)
222 child = real_pids_to_sandbox_pids[child];
[email protected]443b80e2010-12-14 00:42:23223 if (child) {
224 status = base::GetTerminationStatus(child, &exit_code);
225 } else {
226 // Assume that if we can't find the child in the sandbox, then
227 // it terminated normally.
228 status = base::TERMINATION_STATUS_NORMAL_TERMINATION;
229 exit_code = ResultCodes::NORMAL_EXIT;
230 }
[email protected]57113ea2009-06-18 02:23:16231
232 Pickle write_pickle;
[email protected]443b80e2010-12-14 00:42:23233 write_pickle.WriteInt(static_cast<int>(status));
234 write_pickle.WriteInt(exit_code);
[email protected]4b809bc2010-04-19 16:12:05235 if (HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size())) !=
236 write_pickle.size()) {
[email protected]19cb9292010-04-16 23:00:15237 PLOG(ERROR) << "write";
[email protected]4b809bc2010-04-19 16:12:05238 }
[email protected]57113ea2009-06-18 02:23:16239 }
240
[email protected]28ecba32010-09-16 10:03:09241 // This is equivalent to fork(), except that, when using the SUID
242 // sandbox, it returns the real PID of the child process as it
243 // appears outside the sandbox, rather than returning the PID inside
244 // the sandbox.
245 int ForkWithRealPid() {
246 if (!g_suid_sandbox_active)
247 return fork();
248
249 int dummy_fd;
250 ino_t dummy_inode;
251 int pipe_fds[2] = { -1, -1 };
[email protected]073040b2010-09-26 02:35:45252 base::ProcessId pid = 0;
[email protected]28ecba32010-09-16 10:03:09253
254 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
255 if (dummy_fd < 0) {
256 LOG(ERROR) << "Failed to create dummy FD";
257 goto error;
258 }
259 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd)) {
260 LOG(ERROR) << "Failed to get inode for dummy FD";
261 goto error;
262 }
263 if (pipe(pipe_fds) != 0) {
264 LOG(ERROR) << "Failed to create pipe";
265 goto error;
266 }
267
268 pid = fork();
269 if (pid < 0) {
270 goto error;
271 } else if (pid == 0) {
272 // In the child process.
273 close(pipe_fds[1]);
274 char buffer[1];
275 // Wait until the parent process has discovered our PID. We
276 // should not fork any child processes (which the seccomp
277 // sandbox does) until then, because that can interfere with the
278 // parent's discovery of our PID.
279 if (HANDLE_EINTR(read(pipe_fds[0], buffer, 1)) != 1 ||
280 buffer[0] != 'x') {
281 LOG(FATAL) << "Failed to synchronise with parent zygote process";
282 }
283 close(pipe_fds[0]);
284 close(dummy_fd);
285 return 0;
286 } else {
287 // In the parent process.
288 close(dummy_fd);
289 dummy_fd = -1;
290 close(pipe_fds[0]);
291 pipe_fds[0] = -1;
292 uint8_t reply_buf[512];
293 Pickle request;
294 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
295 request.WriteUInt64(dummy_inode);
296
[email protected]cf3ac3972010-12-22 20:02:29297 const ssize_t r = UnixDomainSocket::SendRecvMsg(
298 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL,
299 request);
[email protected]28ecba32010-09-16 10:03:09300 if (r == -1) {
301 LOG(ERROR) << "Failed to get child process's real PID";
302 goto error;
303 }
304
305 base::ProcessId real_pid;
306 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
307 void* iter2 = NULL;
308 if (!reply.ReadInt(&iter2, &real_pid))
309 goto error;
[email protected]073040b2010-09-26 02:35:45310 if (real_pid <= 0) {
311 // METHOD_GET_CHILD_WITH_INODE failed. Did the child die already?
312 LOG(ERROR) << "METHOD_GET_CHILD_WITH_INODE failed";
313 goto error;
314 }
[email protected]28ecba32010-09-16 10:03:09315 real_pids_to_sandbox_pids[real_pid] = pid;
316 if (HANDLE_EINTR(write(pipe_fds[1], "x", 1)) != 1) {
317 LOG(ERROR) << "Failed to synchronise with child process";
318 goto error;
319 }
320 close(pipe_fds[1]);
321 return real_pid;
322 }
323
324 error:
[email protected]72f891f42011-01-12 17:00:52325 if (pid > 0) {
326 if (waitpid(pid, NULL, WNOHANG) == -1)
327 LOG(ERROR) << "Failed to wait for process";
328 }
[email protected]28ecba32010-09-16 10:03:09329 if (dummy_fd >= 0)
330 close(dummy_fd);
331 if (pipe_fds[0] >= 0)
332 close(pipe_fds[0]);
333 if (pipe_fds[1] >= 0)
334 close(pipe_fds[1]);
335 return -1;
336 }
337
[email protected]cc8f1462009-06-12 17:36:55338 // Handle a 'fork' request from the browser: this means that the browser
339 // wishes to start a new renderer.
[email protected]8ecd3aad2009-11-04 08:32:22340 bool HandleForkRequest(int fd, const Pickle& pickle, void* iter,
[email protected]cc8f1462009-06-12 17:36:55341 std::vector<int>& fds) {
342 std::vector<std::string> args;
343 int argc, numfds;
344 base::GlobalDescriptors::Mapping mapping;
[email protected]8ecd3aad2009-11-04 08:32:22345 base::ProcessId child;
[email protected]cc8f1462009-06-12 17:36:55346
347 if (!pickle.ReadInt(&iter, &argc))
348 goto error;
349
350 for (int i = 0; i < argc; ++i) {
351 std::string arg;
352 if (!pickle.ReadString(&iter, &arg))
353 goto error;
354 args.push_back(arg);
355 }
356
357 if (!pickle.ReadInt(&iter, &numfds))
358 goto error;
359 if (numfds != static_cast<int>(fds.size()))
360 goto error;
361
362 for (int i = 0; i < numfds; ++i) {
363 base::GlobalDescriptors::Key key;
364 if (!pickle.ReadUInt32(&iter, &key))
365 goto error;
366 mapping.push_back(std::make_pair(key, fds[i]));
367 }
368
[email protected]abe3ad92009-06-15 18:15:08369 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22370 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
371
[email protected]28ecba32010-09-16 10:03:09372 child = ForkWithRealPid();
[email protected]cc8f1462009-06-12 17:36:55373
374 if (!child) {
[email protected]36ea6c6f2010-03-17 20:08:01375#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38376 // Try to open /proc/self/maps as the seccomp sandbox needs access to it
377 if (g_proc_fd >= 0) {
378 int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY);
379 if (proc_self_maps >= 0) {
380 SeccompSandboxSetProcSelfMaps(proc_self_maps);
381 }
382 close(g_proc_fd);
383 g_proc_fd = -1;
384 }
[email protected]8015de32009-11-18 04:13:33385#endif
[email protected]a9c54a172009-11-07 06:09:38386
[email protected]8ecd3aad2009-11-04 08:32:22387 close(kBrowserDescriptor); // our socket from the browser
[email protected]cbcf9cc32010-02-17 04:05:59388 if (g_suid_sandbox_active)
389 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]9b85081af2010-12-07 21:26:47390 base::GlobalDescriptors::GetInstance()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39391
[email protected]a89a55dd2010-04-19 14:51:13392#if defined(CHROMIUM_SELINUX)
393 SELinuxTransitionToTypeOrDie("chromium_renderer_t");
394#endif
395
[email protected]0189bbd2009-10-12 22:50:39396 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55397 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39398 CommandLine::Init(0, NULL);
399 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]74e9fa22010-12-29 21:06:43400
401 // Update the process title. The argv was already cached by the call to
402 // SetProcessTitleFromCommandLine in ChromeMain, so we can pass NULL here
403 // (we don't have the original argv at this point).
404 SetProcessTitleFromCommandLine(NULL);
405
[email protected]c548be22010-03-08 12:55:57406 // The fork() request is handled further up the call stack.
[email protected]cc8f1462009-06-12 17:36:55407 return true;
[email protected]8ecd3aad2009-11-04 08:32:22408 } else if (child < 0) {
[email protected]466cffd2010-06-09 18:10:56409 LOG(ERROR) << "Zygote could not fork: " << errno;
[email protected]8ecd3aad2009-11-04 08:32:22410 goto error;
[email protected]cc8f1462009-06-12 17:36:55411 }
412
[email protected]28ecba32010-09-16 10:03:09413 for (std::vector<int>::const_iterator
414 i = fds.begin(); i != fds.end(); ++i)
415 close(*i);
[email protected]83a0cbe2009-11-04 04:22:47416
[email protected]28ecba32010-09-16 10:03:09417 if (HANDLE_EINTR(write(fd, &child, sizeof(child))) < 0)
418 PLOG(ERROR) << "write";
419 return false;
[email protected]83a0cbe2009-11-04 04:22:47420
421 error:
[email protected]8ecd3aad2009-11-04 08:32:22422 LOG(ERROR) << "Error parsing fork request from browser";
[email protected]83a0cbe2009-11-04 04:22:47423 for (std::vector<int>::const_iterator
424 i = fds.begin(); i != fds.end(); ++i)
425 close(*i);
[email protected]cc8f1462009-06-12 17:36:55426 return false;
427 }
[email protected]8ecd3aad2009-11-04 08:32:22428
[email protected]715b4f262010-07-13 14:17:28429 bool HandleGetSandboxStatus(int fd, const Pickle& pickle, void* iter) {
430 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_)) !=
431 sizeof(sandbox_flags_))) {
432 PLOG(ERROR) << "write";
433 }
434
435 return false;
436 }
437
[email protected]8ecd3aad2009-11-04 08:32:22438 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
439 // fork() returns are not the real PIDs, so we need to map the Real PIDS
440 // into the sandbox PID namespace.
441 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
442 ProcessMap real_pids_to_sandbox_pids;
[email protected]715b4f262010-07-13 14:17:28443
444 const int sandbox_flags_;
[email protected]cc8f1462009-06-12 17:36:55445};
446
[email protected]ad6d2c42009-09-15 20:13:38447// With SELinux we can carve out a precise sandbox, so we don't have to play
448// with intercepting libc calls.
449#if !defined(CHROMIUM_SELINUX)
450
[email protected]a0f200ea2009-08-06 18:44:06451static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
452 char* timezone_out,
453 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07454 Pickle request;
455 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
456 request.WriteString(
457 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
458
459 uint8_t reply_buf[512];
[email protected]cf3ac3972010-12-22 20:02:29460 const ssize_t r = UnixDomainSocket::SendRecvMsg(
[email protected]73fa63992009-07-20 20:30:07461 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
462 if (r == -1) {
463 memset(output, 0, sizeof(struct tm));
464 return;
465 }
466
467 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
468 void* iter = NULL;
[email protected]9debcda52009-07-22 20:06:51469 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07470 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51471 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07472 result.size() != sizeof(struct tm)) {
473 memset(output, 0, sizeof(struct tm));
474 return;
475 }
476
477 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51478 if (timezone_out_len) {
479 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
480 memcpy(timezone_out, timezone.data(), copy_len);
481 timezone_out[copy_len] = 0;
482 output->tm_zone = timezone_out;
483 } else {
484 output->tm_zone = NULL;
485 }
[email protected]73fa63992009-07-20 20:30:07486}
487
[email protected]a0f200ea2009-08-06 18:44:06488static bool g_am_zygote_or_renderer = false;
489
490// Sandbox interception of libc calls.
491//
492// Because we are running in a sandbox certain libc calls will fail (localtime
493// being the motivating example - it needs to read /etc/localtime). We need to
494// intercept these calls and proxy them to the browser. However, these calls
495// may come from us or from our libraries. In some cases we can't just change
496// our code.
497//
498// It's for these cases that we have the following setup:
499//
500// We define global functions for those functions which we wish to override.
501// Since we will be first in the dynamic resolution order, the dynamic linker
502// will point callers to our versions of these functions. However, we have the
503// same binary for both the browser and the renderers, which means that our
504// overrides will apply in the browser too.
505//
506// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
507// renderer process. It's set in ZygoteMain and inherited by the renderers when
508// they fork. (This means that it'll be incorrect for global constructor
509// functions and before ZygoteMain is called - beware).
510//
511// Our replacement functions can check this global and either proxy
512// the call to the browser over the sandbox IPC
513// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
514// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
515// current module.
516//
517// Other avenues:
518//
519// Our first attempt involved some assembly to patch the GOT of the current
520// module. This worked, but was platform specific and doesn't catch the case
521// where a library makes a call rather than current module.
522//
523// We also considered patching the function in place, but this would again by
524// platform specific and the above technique seems to work well enough.
525
[email protected]724df992010-09-21 18:19:10526typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
527typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
528 struct tm* result);
529
530static pthread_once_t g_libc_localtime_funcs_guard = PTHREAD_ONCE_INIT;
531static LocaltimeFunction g_libc_localtime;
532static LocaltimeRFunction g_libc_localtime_r;
533
534static void InitLibcLocaltimeFunctions() {
535 g_libc_localtime = reinterpret_cast<LocaltimeFunction>(
536 dlsym(RTLD_NEXT, "localtime"));
537 g_libc_localtime_r = reinterpret_cast<LocaltimeRFunction>(
538 dlsym(RTLD_NEXT, "localtime_r"));
539
540 if (!g_libc_localtime || !g_libc_localtime_r) {
541 // http://code.google.com/p/chromium/issues/detail?id=16800
542 //
543 // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces
544 // it with a version which doesn't work. In this case we'll get a NULL
545 // result. There's not a lot we can do at this point, so we just bodge it!
546 LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been "
547 "reported to be caused by Nvidia's libGL. You should expect"
548 " time related functions to misbehave. "
549 "http://code.google.com/p/chromium/issues/detail?id=16800";
550 }
551
552 if (!g_libc_localtime)
553 g_libc_localtime = gmtime;
554 if (!g_libc_localtime_r)
555 g_libc_localtime_r = gmtime_r;
556}
[email protected]a5d7bb82009-09-08 22:30:58557
[email protected]73fa63992009-07-20 20:30:07558struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06559 if (g_am_zygote_or_renderer) {
560 static struct tm time_struct;
561 static char timezone_string[64];
562 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
563 sizeof(timezone_string));
564 return &time_struct;
565 } else {
[email protected]724df992010-09-21 18:19:10566 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
567 InitLibcLocaltimeFunctions));
568 return g_libc_localtime(timep);
[email protected]a0f200ea2009-08-06 18:44:06569 }
[email protected]73fa63992009-07-20 20:30:07570}
571
572struct tm* localtime_r(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06573 if (g_am_zygote_or_renderer) {
574 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
575 return result;
576 } else {
[email protected]724df992010-09-21 18:19:10577 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
578 InitLibcLocaltimeFunctions));
579 return g_libc_localtime_r(timep, result);
[email protected]a0f200ea2009-08-06 18:44:06580 }
[email protected]73fa63992009-07-20 20:30:07581}
582
[email protected]ad6d2c42009-09-15 20:13:38583#endif // !CHROMIUM_SELINUX
[email protected]a5d7bb82009-09-08 22:30:58584
[email protected]ad6d2c42009-09-15 20:13:38585// This function triggers the static and lazy construction of objects that need
586// to be created before imposing the sandbox.
587static void PreSandboxInit() {
[email protected]b8419412010-03-29 20:18:29588 base::RandUint64();
[email protected]4378a822009-07-08 01:15:14589
[email protected]b8419412010-03-29 20:18:29590 base::SysInfo::MaxSharedMemorySize();
[email protected]80a086c52009-08-04 17:52:04591
[email protected]b8419412010-03-29 20:18:29592 // To make wcstombs/mbstowcs work in a renderer, setlocale() has to be
593 // called before the sandbox is triggered. It's possible to avoid calling
594 // setlocale() by pulling out the conversion between FilePath and
595 // WebCore String out of the renderer and using string16 in place of
596 // FilePath for IPC.
597 const char* locale = setlocale(LC_ALL, "");
598 LOG_IF(WARNING, locale == NULL) << "setlocale failed.";
[email protected]e6acd672009-07-24 21:51:33599
[email protected]b8419412010-03-29 20:18:29600 // ICU DateFormat class (used in base/time_format.cc) needs to get the
601 // Olson timezone ID by accessing the zoneinfo files on disk. After
602 // TimeZone::createDefault is called once here, the timezone ID is
603 // cached and there's no more need to access the file system.
604 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
[email protected]1831acf2009-10-05 16:38:41605
[email protected]b8419412010-03-29 20:18:29606 FilePath module_path;
607 if (PathService::Get(base::DIR_MODULE, &module_path))
608 media::InitializeMediaLibrary(module_path);
[email protected]17773042010-07-28 17:35:07609
610 // Ensure access to the Pepper plugins before the sandbox is turned on.
611 PepperPluginRegistry::PreloadModules();
[email protected]ad6d2c42009-09-15 20:13:38612}
613
614#if !defined(CHROMIUM_SELINUX)
615static bool EnterSandbox() {
[email protected]b8419412010-03-29 20:18:29616 // The SUID sandbox sets this environment variable to a file descriptor
617 // over which we can signal that we have completed our startup and can be
618 // chrooted.
[email protected]ad6d2c42009-09-15 20:13:38619 const char* const sandbox_fd_string = getenv("SBX_D");
[email protected]ad6d2c42009-09-15 20:13:38620
[email protected]0dc32322010-09-16 09:46:59621 if (sandbox_fd_string) {
622 // Use the SUID sandbox. This still allows the seccomp sandbox to
623 // be enabled by the process later.
[email protected]8ecd3aad2009-11-04 08:32:22624 g_suid_sandbox_active = true;
625
[email protected]ad6d2c42009-09-15 20:13:38626 char* endptr;
627 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
628 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
629 return false;
630 const int fd = fd_long;
631
632 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08633
[email protected]eaebefaf2010-02-23 22:58:18634 static const char kMsgChrootMe = 'C';
635 static const char kMsgChrootSuccessful = 'O';
[email protected]abe3ad92009-06-15 18:15:08636
[email protected]eaebefaf2010-02-23 22:58:18637 if (HANDLE_EINTR(write(fd, &kMsgChrootMe, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32638 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08639 return false;
[email protected]0e1611122009-07-10 18:17:32640 }
[email protected]abe3ad92009-06-15 18:15:08641
[email protected]87ed6952009-07-16 02:52:15642 // We need to reap the chroot helper process in any event:
643 wait(NULL);
644
[email protected]abe3ad92009-06-15 18:15:08645 char reply;
[email protected]87f8ce62009-07-10 19:14:31646 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32647 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08648 return false;
[email protected]0e1611122009-07-10 18:17:32649 }
[email protected]87f8ce62009-07-10 19:14:31650
[email protected]eaebefaf2010-02-23 22:58:18651 if (reply != kMsgChrootSuccessful) {
[email protected]0e1611122009-07-10 18:17:32652 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08653 return false;
[email protected]0e1611122009-07-10 18:17:32654 }
[email protected]abe3ad92009-06-15 18:15:08655
[email protected]cf3ac3972010-12-22 20:02:29656 SkiaFontConfigSetImplementation(
657 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08658
[email protected]415493be2009-07-10 17:50:24659 // Previously, we required that the binary be non-readable. This causes the
660 // kernel to mark the process as non-dumpable at startup. The thinking was
661 // that, although we were putting the renderers into a PID namespace (with
662 // the SUID sandbox), they would nonetheless be in the /same/ PID
663 // namespace. So they could ptrace each other unless they were non-dumpable.
664 //
665 // If the binary was readable, then there would be a window between process
666 // startup and the point where we set the non-dumpable flag in which a
667 // compromised renderer could ptrace attach.
668 //
669 // However, now that we have a zygote model, only the (trusted) zygote
670 // exists at this point and we can set the non-dumpable flag which is
671 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48672 //
673 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
674 // issues, one can specify --allow-sandbox-debugging to let the process be
675 // dumpable.
676 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
677 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
678 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
679 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
680 LOG(ERROR) << "Failed to set non-dumpable flag";
681 return false;
682 }
[email protected]0e1611122009-07-10 18:17:32683 }
[email protected]0dc32322010-09-16 09:46:59684 } else if (switches::SeccompSandboxEnabled()) {
685 PreSandboxInit();
[email protected]cf3ac3972010-12-22 20:02:29686 SkiaFontConfigSetImplementation(
687 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08688 } else {
689 SkiaFontConfigUseDirectImplementation();
690 }
691
692 return true;
693}
[email protected]ad6d2c42009-09-15 20:13:38694#else // CHROMIUM_SELINUX
695
696static bool EnterSandbox() {
697 PreSandboxInit();
698 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
[email protected]ad6d2c42009-09-15 20:13:38699 return true;
700}
701
702#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08703
[email protected]cc8f1462009-06-12 17:36:55704bool ZygoteMain(const MainFunctionParams& params) {
[email protected]ad6d2c42009-09-15 20:13:38705#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06706 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38707#endif
[email protected]a0f200ea2009-08-06 18:44:06708
[email protected]36ea6c6f2010-03-17 20:08:01709#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38710 // The seccomp sandbox needs access to files in /proc, which might be denied
711 // after one of the other sandboxes have been started. So, obtain a suitable
712 // file handle in advance.
[email protected]39c4e1a82010-03-30 19:47:41713 if (switches::SeccompSandboxEnabled()) {
[email protected]a9c54a172009-11-07 06:09:38714 g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY);
715 if (g_proc_fd < 0) {
716 LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp "
717 "sandboxing.";
718 }
719 }
[email protected]36ea6c6f2010-03-17 20:08:01720#endif // SECCOMP_SANDBOX
[email protected]a9c54a172009-11-07 06:09:38721
722 // Turn on the SELinux or SUID sandbox
723 if (!EnterSandbox()) {
724 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
725 << errno << ")";
726 return false;
727 }
728
[email protected]715b4f262010-07-13 14:17:28729 int sandbox_flags = 0;
730 if (getenv("SBX_D"))
731 sandbox_flags |= ZygoteHost::kSandboxSUID;
732 if (getenv("SBX_PID_NS"))
733 sandbox_flags |= ZygoteHost::kSandboxPIDNS;
734 if (getenv("SBX_NET_NS"))
735 sandbox_flags |= ZygoteHost::kSandboxNetNS;
736
[email protected]36ea6c6f2010-03-17 20:08:01737#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38738 // The seccomp sandbox will be turned on when the renderers start. But we can
739 // already check if sufficient support is available so that we only need to
740 // print one error message for the entire browser session.
[email protected]39c4e1a82010-03-30 19:47:41741 if (g_proc_fd >= 0 && switches::SeccompSandboxEnabled()) {
[email protected]a9c54a172009-11-07 06:09:38742 if (!SupportsSeccompSandbox(g_proc_fd)) {
[email protected]e8c916a2009-11-04 17:52:47743 // There are a good number of users who cannot use the seccomp sandbox
744 // (e.g. because their distribution does not enable seccomp mode by
745 // default). While we would prefer to deny execution in this case, it
746 // seems more realistic to continue in degraded mode.
[email protected]1b5d28f2010-02-18 15:53:36747 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
748 "Seccomp sandbox. Running renderers with Seccomp "
749 "sandboxing disabled.";
[email protected]e8c916a2009-11-04 17:52:47750 } else {
[email protected]8e96e502010-10-21 20:57:12751 VLOG(1) << "Enabling experimental Seccomp sandbox.";
[email protected]715b4f262010-07-13 14:17:28752 sandbox_flags |= ZygoteHost::kSandboxSeccomp;
[email protected]e8c916a2009-11-04 17:52:47753 }
754 }
[email protected]36ea6c6f2010-03-17 20:08:01755#endif // SECCOMP_SANDBOX
[email protected]e8c916a2009-11-04 17:52:47756
[email protected]715b4f262010-07-13 14:17:28757 Zygote zygote(sandbox_flags);
[email protected]c548be22010-03-08 12:55:57758 // This function call can return multiple times, once per fork().
[email protected]cc8f1462009-06-12 17:36:55759 return zygote.ProcessRequests();
760}