blob: c4f0b3205dc14f74334c33ea9dd0100ff8e1819c [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]a0421732011-02-23 03:55:4022#include "content/browser/zygote_host_linux.h"
23
[email protected]789f70c72009-07-24 13:55:4324#include "base/basictypes.h"
[email protected]cc8f1462009-06-12 17:36:5525#include "base/command_line.h"
26#include "base/eintr_wrapper.h"
[email protected]5d91c9e2010-07-28 17:25:2827#include "base/file_path.h"
[email protected]cc8f1462009-06-12 17:36:5528#include "base/global_descriptors_posix.h"
[email protected]8ecd3aad2009-11-04 08:32:2229#include "base/hash_tables.h"
30#include "base/linux_util.h"
[email protected]c9e45da02009-08-05 17:35:0831#include "base/path_service.h"
[email protected]cc8f1462009-06-12 17:36:5532#include "base/pickle.h"
[email protected]4d36536b2010-08-20 06:23:2733#include "base/process_util.h"
[email protected]4378a822009-07-08 01:15:1434#include "base/rand_util.h"
[email protected]1831acf2009-10-05 16:38:4135#include "base/scoped_ptr.h"
[email protected]80a086c52009-08-04 17:52:0436#include "base/sys_info.h"
[email protected]8015de32009-11-18 04:13:3337#include "build/build_config.h"
[email protected]cc8f1462009-06-12 17:36:5538#include "chrome/common/chrome_descriptors.h"
[email protected]4730db92009-07-22 00:40:4839#include "chrome/common/chrome_switches.h"
[email protected]cf3ac3972010-12-22 20:02:2940#include "chrome/common/font_config_ipc_linux.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]415c2cd2011-03-11 21:56:1147#include "content/common/main_function_params.h"
[email protected]c9e45da02009-08-05 17:35:0848#include "media/base/media.h"
[email protected]808ea572010-09-01 16:22:0149#include "seccompsandbox/sandbox.h"
[email protected]cf3ac3972010-12-22 20:02:2950#include "skia/ext/SkFontHost_fontconfig_control.h"
[email protected]1831acf2009-10-05 16:38:4151#include "unicode/timezone.h"
52
[email protected]58680ce2010-09-18 00:09:1553#if defined(ARCH_CPU_X86_FAMILY) && !defined(CHROMIUM_SELINUX) && \
54 !defined(__clang__)
[email protected]36ea6c6f2010-03-17 20:08:0155// The seccomp sandbox is enabled on all ia32 and x86-64 processor as long as
[email protected]58680ce2010-09-18 00:09:1556// we aren't using SELinux or clang.
[email protected]36ea6c6f2010-03-17 20:08:0157#define SECCOMP_SANDBOX
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]36ea6c6f2010-03-17 20:08:0166#if defined(SECCOMP_SANDBOX)
[email protected]4f03cbc2009-11-19 00:50:4867// |g_proc_fd| is used only by the seccomp sandbox.
[email protected]a9c54a172009-11-07 06:09:3868static int g_proc_fd = -1;
[email protected]4f03cbc2009-11-19 00:50:4869#endif
[email protected]73fa63992009-07-20 20:30:0770
[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]715b4f262010-07-13 14:17:2896 explicit Zygote(int sandbox_flags)
97 : sandbox_flags_(sandbox_flags) {
98 }
99
[email protected]cc8f1462009-06-12 17:36:55100 bool ProcessRequests() {
101 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
102 // browser on it.
[email protected]8ecd3aad2009-11-04 08:32:22103 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
[email protected]abe3ad92009-06-15 18:15:08104 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:55105
106 // We need to accept SIGCHLD, even though our handler is a no-op because
107 // otherwise we cannot wait on children. (According to POSIX 2001.)
108 struct sigaction action;
109 memset(&action, 0, sizeof(action));
110 action.sa_handler = SIGCHLDHandler;
111 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
112
[email protected]8ecd3aad2009-11-04 08:32:22113 if (g_suid_sandbox_active) {
114 // Let the ZygoteHost know we are ready to go.
115 // The receiving code is in chrome/browser/zygote_host_linux.cc.
116 std::vector<int> empty;
[email protected]cf3ac3972010-12-22 20:02:29117 bool r = UnixDomainSocket::SendMsg(kBrowserDescriptor, kZygoteMagic,
118 sizeof(kZygoteMagic), empty);
[email protected]8ecd3aad2009-11-04 08:32:22119 CHECK(r) << "Sending zygote magic failed";
120 }
121
[email protected]cc8f1462009-06-12 17:36:55122 for (;;) {
[email protected]c548be22010-03-08 12:55:57123 // This function call can return multiple times, once per fork().
[email protected]8ecd3aad2009-11-04 08:32:22124 if (HandleRequestFromBrowser(kBrowserDescriptor))
[email protected]cc8f1462009-06-12 17:36:55125 return true;
126 }
127 }
128
129 private:
130 // See comment below, where sigaction is called.
131 static void SIGCHLDHandler(int signal) { }
132
133 // ---------------------------------------------------------------------------
134 // Requests from the browser...
135
136 // Read and process a request from the browser. Returns true if we are in a
137 // new process and thus need to unwind back into ChromeMain.
138 bool HandleRequestFromBrowser(int fd) {
139 std::vector<int> fds;
[email protected]abe3ad92009-06-15 18:15:08140 static const unsigned kMaxMessageLength = 1024;
[email protected]cc8f1462009-06-12 17:36:55141 char buf[kMaxMessageLength];
[email protected]cf3ac3972010-12-22 20:02:29142 const ssize_t len = UnixDomainSocket::RecvMsg(fd, buf, sizeof(buf), &fds);
[email protected]7ae27b92010-09-24 17:33:36143
144 if (len == 0 || (len == -1 && errno == ECONNRESET)) {
145 // EOF from the browser. We should die.
146 _exit(0);
[email protected]cc8f1462009-06-12 17:36:55147 return false;
148 }
149
[email protected]7ae27b92010-09-24 17:33:36150 if (len == -1) {
151 PLOG(ERROR) << "Error reading message from browser";
[email protected]cc8f1462009-06-12 17:36:55152 return false;
153 }
154
155 Pickle pickle(buf, len);
156 void* iter = NULL;
157
158 int kind;
[email protected]57113ea2009-06-18 02:23:16159 if (pickle.ReadInt(&iter, &kind)) {
160 switch (kind) {
161 case ZygoteHost::kCmdFork:
[email protected]c548be22010-03-08 12:55:57162 // This function call can return multiple times, once per fork().
[email protected]57113ea2009-06-18 02:23:16163 return HandleForkRequest(fd, pickle, iter, fds);
164 case ZygoteHost::kCmdReap:
165 if (!fds.empty())
166 break;
[email protected]c548be22010-03-08 12:55:57167 HandleReapRequest(fd, pickle, iter);
168 return false;
[email protected]443b80e2010-12-14 00:42:23169 case ZygoteHost::kCmdGetTerminationStatus:
[email protected]57113ea2009-06-18 02:23:16170 if (!fds.empty())
171 break;
[email protected]443b80e2010-12-14 00:42:23172 HandleGetTerminationStatus(fd, pickle, iter);
[email protected]c548be22010-03-08 12:55:57173 return false;
[email protected]715b4f262010-07-13 14:17:28174 case ZygoteHost::kCmdGetSandboxStatus:
175 HandleGetSandboxStatus(fd, pickle, iter);
176 return false;
[email protected]57113ea2009-06-18 02:23:16177 default:
178 NOTREACHED();
179 break;
180 }
[email protected]cc8f1462009-06-12 17:36:55181 }
182
[email protected]cc8f1462009-06-12 17:36:55183 LOG(WARNING) << "Error parsing message from browser";
184 for (std::vector<int>::const_iterator
185 i = fds.begin(); i != fds.end(); ++i)
186 close(*i);
187 return false;
188 }
189
[email protected]c548be22010-03-08 12:55:57190 void HandleReapRequest(int fd, const Pickle& pickle, void* iter) {
[email protected]8ecd3aad2009-11-04 08:32:22191 base::ProcessId child;
192 base::ProcessId actual_child;
[email protected]cc8f1462009-06-12 17:36:55193
194 if (!pickle.ReadInt(&iter, &child)) {
195 LOG(WARNING) << "Error parsing reap request from browser";
[email protected]c548be22010-03-08 12:55:57196 return;
[email protected]cc8f1462009-06-12 17:36:55197 }
198
[email protected]8ecd3aad2009-11-04 08:32:22199 if (g_suid_sandbox_active) {
200 actual_child = real_pids_to_sandbox_pids[child];
201 if (!actual_child)
[email protected]c548be22010-03-08 12:55:57202 return;
[email protected]8ecd3aad2009-11-04 08:32:22203 real_pids_to_sandbox_pids.erase(child);
204 } else {
205 actual_child = child;
206 }
207
208 ProcessWatcher::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55209 }
210
[email protected]443b80e2010-12-14 00:42:23211 void HandleGetTerminationStatus(int fd, const Pickle& pickle, void* iter) {
[email protected]57113ea2009-06-18 02:23:16212 base::ProcessHandle child;
213
214 if (!pickle.ReadInt(&iter, &child)) {
[email protected]443b80e2010-12-14 00:42:23215 LOG(WARNING) << "Error parsing GetTerminationStatus request "
216 << "from browser";
[email protected]c548be22010-03-08 12:55:57217 return;
[email protected]57113ea2009-06-18 02:23:16218 }
219
[email protected]443b80e2010-12-14 00:42:23220 base::TerminationStatus status;
221 int exit_code;
[email protected]8ecd3aad2009-11-04 08:32:22222 if (g_suid_sandbox_active)
223 child = real_pids_to_sandbox_pids[child];
[email protected]443b80e2010-12-14 00:42:23224 if (child) {
225 status = base::GetTerminationStatus(child, &exit_code);
226 } else {
227 // Assume that if we can't find the child in the sandbox, then
228 // it terminated normally.
229 status = base::TERMINATION_STATUS_NORMAL_TERMINATION;
230 exit_code = ResultCodes::NORMAL_EXIT;
231 }
[email protected]57113ea2009-06-18 02:23:16232
233 Pickle write_pickle;
[email protected]443b80e2010-12-14 00:42:23234 write_pickle.WriteInt(static_cast<int>(status));
235 write_pickle.WriteInt(exit_code);
[email protected]8a861402011-01-28 19:59:11236 ssize_t written =
237 HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size()));
238 if (written != static_cast<ssize_t>(write_pickle.size()))
[email protected]19cb9292010-04-16 23:00:15239 PLOG(ERROR) << "write";
[email protected]57113ea2009-06-18 02:23:16240 }
241
[email protected]28ecba32010-09-16 10:03:09242 // This is equivalent to fork(), except that, when using the SUID
243 // sandbox, it returns the real PID of the child process as it
244 // appears outside the sandbox, rather than returning the PID inside
245 // the sandbox.
246 int ForkWithRealPid() {
247 if (!g_suid_sandbox_active)
248 return fork();
249
250 int dummy_fd;
251 ino_t dummy_inode;
252 int pipe_fds[2] = { -1, -1 };
[email protected]073040b2010-09-26 02:35:45253 base::ProcessId pid = 0;
[email protected]28ecba32010-09-16 10:03:09254
255 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
256 if (dummy_fd < 0) {
257 LOG(ERROR) << "Failed to create dummy FD";
258 goto error;
259 }
260 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd)) {
261 LOG(ERROR) << "Failed to get inode for dummy FD";
262 goto error;
263 }
264 if (pipe(pipe_fds) != 0) {
265 LOG(ERROR) << "Failed to create pipe";
266 goto error;
267 }
268
269 pid = fork();
270 if (pid < 0) {
271 goto error;
272 } else if (pid == 0) {
273 // In the child process.
274 close(pipe_fds[1]);
275 char buffer[1];
276 // Wait until the parent process has discovered our PID. We
277 // should not fork any child processes (which the seccomp
278 // sandbox does) until then, because that can interfere with the
279 // parent's discovery of our PID.
280 if (HANDLE_EINTR(read(pipe_fds[0], buffer, 1)) != 1 ||
281 buffer[0] != 'x') {
282 LOG(FATAL) << "Failed to synchronise with parent zygote process";
283 }
284 close(pipe_fds[0]);
285 close(dummy_fd);
286 return 0;
287 } else {
288 // In the parent process.
289 close(dummy_fd);
290 dummy_fd = -1;
291 close(pipe_fds[0]);
292 pipe_fds[0] = -1;
293 uint8_t reply_buf[512];
294 Pickle request;
295 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
296 request.WriteUInt64(dummy_inode);
297
[email protected]cf3ac3972010-12-22 20:02:29298 const ssize_t r = UnixDomainSocket::SendRecvMsg(
299 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL,
300 request);
[email protected]28ecba32010-09-16 10:03:09301 if (r == -1) {
302 LOG(ERROR) << "Failed to get child process's real PID";
303 goto error;
304 }
305
306 base::ProcessId real_pid;
307 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
308 void* iter2 = NULL;
309 if (!reply.ReadInt(&iter2, &real_pid))
310 goto error;
[email protected]073040b2010-09-26 02:35:45311 if (real_pid <= 0) {
312 // METHOD_GET_CHILD_WITH_INODE failed. Did the child die already?
313 LOG(ERROR) << "METHOD_GET_CHILD_WITH_INODE failed";
314 goto error;
315 }
[email protected]28ecba32010-09-16 10:03:09316 real_pids_to_sandbox_pids[real_pid] = pid;
317 if (HANDLE_EINTR(write(pipe_fds[1], "x", 1)) != 1) {
318 LOG(ERROR) << "Failed to synchronise with child process";
319 goto error;
320 }
321 close(pipe_fds[1]);
322 return real_pid;
323 }
324
325 error:
[email protected]72f891f42011-01-12 17:00:52326 if (pid > 0) {
327 if (waitpid(pid, NULL, WNOHANG) == -1)
328 LOG(ERROR) << "Failed to wait for process";
329 }
[email protected]28ecba32010-09-16 10:03:09330 if (dummy_fd >= 0)
331 close(dummy_fd);
332 if (pipe_fds[0] >= 0)
333 close(pipe_fds[0]);
334 if (pipe_fds[1] >= 0)
335 close(pipe_fds[1]);
336 return -1;
337 }
338
[email protected]cc8f1462009-06-12 17:36:55339 // Handle a 'fork' request from the browser: this means that the browser
340 // wishes to start a new renderer.
[email protected]8ecd3aad2009-11-04 08:32:22341 bool HandleForkRequest(int fd, const Pickle& pickle, void* iter,
[email protected]cc8f1462009-06-12 17:36:55342 std::vector<int>& fds) {
343 std::vector<std::string> args;
344 int argc, numfds;
345 base::GlobalDescriptors::Mapping mapping;
[email protected]8ecd3aad2009-11-04 08:32:22346 base::ProcessId child;
[email protected]cc8f1462009-06-12 17:36:55347
348 if (!pickle.ReadInt(&iter, &argc))
349 goto error;
350
351 for (int i = 0; i < argc; ++i) {
352 std::string arg;
353 if (!pickle.ReadString(&iter, &arg))
354 goto error;
355 args.push_back(arg);
356 }
357
358 if (!pickle.ReadInt(&iter, &numfds))
359 goto error;
360 if (numfds != static_cast<int>(fds.size()))
361 goto error;
362
363 for (int i = 0; i < numfds; ++i) {
364 base::GlobalDescriptors::Key key;
365 if (!pickle.ReadUInt32(&iter, &key))
366 goto error;
367 mapping.push_back(std::make_pair(key, fds[i]));
368 }
369
[email protected]abe3ad92009-06-15 18:15:08370 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22371 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
372
[email protected]28ecba32010-09-16 10:03:09373 child = ForkWithRealPid();
[email protected]cc8f1462009-06-12 17:36:55374
375 if (!child) {
[email protected]36ea6c6f2010-03-17 20:08:01376#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38377 // Try to open /proc/self/maps as the seccomp sandbox needs access to it
378 if (g_proc_fd >= 0) {
379 int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY);
380 if (proc_self_maps >= 0) {
381 SeccompSandboxSetProcSelfMaps(proc_self_maps);
382 }
383 close(g_proc_fd);
384 g_proc_fd = -1;
385 }
[email protected]8015de32009-11-18 04:13:33386#endif
[email protected]a9c54a172009-11-07 06:09:38387
[email protected]8ecd3aad2009-11-04 08:32:22388 close(kBrowserDescriptor); // our socket from the browser
[email protected]cbcf9cc32010-02-17 04:05:59389 if (g_suid_sandbox_active)
390 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]9b85081af2010-12-07 21:26:47391 base::GlobalDescriptors::GetInstance()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39392
[email protected]a89a55dd2010-04-19 14:51:13393#if defined(CHROMIUM_SELINUX)
394 SELinuxTransitionToTypeOrDie("chromium_renderer_t");
395#endif
396
[email protected]0189bbd2009-10-12 22:50:39397 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55398 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39399 CommandLine::Init(0, NULL);
400 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]74e9fa22010-12-29 21:06:43401
402 // Update the process title. The argv was already cached by the call to
403 // SetProcessTitleFromCommandLine in ChromeMain, so we can pass NULL here
404 // (we don't have the original argv at this point).
405 SetProcessTitleFromCommandLine(NULL);
406
[email protected]c548be22010-03-08 12:55:57407 // The fork() request is handled further up the call stack.
[email protected]cc8f1462009-06-12 17:36:55408 return true;
[email protected]8ecd3aad2009-11-04 08:32:22409 } else if (child < 0) {
[email protected]466cffd2010-06-09 18:10:56410 LOG(ERROR) << "Zygote could not fork: " << errno;
[email protected]8ecd3aad2009-11-04 08:32:22411 goto error;
[email protected]cc8f1462009-06-12 17:36:55412 }
413
[email protected]28ecba32010-09-16 10:03:09414 for (std::vector<int>::const_iterator
415 i = fds.begin(); i != fds.end(); ++i)
416 close(*i);
[email protected]83a0cbe2009-11-04 04:22:47417
[email protected]28ecba32010-09-16 10:03:09418 if (HANDLE_EINTR(write(fd, &child, sizeof(child))) < 0)
419 PLOG(ERROR) << "write";
420 return false;
[email protected]83a0cbe2009-11-04 04:22:47421
422 error:
[email protected]8ecd3aad2009-11-04 08:32:22423 LOG(ERROR) << "Error parsing fork request from browser";
[email protected]83a0cbe2009-11-04 04:22:47424 for (std::vector<int>::const_iterator
425 i = fds.begin(); i != fds.end(); ++i)
426 close(*i);
[email protected]cc8f1462009-06-12 17:36:55427 return false;
428 }
[email protected]8ecd3aad2009-11-04 08:32:22429
[email protected]715b4f262010-07-13 14:17:28430 bool HandleGetSandboxStatus(int fd, const Pickle& pickle, void* iter) {
431 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_)) !=
432 sizeof(sandbox_flags_))) {
433 PLOG(ERROR) << "write";
434 }
435
436 return false;
437 }
438
[email protected]8ecd3aad2009-11-04 08:32:22439 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
440 // fork() returns are not the real PIDs, so we need to map the Real PIDS
441 // into the sandbox PID namespace.
442 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
443 ProcessMap real_pids_to_sandbox_pids;
[email protected]715b4f262010-07-13 14:17:28444
445 const int sandbox_flags_;
[email protected]cc8f1462009-06-12 17:36:55446};
447
[email protected]ad6d2c42009-09-15 20:13:38448// With SELinux we can carve out a precise sandbox, so we don't have to play
449// with intercepting libc calls.
450#if !defined(CHROMIUM_SELINUX)
451
[email protected]a0f200ea2009-08-06 18:44:06452static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
453 char* timezone_out,
454 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07455 Pickle request;
456 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
457 request.WriteString(
458 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
459
460 uint8_t reply_buf[512];
[email protected]cf3ac3972010-12-22 20:02:29461 const ssize_t r = UnixDomainSocket::SendRecvMsg(
[email protected]73fa63992009-07-20 20:30:07462 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
463 if (r == -1) {
464 memset(output, 0, sizeof(struct tm));
465 return;
466 }
467
468 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
469 void* iter = NULL;
[email protected]9debcda52009-07-22 20:06:51470 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07471 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51472 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07473 result.size() != sizeof(struct tm)) {
474 memset(output, 0, sizeof(struct tm));
475 return;
476 }
477
478 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51479 if (timezone_out_len) {
480 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
481 memcpy(timezone_out, timezone.data(), copy_len);
482 timezone_out[copy_len] = 0;
483 output->tm_zone = timezone_out;
484 } else {
485 output->tm_zone = NULL;
486 }
[email protected]73fa63992009-07-20 20:30:07487}
488
[email protected]a0f200ea2009-08-06 18:44:06489static bool g_am_zygote_or_renderer = false;
490
491// Sandbox interception of libc calls.
492//
493// Because we are running in a sandbox certain libc calls will fail (localtime
494// being the motivating example - it needs to read /etc/localtime). We need to
495// intercept these calls and proxy them to the browser. However, these calls
496// may come from us or from our libraries. In some cases we can't just change
497// our code.
498//
499// It's for these cases that we have the following setup:
500//
501// We define global functions for those functions which we wish to override.
502// Since we will be first in the dynamic resolution order, the dynamic linker
503// will point callers to our versions of these functions. However, we have the
504// same binary for both the browser and the renderers, which means that our
505// overrides will apply in the browser too.
506//
507// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
508// renderer process. It's set in ZygoteMain and inherited by the renderers when
509// they fork. (This means that it'll be incorrect for global constructor
510// functions and before ZygoteMain is called - beware).
511//
512// Our replacement functions can check this global and either proxy
513// the call to the browser over the sandbox IPC
514// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
515// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
516// current module.
517//
518// Other avenues:
519//
520// Our first attempt involved some assembly to patch the GOT of the current
521// module. This worked, but was platform specific and doesn't catch the case
522// where a library makes a call rather than current module.
523//
524// We also considered patching the function in place, but this would again by
525// platform specific and the above technique seems to work well enough.
526
[email protected]724df992010-09-21 18:19:10527typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
528typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
529 struct tm* result);
530
531static pthread_once_t g_libc_localtime_funcs_guard = PTHREAD_ONCE_INIT;
532static LocaltimeFunction g_libc_localtime;
533static LocaltimeRFunction g_libc_localtime_r;
534
535static void InitLibcLocaltimeFunctions() {
536 g_libc_localtime = reinterpret_cast<LocaltimeFunction>(
537 dlsym(RTLD_NEXT, "localtime"));
538 g_libc_localtime_r = reinterpret_cast<LocaltimeRFunction>(
539 dlsym(RTLD_NEXT, "localtime_r"));
540
541 if (!g_libc_localtime || !g_libc_localtime_r) {
542 // http://code.google.com/p/chromium/issues/detail?id=16800
543 //
544 // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces
545 // it with a version which doesn't work. In this case we'll get a NULL
546 // result. There's not a lot we can do at this point, so we just bodge it!
547 LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been "
548 "reported to be caused by Nvidia's libGL. You should expect"
549 " time related functions to misbehave. "
550 "http://code.google.com/p/chromium/issues/detail?id=16800";
551 }
552
553 if (!g_libc_localtime)
554 g_libc_localtime = gmtime;
555 if (!g_libc_localtime_r)
556 g_libc_localtime_r = gmtime_r;
557}
[email protected]a5d7bb82009-09-08 22:30:58558
[email protected]73fa63992009-07-20 20:30:07559struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06560 if (g_am_zygote_or_renderer) {
561 static struct tm time_struct;
562 static char timezone_string[64];
563 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
564 sizeof(timezone_string));
565 return &time_struct;
566 } else {
[email protected]724df992010-09-21 18:19:10567 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
568 InitLibcLocaltimeFunctions));
569 return g_libc_localtime(timep);
[email protected]a0f200ea2009-08-06 18:44:06570 }
[email protected]73fa63992009-07-20 20:30:07571}
572
573struct tm* localtime_r(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06574 if (g_am_zygote_or_renderer) {
575 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
576 return result;
577 } else {
[email protected]724df992010-09-21 18:19:10578 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
579 InitLibcLocaltimeFunctions));
580 return g_libc_localtime_r(timep, result);
[email protected]a0f200ea2009-08-06 18:44:06581 }
[email protected]73fa63992009-07-20 20:30:07582}
583
[email protected]ad6d2c42009-09-15 20:13:38584#endif // !CHROMIUM_SELINUX
[email protected]a5d7bb82009-09-08 22:30:58585
[email protected]ad6d2c42009-09-15 20:13:38586// This function triggers the static and lazy construction of objects that need
587// to be created before imposing the sandbox.
588static void PreSandboxInit() {
[email protected]b8419412010-03-29 20:18:29589 base::RandUint64();
[email protected]4378a822009-07-08 01:15:14590
[email protected]b8419412010-03-29 20:18:29591 base::SysInfo::MaxSharedMemorySize();
[email protected]80a086c52009-08-04 17:52:04592
[email protected]b8419412010-03-29 20:18:29593 // ICU DateFormat class (used in base/time_format.cc) needs to get the
594 // Olson timezone ID by accessing the zoneinfo files on disk. After
595 // TimeZone::createDefault is called once here, the timezone ID is
596 // cached and there's no more need to access the file system.
597 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
[email protected]1831acf2009-10-05 16:38:41598
[email protected]b8419412010-03-29 20:18:29599 FilePath module_path;
600 if (PathService::Get(base::DIR_MODULE, &module_path))
601 media::InitializeMediaLibrary(module_path);
[email protected]17773042010-07-28 17:35:07602
603 // Ensure access to the Pepper plugins before the sandbox is turned on.
604 PepperPluginRegistry::PreloadModules();
[email protected]ad6d2c42009-09-15 20:13:38605}
606
607#if !defined(CHROMIUM_SELINUX)
608static bool EnterSandbox() {
[email protected]b8419412010-03-29 20:18:29609 // The SUID sandbox sets this environment variable to a file descriptor
610 // over which we can signal that we have completed our startup and can be
611 // chrooted.
[email protected]ad6d2c42009-09-15 20:13:38612 const char* const sandbox_fd_string = getenv("SBX_D");
[email protected]ad6d2c42009-09-15 20:13:38613
[email protected]0dc32322010-09-16 09:46:59614 if (sandbox_fd_string) {
615 // Use the SUID sandbox. This still allows the seccomp sandbox to
616 // be enabled by the process later.
[email protected]8ecd3aad2009-11-04 08:32:22617 g_suid_sandbox_active = true;
618
[email protected]ad6d2c42009-09-15 20:13:38619 char* endptr;
620 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
621 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
622 return false;
623 const int fd = fd_long;
624
625 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08626
[email protected]eaebefaf2010-02-23 22:58:18627 static const char kMsgChrootMe = 'C';
628 static const char kMsgChrootSuccessful = 'O';
[email protected]abe3ad92009-06-15 18:15:08629
[email protected]eaebefaf2010-02-23 22:58:18630 if (HANDLE_EINTR(write(fd, &kMsgChrootMe, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32631 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08632 return false;
[email protected]0e1611122009-07-10 18:17:32633 }
[email protected]abe3ad92009-06-15 18:15:08634
[email protected]87ed6952009-07-16 02:52:15635 // We need to reap the chroot helper process in any event:
636 wait(NULL);
637
[email protected]abe3ad92009-06-15 18:15:08638 char reply;
[email protected]87f8ce62009-07-10 19:14:31639 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32640 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08641 return false;
[email protected]0e1611122009-07-10 18:17:32642 }
[email protected]87f8ce62009-07-10 19:14:31643
[email protected]eaebefaf2010-02-23 22:58:18644 if (reply != kMsgChrootSuccessful) {
[email protected]0e1611122009-07-10 18:17:32645 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08646 return false;
[email protected]0e1611122009-07-10 18:17:32647 }
[email protected]abe3ad92009-06-15 18:15:08648
[email protected]cf3ac3972010-12-22 20:02:29649 SkiaFontConfigSetImplementation(
650 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08651
[email protected]415493be2009-07-10 17:50:24652 // Previously, we required that the binary be non-readable. This causes the
653 // kernel to mark the process as non-dumpable at startup. The thinking was
654 // that, although we were putting the renderers into a PID namespace (with
655 // the SUID sandbox), they would nonetheless be in the /same/ PID
656 // namespace. So they could ptrace each other unless they were non-dumpable.
657 //
658 // If the binary was readable, then there would be a window between process
659 // startup and the point where we set the non-dumpable flag in which a
660 // compromised renderer could ptrace attach.
661 //
662 // However, now that we have a zygote model, only the (trusted) zygote
663 // exists at this point and we can set the non-dumpable flag which is
664 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48665 //
666 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
667 // issues, one can specify --allow-sandbox-debugging to let the process be
668 // dumpable.
669 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
670 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
671 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
672 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
673 LOG(ERROR) << "Failed to set non-dumpable flag";
674 return false;
675 }
[email protected]0e1611122009-07-10 18:17:32676 }
[email protected]cc9a9a882011-03-03 20:09:10677 } else if (CommandLine::ForCurrentProcess()->HasSwitch(
678 switches::kEnableSeccompSandbox)) {
[email protected]0dc32322010-09-16 09:46:59679 PreSandboxInit();
[email protected]cf3ac3972010-12-22 20:02:29680 SkiaFontConfigSetImplementation(
681 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08682 } else {
683 SkiaFontConfigUseDirectImplementation();
684 }
685
686 return true;
687}
[email protected]ad6d2c42009-09-15 20:13:38688#else // CHROMIUM_SELINUX
689
690static bool EnterSandbox() {
691 PreSandboxInit();
692 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
[email protected]ad6d2c42009-09-15 20:13:38693 return true;
694}
695
696#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08697
[email protected]cc8f1462009-06-12 17:36:55698bool ZygoteMain(const MainFunctionParams& params) {
[email protected]ad6d2c42009-09-15 20:13:38699#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06700 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38701#endif
[email protected]a0f200ea2009-08-06 18:44:06702
[email protected]36ea6c6f2010-03-17 20:08:01703#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38704 // The seccomp sandbox needs access to files in /proc, which might be denied
705 // after one of the other sandboxes have been started. So, obtain a suitable
706 // file handle in advance.
[email protected]cc9a9a882011-03-03 20:09:10707 if (CommandLine::ForCurrentProcess()->HasSwitch(
708 switches::kEnableSeccompSandbox)) {
[email protected]a9c54a172009-11-07 06:09:38709 g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY);
710 if (g_proc_fd < 0) {
711 LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp "
712 "sandboxing.";
713 }
714 }
[email protected]36ea6c6f2010-03-17 20:08:01715#endif // SECCOMP_SANDBOX
[email protected]a9c54a172009-11-07 06:09:38716
717 // Turn on the SELinux or SUID sandbox
718 if (!EnterSandbox()) {
719 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
720 << errno << ")";
721 return false;
722 }
723
[email protected]715b4f262010-07-13 14:17:28724 int sandbox_flags = 0;
725 if (getenv("SBX_D"))
726 sandbox_flags |= ZygoteHost::kSandboxSUID;
727 if (getenv("SBX_PID_NS"))
728 sandbox_flags |= ZygoteHost::kSandboxPIDNS;
729 if (getenv("SBX_NET_NS"))
730 sandbox_flags |= ZygoteHost::kSandboxNetNS;
731
[email protected]36ea6c6f2010-03-17 20:08:01732#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38733 // The seccomp sandbox will be turned on when the renderers start. But we can
734 // already check if sufficient support is available so that we only need to
735 // print one error message for the entire browser session.
[email protected]cc9a9a882011-03-03 20:09:10736 if (g_proc_fd >= 0 && CommandLine::ForCurrentProcess()->HasSwitch(
737 switches::kEnableSeccompSandbox)) {
[email protected]a9c54a172009-11-07 06:09:38738 if (!SupportsSeccompSandbox(g_proc_fd)) {
[email protected]e8c916a2009-11-04 17:52:47739 // There are a good number of users who cannot use the seccomp sandbox
740 // (e.g. because their distribution does not enable seccomp mode by
741 // default). While we would prefer to deny execution in this case, it
742 // seems more realistic to continue in degraded mode.
[email protected]1b5d28f2010-02-18 15:53:36743 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
744 "Seccomp sandbox. Running renderers with Seccomp "
745 "sandboxing disabled.";
[email protected]e8c916a2009-11-04 17:52:47746 } else {
[email protected]8e96e502010-10-21 20:57:12747 VLOG(1) << "Enabling experimental Seccomp sandbox.";
[email protected]715b4f262010-07-13 14:17:28748 sandbox_flags |= ZygoteHost::kSandboxSeccomp;
[email protected]e8c916a2009-11-04 17:52:47749 }
750 }
[email protected]36ea6c6f2010-03-17 20:08:01751#endif // SECCOMP_SANDBOX
[email protected]e8c916a2009-11-04 17:52:47752
[email protected]715b4f262010-07-13 14:17:28753 Zygote zygote(sandbox_flags);
[email protected]c548be22010-03-08 12:55:57754 // This function call can return multiple times, once per fork().
[email protected]cc8f1462009-06-12 17:36:55755 return zygote.ProcessRequests();
756}