blob: 40f238c422cf17bad9218fb9f3d0d704a1a573de [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]808ea572010-09-01 16:22:0141#include "seccompsandbox/sandbox.h"
[email protected]cf3ac3972010-12-22 20:02:2942#include "skia/ext/SkFontHost_fontconfig_control.h"
[email protected]1831acf2009-10-05 16:38:4143#include "unicode/timezone.h"
[email protected]ce2ae442011-07-18 16:13:4144#include "ipc/ipc_switches.h"
[email protected]1831acf2009-10-05 16:38:4145
[email protected]e63c4d72011-05-31 22:38:2946#if defined(OS_LINUX)
47#include <sys/epoll.h>
48#include <sys/prctl.h>
49#include <sys/signal.h>
50#else
51#include <signal.h>
52#endif
53
54#if defined(CHROMIUM_SELINUX)
55#include <selinux/selinux.h>
56#include <selinux/context.h>
57#endif
58
[email protected]58680ce2010-09-18 00:09:1559#if defined(ARCH_CPU_X86_FAMILY) && !defined(CHROMIUM_SELINUX) && \
60 !defined(__clang__)
[email protected]36ea6c6f2010-03-17 20:08:0161// The seccomp sandbox is enabled on all ia32 and x86-64 processor as long as
[email protected]58680ce2010-09-18 00:09:1562// we aren't using SELinux or clang.
[email protected]36ea6c6f2010-03-17 20:08:0163#define SECCOMP_SANDBOX
64#endif
65
[email protected]cc8f1462009-06-12 17:36:5566// http://code.google.com/p/chromium/wiki/LinuxZygote
67
[email protected]8ecd3aad2009-11-04 08:32:2268static const int kBrowserDescriptor = 3;
[email protected]73fa63992009-07-20 20:30:0769static const int kMagicSandboxIPCDescriptor = 5;
[email protected]8ecd3aad2009-11-04 08:32:2270static const int kZygoteIdDescriptor = 7;
71static bool g_suid_sandbox_active = false;
[email protected]36ea6c6f2010-03-17 20:08:0172#if defined(SECCOMP_SANDBOX)
[email protected]4f03cbc2009-11-19 00:50:4873// |g_proc_fd| is used only by the seccomp sandbox.
[email protected]a9c54a172009-11-07 06:09:3874static int g_proc_fd = -1;
[email protected]4f03cbc2009-11-19 00:50:4875#endif
[email protected]73fa63992009-07-20 20:30:0776
[email protected]a89a55dd2010-04-19 14:51:1377#if defined(CHROMIUM_SELINUX)
78static void SELinuxTransitionToTypeOrDie(const char* type) {
79 security_context_t security_context;
80 if (getcon(&security_context))
81 LOG(FATAL) << "Cannot get SELinux context";
82
83 context_t context = context_new(security_context);
84 context_type_set(context, type);
85 const int r = setcon(context_str(context));
86 context_free(context);
87 freecon(security_context);
88
89 if (r) {
90 LOG(FATAL) << "dynamic transition to type '" << type << "' failed. "
91 "(this binary has been built with SELinux support, but maybe "
92 "the policies haven't been loaded into the kernel?)";
93 }
94}
95#endif // CHROMIUM_SELINUX
96
[email protected]cc8f1462009-06-12 17:36:5597// This is the object which implements the zygote. The ZygoteMain function,
[email protected]61883442010-07-12 15:14:3498// which is called from ChromeMain, simply constructs one of these objects and
99// runs it.
[email protected]cc8f1462009-06-12 17:36:55100class Zygote {
101 public:
[email protected]e7e38032011-07-26 17:25:25102 Zygote(int sandbox_flags, ZygoteForkDelegate* helper)
103 : sandbox_flags_(sandbox_flags), helper_(helper) {
[email protected]715b4f262010-07-13 14:17:28104 }
105
[email protected]cc8f1462009-06-12 17:36:55106 bool ProcessRequests() {
107 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
108 // browser on it.
[email protected]8ecd3aad2009-11-04 08:32:22109 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
[email protected]abe3ad92009-06-15 18:15:08110 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:55111
112 // We need to accept SIGCHLD, even though our handler is a no-op because
113 // otherwise we cannot wait on children. (According to POSIX 2001.)
114 struct sigaction action;
115 memset(&action, 0, sizeof(action));
116 action.sa_handler = SIGCHLDHandler;
117 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
118
[email protected]8ecd3aad2009-11-04 08:32:22119 if (g_suid_sandbox_active) {
120 // Let the ZygoteHost know we are ready to go.
121 // The receiving code is in chrome/browser/zygote_host_linux.cc.
122 std::vector<int> empty;
[email protected]cf3ac3972010-12-22 20:02:29123 bool r = UnixDomainSocket::SendMsg(kBrowserDescriptor, kZygoteMagic,
124 sizeof(kZygoteMagic), empty);
[email protected]bfc672a52011-07-20 07:51:27125#if defined(OS_CHROMEOS)
126 LOG_IF(WARNING, r) << "Sending zygote magic failed";
127 // Exit normally on chromeos because session manager may send SIGTERM
128 // right after the process starts and it may fail to send zygote magic
129 // number to browser process.
[email protected]a45f1832011-07-20 18:01:15130 if (!r)
131 _exit(content::RESULT_CODE_NORMAL_EXIT);
[email protected]bfc672a52011-07-20 07:51:27132#else
[email protected]8ecd3aad2009-11-04 08:32:22133 CHECK(r) << "Sending zygote magic failed";
[email protected]bfc672a52011-07-20 07:51:27134#endif
[email protected]8ecd3aad2009-11-04 08:32:22135 }
136
[email protected]cc8f1462009-06-12 17:36:55137 for (;;) {
[email protected]c548be22010-03-08 12:55:57138 // This function call can return multiple times, once per fork().
[email protected]8ecd3aad2009-11-04 08:32:22139 if (HandleRequestFromBrowser(kBrowserDescriptor))
[email protected]cc8f1462009-06-12 17:36:55140 return true;
141 }
142 }
143
144 private:
145 // See comment below, where sigaction is called.
146 static void SIGCHLDHandler(int signal) { }
147
148 // ---------------------------------------------------------------------------
149 // Requests from the browser...
150
151 // Read and process a request from the browser. Returns true if we are in a
152 // new process and thus need to unwind back into ChromeMain.
153 bool HandleRequestFromBrowser(int fd) {
154 std::vector<int> fds;
[email protected]2752bf62011-03-23 21:06:06155 static const unsigned kMaxMessageLength = 2048;
[email protected]cc8f1462009-06-12 17:36:55156 char buf[kMaxMessageLength];
[email protected]cf3ac3972010-12-22 20:02:29157 const ssize_t len = UnixDomainSocket::RecvMsg(fd, buf, sizeof(buf), &fds);
[email protected]7ae27b92010-09-24 17:33:36158
159 if (len == 0 || (len == -1 && errno == ECONNRESET)) {
160 // EOF from the browser. We should die.
161 _exit(0);
[email protected]cc8f1462009-06-12 17:36:55162 return false;
163 }
164
[email protected]7ae27b92010-09-24 17:33:36165 if (len == -1) {
166 PLOG(ERROR) << "Error reading message from browser";
[email protected]cc8f1462009-06-12 17:36:55167 return false;
168 }
169
170 Pickle pickle(buf, len);
171 void* iter = NULL;
172
173 int kind;
[email protected]57113ea2009-06-18 02:23:16174 if (pickle.ReadInt(&iter, &kind)) {
175 switch (kind) {
176 case ZygoteHost::kCmdFork:
[email protected]c548be22010-03-08 12:55:57177 // This function call can return multiple times, once per fork().
[email protected]57113ea2009-06-18 02:23:16178 return HandleForkRequest(fd, pickle, iter, fds);
[email protected]ce2ae442011-07-18 16:13:41179
[email protected]57113ea2009-06-18 02:23:16180 case ZygoteHost::kCmdReap:
181 if (!fds.empty())
182 break;
[email protected]c548be22010-03-08 12:55:57183 HandleReapRequest(fd, pickle, iter);
184 return false;
[email protected]443b80e2010-12-14 00:42:23185 case ZygoteHost::kCmdGetTerminationStatus:
[email protected]57113ea2009-06-18 02:23:16186 if (!fds.empty())
187 break;
[email protected]443b80e2010-12-14 00:42:23188 HandleGetTerminationStatus(fd, pickle, iter);
[email protected]c548be22010-03-08 12:55:57189 return false;
[email protected]715b4f262010-07-13 14:17:28190 case ZygoteHost::kCmdGetSandboxStatus:
191 HandleGetSandboxStatus(fd, pickle, iter);
192 return false;
[email protected]57113ea2009-06-18 02:23:16193 default:
194 NOTREACHED();
195 break;
196 }
[email protected]cc8f1462009-06-12 17:36:55197 }
198
[email protected]cc8f1462009-06-12 17:36:55199 LOG(WARNING) << "Error parsing message from browser";
200 for (std::vector<int>::const_iterator
201 i = fds.begin(); i != fds.end(); ++i)
202 close(*i);
203 return false;
204 }
205
[email protected]c548be22010-03-08 12:55:57206 void HandleReapRequest(int fd, const Pickle& pickle, void* iter) {
[email protected]8ecd3aad2009-11-04 08:32:22207 base::ProcessId child;
208 base::ProcessId actual_child;
[email protected]cc8f1462009-06-12 17:36:55209
210 if (!pickle.ReadInt(&iter, &child)) {
211 LOG(WARNING) << "Error parsing reap request from browser";
[email protected]c548be22010-03-08 12:55:57212 return;
[email protected]cc8f1462009-06-12 17:36:55213 }
214
[email protected]8ecd3aad2009-11-04 08:32:22215 if (g_suid_sandbox_active) {
216 actual_child = real_pids_to_sandbox_pids[child];
217 if (!actual_child)
[email protected]c548be22010-03-08 12:55:57218 return;
[email protected]8ecd3aad2009-11-04 08:32:22219 real_pids_to_sandbox_pids.erase(child);
220 } else {
221 actual_child = child;
222 }
223
224 ProcessWatcher::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55225 }
226
[email protected]443b80e2010-12-14 00:42:23227 void HandleGetTerminationStatus(int fd, const Pickle& pickle, void* iter) {
[email protected]57113ea2009-06-18 02:23:16228 base::ProcessHandle child;
229
230 if (!pickle.ReadInt(&iter, &child)) {
[email protected]443b80e2010-12-14 00:42:23231 LOG(WARNING) << "Error parsing GetTerminationStatus request "
232 << "from browser";
[email protected]c548be22010-03-08 12:55:57233 return;
[email protected]57113ea2009-06-18 02:23:16234 }
235
[email protected]443b80e2010-12-14 00:42:23236 base::TerminationStatus status;
237 int exit_code;
[email protected]8ecd3aad2009-11-04 08:32:22238 if (g_suid_sandbox_active)
239 child = real_pids_to_sandbox_pids[child];
[email protected]443b80e2010-12-14 00:42:23240 if (child) {
241 status = base::GetTerminationStatus(child, &exit_code);
242 } else {
243 // Assume that if we can't find the child in the sandbox, then
244 // it terminated normally.
245 status = base::TERMINATION_STATUS_NORMAL_TERMINATION;
[email protected]1fcfb202011-07-19 19:53:14246 exit_code = content::RESULT_CODE_NORMAL_EXIT;
[email protected]443b80e2010-12-14 00:42:23247 }
[email protected]57113ea2009-06-18 02:23:16248
249 Pickle write_pickle;
[email protected]443b80e2010-12-14 00:42:23250 write_pickle.WriteInt(static_cast<int>(status));
251 write_pickle.WriteInt(exit_code);
[email protected]8a861402011-01-28 19:59:11252 ssize_t written =
253 HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size()));
254 if (written != static_cast<ssize_t>(write_pickle.size()))
[email protected]19cb9292010-04-16 23:00:15255 PLOG(ERROR) << "write";
[email protected]57113ea2009-06-18 02:23:16256 }
257
[email protected]28ecba32010-09-16 10:03:09258 // This is equivalent to fork(), except that, when using the SUID
259 // sandbox, it returns the real PID of the child process as it
260 // appears outside the sandbox, rather than returning the PID inside
261 // the sandbox.
[email protected]ce2ae442011-07-18 16:13:41262 int ForkWithRealPid(const std::string& process_type, std::vector<int>& fds,
263 const std::string& channel_switch) {
264 const bool use_helper = (helper_ && helper_->CanHelp(process_type));
265 if (!(use_helper || g_suid_sandbox_active)) {
[email protected]28ecba32010-09-16 10:03:09266 return fork();
[email protected]ce2ae442011-07-18 16:13:41267 }
[email protected]28ecba32010-09-16 10:03:09268
269 int dummy_fd;
270 ino_t dummy_inode;
271 int pipe_fds[2] = { -1, -1 };
[email protected]073040b2010-09-26 02:35:45272 base::ProcessId pid = 0;
[email protected]28ecba32010-09-16 10:03:09273
274 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
275 if (dummy_fd < 0) {
276 LOG(ERROR) << "Failed to create dummy FD";
277 goto error;
278 }
279 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd)) {
280 LOG(ERROR) << "Failed to get inode for dummy FD";
281 goto error;
282 }
283 if (pipe(pipe_fds) != 0) {
284 LOG(ERROR) << "Failed to create pipe";
285 goto error;
286 }
287
[email protected]ce2ae442011-07-18 16:13:41288 if (use_helper) {
289 fds.push_back(dummy_fd);
290 fds.push_back(pipe_fds[0]);
291 pid = helper_->Fork(fds);
292 } else {
293 pid = fork();
294 }
[email protected]28ecba32010-09-16 10:03:09295 if (pid < 0) {
296 goto error;
297 } else if (pid == 0) {
298 // In the child process.
299 close(pipe_fds[1]);
300 char buffer[1];
301 // Wait until the parent process has discovered our PID. We
302 // should not fork any child processes (which the seccomp
303 // sandbox does) until then, because that can interfere with the
304 // parent's discovery of our PID.
305 if (HANDLE_EINTR(read(pipe_fds[0], buffer, 1)) != 1 ||
306 buffer[0] != 'x') {
307 LOG(FATAL) << "Failed to synchronise with parent zygote process";
308 }
309 close(pipe_fds[0]);
310 close(dummy_fd);
311 return 0;
312 } else {
313 // In the parent process.
314 close(dummy_fd);
315 dummy_fd = -1;
316 close(pipe_fds[0]);
317 pipe_fds[0] = -1;
[email protected]4229b5d2011-07-16 02:47:37318 base::ProcessId real_pid;
[email protected]ce2ae442011-07-18 16:13:41319 if (g_suid_sandbox_active) {
320 uint8_t reply_buf[512];
321 Pickle request;
322 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
323 request.WriteUInt64(dummy_inode);
324
325 const ssize_t r = UnixDomainSocket::SendRecvMsg(
326 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL,
327 request);
328 if (r == -1) {
329 LOG(ERROR) << "Failed to get child process's real PID";
330 goto error;
331 }
332
333 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
334 void* iter = NULL;
335 if (!reply.ReadInt(&iter, &real_pid))
336 goto error;
337 if (real_pid <= 0) {
338 // METHOD_GET_CHILD_WITH_INODE failed. Did the child die already?
339 LOG(ERROR) << "METHOD_GET_CHILD_WITH_INODE failed";
340 goto error;
341 }
342 real_pids_to_sandbox_pids[real_pid] = pid;
[email protected]4229b5d2011-07-16 02:47:37343 }
[email protected]ce2ae442011-07-18 16:13:41344 if (use_helper) {
345 real_pid = pid;
346 if (!helper_->AckChild(pipe_fds[1], channel_switch)) {
347 LOG(ERROR) << "Failed to synchronise with zygote fork helper";
348 goto error;
349 }
350 } else {
351 if (HANDLE_EINTR(write(pipe_fds[1], "x", 1)) != 1) {
352 LOG(ERROR) << "Failed to synchronise with child process";
353 goto error;
354 }
[email protected]28ecba32010-09-16 10:03:09355 }
356 close(pipe_fds[1]);
357 return real_pid;
358 }
359
360 error:
[email protected]72f891f42011-01-12 17:00:52361 if (pid > 0) {
362 if (waitpid(pid, NULL, WNOHANG) == -1)
363 LOG(ERROR) << "Failed to wait for process";
364 }
[email protected]28ecba32010-09-16 10:03:09365 if (dummy_fd >= 0)
366 close(dummy_fd);
367 if (pipe_fds[0] >= 0)
368 close(pipe_fds[0]);
369 if (pipe_fds[1] >= 0)
370 close(pipe_fds[1]);
371 return -1;
372 }
373
[email protected]cc8f1462009-06-12 17:36:55374 // Handle a 'fork' request from the browser: this means that the browser
375 // wishes to start a new renderer.
[email protected]ce2ae442011-07-18 16:13:41376 bool HandleForkRequest(int fd, const Pickle& pickle,
377 void* iter, std::vector<int>& fds) {
[email protected]cc8f1462009-06-12 17:36:55378 std::vector<std::string> args;
379 int argc, numfds;
380 base::GlobalDescriptors::Mapping mapping;
[email protected]8ecd3aad2009-11-04 08:32:22381 base::ProcessId child;
[email protected]ce2ae442011-07-18 16:13:41382 std::string process_type;
383 std::string channel_id;
384 const std::string channel_id_prefix = std::string("--")
385 + switches::kProcessChannelID + std::string("=");
386
387 if (!pickle.ReadString(&iter, &process_type))
388 goto error;
[email protected]cc8f1462009-06-12 17:36:55389
390 if (!pickle.ReadInt(&iter, &argc))
391 goto error;
392
393 for (int i = 0; i < argc; ++i) {
394 std::string arg;
395 if (!pickle.ReadString(&iter, &arg))
396 goto error;
397 args.push_back(arg);
[email protected]ce2ae442011-07-18 16:13:41398 if (arg.compare(0, channel_id_prefix.length(), channel_id_prefix) == 0)
399 channel_id = arg;
[email protected]cc8f1462009-06-12 17:36:55400 }
401
402 if (!pickle.ReadInt(&iter, &numfds))
403 goto error;
404 if (numfds != static_cast<int>(fds.size()))
405 goto error;
406
407 for (int i = 0; i < numfds; ++i) {
408 base::GlobalDescriptors::Key key;
409 if (!pickle.ReadUInt32(&iter, &key))
410 goto error;
411 mapping.push_back(std::make_pair(key, fds[i]));
412 }
413
[email protected]abe3ad92009-06-15 18:15:08414 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22415 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
416
[email protected]ce2ae442011-07-18 16:13:41417 child = ForkWithRealPid(process_type, fds, channel_id);
[email protected]cc8f1462009-06-12 17:36:55418
419 if (!child) {
[email protected]36ea6c6f2010-03-17 20:08:01420#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38421 // Try to open /proc/self/maps as the seccomp sandbox needs access to it
422 if (g_proc_fd >= 0) {
423 int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY);
424 if (proc_self_maps >= 0) {
425 SeccompSandboxSetProcSelfMaps(proc_self_maps);
426 }
427 close(g_proc_fd);
428 g_proc_fd = -1;
429 }
[email protected]8015de32009-11-18 04:13:33430#endif
[email protected]a9c54a172009-11-07 06:09:38431
[email protected]8ecd3aad2009-11-04 08:32:22432 close(kBrowserDescriptor); // our socket from the browser
[email protected]cbcf9cc32010-02-17 04:05:59433 if (g_suid_sandbox_active)
434 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]9b85081af2010-12-07 21:26:47435 base::GlobalDescriptors::GetInstance()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39436
[email protected]a89a55dd2010-04-19 14:51:13437#if defined(CHROMIUM_SELINUX)
438 SELinuxTransitionToTypeOrDie("chromium_renderer_t");
439#endif
440
[email protected]0189bbd2009-10-12 22:50:39441 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55442 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39443 CommandLine::Init(0, NULL);
444 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]74e9fa22010-12-29 21:06:43445
446 // Update the process title. The argv was already cached by the call to
447 // SetProcessTitleFromCommandLine in ChromeMain, so we can pass NULL here
448 // (we don't have the original argv at this point).
449 SetProcessTitleFromCommandLine(NULL);
450
[email protected]c548be22010-03-08 12:55:57451 // The fork() request is handled further up the call stack.
[email protected]cc8f1462009-06-12 17:36:55452 return true;
[email protected]8ecd3aad2009-11-04 08:32:22453 } else if (child < 0) {
[email protected]466cffd2010-06-09 18:10:56454 LOG(ERROR) << "Zygote could not fork: " << errno;
[email protected]8ecd3aad2009-11-04 08:32:22455 goto error;
[email protected]cc8f1462009-06-12 17:36:55456 }
457
[email protected]28ecba32010-09-16 10:03:09458 for (std::vector<int>::const_iterator
459 i = fds.begin(); i != fds.end(); ++i)
460 close(*i);
[email protected]83a0cbe2009-11-04 04:22:47461
[email protected]28ecba32010-09-16 10:03:09462 if (HANDLE_EINTR(write(fd, &child, sizeof(child))) < 0)
463 PLOG(ERROR) << "write";
464 return false;
[email protected]83a0cbe2009-11-04 04:22:47465
466 error:
[email protected]8ecd3aad2009-11-04 08:32:22467 LOG(ERROR) << "Error parsing fork request from browser";
[email protected]83a0cbe2009-11-04 04:22:47468 for (std::vector<int>::const_iterator
469 i = fds.begin(); i != fds.end(); ++i)
470 close(*i);
[email protected]cc8f1462009-06-12 17:36:55471 return false;
472 }
[email protected]8ecd3aad2009-11-04 08:32:22473
[email protected]715b4f262010-07-13 14:17:28474 bool HandleGetSandboxStatus(int fd, const Pickle& pickle, void* iter) {
475 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_)) !=
476 sizeof(sandbox_flags_))) {
477 PLOG(ERROR) << "write";
478 }
479
480 return false;
481 }
482
[email protected]8ecd3aad2009-11-04 08:32:22483 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
484 // fork() returns are not the real PIDs, so we need to map the Real PIDS
485 // into the sandbox PID namespace.
486 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
487 ProcessMap real_pids_to_sandbox_pids;
[email protected]715b4f262010-07-13 14:17:28488
489 const int sandbox_flags_;
[email protected]ce2ae442011-07-18 16:13:41490 ZygoteForkDelegate* helper_;
[email protected]cc8f1462009-06-12 17:36:55491};
492
[email protected]ad6d2c42009-09-15 20:13:38493// With SELinux we can carve out a precise sandbox, so we don't have to play
494// with intercepting libc calls.
495#if !defined(CHROMIUM_SELINUX)
496
[email protected]a0f200ea2009-08-06 18:44:06497static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
498 char* timezone_out,
499 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07500 Pickle request;
501 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
502 request.WriteString(
503 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
504
505 uint8_t reply_buf[512];
[email protected]cf3ac3972010-12-22 20:02:29506 const ssize_t r = UnixDomainSocket::SendRecvMsg(
[email protected]73fa63992009-07-20 20:30:07507 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
508 if (r == -1) {
509 memset(output, 0, sizeof(struct tm));
510 return;
511 }
512
513 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
514 void* iter = NULL;
[email protected]9debcda52009-07-22 20:06:51515 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07516 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51517 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07518 result.size() != sizeof(struct tm)) {
519 memset(output, 0, sizeof(struct tm));
520 return;
521 }
522
523 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51524 if (timezone_out_len) {
525 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
526 memcpy(timezone_out, timezone.data(), copy_len);
527 timezone_out[copy_len] = 0;
528 output->tm_zone = timezone_out;
529 } else {
530 output->tm_zone = NULL;
531 }
[email protected]73fa63992009-07-20 20:30:07532}
533
[email protected]a0f200ea2009-08-06 18:44:06534static bool g_am_zygote_or_renderer = false;
535
536// Sandbox interception of libc calls.
537//
538// Because we are running in a sandbox certain libc calls will fail (localtime
539// being the motivating example - it needs to read /etc/localtime). We need to
540// intercept these calls and proxy them to the browser. However, these calls
541// may come from us or from our libraries. In some cases we can't just change
542// our code.
543//
544// It's for these cases that we have the following setup:
545//
546// We define global functions for those functions which we wish to override.
547// Since we will be first in the dynamic resolution order, the dynamic linker
548// will point callers to our versions of these functions. However, we have the
549// same binary for both the browser and the renderers, which means that our
550// overrides will apply in the browser too.
551//
552// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
553// renderer process. It's set in ZygoteMain and inherited by the renderers when
554// they fork. (This means that it'll be incorrect for global constructor
555// functions and before ZygoteMain is called - beware).
556//
557// Our replacement functions can check this global and either proxy
558// the call to the browser over the sandbox IPC
559// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
560// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
561// current module.
562//
563// Other avenues:
564//
565// Our first attempt involved some assembly to patch the GOT of the current
566// module. This worked, but was platform specific and doesn't catch the case
567// where a library makes a call rather than current module.
568//
569// We also considered patching the function in place, but this would again by
570// platform specific and the above technique seems to work well enough.
571
[email protected]724df992010-09-21 18:19:10572typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
573typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
574 struct tm* result);
575
576static pthread_once_t g_libc_localtime_funcs_guard = PTHREAD_ONCE_INIT;
577static LocaltimeFunction g_libc_localtime;
578static LocaltimeRFunction g_libc_localtime_r;
579
580static void InitLibcLocaltimeFunctions() {
581 g_libc_localtime = reinterpret_cast<LocaltimeFunction>(
582 dlsym(RTLD_NEXT, "localtime"));
583 g_libc_localtime_r = reinterpret_cast<LocaltimeRFunction>(
584 dlsym(RTLD_NEXT, "localtime_r"));
585
586 if (!g_libc_localtime || !g_libc_localtime_r) {
587 // http://code.google.com/p/chromium/issues/detail?id=16800
588 //
589 // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces
590 // it with a version which doesn't work. In this case we'll get a NULL
591 // result. There's not a lot we can do at this point, so we just bodge it!
592 LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been "
593 "reported to be caused by Nvidia's libGL. You should expect"
594 " time related functions to misbehave. "
595 "http://code.google.com/p/chromium/issues/detail?id=16800";
596 }
597
598 if (!g_libc_localtime)
599 g_libc_localtime = gmtime;
600 if (!g_libc_localtime_r)
601 g_libc_localtime_r = gmtime_r;
602}
[email protected]a5d7bb82009-09-08 22:30:58603
[email protected]73fa63992009-07-20 20:30:07604struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06605 if (g_am_zygote_or_renderer) {
606 static struct tm time_struct;
607 static char timezone_string[64];
608 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
609 sizeof(timezone_string));
610 return &time_struct;
611 } else {
[email protected]724df992010-09-21 18:19:10612 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
613 InitLibcLocaltimeFunctions));
614 return g_libc_localtime(timep);
[email protected]a0f200ea2009-08-06 18:44:06615 }
[email protected]73fa63992009-07-20 20:30:07616}
617
618struct tm* localtime_r(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06619 if (g_am_zygote_or_renderer) {
620 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
621 return result;
622 } else {
[email protected]724df992010-09-21 18:19:10623 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
624 InitLibcLocaltimeFunctions));
625 return g_libc_localtime_r(timep, result);
[email protected]a0f200ea2009-08-06 18:44:06626 }
[email protected]73fa63992009-07-20 20:30:07627}
628
[email protected]ad6d2c42009-09-15 20:13:38629#endif // !CHROMIUM_SELINUX
[email protected]a5d7bb82009-09-08 22:30:58630
[email protected]ad6d2c42009-09-15 20:13:38631// This function triggers the static and lazy construction of objects that need
632// to be created before imposing the sandbox.
633static void PreSandboxInit() {
[email protected]b8419412010-03-29 20:18:29634 base::RandUint64();
[email protected]4378a822009-07-08 01:15:14635
[email protected]b8419412010-03-29 20:18:29636 base::SysInfo::MaxSharedMemorySize();
[email protected]80a086c52009-08-04 17:52:04637
[email protected]b8419412010-03-29 20:18:29638 // ICU DateFormat class (used in base/time_format.cc) needs to get the
639 // Olson timezone ID by accessing the zoneinfo files on disk. After
640 // TimeZone::createDefault is called once here, the timezone ID is
641 // cached and there's no more need to access the file system.
642 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
[email protected]1831acf2009-10-05 16:38:41643
[email protected]eeb08552011-03-18 11:15:39644#if defined(USE_NSS)
[email protected]f6a67b42011-03-17 23:49:21645 // NSS libraries are loaded before sandbox is activated. This is to allow
646 // successful initialization of NSS which tries to load extra library files.
647 // Doing so will allow NSS to be used within sandbox for chromoting.
[email protected]4b559b4d2011-04-14 17:37:14648 crypto::LoadNSSLibraries();
[email protected]eeb08552011-03-18 11:15:39649#else
650 // TODO(bulach): implement openssl support.
651 NOTREACHED() << "Remoting is not supported for openssl";
[email protected]f6a67b42011-03-17 23:49:21652#endif
[email protected]ed450f32011-03-16 01:26:49653
[email protected]17773042010-07-28 17:35:07654 // Ensure access to the Pepper plugins before the sandbox is turned on.
655 PepperPluginRegistry::PreloadModules();
[email protected]ad6d2c42009-09-15 20:13:38656}
657
658#if !defined(CHROMIUM_SELINUX)
659static bool EnterSandbox() {
[email protected]b8419412010-03-29 20:18:29660 // The SUID sandbox sets this environment variable to a file descriptor
661 // over which we can signal that we have completed our startup and can be
662 // chrooted.
[email protected]ad6d2c42009-09-15 20:13:38663 const char* const sandbox_fd_string = getenv("SBX_D");
[email protected]ad6d2c42009-09-15 20:13:38664
[email protected]0dc32322010-09-16 09:46:59665 if (sandbox_fd_string) {
666 // Use the SUID sandbox. This still allows the seccomp sandbox to
667 // be enabled by the process later.
[email protected]8ecd3aad2009-11-04 08:32:22668 g_suid_sandbox_active = true;
669
[email protected]ad6d2c42009-09-15 20:13:38670 char* endptr;
671 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
672 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
673 return false;
674 const int fd = fd_long;
675
676 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08677
[email protected]eaebefaf2010-02-23 22:58:18678 static const char kMsgChrootMe = 'C';
679 static const char kMsgChrootSuccessful = 'O';
[email protected]abe3ad92009-06-15 18:15:08680
[email protected]eaebefaf2010-02-23 22:58:18681 if (HANDLE_EINTR(write(fd, &kMsgChrootMe, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32682 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08683 return false;
[email protected]0e1611122009-07-10 18:17:32684 }
[email protected]abe3ad92009-06-15 18:15:08685
[email protected]87ed6952009-07-16 02:52:15686 // We need to reap the chroot helper process in any event:
687 wait(NULL);
688
[email protected]abe3ad92009-06-15 18:15:08689 char reply;
[email protected]87f8ce62009-07-10 19:14:31690 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32691 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08692 return false;
[email protected]0e1611122009-07-10 18:17:32693 }
[email protected]87f8ce62009-07-10 19:14:31694
[email protected]eaebefaf2010-02-23 22:58:18695 if (reply != kMsgChrootSuccessful) {
[email protected]0e1611122009-07-10 18:17:32696 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08697 return false;
[email protected]0e1611122009-07-10 18:17:32698 }
[email protected]abe3ad92009-06-15 18:15:08699
[email protected]cf3ac3972010-12-22 20:02:29700 SkiaFontConfigSetImplementation(
701 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08702
[email protected]415493be2009-07-10 17:50:24703 // Previously, we required that the binary be non-readable. This causes the
704 // kernel to mark the process as non-dumpable at startup. The thinking was
705 // that, although we were putting the renderers into a PID namespace (with
706 // the SUID sandbox), they would nonetheless be in the /same/ PID
707 // namespace. So they could ptrace each other unless they were non-dumpable.
708 //
709 // If the binary was readable, then there would be a window between process
710 // startup and the point where we set the non-dumpable flag in which a
711 // compromised renderer could ptrace attach.
712 //
713 // However, now that we have a zygote model, only the (trusted) zygote
714 // exists at this point and we can set the non-dumpable flag which is
715 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48716 //
717 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
718 // issues, one can specify --allow-sandbox-debugging to let the process be
719 // dumpable.
720 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
721 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
722 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
723 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
724 LOG(ERROR) << "Failed to set non-dumpable flag";
725 return false;
726 }
[email protected]0e1611122009-07-10 18:17:32727 }
[email protected]cc9a9a882011-03-03 20:09:10728 } else if (CommandLine::ForCurrentProcess()->HasSwitch(
729 switches::kEnableSeccompSandbox)) {
[email protected]0dc32322010-09-16 09:46:59730 PreSandboxInit();
[email protected]cf3ac3972010-12-22 20:02:29731 SkiaFontConfigSetImplementation(
732 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08733 } else {
734 SkiaFontConfigUseDirectImplementation();
735 }
736
737 return true;
738}
[email protected]ad6d2c42009-09-15 20:13:38739#else // CHROMIUM_SELINUX
740
741static bool EnterSandbox() {
742 PreSandboxInit();
743 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
[email protected]ad6d2c42009-09-15 20:13:38744 return true;
745}
746
747#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08748
[email protected]ce2ae442011-07-18 16:13:41749bool ZygoteMain(const MainFunctionParams& params,
750 ZygoteForkDelegate* forkdelegate) {
[email protected]ad6d2c42009-09-15 20:13:38751#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06752 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38753#endif
[email protected]a0f200ea2009-08-06 18:44:06754
[email protected]36ea6c6f2010-03-17 20:08:01755#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38756 // The seccomp sandbox needs access to files in /proc, which might be denied
757 // after one of the other sandboxes have been started. So, obtain a suitable
758 // file handle in advance.
[email protected]cc9a9a882011-03-03 20:09:10759 if (CommandLine::ForCurrentProcess()->HasSwitch(
760 switches::kEnableSeccompSandbox)) {
[email protected]a9c54a172009-11-07 06:09:38761 g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY);
762 if (g_proc_fd < 0) {
763 LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp "
764 "sandboxing.";
765 }
766 }
[email protected]36ea6c6f2010-03-17 20:08:01767#endif // SECCOMP_SANDBOX
[email protected]a9c54a172009-11-07 06:09:38768
[email protected]ce2ae442011-07-18 16:13:41769 if (forkdelegate != NULL) {
770 VLOG(1) << "ZygoteMain: initializing fork delegate";
771 forkdelegate->Init(getenv("SBX_D") != NULL, // g_suid_sandbox_active,
772 kBrowserDescriptor,
773 kMagicSandboxIPCDescriptor);
774 } else {
775 VLOG(1) << "ZygoteMain: fork delegate is NULL";
776 }
777
[email protected]a9c54a172009-11-07 06:09:38778 // Turn on the SELinux or SUID sandbox
779 if (!EnterSandbox()) {
780 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
781 << errno << ")";
782 return false;
783 }
784
[email protected]715b4f262010-07-13 14:17:28785 int sandbox_flags = 0;
786 if (getenv("SBX_D"))
787 sandbox_flags |= ZygoteHost::kSandboxSUID;
788 if (getenv("SBX_PID_NS"))
789 sandbox_flags |= ZygoteHost::kSandboxPIDNS;
790 if (getenv("SBX_NET_NS"))
791 sandbox_flags |= ZygoteHost::kSandboxNetNS;
792
[email protected]36ea6c6f2010-03-17 20:08:01793#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38794 // The seccomp sandbox will be turned on when the renderers start. But we can
795 // already check if sufficient support is available so that we only need to
796 // print one error message for the entire browser session.
[email protected]cc9a9a882011-03-03 20:09:10797 if (g_proc_fd >= 0 && CommandLine::ForCurrentProcess()->HasSwitch(
798 switches::kEnableSeccompSandbox)) {
[email protected]a9c54a172009-11-07 06:09:38799 if (!SupportsSeccompSandbox(g_proc_fd)) {
[email protected]e8c916a2009-11-04 17:52:47800 // There are a good number of users who cannot use the seccomp sandbox
801 // (e.g. because their distribution does not enable seccomp mode by
802 // default). While we would prefer to deny execution in this case, it
803 // seems more realistic to continue in degraded mode.
[email protected]1b5d28f2010-02-18 15:53:36804 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
805 "Seccomp sandbox. Running renderers with Seccomp "
806 "sandboxing disabled.";
[email protected]e8c916a2009-11-04 17:52:47807 } else {
[email protected]8e96e502010-10-21 20:57:12808 VLOG(1) << "Enabling experimental Seccomp sandbox.";
[email protected]715b4f262010-07-13 14:17:28809 sandbox_flags |= ZygoteHost::kSandboxSeccomp;
[email protected]e8c916a2009-11-04 17:52:47810 }
811 }
[email protected]36ea6c6f2010-03-17 20:08:01812#endif // SECCOMP_SANDBOX
[email protected]e8c916a2009-11-04 17:52:47813
[email protected]ce2ae442011-07-18 16:13:41814 Zygote zygote(sandbox_flags, forkdelegate);
[email protected]c548be22010-03-08 12:55:57815 // This function call can return multiple times, once per fork().
[email protected]cc8f1462009-06-12 17:36:55816 return zygote.ProcessRequests();
817}