blob: 65a44ae9f4dff65045c2af0a37fb7b57a30d6ac6 [file] [log] [blame]
[email protected]17773042010-07-28 17:35:071// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]cc8f1462009-06-12 17:36:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]a0f200ea2009-08-06 18:44:065#include <dlfcn.h>
[email protected]a9c54a172009-11-07 06:09:386#include <fcntl.h>
[email protected]724df992010-09-21 18:19:107#include <pthread.h>
[email protected]83a0cbe2009-11-04 04:22:478#include <sys/epoll.h>
[email protected]83a0cbe2009-11-04 04:22:479#include <sys/prctl.h>
[email protected]8ecd3aad2009-11-04 08:32:2210#include <sys/signal.h>
11#include <sys/socket.h>
[email protected]a9c54a172009-11-07 06:09:3812#include <sys/stat.h>
[email protected]8ecd3aad2009-11-04 08:32:2213#include <sys/types.h>
[email protected]83a0cbe2009-11-04 04:22:4714#include <sys/wait.h>
[email protected]8ecd3aad2009-11-04 08:32:2215#include <unistd.h>
16
17#if defined(CHROMIUM_SELINUX)
18#include <selinux/selinux.h>
19#include <selinux/context.h>
20#endif
[email protected]cc8f1462009-06-12 17:36:5521
[email protected]789f70c72009-07-24 13:55:4322#include "base/basictypes.h"
[email protected]cc8f1462009-06-12 17:36:5523#include "base/command_line.h"
24#include "base/eintr_wrapper.h"
[email protected]5d91c9e2010-07-28 17:25:2825#include "base/file_path.h"
[email protected]cc8f1462009-06-12 17:36:5526#include "base/global_descriptors_posix.h"
[email protected]8ecd3aad2009-11-04 08:32:2227#include "base/hash_tables.h"
28#include "base/linux_util.h"
[email protected]c9e45da02009-08-05 17:35:0829#include "base/path_service.h"
[email protected]cc8f1462009-06-12 17:36:5530#include "base/pickle.h"
[email protected]4d36536b2010-08-20 06:23:2731#include "base/process_util.h"
[email protected]4378a822009-07-08 01:15:1432#include "base/rand_util.h"
[email protected]1831acf2009-10-05 16:38:4133#include "base/scoped_ptr.h"
[email protected]80a086c52009-08-04 17:52:0434#include "base/sys_info.h"
[email protected]cc8f1462009-06-12 17:36:5535#include "base/unix_domain_socket_posix.h"
[email protected]8015de32009-11-18 04:13:3336#include "build/build_config.h"
[email protected]cc8f1462009-06-12 17:36:5537
38#include "chrome/browser/zygote_host_linux.h"
39#include "chrome/common/chrome_descriptors.h"
[email protected]4730db92009-07-22 00:40:4840#include "chrome/common/chrome_switches.h"
[email protected]cc8f1462009-06-12 17:36:5541#include "chrome/common/main_function_params.h"
[email protected]17773042010-07-28 17:35:0742#include "chrome/common/pepper_plugin_registry.h"
[email protected]cc8f1462009-06-12 17:36:5543#include "chrome/common/process_watcher.h"
[email protected]73fa63992009-07-20 20:30:0744#include "chrome/common/sandbox_methods_linux.h"
[email protected]cc8f1462009-06-12 17:36:5545
[email protected]c9e45da02009-08-05 17:35:0846#include "media/base/media.h"
47
[email protected]abe3ad92009-06-15 18:15:0848#include "skia/ext/SkFontHost_fontconfig_control.h"
49
[email protected]808ea572010-09-01 16:22:0150#include "seccompsandbox/sandbox.h"
[email protected]e8c916a2009-11-04 17:52:4751
[email protected]1831acf2009-10-05 16:38:4152#include "unicode/timezone.h"
53
[email protected]58680ce2010-09-18 00:09:1554#if defined(ARCH_CPU_X86_FAMILY) && !defined(CHROMIUM_SELINUX) && \
55 !defined(__clang__)
[email protected]36ea6c6f2010-03-17 20:08:0156// The seccomp sandbox is enabled on all ia32 and x86-64 processor as long as
[email protected]58680ce2010-09-18 00:09:1557// we aren't using SELinux or clang.
[email protected]36ea6c6f2010-03-17 20:08:0158#define SECCOMP_SANDBOX
59#endif
60
[email protected]cc8f1462009-06-12 17:36:5561// http://code.google.com/p/chromium/wiki/LinuxZygote
62
[email protected]8ecd3aad2009-11-04 08:32:2263static const int kBrowserDescriptor = 3;
[email protected]73fa63992009-07-20 20:30:0764static const int kMagicSandboxIPCDescriptor = 5;
[email protected]8ecd3aad2009-11-04 08:32:2265static const int kZygoteIdDescriptor = 7;
66static bool g_suid_sandbox_active = false;
[email protected]36ea6c6f2010-03-17 20:08:0167#if defined(SECCOMP_SANDBOX)
[email protected]4f03cbc2009-11-19 00:50:4868// |g_proc_fd| is used only by the seccomp sandbox.
[email protected]a9c54a172009-11-07 06:09:3869static int g_proc_fd = -1;
[email protected]4f03cbc2009-11-19 00:50:4870#endif
[email protected]73fa63992009-07-20 20:30:0771
[email protected]a89a55dd2010-04-19 14:51:1372#if defined(CHROMIUM_SELINUX)
73static void SELinuxTransitionToTypeOrDie(const char* type) {
74 security_context_t security_context;
75 if (getcon(&security_context))
76 LOG(FATAL) << "Cannot get SELinux context";
77
78 context_t context = context_new(security_context);
79 context_type_set(context, type);
80 const int r = setcon(context_str(context));
81 context_free(context);
82 freecon(security_context);
83
84 if (r) {
85 LOG(FATAL) << "dynamic transition to type '" << type << "' failed. "
86 "(this binary has been built with SELinux support, but maybe "
87 "the policies haven't been loaded into the kernel?)";
88 }
89}
90#endif // CHROMIUM_SELINUX
91
[email protected]cc8f1462009-06-12 17:36:5592// This is the object which implements the zygote. The ZygoteMain function,
[email protected]61883442010-07-12 15:14:3493// which is called from ChromeMain, simply constructs one of these objects and
94// runs it.
[email protected]cc8f1462009-06-12 17:36:5595class Zygote {
96 public:
[email protected]715b4f262010-07-13 14:17:2897 explicit Zygote(int sandbox_flags)
98 : sandbox_flags_(sandbox_flags) {
99 }
100
[email protected]cc8f1462009-06-12 17:36:55101 bool ProcessRequests() {
102 // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the
103 // browser on it.
[email protected]8ecd3aad2009-11-04 08:32:22104 // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel.
[email protected]abe3ad92009-06-15 18:15:08105 // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC
[email protected]cc8f1462009-06-12 17:36:55106
107 // We need to accept SIGCHLD, even though our handler is a no-op because
108 // otherwise we cannot wait on children. (According to POSIX 2001.)
109 struct sigaction action;
110 memset(&action, 0, sizeof(action));
111 action.sa_handler = SIGCHLDHandler;
112 CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
113
[email protected]8ecd3aad2009-11-04 08:32:22114 if (g_suid_sandbox_active) {
115 // Let the ZygoteHost know we are ready to go.
116 // The receiving code is in chrome/browser/zygote_host_linux.cc.
117 std::vector<int> empty;
118 bool r = base::SendMsg(kBrowserDescriptor, kZygoteMagic,
119 sizeof(kZygoteMagic), empty);
120 CHECK(r) << "Sending zygote magic failed";
121 }
122
[email protected]cc8f1462009-06-12 17:36:55123 for (;;) {
[email protected]c548be22010-03-08 12:55:57124 // This function call can return multiple times, once per fork().
[email protected]8ecd3aad2009-11-04 08:32:22125 if (HandleRequestFromBrowser(kBrowserDescriptor))
[email protected]cc8f1462009-06-12 17:36:55126 return true;
127 }
128 }
129
130 private:
131 // See comment below, where sigaction is called.
132 static void SIGCHLDHandler(int signal) { }
133
134 // ---------------------------------------------------------------------------
135 // Requests from the browser...
136
137 // Read and process a request from the browser. Returns true if we are in a
138 // new process and thus need to unwind back into ChromeMain.
139 bool HandleRequestFromBrowser(int fd) {
140 std::vector<int> fds;
[email protected]abe3ad92009-06-15 18:15:08141 static const unsigned kMaxMessageLength = 1024;
[email protected]cc8f1462009-06-12 17:36:55142 char buf[kMaxMessageLength];
143 const ssize_t len = base::RecvMsg(fd, buf, sizeof(buf), &fds);
[email protected]7ae27b92010-09-24 17:33:36144
145 if (len == 0 || (len == -1 && errno == ECONNRESET)) {
146 // EOF from the browser. We should die.
147 _exit(0);
[email protected]cc8f1462009-06-12 17:36:55148 return false;
149 }
150
[email protected]7ae27b92010-09-24 17:33:36151 if (len == -1) {
152 PLOG(ERROR) << "Error reading message from browser";
[email protected]cc8f1462009-06-12 17:36:55153 return false;
154 }
155
156 Pickle pickle(buf, len);
157 void* iter = NULL;
158
159 int kind;
[email protected]57113ea2009-06-18 02:23:16160 if (pickle.ReadInt(&iter, &kind)) {
161 switch (kind) {
162 case ZygoteHost::kCmdFork:
[email protected]c548be22010-03-08 12:55:57163 // This function call can return multiple times, once per fork().
[email protected]57113ea2009-06-18 02:23:16164 return HandleForkRequest(fd, pickle, iter, fds);
165 case ZygoteHost::kCmdReap:
166 if (!fds.empty())
167 break;
[email protected]c548be22010-03-08 12:55:57168 HandleReapRequest(fd, pickle, iter);
169 return false;
[email protected]57113ea2009-06-18 02:23:16170 case ZygoteHost::kCmdDidProcessCrash:
171 if (!fds.empty())
172 break;
[email protected]c548be22010-03-08 12:55:57173 HandleDidProcessCrash(fd, pickle, iter);
174 return false;
[email protected]715b4f262010-07-13 14:17:28175 case ZygoteHost::kCmdGetSandboxStatus:
176 HandleGetSandboxStatus(fd, pickle, iter);
177 return false;
[email protected]57113ea2009-06-18 02:23:16178 default:
179 NOTREACHED();
180 break;
181 }
[email protected]cc8f1462009-06-12 17:36:55182 }
183
[email protected]cc8f1462009-06-12 17:36:55184 LOG(WARNING) << "Error parsing message from browser";
185 for (std::vector<int>::const_iterator
186 i = fds.begin(); i != fds.end(); ++i)
187 close(*i);
188 return false;
189 }
190
[email protected]c548be22010-03-08 12:55:57191 void HandleReapRequest(int fd, const Pickle& pickle, void* iter) {
[email protected]8ecd3aad2009-11-04 08:32:22192 base::ProcessId child;
193 base::ProcessId actual_child;
[email protected]cc8f1462009-06-12 17:36:55194
195 if (!pickle.ReadInt(&iter, &child)) {
196 LOG(WARNING) << "Error parsing reap request from browser";
[email protected]c548be22010-03-08 12:55:57197 return;
[email protected]cc8f1462009-06-12 17:36:55198 }
199
[email protected]8ecd3aad2009-11-04 08:32:22200 if (g_suid_sandbox_active) {
201 actual_child = real_pids_to_sandbox_pids[child];
202 if (!actual_child)
[email protected]c548be22010-03-08 12:55:57203 return;
[email protected]8ecd3aad2009-11-04 08:32:22204 real_pids_to_sandbox_pids.erase(child);
205 } else {
206 actual_child = child;
207 }
208
209 ProcessWatcher::EnsureProcessTerminated(actual_child);
[email protected]cc8f1462009-06-12 17:36:55210 }
211
[email protected]c548be22010-03-08 12:55:57212 void HandleDidProcessCrash(int fd, const Pickle& pickle, void* iter) {
[email protected]57113ea2009-06-18 02:23:16213 base::ProcessHandle child;
214
215 if (!pickle.ReadInt(&iter, &child)) {
216 LOG(WARNING) << "Error parsing DidProcessCrash request from browser";
[email protected]c548be22010-03-08 12:55:57217 return;
[email protected]57113ea2009-06-18 02:23:16218 }
219
220 bool child_exited;
[email protected]8ecd3aad2009-11-04 08:32:22221 bool did_crash;
222 if (g_suid_sandbox_active)
223 child = real_pids_to_sandbox_pids[child];
224 if (child)
225 did_crash = base::DidProcessCrash(&child_exited, child);
226 else
227 did_crash = child_exited = false;
[email protected]57113ea2009-06-18 02:23:16228
229 Pickle write_pickle;
230 write_pickle.WriteBool(did_crash);
231 write_pickle.WriteBool(child_exited);
[email protected]4b809bc2010-04-19 16:12:05232 if (HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size())) !=
233 write_pickle.size()) {
[email protected]19cb9292010-04-16 23:00:15234 PLOG(ERROR) << "write";
[email protected]4b809bc2010-04-19 16:12:05235 }
[email protected]57113ea2009-06-18 02:23:16236 }
237
[email protected]28ecba32010-09-16 10:03:09238 // This is equivalent to fork(), except that, when using the SUID
239 // sandbox, it returns the real PID of the child process as it
240 // appears outside the sandbox, rather than returning the PID inside
241 // the sandbox.
242 int ForkWithRealPid() {
243 if (!g_suid_sandbox_active)
244 return fork();
245
246 int dummy_fd;
247 ino_t dummy_inode;
248 int pipe_fds[2] = { -1, -1 };
[email protected]073040b2010-09-26 02:35:45249 base::ProcessId pid = 0;
[email protected]28ecba32010-09-16 10:03:09250
251 dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
252 if (dummy_fd < 0) {
253 LOG(ERROR) << "Failed to create dummy FD";
254 goto error;
255 }
256 if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd)) {
257 LOG(ERROR) << "Failed to get inode for dummy FD";
258 goto error;
259 }
260 if (pipe(pipe_fds) != 0) {
261 LOG(ERROR) << "Failed to create pipe";
262 goto error;
263 }
264
265 pid = fork();
266 if (pid < 0) {
267 goto error;
268 } else if (pid == 0) {
269 // In the child process.
270 close(pipe_fds[1]);
271 char buffer[1];
272 // Wait until the parent process has discovered our PID. We
273 // should not fork any child processes (which the seccomp
274 // sandbox does) until then, because that can interfere with the
275 // parent's discovery of our PID.
276 if (HANDLE_EINTR(read(pipe_fds[0], buffer, 1)) != 1 ||
277 buffer[0] != 'x') {
278 LOG(FATAL) << "Failed to synchronise with parent zygote process";
279 }
280 close(pipe_fds[0]);
281 close(dummy_fd);
282 return 0;
283 } else {
284 // In the parent process.
285 close(dummy_fd);
286 dummy_fd = -1;
287 close(pipe_fds[0]);
288 pipe_fds[0] = -1;
289 uint8_t reply_buf[512];
290 Pickle request;
291 request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
292 request.WriteUInt64(dummy_inode);
293
294 const ssize_t r = base::SendRecvMsg(kMagicSandboxIPCDescriptor,
295 reply_buf, sizeof(reply_buf),
296 NULL, request);
297 if (r == -1) {
298 LOG(ERROR) << "Failed to get child process's real PID";
299 goto error;
300 }
301
302 base::ProcessId real_pid;
303 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
304 void* iter2 = NULL;
305 if (!reply.ReadInt(&iter2, &real_pid))
306 goto error;
[email protected]073040b2010-09-26 02:35:45307 if (real_pid <= 0) {
308 // METHOD_GET_CHILD_WITH_INODE failed. Did the child die already?
309 LOG(ERROR) << "METHOD_GET_CHILD_WITH_INODE failed";
310 goto error;
311 }
[email protected]28ecba32010-09-16 10:03:09312 real_pids_to_sandbox_pids[real_pid] = pid;
313 if (HANDLE_EINTR(write(pipe_fds[1], "x", 1)) != 1) {
314 LOG(ERROR) << "Failed to synchronise with child process";
315 goto error;
316 }
317 close(pipe_fds[1]);
318 return real_pid;
319 }
320
321 error:
[email protected]073040b2010-09-26 02:35:45322 if (pid > 0)
323 waitpid(pid, NULL, WNOHANG);
[email protected]28ecba32010-09-16 10:03:09324 if (dummy_fd >= 0)
325 close(dummy_fd);
326 if (pipe_fds[0] >= 0)
327 close(pipe_fds[0]);
328 if (pipe_fds[1] >= 0)
329 close(pipe_fds[1]);
330 return -1;
331 }
332
[email protected]cc8f1462009-06-12 17:36:55333 // Handle a 'fork' request from the browser: this means that the browser
334 // wishes to start a new renderer.
[email protected]8ecd3aad2009-11-04 08:32:22335 bool HandleForkRequest(int fd, const Pickle& pickle, void* iter,
[email protected]cc8f1462009-06-12 17:36:55336 std::vector<int>& fds) {
337 std::vector<std::string> args;
338 int argc, numfds;
339 base::GlobalDescriptors::Mapping mapping;
[email protected]8ecd3aad2009-11-04 08:32:22340 base::ProcessId child;
[email protected]cc8f1462009-06-12 17:36:55341
342 if (!pickle.ReadInt(&iter, &argc))
343 goto error;
344
345 for (int i = 0; i < argc; ++i) {
346 std::string arg;
347 if (!pickle.ReadString(&iter, &arg))
348 goto error;
349 args.push_back(arg);
350 }
351
352 if (!pickle.ReadInt(&iter, &numfds))
353 goto error;
354 if (numfds != static_cast<int>(fds.size()))
355 goto error;
356
357 for (int i = 0; i < numfds; ++i) {
358 base::GlobalDescriptors::Key key;
359 if (!pickle.ReadUInt32(&iter, &key))
360 goto error;
361 mapping.push_back(std::make_pair(key, fds[i]));
362 }
363
[email protected]abe3ad92009-06-15 18:15:08364 mapping.push_back(std::make_pair(
[email protected]8ecd3aad2009-11-04 08:32:22365 static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor));
366
[email protected]28ecba32010-09-16 10:03:09367 child = ForkWithRealPid();
[email protected]cc8f1462009-06-12 17:36:55368
369 if (!child) {
[email protected]36ea6c6f2010-03-17 20:08:01370#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38371 // Try to open /proc/self/maps as the seccomp sandbox needs access to it
372 if (g_proc_fd >= 0) {
373 int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY);
374 if (proc_self_maps >= 0) {
375 SeccompSandboxSetProcSelfMaps(proc_self_maps);
376 }
377 close(g_proc_fd);
378 g_proc_fd = -1;
379 }
[email protected]8015de32009-11-18 04:13:33380#endif
[email protected]a9c54a172009-11-07 06:09:38381
[email protected]8ecd3aad2009-11-04 08:32:22382 close(kBrowserDescriptor); // our socket from the browser
[email protected]cbcf9cc32010-02-17 04:05:59383 if (g_suid_sandbox_active)
384 close(kZygoteIdDescriptor); // another socket from the browser
[email protected]cc8f1462009-06-12 17:36:55385 Singleton<base::GlobalDescriptors>()->Reset(mapping);
[email protected]0189bbd2009-10-12 22:50:39386
[email protected]a89a55dd2010-04-19 14:51:13387#if defined(CHROMIUM_SELINUX)
388 SELinuxTransitionToTypeOrDie("chromium_renderer_t");
389#endif
390
[email protected]0189bbd2009-10-12 22:50:39391 // Reset the process-wide command line to our new command line.
[email protected]cc8f1462009-06-12 17:36:55392 CommandLine::Reset();
[email protected]0189bbd2009-10-12 22:50:39393 CommandLine::Init(0, NULL);
394 CommandLine::ForCurrentProcess()->InitFromArgv(args);
[email protected]7f113f32009-09-10 18:02:17395 CommandLine::SetProcTitle();
[email protected]c548be22010-03-08 12:55:57396 // The fork() request is handled further up the call stack.
[email protected]cc8f1462009-06-12 17:36:55397 return true;
[email protected]8ecd3aad2009-11-04 08:32:22398 } else if (child < 0) {
[email protected]466cffd2010-06-09 18:10:56399 LOG(ERROR) << "Zygote could not fork: " << errno;
[email protected]8ecd3aad2009-11-04 08:32:22400 goto error;
[email protected]cc8f1462009-06-12 17:36:55401 }
402
[email protected]28ecba32010-09-16 10:03:09403 for (std::vector<int>::const_iterator
404 i = fds.begin(); i != fds.end(); ++i)
405 close(*i);
[email protected]83a0cbe2009-11-04 04:22:47406
[email protected]28ecba32010-09-16 10:03:09407 if (HANDLE_EINTR(write(fd, &child, sizeof(child))) < 0)
408 PLOG(ERROR) << "write";
409 return false;
[email protected]83a0cbe2009-11-04 04:22:47410
411 error:
[email protected]8ecd3aad2009-11-04 08:32:22412 LOG(ERROR) << "Error parsing fork request from browser";
[email protected]83a0cbe2009-11-04 04:22:47413 for (std::vector<int>::const_iterator
414 i = fds.begin(); i != fds.end(); ++i)
415 close(*i);
[email protected]cc8f1462009-06-12 17:36:55416 return false;
417 }
[email protected]8ecd3aad2009-11-04 08:32:22418
[email protected]715b4f262010-07-13 14:17:28419 bool HandleGetSandboxStatus(int fd, const Pickle& pickle, void* iter) {
420 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_)) !=
421 sizeof(sandbox_flags_))) {
422 PLOG(ERROR) << "write";
423 }
424
425 return false;
426 }
427
[email protected]8ecd3aad2009-11-04 08:32:22428 // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs
429 // fork() returns are not the real PIDs, so we need to map the Real PIDS
430 // into the sandbox PID namespace.
431 typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap;
432 ProcessMap real_pids_to_sandbox_pids;
[email protected]715b4f262010-07-13 14:17:28433
434 const int sandbox_flags_;
[email protected]cc8f1462009-06-12 17:36:55435};
436
[email protected]ad6d2c42009-09-15 20:13:38437// With SELinux we can carve out a precise sandbox, so we don't have to play
438// with intercepting libc calls.
439#if !defined(CHROMIUM_SELINUX)
440
[email protected]a0f200ea2009-08-06 18:44:06441static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output,
442 char* timezone_out,
443 size_t timezone_out_len) {
[email protected]73fa63992009-07-20 20:30:07444 Pickle request;
445 request.WriteInt(LinuxSandbox::METHOD_LOCALTIME);
446 request.WriteString(
447 std::string(reinterpret_cast<char*>(&input), sizeof(input)));
448
449 uint8_t reply_buf[512];
450 const ssize_t r = base::SendRecvMsg(
451 kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request);
452 if (r == -1) {
453 memset(output, 0, sizeof(struct tm));
454 return;
455 }
456
457 Pickle reply(reinterpret_cast<char*>(reply_buf), r);
458 void* iter = NULL;
[email protected]9debcda52009-07-22 20:06:51459 std::string result, timezone;
[email protected]73fa63992009-07-20 20:30:07460 if (!reply.ReadString(&iter, &result) ||
[email protected]9debcda52009-07-22 20:06:51461 !reply.ReadString(&iter, &timezone) ||
[email protected]73fa63992009-07-20 20:30:07462 result.size() != sizeof(struct tm)) {
463 memset(output, 0, sizeof(struct tm));
464 return;
465 }
466
467 memcpy(output, result.data(), sizeof(struct tm));
[email protected]9debcda52009-07-22 20:06:51468 if (timezone_out_len) {
469 const size_t copy_len = std::min(timezone_out_len - 1, timezone.size());
470 memcpy(timezone_out, timezone.data(), copy_len);
471 timezone_out[copy_len] = 0;
472 output->tm_zone = timezone_out;
473 } else {
474 output->tm_zone = NULL;
475 }
[email protected]73fa63992009-07-20 20:30:07476}
477
[email protected]a0f200ea2009-08-06 18:44:06478static bool g_am_zygote_or_renderer = false;
479
480// Sandbox interception of libc calls.
481//
482// Because we are running in a sandbox certain libc calls will fail (localtime
483// being the motivating example - it needs to read /etc/localtime). We need to
484// intercept these calls and proxy them to the browser. However, these calls
485// may come from us or from our libraries. In some cases we can't just change
486// our code.
487//
488// It's for these cases that we have the following setup:
489//
490// We define global functions for those functions which we wish to override.
491// Since we will be first in the dynamic resolution order, the dynamic linker
492// will point callers to our versions of these functions. However, we have the
493// same binary for both the browser and the renderers, which means that our
494// overrides will apply in the browser too.
495//
496// The global |g_am_zygote_or_renderer| is true iff we are in a zygote or
497// renderer process. It's set in ZygoteMain and inherited by the renderers when
498// they fork. (This means that it'll be incorrect for global constructor
499// functions and before ZygoteMain is called - beware).
500//
501// Our replacement functions can check this global and either proxy
502// the call to the browser over the sandbox IPC
503// (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use
504// dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the
505// current module.
506//
507// Other avenues:
508//
509// Our first attempt involved some assembly to patch the GOT of the current
510// module. This worked, but was platform specific and doesn't catch the case
511// where a library makes a call rather than current module.
512//
513// We also considered patching the function in place, but this would again by
514// platform specific and the above technique seems to work well enough.
515
[email protected]724df992010-09-21 18:19:10516typedef struct tm* (*LocaltimeFunction)(const time_t* timep);
517typedef struct tm* (*LocaltimeRFunction)(const time_t* timep,
518 struct tm* result);
519
520static pthread_once_t g_libc_localtime_funcs_guard = PTHREAD_ONCE_INIT;
521static LocaltimeFunction g_libc_localtime;
522static LocaltimeRFunction g_libc_localtime_r;
523
524static void InitLibcLocaltimeFunctions() {
525 g_libc_localtime = reinterpret_cast<LocaltimeFunction>(
526 dlsym(RTLD_NEXT, "localtime"));
527 g_libc_localtime_r = reinterpret_cast<LocaltimeRFunction>(
528 dlsym(RTLD_NEXT, "localtime_r"));
529
530 if (!g_libc_localtime || !g_libc_localtime_r) {
531 // http://code.google.com/p/chromium/issues/detail?id=16800
532 //
533 // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces
534 // it with a version which doesn't work. In this case we'll get a NULL
535 // result. There's not a lot we can do at this point, so we just bodge it!
536 LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been "
537 "reported to be caused by Nvidia's libGL. You should expect"
538 " time related functions to misbehave. "
539 "http://code.google.com/p/chromium/issues/detail?id=16800";
540 }
541
542 if (!g_libc_localtime)
543 g_libc_localtime = gmtime;
544 if (!g_libc_localtime_r)
545 g_libc_localtime_r = gmtime_r;
546}
[email protected]a5d7bb82009-09-08 22:30:58547
[email protected]73fa63992009-07-20 20:30:07548struct tm* localtime(const time_t* timep) {
[email protected]a0f200ea2009-08-06 18:44:06549 if (g_am_zygote_or_renderer) {
550 static struct tm time_struct;
551 static char timezone_string[64];
552 ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string,
553 sizeof(timezone_string));
554 return &time_struct;
555 } else {
[email protected]724df992010-09-21 18:19:10556 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
557 InitLibcLocaltimeFunctions));
558 return g_libc_localtime(timep);
[email protected]a0f200ea2009-08-06 18:44:06559 }
[email protected]73fa63992009-07-20 20:30:07560}
561
562struct tm* localtime_r(const time_t* timep, struct tm* result) {
[email protected]a0f200ea2009-08-06 18:44:06563 if (g_am_zygote_or_renderer) {
564 ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0);
565 return result;
566 } else {
[email protected]724df992010-09-21 18:19:10567 CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard,
568 InitLibcLocaltimeFunctions));
569 return g_libc_localtime_r(timep, result);
[email protected]a0f200ea2009-08-06 18:44:06570 }
[email protected]73fa63992009-07-20 20:30:07571}
572
[email protected]ad6d2c42009-09-15 20:13:38573#endif // !CHROMIUM_SELINUX
[email protected]a5d7bb82009-09-08 22:30:58574
[email protected]ad6d2c42009-09-15 20:13:38575// This function triggers the static and lazy construction of objects that need
576// to be created before imposing the sandbox.
577static void PreSandboxInit() {
[email protected]b8419412010-03-29 20:18:29578 base::RandUint64();
[email protected]4378a822009-07-08 01:15:14579
[email protected]b8419412010-03-29 20:18:29580 base::SysInfo::MaxSharedMemorySize();
[email protected]80a086c52009-08-04 17:52:04581
[email protected]b8419412010-03-29 20:18:29582 // To make wcstombs/mbstowcs work in a renderer, setlocale() has to be
583 // called before the sandbox is triggered. It's possible to avoid calling
584 // setlocale() by pulling out the conversion between FilePath and
585 // WebCore String out of the renderer and using string16 in place of
586 // FilePath for IPC.
587 const char* locale = setlocale(LC_ALL, "");
588 LOG_IF(WARNING, locale == NULL) << "setlocale failed.";
[email protected]e6acd672009-07-24 21:51:33589
[email protected]b8419412010-03-29 20:18:29590 // ICU DateFormat class (used in base/time_format.cc) needs to get the
591 // Olson timezone ID by accessing the zoneinfo files on disk. After
592 // TimeZone::createDefault is called once here, the timezone ID is
593 // cached and there's no more need to access the file system.
594 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
[email protected]1831acf2009-10-05 16:38:41595
[email protected]b8419412010-03-29 20:18:29596 FilePath module_path;
597 if (PathService::Get(base::DIR_MODULE, &module_path))
598 media::InitializeMediaLibrary(module_path);
[email protected]17773042010-07-28 17:35:07599
600 // Ensure access to the Pepper plugins before the sandbox is turned on.
601 PepperPluginRegistry::PreloadModules();
[email protected]ad6d2c42009-09-15 20:13:38602}
603
604#if !defined(CHROMIUM_SELINUX)
605static bool EnterSandbox() {
[email protected]b8419412010-03-29 20:18:29606 // The SUID sandbox sets this environment variable to a file descriptor
607 // over which we can signal that we have completed our startup and can be
608 // chrooted.
[email protected]ad6d2c42009-09-15 20:13:38609 const char* const sandbox_fd_string = getenv("SBX_D");
[email protected]ad6d2c42009-09-15 20:13:38610
[email protected]0dc32322010-09-16 09:46:59611 if (sandbox_fd_string) {
612 // Use the SUID sandbox. This still allows the seccomp sandbox to
613 // be enabled by the process later.
[email protected]8ecd3aad2009-11-04 08:32:22614 g_suid_sandbox_active = true;
615
[email protected]ad6d2c42009-09-15 20:13:38616 char* endptr;
617 const long fd_long = strtol(sandbox_fd_string, &endptr, 10);
618 if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX)
619 return false;
620 const int fd = fd_long;
621
622 PreSandboxInit();
[email protected]c9e45da02009-08-05 17:35:08623
[email protected]eaebefaf2010-02-23 22:58:18624 static const char kMsgChrootMe = 'C';
625 static const char kMsgChrootSuccessful = 'O';
[email protected]abe3ad92009-06-15 18:15:08626
[email protected]eaebefaf2010-02-23 22:58:18627 if (HANDLE_EINTR(write(fd, &kMsgChrootMe, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32628 LOG(ERROR) << "Failed to write to chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08629 return false;
[email protected]0e1611122009-07-10 18:17:32630 }
[email protected]abe3ad92009-06-15 18:15:08631
[email protected]87ed6952009-07-16 02:52:15632 // We need to reap the chroot helper process in any event:
633 wait(NULL);
634
[email protected]abe3ad92009-06-15 18:15:08635 char reply;
[email protected]87f8ce62009-07-10 19:14:31636 if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) {
[email protected]0e1611122009-07-10 18:17:32637 LOG(ERROR) << "Failed to read from chroot pipe: " << errno;
[email protected]abe3ad92009-06-15 18:15:08638 return false;
[email protected]0e1611122009-07-10 18:17:32639 }
[email protected]87f8ce62009-07-10 19:14:31640
[email protected]eaebefaf2010-02-23 22:58:18641 if (reply != kMsgChrootSuccessful) {
[email protected]0e1611122009-07-10 18:17:32642 LOG(ERROR) << "Error code reply from chroot helper";
[email protected]abe3ad92009-06-15 18:15:08643 return false;
[email protected]0e1611122009-07-10 18:17:32644 }
[email protected]abe3ad92009-06-15 18:15:08645
[email protected]abe3ad92009-06-15 18:15:08646 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
647
[email protected]415493be2009-07-10 17:50:24648 // Previously, we required that the binary be non-readable. This causes the
649 // kernel to mark the process as non-dumpable at startup. The thinking was
650 // that, although we were putting the renderers into a PID namespace (with
651 // the SUID sandbox), they would nonetheless be in the /same/ PID
652 // namespace. So they could ptrace each other unless they were non-dumpable.
653 //
654 // If the binary was readable, then there would be a window between process
655 // startup and the point where we set the non-dumpable flag in which a
656 // compromised renderer could ptrace attach.
657 //
658 // However, now that we have a zygote model, only the (trusted) zygote
659 // exists at this point and we can set the non-dumpable flag which is
660 // inherited by all our renderer children.
[email protected]4730db92009-07-22 00:40:48661 //
662 // Note: a non-dumpable process can't be debugged. To debug sandbox-related
663 // issues, one can specify --allow-sandbox-debugging to let the process be
664 // dumpable.
665 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
666 if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) {
667 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
668 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
669 LOG(ERROR) << "Failed to set non-dumpable flag";
670 return false;
671 }
[email protected]0e1611122009-07-10 18:17:32672 }
[email protected]0dc32322010-09-16 09:46:59673 } else if (switches::SeccompSandboxEnabled()) {
674 PreSandboxInit();
675 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
[email protected]abe3ad92009-06-15 18:15:08676 } else {
677 SkiaFontConfigUseDirectImplementation();
678 }
679
680 return true;
681}
[email protected]ad6d2c42009-09-15 20:13:38682#else // CHROMIUM_SELINUX
683
684static bool EnterSandbox() {
685 PreSandboxInit();
686 SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor);
[email protected]ad6d2c42009-09-15 20:13:38687 return true;
688}
689
690#endif // CHROMIUM_SELINUX
[email protected]abe3ad92009-06-15 18:15:08691
[email protected]cc8f1462009-06-12 17:36:55692bool ZygoteMain(const MainFunctionParams& params) {
[email protected]ad6d2c42009-09-15 20:13:38693#if !defined(CHROMIUM_SELINUX)
[email protected]a0f200ea2009-08-06 18:44:06694 g_am_zygote_or_renderer = true;
[email protected]ad6d2c42009-09-15 20:13:38695#endif
[email protected]a0f200ea2009-08-06 18:44:06696
[email protected]36ea6c6f2010-03-17 20:08:01697#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38698 // The seccomp sandbox needs access to files in /proc, which might be denied
699 // after one of the other sandboxes have been started. So, obtain a suitable
700 // file handle in advance.
[email protected]39c4e1a82010-03-30 19:47:41701 if (switches::SeccompSandboxEnabled()) {
[email protected]a9c54a172009-11-07 06:09:38702 g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY);
703 if (g_proc_fd < 0) {
704 LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp "
705 "sandboxing.";
706 }
707 }
[email protected]36ea6c6f2010-03-17 20:08:01708#endif // SECCOMP_SANDBOX
[email protected]a9c54a172009-11-07 06:09:38709
710 // Turn on the SELinux or SUID sandbox
711 if (!EnterSandbox()) {
712 LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: "
713 << errno << ")";
714 return false;
715 }
716
[email protected]715b4f262010-07-13 14:17:28717 int sandbox_flags = 0;
718 if (getenv("SBX_D"))
719 sandbox_flags |= ZygoteHost::kSandboxSUID;
720 if (getenv("SBX_PID_NS"))
721 sandbox_flags |= ZygoteHost::kSandboxPIDNS;
722 if (getenv("SBX_NET_NS"))
723 sandbox_flags |= ZygoteHost::kSandboxNetNS;
724
[email protected]36ea6c6f2010-03-17 20:08:01725#if defined(SECCOMP_SANDBOX)
[email protected]a9c54a172009-11-07 06:09:38726 // The seccomp sandbox will be turned on when the renderers start. But we can
727 // already check if sufficient support is available so that we only need to
728 // print one error message for the entire browser session.
[email protected]39c4e1a82010-03-30 19:47:41729 if (g_proc_fd >= 0 && switches::SeccompSandboxEnabled()) {
[email protected]a9c54a172009-11-07 06:09:38730 if (!SupportsSeccompSandbox(g_proc_fd)) {
[email protected]e8c916a2009-11-04 17:52:47731 // There are a good number of users who cannot use the seccomp sandbox
732 // (e.g. because their distribution does not enable seccomp mode by
733 // default). While we would prefer to deny execution in this case, it
734 // seems more realistic to continue in degraded mode.
[email protected]1b5d28f2010-02-18 15:53:36735 LOG(ERROR) << "WARNING! This machine lacks support needed for the "
736 "Seccomp sandbox. Running renderers with Seccomp "
737 "sandboxing disabled.";
[email protected]e8c916a2009-11-04 17:52:47738 } else {
739 LOG(INFO) << "Enabling experimental Seccomp sandbox.";
[email protected]715b4f262010-07-13 14:17:28740 sandbox_flags |= ZygoteHost::kSandboxSeccomp;
[email protected]e8c916a2009-11-04 17:52:47741 }
742 }
[email protected]36ea6c6f2010-03-17 20:08:01743#endif // SECCOMP_SANDBOX
[email protected]e8c916a2009-11-04 17:52:47744
[email protected]715b4f262010-07-13 14:17:28745 Zygote zygote(sandbox_flags);
[email protected]c548be22010-03-08 12:55:57746 // This function call can return multiple times, once per fork().
[email protected]cc8f1462009-06-12 17:36:55747 return zygote.ProcessRequests();
748}