This source file includes following definitions.
- BecomeNaClLoader
- ChildNaClLoaderInit
- HandleForkRequest
- HandleGetTerminationStatusRequest
- IsSandboxed
- HonorRequestAndReply
- HandleZygoteRequest
- CheckRDebug
- CheckReservedAtZero
- __asan_default_options
- main
#include "components/nacl/loader/nacl_helper_linux.h"
#include <errno.h>
#include <fcntl.h>
#include <link.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include <vector>
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.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 "base/rand_util.h"
#include "components/nacl/loader/nacl_listener.h"
#include "components/nacl/loader/nacl_sandbox_linux.h"
#include "content/public/common/zygote_fork_delegate_linux.h"
#include "crypto/nss_util.h"
#include "ipc/ipc_descriptors.h"
#include "ipc/ipc_switches.h"
#include "sandbox/linux/services/libc_urandom_override.h"
namespace {
struct NaClLoaderSystemInfo {
size_t prereserved_sandbox_size;
long number_of_cores;
};
void BecomeNaClLoader(const std::vector<int>& child_fds,
const NaClLoaderSystemInfo& system_info,
bool uses_nonsfi_mode) {
VLOG(1) << "NaCl loader: setting up IPC descriptor";
if (IGNORE_EINTR(close(kNaClZygoteDescriptor)) != 0)
LOG(ERROR) << "close(kNaClZygoteDescriptor) failed.";
bool sandbox_initialized = InitializeBPFSandbox();
if (!sandbox_initialized) {
LOG(ERROR) << "Could not initialize NaCl's second "
<< "layer sandbox (seccomp-bpf).";
}
base::GlobalDescriptors::GetInstance()->Set(
kPrimaryIPCChannel,
child_fds[content::ZygoteForkDelegate::kBrowserFDIndex]);
base::MessageLoopForIO main_message_loop;
NaClListener listener;
listener.set_uses_nonsfi_mode(uses_nonsfi_mode);
listener.set_prereserved_sandbox_size(system_info.prereserved_sandbox_size);
listener.set_number_of_cores(system_info.number_of_cores);
listener.Listen();
_exit(0);
}
void ChildNaClLoaderInit(const std::vector<int>& child_fds,
const NaClLoaderSystemInfo& system_info,
bool uses_nonsfi_mode) {
const int parent_fd = child_fds[content::ZygoteForkDelegate::kParentFDIndex];
const int dummy_fd = child_fds[content::ZygoteForkDelegate::kDummyFDIndex];
bool validack = false;
const size_t kMaxReadSize = 1024;
char buffer[kMaxReadSize];
const int nread = HANDLE_EINTR(read(parent_fd, buffer, kMaxReadSize));
const std::string switch_prefix = std::string("--") +
switches::kProcessChannelID + std::string("=");
const size_t len = switch_prefix.length();
if (nread < 0) {
perror("read");
LOG(ERROR) << "read returned " << nread;
} else if (nread > static_cast<int>(len)) {
if (switch_prefix.compare(0, len, buffer, 0, len) == 0) {
VLOG(1) << "NaCl loader is synchronised with Chrome zygote";
CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kProcessChannelID,
std::string(&buffer[len], nread - len));
validack = true;
}
}
if (IGNORE_EINTR(close(dummy_fd)) != 0)
LOG(ERROR) << "close(dummy_fd) failed";
if (IGNORE_EINTR(close(parent_fd)) != 0)
LOG(ERROR) << "close(parent_fd) failed";
if (validack) {
BecomeNaClLoader(child_fds, system_info, uses_nonsfi_mode);
} else {
LOG(ERROR) << "Failed to synch with zygote";
}
_exit(1);
}
bool HandleForkRequest(const std::vector<int>& child_fds,
const NaClLoaderSystemInfo& system_info,
PickleIterator* input_iter,
Pickle* output_pickle) {
bool uses_nonsfi_mode;
if (!input_iter->ReadBool(&uses_nonsfi_mode)) {
LOG(ERROR) << "Could not read uses_nonsfi_mode status";
return false;
}
if (content::ZygoteForkDelegate::kNumPassedFDs != child_fds.size()) {
LOG(ERROR) << "nacl_helper: unexpected number of fds, got "
<< child_fds.size();
return false;
}
VLOG(1) << "nacl_helper: forking";
pid_t child_pid = fork();
if (child_pid < 0) {
PLOG(ERROR) << "*** fork() failed.";
}
if (child_pid == 0) {
ChildNaClLoaderInit(child_fds, system_info, uses_nonsfi_mode);
NOTREACHED();
}
for (size_t i = 0; i < child_fds.size(); i++) {
if (IGNORE_EINTR(close(child_fds[i])) != 0)
LOG(ERROR) << "close(child_fds[i]) failed";
}
VLOG(1) << "nacl_helper: child_pid is " << child_pid;
output_pickle->WriteInt(child_pid);
return true;
}
bool HandleGetTerminationStatusRequest(PickleIterator* input_iter,
Pickle* output_pickle) {
pid_t child_to_wait;
if (!input_iter->ReadInt(&child_to_wait)) {
LOG(ERROR) << "Could not read pid to wait for";
return false;
}
bool known_dead;
if (!input_iter->ReadBool(&known_dead)) {
LOG(ERROR) << "Could not read known_dead status";
return false;
}
int exit_code;
base::TerminationStatus status;
if (known_dead)
status = base::GetKnownDeadTerminationStatus(child_to_wait, &exit_code);
else
status = base::GetTerminationStatus(child_to_wait, &exit_code);
output_pickle->WriteInt(static_cast<int>(status));
output_pickle->WriteInt(exit_code);
return true;
}
bool IsSandboxed() {
int proc_fd = open("/proc/self/exe", O_RDONLY);
if (proc_fd >= 0) {
close(proc_fd);
return false;
}
return true;
}
bool HonorRequestAndReply(int reply_fd,
int command_type,
const std::vector<int>& attached_fds,
const NaClLoaderSystemInfo& system_info,
PickleIterator* input_iter) {
Pickle write_pickle;
bool have_to_reply = false;
switch (command_type) {
case nacl::kNaClForkRequest:
have_to_reply = HandleForkRequest(attached_fds, system_info,
input_iter, &write_pickle);
break;
case nacl::kNaClGetTerminationStatusRequest:
have_to_reply =
HandleGetTerminationStatusRequest(input_iter, &write_pickle);
break;
default:
LOG(ERROR) << "Unsupported command from Zygote";
return false;
}
if (!have_to_reply)
return false;
const std::vector<int> empty;
if (!UnixDomainSocket::SendMsg(reply_fd, write_pickle.data(),
write_pickle.size(), empty)) {
LOG(ERROR) << "*** send() to zygote failed";
return false;
}
return true;
}
bool HandleZygoteRequest(int zygote_ipc_fd,
const NaClLoaderSystemInfo& system_info) {
std::vector<int> fds;
char buf[kNaClMaxIPCMessageLength];
const ssize_t msglen = UnixDomainSocket::RecvMsg(zygote_ipc_fd,
&buf, sizeof(buf), &fds);
if (!IsSandboxed()) {
LOG(ERROR) << "NaCl helper process running without a sandbox!\n"
<< "Most likely you need to configure your SUID sandbox "
<< "correctly";
}
if (msglen == 0 || (msglen == -1 && errno == ECONNRESET)) {
_exit(0);
}
if (msglen < 0) {
PLOG(ERROR) << "nacl_helper: receive from zygote failed";
return false;
}
Pickle read_pickle(buf, msglen);
PickleIterator read_iter(read_pickle);
int command_type;
if (!read_iter.ReadInt(&command_type)) {
LOG(ERROR) << "Unable to read command from Zygote";
return false;
}
return HonorRequestAndReply(zygote_ipc_fd, command_type, fds, system_info,
&read_iter);
}
static const char kNaClHelperReservedAtZero[] = "reserved_at_zero";
static const char kNaClHelperRDebug[] = "r_debug";
static void CheckRDebug(char* argv0) {
std::string r_debug_switch_value =
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kNaClHelperRDebug);
if (!r_debug_switch_value.empty()) {
char* endp;
uintptr_t r_debug_addr = strtoul(r_debug_switch_value.c_str(), &endp, 0);
if (r_debug_addr != 0 && *endp == '\0') {
r_debug* bootstrap_r_debug = reinterpret_cast<r_debug*>(r_debug_addr);
*bootstrap_r_debug = _r_debug;
link_map* l = _r_debug.r_map;
if (l->l_name[0] == '\0')
l->l_name = argv0;
}
}
}
static size_t CheckReservedAtZero() {
size_t prereserved_sandbox_size = 0;
std::string reserved_at_zero_switch_value =
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
kNaClHelperReservedAtZero);
if (!reserved_at_zero_switch_value.empty()) {
char* endp;
prereserved_sandbox_size =
strtoul(reserved_at_zero_switch_value.c_str(), &endp, 0);
if (*endp != '\0')
LOG(ERROR) << "Could not parse reserved_at_zero argument value of "
<< reserved_at_zero_switch_value;
}
return prereserved_sandbox_size;
}
}
#if defined(ADDRESS_SANITIZER)
static const char kAsanDefaultOptionsNaCl[] = "handle_segv=0";
extern "C"
__attribute__((no_sanitize_address))
__attribute__((used))
__attribute__((visibility("default")))
const char* __asan_default_options() {
return kAsanDefaultOptionsNaCl;
}
#endif
int main(int argc, char* argv[]) {
CommandLine::Init(argc, argv);
base::AtExitManager exit_manager;
base::RandUint64();
sandbox::InitLibcUrandomOverrides();
#if defined(USE_NSS)
crypto::DisableNSSForkCheck();
crypto::ForceNSSNoDBInit();
crypto::LoadNSSLibraries();
#endif
const NaClLoaderSystemInfo system_info = {
CheckReservedAtZero(),
sysconf(_SC_NPROCESSORS_ONLN)
};
CheckRDebug(argv[0]);
CHECK(!IsSandboxed()) << "Unexpectedly sandboxed!";
const std::vector<int> empty;
if (!UnixDomainSocket::SendMsg(kNaClZygoteDescriptor,
kNaClHelperStartupAck,
sizeof(kNaClHelperStartupAck), empty)) {
LOG(ERROR) << "*** send() to zygote failed";
}
while (true) {
bool request_handled = HandleZygoteRequest(kNaClZygoteDescriptor,
system_info);
DCHECK(request_handled);
}
NOTREACHED();
}