This source file includes following definitions.
- CreateMemoryObject
- CreateMemoryObject
- BrokerDuplicateHandle
- AttachDebugExceptionHandler
- SetUpIPCAdapter
- QueryKnownToValidate
- SetKnownToValidate
- ResolveFileToken
- main_loop_
- Send
- OnMessageReceived
- OnStart
#include "components/nacl/loader/nacl_listener.h"
#include <errno.h>
#include <stdlib.h>
#if defined(OS_POSIX)
#include <unistd.h>
#endif
#include "base/command_line.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/rand_util.h"
#include "components/nacl/common/nacl_messages.h"
#include "components/nacl/loader/nacl_ipc_adapter.h"
#include "components/nacl/loader/nacl_validation_db.h"
#include "components/nacl/loader/nacl_validation_query.h"
#include "ipc/ipc_channel_handle.h"
#include "ipc/ipc_switches.h"
#include "ipc/ipc_sync_channel.h"
#include "ipc/ipc_sync_message_filter.h"
#include "native_client/src/public/chrome_main.h"
#include "native_client/src/public/nacl_app.h"
#include "native_client/src/trusted/validator/nacl_file_info.h"
#if defined(OS_POSIX)
#include "base/file_descriptor_posix.h"
#endif
#if defined(OS_LINUX)
#include "components/nacl/loader/nonsfi/nonsfi_main.h"
#include "content/public/common/child_process_sandbox_support_linux.h"
#include "ppapi/proxy/plugin_main_irt.h"
#endif
#if defined(OS_WIN)
#include <fcntl.h>
#include <io.h>
#include "content/public/common/sandbox_init.h"
#endif
namespace {
#if defined(OS_MACOSX)
base::subtle::Atomic32 g_shm_fd = -1;
int CreateMemoryObject(size_t size, int executable) {
if (executable && size > 0) {
int result_fd = base::subtle::NoBarrier_AtomicExchange(&g_shm_fd, -1);
if (result_fd != -1) {
if (lseek(result_fd, size - 1, SEEK_SET) == -1) {
LOG(ERROR) << "lseek() failed: " << errno;
return -1;
}
if (write(result_fd, "", 1) != 1) {
LOG(ERROR) << "write() failed: " << errno;
return -1;
}
return result_fd;
}
}
return -1;
}
#elif defined(OS_LINUX)
int CreateMemoryObject(size_t size, int executable) {
return content::MakeSharedMemorySegmentViaIPC(size, executable);
}
#elif defined(OS_WIN)
NaClListener* g_listener;
int BrokerDuplicateHandle(NaClHandle source_handle,
uint32_t process_id,
NaClHandle* target_handle,
uint32_t desired_access,
uint32_t options) {
return content::BrokerDuplicateHandle(source_handle, process_id,
target_handle, desired_access,
options);
}
int AttachDebugExceptionHandler(const void* info, size_t info_size) {
std::string info_string(reinterpret_cast<const char*>(info), info_size);
bool result = false;
if (!g_listener->Send(new NaClProcessMsg_AttachDebugExceptionHandler(
info_string, &result)))
return false;
return result;
}
#endif
void SetUpIPCAdapter(IPC::ChannelHandle* handle,
scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
struct NaClApp* nap,
int nacl_fd) {
scoped_refptr<NaClIPCAdapter> ipc_adapter(
new NaClIPCAdapter(*handle, message_loop_proxy.get()));
ipc_adapter->ConnectChannel();
#if defined(OS_POSIX)
handle->socket =
base::FileDescriptor(ipc_adapter->TakeClientFileDescriptor(), true);
#endif
NaClAppSetDesc(nap, nacl_fd, ipc_adapter->MakeNaClDesc());
}
}
class BrowserValidationDBProxy : public NaClValidationDB {
public:
explicit BrowserValidationDBProxy(NaClListener* listener)
: listener_(listener) {
}
virtual bool QueryKnownToValidate(const std::string& signature) OVERRIDE {
bool result = false;
if (!listener_->Send(new NaClProcessMsg_QueryKnownToValidate(signature,
&result))) {
LOG(ERROR) << "Failed to query NaCl validation cache.";
result = false;
}
return result;
}
virtual void SetKnownToValidate(const std::string& signature) OVERRIDE {
if (!listener_->Send(new NaClProcessMsg_SetKnownToValidate(signature))) {
LOG(ERROR) << "Failed to update NaCl validation cache.";
}
}
virtual bool ResolveFileToken(struct NaClFileToken* file_token,
int32* fd, std::string* path) OVERRIDE {
*fd = -1;
*path = "";
if (file_token->lo == 0 && file_token->hi == 0) {
return false;
}
IPC::PlatformFileForTransit ipc_fd = IPC::InvalidPlatformFileForTransit();
base::FilePath ipc_path;
if (!listener_->Send(new NaClProcessMsg_ResolveFileToken(file_token->lo,
file_token->hi,
&ipc_fd,
&ipc_path))) {
return false;
}
if (ipc_fd == IPC::InvalidPlatformFileForTransit()) {
return false;
}
base::PlatformFile handle =
IPC::PlatformFileForTransitToPlatformFile(ipc_fd);
#if defined(OS_WIN)
*fd = reinterpret_cast<uintptr_t>(handle);
#else
*fd = handle;
#endif
*path = ipc_path.AsUTF8Unsafe();
return true;
}
private:
NaClListener* listener_;
};
NaClListener::NaClListener() : shutdown_event_(true, false),
io_thread_("NaCl_IOThread"),
uses_nonsfi_mode_(false),
#if defined(OS_LINUX)
prereserved_sandbox_size_(0),
#endif
#if defined(OS_POSIX)
number_of_cores_(-1),
#endif
main_loop_(NULL) {
io_thread_.StartWithOptions(
base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
#if defined(OS_WIN)
DCHECK(g_listener == NULL);
g_listener = this;
#endif
}
NaClListener::~NaClListener() {
NOTREACHED();
shutdown_event_.Signal();
#if defined(OS_WIN)
g_listener = NULL;
#endif
}
bool NaClListener::Send(IPC::Message* msg) {
DCHECK(main_loop_ != NULL);
if (base::MessageLoop::current() == main_loop_) {
return channel_->Send(msg);
} else {
return filter_->Send(msg);
}
}
void NaClListener::Listen() {
std::string channel_name =
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kProcessChannelID);
channel_.reset(new IPC::SyncChannel(
this, io_thread_.message_loop_proxy().get(), &shutdown_event_));
filter_ = new IPC::SyncMessageFilter(&shutdown_event_);
channel_->AddFilter(filter_.get());
channel_->Init(channel_name, IPC::Channel::MODE_CLIENT, true);
main_loop_ = base::MessageLoop::current();
main_loop_->Run();
}
bool NaClListener::OnMessageReceived(const IPC::Message& msg) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(NaClListener, msg)
IPC_MESSAGE_HANDLER(NaClProcessMsg_Start, OnStart)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void NaClListener::OnStart(const nacl::NaClStartParams& params) {
#if !defined(OS_LINUX)
CHECK(!uses_nonsfi_mode_) << "Non-SFI NaCl is only supported on Linux";
#endif
#if defined(OS_LINUX) || defined(OS_MACOSX)
int urandom_fd = dup(base::GetUrandomFD());
if (urandom_fd < 0) {
LOG(ERROR) << "Failed to dup() the urandom FD";
return;
}
NaClChromeMainSetUrandomFd(urandom_fd);
#endif
struct NaClApp* nap = NULL;
if (!uses_nonsfi_mode_) {
NaClChromeMainInit();
nap = NaClAppCreate();
if (nap == NULL) {
LOG(ERROR) << "NaClAppCreate() failed";
return;
}
}
IPC::ChannelHandle browser_handle;
IPC::ChannelHandle ppapi_renderer_handle;
if (params.enable_ipc_proxy) {
browser_handle = IPC::Channel::GenerateVerifiedChannelID("nacl");
ppapi_renderer_handle = IPC::Channel::GenerateVerifiedChannelID("nacl");
#if defined(OS_LINUX)
if (uses_nonsfi_mode_) {
int browser_server_ppapi_fd;
int browser_client_ppapi_fd;
int renderer_server_ppapi_fd;
int renderer_client_ppapi_fd;
if (!IPC::SocketPair(
&browser_server_ppapi_fd, &browser_client_ppapi_fd) ||
!IPC::SocketPair(
&renderer_server_ppapi_fd, &renderer_client_ppapi_fd)) {
LOG(ERROR) << "Failed to create sockets for IPC.";
return;
}
SetIPCFileDescriptors(
browser_server_ppapi_fd, renderer_server_ppapi_fd);
browser_handle.socket =
base::FileDescriptor(browser_client_ppapi_fd, true);
ppapi_renderer_handle.socket =
base::FileDescriptor(renderer_client_ppapi_fd, true);
} else {
#endif
SetUpIPCAdapter(&browser_handle, io_thread_.message_loop_proxy(),
nap, NACL_CHROME_DESC_BASE);
SetUpIPCAdapter(&ppapi_renderer_handle, io_thread_.message_loop_proxy(),
nap, NACL_CHROME_DESC_BASE + 1);
#if defined(OS_LINUX)
}
#endif
}
IPC::ChannelHandle trusted_renderer_handle =
IPC::Channel::GenerateVerifiedChannelID("nacl");
trusted_listener_ = new NaClTrustedListener(
trusted_renderer_handle, io_thread_.message_loop_proxy(),
&shutdown_event_);
#if defined(OS_POSIX)
trusted_renderer_handle.socket = base::FileDescriptor(
trusted_listener_->TakeClientFileDescriptor(), true);
#endif
if (!Send(new NaClProcessHostMsg_PpapiChannelsCreated(
browser_handle, ppapi_renderer_handle, trusted_renderer_handle)))
LOG(ERROR) << "Failed to send IPC channel handle to NaClProcessHost.";
std::vector<nacl::FileDescriptor> handles = params.handles;
#if defined(OS_LINUX)
if (uses_nonsfi_mode_) {
if (params.uses_irt) {
LOG(ERROR) << "IRT must not be used for non-SFI NaCl.";
return;
}
CHECK(handles.size() == 1);
int imc_bootstrap_handle = nacl::ToNativeHandle(handles[0]);
nacl::nonsfi::MainStart(imc_bootstrap_handle);
return;
}
#endif
struct NaClChromeMainArgs* args = NaClChromeMainArgsCreate();
if (args == NULL) {
LOG(ERROR) << "NaClChromeMainArgsCreate() failed";
return;
}
#if defined(OS_LINUX) || defined(OS_MACOSX)
args->number_of_cores = number_of_cores_;
args->create_memory_object_func = CreateMemoryObject;
# if defined(OS_MACOSX)
CHECK(handles.size() >= 1);
g_shm_fd = nacl::ToNativeHandle(handles[handles.size() - 1]);
handles.pop_back();
# endif
#endif
if (params.uses_irt) {
CHECK(handles.size() >= 1);
NaClHandle irt_handle = nacl::ToNativeHandle(handles[handles.size() - 1]);
handles.pop_back();
#if defined(OS_WIN)
args->irt_fd = _open_osfhandle(reinterpret_cast<intptr_t>(irt_handle),
_O_RDONLY | _O_BINARY);
if (args->irt_fd < 0) {
LOG(ERROR) << "_open_osfhandle() failed";
return;
}
#else
args->irt_fd = irt_handle;
#endif
} else {
args->irt_fd = -1;
}
if (params.validation_cache_enabled) {
CHECK_EQ(params.validation_cache_key.length(), (size_t) 64);
args->validation_cache = CreateValidationCache(
new BrowserValidationDBProxy(this), params.validation_cache_key,
params.version);
}
CHECK(handles.size() == 1);
args->imc_bootstrap_handle = nacl::ToNativeHandle(handles[0]);
args->enable_exception_handling = params.enable_exception_handling;
args->enable_debug_stub = params.enable_debug_stub;
args->enable_dyncode_syscalls = params.enable_dyncode_syscalls;
if (!params.enable_dyncode_syscalls) {
args->initial_nexe_max_code_bytes = 32 << 20;
args->pnacl_mode = 1;
}
#if defined(OS_LINUX) || defined(OS_MACOSX)
args->debug_stub_server_bound_socket_fd = nacl::ToNativeHandle(
params.debug_stub_server_bound_socket);
#endif
#if defined(OS_WIN)
args->broker_duplicate_handle_func = BrokerDuplicateHandle;
args->attach_debug_exception_handler_func = AttachDebugExceptionHandler;
#endif
#if defined(OS_LINUX)
args->prereserved_sandbox_size = prereserved_sandbox_size_;
#endif
NaClChromeMainStartApp(nap, args);
NOTREACHED();
}