blob: 99a773556ff960383693cdc3e3fe7a0d1ae05ce2 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]cc8f1462009-06-12 17:36:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e63c4d72011-05-31 22:38:295#include "content/browser/zygote_host_linux.h"
6
[email protected]a0f200ea2009-08-06 18:44:067#include <dlfcn.h>
[email protected]a9c54a172009-11-07 06:09:388#include <fcntl.h>
[email protected]724df992010-09-21 18:19:109#include <pthread.h>
[email protected]8ecd3aad2009-11-04 08:32:2210#include <sys/socket.h>
[email protected]a9c54a172009-11-07 06:09:3811#include <sys/stat.h>
[email protected]8ecd3aad2009-11-04 08:32:2212#include <sys/types.h>
[email protected]83a0cbe2009-11-04 04:22:4713#include <sys/wait.h>
[email protected]8ecd3aad2009-11-04 08:32:2214#include <unistd.h>
15
[email protected]789f70c72009-07-24 13:55:4316#include "base/basictypes.h"
[email protected]cc8f1462009-06-12 17:36:5517#include "base/command_line.h"
18#include "base/eintr_wrapper.h"
[email protected]5d91c9e2010-07-28 17:25:2819#include "base/file_path.h"
[email protected]cc8f1462009-06-12 17:36:5520#include "base/global_descriptors_posix.h"
[email protected]8ecd3aad2009-11-04 08:32:2221#include "base/hash_tables.h"
22#include "base/linux_util.h"
[email protected]3b63f8f42011-03-28 01:54:1523#include "base/memory/scoped_ptr.h"
[email protected]cc8f1462009-06-12 17:36:5524#include "base/pickle.h"
[email protected]4d36536b2010-08-20 06:23:2725#include "base/process_util.h"
[email protected]4378a822009-07-08 01:15:1426#include "base/rand_util.h"
[email protected]80a086c52009-08-04 17:52:0427#include "base/sys_info.h"
[email protected]8015de32009-11-18 04:13:3328#include "build/build_config.h"
[email protected]4b559b4d2011-04-14 17:37:1429#include "crypto/nss_util.h"
[email protected]acb94722011-03-18 01:33:3430#include "content/common/chrome_descriptors.h"
[email protected]4287a3d2011-06-13 23:56:5131#include "content/common/content_switches.h"
[email protected]797c3552011-03-17 00:26:1832#include "content/common/font_config_ipc_linux.h"
[email protected]415c2cd2011-03-11 21:56:1133#include "content/common/main_function_params.h"
[email protected]cebc3dc2011-04-18 17:15:0034#include "content/common/pepper_plugin_registry.h"
[email protected]4dd57932011-03-17 06:06:1235#include "content/common/process_watcher.h"
36#include "content/common/result_codes.h"
37#include "content/common/sandbox_methods_linux.h"
38#include "content/common/set_process_title.h"
[email protected]797c3552011-03-17 00:26:1839#include "content/common/unix_domain_socket_posix.h"
[email protected]808ea572010-09-01 16:22:0140#include "seccompsandbox/sandbox.h"
[email protected]cf3ac3972010-12-22 20:02:2941#include "skia/ext/SkFontHost_fontconfig_control.h"
[email protected]1831acf2009-10-05 16:38:4142#include "unicode/timezone.h"
43
[email protected]e63c4d72011-05-31 22:38:2944#if defined(OS_LINUX)
45#include <sys/epoll.h>
46#include <sys/prctl.h>
47#include <sys/signal.h>
48#else
49#include <signal.h>
50#endif
51
52#if defined(CHROMIUM_SELINUX)
53#include <selinux/selinux.h>
54#include <selinux/context.h>
55#endif
56
[email protected]58680ce2010-09-18 00:09:1557#if defined(ARCH_CPU_X86_FAMILY) && !defined(CHROMIUM_SELINUX) && \
58 !defined(__clang__)
[email protected]36ea6c6f2010-03-17 20:08:0159// The seccomp sandbox is enabled on all ia32 and x86-64 processor as long as
[email protected]58680ce2010-09-18 00:09:1560// we aren't using SELinux or clang.
[email protected]36ea6c6f2010-03-17 20:08:0161#define SECCOMP_SANDBOX
62#endif
63
[email protected]cc8f1462009-06-12 17:36:5564// http://code.google.com/p/chromium/wiki/LinuxZygote
65
[email protected]8ecd3aad2009-11-04 08:32:2266static const int kBrowserDescriptor = 3;
[email protected]73fa63992009-07-20 20:30:0767static const int kMagicSandboxIPCDescriptor = 5;
[email protected]8ecd3aad2009-11-04 08:32:2268static const int kZygoteIdDescriptor = 7;
69static bool g_suid_sandbox_active = false;
[email protected]36ea6c6f2010-03-17 20:08:0170#if defined(SECCOMP_SANDBOX)
[email protected]4f03cbc2009-11-19 00:50:4871// |g_proc_fd| is used only by the seccomp sandbox.
[email protected]a9c54a172009-11-07 06:09:3872static int g_proc_fd = -1;
[email protected]4f03cbc2009-11-19 00:50:4873#endif
[email protected]73fa63992009-07-20 20:30:0774
[email protected]a89a55dd2010-04-19 14:51:1375#if defined(CHROMIUM_SELINUX)
76static void SELinuxTransitionToTypeOrDie(const char* type) {
77 security_context_t security_context;
78 if (getcon(&security_context))
79 LOG(FATAL) << "Cannot get SELinux context";
80
81 context_t context = context_new(security_context);
82 context_type_set(context, type);
83 const int r = setcon(context_str(context));
84 context_free(context);
85 freecon(security_context);
86
87 if (r) {
88 LOG(FATAL) << "dynamic transition to type '" << type << "' failed. "
89 "(this binary has been built with SELinux support, but maybe "
90 "the policies haven't been loaded into the kernel?)";
91 }
92}
93#endif // CHROMIUM_SELINUX
94
[email protected]cc8f1462009-06-12 17:36:5595// This is the object which implements the zygote. The ZygoteMain function,
[email protected]61883442010-07-12 15:14:3496// which is called from ChromeMain, simply constructs one of these objects and
97// runs it.
[email protected]cc8f1462009-06-12 17:36:5598class Zygote {
99 public:
[email protected]4229b5d2011-07-16 02:47:37100 explicit Zygote(int sandbox_flags)
101 : sandbox_flags_(sandbox_flags) {
[email protected]715b4f262010-07-13 14:17:28102 }
103
[email protected]cc8f1462009-06-12 17:36:55104 bool ProcessRequests() {
105 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
106 // browser on it.
[email protected]8ecd3aad2009-11-04 08:32:22107 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
[email protected]abe3ad92009-06-15 18:15:08108 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:55109
110 // We need to accept SIGCHLD, even though our handler is a no-op because
111 // otherwise we cannot wait on children. (According to POSIX 2001.)
112 struct sigaction action;
113 memset(&action, 0, sizeof(action));
114 action.sa_handler = SIGCHLDHandler;
115 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
116
[email protected]8ecd3aad2009-11-04 08:32:22117 if (g_suid_sandbox_active) {
118 // Let the ZygoteHost know we are ready to go.
119 // The receiving code is in chrome/browser/zygote_host_linux.cc.
120 std::vector<int> empty;
[email protected]cf3ac3972010-12-22 20:02:29121 bool r = UnixDomainSocket::SendMsg(kBrowserDescriptor, kZygoteMagic,
122 sizeof(kZygoteMagic), empty);
[email protected]8ecd3aad2009-11-04 08:32:22123 CHECK(r) << "Sending zygote magic failed";
124 }
125
[email protected]cc8f1462009-06-12 17:36:55126 for (;;) {
[email protected]c548be22010-03-08 12:55:57127 // This function call can return multiple times, once per fork().
[email protected]8ecd3aad2009-11-04 08:32:22128 if (HandleRequestFromBrowser(kBrowserDescriptor))
[email protected]cc8f1462009-06-12 17:36:55129 return true;
130 }
131 }
132
133 private:
134 // See comment below, where sigaction is called.
135 static void SIGCHLDHandler(int signal) { }
136
137 // ---------------------------------------------------------------------------
138 // Requests from the browser...
139
140 // Read and process a request from the browser. Returns true if we are in a
141 // new process and thus need to unwind back into ChromeMain.
142 bool HandleRequestFromBrowser(int fd) {
143 std::vector<int> fds;
[email protected]2752bf62011-03-23 21:06:06144 static const unsigned kMaxMessageLength = 2048;
[email protected]cc8f1462009-06-12 17:36:55145 char buf[kMaxMessageLength];
[email protected]cf3ac3972010-12-22 20:02:29146 const ssize_t len = UnixDomainSocket::RecvMsg(fd, buf, sizeof(buf), &fds);
[email protected]7ae27b92010-09-24 17:33:36147
148 if (len == 0 || (len == -1 && errno == ECONNRESET)) {
149 // EOF from the browser. We should die.
150 _exit(0);
[email protected]cc8f1462009-06-12 17:36:55151 return false;
152 }
153
[email protected]7ae27b92010-09-24 17:33:36154 if (len == -1) {
155 PLOG(ERROR) << "Error reading message from browser";
[email protected]cc8f1462009-06-12 17:36:55156 return false;
157 }
158
159 Pickle pickle(buf, len);
160 void* iter = NULL;
161
162 int kind;
[email protected]57113ea2009-06-18 02:23:16163 if (pickle.ReadInt(&iter, &kind)) {
164 switch (kind) {
165 case ZygoteHost::kCmdFork:
[email protected]c548be22010-03-08 12:55:57166 // This function call can return multiple times, once per fork().
[email protected]57113ea2009-06-18 02:23:16167 return HandleForkRequest(fd, pickle, iter, fds);
168 case ZygoteHost::kCmdReap:
169 if (!fds.empty())
170 break;
[email protected]c548be22010-03-08 12:55:57171 HandleReapRequest(fd, pickle, iter);
172 return false;
[email protected]443b80e2010-12-14 00:42:23173 case ZygoteHost::kCmdGetTerminationStatus:
[email protected]57113ea2009-06-18 02:23:16174 if (!fds.empty())
175 break;
[email protected]443b80e2010-12-14 00:42:23176 HandleGetTerminationStatus(fd, pickle, iter);
[email protected]c548be22010-03-08 12:55:57177 return false;
[email protected]715b4f262010-07-13 14:17:28178 case ZygoteHost::kCmdGetSandboxStatus:
179 HandleGetSandboxStatus(fd, pickle, iter);
180 return false;
[email protected]57113ea2009-06-18 02:23:16181 default:
182 NOTREACHED();
183 break;
184 }
[email protected]cc8f1462009-06-12 17:36:55185 }
186
[email protected]cc8f1462009-06-12 17:36:55187 LOG(WARNING) << "Error parsing message from browser";
188 for (std::vector<int>::const_iterator
189 i = fds.begin(); i != fds.end(); ++i)
190 close(*i);
191 return false;
192 }
193
[email protected]c548be22010-03-08 12:55:57194 void HandleReapRequest(int fd, const Pickle& pickle, void* iter) {
[email protected]8ecd3aad2009-11-04 08:32:22195 base::ProcessId child;
196 base::ProcessId actual_child;
[email protected]cc8f1462009-06-12 17:36:55197
198 if (!pickle.ReadInt(&iter, &child)) {
199 LOG(WARNING) << "Error parsing reap request from browser";
[email protected]c548be22010-03-08 12:55:57200 return;
[email protected]cc8f1462009-06-12 17:36:55201 }
202
[email protected]8ecd3aad2009-11-04 08:32:22203 if (g_suid_sandbox_active) {
204 actual_child = real_pids_to_sandbox_pids[child];
205 if (!actual_child)
[email protected]c548be22010-03-08 12:55:57206 return;
[email protected]8ecd3aad2009-11-04 08:32:22207 real_pids_to_sandbox_pids.erase(child);
208 } else {
209 actual_child = child;
210 }
211
212 ProcessWatcher::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55213 }
214
[email protected]443b80e2010-12-14 00:42:23215 void HandleGetTerminationStatus(int fd, const Pickle& pickle, void* iter) {
[email protected]57113ea2009-06-18 02:23:16216 base::ProcessHandle child;
217
218 if (!pickle.ReadInt(&iter, &child)) {
[email protected]443b80e2010-12-14 00:42:23219 LOG(WARNING) << "Error parsing GetTerminationStatus request "
220 << "from browser";
[email protected]c548be22010-03-08 12:55:57221 return;
[email protected]57113ea2009-06-18 02:23:16222 }
223
[email protected]443b80e2010-12-14 00:42:23224 base::TerminationStatus status;
225 int exit_code;
[email protected]8ecd3aad2009-11-04 08:32:22226 if (g_suid_sandbox_active)
227 child = real_pids_to_sandbox_pids[child];
[email protected]443b80e2010-12-14 00:42:23228 if (child) {
229 status = base::GetTerminationStatus(child, &exit_code);
230 } else {
231 // Assume that if we can't find the child in the sandbox, then
232 // it terminated normally.
233 status = base::TERMINATION_STATUS_NORMAL_TERMINATION;
[email protected]36d772c2011-07-15 20:47:40234 exit_code = ResultCodes::NORMAL_EXIT;
[email protected]443b80e2010-12-14 00:42:23235 }
[email protected]57113ea2009-06-18 02:23:16236
237 Pickle write_pickle;
[email protected]443b80e2010-12-14 00:42:23238 write_pickle.WriteInt(static_cast<int>(status));
239 write_pickle.WriteInt(exit_code);
[email protected]8a861402011-01-28 19:59:11240 ssize_t written =
241 HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size()));
242 if (written != static_cast<ssize_t>(write_pickle.size()))
[email protected]19cb9292010-04-16 23:00:15243 PLOG(ERROR) << "write";
[email protected]57113ea2009-06-18 02:23:16244 }
245
[email protected]28ecba32010-09-16 10:03:09246 // This is equivalent to fork(), except that, when using the SUID
247 // sandbox, it returns the real PID of the child process as it
248 // appears outside the sandbox, rather than returning the PID inside
249 // the sandbox.
[email protected]4229b5d2011-07-16 02:47:37250 int ForkWithRealPid() {
251 if (!g_suid_sandbox_active)
[email protected]28ecba32010-09-16 10:03:09252 return fork();
253
254 int dummy_fd;
255 ino_t dummy_inode;
256 int pipe_fds[2] = { -1, -1 };
[email protected]073040b2010-09-26 02:35:45257 base::ProcessId pid = 0;
[email protected]28ecba32010-09-16 10:03:09258
259 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
260 if (dummy_fd < 0) {
261 LOG(ERROR) << "Failed to create dummy FD";
262 goto error;
263 }
264 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd)) {
265 LOG(ERROR) << "Failed to get inode for dummy FD";
266 goto error;
267 }
268 if (pipe(pipe_fds) != 0) {
269 LOG(ERROR) << "Failed to create pipe";
270 goto error;
271 }
272
[email protected]4229b5d2011-07-16 02:47:37273 pid = fork();
[email protected]28ecba32010-09-16 10:03:09274 if (pid < 0) {
275 goto error;
276 } else if (pid == 0) {
277 // In the child process.
278 close(pipe_fds[1]);
279 char buffer[1];
280 // Wait until the parent process has discovered our PID. We
281 // should not fork any child processes (which the seccomp
282 // sandbox does) until then, because that can interfere with the
283 // parent's discovery of our PID.
284 if (HANDLE_EINTR(read(pipe_fds[0], buffer, 1)) != 1 ||
285 buffer[0] != 'x') {
286 LOG(FATAL) << "Failed to synchronise with parent zygote process";
287 }
288 close(pipe_fds[0]);
289 close(dummy_fd);
290 return 0;
291 } else {
292 // In the parent process.
293 close(dummy_fd);
294 dummy_fd = -1;
295 close(pipe_fds[0]);
296 pipe_fds[0] = -1;
[email protected]4229b5d2011-07-16 02:47:37297 uint8_t reply_buf[512];
298 Pickle request;
299 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
300 request.WriteUInt64(dummy_inode);
[email protected]ae27f1c2011-07-16 02:28:28301
[email protected]4229b5d2011-07-16 02:47:37302 const ssize_t r = UnixDomainSocket::SendRecvMsg(
303 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL,
304 request);
305 if (r == -1) {
306 LOG(ERROR) << "Failed to get child process's real PID";
307 goto error;
[email protected]2b64f7b2011-06-28 19:06:46308 }
[email protected]4229b5d2011-07-16 02:47:37309
310 base::ProcessId real_pid;
311 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
312 void* iter2 = NULL;
313 if (!reply.ReadInt(&iter2, &real_pid))
314 goto error;
315 if (real_pid <= 0) {
316 // METHOD_GET_CHILD_WITH_INODE failed. Did the child die already?
317 LOG(ERROR) << "METHOD_GET_CHILD_WITH_INODE failed";
318 goto error;
319 }
320 real_pids_to_sandbox_pids[real_pid] = pid;
321 if (HANDLE_EINTR(write(pipe_fds[1], "x", 1)) != 1) {
322 LOG(ERROR) << "Failed to synchronise with child process";
323 goto error;
[email protected]28ecba32010-09-16 10:03:09324 }
325 close(pipe_fds[1]);
326 return real_pid;
327 }
328
329 error:
[email protected]72f891f42011-01-12 17:00:52330 if (pid > 0) {
331 if (waitpid(pid, NULL, WNOHANG) == -1)
332 LOG(ERROR) << "Failed to wait for process";
333 }
[email protected]28ecba32010-09-16 10:03:09334 if (dummy_fd >= 0)
335 close(dummy_fd);
336 if (pipe_fds[0] >= 0)
337 close(pipe_fds[0]);
338 if (pipe_fds[1] >= 0)
339 close(pipe_fds[1]);
340 return -1;
341 }
342
[email protected]cc8f1462009-06-12 17:36:55343 // Handle a 'fork' request from the browser: this means that the browser
344 // wishes to start a new renderer.
[email protected]4229b5d2011-07-16 02:47:37345 bool HandleForkRequest(int fd, const Pickle& pickle, void* iter,
346 std::vector<int>& fds) {
[email protected]cc8f1462009-06-12 17:36:55347 std::vector<std::string> args;
348 int argc, numfds;
349 base::GlobalDescriptors::Mapping mapping;
[email protected]8ecd3aad2009-11-04 08:32:22350 base::ProcessId child;
[email protected]cc8f1462009-06-12 17:36:55351
352 if (!pickle.ReadInt(&iter, &argc))
353 goto error;
354
355 for (int i = 0; i < argc; ++i) {
356 std::string arg;
357 if (!pickle.ReadString(&iter, &arg))
358 goto error;
359 args.push_back(arg);
360 }
361
362 if (!pickle.ReadInt(&iter, &numfds))
363 goto error;
364 if (numfds != static_cast<int>(fds.size()))
365 goto error;
366
367 for (int i = 0; i < numfds; ++i) {
368 base::GlobalDescriptors::Key key;
369 if (!pickle.ReadUInt32(&iter, &key))
370 goto error;
371 mapping.push_back(std::make_pair(key, fds[i]));
372 }
373
[email protected]abe3ad92009-06-15 18:15:08374 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22375 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
376
[email protected]4229b5d2011-07-16 02:47:37377 child = ForkWithRealPid();
[email protected]cc8f1462009-06-12 17:36:55378
379 if (!child) {
[email protected]36ea6c6f2010-03-17 20:08:01380#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38381 // Try to open /proc/self/maps as the seccomp sandbox needs access to it
382 if (g_proc_fd >= 0) {
383 int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY);
384 if (proc_self_maps >= 0) {
385 SeccompSandboxSetProcSelfMaps(proc_self_maps);
386 }
387 close(g_proc_fd);
388 g_proc_fd = -1;
389 }
[email protected]8015de32009-11-18 04:13:33390#endif
[email protected]a9c54a172009-11-07 06:09:38391
[email protected]8ecd3aad2009-11-04 08:32:22392 close(kBrowserDescriptor); // our socket from the browser
[email protected]cbcf9cc32010-02-17 04:05:59393 if (g_suid_sandbox_active)
394 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]9b85081af2010-12-07 21:26:47395 base::GlobalDescriptors::GetInstance()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39396
[email protected]a89a55dd2010-04-19 14:51:13397#if defined(CHROMIUM_SELINUX)
398 SELinuxTransitionToTypeOrDie("chromium_renderer_t");
399#endif
400
[email protected]0189bbd2009-10-12 22:50:39401 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55402 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39403 CommandLine::Init(0, NULL);
404 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]74e9fa22010-12-29 21:06:43405
406 // Update the process title. The argv was already cached by the call to
407 // SetProcessTitleFromCommandLine in ChromeMain, so we can pass NULL here
408 // (we don't have the original argv at this point).
409 SetProcessTitleFromCommandLine(NULL);
410
[email protected]c548be22010-03-08 12:55:57411 // The fork() request is handled further up the call stack.
[email protected]cc8f1462009-06-12 17:36:55412 return true;
[email protected]8ecd3aad2009-11-04 08:32:22413 } else if (child < 0) {
[email protected]466cffd2010-06-09 18:10:56414 LOG(ERROR) << "Zygote could not fork: " << errno;
[email protected]8ecd3aad2009-11-04 08:32:22415 goto error;
[email protected]cc8f1462009-06-12 17:36:55416 }
417
[email protected]28ecba32010-09-16 10:03:09418 for (std::vector<int>::const_iterator
419 i = fds.begin(); i != fds.end(); ++i)
420 close(*i);
[email protected]83a0cbe2009-11-04 04:22:47421
[email protected]28ecba32010-09-16 10:03:09422 if (HANDLE_EINTR(write(fd, &child, sizeof(child))) < 0)
423 PLOG(ERROR) << "write";
424 return false;
[email protected]83a0cbe2009-11-04 04:22:47425
426 error:
[email protected]8ecd3aad2009-11-04 08:32:22427 LOG(ERROR) << "Error parsing fork request from browser";
[email protected]83a0cbe2009-11-04 04:22:47428 for (std::vector<int>::const_iterator
429 i = fds.begin(); i != fds.end(); ++i)
430 close(*i);
[email protected]cc8f1462009-06-12 17:36:55431 return false;
432 }
[email protected]8ecd3aad2009-11-04 08:32:22433
[email protected]715b4f262010-07-13 14:17:28434 bool HandleGetSandboxStatus(int fd, const Pickle& pickle, void* iter) {
435 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_)) !=
436 sizeof(sandbox_flags_))) {
437 PLOG(ERROR) << "write";
438 }
439
440 return false;
441 }
442
[email protected]8ecd3aad2009-11-04 08:32:22443 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
444 // fork() returns are not the real PIDs, so we need to map the Real PIDS
445 // into the sandbox PID namespace.
446 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
447 ProcessMap real_pids_to_sandbox_pids;
[email protected]715b4f262010-07-13 14:17:28448
449 const int sandbox_flags_;
[email protected]cc8f1462009-06-12 17:36:55450};
451
[email protected]ad6d2c42009-09-15 20:13:38452// With SELinux we can carve out a precise sandbox, so we don't have to play
453// with intercepting libc calls.
454#if !defined(CHROMIUM_SELINUX)
455
[email protected]a0f200ea2009-08-06 18:44:06456static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
457 char* timezone_out,
458 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07459 Pickle request;
460 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
461 request.WriteString(
462 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
463
464 uint8_t reply_buf[512];
[email protected]cf3ac3972010-12-22 20:02:29465 const ssize_t r = UnixDomainSocket::SendRecvMsg(
[email protected]73fa63992009-07-20 20:30:07466 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
467 if (r == -1) {
468 memset(output, 0, sizeof(struct tm));
469 return;
470 }
471
472 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
473 void* iter = NULL;
[email protected]9debcda52009-07-22 20:06:51474 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07475 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51476 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07477 result.size() != sizeof(struct tm)) {
478 memset(output, 0, sizeof(struct tm));
479 return;
480 }
481
482 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51483 if (timezone_out_len) {
484 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
485 memcpy(timezone_out, timezone.data(), copy_len);
486 timezone_out[copy_len] = 0;
487 output->tm_zone = timezone_out;
488 } else {
489 output->tm_zone = NULL;
490 }
[email protected]73fa63992009-07-20 20:30:07491}
492
[email protected]a0f200ea2009-08-06 18:44:06493static bool g_am_zygote_or_renderer = false;
494
495// Sandbox interception of libc calls.
496//
497// Because we are running in a sandbox certain libc calls will fail (localtime
498// being the motivating example - it needs to read /etc/localtime). We need to
499// intercept these calls and proxy them to the browser. However, these calls
500// may come from us or from our libraries. In some cases we can't just change
501// our code.
502//
503// It's for these cases that we have the following setup:
504//
505// We define global functions for those functions which we wish to override.
506// Since we will be first in the dynamic resolution order, the dynamic linker
507// will point callers to our versions of these functions. However, we have the
508// same binary for both the browser and the renderers, which means that our
509// overrides will apply in the browser too.
510//
511// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
512// renderer process. It's set in ZygoteMain and inherited by the renderers when
513// they fork. (This means that it'll be incorrect for global constructor
514// functions and before ZygoteMain is called - beware).
515//
516// Our replacement functions can check this global and either proxy
517// the call to the browser over the sandbox IPC
518// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
519// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
520// current module.
521//
522// Other avenues:
523//
524// Our first attempt involved some assembly to patch the GOT of the current
525// module. This worked, but was platform specific and doesn't catch the case
526// where a library makes a call rather than current module.
527//
528// We also considered patching the function in place, but this would again by
529// platform specific and the above technique seems to work well enough.
530
[email protected]724df992010-09-21 18:19:10531typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
532typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
533 struct tm* result);
534
535static pthread_once_t g_libc_localtime_funcs_guard = PTHREAD_ONCE_INIT;
536static LocaltimeFunction g_libc_localtime;
537static LocaltimeRFunction g_libc_localtime_r;
538
539static void InitLibcLocaltimeFunctions() {
540 g_libc_localtime = reinterpret_cast<LocaltimeFunction>(
541 dlsym(RTLD_NEXT, "localtime"));
542 g_libc_localtime_r = reinterpret_cast<LocaltimeRFunction>(
543 dlsym(RTLD_NEXT, "localtime_r"));
544
545 if (!g_libc_localtime || !g_libc_localtime_r) {
546 // http://code.google.com/p/chromium/issues/detail?id=16800
547 //
548 // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces
549 // it with a version which doesn't work. In this case we'll get a NULL
550 // result. There's not a lot we can do at this point, so we just bodge it!
551 LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been "
552 "reported to be caused by Nvidia's libGL. You should expect"
553 " time related functions to misbehave. "
554 "http://code.google.com/p/chromium/issues/detail?id=16800";
555 }
556
557 if (!g_libc_localtime)
558 g_libc_localtime = gmtime;
559 if (!g_libc_localtime_r)
560 g_libc_localtime_r = gmtime_r;
561}
[email protected]a5d7bb82009-09-08 22:30:58562
[email protected]73fa63992009-07-20 20:30:07563struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06564 if (g_am_zygote_or_renderer) {
565 static struct tm time_struct;
566 static char timezone_string[64];
567 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
568 sizeof(timezone_string));
569 return &time_struct;
570 } else {
[email protected]724df992010-09-21 18:19:10571 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
572 InitLibcLocaltimeFunctions));
573 return g_libc_localtime(timep);
[email protected]a0f200ea2009-08-06 18:44:06574 }
[email protected]73fa63992009-07-20 20:30:07575}
576
577struct tm* localtime_r(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06578 if (g_am_zygote_or_renderer) {
579 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
580 return result;
581 } else {
[email protected]724df992010-09-21 18:19:10582 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
583 InitLibcLocaltimeFunctions));
584 return g_libc_localtime_r(timep, result);
[email protected]a0f200ea2009-08-06 18:44:06585 }
[email protected]73fa63992009-07-20 20:30:07586}
587
[email protected]ad6d2c42009-09-15 20:13:38588#endif // !CHROMIUM_SELINUX
[email protected]a5d7bb82009-09-08 22:30:58589
[email protected]ad6d2c42009-09-15 20:13:38590// This function triggers the static and lazy construction of objects that need
591// to be created before imposing the sandbox.
592static void PreSandboxInit() {
[email protected]b8419412010-03-29 20:18:29593 base::RandUint64();
[email protected]4378a822009-07-08 01:15:14594
[email protected]b8419412010-03-29 20:18:29595 base::SysInfo::MaxSharedMemorySize();
[email protected]80a086c52009-08-04 17:52:04596
[email protected]b8419412010-03-29 20:18:29597 // ICU DateFormat class (used in base/time_format.cc) needs to get the
598 // Olson timezone ID by accessing the zoneinfo files on disk. After
599 // TimeZone::createDefault is called once here, the timezone ID is
600 // cached and there's no more need to access the file system.
601 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
[email protected]1831acf2009-10-05 16:38:41602
[email protected]eeb08552011-03-18 11:15:39603#if defined(USE_NSS)
[email protected]f6a67b42011-03-17 23:49:21604 // NSS libraries are loaded before sandbox is activated. This is to allow
605 // successful initialization of NSS which tries to load extra library files.
606 // Doing so will allow NSS to be used within sandbox for chromoting.
[email protected]4b559b4d2011-04-14 17:37:14607 crypto::LoadNSSLibraries();
[email protected]eeb08552011-03-18 11:15:39608#else
609 // TODO(bulach): implement openssl support.
610 NOTREACHED() << "Remoting is not supported for openssl";
[email protected]f6a67b42011-03-17 23:49:21611#endif
[email protected]ed450f32011-03-16 01:26:49612
[email protected]17773042010-07-28 17:35:07613 // Ensure access to the Pepper plugins before the sandbox is turned on.
614 PepperPluginRegistry::PreloadModules();
[email protected]ad6d2c42009-09-15 20:13:38615}
616
617#if !defined(CHROMIUM_SELINUX)
618static bool EnterSandbox() {
[email protected]b8419412010-03-29 20:18:29619 // The SUID sandbox sets this environment variable to a file descriptor
620 // over which we can signal that we have completed our startup and can be
621 // chrooted.
[email protected]ad6d2c42009-09-15 20:13:38622 const char* const sandbox_fd_string = getenv("SBX_D");
[email protected]ad6d2c42009-09-15 20:13:38623
[email protected]0dc32322010-09-16 09:46:59624 if (sandbox_fd_string) {
625 // Use the SUID sandbox. This still allows the seccomp sandbox to
626 // be enabled by the process later.
[email protected]8ecd3aad2009-11-04 08:32:22627 g_suid_sandbox_active = true;
628
[email protected]ad6d2c42009-09-15 20:13:38629 char* endptr;
630 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
631 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
632 return false;
633 const int fd = fd_long;
634
635 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08636
[email protected]eaebefaf2010-02-23 22:58:18637 static const char kMsgChrootMe = 'C';
638 static const char kMsgChrootSuccessful = 'O';
[email protected]abe3ad92009-06-15 18:15:08639
[email protected]eaebefaf2010-02-23 22:58:18640 if (HANDLE_EINTR(write(fd, &kMsgChrootMe, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32641 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08642 return false;
[email protected]0e1611122009-07-10 18:17:32643 }
[email protected]abe3ad92009-06-15 18:15:08644
[email protected]87ed6952009-07-16 02:52:15645 // We need to reap the chroot helper process in any event:
646 wait(NULL);
647
[email protected]abe3ad92009-06-15 18:15:08648 char reply;
[email protected]87f8ce62009-07-10 19:14:31649 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32650 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08651 return false;
[email protected]0e1611122009-07-10 18:17:32652 }
[email protected]87f8ce62009-07-10 19:14:31653
[email protected]eaebefaf2010-02-23 22:58:18654 if (reply != kMsgChrootSuccessful) {
[email protected]0e1611122009-07-10 18:17:32655 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08656 return false;
[email protected]0e1611122009-07-10 18:17:32657 }
[email protected]abe3ad92009-06-15 18:15:08658
[email protected]cf3ac3972010-12-22 20:02:29659 SkiaFontConfigSetImplementation(
660 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08661
[email protected]415493be2009-07-10 17:50:24662 // Previously, we required that the binary be non-readable. This causes the
663 // kernel to mark the process as non-dumpable at startup. The thinking was
664 // that, although we were putting the renderers into a PID namespace (with
665 // the SUID sandbox), they would nonetheless be in the /same/ PID
666 // namespace. So they could ptrace each other unless they were non-dumpable.
667 //
668 // If the binary was readable, then there would be a window between process
669 // startup and the point where we set the non-dumpable flag in which a
670 // compromised renderer could ptrace attach.
671 //
672 // However, now that we have a zygote model, only the (trusted) zygote
673 // exists at this point and we can set the non-dumpable flag which is
674 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48675 //
676 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
677 // issues, one can specify --allow-sandbox-debugging to let the process be
678 // dumpable.
679 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
680 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
681 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
682 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
683 LOG(ERROR) << "Failed to set non-dumpable flag";
684 return false;
685 }
[email protected]0e1611122009-07-10 18:17:32686 }
[email protected]cc9a9a882011-03-03 20:09:10687 } else if (CommandLine::ForCurrentProcess()->HasSwitch(
688 switches::kEnableSeccompSandbox)) {
[email protected]0dc32322010-09-16 09:46:59689 PreSandboxInit();
[email protected]cf3ac3972010-12-22 20:02:29690 SkiaFontConfigSetImplementation(
691 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08692 } else {
693 SkiaFontConfigUseDirectImplementation();
694 }
695
696 return true;
697}
[email protected]ad6d2c42009-09-15 20:13:38698#else // CHROMIUM_SELINUX
699
700static bool EnterSandbox() {
701 PreSandboxInit();
702 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
[email protected]ad6d2c42009-09-15 20:13:38703 return true;
704}
705
706#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08707
[email protected]4229b5d2011-07-16 02:47:37708bool ZygoteMain(const MainFunctionParams& params) {
[email protected]ad6d2c42009-09-15 20:13:38709#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06710 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38711#endif
[email protected]a0f200ea2009-08-06 18:44:06712
[email protected]36ea6c6f2010-03-17 20:08:01713#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38714 // The seccomp sandbox needs access to files in /proc, which might be denied
715 // after one of the other sandboxes have been started. So, obtain a suitable
716 // file handle in advance.
[email protected]cc9a9a882011-03-03 20:09:10717 if (CommandLine::ForCurrentProcess()->HasSwitch(
718 switches::kEnableSeccompSandbox)) {
[email protected]a9c54a172009-11-07 06:09:38719 g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY);
720 if (g_proc_fd < 0) {
721 LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp "
722 "sandboxing.";
723 }
724 }
[email protected]36ea6c6f2010-03-17 20:08:01725#endif // SECCOMP_SANDBOX
[email protected]a9c54a172009-11-07 06:09:38726
727 // Turn on the SELinux or SUID sandbox
728 if (!EnterSandbox()) {
729 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
730 << errno << ")";
731 return false;
732 }
733
[email protected]715b4f262010-07-13 14:17:28734 int sandbox_flags = 0;
735 if (getenv("SBX_D"))
736 sandbox_flags |= ZygoteHost::kSandboxSUID;
737 if (getenv("SBX_PID_NS"))
738 sandbox_flags |= ZygoteHost::kSandboxPIDNS;
739 if (getenv("SBX_NET_NS"))
740 sandbox_flags |= ZygoteHost::kSandboxNetNS;
741
[email protected]36ea6c6f2010-03-17 20:08:01742#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38743 // The seccomp sandbox will be turned on when the renderers start. But we can
744 // already check if sufficient support is available so that we only need to
745 // print one error message for the entire browser session.
[email protected]cc9a9a882011-03-03 20:09:10746 if (g_proc_fd >= 0 && CommandLine::ForCurrentProcess()->HasSwitch(
747 switches::kEnableSeccompSandbox)) {
[email protected]a9c54a172009-11-07 06:09:38748 if (!SupportsSeccompSandbox(g_proc_fd)) {
[email protected]e8c916a2009-11-04 17:52:47749 // There are a good number of users who cannot use the seccomp sandbox
750 // (e.g. because their distribution does not enable seccomp mode by
751 // default). While we would prefer to deny execution in this case, it
752 // seems more realistic to continue in degraded mode.
[email protected]1b5d28f2010-02-18 15:53:36753 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
754 "Seccomp sandbox. Running renderers with Seccomp "
755 "sandboxing disabled.";
[email protected]e8c916a2009-11-04 17:52:47756 } else {
[email protected]8e96e502010-10-21 20:57:12757 VLOG(1) << "Enabling experimental Seccomp sandbox.";
[email protected]715b4f262010-07-13 14:17:28758 sandbox_flags |= ZygoteHost::kSandboxSeccomp;
[email protected]e8c916a2009-11-04 17:52:47759 }
760 }
[email protected]36ea6c6f2010-03-17 20:08:01761#endif // SECCOMP_SANDBOX
[email protected]e8c916a2009-11-04 17:52:47762
[email protected]4229b5d2011-07-16 02:47:37763 Zygote zygote(sandbox_flags);
[email protected]c548be22010-03-08 12:55:57764 // This function call can return multiple times, once per fork().
[email protected]cc8f1462009-06-12 17:36:55765 return zygote.ProcessRequests();
766}