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