2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "sandboxed_api/rpcchannel.h"
|
|
|
|
|
|
|
|
#include <glog/logging.h>
|
2020-09-02 23:46:48 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
|
|
|
#include "absl/synchronization/mutex.h"
|
2019-11-22 22:44:03 +08:00
|
|
|
#include "sandboxed_api/call.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/comms.h"
|
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
|
|
|
|
|
|
|
namespace sapi {
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status RPCChannel::Call(const FuncCall& call, uint32_t tag, FuncRet* ret,
|
2019-08-23 23:08:23 +08:00
|
|
|
v::Type exp_type) {
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::MutexLock lock(&mutex_);
|
|
|
|
if (!comms_->SendTLV(tag, sizeof(call),
|
|
|
|
reinterpret_cast<const uint8_t*>(&call))) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sending TLV value failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
SAPI_ASSIGN_OR_RETURN(auto fret, Return(exp_type));
|
|
|
|
*ret = fret;
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<FuncRet> RPCChannel::Return(v::Type exp_type) {
|
2019-03-19 00:21:48 +08:00
|
|
|
uint32_t tag;
|
|
|
|
uint64_t len;
|
|
|
|
FuncRet ret;
|
|
|
|
if (!comms_->RecvTLV(&tag, &len, &ret, sizeof(ret))) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Receiving TLV value failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (tag != comms::kMsgReturn) {
|
|
|
|
LOG(ERROR) << "tag != comms::kMsgReturn (" << absl::StrCat(absl::Hex(tag))
|
|
|
|
<< " != " << absl::StrCat(absl::Hex(comms::kMsgReturn)) << ")";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Received TLV has incorrect tag");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (len != sizeof(FuncRet)) {
|
|
|
|
LOG(ERROR) << "len != sizeof(FuncReturn) (" << len
|
|
|
|
<< " != " << sizeof(FuncRet) << ")";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Received TLV has incorrect length");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (ret.ret_type != exp_type) {
|
|
|
|
LOG(ERROR) << "FuncRet->type != exp_type (" << ret.ret_type
|
|
|
|
<< " != " << exp_type << ")";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Received TLV has incorrect return type");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (!ret.success) {
|
|
|
|
LOG(ERROR) << "FuncRet->success == false";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Function call failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status RPCChannel::Allocate(size_t size, void** addr) {
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::MutexLock lock(&mutex_);
|
|
|
|
uint64_t sz = size;
|
|
|
|
if (!comms_->SendTLV(comms::kMsgAllocate, sizeof(sz),
|
|
|
|
reinterpret_cast<uint8_t*>(&sz))) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sending TLV value failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SAPI_ASSIGN_OR_RETURN(auto fret, Return(v::Type::kPointer));
|
|
|
|
*addr = reinterpret_cast<void*>(fret.int_val);
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status RPCChannel::Reallocate(void* old_addr, size_t size,
|
2019-08-23 23:08:23 +08:00
|
|
|
void** new_addr) {
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::MutexLock lock(&mutex_);
|
|
|
|
comms::ReallocRequest req;
|
|
|
|
req.old_addr = reinterpret_cast<uint64_t>(old_addr);
|
|
|
|
req.size = size;
|
|
|
|
|
|
|
|
if (!comms_->SendTLV(comms::kMsgReallocate, sizeof(comms::ReallocRequest),
|
|
|
|
reinterpret_cast<uint8_t*>(&req))) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sending TLV value failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
auto fret_or = Return(v::Type::kPointer);
|
|
|
|
if (!fret_or.ok()) {
|
|
|
|
*new_addr = nullptr;
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("Reallocate() failed on the remote side: ",
|
|
|
|
fret_or.status().message()));
|
|
|
|
}
|
2020-04-02 22:42:17 +08:00
|
|
|
*new_addr = reinterpret_cast<void*>(fret_or->int_val);
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status RPCChannel::Free(void* addr) {
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::MutexLock lock(&mutex_);
|
|
|
|
uint64_t remote = reinterpret_cast<uint64_t>(addr);
|
|
|
|
if (!comms_->SendTLV(comms::kMsgFree, sizeof(remote),
|
|
|
|
reinterpret_cast<uint8_t*>(&remote))) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sending TLV value failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SAPI_ASSIGN_OR_RETURN(auto fret, Return(v::Type::kVoid));
|
|
|
|
if (!fret.success) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Free() failed on the remote side");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status RPCChannel::Symbol(const char* symname, void** addr) {
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::MutexLock lock(&mutex_);
|
|
|
|
if (!comms_->SendTLV(comms::kMsgSymbol, strlen(symname) + 1,
|
|
|
|
reinterpret_cast<const uint8_t*>(symname))) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sending TLV value failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SAPI_ASSIGN_OR_RETURN(auto fret, Return(v::Type::kPointer));
|
|
|
|
*addr = reinterpret_cast<void*>(fret.int_val);
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status RPCChannel::Exit() {
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::MutexLock lock(&mutex_);
|
|
|
|
if (comms_->IsTerminated()) {
|
|
|
|
VLOG(2) << "Comms channel already terminated";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try the RPC exit sequence. But, the only thing that matters as a success
|
|
|
|
// indicator is whether the Comms channel had been closed
|
|
|
|
bool unused = true;
|
|
|
|
comms_->SendTLV(comms::kMsgExit, sizeof(unused),
|
|
|
|
reinterpret_cast<uint8_t*>(&unused));
|
|
|
|
comms_->RecvBool(&unused);
|
|
|
|
|
|
|
|
if (!comms_->IsTerminated()) {
|
|
|
|
LOG(ERROR) << "Comms channel not terminated in Exit()";
|
|
|
|
// TODO(hamacher): Better error code
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
"Comms channel not terminated in Exit()");
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status RPCChannel::SendFD(int local_fd, int* remote_fd) {
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::MutexLock lock(&mutex_);
|
|
|
|
bool unused = true;
|
|
|
|
if (!comms_->SendTLV(comms::kMsgSendFd, sizeof(unused),
|
|
|
|
reinterpret_cast<uint8_t*>(&unused))) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sending TLV value failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (!comms_->SendFD(local_fd)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sending FD failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SAPI_ASSIGN_OR_RETURN(auto fret, Return(v::Type::kInt));
|
|
|
|
if (!fret.success) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("SendFD failed on the remote side");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
*remote_fd = fret.int_val;
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status RPCChannel::RecvFD(int remote_fd, int* local_fd) {
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::MutexLock lock(&mutex_);
|
|
|
|
if (!comms_->SendTLV(comms::kMsgRecvFd, sizeof(remote_fd),
|
|
|
|
reinterpret_cast<uint8_t*>(&remote_fd))) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sending TLV value failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!comms_->RecvFD(local_fd)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Receving FD failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SAPI_ASSIGN_OR_RETURN(auto fret, Return(v::Type::kVoid));
|
|
|
|
if (!fret.success) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("RecvFD failed on the remote side");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status RPCChannel::Close(int remote_fd) {
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::MutexLock lock(&mutex_);
|
|
|
|
if (!comms_->SendTLV(comms::kMsgClose, sizeof(remote_fd),
|
|
|
|
reinterpret_cast<uint8_t*>(&remote_fd))) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sending TLV value failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SAPI_ASSIGN_OR_RETURN(auto fret, Return(v::Type::kVoid));
|
|
|
|
if (!fret.success) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Close() failed on the remote side");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<uint64_t> RPCChannel::Strlen(void* str) {
|
2019-11-22 22:44:03 +08:00
|
|
|
absl::MutexLock lock(&mutex_);
|
|
|
|
if (!comms_->SendTLV(comms::kMsgStrlen, sizeof(str),
|
|
|
|
reinterpret_cast<uint8_t*>(&str))) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sending TLV value failed");
|
2019-11-22 22:44:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SAPI_ASSIGN_OR_RETURN(auto fret, Return(v::Type::kInt));
|
|
|
|
if (!fret.success) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Close() failed on the remote side");
|
2019-11-22 22:44:03 +08:00
|
|
|
}
|
|
|
|
return fret.int_val;
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace sapi
|