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 #ifndef SANDBOXED_API_SANDBOX2_EXECUTOR_H_ 16 #define SANDBOXED_API_SANDBOX2_EXECUTOR_H_ 17 18 #include <unistd.h> 19 20 #include <memory> 21 #include <string> 22 #include <utility> 23 #include <vector> 24 25 #include "absl/base/macros.h" 26 #include "absl/log/check.h" 27 #include "absl/log/log.h" 28 #include "absl/status/statusor.h" 29 #include "absl/strings/string_view.h" 30 #include "absl/types/span.h" 31 #include "sandboxed_api/sandbox2/fork_client.h" 32 #include "sandboxed_api/sandbox2/forkserver.pb.h" 33 #include "sandboxed_api/sandbox2/ipc.h" 34 #include "sandboxed_api/sandbox2/limits.h" 35 #include "sandboxed_api/sandbox2/namespace.h" 36 #include "sandboxed_api/util/fileops.h" 37 38 namespace sandbox2 { 39 40 // The sandbox2::Executor class is responsible for both creating and executing 41 // new processes which will be sandboxed. 42 class Executor final { 43 public: 44 Executor(const Executor&) = delete; 45 Executor& operator=(const Executor&) = delete; 46 47 // Initialized with a path to the process that the Executor class will 48 // execute 49 Executor( 50 absl::string_view path, absl::Span<const std::string> argv, 51 absl::Span<const std::string> envp = absl::MakeConstSpan(CopyEnviron())) path_(std::string (path))52 : path_(std::string(path)), 53 argv_(argv.begin(), argv.end()), 54 envp_(envp.begin(), envp.end()) { 55 CHECK(!path_.empty()); 56 SetUpServerSideCommsFd(); 57 } 58 59 // Executor will own this file-descriptor, so if intend to use it, pass here 60 // dup(fd) instead 61 Executor( 62 int exec_fd, absl::Span<const std::string> argv, 63 absl::Span<const std::string> envp = absl::MakeConstSpan(CopyEnviron())) exec_fd_(exec_fd)64 : exec_fd_(exec_fd), 65 argv_(argv.begin(), argv.end()), 66 envp_(envp.begin(), envp.end()) { 67 CHECK_GE(exec_fd, 0); 68 SetUpServerSideCommsFd(); 69 } 70 71 // Uses a custom ForkServer (which the supplied ForkClient can communicate 72 // with), which knows how to fork (or even execute) new sandboxed processes 73 // (hence, no need to supply path/argv/envp here) Executor(ForkClient * fork_client)74 explicit Executor(ForkClient* fork_client) 75 : enable_sandboxing_pre_execve_(false), fork_client_(fork_client) { 76 CHECK(fork_client != nullptr); 77 SetUpServerSideCommsFd(); 78 } 79 80 // Creates a new process which will act as a custom ForkServer. Should be used 81 // with custom fork servers only. 82 // This function returns immediately and returns a nullptr on failure. 83 std::unique_ptr<ForkClient> StartForkServer(); 84 85 // Accessors ipc()86 IPC* ipc() { return &ipc_; } 87 limits()88 Limits* limits() { return &limits_; } 89 set_enable_sandbox_before_exec(bool value)90 Executor& set_enable_sandbox_before_exec(bool value) { 91 enable_sandboxing_pre_execve_ = value; 92 return *this; 93 } 94 set_cwd(std::string value)95 Executor& set_cwd(std::string value) { 96 cwd_ = std::move(value); 97 return *this; 98 } 99 libunwind_recursion_depth()100 int libunwind_recursion_depth() { return libunwind_recursion_depth_; } 101 102 private: 103 friend class MonitorBase; 104 friend class PtraceMonitor; 105 friend class StackTracePeer; 106 107 // Internal constructor for executing libunwind on the given pid 108 // enable_sandboxing_pre_execve=false as we are not going to execve. Executor(pid_t libunwind_sbox_for_pid,int libunwind_recursion_depth)109 explicit Executor(pid_t libunwind_sbox_for_pid, int libunwind_recursion_depth) 110 : libunwind_sbox_for_pid_(libunwind_sbox_for_pid), 111 libunwind_recursion_depth_(libunwind_recursion_depth), 112 enable_sandboxing_pre_execve_(false) { 113 CHECK_GE(libunwind_sbox_for_pid_, 0); 114 SetUpServerSideCommsFd(); 115 } 116 117 // Creates a copy of the environment 118 static std::vector<std::string> CopyEnviron(); 119 120 // Creates a server-side Comms end-point using a pre-connected file 121 // descriptor. 122 void SetUpServerSideCommsFd(); 123 124 // Starts a new process which is connected with this Executor instance via a 125 // Comms channel. 126 // For clone_flags refer to Linux' 'man 2 clone'. 127 absl::StatusOr<SandboxeeProcess> StartSubProcess( 128 int clone_flags, const Namespace* ns = nullptr, 129 MonitorType type = FORKSERVER_MONITOR_PTRACE); 130 131 // Whether the Executor has been started yet 132 bool started_ = false; 133 134 // If this executor is running the libunwind sandbox for a process, 135 // this variable will hold the PID of the process. Otherwise it is zero. 136 pid_t libunwind_sbox_for_pid_ = 0; 137 int libunwind_recursion_depth_ = 0; 138 139 // Should the sandboxing be enabled before execve() occurs, or the binary will 140 // do it by itself, using the Client object's methods 141 bool enable_sandboxing_pre_execve_ = true; 142 143 // Alternate (path/fd)/argv/envp to be used the in the __NR_execve call. 144 sapi::file_util::fileops::FDCloser exec_fd_; 145 std::string path_; 146 std::vector<std::string> argv_; 147 std::vector<std::string> envp_; 148 149 // chdir to cwd_, if set. Defaults to current working directory. 150 std::string cwd_ = []() { 151 std::string cwd = sapi::file_util::fileops::GetCWD(); 152 if (cwd.empty()) { 153 PLOG(WARNING) << "Getting current working directory"; 154 } 155 return cwd; 156 }(); 157 158 // Client (sandboxee) end-point of a socket-pair used to create Comms channel 159 sapi::file_util::fileops::FDCloser client_comms_fd_; 160 161 // ForkClient connecting to the ForkServer - not owned by the object 162 ForkClient* fork_client_ = nullptr; 163 164 IPC ipc_; // Used for communication with the sandboxee 165 Limits limits_; // Defines server- and client-side limits 166 }; 167 168 } // namespace sandbox2 169 170 #endif // SANDBOXED_API_SANDBOX2_EXECUTOR_H_ 171