xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/client.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2019 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li // This class should be used in the client code, in a place where sandboxing
16*ec63e07aSXin Li // should be engaged.
17*ec63e07aSXin Li 
18*ec63e07aSXin Li #ifndef SANDBOXED_API_SANDBOX2_CLIENT_H_
19*ec63e07aSXin Li #define SANDBOXED_API_SANDBOX2_CLIENT_H_
20*ec63e07aSXin Li 
21*ec63e07aSXin Li #include <cstdint>
22*ec63e07aSXin Li #include <memory>
23*ec63e07aSXin Li #include <string>
24*ec63e07aSXin Li #include <vector>
25*ec63e07aSXin Li 
26*ec63e07aSXin Li #include "absl/container/flat_hash_map.h"
27*ec63e07aSXin Li #include "absl/status/status.h"
28*ec63e07aSXin Li #include "sandboxed_api/sandbox2/comms.h"
29*ec63e07aSXin Li #include "sandboxed_api/sandbox2/logsink.h"
30*ec63e07aSXin Li #include "sandboxed_api/sandbox2/network_proxy/client.h"
31*ec63e07aSXin Li 
32*ec63e07aSXin Li namespace sandbox2 {
33*ec63e07aSXin Li 
34*ec63e07aSXin Li class Client {
35*ec63e07aSXin Li  public:
36*ec63e07aSXin Li   // Client is ready to be sandboxed.
37*ec63e07aSXin Li   static constexpr uint32_t kClient2SandboxReady = 0x0A0B0C01;
38*ec63e07aSXin Li   // Sandbox is ready to monitor the sandboxee.
39*ec63e07aSXin Li   static constexpr uint32_t kSandbox2ClientDone = 0x0A0B0C02;
40*ec63e07aSXin Li   // Sandboxe should setup seccomp_unotify and send back the FD.
41*ec63e07aSXin Li   static constexpr uint32_t kSandbox2ClientUnotify = 0x0A0B0C03;
42*ec63e07aSXin Li 
43*ec63e07aSXin Li   explicit Client(Comms* comms);
44*ec63e07aSXin Li 
45*ec63e07aSXin Li   Client(const Client&) = delete;
46*ec63e07aSXin Li   Client& operator=(const Client&) = delete;
47*ec63e07aSXin Li 
48*ec63e07aSXin Li   // Receives a sandbox policy over the comms channel and enables sandboxing.
49*ec63e07aSXin Li   // Using this method allows to have a sandbox-aware sandboxee perform complex
50*ec63e07aSXin Li   // initialization first and then enable sandboxing for actual processing.
51*ec63e07aSXin Li   void SandboxMeHere();
52*ec63e07aSXin Li 
53*ec63e07aSXin Li   // Returns the file descriptor that was mapped to the sandboxee using
54*ec63e07aSXin Li   // IPC::ReceiveFd(name).
55*ec63e07aSXin Li   int GetMappedFD(const std::string& name);
56*ec63e07aSXin Li   bool HasMappedFD(const std::string& name);
57*ec63e07aSXin Li 
58*ec63e07aSXin Li   // Registers a LogSink that forwards all logs to the supervisor.
59*ec63e07aSXin Li   void SendLogsToSupervisor();
60*ec63e07aSXin Li 
61*ec63e07aSXin Li   // Returns the network proxy client and starts it if this function is called
62*ec63e07aSXin Li   // for the first time.
63*ec63e07aSXin Li   NetworkProxyClient* GetNetworkProxyClient();
64*ec63e07aSXin Li 
65*ec63e07aSXin Li   // Redirects the connect() syscall to the ConnectHandler() method in
66*ec63e07aSXin Li   // the NetworkProxyClient class.
67*ec63e07aSXin Li   absl::Status InstallNetworkProxyHandler();
68*ec63e07aSXin Li 
69*ec63e07aSXin Li  protected:
70*ec63e07aSXin Li   // Comms used for synchronization with the monitor, not owned by the object.
71*ec63e07aSXin Li   Comms* comms_;
72*ec63e07aSXin Li 
73*ec63e07aSXin Li  private:
74*ec63e07aSXin Li   static constexpr const char* kFDMapEnvVar = "SB2_FD_MAPPINGS";
75*ec63e07aSXin Li 
76*ec63e07aSXin Li   friend class ForkServer;
77*ec63e07aSXin Li 
78*ec63e07aSXin Li   // Seccomp-bpf policy received from the monitor.
79*ec63e07aSXin Li   std::vector<uint8_t> policy_;
80*ec63e07aSXin Li 
81*ec63e07aSXin Li   // LogSink that forwards all log messages to the supervisor.
82*ec63e07aSXin Li   std::unique_ptr<LogSink> logsink_;
83*ec63e07aSXin Li 
84*ec63e07aSXin Li   // NetworkProxyClient that forwards network connection requests to the
85*ec63e07aSXin Li   // supervisor.
86*ec63e07aSXin Li   std::unique_ptr<NetworkProxyClient> proxy_client_;
87*ec63e07aSXin Li 
88*ec63e07aSXin Li   // In the pre-execve case, the sandboxee has to pass the information about
89*ec63e07aSXin Li   // file descriptors to the new process. We set an environment variable for
90*ec63e07aSXin Li   // this case that is parsed in the Client constructor if present.
91*ec63e07aSXin Li   absl::flat_hash_map<std::string, int> fd_map_;
92*ec63e07aSXin Li 
93*ec63e07aSXin Li   std::string GetFdMapEnvVar() const;
94*ec63e07aSXin Li 
95*ec63e07aSXin Li   // Sets up communication channels with the sandbox.
96*ec63e07aSXin Li   // preserved_fd contains file descriptor that should be kept open and alive.
97*ec63e07aSXin Li   // The FD number might be changed if needed.
98*ec63e07aSXin Li   // preserved_fd can be a nullptr.
99*ec63e07aSXin Li   void SetUpIPC(int* preserved_fd);
100*ec63e07aSXin Li 
101*ec63e07aSXin Li   // Sets up the current working directory.
102*ec63e07aSXin Li   void SetUpCwd();
103*ec63e07aSXin Li 
104*ec63e07aSXin Li   // Receives seccomp-bpf policy from the monitor.
105*ec63e07aSXin Li   void ReceivePolicy();
106*ec63e07aSXin Li 
107*ec63e07aSXin Li   // Applies sandbox-bpf policy, have limits applied on us, and become ptrace'd.
108*ec63e07aSXin Li   void ApplyPolicyAndBecomeTracee();
109*ec63e07aSXin Li 
110*ec63e07aSXin Li   void PrepareEnvironment(int* preserved_fd = nullptr);
111*ec63e07aSXin Li   void EnableSandbox();
112*ec63e07aSXin Li };
113*ec63e07aSXin Li 
114*ec63e07aSXin Li }  // namespace sandbox2
115*ec63e07aSXin Li 
116*ec63e07aSXin Li #endif  // SANDBOXED_API_SANDBOX2_CLIENT_H_
117