[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 | |
| 5 | #include <unistd.h> |
| 6 | #include <sys/epoll.h> |
| 7 | #include <sys/types.h> |
| 8 | #include <sys/socket.h> |
| 9 | #include <sys/signal.h> |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 10 | #include <sys/prctl.h> |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 11 | |
| 12 | #include "base/command_line.h" |
| 13 | #include "base/eintr_wrapper.h" |
| 14 | #include "base/global_descriptors_posix.h" |
| 15 | #include "base/pickle.h" |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 16 | #include "base/rand_util.h" |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 17 | #include "base/unix_domain_socket_posix.h" |
| 18 | |
| 19 | #include "chrome/browser/zygote_host_linux.h" |
| 20 | #include "chrome/common/chrome_descriptors.h" |
| 21 | #include "chrome/common/main_function_params.h" |
| 22 | #include "chrome/common/process_watcher.h" |
| 23 | |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 24 | #include "skia/ext/SkFontHost_fontconfig_control.h" |
| 25 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 26 | // http://code.google.com/p/chromium/wiki/LinuxZygote |
| 27 | |
| 28 | // This is the object which implements the zygote. The ZygoteMain function, |
| 29 | // which is called from ChromeMain, at the the bottom and simple constructs one |
| 30 | // of these objects and runs it. |
| 31 | class Zygote { |
| 32 | public: |
| 33 | bool ProcessRequests() { |
| 34 | // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the |
| 35 | // browser on it. |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 36 | // A SOCK_DGRAM is installed in fd 4. This is the sandbox IPC channel. |
| 37 | // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 38 | |
| 39 | // We need to accept SIGCHLD, even though our handler is a no-op because |
| 40 | // otherwise we cannot wait on children. (According to POSIX 2001.) |
| 41 | struct sigaction action; |
| 42 | memset(&action, 0, sizeof(action)); |
| 43 | action.sa_handler = SIGCHLDHandler; |
| 44 | CHECK(sigaction(SIGCHLD, &action, NULL) == 0); |
| 45 | |
| 46 | for (;;) { |
| 47 | if (HandleRequestFromBrowser(3)) |
| 48 | return true; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private: |
| 53 | // See comment below, where sigaction is called. |
| 54 | static void SIGCHLDHandler(int signal) { } |
| 55 | |
| 56 | // --------------------------------------------------------------------------- |
| 57 | // Requests from the browser... |
| 58 | |
| 59 | // Read and process a request from the browser. Returns true if we are in a |
| 60 | // new process and thus need to unwind back into ChromeMain. |
| 61 | bool HandleRequestFromBrowser(int fd) { |
| 62 | std::vector<int> fds; |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 63 | static const unsigned kMaxMessageLength = 1024; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 64 | char buf[kMaxMessageLength]; |
| 65 | const ssize_t len = base::RecvMsg(fd, buf, sizeof(buf), &fds); |
| 66 | if (len == -1) { |
| 67 | LOG(WARNING) << "Error reading message from browser: " << errno; |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | if (len == 0) { |
| 72 | // EOF from the browser. We should die. |
| 73 | _exit(0); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | Pickle pickle(buf, len); |
| 78 | void* iter = NULL; |
| 79 | |
| 80 | int kind; |
[email protected] | 57113ea | 2009-06-18 02:23:16 | [diff] [blame] | 81 | if (pickle.ReadInt(&iter, &kind)) { |
| 82 | switch (kind) { |
| 83 | case ZygoteHost::kCmdFork: |
| 84 | return HandleForkRequest(fd, pickle, iter, fds); |
| 85 | case ZygoteHost::kCmdReap: |
| 86 | if (!fds.empty()) |
| 87 | break; |
| 88 | return HandleReapRequest(fd, pickle, iter); |
| 89 | case ZygoteHost::kCmdDidProcessCrash: |
| 90 | if (!fds.empty()) |
| 91 | break; |
| 92 | return HandleDidProcessCrash(fd, pickle, iter); |
| 93 | default: |
| 94 | NOTREACHED(); |
| 95 | break; |
| 96 | } |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 99 | LOG(WARNING) << "Error parsing message from browser"; |
| 100 | for (std::vector<int>::const_iterator |
| 101 | i = fds.begin(); i != fds.end(); ++i) |
| 102 | close(*i); |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | bool HandleReapRequest(int fd, Pickle& pickle, void* iter) { |
| 107 | pid_t child; |
| 108 | |
| 109 | if (!pickle.ReadInt(&iter, &child)) { |
| 110 | LOG(WARNING) << "Error parsing reap request from browser"; |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | ProcessWatcher::EnsureProcessTerminated(child); |
| 115 | |
| 116 | return false; |
| 117 | } |
| 118 | |
[email protected] | 57113ea | 2009-06-18 02:23:16 | [diff] [blame] | 119 | bool HandleDidProcessCrash(int fd, Pickle& pickle, void* iter) { |
| 120 | base::ProcessHandle child; |
| 121 | |
| 122 | if (!pickle.ReadInt(&iter, &child)) { |
| 123 | LOG(WARNING) << "Error parsing DidProcessCrash request from browser"; |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | bool child_exited; |
| 128 | bool did_crash = base::DidProcessCrash(&child_exited, child); |
| 129 | |
| 130 | Pickle write_pickle; |
| 131 | write_pickle.WriteBool(did_crash); |
| 132 | write_pickle.WriteBool(child_exited); |
| 133 | HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size())); |
| 134 | |
| 135 | return false; |
| 136 | } |
| 137 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 138 | // Handle a 'fork' request from the browser: this means that the browser |
| 139 | // wishes to start a new renderer. |
| 140 | bool HandleForkRequest(int fd, Pickle& pickle, void* iter, |
| 141 | std::vector<int>& fds) { |
| 142 | std::vector<std::string> args; |
| 143 | int argc, numfds; |
| 144 | base::GlobalDescriptors::Mapping mapping; |
| 145 | pid_t child; |
| 146 | |
| 147 | if (!pickle.ReadInt(&iter, &argc)) |
| 148 | goto error; |
| 149 | |
| 150 | for (int i = 0; i < argc; ++i) { |
| 151 | std::string arg; |
| 152 | if (!pickle.ReadString(&iter, &arg)) |
| 153 | goto error; |
| 154 | args.push_back(arg); |
| 155 | } |
| 156 | |
| 157 | if (!pickle.ReadInt(&iter, &numfds)) |
| 158 | goto error; |
| 159 | if (numfds != static_cast<int>(fds.size())) |
| 160 | goto error; |
| 161 | |
| 162 | for (int i = 0; i < numfds; ++i) { |
| 163 | base::GlobalDescriptors::Key key; |
| 164 | if (!pickle.ReadUInt32(&iter, &key)) |
| 165 | goto error; |
| 166 | mapping.push_back(std::make_pair(key, fds[i])); |
| 167 | } |
| 168 | |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 169 | mapping.push_back(std::make_pair( |
[email protected] | e00f9da | 2009-06-26 00:47:05 | [diff] [blame] | 170 | static_cast<uint32_t>(kSandboxIPCChannel), 5)); |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 171 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 172 | child = fork(); |
| 173 | |
| 174 | if (!child) { |
| 175 | close(3); // our socket from the browser is in fd 3 |
| 176 | Singleton<base::GlobalDescriptors>()->Reset(mapping); |
| 177 | CommandLine::Reset(); |
| 178 | CommandLine::Init(args); |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | for (std::vector<int>::const_iterator |
| 183 | i = fds.begin(); i != fds.end(); ++i) |
| 184 | close(*i); |
| 185 | |
| 186 | HANDLE_EINTR(write(fd, &child, sizeof(child))); |
| 187 | return false; |
| 188 | |
| 189 | error: |
| 190 | LOG(WARNING) << "Error parsing fork request from browser"; |
| 191 | for (std::vector<int>::const_iterator |
| 192 | i = fds.begin(); i != fds.end(); ++i) |
| 193 | close(*i); |
| 194 | return false; |
| 195 | } |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 196 | }; |
| 197 | |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 198 | static bool MaybeEnterChroot() { |
| 199 | const char* const sandbox_fd_string = getenv("SBX_D"); |
| 200 | if (sandbox_fd_string) { |
| 201 | // The SUID sandbox sets this environment variable to a file descriptor |
| 202 | // over which we can signal that we have completed our startup and can be |
| 203 | // chrooted. |
| 204 | |
| 205 | char* endptr; |
| 206 | const long fd_long = strtol(sandbox_fd_string, &endptr, 10); |
| 207 | if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX) |
| 208 | return false; |
| 209 | const int fd = fd_long; |
| 210 | |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 211 | // Before entering the sandbox, "prime" any systems that need to open |
| 212 | // files and cache the results or the descriptors. |
| 213 | base::RandUint64(); |
| 214 | |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 215 | static const char kChrootMe = 'C'; |
| 216 | static const char kChrootMeSuccess = 'O'; |
| 217 | |
| 218 | if (HANDLE_EINTR(write(fd, &kChrootMe, 1)) != 1) |
| 219 | return false; |
| 220 | |
| 221 | char reply; |
| 222 | if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) |
| 223 | return false; |
| 224 | if (reply != kChrootMeSuccess) |
| 225 | return false; |
| 226 | if (chdir("/") == -1) |
| 227 | return false; |
| 228 | |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 229 | static const int kMagicSandboxIPCDescriptor = 5; |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 230 | SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor); |
| 231 | |
[email protected] | 415493be | 2009-07-10 17:50:24 | [diff] [blame^] | 232 | // Previously, we required that the binary be non-readable. This causes the |
| 233 | // kernel to mark the process as non-dumpable at startup. The thinking was |
| 234 | // that, although we were putting the renderers into a PID namespace (with |
| 235 | // the SUID sandbox), they would nonetheless be in the /same/ PID |
| 236 | // namespace. So they could ptrace each other unless they were non-dumpable. |
| 237 | // |
| 238 | // If the binary was readable, then there would be a window between process |
| 239 | // startup and the point where we set the non-dumpable flag in which a |
| 240 | // compromised renderer could ptrace attach. |
| 241 | // |
| 242 | // However, now that we have a zygote model, only the (trusted) zygote |
| 243 | // exists at this point and we can set the non-dumpable flag which is |
| 244 | // inherited by all our renderer children. |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 245 | prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); |
| 246 | if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) |
| 247 | return false; |
| 248 | } else { |
| 249 | SkiaFontConfigUseDirectImplementation(); |
| 250 | } |
| 251 | |
| 252 | return true; |
| 253 | } |
| 254 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 255 | bool ZygoteMain(const MainFunctionParams& params) { |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 256 | if (!MaybeEnterChroot()) { |
| 257 | LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: " |
| 258 | << errno << ")"; |
| 259 | return false; |
| 260 | } |
| 261 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 262 | Zygote zygote; |
| 263 | return zygote.ProcessRequests(); |
| 264 | } |