Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/zygote/zygote_linux.h" | 5 #include "content/zygote/zygote_linux.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 #include <sys/socket.h> | 9 #include <sys/socket.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 29 #include "content/common/sandbox_linux/sandbox_linux.h" | 29 #include "content/common/sandbox_linux/sandbox_linux.h" |
| 30 #include "content/common/set_process_title.h" | 30 #include "content/common/set_process_title.h" |
| 31 #include "content/common/zygote_commands_linux.h" | 31 #include "content/common/zygote_commands_linux.h" |
| 32 #include "content/public/common/content_descriptors.h" | 32 #include "content/public/common/content_descriptors.h" |
| 33 #include "content/public/common/result_codes.h" | 33 #include "content/public/common/result_codes.h" |
| 34 #include "content/public/common/sandbox_linux.h" | 34 #include "content/public/common/sandbox_linux.h" |
| 35 #include "content/public/common/send_zygote_child_ping_linux.h" | 35 #include "content/public/common/send_zygote_child_ping_linux.h" |
| 36 #include "content/public/common/zygote_fork_delegate_linux.h" | 36 #include "content/public/common/zygote_fork_delegate_linux.h" |
| 37 #include "ipc/ipc_channel.h" | 37 #include "ipc/ipc_channel.h" |
| 38 #include "ipc/ipc_switches.h" | 38 #include "ipc/ipc_switches.h" |
| 39 #include "sandbox/linux/services/credentials.h" | |
| 40 #include "sandbox/linux/services/namespace_sandbox.h" | |
| 39 | 41 |
| 40 // See http://code.google.com/p/chromium/wiki/LinuxZygote | 42 // See http://code.google.com/p/chromium/wiki/LinuxZygote |
| 41 | 43 |
| 42 namespace content { | 44 namespace content { |
| 43 | 45 |
| 44 namespace { | 46 namespace { |
| 45 | 47 |
| 46 // NOP function. See below where this handler is installed. | 48 // NOP function. See below where this handler is installed. |
| 47 void SIGCHLDHandler(int signal) { | 49 void SIGCHLDHandler(int signal) { |
| 48 } | 50 } |
| 49 | 51 |
| 52 // On Linux, when a process is the init process of a PID namespace, it cannot be | |
| 53 // terminated by signals like SIGTERM or SIGINT, since they are ignored unless | |
| 54 // we register a handler for them. In the handlers, we exit with this special | |
| 55 // exit code that GetTerminationStatus understands to mean that we were | |
| 56 // terminated by an external signal. | |
| 57 const int kKilledExitCode = 0x80; | |
| 58 const int kUnexpectedExitCode = 0x81; | |
| 59 | |
| 50 int LookUpFd(const base::GlobalDescriptors::Mapping& fd_mapping, uint32_t key) { | 60 int LookUpFd(const base::GlobalDescriptors::Mapping& fd_mapping, uint32_t key) { |
| 51 for (size_t index = 0; index < fd_mapping.size(); ++index) { | 61 for (size_t index = 0; index < fd_mapping.size(); ++index) { |
| 52 if (fd_mapping[index].key == key) | 62 if (fd_mapping[index].key == key) |
| 53 return fd_mapping[index].fd; | 63 return fd_mapping[index].fd; |
| 54 } | 64 } |
| 55 return -1; | 65 return -1; |
| 56 } | 66 } |
| 57 | 67 |
| 58 void CreatePipe(base::ScopedFD* read_pipe, base::ScopedFD* write_pipe) { | 68 void CreatePipe(base::ScopedFD* read_pipe, base::ScopedFD* write_pipe) { |
| 59 int raw_pipe[2]; | 69 int raw_pipe[2]; |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 268 GetTerminationStatus(child, true /* known_dead */, &status, &exit_code); | 278 GetTerminationStatus(child, true /* known_dead */, &status, &exit_code); |
| 269 DCHECK(got_termination_status); | 279 DCHECK(got_termination_status); |
| 270 } | 280 } |
| 271 process_info_map_.erase(child); | 281 process_info_map_.erase(child); |
| 272 } | 282 } |
| 273 | 283 |
| 274 bool Zygote::GetTerminationStatus(base::ProcessHandle real_pid, | 284 bool Zygote::GetTerminationStatus(base::ProcessHandle real_pid, |
| 275 bool known_dead, | 285 bool known_dead, |
| 276 base::TerminationStatus* status, | 286 base::TerminationStatus* status, |
| 277 int* exit_code) { | 287 int* exit_code) { |
| 278 | |
| 279 ZygoteProcessInfo child_info; | 288 ZygoteProcessInfo child_info; |
| 280 if (!GetProcessInfo(real_pid, &child_info)) { | 289 if (!GetProcessInfo(real_pid, &child_info)) { |
| 281 LOG(ERROR) << "Zygote::GetTerminationStatus for unknown PID " | 290 LOG(FATAL) << "Zygote::GetTerminationStatus for unknown PID " |
| 282 << real_pid; | 291 << real_pid; |
| 283 NOTREACHED(); | |
|
jln (very slow on Chromium)
2015/03/24 22:03:34
NOTREACHED() crash is debug-only.
I think it coul
rickyz (no longer on Chrome)
2015/03/25 22:47:46
Done.
| |
| 284 return false; | 292 return false; |
| 285 } | 293 } |
| 286 // We know about |real_pid|. | 294 // We know about |real_pid|. |
| 287 const base::ProcessHandle child = child_info.internal_pid; | 295 const base::ProcessHandle child = child_info.internal_pid; |
| 288 if (child_info.started_from_helper) { | 296 if (child_info.started_from_helper) { |
| 289 if (!child_info.started_from_helper->GetTerminationStatus( | 297 if (!child_info.started_from_helper->GetTerminationStatus( |
| 290 child, known_dead, status, exit_code)) { | 298 child, known_dead, status, exit_code)) { |
| 291 return false; | 299 return false; |
| 292 } | 300 } |
| 293 } else { | 301 } else { |
| 294 // Handle the request directly. | 302 // Handle the request directly. |
| 295 if (known_dead) { | 303 if (known_dead) { |
| 296 *status = base::GetKnownDeadTerminationStatus(child, exit_code); | 304 *status = base::GetKnownDeadTerminationStatus(child, exit_code); |
| 297 } else { | 305 } else { |
| 298 // We don't know if the process is dying, so get its status but don't | 306 // We don't know if the process is dying, so get its status but don't |
| 299 // wait. | 307 // wait. |
| 300 *status = base::GetTerminationStatus(child, exit_code); | 308 *status = base::GetTerminationStatus(child, exit_code); |
| 301 } | 309 } |
| 302 } | 310 } |
| 303 // Successfully got a status for |real_pid|. | 311 // Successfully got a status for |real_pid|. |
| 304 if (*status != base::TERMINATION_STATUS_STILL_RUNNING) { | 312 if (*status != base::TERMINATION_STATUS_STILL_RUNNING) { |
| 305 // Time to forget about this process. | 313 // Time to forget about this process. |
| 306 process_info_map_.erase(real_pid); | 314 process_info_map_.erase(real_pid); |
| 307 } | 315 } |
| 316 | |
| 317 if (WIFEXITED(*exit_code) && WEXITSTATUS(*exit_code) == kKilledExitCode) { | |
| 318 *status = base::TERMINATION_STATUS_PROCESS_WAS_KILLED; | |
| 319 } | |
| 320 | |
| 308 return true; | 321 return true; |
| 309 } | 322 } |
| 310 | 323 |
| 311 void Zygote::HandleGetTerminationStatus(int fd, | 324 void Zygote::HandleGetTerminationStatus(int fd, |
| 312 PickleIterator iter) { | 325 PickleIterator iter) { |
| 313 bool known_dead; | 326 bool known_dead; |
| 314 base::ProcessHandle child_requested; | 327 base::ProcessHandle child_requested; |
| 315 | 328 |
| 316 if (!iter.ReadBool(&known_dead) || !iter.ReadInt(&child_requested)) { | 329 if (!iter.ReadBool(&known_dead) || !iter.ReadInt(&child_requested)) { |
| 317 LOG(WARNING) << "Error parsing GetTerminationStatus request " | 330 LOG(WARNING) << "Error parsing GetTerminationStatus request " |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 368 } | 381 } |
| 369 std::vector<int> fds; | 382 std::vector<int> fds; |
| 370 fds.push_back(ipc_channel_fd); // kBrowserFDIndex | 383 fds.push_back(ipc_channel_fd); // kBrowserFDIndex |
| 371 fds.push_back(pid_oracle.get()); // kPIDOracleFDIndex | 384 fds.push_back(pid_oracle.get()); // kPIDOracleFDIndex |
| 372 pid = helper->Fork(process_type, fds, channel_id); | 385 pid = helper->Fork(process_type, fds, channel_id); |
| 373 | 386 |
| 374 // Helpers should never return in the child process. | 387 // Helpers should never return in the child process. |
| 375 CHECK_NE(pid, 0); | 388 CHECK_NE(pid, 0); |
| 376 } else { | 389 } else { |
| 377 CreatePipe(&read_pipe, &write_pipe); | 390 CreatePipe(&read_pipe, &write_pipe); |
| 378 // This is roughly equivalent to a fork(). We are using ForkWithFlags mainly | 391 if (sandbox_flags_ & kSandboxLinuxPIDNS && |
| 379 // to give it some more diverse test coverage. | 392 sandbox_flags_ & kSandboxLinuxUserNS) { |
| 380 pid = base::ForkWithFlags(SIGCHLD, nullptr, nullptr); | 393 pid = sandbox::NamespaceSandbox::ForkInNewPidNamespace(); |
| 394 } else { | |
| 395 pid = fork(); | |
| 396 } | |
| 381 } | 397 } |
| 382 | 398 |
| 383 if (pid == 0) { | 399 if (pid == 0) { |
| 400 if (sandbox::Credentials::HasAnyCapability()) { | |
| 401 CHECK(sandbox::Credentials::DropAllCapabilities( | |
|
jln (very slow on Chromium)
2015/03/25 02:09:13
We'll need an API to drop all capabilities only on
rickyz (no longer on Chrome)
2015/03/25 22:47:46
Good idea, done. I made it drop capabilities based
| |
| 402 LinuxSandbox::GetInstance()->proc_fd())); | |
| 403 } | |
| 404 | |
| 405 // If the process is the init process inside a PID namespace, it must have | |
| 406 // explicit signal handlers. | |
| 407 if (getpid() == 1) { | |
| 408 for (const int sig : {SIGINT, SIGTERM}) { | |
| 409 sandbox::NamespaceSandbox::InstallTerminationSignalHandler( | |
| 410 sig, kKilledExitCode); | |
| 411 } | |
| 412 | |
| 413 static const int kUnexpectedSignals[] = { | |
| 414 SIGHUP, SIGQUIT, SIGABRT, SIGPIPE, SIGUSR1, SIGUSR2, | |
| 415 }; | |
| 416 for (const int sig : kUnexpectedSignals) { | |
| 417 sandbox::NamespaceSandbox::InstallTerminationSignalHandler( | |
| 418 sig, kUnexpectedExitCode); | |
| 419 } | |
| 420 } | |
| 421 | |
| 384 // In the child process. | 422 // In the child process. |
| 385 write_pipe.reset(); | 423 write_pipe.reset(); |
| 386 | 424 |
| 387 // Ping the PID oracle socket so the browser can find our PID. | 425 // Ping the PID oracle socket so the browser can find our PID. |
| 388 CHECK(SendZygoteChildPing(pid_oracle.get())); | 426 CHECK(SendZygoteChildPing(pid_oracle.get())); |
| 389 | 427 |
| 390 // Now read back our real PID from the zygote. | 428 // Now read back our real PID from the zygote. |
| 391 base::ProcessId real_pid; | 429 base::ProcessId real_pid; |
| 392 if (!base::ReadFromFD(read_pipe.get(), | 430 if (!base::ReadFromFD(read_pipe.get(), |
| 393 reinterpret_cast<char*>(&real_pid), | 431 reinterpret_cast<char*>(&real_pid), |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 585 PickleIterator iter) { | 623 PickleIterator iter) { |
| 586 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_))) != | 624 if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_))) != |
| 587 sizeof(sandbox_flags_)) { | 625 sizeof(sandbox_flags_)) { |
| 588 PLOG(ERROR) << "write"; | 626 PLOG(ERROR) << "write"; |
| 589 } | 627 } |
| 590 | 628 |
| 591 return false; | 629 return false; |
| 592 } | 630 } |
| 593 | 631 |
| 594 } // namespace content | 632 } // namespace content |
| OLD | NEW |