blob: a0d0ef02e5dd67995d3c8afc87d1dc7d0684d7cd [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]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]3b63f8f42011-03-28 01:54:1531#include "base/memory/scoped_ptr.h"
[email protected]c9e45da02009-08-05 17:35:0832#include "base/path_service.h"
[email protected]cc8f1462009-06-12 17:36:5533#include "base/pickle.h"
[email protected]4d36536b2010-08-20 06:23:2734#include "base/process_util.h"
[email protected]4378a822009-07-08 01:15:1435#include "base/rand_util.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]4b559b4d2011-04-14 17:37:1438#include "crypto/nss_util.h"
[email protected]1a40dd7f2011-03-17 11:49:2539#include "chrome/common/chrome_paths.h"
[email protected]4730db92009-07-22 00:40:4840#include "chrome/common/chrome_switches.h"
[email protected]17773042010-07-28 17:35:0741#include "chrome/common/pepper_plugin_registry.h"
[email protected]acb94722011-03-18 01:33:3442#include "content/common/chrome_descriptors.h"
[email protected]797c3552011-03-17 00:26:1843#include "content/common/font_config_ipc_linux.h"
[email protected]415c2cd2011-03-11 21:56:1144#include "content/common/main_function_params.h"
[email protected]4dd57932011-03-17 06:06:1245#include "content/common/process_watcher.h"
46#include "content/common/result_codes.h"
47#include "content/common/sandbox_methods_linux.h"
48#include "content/common/set_process_title.h"
[email protected]797c3552011-03-17 00:26:1849#include "content/common/unix_domain_socket_posix.h"
[email protected]c9e45da02009-08-05 17:35:0850#include "media/base/media.h"
[email protected]808ea572010-09-01 16:22:0151#include "seccompsandbox/sandbox.h"
[email protected]cf3ac3972010-12-22 20:02:2952#include "skia/ext/SkFontHost_fontconfig_control.h"
[email protected]1831acf2009-10-05 16:38:4153#include "unicode/timezone.h"
54
[email protected]58680ce2010-09-18 00:09:1555#if defined(ARCH_CPU_X86_FAMILY) && !defined(CHROMIUM_SELINUX) && \
56 !defined(__clang__)
[email protected]36ea6c6f2010-03-17 20:08:0157// The seccomp sandbox is enabled on all ia32 and x86-64 processor as long as
[email protected]58680ce2010-09-18 00:09:1558// we aren't using SELinux or clang.
[email protected]36ea6c6f2010-03-17 20:08:0159#define SECCOMP_SANDBOX
60#endif
61
[email protected]cc8f1462009-06-12 17:36:5562// http://code.google.com/p/chromium/wiki/LinuxZygote
63
[email protected]8ecd3aad2009-11-04 08:32:2264static const int kBrowserDescriptor = 3;
[email protected]73fa63992009-07-20 20:30:0765static const int kMagicSandboxIPCDescriptor = 5;
[email protected]8ecd3aad2009-11-04 08:32:2266static const int kZygoteIdDescriptor = 7;
67static bool g_suid_sandbox_active = false;
[email protected]36ea6c6f2010-03-17 20:08:0168#if defined(SECCOMP_SANDBOX)
[email protected]4f03cbc2009-11-19 00:50:4869// |g_proc_fd| is used only by the seccomp sandbox.
[email protected]a9c54a172009-11-07 06:09:3870static int g_proc_fd = -1;
[email protected]4f03cbc2009-11-19 00:50:4871#endif
[email protected]73fa63992009-07-20 20:30:0772
[email protected]a89a55dd2010-04-19 14:51:1373#if defined(CHROMIUM_SELINUX)
74static void SELinuxTransitionToTypeOrDie(const char* type) {
75 security_context_t security_context;
76 if (getcon(&security_context))
77 LOG(FATAL) << "Cannot get SELinux context";
78
79 context_t context = context_new(security_context);
80 context_type_set(context, type);
81 const int r = setcon(context_str(context));
82 context_free(context);
83 freecon(security_context);
84
85 if (r) {
86 LOG(FATAL) << "dynamic transition to type '" << type << "' failed. "
87 "(this binary has been built with SELinux support, but maybe "
88 "the policies haven't been loaded into the kernel?)";
89 }
90}
91#endif // CHROMIUM_SELINUX
92
[email protected]cc8f1462009-06-12 17:36:5593// This is the object which implements the zygote. The ZygoteMain function,
[email protected]61883442010-07-12 15:14:3494// which is called from ChromeMain, simply constructs one of these objects and
95// runs it.
[email protected]cc8f1462009-06-12 17:36:5596class Zygote {
97 public:
[email protected]715b4f262010-07-13 14:17:2898 explicit Zygote(int sandbox_flags)
99 : sandbox_flags_(sandbox_flags) {
100 }
101
[email protected]cc8f1462009-06-12 17:36:55102 bool ProcessRequests() {
103 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
104 // browser on it.
[email protected]8ecd3aad2009-11-04 08:32:22105 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
[email protected]abe3ad92009-06-15 18:15:08106 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:55107
108 // We need to accept SIGCHLD, even though our handler is a no-op because
109 // otherwise we cannot wait on children. (According to POSIX 2001.)
110 struct sigaction action;
111 memset(&action, 0, sizeof(action));
112 action.sa_handler = SIGCHLDHandler;
113 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
114
[email protected]8ecd3aad2009-11-04 08:32:22115 if (g_suid_sandbox_active) {
116 // Let the ZygoteHost know we are ready to go.
117 // The receiving code is in chrome/browser/zygote_host_linux.cc.
118 std::vector<int> empty;
[email protected]cf3ac3972010-12-22 20:02:29119 bool r = UnixDomainSocket::SendMsg(kBrowserDescriptor, kZygoteMagic,
120 sizeof(kZygoteMagic), empty);
[email protected]8ecd3aad2009-11-04 08:32:22121 CHECK(r) << "Sending zygote magic failed";
122 }
123
[email protected]cc8f1462009-06-12 17:36:55124 for (;;) {
[email protected]c548be22010-03-08 12:55:57125 // This function call can return multiple times, once per fork().
[email protected]8ecd3aad2009-11-04 08:32:22126 if (HandleRequestFromBrowser(kBrowserDescriptor))
[email protected]cc8f1462009-06-12 17:36:55127 return true;
128 }
129 }
130
131 private:
132 // See comment below, where sigaction is called.
133 static void SIGCHLDHandler(int signal) { }
134
135 // ---------------------------------------------------------------------------
136 // Requests from the browser...
137
138 // Read and process a request from the browser. Returns true if we are in a
139 // new process and thus need to unwind back into ChromeMain.
140 bool HandleRequestFromBrowser(int fd) {
141 std::vector<int> fds;
[email protected]2752bf62011-03-23 21:06:06142 static const unsigned kMaxMessageLength = 2048;
[email protected]cc8f1462009-06-12 17:36:55143 char buf[kMaxMessageLength];
[email protected]cf3ac3972010-12-22 20:02:29144 const ssize_t len = UnixDomainSocket::RecvMsg(fd, buf, sizeof(buf), &fds);
[email protected]7ae27b92010-09-24 17:33:36145
146 if (len == 0 || (len == -1 && errno == ECONNRESET)) {
147 // EOF from the browser. We should die.
148 _exit(0);
[email protected]cc8f1462009-06-12 17:36:55149 return false;
150 }
151
[email protected]7ae27b92010-09-24 17:33:36152 if (len == -1) {
153 PLOG(ERROR) << "Error reading message from browser";
[email protected]cc8f1462009-06-12 17:36:55154 return false;
155 }
156
157 Pickle pickle(buf, len);
158 void* iter = NULL;
159
160 int kind;
[email protected]57113ea2009-06-18 02:23:16161 if (pickle.ReadInt(&iter, &kind)) {
162 switch (kind) {
163 case ZygoteHost::kCmdFork:
[email protected]c548be22010-03-08 12:55:57164 // This function call can return multiple times, once per fork().
[email protected]57113ea2009-06-18 02:23:16165 return HandleForkRequest(fd, pickle, iter, fds);
166 case ZygoteHost::kCmdReap:
167 if (!fds.empty())
168 break;
[email protected]c548be22010-03-08 12:55:57169 HandleReapRequest(fd, pickle, iter);
170 return false;
[email protected]443b80e2010-12-14 00:42:23171 case ZygoteHost::kCmdGetTerminationStatus:
[email protected]57113ea2009-06-18 02:23:16172 if (!fds.empty())
173 break;
[email protected]443b80e2010-12-14 00:42:23174 HandleGetTerminationStatus(fd, pickle, iter);
[email protected]c548be22010-03-08 12:55:57175 return false;
[email protected]715b4f262010-07-13 14:17:28176 case ZygoteHost::kCmdGetSandboxStatus:
177 HandleGetSandboxStatus(fd, pickle, iter);
178 return false;
[email protected]57113ea2009-06-18 02:23:16179 default:
180 NOTREACHED();
181 break;
182 }
[email protected]cc8f1462009-06-12 17:36:55183 }
184
[email protected]cc8f1462009-06-12 17:36:55185 LOG(WARNING) << "Error parsing message from browser";
186 for (std::vector<int>::const_iterator
187 i = fds.begin(); i != fds.end(); ++i)
188 close(*i);
189 return false;
190 }
191
[email protected]c548be22010-03-08 12:55:57192 void HandleReapRequest(int fd, const Pickle& pickle, void* iter) {
[email protected]8ecd3aad2009-11-04 08:32:22193 base::ProcessId child;
194 base::ProcessId actual_child;
[email protected]cc8f1462009-06-12 17:36:55195
196 if (!pickle.ReadInt(&iter, &child)) {
197 LOG(WARNING) << "Error parsing reap request from browser";
[email protected]c548be22010-03-08 12:55:57198 return;
[email protected]cc8f1462009-06-12 17:36:55199 }
200
[email protected]8ecd3aad2009-11-04 08:32:22201 if (g_suid_sandbox_active) {
202 actual_child = real_pids_to_sandbox_pids[child];
203 if (!actual_child)
[email protected]c548be22010-03-08 12:55:57204 return;
[email protected]8ecd3aad2009-11-04 08:32:22205 real_pids_to_sandbox_pids.erase(child);
206 } else {
207 actual_child = child;
208 }
209
210 ProcessWatcher::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55211 }
212
[email protected]443b80e2010-12-14 00:42:23213 void HandleGetTerminationStatus(int fd, const Pickle& pickle, void* iter) {
[email protected]57113ea2009-06-18 02:23:16214 base::ProcessHandle child;
215
216 if (!pickle.ReadInt(&iter, &child)) {
[email protected]443b80e2010-12-14 00:42:23217 LOG(WARNING) << "Error parsing GetTerminationStatus request "
218 << "from browser";
[email protected]c548be22010-03-08 12:55:57219 return;
[email protected]57113ea2009-06-18 02:23:16220 }
221
[email protected]443b80e2010-12-14 00:42:23222 base::TerminationStatus status;
223 int exit_code;
[email protected]8ecd3aad2009-11-04 08:32:22224 if (g_suid_sandbox_active)
225 child = real_pids_to_sandbox_pids[child];
[email protected]443b80e2010-12-14 00:42:23226 if (child) {
227 status = base::GetTerminationStatus(child, &exit_code);
228 } else {
229 // Assume that if we can't find the child in the sandbox, then
230 // it terminated normally.
231 status = base::TERMINATION_STATUS_NORMAL_TERMINATION;
232 exit_code = ResultCodes::NORMAL_EXIT;
233 }
[email protected]57113ea2009-06-18 02:23:16234
235 Pickle write_pickle;
[email protected]443b80e2010-12-14 00:42:23236 write_pickle.WriteInt(static_cast<int>(status));
237 write_pickle.WriteInt(exit_code);
[email protected]8a861402011-01-28 19:59:11238 ssize_t written =
239 HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size()));
240 if (written != static_cast<ssize_t>(write_pickle.size()))
[email protected]19cb9292010-04-16 23:00:15241 PLOG(ERROR) << "write";
[email protected]57113ea2009-06-18 02:23:16242 }
243
[email protected]28ecba32010-09-16 10:03:09244 // This is equivalent to fork(), except that, when using the SUID
245 // sandbox, it returns the real PID of the child process as it
246 // appears outside the sandbox, rather than returning the PID inside
247 // the sandbox.
248 int ForkWithRealPid() {
249 if (!g_suid_sandbox_active)
250 return fork();
251
252 int dummy_fd;
253 ino_t dummy_inode;
254 int pipe_fds[2] = { -1, -1 };
[email protected]073040b2010-09-26 02:35:45255 base::ProcessId pid = 0;
[email protected]28ecba32010-09-16 10:03:09256
257 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
258 if (dummy_fd < 0) {
259 LOG(ERROR) << "Failed to create dummy FD";
260 goto error;
261 }
262 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd)) {
263 LOG(ERROR) << "Failed to get inode for dummy FD";
264 goto error;
265 }
266 if (pipe(pipe_fds) != 0) {
267 LOG(ERROR) << "Failed to create pipe";
268 goto error;
269 }
270
271 pid = fork();
272 if (pid < 0) {
273 goto error;
274 } else if (pid == 0) {
275 // In the child process.
276 close(pipe_fds[1]);
277 char buffer[1];
278 // Wait until the parent process has discovered our PID. We
279 // should not fork any child processes (which the seccomp
280 // sandbox does) until then, because that can interfere with the
281 // parent's discovery of our PID.
282 if (HANDLE_EINTR(read(pipe_fds[0], buffer, 1)) != 1 ||
283 buffer[0] != 'x') {
284 LOG(FATAL) << "Failed to synchronise with parent zygote process";
285 }
286 close(pipe_fds[0]);
287 close(dummy_fd);
288 return 0;
289 } else {
290 // In the parent process.
291 close(dummy_fd);
292 dummy_fd = -1;
293 close(pipe_fds[0]);
294 pipe_fds[0] = -1;
295 uint8_t reply_buf[512];
296 Pickle request;
297 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
298 request.WriteUInt64(dummy_inode);
299
[email protected]cf3ac3972010-12-22 20:02:29300 const ssize_t r = UnixDomainSocket::SendRecvMsg(
301 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL,
302 request);
[email protected]28ecba32010-09-16 10:03:09303 if (r == -1) {
304 LOG(ERROR) << "Failed to get child process's real PID";
305 goto error;
306 }
307
308 base::ProcessId real_pid;
309 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
310 void* iter2 = NULL;
311 if (!reply.ReadInt(&iter2, &real_pid))
312 goto error;
[email protected]073040b2010-09-26 02:35:45313 if (real_pid <= 0) {
314 // METHOD_GET_CHILD_WITH_INODE failed. Did the child die already?
315 LOG(ERROR) << "METHOD_GET_CHILD_WITH_INODE failed";
316 goto error;
317 }
[email protected]28ecba32010-09-16 10:03:09318 real_pids_to_sandbox_pids[real_pid] = pid;
319 if (HANDLE_EINTR(write(pipe_fds[1], "x", 1)) != 1) {
320 LOG(ERROR) << "Failed to synchronise with child process";
321 goto error;
322 }
323 close(pipe_fds[1]);
324 return real_pid;
325 }
326
327 error:
[email protected]72f891f42011-01-12 17:00:52328 if (pid > 0) {
329 if (waitpid(pid, NULL, WNOHANG) == -1)
330 LOG(ERROR) << "Failed to wait for process";
331 }
[email protected]28ecba32010-09-16 10:03:09332 if (dummy_fd >= 0)
333 close(dummy_fd);
334 if (pipe_fds[0] >= 0)
335 close(pipe_fds[0]);
336 if (pipe_fds[1] >= 0)
337 close(pipe_fds[1]);
338 return -1;
339 }
340
[email protected]cc8f1462009-06-12 17:36:55341 // Handle a 'fork' request from the browser: this means that the browser
342 // wishes to start a new renderer.
[email protected]8ecd3aad2009-11-04 08:32:22343 bool HandleForkRequest(int fd, const Pickle& pickle, void* iter,
[email protected]cc8f1462009-06-12 17:36:55344 std::vector<int>& fds) {
345 std::vector<std::string> args;
346 int argc, numfds;
347 base::GlobalDescriptors::Mapping mapping;
[email protected]8ecd3aad2009-11-04 08:32:22348 base::ProcessId child;
[email protected]cc8f1462009-06-12 17:36:55349
350 if (!pickle.ReadInt(&iter, &argc))
351 goto error;
352
353 for (int i = 0; i < argc; ++i) {
354 std::string arg;
355 if (!pickle.ReadString(&iter, &arg))
356 goto error;
357 args.push_back(arg);
358 }
359
360 if (!pickle.ReadInt(&iter, &numfds))
361 goto error;
362 if (numfds != static_cast<int>(fds.size()))
363 goto error;
364
365 for (int i = 0; i < numfds; ++i) {
366 base::GlobalDescriptors::Key key;
367 if (!pickle.ReadUInt32(&iter, &key))
368 goto error;
369 mapping.push_back(std::make_pair(key, fds[i]));
370 }
371
[email protected]abe3ad92009-06-15 18:15:08372 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22373 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
374
[email protected]28ecba32010-09-16 10:03:09375 child = ForkWithRealPid();
[email protected]cc8f1462009-06-12 17:36:55376
377 if (!child) {
[email protected]36ea6c6f2010-03-17 20:08:01378#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38379 // Try to open /proc/self/maps as the seccomp sandbox needs access to it
380 if (g_proc_fd >= 0) {
381 int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY);
382 if (proc_self_maps >= 0) {
383 SeccompSandboxSetProcSelfMaps(proc_self_maps);
384 }
385 close(g_proc_fd);
386 g_proc_fd = -1;
387 }
[email protected]8015de32009-11-18 04:13:33388#endif
[email protected]a9c54a172009-11-07 06:09:38389
[email protected]8ecd3aad2009-11-04 08:32:22390 close(kBrowserDescriptor); // our socket from the browser
[email protected]cbcf9cc32010-02-17 04:05:59391 if (g_suid_sandbox_active)
392 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]9b85081af2010-12-07 21:26:47393 base::GlobalDescriptors::GetInstance()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39394
[email protected]a89a55dd2010-04-19 14:51:13395#if defined(CHROMIUM_SELINUX)
396 SELinuxTransitionToTypeOrDie("chromium_renderer_t");
397#endif
398
[email protected]0189bbd2009-10-12 22:50:39399 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55400 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39401 CommandLine::Init(0, NULL);
402 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]74e9fa22010-12-29 21:06:43403
404 // Update the process title. The argv was already cached by the call to
405 // SetProcessTitleFromCommandLine in ChromeMain, so we can pass NULL here
406 // (we don't have the original argv at this point).
407 SetProcessTitleFromCommandLine(NULL);
408
[email protected]c548be22010-03-08 12:55:57409 // The fork() request is handled further up the call stack.
[email protected]cc8f1462009-06-12 17:36:55410 return true;
[email protected]8ecd3aad2009-11-04 08:32:22411 } else if (child < 0) {
[email protected]466cffd2010-06-09 18:10:56412 LOG(ERROR) << "Zygote could not fork: " << errno;
[email protected]8ecd3aad2009-11-04 08:32:22413 goto error;
[email protected]cc8f1462009-06-12 17:36:55414 }
415
[email protected]28ecba32010-09-16 10:03:09416 for (std::vector<int>::const_iterator
417 i = fds.begin(); i != fds.end(); ++i)
418 close(*i);
[email protected]83a0cbe2009-11-04 04:22:47419
[email protected]28ecba32010-09-16 10:03:09420 if (HANDLE_EINTR(write(fd, &child, sizeof(child))) < 0)
421 PLOG(ERROR) << "write";
422 return false;
[email protected]83a0cbe2009-11-04 04:22:47423
424 error:
[email protected]8ecd3aad2009-11-04 08:32:22425 LOG(ERROR) << "Error parsing fork request from browser";
[email protected]83a0cbe2009-11-04 04:22:47426 for (std::vector<int>::const_iterator
427 i = fds.begin(); i != fds.end(); ++i)
428 close(*i);
[email protected]cc8f1462009-06-12 17:36:55429 return false;
430 }
[email protected]8ecd3aad2009-11-04 08:32:22431
[email protected]715b4f262010-07-13 14:17:28432 bool HandleGetSandboxStatus(int fd, const Pickle& pickle, void* iter) {
433 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_)) !=
434 sizeof(sandbox_flags_))) {
435 PLOG(ERROR) << "write";
436 }
437
438 return false;
439 }
440
[email protected]8ecd3aad2009-11-04 08:32:22441 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
442 // fork() returns are not the real PIDs, so we need to map the Real PIDS
443 // into the sandbox PID namespace.
444 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
445 ProcessMap real_pids_to_sandbox_pids;
[email protected]715b4f262010-07-13 14:17:28446
447 const int sandbox_flags_;
[email protected]cc8f1462009-06-12 17:36:55448};
449
[email protected]ad6d2c42009-09-15 20:13:38450// With SELinux we can carve out a precise sandbox, so we don't have to play
451// with intercepting libc calls.
452#if !defined(CHROMIUM_SELINUX)
453
[email protected]a0f200ea2009-08-06 18:44:06454static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
455 char* timezone_out,
456 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07457 Pickle request;
458 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
459 request.WriteString(
460 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
461
462 uint8_t reply_buf[512];
[email protected]cf3ac3972010-12-22 20:02:29463 const ssize_t r = UnixDomainSocket::SendRecvMsg(
[email protected]73fa63992009-07-20 20:30:07464 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
465 if (r == -1) {
466 memset(output, 0, sizeof(struct tm));
467 return;
468 }
469
470 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
471 void* iter = NULL;
[email protected]9debcda52009-07-22 20:06:51472 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07473 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51474 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07475 result.size() != sizeof(struct tm)) {
476 memset(output, 0, sizeof(struct tm));
477 return;
478 }
479
480 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51481 if (timezone_out_len) {
482 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
483 memcpy(timezone_out, timezone.data(), copy_len);
484 timezone_out[copy_len] = 0;
485 output->tm_zone = timezone_out;
486 } else {
487 output->tm_zone = NULL;
488 }
[email protected]73fa63992009-07-20 20:30:07489}
490
[email protected]a0f200ea2009-08-06 18:44:06491static bool g_am_zygote_or_renderer = false;
492
493// Sandbox interception of libc calls.
494//
495// Because we are running in a sandbox certain libc calls will fail (localtime
496// being the motivating example - it needs to read /etc/localtime). We need to
497// intercept these calls and proxy them to the browser. However, these calls
498// may come from us or from our libraries. In some cases we can't just change
499// our code.
500//
501// It's for these cases that we have the following setup:
502//
503// We define global functions for those functions which we wish to override.
504// Since we will be first in the dynamic resolution order, the dynamic linker
505// will point callers to our versions of these functions. However, we have the
506// same binary for both the browser and the renderers, which means that our
507// overrides will apply in the browser too.
508//
509// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
510// renderer process. It's set in ZygoteMain and inherited by the renderers when
511// they fork. (This means that it'll be incorrect for global constructor
512// functions and before ZygoteMain is called - beware).
513//
514// Our replacement functions can check this global and either proxy
515// the call to the browser over the sandbox IPC
516// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
517// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
518// current module.
519//
520// Other avenues:
521//
522// Our first attempt involved some assembly to patch the GOT of the current
523// module. This worked, but was platform specific and doesn't catch the case
524// where a library makes a call rather than current module.
525//
526// We also considered patching the function in place, but this would again by
527// platform specific and the above technique seems to work well enough.
528
[email protected]724df992010-09-21 18:19:10529typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
530typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
531 struct tm* result);
532
533static pthread_once_t g_libc_localtime_funcs_guard = PTHREAD_ONCE_INIT;
534static LocaltimeFunction g_libc_localtime;
535static LocaltimeRFunction g_libc_localtime_r;
536
537static void InitLibcLocaltimeFunctions() {
538 g_libc_localtime = reinterpret_cast<LocaltimeFunction>(
539 dlsym(RTLD_NEXT, "localtime"));
540 g_libc_localtime_r = reinterpret_cast<LocaltimeRFunction>(
541 dlsym(RTLD_NEXT, "localtime_r"));
542
543 if (!g_libc_localtime || !g_libc_localtime_r) {
544 // http://code.google.com/p/chromium/issues/detail?id=16800
545 //
546 // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces
547 // it with a version which doesn't work. In this case we'll get a NULL
548 // result. There's not a lot we can do at this point, so we just bodge it!
549 LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been "
550 "reported to be caused by Nvidia's libGL. You should expect"
551 " time related functions to misbehave. "
552 "http://code.google.com/p/chromium/issues/detail?id=16800";
553 }
554
555 if (!g_libc_localtime)
556 g_libc_localtime = gmtime;
557 if (!g_libc_localtime_r)
558 g_libc_localtime_r = gmtime_r;
559}
[email protected]a5d7bb82009-09-08 22:30:58560
[email protected]73fa63992009-07-20 20:30:07561struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06562 if (g_am_zygote_or_renderer) {
563 static struct tm time_struct;
564 static char timezone_string[64];
565 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
566 sizeof(timezone_string));
567 return &time_struct;
568 } else {
[email protected]724df992010-09-21 18:19:10569 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
570 InitLibcLocaltimeFunctions));
571 return g_libc_localtime(timep);
[email protected]a0f200ea2009-08-06 18:44:06572 }
[email protected]73fa63992009-07-20 20:30:07573}
574
575struct tm* localtime_r(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06576 if (g_am_zygote_or_renderer) {
577 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
578 return result;
579 } else {
[email protected]724df992010-09-21 18:19:10580 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
581 InitLibcLocaltimeFunctions));
582 return g_libc_localtime_r(timep, result);
[email protected]a0f200ea2009-08-06 18:44:06583 }
[email protected]73fa63992009-07-20 20:30:07584}
585
[email protected]ad6d2c42009-09-15 20:13:38586#endif // !CHROMIUM_SELINUX
[email protected]a5d7bb82009-09-08 22:30:58587
[email protected]ad6d2c42009-09-15 20:13:38588// This function triggers the static and lazy construction of objects that need
589// to be created before imposing the sandbox.
590static void PreSandboxInit() {
[email protected]b8419412010-03-29 20:18:29591 base::RandUint64();
[email protected]4378a822009-07-08 01:15:14592
[email protected]b8419412010-03-29 20:18:29593 base::SysInfo::MaxSharedMemorySize();
[email protected]80a086c52009-08-04 17:52:04594
[email protected]b8419412010-03-29 20:18:29595 // ICU DateFormat class (used in base/time_format.cc) needs to get the
596 // Olson timezone ID by accessing the zoneinfo files on disk. After
597 // TimeZone::createDefault is called once here, the timezone ID is
598 // cached and there's no more need to access the file system.
599 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
[email protected]1831acf2009-10-05 16:38:41600
[email protected]1a40dd7f2011-03-17 11:49:25601 // Each Renderer we spawn will re-attempt initialization of the media
602 // libraries, at which point failure will be detected and handled, so
603 // we do not need to cope with initialization failures here.
604 FilePath media_path;
605 if (PathService::Get(chrome::DIR_MEDIA_LIBS, &media_path))
606 media::InitializeMediaLibrary(media_path);
[email protected]eeb08552011-03-18 11:15:39607#if defined(USE_NSS)
[email protected]f6a67b42011-03-17 23:49:21608 // NSS libraries are loaded before sandbox is activated. This is to allow
609 // successful initialization of NSS which tries to load extra library files.
610 // Doing so will allow NSS to be used within sandbox for chromoting.
[email protected]4b559b4d2011-04-14 17:37:14611 crypto::LoadNSSLibraries();
[email protected]eeb08552011-03-18 11:15:39612#else
613 // TODO(bulach): implement openssl support.
614 NOTREACHED() << "Remoting is not supported for openssl";
[email protected]f6a67b42011-03-17 23:49:21615#endif
[email protected]ed450f32011-03-16 01:26:49616
[email protected]17773042010-07-28 17:35:07617 // Ensure access to the Pepper plugins before the sandbox is turned on.
618 PepperPluginRegistry::PreloadModules();
[email protected]ad6d2c42009-09-15 20:13:38619}
620
621#if !defined(CHROMIUM_SELINUX)
622static bool EnterSandbox() {
[email protected]b8419412010-03-29 20:18:29623 // The SUID sandbox sets this environment variable to a file descriptor
624 // over which we can signal that we have completed our startup and can be
625 // chrooted.
[email protected]ad6d2c42009-09-15 20:13:38626 const char* const sandbox_fd_string = getenv("SBX_D");
[email protected]ad6d2c42009-09-15 20:13:38627
[email protected]0dc32322010-09-16 09:46:59628 if (sandbox_fd_string) {
629 // Use the SUID sandbox. This still allows the seccomp sandbox to
630 // be enabled by the process later.
[email protected]8ecd3aad2009-11-04 08:32:22631 g_suid_sandbox_active = true;
632
[email protected]ad6d2c42009-09-15 20:13:38633 char* endptr;
634 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
635 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
636 return false;
637 const int fd = fd_long;
638
639 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08640
[email protected]eaebefaf2010-02-23 22:58:18641 static const char kMsgChrootMe = 'C';
642 static const char kMsgChrootSuccessful = 'O';
[email protected]abe3ad92009-06-15 18:15:08643
[email protected]eaebefaf2010-02-23 22:58:18644 if (HANDLE_EINTR(write(fd, &kMsgChrootMe, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32645 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[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]87ed6952009-07-16 02:52:15649 // We need to reap the chroot helper process in any event:
650 wait(NULL);
651
[email protected]abe3ad92009-06-15 18:15:08652 char reply;
[email protected]87f8ce62009-07-10 19:14:31653 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32654 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08655 return false;
[email protected]0e1611122009-07-10 18:17:32656 }
[email protected]87f8ce62009-07-10 19:14:31657
[email protected]eaebefaf2010-02-23 22:58:18658 if (reply != kMsgChrootSuccessful) {
[email protected]0e1611122009-07-10 18:17:32659 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08660 return false;
[email protected]0e1611122009-07-10 18:17:32661 }
[email protected]abe3ad92009-06-15 18:15:08662
[email protected]cf3ac3972010-12-22 20:02:29663 SkiaFontConfigSetImplementation(
664 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08665
[email protected]415493be2009-07-10 17:50:24666 // Previously, we required that the binary be non-readable. This causes the
667 // kernel to mark the process as non-dumpable at startup. The thinking was
668 // that, although we were putting the renderers into a PID namespace (with
669 // the SUID sandbox), they would nonetheless be in the /same/ PID
670 // namespace. So they could ptrace each other unless they were non-dumpable.
671 //
672 // If the binary was readable, then there would be a window between process
673 // startup and the point where we set the non-dumpable flag in which a
674 // compromised renderer could ptrace attach.
675 //
676 // However, now that we have a zygote model, only the (trusted) zygote
677 // exists at this point and we can set the non-dumpable flag which is
678 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48679 //
680 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
681 // issues, one can specify --allow-sandbox-debugging to let the process be
682 // dumpable.
683 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
684 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
685 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
686 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
687 LOG(ERROR) << "Failed to set non-dumpable flag";
688 return false;
689 }
[email protected]0e1611122009-07-10 18:17:32690 }
[email protected]cc9a9a882011-03-03 20:09:10691 } else if (CommandLine::ForCurrentProcess()->HasSwitch(
692 switches::kEnableSeccompSandbox)) {
[email protected]0dc32322010-09-16 09:46:59693 PreSandboxInit();
[email protected]cf3ac3972010-12-22 20:02:29694 SkiaFontConfigSetImplementation(
695 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08696 } else {
697 SkiaFontConfigUseDirectImplementation();
698 }
699
700 return true;
701}
[email protected]ad6d2c42009-09-15 20:13:38702#else // CHROMIUM_SELINUX
703
704static bool EnterSandbox() {
705 PreSandboxInit();
706 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
[email protected]ad6d2c42009-09-15 20:13:38707 return true;
708}
709
710#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08711
[email protected]cc8f1462009-06-12 17:36:55712bool ZygoteMain(const MainFunctionParams& params) {
[email protected]ad6d2c42009-09-15 20:13:38713#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06714 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38715#endif
[email protected]a0f200ea2009-08-06 18:44:06716
[email protected]36ea6c6f2010-03-17 20:08:01717#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38718 // The seccomp sandbox needs access to files in /proc, which might be denied
719 // after one of the other sandboxes have been started. So, obtain a suitable
720 // file handle in advance.
[email protected]cc9a9a882011-03-03 20:09:10721 if (CommandLine::ForCurrentProcess()->HasSwitch(
722 switches::kEnableSeccompSandbox)) {
[email protected]a9c54a172009-11-07 06:09:38723 g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY);
724 if (g_proc_fd < 0) {
725 LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp "
726 "sandboxing.";
727 }
728 }
[email protected]36ea6c6f2010-03-17 20:08:01729#endif // SECCOMP_SANDBOX
[email protected]a9c54a172009-11-07 06:09:38730
731 // Turn on the SELinux or SUID sandbox
732 if (!EnterSandbox()) {
733 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
734 << errno << ")";
735 return false;
736 }
737
[email protected]715b4f262010-07-13 14:17:28738 int sandbox_flags = 0;
739 if (getenv("SBX_D"))
740 sandbox_flags |= ZygoteHost::kSandboxSUID;
741 if (getenv("SBX_PID_NS"))
742 sandbox_flags |= ZygoteHost::kSandboxPIDNS;
743 if (getenv("SBX_NET_NS"))
744 sandbox_flags |= ZygoteHost::kSandboxNetNS;
745
[email protected]36ea6c6f2010-03-17 20:08:01746#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38747 // The seccomp sandbox will be turned on when the renderers start. But we can
748 // already check if sufficient support is available so that we only need to
749 // print one error message for the entire browser session.
[email protected]cc9a9a882011-03-03 20:09:10750 if (g_proc_fd >= 0 && CommandLine::ForCurrentProcess()->HasSwitch(
751 switches::kEnableSeccompSandbox)) {
[email protected]a9c54a172009-11-07 06:09:38752 if (!SupportsSeccompSandbox(g_proc_fd)) {
[email protected]e8c916a2009-11-04 17:52:47753 // There are a good number of users who cannot use the seccomp sandbox
754 // (e.g. because their distribution does not enable seccomp mode by
755 // default). While we would prefer to deny execution in this case, it
756 // seems more realistic to continue in degraded mode.
[email protected]1b5d28f2010-02-18 15:53:36757 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
758 "Seccomp sandbox. Running renderers with Seccomp "
759 "sandboxing disabled.";
[email protected]e8c916a2009-11-04 17:52:47760 } else {
[email protected]8e96e502010-10-21 20:57:12761 VLOG(1) << "Enabling experimental Seccomp sandbox.";
[email protected]715b4f262010-07-13 14:17:28762 sandbox_flags |= ZygoteHost::kSandboxSeccomp;
[email protected]e8c916a2009-11-04 17:52:47763 }
764 }
[email protected]36ea6c6f2010-03-17 20:08:01765#endif // SECCOMP_SANDBOX
[email protected]e8c916a2009-11-04 17:52:47766
[email protected]715b4f262010-07-13 14:17:28767 Zygote zygote(sandbox_flags);
[email protected]c548be22010-03-08 12:55:57768 // This function call can return multiple times, once per fork().
[email protected]cc8f1462009-06-12 17:36:55769 return zygote.ProcessRequests();
770}