blob: 95b3975b921145bc626bf0a9cf8ebaaf140428d1 [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]ce2ae442011-07-18 16:13:41102 explicit Zygote(int sandbox_flags, ZygoteForkDelegate* helper)
103 : sandbox_flags_(sandbox_flags),
104 helper_(helper) {
[email protected]715b4f262010-07-13 14:17:28105 }
106
[email protected]cc8f1462009-06-12 17:36:55107 bool ProcessRequests() {
108 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
109 // browser on it.
[email protected]8ecd3aad2009-11-04 08:32:22110 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
[email protected]abe3ad92009-06-15 18:15:08111 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:55112
113 // We need to accept SIGCHLD, even though our handler is a no-op because
114 // otherwise we cannot wait on children. (According to POSIX 2001.)
115 struct sigaction action;
116 memset(&action, 0, sizeof(action));
117 action.sa_handler = SIGCHLDHandler;
118 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
119
[email protected]8ecd3aad2009-11-04 08:32:22120 if (g_suid_sandbox_active) {
121 // Let the ZygoteHost know we are ready to go.
122 // The receiving code is in chrome/browser/zygote_host_linux.cc.
123 std::vector<int> empty;
[email protected]cf3ac3972010-12-22 20:02:29124 bool r = UnixDomainSocket::SendMsg(kBrowserDescriptor, kZygoteMagic,
125 sizeof(kZygoteMagic), empty);
[email protected]8ecd3aad2009-11-04 08:32:22126 CHECK(r) << "Sending zygote magic failed";
127 }
128
[email protected]cc8f1462009-06-12 17:36:55129 for (;;) {
[email protected]c548be22010-03-08 12:55:57130 // This function call can return multiple times, once per fork().
[email protected]8ecd3aad2009-11-04 08:32:22131 if (HandleRequestFromBrowser(kBrowserDescriptor))
[email protected]cc8f1462009-06-12 17:36:55132 return true;
133 }
134 }
135
136 private:
137 // See comment below, where sigaction is called.
138 static void SIGCHLDHandler(int signal) { }
139
140 // ---------------------------------------------------------------------------
141 // Requests from the browser...
142
143 // Read and process a request from the browser. Returns true if we are in a
144 // new process and thus need to unwind back into ChromeMain.
145 bool HandleRequestFromBrowser(int fd) {
146 std::vector<int> fds;
[email protected]2752bf62011-03-23 21:06:06147 static const unsigned kMaxMessageLength = 2048;
[email protected]cc8f1462009-06-12 17:36:55148 char buf[kMaxMessageLength];
[email protected]cf3ac3972010-12-22 20:02:29149 const ssize_t len = UnixDomainSocket::RecvMsg(fd, buf, sizeof(buf), &fds);
[email protected]7ae27b92010-09-24 17:33:36150
151 if (len == 0 || (len == -1 && errno == ECONNRESET)) {
152 // EOF from the browser. We should die.
153 _exit(0);
[email protected]cc8f1462009-06-12 17:36:55154 return false;
155 }
156
[email protected]7ae27b92010-09-24 17:33:36157 if (len == -1) {
158 PLOG(ERROR) << "Error reading message from browser";
[email protected]cc8f1462009-06-12 17:36:55159 return false;
160 }
161
162 Pickle pickle(buf, len);
163 void* iter = NULL;
164
165 int kind;
[email protected]57113ea2009-06-18 02:23:16166 if (pickle.ReadInt(&iter, &kind)) {
167 switch (kind) {
168 case ZygoteHost::kCmdFork:
[email protected]c548be22010-03-08 12:55:57169 // This function call can return multiple times, once per fork().
[email protected]57113ea2009-06-18 02:23:16170 return HandleForkRequest(fd, pickle, iter, fds);
[email protected]ce2ae442011-07-18 16:13:41171
[email protected]57113ea2009-06-18 02:23:16172 case ZygoteHost::kCmdReap:
173 if (!fds.empty())
174 break;
[email protected]c548be22010-03-08 12:55:57175 HandleReapRequest(fd, pickle, iter);
176 return false;
[email protected]443b80e2010-12-14 00:42:23177 case ZygoteHost::kCmdGetTerminationStatus:
[email protected]57113ea2009-06-18 02:23:16178 if (!fds.empty())
179 break;
[email protected]443b80e2010-12-14 00:42:23180 HandleGetTerminationStatus(fd, pickle, iter);
[email protected]c548be22010-03-08 12:55:57181 return false;
[email protected]715b4f262010-07-13 14:17:28182 case ZygoteHost::kCmdGetSandboxStatus:
183 HandleGetSandboxStatus(fd, pickle, iter);
184 return false;
[email protected]57113ea2009-06-18 02:23:16185 default:
186 NOTREACHED();
187 break;
188 }
[email protected]cc8f1462009-06-12 17:36:55189 }
190
[email protected]cc8f1462009-06-12 17:36:55191 LOG(WARNING) << "Error parsing message from browser";
192 for (std::vector<int>::const_iterator
193 i = fds.begin(); i != fds.end(); ++i)
194 close(*i);
195 return false;
196 }
197
[email protected]c548be22010-03-08 12:55:57198 void HandleReapRequest(int fd, const Pickle& pickle, void* iter) {
[email protected]8ecd3aad2009-11-04 08:32:22199 base::ProcessId child;
200 base::ProcessId actual_child;
[email protected]cc8f1462009-06-12 17:36:55201
202 if (!pickle.ReadInt(&iter, &child)) {
203 LOG(WARNING) << "Error parsing reap request from browser";
[email protected]c548be22010-03-08 12:55:57204 return;
[email protected]cc8f1462009-06-12 17:36:55205 }
206
[email protected]8ecd3aad2009-11-04 08:32:22207 if (g_suid_sandbox_active) {
208 actual_child = real_pids_to_sandbox_pids[child];
209 if (!actual_child)
[email protected]c548be22010-03-08 12:55:57210 return;
[email protected]8ecd3aad2009-11-04 08:32:22211 real_pids_to_sandbox_pids.erase(child);
212 } else {
213 actual_child = child;
214 }
215
216 ProcessWatcher::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55217 }
218
[email protected]443b80e2010-12-14 00:42:23219 void HandleGetTerminationStatus(int fd, const Pickle& pickle, void* iter) {
[email protected]57113ea2009-06-18 02:23:16220 base::ProcessHandle child;
221
222 if (!pickle.ReadInt(&iter, &child)) {
[email protected]443b80e2010-12-14 00:42:23223 LOG(WARNING) << "Error parsing GetTerminationStatus request "
224 << "from browser";
[email protected]c548be22010-03-08 12:55:57225 return;
[email protected]57113ea2009-06-18 02:23:16226 }
227
[email protected]443b80e2010-12-14 00:42:23228 base::TerminationStatus status;
229 int exit_code;
[email protected]8ecd3aad2009-11-04 08:32:22230 if (g_suid_sandbox_active)
231 child = real_pids_to_sandbox_pids[child];
[email protected]443b80e2010-12-14 00:42:23232 if (child) {
233 status = base::GetTerminationStatus(child, &exit_code);
234 } else {
235 // Assume that if we can't find the child in the sandbox, then
236 // it terminated normally.
237 status = base::TERMINATION_STATUS_NORMAL_TERMINATION;
[email protected]36d772c2011-07-15 20:47:40238 exit_code = ResultCodes::NORMAL_EXIT;
[email protected]443b80e2010-12-14 00:42:23239 }
[email protected]57113ea2009-06-18 02:23:16240
241 Pickle write_pickle;
[email protected]443b80e2010-12-14 00:42:23242 write_pickle.WriteInt(static_cast<int>(status));
243 write_pickle.WriteInt(exit_code);
[email protected]8a861402011-01-28 19:59:11244 ssize_t written =
245 HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size()));
246 if (written != static_cast<ssize_t>(write_pickle.size()))
[email protected]19cb9292010-04-16 23:00:15247 PLOG(ERROR) << "write";
[email protected]57113ea2009-06-18 02:23:16248 }
249
[email protected]28ecba32010-09-16 10:03:09250 // This is equivalent to fork(), except that, when using the SUID
251 // sandbox, it returns the real PID of the child process as it
252 // appears outside the sandbox, rather than returning the PID inside
253 // the sandbox.
[email protected]ce2ae442011-07-18 16:13:41254 int ForkWithRealPid(const std::string& process_type, std::vector<int>& fds,
255 const std::string& channel_switch) {
256 const bool use_helper = (helper_ && helper_->CanHelp(process_type));
257 if (!(use_helper || g_suid_sandbox_active)) {
[email protected]28ecba32010-09-16 10:03:09258 return fork();
[email protected]ce2ae442011-07-18 16:13:41259 }
[email protected]28ecba32010-09-16 10:03:09260
261 int dummy_fd;
262 ino_t dummy_inode;
263 int pipe_fds[2] = { -1, -1 };
[email protected]073040b2010-09-26 02:35:45264 base::ProcessId pid = 0;
[email protected]28ecba32010-09-16 10:03:09265
266 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
267 if (dummy_fd < 0) {
268 LOG(ERROR) << "Failed to create dummy FD";
269 goto error;
270 }
271 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd)) {
272 LOG(ERROR) << "Failed to get inode for dummy FD";
273 goto error;
274 }
275 if (pipe(pipe_fds) != 0) {
276 LOG(ERROR) << "Failed to create pipe";
277 goto error;
278 }
279
[email protected]ce2ae442011-07-18 16:13:41280 if (use_helper) {
281 fds.push_back(dummy_fd);
282 fds.push_back(pipe_fds[0]);
283 pid = helper_->Fork(fds);
284 } else {
285 pid = fork();
286 }
[email protected]28ecba32010-09-16 10:03:09287 if (pid < 0) {
288 goto error;
289 } else if (pid == 0) {
290 // In the child process.
291 close(pipe_fds[1]);
292 char buffer[1];
293 // Wait until the parent process has discovered our PID. We
294 // should not fork any child processes (which the seccomp
295 // sandbox does) until then, because that can interfere with the
296 // parent's discovery of our PID.
297 if (HANDLE_EINTR(read(pipe_fds[0], buffer, 1)) != 1 ||
298 buffer[0] != 'x') {
299 LOG(FATAL) << "Failed to synchronise with parent zygote process";
300 }
301 close(pipe_fds[0]);
302 close(dummy_fd);
303 return 0;
304 } else {
305 // In the parent process.
306 close(dummy_fd);
307 dummy_fd = -1;
308 close(pipe_fds[0]);
309 pipe_fds[0] = -1;
[email protected]4229b5d2011-07-16 02:47:37310 base::ProcessId real_pid;
[email protected]ce2ae442011-07-18 16:13:41311 if (g_suid_sandbox_active) {
312 uint8_t reply_buf[512];
313 Pickle request;
314 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
315 request.WriteUInt64(dummy_inode);
316
317 const ssize_t r = UnixDomainSocket::SendRecvMsg(
318 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL,
319 request);
320 if (r == -1) {
321 LOG(ERROR) << "Failed to get child process's real PID";
322 goto error;
323 }
324
325 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
326 void* iter = NULL;
327 if (!reply.ReadInt(&iter, &real_pid))
328 goto error;
329 if (real_pid <= 0) {
330 // METHOD_GET_CHILD_WITH_INODE failed. Did the child die already?
331 LOG(ERROR) << "METHOD_GET_CHILD_WITH_INODE failed";
332 goto error;
333 }
334 real_pids_to_sandbox_pids[real_pid] = pid;
[email protected]4229b5d2011-07-16 02:47:37335 }
[email protected]ce2ae442011-07-18 16:13:41336 if (use_helper) {
337 real_pid = pid;
338 if (!helper_->AckChild(pipe_fds[1], channel_switch)) {
339 LOG(ERROR) << "Failed to synchronise with zygote fork helper";
340 goto error;
341 }
342 } else {
343 if (HANDLE_EINTR(write(pipe_fds[1], "x", 1)) != 1) {
344 LOG(ERROR) << "Failed to synchronise with child process";
345 goto error;
346 }
[email protected]28ecba32010-09-16 10:03:09347 }
348 close(pipe_fds[1]);
349 return real_pid;
350 }
351
352 error:
[email protected]72f891f42011-01-12 17:00:52353 if (pid > 0) {
354 if (waitpid(pid, NULL, WNOHANG) == -1)
355 LOG(ERROR) << "Failed to wait for process";
356 }
[email protected]28ecba32010-09-16 10:03:09357 if (dummy_fd >= 0)
358 close(dummy_fd);
359 if (pipe_fds[0] >= 0)
360 close(pipe_fds[0]);
361 if (pipe_fds[1] >= 0)
362 close(pipe_fds[1]);
363 return -1;
364 }
365
[email protected]cc8f1462009-06-12 17:36:55366 // Handle a 'fork' request from the browser: this means that the browser
367 // wishes to start a new renderer.
[email protected]ce2ae442011-07-18 16:13:41368 bool HandleForkRequest(int fd, const Pickle& pickle,
369 void* iter, std::vector<int>& fds) {
[email protected]cc8f1462009-06-12 17:36:55370 std::vector<std::string> args;
371 int argc, numfds;
372 base::GlobalDescriptors::Mapping mapping;
[email protected]8ecd3aad2009-11-04 08:32:22373 base::ProcessId child;
[email protected]ce2ae442011-07-18 16:13:41374 std::string process_type;
375 std::string channel_id;
376 const std::string channel_id_prefix = std::string("--")
377 + switches::kProcessChannelID + std::string("=");
378
379 if (!pickle.ReadString(&iter, &process_type))
380 goto error;
[email protected]cc8f1462009-06-12 17:36:55381
382 if (!pickle.ReadInt(&iter, &argc))
383 goto error;
384
385 for (int i = 0; i < argc; ++i) {
386 std::string arg;
387 if (!pickle.ReadString(&iter, &arg))
388 goto error;
389 args.push_back(arg);
[email protected]ce2ae442011-07-18 16:13:41390 if (arg.compare(0, channel_id_prefix.length(), channel_id_prefix) == 0)
391 channel_id = arg;
[email protected]cc8f1462009-06-12 17:36:55392 }
393
394 if (!pickle.ReadInt(&iter, &numfds))
395 goto error;
396 if (numfds != static_cast<int>(fds.size()))
397 goto error;
398
399 for (int i = 0; i < numfds; ++i) {
400 base::GlobalDescriptors::Key key;
401 if (!pickle.ReadUInt32(&iter, &key))
402 goto error;
403 mapping.push_back(std::make_pair(key, fds[i]));
404 }
405
[email protected]abe3ad92009-06-15 18:15:08406 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22407 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
408
[email protected]ce2ae442011-07-18 16:13:41409 child = ForkWithRealPid(process_type, fds, channel_id);
[email protected]cc8f1462009-06-12 17:36:55410
411 if (!child) {
[email protected]36ea6c6f2010-03-17 20:08:01412#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38413 // Try to open /proc/self/maps as the seccomp sandbox needs access to it
414 if (g_proc_fd >= 0) {
415 int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY);
416 if (proc_self_maps >= 0) {
417 SeccompSandboxSetProcSelfMaps(proc_self_maps);
418 }
419 close(g_proc_fd);
420 g_proc_fd = -1;
421 }
[email protected]8015de32009-11-18 04:13:33422#endif
[email protected]a9c54a172009-11-07 06:09:38423
[email protected]8ecd3aad2009-11-04 08:32:22424 close(kBrowserDescriptor); // our socket from the browser
[email protected]cbcf9cc32010-02-17 04:05:59425 if (g_suid_sandbox_active)
426 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]9b85081af2010-12-07 21:26:47427 base::GlobalDescriptors::GetInstance()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39428
[email protected]a89a55dd2010-04-19 14:51:13429#if defined(CHROMIUM_SELINUX)
430 SELinuxTransitionToTypeOrDie("chromium_renderer_t");
431#endif
432
[email protected]0189bbd2009-10-12 22:50:39433 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55434 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39435 CommandLine::Init(0, NULL);
436 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]74e9fa22010-12-29 21:06:43437
438 // Update the process title. The argv was already cached by the call to
439 // SetProcessTitleFromCommandLine in ChromeMain, so we can pass NULL here
440 // (we don't have the original argv at this point).
441 SetProcessTitleFromCommandLine(NULL);
442
[email protected]c548be22010-03-08 12:55:57443 // The fork() request is handled further up the call stack.
[email protected]cc8f1462009-06-12 17:36:55444 return true;
[email protected]8ecd3aad2009-11-04 08:32:22445 } else if (child < 0) {
[email protected]466cffd2010-06-09 18:10:56446 LOG(ERROR) << "Zygote could not fork: " << errno;
[email protected]8ecd3aad2009-11-04 08:32:22447 goto error;
[email protected]cc8f1462009-06-12 17:36:55448 }
449
[email protected]28ecba32010-09-16 10:03:09450 for (std::vector<int>::const_iterator
451 i = fds.begin(); i != fds.end(); ++i)
452 close(*i);
[email protected]83a0cbe2009-11-04 04:22:47453
[email protected]28ecba32010-09-16 10:03:09454 if (HANDLE_EINTR(write(fd, &child, sizeof(child))) < 0)
455 PLOG(ERROR) << "write";
456 return false;
[email protected]83a0cbe2009-11-04 04:22:47457
458 error:
[email protected]8ecd3aad2009-11-04 08:32:22459 LOG(ERROR) << "Error parsing fork request from browser";
[email protected]83a0cbe2009-11-04 04:22:47460 for (std::vector<int>::const_iterator
461 i = fds.begin(); i != fds.end(); ++i)
462 close(*i);
[email protected]cc8f1462009-06-12 17:36:55463 return false;
464 }
[email protected]8ecd3aad2009-11-04 08:32:22465
[email protected]715b4f262010-07-13 14:17:28466 bool HandleGetSandboxStatus(int fd, const Pickle& pickle, void* iter) {
467 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_)) !=
468 sizeof(sandbox_flags_))) {
469 PLOG(ERROR) << "write";
470 }
471
472 return false;
473 }
474
[email protected]8ecd3aad2009-11-04 08:32:22475 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
476 // fork() returns are not the real PIDs, so we need to map the Real PIDS
477 // into the sandbox PID namespace.
478 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
479 ProcessMap real_pids_to_sandbox_pids;
[email protected]715b4f262010-07-13 14:17:28480
481 const int sandbox_flags_;
[email protected]ce2ae442011-07-18 16:13:41482 ZygoteForkDelegate* helper_;
[email protected]cc8f1462009-06-12 17:36:55483};
484
[email protected]ad6d2c42009-09-15 20:13:38485// With SELinux we can carve out a precise sandbox, so we don't have to play
486// with intercepting libc calls.
487#if !defined(CHROMIUM_SELINUX)
488
[email protected]a0f200ea2009-08-06 18:44:06489static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
490 char* timezone_out,
491 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07492 Pickle request;
493 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
494 request.WriteString(
495 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
496
497 uint8_t reply_buf[512];
[email protected]cf3ac3972010-12-22 20:02:29498 const ssize_t r = UnixDomainSocket::SendRecvMsg(
[email protected]73fa63992009-07-20 20:30:07499 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
500 if (r == -1) {
501 memset(output, 0, sizeof(struct tm));
502 return;
503 }
504
505 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
506 void* iter = NULL;
[email protected]9debcda52009-07-22 20:06:51507 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07508 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51509 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07510 result.size() != sizeof(struct tm)) {
511 memset(output, 0, sizeof(struct tm));
512 return;
513 }
514
515 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51516 if (timezone_out_len) {
517 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
518 memcpy(timezone_out, timezone.data(), copy_len);
519 timezone_out[copy_len] = 0;
520 output->tm_zone = timezone_out;
521 } else {
522 output->tm_zone = NULL;
523 }
[email protected]73fa63992009-07-20 20:30:07524}
525
[email protected]a0f200ea2009-08-06 18:44:06526static bool g_am_zygote_or_renderer = false;
527
528// Sandbox interception of libc calls.
529//
530// Because we are running in a sandbox certain libc calls will fail (localtime
531// being the motivating example - it needs to read /etc/localtime). We need to
532// intercept these calls and proxy them to the browser. However, these calls
533// may come from us or from our libraries. In some cases we can't just change
534// our code.
535//
536// It's for these cases that we have the following setup:
537//
538// We define global functions for those functions which we wish to override.
539// Since we will be first in the dynamic resolution order, the dynamic linker
540// will point callers to our versions of these functions. However, we have the
541// same binary for both the browser and the renderers, which means that our
542// overrides will apply in the browser too.
543//
544// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
545// renderer process. It's set in ZygoteMain and inherited by the renderers when
546// they fork. (This means that it'll be incorrect for global constructor
547// functions and before ZygoteMain is called - beware).
548//
549// Our replacement functions can check this global and either proxy
550// the call to the browser over the sandbox IPC
551// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
552// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
553// current module.
554//
555// Other avenues:
556//
557// Our first attempt involved some assembly to patch the GOT of the current
558// module. This worked, but was platform specific and doesn't catch the case
559// where a library makes a call rather than current module.
560//
561// We also considered patching the function in place, but this would again by
562// platform specific and the above technique seems to work well enough.
563
[email protected]724df992010-09-21 18:19:10564typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
565typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
566 struct tm* result);
567
568static pthread_once_t g_libc_localtime_funcs_guard = PTHREAD_ONCE_INIT;
569static LocaltimeFunction g_libc_localtime;
570static LocaltimeRFunction g_libc_localtime_r;
571
572static void InitLibcLocaltimeFunctions() {
573 g_libc_localtime = reinterpret_cast<LocaltimeFunction>(
574 dlsym(RTLD_NEXT, "localtime"));
575 g_libc_localtime_r = reinterpret_cast<LocaltimeRFunction>(
576 dlsym(RTLD_NEXT, "localtime_r"));
577
578 if (!g_libc_localtime || !g_libc_localtime_r) {
579 // http://code.google.com/p/chromium/issues/detail?id=16800
580 //
581 // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces
582 // it with a version which doesn't work. In this case we'll get a NULL
583 // result. There's not a lot we can do at this point, so we just bodge it!
584 LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been "
585 "reported to be caused by Nvidia's libGL. You should expect"
586 " time related functions to misbehave. "
587 "http://code.google.com/p/chromium/issues/detail?id=16800";
588 }
589
590 if (!g_libc_localtime)
591 g_libc_localtime = gmtime;
592 if (!g_libc_localtime_r)
593 g_libc_localtime_r = gmtime_r;
594}
[email protected]a5d7bb82009-09-08 22:30:58595
[email protected]73fa63992009-07-20 20:30:07596struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06597 if (g_am_zygote_or_renderer) {
598 static struct tm time_struct;
599 static char timezone_string[64];
600 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
601 sizeof(timezone_string));
602 return &time_struct;
603 } else {
[email protected]724df992010-09-21 18:19:10604 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
605 InitLibcLocaltimeFunctions));
606 return g_libc_localtime(timep);
[email protected]a0f200ea2009-08-06 18:44:06607 }
[email protected]73fa63992009-07-20 20:30:07608}
609
610struct tm* localtime_r(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06611 if (g_am_zygote_or_renderer) {
612 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
613 return result;
614 } else {
[email protected]724df992010-09-21 18:19:10615 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
616 InitLibcLocaltimeFunctions));
617 return g_libc_localtime_r(timep, result);
[email protected]a0f200ea2009-08-06 18:44:06618 }
[email protected]73fa63992009-07-20 20:30:07619}
620
[email protected]ad6d2c42009-09-15 20:13:38621#endif // !CHROMIUM_SELINUX
[email protected]a5d7bb82009-09-08 22:30:58622
[email protected]ad6d2c42009-09-15 20:13:38623// This function triggers the static and lazy construction of objects that need
624// to be created before imposing the sandbox.
625static void PreSandboxInit() {
[email protected]b8419412010-03-29 20:18:29626 base::RandUint64();
[email protected]4378a822009-07-08 01:15:14627
[email protected]b8419412010-03-29 20:18:29628 base::SysInfo::MaxSharedMemorySize();
[email protected]80a086c52009-08-04 17:52:04629
[email protected]b8419412010-03-29 20:18:29630 // ICU DateFormat class (used in base/time_format.cc) needs to get the
631 // Olson timezone ID by accessing the zoneinfo files on disk. After
632 // TimeZone::createDefault is called once here, the timezone ID is
633 // cached and there's no more need to access the file system.
634 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
[email protected]1831acf2009-10-05 16:38:41635
[email protected]eeb08552011-03-18 11:15:39636#if defined(USE_NSS)
[email protected]f6a67b42011-03-17 23:49:21637 // NSS libraries are loaded before sandbox is activated. This is to allow
638 // successful initialization of NSS which tries to load extra library files.
639 // Doing so will allow NSS to be used within sandbox for chromoting.
[email protected]4b559b4d2011-04-14 17:37:14640 crypto::LoadNSSLibraries();
[email protected]eeb08552011-03-18 11:15:39641#else
642 // TODO(bulach): implement openssl support.
643 NOTREACHED() << "Remoting is not supported for openssl";
[email protected]f6a67b42011-03-17 23:49:21644#endif
[email protected]ed450f32011-03-16 01:26:49645
[email protected]17773042010-07-28 17:35:07646 // Ensure access to the Pepper plugins before the sandbox is turned on.
647 PepperPluginRegistry::PreloadModules();
[email protected]ad6d2c42009-09-15 20:13:38648}
649
650#if !defined(CHROMIUM_SELINUX)
651static bool EnterSandbox() {
[email protected]b8419412010-03-29 20:18:29652 // The SUID sandbox sets this environment variable to a file descriptor
653 // over which we can signal that we have completed our startup and can be
654 // chrooted.
[email protected]ad6d2c42009-09-15 20:13:38655 const char* const sandbox_fd_string = getenv("SBX_D");
[email protected]ad6d2c42009-09-15 20:13:38656
[email protected]0dc32322010-09-16 09:46:59657 if (sandbox_fd_string) {
658 // Use the SUID sandbox. This still allows the seccomp sandbox to
659 // be enabled by the process later.
[email protected]8ecd3aad2009-11-04 08:32:22660 g_suid_sandbox_active = true;
661
[email protected]ad6d2c42009-09-15 20:13:38662 char* endptr;
663 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
664 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
665 return false;
666 const int fd = fd_long;
667
668 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08669
[email protected]eaebefaf2010-02-23 22:58:18670 static const char kMsgChrootMe = 'C';
671 static const char kMsgChrootSuccessful = 'O';
[email protected]abe3ad92009-06-15 18:15:08672
[email protected]eaebefaf2010-02-23 22:58:18673 if (HANDLE_EINTR(write(fd, &kMsgChrootMe, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32674 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08675 return false;
[email protected]0e1611122009-07-10 18:17:32676 }
[email protected]abe3ad92009-06-15 18:15:08677
[email protected]87ed6952009-07-16 02:52:15678 // We need to reap the chroot helper process in any event:
679 wait(NULL);
680
[email protected]abe3ad92009-06-15 18:15:08681 char reply;
[email protected]87f8ce62009-07-10 19:14:31682 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32683 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08684 return false;
[email protected]0e1611122009-07-10 18:17:32685 }
[email protected]87f8ce62009-07-10 19:14:31686
[email protected]eaebefaf2010-02-23 22:58:18687 if (reply != kMsgChrootSuccessful) {
[email protected]0e1611122009-07-10 18:17:32688 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08689 return false;
[email protected]0e1611122009-07-10 18:17:32690 }
[email protected]abe3ad92009-06-15 18:15:08691
[email protected]cf3ac3972010-12-22 20:02:29692 SkiaFontConfigSetImplementation(
693 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08694
[email protected]415493be2009-07-10 17:50:24695 // Previously, we required that the binary be non-readable. This causes the
696 // kernel to mark the process as non-dumpable at startup. The thinking was
697 // that, although we were putting the renderers into a PID namespace (with
698 // the SUID sandbox), they would nonetheless be in the /same/ PID
699 // namespace. So they could ptrace each other unless they were non-dumpable.
700 //
701 // If the binary was readable, then there would be a window between process
702 // startup and the point where we set the non-dumpable flag in which a
703 // compromised renderer could ptrace attach.
704 //
705 // However, now that we have a zygote model, only the (trusted) zygote
706 // exists at this point and we can set the non-dumpable flag which is
707 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48708 //
709 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
710 // issues, one can specify --allow-sandbox-debugging to let the process be
711 // dumpable.
712 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
713 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
714 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
715 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
716 LOG(ERROR) << "Failed to set non-dumpable flag";
717 return false;
718 }
[email protected]0e1611122009-07-10 18:17:32719 }
[email protected]cc9a9a882011-03-03 20:09:10720 } else if (CommandLine::ForCurrentProcess()->HasSwitch(
721 switches::kEnableSeccompSandbox)) {
[email protected]0dc32322010-09-16 09:46:59722 PreSandboxInit();
[email protected]cf3ac3972010-12-22 20:02:29723 SkiaFontConfigSetImplementation(
724 new FontConfigIPC(kMagicSandboxIPCDescriptor));
[email protected]abe3ad92009-06-15 18:15:08725 } else {
726 SkiaFontConfigUseDirectImplementation();
727 }
728
729 return true;
730}
[email protected]ad6d2c42009-09-15 20:13:38731#else // CHROMIUM_SELINUX
732
733static bool EnterSandbox() {
734 PreSandboxInit();
735 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
[email protected]ad6d2c42009-09-15 20:13:38736 return true;
737}
738
739#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08740
[email protected]ce2ae442011-07-18 16:13:41741bool ZygoteMain(const MainFunctionParams& params,
742 ZygoteForkDelegate* forkdelegate) {
[email protected]ad6d2c42009-09-15 20:13:38743#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06744 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38745#endif
[email protected]a0f200ea2009-08-06 18:44:06746
[email protected]36ea6c6f2010-03-17 20:08:01747#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38748 // The seccomp sandbox needs access to files in /proc, which might be denied
749 // after one of the other sandboxes have been started. So, obtain a suitable
750 // file handle in advance.
[email protected]cc9a9a882011-03-03 20:09:10751 if (CommandLine::ForCurrentProcess()->HasSwitch(
752 switches::kEnableSeccompSandbox)) {
[email protected]a9c54a172009-11-07 06:09:38753 g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY);
754 if (g_proc_fd < 0) {
755 LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp "
756 "sandboxing.";
757 }
758 }
[email protected]36ea6c6f2010-03-17 20:08:01759#endif // SECCOMP_SANDBOX
[email protected]a9c54a172009-11-07 06:09:38760
[email protected]ce2ae442011-07-18 16:13:41761 if (forkdelegate != NULL) {
762 VLOG(1) << "ZygoteMain: initializing fork delegate";
763 forkdelegate->Init(getenv("SBX_D") != NULL, // g_suid_sandbox_active,
764 kBrowserDescriptor,
765 kMagicSandboxIPCDescriptor);
766 } else {
767 VLOG(1) << "ZygoteMain: fork delegate is NULL";
768 }
769
[email protected]a9c54a172009-11-07 06:09:38770 // Turn on the SELinux or SUID sandbox
771 if (!EnterSandbox()) {
772 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
773 << errno << ")";
774 return false;
775 }
776
[email protected]715b4f262010-07-13 14:17:28777 int sandbox_flags = 0;
778 if (getenv("SBX_D"))
779 sandbox_flags |= ZygoteHost::kSandboxSUID;
780 if (getenv("SBX_PID_NS"))
781 sandbox_flags |= ZygoteHost::kSandboxPIDNS;
782 if (getenv("SBX_NET_NS"))
783 sandbox_flags |= ZygoteHost::kSandboxNetNS;
784
[email protected]36ea6c6f2010-03-17 20:08:01785#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38786 // The seccomp sandbox will be turned on when the renderers start. But we can
787 // already check if sufficient support is available so that we only need to
788 // print one error message for the entire browser session.
[email protected]cc9a9a882011-03-03 20:09:10789 if (g_proc_fd >= 0 && CommandLine::ForCurrentProcess()->HasSwitch(
790 switches::kEnableSeccompSandbox)) {
[email protected]a9c54a172009-11-07 06:09:38791 if (!SupportsSeccompSandbox(g_proc_fd)) {
[email protected]e8c916a2009-11-04 17:52:47792 // There are a good number of users who cannot use the seccomp sandbox
793 // (e.g. because their distribution does not enable seccomp mode by
794 // default). While we would prefer to deny execution in this case, it
795 // seems more realistic to continue in degraded mode.
[email protected]1b5d28f2010-02-18 15:53:36796 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
797 "Seccomp sandbox. Running renderers with Seccomp "
798 "sandboxing disabled.";
[email protected]e8c916a2009-11-04 17:52:47799 } else {
[email protected]8e96e502010-10-21 20:57:12800 VLOG(1) << "Enabling experimental Seccomp sandbox.";
[email protected]715b4f262010-07-13 14:17:28801 sandbox_flags |= ZygoteHost::kSandboxSeccomp;
[email protected]e8c916a2009-11-04 17:52:47802 }
803 }
[email protected]36ea6c6f2010-03-17 20:08:01804#endif // SECCOMP_SANDBOX
[email protected]e8c916a2009-11-04 17:52:47805
[email protected]ce2ae442011-07-18 16:13:41806 Zygote zygote(sandbox_flags, forkdelegate);
[email protected]c548be22010-03-08 12:55:57807 // This function call can return multiple times, once per fork().
[email protected]cc8f1462009-06-12 17:36:55808 return zygote.ProcessRequests();
809}