2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// The sandbox2::sanitizer namespace provides functions which bring a process
|
|
|
|
// into a state in which it can be safely sandboxed.
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_SANDBOX2_SANITIZER_H_
|
|
|
|
#define SANDBOXED_API_SANDBOX2_SANITIZER_H_
|
|
|
|
|
2021-09-06 16:00:42 +08:00
|
|
|
#include "absl/container/flat_hash_set.h"
|
2021-09-10 18:34:21 +08:00
|
|
|
#include "absl/status/status.h"
|
2021-09-06 16:00:42 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
namespace sanitizer {
|
|
|
|
|
|
|
|
// Reads a list of open file descriptors in the current process.
|
2021-09-06 16:00:42 +08:00
|
|
|
absl::StatusOr<absl::flat_hash_set<int>> GetListOfFDs();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Closes all file descriptors in the current process except the ones in
|
|
|
|
// fd_exceptions.
|
2021-09-10 18:34:21 +08:00
|
|
|
absl::Status CloseAllFDsExcept(const absl::flat_hash_set<int>& fd_exceptions);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Marks all file descriptors as close-on-exec, except the ones in
|
|
|
|
// fd_exceptions.
|
2021-09-10 18:34:21 +08:00
|
|
|
absl::Status MarkAllFDsAsCOEExcept(
|
|
|
|
const absl::flat_hash_set<int>& fd_exceptions);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Returns the number of threads in the process 'pid'. Returns -1 in case of
|
|
|
|
// errors.
|
|
|
|
int GetNumberOfThreads(int pid);
|
|
|
|
|
2021-12-08 09:58:34 +08:00
|
|
|
// When running under a sanitizer, it may spawn a background threads. This is
|
|
|
|
// not desirable for sandboxing purposes. We will notify its background thread
|
|
|
|
// that we wish for it to finish and then wait for it to be done. It is safe to
|
|
|
|
// call this function more than once, since it keeps track of whether it has
|
|
|
|
// already notified the sanitizer. This function does nothing if not running
|
|
|
|
// under a sanitizer.
|
|
|
|
void WaitForSanitizer();
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Sanitizes current process (which will not execve a sandboxed binary).
|
|
|
|
// File-descriptors in fd_exceptions will be either closed
|
|
|
|
// (close_fds == true), or marked as close-on-exec (close_fds == false).
|
2021-09-10 18:34:21 +08:00
|
|
|
absl::Status SanitizeCurrentProcess(
|
|
|
|
const absl::flat_hash_set<int>& fd_exceptions, bool close_fds);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Returns a list of tasks for a pid.
|
2021-09-10 18:34:21 +08:00
|
|
|
absl::StatusOr<absl::flat_hash_set<int>> GetListOfTasks(int pid);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
} // namespace sanitizer
|
|
|
|
} // namespace sandbox2
|
|
|
|
|
|
|
|
#endif // SANDBOXED_API_SANDBOX2_SANITIZER_H_
|