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.
|
|
|
|
|
|
|
|
// Implementation of sapi::v::Var
|
|
|
|
|
|
|
|
#include "sandboxed_api/var_abstract.h"
|
|
|
|
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
2021-12-02 01:54:09 +08:00
|
|
|
#include <memory>
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <glog/logging.h>
|
|
|
|
#include "absl/strings/str_cat.h"
|
|
|
|
#include "sandboxed_api/rpcchannel.h"
|
2021-01-22 22:01:05 +08:00
|
|
|
#include "sandboxed_api/sandbox2/comms.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
2021-12-02 01:54:09 +08:00
|
|
|
#include "sandboxed_api/var_ptr.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
namespace sapi::v {
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
Var::~Var() {
|
|
|
|
if (free_rpc_channel_ && GetRemote()) {
|
|
|
|
this->Free(free_rpc_channel_).IgnoreError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 01:54:09 +08:00
|
|
|
void Var::PtrDeleter::operator()(Ptr* p) { delete p; }
|
|
|
|
|
|
|
|
Ptr* Var::PtrNone() {
|
|
|
|
if (!ptr_none_) {
|
|
|
|
ptr_none_.reset(new Ptr(this, kSyncNone));
|
|
|
|
}
|
|
|
|
return ptr_none_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ptr* Var::PtrBoth() {
|
|
|
|
if (!ptr_both_) {
|
|
|
|
ptr_both_.reset(new Ptr(this, kSyncBoth));
|
|
|
|
}
|
|
|
|
return ptr_both_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ptr* Var::PtrBefore() {
|
|
|
|
if (!ptr_before_) {
|
|
|
|
ptr_before_.reset(new Ptr(this, kSyncBefore));
|
|
|
|
}
|
|
|
|
return ptr_before_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ptr* Var::PtrAfter() {
|
|
|
|
if (!ptr_after_) {
|
|
|
|
ptr_after_.reset(new Ptr(this, kSyncAfter));
|
|
|
|
}
|
|
|
|
return ptr_after_.get();
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Var::Allocate(RPCChannel* rpc_channel, bool automatic_free) {
|
2019-03-19 00:21:48 +08:00
|
|
|
void* addr;
|
|
|
|
SAPI_RETURN_IF_ERROR(rpc_channel->Allocate(GetSize(), &addr));
|
|
|
|
|
|
|
|
if (!addr) {
|
|
|
|
LOG(ERROR) << "Allocate: returned nullptr";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Allocating memory failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SetRemote(addr);
|
|
|
|
if (automatic_free) {
|
|
|
|
SetFreeRPCChannel(rpc_channel);
|
|
|
|
}
|
|
|
|
|
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 Var::Free(RPCChannel* rpc_channel) {
|
2019-03-19 00:21:48 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(rpc_channel->Free(GetRemote()));
|
|
|
|
|
|
|
|
SetRemote(nullptr);
|
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 Var::TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid) {
|
2019-03-19 00:21:48 +08:00
|
|
|
VLOG(3) << "TransferToSandboxee for: " << ToString()
|
|
|
|
<< ", local: " << GetLocal() << ", remote: " << GetRemote()
|
|
|
|
<< ", size: " << GetSize();
|
|
|
|
|
|
|
|
if (remote_ == nullptr) {
|
|
|
|
LOG(WARNING) << "Object: " << GetType() << " has no remote object set";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("Object: ", GetType(), " has no remote object set"));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct iovec local = {
|
|
|
|
.iov_base = GetLocal(),
|
|
|
|
.iov_len = GetSize(),
|
|
|
|
};
|
|
|
|
struct iovec remote = {
|
|
|
|
.iov_base = GetRemote(),
|
|
|
|
.iov_len = GetSize(),
|
|
|
|
};
|
|
|
|
|
|
|
|
ssize_t ret = process_vm_writev(pid, &local, 1, &remote, 1, 0);
|
|
|
|
if (ret == -1) {
|
|
|
|
PLOG(WARNING) << "process_vm_writev(pid: " << pid
|
|
|
|
<< " laddr: " << GetLocal() << " raddr: " << GetRemote()
|
|
|
|
<< " size: " << GetSize() << ")";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("process_vm_writev failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (ret != GetSize()) {
|
|
|
|
LOG(WARNING) << "process_vm_writev(pid: " << pid << " laddr: " << GetLocal()
|
|
|
|
<< " raddr: " << GetRemote() << " size: " << GetSize() << ")"
|
|
|
|
<< " transferred " << ret << " bytes";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("process_vm_writev: partial success");
|
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 Var::TransferFromSandboxee(RPCChannel* rpc_channel, pid_t pid) {
|
2019-03-19 00:21:48 +08:00
|
|
|
VLOG(3) << "TransferFromSandboxee for: " << ToString()
|
|
|
|
<< ", local: " << GetLocal() << ", remote: " << GetRemote()
|
|
|
|
<< ", size: " << GetSize();
|
|
|
|
|
|
|
|
if (local_ == nullptr) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("Object: ", GetType(), " has no local storage set"));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct iovec local = {
|
|
|
|
.iov_base = GetLocal(),
|
|
|
|
.iov_len = GetSize(),
|
|
|
|
};
|
|
|
|
struct iovec remote = {
|
|
|
|
.iov_base = GetRemote(),
|
|
|
|
.iov_len = GetSize(),
|
|
|
|
};
|
|
|
|
|
|
|
|
ssize_t ret = process_vm_readv(pid, &local, 1, &remote, 1, 0);
|
|
|
|
if (ret == -1) {
|
|
|
|
PLOG(WARNING) << "process_vm_readv(pid: " << pid << " laddr: " << GetLocal()
|
|
|
|
<< " raddr: " << GetRemote() << " size: " << GetSize() << ")";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("process_vm_readv failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (ret != GetSize()) {
|
|
|
|
LOG(WARNING) << "process_vm_readv(pid: " << pid << " laddr: " << GetLocal()
|
|
|
|
<< " raddr: " << GetRemote() << " size: " << GetSize() << ")"
|
|
|
|
<< " transferred " << ret << " bytes";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("process_vm_readv succeeded partially");
|
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
|
|
|
}
|
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
} // namespace sapi::v
|