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 <utility>
|
|
|
|
|
|
|
|
#include "absl/base/macros.h"
|
2023-08-08 00:06:31 +08:00
|
|
|
#include "absl/log/check.h"
|
2020-09-02 23:46:48 +08:00
|
|
|
#include "absl/status/statusor.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"
|
2023-01-30 21:08:42 +08:00
|
|
|
#include "sandboxed_api/sandbox2/monitor_base.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2023-01-23 17:41:42 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-03-09 00:08:35 +08:00
|
|
|
absl::Status EnableUnotifyMonitor();
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
private:
|
|
|
|
// Launches the Monitor.
|
|
|
|
void Launch();
|
|
|
|
|
2023-03-09 00:08:35 +08:00
|
|
|
std::unique_ptr<MonitorBase> CreateMonitor();
|
|
|
|
|
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.
|
2023-01-23 17:41:42 +08:00
|
|
|
std::unique_ptr<MonitorBase> monitor_;
|
2023-03-09 00:08:35 +08:00
|
|
|
|
|
|
|
bool use_unotify_monitor_ = false;
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sandbox2
|
|
|
|
|
|
|
|
#endif // SANDBOXED_API_SANDBOX2_SANDBOX2_H_
|