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