This source file includes following definitions.
- WriteHandle
- ScanParam
- HandleWriter
- ScanParam
- ScanParam
- ScanParam
- ScanParam
- ScanTuple
- ScanTuple
- ScanTuple
- ScanTuple
- ScanMessage
- ScanReply
- UpdateReservedQuota
- max_written_offset_
- SetMaxWrittenOffset
- Grow
- ScanMessage
- ScanUntrustedMessage
- RegisterSyncMessageForReply
- GetFile
- AuditNestedMessage
#include "ppapi/proxy/nacl_message_scanner.h"
#include <vector>
#include "base/bind.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_message_macros.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/proxy/resource_message_params.h"
#include "ppapi/proxy/serialized_handle.h"
#include "ppapi/proxy/serialized_var.h"
class NaClDescImcShm;
namespace IPC {
class Message;
}
using ppapi::proxy::ResourceMessageReplyParams;
using ppapi::proxy::SerializedHandle;
using ppapi::proxy::SerializedVar;
namespace {
typedef std::vector<SerializedHandle> Handles;
struct ScanningResults {
ScanningResults() : handle_index(0), pp_resource(0) {}
Handles handles;
int handle_index;
scoped_ptr<IPC::Message> new_msg;
PP_Resource pp_resource;
base::Callback<void(PP_Resource, const IPC::Message&, SerializedHandle*)>
nested_msg_callback;
};
void WriteHandle(int handle_index,
const SerializedHandle& handle,
IPC::Message* msg) {
SerializedHandle::WriteHeader(handle.header(), msg);
msg->WriteBool(true);
msg->WriteInt(handle_index);
}
void ScanParam(const SerializedHandle& handle, ScanningResults* results) {
results->handles.push_back(handle);
if (results->new_msg)
WriteHandle(results->handle_index++, handle, results->new_msg.get());
}
void HandleWriter(int* handle_index,
IPC::Message* m,
const SerializedHandle& handle) {
WriteHandle((*handle_index)++, handle, m);
}
void ScanParam(const SerializedVar& var, ScanningResults* results) {
std::vector<SerializedHandle*> var_handles = var.GetHandles();
for (size_t i = 0; i < var_handles.size(); ++i)
results->handles.push_back(*var_handles[i]);
if (results->new_msg)
var.WriteDataToMessage(results->new_msg.get(),
base::Bind(&HandleWriter, &results->handle_index));
}
void ScanParam(const ResourceMessageReplyParams& params,
ScanningResults* results) {
results->pp_resource = params.pp_resource();
if (params.handles().empty()) {
results->new_msg.reset(NULL);
return;
}
if (results->new_msg) {
params.WriteReplyHeader(results->new_msg.get());
results->new_msg->WriteInt(static_cast<int>(params.handles().size()));
}
for (Handles::const_iterator iter = params.handles().begin();
iter != params.handles().end();
++iter) {
ScanParam(*iter, results);
}
params.ConsumeHandles();
}
void ScanParam(const IPC::Message& param, ScanningResults* results) {
if (results->pp_resource && !results->nested_msg_callback.is_null()) {
SerializedHandle* handle = NULL;
if (results->handles.size() == 1)
handle = &results->handles[0];
results->nested_msg_callback.Run(results->pp_resource, param, handle);
}
if (results->new_msg)
IPC::WriteParam(results->new_msg.get(), param);
}
template <class T>
void ScanParam(const T& param, ScanningResults* results) {
if (results->new_msg)
IPC::WriteParam(results->new_msg.get(), param);
}
template <class A>
void ScanTuple(const Tuple1<A>& t1, ScanningResults* results) {
ScanParam(t1.a, results);
}
template <class A, class B>
void ScanTuple(const Tuple2<A, B>& t1, ScanningResults* results) {
ScanParam(t1.a, results);
ScanParam(t1.b, results);
}
template <class A, class B, class C>
void ScanTuple(const Tuple3<A, B, C>& t1, ScanningResults* results) {
ScanParam(t1.a, results);
ScanParam(t1.b, results);
ScanParam(t1.c, results);
}
template <class A, class B, class C, class D>
void ScanTuple(const Tuple4<A, B, C, D>& t1, ScanningResults* results) {
ScanParam(t1.a, results);
ScanParam(t1.b, results);
ScanParam(t1.c, results);
ScanParam(t1.d, results);
}
template <class MessageType>
class MessageScannerImpl {
public:
explicit MessageScannerImpl(const IPC::Message* msg)
: msg_(static_cast<const MessageType*>(msg)) {
}
bool ScanMessage(ScanningResults* results) {
typename TupleTypes<typename MessageType::Schema::Param>::ValueTuple params;
if (!MessageType::Read(msg_, ¶ms))
return false;
ScanTuple(params, results);
return true;
}
bool ScanReply(ScanningResults* results) {
typename TupleTypes<typename MessageType::Schema::ReplyParam>::ValueTuple
params;
if (!MessageType::ReadReplyParam(msg_, ¶ms))
return false;
if (results->new_msg) {
results->new_msg->set_reply();
int id = IPC::SyncMessage::GetMessageId(*msg_);
results->new_msg->WriteInt(id);
}
ScanTuple(params, results);
return true;
}
private:
const MessageType* msg_;
};
}
#define CASE_FOR_MESSAGE(MESSAGE_TYPE) \
case MESSAGE_TYPE::ID: { \
MessageScannerImpl<MESSAGE_TYPE> scanner(&msg); \
if (rewrite_msg) \
results.new_msg.reset( \
new IPC::Message(msg.routing_id(), msg.type(), \
IPC::Message::PRIORITY_NORMAL)); \
if (!scanner.ScanMessage(&results)) \
return false; \
break; \
}
#define CASE_FOR_REPLY(MESSAGE_TYPE) \
case MESSAGE_TYPE::ID: { \
MessageScannerImpl<MESSAGE_TYPE> scanner(&msg); \
if (rewrite_msg) \
results.new_msg.reset( \
new IPC::Message(msg.routing_id(), msg.type(), \
IPC::Message::PRIORITY_NORMAL)); \
if (!scanner.ScanReply(&results)) \
return false; \
break; \
}
namespace ppapi {
namespace proxy {
class SerializedHandle;
NaClMessageScanner::FileSystem::FileSystem()
: reserved_quota_(0) {
}
NaClMessageScanner::FileSystem::~FileSystem() {
}
bool NaClMessageScanner::FileSystem::UpdateReservedQuota(int64_t delta) {
base::AutoLock lock(lock_);
if (std::numeric_limits<int64_t>::max() - reserved_quota_ < delta)
return false;
if (reserved_quota_ + delta < 0)
return false;
reserved_quota_ += delta;
return true;
}
NaClMessageScanner::FileIO::FileIO(FileSystem* file_system,
int64_t max_written_offset)
: file_system_(file_system),
max_written_offset_(max_written_offset) {
}
NaClMessageScanner::FileIO::~FileIO() {
}
void NaClMessageScanner::FileIO::SetMaxWrittenOffset(
int64_t max_written_offset) {
base::AutoLock lock(lock_);
max_written_offset_ = max_written_offset;
}
bool NaClMessageScanner::FileIO::Grow(int64_t amount) {
base::AutoLock lock(lock_);
DCHECK(amount > 0);
if (!file_system_->UpdateReservedQuota(-amount))
return false;
max_written_offset_ += amount;
return true;
}
NaClMessageScanner::NaClMessageScanner() {
}
NaClMessageScanner::~NaClMessageScanner() {
for (FileSystemMap::iterator it = file_systems_.begin();
it != file_systems_.end(); ++it)
delete it->second;
for (FileIOMap::iterator it = files_.begin(); it != files_.end(); ++it)
delete it->second;
}
bool NaClMessageScanner::ScanMessage(
const IPC::Message& msg,
std::vector<SerializedHandle>* handles,
scoped_ptr<IPC::Message>* new_msg_ptr) {
DCHECK(handles);
DCHECK(handles->empty());
DCHECK(new_msg_ptr);
DCHECK(!new_msg_ptr->get());
bool rewrite_msg =
#if defined(OS_WIN)
true;
#else
false;
#endif
ScanningResults results;
results.nested_msg_callback =
base::Bind(&NaClMessageScanner::AuditNestedMessage,
base::Unretained(this));
switch (msg.type()) {
CASE_FOR_MESSAGE(PpapiMsg_PPBAudio_NotifyAudioStreamCreated)
CASE_FOR_MESSAGE(PpapiMsg_PPPMessaging_HandleMessage)
CASE_FOR_MESSAGE(PpapiPluginMsg_ResourceReply)
case IPC_REPLY_ID: {
int id = IPC::SyncMessage::GetMessageId(msg);
PendingSyncMsgMap::iterator iter(pending_sync_msgs_.find(id));
if (iter == pending_sync_msgs_.end()) {
NOTREACHED();
return false;
}
uint32_t type = iter->second;
pending_sync_msgs_.erase(iter);
switch (type) {
CASE_FOR_REPLY(PpapiHostMsg_PPBGraphics3D_CreateTransferBuffer)
CASE_FOR_REPLY(PpapiHostMsg_PPBImageData_CreateSimple)
CASE_FOR_REPLY(PpapiHostMsg_ResourceSyncCall)
CASE_FOR_REPLY(PpapiHostMsg_SharedMemory_CreateSharedMemory)
default:
break;
}
break;
}
default:
break;
}
if (!results.handles.empty()) {
handles->swap(results.handles);
*new_msg_ptr = results.new_msg.Pass();
}
return true;
}
void NaClMessageScanner::ScanUntrustedMessage(
const IPC::Message& untrusted_msg,
scoped_ptr<IPC::Message>* new_msg_ptr) {
if (untrusted_msg.is_sync())
RegisterSyncMessageForReply(untrusted_msg);
if (untrusted_msg.type() == PpapiHostMsg_ResourceCall::ID) {
ResourceMessageCallParams params;
IPC::Message nested_msg;
if (!UnpackMessage<PpapiHostMsg_ResourceCall>(
untrusted_msg, ¶ms, &nested_msg))
return;
switch (nested_msg.type()) {
case PpapiHostMsg_FileIO_Close::ID: {
FileIOMap::iterator it = files_.find(params.pp_resource());
if (it == files_.end())
return;
FileGrowth file_growth;
if (!UnpackMessage<PpapiHostMsg_FileIO_Close>(
nested_msg, &file_growth))
return;
int64_t trusted_max_written_offset = it->second->max_written_offset();
delete it->second;
files_.erase(it);
if (trusted_max_written_offset > file_growth.max_written_offset) {
new_msg_ptr->reset(
new PpapiHostMsg_ResourceCall(
params,
PpapiHostMsg_FileIO_Close(
FileGrowth(trusted_max_written_offset, 0))));
}
}
case PpapiHostMsg_FileIO_SetLength::ID: {
FileIOMap::iterator it = files_.find(params.pp_resource());
if (it == files_.end())
return;
int64_t length = 0;
if (!UnpackMessage<PpapiHostMsg_FileIO_SetLength>(
nested_msg, &length))
return;
if (length < 0)
return;
int64_t trusted_max_written_offset = it->second->max_written_offset();
int64_t increase = length - trusted_max_written_offset;
if (increase <= 0)
return;
if (!it->second->Grow(increase)) {
new_msg_ptr->reset(
new PpapiHostMsg_ResourceCall(
params,
PpapiHostMsg_FileIO_SetLength(-1)));
}
break;
}
case PpapiHostMsg_FileSystem_ReserveQuota::ID: {
int64_t amount = 0;
FileGrowthMap file_growths;
if (!UnpackMessage<PpapiHostMsg_FileSystem_ReserveQuota>(
nested_msg, &amount, &file_growths))
return;
bool audit_failed = false;
for (FileGrowthMap::iterator it = file_growths.begin();
it != file_growths.end(); ++it) {
FileIOMap::iterator file_it = files_.find(it->first);
if (file_it == files_.end())
continue;
int64_t trusted_max_written_offset =
file_it->second->max_written_offset();
if (trusted_max_written_offset > it->second.max_written_offset) {
audit_failed = true;
it->second.max_written_offset = trusted_max_written_offset;
}
if (it->second.append_mode_write_amount < 0) {
audit_failed = true;
it->second.append_mode_write_amount = 0;
}
}
if (audit_failed) {
new_msg_ptr->reset(
new PpapiHostMsg_ResourceCall(
params,
PpapiHostMsg_FileSystem_ReserveQuota(
amount, file_growths)));
}
break;
}
case PpapiHostMsg_ResourceDestroyed::ID: {
PP_Resource resource;
if (!UnpackMessage<PpapiHostMsg_ResourceDestroyed>(
nested_msg, &resource))
return;
FileSystemMap::iterator fs_it = file_systems_.find(resource);
if (fs_it != file_systems_.end()) {
delete fs_it->second;
file_systems_.erase(fs_it);
}
}
}
}
}
void NaClMessageScanner::RegisterSyncMessageForReply(const IPC::Message& msg) {
int msg_id = IPC::SyncMessage::GetMessageId(msg);
DCHECK(pending_sync_msgs_.find(msg_id) == pending_sync_msgs_.end());
pending_sync_msgs_[msg_id] = msg.type();
}
NaClMessageScanner::FileIO* NaClMessageScanner::GetFile(
PP_Resource file_io) {
FileIOMap::iterator it = files_.find(file_io);
DCHECK(it != files_.end());
return it->second;
}
void NaClMessageScanner::AuditNestedMessage(PP_Resource resource,
const IPC::Message& msg,
SerializedHandle* handle) {
switch (msg.type()) {
case PpapiPluginMsg_FileIO_OpenReply::ID: {
PP_Resource quota_file_system;
int64_t max_written_offset = 0;
if (ppapi::UnpackMessage<PpapiPluginMsg_FileIO_OpenReply>(
msg, "a_file_system, &max_written_offset)) {
if (quota_file_system) {
FileSystem* file_system = NULL;
std::pair<FileSystemMap::iterator, bool> insert_result =
file_systems_.insert(std::make_pair(quota_file_system,
file_system));
if (insert_result.second)
insert_result.first->second = new FileSystem();
file_system = insert_result.first->second;
DCHECK(files_.find(resource) == files_.end());
files_.insert(std::make_pair(
resource,
new FileIO(file_system, max_written_offset)));
}
}
break;
}
case PpapiPluginMsg_FileSystem_ReserveQuotaReply::ID: {
int64_t amount = 0;
FileSizeMap file_sizes;
if (ppapi::UnpackMessage<PpapiPluginMsg_FileSystem_ReserveQuotaReply>(
msg, &amount, &file_sizes)) {
FileSystemMap::iterator it = file_systems_.find(resource);
DCHECK(it != file_systems_.end());
it->second->UpdateReservedQuota(amount);
FileSizeMap::const_iterator offset_it = file_sizes.begin();
for (; offset_it != file_sizes.end(); ++offset_it) {
FileIOMap::iterator fio_it = files_.find(offset_it->first);
DCHECK(fio_it != files_.end());
if (fio_it != files_.end())
fio_it->second->SetMaxWrittenOffset(offset_it->second);
}
}
break;
}
}
}
}
}