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 // The sandbox2::sanitizer namespace provides functions which bring a process 16*ec63e07aSXin Li // into a state in which it can be safely sandboxed. 17*ec63e07aSXin Li 18*ec63e07aSXin Li #ifndef SANDBOXED_API_SANDBOX2_SANITIZER_H_ 19*ec63e07aSXin Li #define SANDBOXED_API_SANDBOX2_SANITIZER_H_ 20*ec63e07aSXin Li 21*ec63e07aSXin Li #include "absl/container/flat_hash_set.h" 22*ec63e07aSXin Li #include "absl/status/status.h" 23*ec63e07aSXin Li #include "absl/status/statusor.h" 24*ec63e07aSXin Li 25*ec63e07aSXin Li namespace sandbox2 { 26*ec63e07aSXin Li namespace sanitizer { 27*ec63e07aSXin Li 28*ec63e07aSXin Li // Reads a list of open file descriptors in the current process. 29*ec63e07aSXin Li absl::StatusOr<absl::flat_hash_set<int>> GetListOfFDs(); 30*ec63e07aSXin Li 31*ec63e07aSXin Li // Closes all file descriptors in the current process except the ones in 32*ec63e07aSXin Li // fd_exceptions. 33*ec63e07aSXin Li absl::Status CloseAllFDsExcept(const absl::flat_hash_set<int>& fd_exceptions); 34*ec63e07aSXin Li 35*ec63e07aSXin Li // Marks all file descriptors as close-on-exec, except the ones in 36*ec63e07aSXin Li // fd_exceptions. 37*ec63e07aSXin Li absl::Status MarkAllFDsAsCOEExcept( 38*ec63e07aSXin Li const absl::flat_hash_set<int>& fd_exceptions); 39*ec63e07aSXin Li 40*ec63e07aSXin Li // Returns the number of threads in the process 'pid'. Returns -1 in case of 41*ec63e07aSXin Li // errors. 42*ec63e07aSXin Li int GetNumberOfThreads(int pid); 43*ec63e07aSXin Li 44*ec63e07aSXin Li // When running under a sanitizer, it may spawn a background threads. This is 45*ec63e07aSXin Li // not desirable for sandboxing purposes. We will notify its background thread 46*ec63e07aSXin Li // that we wish for it to finish and then wait for it to be done. It is safe to 47*ec63e07aSXin Li // call this function more than once, since it keeps track of whether it has 48*ec63e07aSXin Li // already notified the sanitizer. This function does nothing if not running 49*ec63e07aSXin Li // under a sanitizer. 50*ec63e07aSXin Li void WaitForSanitizer(); 51*ec63e07aSXin Li 52*ec63e07aSXin Li // Sanitizes current process (which will not execve a sandboxed binary). 53*ec63e07aSXin Li // File-descriptors in fd_exceptions will be either closed 54*ec63e07aSXin Li // (close_fds == true), or marked as close-on-exec (close_fds == false). 55*ec63e07aSXin Li absl::Status SanitizeCurrentProcess( 56*ec63e07aSXin Li const absl::flat_hash_set<int>& fd_exceptions, bool close_fds); 57*ec63e07aSXin Li 58*ec63e07aSXin Li // Returns a list of tasks for a pid. 59*ec63e07aSXin Li absl::StatusOr<absl::flat_hash_set<int>> GetListOfTasks(int pid); 60*ec63e07aSXin Li 61*ec63e07aSXin Li } // namespace sanitizer 62*ec63e07aSXin Li } // namespace sandbox2 63*ec63e07aSXin Li 64*ec63e07aSXin Li #endif // SANDBOXED_API_SANDBOX2_SANITIZER_H_ 65