1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef ANDROID_DEVICE_GOOGLE_CUTTLEFISH_HOST_COMMANDS_PROCESS_SANDBOXER_SANDBOX_MANAGER_H 17 #define ANDROID_DEVICE_GOOGLE_CUTTLEFISH_HOST_COMMANDS_PROCESS_SANDBOXER_SANDBOX_MANAGER_H 18 19 #include <list> 20 #include <memory> 21 #include <optional> 22 #include <string> 23 #include <utility> 24 #include <vector> 25 26 #include <absl/random/random.h> 27 #include <absl/status/status.h> 28 #include <absl/status/statusor.h> 29 #include <absl/types/span.h> 30 #include <sandboxed_api/sandbox2/policy.h> 31 32 #include "host/commands/process_sandboxer/credentialed_unix_server.h" 33 #include "host/commands/process_sandboxer/policies.h" 34 #include "host/commands/process_sandboxer/signal_fd.h" 35 #include "host/commands/process_sandboxer/unique_fd.h" 36 37 namespace cuttlefish::process_sandboxer { 38 39 class SandboxManager { 40 public: 41 static absl::StatusOr<std::unique_ptr<SandboxManager>> Create( 42 HostInfo host_info); 43 44 SandboxManager(SandboxManager&) = delete; 45 ~SandboxManager(); 46 47 /** Start a process with the given `argv` and file descriptors in `fds`. 48 * 49 * For (key, value) pairs in `fds`, `key` on the outside is mapped to `value` 50 * in the sandbox, and `key` is `close`d on the outside. */ 51 absl::Status RunProcess(std::optional<int> client_fd, 52 absl::Span<const std::string> argv, 53 std::vector<std::pair<UniqueFd, int>> fds, 54 absl::Span<const std::string> env); 55 56 /** Block until an event happens, and process all open events. */ 57 absl::Status Iterate(); 58 bool Running() const; 59 60 private: 61 class ManagedProcess { 62 public: 63 virtual ~ManagedProcess() = default; 64 virtual std::optional<int> ClientFd() const = 0; 65 virtual int PollFd() const = 0; 66 virtual absl::StatusOr<uintptr_t> ExitCode() = 0; 67 }; 68 class ProcessNoSandbox; 69 class SandboxedProcess; 70 class SocketClient; 71 72 using ClientIter = std::list<std::unique_ptr<SocketClient>>::iterator; 73 using SboxIter = std::list<std::unique_ptr<ManagedProcess>>::iterator; 74 75 SandboxManager(HostInfo, std::string runtime_dir, SignalFd, 76 CredentialedUnixServer); 77 78 absl::Status RunSandboxedProcess(std::optional<int> client_fd, 79 absl::Span<const std::string> argv, 80 std::vector<std::pair<UniqueFd, int>> fds, 81 absl::Span<const std::string> env, 82 std::unique_ptr<sandbox2::Policy> policy); 83 absl::Status RunProcessNoSandbox(std::optional<int> client_fd, 84 absl::Span<const std::string> argv, 85 std::vector<std::pair<UniqueFd, int>> fds, 86 absl::Span<const std::string> env); 87 88 // Callbacks for the Iterate() `poll` loop. 89 absl::Status ClientMessage(ClientIter it, short revents); 90 absl::Status NewClient(short revents); 91 absl::Status ProcessExit(SboxIter it, short revents); 92 absl::Status Signalled(short revents); 93 94 HostInfo host_info_; 95 bool running_ = true; 96 std::string runtime_dir_; 97 std::list<std::unique_ptr<ManagedProcess>> subprocesses_; 98 std::list<std::unique_ptr<SocketClient>> clients_; 99 SignalFd signals_; 100 CredentialedUnixServer server_; 101 absl::BitGen bit_gen_; 102 }; 103 104 } // namespace cuttlefish::process_sandboxer 105 106 #endif 107