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
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// 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/sandbox.h"
|
|
|
|
|
|
|
|
#include <sys/resource.h>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <sys/types.h>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <sys/uio.h>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <syscall.h>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#include <cstdio>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <initializer_list>
|
2022-10-12 20:22:51 +08:00
|
|
|
#include <memory>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-04-13 16:43:36 +08:00
|
|
|
#include "absl/base/dynamic_annotations.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/base/macros.h"
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/log/log.h"
|
2020-02-28 01:23:44 +08:00
|
|
|
#include "absl/status/status.h"
|
2020-10-27 00:08:06 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
|
|
|
#include "absl/strings/str_format.h"
|
|
|
|
#include "absl/time/time.h"
|
2021-02-01 23:10:43 +08:00
|
|
|
#include "sandboxed_api/config.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/embed_file.h"
|
|
|
|
#include "sandboxed_api/rpcchannel.h"
|
|
|
|
#include "sandboxed_api/sandbox2/executor.h"
|
|
|
|
#include "sandboxed_api/sandbox2/policy.h"
|
|
|
|
#include "sandboxed_api/sandbox2/policybuilder.h"
|
2023-12-08 23:47:19 +08:00
|
|
|
#include "sandboxed_api/sandbox2/result.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/sandbox2.h"
|
|
|
|
#include "sandboxed_api/sandbox2/util/bpf_helper.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/fileops.h"
|
|
|
|
#include "sandboxed_api/util/path.h"
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "sandboxed_api/util/raw_logging.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/runfiles.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
|
|
|
|
|
|
|
namespace sapi {
|
|
|
|
|
|
|
|
Sandbox::~Sandbox() {
|
|
|
|
Terminate();
|
|
|
|
// The forkserver will die automatically when the executor goes out of scope
|
|
|
|
// and closes the comms object.
|
|
|
|
}
|
|
|
|
|
|
|
|
// A generic policy which should work with majority of typical libraries, which
|
|
|
|
// are single-threaded and require ~30 basic syscalls.
|
|
|
|
void InitDefaultPolicyBuilder(sandbox2::PolicyBuilder* builder) {
|
|
|
|
(*builder)
|
|
|
|
.AllowRead()
|
|
|
|
.AllowWrite()
|
|
|
|
.AllowExit()
|
|
|
|
.AllowGetRlimit()
|
|
|
|
.AllowGetIDs()
|
|
|
|
.AllowTCGETS()
|
|
|
|
.AllowTime()
|
|
|
|
.AllowOpen()
|
|
|
|
.AllowStat()
|
|
|
|
.AllowHandleSignals()
|
|
|
|
.AllowSystemMalloc()
|
|
|
|
.AllowSafeFcntl()
|
2023-08-17 17:51:24 +08:00
|
|
|
.AllowGetPIDs()
|
2023-01-07 00:24:34 +08:00
|
|
|
.AllowSleep()
|
2023-08-17 17:51:24 +08:00
|
|
|
.AllowReadlink()
|
2024-02-01 18:51:09 +08:00
|
|
|
.AllowAccess()
|
2023-08-17 17:51:24 +08:00
|
|
|
.AllowSyscalls({
|
|
|
|
__NR_recvmsg,
|
|
|
|
__NR_sendmsg,
|
|
|
|
__NR_futex,
|
|
|
|
__NR_close,
|
|
|
|
__NR_lseek,
|
|
|
|
__NR_uname,
|
|
|
|
__NR_kill,
|
|
|
|
__NR_tgkill,
|
|
|
|
__NR_tkill,
|
|
|
|
});
|
2023-01-07 00:24:34 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
#ifdef __NR_arch_prctl // x86-64 only
|
2023-01-07 00:24:34 +08:00
|
|
|
builder->AllowSyscall(__NR_arch_prctl);
|
2019-03-19 00:21:48 +08:00
|
|
|
#endif
|
2023-01-07 00:24:34 +08:00
|
|
|
|
2021-02-01 23:10:43 +08:00
|
|
|
if constexpr (sanitizers::IsAny()) {
|
|
|
|
LOG(WARNING) << "Allowing additional calls to support the LLVM "
|
|
|
|
<< "(ASAN/MSAN/TSAN) sanitizer";
|
|
|
|
builder->AllowLlvmSanitizers();
|
|
|
|
}
|
2022-05-19 19:54:57 +08:00
|
|
|
builder->AddFile("/etc/localtime")
|
|
|
|
.AddTmpfs("/tmp", 1ULL << 30 /* 1GiB tmpfs (max size */);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sandbox::Terminate(bool attempt_graceful_exit) {
|
2020-08-07 15:30:04 +08:00
|
|
|
if (!is_active()) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-08 23:47:19 +08:00
|
|
|
absl::StatusOr<sandbox2::Result> result;
|
2019-03-19 00:21:48 +08:00
|
|
|
if (attempt_graceful_exit) {
|
2023-12-08 23:47:19 +08:00
|
|
|
if (absl::Status requested_exit = rpc_channel_->Exit();
|
|
|
|
!requested_exit.ok()) {
|
|
|
|
LOG(WARNING)
|
|
|
|
<< "rpc_channel->Exit() failed, calling AwaitResultWithTimeout(1) "
|
|
|
|
<< requested_exit;
|
|
|
|
}
|
|
|
|
result = s2_->AwaitResultWithTimeout(absl::Seconds(1));
|
|
|
|
if (!result.ok()) {
|
|
|
|
LOG(WARNING) << "s2_->AwaitResultWithTimeout failed, status: "
|
|
|
|
<< result.status() << " Killing PID: " << pid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!attempt_graceful_exit || !result.ok()) {
|
2019-03-19 00:21:48 +08:00
|
|
|
s2_->Kill();
|
2023-12-08 23:47:19 +08:00
|
|
|
result = s2_->AwaitResult();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2023-12-08 23:47:19 +08:00
|
|
|
if (result->final_status() == sandbox2::Result::OK &&
|
|
|
|
result->reason_code() == 0) {
|
|
|
|
VLOG(2) << "Sandbox2 finished with: " << result->ToString();
|
2019-03-19 00:21:48 +08:00
|
|
|
} else {
|
2023-12-08 23:47:19 +08:00
|
|
|
LOG(WARNING) << "Sandbox2 finished with: " << result->ToString();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string PathToSAPILib(const std::string& lib_path) {
|
2021-01-14 01:25:25 +08:00
|
|
|
return file::IsAbsolutePath(lib_path) ? lib_path
|
|
|
|
: GetDataDependencyFilePath(lib_path);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2023-12-14 16:47:34 +08:00
|
|
|
absl::Status Sandbox::Init(bool use_unotify_monitor) {
|
2019-03-19 00:21:48 +08:00
|
|
|
// It's already initialized
|
2020-08-07 15:30:04 +08:00
|
|
|
if (is_active()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the forkserver if it is not already running.
|
|
|
|
if (!fork_client_) {
|
|
|
|
// If FileToc was specified, it will be used over any paths to the SAPI
|
|
|
|
// library.
|
|
|
|
std::string lib_path;
|
|
|
|
int embed_lib_fd = -1;
|
2022-05-19 19:54:57 +08:00
|
|
|
if (embed_lib_toc_ && !sapi::host_os::IsAndroid()) {
|
2021-07-14 23:00:30 +08:00
|
|
|
embed_lib_fd = EmbedFile::instance()->GetDupFdForFileToc(embed_lib_toc_);
|
2019-03-19 00:21:48 +08:00
|
|
|
if (embed_lib_fd == -1) {
|
|
|
|
PLOG(ERROR) << "Cannot create executable FD for TOC:'"
|
|
|
|
<< embed_lib_toc_->name << "'";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Could not create executable FD");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
lib_path = embed_lib_toc_->name;
|
|
|
|
} else {
|
|
|
|
lib_path = PathToSAPILib(GetLibPath());
|
|
|
|
if (lib_path.empty()) {
|
|
|
|
LOG(ERROR) << "SAPI library path is empty";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError("No SAPI library path given");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
2020-08-07 15:30:04 +08:00
|
|
|
std::vector<std::string> args = {lib_path};
|
2019-03-19 00:21:48 +08:00
|
|
|
// Additional arguments, if needed.
|
|
|
|
GetArgs(&args);
|
|
|
|
std::vector<std::string> envs{};
|
|
|
|
// Additional envvars, if needed.
|
|
|
|
GetEnvs(&envs);
|
|
|
|
|
|
|
|
forkserver_executor_ =
|
|
|
|
(embed_lib_fd >= 0)
|
2022-10-12 20:22:51 +08:00
|
|
|
? std::make_unique<sandbox2::Executor>(embed_lib_fd, args, envs)
|
|
|
|
: std::make_unique<sandbox2::Executor>(lib_path, args, envs);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
fork_client_ = forkserver_executor_->StartForkServer();
|
|
|
|
|
|
|
|
if (!fork_client_) {
|
|
|
|
LOG(ERROR) << "Could not start forkserver";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Could not start the forkserver");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
2019-03-19 18:40:51 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
sandbox2::PolicyBuilder policy_builder;
|
|
|
|
InitDefaultPolicyBuilder(&policy_builder);
|
2023-12-14 16:47:34 +08:00
|
|
|
if (use_unotify_monitor) {
|
|
|
|
policy_builder.CollectStacktracesOnSignal(false);
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
auto s2p = ModifyPolicy(&policy_builder);
|
|
|
|
|
|
|
|
// Spawn new process from the forkserver.
|
2022-10-12 20:22:51 +08:00
|
|
|
auto executor = std::make_unique<sandbox2::Executor>(fork_client_.get());
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
executor
|
|
|
|
// The client.cc code is capable of enabling sandboxing on its own.
|
|
|
|
->set_enable_sandbox_before_exec(false)
|
|
|
|
// By default, set cwd to "/", can be changed in ModifyExecutor().
|
|
|
|
.set_cwd("/")
|
|
|
|
.limits()
|
|
|
|
// Disable time limits.
|
|
|
|
->set_walltime_limit(absl::ZeroDuration())
|
2023-03-03 17:13:44 +08:00
|
|
|
.set_rlimit_cpu(RLIM64_INFINITY);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Modify the executor, e.g. by setting custom limits and IPC.
|
|
|
|
ModifyExecutor(executor.get());
|
|
|
|
|
2022-10-12 20:22:51 +08:00
|
|
|
s2_ = std::make_unique<sandbox2::Sandbox2>(std::move(executor),
|
|
|
|
std::move(s2p), CreateNotifier());
|
2023-12-14 16:47:34 +08:00
|
|
|
if (use_unotify_monitor) {
|
|
|
|
SAPI_RETURN_IF_ERROR(s2_->EnableUnotifyMonitor());
|
|
|
|
}
|
2022-03-23 18:34:56 +08:00
|
|
|
s2_awaited_ = false;
|
2019-03-19 00:21:48 +08:00
|
|
|
auto res = s2_->RunAsync();
|
|
|
|
|
|
|
|
comms_ = s2_->comms();
|
2022-02-03 01:56:33 +08:00
|
|
|
pid_ = s2_->pid();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2022-10-12 20:22:51 +08:00
|
|
|
rpc_channel_ = std::make_unique<RPCChannel>(comms_);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
Terminate();
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Could not start the sandbox");
|
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-08-07 15:30:04 +08:00
|
|
|
bool Sandbox::is_active() const { return s2_ && !s2_->IsTerminated(); }
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Sandbox::Allocate(v::Var* var, bool automatic_free) {
|
2020-08-07 15:30:04 +08:00
|
|
|
if (!is_active()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sandbox not active");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2021-03-18 20:39:27 +08:00
|
|
|
return var->Allocate(rpc_channel(), automatic_free);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Sandbox::Free(v::Var* var) {
|
2020-08-07 15:30:04 +08:00
|
|
|
if (!is_active()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sandbox not active");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2021-03-18 20:39:27 +08:00
|
|
|
return var->Free(rpc_channel());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Sandbox::SynchronizePtrBefore(v::Callable* ptr) {
|
2020-08-07 15:30:04 +08:00
|
|
|
if (!is_active()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sandbox not active");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (ptr->GetType() != v::Type::kPointer) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
// Cast is safe, since type is v::Type::kPointer
|
|
|
|
auto* p = static_cast<v::Ptr*>(ptr);
|
2022-10-20 21:48:06 +08:00
|
|
|
// NOLINTNEXTLINE(clang-diagnostic-deprecated-declarations)
|
2020-07-20 22:05:24 +08:00
|
|
|
if (p->GetSyncType() == v::Pointable::kSyncNone) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p->GetPointedVar()->GetRemote() == nullptr) {
|
|
|
|
// Allocate the memory, and make it automatically free-able, upon this
|
|
|
|
// object's (p->GetPointedVar()) end of life-time.
|
|
|
|
SAPI_RETURN_IF_ERROR(Allocate(p->GetPointedVar(), /*automatic_free=*/true));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocation occurs during both before/after synchronization modes. But the
|
2020-07-20 22:05:24 +08:00
|
|
|
// memory is transferred to the sandboxee only if v::Pointable::kSyncBefore
|
2019-03-19 00:21:48 +08:00
|
|
|
// was requested.
|
2022-10-20 21:48:06 +08:00
|
|
|
// NOLINTNEXTLINE(clang-diagnostic-deprecated-declarations)
|
2020-07-20 22:05:24 +08:00
|
|
|
if ((p->GetSyncType() & v::Pointable::kSyncBefore) == 0) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VLOG(3) << "Synchronization (TO), ptr " << p << ", Type: " << p->GetSyncType()
|
|
|
|
<< " for var: " << p->GetPointedVar()->ToString();
|
|
|
|
|
2021-03-18 20:39:27 +08:00
|
|
|
return p->GetPointedVar()->TransferToSandboxee(rpc_channel(), pid());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Sandbox::SynchronizePtrAfter(v::Callable* ptr) const {
|
2020-08-07 15:30:04 +08:00
|
|
|
if (!is_active()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sandbox not active");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (ptr->GetType() != v::Type::kPointer) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
v::Ptr* p = reinterpret_cast<v::Ptr*>(ptr);
|
2022-10-20 21:48:06 +08:00
|
|
|
// NOLINTNEXTLINE(clang-diagnostic-deprecated-declarations)
|
2020-07-20 22:05:24 +08:00
|
|
|
if ((p->GetSyncType() & v::Pointable::kSyncAfter) == 0) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VLOG(3) << "Synchronization (FROM), ptr " << p
|
|
|
|
<< ", Type: " << p->GetSyncType()
|
|
|
|
<< " for var: " << p->GetPointedVar()->ToString();
|
|
|
|
|
|
|
|
if (p->GetPointedVar()->GetRemote() == nullptr) {
|
|
|
|
LOG(ERROR) << "Trying to synchronize a variable which is not allocated in "
|
|
|
|
<< "the sandboxee p=" << p->ToString();
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(absl::StrCat(
|
2019-03-19 00:21:48 +08:00
|
|
|
"Trying to synchronize a variable which is not allocated in the "
|
|
|
|
"sandboxee p=",
|
|
|
|
p->ToString()));
|
|
|
|
}
|
|
|
|
|
2021-03-18 20:39:27 +08:00
|
|
|
return p->GetPointedVar()->TransferFromSandboxee(rpc_channel(), pid());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Sandbox::Call(const std::string& func, v::Callable* ret,
|
2019-08-23 23:08:23 +08:00
|
|
|
std::initializer_list<v::Callable*> args) {
|
2020-08-07 15:30:04 +08:00
|
|
|
if (!is_active()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sandbox not active");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
// Send data.
|
|
|
|
FuncCall rfcall{};
|
|
|
|
rfcall.argc = args.size();
|
|
|
|
absl::SNPrintF(rfcall.func, ABSL_ARRAYSIZE(rfcall.func), "%s", func);
|
|
|
|
|
|
|
|
VLOG(1) << "CALL ENTRY: '" << func << "' with " << args.size()
|
|
|
|
<< " argument(s)";
|
|
|
|
|
|
|
|
// Copy all arguments into rfcall.
|
|
|
|
int i = 0;
|
|
|
|
for (auto* arg : args) {
|
2024-01-29 19:22:32 +08:00
|
|
|
if (arg == nullptr) {
|
|
|
|
rfcall.arg_type[i] = v::Type::kPointer;
|
|
|
|
rfcall.arg_size[i] = sizeof(void*);
|
|
|
|
rfcall.args[i].arg_int = 0;
|
|
|
|
VLOG(1) << "CALL ARG: (" << i << "): nullptr";
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
rfcall.arg_size[i] = arg->GetSize();
|
|
|
|
rfcall.arg_type[i] = arg->GetType();
|
|
|
|
|
|
|
|
// For pointers, set the auxiliary type and size.
|
|
|
|
if (rfcall.arg_type[i] == v::Type::kPointer) {
|
|
|
|
// Cast is safe, since type is v::Type::kPointer
|
|
|
|
auto* p = static_cast<v::Ptr*>(arg);
|
|
|
|
rfcall.aux_type[i] = p->GetPointedVar()->GetType();
|
|
|
|
rfcall.aux_size[i] = p->GetPointedVar()->GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Synchronize all pointers before the call if it's needed.
|
|
|
|
SAPI_RETURN_IF_ERROR(SynchronizePtrBefore(arg));
|
|
|
|
|
|
|
|
if (arg->GetType() == v::Type::kFloat) {
|
|
|
|
arg->GetDataFromPtr(&rfcall.args[i].arg_float,
|
|
|
|
sizeof(rfcall.args[0].arg_float));
|
2021-04-13 16:43:36 +08:00
|
|
|
// Make MSAN happy with long double.
|
|
|
|
ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(&rfcall.args[i].arg_float,
|
|
|
|
sizeof(rfcall.args[0].arg_float));
|
2019-03-19 00:21:48 +08:00
|
|
|
} else {
|
|
|
|
arg->GetDataFromPtr(&rfcall.args[i].arg_int,
|
|
|
|
sizeof(rfcall.args[0].arg_int));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rfcall.arg_type[i] == v::Type::kFd) {
|
|
|
|
// Cast is safe, since type is v::Type::kFd
|
|
|
|
auto* fd = static_cast<v::Fd*>(arg);
|
|
|
|
if (fd->GetRemoteFd() < 0) {
|
|
|
|
SAPI_RETURN_IF_ERROR(TransferToSandboxee(fd));
|
|
|
|
}
|
|
|
|
rfcall.args[i].arg_int = fd->GetRemoteFd();
|
|
|
|
}
|
|
|
|
VLOG(1) << "CALL ARG: (" << i << "), Type: " << arg->GetTypeString()
|
|
|
|
<< ", Size: " << arg->GetSize() << ", Val: " << arg->ToString();
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
rfcall.ret_type = ret->GetType();
|
|
|
|
rfcall.ret_size = ret->GetSize();
|
|
|
|
|
|
|
|
// Call & receive data.
|
|
|
|
FuncRet fret;
|
|
|
|
SAPI_RETURN_IF_ERROR(
|
2021-03-18 20:39:27 +08:00
|
|
|
rpc_channel()->Call(rfcall, comms::kMsgCall, &fret, rfcall.ret_type));
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
if (fret.ret_type == v::Type::kFloat) {
|
|
|
|
ret->SetDataFromPtr(&fret.float_val, sizeof(fret.float_val));
|
|
|
|
} else {
|
|
|
|
ret->SetDataFromPtr(&fret.int_val, sizeof(fret.int_val));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fret.ret_type == v::Type::kFd) {
|
|
|
|
SAPI_RETURN_IF_ERROR(TransferFromSandboxee(reinterpret_cast<v::Fd*>(ret)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Synchronize all pointers after the call if it's needed.
|
|
|
|
for (auto* arg : args) {
|
2024-01-29 19:22:32 +08:00
|
|
|
if (arg != nullptr) {
|
|
|
|
SAPI_RETURN_IF_ERROR(SynchronizePtrAfter(arg));
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VLOG(1) << "CALL EXIT: Type: " << ret->GetTypeString()
|
|
|
|
<< ", Size: " << ret->GetSize() << ", Val: " << ret->ToString();
|
|
|
|
|
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 Sandbox::Symbol(const char* symname, void** addr) {
|
2020-08-07 15:30:04 +08:00
|
|
|
if (!is_active()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sandbox not active");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
return rpc_channel_->Symbol(symname, addr);
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Sandbox::TransferToSandboxee(v::Var* var) {
|
2020-08-07 15:30:04 +08:00
|
|
|
if (!is_active()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sandbox not active");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2021-03-18 20:39:27 +08:00
|
|
|
return var->TransferToSandboxee(rpc_channel(), pid());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Sandbox::TransferFromSandboxee(v::Var* var) {
|
2020-08-07 15:30:04 +08:00
|
|
|
if (!is_active()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sandbox not active");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2021-03-18 20:39:27 +08:00
|
|
|
return var->TransferFromSandboxee(rpc_channel(), pid());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<std::string> Sandbox::GetCString(const v::RemotePtr& str,
|
2020-09-18 22:44:34 +08:00
|
|
|
size_t max_length) {
|
2020-08-07 15:30:04 +08:00
|
|
|
if (!is_active()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sandbox not active");
|
2019-11-22 22:59:37 +08:00
|
|
|
}
|
|
|
|
|
2021-03-18 20:39:27 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(auto len, rpc_channel()->Strlen(str.GetValue()));
|
2019-11-22 22:59:37 +08:00
|
|
|
if (len > max_length) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InvalidArgumentError(
|
2019-11-22 22:59:37 +08:00
|
|
|
absl::StrCat("Target string too large: ", len, " > ", max_length));
|
|
|
|
}
|
|
|
|
std::string buffer(len, '\0');
|
|
|
|
struct iovec local = {
|
|
|
|
.iov_base = &buffer[0],
|
|
|
|
.iov_len = len,
|
|
|
|
};
|
|
|
|
struct iovec remote = {
|
|
|
|
.iov_base = str.GetValue(),
|
|
|
|
.iov_len = len,
|
|
|
|
};
|
|
|
|
|
|
|
|
ssize_t ret = process_vm_readv(pid_, &local, 1, &remote, 1, 0);
|
|
|
|
if (ret == -1) {
|
|
|
|
PLOG(WARNING) << "reading c-string failed: process_vm_readv(pid: " << pid_
|
|
|
|
<< " raddr: " << str.GetValue() << " size: " << len << ")";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("process_vm_readv failed");
|
2019-11-22 22:59:37 +08:00
|
|
|
}
|
|
|
|
if (ret != len) {
|
|
|
|
LOG(WARNING) << "partial read when reading c-string: process_vm_readv(pid: "
|
|
|
|
<< pid_ << " raddr: " << str.GetValue() << " size: " << len
|
|
|
|
<< ") transferred " << ret << " bytes";
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("process_vm_readv succeeded partially");
|
2019-11-22 22:59:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
const sandbox2::Result& Sandbox::AwaitResult() {
|
2022-03-23 18:34:56 +08:00
|
|
|
if (s2_ && !s2_awaited_) {
|
2019-03-19 00:21:48 +08:00
|
|
|
result_ = s2_->AwaitResult();
|
2022-03-23 18:34:56 +08:00
|
|
|
s2_awaited_ = true;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
return result_;
|
|
|
|
}
|
|
|
|
|
2020-08-07 15:30:04 +08:00
|
|
|
absl::Status Sandbox::SetWallTimeLimit(absl::Duration limit) const {
|
|
|
|
if (!is_active()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::UnavailableError("Sandbox not active");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2020-08-07 15:30:04 +08:00
|
|
|
s2_->set_walltime_limit(limit);
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sandbox::Exit() const {
|
2020-08-07 15:30:04 +08:00
|
|
|
if (!is_active()) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-08-07 15:30:04 +08:00
|
|
|
s2_->set_walltime_limit(absl::Seconds(1));
|
2019-03-19 00:21:48 +08:00
|
|
|
if (!rpc_channel_->Exit().ok()) {
|
2020-08-07 15:30:04 +08:00
|
|
|
LOG(WARNING) << "rpc_channel->Exit() failed, killing PID: " << pid();
|
2019-03-19 00:21:48 +08:00
|
|
|
s2_->Kill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<sandbox2::Policy> Sandbox::ModifyPolicy(
|
|
|
|
sandbox2::PolicyBuilder* builder) {
|
|
|
|
return builder->BuildOrDie();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sapi
|