This source file includes following definitions.
- GetInstance
- should_teardown_after_last_child_exits_
- GetInstance
- Init
- TearDownAfterLastChild
- TearDown
- ZygoteChildBorn
- ZygoteChildDied
- SendMessage
- ReadReply
- ForkRequest
- AdjustRendererOOMScore
- EnsureProcessTerminated
- GetTerminationStatus
- GetPid
- GetSandboxHelperPid
- GetSandboxStatus
#include "content/browser/zygote_host/zygote_host_impl_linux.h"
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/environment.h"
#include "base/file_util.h"
#include "base/files/file_enumerator.h"
#include "base/files/scoped_file.h"
#include "base/linux_util.h"
#include "base/logging.h"
#include "base/memory/linked_ptr.h"
#include "base/memory/scoped_ptr.h"
#include "base/metrics/histogram.h"
#include "base/path_service.h"
#include "base/posix/eintr_wrapper.h"
#include "base/posix/unix_domain_socket_linux.h"
#include "base/process/launch.h"
#include "base/process/memory.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "content/browser/renderer_host/render_sandbox_host_linux.h"
#include "content/common/child_process_sandbox_support_impl_linux.h"
#include "content/common/zygote_commands_linux.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/result_codes.h"
#include "sandbox/linux/suid/client/setuid_sandbox_client.h"
#include "sandbox/linux/suid/common/sandbox.h"
#include "ui/base/ui_base_switches.h"
#include "ui/gfx/switches.h"
#if defined(USE_TCMALLOC)
#include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
#endif
namespace content {
ZygoteHost* ZygoteHost::GetInstance() {
return ZygoteHostImpl::GetInstance();
}
ZygoteHostImpl::ZygoteHostImpl()
: control_fd_(-1),
control_lock_(),
pid_(-1),
init_(false),
using_suid_sandbox_(false),
sandbox_binary_(),
have_read_sandbox_status_word_(false),
sandbox_status_(0),
child_tracking_lock_(),
list_of_running_zygote_children_(),
should_teardown_after_last_child_exits_(false) {}
ZygoteHostImpl::~ZygoteHostImpl() { TearDown(); }
ZygoteHostImpl* ZygoteHostImpl::GetInstance() {
return Singleton<ZygoteHostImpl>::get();
}
void ZygoteHostImpl::Init(const std::string& sandbox_cmd) {
DCHECK(!init_);
init_ = true;
base::FilePath chrome_path;
CHECK(PathService::Get(base::FILE_EXE, &chrome_path));
CommandLine cmd_line(chrome_path);
cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kZygoteProcess);
int fds[2];
#if defined(OS_FREEBSD) || defined(OS_OPENBSD)
if (socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fds) != 0)
CHECK(socketpair(PF_UNIX, SOCK_DGRAM, 0, fds) == 0);
#else
CHECK(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fds) == 0);
#endif
base::FileHandleMappingVector fds_to_map;
fds_to_map.push_back(std::make_pair(fds[1], kZygoteSocketPairFd));
const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
if (browser_command_line.HasSwitch(switches::kZygoteCmdPrefix)) {
cmd_line.PrependWrapper(
browser_command_line.GetSwitchValueNative(switches::kZygoteCmdPrefix));
}
static const char* kForwardSwitches[] = {
switches::kAllowSandboxDebugging,
switches::kLoggingLevel,
switches::kEnableLogging,
switches::kV,
switches::kVModule,
switches::kRegisterPepperPlugins,
switches::kDisableSeccompFilterSandbox,
switches::kForceDeviceScaleFactor,
switches::kTouchOptimizedUI,
switches::kNoSandbox,
};
cmd_line.CopySwitchesFrom(browser_command_line, kForwardSwitches,
arraysize(kForwardSwitches));
GetContentClient()->browser()->AppendExtraCommandLineSwitches(&cmd_line, -1);
sandbox_binary_ = sandbox_cmd.c_str();
using_suid_sandbox_ = !sandbox_cmd.empty();
if (using_suid_sandbox_) {
struct stat st;
if (stat(sandbox_binary_.c_str(), &st) != 0) {
LOG(FATAL) << "The SUID sandbox helper binary is missing: "
<< sandbox_binary_ << " Aborting now.";
}
if (access(sandbox_binary_.c_str(), X_OK) == 0 &&
(st.st_uid == 0) &&
(st.st_mode & S_ISUID) &&
(st.st_mode & S_IXOTH)) {
cmd_line.PrependWrapper(sandbox_binary_);
scoped_ptr<sandbox::SetuidSandboxClient>
sandbox_client(sandbox::SetuidSandboxClient::Create());
sandbox_client->SetupLaunchEnvironment();
} else {
LOG(FATAL) << "The SUID sandbox helper binary was found, but is not "
"configured correctly. Rather than run without sandboxing "
"I'm aborting now. You need to make sure that "
<< sandbox_binary_ << " is owned by root and has mode 4755.";
}
}
const int sfd = RenderSandboxHostLinux::GetInstance()->GetRendererSocket();
fds_to_map.push_back(std::make_pair(sfd, GetSandboxFD()));
int dummy_fd = -1;
if (using_suid_sandbox_) {
dummy_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
CHECK(dummy_fd >= 0);
fds_to_map.push_back(std::make_pair(dummy_fd, kZygoteIdFd));
}
base::ProcessHandle process = -1;
base::LaunchOptions options;
options.fds_to_remap = &fds_to_map;
base::LaunchProcess(cmd_line.argv(), options, &process);
CHECK(process != -1) << "Failed to launch zygote process";
if (using_suid_sandbox_) {
std::vector<int> fds_vec;
const int kExpectedLength = sizeof(kZygoteHelloMessage);
char buf[kExpectedLength];
const ssize_t len = UnixDomainSocket::RecvMsg(fds[0], buf, sizeof(buf),
&fds_vec);
CHECK(len == kExpectedLength) << "Incorrect zygote magic length";
CHECK(0 == strcmp(buf, kZygoteHelloMessage))
<< "Incorrect zygote hello";
std::string inode_output;
ino_t inode = 0;
if (base::FileDescriptorGetInode(&inode, dummy_fd)) {
close(dummy_fd);
std::vector<std::string> get_inode_cmdline;
get_inode_cmdline.push_back(sandbox_binary_);
get_inode_cmdline.push_back(base::kFindInodeSwitch);
get_inode_cmdline.push_back(base::Int64ToString(inode));
CommandLine get_inode_cmd(get_inode_cmdline);
if (base::GetAppOutput(get_inode_cmd, &inode_output)) {
base::StringToInt(inode_output, &pid_);
}
}
CHECK(pid_ > 0) << "Did not find zygote process (using sandbox binary "
<< sandbox_binary_ << ")";
if (process != pid_) {
base::EnsureProcessGetsReaped(process);
}
} else {
pid_ = process;
}
close(fds[1]);
control_fd_ = fds[0];
Pickle pickle;
pickle.WriteInt(kZygoteCommandGetSandboxStatus);
if (!SendMessage(pickle, NULL))
LOG(FATAL) << "Cannot communicate with zygote";
}
void ZygoteHostImpl::TearDownAfterLastChild() {
bool do_teardown = false;
{
base::AutoLock lock(child_tracking_lock_);
should_teardown_after_last_child_exits_ = true;
do_teardown = list_of_running_zygote_children_.empty();
}
if (do_teardown) {
TearDown();
}
}
void ZygoteHostImpl::TearDown() {
base::AutoLock lock(control_lock_);
if (control_fd_ > -1) {
if (IGNORE_EINTR(close(control_fd_))) {
PLOG(ERROR) << "Could not close Zygote control channel.";
NOTREACHED();
}
control_fd_ = -1;
}
}
void ZygoteHostImpl::ZygoteChildBorn(pid_t process) {
base::AutoLock lock(child_tracking_lock_);
bool new_element_inserted =
list_of_running_zygote_children_.insert(process).second;
DCHECK(new_element_inserted);
}
void ZygoteHostImpl::ZygoteChildDied(pid_t process) {
bool do_teardown = false;
{
base::AutoLock lock(child_tracking_lock_);
size_t num_erased = list_of_running_zygote_children_.erase(process);
DCHECK_EQ(1U, num_erased);
do_teardown = should_teardown_after_last_child_exits_ &&
list_of_running_zygote_children_.empty();
}
if (do_teardown) {
TearDown();
}
}
bool ZygoteHostImpl::SendMessage(const Pickle& data,
const std::vector<int>* fds) {
DCHECK_NE(-1, control_fd_);
CHECK(data.size() <= kZygoteMaxMessageLength)
<< "Trying to send too-large message to zygote (sending " << data.size()
<< " bytes, max is " << kZygoteMaxMessageLength << ")";
CHECK(!fds || fds->size() <= UnixDomainSocket::kMaxFileDescriptors)
<< "Trying to send message with too many file descriptors to zygote "
<< "(sending " << fds->size() << ", max is "
<< UnixDomainSocket::kMaxFileDescriptors << ")";
return UnixDomainSocket::SendMsg(control_fd_,
data.data(), data.size(),
fds ? *fds : std::vector<int>());
}
ssize_t ZygoteHostImpl::ReadReply(void* buf, size_t buf_len) {
DCHECK_NE(-1, control_fd_);
if (!have_read_sandbox_status_word_) {
if (HANDLE_EINTR(read(control_fd_, &sandbox_status_,
sizeof(sandbox_status_))) !=
sizeof(sandbox_status_)) {
return -1;
}
have_read_sandbox_status_word_ = true;
}
return HANDLE_EINTR(read(control_fd_, buf, buf_len));
}
pid_t ZygoteHostImpl::ForkRequest(
const std::vector<std::string>& argv,
const std::vector<FileDescriptorInfo>& mapping,
const std::string& process_type) {
DCHECK(init_);
Pickle pickle;
pickle.WriteInt(kZygoteCommandFork);
pickle.WriteString(process_type);
pickle.WriteInt(argv.size());
for (std::vector<std::string>::const_iterator
i = argv.begin(); i != argv.end(); ++i)
pickle.WriteString(*i);
pickle.WriteInt(mapping.size());
std::vector<int> fds;
std::vector<linked_ptr<base::ScopedFD> > autodelete_fds;
for (std::vector<FileDescriptorInfo>::const_iterator
i = mapping.begin(); i != mapping.end(); ++i) {
pickle.WriteUInt32(i->id);
fds.push_back(i->fd.fd);
if (i->fd.auto_close) {
linked_ptr<base::ScopedFD> ptr(new base::ScopedFD(fds.back()));
autodelete_fds.push_back(ptr);
}
}
pid_t pid;
{
base::AutoLock lock(control_lock_);
if (!SendMessage(pickle, &fds))
return base::kNullProcessHandle;
static const unsigned kMaxReplyLength = 2048;
char buf[kMaxReplyLength];
const ssize_t len = ReadReply(buf, sizeof(buf));
Pickle reply_pickle(buf, len);
PickleIterator iter(reply_pickle);
if (len <= 0 || !reply_pickle.ReadInt(&iter, &pid))
return base::kNullProcessHandle;
std::string uma_name;
int uma_sample;
int uma_boundary_value;
if (reply_pickle.ReadString(&iter, &uma_name) &&
!uma_name.empty() &&
reply_pickle.ReadInt(&iter, &uma_sample) &&
reply_pickle.ReadInt(&iter, &uma_boundary_value)) {
static base::HistogramBase* uma_histogram;
if (!uma_histogram || uma_histogram->histogram_name() != uma_name) {
uma_histogram = base::LinearHistogram::FactoryGet(
uma_name, 1,
uma_boundary_value,
uma_boundary_value + 1,
base::HistogramBase::kUmaTargetedHistogramFlag);
}
uma_histogram->Add(uma_sample);
}
if (pid <= 0)
return base::kNullProcessHandle;
}
#if !defined(OS_OPENBSD)
const int kLowestRendererOomScore = 300;
AdjustRendererOOMScore(pid, kLowestRendererOomScore);
#endif
ZygoteChildBorn(pid);
return pid;
}
#if !defined(OS_OPENBSD)
void ZygoteHostImpl::AdjustRendererOOMScore(base::ProcessHandle pid,
int score) {
static bool selinux;
static bool selinux_valid = false;
if (!selinux_valid) {
const base::FilePath kSelinuxPath("/selinux");
base::FileEnumerator en(kSelinuxPath, false, base::FileEnumerator::FILES);
bool has_selinux_files = !en.Next().empty();
selinux = access(kSelinuxPath.value().c_str(), X_OK) == 0 &&
has_selinux_files;
selinux_valid = true;
}
if (using_suid_sandbox_ && !selinux) {
#if defined(USE_TCMALLOC)
if (IsHeapProfilerRunning())
return;
#endif
std::vector<std::string> adj_oom_score_cmdline;
adj_oom_score_cmdline.push_back(sandbox_binary_);
adj_oom_score_cmdline.push_back(sandbox::kAdjustOOMScoreSwitch);
adj_oom_score_cmdline.push_back(base::Int64ToString(pid));
adj_oom_score_cmdline.push_back(base::IntToString(score));
base::ProcessHandle sandbox_helper_process;
if (base::LaunchProcess(adj_oom_score_cmdline, base::LaunchOptions(),
&sandbox_helper_process)) {
base::EnsureProcessGetsReaped(sandbox_helper_process);
}
} else if (!using_suid_sandbox_) {
if (!base::AdjustOOMScore(pid, score))
PLOG(ERROR) << "Failed to adjust OOM score of renderer with pid " << pid;
}
}
#endif
void ZygoteHostImpl::EnsureProcessTerminated(pid_t process) {
DCHECK(init_);
Pickle pickle;
pickle.WriteInt(kZygoteCommandReap);
pickle.WriteInt(process);
if (!SendMessage(pickle, NULL))
LOG(ERROR) << "Failed to send Reap message to zygote";
ZygoteChildDied(process);
}
base::TerminationStatus ZygoteHostImpl::GetTerminationStatus(
base::ProcessHandle handle,
bool known_dead,
int* exit_code) {
DCHECK(init_);
Pickle pickle;
pickle.WriteInt(kZygoteCommandGetTerminationStatus);
pickle.WriteBool(known_dead);
pickle.WriteInt(handle);
static const unsigned kMaxMessageLength = 128;
char buf[kMaxMessageLength];
ssize_t len;
{
base::AutoLock lock(control_lock_);
if (!SendMessage(pickle, NULL))
LOG(ERROR) << "Failed to send GetTerminationStatus message to zygote";
len = ReadReply(buf, sizeof(buf));
}
if (exit_code)
*exit_code = RESULT_CODE_NORMAL_EXIT;
int status = base::TERMINATION_STATUS_NORMAL_TERMINATION;
if (len == -1) {
LOG(WARNING) << "Error reading message from zygote: " << errno;
} else if (len == 0) {
LOG(WARNING) << "Socket closed prematurely.";
} else {
Pickle read_pickle(buf, len);
int tmp_status, tmp_exit_code;
PickleIterator iter(read_pickle);
if (!read_pickle.ReadInt(&iter, &tmp_status) ||
!read_pickle.ReadInt(&iter, &tmp_exit_code)) {
LOG(WARNING)
<< "Error parsing GetTerminationStatus response from zygote.";
} else {
if (exit_code)
*exit_code = tmp_exit_code;
status = tmp_status;
}
}
if (status != base::TERMINATION_STATUS_STILL_RUNNING) {
ZygoteChildDied(handle);
}
return static_cast<base::TerminationStatus>(status);
}
pid_t ZygoteHostImpl::GetPid() const {
return pid_;
}
pid_t ZygoteHostImpl::GetSandboxHelperPid() const {
return RenderSandboxHostLinux::GetInstance()->pid();
}
int ZygoteHostImpl::GetSandboxStatus() const {
if (have_read_sandbox_status_word_)
return sandbox_status_;
return 0;
}
}