[email protected] | 13f1d5c | 2012-03-06 03:10:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 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] | c2c68b1f | 2012-02-25 00:29:15 | [diff] [blame] | 5 | #include "content/browser/zygote_host_impl_linux.h" |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 6 | |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 7 | #include <dlfcn.h> |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 8 | #include <fcntl.h> |
[email protected] | 724df99 | 2010-09-21 18:19:10 | [diff] [blame] | 9 | #include <pthread.h> |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 10 | #include <sys/socket.h> |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 11 | #include <sys/stat.h> |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 12 | #include <sys/types.h> |
[email protected] | 83a0cbe | 2009-11-04 04:22:47 | [diff] [blame] | 13 | #include <sys/wait.h> |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 14 | #include <unistd.h> |
| 15 | |
[email protected] | 789f70c7 | 2009-07-24 13:55:43 | [diff] [blame] | 16 | #include "base/basictypes.h" |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 17 | #include "base/command_line.h" |
| 18 | #include "base/eintr_wrapper.h" |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 19 | #include "base/file_path.h" |
[email protected] | e1d67a88 | 2011-08-31 21:11:04 | [diff] [blame] | 20 | #include "base/file_util.h" |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 21 | #include "base/global_descriptors_posix.h" |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 22 | #include "base/hash_tables.h" |
| 23 | #include "base/linux_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 24 | #include "base/memory/scoped_ptr.h" |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 25 | #include "base/pickle.h" |
[email protected] | 4d36536b | 2010-08-20 06:23:27 | [diff] [blame] | 26 | #include "base/process_util.h" |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 27 | #include "base/rand_util.h" |
[email protected] | 80a086c5 | 2009-08-04 17:52:04 | [diff] [blame] | 28 | #include "base/sys_info.h" |
[email protected] | 8015de3 | 2009-11-18 04:13:33 | [diff] [blame] | 29 | #include "build/build_config.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 30 | #include "crypto/nss_util.h" |
[email protected] | acb9472 | 2011-03-18 01:33:34 | [diff] [blame] | 31 | #include "content/common/chrome_descriptors.h" |
[email protected] | 797c355 | 2011-03-17 00:26:18 | [diff] [blame] | 32 | #include "content/common/font_config_ipc_linux.h" |
[email protected] | cebc3dc | 2011-04-18 17:15:00 | [diff] [blame] | 33 | #include "content/common/pepper_plugin_registry.h" |
[email protected] | 4dd5793 | 2011-03-17 06:06:12 | [diff] [blame] | 34 | #include "content/common/sandbox_methods_linux.h" |
[email protected] | 65f607f | 2011-08-03 21:08:12 | [diff] [blame] | 35 | #include "content/common/seccomp_sandbox.h" |
[email protected] | 4dd5793 | 2011-03-17 06:06:12 | [diff] [blame] | 36 | #include "content/common/set_process_title.h" |
[email protected] | 797c355 | 2011-03-17 00:26:18 | [diff] [blame] | 37 | #include "content/common/unix_domain_socket_posix.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 38 | #include "content/public/common/content_switches.h" |
[email protected] | 4573fbd | 2011-10-31 20:25:18 | [diff] [blame] | 39 | #include "content/public/common/main_function_params.h" |
[email protected] | b39ef1cb | 2011-10-25 04:46:55 | [diff] [blame] | 40 | #include "content/public/common/result_codes.h" |
[email protected] | f8ba13d | 2011-10-25 16:08:52 | [diff] [blame] | 41 | #include "content/public/common/zygote_fork_delegate_linux.h" |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 42 | #include "skia/ext/SkFontHost_fontconfig_control.h" |
[email protected] | 1831acf | 2009-10-05 16:38:41 | [diff] [blame] | 43 | #include "unicode/timezone.h" |
[email protected] | e1d67a88 | 2011-08-31 21:11:04 | [diff] [blame] | 44 | #include "ipc/ipc_channel.h" |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 45 | #include "ipc/ipc_switches.h" |
[email protected] | 1831acf | 2009-10-05 16:38:41 | [diff] [blame] | 46 | |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 47 | #if defined(OS_LINUX) |
| 48 | #include <sys/epoll.h> |
| 49 | #include <sys/prctl.h> |
| 50 | #include <sys/signal.h> |
| 51 | #else |
| 52 | #include <signal.h> |
| 53 | #endif |
| 54 | |
| 55 | #if defined(CHROMIUM_SELINUX) |
| 56 | #include <selinux/selinux.h> |
| 57 | #include <selinux/context.h> |
| 58 | #endif |
| 59 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 60 | // http://code.google.com/p/chromium/wiki/LinuxZygote |
| 61 | |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 62 | static const int kBrowserDescriptor = 3; |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 63 | static const int kMagicSandboxIPCDescriptor = 5; |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 64 | static const int kZygoteIdDescriptor = 7; |
| 65 | static bool g_suid_sandbox_active = false; |
[email protected] | 2bc039a | 2011-07-29 23:05:41 | [diff] [blame] | 66 | |
[email protected] | 2bc039a | 2011-07-29 23:05:41 | [diff] [blame] | 67 | #if defined(SECCOMP_SANDBOX) |
[email protected] | 2bc039a | 2011-07-29 23:05:41 | [diff] [blame] | 68 | static int g_proc_fd = -1; |
[email protected] | 65f607f | 2011-08-03 21:08:12 | [diff] [blame] | 69 | #endif |
[email protected] | 2bc039a | 2011-07-29 23:05:41 | [diff] [blame] | 70 | |
[email protected] | a89a55dd | 2010-04-19 14:51:13 | [diff] [blame] | 71 | #if defined(CHROMIUM_SELINUX) |
| 72 | static void SELinuxTransitionToTypeOrDie(const char* type) { |
| 73 | security_context_t security_context; |
| 74 | if (getcon(&security_context)) |
| 75 | LOG(FATAL) << "Cannot get SELinux context"; |
| 76 | |
| 77 | context_t context = context_new(security_context); |
| 78 | context_type_set(context, type); |
| 79 | const int r = setcon(context_str(context)); |
| 80 | context_free(context); |
| 81 | freecon(security_context); |
| 82 | |
| 83 | if (r) { |
| 84 | LOG(FATAL) << "dynamic transition to type '" << type << "' failed. " |
| 85 | "(this binary has been built with SELinux support, but maybe " |
| 86 | "the policies haven't been loaded into the kernel?)"; |
| 87 | } |
| 88 | } |
| 89 | #endif // CHROMIUM_SELINUX |
| 90 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 91 | // This is the object which implements the zygote. The ZygoteMain function, |
[email protected] | 6188344 | 2010-07-12 15:14:34 | [diff] [blame] | 92 | // which is called from ChromeMain, simply constructs one of these objects and |
| 93 | // runs it. |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 94 | class Zygote { |
| 95 | public: |
[email protected] | f8ba13d | 2011-10-25 16:08:52 | [diff] [blame] | 96 | Zygote(int sandbox_flags, content::ZygoteForkDelegate* helper) |
[email protected] | 5abe630 | 2011-12-20 23:44:32 | [diff] [blame] | 97 | : sandbox_flags_(sandbox_flags), |
| 98 | helper_(helper), |
| 99 | initial_uma_sample_(0), |
| 100 | initial_uma_boundary_value_(0) { |
[email protected] | 8d5d761 | 2011-10-20 18:23:14 | [diff] [blame] | 101 | if (helper_) |
| 102 | helper_->InitialUMA(&initial_uma_name_, |
| 103 | &initial_uma_sample_, |
| 104 | &initial_uma_boundary_value_); |
[email protected] | 715b4f26 | 2010-07-13 14:17:28 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 107 | bool ProcessRequests() { |
| 108 | // A SOCK_SEQPACKET socket is installed in fd 3. We get commands from the |
| 109 | // browser on it. |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 110 | // 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] | 111 | // See http://code.google.com/p/chromium/wiki/LinuxSandboxIPC |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 112 | |
| 113 | // We need to accept SIGCHLD, even though our handler is a no-op because |
| 114 | // otherwise we cannot wait on children. (According to POSIX 2001.) |
| 115 | struct sigaction action; |
| 116 | memset(&action, 0, sizeof(action)); |
| 117 | action.sa_handler = SIGCHLDHandler; |
| 118 | CHECK(sigaction(SIGCHLD, &action, NULL) == 0); |
| 119 | |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 120 | if (g_suid_sandbox_active) { |
| 121 | // Let the ZygoteHost know we are ready to go. |
[email protected] | 8a28271 | 2011-08-23 19:28:00 | [diff] [blame] | 122 | // The receiving code is in content/browser/zygote_host_linux.cc. |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 123 | std::vector<int> empty; |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 124 | bool r = UnixDomainSocket::SendMsg(kBrowserDescriptor, kZygoteMagic, |
| 125 | sizeof(kZygoteMagic), empty); |
[email protected] | bfc672a5 | 2011-07-20 07:51:27 | [diff] [blame] | 126 | #if defined(OS_CHROMEOS) |
[email protected] | 13f1d5c | 2012-03-06 03:10:33 | [diff] [blame] | 127 | LOG_IF(WARNING, !r) << "Sending zygote magic failed"; |
[email protected] | bfc672a5 | 2011-07-20 07:51:27 | [diff] [blame] | 128 | // Exit normally on chromeos because session manager may send SIGTERM |
| 129 | // right after the process starts and it may fail to send zygote magic |
| 130 | // number to browser process. |
[email protected] | a45f183 | 2011-07-20 18:01:15 | [diff] [blame] | 131 | if (!r) |
| 132 | _exit(content::RESULT_CODE_NORMAL_EXIT); |
[email protected] | bfc672a5 | 2011-07-20 07:51:27 | [diff] [blame] | 133 | #else |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 134 | CHECK(r) << "Sending zygote magic failed"; |
[email protected] | bfc672a5 | 2011-07-20 07:51:27 | [diff] [blame] | 135 | #endif |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 138 | for (;;) { |
[email protected] | c548be2 | 2010-03-08 12:55:57 | [diff] [blame] | 139 | // This function call can return multiple times, once per fork(). |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 140 | if (HandleRequestFromBrowser(kBrowserDescriptor)) |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | // See comment below, where sigaction is called. |
| 147 | static void SIGCHLDHandler(int signal) { } |
| 148 | |
| 149 | // --------------------------------------------------------------------------- |
| 150 | // Requests from the browser... |
| 151 | |
| 152 | // Read and process a request from the browser. Returns true if we are in a |
| 153 | // new process and thus need to unwind back into ChromeMain. |
| 154 | bool HandleRequestFromBrowser(int fd) { |
| 155 | std::vector<int> fds; |
[email protected] | 2752bf6 | 2011-03-23 21:06:06 | [diff] [blame] | 156 | static const unsigned kMaxMessageLength = 2048; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 157 | char buf[kMaxMessageLength]; |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 158 | const ssize_t len = UnixDomainSocket::RecvMsg(fd, buf, sizeof(buf), &fds); |
[email protected] | 7ae27b9 | 2010-09-24 17:33:36 | [diff] [blame] | 159 | |
| 160 | if (len == 0 || (len == -1 && errno == ECONNRESET)) { |
| 161 | // EOF from the browser. We should die. |
| 162 | _exit(0); |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 163 | return false; |
| 164 | } |
| 165 | |
[email protected] | 7ae27b9 | 2010-09-24 17:33:36 | [diff] [blame] | 166 | if (len == -1) { |
| 167 | PLOG(ERROR) << "Error reading message from browser"; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | |
| 171 | Pickle pickle(buf, len); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 172 | PickleIterator iter(pickle); |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 173 | |
| 174 | int kind; |
[email protected] | 57113ea | 2009-06-18 02:23:16 | [diff] [blame] | 175 | if (pickle.ReadInt(&iter, &kind)) { |
| 176 | switch (kind) { |
[email protected] | c2c68b1f | 2012-02-25 00:29:15 | [diff] [blame] | 177 | case ZygoteHostImpl::kCmdFork: |
[email protected] | c548be2 | 2010-03-08 12:55:57 | [diff] [blame] | 178 | // This function call can return multiple times, once per fork(). |
[email protected] | 57113ea | 2009-06-18 02:23:16 | [diff] [blame] | 179 | return HandleForkRequest(fd, pickle, iter, fds); |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 180 | |
[email protected] | c2c68b1f | 2012-02-25 00:29:15 | [diff] [blame] | 181 | case ZygoteHostImpl::kCmdReap: |
[email protected] | 57113ea | 2009-06-18 02:23:16 | [diff] [blame] | 182 | if (!fds.empty()) |
| 183 | break; |
[email protected] | c548be2 | 2010-03-08 12:55:57 | [diff] [blame] | 184 | HandleReapRequest(fd, pickle, iter); |
| 185 | return false; |
[email protected] | c2c68b1f | 2012-02-25 00:29:15 | [diff] [blame] | 186 | case ZygoteHostImpl::kCmdGetTerminationStatus: |
[email protected] | 57113ea | 2009-06-18 02:23:16 | [diff] [blame] | 187 | if (!fds.empty()) |
| 188 | break; |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 189 | HandleGetTerminationStatus(fd, pickle, iter); |
[email protected] | c548be2 | 2010-03-08 12:55:57 | [diff] [blame] | 190 | return false; |
[email protected] | c2c68b1f | 2012-02-25 00:29:15 | [diff] [blame] | 191 | case ZygoteHostImpl::kCmdGetSandboxStatus: |
[email protected] | 715b4f26 | 2010-07-13 14:17:28 | [diff] [blame] | 192 | HandleGetSandboxStatus(fd, pickle, iter); |
| 193 | return false; |
[email protected] | 57113ea | 2009-06-18 02:23:16 | [diff] [blame] | 194 | default: |
| 195 | NOTREACHED(); |
| 196 | break; |
| 197 | } |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 198 | } |
| 199 | |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 200 | LOG(WARNING) << "Error parsing message from browser"; |
| 201 | for (std::vector<int>::const_iterator |
| 202 | i = fds.begin(); i != fds.end(); ++i) |
| 203 | close(*i); |
| 204 | return false; |
| 205 | } |
| 206 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 207 | void HandleReapRequest(int fd, const Pickle& pickle, PickleIterator iter) { |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 208 | base::ProcessId child; |
| 209 | base::ProcessId actual_child; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 210 | |
| 211 | if (!pickle.ReadInt(&iter, &child)) { |
| 212 | LOG(WARNING) << "Error parsing reap request from browser"; |
[email protected] | c548be2 | 2010-03-08 12:55:57 | [diff] [blame] | 213 | return; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 214 | } |
| 215 | |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 216 | if (g_suid_sandbox_active) { |
| 217 | actual_child = real_pids_to_sandbox_pids[child]; |
| 218 | if (!actual_child) |
[email protected] | c548be2 | 2010-03-08 12:55:57 | [diff] [blame] | 219 | return; |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 220 | real_pids_to_sandbox_pids.erase(child); |
| 221 | } else { |
| 222 | actual_child = child; |
| 223 | } |
| 224 | |
[email protected] | eaac7159 | 2011-11-23 18:32:00 | [diff] [blame] | 225 | base::EnsureProcessTerminated(actual_child); |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 226 | } |
| 227 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 228 | void HandleGetTerminationStatus(int fd, |
| 229 | const Pickle& pickle, |
| 230 | PickleIterator iter) { |
[email protected] | 57113ea | 2009-06-18 02:23:16 | [diff] [blame] | 231 | base::ProcessHandle child; |
| 232 | |
| 233 | if (!pickle.ReadInt(&iter, &child)) { |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 234 | LOG(WARNING) << "Error parsing GetTerminationStatus request " |
| 235 | << "from browser"; |
[email protected] | c548be2 | 2010-03-08 12:55:57 | [diff] [blame] | 236 | return; |
[email protected] | 57113ea | 2009-06-18 02:23:16 | [diff] [blame] | 237 | } |
| 238 | |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 239 | base::TerminationStatus status; |
| 240 | int exit_code; |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 241 | if (g_suid_sandbox_active) |
| 242 | child = real_pids_to_sandbox_pids[child]; |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 243 | if (child) { |
| 244 | status = base::GetTerminationStatus(child, &exit_code); |
| 245 | } else { |
| 246 | // Assume that if we can't find the child in the sandbox, then |
| 247 | // it terminated normally. |
| 248 | status = base::TERMINATION_STATUS_NORMAL_TERMINATION; |
[email protected] | 1fcfb20 | 2011-07-19 19:53:14 | [diff] [blame] | 249 | exit_code = content::RESULT_CODE_NORMAL_EXIT; |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 250 | } |
[email protected] | 57113ea | 2009-06-18 02:23:16 | [diff] [blame] | 251 | |
| 252 | Pickle write_pickle; |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 253 | write_pickle.WriteInt(static_cast<int>(status)); |
| 254 | write_pickle.WriteInt(exit_code); |
[email protected] | 8a86140 | 2011-01-28 19:59:11 | [diff] [blame] | 255 | ssize_t written = |
| 256 | HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size())); |
| 257 | if (written != static_cast<ssize_t>(write_pickle.size())) |
[email protected] | 19cb929 | 2010-04-16 23:00:15 | [diff] [blame] | 258 | PLOG(ERROR) << "write"; |
[email protected] | 57113ea | 2009-06-18 02:23:16 | [diff] [blame] | 259 | } |
| 260 | |
[email protected] | 28ecba3 | 2010-09-16 10:03:09 | [diff] [blame] | 261 | // This is equivalent to fork(), except that, when using the SUID |
| 262 | // sandbox, it returns the real PID of the child process as it |
| 263 | // appears outside the sandbox, rather than returning the PID inside |
[email protected] | 8d5d761 | 2011-10-20 18:23:14 | [diff] [blame] | 264 | // the sandbox. Optionally, it fills in uma_name et al with a report |
| 265 | // the helper wants to make via UMA_HISTOGRAM_ENUMERATION. |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 266 | int ForkWithRealPid(const std::string& process_type, std::vector<int>& fds, |
[email protected] | 8d5d761 | 2011-10-20 18:23:14 | [diff] [blame] | 267 | const std::string& channel_switch, |
| 268 | std::string* uma_name, |
| 269 | int* uma_sample, int* uma_boundary_value) { |
| 270 | const bool use_helper = (helper_ && helper_->CanHelp(process_type, |
| 271 | uma_name, |
| 272 | uma_sample, |
| 273 | uma_boundary_value)); |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 274 | if (!(use_helper || g_suid_sandbox_active)) { |
[email protected] | 28ecba3 | 2010-09-16 10:03:09 | [diff] [blame] | 275 | return fork(); |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 276 | } |
[email protected] | 28ecba3 | 2010-09-16 10:03:09 | [diff] [blame] | 277 | |
| 278 | int dummy_fd; |
| 279 | ino_t dummy_inode; |
| 280 | int pipe_fds[2] = { -1, -1 }; |
[email protected] | 073040b | 2010-09-26 02:35:45 | [diff] [blame] | 281 | base::ProcessId pid = 0; |
[email protected] | 28ecba3 | 2010-09-16 10:03:09 | [diff] [blame] | 282 | |
| 283 | dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0); |
| 284 | if (dummy_fd < 0) { |
| 285 | LOG(ERROR) << "Failed to create dummy FD"; |
| 286 | goto error; |
| 287 | } |
| 288 | if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd)) { |
| 289 | LOG(ERROR) << "Failed to get inode for dummy FD"; |
| 290 | goto error; |
| 291 | } |
| 292 | if (pipe(pipe_fds) != 0) { |
| 293 | LOG(ERROR) << "Failed to create pipe"; |
| 294 | goto error; |
| 295 | } |
| 296 | |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 297 | if (use_helper) { |
| 298 | fds.push_back(dummy_fd); |
| 299 | fds.push_back(pipe_fds[0]); |
| 300 | pid = helper_->Fork(fds); |
| 301 | } else { |
| 302 | pid = fork(); |
| 303 | } |
[email protected] | 28ecba3 | 2010-09-16 10:03:09 | [diff] [blame] | 304 | if (pid < 0) { |
| 305 | goto error; |
| 306 | } else if (pid == 0) { |
| 307 | // In the child process. |
| 308 | close(pipe_fds[1]); |
[email protected] | e1d67a88 | 2011-08-31 21:11:04 | [diff] [blame] | 309 | base::ProcessId real_pid; |
[email protected] | 28ecba3 | 2010-09-16 10:03:09 | [diff] [blame] | 310 | // Wait until the parent process has discovered our PID. We |
| 311 | // should not fork any child processes (which the seccomp |
| 312 | // sandbox does) until then, because that can interfere with the |
| 313 | // parent's discovery of our PID. |
[email protected] | e1d67a88 | 2011-08-31 21:11:04 | [diff] [blame] | 314 | if (!file_util::ReadFromFD(pipe_fds[0], |
| 315 | reinterpret_cast<char*>(&real_pid), |
| 316 | sizeof(real_pid))) { |
[email protected] | 28ecba3 | 2010-09-16 10:03:09 | [diff] [blame] | 317 | LOG(FATAL) << "Failed to synchronise with parent zygote process"; |
| 318 | } |
[email protected] | e1d67a88 | 2011-08-31 21:11:04 | [diff] [blame] | 319 | if (real_pid <= 0) { |
| 320 | LOG(FATAL) << "Invalid pid from parent zygote"; |
| 321 | } |
| 322 | #if defined(OS_LINUX) |
| 323 | // Sandboxed processes need to send the global, non-namespaced PID when |
| 324 | // setting up an IPC channel to their parent. |
| 325 | IPC::Channel::SetGlobalPid(real_pid); |
| 326 | #endif |
[email protected] | 28ecba3 | 2010-09-16 10:03:09 | [diff] [blame] | 327 | close(pipe_fds[0]); |
| 328 | close(dummy_fd); |
| 329 | return 0; |
| 330 | } else { |
| 331 | // In the parent process. |
| 332 | close(dummy_fd); |
| 333 | dummy_fd = -1; |
| 334 | close(pipe_fds[0]); |
| 335 | pipe_fds[0] = -1; |
[email protected] | 4229b5d | 2011-07-16 02:47:37 | [diff] [blame] | 336 | base::ProcessId real_pid; |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 337 | if (g_suid_sandbox_active) { |
| 338 | uint8_t reply_buf[512]; |
| 339 | Pickle request; |
| 340 | request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE); |
| 341 | request.WriteUInt64(dummy_inode); |
| 342 | |
| 343 | const ssize_t r = UnixDomainSocket::SendRecvMsg( |
| 344 | kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, |
| 345 | request); |
| 346 | if (r == -1) { |
| 347 | LOG(ERROR) << "Failed to get child process's real PID"; |
| 348 | goto error; |
| 349 | } |
| 350 | |
| 351 | Pickle reply(reinterpret_cast<char*>(reply_buf), r); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 352 | PickleIterator iter(reply); |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 353 | if (!reply.ReadInt(&iter, &real_pid)) |
| 354 | goto error; |
| 355 | if (real_pid <= 0) { |
| 356 | // METHOD_GET_CHILD_WITH_INODE failed. Did the child die already? |
| 357 | LOG(ERROR) << "METHOD_GET_CHILD_WITH_INODE failed"; |
| 358 | goto error; |
| 359 | } |
| 360 | real_pids_to_sandbox_pids[real_pid] = pid; |
[email protected] | 4229b5d | 2011-07-16 02:47:37 | [diff] [blame] | 361 | } |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 362 | if (use_helper) { |
| 363 | real_pid = pid; |
| 364 | if (!helper_->AckChild(pipe_fds[1], channel_switch)) { |
| 365 | LOG(ERROR) << "Failed to synchronise with zygote fork helper"; |
| 366 | goto error; |
| 367 | } |
| 368 | } else { |
[email protected] | e1d67a88 | 2011-08-31 21:11:04 | [diff] [blame] | 369 | int written = |
| 370 | HANDLE_EINTR(write(pipe_fds[1], &real_pid, sizeof(real_pid))); |
| 371 | if (written != sizeof(real_pid)) { |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 372 | LOG(ERROR) << "Failed to synchronise with child process"; |
| 373 | goto error; |
| 374 | } |
[email protected] | 28ecba3 | 2010-09-16 10:03:09 | [diff] [blame] | 375 | } |
| 376 | close(pipe_fds[1]); |
| 377 | return real_pid; |
| 378 | } |
| 379 | |
| 380 | error: |
[email protected] | 72f891f4 | 2011-01-12 17:00:52 | [diff] [blame] | 381 | if (pid > 0) { |
| 382 | if (waitpid(pid, NULL, WNOHANG) == -1) |
| 383 | LOG(ERROR) << "Failed to wait for process"; |
| 384 | } |
[email protected] | 28ecba3 | 2010-09-16 10:03:09 | [diff] [blame] | 385 | if (dummy_fd >= 0) |
| 386 | close(dummy_fd); |
| 387 | if (pipe_fds[0] >= 0) |
| 388 | close(pipe_fds[0]); |
| 389 | if (pipe_fds[1] >= 0) |
| 390 | close(pipe_fds[1]); |
| 391 | return -1; |
| 392 | } |
| 393 | |
[email protected] | 35d8624 | 2011-09-30 15:29:08 | [diff] [blame] | 394 | // Unpacks process type and arguments from |pickle| and forks a new process. |
| 395 | // Returns -1 on error, otherwise returns twice, returning 0 to the child |
| 396 | // process and the child process ID to the parent process, like fork(). |
| 397 | base::ProcessId ReadArgsAndFork(const Pickle& pickle, |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 398 | PickleIterator iter, |
[email protected] | 8d5d761 | 2011-10-20 18:23:14 | [diff] [blame] | 399 | std::vector<int>& fds, |
| 400 | std::string* uma_name, |
| 401 | int* uma_sample, |
| 402 | int* uma_boundary_value) { |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 403 | std::vector<std::string> args; |
[email protected] | 35d8624 | 2011-09-30 15:29:08 | [diff] [blame] | 404 | int argc = 0; |
| 405 | int numfds = 0; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 406 | base::GlobalDescriptors::Mapping mapping; |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 407 | std::string process_type; |
| 408 | std::string channel_id; |
| 409 | const std::string channel_id_prefix = std::string("--") |
| 410 | + switches::kProcessChannelID + std::string("="); |
| 411 | |
| 412 | if (!pickle.ReadString(&iter, &process_type)) |
[email protected] | 35d8624 | 2011-09-30 15:29:08 | [diff] [blame] | 413 | return -1; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 414 | if (!pickle.ReadInt(&iter, &argc)) |
[email protected] | 35d8624 | 2011-09-30 15:29:08 | [diff] [blame] | 415 | return -1; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 416 | |
| 417 | for (int i = 0; i < argc; ++i) { |
| 418 | std::string arg; |
| 419 | if (!pickle.ReadString(&iter, &arg)) |
[email protected] | 35d8624 | 2011-09-30 15:29:08 | [diff] [blame] | 420 | return -1; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 421 | args.push_back(arg); |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 422 | if (arg.compare(0, channel_id_prefix.length(), channel_id_prefix) == 0) |
| 423 | channel_id = arg; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | if (!pickle.ReadInt(&iter, &numfds)) |
[email protected] | 35d8624 | 2011-09-30 15:29:08 | [diff] [blame] | 427 | return -1; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 428 | if (numfds != static_cast<int>(fds.size())) |
[email protected] | 35d8624 | 2011-09-30 15:29:08 | [diff] [blame] | 429 | return -1; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 430 | |
| 431 | for (int i = 0; i < numfds; ++i) { |
| 432 | base::GlobalDescriptors::Key key; |
| 433 | if (!pickle.ReadUInt32(&iter, &key)) |
[email protected] | 35d8624 | 2011-09-30 15:29:08 | [diff] [blame] | 434 | return -1; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 435 | mapping.push_back(std::make_pair(key, fds[i])); |
| 436 | } |
| 437 | |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 438 | mapping.push_back(std::make_pair( |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 439 | static_cast<uint32_t>(kSandboxIPCChannel), kMagicSandboxIPCDescriptor)); |
| 440 | |
[email protected] | 35d8624 | 2011-09-30 15:29:08 | [diff] [blame] | 441 | // Returns twice, once per process. |
[email protected] | 8d5d761 | 2011-10-20 18:23:14 | [diff] [blame] | 442 | base::ProcessId child_pid = ForkWithRealPid(process_type, fds, channel_id, |
| 443 | uma_name, uma_sample, |
| 444 | uma_boundary_value); |
[email protected] | 35d8624 | 2011-09-30 15:29:08 | [diff] [blame] | 445 | if (!child_pid) { |
| 446 | // This is the child process. |
[email protected] | 36ea6c6f | 2010-03-17 20:08:01 | [diff] [blame] | 447 | #if defined(SECCOMP_SANDBOX) |
[email protected] | 65f607f | 2011-08-03 21:08:12 | [diff] [blame] | 448 | if (SeccompSandboxEnabled() && g_proc_fd >= 0) { |
[email protected] | 2bc039a | 2011-07-29 23:05:41 | [diff] [blame] | 449 | // Try to open /proc/self/maps as the seccomp sandbox needs access to it |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 450 | int proc_self_maps = openat(g_proc_fd, "self/maps", O_RDONLY); |
| 451 | if (proc_self_maps >= 0) { |
| 452 | SeccompSandboxSetProcSelfMaps(proc_self_maps); |
[email protected] | 2bc039a | 2011-07-29 23:05:41 | [diff] [blame] | 453 | } else { |
| 454 | PLOG(ERROR) << "openat(/proc/self/maps)"; |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 455 | } |
| 456 | close(g_proc_fd); |
| 457 | g_proc_fd = -1; |
| 458 | } |
[email protected] | 8015de3 | 2009-11-18 04:13:33 | [diff] [blame] | 459 | #endif |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 460 | |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 461 | close(kBrowserDescriptor); // our socket from the browser |
[email protected] | cbcf9cc3 | 2010-02-17 04:05:59 | [diff] [blame] | 462 | if (g_suid_sandbox_active) |
| 463 | close(kZygoteIdDescriptor); // another socket from the browser |
[email protected] | 9b85081af | 2010-12-07 21:26:47 | [diff] [blame] | 464 | base::GlobalDescriptors::GetInstance()->Reset(mapping); |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 465 | |
[email protected] | a89a55dd | 2010-04-19 14:51:13 | [diff] [blame] | 466 | #if defined(CHROMIUM_SELINUX) |
| 467 | SELinuxTransitionToTypeOrDie("chromium_renderer_t"); |
| 468 | #endif |
| 469 | |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 470 | // Reset the process-wide command line to our new command line. |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 471 | CommandLine::Reset(); |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 472 | CommandLine::Init(0, NULL); |
| 473 | CommandLine::ForCurrentProcess()->InitFromArgv(args); |
[email protected] | 74e9fa2 | 2010-12-29 21:06:43 | [diff] [blame] | 474 | |
| 475 | // Update the process title. The argv was already cached by the call to |
| 476 | // SetProcessTitleFromCommandLine in ChromeMain, so we can pass NULL here |
| 477 | // (we don't have the original argv at this point). |
| 478 | SetProcessTitleFromCommandLine(NULL); |
[email protected] | 35d8624 | 2011-09-30 15:29:08 | [diff] [blame] | 479 | } else if (child_pid < 0) { |
| 480 | LOG(ERROR) << "Zygote could not fork: process_type " << process_type |
| 481 | << " numfds " << numfds << " child_pid " << child_pid; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 482 | } |
[email protected] | 35d8624 | 2011-09-30 15:29:08 | [diff] [blame] | 483 | return child_pid; |
| 484 | } |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 485 | |
[email protected] | 35d8624 | 2011-09-30 15:29:08 | [diff] [blame] | 486 | // Handle a 'fork' request from the browser: this means that the browser |
| 487 | // wishes to start a new renderer. Returns true if we are in a new process, |
| 488 | // otherwise writes the child_pid back to the browser via |fd|. Writes a |
| 489 | // child_pid of -1 on error. |
| 490 | bool HandleForkRequest(int fd, const Pickle& pickle, |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 491 | PickleIterator iter, std::vector<int>& fds) { |
[email protected] | 8d5d761 | 2011-10-20 18:23:14 | [diff] [blame] | 492 | std::string uma_name; |
| 493 | int uma_sample; |
| 494 | int uma_boundary_value; |
| 495 | base::ProcessId child_pid = ReadArgsAndFork(pickle, iter, fds, |
| 496 | &uma_name, &uma_sample, |
| 497 | &uma_boundary_value); |
[email protected] | 35d8624 | 2011-09-30 15:29:08 | [diff] [blame] | 498 | if (child_pid == 0) |
| 499 | return true; |
[email protected] | 28ecba3 | 2010-09-16 10:03:09 | [diff] [blame] | 500 | for (std::vector<int>::const_iterator |
| 501 | i = fds.begin(); i != fds.end(); ++i) |
| 502 | close(*i); |
[email protected] | 8d5d761 | 2011-10-20 18:23:14 | [diff] [blame] | 503 | if (uma_name.empty()) { |
| 504 | // There is no UMA report from this particular fork. |
| 505 | // Use the initial UMA report if any, and clear that record for next time. |
| 506 | // Note the swap method here is the efficient way to do this, since |
| 507 | // we know uma_name is empty. |
| 508 | uma_name.swap(initial_uma_name_); |
| 509 | uma_sample = initial_uma_sample_; |
| 510 | uma_boundary_value = initial_uma_boundary_value_; |
| 511 | } |
[email protected] | 35d8624 | 2011-09-30 15:29:08 | [diff] [blame] | 512 | // Must always send reply, as ZygoteHost blocks while waiting for it. |
[email protected] | 8d5d761 | 2011-10-20 18:23:14 | [diff] [blame] | 513 | Pickle reply_pickle; |
| 514 | reply_pickle.WriteInt(child_pid); |
| 515 | reply_pickle.WriteString(uma_name); |
| 516 | if (!uma_name.empty()) { |
| 517 | reply_pickle.WriteInt(uma_sample); |
| 518 | reply_pickle.WriteInt(uma_boundary_value); |
| 519 | } |
| 520 | if (HANDLE_EINTR(write(fd, reply_pickle.data(), reply_pickle.size())) != |
| 521 | static_cast<ssize_t> (reply_pickle.size())) |
[email protected] | 28ecba3 | 2010-09-16 10:03:09 | [diff] [blame] | 522 | PLOG(ERROR) << "write"; |
| 523 | return false; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 524 | } |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 525 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 526 | bool HandleGetSandboxStatus(int fd, |
| 527 | const Pickle& pickle, |
| 528 | PickleIterator iter) { |
[email protected] | 715b4f26 | 2010-07-13 14:17:28 | [diff] [blame] | 529 | if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_)) != |
| 530 | sizeof(sandbox_flags_))) { |
| 531 | PLOG(ERROR) << "write"; |
| 532 | } |
| 533 | |
| 534 | return false; |
| 535 | } |
| 536 | |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 537 | // In the SUID sandbox, we try to use a new PID namespace. Thus the PIDs |
| 538 | // fork() returns are not the real PIDs, so we need to map the Real PIDS |
| 539 | // into the sandbox PID namespace. |
| 540 | typedef base::hash_map<base::ProcessHandle, base::ProcessHandle> ProcessMap; |
| 541 | ProcessMap real_pids_to_sandbox_pids; |
[email protected] | 715b4f26 | 2010-07-13 14:17:28 | [diff] [blame] | 542 | |
| 543 | const int sandbox_flags_; |
[email protected] | f8ba13d | 2011-10-25 16:08:52 | [diff] [blame] | 544 | content::ZygoteForkDelegate* helper_; |
[email protected] | 8d5d761 | 2011-10-20 18:23:14 | [diff] [blame] | 545 | |
| 546 | // These might be set by helper_->InitialUMA. They supply a UMA |
| 547 | // enumeration sample we should report on the first fork. |
| 548 | std::string initial_uma_name_; |
| 549 | int initial_uma_sample_; |
| 550 | int initial_uma_boundary_value_; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 551 | }; |
| 552 | |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 553 | // With SELinux we can carve out a precise sandbox, so we don't have to play |
| 554 | // with intercepting libc calls. |
| 555 | #if !defined(CHROMIUM_SELINUX) |
| 556 | |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 557 | static void ProxyLocaltimeCallToBrowser(time_t input, struct tm* output, |
| 558 | char* timezone_out, |
| 559 | size_t timezone_out_len) { |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 560 | Pickle request; |
| 561 | request.WriteInt(LinuxSandbox::METHOD_LOCALTIME); |
| 562 | request.WriteString( |
| 563 | std::string(reinterpret_cast<char*>(&input), sizeof(input))); |
| 564 | |
| 565 | uint8_t reply_buf[512]; |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 566 | const ssize_t r = UnixDomainSocket::SendRecvMsg( |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 567 | kMagicSandboxIPCDescriptor, reply_buf, sizeof(reply_buf), NULL, request); |
| 568 | if (r == -1) { |
| 569 | memset(output, 0, sizeof(struct tm)); |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | Pickle reply(reinterpret_cast<char*>(reply_buf), r); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 574 | PickleIterator iter(reply); |
[email protected] | 9debcda5 | 2009-07-22 20:06:51 | [diff] [blame] | 575 | std::string result, timezone; |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 576 | if (!reply.ReadString(&iter, &result) || |
[email protected] | 9debcda5 | 2009-07-22 20:06:51 | [diff] [blame] | 577 | !reply.ReadString(&iter, &timezone) || |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 578 | result.size() != sizeof(struct tm)) { |
| 579 | memset(output, 0, sizeof(struct tm)); |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | memcpy(output, result.data(), sizeof(struct tm)); |
[email protected] | 9debcda5 | 2009-07-22 20:06:51 | [diff] [blame] | 584 | if (timezone_out_len) { |
| 585 | const size_t copy_len = std::min(timezone_out_len - 1, timezone.size()); |
| 586 | memcpy(timezone_out, timezone.data(), copy_len); |
| 587 | timezone_out[copy_len] = 0; |
| 588 | output->tm_zone = timezone_out; |
| 589 | } else { |
| 590 | output->tm_zone = NULL; |
| 591 | } |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 592 | } |
| 593 | |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 594 | static bool g_am_zygote_or_renderer = false; |
| 595 | |
| 596 | // Sandbox interception of libc calls. |
| 597 | // |
| 598 | // Because we are running in a sandbox certain libc calls will fail (localtime |
| 599 | // being the motivating example - it needs to read /etc/localtime). We need to |
| 600 | // intercept these calls and proxy them to the browser. However, these calls |
| 601 | // may come from us or from our libraries. In some cases we can't just change |
| 602 | // our code. |
| 603 | // |
| 604 | // It's for these cases that we have the following setup: |
| 605 | // |
| 606 | // We define global functions for those functions which we wish to override. |
| 607 | // Since we will be first in the dynamic resolution order, the dynamic linker |
| 608 | // will point callers to our versions of these functions. However, we have the |
| 609 | // same binary for both the browser and the renderers, which means that our |
| 610 | // overrides will apply in the browser too. |
| 611 | // |
| 612 | // The global |g_am_zygote_or_renderer| is true iff we are in a zygote or |
| 613 | // renderer process. It's set in ZygoteMain and inherited by the renderers when |
| 614 | // they fork. (This means that it'll be incorrect for global constructor |
| 615 | // functions and before ZygoteMain is called - beware). |
| 616 | // |
| 617 | // Our replacement functions can check this global and either proxy |
| 618 | // the call to the browser over the sandbox IPC |
| 619 | // (http://code.google.com/p/chromium/wiki/LinuxSandboxIPC) or they can use |
| 620 | // dlsym with RTLD_NEXT to resolve the symbol, ignoring any symbols in the |
| 621 | // current module. |
| 622 | // |
| 623 | // Other avenues: |
| 624 | // |
| 625 | // Our first attempt involved some assembly to patch the GOT of the current |
| 626 | // module. This worked, but was platform specific and doesn't catch the case |
| 627 | // where a library makes a call rather than current module. |
| 628 | // |
| 629 | // We also considered patching the function in place, but this would again by |
| 630 | // platform specific and the above technique seems to work well enough. |
| 631 | |
[email protected] | 724df99 | 2010-09-21 18:19:10 | [diff] [blame] | 632 | typedef struct tm* (*LocaltimeFunction)(const time_t* timep); |
| 633 | typedef struct tm* (*LocaltimeRFunction)(const time_t* timep, |
| 634 | struct tm* result); |
| 635 | |
| 636 | static pthread_once_t g_libc_localtime_funcs_guard = PTHREAD_ONCE_INIT; |
| 637 | static LocaltimeFunction g_libc_localtime; |
| 638 | static LocaltimeRFunction g_libc_localtime_r; |
| 639 | |
| 640 | static void InitLibcLocaltimeFunctions() { |
| 641 | g_libc_localtime = reinterpret_cast<LocaltimeFunction>( |
| 642 | dlsym(RTLD_NEXT, "localtime")); |
| 643 | g_libc_localtime_r = reinterpret_cast<LocaltimeRFunction>( |
| 644 | dlsym(RTLD_NEXT, "localtime_r")); |
| 645 | |
| 646 | if (!g_libc_localtime || !g_libc_localtime_r) { |
| 647 | // http://code.google.com/p/chromium/issues/detail?id=16800 |
| 648 | // |
| 649 | // Nvidia's libGL.so overrides dlsym for an unknown reason and replaces |
| 650 | // it with a version which doesn't work. In this case we'll get a NULL |
| 651 | // result. There's not a lot we can do at this point, so we just bodge it! |
| 652 | LOG(ERROR) << "Your system is broken: dlsym doesn't work! This has been " |
| 653 | "reported to be caused by Nvidia's libGL. You should expect" |
| 654 | " time related functions to misbehave. " |
| 655 | "http://code.google.com/p/chromium/issues/detail?id=16800"; |
| 656 | } |
| 657 | |
| 658 | if (!g_libc_localtime) |
| 659 | g_libc_localtime = gmtime; |
| 660 | if (!g_libc_localtime_r) |
| 661 | g_libc_localtime_r = gmtime_r; |
| 662 | } |
[email protected] | a5d7bb8 | 2009-09-08 22:30:58 | [diff] [blame] | 663 | |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 664 | struct tm* localtime(const time_t* timep) { |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 665 | if (g_am_zygote_or_renderer) { |
| 666 | static struct tm time_struct; |
| 667 | static char timezone_string[64]; |
| 668 | ProxyLocaltimeCallToBrowser(*timep, &time_struct, timezone_string, |
| 669 | sizeof(timezone_string)); |
| 670 | return &time_struct; |
| 671 | } else { |
[email protected] | 724df99 | 2010-09-21 18:19:10 | [diff] [blame] | 672 | CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard, |
| 673 | InitLibcLocaltimeFunctions)); |
| 674 | return g_libc_localtime(timep); |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 675 | } |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | struct tm* localtime_r(const time_t* timep, struct tm* result) { |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 679 | if (g_am_zygote_or_renderer) { |
| 680 | ProxyLocaltimeCallToBrowser(*timep, result, NULL, 0); |
| 681 | return result; |
| 682 | } else { |
[email protected] | 724df99 | 2010-09-21 18:19:10 | [diff] [blame] | 683 | CHECK_EQ(0, pthread_once(&g_libc_localtime_funcs_guard, |
| 684 | InitLibcLocaltimeFunctions)); |
| 685 | return g_libc_localtime_r(timep, result); |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 686 | } |
[email protected] | 73fa6399 | 2009-07-20 20:30:07 | [diff] [blame] | 687 | } |
| 688 | |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 689 | #endif // !CHROMIUM_SELINUX |
[email protected] | a5d7bb8 | 2009-09-08 22:30:58 | [diff] [blame] | 690 | |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 691 | // This function triggers the static and lazy construction of objects that need |
| 692 | // to be created before imposing the sandbox. |
| 693 | static void PreSandboxInit() { |
[email protected] | b841941 | 2010-03-29 20:18:29 | [diff] [blame] | 694 | base::RandUint64(); |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 695 | |
[email protected] | b841941 | 2010-03-29 20:18:29 | [diff] [blame] | 696 | base::SysInfo::MaxSharedMemorySize(); |
[email protected] | 80a086c5 | 2009-08-04 17:52:04 | [diff] [blame] | 697 | |
[email protected] | b841941 | 2010-03-29 20:18:29 | [diff] [blame] | 698 | // ICU DateFormat class (used in base/time_format.cc) needs to get the |
| 699 | // Olson timezone ID by accessing the zoneinfo files on disk. After |
| 700 | // TimeZone::createDefault is called once here, the timezone ID is |
| 701 | // cached and there's no more need to access the file system. |
| 702 | scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); |
[email protected] | 1831acf | 2009-10-05 16:38:41 | [diff] [blame] | 703 | |
[email protected] | eeb0855 | 2011-03-18 11:15:39 | [diff] [blame] | 704 | #if defined(USE_NSS) |
[email protected] | f6a67b4 | 2011-03-17 23:49:21 | [diff] [blame] | 705 | // NSS libraries are loaded before sandbox is activated. This is to allow |
| 706 | // successful initialization of NSS which tries to load extra library files. |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 707 | crypto::LoadNSSLibraries(); |
[email protected] | 1f554845 | 2011-10-06 06:46:15 | [diff] [blame] | 708 | #elif defined(USE_OPENSSL) |
| 709 | // OpenSSL is intentionally not supported in the sandboxed processes, see |
| 710 | // http://crbug.com/99163. If that ever changes we'll likely need to init |
| 711 | // OpenSSL here (at least, load the library and error strings). |
[email protected] | eeb0855 | 2011-03-18 11:15:39 | [diff] [blame] | 712 | #else |
[email protected] | 1f554845 | 2011-10-06 06:46:15 | [diff] [blame] | 713 | // It's possible that another hypothetical crypto stack would not require |
| 714 | // pre-sandbox init, but more likely this is just a build configuration error. |
| 715 | #error Which SSL library are you using? |
[email protected] | f6a67b4 | 2011-03-17 23:49:21 | [diff] [blame] | 716 | #endif |
[email protected] | ed450f3 | 2011-03-16 01:26:49 | [diff] [blame] | 717 | |
[email protected] | 1777304 | 2010-07-28 17:35:07 | [diff] [blame] | 718 | // Ensure access to the Pepper plugins before the sandbox is turned on. |
| 719 | PepperPluginRegistry::PreloadModules(); |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | #if !defined(CHROMIUM_SELINUX) |
| 723 | static bool EnterSandbox() { |
[email protected] | b841941 | 2010-03-29 20:18:29 | [diff] [blame] | 724 | // The SUID sandbox sets this environment variable to a file descriptor |
| 725 | // over which we can signal that we have completed our startup and can be |
| 726 | // chrooted. |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 727 | const char* const sandbox_fd_string = getenv("SBX_D"); |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 728 | |
[email protected] | 0dc3232 | 2010-09-16 09:46:59 | [diff] [blame] | 729 | if (sandbox_fd_string) { |
| 730 | // Use the SUID sandbox. This still allows the seccomp sandbox to |
| 731 | // be enabled by the process later. |
[email protected] | 8ecd3aad | 2009-11-04 08:32:22 | [diff] [blame] | 732 | g_suid_sandbox_active = true; |
| 733 | |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 734 | char* endptr; |
| 735 | const long fd_long = strtol(sandbox_fd_string, &endptr, 10); |
| 736 | if (!*sandbox_fd_string || *endptr || fd_long < 0 || fd_long > INT_MAX) |
| 737 | return false; |
| 738 | const int fd = fd_long; |
| 739 | |
| 740 | PreSandboxInit(); |
[email protected] | c9e45da0 | 2009-08-05 17:35:08 | [diff] [blame] | 741 | |
[email protected] | eaebefaf | 2010-02-23 22:58:18 | [diff] [blame] | 742 | static const char kMsgChrootMe = 'C'; |
| 743 | static const char kMsgChrootSuccessful = 'O'; |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 744 | |
[email protected] | eaebefaf | 2010-02-23 22:58:18 | [diff] [blame] | 745 | if (HANDLE_EINTR(write(fd, &kMsgChrootMe, 1)) != 1) { |
[email protected] | 0e161112 | 2009-07-10 18:17:32 | [diff] [blame] | 746 | LOG(ERROR) << "Failed to write to chroot pipe: " << errno; |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 747 | return false; |
[email protected] | 0e161112 | 2009-07-10 18:17:32 | [diff] [blame] | 748 | } |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 749 | |
[email protected] | 87ed695 | 2009-07-16 02:52:15 | [diff] [blame] | 750 | // We need to reap the chroot helper process in any event: |
| 751 | wait(NULL); |
| 752 | |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 753 | char reply; |
[email protected] | 87f8ce6 | 2009-07-10 19:14:31 | [diff] [blame] | 754 | if (HANDLE_EINTR(read(fd, &reply, 1)) != 1) { |
[email protected] | 0e161112 | 2009-07-10 18:17:32 | [diff] [blame] | 755 | LOG(ERROR) << "Failed to read from chroot pipe: " << errno; |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 756 | return false; |
[email protected] | 0e161112 | 2009-07-10 18:17:32 | [diff] [blame] | 757 | } |
[email protected] | 87f8ce6 | 2009-07-10 19:14:31 | [diff] [blame] | 758 | |
[email protected] | eaebefaf | 2010-02-23 22:58:18 | [diff] [blame] | 759 | if (reply != kMsgChrootSuccessful) { |
[email protected] | 0e161112 | 2009-07-10 18:17:32 | [diff] [blame] | 760 | LOG(ERROR) << "Error code reply from chroot helper"; |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 761 | return false; |
[email protected] | 0e161112 | 2009-07-10 18:17:32 | [diff] [blame] | 762 | } |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 763 | |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 764 | SkiaFontConfigSetImplementation( |
| 765 | new FontConfigIPC(kMagicSandboxIPCDescriptor)); |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 766 | |
[email protected] | e60c023 | 2011-11-11 19:56:35 | [diff] [blame] | 767 | #if !defined(OS_OPENBSD) |
[email protected] | 415493be | 2009-07-10 17:50:24 | [diff] [blame] | 768 | // Previously, we required that the binary be non-readable. This causes the |
| 769 | // kernel to mark the process as non-dumpable at startup. The thinking was |
| 770 | // that, although we were putting the renderers into a PID namespace (with |
| 771 | // the SUID sandbox), they would nonetheless be in the /same/ PID |
| 772 | // namespace. So they could ptrace each other unless they were non-dumpable. |
| 773 | // |
| 774 | // If the binary was readable, then there would be a window between process |
| 775 | // startup and the point where we set the non-dumpable flag in which a |
| 776 | // compromised renderer could ptrace attach. |
| 777 | // |
| 778 | // However, now that we have a zygote model, only the (trusted) zygote |
| 779 | // exists at this point and we can set the non-dumpable flag which is |
| 780 | // inherited by all our renderer children. |
[email protected] | 4730db9 | 2009-07-22 00:40:48 | [diff] [blame] | 781 | // |
| 782 | // Note: a non-dumpable process can't be debugged. To debug sandbox-related |
| 783 | // issues, one can specify --allow-sandbox-debugging to let the process be |
| 784 | // dumpable. |
| 785 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 786 | if (!command_line.HasSwitch(switches::kAllowSandboxDebugging)) { |
| 787 | prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); |
| 788 | if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { |
| 789 | LOG(ERROR) << "Failed to set non-dumpable flag"; |
| 790 | return false; |
| 791 | } |
[email protected] | 0e161112 | 2009-07-10 18:17:32 | [diff] [blame] | 792 | } |
[email protected] | e60c023 | 2011-11-11 19:56:35 | [diff] [blame] | 793 | #endif |
[email protected] | 2bc039a | 2011-07-29 23:05:41 | [diff] [blame] | 794 | #if defined(SECCOMP_SANDBOX) |
[email protected] | 65f607f | 2011-08-03 21:08:12 | [diff] [blame] | 795 | } else if (SeccompSandboxEnabled()) { |
[email protected] | 0dc3232 | 2010-09-16 09:46:59 | [diff] [blame] | 796 | PreSandboxInit(); |
[email protected] | cf3ac397 | 2010-12-22 20:02:29 | [diff] [blame] | 797 | SkiaFontConfigSetImplementation( |
| 798 | new FontConfigIPC(kMagicSandboxIPCDescriptor)); |
[email protected] | 2bc039a | 2011-07-29 23:05:41 | [diff] [blame] | 799 | #endif |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 800 | } else { |
| 801 | SkiaFontConfigUseDirectImplementation(); |
| 802 | } |
| 803 | |
| 804 | return true; |
| 805 | } |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 806 | #else // CHROMIUM_SELINUX |
| 807 | |
| 808 | static bool EnterSandbox() { |
| 809 | PreSandboxInit(); |
| 810 | SkiaFontConfigUseIPCImplementation(kMagicSandboxIPCDescriptor); |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 811 | return true; |
| 812 | } |
| 813 | |
| 814 | #endif // CHROMIUM_SELINUX |
[email protected] | abe3ad9 | 2009-06-15 18:15:08 | [diff] [blame] | 815 | |
[email protected] | 4573fbd | 2011-10-31 20:25:18 | [diff] [blame] | 816 | bool ZygoteMain(const content::MainFunctionParams& params, |
[email protected] | f8ba13d | 2011-10-25 16:08:52 | [diff] [blame] | 817 | content::ZygoteForkDelegate* forkdelegate) { |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 818 | #if !defined(CHROMIUM_SELINUX) |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 819 | g_am_zygote_or_renderer = true; |
[email protected] | ad6d2c4 | 2009-09-15 20:13:38 | [diff] [blame] | 820 | #endif |
[email protected] | a0f200ea | 2009-08-06 18:44:06 | [diff] [blame] | 821 | |
[email protected] | 36ea6c6f | 2010-03-17 20:08:01 | [diff] [blame] | 822 | #if defined(SECCOMP_SANDBOX) |
[email protected] | 65f607f | 2011-08-03 21:08:12 | [diff] [blame] | 823 | if (SeccompSandboxEnabled()) { |
[email protected] | 2bc039a | 2011-07-29 23:05:41 | [diff] [blame] | 824 | // The seccomp sandbox needs access to files in /proc, which might be denied |
| 825 | // after one of the other sandboxes have been started. So, obtain a suitable |
| 826 | // file handle in advance. |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 827 | g_proc_fd = open("/proc", O_DIRECTORY | O_RDONLY); |
| 828 | if (g_proc_fd < 0) { |
| 829 | LOG(ERROR) << "WARNING! Cannot access \"/proc\". Disabling seccomp " |
[email protected] | 2bc039a | 2011-07-29 23:05:41 | [diff] [blame] | 830 | "sandboxing."; |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 831 | } |
| 832 | } |
[email protected] | 36ea6c6f | 2010-03-17 20:08:01 | [diff] [blame] | 833 | #endif // SECCOMP_SANDBOX |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 834 | |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 835 | if (forkdelegate != NULL) { |
| 836 | VLOG(1) << "ZygoteMain: initializing fork delegate"; |
| 837 | forkdelegate->Init(getenv("SBX_D") != NULL, // g_suid_sandbox_active, |
| 838 | kBrowserDescriptor, |
| 839 | kMagicSandboxIPCDescriptor); |
| 840 | } else { |
| 841 | VLOG(1) << "ZygoteMain: fork delegate is NULL"; |
| 842 | } |
| 843 | |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 844 | // Turn on the SELinux or SUID sandbox |
| 845 | if (!EnterSandbox()) { |
| 846 | LOG(FATAL) << "Failed to enter sandbox. Fail safe abort. (errno: " |
| 847 | << errno << ")"; |
| 848 | return false; |
| 849 | } |
| 850 | |
[email protected] | 715b4f26 | 2010-07-13 14:17:28 | [diff] [blame] | 851 | int sandbox_flags = 0; |
| 852 | if (getenv("SBX_D")) |
[email protected] | c2c68b1f | 2012-02-25 00:29:15 | [diff] [blame] | 853 | sandbox_flags |= ZygoteHostImpl::kSandboxSUID; |
[email protected] | 715b4f26 | 2010-07-13 14:17:28 | [diff] [blame] | 854 | if (getenv("SBX_PID_NS")) |
[email protected] | c2c68b1f | 2012-02-25 00:29:15 | [diff] [blame] | 855 | sandbox_flags |= ZygoteHostImpl::kSandboxPIDNS; |
[email protected] | 715b4f26 | 2010-07-13 14:17:28 | [diff] [blame] | 856 | if (getenv("SBX_NET_NS")) |
[email protected] | c2c68b1f | 2012-02-25 00:29:15 | [diff] [blame] | 857 | sandbox_flags |= ZygoteHostImpl::kSandboxNetNS; |
[email protected] | 715b4f26 | 2010-07-13 14:17:28 | [diff] [blame] | 858 | |
[email protected] | 36ea6c6f | 2010-03-17 20:08:01 | [diff] [blame] | 859 | #if defined(SECCOMP_SANDBOX) |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 860 | // The seccomp sandbox will be turned on when the renderers start. But we can |
| 861 | // already check if sufficient support is available so that we only need to |
| 862 | // print one error message for the entire browser session. |
[email protected] | 65f607f | 2011-08-03 21:08:12 | [diff] [blame] | 863 | if (g_proc_fd >= 0 && SeccompSandboxEnabled()) { |
[email protected] | a9c54a17 | 2009-11-07 06:09:38 | [diff] [blame] | 864 | if (!SupportsSeccompSandbox(g_proc_fd)) { |
[email protected] | e8c916a | 2009-11-04 17:52:47 | [diff] [blame] | 865 | // There are a good number of users who cannot use the seccomp sandbox |
| 866 | // (e.g. because their distribution does not enable seccomp mode by |
| 867 | // default). While we would prefer to deny execution in this case, it |
| 868 | // seems more realistic to continue in degraded mode. |
[email protected] | 1b5d28f | 2010-02-18 15:53:36 | [diff] [blame] | 869 | LOG(ERROR) << "WARNING! This machine lacks support needed for the " |
| 870 | "Seccomp sandbox. Running renderers with Seccomp " |
| 871 | "sandboxing disabled."; |
[email protected] | e8c916a | 2009-11-04 17:52:47 | [diff] [blame] | 872 | } else { |
[email protected] | 0b4610a9 | 2011-08-04 18:08:09 | [diff] [blame] | 873 | VLOG(1) << "Enabling experimental Seccomp sandbox."; |
[email protected] | c2c68b1f | 2012-02-25 00:29:15 | [diff] [blame] | 874 | sandbox_flags |= ZygoteHostImpl::kSandboxSeccomp; |
[email protected] | e8c916a | 2009-11-04 17:52:47 | [diff] [blame] | 875 | } |
| 876 | } |
[email protected] | 36ea6c6f | 2010-03-17 20:08:01 | [diff] [blame] | 877 | #endif // SECCOMP_SANDBOX |
[email protected] | e8c916a | 2009-11-04 17:52:47 | [diff] [blame] | 878 | |
[email protected] | ce2ae44 | 2011-07-18 16:13:41 | [diff] [blame] | 879 | Zygote zygote(sandbox_flags, forkdelegate); |
[email protected] | c548be2 | 2010-03-08 12:55:57 | [diff] [blame] | 880 | // This function call can return multiple times, once per fork(). |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 881 | return zygote.ProcessRequests(); |
| 882 | } |