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.
|
|
|
|
|
|
|
|
// The sandbox2::Sandbox object is the central object of the Sandbox2.
|
|
|
|
// It handles sandboxed jobs.
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_SANDBOX2_SANDBOX2_H_
|
|
|
|
#define SANDBOXED_API_SANDBOX2_SANDBOX2_H_
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
#include <memory>
|
|
|
|
#include <thread> // NOLINT(build/c++11)
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include <glog/logging.h>
|
|
|
|
#include "absl/base/macros.h"
|
|
|
|
#include "absl/memory/memory.h"
|
2020-09-02 23:46:48 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2021-11-17 17:39:46 +08:00
|
|
|
#include "absl/synchronization/mutex.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/comms.h"
|
|
|
|
#include "sandboxed_api/sandbox2/executor.h"
|
|
|
|
#include "sandboxed_api/sandbox2/ipc.h"
|
|
|
|
#include "sandboxed_api/sandbox2/monitor.h"
|
|
|
|
#include "sandboxed_api/sandbox2/notify.h"
|
|
|
|
#include "sandboxed_api/sandbox2/policy.h"
|
|
|
|
#include "sandboxed_api/sandbox2/result.h"
|
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
|
|
|
|
class Sandbox2 final {
|
|
|
|
public:
|
|
|
|
Sandbox2(std::unique_ptr<Executor> executor, std::unique_ptr<Policy> policy)
|
|
|
|
: Sandbox2(std::move(executor), std::move(policy), /*notify=*/nullptr) {}
|
|
|
|
|
|
|
|
Sandbox2(std::unique_ptr<Executor> executor, std::unique_ptr<Policy> policy,
|
|
|
|
std::unique_ptr<Notify> notify)
|
|
|
|
: executor_(std::move(executor)),
|
|
|
|
policy_(std::move(policy)),
|
|
|
|
notify_(std::move(notify)) {
|
|
|
|
CHECK(executor_ != nullptr);
|
|
|
|
CHECK(policy_ != nullptr);
|
|
|
|
if (notify_ == nullptr) {
|
|
|
|
notify_ = absl::make_unique<Notify>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~Sandbox2();
|
|
|
|
|
|
|
|
Sandbox2(const Sandbox2&) = delete;
|
|
|
|
Sandbox2& operator=(const Sandbox2&) = delete;
|
|
|
|
|
2021-12-06 20:42:46 +08:00
|
|
|
// Runs the sandbox, blocking until there is a result.
|
2019-06-14 18:00:37 +08:00
|
|
|
ABSL_MUST_USE_RESULT Result Run() {
|
2019-03-19 00:21:48 +08:00
|
|
|
RunAsync();
|
|
|
|
return AwaitResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runs asynchronously. The return value indicates whether the sandboxee
|
|
|
|
// set-up process succeeded
|
|
|
|
// Even if set-up fails AwaitResult can still used to get a more specific
|
|
|
|
// failure reason.
|
|
|
|
bool RunAsync();
|
|
|
|
// Waits for sandbox execution to finish and returns the execution result.
|
|
|
|
ABSL_MUST_USE_RESULT Result AwaitResult();
|
|
|
|
|
|
|
|
// Waits for sandbox execution to finish within the timeout.
|
|
|
|
// Returns execution result or a DeadlineExceededError if the sandboxee does
|
|
|
|
// not finish in time.
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<Result> AwaitResultWithTimeout(absl::Duration timeout);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Requests termination of the sandboxee.
|
|
|
|
// Sandbox should still waited with AwaitResult(), as it may finish for other
|
|
|
|
// reason before the request is handled.
|
|
|
|
void Kill();
|
|
|
|
|
|
|
|
// Dumps the main sandboxed process's stack trace to log.
|
|
|
|
void DumpStackTrace();
|
|
|
|
|
|
|
|
// Returns whether sandboxing task has ended.
|
|
|
|
bool IsTerminated() const;
|
|
|
|
|
|
|
|
// Sets a wall time limit on a running sandboxee, 0 to disarm.
|
|
|
|
// Limit is a timeout duration (e.g. 10 secs) not a deadline (e.g. 12:00).
|
|
|
|
// This can be useful in a persistent sandbox scenario, to impose a deadline
|
|
|
|
// for responses after each request and reset the deadline in between.
|
|
|
|
// Sandboxed API can be used to implement persistent sandboxes.
|
2019-11-29 00:06:37 +08:00
|
|
|
ABSL_DEPRECATED("Use set_walltime_limit() instead")
|
2021-09-16 21:56:52 +08:00
|
|
|
void SetWallTimeLimit(time_t limit) const {
|
|
|
|
this->set_walltime_limit(absl::Seconds(limit));
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2019-11-29 00:06:37 +08:00
|
|
|
// Sets a wall time limit on a running sandboxee, absl::ZeroDuration() to
|
|
|
|
// disarm. This can be useful in a persistent sandbox scenario, to impose a
|
|
|
|
// deadline for responses after each request and reset the deadline in
|
|
|
|
// between. Sandboxed API can be used to implement persistent sandboxes.
|
|
|
|
void set_walltime_limit(absl::Duration limit) const;
|
|
|
|
|
2021-09-16 21:56:52 +08:00
|
|
|
// Returns the process id inside the executor.
|
|
|
|
ABSL_DEPRECATED("Use pid() instead")
|
|
|
|
pid_t GetPid() { return this->pid(); }
|
|
|
|
|
|
|
|
pid_t pid() const { return monitor_ != nullptr ? monitor_->pid_ : -1; }
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Gets the comms inside the executor.
|
|
|
|
Comms* comms() {
|
|
|
|
return executor_ != nullptr ? executor_->ipc()->comms() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Launches the Monitor.
|
|
|
|
void Launch();
|
2019-05-15 22:46:26 +08:00
|
|
|
// Notifies monitor about a state change
|
|
|
|
void NotifyMonitor();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Executor set by user - owned by Sandbox2.
|
|
|
|
std::unique_ptr<Executor> executor_;
|
|
|
|
|
|
|
|
// Seccomp policy set by the user - owned by Sandbox2.
|
|
|
|
std::unique_ptr<Policy> policy_;
|
|
|
|
|
|
|
|
// Notify object - owned by Sandbox2.
|
|
|
|
std::unique_ptr<Notify> notify_;
|
|
|
|
|
|
|
|
// Monitor object - owned by Sandbox2.
|
|
|
|
std::unique_ptr<Monitor> monitor_;
|
|
|
|
|
|
|
|
// Monitor thread object - owned by Sandbox2.
|
|
|
|
std::unique_ptr<std::thread> monitor_thread_;
|
2021-11-17 17:39:46 +08:00
|
|
|
|
|
|
|
// Synchronizes monitor thread deletion and notifying the monitor.
|
|
|
|
absl::Mutex monitor_notify_mutex_;
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sandbox2
|
|
|
|
|
|
|
|
#endif // SANDBOXED_API_SANDBOX2_SANDBOX2_H_
|