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