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