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 // Implementation file for the sandbox2::sanitizer namespace.
16*ec63e07aSXin Li
17*ec63e07aSXin Li #include "sandboxed_api/sandbox2/sanitizer.h"
18*ec63e07aSXin Li
19*ec63e07aSXin Li #include <fcntl.h>
20*ec63e07aSXin Li #include <sys/prctl.h>
21*ec63e07aSXin Li #include <syscall.h>
22*ec63e07aSXin Li #include <unistd.h>
23*ec63e07aSXin Li
24*ec63e07aSXin Li #include <cerrno>
25*ec63e07aSXin Li #include <csignal>
26*ec63e07aSXin Li #include <string>
27*ec63e07aSXin Li #include <vector>
28*ec63e07aSXin Li
29*ec63e07aSXin Li #include "absl/container/flat_hash_set.h"
30*ec63e07aSXin Li #include "absl/status/status.h"
31*ec63e07aSXin Li #include "absl/status/statusor.h"
32*ec63e07aSXin Li #include "absl/strings/numbers.h"
33*ec63e07aSXin Li #include "absl/strings/str_cat.h"
34*ec63e07aSXin Li #include "sandboxed_api/sandbox2/util.h"
35*ec63e07aSXin Li #include "sandboxed_api/util/fileops.h"
36*ec63e07aSXin Li #include "sandboxed_api/util/raw_logging.h"
37*ec63e07aSXin Li #include "sandboxed_api/util/status_macros.h"
38*ec63e07aSXin Li
39*ec63e07aSXin Li #if defined(ABSL_HAVE_ADDRESS_SANITIZER) || \
40*ec63e07aSXin Li defined(ABSL_HAVE_HWADDRESS_SANITIZER) || \
41*ec63e07aSXin Li defined(ABSL_HAVE_LEAK_SANITIZER) || \
42*ec63e07aSXin Li defined(ABSL_HAVE_MEMORY_SANITIZER) || defined(ABSL_HAVE_THREAD_SANITIZER)
43*ec63e07aSXin Li #include <sanitizer/common_interface_defs.h>
44*ec63e07aSXin Li #endif
45*ec63e07aSXin Li
46*ec63e07aSXin Li namespace sandbox2::sanitizer {
47*ec63e07aSXin Li namespace {
48*ec63e07aSXin Li
49*ec63e07aSXin Li namespace file_util = ::sapi::file_util;
50*ec63e07aSXin Li
51*ec63e07aSXin Li constexpr char kProcSelfFd[] = "/proc/self/fd";
52*ec63e07aSXin Li
53*ec63e07aSXin Li // Reads filenames inside the directory and converts them to numerical values.
ListNumericalDirectoryEntries(const std::string & directory)54*ec63e07aSXin Li absl::StatusOr<absl::flat_hash_set<int>> ListNumericalDirectoryEntries(
55*ec63e07aSXin Li const std::string& directory) {
56*ec63e07aSXin Li absl::flat_hash_set<int> result;
57*ec63e07aSXin Li std::vector<std::string> entries;
58*ec63e07aSXin Li std::string error;
59*ec63e07aSXin Li if (!file_util::fileops::ListDirectoryEntries(directory, &entries, &error)) {
60*ec63e07aSXin Li return absl::InternalError(absl::StrCat("List directory entries for '",
61*ec63e07aSXin Li directory, "' failed: ", error));
62*ec63e07aSXin Li }
63*ec63e07aSXin Li result.reserve(entries.size());
64*ec63e07aSXin Li for (const auto& entry : entries) {
65*ec63e07aSXin Li int num;
66*ec63e07aSXin Li if (!absl::SimpleAtoi(entry, &num)) {
67*ec63e07aSXin Li return absl::InternalError(
68*ec63e07aSXin Li absl::StrCat("Cannot convert ", entry, " to a number"));
69*ec63e07aSXin Li }
70*ec63e07aSXin Li result.insert(num);
71*ec63e07aSXin Li }
72*ec63e07aSXin Li return result;
73*ec63e07aSXin Li }
74*ec63e07aSXin Li
75*ec63e07aSXin Li } // namespace
76*ec63e07aSXin Li
GetListOfFDs()77*ec63e07aSXin Li absl::StatusOr<absl::flat_hash_set<int>> GetListOfFDs() {
78*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(absl::flat_hash_set<int> fds,
79*ec63e07aSXin Li ListNumericalDirectoryEntries(kProcSelfFd));
80*ec63e07aSXin Li
81*ec63e07aSXin Li // Exclude the dirfd which was opened in ListDirectoryEntries.
82*ec63e07aSXin Li for (auto it = fds.begin(), end = fds.end(); it != end; ++it) {
83*ec63e07aSXin Li if (access(absl::StrCat(kProcSelfFd, "/", *it).c_str(), F_OK) != 0) {
84*ec63e07aSXin Li fds.erase(it);
85*ec63e07aSXin Li break;
86*ec63e07aSXin Li }
87*ec63e07aSXin Li }
88*ec63e07aSXin Li return fds;
89*ec63e07aSXin Li }
90*ec63e07aSXin Li
GetListOfTasks(int pid)91*ec63e07aSXin Li absl::StatusOr<absl::flat_hash_set<int>> GetListOfTasks(int pid) {
92*ec63e07aSXin Li const std::string task_dir = absl::StrCat("/proc/", pid, "/task");
93*ec63e07aSXin Li return ListNumericalDirectoryEntries(task_dir);
94*ec63e07aSXin Li }
95*ec63e07aSXin Li
CloseAllFDsExcept(const absl::flat_hash_set<int> & fd_exceptions)96*ec63e07aSXin Li absl::Status CloseAllFDsExcept(const absl::flat_hash_set<int>& fd_exceptions) {
97*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(absl::flat_hash_set<int> fds, GetListOfFDs());
98*ec63e07aSXin Li
99*ec63e07aSXin Li for (const auto& fd : fds) {
100*ec63e07aSXin Li if (fd_exceptions.find(fd) != fd_exceptions.end()) {
101*ec63e07aSXin Li continue;
102*ec63e07aSXin Li }
103*ec63e07aSXin Li SAPI_RAW_VLOG(2, "Closing FD:%d", fd);
104*ec63e07aSXin Li close(fd);
105*ec63e07aSXin Li }
106*ec63e07aSXin Li return absl::OkStatus();
107*ec63e07aSXin Li }
108*ec63e07aSXin Li
MarkAllFDsAsCOEExcept(const absl::flat_hash_set<int> & fd_exceptions)109*ec63e07aSXin Li absl::Status MarkAllFDsAsCOEExcept(
110*ec63e07aSXin Li const absl::flat_hash_set<int>& fd_exceptions) {
111*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(absl::flat_hash_set<int> fds, GetListOfFDs());
112*ec63e07aSXin Li
113*ec63e07aSXin Li for (const auto& fd : fds) {
114*ec63e07aSXin Li if (fd_exceptions.find(fd) != fd_exceptions.end()) {
115*ec63e07aSXin Li continue;
116*ec63e07aSXin Li }
117*ec63e07aSXin Li
118*ec63e07aSXin Li SAPI_RAW_VLOG(2, "Marking FD:%d as close-on-exec", fd);
119*ec63e07aSXin Li
120*ec63e07aSXin Li int flags = fcntl(fd, F_GETFD);
121*ec63e07aSXin Li if (flags == -1) {
122*ec63e07aSXin Li return absl::ErrnoToStatus(
123*ec63e07aSXin Li errno, absl::StrCat("fcntl(", fd, ", F_GETFD) failed"));
124*ec63e07aSXin Li }
125*ec63e07aSXin Li if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
126*ec63e07aSXin Li return absl::ErrnoToStatus(
127*ec63e07aSXin Li errno, absl::StrCat("fcntl(", fd, ", F_SETFD, ", flags,
128*ec63e07aSXin Li " | FD_CLOEXEC) failed"));
129*ec63e07aSXin Li }
130*ec63e07aSXin Li }
131*ec63e07aSXin Li
132*ec63e07aSXin Li return absl::OkStatus();
133*ec63e07aSXin Li }
134*ec63e07aSXin Li
GetNumberOfThreads(int pid)135*ec63e07aSXin Li int GetNumberOfThreads(int pid) {
136*ec63e07aSXin Li std::string thread_str = util::GetProcStatusLine(pid, "Threads");
137*ec63e07aSXin Li if (thread_str.empty()) {
138*ec63e07aSXin Li return -1;
139*ec63e07aSXin Li }
140*ec63e07aSXin Li int threads;
141*ec63e07aSXin Li if (!absl::SimpleAtoi(thread_str, &threads)) {
142*ec63e07aSXin Li SAPI_RAW_LOG(ERROR, "Couldn't convert '%s' to a number",
143*ec63e07aSXin Li thread_str.c_str());
144*ec63e07aSXin Li return -1;
145*ec63e07aSXin Li }
146*ec63e07aSXin Li SAPI_RAW_VLOG(1, "Found %d threads in pid: %d", threads, pid);
147*ec63e07aSXin Li return threads;
148*ec63e07aSXin Li }
149*ec63e07aSXin Li
WaitForSanitizer()150*ec63e07aSXin Li void WaitForSanitizer() {
151*ec63e07aSXin Li #if defined(ABSL_HAVE_ADDRESS_SANITIZER) || \
152*ec63e07aSXin Li defined(ABSL_HAVE_HWADDRESS_SANITIZER) || \
153*ec63e07aSXin Li defined(ABSL_HAVE_LEAK_SANITIZER) || \
154*ec63e07aSXin Li defined(ABSL_HAVE_MEMORY_SANITIZER) || defined(ABSL_HAVE_THREAD_SANITIZER)
155*ec63e07aSXin Li static bool ABSL_ATTRIBUTE_UNUSED dummy_once = []() {
156*ec63e07aSXin Li __sanitizer_sandbox_on_notify(nullptr);
157*ec63e07aSXin Li return true;
158*ec63e07aSXin Li }();
159*ec63e07aSXin Li const pid_t pid = getpid();
160*ec63e07aSXin Li int threads;
161*ec63e07aSXin Li for (int retry = 0; retry < 10; ++retry) {
162*ec63e07aSXin Li threads = GetNumberOfThreads(pid);
163*ec63e07aSXin Li if (threads == -1 || threads == 1) {
164*ec63e07aSXin Li break;
165*ec63e07aSXin Li }
166*ec63e07aSXin Li absl::SleepFor(absl::Milliseconds(100));
167*ec63e07aSXin Li }
168*ec63e07aSXin Li #endif
169*ec63e07aSXin Li }
170*ec63e07aSXin Li
SanitizeCurrentProcess(const absl::flat_hash_set<int> & fd_exceptions,bool close_fds)171*ec63e07aSXin Li absl::Status SanitizeCurrentProcess(
172*ec63e07aSXin Li const absl::flat_hash_set<int>& fd_exceptions, bool close_fds) {
173*ec63e07aSXin Li SAPI_RAW_VLOG(1, "Sanitizing PID: %zu, close_fds: %d", syscall(__NR_getpid),
174*ec63e07aSXin Li close_fds);
175*ec63e07aSXin Li
176*ec63e07aSXin Li // Put process in a separate session (and a new process group).
177*ec63e07aSXin Li setsid();
178*ec63e07aSXin Li
179*ec63e07aSXin Li // If the parent goes down, so should we.
180*ec63e07aSXin Li if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) != 0) {
181*ec63e07aSXin Li return absl::ErrnoToStatus(errno,
182*ec63e07aSXin Li "prctl(PR_SET_PDEATHSIG, SIGKILL) failed");
183*ec63e07aSXin Li }
184*ec63e07aSXin Li
185*ec63e07aSXin Li // Close or mark as close-on-exec open file descriptors.
186*ec63e07aSXin Li return close_fds ? CloseAllFDsExcept(fd_exceptions)
187*ec63e07aSXin Li : MarkAllFDsAsCOEExcept(fd_exceptions);
188*ec63e07aSXin Li }
189*ec63e07aSXin Li
190*ec63e07aSXin Li } // namespace sandbox2::sanitizer
191