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