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.
|
|
|
|
|
|
|
|
// Implementation file for the sandbox2::sanitizer namespace.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/sanitizer.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <syscall.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <cerrno>
|
|
|
|
#include <csignal>
|
2019-03-26 22:54:02 +08:00
|
|
|
#include <string>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2021-09-06 16:00:42 +08:00
|
|
|
#include "absl/container/flat_hash_set.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/status/status.h"
|
|
|
|
#include "absl/status/statusor.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/numbers.h"
|
|
|
|
#include "absl/strings/str_cat.h"
|
2021-04-17 03:42:41 +08:00
|
|
|
#include "sandboxed_api/sandbox2/util.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/fileops.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/raw_logging.h"
|
2021-09-06 16:00:42 +08:00
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#if defined(ABSL_HAVE_ADDRESS_SANITIZER) || \
|
|
|
|
defined(ABSL_HAVE_HWADDRESS_SANITIZER) || \
|
|
|
|
defined(ABSL_HAVE_LEAK_SANITIZER) || \
|
|
|
|
defined(ABSL_HAVE_MEMORY_SANITIZER) || defined(ABSL_HAVE_THREAD_SANITIZER)
|
|
|
|
#include <sanitizer/common_interface_defs.h>
|
|
|
|
#endif
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
namespace sandbox2::sanitizer {
|
2019-03-19 00:21:48 +08:00
|
|
|
namespace {
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
namespace file_util = ::sapi::file_util;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
constexpr char kProcSelfFd[] = "/proc/self/fd";
|
|
|
|
|
|
|
|
// Reads filenames inside the directory and converts them to numerical values.
|
2021-09-06 16:00:42 +08:00
|
|
|
absl::StatusOr<absl::flat_hash_set<int>> ListNumericalDirectoryEntries(
|
|
|
|
const std::string& directory) {
|
|
|
|
absl::flat_hash_set<int> result;
|
2019-03-19 00:21:48 +08:00
|
|
|
std::vector<std::string> entries;
|
|
|
|
std::string error;
|
|
|
|
if (!file_util::fileops::ListDirectoryEntries(directory, &entries, &error)) {
|
2021-09-06 16:00:42 +08:00
|
|
|
return absl::InternalError(absl::StrCat("List directory entries for '",
|
|
|
|
directory, "' failed: ", error));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2021-09-06 16:00:42 +08:00
|
|
|
result.reserve(entries.size());
|
2019-03-19 00:21:48 +08:00
|
|
|
for (const auto& entry : entries) {
|
|
|
|
int num;
|
|
|
|
if (!absl::SimpleAtoi(entry, &num)) {
|
2021-09-06 16:00:42 +08:00
|
|
|
return absl::InternalError(
|
|
|
|
absl::StrCat("Cannot convert ", entry, " to a number"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2021-09-06 16:00:42 +08:00
|
|
|
result.insert(num);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2021-09-06 16:00:42 +08:00
|
|
|
return result;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2021-09-06 16:00:42 +08:00
|
|
|
absl::StatusOr<absl::flat_hash_set<int>> GetListOfFDs() {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(absl::flat_hash_set<int> fds,
|
|
|
|
ListNumericalDirectoryEntries(kProcSelfFd));
|
|
|
|
|
|
|
|
// Exclude the dirfd which was opened in ListDirectoryEntries.
|
|
|
|
for (auto it = fds.begin(), end = fds.end(); it != end; ++it) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (access(absl::StrCat(kProcSelfFd, "/", *it).c_str(), F_OK) != 0) {
|
2021-09-06 16:00:42 +08:00
|
|
|
fds.erase(it);
|
2019-03-19 00:21:48 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 16:00:42 +08:00
|
|
|
return fds;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
const std::string task_dir = absl::StrCat("/proc/", pid, "/task");
|
2021-09-10 18:34:21 +08:00
|
|
|
return ListNumericalDirectoryEntries(task_dir);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2021-09-10 18:34:21 +08:00
|
|
|
absl::Status CloseAllFDsExcept(const absl::flat_hash_set<int>& fd_exceptions) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(absl::flat_hash_set<int> fds, GetListOfFDs());
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-09-10 18:34:21 +08:00
|
|
|
for (const auto& fd : fds) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (fd_exceptions.find(fd) != fd_exceptions.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SAPI_RAW_VLOG(2, "Closing FD:%d", fd);
|
|
|
|
close(fd);
|
|
|
|
}
|
2021-09-10 18:34:21 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2021-09-10 18:34:21 +08:00
|
|
|
absl::Status MarkAllFDsAsCOEExcept(
|
|
|
|
const absl::flat_hash_set<int>& fd_exceptions) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(absl::flat_hash_set<int> fds, GetListOfFDs());
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-09-10 18:34:21 +08:00
|
|
|
for (const auto& fd : fds) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (fd_exceptions.find(fd) != fd_exceptions.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SAPI_RAW_VLOG(2, "Marking FD:%d as close-on-exec", fd);
|
|
|
|
|
|
|
|
int flags = fcntl(fd, F_GETFD);
|
|
|
|
if (flags == -1) {
|
2022-04-21 21:15:07 +08:00
|
|
|
return absl::ErrnoToStatus(
|
|
|
|
errno, absl::StrCat("fcntl(", fd, ", F_GETFD) failed"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
|
2022-04-21 21:15:07 +08:00
|
|
|
return absl::ErrnoToStatus(
|
|
|
|
errno, absl::StrCat("fcntl(", fd, ", F_SETFD, ", flags,
|
|
|
|
" | FD_CLOEXEC) failed"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 18:34:21 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int GetNumberOfThreads(int pid) {
|
2021-04-17 03:42:41 +08:00
|
|
|
std::string thread_str = util::GetProcStatusLine(pid, "Threads");
|
2019-03-19 00:21:48 +08:00
|
|
|
if (thread_str.empty()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int threads;
|
|
|
|
if (!absl::SimpleAtoi(thread_str, &threads)) {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_LOG(ERROR, "Couldn't convert '%s' to a number",
|
|
|
|
thread_str.c_str());
|
2019-03-19 00:21:48 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
SAPI_RAW_VLOG(1, "Found %d threads in pid: %d", threads, pid);
|
|
|
|
return threads;
|
|
|
|
}
|
|
|
|
|
2021-12-08 09:58:34 +08:00
|
|
|
void WaitForSanitizer() {
|
|
|
|
#if defined(ABSL_HAVE_ADDRESS_SANITIZER) || \
|
|
|
|
defined(ABSL_HAVE_HWADDRESS_SANITIZER) || \
|
|
|
|
defined(ABSL_HAVE_LEAK_SANITIZER) || \
|
|
|
|
defined(ABSL_HAVE_MEMORY_SANITIZER) || defined(ABSL_HAVE_THREAD_SANITIZER)
|
|
|
|
static bool ABSL_ATTRIBUTE_UNUSED dummy_once = []() {
|
2019-03-19 00:21:48 +08:00
|
|
|
__sanitizer_sandbox_on_notify(nullptr);
|
|
|
|
return true;
|
|
|
|
}();
|
|
|
|
const pid_t pid = getpid();
|
|
|
|
int threads;
|
|
|
|
for (int retry = 0; retry < 10; ++retry) {
|
|
|
|
threads = GetNumberOfThreads(pid);
|
|
|
|
if (threads == -1 || threads == 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
absl::SleepFor(absl::Milliseconds(100));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-09-10 18:34:21 +08:00
|
|
|
absl::Status SanitizeCurrentProcess(
|
|
|
|
const absl::flat_hash_set<int>& fd_exceptions, bool close_fds) {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_VLOG(1, "Sanitizing PID: %zu, close_fds: %d", syscall(__NR_getpid),
|
2019-03-19 00:21:48 +08:00
|
|
|
close_fds);
|
|
|
|
|
|
|
|
// Put process in a separate session (and a new process group).
|
|
|
|
setsid();
|
|
|
|
|
|
|
|
// If the parent goes down, so should we.
|
|
|
|
if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) != 0) {
|
2022-04-21 21:15:07 +08:00
|
|
|
return absl::ErrnoToStatus(errno,
|
|
|
|
"prctl(PR_SET_PDEATHSIG, SIGKILL) failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close or mark as close-on-exec open file descriptors.
|
2021-09-10 18:34:21 +08:00
|
|
|
return close_fds ? CloseAllFDsExcept(fd_exceptions)
|
|
|
|
: MarkAllFDsAsCOEExcept(fd_exceptions);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
} // namespace sandbox2::sanitizer
|