[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 5 | #include <dlfcn.h> |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 6 | #include <fcntl.h> |
[email protected] | 83a0cbe | 2009-11-04 04:22:47 | [diff] [blame] | 7 | #include <sys/epoll.h> |
[email protected] | 83a0cbe | 2009-11-04 04:22:47 | [diff] [blame] | 8 | #include <sys/prctl.h> |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 9 | #include <sys/signal.h> |
| 10 | #include <sys/socket.h> |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 11 | #include <sys/stat.h> |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 12 | #include <sys/types.h> |
[email protected] | 83a0cbe | 2009-11-04 04:22:47 | [diff] [blame] | 13 | #include <sys/wait.h> |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 14 | #include <unistd.h> |
| 15 | |
| 16 | #if defined(CHROMIUM_SELINUX) |
| 17 | #include <selinux/selinux.h> |
| 18 | #include <selinux/context.h> |
| 19 | #endif |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 20 | |
[email protected] | 789f70c7 | 2009-07-24 13:55:43 | [diff] [blame] | 21 | #include "base/basictypes.h" |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 22 | #include "base/command_line.h" |
| 23 | #include "base/eintr_wrapper.h" |
| 24 | #include "base/global_descriptors_posix.h" |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 25 | #include "base/hash_tables.h" |
| 26 | #include "base/linux_util.h" |
[email protected] | c9e45da0 | 2009-08-05 17:35:08 | [diff] [blame] | 27 | #include "base/path_service.h" |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 28 | #include "base/pickle.h" |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 29 | #include "base/rand_util.h" |
[email protected] | 1831acf | 2009-10-05 16:38:41 | [diff] [blame] | 30 | #include "base/scoped_ptr.h" |
[email protected] | 80a086c5 | 2009-08-04 17:52:04 | [diff] [blame] | 31 | #include "base/sys_info.h" |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 32 | #include "base/unix_domain_socket_posix.h" |
[email protected] | 8015de3 | 2009-11-18 04:13:33 | [diff] [blame] | 33 | #include "build/build_config.h" |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 34 | |
| 35 | #include "chrome/browser/zygote_host_linux.h" |
| 36 | #include "chrome/common/chrome_descriptors.h" |
[email protected] | 4730db9 | 2009-07-22 00:40:48 | [diff] [blame] | 37 | #include "chrome/common/chrome_switches.h" |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 38 | #include "chrome/common/main_function_params.h" |
| 39 | #include "chrome/common/process_watcher.h" |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 40 | #include "chrome/common/sandbox_methods_linux.h" |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 41 | |
[email protected] | c9e45da0 | 2009-08-05 17:35:08 | [diff] [blame] | 42 | #include "media/base/media.h" |
| 43 | |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 44 | #include "skia/ext/SkFontHost_fontconfig_control.h" |
| 45 | |
[email protected] | e8c916a | 2009-11-04 17:52:47 | [diff] [blame] | 46 | #include "sandbox/linux/seccomp/sandbox.h" |
| 47 | |
[email protected] | 1831acf | 2009-10-05 16:38:41 | [diff] [blame] | 48 | #include "unicode/timezone.h" |
| 49 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 50 | // http://code.google.com/p/chromium/wiki/LinuxZygote |
| 51 | |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 52 | static const int kBrowserDescriptor = 3; |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 53 | static const int kMagicSandboxIPCDescriptor = 5; |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 54 | static const int kZygoteIdDescriptor = 7; |
| 55 | static bool g_suid_sandbox_active = false; |
[email protected] | 4f03cbc | 2009-11-19 00:50:48 | [diff] [blame] | 56 | #if defined(ARCH_CPU_X86_FAMILY) |
| 57 | // |g_proc_fd| is used only by the seccomp sandbox. |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 58 | static int g_proc_fd = -1; |
[email protected] | 4f03cbc | 2009-11-19 00:50:48 | [diff] [blame] | 59 | #endif |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 60 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 61 | // This is the object which implements the zygote. The ZygoteMain function, |
| 62 | // which is called from ChromeMain, at the the bottom and simple constructs one |
| 63 | // of these objects and runs it. |
| 64 | class Zygote { |
| 65 | public: |
| 66 | bool ProcessRequests() { |
| 67 | // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the |
| 68 | // browser on it. |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 69 | // A SOCK_DGRAM is installed in fd 5. This is the sandbox IPC channel. |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 70 | // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 71 | |
| 72 | // We need to accept SIGCHLD, even though our handler is a no-op because |
| 73 | // otherwise we cannot wait on children. (According to POSIX 2001.) |
| 74 | struct sigaction action; |
| 75 | memset(&action, 0, sizeof(action)); |
| 76 | action.sa_handler = SIGCHLDHandler; |
| 77 | CHECK(sigaction(SIGCHLD, &action, NULL) == 0); |
| 78 | |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 79 | if (g_suid_sandbox_active) { |
| 80 | // Let the ZygoteHost know we are ready to go. |
| 81 | // The receiving code is in chrome/browser/zygote_host_linux.cc. |
| 82 | std::vector<int> empty; |
| 83 | bool r = base::SendMsg(kBrowserDescriptor, kZygoteMagic, |
| 84 | sizeof(kZygoteMagic), empty); |
| 85 | CHECK(r) << "Sending zygote magic failed"; |
| 86 | } |
| 87 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 88 | for (;;) { |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 89 | if (HandleRequestFromBrowser(kBrowserDescriptor)) |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 90 | return true; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private: |
| 95 | // See comment below, where sigaction is called. |
| 96 | static void SIGCHLDHandler(int signal) { } |
| 97 | |
| 98 | // --------------------------------------------------------------------------- |
| 99 | // Requests from the browser... |
| 100 | |
| 101 | // Read and process a request from the browser. Returns true if we are in a |
| 102 | // new process and thus need to unwind back into ChromeMain. |
| 103 | bool HandleRequestFromBrowser(int fd) { |
| 104 | std::vector<int> fds; |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 105 | static const unsigned kMaxMessageLength = 1024; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 106 | char buf[kMaxMessageLength]; |
| 107 | const ssize_t len = base::RecvMsg(fd, buf, sizeof(buf), &fds); |
| 108 | if (len == -1) { |
| 109 | LOG(WARNING) << "Error reading message from browser: " << errno; |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | if (len == 0) { |
| 114 | // EOF from the browser. We should die. |
| 115 | _exit(0); |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | Pickle pickle(buf, len); |
| 120 | void* iter = NULL; |
| 121 | |
| 122 | int kind; |
[email protected] | 57113ea | 2009-06-18 02:23:16 | [diff] [blame] | 123 | if (pickle.ReadInt(&iter, &kind)) { |
| 124 | switch (kind) { |
| 125 | case ZygoteHost::kCmdFork: |
| 126 | return HandleForkRequest(fd, pickle, iter, fds); |
| 127 | case ZygoteHost::kCmdReap: |
| 128 | if (!fds.empty()) |
| 129 | break; |
| 130 | return HandleReapRequest(fd, pickle, iter); |
| 131 | case ZygoteHost::kCmdDidProcessCrash: |
| 132 | if (!fds.empty()) |
| 133 | break; |
| 134 | return HandleDidProcessCrash(fd, pickle, iter); |
| 135 | default: |
| 136 | NOTREACHED(); |
| 137 | break; |
| 138 | } |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 139 | } |
| 140 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 141 | LOG(WARNING) << "Error parsing message from browser"; |
| 142 | for (std::vector<int>::const_iterator |
| 143 | i = fds.begin(); i != fds.end(); ++i) |
| 144 | close(*i); |
| 145 | return false; |
| 146 | } |
| 147 | |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 148 | bool HandleReapRequest(int fd, const Pickle& pickle, void* iter) { |
| 149 | base::ProcessId child; |
| 150 | base::ProcessId actual_child; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 151 | |
| 152 | if (!pickle.ReadInt(&iter, &child)) { |
| 153 | LOG(WARNING) << "Error parsing reap request from browser"; |
| 154 | return false; |
| 155 | } |
| 156 | |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 157 | if (g_suid_sandbox_active) { |
| 158 | actual_child = real_pids_to_sandbox_pids[child]; |
| 159 | if (!actual_child) |
| 160 | return false; |
| 161 | real_pids_to_sandbox_pids.erase(child); |
| 162 | } else { |
| 163 | actual_child = child; |
| 164 | } |
| 165 | |
| 166 | ProcessWatcher::EnsureProcessTerminated(actual_child); |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 167 | |
| 168 | return false; |
| 169 | } |
| 170 | |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 171 | bool HandleDidProcessCrash(int fd, const Pickle& pickle, void* iter) { |
[email protected] | 57113ea | 2009-06-18 02:23:16 | [diff] [blame] | 172 | base::ProcessHandle child; |
| 173 | |
| 174 | if (!pickle.ReadInt(&iter, &child)) { |
| 175 | LOG(WARNING) << "Error parsing DidProcessCrash request from browser"; |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | bool child_exited; |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 180 | bool did_crash; |
| 181 | if (g_suid_sandbox_active) |
| 182 | child = real_pids_to_sandbox_pids[child]; |
| 183 | if (child) |
| 184 | did_crash = base::DidProcessCrash(&child_exited, child); |
| 185 | else |
| 186 | did_crash = child_exited = false; |
[email protected] | 57113ea | 2009-06-18 02:23:16 | [diff] [blame] | 187 | |
| 188 | Pickle write_pickle; |
| 189 | write_pickle.WriteBool(did_crash); |
| 190 | write_pickle.WriteBool(child_exited); |
| 191 | HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size())); |
| 192 | |
| 193 | return false; |
| 194 | } |
| 195 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 196 | // Handle a 'fork' request from the browser: this means that the browser |
| 197 | // wishes to start a new renderer. |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 198 | bool HandleForkRequest(int fd, const Pickle& pickle, void* iter, |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 199 | std::vector<int>& fds) { |
| 200 | std::vector<std::string> args; |
| 201 | int argc, numfds; |
| 202 | base::GlobalDescriptors::Mapping mapping; |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 203 | base::ProcessId child; |
| 204 | uint64_t dummy_inode = 0; |
| 205 | int dummy_fd = -1; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 206 | |
| 207 | if (!pickle.ReadInt(&iter, &argc)) |
| 208 | goto error; |
| 209 | |
| 210 | for (int i = 0; i < argc; ++i) { |
| 211 | std::string arg; |
| 212 | if (!pickle.ReadString(&iter, &arg)) |
| 213 | goto error; |
| 214 | args.push_back(arg); |
| 215 | } |
| 216 | |
| 217 | if (!pickle.ReadInt(&iter, &numfds)) |
| 218 | goto error; |
| 219 | if (numfds != static_cast<int>(fds.size())) |
| 220 | goto error; |
| 221 | |
| 222 | for (int i = 0; i < numfds; ++i) { |
| 223 | base::GlobalDescriptors::Key key; |
| 224 | if (!pickle.ReadUInt32(&iter, &key)) |
| 225 | goto error; |
| 226 | mapping.push_back(std::make_pair(key, fds[i])); |
| 227 | } |
| 228 | |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 229 | mapping.push_back(std::make_pair( |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 230 | static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor)); |
| 231 | |
| 232 | if (g_suid_sandbox_active) { |
| 233 | dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0); |
| 234 | if (dummy_fd < 0) |
| 235 | goto error; |
| 236 | |
| 237 | if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd)) |
| 238 | goto error; |
| 239 | } |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 240 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 241 | child = fork(); |
| 242 | |
| 243 | if (!child) { |
[email protected] | 8015de3 | 2009-11-18 04:13:33 | [diff] [blame] | 244 | #if defined(ARCH_CPU_X86_FAMILY) |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 245 | // Try to open /proc/self/maps as the seccomp sandbox needs access to it |
| 246 | if (g_proc_fd >= 0) { |
| 247 | int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY); |
| 248 | if (proc_self_maps >= 0) { |
| 249 | SeccompSandboxSetProcSelfMaps(proc_self_maps); |
| 250 | } |
| 251 | close(g_proc_fd); |
| 252 | g_proc_fd = -1; |
| 253 | } |
[email protected] | 8015de3 | 2009-11-18 04:13:33 | [diff] [blame] | 254 | #endif |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 255 | |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 256 | close(kBrowserDescriptor); // our socket from the browser |
[email protected] | cbcf9cc3 | 2010-02-17 04:05:59 | [diff] [blame^] | 257 | if (g_suid_sandbox_active) |
| 258 | close(kZygoteIdDescriptor); // another socket from the browser |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 259 | Singleton<base::GlobalDescriptors>()->Reset(mapping); |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 260 | |
| 261 | // Reset the process-wide command line to our new command line. |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 262 | CommandLine::Reset(); |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 263 | CommandLine::Init(0, NULL); |
| 264 | CommandLine::ForCurrentProcess()->InitFromArgv(args); |
[email protected] | 7f113f3 | 2009-09-10 18:02:17 | [diff] [blame] | 265 | CommandLine::SetProcTitle(); |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 266 | return true; |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 267 | } else if (child < 0) { |
| 268 | LOG(ERROR) << "Zygote could not fork"; |
| 269 | goto error; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 270 | } |
| 271 | |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 272 | { |
| 273 | base::ProcessId proc_id; |
| 274 | if (g_suid_sandbox_active) { |
| 275 | close(dummy_fd); |
| 276 | dummy_fd = -1; |
| 277 | uint8_t reply_buf[512]; |
| 278 | Pickle request; |
| 279 | request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE); |
| 280 | request.WriteUInt64(dummy_inode); |
[email protected] | 83a0cbe | 2009-11-04 04:22:47 | [diff] [blame] | 281 | |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 282 | const ssize_t r = base::SendRecvMsg(kMagicSandboxIPCDescriptor, |
| 283 | reply_buf, sizeof(reply_buf), |
| 284 | NULL, request); |
| 285 | if (r == -1) |
| 286 | goto error; |
| 287 | |
| 288 | Pickle reply(reinterpret_cast<char*>(reply_buf), r); |
| 289 | void* iter2 = NULL; |
| 290 | if (!reply.ReadInt(&iter2, &proc_id)) |
| 291 | goto error; |
| 292 | real_pids_to_sandbox_pids[proc_id] = child; |
| 293 | } else { |
| 294 | proc_id = child; |
| 295 | } |
| 296 | |
| 297 | for (std::vector<int>::const_iterator |
| 298 | i = fds.begin(); i != fds.end(); ++i) |
| 299 | close(*i); |
| 300 | |
| 301 | HANDLE_EINTR(write(fd, &proc_id, sizeof(proc_id))); |
| 302 | return false; |
| 303 | } |
[email protected] | 83a0cbe | 2009-11-04 04:22:47 | [diff] [blame] | 304 | |
| 305 | error: |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 306 | LOG(ERROR) << "Error parsing fork request from browser"; |
[email protected] | 83a0cbe | 2009-11-04 04:22:47 | [diff] [blame] | 307 | for (std::vector<int>::const_iterator |
| 308 | i = fds.begin(); i != fds.end(); ++i) |
| 309 | close(*i); |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 310 | if (dummy_fd >= 0) |
| 311 | close(dummy_fd); |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 312 | return false; |
| 313 | } |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 314 | |
| 315 | // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs |
| 316 | // fork() returns are not the real PIDs, so we need to map the Real PIDS |
| 317 | // into the sandbox PID namespace. |
| 318 | typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap; |
| 319 | ProcessMap real_pids_to_sandbox_pids; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 320 | }; |
| 321 | |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 322 | // With SELinux we can carve out a precise sandbox, so we don't have to play |
| 323 | // with intercepting libc calls. |
| 324 | #if !defined(CHROMIUM_SELINUX) |
| 325 | |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 326 | static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output, |
| 327 | char* timezone_out, |
| 328 | size_t timezone_out_len) { |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 329 | Pickle request; |
| 330 | request.WriteInt(LinuxSandbox::METHOD_LOCALTIME); |
| 331 | request.WriteString( |
| 332 | std::string(reinterpret_cast<char*>(&input), sizeof(input))); |
| 333 | |
| 334 | uint8_t reply_buf[512]; |
| 335 | const ssize_t r = base::SendRecvMsg( |
| 336 | kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request); |
| 337 | if (r == -1) { |
| 338 | memset(output, 0, sizeof(struct tm)); |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | Pickle reply(reinterpret_cast<char*>(reply_buf), r); |
| 343 | void* iter = NULL; |
[email protected] | 9debcda5 | 2009-07-22 20:06:51 | [diff] [blame] | 344 | std::string result, timezone; |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 345 | if (!reply.ReadString(&iter, &result) || |
[email protected] | 9debcda5 | 2009-07-22 20:06:51 | [diff] [blame] | 346 | !reply.ReadString(&iter, &timezone) || |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 347 | result.size() != sizeof(struct tm)) { |
| 348 | memset(output, 0, sizeof(struct tm)); |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | memcpy(output, result.data(), sizeof(struct tm)); |
[email protected] | 9debcda5 | 2009-07-22 20:06:51 | [diff] [blame] | 353 | if (timezone_out_len) { |
| 354 | const size_t copy_len = std::min(timezone_out_len - 1, timezone.size()); |
| 355 | memcpy(timezone_out, timezone.data(), copy_len); |
| 356 | timezone_out[copy_len] = 0; |
| 357 | output->tm_zone = timezone_out; |
| 358 | } else { |
| 359 | output->tm_zone = NULL; |
| 360 | } |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 361 | } |
| 362 | |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 363 | static bool g_am_zygote_or_renderer = false; |
| 364 | |
| 365 | // Sandbox interception of libc calls. |
| 366 | // |
| 367 | // Because we are running in a sandbox certain libc calls will fail (localtime |
| 368 | // being the motivating example - it needs to read /etc/localtime). We need to |
| 369 | // intercept these calls and proxy them to the browser. However, these calls |
| 370 | // may come from us or from our libraries. In some cases we can't just change |
| 371 | // our code. |
| 372 | // |
| 373 | // It's for these cases that we have the following setup: |
| 374 | // |
| 375 | // We define global functions for those functions which we wish to override. |
| 376 | // Since we will be first in the dynamic resolution order, the dynamic linker |
| 377 | // will point callers to our versions of these functions. However, we have the |
| 378 | // same binary for both the browser and the renderers, which means that our |
| 379 | // overrides will apply in the browser too. |
| 380 | // |
| 381 | // The global |g_am_zygote_or_renderer| is true iff we are in a zygote or |
| 382 | // renderer process. It's set in ZygoteMain and inherited by the renderers when |
| 383 | // they fork. (This means that it'll be incorrect for global constructor |
| 384 | // functions and before ZygoteMain is called - beware). |
| 385 | // |
| 386 | // Our replacement functions can check this global and either proxy |
| 387 | // the call to the browser over the sandbox IPC |
| 388 | // (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use |
| 389 | // dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the |
| 390 | // current module. |
| 391 | // |
| 392 | // Other avenues: |
| 393 | // |
| 394 | // Our first attempt involved some assembly to patch the GOT of the current |
| 395 | // module. This worked, but was platform specific and doesn't catch the case |
| 396 | // where a library makes a call rather than current module. |
| 397 | // |
| 398 | // We also considered patching the function in place, but this would again by |
| 399 | // platform specific and the above technique seems to work well enough. |
| 400 | |
[email protected] | a5d7bb8 | 2009-09-08 22:30:58 | [diff] [blame] | 401 | static void WarnOnceAboutBrokenDlsym(); |
| 402 | |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 403 | struct tm* localtime(const time_t* timep) { |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 404 | if (g_am_zygote_or_renderer) { |
| 405 | static struct tm time_struct; |
| 406 | static char timezone_string[64]; |
| 407 | ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string, |
| 408 | sizeof(timezone_string)); |
| 409 | return &time_struct; |
| 410 | } else { |
| 411 | typedef struct tm* (*LocaltimeFunction)(const time_t* timep); |
| 412 | static LocaltimeFunction libc_localtime; |
[email protected] | a5d7bb8 | 2009-09-08 22:30:58 | [diff] [blame] | 413 | static bool have_libc_localtime = false; |
| 414 | if (!have_libc_localtime) { |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 415 | libc_localtime = (LocaltimeFunction) dlsym(RTLD_NEXT, "localtime"); |
[email protected] | a5d7bb8 | 2009-09-08 22:30:58 | [diff] [blame] | 416 | have_libc_localtime = true; |
| 417 | } |
| 418 | |
| 419 | if (!libc_localtime) { |
| 420 | // http://code.google.com/p/chromium/issues/detail?id=16800 |
| 421 | // |
| 422 | // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces |
| 423 | // it with a version which doesn't work. In this case we'll get a NULL |
| 424 | // result. There's not a lot we can do at this point, so we just bodge it! |
| 425 | WarnOnceAboutBrokenDlsym(); |
| 426 | |
| 427 | return gmtime(timep); |
| 428 | } |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 429 | |
| 430 | return libc_localtime(timep); |
| 431 | } |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | struct tm* localtime_r(const time_t* timep, struct tm* result) { |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 435 | if (g_am_zygote_or_renderer) { |
| 436 | ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0); |
| 437 | return result; |
| 438 | } else { |
| 439 | typedef struct tm* (*LocaltimeRFunction)(const time_t* timep, |
| 440 | struct tm* result); |
| 441 | static LocaltimeRFunction libc_localtime_r; |
[email protected] | a5d7bb8 | 2009-09-08 22:30:58 | [diff] [blame] | 442 | static bool have_libc_localtime_r = false; |
| 443 | if (!have_libc_localtime_r) { |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 444 | libc_localtime_r = (LocaltimeRFunction) dlsym(RTLD_NEXT, "localtime_r"); |
[email protected] | a5d7bb8 | 2009-09-08 22:30:58 | [diff] [blame] | 445 | have_libc_localtime_r = true; |
| 446 | } |
| 447 | |
| 448 | if (!libc_localtime_r) { |
| 449 | // See |localtime|, above. |
| 450 | WarnOnceAboutBrokenDlsym(); |
| 451 | |
| 452 | return gmtime_r(timep, result); |
| 453 | } |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 454 | |
| 455 | return libc_localtime_r(timep, result); |
| 456 | } |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 457 | } |
| 458 | |
[email protected] | a5d7bb8 | 2009-09-08 22:30:58 | [diff] [blame] | 459 | // See the comments at the callsite in |localtime| about this function. |
| 460 | static void WarnOnceAboutBrokenDlsym() { |
| 461 | static bool have_shown_warning = false; |
| 462 | if (!have_shown_warning) { |
| 463 | LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been " |
| 464 | "reported to be caused by Nvidia's libGL. You should expect " |
| 465 | "time related functions to misbehave. " |
| 466 | "http://code.google.com/p/chromium/issues/detail?id=16800"; |
| 467 | have_shown_warning = true; |
| 468 | } |
| 469 | } |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 470 | #endif // !CHROMIUM_SELINUX |
[email protected] | a5d7bb8 | 2009-09-08 22:30:58 | [diff] [blame] | 471 | |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 472 | // This function triggers the static and lazy construction of objects that need |
| 473 | // to be created before imposing the sandbox. |
| 474 | static void PreSandboxInit() { |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 475 | base::RandUint64(); |
| 476 | |
[email protected] | 80a086c5 | 2009-08-04 17:52:04 | [diff] [blame] | 477 | base::SysInfo::MaxSharedMemorySize(); |
| 478 | |
[email protected] | e6acd67 | 2009-07-24 21:51:33 | [diff] [blame] | 479 | // To make wcstombs/mbstowcs work in a renderer, setlocale() has to be |
| 480 | // called before the sandbox is triggered. It's possible to avoid calling |
| 481 | // setlocale() by pulling out the conversion between FilePath and |
| 482 | // WebCore String out of the renderer and using string16 in place of |
| 483 | // FilePath for IPC. |
| 484 | const char* locale = setlocale(LC_ALL, ""); |
| 485 | LOG_IF(WARNING, locale == NULL) << "setlocale failed."; |
| 486 | |
[email protected] | 1831acf | 2009-10-05 16:38:41 | [diff] [blame] | 487 | // ICU DateFormat class (used in base/time_format.cc) needs to get the |
| 488 | // Olson timezone ID by accessing the zoneinfo files on disk. After |
| 489 | // TimeZone::createDefault is called once here, the timezone ID is |
| 490 | // cached and there's no more need to access the file system. |
| 491 | scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); |
| 492 | |
[email protected] | c9e45da0 | 2009-08-05 17:35:08 | [diff] [blame] | 493 | FilePath module_path; |
| 494 | if (PathService::Get(base::DIR_MODULE, &module_path)) |
| 495 | media::InitializeMediaLibrary(module_path); |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | #if !defined(CHROMIUM_SELINUX) |
| 499 | static bool EnterSandbox() { |
| 500 | const char* const sandbox_fd_string = getenv("SBX_D"); |
| 501 | if (sandbox_fd_string) { |
| 502 | // The SUID sandbox sets this environment variable to a file descriptor |
| 503 | // over which we can signal that we have completed our startup and can be |
| 504 | // chrooted. |
| 505 | |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 506 | g_suid_sandbox_active = true; |
| 507 | |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 508 | char* endptr; |
| 509 | const long fd_long = strtol(sandbox_fd_string, &endptr, 10); |
| 510 | if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX) |
| 511 | return false; |
| 512 | const int fd = fd_long; |
| 513 | |
| 514 | PreSandboxInit(); |
[email protected] | c9e45da0 | 2009-08-05 17:35:08 | [diff] [blame] | 515 | |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 516 | static const char kChrootMe = 'C'; |
| 517 | static const char kChrootMeSuccess = 'O'; |
| 518 | |
[email protected] | 0e161112 | 2009-07-10 18:17:32 | [diff] [blame] | 519 | if (HANDLE_EINTR(write(fd, &kChrootMe, 1)) != 1) { |
| 520 | LOG(ERROR) << "Failed to write to chroot pipe: " << errno; |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 521 | return false; |
[email protected] | 0e161112 | 2009-07-10 18:17:32 | [diff] [blame] | 522 | } |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 523 | |
[email protected] | 87ed695 | 2009-07-16 02:52:15 | [diff] [blame] | 524 | // We need to reap the chroot helper process in any event: |
| 525 | wait(NULL); |
| 526 | |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 527 | char reply; |
[email protected] | 87f8ce6 | 2009-07-10 19:14:31 | [diff] [blame] | 528 | if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) { |
[email protected] | 0e161112 | 2009-07-10 18:17:32 | [diff] [blame] | 529 | LOG(ERROR) << "Failed to read from chroot pipe: " << errno; |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 530 | return false; |
[email protected] | 0e161112 | 2009-07-10 18:17:32 | [diff] [blame] | 531 | } |
[email protected] | 87f8ce6 | 2009-07-10 19:14:31 | [diff] [blame] | 532 | |
[email protected] | 0e161112 | 2009-07-10 18:17:32 | [diff] [blame] | 533 | if (reply != kChrootMeSuccess) { |
| 534 | LOG(ERROR) << "Error code reply from chroot helper"; |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 535 | return false; |
[email protected] | 0e161112 | 2009-07-10 18:17:32 | [diff] [blame] | 536 | } |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 537 | |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 538 | SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor); |
| 539 | |
[email protected] | 415493be | 2009-07-10 17:50:24 | [diff] [blame] | 540 | // Previously, we required that the binary be non-readable. This causes the |
| 541 | // kernel to mark the process as non-dumpable at startup. The thinking was |
| 542 | // that, although we were putting the renderers into a PID namespace (with |
| 543 | // the SUID sandbox), they would nonetheless be in the /same/ PID |
| 544 | // namespace. So they could ptrace each other unless they were non-dumpable. |
| 545 | // |
| 546 | // If the binary was readable, then there would be a window between process |
| 547 | // startup and the point where we set the non-dumpable flag in which a |
| 548 | // compromised renderer could ptrace attach. |
| 549 | // |
| 550 | // However, now that we have a zygote model, only the (trusted) zygote |
| 551 | // exists at this point and we can set the non-dumpable flag which is |
| 552 | // inherited by all our renderer children. |
[email protected] | 4730db9 | 2009-07-22 00:40:48 | [diff] [blame] | 553 | // |
| 554 | // Note: a non-dumpable process can't be debugged. To debug sandbox-related |
| 555 | // issues, one can specify --allow-sandbox-debugging to let the process be |
| 556 | // dumpable. |
| 557 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 558 | if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) { |
| 559 | prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); |
| 560 | if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { |
| 561 | LOG(ERROR) << "Failed to set non-dumpable flag"; |
| 562 | return false; |
| 563 | } |
[email protected] | 0e161112 | 2009-07-10 18:17:32 | [diff] [blame] | 564 | } |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 565 | } else { |
| 566 | SkiaFontConfigUseDirectImplementation(); |
| 567 | } |
| 568 | |
| 569 | return true; |
| 570 | } |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 571 | #else // CHROMIUM_SELINUX |
| 572 | |
| 573 | static bool EnterSandbox() { |
| 574 | PreSandboxInit(); |
| 575 | SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor); |
| 576 | |
| 577 | security_context_t security_context; |
| 578 | if (getcon(&security_context)) { |
| 579 | LOG(ERROR) << "Cannot get SELinux context"; |
| 580 | return false; |
| 581 | } |
| 582 | |
| 583 | context_t context = context_new(security_context); |
| 584 | context_type_set(context, "chromium_renderer_t"); |
| 585 | const int r = setcon(context_str(context)); |
| 586 | context_free(context); |
| 587 | freecon(security_context); |
| 588 | |
| 589 | if (r) { |
| 590 | LOG(ERROR) << "dynamic transition to type 'chromium_renderer_t' failed. " |
| 591 | "(this binary has been built with SELinux support, but maybe " |
| 592 | "the policies haven't been loaded into the kernel?"; |
| 593 | return false; |
| 594 | } |
| 595 | |
| 596 | return true; |
| 597 | } |
| 598 | |
| 599 | #endif // CHROMIUM_SELINUX |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 600 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 601 | bool ZygoteMain(const MainFunctionParams& params) { |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 602 | #if !defined(CHROMIUM_SELINUX) |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 603 | g_am_zygote_or_renderer = true; |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 604 | #endif |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 605 | |
[email protected] | 8015de3 | 2009-11-18 04:13:33 | [diff] [blame] | 606 | #if defined(ARCH_CPU_X86_FAMILY) |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 607 | // The seccomp sandbox needs access to files in /proc, which might be denied |
| 608 | // after one of the other sandboxes have been started. So, obtain a suitable |
| 609 | // file handle in advance. |
[email protected] | e8c916a | 2009-11-04 17:52:47 | [diff] [blame] | 610 | if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 611 | switches::kEnableSeccompSandbox)) { |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 612 | g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY); |
| 613 | if (g_proc_fd < 0) { |
| 614 | LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp " |
| 615 | "sandboxing."; |
| 616 | } |
| 617 | } |
[email protected] | 8015de3 | 2009-11-18 04:13:33 | [diff] [blame] | 618 | #endif // ARCH_CPU_X86_FAMILY |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 619 | |
| 620 | // Turn on the SELinux or SUID sandbox |
| 621 | if (!EnterSandbox()) { |
| 622 | LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: " |
| 623 | << errno << ")"; |
| 624 | return false; |
| 625 | } |
| 626 | |
[email protected] | 8015de3 | 2009-11-18 04:13:33 | [diff] [blame] | 627 | #if defined(ARCH_CPU_X86_FAMILY) |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 628 | // The seccomp sandbox will be turned on when the renderers start. But we can |
| 629 | // already check if sufficient support is available so that we only need to |
| 630 | // print one error message for the entire browser session. |
| 631 | if (g_proc_fd >= 0 && |
| 632 | CommandLine::ForCurrentProcess()->HasSwitch( |
| 633 | switches::kEnableSeccompSandbox)) { |
| 634 | if (!SupportsSeccompSandbox(g_proc_fd)) { |
[email protected] | e8c916a | 2009-11-04 17:52:47 | [diff] [blame] | 635 | // There are a good number of users who cannot use the seccomp sandbox |
| 636 | // (e.g. because their distribution does not enable seccomp mode by |
| 637 | // default). While we would prefer to deny execution in this case, it |
| 638 | // seems more realistic to continue in degraded mode. |
| 639 | LOG(ERROR) << "WARNING! This machine lacks support needed for the " |
| 640 | "Seccomp sandbox. Running renderers with Seccomp " |
| 641 | "sandboxing disabled."; |
| 642 | } else { |
| 643 | LOG(INFO) << "Enabling experimental Seccomp sandbox."; |
| 644 | } |
| 645 | } |
[email protected] | 8015de3 | 2009-11-18 04:13:33 | [diff] [blame] | 646 | #endif // ARCH_CPU_X86_FAMILY |
[email protected] | e8c916a | 2009-11-04 17:52:47 | [diff] [blame] | 647 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 648 | Zygote zygote; |
| 649 | return zygote.ProcessRequests(); |
| 650 | } |