1 // Copyright 2013 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 // A mini-zygote specifically for Native Client. 6 7 #include "components/nacl/loader/nacl_helper_linux.h" 8 9 #include <errno.h> 10 #include <fcntl.h> 11 #include <link.h> 12 #include <signal.h> 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <sys/socket.h> 16 #include <sys/stat.h> 17 #include <sys/types.h> 18 19 #include <string> 20 #include <vector> 21 22 #include "base/at_exit.h" 23 #include "base/command_line.h" 24 #include "base/logging.h" 25 #include "base/message_loop/message_loop.h" 26 #include "base/posix/eintr_wrapper.h" 27 #include "base/posix/global_descriptors.h" 28 #include "base/posix/unix_domain_socket_linux.h" 29 #include "base/process/kill.h" 30 #include "base/rand_util.h" 31 #include "components/nacl/loader/nacl_listener.h" 32 #include "components/nacl/loader/nacl_sandbox_linux.h" 33 #include "content/public/common/zygote_fork_delegate_linux.h" 34 #include "crypto/nss_util.h" 35 #include "ipc/ipc_descriptors.h" 36 #include "ipc/ipc_switches.h" 37 #include "sandbox/linux/services/libc_urandom_override.h" 38 39 namespace { 40 41 struct NaClLoaderSystemInfo { 42 size_t prereserved_sandbox_size; 43 long number_of_cores; 44 }; 45 46 // The child must mimic the behavior of zygote_main_linux.cc on the child 47 // side of the fork. See zygote_main_linux.cc:HandleForkRequest from 48 // if (!child) { 49 void BecomeNaClLoader(const std::vector<int>& child_fds, 50 const NaClLoaderSystemInfo& system_info) { 51 VLOG(1) << "NaCl loader: setting up IPC descriptor"; 52 // don't need zygote FD any more 53 if (IGNORE_EINTR(close(kNaClZygoteDescriptor)) != 0) 54 LOG(ERROR) << "close(kNaClZygoteDescriptor) failed."; 55 bool sandbox_initialized = InitializeBPFSandbox(); 56 if (!sandbox_initialized) { 57 LOG(ERROR) << "Could not initialize NaCl's second " 58 << "layer sandbox (seccomp-bpf)."; 59 } 60 base::GlobalDescriptors::GetInstance()->Set( 61 kPrimaryIPCChannel, 62 child_fds[content::ZygoteForkDelegate::kBrowserFDIndex]); 63 64 base::MessageLoopForIO main_message_loop; 65 NaClListener listener; 66 listener.set_prereserved_sandbox_size(system_info.prereserved_sandbox_size); 67 listener.set_number_of_cores(system_info.number_of_cores); 68 listener.Listen(); 69 _exit(0); 70 } 71 72 // Start the NaCl loader in a child created by the NaCl loader Zygote. 73 void ChildNaClLoaderInit(const std::vector<int>& child_fds, 74 const NaClLoaderSystemInfo& system_info) { 75 const int parent_fd = child_fds[content::ZygoteForkDelegate::kParentFDIndex]; 76 const int dummy_fd = child_fds[content::ZygoteForkDelegate::kDummyFDIndex]; 77 bool validack = false; 78 const size_t kMaxReadSize = 1024; 79 char buffer[kMaxReadSize]; 80 // Wait until the parent process has discovered our PID. We 81 // should not fork any child processes (which the seccomp 82 // sandbox does) until then, because that can interfere with the 83 // parent's discovery of our PID. 84 const int nread = HANDLE_EINTR(read(parent_fd, buffer, kMaxReadSize)); 85 const std::string switch_prefix = std::string("--") + 86 switches::kProcessChannelID + std::string("="); 87 const size_t len = switch_prefix.length(); 88 89 if (nread < 0) { 90 perror("read"); 91 LOG(ERROR) << "read returned " << nread; 92 } else if (nread > static_cast<int>(len)) { 93 if (switch_prefix.compare(0, len, buffer, 0, len) == 0) { 94 VLOG(1) << "NaCl loader is synchronised with Chrome zygote"; 95 CommandLine::ForCurrentProcess()->AppendSwitchASCII( 96 switches::kProcessChannelID, 97 std::string(&buffer[len], nread - len)); 98 validack = true; 99 } 100 } 101 if (IGNORE_EINTR(close(dummy_fd)) != 0) 102 LOG(ERROR) << "close(dummy_fd) failed"; 103 if (IGNORE_EINTR(close(parent_fd)) != 0) 104 LOG(ERROR) << "close(parent_fd) failed"; 105 if (validack) { 106 BecomeNaClLoader(child_fds, system_info); 107 } else { 108 LOG(ERROR) << "Failed to synch with zygote"; 109 } 110 _exit(1); 111 } 112 113 // Handle a fork request from the Zygote. 114 // Some of this code was lifted from 115 // content/browser/zygote_main_linux.cc:ForkWithRealPid() 116 bool HandleForkRequest(const std::vector<int>& child_fds, 117 const NaClLoaderSystemInfo& system_info, 118 Pickle* output_pickle) { 119 if (content::ZygoteForkDelegate::kNumPassedFDs != child_fds.size()) { 120 LOG(ERROR) << "nacl_helper: unexpected number of fds, got " 121 << child_fds.size(); 122 return false; 123 } 124 125 VLOG(1) << "nacl_helper: forking"; 126 pid_t child_pid = fork(); 127 if (child_pid < 0) { 128 PLOG(ERROR) << "*** fork() failed."; 129 } 130 131 if (child_pid == 0) { 132 ChildNaClLoaderInit(child_fds, system_info); 133 NOTREACHED(); 134 } 135 136 // I am the parent. 137 // First, close the dummy_fd so the sandbox won't find me when 138 // looking for the child's pid in /proc. Also close other fds. 139 for (size_t i = 0; i < child_fds.size(); i++) { 140 if (IGNORE_EINTR(close(child_fds[i])) != 0) 141 LOG(ERROR) << "close(child_fds[i]) failed"; 142 } 143 VLOG(1) << "nacl_helper: child_pid is " << child_pid; 144 145 // Now send child_pid (eventually -1 if fork failed) to the Chrome Zygote. 146 output_pickle->WriteInt(child_pid); 147 return true; 148 } 149 150 bool HandleGetTerminationStatusRequest(PickleIterator* input_iter, 151 Pickle* output_pickle) { 152 pid_t child_to_wait; 153 if (!input_iter->ReadInt(&child_to_wait)) { 154 LOG(ERROR) << "Could not read pid to wait for"; 155 return false; 156 } 157 158 bool known_dead; 159 if (!input_iter->ReadBool(&known_dead)) { 160 LOG(ERROR) << "Could not read known_dead status"; 161 return false; 162 } 163 // TODO(jln): With NaCl, known_dead seems to never be set to true (unless 164 // called from the Zygote's kZygoteCommandReap command). This means that we 165 // will sometimes detect the process as still running when it's not. Fix 166 // this! 167 168 int exit_code; 169 base::TerminationStatus status; 170 if (known_dead) 171 status = base::GetKnownDeadTerminationStatus(child_to_wait, &exit_code); 172 else 173 status = base::GetTerminationStatus(child_to_wait, &exit_code); 174 output_pickle->WriteInt(static_cast<int>(status)); 175 output_pickle->WriteInt(exit_code); 176 return true; 177 } 178 179 // This is a poor man's check on whether we are sandboxed. 180 bool IsSandboxed() { 181 int proc_fd = open("/proc/self/exe", O_RDONLY); 182 if (proc_fd >= 0) { 183 close(proc_fd); 184 return false; 185 } 186 return true; 187 } 188 189 // Honor a command |command_type|. Eventual command parameters are 190 // available in |input_iter| and eventual file descriptors attached to 191 // the command are in |attached_fds|. 192 // Reply to the command on |reply_fds|. 193 bool HonorRequestAndReply(int reply_fd, 194 int command_type, 195 const std::vector<int>& attached_fds, 196 const NaClLoaderSystemInfo& system_info, 197 PickleIterator* input_iter) { 198 Pickle write_pickle; 199 bool have_to_reply = false; 200 // Commands must write anything to send back to |write_pickle|. 201 switch (command_type) { 202 case nacl::kNaClForkRequest: 203 have_to_reply = HandleForkRequest(attached_fds, system_info, 204 &write_pickle); 205 break; 206 case nacl::kNaClGetTerminationStatusRequest: 207 have_to_reply = 208 HandleGetTerminationStatusRequest(input_iter, &write_pickle); 209 break; 210 default: 211 LOG(ERROR) << "Unsupported command from Zygote"; 212 return false; 213 } 214 if (!have_to_reply) 215 return false; 216 const std::vector<int> empty; // We never send file descriptors back. 217 if (!UnixDomainSocket::SendMsg(reply_fd, write_pickle.data(), 218 write_pickle.size(), empty)) { 219 LOG(ERROR) << "*** send() to zygote failed"; 220 return false; 221 } 222 return true; 223 } 224 225 // Read a request from the Zygote from |zygote_ipc_fd| and handle it. 226 // Die on EOF from |zygote_ipc_fd|. 227 bool HandleZygoteRequest(int zygote_ipc_fd, 228 const NaClLoaderSystemInfo& system_info) { 229 std::vector<int> fds; 230 char buf[kNaClMaxIPCMessageLength]; 231 const ssize_t msglen = UnixDomainSocket::RecvMsg(zygote_ipc_fd, 232 &buf, sizeof(buf), &fds); 233 // If the Zygote has started handling requests, we should be sandboxed via 234 // the setuid sandbox. 235 if (!IsSandboxed()) { 236 LOG(ERROR) << "NaCl helper process running without a sandbox!\n" 237 << "Most likely you need to configure your SUID sandbox " 238 << "correctly"; 239 } 240 if (msglen == 0 || (msglen == -1 && errno == ECONNRESET)) { 241 // EOF from the browser. Goodbye! 242 _exit(0); 243 } 244 if (msglen < 0) { 245 PLOG(ERROR) << "nacl_helper: receive from zygote failed"; 246 return false; 247 } 248 249 Pickle read_pickle(buf, msglen); 250 PickleIterator read_iter(read_pickle); 251 int command_type; 252 if (!read_iter.ReadInt(&command_type)) { 253 LOG(ERROR) << "Unable to read command from Zygote"; 254 return false; 255 } 256 return HonorRequestAndReply(zygote_ipc_fd, command_type, fds, system_info, 257 &read_iter); 258 } 259 260 static const char kNaClHelperReservedAtZero[] = "reserved_at_zero"; 261 static const char kNaClHelperRDebug[] = "r_debug"; 262 263 // Since we were started by nacl_helper_bootstrap rather than in the 264 // usual way, the debugger cannot figure out where our executable 265 // or the dynamic linker or the shared libraries are in memory, 266 // so it won't find any symbols. But we can fake it out to find us. 267 // 268 // The zygote passes --r_debug=0xXXXXXXXXXXXXXXXX. 269 // nacl_helper_bootstrap replaces the Xs with the address of its _r_debug 270 // structure. The debugger will look for that symbol by name to 271 // discover the addresses of key dynamic linker data structures. 272 // Since all it knows about is the original main executable, which 273 // is the bootstrap program, it finds the symbol defined there. The 274 // dynamic linker's structure is somewhere else, but it is filled in 275 // after initialization. The parts that really matter to the 276 // debugger never change. So we just copy the contents of the 277 // dynamic linker's structure into the address provided by the option. 278 // Hereafter, if someone attaches a debugger (or examines a core dump), 279 // the debugger will find all the symbols in the normal way. 280 static void CheckRDebug(char* argv0) { 281 std::string r_debug_switch_value = 282 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kNaClHelperRDebug); 283 if (!r_debug_switch_value.empty()) { 284 char* endp; 285 uintptr_t r_debug_addr = strtoul(r_debug_switch_value.c_str(), &endp, 0); 286 if (r_debug_addr != 0 && *endp == '\0') { 287 r_debug* bootstrap_r_debug = reinterpret_cast<r_debug*>(r_debug_addr); 288 *bootstrap_r_debug = _r_debug; 289 290 // Since the main executable (the bootstrap program) does not 291 // have a dynamic section, the debugger will not skip the 292 // first element of the link_map list as it usually would for 293 // an executable or PIE that was loaded normally. But the 294 // dynamic linker has set l_name for the PIE to "" as is 295 // normal for the main executable. So the debugger doesn't 296 // know which file it is. Fill in the actual file name, which 297 // came in as our argv[0]. 298 link_map* l = _r_debug.r_map; 299 if (l->l_name[0] == '\0') 300 l->l_name = argv0; 301 } 302 } 303 } 304 305 // The zygote passes --reserved_at_zero=0xXXXXXXXXXXXXXXXX. 306 // nacl_helper_bootstrap replaces the Xs with the amount of prereserved 307 // sandbox memory. 308 // 309 // CheckReservedAtZero parses the value of the argument reserved_at_zero 310 // and returns the amount of prereserved sandbox memory. 311 static size_t CheckReservedAtZero() { 312 size_t prereserved_sandbox_size = 0; 313 std::string reserved_at_zero_switch_value = 314 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 315 kNaClHelperReservedAtZero); 316 if (!reserved_at_zero_switch_value.empty()) { 317 char* endp; 318 prereserved_sandbox_size = 319 strtoul(reserved_at_zero_switch_value.c_str(), &endp, 0); 320 if (*endp != '\0') 321 LOG(ERROR) << "Could not parse reserved_at_zero argument value of " 322 << reserved_at_zero_switch_value; 323 } 324 return prereserved_sandbox_size; 325 } 326 327 } // namespace 328 329 #if defined(ADDRESS_SANITIZER) 330 // Do not install the SIGSEGV handler in ASan. This should make the NaCl 331 // platform qualification test pass. 332 static const char kAsanDefaultOptionsNaCl[] = "handle_segv=0"; 333 334 // Override the default ASan options for the NaCl helper. 335 // __asan_default_options should not be instrumented, because it is called 336 // before ASan is initialized. 337 extern "C" 338 __attribute__((no_sanitize_address)) 339 const char* __asan_default_options() { 340 return kAsanDefaultOptionsNaCl; 341 } 342 #endif 343 344 int main(int argc, char* argv[]) { 345 CommandLine::Init(argc, argv); 346 base::AtExitManager exit_manager; 347 base::RandUint64(); // acquire /dev/urandom fd before sandbox is raised 348 // Allows NSS to fopen() /dev/urandom. 349 sandbox::InitLibcUrandomOverrides(); 350 #if defined(USE_NSS) 351 // Configure NSS for use inside the NaCl process. 352 // The fork check has not caused problems for NaCl, but this appears to be 353 // best practice (see other places LoadNSSLibraries is called.) 354 crypto::DisableNSSForkCheck(); 355 // Without this line on Linux, HMAC::Init will instantiate a singleton that 356 // in turn attempts to open a file. Disabling this behavior avoids a ~70 ms 357 // stall the first time HMAC is used. 358 crypto::ForceNSSNoDBInit(); 359 // Load shared libraries before sandbox is raised. 360 // NSS is needed to perform hashing for validation caching. 361 crypto::LoadNSSLibraries(); 362 #endif 363 const NaClLoaderSystemInfo system_info = { 364 CheckReservedAtZero(), 365 sysconf(_SC_NPROCESSORS_ONLN) 366 }; 367 368 CheckRDebug(argv[0]); 369 370 // Check that IsSandboxed() works. We should not be sandboxed at this point. 371 CHECK(!IsSandboxed()) << "Unexpectedly sandboxed!"; 372 373 const std::vector<int> empty; 374 // Send the zygote a message to let it know we are ready to help 375 if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor, 376 kNaClHelperStartupAck, 377 sizeof(kNaClHelperStartupAck), empty)) { 378 LOG(ERROR) << "*** send() to zygote failed"; 379 } 380 381 // Now handle requests from the Zygote. 382 while (true) { 383 bool request_handled = HandleZygoteRequest(kNaClZygoteDescriptor, 384 system_info); 385 // Do not turn this into a CHECK() without thinking about robustness 386 // against malicious IPC requests. 387 DCHECK(request_handled); 388 } 389 NOTREACHED(); 390 } 391