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::Policy class provides methods for manipulating seccomp-bpf
|
|
|
|
// syscall policies.
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_SANDBOX2_POLICY_H_
|
|
|
|
#define SANDBOXED_API_SANDBOX2_POLICY_H_
|
|
|
|
|
2023-08-25 21:49:42 +08:00
|
|
|
#include <linux/bpf_common.h>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <linux/filter.h> // IWYU pragma: export
|
|
|
|
#include <linux/seccomp.h> // IWYU pragma: export
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <cstdint>
|
2023-08-09 21:43:50 +08:00
|
|
|
#include <optional>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/namespace.h"
|
2020-02-20 23:45:22 +08:00
|
|
|
#include "sandboxed_api/sandbox2/network_proxy/filtering.h"
|
2023-08-25 15:33:34 +08:00
|
|
|
#include "sandboxed_api/sandbox2/syscall.h" // IWYU pragma: export
|
2019-03-20 20:19:28 +08:00
|
|
|
#include "sandboxed_api/sandbox2/violation.pb.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#define SANDBOX2_TRACE \
|
|
|
|
BPF_STMT(BPF_RET + BPF_K, \
|
|
|
|
SECCOMP_RET_TRACE | \
|
|
|
|
(::sandbox2::Syscall::GetHostArch() & SECCOMP_RET_DATA))
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
// Magic values of registers when executing sys_execveat, so we can recognize
|
|
|
|
// the pre-sandboxing state and notify the Monitor
|
2021-07-12 17:37:17 +08:00
|
|
|
inline constexpr uintptr_t kExecveMagic = 0x921c2c34;
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
class Comms;
|
2023-08-09 21:43:50 +08:00
|
|
|
class MonitorBase;
|
2023-08-24 21:23:03 +08:00
|
|
|
class PolicyBuilder;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
class Policy final {
|
|
|
|
public:
|
2023-08-09 21:43:50 +08:00
|
|
|
Policy(const Policy&) = default;
|
|
|
|
Policy& operator=(const Policy&) = default;
|
|
|
|
|
|
|
|
Policy(Policy&&) = delete;
|
|
|
|
Policy& operator=(Policy&&) = delete;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Stores information about the policy (and the policy builder if existing)
|
|
|
|
// in the protobuf structure.
|
|
|
|
void GetPolicyDescription(PolicyDescription* policy) const;
|
|
|
|
|
|
|
|
// Sends the policy over the IPC channel.
|
2023-03-09 00:08:35 +08:00
|
|
|
bool SendPolicy(Comms* comms, bool user_notif) const;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Returns the policy, but modifies it according to FLAGS and internal
|
|
|
|
// requirements (message passing via Comms, Executor::WaitForExecve etc.).
|
2023-03-09 00:08:35 +08:00
|
|
|
std::vector<sock_filter> GetPolicy(bool user_notif) const;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-08-09 21:43:50 +08:00
|
|
|
const std::optional<Namespace>& GetNamespace() const { return namespace_; }
|
|
|
|
const Namespace* GetNamespaceOrNull() const {
|
|
|
|
return namespace_ ? &namespace_.value() : nullptr;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2021-05-20 23:16:45 +08:00
|
|
|
// Returns the default policy, which blocks certain dangerous syscalls and
|
|
|
|
// mismatched syscall tables.
|
2023-03-09 00:08:35 +08:00
|
|
|
std::vector<sock_filter> GetDefaultPolicy(bool user_notif) const;
|
2021-05-20 23:16:45 +08:00
|
|
|
// Returns a policy allowing the Monitor module to track all syscalls.
|
|
|
|
std::vector<sock_filter> GetTrackingPolicy() const;
|
|
|
|
|
2023-08-09 21:43:50 +08:00
|
|
|
bool collect_stacktrace_on_signal() const {
|
|
|
|
return collect_stacktrace_on_signal_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool collect_stacktrace_on_exit() const {
|
|
|
|
return collect_stacktrace_on_exit_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class PolicyBuilder;
|
|
|
|
friend class MonitorBase;
|
|
|
|
|
|
|
|
// Private constructor only called by the PolicyBuilder.
|
|
|
|
Policy() = default;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// The Namespace object, defines ways of putting sandboxee into namespaces.
|
2023-08-09 21:43:50 +08:00
|
|
|
std::optional<Namespace> namespace_;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Gather stack traces on violations, signals, timeouts or when getting
|
|
|
|
// killed. See policybuilder.h for more information.
|
|
|
|
bool collect_stacktrace_on_violation_ = true;
|
|
|
|
bool collect_stacktrace_on_signal_ = true;
|
|
|
|
bool collect_stacktrace_on_timeout_ = true;
|
|
|
|
bool collect_stacktrace_on_kill_ = true;
|
2021-08-16 18:12:39 +08:00
|
|
|
bool collect_stacktrace_on_exit_ = false;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Optional pointer to a PolicyBuilder description pb object.
|
2023-08-09 21:43:50 +08:00
|
|
|
std::optional<PolicyBuilderDescription> policy_builder_description_;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// The policy set by the user.
|
|
|
|
std::vector<sock_filter> user_policy_;
|
2020-12-03 00:37:55 +08:00
|
|
|
bool user_policy_handles_bpf_ = false;
|
2022-05-27 17:57:03 +08:00
|
|
|
bool user_policy_handles_ptrace_ = false;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-02-20 23:45:22 +08:00
|
|
|
// Contains a list of hosts the sandboxee is allowed to connect to.
|
2023-08-09 21:43:50 +08:00
|
|
|
std::optional<AllowedHosts> allowed_hosts_;
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sandbox2
|
|
|
|
|
|
|
|
#endif // SANDBOXED_API_SANDBOX2_POLICY_H_
|