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.
|
|
|
|
|
|
|
|
// Implementation file for the sandbox2::Sandbox class.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/sandbox2.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2023-02-16 21:06:50 +08:00
|
|
|
#include "absl/base/call_once.h"
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/log/check.h"
|
2023-08-08 00:06:31 +08:00
|
|
|
#include "absl/log/log.h"
|
2023-03-09 00:08:35 +08:00
|
|
|
#include "absl/status/status.h"
|
2020-09-02 23:46:48 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2019-11-29 00:06:37 +08:00
|
|
|
#include "absl/time/time.h"
|
2023-02-16 21:06:50 +08:00
|
|
|
#include "sandboxed_api/sandbox2/monitor_base.h"
|
2023-01-30 21:08:42 +08:00
|
|
|
#include "sandboxed_api/sandbox2/monitor_ptrace.h"
|
2023-03-09 00:08:35 +08:00
|
|
|
#include "sandboxed_api/sandbox2/monitor_unotify.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/result.h"
|
2023-02-16 21:06:50 +08:00
|
|
|
#include "sandboxed_api/sandbox2/stack_trace.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
|
2023-02-16 21:06:50 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class Sandbox2Peer : public internal::SandboxPeer {
|
|
|
|
public:
|
|
|
|
static std::unique_ptr<SandboxPeer> Spawn(std::unique_ptr<Executor> executor,
|
|
|
|
std::unique_ptr<Policy> policy) {
|
|
|
|
return std::make_unique<Sandbox2Peer>(std::move(executor),
|
|
|
|
std::move(policy));
|
|
|
|
}
|
|
|
|
|
|
|
|
Sandbox2Peer(std::unique_ptr<Executor> executor,
|
|
|
|
std::unique_ptr<Policy> policy)
|
|
|
|
: sandbox_(std::move(executor), std::move(policy)) {
|
|
|
|
sandbox_.RunAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
Comms* comms() override { return sandbox_.comms(); }
|
|
|
|
void Kill() override { sandbox_.Kill(); }
|
|
|
|
Result AwaitResult() override { return sandbox_.AwaitResult(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Sandbox2 sandbox_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<Result> Sandbox2::AwaitResultWithTimeout(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::Duration timeout) {
|
|
|
|
CHECK(monitor_ != nullptr) << "Sandbox was not launched yet";
|
2023-01-23 17:41:42 +08:00
|
|
|
return monitor_->AwaitResultWithTimeout(timeout);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Result Sandbox2::AwaitResult() {
|
2020-04-02 22:42:17 +08:00
|
|
|
return AwaitResultWithTimeout(absl::InfiniteDuration()).value();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Sandbox2::RunAsync() {
|
2023-08-02 21:43:35 +08:00
|
|
|
CHECK(monitor_ == nullptr) << "Sandbox was launched already";
|
2019-03-19 00:21:48 +08:00
|
|
|
Launch();
|
|
|
|
|
|
|
|
// If the sandboxee setup failed we return 'false' here.
|
|
|
|
if (monitor_->IsDone() &&
|
2023-01-23 17:41:42 +08:00
|
|
|
monitor_->result().final_status() == Result::SETUP_ERROR) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sandbox2::Kill() {
|
|
|
|
CHECK(monitor_ != nullptr) << "Sandbox was not launched yet";
|
2023-01-23 17:41:42 +08:00
|
|
|
monitor_->Kill();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sandbox2::DumpStackTrace() {
|
|
|
|
CHECK(monitor_ != nullptr) << "Sandbox was not launched yet";
|
2023-01-23 17:41:42 +08:00
|
|
|
monitor_->DumpStackTrace();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Sandbox2::IsTerminated() const {
|
|
|
|
CHECK(monitor_ != nullptr) << "Sandbox was not launched yet";
|
|
|
|
return monitor_->IsDone();
|
|
|
|
}
|
|
|
|
|
2019-11-29 00:06:37 +08:00
|
|
|
void Sandbox2::set_walltime_limit(absl::Duration limit) const {
|
2023-01-23 17:41:42 +08:00
|
|
|
CHECK(monitor_ != nullptr) << "Sandbox was not launched yet";
|
|
|
|
monitor_->SetWallTimeLimit(limit);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sandbox2::Launch() {
|
2023-02-16 21:06:50 +08:00
|
|
|
static absl::once_flag init_sandbox_peer_flag;
|
|
|
|
absl::call_once(init_sandbox_peer_flag, []() {
|
|
|
|
internal::SandboxPeer::spawn_fn_ = Sandbox2Peer::Spawn;
|
|
|
|
});
|
|
|
|
|
2023-08-08 00:06:31 +08:00
|
|
|
// This is a technical limitation in our stack trace collection
|
|
|
|
// functionality.
|
|
|
|
LOG_IF(WARNING, !policy_->GetNamespace())
|
|
|
|
<< "Using policy without namespaces, disabling stack traces on crash";
|
|
|
|
|
2023-03-09 00:08:35 +08:00
|
|
|
monitor_ = CreateMonitor();
|
2023-01-23 17:41:42 +08:00
|
|
|
monitor_->Launch();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2023-03-09 00:08:35 +08:00
|
|
|
absl::Status Sandbox2::EnableUnotifyMonitor() {
|
|
|
|
if (notify_) {
|
2023-03-10 16:58:59 +08:00
|
|
|
LOG(WARNING) << "Running UnotifyMonitor with sandbox2::Notify is not fully "
|
|
|
|
"supported. Runtime syscall decisions via "
|
|
|
|
"EventSyscallTrap/EventSyscallTrace, notifications about "
|
|
|
|
"signals via EventSignal will not work";
|
2023-03-09 00:08:35 +08:00
|
|
|
}
|
2023-08-09 21:43:50 +08:00
|
|
|
if (!policy_->GetNamespace()) {
|
2023-03-09 00:08:35 +08:00
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unotify monitor can only be used together with namespaces");
|
|
|
|
}
|
2023-08-09 21:43:50 +08:00
|
|
|
if (policy_->collect_stacktrace_on_signal()) {
|
2023-03-09 00:08:35 +08:00
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unotify monitor cannot collect stack traces on signal");
|
|
|
|
}
|
|
|
|
|
2023-08-09 21:43:50 +08:00
|
|
|
if (policy_->collect_stacktrace_on_exit()) {
|
2023-03-09 00:08:35 +08:00
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
"Unotify monitor cannot collect stack traces on normal exit");
|
|
|
|
}
|
|
|
|
use_unotify_monitor_ = true;
|
|
|
|
return absl::OkStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<MonitorBase> Sandbox2::CreateMonitor() {
|
|
|
|
if (!notify_) {
|
|
|
|
notify_ = std::make_unique<Notify>();
|
|
|
|
}
|
|
|
|
if (use_unotify_monitor_) {
|
|
|
|
return std::make_unique<UnotifyMonitor>(executor_.get(), policy_.get(),
|
|
|
|
notify_.get());
|
|
|
|
}
|
|
|
|
return std::make_unique<PtraceMonitor>(executor_.get(), policy_.get(),
|
|
|
|
notify_.get());
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace sandbox2
|