blob: 76fe656619c9118030f320079fd5215f61b0f4b3 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]cc8f1462009-06-12 17:36:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e63c4d72011-05-31 22:38:295#include "content/browser/zygote_host_linux.h"
6
[email protected]a0f200ea2009-08-06 18:44:067#include <dlfcn.h>
[email protected]a9c54a172009-11-07 06:09:388#include <fcntl.h>
[email protected]724df992010-09-21 18:19:109#include <pthread.h>
[email protected]8ecd3aad2009-11-04 08:32:2210#include <sys/socket.h>
[email protected]a9c54a172009-11-07 06:09:3811#include <sys/stat.h>
[email protected]8ecd3aad2009-11-04 08:32:2212#include <sys/types.h>
[email protected]83a0cbe2009-11-04 04:22:4713#include <sys/wait.h>
[email protected]8ecd3aad2009-11-04 08:32:2214#include <unistd.h>
15
[email protected]789f70c72009-07-24 13:55:4316#include "base/basictypes.h"
[email protected]cc8f1462009-06-12 17:36:5517#include "base/command_line.h"
18#include "base/eintr_wrapper.h"
[email protected]5d91c9e2010-07-28 17:25:2819#include "base/file_path.h"
[email protected]cc8f1462009-06-12 17:36:5520#include "base/global_descriptors_posix.h"
[email protected]8ecd3aad2009-11-04 08:32:2221#include "base/hash_tables.h"
22#include "base/linux_util.h"
[email protected]3b63f8f42011-03-28 01:54:1523#include "base/memory/scoped_ptr.h"
[email protected]cc8f1462009-06-12 17:36:5524#include "base/pickle.h"
[email protected]4d36536b2010-08-20 06:23:2725#include "base/process_util.h"
[email protected]4378a822009-07-08 01:15:1426#include "base/rand_util.h"
[email protected]80a086c52009-08-04 17:52:0427#include "base/sys_info.h"
[email protected]8015de32009-11-18 04:13:3328#include "build/build_config.h"
[email protected]4b559b4d2011-04-14 17:37:1429#include "crypto/nss_util.h"
[email protected]acb94722011-03-18 01:33:3430#include "content/common/chrome_descriptors.h"
[email protected]4287a3d2011-06-13 23:56:5131#include "content/common/content_switches.h"
[email protected]797c3552011-03-17 00:26:1832#include "content/common/font_config_ipc_linux.h"
[email protected]415c2cd2011-03-11 21:56:1133#include "content/common/main_function_params.h"
[email protected]cebc3dc2011-04-18 17:15:0034#include "content/common/pepper_plugin_registry.h"
[email protected]4dd57932011-03-17 06:06:1235#include "content/common/process_watcher.h"
36#include "content/common/result_codes.h"
37#include "content/common/sandbox_methods_linux.h"
38#include "content/common/set_process_title.h"
[email protected]797c3552011-03-17 00:26:1839#include "content/common/unix_domain_socket_posix.h"
[email protected]ce2ae442011-07-18 16:13:4140#include "content/common/zygote_fork_delegate_linux.h"
[email protected]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"
[email protected]ce2ae442011-07-18 16:13:4143#include "ipc/ipc_switches.h"
[email protected]1831acf2009-10-05 16:38:4144
[email protected]e63c4d72011-05-31 22:38:2945#if defined(OS_LINUX)
46#include <sys/epoll.h>
47#include <sys/prctl.h>
48#include <sys/signal.h>
49#else
50#include <signal.h>
51#endif
52
53#if defined(CHROMIUM_SELINUX)
54#include <selinux/selinux.h>
55#include <selinux/context.h>
56#endif
57
[email protected]cc8f1462009-06-12 17:36:5558// http://code.google.com/p/chromium/wiki/LinuxZygote
59
[email protected]8ecd3aad2009-11-04 08:32:2260static const int kBrowserDescriptor = 3;
[email protected]73fa63992009-07-20 20:30:0761static const int kMagicSandboxIPCDescriptor = 5;
[email protected]8ecd3aad2009-11-04 08:32:2262static const int kZygoteIdDescriptor = 7;
63static bool g_suid_sandbox_active = false;
[email protected]2bc039a2011-07-29 23:05:4164
65// Seccomp enable/disable logic is centralized here.
66// - We define SECCOMP_SANDBOX if seccomp is compiled in at all: currently,
67// on non-views (non-ChromeOS) non-ARM non-Clang Linux only.
68// - If we have SECCOMP_SANDBOX, we provide SeccompEnabled() as a
69// run-time test to determine whether to turn on seccomp: currently
70// it's behind an --enable-seccomp-sandbox switch.
71
72// This #ifdef logic must be kept in sync with
73// renderer_main_platform_delegate_linux.cc. See TODO in that file.
74#if defined(ARCH_CPU_X86_FAMILY) && !defined(CHROMIUM_SELINUX) && \
75 !defined(__clang__) && !defined(OS_CHROMEOS) && !defined(TOOLKIT_VIEWS)
76#define SECCOMP_SANDBOX
77#include "seccompsandbox/sandbox.h"
[email protected]4f03cbc2009-11-19 00:50:4878#endif
[email protected]73fa63992009-07-20 20:30:0779
[email protected]2bc039a2011-07-29 23:05:4180#if defined(SECCOMP_SANDBOX)
81static bool SeccompEnabled() {
82 return CommandLine::ForCurrentProcess()->HasSwitch(
83 switches::kEnableSeccompSandbox) &&
84 !CommandLine::ForCurrentProcess()->HasSwitch(
85 switches::kDisableSeccompSandbox);
86}
87static int g_proc_fd = -1;
88#endif // SECCOMP_SANDBOX
89
[email protected]a89a55dd2010-04-19 14:51:1390#if defined(CHROMIUM_SELINUX)
91static void SELinuxTransitionToTypeOrDie(const char* type) {
92 security_context_t security_context;
93 if (getcon(&security_context))
94 LOG(FATAL) << "Cannot get SELinux context";
95
96 context_t context = context_new(security_context);
97 context_type_set(context, type);
98 const int r = setcon(context_str(context));
99 context_free(context);
100 freecon(security_context);
101
102 if (r) {
103 LOG(FATAL) << "dynamic transition to type '" << type << "' failed. "
104 "(this binary has been built with SELinux support, but maybe "
105 "the policies haven't been loaded into the kernel?)";
106 }
107}
108#endif // CHROMIUM_SELINUX
109
[email protected]cc8f1462009-06-12 17:36:55110// This is the object which implements the zygote. The ZygoteMain function,
[email protected]61883442010-07-12 15:14:34111// which is called from ChromeMain, simply constructs one of these objects and
112// runs it.
[email protected]cc8f1462009-06-12 17:36:55113class Zygote {
114 public:
[email protected]e7e38032011-07-26 17:25:25115 Zygote(int sandbox_flags, ZygoteForkDelegate* helper)
116 : sandbox_flags_(sandbox_flags), helper_(helper) {
[email protected]715b4f262010-07-13 14:17:28117 }
118
[email protected]cc8f1462009-06-12 17:36:55119 bool ProcessRequests() {
120 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
121 // browser on it.
[email protected]8ecd3aad2009-11-04 08:32:22122 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
[email protected]abe3ad92009-06-15 18:15:08123 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:55124
125 // We need to accept SIGCHLD, even though our handler is a no-op because
126 // otherwise we cannot wait on children. (According to POSIX 2001.)
127 struct sigaction action;
128 memset(&action, 0, sizeof(action));
129 action.sa_handler = SIGCHLDHandler;
130 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
131
[email protected]8ecd3aad2009-11-04 08:32:22132 if (g_suid_sandbox_active) {
133 // Let the ZygoteHost know we are ready to go.
134 // The receiving code is in chrome/browser/zygote_host_linux.cc.
135 std::vector<int> empty;
[email protected]cf3ac3972010-12-22 20:02:29136 bool r = UnixDomainSocket::SendMsg(kBrowserDescriptor, kZygoteMagic,
137 sizeof(kZygoteMagic), empty);
[email protected]bfc672a52011-07-20 07:51:27138#if defined(OS_CHROMEOS)
139 LOG_IF(WARNING, r) << "Sending zygote magic failed";
140 // Exit normally on chromeos because session manager may send SIGTERM
141 // right after the process starts and it may fail to send zygote magic
142 // number to browser process.
[email protected]a45f1832011-07-20 18:01:15143 if (!r)
144 _exit(content::RESULT_CODE_NORMAL_EXIT);
[email protected]bfc672a52011-07-20 07:51:27145#else
[email protected]8ecd3aad2009-11-04 08:32:22146 CHECK(r) << "Sending zygote magic failed";
[email protected]bfc672a52011-07-20 07:51:27147#endif
[email protected]8ecd3aad2009-11-04 08:32:22148 }
149
[email protected]cc8f1462009-06-12 17:36:55150 for (;;) {
[email protected]c548be22010-03-08 12:55:57151 // This function call can return multiple times, once per fork().
[email protected]8ecd3aad2009-11-04 08:32:22152 if (HandleRequestFromBrowser(kBrowserDescriptor))
[email protected]cc8f1462009-06-12 17:36:55153 return true;
154 }
155 }
156
157 private:
158 // See comment below, where sigaction is called.
159 static void SIGCHLDHandler(int signal) { }
160
161 // ---------------------------------------------------------------------------
162 // Requests from the browser...
163
164 // Read and process a request from the browser. Returns true if we are in a
165 // new process and thus need to unwind back into ChromeMain.
166 bool HandleRequestFromBrowser(int fd) {
167 std::vector<int> fds;
[email protected]2752bf62011-03-23 21:06:06168 static const unsigned kMaxMessageLength = 2048;
[email protected]cc8f1462009-06-12 17:36:55169 char buf[kMaxMessageLength];
[email protected]cf3ac3972010-12-22 20:02:29170 const ssize_t len = UnixDomainSocket::RecvMsg(fd, buf, sizeof(buf), &fds);
[email protected]7ae27b92010-09-24 17:33:36171
172 if (len == 0 || (len == -1 && errno == ECONNRESET)) {
173 // EOF from the browser. We should die.
174 _exit(0);
[email protected]cc8f1462009-06-12 17:36:55175 return false;
176 }
177
[email protected]7ae27b92010-09-24 17:33:36178 if (len == -1) {
179 PLOG(ERROR) << "Error reading message from browser";
[email protected]cc8f1462009-06-12 17:36:55180 return false;
181 }
182
183 Pickle pickle(buf, len);
184 void* iter = NULL;
185
186 int kind;
[email protected]57113ea2009-06-18 02:23:16187 if (pickle.ReadInt(&iter, &kind)) {
188 switch (kind) {
189 case ZygoteHost::kCmdFork:
[email protected]c548be22010-03-08 12:55:57190 // This function call can return multiple times, once per fork().
[email protected]57113ea2009-06-18 02:23:16191 return HandleForkRequest(fd, pickle, iter, fds);
[email protected]ce2ae442011-07-18 16:13:41192
[email protected]57113ea2009-06-18 02:23:16193 case ZygoteHost::kCmdReap:
194 if (!fds.empty())
195 break;
[email protected]c548be22010-03-08 12:55:57196 HandleReapRequest(fd, pickle, iter);
197 return false;
[email protected]443b80e2010-12-14 00:42:23198 case ZygoteHost::kCmdGetTerminationStatus:
[email protected]57113ea2009-06-18 02:23:16199 if (!fds.empty())
200 break;
[email protected]443b80e2010-12-14 00:42:23201 HandleGetTerminationStatus(fd, pickle, iter);
[email protected]c548be22010-03-08 12:55:57202 return false;
[email protected]715b4f262010-07-13 14:17:28203 case ZygoteHost::kCmdGetSandboxStatus:
204 HandleGetSandboxStatus(fd, pickle, iter);
205 return false;
[email protected]57113ea2009-06-18 02:23:16206 default:
207 NOTREACHED();
208 break;
209 }
[email protected]cc8f1462009-06-12 17:36:55210 }
211
[email protected]cc8f1462009-06-12 17:36:55212 LOG(WARNING) << "Error parsing message from browser";
213 for (std::vector<int>::const_iterator
214 i = fds.begin(); i != fds.end(); ++i)
215 close(*i);
216 return false;
217 }
218
[email protected]c548be22010-03-08 12:55:57219 void HandleReapRequest(int fd, const Pickle& pickle, void* iter) {
[email protected]8ecd3aad2009-11-04 08:32:22220 base::ProcessId child;
221 base::ProcessId actual_child;
[email protected]cc8f1462009-06-12 17:36:55222
223 if (!pickle.ReadInt(&iter, &child)) {
224 LOG(WARNING) << "Error parsing reap request from browser";
[email protected]c548be22010-03-08 12:55:57225 return;
[email protected]cc8f1462009-06-12 17:36:55226 }
227
[email protected]8ecd3aad2009-11-04 08:32:22228 if (g_suid_sandbox_active) {
229 actual_child = real_pids_to_sandbox_pids[child];
230 if (!actual_child)
[email protected]c548be22010-03-08 12:55:57231 return;
[email protected]8ecd3aad2009-11-04 08:32:22232 real_pids_to_sandbox_pids.erase(child);
233 } else {
234 actual_child = child;
235 }
236
237 ProcessWatcher::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55238 }
239
[email protected]443b80e2010-12-14 00:42:23240 void HandleGetTerminationStatus(int fd, const Pickle& pickle, void* iter) {
[email protected]57113ea2009-06-18 02:23:16241 base::ProcessHandle child;
242
243 if (!pickle.ReadInt(&iter, &child)) {
[email protected]443b80e2010-12-14 00:42:23244 LOG(WARNING) << "Error parsing GetTerminationStatus request "
245 << "from browser";
[email protected]c548be22010-03-08 12:55:57246 return;
[email protected]57113ea2009-06-18 02:23:16247 }
248
[email protected]443b80e2010-12-14 00:42:23249 base::TerminationStatus status;
250 int exit_code;
[email protected]8ecd3aad2009-11-04 08:32:22251 if (g_suid_sandbox_active)
252 child = real_pids_to_sandbox_pids[child];
[email protected]443b80e2010-12-14 00:42:23253 if (child) {
254 status = base::GetTerminationStatus(child, &exit_code);
255 } else {
256 // Assume that if we can't find the child in the sandbox, then
257 // it terminated normally.
258 status = base::TERMINATION_STATUS_NORMAL_TERMINATION;
[email protected]1fcfb202011-07-19 19:53:14259 exit_code = content::RESULT_CODE_NORMAL_EXIT;
[email protected]443b80e2010-12-14 00:42:23260 }
[email protected]57113ea2009-06-18 02:23:16261
262 Pickle write_pickle;
[email protected]443b80e2010-12-14 00:42:23263 write_pickle.WriteInt(static_cast<int>(status));
264 write_pickle.WriteInt(exit_code);
[email protected]8a861402011-01-28 19:59:11265 ssize_t written =
266 HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size()));
267 if (written != static_cast<ssize_t>(write_pickle.size()))
[email protected]19cb9292010-04-16 23:00:15268 PLOG(ERROR) << "write";
[email protected]57113ea2009-06-18 02:23:16269 }
270
[email protected]28ecba32010-09-16 10:03:09271 // This is equivalent to fork(), except that, when using the SUID
272 // sandbox, it returns the real PID of the child process as it
273 // appears outside the sandbox, rather than returning the PID inside
274 // the sandbox.
[email protected]ce2ae442011-07-18 16:13:41275 int ForkWithRealPid(const std::string& process_type, std::vector<int>& fds,
276 const std::string& channel_switch) {
277 const bool use_helper = (helper_ && helper_->CanHelp(process_type));
278 if (!(use_helper || g_suid_sandbox_active)) {
[email protected]28ecba32010-09-16 10:03:09279 return fork();
[email protected]ce2ae442011-07-18 16:13:41280 }
[email protected]28ecba32010-09-16 10:03:09281
282 int dummy_fd;
283 ino_t dummy_inode;
284 int pipe_fds[2] = { -1, -1 };
[email protected]073040b2010-09-26 02:35:45285 base::ProcessId pid = 0;
[email protected]28ecba32010-09-16 10:03:09286
287 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
288 if (dummy_fd < 0) {
289 LOG(ERROR) << "Failed to create dummy FD";
290 goto error;
291 }
292 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd)) {
293 LOG(ERROR) << "Failed to get inode for dummy FD";
294 goto error;
295 }
296 if (pipe(pipe_fds) != 0) {
297 LOG(ERROR) << "Failed to create pipe";
298 goto error;
299 }
300
[email protected]ce2ae442011-07-18 16:13:41301 if (use_helper) {
302 fds.push_back(dummy_fd);
303 fds.push_back(pipe_fds[0]);
304 pid = helper_->Fork(fds);
305 } else {
306 pid = fork();
307 }
[email protected]28ecba32010-09-16 10:03:09308 if (pid < 0) {
309 goto error;
310 } else if (pid == 0) {
311 // In the child process.
312 close(pipe_fds[1]);
313 char buffer[1];
314 // Wait until the parent process has discovered our PID. We
315 // should not fork any child processes (which the seccomp
316 // sandbox does) until then, because that can interfere with the
317 // parent's discovery of our PID.
318 if (HANDLE_EINTR(read(pipe_fds[0], buffer, 1)) != 1 ||
319 buffer[0] != 'x') {
320 LOG(FATAL) << "Failed to synchronise with parent zygote process";
321 }
322 close(pipe_fds[0]);
323 close(dummy_fd);
324 return 0;
325 } else {
326 // In the parent process.
327 close(dummy_fd);
328 dummy_fd = -1;
329 close(pipe_fds[0]);
330 pipe_fds[0] = -1;
[email protected]4229b5d2011-07-16 02:47:37331 base::ProcessId real_pid;
[email protected]ce2ae442011-07-18 16:13:41332 if (g_suid_sandbox_active) {
333 uint8_t reply_buf[512];
334 Pickle request;
335 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
336 request.WriteUInt64(dummy_inode);
337
338 const ssize_t r = UnixDomainSocket::SendRecvMsg(
339 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL,
340 request);
341 if (r == -1) {
342 LOG(ERROR) << "Failed to get child process's real PID";
343 goto error;
344 }
345
346 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
347 void* iter = NULL;
348 if (!reply.ReadInt(&iter, &real_pid))
349 goto error;
350 if (real_pid <= 0) {
351 // METHOD_GET_CHILD_WITH_INODE failed. Did the child die already?
352 LOG(ERROR) << "METHOD_GET_CHILD_WITH_INODE failed";
353 goto error;
354 }
355 real_pids_to_sandbox_pids[real_pid] = pid;
[email protected]4229b5d2011-07-16 02:47:37356 }
[email protected]ce2ae442011-07-18 16:13:41357 if (use_helper) {
358 real_pid = pid;
359 if (!helper_->AckChild(pipe_fds[1], channel_switch)) {
360 LOG(ERROR) << "Failed to synchronise with zygote fork helper";
361 goto error;
362 }
363 } else {
364 if (HANDLE_EINTR(write(pipe_fds[1], "x", 1)) != 1) {
365 LOG(ERROR) << "Failed to synchronise with child process";
366 goto error;
367 }
[email protected]28ecba32010-09-16 10:03:09368 }
369 close(pipe_fds[1]);
370 return real_pid;
371 }
372
373 error:
[email protected]72f891f42011-01-12 17:00:52374 if (pid > 0) {
375 if (waitpid(pid, NULL, WNOHANG) == -1)
376 LOG(ERROR) << "Failed to wait for process";
377 }
[email protected]28ecba32010-09-16 10:03:09378 if (dummy_fd >= 0)
379 close(dummy_fd);
380 if (pipe_fds[0] >= 0)
381 close(pipe_fds[0]);
382 if (pipe_fds[1] >= 0)
383 close(pipe_fds[1]);
384 return -1;
385 }
386
[email protected]cc8f1462009-06-12 17:36:55387 // Handle a 'fork' request from the browser: this means that the browser
388 // wishes to start a new renderer.
[email protected]ce2ae442011-07-18 16:13:41389 bool HandleForkRequest(int fd, const Pickle& pickle,
390 void* iter, std::vector<int>& fds) {
[email protected]cc8f1462009-06-12 17:36:55391 std::vector<std::string> args;
392 int argc, numfds;
393 base::GlobalDescriptors::Mapping mapping;
[email protected]8ecd3aad2009-11-04 08:32:22394 base::ProcessId child;
[email protected]ce2ae442011-07-18 16:13:41395 std::string process_type;
396 std::string channel_id;
397 const std::string channel_id_prefix = std::string("--")
398 + switches::kProcessChannelID + std::string("=");
399
400 if (!pickle.ReadString(&iter, &process_type))
401 goto error;
[email protected]cc8f1462009-06-12 17:36:55402
403 if (!pickle.ReadInt(&iter, &argc))
404 goto error;
405
406 for (int i = 0; i < argc; ++i) {
407 std::string arg;
408 if (!pickle.ReadString(&iter, &arg))
409 goto error;
410 args.push_back(arg);
[email protected]ce2ae442011-07-18 16:13:41411 if (arg.compare(0, channel_id_prefix.length(), channel_id_prefix) == 0)
412 channel_id = arg;
[email protected]cc8f1462009-06-12 17:36:55413 }
414
415 if (!pickle.ReadInt(&iter, &numfds))
416 goto error;
417 if (numfds != static_cast<int>(fds.size()))
418 goto error;
419
420 for (int i = 0; i < numfds; ++i) {
421 base::GlobalDescriptors::Key key;
422 if (!pickle.ReadUInt32(&iter, &key))
423 goto error;
424 mapping.push_back(std::make_pair(key, fds[i]));
425 }
426
[email protected]abe3ad92009-06-15 18:15:08427 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22428 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
429
[email protected]ce2ae442011-07-18 16:13:41430 child = ForkWithRealPid(process_type, fds, channel_id);
[email protected]cc8f1462009-06-12 17:36:55431
432 if (!child) {
[email protected]36ea6c6f2010-03-17 20:08:01433#if defined(SECCOMP_SANDBOX)
[email protected]2bc039a2011-07-29 23:05:41434 if (SeccompEnabled() && g_proc_fd >= 0) {
435 // Try to open /proc/self/maps as the seccomp sandbox needs access to it
[email protected]a9c54a172009-11-07 06:09:38436 int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY);
437 if (proc_self_maps >= 0) {
438 SeccompSandboxSetProcSelfMaps(proc_self_maps);
[email protected]2bc039a2011-07-29 23:05:41439 } else {
440 PLOG(ERROR) << "openat(/proc/self/maps)";
[email protected]a9c54a172009-11-07 06:09:38441 }
442 close(g_proc_fd);
443 g_proc_fd = -1;
444 }
[email protected]8015de32009-11-18 04:13:33445#endif
[email protected]a9c54a172009-11-07 06:09:38446
[email protected]8ecd3aad2009-11-04 08:32:22447 close(kBrowserDescriptor); // our socket from the browser
[email protected]cbcf9cc32010-02-17 04:05:59448 if (g_suid_sandbox_active)
449 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]9b85081af2010-12-07 21:26:47450 base::GlobalDescriptors::GetInstance()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39451
[email protected]a89a55dd2010-04-19 14:51:13452#if defined(CHROMIUM_SELINUX)
453 SELinuxTransitionToTypeOrDie("chromium_renderer_t");
454#endif
455
[email protected]0189bbd2009-10-12 22:50:39456 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55457 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39458 CommandLine::Init(0, NULL);
459 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]74e9fa22010-12-29 21:06:43460
461 // Update the process title. The argv was already cached by the call to
462 // SetProcessTitleFromCommandLine in ChromeMain, so we can pass NULL here
463 // (we don't have the original argv at this point).
464 SetProcessTitleFromCommandLine(NULL);
465
[email protected]c548be22010-03-08 12:55:57466 // The fork() request is handled further up the call stack.
[email protected]cc8f1462009-06-12 17:36:55467 return true;
[email protected]8ecd3aad2009-11-04 08:32:22468 } else if (child < 0) {
[email protected]466cffd2010-06-09 18:10:56469 LOG(ERROR) << "Zygote could not fork: " << errno;
[email protected]8ecd3aad2009-11-04 08:32:22470 goto error;
[email protected]cc8f1462009-06-12 17:36:55471 }
472
[email protected]28ecba32010-09-16 10:03:09473 for (std::vector<int>::const_iterator
474 i = fds.begin(); i != fds.end(); ++i)
475 close(*i);
[email protected]83a0cbe2009-11-04 04:22:47476
[email protected]28ecba32010-09-16 10:03:09477 if (HANDLE_EINTR(write(fd, &child, sizeof(child))) < 0)
478 PLOG(ERROR) << "write";
479 return false;
[email protected]83a0cbe2009-11-04 04:22:47480
481 error:
[email protected]8ecd3aad2009-11-04 08:32:22482 LOG(ERROR) << "Error parsing fork request from browser";
[email protected]83a0cbe2009-11-04 04:22:47483 for (std::vector<int>::const_iterator
484 i = fds.begin(); i != fds.end(); ++i)
485 close(*i);
[email protected]cc8f1462009-06-12 17:36:55486 return false;
487 }
[email protected]8ecd3aad2009-11-04 08:32:22488
[email protected]715b4f262010-07-13 14:17:28489 bool HandleGetSandboxStatus(int fd, const Pickle& pickle, void* iter) {
490 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_)) !=
491 sizeof(sandbox_flags_))) {
492 PLOG(ERROR) << "write";
493 }
494
495 return false;
496 }
497
[email protected]8ecd3aad2009-11-04 08:32:22498 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
499 // fork() returns are not the real PIDs, so we need to map the Real PIDS
500 // into the sandbox PID namespace.
501 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
502 ProcessMap real_pids_to_sandbox_pids;
[email protected]715b4f262010-07-13 14:17:28503
504 const int sandbox_flags_;
[email protected]ce2ae442011-07-18 16:13:41505 ZygoteForkDelegate* helper_;
[email protected]cc8f1462009-06-12 17:36:55506};
507
[email protected]ad6d2c42009-09-15 20:13:38508// With SELinux we can carve out a precise sandbox, so we don't have to play
509// with intercepting libc calls.
510#if !defined(CHROMIUM_SELINUX)
511
[email protected]a0f200ea2009-08-06 18:44:06512static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
513 char* timezone_out,
514 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07515 Pickle request;
516 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
517 request.WriteString(
518 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
519
520 uint8_t reply_buf[512];
[email protected]cf3ac3972010-12-22 20:02:29521 const ssize_t r = UnixDomainSocket::SendRecvMsg(
[email protected]73fa63992009-07-20 20:30:07522 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
523 if (r == -1) {
524 memset(output, 0, sizeof(struct tm));
525 return;
526 }
527
528 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
529 void* iter = NULL;
[email protected]9debcda52009-07-22 20:06:51530 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07531 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51532 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07533 result.size() != sizeof(struct tm)) {
534 memset(output, 0, sizeof(struct tm));
535 return;
536 }
537
538 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51539 if (timezone_out_len) {
540 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
541 memcpy(timezone_out, timezone.data(), copy_len);
542 timezone_out[copy_len] = 0;
543 output->tm_zone = timezone_out;
544 } else {
545 output->tm_zone = NULL;
546 }
[email protected]73fa63992009-07-20 20:30:07547}
548
[email protected]a0f200ea2009-08-06 18:44:06549static bool g_am_zygote_or_renderer = false;
550
551// Sandbox interception of libc calls.
552//
553// Because we are running in a sandbox certain libc calls will fail (localtime
554// being the motivating example - it needs to read /etc/localtime). We need to
555// intercept these calls and proxy them to the browser. However, these calls
556// may come from us or from our libraries. In some cases we can't just change
557// our code.
558//
559// It's for these cases that we have the following setup:
560//
561// We define global functions for those functions which we wish to override.
562// Since we will be first in the dynamic resolution order, the dynamic linker
563// will point callers to our versions of these functions. However, we have the
564// same binary for both the browser and the renderers, which means that our
565// overrides will apply in the browser too.
566//
567// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
568// renderer process. It's set in ZygoteMain and inherited by the renderers when
569// they fork. (This means that it'll be incorrect for global constructor
570// functions and before ZygoteMain is called - beware).
571//
572// Our replacement functions can check this global and either proxy
573// the call to the browser over the sandbox IPC
574// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
575// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
576// current module.
577//
578// Other avenues:
579//
580// Our first attempt involved some assembly to patch the GOT of the current
581// module. This worked, but was platform specific and doesn't catch the case
582// where a library makes a call rather than current module.
583//
584// We also considered patching the function in place, but this would again by
585// platform specific and the above technique seems to work well enough.
586
[email protected]724df992010-09-21 18:19:10587typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
588typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
589 struct tm* result);
590
591static pthread_once_t g_libc_localtime_funcs_guard = PTHREAD_ONCE_INIT;
592static LocaltimeFunction g_libc_localtime;
593static LocaltimeRFunction g_libc_localtime_r;
594
595static void InitLibcLocaltimeFunctions() {
596 g_libc_localtime = reinterpret_cast<LocaltimeFunction>(
597 dlsym(RTLD_NEXT, "localtime"));
598 g_libc_localtime_r = reinterpret_cast<LocaltimeRFunction>(
599 dlsym(RTLD_NEXT, "localtime_r"));
600
601 if (!g_libc_localtime || !g_libc_localtime_r) {
602 // http://code.google.com/p/chromium/issues/detail?id=16800
603 //
604 // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces
605 // it with a version which doesn't work. In this case we'll get a NULL
606 // result. There's not a lot we can do at this point, so we just bodge it!
607 LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been "
608 "reported to be caused by Nvidia's libGL. You should expect"
609 " time related functions to misbehave. "
610 "http://code.google.com/p/chromium/issues/detail?id=16800";
611 }
612
613 if (!g_libc_localtime)
614 g_libc_localtime = gmtime;
615 if (!g_libc_localtime_r)
616 g_libc_localtime_r = gmtime_r;
617}
[email protected]a5d7bb82009-09-08 22:30:58618
[email protected]73fa63992009-07-20 20:30:07619struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06620 if (g_am_zygote_or_renderer) {
621 static struct tm time_struct;
622 static char timezone_string[64];
623 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
624 sizeof(timezone_string));
625 return &time_struct;
626 } else {
[email protected]724df992010-09-21 18:19:10627 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
628 InitLibcLocaltimeFunctions));
629 return g_libc_localtime(timep);
[email protected]a0f200ea2009-08-06 18:44:06630 }
[email protected]73fa63992009-07-20 20:30:07631}
632
633struct tm* localtime_r(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06634 if (g_am_zygote_or_renderer) {
635 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
636 return result;
637 } else {
[email protected]724df992010-09-21 18:19:10638 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
639 InitLibcLocaltimeFunctions));
640 return g_libc_localtime_r(timep, result);
[email protected]a0f200ea2009-08-06 18:44:06641 }
[email protected]73fa63992009-07-20 20:30:07642}
643
[email protected]ad6d2c42009-09-15 20:13:38644#endif // !CHROMIUM_SELINUX
[email protected]a5d7bb82009-09-08 22:30:58645
[email protected]ad6d2c42009-09-15 20:13:38646// This function triggers the static and lazy construction of objects that need
647// to be created before imposing the sandbox.
648static void PreSandboxInit() {
[email protected]b8419412010-03-29 20:18:29649 base::RandUint64();
[email protected]4378a822009-07-08 01:15:14650
[email protected]b8419412010-03-29 20:18:29651 base::SysInfo::MaxSharedMemorySize();
[email protected]80a086c52009-08-04 17:52:04652
[email protected]b8419412010-03-29 20:18:29653 // ICU DateFormat class (used in base/time_format.cc) needs to get the
654 // Olson timezone ID by accessing the zoneinfo files on disk. After
655 // TimeZone::createDefault is called once here, the timezone ID is
656 // cached and there's no more need to access the file system.
657 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
[email protected]1831acf2009-10-05 16:38:41658
[email protected]eeb08552011-03-18 11:15:39659#if defined(USE_NSS)
[email protected]f6a67b42011-03-17 23:49:21660 // NSS libraries are loaded before sandbox is activated. This is to allow
661 // successful initialization of NSS which tries to load extra library files.
662 // Doing so will allow NSS to be used within sandbox for chromoting.
[email protected]4b559b4d2011-04-14 17:37:14663 crypto::LoadNSSLibraries();
[email protected]eeb08552011-03-18 11:15:39664#else
665 // TODO(bulach): implement openssl support.
666 NOTREACHED() << "Remoting is not supported for openssl";
[email protected]f6a67b42011-03-17 23:49:21667#endif
[email protected]ed450f32011-03-16 01:26:49668
[email protected]17773042010-07-28 17:35:07669 // Ensure access to the Pepper plugins before the sandbox is turned on.
670 PepperPluginRegistry::PreloadModules();
[email protected]ad6d2c42009-09-15 20:13:38671}
672
673#if !defined(CHROMIUM_SELINUX)
674static bool EnterSandbox() {
[email protected]b8419412010-03-29 20:18:29675 // The SUID sandbox sets this environment variable to a file descriptor
676 // over which we can signal that we have completed our startup and can be
677 // chrooted.
[email protected]ad6d2c42009-09-15 20:13:38678 const char* const sandbox_fd_string = getenv("SBX_D");
[email protected]ad6d2c42009-09-15 20:13:38679
[email protected]0dc32322010-09-16 09:46:59680 if (sandbox_fd_string) {
681 // Use the SUID sandbox. This still allows the seccomp sandbox to
682 // be enabled by the process later.
[email protected]8ecd3aad2009-11-04 08:32:22683 g_suid_sandbox_active = true;
684
[email protected]ad6d2c42009-09-15 20:13:38685 char* endptr;
686 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
687 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
688 return false;
689 const int fd = fd_long;
690
691 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08692
[email protected]eaebefaf2010-02-23 22:58:18693 static const char kMsgChrootMe = 'C';
694 static const char kMsgChrootSuccessful = 'O';
[email protected]abe3ad92009-06-15 18:15:08695
[email protected]eaebefaf2010-02-23 22:58:18696 if (HANDLE_EINTR(write(fd, &kMsgChrootMe, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32697 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08698 return false;
[email protected]0e1611122009-07-10 18:17:32699 }
[email protected]abe3ad92009-06-15 18:15:08700
[email protected]87ed6952009-07-16 02:52:15701 // We need to reap the chroot helper process in any event:
702 wait(NULL);
703
[email protected]abe3ad92009-06-15 18:15:08704 char reply;
[email protected]87f8ce62009-07-10 19:14:31705 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32706 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08707 return false;
[email protected]0e1611122009-07-10 18:17:32708 }
[email protected]87f8ce62009-07-10 19:14:31709
[email protected]eaebefaf2010-02-23 22:58:18710 if (reply != kMsgChrootSuccessful) {
[email protected]0e1611122009-07-10 18:17:32711 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08712 return false;
[email protected]0e1611122009-07-10 18:17:32713 }
[email protected]abe3ad92009-06-15 18:15:08714
[email protected]cf3ac3972010-12-22 20:02:29715 SkiaFontConfigSetImplementation(
716 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08717
[email protected]415493be2009-07-10 17:50:24718 // Previously, we required that the binary be non-readable. This causes the
719 // kernel to mark the process as non-dumpable at startup. The thinking was
720 // that, although we were putting the renderers into a PID namespace (with
721 // the SUID sandbox), they would nonetheless be in the /same/ PID
722 // namespace. So they could ptrace each other unless they were non-dumpable.
723 //
724 // If the binary was readable, then there would be a window between process
725 // startup and the point where we set the non-dumpable flag in which a
726 // compromised renderer could ptrace attach.
727 //
728 // However, now that we have a zygote model, only the (trusted) zygote
729 // exists at this point and we can set the non-dumpable flag which is
730 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48731 //
732 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
733 // issues, one can specify --allow-sandbox-debugging to let the process be
734 // dumpable.
735 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
736 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
737 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
738 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
739 LOG(ERROR) << "Failed to set non-dumpable flag";
740 return false;
741 }
[email protected]0e1611122009-07-10 18:17:32742 }
[email protected]2bc039a2011-07-29 23:05:41743#if defined(SECCOMP_SANDBOX)
744 } else if (SeccompEnabled()) {
[email protected]0dc32322010-09-16 09:46:59745 PreSandboxInit();
[email protected]cf3ac3972010-12-22 20:02:29746 SkiaFontConfigSetImplementation(
747 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]2bc039a2011-07-29 23:05:41748#endif
[email protected]abe3ad92009-06-15 18:15:08749 } else {
750 SkiaFontConfigUseDirectImplementation();
751 }
752
753 return true;
754}
[email protected]ad6d2c42009-09-15 20:13:38755#else // CHROMIUM_SELINUX
756
757static bool EnterSandbox() {
758 PreSandboxInit();
759 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
[email protected]ad6d2c42009-09-15 20:13:38760 return true;
761}
762
763#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08764
[email protected]ce2ae442011-07-18 16:13:41765bool ZygoteMain(const MainFunctionParams& params,
766 ZygoteForkDelegate* forkdelegate) {
[email protected]ad6d2c42009-09-15 20:13:38767#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06768 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38769#endif
[email protected]a0f200ea2009-08-06 18:44:06770
[email protected]36ea6c6f2010-03-17 20:08:01771#if defined(SECCOMP_SANDBOX)
[email protected]2bc039a2011-07-29 23:05:41772 if (SeccompEnabled()) {
773 // The seccomp sandbox needs access to files in /proc, which might be denied
774 // after one of the other sandboxes have been started. So, obtain a suitable
775 // file handle in advance.
[email protected]a9c54a172009-11-07 06:09:38776 g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY);
777 if (g_proc_fd < 0) {
778 LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp "
[email protected]2bc039a2011-07-29 23:05:41779 "sandboxing.";
[email protected]a9c54a172009-11-07 06:09:38780 }
781 }
[email protected]36ea6c6f2010-03-17 20:08:01782#endif // SECCOMP_SANDBOX
[email protected]a9c54a172009-11-07 06:09:38783
[email protected]ce2ae442011-07-18 16:13:41784 if (forkdelegate != NULL) {
785 VLOG(1) << "ZygoteMain: initializing fork delegate";
786 forkdelegate->Init(getenv("SBX_D") != NULL, // g_suid_sandbox_active,
787 kBrowserDescriptor,
788 kMagicSandboxIPCDescriptor);
789 } else {
790 VLOG(1) << "ZygoteMain: fork delegate is NULL";
791 }
792
[email protected]a9c54a172009-11-07 06:09:38793 // Turn on the SELinux or SUID sandbox
794 if (!EnterSandbox()) {
795 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
796 << errno << ")";
797 return false;
798 }
799
[email protected]715b4f262010-07-13 14:17:28800 int sandbox_flags = 0;
801 if (getenv("SBX_D"))
802 sandbox_flags |= ZygoteHost::kSandboxSUID;
803 if (getenv("SBX_PID_NS"))
804 sandbox_flags |= ZygoteHost::kSandboxPIDNS;
805 if (getenv("SBX_NET_NS"))
806 sandbox_flags |= ZygoteHost::kSandboxNetNS;
807
[email protected]36ea6c6f2010-03-17 20:08:01808#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38809 // The seccomp sandbox will be turned on when the renderers start. But we can
810 // already check if sufficient support is available so that we only need to
811 // print one error message for the entire browser session.
[email protected]2bc039a2011-07-29 23:05:41812 if (g_proc_fd >= 0 && SeccompEnabled()) {
[email protected]a9c54a172009-11-07 06:09:38813 if (!SupportsSeccompSandbox(g_proc_fd)) {
[email protected]e8c916a2009-11-04 17:52:47814 // There are a good number of users who cannot use the seccomp sandbox
815 // (e.g. because their distribution does not enable seccomp mode by
816 // default). While we would prefer to deny execution in this case, it
817 // seems more realistic to continue in degraded mode.
[email protected]1b5d28f2010-02-18 15:53:36818 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
819 "Seccomp sandbox. Running renderers with Seccomp "
820 "sandboxing disabled.";
[email protected]e8c916a2009-11-04 17:52:47821 } else {
[email protected]8e96e502010-10-21 20:57:12822 VLOG(1) << "Enabling experimental Seccomp sandbox.";
[email protected]715b4f262010-07-13 14:17:28823 sandbox_flags |= ZygoteHost::kSandboxSeccomp;
[email protected]e8c916a2009-11-04 17:52:47824 }
825 }
[email protected]36ea6c6f2010-03-17 20:08:01826#endif // SECCOMP_SANDBOX
[email protected]e8c916a2009-11-04 17:52:47827
[email protected]ce2ae442011-07-18 16:13:41828 Zygote zygote(sandbox_flags, forkdelegate);
[email protected]c548be22010-03-08 12:55:57829 // This function call can return multiple times, once per fork().
[email protected]cc8f1462009-06-12 17:36:55830 return zygote.ProcessRequests();
831}