This source file includes following definitions.
- SIGCHLDHandler
- LookUpFd
- initial_uma_boundary_value_
- ProcessRequests
- GetProcessInfo
- UsingSUIDSandbox
- HandleRequestFromBrowser
- HandleReapRequest
- GetTerminationStatus
- HandleGetTerminationStatus
- ForkWithRealPid
- ReadArgsAndFork
- HandleForkRequest
- HandleGetSandboxStatus
#include "content/zygote/zygote_linux.h"
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "base/command_line.h"
#include "base/debug/trace_event.h"
#include "base/file_util.h"
#include "base/linux_util.h"
#include "base/logging.h"
#include "base/pickle.h"
#include "base/posix/eintr_wrapper.h"
#include "base/posix/global_descriptors.h"
#include "base/posix/unix_domain_socket_linux.h"
#include "base/process/kill.h"
#include "content/common/child_process_sandbox_support_impl_linux.h"
#include "content/common/sandbox_linux/sandbox_linux.h"
#include "content/common/set_process_title.h"
#include "content/common/zygote_commands_linux.h"
#include "content/public/common/content_descriptors.h"
#include "content/public/common/result_codes.h"
#include "content/public/common/sandbox_linux.h"
#include "content/public/common/zygote_fork_delegate_linux.h"
#include "ipc/ipc_channel.h"
#include "ipc/ipc_switches.h"
namespace content {
namespace {
void SIGCHLDHandler(int signal) {
}
int LookUpFd(const base::GlobalDescriptors::Mapping& fd_mapping, uint32_t key) {
for (size_t index = 0; index < fd_mapping.size(); ++index) {
if (fd_mapping[index].first == key)
return fd_mapping[index].second;
}
return -1;
}
}
Zygote::Zygote(int sandbox_flags,
ZygoteForkDelegate* helper)
: sandbox_flags_(sandbox_flags),
helper_(helper),
initial_uma_sample_(0),
initial_uma_boundary_value_(0) {
if (helper_) {
helper_->InitialUMA(&initial_uma_name_,
&initial_uma_sample_,
&initial_uma_boundary_value_);
}
}
Zygote::~Zygote() {
}
bool Zygote::ProcessRequests() {
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = &SIGCHLDHandler;
CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
if (UsingSUIDSandbox()) {
std::vector<int> empty;
bool r = UnixDomainSocket::SendMsg(kZygoteSocketPairFd,
kZygoteHelloMessage,
sizeof(kZygoteHelloMessage), empty);
#if defined(OS_CHROMEOS)
LOG_IF(WARNING, !r) << "Sending zygote magic failed";
if (!r)
_exit(RESULT_CODE_NORMAL_EXIT);
#else
CHECK(r) << "Sending zygote magic failed";
#endif
}
for (;;) {
if (HandleRequestFromBrowser(kZygoteSocketPairFd))
return true;
}
}
bool Zygote::GetProcessInfo(base::ProcessHandle pid,
ZygoteProcessInfo* process_info) {
DCHECK(process_info);
const ZygoteProcessMap::const_iterator it = process_info_map_.find(pid);
if (it == process_info_map_.end()) {
return false;
}
*process_info = it->second;
return true;
}
bool Zygote::UsingSUIDSandbox() const {
return sandbox_flags_ & kSandboxLinuxSUID;
}
bool Zygote::HandleRequestFromBrowser(int fd) {
std::vector<int> fds;
char buf[kZygoteMaxMessageLength];
const ssize_t len = UnixDomainSocket::RecvMsg(fd, buf, sizeof(buf), &fds);
if (len == 0 || (len == -1 && errno == ECONNRESET)) {
_exit(0);
return false;
}
if (len == -1) {
PLOG(ERROR) << "Error reading message from browser";
return false;
}
Pickle pickle(buf, len);
PickleIterator iter(pickle);
int kind;
if (pickle.ReadInt(&iter, &kind)) {
switch (kind) {
case kZygoteCommandFork:
return HandleForkRequest(fd, pickle, iter, fds);
case kZygoteCommandReap:
if (!fds.empty())
break;
HandleReapRequest(fd, pickle, iter);
return false;
case kZygoteCommandGetTerminationStatus:
if (!fds.empty())
break;
HandleGetTerminationStatus(fd, pickle, iter);
return false;
case kZygoteCommandGetSandboxStatus:
HandleGetSandboxStatus(fd, pickle, iter);
return false;
default:
NOTREACHED();
break;
}
}
LOG(WARNING) << "Error parsing message from browser";
for (std::vector<int>::const_iterator
i = fds.begin(); i != fds.end(); ++i)
close(*i);
return false;
}
void Zygote::HandleReapRequest(int fd,
const Pickle& pickle,
PickleIterator iter) {
base::ProcessId child;
if (!pickle.ReadInt(&iter, &child)) {
LOG(WARNING) << "Error parsing reap request from browser";
return;
}
ZygoteProcessInfo child_info;
if (!GetProcessInfo(child, &child_info)) {
LOG(ERROR) << "Child not found!";
NOTREACHED();
return;
}
if (!child_info.started_from_helper) {
#if !defined(THREAD_SANITIZER)
base::EnsureProcessTerminated(child_info.internal_pid);
#else
LOG(WARNING) << "Zygote process omitting a call to "
<< "base::EnsureProcessTerminated() for child pid " << child
<< " under ThreadSanitizer. See http://crbug.com/274855.";
#endif
} else {
base::TerminationStatus status;
int exit_code;
bool got_termination_status =
GetTerminationStatus(child, true , &status, &exit_code);
DCHECK(got_termination_status);
}
process_info_map_.erase(child);
}
bool Zygote::GetTerminationStatus(base::ProcessHandle real_pid,
bool known_dead,
base::TerminationStatus* status,
int* exit_code) {
ZygoteProcessInfo child_info;
if (!GetProcessInfo(real_pid, &child_info)) {
LOG(ERROR) << "Zygote::GetTerminationStatus for unknown PID "
<< real_pid;
NOTREACHED();
return false;
}
const base::ProcessHandle child = child_info.internal_pid;
if (child_info.started_from_helper) {
DCHECK(helper_);
if (!helper_->GetTerminationStatus(child, known_dead, status, exit_code)) {
return false;
}
} else {
if (known_dead) {
*status = base::GetKnownDeadTerminationStatus(child, exit_code);
} else {
*status = base::GetTerminationStatus(child, exit_code);
}
}
if (*status != base::TERMINATION_STATUS_STILL_RUNNING) {
process_info_map_.erase(real_pid);
}
return true;
}
void Zygote::HandleGetTerminationStatus(int fd,
const Pickle& pickle,
PickleIterator iter) {
bool known_dead;
base::ProcessHandle child_requested;
if (!pickle.ReadBool(&iter, &known_dead) ||
!pickle.ReadInt(&iter, &child_requested)) {
LOG(WARNING) << "Error parsing GetTerminationStatus request "
<< "from browser";
return;
}
base::TerminationStatus status;
int exit_code;
bool got_termination_status =
GetTerminationStatus(child_requested, known_dead, &status, &exit_code);
if (!got_termination_status) {
NOTREACHED();
status = base::TERMINATION_STATUS_NORMAL_TERMINATION;
exit_code = RESULT_CODE_NORMAL_EXIT;
}
Pickle write_pickle;
write_pickle.WriteInt(static_cast<int>(status));
write_pickle.WriteInt(exit_code);
ssize_t written =
HANDLE_EINTR(write(fd, write_pickle.data(), write_pickle.size()));
if (written != static_cast<ssize_t>(write_pickle.size()))
PLOG(ERROR) << "write";
}
int Zygote::ForkWithRealPid(const std::string& process_type,
const base::GlobalDescriptors::Mapping& fd_mapping,
const std::string& channel_switch,
std::string* uma_name,
int* uma_sample,
int* uma_boundary_value) {
const bool use_helper = (helper_ && helper_->CanHelp(process_type,
uma_name,
uma_sample,
uma_boundary_value));
int dummy_fd;
ino_t dummy_inode;
int pipe_fds[2] = { -1, -1 };
base::ProcessId pid = 0;
dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
if (dummy_fd < 0) {
LOG(ERROR) << "Failed to create dummy FD";
goto error;
}
if (!base::FileDescriptorGetInode(&dummy_inode, dummy_fd)) {
LOG(ERROR) << "Failed to get inode for dummy FD";
goto error;
}
if (pipe(pipe_fds) != 0) {
LOG(ERROR) << "Failed to create pipe";
goto error;
}
if (use_helper) {
std::vector<int> fds;
int ipc_channel_fd = LookUpFd(fd_mapping, kPrimaryIPCChannel);
if (ipc_channel_fd < 0) {
DLOG(ERROR) << "Failed to find kPrimaryIPCChannel in FD mapping";
goto error;
}
fds.push_back(ipc_channel_fd);
fds.push_back(dummy_fd);
fds.push_back(pipe_fds[0]);
pid = helper_->Fork(process_type, fds);
} else {
pid = fork();
}
if (pid < 0) {
goto error;
} else if (pid == 0) {
close(pipe_fds[1]);
base::ProcessId real_pid;
if (!base::ReadFromFD(pipe_fds[0], reinterpret_cast<char*>(&real_pid),
sizeof(real_pid))) {
LOG(FATAL) << "Failed to synchronise with parent zygote process";
}
if (real_pid <= 0) {
LOG(FATAL) << "Invalid pid from parent zygote";
}
#if defined(OS_LINUX)
IPC::Channel::SetGlobalPid(real_pid);
base::debug::TraceLog::GetInstance()->SetProcessID(
static_cast<int>(real_pid));
#endif
close(pipe_fds[0]);
close(dummy_fd);
return 0;
} else {
close(dummy_fd);
dummy_fd = -1;
close(pipe_fds[0]);
pipe_fds[0] = -1;
base::ProcessId real_pid;
if (UsingSUIDSandbox()) {
uint8_t reply_buf[512];
Pickle request;
request.WriteInt(LinuxSandbox::METHOD_GET_CHILD_WITH_INODE);
request.WriteUInt64(dummy_inode);
const ssize_t r = UnixDomainSocket::SendRecvMsg(
GetSandboxFD(), reply_buf, sizeof(reply_buf), NULL,
request);
if (r == -1) {
LOG(ERROR) << "Failed to get child process's real PID";
goto error;
}
Pickle reply(reinterpret_cast<char*>(reply_buf), r);
PickleIterator iter(reply);
if (!reply.ReadInt(&iter, &real_pid))
goto error;
if (real_pid <= 0) {
LOG(ERROR) << "METHOD_GET_CHILD_WITH_INODE failed";
goto error;
}
} else {
real_pid = pid;
}
if (process_info_map_.find(real_pid) != process_info_map_.end()) {
LOG(ERROR) << "Already tracking PID " << real_pid;
NOTREACHED();
}
process_info_map_[real_pid].internal_pid = pid;
process_info_map_[real_pid].started_from_helper = use_helper;
if (use_helper) {
if (!helper_->AckChild(pipe_fds[1], channel_switch)) {
LOG(ERROR) << "Failed to synchronise with zygote fork helper";
goto error;
}
} else {
int written =
HANDLE_EINTR(write(pipe_fds[1], &real_pid, sizeof(real_pid)));
if (written != sizeof(real_pid)) {
LOG(ERROR) << "Failed to synchronise with child process";
goto error;
}
}
close(pipe_fds[1]);
return real_pid;
}
error:
if (pid > 0) {
if (waitpid(pid, NULL, WNOHANG) == -1)
LOG(ERROR) << "Failed to wait for process";
}
if (dummy_fd >= 0)
close(dummy_fd);
if (pipe_fds[0] >= 0)
close(pipe_fds[0]);
if (pipe_fds[1] >= 0)
close(pipe_fds[1]);
return -1;
}
base::ProcessId Zygote::ReadArgsAndFork(const Pickle& pickle,
PickleIterator iter,
std::vector<int>& fds,
std::string* uma_name,
int* uma_sample,
int* uma_boundary_value) {
std::vector<std::string> args;
int argc = 0;
int numfds = 0;
base::GlobalDescriptors::Mapping mapping;
std::string process_type;
std::string channel_id;
const std::string channel_id_prefix = std::string("--")
+ switches::kProcessChannelID + std::string("=");
if (!pickle.ReadString(&iter, &process_type))
return -1;
if (!pickle.ReadInt(&iter, &argc))
return -1;
for (int i = 0; i < argc; ++i) {
std::string arg;
if (!pickle.ReadString(&iter, &arg))
return -1;
args.push_back(arg);
if (arg.compare(0, channel_id_prefix.length(), channel_id_prefix) == 0)
channel_id = arg;
}
if (!pickle.ReadInt(&iter, &numfds))
return -1;
if (numfds != static_cast<int>(fds.size()))
return -1;
for (int i = 0; i < numfds; ++i) {
base::GlobalDescriptors::Key key;
if (!pickle.ReadUInt32(&iter, &key))
return -1;
mapping.push_back(std::make_pair(key, fds[i]));
}
mapping.push_back(std::make_pair(
static_cast<uint32_t>(kSandboxIPCChannel), GetSandboxFD()));
base::ProcessId child_pid = ForkWithRealPid(process_type, mapping, channel_id,
uma_name, uma_sample,
uma_boundary_value);
if (!child_pid) {
close(kZygoteSocketPairFd);
if (UsingSUIDSandbox())
close(kZygoteIdFd);
base::GlobalDescriptors::GetInstance()->Reset(mapping);
CommandLine::Reset();
CommandLine::Init(0, NULL);
CommandLine::ForCurrentProcess()->InitFromArgv(args);
SetProcessTitleFromCommandLine(NULL);
} else if (child_pid < 0) {
LOG(ERROR) << "Zygote could not fork: process_type " << process_type
<< " numfds " << numfds << " child_pid " << child_pid;
}
return child_pid;
}
bool Zygote::HandleForkRequest(int fd,
const Pickle& pickle,
PickleIterator iter,
std::vector<int>& fds) {
std::string uma_name;
int uma_sample;
int uma_boundary_value;
base::ProcessId child_pid = ReadArgsAndFork(pickle, iter, fds,
&uma_name, &uma_sample,
&uma_boundary_value);
if (child_pid == 0)
return true;
for (std::vector<int>::const_iterator
i = fds.begin(); i != fds.end(); ++i)
close(*i);
if (uma_name.empty()) {
uma_name.swap(initial_uma_name_);
uma_sample = initial_uma_sample_;
uma_boundary_value = initial_uma_boundary_value_;
}
Pickle reply_pickle;
reply_pickle.WriteInt(child_pid);
reply_pickle.WriteString(uma_name);
if (!uma_name.empty()) {
reply_pickle.WriteInt(uma_sample);
reply_pickle.WriteInt(uma_boundary_value);
}
if (HANDLE_EINTR(write(fd, reply_pickle.data(), reply_pickle.size())) !=
static_cast<ssize_t> (reply_pickle.size()))
PLOG(ERROR) << "write";
return false;
}
bool Zygote::HandleGetSandboxStatus(int fd,
const Pickle& pickle,
PickleIterator iter) {
if (HANDLE_EINTR(write(fd, &sandbox_flags_, sizeof(sandbox_flags_))) !=
sizeof(sandbox_flags_)) {
PLOG(ERROR) << "write";
}
return false;
}
}