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.
|
|
|
|
|
|
|
|
// This class should be used in the client code, in a place where sandboxing
|
|
|
|
// should be engaged.
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_SANDBOX2_CLIENT_H_
|
|
|
|
#define SANDBOXED_API_SANDBOX2_CLIENT_H_
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
2020-11-05 17:48:46 +08:00
|
|
|
#include "absl/container/flat_hash_map.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/comms.h"
|
|
|
|
#include "sandboxed_api/sandbox2/logsink.h"
|
2020-02-20 23:45:22 +08:00
|
|
|
#include "sandboxed_api/sandbox2/network_proxy/client.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
|
|
|
|
class Client {
|
|
|
|
public:
|
|
|
|
// Client is ready to be sandboxed.
|
|
|
|
static constexpr uint32_t kClient2SandboxReady = 0x0A0B0C01;
|
|
|
|
// Sandbox is ready to monitor the sandboxee.
|
|
|
|
static constexpr uint32_t kSandbox2ClientDone = 0x0A0B0C02;
|
|
|
|
|
|
|
|
explicit Client(Comms* comms);
|
|
|
|
|
|
|
|
Client(const Client&) = delete;
|
|
|
|
Client& operator=(const Client&) = delete;
|
|
|
|
|
|
|
|
// Receives a sandbox policy over the comms channel and enables sandboxing.
|
|
|
|
// Using this method allows to have a sandbox-aware sandboxee perform complex
|
|
|
|
// initialization first and then enable sandboxing for actual processing.
|
|
|
|
void SandboxMeHere();
|
|
|
|
|
|
|
|
// Returns the file descriptor that was mapped to the sandboxee using
|
|
|
|
// IPC::ReceiveFd(name).
|
|
|
|
int GetMappedFD(const std::string& name);
|
|
|
|
bool HasMappedFD(const std::string& name);
|
|
|
|
|
|
|
|
// Registers a LogSink that forwards all logs to the supervisor.
|
|
|
|
void SendLogsToSupervisor();
|
|
|
|
|
|
|
|
// Returns the network proxy client and starts it if this function is called
|
|
|
|
// for the first time.
|
|
|
|
NetworkProxyClient* GetNetworkProxyClient();
|
|
|
|
|
2019-07-23 19:40:47 +08:00
|
|
|
// Redirects the connect() syscall to the ConnectHandler() method in
|
|
|
|
// the NetworkProxyClient class.
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status InstallNetworkProxyHandler();
|
2019-07-23 19:40:47 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
protected:
|
|
|
|
// Comms used for synchronization with the monitor, not owned by the object.
|
|
|
|
Comms* comms_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr const char* kFDMapEnvVar = "SB2_FD_MAPPINGS";
|
|
|
|
|
|
|
|
friend class ForkServer;
|
|
|
|
|
|
|
|
// Seccomp-bpf policy received from the monitor.
|
2020-11-05 18:03:20 +08:00
|
|
|
std::vector<uint8_t> policy_;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// LogSink that forwards all log messages to the supervisor.
|
|
|
|
std::unique_ptr<LogSink> logsink_;
|
|
|
|
|
|
|
|
// NetworkProxyClient that forwards network connection requests to the
|
|
|
|
// supervisor.
|
|
|
|
std::unique_ptr<NetworkProxyClient> proxy_client_;
|
|
|
|
|
|
|
|
// In the pre-execve case, the sandboxee has to pass the information about
|
|
|
|
// file descriptors to the new process. We set an environment variable for
|
|
|
|
// this case that is parsed in the Client constructor if present.
|
2020-11-05 17:48:46 +08:00
|
|
|
absl::flat_hash_map<std::string, int> fd_map_;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
std::string GetFdMapEnvVar() const;
|
|
|
|
|
|
|
|
// Sets up communication channels with the sandbox.
|
2022-10-11 00:38:14 +08:00
|
|
|
// preserve_fds contains file descriptors that should be kept open and alive.
|
|
|
|
// The FD numbers might be changed if needed and are updated in the vector.
|
|
|
|
// preserve_fds can be a nullptr, equivallent to an empty vector.
|
|
|
|
void SetUpIPC(std::vector<int>* preserve_fds);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Sets up the current working directory.
|
|
|
|
void SetUpCwd();
|
|
|
|
|
|
|
|
// Receives seccomp-bpf policy from the monitor.
|
|
|
|
void ReceivePolicy();
|
|
|
|
|
|
|
|
// Applies sandbox-bpf policy, have limits applied on us, and become ptrace'd.
|
|
|
|
void ApplyPolicyAndBecomeTracee();
|
|
|
|
|
2022-10-11 00:38:14 +08:00
|
|
|
void PrepareEnvironment(std::vector<int>* preserve_fds = nullptr);
|
2019-03-19 00:21:48 +08:00
|
|
|
void EnableSandbox();
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sandbox2
|
|
|
|
|
|
|
|
#endif // SANDBOXED_API_SANDBOX2_CLIENT_H_
|